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> |
Mathias Agopian | b93a03f8 | 2012-02-17 15:34:57 -0800 | [diff] [blame] | 42 | #include <androidfw/Keyboard.h> |
| 43 | #include <androidfw/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 | |
Jeff Brown | 6069139 | 2011-07-15 19:08:26 -0700 | [diff] [blame] | 123 | static 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 | |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 128 | static void rotateDelta(int32_t orientation, float* deltaX, float* deltaY) { |
| 129 | float temp; |
| 130 | switch (orientation) { |
| 131 | case DISPLAY_ORIENTATION_90: |
| 132 | temp = *deltaX; |
| 133 | *deltaX = *deltaY; |
| 134 | *deltaY = -temp; |
| 135 | break; |
| 136 | |
| 137 | case DISPLAY_ORIENTATION_180: |
| 138 | *deltaX = -*deltaX; |
| 139 | *deltaY = -*deltaY; |
| 140 | break; |
| 141 | |
| 142 | case DISPLAY_ORIENTATION_270: |
| 143 | temp = *deltaX; |
| 144 | *deltaX = -*deltaY; |
| 145 | *deltaY = temp; |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 150 | static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) { |
| 151 | return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0; |
| 152 | } |
| 153 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 154 | // 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] | 155 | // button states. This determines whether the event is reported as a touch event. |
| 156 | static bool isPointerDown(int32_t buttonState) { |
| 157 | return buttonState & |
| 158 | (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY |
Jeff Brown | 53ca3f1 | 2011-06-27 18:36:00 -0700 | [diff] [blame] | 159 | | AMOTION_EVENT_BUTTON_TERTIARY); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 162 | static float calculateCommonVector(float a, float b) { |
| 163 | if (a > 0 && b > 0) { |
| 164 | return a < b ? a : b; |
| 165 | } else if (a < 0 && b < 0) { |
| 166 | return a > b ? a : b; |
| 167 | } else { |
| 168 | return 0; |
| 169 | } |
| 170 | } |
| 171 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 172 | static void synthesizeButtonKey(InputReaderContext* context, int32_t action, |
| 173 | nsecs_t when, int32_t deviceId, uint32_t source, |
| 174 | uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState, |
| 175 | int32_t buttonState, int32_t keyCode) { |
| 176 | if ( |
| 177 | (action == AKEY_EVENT_ACTION_DOWN |
| 178 | && !(lastButtonState & buttonState) |
| 179 | && (currentButtonState & buttonState)) |
| 180 | || (action == AKEY_EVENT_ACTION_UP |
| 181 | && (lastButtonState & buttonState) |
| 182 | && !(currentButtonState & buttonState))) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 183 | NotifyKeyArgs args(when, deviceId, source, policyFlags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 184 | action, 0, keyCode, 0, context->getGlobalMetaState(), when); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 185 | context->getListener()->notifyKey(&args); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | static void synthesizeButtonKeys(InputReaderContext* context, int32_t action, |
| 190 | nsecs_t when, int32_t deviceId, uint32_t source, |
| 191 | uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState) { |
| 192 | synthesizeButtonKey(context, action, when, deviceId, source, policyFlags, |
| 193 | lastButtonState, currentButtonState, |
| 194 | AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK); |
| 195 | synthesizeButtonKey(context, action, when, deviceId, source, policyFlags, |
| 196 | lastButtonState, currentButtonState, |
| 197 | AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD); |
| 198 | } |
| 199 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 200 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 201 | // --- InputReaderConfiguration --- |
| 202 | |
| 203 | bool InputReaderConfiguration::getDisplayInfo(int32_t displayId, bool external, |
| 204 | int32_t* width, int32_t* height, int32_t* orientation) const { |
| 205 | if (displayId == 0) { |
| 206 | const DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay; |
| 207 | if (info.width > 0 && info.height > 0) { |
| 208 | if (width) { |
| 209 | *width = info.width; |
| 210 | } |
| 211 | if (height) { |
| 212 | *height = info.height; |
| 213 | } |
| 214 | if (orientation) { |
| 215 | *orientation = info.orientation; |
| 216 | } |
| 217 | return true; |
| 218 | } |
| 219 | } |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | void InputReaderConfiguration::setDisplayInfo(int32_t displayId, bool external, |
| 224 | int32_t width, int32_t height, int32_t orientation) { |
| 225 | if (displayId == 0) { |
| 226 | DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay; |
| 227 | info.width = width; |
| 228 | info.height = height; |
| 229 | info.orientation = orientation; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 234 | // --- InputReader --- |
| 235 | |
| 236 | InputReader::InputReader(const sp<EventHubInterface>& eventHub, |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 237 | const sp<InputReaderPolicyInterface>& policy, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 238 | const sp<InputListenerInterface>& listener) : |
| 239 | mContext(this), mEventHub(eventHub), mPolicy(policy), |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 240 | mGlobalMetaState(0), mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX), |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 241 | mConfigurationChangesToRefresh(0) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 242 | mQueuedListener = new QueuedInputListener(listener); |
| 243 | |
| 244 | { // acquire lock |
| 245 | AutoMutex _l(mLock); |
| 246 | |
| 247 | refreshConfigurationLocked(0); |
| 248 | updateGlobalMetaStateLocked(); |
| 249 | updateInputConfigurationLocked(); |
| 250 | } // release lock |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | InputReader::~InputReader() { |
| 254 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 255 | delete mDevices.valueAt(i); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | void InputReader::loopOnce() { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 260 | int32_t timeoutMillis; |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 261 | { // acquire lock |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 262 | AutoMutex _l(mLock); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 263 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 264 | uint32_t changes = mConfigurationChangesToRefresh; |
| 265 | if (changes) { |
| 266 | mConfigurationChangesToRefresh = 0; |
| 267 | refreshConfigurationLocked(changes); |
| 268 | } |
| 269 | |
| 270 | timeoutMillis = -1; |
| 271 | if (mNextTimeout != LLONG_MAX) { |
| 272 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 273 | timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout); |
| 274 | } |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 275 | } // release lock |
| 276 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 277 | size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 278 | |
| 279 | { // acquire lock |
| 280 | AutoMutex _l(mLock); |
Jeff Brown | 112b5f5 | 2012-01-27 17:32:06 -0800 | [diff] [blame] | 281 | mReaderIsAliveCondition.broadcast(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 282 | |
| 283 | if (count) { |
| 284 | processEventsLocked(mEventBuffer, count); |
| 285 | } |
| 286 | if (!count || timeoutMillis == 0) { |
| 287 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
Jeff Brown | 112b5f5 | 2012-01-27 17:32:06 -0800 | [diff] [blame] | 288 | if (now >= mNextTimeout) { |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 289 | #if DEBUG_RAW_EVENTS |
Jeff Brown | 112b5f5 | 2012-01-27 17:32:06 -0800 | [diff] [blame] | 290 | ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 291 | #endif |
Jeff Brown | 112b5f5 | 2012-01-27 17:32:06 -0800 | [diff] [blame] | 292 | mNextTimeout = LLONG_MAX; |
| 293 | timeoutExpiredLocked(now); |
| 294 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 295 | } |
| 296 | } // release lock |
| 297 | |
| 298 | // Flush queued events out to the listener. |
| 299 | // This must happen outside of the lock because the listener could potentially call |
| 300 | // back into the InputReader's methods, such as getScanCodeState, or become blocked |
| 301 | // on another thread similarly waiting to acquire the InputReader lock thereby |
| 302 | // resulting in a deadlock. This situation is actually quite plausible because the |
| 303 | // listener is actually the input dispatcher, which calls into the window manager, |
| 304 | // which occasionally calls into the input reader. |
| 305 | mQueuedListener->flush(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 308 | void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) { |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 309 | for (const RawEvent* rawEvent = rawEvents; count;) { |
| 310 | int32_t type = rawEvent->type; |
| 311 | size_t batchSize = 1; |
| 312 | if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) { |
| 313 | int32_t deviceId = rawEvent->deviceId; |
| 314 | while (batchSize < count) { |
| 315 | if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT |
| 316 | || rawEvent[batchSize].deviceId != deviceId) { |
| 317 | break; |
| 318 | } |
| 319 | batchSize += 1; |
| 320 | } |
| 321 | #if DEBUG_RAW_EVENTS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 322 | ALOGD("BatchSize: %d Count: %d", batchSize, count); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 323 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 324 | processEventsForDeviceLocked(deviceId, rawEvent, batchSize); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 325 | } else { |
| 326 | switch (rawEvent->type) { |
| 327 | case EventHubInterface::DEVICE_ADDED: |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 328 | addDeviceLocked(rawEvent->when, rawEvent->deviceId); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 329 | break; |
| 330 | case EventHubInterface::DEVICE_REMOVED: |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 331 | removeDeviceLocked(rawEvent->when, rawEvent->deviceId); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 332 | break; |
| 333 | case EventHubInterface::FINISHED_DEVICE_SCAN: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 334 | handleConfigurationChangedLocked(rawEvent->when); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 335 | break; |
| 336 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 337 | ALOG_ASSERT(false); // can't happen |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 338 | break; |
| 339 | } |
| 340 | } |
| 341 | count -= batchSize; |
| 342 | rawEvent += batchSize; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 346 | void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) { |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 347 | InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(deviceId); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 348 | uint32_t classes = mEventHub->getDeviceClasses(deviceId); |
| 349 | |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 350 | InputDevice* device = createDeviceLocked(deviceId, identifier, classes); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 351 | device->configure(when, &mConfig, 0); |
| 352 | device->reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 353 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 354 | if (device->isIgnored()) { |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 355 | ALOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, |
| 356 | identifier.name.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 357 | } else { |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 358 | ALOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, |
| 359 | identifier.name.string(), device->getSources()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 362 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 363 | if (deviceIndex < 0) { |
| 364 | mDevices.add(deviceId, device); |
| 365 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 366 | ALOGW("Ignoring spurious device added event for deviceId %d.", deviceId); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 367 | delete device; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 368 | return; |
| 369 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 372 | void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 373 | InputDevice* device = NULL; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 374 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 375 | if (deviceIndex >= 0) { |
| 376 | device = mDevices.valueAt(deviceIndex); |
| 377 | mDevices.removeItemsAt(deviceIndex, 1); |
| 378 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 379 | ALOGW("Ignoring spurious device removed event for deviceId %d.", deviceId); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 380 | return; |
| 381 | } |
| 382 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 383 | if (device->isIgnored()) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 384 | ALOGI("Device removed: id=%d, name='%s' (ignored non-input device)", |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 385 | device->getId(), device->getName().string()); |
| 386 | } else { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 387 | ALOGI("Device removed: id=%d, name='%s', sources=0x%08x", |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 388 | device->getId(), device->getName().string(), device->getSources()); |
| 389 | } |
| 390 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 391 | device->reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 392 | delete device; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 395 | InputDevice* InputReader::createDeviceLocked(int32_t deviceId, |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 396 | const InputDeviceIdentifier& identifier, uint32_t classes) { |
| 397 | InputDevice* device = new InputDevice(&mContext, deviceId, identifier, classes); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 398 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 399 | // External devices. |
| 400 | if (classes & INPUT_DEVICE_CLASS_EXTERNAL) { |
| 401 | device->setExternal(true); |
| 402 | } |
| 403 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 404 | // Switch-like devices. |
| 405 | if (classes & INPUT_DEVICE_CLASS_SWITCH) { |
| 406 | device->addMapper(new SwitchInputMapper(device)); |
| 407 | } |
| 408 | |
| 409 | // Keyboard-like devices. |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 410 | uint32_t keyboardSource = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 411 | int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC; |
| 412 | if (classes & INPUT_DEVICE_CLASS_KEYBOARD) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 413 | keyboardSource |= AINPUT_SOURCE_KEYBOARD; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 414 | } |
| 415 | if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) { |
| 416 | keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC; |
| 417 | } |
| 418 | if (classes & INPUT_DEVICE_CLASS_DPAD) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 419 | keyboardSource |= AINPUT_SOURCE_DPAD; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 420 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 421 | if (classes & INPUT_DEVICE_CLASS_GAMEPAD) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 422 | keyboardSource |= AINPUT_SOURCE_GAMEPAD; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 423 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 424 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 425 | if (keyboardSource != 0) { |
| 426 | device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 429 | // Cursor-like devices. |
| 430 | if (classes & INPUT_DEVICE_CLASS_CURSOR) { |
| 431 | device->addMapper(new CursorInputMapper(device)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 434 | // Touchscreens and touchpad devices. |
| 435 | if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 436 | device->addMapper(new MultiTouchInputMapper(device)); |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 437 | } else if (classes & INPUT_DEVICE_CLASS_TOUCH) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 438 | device->addMapper(new SingleTouchInputMapper(device)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 441 | // Joystick-like devices. |
| 442 | if (classes & INPUT_DEVICE_CLASS_JOYSTICK) { |
| 443 | device->addMapper(new JoystickInputMapper(device)); |
| 444 | } |
| 445 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 446 | return device; |
| 447 | } |
| 448 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 449 | void InputReader::processEventsForDeviceLocked(int32_t deviceId, |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 450 | const RawEvent* rawEvents, size_t count) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 451 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 452 | if (deviceIndex < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 453 | ALOGW("Discarding event for unknown deviceId %d.", deviceId); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 454 | return; |
| 455 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 456 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 457 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 458 | if (device->isIgnored()) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 459 | //ALOGD("Discarding event for ignored deviceId %d.", deviceId); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 460 | return; |
| 461 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 462 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 463 | device->process(rawEvents, count); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 466 | void InputReader::timeoutExpiredLocked(nsecs_t when) { |
| 467 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 468 | InputDevice* device = mDevices.valueAt(i); |
| 469 | if (!device->isIgnored()) { |
| 470 | device->timeoutExpired(when); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 471 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 472 | } |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 475 | void InputReader::handleConfigurationChangedLocked(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 476 | // Reset global meta state because it depends on the list of all configured devices. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 477 | updateGlobalMetaStateLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 478 | |
| 479 | // Update input configuration. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 480 | updateInputConfigurationLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 481 | |
| 482 | // Enqueue configuration changed. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 483 | NotifyConfigurationChangedArgs args(when); |
| 484 | mQueuedListener->notifyConfigurationChanged(&args); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 487 | void InputReader::refreshConfigurationLocked(uint32_t changes) { |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 488 | mPolicy->getReaderConfiguration(&mConfig); |
| 489 | mEventHub->setExcludedDevices(mConfig.excludedDeviceNames); |
| 490 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 491 | if (changes) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 492 | ALOGI("Reconfiguring input devices. changes=0x%08x", changes); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 493 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 494 | |
| 495 | if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) { |
| 496 | mEventHub->requestReopenDevices(); |
| 497 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 498 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 499 | InputDevice* device = mDevices.valueAt(i); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 500 | device->configure(now, &mConfig, changes); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 501 | } |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 502 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 506 | void InputReader::updateGlobalMetaStateLocked() { |
| 507 | mGlobalMetaState = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 508 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 509 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 510 | InputDevice* device = mDevices.valueAt(i); |
| 511 | mGlobalMetaState |= device->getMetaState(); |
| 512 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 515 | int32_t InputReader::getGlobalMetaStateLocked() { |
| 516 | return mGlobalMetaState; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 519 | void InputReader::updateInputConfigurationLocked() { |
| 520 | int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH; |
| 521 | int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS; |
| 522 | int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV; |
| 523 | InputDeviceInfo deviceInfo; |
| 524 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 525 | InputDevice* device = mDevices.valueAt(i); |
| 526 | device->getDeviceInfo(& deviceInfo); |
| 527 | uint32_t sources = deviceInfo.getSources(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 528 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 529 | if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) { |
| 530 | touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER; |
| 531 | } |
| 532 | if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) { |
| 533 | navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL; |
| 534 | } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) { |
| 535 | navigationConfig = InputConfiguration::NAVIGATION_DPAD; |
| 536 | } |
| 537 | if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) { |
| 538 | keyboardConfig = InputConfiguration::KEYBOARD_QWERTY; |
| 539 | } |
| 540 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 541 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 542 | mInputConfiguration.touchScreen = touchScreenConfig; |
| 543 | mInputConfiguration.keyboard = keyboardConfig; |
| 544 | mInputConfiguration.navigation = navigationConfig; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 547 | void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) { |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 548 | mDisableVirtualKeysTimeout = time; |
| 549 | } |
| 550 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 551 | bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 552 | InputDevice* device, int32_t keyCode, int32_t scanCode) { |
| 553 | if (now < mDisableVirtualKeysTimeout) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 554 | ALOGI("Dropping virtual key from device %s because virtual keys are " |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 555 | "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d", |
| 556 | device->getName().string(), |
| 557 | (mDisableVirtualKeysTimeout - now) * 0.000001, |
| 558 | keyCode, scanCode); |
| 559 | return true; |
| 560 | } else { |
| 561 | return false; |
| 562 | } |
| 563 | } |
| 564 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 565 | void InputReader::fadePointerLocked() { |
| 566 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 567 | InputDevice* device = mDevices.valueAt(i); |
| 568 | device->fadePointer(); |
| 569 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 570 | } |
| 571 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 572 | void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) { |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 573 | if (when < mNextTimeout) { |
| 574 | mNextTimeout = when; |
| 575 | } |
| 576 | } |
| 577 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 578 | void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 579 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 580 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 581 | *outConfiguration = mInputConfiguration; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 585 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 586 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 587 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 588 | if (deviceIndex < 0) { |
| 589 | return NAME_NOT_FOUND; |
| 590 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 591 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 592 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 593 | if (device->isIgnored()) { |
| 594 | return NAME_NOT_FOUND; |
| 595 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 596 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 597 | device->getDeviceInfo(outDeviceInfo); |
| 598 | return OK; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 602 | AutoMutex _l(mLock); |
| 603 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 604 | outDeviceIds.clear(); |
| 605 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 606 | size_t numDevices = mDevices.size(); |
| 607 | for (size_t i = 0; i < numDevices; i++) { |
| 608 | InputDevice* device = mDevices.valueAt(i); |
| 609 | if (!device->isIgnored()) { |
| 610 | outDeviceIds.add(device->getId()); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 611 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 612 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, |
| 616 | int32_t keyCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 617 | AutoMutex _l(mLock); |
| 618 | |
| 619 | return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, |
| 623 | int32_t scanCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 624 | AutoMutex _l(mLock); |
| 625 | |
| 626 | return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 630 | AutoMutex _l(mLock); |
| 631 | |
| 632 | return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 635 | int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code, |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 636 | GetStateFunc getStateFunc) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 637 | int32_t result = AKEY_STATE_UNKNOWN; |
| 638 | if (deviceId >= 0) { |
| 639 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 640 | if (deviceIndex >= 0) { |
| 641 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 642 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 643 | result = (device->*getStateFunc)(sourceMask, code); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 644 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 645 | } |
| 646 | } else { |
| 647 | size_t numDevices = mDevices.size(); |
| 648 | for (size_t i = 0; i < numDevices; i++) { |
| 649 | InputDevice* device = mDevices.valueAt(i); |
| 650 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
David Deephanphongs | fbca596 | 2011-11-14 14:50:45 -0800 | [diff] [blame] | 651 | // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that |
| 652 | // value. Otherwise, return AKEY_STATE_UP as long as one device reports it. |
| 653 | int32_t currentResult = (device->*getStateFunc)(sourceMask, code); |
| 654 | if (currentResult >= AKEY_STATE_DOWN) { |
| 655 | return currentResult; |
| 656 | } else if (currentResult == AKEY_STATE_UP) { |
| 657 | result = currentResult; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 661 | } |
| 662 | return result; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, |
| 666 | size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 667 | AutoMutex _l(mLock); |
| 668 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 669 | memset(outFlags, 0, numCodes); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 670 | return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 673 | bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, |
| 674 | size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) { |
| 675 | bool result = false; |
| 676 | if (deviceId >= 0) { |
| 677 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 678 | if (deviceIndex >= 0) { |
| 679 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 680 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 681 | result = device->markSupportedKeyCodes(sourceMask, |
| 682 | numCodes, keyCodes, outFlags); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 683 | } |
| 684 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 685 | } else { |
| 686 | size_t numDevices = mDevices.size(); |
| 687 | for (size_t i = 0; i < numDevices; i++) { |
| 688 | InputDevice* device = mDevices.valueAt(i); |
| 689 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 690 | result |= device->markSupportedKeyCodes(sourceMask, |
| 691 | numCodes, keyCodes, outFlags); |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | return result; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 698 | void InputReader::requestRefreshConfiguration(uint32_t changes) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 699 | AutoMutex _l(mLock); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 700 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 701 | if (changes) { |
| 702 | bool needWake = !mConfigurationChangesToRefresh; |
| 703 | mConfigurationChangesToRefresh |= changes; |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 704 | |
| 705 | if (needWake) { |
| 706 | mEventHub->wake(); |
| 707 | } |
| 708 | } |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 711 | void InputReader::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 712 | AutoMutex _l(mLock); |
| 713 | |
Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 714 | mEventHub->dump(dump); |
| 715 | dump.append("\n"); |
| 716 | |
| 717 | dump.append("Input Reader State:\n"); |
| 718 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 719 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 720 | mDevices.valueAt(i)->dump(dump); |
| 721 | } |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 722 | |
| 723 | dump.append(INDENT "Configuration:\n"); |
| 724 | dump.append(INDENT2 "ExcludedDeviceNames: ["); |
| 725 | for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) { |
| 726 | if (i != 0) { |
| 727 | dump.append(", "); |
| 728 | } |
| 729 | dump.append(mConfig.excludedDeviceNames.itemAt(i).string()); |
| 730 | } |
| 731 | dump.append("]\n"); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 732 | dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n", |
| 733 | mConfig.virtualKeyQuietTime * 0.000001f); |
| 734 | |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 735 | dump.appendFormat(INDENT2 "PointerVelocityControlParameters: " |
| 736 | "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n", |
| 737 | mConfig.pointerVelocityControlParameters.scale, |
| 738 | mConfig.pointerVelocityControlParameters.lowThreshold, |
| 739 | mConfig.pointerVelocityControlParameters.highThreshold, |
| 740 | mConfig.pointerVelocityControlParameters.acceleration); |
| 741 | |
| 742 | dump.appendFormat(INDENT2 "WheelVelocityControlParameters: " |
| 743 | "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n", |
| 744 | mConfig.wheelVelocityControlParameters.scale, |
| 745 | mConfig.wheelVelocityControlParameters.lowThreshold, |
| 746 | mConfig.wheelVelocityControlParameters.highThreshold, |
| 747 | mConfig.wheelVelocityControlParameters.acceleration); |
| 748 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 749 | dump.appendFormat(INDENT2 "PointerGesture:\n"); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 750 | dump.appendFormat(INDENT3 "Enabled: %s\n", |
| 751 | toString(mConfig.pointerGesturesEnabled)); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 752 | dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n", |
| 753 | mConfig.pointerGestureQuietInterval * 0.000001f); |
| 754 | dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n", |
| 755 | mConfig.pointerGestureDragMinSwitchSpeed); |
| 756 | dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n", |
| 757 | mConfig.pointerGestureTapInterval * 0.000001f); |
| 758 | dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n", |
| 759 | mConfig.pointerGestureTapDragInterval * 0.000001f); |
| 760 | dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n", |
| 761 | mConfig.pointerGestureTapSlop); |
| 762 | dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n", |
| 763 | mConfig.pointerGestureMultitouchSettleInterval * 0.000001f); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 764 | dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n", |
| 765 | mConfig.pointerGestureMultitouchMinDistance); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 766 | dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n", |
| 767 | mConfig.pointerGestureSwipeTransitionAngleCosine); |
| 768 | dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n", |
| 769 | mConfig.pointerGestureSwipeMaxWidthRatio); |
| 770 | dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n", |
| 771 | mConfig.pointerGestureMovementSpeedRatio); |
| 772 | dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n", |
| 773 | mConfig.pointerGestureZoomSpeedRatio); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Jeff Brown | 89ef072 | 2011-08-10 16:25:21 -0700 | [diff] [blame] | 776 | void InputReader::monitor() { |
| 777 | // Acquire and release the lock to ensure that the reader has not deadlocked. |
| 778 | mLock.lock(); |
Jeff Brown | 112b5f5 | 2012-01-27 17:32:06 -0800 | [diff] [blame] | 779 | mEventHub->wake(); |
| 780 | mReaderIsAliveCondition.wait(mLock); |
Jeff Brown | 89ef072 | 2011-08-10 16:25:21 -0700 | [diff] [blame] | 781 | mLock.unlock(); |
| 782 | |
| 783 | // Check the EventHub |
| 784 | mEventHub->monitor(); |
| 785 | } |
| 786 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 787 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 788 | // --- InputReader::ContextImpl --- |
| 789 | |
| 790 | InputReader::ContextImpl::ContextImpl(InputReader* reader) : |
| 791 | mReader(reader) { |
| 792 | } |
| 793 | |
| 794 | void InputReader::ContextImpl::updateGlobalMetaState() { |
| 795 | // lock is already held by the input loop |
| 796 | mReader->updateGlobalMetaStateLocked(); |
| 797 | } |
| 798 | |
| 799 | int32_t InputReader::ContextImpl::getGlobalMetaState() { |
| 800 | // lock is already held by the input loop |
| 801 | return mReader->getGlobalMetaStateLocked(); |
| 802 | } |
| 803 | |
| 804 | void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) { |
| 805 | // lock is already held by the input loop |
| 806 | mReader->disableVirtualKeysUntilLocked(time); |
| 807 | } |
| 808 | |
| 809 | bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, |
| 810 | InputDevice* device, int32_t keyCode, int32_t scanCode) { |
| 811 | // lock is already held by the input loop |
| 812 | return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode); |
| 813 | } |
| 814 | |
| 815 | void InputReader::ContextImpl::fadePointer() { |
| 816 | // lock is already held by the input loop |
| 817 | mReader->fadePointerLocked(); |
| 818 | } |
| 819 | |
| 820 | void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) { |
| 821 | // lock is already held by the input loop |
| 822 | mReader->requestTimeoutAtTimeLocked(when); |
| 823 | } |
| 824 | |
| 825 | InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() { |
| 826 | return mReader->mPolicy.get(); |
| 827 | } |
| 828 | |
| 829 | InputListenerInterface* InputReader::ContextImpl::getListener() { |
| 830 | return mReader->mQueuedListener.get(); |
| 831 | } |
| 832 | |
| 833 | EventHubInterface* InputReader::ContextImpl::getEventHub() { |
| 834 | return mReader->mEventHub.get(); |
| 835 | } |
| 836 | |
| 837 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 838 | // --- InputReaderThread --- |
| 839 | |
| 840 | InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) : |
| 841 | Thread(/*canCallJava*/ true), mReader(reader) { |
| 842 | } |
| 843 | |
| 844 | InputReaderThread::~InputReaderThread() { |
| 845 | } |
| 846 | |
| 847 | bool InputReaderThread::threadLoop() { |
| 848 | mReader->loopOnce(); |
| 849 | return true; |
| 850 | } |
| 851 | |
| 852 | |
| 853 | // --- InputDevice --- |
| 854 | |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 855 | InputDevice::InputDevice(InputReaderContext* context, int32_t id, |
| 856 | const InputDeviceIdentifier& identifier, uint32_t classes) : |
| 857 | mContext(context), mId(id), mIdentifier(identifier), mClasses(classes), |
Jeff Brown | 9ee285af | 2011-08-31 12:56:34 -0700 | [diff] [blame] | 858 | mSources(0), mIsExternal(false), mDropUntilNextSync(false) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | InputDevice::~InputDevice() { |
| 862 | size_t numMappers = mMappers.size(); |
| 863 | for (size_t i = 0; i < numMappers; i++) { |
| 864 | delete mMappers[i]; |
| 865 | } |
| 866 | mMappers.clear(); |
| 867 | } |
| 868 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 869 | void InputDevice::dump(String8& dump) { |
| 870 | InputDeviceInfo deviceInfo; |
| 871 | getDeviceInfo(& deviceInfo); |
| 872 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 873 | dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(), |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 874 | deviceInfo.getName().string()); |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 875 | dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal)); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 876 | dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources()); |
| 877 | dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType()); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 878 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 879 | const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges(); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 880 | if (!ranges.isEmpty()) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 881 | dump.append(INDENT2 "Motion Ranges:\n"); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 882 | for (size_t i = 0; i < ranges.size(); i++) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 883 | const InputDeviceInfo::MotionRange& range = ranges.itemAt(i); |
| 884 | const char* label = getAxisLabel(range.axis); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 885 | char name[32]; |
| 886 | if (label) { |
| 887 | strncpy(name, label, sizeof(name)); |
| 888 | name[sizeof(name) - 1] = '\0'; |
| 889 | } else { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 890 | snprintf(name, sizeof(name), "%d", range.axis); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 891 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 892 | dump.appendFormat(INDENT3 "%s: source=0x%08x, " |
| 893 | "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n", |
| 894 | name, range.source, range.min, range.max, range.flat, range.fuzz); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 895 | } |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | size_t numMappers = mMappers.size(); |
| 899 | for (size_t i = 0; i < numMappers; i++) { |
| 900 | InputMapper* mapper = mMappers[i]; |
| 901 | mapper->dump(dump); |
| 902 | } |
| 903 | } |
| 904 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 905 | void InputDevice::addMapper(InputMapper* mapper) { |
| 906 | mMappers.add(mapper); |
| 907 | } |
| 908 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 909 | void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 910 | mSources = 0; |
| 911 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 912 | if (!isIgnored()) { |
| 913 | if (!changes) { // first time only |
| 914 | mContext->getEventHub()->getConfiguration(mId, &mConfiguration); |
| 915 | } |
| 916 | |
| 917 | size_t numMappers = mMappers.size(); |
| 918 | for (size_t i = 0; i < numMappers; i++) { |
| 919 | InputMapper* mapper = mMappers[i]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 920 | mapper->configure(when, config, changes); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 921 | mSources |= mapper->getSources(); |
| 922 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 923 | } |
| 924 | } |
| 925 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 926 | void InputDevice::reset(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 927 | size_t numMappers = mMappers.size(); |
| 928 | for (size_t i = 0; i < numMappers; i++) { |
| 929 | InputMapper* mapper = mMappers[i]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 930 | mapper->reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 931 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 932 | |
| 933 | mContext->updateGlobalMetaState(); |
| 934 | |
| 935 | notifyReset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 936 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 937 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 938 | void InputDevice::process(const RawEvent* rawEvents, size_t count) { |
| 939 | // Process all of the events in order for each mapper. |
| 940 | // We cannot simply ask each mapper to process them in bulk because mappers may |
| 941 | // have side-effects that must be interleaved. For example, joystick movement events and |
| 942 | // gamepad button presses are handled by different mappers but they should be dispatched |
| 943 | // in the order received. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 944 | size_t numMappers = mMappers.size(); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 945 | for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) { |
| 946 | #if DEBUG_RAW_EVENTS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 947 | ALOGD("Input event: device=%d type=0x%04x scancode=0x%04x " |
Jeff Brown | 2e45fb6 | 2011-06-29 21:19:05 -0700 | [diff] [blame] | 948 | "keycode=0x%04x value=0x%08x flags=0x%08x", |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 949 | rawEvent->deviceId, rawEvent->type, rawEvent->scanCode, rawEvent->keyCode, |
| 950 | rawEvent->value, rawEvent->flags); |
| 951 | #endif |
| 952 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 953 | if (mDropUntilNextSync) { |
| 954 | if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) { |
| 955 | mDropUntilNextSync = false; |
| 956 | #if DEBUG_RAW_EVENTS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 957 | ALOGD("Recovered from input event buffer overrun."); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 958 | #endif |
| 959 | } else { |
| 960 | #if DEBUG_RAW_EVENTS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 961 | ALOGD("Dropped input event while waiting for next input sync."); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 962 | #endif |
| 963 | } |
| 964 | } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_DROPPED) { |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 965 | ALOGI("Detected input event buffer overrun for device %s.", getName().string()); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 966 | mDropUntilNextSync = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 967 | reset(rawEvent->when); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 968 | } else { |
| 969 | for (size_t i = 0; i < numMappers; i++) { |
| 970 | InputMapper* mapper = mMappers[i]; |
| 971 | mapper->process(rawEvent); |
| 972 | } |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 973 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 974 | } |
| 975 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 976 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 977 | void InputDevice::timeoutExpired(nsecs_t when) { |
| 978 | size_t numMappers = mMappers.size(); |
| 979 | for (size_t i = 0; i < numMappers; i++) { |
| 980 | InputMapper* mapper = mMappers[i]; |
| 981 | mapper->timeoutExpired(when); |
| 982 | } |
| 983 | } |
| 984 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 985 | void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) { |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 986 | outDeviceInfo->initialize(mId, mIdentifier.name, mIdentifier.descriptor); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 987 | |
| 988 | size_t numMappers = mMappers.size(); |
| 989 | for (size_t i = 0; i < numMappers; i++) { |
| 990 | InputMapper* mapper = mMappers[i]; |
| 991 | mapper->populateDeviceInfo(outDeviceInfo); |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 996 | return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState); |
| 997 | } |
| 998 | |
| 999 | int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 1000 | return getState(sourceMask, scanCode, & InputMapper::getScanCodeState); |
| 1001 | } |
| 1002 | |
| 1003 | int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 1004 | return getState(sourceMask, switchCode, & InputMapper::getSwitchState); |
| 1005 | } |
| 1006 | |
| 1007 | int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) { |
| 1008 | int32_t result = AKEY_STATE_UNKNOWN; |
| 1009 | size_t numMappers = mMappers.size(); |
| 1010 | for (size_t i = 0; i < numMappers; i++) { |
| 1011 | InputMapper* mapper = mMappers[i]; |
| 1012 | if (sourcesMatchMask(mapper->getSources(), sourceMask)) { |
David Deephanphongs | fbca596 | 2011-11-14 14:50:45 -0800 | [diff] [blame] | 1013 | // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that |
| 1014 | // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it. |
| 1015 | int32_t currentResult = (mapper->*getStateFunc)(sourceMask, code); |
| 1016 | if (currentResult >= AKEY_STATE_DOWN) { |
| 1017 | return currentResult; |
| 1018 | } else if (currentResult == AKEY_STATE_UP) { |
| 1019 | result = currentResult; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1020 | } |
| 1021 | } |
| 1022 | } |
| 1023 | return result; |
| 1024 | } |
| 1025 | |
| 1026 | bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 1027 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 1028 | bool result = false; |
| 1029 | size_t numMappers = mMappers.size(); |
| 1030 | for (size_t i = 0; i < numMappers; i++) { |
| 1031 | InputMapper* mapper = mMappers[i]; |
| 1032 | if (sourcesMatchMask(mapper->getSources(), sourceMask)) { |
| 1033 | result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags); |
| 1034 | } |
| 1035 | } |
| 1036 | return result; |
| 1037 | } |
| 1038 | |
| 1039 | int32_t InputDevice::getMetaState() { |
| 1040 | int32_t result = 0; |
| 1041 | size_t numMappers = mMappers.size(); |
| 1042 | for (size_t i = 0; i < numMappers; i++) { |
| 1043 | InputMapper* mapper = mMappers[i]; |
| 1044 | result |= mapper->getMetaState(); |
| 1045 | } |
| 1046 | return result; |
| 1047 | } |
| 1048 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1049 | void InputDevice::fadePointer() { |
| 1050 | size_t numMappers = mMappers.size(); |
| 1051 | for (size_t i = 0; i < numMappers; i++) { |
| 1052 | InputMapper* mapper = mMappers[i]; |
| 1053 | mapper->fadePointer(); |
| 1054 | } |
| 1055 | } |
| 1056 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1057 | void InputDevice::notifyReset(nsecs_t when) { |
| 1058 | NotifyDeviceResetArgs args(when, mId); |
| 1059 | mContext->getListener()->notifyDeviceReset(&args); |
| 1060 | } |
| 1061 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1062 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1063 | // --- CursorButtonAccumulator --- |
| 1064 | |
| 1065 | CursorButtonAccumulator::CursorButtonAccumulator() { |
| 1066 | clearButtons(); |
| 1067 | } |
| 1068 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1069 | void CursorButtonAccumulator::reset(InputDevice* device) { |
| 1070 | mBtnLeft = device->isKeyPressed(BTN_LEFT); |
| 1071 | mBtnRight = device->isKeyPressed(BTN_RIGHT); |
| 1072 | mBtnMiddle = device->isKeyPressed(BTN_MIDDLE); |
| 1073 | mBtnBack = device->isKeyPressed(BTN_BACK); |
| 1074 | mBtnSide = device->isKeyPressed(BTN_SIDE); |
| 1075 | mBtnForward = device->isKeyPressed(BTN_FORWARD); |
| 1076 | mBtnExtra = device->isKeyPressed(BTN_EXTRA); |
| 1077 | mBtnTask = device->isKeyPressed(BTN_TASK); |
| 1078 | } |
| 1079 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1080 | void CursorButtonAccumulator::clearButtons() { |
| 1081 | mBtnLeft = 0; |
| 1082 | mBtnRight = 0; |
| 1083 | mBtnMiddle = 0; |
| 1084 | mBtnBack = 0; |
| 1085 | mBtnSide = 0; |
| 1086 | mBtnForward = 0; |
| 1087 | mBtnExtra = 0; |
| 1088 | mBtnTask = 0; |
| 1089 | } |
| 1090 | |
| 1091 | void CursorButtonAccumulator::process(const RawEvent* rawEvent) { |
| 1092 | if (rawEvent->type == EV_KEY) { |
| 1093 | switch (rawEvent->scanCode) { |
| 1094 | case BTN_LEFT: |
| 1095 | mBtnLeft = rawEvent->value; |
| 1096 | break; |
| 1097 | case BTN_RIGHT: |
| 1098 | mBtnRight = rawEvent->value; |
| 1099 | break; |
| 1100 | case BTN_MIDDLE: |
| 1101 | mBtnMiddle = rawEvent->value; |
| 1102 | break; |
| 1103 | case BTN_BACK: |
| 1104 | mBtnBack = rawEvent->value; |
| 1105 | break; |
| 1106 | case BTN_SIDE: |
| 1107 | mBtnSide = rawEvent->value; |
| 1108 | break; |
| 1109 | case BTN_FORWARD: |
| 1110 | mBtnForward = rawEvent->value; |
| 1111 | break; |
| 1112 | case BTN_EXTRA: |
| 1113 | mBtnExtra = rawEvent->value; |
| 1114 | break; |
| 1115 | case BTN_TASK: |
| 1116 | mBtnTask = rawEvent->value; |
| 1117 | break; |
| 1118 | } |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | uint32_t CursorButtonAccumulator::getButtonState() const { |
| 1123 | uint32_t result = 0; |
| 1124 | if (mBtnLeft) { |
| 1125 | result |= AMOTION_EVENT_BUTTON_PRIMARY; |
| 1126 | } |
| 1127 | if (mBtnRight) { |
| 1128 | result |= AMOTION_EVENT_BUTTON_SECONDARY; |
| 1129 | } |
| 1130 | if (mBtnMiddle) { |
| 1131 | result |= AMOTION_EVENT_BUTTON_TERTIARY; |
| 1132 | } |
| 1133 | if (mBtnBack || mBtnSide) { |
| 1134 | result |= AMOTION_EVENT_BUTTON_BACK; |
| 1135 | } |
| 1136 | if (mBtnForward || mBtnExtra) { |
| 1137 | result |= AMOTION_EVENT_BUTTON_FORWARD; |
| 1138 | } |
| 1139 | return result; |
| 1140 | } |
| 1141 | |
| 1142 | |
| 1143 | // --- CursorMotionAccumulator --- |
| 1144 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1145 | CursorMotionAccumulator::CursorMotionAccumulator() { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1146 | clearRelativeAxes(); |
| 1147 | } |
| 1148 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1149 | void CursorMotionAccumulator::reset(InputDevice* device) { |
| 1150 | clearRelativeAxes(); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | void CursorMotionAccumulator::clearRelativeAxes() { |
| 1154 | mRelX = 0; |
| 1155 | mRelY = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | void CursorMotionAccumulator::process(const RawEvent* rawEvent) { |
| 1159 | if (rawEvent->type == EV_REL) { |
| 1160 | switch (rawEvent->scanCode) { |
| 1161 | case REL_X: |
| 1162 | mRelX = rawEvent->value; |
| 1163 | break; |
| 1164 | case REL_Y: |
| 1165 | mRelY = rawEvent->value; |
| 1166 | break; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1167 | } |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | void CursorMotionAccumulator::finishSync() { |
| 1172 | clearRelativeAxes(); |
| 1173 | } |
| 1174 | |
| 1175 | |
| 1176 | // --- CursorScrollAccumulator --- |
| 1177 | |
| 1178 | CursorScrollAccumulator::CursorScrollAccumulator() : |
| 1179 | mHaveRelWheel(false), mHaveRelHWheel(false) { |
| 1180 | clearRelativeAxes(); |
| 1181 | } |
| 1182 | |
| 1183 | void CursorScrollAccumulator::configure(InputDevice* device) { |
| 1184 | mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL); |
| 1185 | mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL); |
| 1186 | } |
| 1187 | |
| 1188 | void CursorScrollAccumulator::reset(InputDevice* device) { |
| 1189 | clearRelativeAxes(); |
| 1190 | } |
| 1191 | |
| 1192 | void CursorScrollAccumulator::clearRelativeAxes() { |
| 1193 | mRelWheel = 0; |
| 1194 | mRelHWheel = 0; |
| 1195 | } |
| 1196 | |
| 1197 | void CursorScrollAccumulator::process(const RawEvent* rawEvent) { |
| 1198 | if (rawEvent->type == EV_REL) { |
| 1199 | switch (rawEvent->scanCode) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1200 | case REL_WHEEL: |
| 1201 | mRelWheel = rawEvent->value; |
| 1202 | break; |
| 1203 | case REL_HWHEEL: |
| 1204 | mRelHWheel = rawEvent->value; |
| 1205 | break; |
| 1206 | } |
| 1207 | } |
| 1208 | } |
| 1209 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1210 | void CursorScrollAccumulator::finishSync() { |
| 1211 | clearRelativeAxes(); |
| 1212 | } |
| 1213 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1214 | |
| 1215 | // --- TouchButtonAccumulator --- |
| 1216 | |
| 1217 | TouchButtonAccumulator::TouchButtonAccumulator() : |
| 1218 | mHaveBtnTouch(false) { |
| 1219 | clearButtons(); |
| 1220 | } |
| 1221 | |
| 1222 | void TouchButtonAccumulator::configure(InputDevice* device) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1223 | mHaveBtnTouch = device->hasKey(BTN_TOUCH); |
| 1224 | } |
| 1225 | |
| 1226 | void TouchButtonAccumulator::reset(InputDevice* device) { |
| 1227 | mBtnTouch = device->isKeyPressed(BTN_TOUCH); |
| 1228 | mBtnStylus = device->isKeyPressed(BTN_STYLUS); |
| 1229 | mBtnStylus2 = device->isKeyPressed(BTN_STYLUS); |
| 1230 | mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER); |
| 1231 | mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN); |
| 1232 | mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER); |
| 1233 | mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH); |
| 1234 | mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL); |
| 1235 | mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH); |
| 1236 | mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE); |
| 1237 | mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS); |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1238 | mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP); |
| 1239 | mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP); |
| 1240 | mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | void TouchButtonAccumulator::clearButtons() { |
| 1244 | mBtnTouch = 0; |
| 1245 | mBtnStylus = 0; |
| 1246 | mBtnStylus2 = 0; |
| 1247 | mBtnToolFinger = 0; |
| 1248 | mBtnToolPen = 0; |
| 1249 | mBtnToolRubber = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1250 | mBtnToolBrush = 0; |
| 1251 | mBtnToolPencil = 0; |
| 1252 | mBtnToolAirbrush = 0; |
| 1253 | mBtnToolMouse = 0; |
| 1254 | mBtnToolLens = 0; |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1255 | mBtnToolDoubleTap = 0; |
| 1256 | mBtnToolTripleTap = 0; |
| 1257 | mBtnToolQuadTap = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | void TouchButtonAccumulator::process(const RawEvent* rawEvent) { |
| 1261 | if (rawEvent->type == EV_KEY) { |
| 1262 | switch (rawEvent->scanCode) { |
| 1263 | case BTN_TOUCH: |
| 1264 | mBtnTouch = rawEvent->value; |
| 1265 | break; |
| 1266 | case BTN_STYLUS: |
| 1267 | mBtnStylus = rawEvent->value; |
| 1268 | break; |
| 1269 | case BTN_STYLUS2: |
| 1270 | mBtnStylus2 = rawEvent->value; |
| 1271 | break; |
| 1272 | case BTN_TOOL_FINGER: |
| 1273 | mBtnToolFinger = rawEvent->value; |
| 1274 | break; |
| 1275 | case BTN_TOOL_PEN: |
| 1276 | mBtnToolPen = rawEvent->value; |
| 1277 | break; |
| 1278 | case BTN_TOOL_RUBBER: |
| 1279 | mBtnToolRubber = rawEvent->value; |
| 1280 | break; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1281 | case BTN_TOOL_BRUSH: |
| 1282 | mBtnToolBrush = rawEvent->value; |
| 1283 | break; |
| 1284 | case BTN_TOOL_PENCIL: |
| 1285 | mBtnToolPencil = rawEvent->value; |
| 1286 | break; |
| 1287 | case BTN_TOOL_AIRBRUSH: |
| 1288 | mBtnToolAirbrush = rawEvent->value; |
| 1289 | break; |
| 1290 | case BTN_TOOL_MOUSE: |
| 1291 | mBtnToolMouse = rawEvent->value; |
| 1292 | break; |
| 1293 | case BTN_TOOL_LENS: |
| 1294 | mBtnToolLens = rawEvent->value; |
| 1295 | break; |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1296 | case BTN_TOOL_DOUBLETAP: |
| 1297 | mBtnToolDoubleTap = rawEvent->value; |
| 1298 | break; |
| 1299 | case BTN_TOOL_TRIPLETAP: |
| 1300 | mBtnToolTripleTap = rawEvent->value; |
| 1301 | break; |
| 1302 | case BTN_TOOL_QUADTAP: |
| 1303 | mBtnToolQuadTap = rawEvent->value; |
| 1304 | break; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1305 | } |
| 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | uint32_t TouchButtonAccumulator::getButtonState() const { |
| 1310 | uint32_t result = 0; |
| 1311 | if (mBtnStylus) { |
| 1312 | result |= AMOTION_EVENT_BUTTON_SECONDARY; |
| 1313 | } |
| 1314 | if (mBtnStylus2) { |
| 1315 | result |= AMOTION_EVENT_BUTTON_TERTIARY; |
| 1316 | } |
| 1317 | return result; |
| 1318 | } |
| 1319 | |
| 1320 | int32_t TouchButtonAccumulator::getToolType() const { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1321 | if (mBtnToolMouse || mBtnToolLens) { |
| 1322 | return AMOTION_EVENT_TOOL_TYPE_MOUSE; |
| 1323 | } |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1324 | if (mBtnToolRubber) { |
| 1325 | return AMOTION_EVENT_TOOL_TYPE_ERASER; |
| 1326 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1327 | if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1328 | return AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 1329 | } |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1330 | if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1331 | return AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 1332 | } |
| 1333 | return AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
| 1334 | } |
| 1335 | |
Jeff Brown | d87c6d5 | 2011-08-10 14:55:59 -0700 | [diff] [blame] | 1336 | bool TouchButtonAccumulator::isToolActive() const { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1337 | return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber |
| 1338 | || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1339 | || mBtnToolMouse || mBtnToolLens |
| 1340 | || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | bool TouchButtonAccumulator::isHovering() const { |
| 1344 | return mHaveBtnTouch && !mBtnTouch; |
| 1345 | } |
| 1346 | |
| 1347 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1348 | // --- RawPointerAxes --- |
| 1349 | |
| 1350 | RawPointerAxes::RawPointerAxes() { |
| 1351 | clear(); |
| 1352 | } |
| 1353 | |
| 1354 | void RawPointerAxes::clear() { |
| 1355 | x.clear(); |
| 1356 | y.clear(); |
| 1357 | pressure.clear(); |
| 1358 | touchMajor.clear(); |
| 1359 | touchMinor.clear(); |
| 1360 | toolMajor.clear(); |
| 1361 | toolMinor.clear(); |
| 1362 | orientation.clear(); |
| 1363 | distance.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1364 | tiltX.clear(); |
| 1365 | tiltY.clear(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1366 | trackingId.clear(); |
| 1367 | slot.clear(); |
| 1368 | } |
| 1369 | |
| 1370 | |
| 1371 | // --- RawPointerData --- |
| 1372 | |
| 1373 | RawPointerData::RawPointerData() { |
| 1374 | clear(); |
| 1375 | } |
| 1376 | |
| 1377 | void RawPointerData::clear() { |
| 1378 | pointerCount = 0; |
| 1379 | clearIdBits(); |
| 1380 | } |
| 1381 | |
| 1382 | void RawPointerData::copyFrom(const RawPointerData& other) { |
| 1383 | pointerCount = other.pointerCount; |
| 1384 | hoveringIdBits = other.hoveringIdBits; |
| 1385 | touchingIdBits = other.touchingIdBits; |
| 1386 | |
| 1387 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 1388 | pointers[i] = other.pointers[i]; |
| 1389 | |
| 1390 | int id = pointers[i].id; |
| 1391 | idToIndex[id] = other.idToIndex[id]; |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const { |
| 1396 | float x = 0, y = 0; |
| 1397 | uint32_t count = touchingIdBits.count(); |
| 1398 | if (count) { |
| 1399 | for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) { |
| 1400 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 1401 | const Pointer& pointer = pointerForId(id); |
| 1402 | x += pointer.x; |
| 1403 | y += pointer.y; |
| 1404 | } |
| 1405 | x /= count; |
| 1406 | y /= count; |
| 1407 | } |
| 1408 | *outX = x; |
| 1409 | *outY = y; |
| 1410 | } |
| 1411 | |
| 1412 | |
| 1413 | // --- CookedPointerData --- |
| 1414 | |
| 1415 | CookedPointerData::CookedPointerData() { |
| 1416 | clear(); |
| 1417 | } |
| 1418 | |
| 1419 | void CookedPointerData::clear() { |
| 1420 | pointerCount = 0; |
| 1421 | hoveringIdBits.clear(); |
| 1422 | touchingIdBits.clear(); |
| 1423 | } |
| 1424 | |
| 1425 | void CookedPointerData::copyFrom(const CookedPointerData& other) { |
| 1426 | pointerCount = other.pointerCount; |
| 1427 | hoveringIdBits = other.hoveringIdBits; |
| 1428 | touchingIdBits = other.touchingIdBits; |
| 1429 | |
| 1430 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 1431 | pointerProperties[i].copyFrom(other.pointerProperties[i]); |
| 1432 | pointerCoords[i].copyFrom(other.pointerCoords[i]); |
| 1433 | |
| 1434 | int id = pointerProperties[i].id; |
| 1435 | idToIndex[id] = other.idToIndex[id]; |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1440 | // --- SingleTouchMotionAccumulator --- |
| 1441 | |
| 1442 | SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() { |
| 1443 | clearAbsoluteAxes(); |
| 1444 | } |
| 1445 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1446 | void SingleTouchMotionAccumulator::reset(InputDevice* device) { |
| 1447 | mAbsX = device->getAbsoluteAxisValue(ABS_X); |
| 1448 | mAbsY = device->getAbsoluteAxisValue(ABS_Y); |
| 1449 | mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE); |
| 1450 | mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH); |
| 1451 | mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE); |
| 1452 | mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X); |
| 1453 | mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y); |
| 1454 | } |
| 1455 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1456 | void SingleTouchMotionAccumulator::clearAbsoluteAxes() { |
| 1457 | mAbsX = 0; |
| 1458 | mAbsY = 0; |
| 1459 | mAbsPressure = 0; |
| 1460 | mAbsToolWidth = 0; |
| 1461 | mAbsDistance = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1462 | mAbsTiltX = 0; |
| 1463 | mAbsTiltY = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) { |
| 1467 | if (rawEvent->type == EV_ABS) { |
| 1468 | switch (rawEvent->scanCode) { |
| 1469 | case ABS_X: |
| 1470 | mAbsX = rawEvent->value; |
| 1471 | break; |
| 1472 | case ABS_Y: |
| 1473 | mAbsY = rawEvent->value; |
| 1474 | break; |
| 1475 | case ABS_PRESSURE: |
| 1476 | mAbsPressure = rawEvent->value; |
| 1477 | break; |
| 1478 | case ABS_TOOL_WIDTH: |
| 1479 | mAbsToolWidth = rawEvent->value; |
| 1480 | break; |
| 1481 | case ABS_DISTANCE: |
| 1482 | mAbsDistance = rawEvent->value; |
| 1483 | break; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1484 | case ABS_TILT_X: |
| 1485 | mAbsTiltX = rawEvent->value; |
| 1486 | break; |
| 1487 | case ABS_TILT_Y: |
| 1488 | mAbsTiltY = rawEvent->value; |
| 1489 | break; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | |
| 1495 | // --- MultiTouchMotionAccumulator --- |
| 1496 | |
| 1497 | MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() : |
| 1498 | mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false) { |
| 1499 | } |
| 1500 | |
| 1501 | MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() { |
| 1502 | delete[] mSlots; |
| 1503 | } |
| 1504 | |
| 1505 | void MultiTouchMotionAccumulator::configure(size_t slotCount, bool usingSlotsProtocol) { |
| 1506 | mSlotCount = slotCount; |
| 1507 | mUsingSlotsProtocol = usingSlotsProtocol; |
| 1508 | |
| 1509 | delete[] mSlots; |
| 1510 | mSlots = new Slot[slotCount]; |
| 1511 | } |
| 1512 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1513 | void MultiTouchMotionAccumulator::reset(InputDevice* device) { |
| 1514 | // Unfortunately there is no way to read the initial contents of the slots. |
| 1515 | // So when we reset the accumulator, we must assume they are all zeroes. |
| 1516 | if (mUsingSlotsProtocol) { |
| 1517 | // Query the driver for the current slot index and use it as the initial slot |
| 1518 | // before we start reading events from the device. It is possible that the |
| 1519 | // current slot index will not be the same as it was when the first event was |
| 1520 | // written into the evdev buffer, which means the input mapper could start |
| 1521 | // out of sync with the initial state of the events in the evdev buffer. |
| 1522 | // In the extremely unlikely case that this happens, the data from |
| 1523 | // two slots will be confused until the next ABS_MT_SLOT event is received. |
| 1524 | // This can cause the touch point to "jump", but at least there will be |
| 1525 | // no stuck touches. |
| 1526 | int32_t initialSlot; |
| 1527 | status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(), |
| 1528 | ABS_MT_SLOT, &initialSlot); |
| 1529 | if (status) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1530 | ALOGD("Could not retrieve current multitouch slot index. status=%d", status); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1531 | initialSlot = -1; |
| 1532 | } |
| 1533 | clearSlots(initialSlot); |
| 1534 | } else { |
| 1535 | clearSlots(-1); |
| 1536 | } |
| 1537 | } |
| 1538 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1539 | void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1540 | if (mSlots) { |
| 1541 | for (size_t i = 0; i < mSlotCount; i++) { |
| 1542 | mSlots[i].clear(); |
| 1543 | } |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1544 | } |
| 1545 | mCurrentSlot = initialSlot; |
| 1546 | } |
| 1547 | |
| 1548 | void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) { |
| 1549 | if (rawEvent->type == EV_ABS) { |
| 1550 | bool newSlot = false; |
| 1551 | if (mUsingSlotsProtocol) { |
| 1552 | if (rawEvent->scanCode == ABS_MT_SLOT) { |
| 1553 | mCurrentSlot = rawEvent->value; |
| 1554 | newSlot = true; |
| 1555 | } |
| 1556 | } else if (mCurrentSlot < 0) { |
| 1557 | mCurrentSlot = 0; |
| 1558 | } |
| 1559 | |
| 1560 | if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) { |
| 1561 | #if DEBUG_POINTERS |
| 1562 | if (newSlot) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1563 | ALOGW("MultiTouch device emitted invalid slot index %d but it " |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1564 | "should be between 0 and %d; ignoring this slot.", |
| 1565 | mCurrentSlot, mSlotCount - 1); |
| 1566 | } |
| 1567 | #endif |
| 1568 | } else { |
| 1569 | Slot* slot = &mSlots[mCurrentSlot]; |
| 1570 | |
| 1571 | switch (rawEvent->scanCode) { |
| 1572 | case ABS_MT_POSITION_X: |
| 1573 | slot->mInUse = true; |
| 1574 | slot->mAbsMTPositionX = rawEvent->value; |
| 1575 | break; |
| 1576 | case ABS_MT_POSITION_Y: |
| 1577 | slot->mInUse = true; |
| 1578 | slot->mAbsMTPositionY = rawEvent->value; |
| 1579 | break; |
| 1580 | case ABS_MT_TOUCH_MAJOR: |
| 1581 | slot->mInUse = true; |
| 1582 | slot->mAbsMTTouchMajor = rawEvent->value; |
| 1583 | break; |
| 1584 | case ABS_MT_TOUCH_MINOR: |
| 1585 | slot->mInUse = true; |
| 1586 | slot->mAbsMTTouchMinor = rawEvent->value; |
| 1587 | slot->mHaveAbsMTTouchMinor = true; |
| 1588 | break; |
| 1589 | case ABS_MT_WIDTH_MAJOR: |
| 1590 | slot->mInUse = true; |
| 1591 | slot->mAbsMTWidthMajor = rawEvent->value; |
| 1592 | break; |
| 1593 | case ABS_MT_WIDTH_MINOR: |
| 1594 | slot->mInUse = true; |
| 1595 | slot->mAbsMTWidthMinor = rawEvent->value; |
| 1596 | slot->mHaveAbsMTWidthMinor = true; |
| 1597 | break; |
| 1598 | case ABS_MT_ORIENTATION: |
| 1599 | slot->mInUse = true; |
| 1600 | slot->mAbsMTOrientation = rawEvent->value; |
| 1601 | break; |
| 1602 | case ABS_MT_TRACKING_ID: |
| 1603 | if (mUsingSlotsProtocol && rawEvent->value < 0) { |
Jeff Brown | 8bcbbef | 2011-08-11 15:49:09 -0700 | [diff] [blame] | 1604 | // The slot is no longer in use but it retains its previous contents, |
| 1605 | // which may be reused for subsequent touches. |
| 1606 | slot->mInUse = false; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1607 | } else { |
| 1608 | slot->mInUse = true; |
| 1609 | slot->mAbsMTTrackingId = rawEvent->value; |
| 1610 | } |
| 1611 | break; |
| 1612 | case ABS_MT_PRESSURE: |
| 1613 | slot->mInUse = true; |
| 1614 | slot->mAbsMTPressure = rawEvent->value; |
| 1615 | break; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1616 | case ABS_MT_DISTANCE: |
| 1617 | slot->mInUse = true; |
| 1618 | slot->mAbsMTDistance = rawEvent->value; |
| 1619 | break; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1620 | case ABS_MT_TOOL_TYPE: |
| 1621 | slot->mInUse = true; |
| 1622 | slot->mAbsMTToolType = rawEvent->value; |
| 1623 | slot->mHaveAbsMTToolType = true; |
| 1624 | break; |
| 1625 | } |
| 1626 | } |
| 1627 | } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_MT_REPORT) { |
| 1628 | // MultiTouch Sync: The driver has returned all data for *one* of the pointers. |
| 1629 | mCurrentSlot += 1; |
| 1630 | } |
| 1631 | } |
| 1632 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1633 | void MultiTouchMotionAccumulator::finishSync() { |
| 1634 | if (!mUsingSlotsProtocol) { |
| 1635 | clearSlots(-1); |
| 1636 | } |
| 1637 | } |
| 1638 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1639 | |
| 1640 | // --- MultiTouchMotionAccumulator::Slot --- |
| 1641 | |
| 1642 | MultiTouchMotionAccumulator::Slot::Slot() { |
| 1643 | clear(); |
| 1644 | } |
| 1645 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1646 | void MultiTouchMotionAccumulator::Slot::clear() { |
| 1647 | mInUse = false; |
| 1648 | mHaveAbsMTTouchMinor = false; |
| 1649 | mHaveAbsMTWidthMinor = false; |
| 1650 | mHaveAbsMTToolType = false; |
| 1651 | mAbsMTPositionX = 0; |
| 1652 | mAbsMTPositionY = 0; |
| 1653 | mAbsMTTouchMajor = 0; |
| 1654 | mAbsMTTouchMinor = 0; |
| 1655 | mAbsMTWidthMajor = 0; |
| 1656 | mAbsMTWidthMinor = 0; |
| 1657 | mAbsMTOrientation = 0; |
| 1658 | mAbsMTTrackingId = -1; |
| 1659 | mAbsMTPressure = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1660 | mAbsMTDistance = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1661 | mAbsMTToolType = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | int32_t MultiTouchMotionAccumulator::Slot::getToolType() const { |
| 1665 | if (mHaveAbsMTToolType) { |
| 1666 | switch (mAbsMTToolType) { |
| 1667 | case MT_TOOL_FINGER: |
| 1668 | return AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 1669 | case MT_TOOL_PEN: |
| 1670 | return AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 1671 | } |
| 1672 | } |
| 1673 | return AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
| 1674 | } |
| 1675 | |
| 1676 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1677 | // --- InputMapper --- |
| 1678 | |
| 1679 | InputMapper::InputMapper(InputDevice* device) : |
| 1680 | mDevice(device), mContext(device->getContext()) { |
| 1681 | } |
| 1682 | |
| 1683 | InputMapper::~InputMapper() { |
| 1684 | } |
| 1685 | |
| 1686 | void InputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 1687 | info->addSource(getSources()); |
| 1688 | } |
| 1689 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1690 | void InputMapper::dump(String8& dump) { |
| 1691 | } |
| 1692 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1693 | void InputMapper::configure(nsecs_t when, |
| 1694 | const InputReaderConfiguration* config, uint32_t changes) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1695 | } |
| 1696 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1697 | void InputMapper::reset(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1698 | } |
| 1699 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 1700 | void InputMapper::timeoutExpired(nsecs_t when) { |
| 1701 | } |
| 1702 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1703 | int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 1704 | return AKEY_STATE_UNKNOWN; |
| 1705 | } |
| 1706 | |
| 1707 | int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 1708 | return AKEY_STATE_UNKNOWN; |
| 1709 | } |
| 1710 | |
| 1711 | int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 1712 | return AKEY_STATE_UNKNOWN; |
| 1713 | } |
| 1714 | |
| 1715 | bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 1716 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 1717 | return false; |
| 1718 | } |
| 1719 | |
| 1720 | int32_t InputMapper::getMetaState() { |
| 1721 | return 0; |
| 1722 | } |
| 1723 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1724 | void InputMapper::fadePointer() { |
| 1725 | } |
| 1726 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1727 | status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) { |
| 1728 | return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo); |
| 1729 | } |
| 1730 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1731 | void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump, |
| 1732 | const RawAbsoluteAxisInfo& axis, const char* name) { |
| 1733 | if (axis.valid) { |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 1734 | dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n", |
| 1735 | name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1736 | } else { |
| 1737 | dump.appendFormat(INDENT4 "%s: unknown range\n", name); |
| 1738 | } |
| 1739 | } |
| 1740 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1741 | |
| 1742 | // --- SwitchInputMapper --- |
| 1743 | |
| 1744 | SwitchInputMapper::SwitchInputMapper(InputDevice* device) : |
| 1745 | InputMapper(device) { |
| 1746 | } |
| 1747 | |
| 1748 | SwitchInputMapper::~SwitchInputMapper() { |
| 1749 | } |
| 1750 | |
| 1751 | uint32_t SwitchInputMapper::getSources() { |
Jeff Brown | 89de57a | 2011-01-19 18:41:38 -0800 | [diff] [blame] | 1752 | return AINPUT_SOURCE_SWITCH; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | void SwitchInputMapper::process(const RawEvent* rawEvent) { |
| 1756 | switch (rawEvent->type) { |
| 1757 | case EV_SW: |
| 1758 | processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value); |
| 1759 | break; |
| 1760 | } |
| 1761 | } |
| 1762 | |
| 1763 | void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1764 | NotifySwitchArgs args(when, 0, switchCode, switchValue); |
| 1765 | getListener()->notifySwitch(&args); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 1769 | return getEventHub()->getSwitchState(getDeviceId(), switchCode); |
| 1770 | } |
| 1771 | |
| 1772 | |
| 1773 | // --- KeyboardInputMapper --- |
| 1774 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1775 | KeyboardInputMapper::KeyboardInputMapper(InputDevice* device, |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1776 | uint32_t source, int32_t keyboardType) : |
| 1777 | InputMapper(device), mSource(source), |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1778 | mKeyboardType(keyboardType) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | KeyboardInputMapper::~KeyboardInputMapper() { |
| 1782 | } |
| 1783 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1784 | uint32_t KeyboardInputMapper::getSources() { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1785 | return mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1786 | } |
| 1787 | |
| 1788 | void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 1789 | InputMapper::populateDeviceInfo(info); |
| 1790 | |
| 1791 | info->setKeyboardType(mKeyboardType); |
Jeff Brown | 1e08fe9 | 2011-11-15 17:48:10 -0800 | [diff] [blame] | 1792 | info->setKeyCharacterMapFile(getEventHub()->getKeyCharacterMapFile(getDeviceId())); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1793 | } |
| 1794 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1795 | void KeyboardInputMapper::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1796 | dump.append(INDENT2 "Keyboard Input Mapper:\n"); |
| 1797 | dumpParameters(dump); |
| 1798 | dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1799 | dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1800 | dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size()); |
| 1801 | dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState); |
| 1802 | dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1803 | } |
| 1804 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1805 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1806 | void KeyboardInputMapper::configure(nsecs_t when, |
| 1807 | const InputReaderConfiguration* config, uint32_t changes) { |
| 1808 | InputMapper::configure(when, config, changes); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1809 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 1810 | if (!changes) { // first time only |
| 1811 | // Configure basic parameters. |
| 1812 | configureParameters(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1813 | } |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1814 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1815 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) { |
| 1816 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) { |
| 1817 | if (!config->getDisplayInfo(mParameters.associatedDisplayId, |
| 1818 | false /*external*/, NULL, NULL, &mOrientation)) { |
| 1819 | mOrientation = DISPLAY_ORIENTATION_0; |
| 1820 | } |
| 1821 | } else { |
| 1822 | mOrientation = DISPLAY_ORIENTATION_0; |
| 1823 | } |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1824 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | void KeyboardInputMapper::configureParameters() { |
| 1828 | mParameters.orientationAware = false; |
| 1829 | getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"), |
| 1830 | mParameters.orientationAware); |
| 1831 | |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 1832 | mParameters.associatedDisplayId = -1; |
| 1833 | if (mParameters.orientationAware) { |
| 1834 | mParameters.associatedDisplayId = 0; |
| 1835 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | void KeyboardInputMapper::dumpParameters(String8& dump) { |
| 1839 | dump.append(INDENT3 "Parameters:\n"); |
| 1840 | dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n", |
| 1841 | mParameters.associatedDisplayId); |
| 1842 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 1843 | toString(mParameters.orientationAware)); |
| 1844 | } |
| 1845 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1846 | void KeyboardInputMapper::reset(nsecs_t when) { |
| 1847 | mMetaState = AMETA_NONE; |
| 1848 | mDownTime = 0; |
| 1849 | mKeyDowns.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1850 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1851 | resetLedState(); |
| 1852 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1853 | InputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | void KeyboardInputMapper::process(const RawEvent* rawEvent) { |
| 1857 | switch (rawEvent->type) { |
| 1858 | case EV_KEY: { |
| 1859 | int32_t scanCode = rawEvent->scanCode; |
| 1860 | if (isKeyboardOrGamepadKey(scanCode)) { |
| 1861 | processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode, |
| 1862 | rawEvent->flags); |
| 1863 | } |
| 1864 | break; |
| 1865 | } |
| 1866 | } |
| 1867 | } |
| 1868 | |
| 1869 | bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) { |
| 1870 | return scanCode < BTN_MOUSE |
| 1871 | || scanCode >= KEY_OK |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 1872 | || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE) |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1873 | || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1874 | } |
| 1875 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1876 | void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode, |
| 1877 | int32_t scanCode, uint32_t policyFlags) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1878 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1879 | if (down) { |
| 1880 | // Rotate key codes according to orientation if needed. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1881 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1882 | keyCode = rotateKeyCode(keyCode, mOrientation); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1883 | } |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 1884 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1885 | // Add key down. |
| 1886 | ssize_t keyDownIndex = findKeyDown(scanCode); |
| 1887 | if (keyDownIndex >= 0) { |
| 1888 | // key repeat, be sure to use same keycode as before in case of rotation |
| 1889 | keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1890 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1891 | // key down |
| 1892 | if ((policyFlags & POLICY_FLAG_VIRTUAL) |
| 1893 | && mContext->shouldDropVirtualKey(when, |
| 1894 | getDevice(), keyCode, scanCode)) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1895 | return; |
| 1896 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1897 | |
| 1898 | mKeyDowns.push(); |
| 1899 | KeyDown& keyDown = mKeyDowns.editTop(); |
| 1900 | keyDown.keyCode = keyCode; |
| 1901 | keyDown.scanCode = scanCode; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1902 | } |
| 1903 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1904 | mDownTime = when; |
| 1905 | } else { |
| 1906 | // Remove key down. |
| 1907 | ssize_t keyDownIndex = findKeyDown(scanCode); |
| 1908 | if (keyDownIndex >= 0) { |
| 1909 | // key up, be sure to use same keycode as before in case of rotation |
| 1910 | keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode; |
| 1911 | mKeyDowns.removeAt(size_t(keyDownIndex)); |
| 1912 | } else { |
| 1913 | // key was not actually down |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1914 | ALOGI("Dropping key up from device %s because the key was not down. " |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1915 | "keyCode=%d, scanCode=%d", |
| 1916 | getDeviceName().string(), keyCode, scanCode); |
| 1917 | return; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1918 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1919 | } |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 1920 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1921 | bool metaStateChanged = false; |
| 1922 | int32_t oldMetaState = mMetaState; |
| 1923 | int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState); |
| 1924 | if (oldMetaState != newMetaState) { |
| 1925 | mMetaState = newMetaState; |
| 1926 | metaStateChanged = true; |
| 1927 | updateLedState(false); |
| 1928 | } |
| 1929 | |
| 1930 | nsecs_t downTime = mDownTime; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1931 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1932 | // Key down on external an keyboard should wake the device. |
| 1933 | // We don't do this for internal keyboards to prevent them from waking up in your pocket. |
| 1934 | // For internal keyboards, the key layout file should specify the policy flags for |
| 1935 | // each wake key individually. |
| 1936 | // TODO: Use the input device configuration to control this behavior more finely. |
| 1937 | if (down && getDevice()->isExternal() |
| 1938 | && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) { |
| 1939 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 1940 | } |
| 1941 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1942 | if (metaStateChanged) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1943 | getContext()->updateGlobalMetaState(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1944 | } |
| 1945 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1946 | if (down && !isMetaKey(keyCode)) { |
| 1947 | getContext()->fadePointer(); |
| 1948 | } |
| 1949 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1950 | NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1951 | down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, |
| 1952 | AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1953 | getListener()->notifyKey(&args); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1954 | } |
| 1955 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1956 | ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) { |
| 1957 | size_t n = mKeyDowns.size(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1958 | for (size_t i = 0; i < n; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1959 | if (mKeyDowns[i].scanCode == scanCode) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1960 | return i; |
| 1961 | } |
| 1962 | } |
| 1963 | return -1; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1964 | } |
| 1965 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1966 | int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 1967 | return getEventHub()->getKeyCodeState(getDeviceId(), keyCode); |
| 1968 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1969 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1970 | int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 1971 | return getEventHub()->getScanCodeState(getDeviceId(), scanCode); |
| 1972 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1973 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1974 | bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 1975 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 1976 | return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags); |
| 1977 | } |
| 1978 | |
| 1979 | int32_t KeyboardInputMapper::getMetaState() { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1980 | return mMetaState; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1981 | } |
| 1982 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1983 | void KeyboardInputMapper::resetLedState() { |
| 1984 | initializeLedState(mCapsLockLedState, LED_CAPSL); |
| 1985 | initializeLedState(mNumLockLedState, LED_NUML); |
| 1986 | initializeLedState(mScrollLockLedState, LED_SCROLLL); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1987 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1988 | updateLedState(true); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1989 | } |
| 1990 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1991 | void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) { |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1992 | ledState.avail = getEventHub()->hasLed(getDeviceId(), led); |
| 1993 | ledState.on = false; |
| 1994 | } |
| 1995 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1996 | void KeyboardInputMapper::updateLedState(bool reset) { |
| 1997 | updateLedStateForModifier(mCapsLockLedState, LED_CAPSL, |
Jeff Brown | 51e7fe75 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 1998 | AMETA_CAPS_LOCK_ON, reset); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1999 | updateLedStateForModifier(mNumLockLedState, LED_NUML, |
Jeff Brown | 51e7fe75 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 2000 | AMETA_NUM_LOCK_ON, reset); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2001 | updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL, |
Jeff Brown | 51e7fe75 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 2002 | AMETA_SCROLL_LOCK_ON, reset); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 2003 | } |
| 2004 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2005 | void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState, |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 2006 | int32_t led, int32_t modifier, bool reset) { |
| 2007 | if (ledState.avail) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2008 | bool desiredState = (mMetaState & modifier) != 0; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2009 | if (reset || ledState.on != desiredState) { |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 2010 | getEventHub()->setLedState(getDeviceId(), led, desiredState); |
| 2011 | ledState.on = desiredState; |
| 2012 | } |
| 2013 | } |
| 2014 | } |
| 2015 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2016 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2017 | // --- CursorInputMapper --- |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2018 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2019 | CursorInputMapper::CursorInputMapper(InputDevice* device) : |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2020 | InputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2021 | } |
| 2022 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2023 | CursorInputMapper::~CursorInputMapper() { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2024 | } |
| 2025 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2026 | uint32_t CursorInputMapper::getSources() { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2027 | return mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2028 | } |
| 2029 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2030 | void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2031 | InputMapper::populateDeviceInfo(info); |
| 2032 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2033 | if (mParameters.mode == Parameters::MODE_POINTER) { |
| 2034 | float minX, minY, maxX, maxY; |
| 2035 | if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2036 | info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f); |
| 2037 | 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] | 2038 | } |
| 2039 | } else { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2040 | info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale); |
| 2041 | 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] | 2042 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2043 | 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] | 2044 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2045 | if (mCursorScrollAccumulator.haveRelativeVWheel()) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2046 | 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] | 2047 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2048 | if (mCursorScrollAccumulator.haveRelativeHWheel()) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2049 | 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] | 2050 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2051 | } |
| 2052 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2053 | void CursorInputMapper::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2054 | dump.append(INDENT2 "Cursor Input Mapper:\n"); |
| 2055 | dumpParameters(dump); |
| 2056 | dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale); |
| 2057 | dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale); |
| 2058 | dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision); |
| 2059 | dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision); |
| 2060 | dump.appendFormat(INDENT3 "HaveVWheel: %s\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2061 | toString(mCursorScrollAccumulator.haveRelativeVWheel())); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2062 | dump.appendFormat(INDENT3 "HaveHWheel: %s\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2063 | toString(mCursorScrollAccumulator.haveRelativeHWheel())); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2064 | dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale); |
| 2065 | dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2066 | dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2067 | dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState); |
| 2068 | dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState))); |
| 2069 | dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2070 | } |
| 2071 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2072 | void CursorInputMapper::configure(nsecs_t when, |
| 2073 | const InputReaderConfiguration* config, uint32_t changes) { |
| 2074 | InputMapper::configure(when, config, changes); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2075 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2076 | if (!changes) { // first time only |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2077 | mCursorScrollAccumulator.configure(getDevice()); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2078 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2079 | // Configure basic parameters. |
| 2080 | configureParameters(); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2081 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2082 | // Configure device mode. |
| 2083 | switch (mParameters.mode) { |
| 2084 | case Parameters::MODE_POINTER: |
| 2085 | mSource = AINPUT_SOURCE_MOUSE; |
| 2086 | mXPrecision = 1.0f; |
| 2087 | mYPrecision = 1.0f; |
| 2088 | mXScale = 1.0f; |
| 2089 | mYScale = 1.0f; |
| 2090 | mPointerController = getPolicy()->obtainPointerController(getDeviceId()); |
| 2091 | break; |
| 2092 | case Parameters::MODE_NAVIGATION: |
| 2093 | mSource = AINPUT_SOURCE_TRACKBALL; |
| 2094 | mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 2095 | mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 2096 | mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 2097 | mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 2098 | break; |
| 2099 | } |
| 2100 | |
| 2101 | mVWheelScale = 1.0f; |
| 2102 | mHWheelScale = 1.0f; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2103 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 2104 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2105 | if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) { |
| 2106 | mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters); |
| 2107 | mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters); |
| 2108 | mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters); |
| 2109 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2110 | |
| 2111 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) { |
| 2112 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) { |
| 2113 | if (!config->getDisplayInfo(mParameters.associatedDisplayId, |
| 2114 | false /*external*/, NULL, NULL, &mOrientation)) { |
| 2115 | mOrientation = DISPLAY_ORIENTATION_0; |
| 2116 | } |
| 2117 | } else { |
| 2118 | mOrientation = DISPLAY_ORIENTATION_0; |
| 2119 | } |
| 2120 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2121 | } |
| 2122 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2123 | void CursorInputMapper::configureParameters() { |
| 2124 | mParameters.mode = Parameters::MODE_POINTER; |
| 2125 | String8 cursorModeString; |
| 2126 | if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) { |
| 2127 | if (cursorModeString == "navigation") { |
| 2128 | mParameters.mode = Parameters::MODE_NAVIGATION; |
| 2129 | } else if (cursorModeString != "pointer" && cursorModeString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2130 | ALOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string()); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2131 | } |
| 2132 | } |
| 2133 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2134 | mParameters.orientationAware = false; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2135 | getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"), |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2136 | mParameters.orientationAware); |
| 2137 | |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2138 | mParameters.associatedDisplayId = -1; |
| 2139 | if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) { |
| 2140 | mParameters.associatedDisplayId = 0; |
| 2141 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2142 | } |
| 2143 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2144 | void CursorInputMapper::dumpParameters(String8& dump) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2145 | dump.append(INDENT3 "Parameters:\n"); |
| 2146 | dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n", |
| 2147 | mParameters.associatedDisplayId); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2148 | |
| 2149 | switch (mParameters.mode) { |
| 2150 | case Parameters::MODE_POINTER: |
| 2151 | dump.append(INDENT4 "Mode: pointer\n"); |
| 2152 | break; |
| 2153 | case Parameters::MODE_NAVIGATION: |
| 2154 | dump.append(INDENT4 "Mode: navigation\n"); |
| 2155 | break; |
| 2156 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 2157 | ALOG_ASSERT(false); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2158 | } |
| 2159 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2160 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 2161 | toString(mParameters.orientationAware)); |
| 2162 | } |
| 2163 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2164 | void CursorInputMapper::reset(nsecs_t when) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2165 | mButtonState = 0; |
| 2166 | mDownTime = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2167 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2168 | mPointerVelocityControl.reset(); |
| 2169 | mWheelXVelocityControl.reset(); |
| 2170 | mWheelYVelocityControl.reset(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2171 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2172 | mCursorButtonAccumulator.reset(getDevice()); |
| 2173 | mCursorMotionAccumulator.reset(getDevice()); |
| 2174 | mCursorScrollAccumulator.reset(getDevice()); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2175 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2176 | InputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2177 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2178 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2179 | void CursorInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2180 | mCursorButtonAccumulator.process(rawEvent); |
| 2181 | mCursorMotionAccumulator.process(rawEvent); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2182 | mCursorScrollAccumulator.process(rawEvent); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2183 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2184 | if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) { |
| 2185 | sync(rawEvent->when); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2186 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2187 | } |
| 2188 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2189 | void CursorInputMapper::sync(nsecs_t when) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2190 | int32_t lastButtonState = mButtonState; |
| 2191 | int32_t currentButtonState = mCursorButtonAccumulator.getButtonState(); |
| 2192 | mButtonState = currentButtonState; |
| 2193 | |
| 2194 | bool wasDown = isPointerDown(lastButtonState); |
| 2195 | bool down = isPointerDown(currentButtonState); |
| 2196 | bool downChanged; |
| 2197 | if (!wasDown && down) { |
| 2198 | mDownTime = when; |
| 2199 | downChanged = true; |
| 2200 | } else if (wasDown && !down) { |
| 2201 | downChanged = true; |
| 2202 | } else { |
| 2203 | downChanged = false; |
| 2204 | } |
| 2205 | nsecs_t downTime = mDownTime; |
| 2206 | bool buttonsChanged = currentButtonState != lastButtonState; |
Jeff Brown | c28306a | 2011-08-23 21:32:42 -0700 | [diff] [blame] | 2207 | bool buttonsPressed = currentButtonState & ~lastButtonState; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2208 | |
| 2209 | float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale; |
| 2210 | float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale; |
| 2211 | bool moved = deltaX != 0 || deltaY != 0; |
| 2212 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2213 | // Rotate delta according to orientation if needed. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2214 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0 |
| 2215 | && (deltaX != 0.0f || deltaY != 0.0f)) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2216 | rotateDelta(mOrientation, &deltaX, &deltaY); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2217 | } |
| 2218 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2219 | // Move the pointer. |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2220 | PointerProperties pointerProperties; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2221 | pointerProperties.clear(); |
| 2222 | pointerProperties.id = 0; |
| 2223 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE; |
| 2224 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2225 | PointerCoords pointerCoords; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2226 | pointerCoords.clear(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2227 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2228 | float vscroll = mCursorScrollAccumulator.getRelativeVWheel(); |
| 2229 | float hscroll = mCursorScrollAccumulator.getRelativeHWheel(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2230 | bool scrolled = vscroll != 0 || hscroll != 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2231 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2232 | mWheelYVelocityControl.move(when, NULL, &vscroll); |
| 2233 | mWheelXVelocityControl.move(when, &hscroll, NULL); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2234 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2235 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2236 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2237 | if (mPointerController != NULL) { |
| 2238 | if (moved || scrolled || buttonsChanged) { |
| 2239 | mPointerController->setPresentation( |
| 2240 | PointerControllerInterface::PRESENTATION_POINTER); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2241 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2242 | if (moved) { |
| 2243 | mPointerController->move(deltaX, deltaY); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2244 | } |
| 2245 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2246 | if (buttonsChanged) { |
| 2247 | mPointerController->setButtonState(currentButtonState); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2248 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2249 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2250 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2251 | } |
| 2252 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2253 | float x, y; |
| 2254 | mPointerController->getPosition(&x, &y); |
| 2255 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 2256 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 2257 | } else { |
| 2258 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX); |
| 2259 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY); |
| 2260 | } |
| 2261 | |
| 2262 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2263 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2264 | // Moving an external trackball or mouse should wake the device. |
| 2265 | // We don't do this for internal cursor devices to prevent them from waking up |
| 2266 | // the device in your pocket. |
| 2267 | // TODO: Use the input device configuration to control this behavior more finely. |
| 2268 | uint32_t policyFlags = 0; |
Jeff Brown | c28306a | 2011-08-23 21:32:42 -0700 | [diff] [blame] | 2269 | if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) { |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2270 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 2271 | } |
| 2272 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2273 | // Synthesize key down from buttons if needed. |
| 2274 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource, |
| 2275 | policyFlags, lastButtonState, currentButtonState); |
| 2276 | |
| 2277 | // Send motion event. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2278 | if (downChanged || moved || scrolled || buttonsChanged) { |
| 2279 | int32_t metaState = mContext->getGlobalMetaState(); |
| 2280 | int32_t motionEventAction; |
| 2281 | if (downChanged) { |
| 2282 | motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP; |
| 2283 | } else if (down || mPointerController == NULL) { |
| 2284 | motionEventAction = AMOTION_EVENT_ACTION_MOVE; |
| 2285 | } else { |
| 2286 | motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE; |
| 2287 | } |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2288 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2289 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 2290 | motionEventAction, 0, metaState, currentButtonState, 0, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2291 | 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2292 | getListener()->notifyMotion(&args); |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2293 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2294 | // Send hover move after UP to tell the application that the mouse is hovering now. |
| 2295 | if (motionEventAction == AMOTION_EVENT_ACTION_UP |
| 2296 | && mPointerController != NULL) { |
| 2297 | NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags, |
| 2298 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 2299 | metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 2300 | 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime); |
| 2301 | getListener()->notifyMotion(&hoverArgs); |
| 2302 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2303 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2304 | // Send scroll events. |
| 2305 | if (scrolled) { |
| 2306 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll); |
| 2307 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll); |
| 2308 | |
| 2309 | NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags, |
| 2310 | AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState, |
| 2311 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 2312 | 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime); |
| 2313 | getListener()->notifyMotion(&scrollArgs); |
| 2314 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2315 | } |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2316 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2317 | // Synthesize key up from buttons if needed. |
| 2318 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource, |
| 2319 | policyFlags, lastButtonState, currentButtonState); |
| 2320 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2321 | mCursorMotionAccumulator.finishSync(); |
| 2322 | mCursorScrollAccumulator.finishSync(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2323 | } |
| 2324 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2325 | int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Jeff Brown | c3fc2d0 | 2010-08-10 15:47:53 -0700 | [diff] [blame] | 2326 | if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) { |
| 2327 | return getEventHub()->getScanCodeState(getDeviceId(), scanCode); |
| 2328 | } else { |
| 2329 | return AKEY_STATE_UNKNOWN; |
| 2330 | } |
| 2331 | } |
| 2332 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2333 | void CursorInputMapper::fadePointer() { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2334 | if (mPointerController != NULL) { |
| 2335 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 2336 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2337 | } |
| 2338 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2339 | |
| 2340 | // --- TouchInputMapper --- |
| 2341 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2342 | TouchInputMapper::TouchInputMapper(InputDevice* device) : |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2343 | InputMapper(device), |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2344 | mSource(0), mDeviceMode(DEVICE_MODE_DISABLED), |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2345 | mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2346 | } |
| 2347 | |
| 2348 | TouchInputMapper::~TouchInputMapper() { |
| 2349 | } |
| 2350 | |
| 2351 | uint32_t TouchInputMapper::getSources() { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2352 | return mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2353 | } |
| 2354 | |
| 2355 | void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 2356 | InputMapper::populateDeviceInfo(info); |
| 2357 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2358 | if (mDeviceMode != DEVICE_MODE_DISABLED) { |
| 2359 | info->addMotionRange(mOrientedRanges.x); |
| 2360 | info->addMotionRange(mOrientedRanges.y); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2361 | info->addMotionRange(mOrientedRanges.pressure); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2362 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2363 | if (mOrientedRanges.haveSize) { |
| 2364 | info->addMotionRange(mOrientedRanges.size); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2365 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2366 | |
| 2367 | if (mOrientedRanges.haveTouchSize) { |
| 2368 | info->addMotionRange(mOrientedRanges.touchMajor); |
| 2369 | info->addMotionRange(mOrientedRanges.touchMinor); |
| 2370 | } |
| 2371 | |
| 2372 | if (mOrientedRanges.haveToolSize) { |
| 2373 | info->addMotionRange(mOrientedRanges.toolMajor); |
| 2374 | info->addMotionRange(mOrientedRanges.toolMinor); |
| 2375 | } |
| 2376 | |
| 2377 | if (mOrientedRanges.haveOrientation) { |
| 2378 | info->addMotionRange(mOrientedRanges.orientation); |
| 2379 | } |
| 2380 | |
| 2381 | if (mOrientedRanges.haveDistance) { |
| 2382 | info->addMotionRange(mOrientedRanges.distance); |
| 2383 | } |
| 2384 | |
| 2385 | if (mOrientedRanges.haveTilt) { |
| 2386 | info->addMotionRange(mOrientedRanges.tilt); |
| 2387 | } |
| 2388 | |
| 2389 | if (mCursorScrollAccumulator.haveRelativeVWheel()) { |
| 2390 | info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f); |
| 2391 | } |
| 2392 | if (mCursorScrollAccumulator.haveRelativeHWheel()) { |
| 2393 | info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f); |
| 2394 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2395 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2396 | } |
| 2397 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2398 | void TouchInputMapper::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2399 | dump.append(INDENT2 "Touch Input Mapper:\n"); |
| 2400 | dumpParameters(dump); |
| 2401 | dumpVirtualKeys(dump); |
| 2402 | dumpRawPointerAxes(dump); |
| 2403 | dumpCalibration(dump); |
| 2404 | dumpSurface(dump); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2405 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2406 | dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n"); |
| 2407 | dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale); |
| 2408 | dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale); |
| 2409 | dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision); |
| 2410 | dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision); |
| 2411 | dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2412 | dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale); |
| 2413 | dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2414 | dump.appendFormat(INDENT4 "OrientationCenter: %0.3f\n", mOrientationCenter); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2415 | dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale); |
| 2416 | dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2417 | dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt)); |
| 2418 | dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter); |
| 2419 | dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale); |
| 2420 | dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter); |
| 2421 | dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2422 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2423 | dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2424 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2425 | dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n", |
| 2426 | mLastRawPointerData.pointerCount); |
| 2427 | for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) { |
| 2428 | const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i]; |
| 2429 | dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, " |
| 2430 | "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2431 | "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, " |
| 2432 | "toolType=%d, isHovering=%s\n", i, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2433 | pointer.id, pointer.x, pointer.y, pointer.pressure, |
| 2434 | pointer.touchMajor, pointer.touchMinor, |
| 2435 | pointer.toolMajor, pointer.toolMinor, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2436 | pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2437 | pointer.toolType, toString(pointer.isHovering)); |
| 2438 | } |
| 2439 | |
| 2440 | dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n", |
| 2441 | mLastCookedPointerData.pointerCount); |
| 2442 | for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) { |
| 2443 | const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i]; |
| 2444 | const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i]; |
| 2445 | dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, " |
| 2446 | "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2447 | "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, " |
| 2448 | "toolType=%d, isHovering=%s\n", i, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2449 | pointerProperties.id, |
| 2450 | pointerCoords.getX(), |
| 2451 | pointerCoords.getY(), |
| 2452 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 2453 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 2454 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 2455 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 2456 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 2457 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2458 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT), |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2459 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), |
| 2460 | pointerProperties.toolType, |
| 2461 | toString(mLastCookedPointerData.isHovering(i))); |
| 2462 | } |
| 2463 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2464 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2465 | dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n"); |
| 2466 | dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2467 | mPointerXMovementScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2468 | dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2469 | mPointerYMovementScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2470 | dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2471 | mPointerXZoomScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2472 | dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2473 | mPointerYZoomScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2474 | dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n", |
| 2475 | mPointerGestureMaxSwipeWidth); |
| 2476 | } |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2477 | } |
| 2478 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2479 | void TouchInputMapper::configure(nsecs_t when, |
| 2480 | const InputReaderConfiguration* config, uint32_t changes) { |
| 2481 | InputMapper::configure(when, config, changes); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2482 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2483 | mConfig = *config; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2484 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2485 | if (!changes) { // first time only |
| 2486 | // Configure basic parameters. |
| 2487 | configureParameters(); |
| 2488 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2489 | // Configure common accumulators. |
| 2490 | mCursorScrollAccumulator.configure(getDevice()); |
| 2491 | mTouchButtonAccumulator.configure(getDevice()); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2492 | |
| 2493 | // Configure absolute axis information. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2494 | configureRawPointerAxes(); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2495 | |
| 2496 | // Prepare input device calibration. |
| 2497 | parseCalibration(); |
| 2498 | resolveCalibration(); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2499 | } |
| 2500 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2501 | if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2502 | // Update pointer speed. |
| 2503 | mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters); |
| 2504 | mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters); |
| 2505 | mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2506 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2507 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2508 | bool resetNeeded = false; |
| 2509 | if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 2510 | | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT |
| 2511 | | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2512 | // Configure device sources, surface dimensions, orientation and |
| 2513 | // scaling factors. |
| 2514 | configureSurface(when, &resetNeeded); |
| 2515 | } |
| 2516 | |
| 2517 | if (changes && resetNeeded) { |
| 2518 | // Send reset, unless this is the first time the device has been configured, |
| 2519 | // in which case the reader will call reset itself after all mappers are ready. |
| 2520 | getDevice()->notifyReset(when); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2521 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2522 | } |
| 2523 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2524 | void TouchInputMapper::configureParameters() { |
Jeff Brown | b126822 | 2011-06-03 17:06:16 -0700 | [diff] [blame] | 2525 | // Use the pointer presentation mode for devices that do not support distinct |
| 2526 | // multitouch. The spot-based presentation relies on being able to accurately |
| 2527 | // locate two or more fingers on the touch pad. |
| 2528 | mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT) |
| 2529 | ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2530 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2531 | String8 gestureModeString; |
| 2532 | if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"), |
| 2533 | gestureModeString)) { |
| 2534 | if (gestureModeString == "pointer") { |
| 2535 | mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER; |
| 2536 | } else if (gestureModeString == "spots") { |
| 2537 | mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS; |
| 2538 | } else if (gestureModeString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2539 | ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string()); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2540 | } |
| 2541 | } |
| 2542 | |
Jeff Brown | deffe07 | 2011-08-26 18:38:46 -0700 | [diff] [blame] | 2543 | if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) { |
| 2544 | // The device is a touch screen. |
| 2545 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
| 2546 | } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) { |
| 2547 | // The device is a pointing device like a track pad. |
| 2548 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
| 2549 | } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X) |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2550 | || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) { |
| 2551 | // The device is a cursor device with a touch pad attached. |
| 2552 | // By default don't use the touch pad to move the pointer. |
| 2553 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; |
| 2554 | } else { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2555 | // The device is a touch pad of unknown purpose. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2556 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
| 2557 | } |
| 2558 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2559 | String8 deviceTypeString; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2560 | if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"), |
| 2561 | deviceTypeString)) { |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 2562 | if (deviceTypeString == "touchScreen") { |
| 2563 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2564 | } else if (deviceTypeString == "touchPad") { |
| 2565 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2566 | } else if (deviceTypeString == "pointer") { |
| 2567 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2568 | } else if (deviceTypeString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2569 | ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string()); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2570 | } |
| 2571 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2572 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2573 | mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2574 | getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"), |
| 2575 | mParameters.orientationAware); |
| 2576 | |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2577 | mParameters.associatedDisplayId = -1; |
| 2578 | mParameters.associatedDisplayIsExternal = false; |
| 2579 | if (mParameters.orientationAware |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2580 | || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2581 | || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) { |
| 2582 | mParameters.associatedDisplayIsExternal = |
| 2583 | mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN |
| 2584 | && getDevice()->isExternal(); |
| 2585 | mParameters.associatedDisplayId = 0; |
| 2586 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2587 | } |
| 2588 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2589 | void TouchInputMapper::dumpParameters(String8& dump) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2590 | dump.append(INDENT3 "Parameters:\n"); |
| 2591 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2592 | switch (mParameters.gestureMode) { |
| 2593 | case Parameters::GESTURE_MODE_POINTER: |
| 2594 | dump.append(INDENT4 "GestureMode: pointer\n"); |
| 2595 | break; |
| 2596 | case Parameters::GESTURE_MODE_SPOTS: |
| 2597 | dump.append(INDENT4 "GestureMode: spots\n"); |
| 2598 | break; |
| 2599 | default: |
| 2600 | assert(false); |
| 2601 | } |
| 2602 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2603 | switch (mParameters.deviceType) { |
| 2604 | case Parameters::DEVICE_TYPE_TOUCH_SCREEN: |
| 2605 | dump.append(INDENT4 "DeviceType: touchScreen\n"); |
| 2606 | break; |
| 2607 | case Parameters::DEVICE_TYPE_TOUCH_PAD: |
| 2608 | dump.append(INDENT4 "DeviceType: touchPad\n"); |
| 2609 | break; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2610 | case Parameters::DEVICE_TYPE_POINTER: |
| 2611 | dump.append(INDENT4 "DeviceType: pointer\n"); |
| 2612 | break; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2613 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 2614 | ALOG_ASSERT(false); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2615 | } |
| 2616 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2617 | dump.appendFormat(INDENT4 "AssociatedDisplay: id=%d, isExternal=%s\n", |
| 2618 | mParameters.associatedDisplayId, toString(mParameters.associatedDisplayIsExternal)); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2619 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 2620 | toString(mParameters.orientationAware)); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2621 | } |
| 2622 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2623 | void TouchInputMapper::configureRawPointerAxes() { |
| 2624 | mRawPointerAxes.clear(); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2625 | } |
| 2626 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2627 | void TouchInputMapper::dumpRawPointerAxes(String8& dump) { |
| 2628 | dump.append(INDENT3 "Raw Touch Axes:\n"); |
| 2629 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X"); |
| 2630 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y"); |
| 2631 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure"); |
| 2632 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor"); |
| 2633 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor"); |
| 2634 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor"); |
| 2635 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor"); |
| 2636 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation"); |
| 2637 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance"); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2638 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX"); |
| 2639 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY"); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2640 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId"); |
| 2641 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot"); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2642 | } |
| 2643 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2644 | void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) { |
| 2645 | int32_t oldDeviceMode = mDeviceMode; |
| 2646 | |
| 2647 | // Determine device mode. |
| 2648 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER |
| 2649 | && mConfig.pointerGesturesEnabled) { |
| 2650 | mSource = AINPUT_SOURCE_MOUSE; |
| 2651 | mDeviceMode = DEVICE_MODE_POINTER; |
| 2652 | } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN |
| 2653 | && mParameters.associatedDisplayId >= 0) { |
| 2654 | mSource = AINPUT_SOURCE_TOUCHSCREEN; |
| 2655 | mDeviceMode = DEVICE_MODE_DIRECT; |
| 2656 | } else { |
| 2657 | mSource = AINPUT_SOURCE_TOUCHPAD; |
| 2658 | mDeviceMode = DEVICE_MODE_UNSCALED; |
| 2659 | } |
| 2660 | |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2661 | // Ensure we have valid X and Y axes. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2662 | if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2663 | ALOGW(INDENT "Touch device '%s' did not report support for X or Y axis! " |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2664 | "The device will be inoperable.", getDeviceName().string()); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2665 | mDeviceMode = DEVICE_MODE_DISABLED; |
| 2666 | return; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2667 | } |
| 2668 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2669 | // Get associated display dimensions. |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2670 | if (mParameters.associatedDisplayId >= 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2671 | if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId, |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2672 | mParameters.associatedDisplayIsExternal, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2673 | &mAssociatedDisplayWidth, &mAssociatedDisplayHeight, |
| 2674 | &mAssociatedDisplayOrientation)) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2675 | ALOGI(INDENT "Touch device '%s' could not query the properties of its associated " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2676 | "display %d. The device will be inoperable until the display size " |
| 2677 | "becomes available.", |
| 2678 | getDeviceName().string(), mParameters.associatedDisplayId); |
| 2679 | mDeviceMode = DEVICE_MODE_DISABLED; |
| 2680 | return; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2681 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2682 | } |
| 2683 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2684 | // Configure dimensions. |
| 2685 | int32_t width, height, orientation; |
| 2686 | if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) { |
| 2687 | width = mAssociatedDisplayWidth; |
| 2688 | height = mAssociatedDisplayHeight; |
| 2689 | orientation = mParameters.orientationAware ? |
| 2690 | mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0; |
| 2691 | } else { |
| 2692 | width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1; |
| 2693 | height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1; |
| 2694 | orientation = DISPLAY_ORIENTATION_0; |
| 2695 | } |
| 2696 | |
| 2697 | // If moving between pointer modes, need to reset some state. |
| 2698 | bool deviceModeChanged; |
| 2699 | if (mDeviceMode != oldDeviceMode) { |
| 2700 | deviceModeChanged = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2701 | mOrientedRanges.clear(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2702 | } |
| 2703 | |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 2704 | // Create pointer controller if needed. |
| 2705 | if (mDeviceMode == DEVICE_MODE_POINTER || |
| 2706 | (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) { |
| 2707 | if (mPointerController == NULL) { |
| 2708 | mPointerController = getPolicy()->obtainPointerController(getDeviceId()); |
| 2709 | } |
| 2710 | } else { |
| 2711 | mPointerController.clear(); |
| 2712 | } |
| 2713 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2714 | bool orientationChanged = mSurfaceOrientation != orientation; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2715 | if (orientationChanged) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2716 | mSurfaceOrientation = orientation; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2717 | } |
| 2718 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2719 | bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2720 | if (sizeChanged || deviceModeChanged) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2721 | ALOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d, mode is %d", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2722 | getDeviceId(), getDeviceName().string(), width, height, mDeviceMode); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2723 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2724 | mSurfaceWidth = width; |
| 2725 | mSurfaceHeight = height; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2726 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2727 | // Configure X and Y factors. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2728 | mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1); |
| 2729 | mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1); |
| 2730 | mXPrecision = 1.0f / mXScale; |
| 2731 | mYPrecision = 1.0f / mYScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2732 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2733 | mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2734 | mOrientedRanges.x.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2735 | mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2736 | mOrientedRanges.y.source = mSource; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2737 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2738 | configureVirtualKeys(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2739 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2740 | // Scale factor for terms that are not oriented in a particular axis. |
| 2741 | // If the pixels are square then xScale == yScale otherwise we fake it |
| 2742 | // by choosing an average. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2743 | mGeometricScale = avg(mXScale, mYScale); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2744 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2745 | // Size of diagonal axis. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2746 | float diagonalSize = hypotf(width, height); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2747 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 2748 | // Size factors. |
| 2749 | if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) { |
| 2750 | if (mRawPointerAxes.touchMajor.valid |
| 2751 | && mRawPointerAxes.touchMajor.maxValue != 0) { |
| 2752 | mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue; |
| 2753 | } else if (mRawPointerAxes.toolMajor.valid |
| 2754 | && mRawPointerAxes.toolMajor.maxValue != 0) { |
| 2755 | mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue; |
| 2756 | } else { |
| 2757 | mSizeScale = 0.0f; |
| 2758 | } |
| 2759 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2760 | mOrientedRanges.haveTouchSize = true; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 2761 | mOrientedRanges.haveToolSize = true; |
| 2762 | mOrientedRanges.haveSize = true; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2763 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2764 | mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2765 | mOrientedRanges.touchMajor.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2766 | mOrientedRanges.touchMajor.min = 0; |
| 2767 | mOrientedRanges.touchMajor.max = diagonalSize; |
| 2768 | mOrientedRanges.touchMajor.flat = 0; |
| 2769 | mOrientedRanges.touchMajor.fuzz = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2770 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2771 | mOrientedRanges.touchMinor = mOrientedRanges.touchMajor; |
| 2772 | mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2773 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2774 | mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2775 | mOrientedRanges.toolMajor.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2776 | mOrientedRanges.toolMajor.min = 0; |
| 2777 | mOrientedRanges.toolMajor.max = diagonalSize; |
| 2778 | mOrientedRanges.toolMajor.flat = 0; |
| 2779 | mOrientedRanges.toolMajor.fuzz = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2780 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2781 | mOrientedRanges.toolMinor = mOrientedRanges.toolMajor; |
| 2782 | mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 2783 | |
| 2784 | mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2785 | mOrientedRanges.size.source = mSource; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 2786 | mOrientedRanges.size.min = 0; |
| 2787 | mOrientedRanges.size.max = 1.0; |
| 2788 | mOrientedRanges.size.flat = 0; |
| 2789 | mOrientedRanges.size.fuzz = 0; |
| 2790 | } else { |
| 2791 | mSizeScale = 0.0f; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2792 | } |
| 2793 | |
| 2794 | // Pressure factors. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2795 | mPressureScale = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2796 | if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL |
| 2797 | || mCalibration.pressureCalibration |
| 2798 | == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) { |
| 2799 | if (mCalibration.havePressureScale) { |
| 2800 | mPressureScale = mCalibration.pressureScale; |
| 2801 | } else if (mRawPointerAxes.pressure.valid |
| 2802 | && mRawPointerAxes.pressure.maxValue != 0) { |
| 2803 | mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2804 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2805 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2806 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2807 | mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE; |
| 2808 | mOrientedRanges.pressure.source = mSource; |
| 2809 | mOrientedRanges.pressure.min = 0; |
| 2810 | mOrientedRanges.pressure.max = 1.0; |
| 2811 | mOrientedRanges.pressure.flat = 0; |
| 2812 | mOrientedRanges.pressure.fuzz = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2813 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2814 | // Tilt |
| 2815 | mTiltXCenter = 0; |
| 2816 | mTiltXScale = 0; |
| 2817 | mTiltYCenter = 0; |
| 2818 | mTiltYScale = 0; |
| 2819 | mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid; |
| 2820 | if (mHaveTilt) { |
| 2821 | mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue, |
| 2822 | mRawPointerAxes.tiltX.maxValue); |
| 2823 | mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue, |
| 2824 | mRawPointerAxes.tiltY.maxValue); |
| 2825 | mTiltXScale = M_PI / 180; |
| 2826 | mTiltYScale = M_PI / 180; |
| 2827 | |
| 2828 | mOrientedRanges.haveTilt = true; |
| 2829 | |
| 2830 | mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT; |
| 2831 | mOrientedRanges.tilt.source = mSource; |
| 2832 | mOrientedRanges.tilt.min = 0; |
| 2833 | mOrientedRanges.tilt.max = M_PI_2; |
| 2834 | mOrientedRanges.tilt.flat = 0; |
| 2835 | mOrientedRanges.tilt.fuzz = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2836 | } |
| 2837 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2838 | // Orientation |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2839 | mOrientationCenter = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2840 | mOrientationScale = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2841 | if (mHaveTilt) { |
| 2842 | mOrientedRanges.haveOrientation = true; |
| 2843 | |
| 2844 | mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION; |
| 2845 | mOrientedRanges.orientation.source = mSource; |
| 2846 | mOrientedRanges.orientation.min = -M_PI; |
| 2847 | mOrientedRanges.orientation.max = M_PI; |
| 2848 | mOrientedRanges.orientation.flat = 0; |
| 2849 | mOrientedRanges.orientation.fuzz = 0; |
| 2850 | } else if (mCalibration.orientationCalibration != |
| 2851 | Calibration::ORIENTATION_CALIBRATION_NONE) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2852 | if (mCalibration.orientationCalibration |
| 2853 | == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2854 | if (mRawPointerAxes.orientation.valid) { |
| 2855 | mOrientationCenter = avg(mRawPointerAxes.orientation.minValue, |
| 2856 | mRawPointerAxes.orientation.maxValue); |
| 2857 | mOrientationScale = M_PI / (mRawPointerAxes.orientation.maxValue - |
| 2858 | mRawPointerAxes.orientation.minValue); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2859 | } |
| 2860 | } |
| 2861 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2862 | mOrientedRanges.haveOrientation = true; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2863 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2864 | mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2865 | mOrientedRanges.orientation.source = mSource; |
| 2866 | mOrientedRanges.orientation.min = -M_PI_2; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2867 | mOrientedRanges.orientation.max = M_PI_2; |
| 2868 | mOrientedRanges.orientation.flat = 0; |
| 2869 | mOrientedRanges.orientation.fuzz = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2870 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2871 | |
| 2872 | // Distance |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2873 | mDistanceScale = 0; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2874 | if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) { |
| 2875 | if (mCalibration.distanceCalibration |
| 2876 | == Calibration::DISTANCE_CALIBRATION_SCALED) { |
| 2877 | if (mCalibration.haveDistanceScale) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2878 | mDistanceScale = mCalibration.distanceScale; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2879 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2880 | mDistanceScale = 1.0f; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2881 | } |
| 2882 | } |
| 2883 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2884 | mOrientedRanges.haveDistance = true; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2885 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2886 | mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2887 | mOrientedRanges.distance.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2888 | mOrientedRanges.distance.min = |
| 2889 | mRawPointerAxes.distance.minValue * mDistanceScale; |
| 2890 | mOrientedRanges.distance.max = |
| 2891 | mRawPointerAxes.distance.minValue * mDistanceScale; |
| 2892 | mOrientedRanges.distance.flat = 0; |
| 2893 | mOrientedRanges.distance.fuzz = |
| 2894 | mRawPointerAxes.distance.fuzz * mDistanceScale; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2895 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2896 | } |
| 2897 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2898 | if (orientationChanged || sizeChanged || deviceModeChanged) { |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2899 | // Compute oriented surface dimensions, precision, scales and ranges. |
| 2900 | // Note that the maximum value reported is an inclusive maximum value so it is one |
| 2901 | // unit less than the total width or height of surface. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2902 | switch (mSurfaceOrientation) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 2903 | case DISPLAY_ORIENTATION_90: |
| 2904 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2905 | mOrientedSurfaceWidth = mSurfaceHeight; |
| 2906 | mOrientedSurfaceHeight = mSurfaceWidth; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2907 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2908 | mOrientedXPrecision = mYPrecision; |
| 2909 | mOrientedYPrecision = mXPrecision; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2910 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2911 | mOrientedRanges.x.min = 0; |
| 2912 | mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue) |
| 2913 | * mYScale; |
| 2914 | mOrientedRanges.x.flat = 0; |
| 2915 | mOrientedRanges.x.fuzz = mYScale; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2916 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2917 | mOrientedRanges.y.min = 0; |
| 2918 | mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue) |
| 2919 | * mXScale; |
| 2920 | mOrientedRanges.y.flat = 0; |
| 2921 | mOrientedRanges.y.fuzz = mXScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2922 | break; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2923 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2924 | default: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2925 | mOrientedSurfaceWidth = mSurfaceWidth; |
| 2926 | mOrientedSurfaceHeight = mSurfaceHeight; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2927 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2928 | mOrientedXPrecision = mXPrecision; |
| 2929 | mOrientedYPrecision = mYPrecision; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2930 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2931 | mOrientedRanges.x.min = 0; |
| 2932 | mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue) |
| 2933 | * mXScale; |
| 2934 | mOrientedRanges.x.flat = 0; |
| 2935 | mOrientedRanges.x.fuzz = mXScale; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2936 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2937 | mOrientedRanges.y.min = 0; |
| 2938 | mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue) |
| 2939 | * mYScale; |
| 2940 | mOrientedRanges.y.flat = 0; |
| 2941 | mOrientedRanges.y.fuzz = mYScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2942 | break; |
| 2943 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2944 | |
| 2945 | // Compute pointer gesture detection parameters. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2946 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2947 | int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1; |
| 2948 | int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2949 | float rawDiagonal = hypotf(rawWidth, rawHeight); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2950 | float displayDiagonal = hypotf(mAssociatedDisplayWidth, |
| 2951 | mAssociatedDisplayHeight); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2952 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2953 | // 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] | 2954 | // given area relative to the diagonal size of the display when no acceleration |
| 2955 | // is applied. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2956 | // Assume that the touch pad has a square aspect ratio such that movements in |
| 2957 | // X and Y of the same number of raw units cover the same physical distance. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2958 | mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2959 | * displayDiagonal / rawDiagonal; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2960 | mPointerYMovementScale = mPointerXMovementScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2961 | |
| 2962 | // Scale zooms to cover a smaller range of the display than movements do. |
| 2963 | // This value determines the area around the pointer that is affected by freeform |
| 2964 | // pointer gestures. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2965 | mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2966 | * displayDiagonal / rawDiagonal; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2967 | mPointerYZoomScale = mPointerXZoomScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2968 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2969 | // Max width between pointers to detect a swipe gesture is more than some fraction |
| 2970 | // of the diagonal axis of the touch pad. Touches that are wider than this are |
| 2971 | // translated into freeform gestures. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2972 | mPointerGestureMaxSwipeWidth = |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2973 | mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2974 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2975 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2976 | // Abort current pointer usages because the state has changed. |
| 2977 | abortPointerUsage(when, 0 /*policyFlags*/); |
| 2978 | |
| 2979 | // Inform the dispatcher about the changes. |
| 2980 | *outResetNeeded = true; |
| 2981 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2982 | } |
| 2983 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2984 | void TouchInputMapper::dumpSurface(String8& dump) { |
| 2985 | dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth); |
| 2986 | dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight); |
| 2987 | dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2988 | } |
| 2989 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2990 | void TouchInputMapper::configureVirtualKeys() { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2991 | Vector<VirtualKeyDefinition> virtualKeyDefinitions; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 2992 | getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2993 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2994 | mVirtualKeys.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2995 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2996 | if (virtualKeyDefinitions.size() == 0) { |
| 2997 | return; |
| 2998 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2999 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3000 | mVirtualKeys.setCapacity(virtualKeyDefinitions.size()); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3001 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3002 | int32_t touchScreenLeft = mRawPointerAxes.x.minValue; |
| 3003 | int32_t touchScreenTop = mRawPointerAxes.y.minValue; |
| 3004 | int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1; |
| 3005 | int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3006 | |
| 3007 | for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3008 | const VirtualKeyDefinition& virtualKeyDefinition = |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3009 | virtualKeyDefinitions[i]; |
| 3010 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3011 | mVirtualKeys.add(); |
| 3012 | VirtualKey& virtualKey = mVirtualKeys.editTop(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3013 | |
| 3014 | virtualKey.scanCode = virtualKeyDefinition.scanCode; |
| 3015 | int32_t keyCode; |
| 3016 | uint32_t flags; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 3017 | if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3018 | & keyCode, & flags)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3019 | ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3020 | virtualKey.scanCode); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3021 | mVirtualKeys.pop(); // drop the key |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3022 | continue; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3023 | } |
| 3024 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3025 | virtualKey.keyCode = keyCode; |
| 3026 | virtualKey.flags = flags; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3027 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3028 | // convert the key definition's display coordinates into touch coordinates for a hit box |
| 3029 | int32_t halfWidth = virtualKeyDefinition.width / 2; |
| 3030 | int32_t halfHeight = virtualKeyDefinition.height / 2; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3031 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3032 | virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3033 | * touchScreenWidth / mSurfaceWidth + touchScreenLeft; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3034 | virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3035 | * touchScreenWidth / mSurfaceWidth + touchScreenLeft; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3036 | virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3037 | * touchScreenHeight / mSurfaceHeight + touchScreenTop; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3038 | virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3039 | * touchScreenHeight / mSurfaceHeight + touchScreenTop; |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3040 | } |
| 3041 | } |
| 3042 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3043 | void TouchInputMapper::dumpVirtualKeys(String8& dump) { |
| 3044 | if (!mVirtualKeys.isEmpty()) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3045 | dump.append(INDENT3 "Virtual Keys:\n"); |
| 3046 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3047 | for (size_t i = 0; i < mVirtualKeys.size(); i++) { |
| 3048 | const VirtualKey& virtualKey = mVirtualKeys.itemAt(i); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3049 | dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, " |
| 3050 | "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n", |
| 3051 | i, virtualKey.scanCode, virtualKey.keyCode, |
| 3052 | virtualKey.hitLeft, virtualKey.hitRight, |
| 3053 | virtualKey.hitTop, virtualKey.hitBottom); |
| 3054 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3055 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3056 | } |
| 3057 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3058 | void TouchInputMapper::parseCalibration() { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 3059 | const PropertyMap& in = getDevice()->getConfiguration(); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3060 | Calibration& out = mCalibration; |
| 3061 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3062 | // Size |
| 3063 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT; |
| 3064 | String8 sizeCalibrationString; |
| 3065 | if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) { |
| 3066 | if (sizeCalibrationString == "none") { |
| 3067 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; |
| 3068 | } else if (sizeCalibrationString == "geometric") { |
| 3069 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC; |
| 3070 | } else if (sizeCalibrationString == "diameter") { |
| 3071 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER; |
| 3072 | } else if (sizeCalibrationString == "area") { |
| 3073 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA; |
| 3074 | } else if (sizeCalibrationString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3075 | ALOGW("Invalid value for touch.size.calibration: '%s'", |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3076 | sizeCalibrationString.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3077 | } |
| 3078 | } |
| 3079 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3080 | out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"), |
| 3081 | out.sizeScale); |
| 3082 | out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"), |
| 3083 | out.sizeBias); |
| 3084 | out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"), |
| 3085 | out.sizeIsSummed); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3086 | |
| 3087 | // Pressure |
| 3088 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT; |
| 3089 | String8 pressureCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 3090 | if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3091 | if (pressureCalibrationString == "none") { |
| 3092 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; |
| 3093 | } else if (pressureCalibrationString == "physical") { |
| 3094 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL; |
| 3095 | } else if (pressureCalibrationString == "amplitude") { |
| 3096 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE; |
| 3097 | } else if (pressureCalibrationString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3098 | ALOGW("Invalid value for touch.pressure.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3099 | pressureCalibrationString.string()); |
| 3100 | } |
| 3101 | } |
| 3102 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3103 | out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"), |
| 3104 | out.pressureScale); |
| 3105 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3106 | // Orientation |
| 3107 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT; |
| 3108 | String8 orientationCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 3109 | if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3110 | if (orientationCalibrationString == "none") { |
| 3111 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; |
| 3112 | } else if (orientationCalibrationString == "interpolated") { |
| 3113 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 3114 | } else if (orientationCalibrationString == "vector") { |
| 3115 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3116 | } else if (orientationCalibrationString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3117 | ALOGW("Invalid value for touch.orientation.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3118 | orientationCalibrationString.string()); |
| 3119 | } |
| 3120 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3121 | |
| 3122 | // Distance |
| 3123 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT; |
| 3124 | String8 distanceCalibrationString; |
| 3125 | if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) { |
| 3126 | if (distanceCalibrationString == "none") { |
| 3127 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE; |
| 3128 | } else if (distanceCalibrationString == "scaled") { |
| 3129 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED; |
| 3130 | } else if (distanceCalibrationString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3131 | ALOGW("Invalid value for touch.distance.calibration: '%s'", |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3132 | distanceCalibrationString.string()); |
| 3133 | } |
| 3134 | } |
| 3135 | |
| 3136 | out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"), |
| 3137 | out.distanceScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3138 | } |
| 3139 | |
| 3140 | void TouchInputMapper::resolveCalibration() { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3141 | // Size |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3142 | if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) { |
| 3143 | if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) { |
| 3144 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3145 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3146 | } else { |
| 3147 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; |
| 3148 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3149 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3150 | // Pressure |
| 3151 | if (mRawPointerAxes.pressure.valid) { |
| 3152 | if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) { |
| 3153 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL; |
| 3154 | } |
| 3155 | } else { |
| 3156 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3157 | } |
| 3158 | |
| 3159 | // Orientation |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3160 | if (mRawPointerAxes.orientation.valid) { |
| 3161 | if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3162 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3163 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3164 | } else { |
| 3165 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3166 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3167 | |
| 3168 | // Distance |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3169 | if (mRawPointerAxes.distance.valid) { |
| 3170 | if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3171 | mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3172 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3173 | } else { |
| 3174 | mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3175 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3176 | } |
| 3177 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3178 | void TouchInputMapper::dumpCalibration(String8& dump) { |
| 3179 | dump.append(INDENT3 "Calibration:\n"); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3180 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3181 | // Size |
| 3182 | switch (mCalibration.sizeCalibration) { |
| 3183 | case Calibration::SIZE_CALIBRATION_NONE: |
| 3184 | dump.append(INDENT4 "touch.size.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3185 | break; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3186 | case Calibration::SIZE_CALIBRATION_GEOMETRIC: |
| 3187 | dump.append(INDENT4 "touch.size.calibration: geometric\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3188 | break; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3189 | case Calibration::SIZE_CALIBRATION_DIAMETER: |
| 3190 | dump.append(INDENT4 "touch.size.calibration: diameter\n"); |
| 3191 | break; |
| 3192 | case Calibration::SIZE_CALIBRATION_AREA: |
| 3193 | dump.append(INDENT4 "touch.size.calibration: area\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3194 | break; |
| 3195 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3196 | ALOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3197 | } |
| 3198 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3199 | if (mCalibration.haveSizeScale) { |
| 3200 | dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n", |
| 3201 | mCalibration.sizeScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3202 | } |
| 3203 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3204 | if (mCalibration.haveSizeBias) { |
| 3205 | dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n", |
| 3206 | mCalibration.sizeBias); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3207 | } |
| 3208 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3209 | if (mCalibration.haveSizeIsSummed) { |
| 3210 | dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n", |
| 3211 | toString(mCalibration.sizeIsSummed)); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3212 | } |
| 3213 | |
| 3214 | // Pressure |
| 3215 | switch (mCalibration.pressureCalibration) { |
| 3216 | case Calibration::PRESSURE_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3217 | dump.append(INDENT4 "touch.pressure.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3218 | break; |
| 3219 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3220 | dump.append(INDENT4 "touch.pressure.calibration: physical\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3221 | break; |
| 3222 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3223 | dump.append(INDENT4 "touch.pressure.calibration: amplitude\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3224 | break; |
| 3225 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3226 | ALOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3227 | } |
| 3228 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3229 | if (mCalibration.havePressureScale) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3230 | dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n", |
| 3231 | mCalibration.pressureScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3232 | } |
| 3233 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3234 | // Orientation |
| 3235 | switch (mCalibration.orientationCalibration) { |
| 3236 | case Calibration::ORIENTATION_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3237 | dump.append(INDENT4 "touch.orientation.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3238 | break; |
| 3239 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3240 | dump.append(INDENT4 "touch.orientation.calibration: interpolated\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3241 | break; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 3242 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: |
| 3243 | dump.append(INDENT4 "touch.orientation.calibration: vector\n"); |
| 3244 | break; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3245 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3246 | ALOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3247 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3248 | |
| 3249 | // Distance |
| 3250 | switch (mCalibration.distanceCalibration) { |
| 3251 | case Calibration::DISTANCE_CALIBRATION_NONE: |
| 3252 | dump.append(INDENT4 "touch.distance.calibration: none\n"); |
| 3253 | break; |
| 3254 | case Calibration::DISTANCE_CALIBRATION_SCALED: |
| 3255 | dump.append(INDENT4 "touch.distance.calibration: scaled\n"); |
| 3256 | break; |
| 3257 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3258 | ALOG_ASSERT(false); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3259 | } |
| 3260 | |
| 3261 | if (mCalibration.haveDistanceScale) { |
| 3262 | dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n", |
| 3263 | mCalibration.distanceScale); |
| 3264 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3265 | } |
| 3266 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3267 | void TouchInputMapper::reset(nsecs_t when) { |
| 3268 | mCursorButtonAccumulator.reset(getDevice()); |
| 3269 | mCursorScrollAccumulator.reset(getDevice()); |
| 3270 | mTouchButtonAccumulator.reset(getDevice()); |
| 3271 | |
| 3272 | mPointerVelocityControl.reset(); |
| 3273 | mWheelXVelocityControl.reset(); |
| 3274 | mWheelYVelocityControl.reset(); |
| 3275 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3276 | mCurrentRawPointerData.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3277 | mLastRawPointerData.clear(); |
| 3278 | mCurrentCookedPointerData.clear(); |
| 3279 | mLastCookedPointerData.clear(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3280 | mCurrentButtonState = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3281 | mLastButtonState = 0; |
| 3282 | mCurrentRawVScroll = 0; |
| 3283 | mCurrentRawHScroll = 0; |
| 3284 | mCurrentFingerIdBits.clear(); |
| 3285 | mLastFingerIdBits.clear(); |
| 3286 | mCurrentStylusIdBits.clear(); |
| 3287 | mLastStylusIdBits.clear(); |
| 3288 | mCurrentMouseIdBits.clear(); |
| 3289 | mLastMouseIdBits.clear(); |
| 3290 | mPointerUsage = POINTER_USAGE_NONE; |
| 3291 | mSentHoverEnter = false; |
| 3292 | mDownTime = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3293 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3294 | mCurrentVirtualKey.down = false; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3295 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3296 | mPointerGesture.reset(); |
| 3297 | mPointerSimple.reset(); |
| 3298 | |
| 3299 | if (mPointerController != NULL) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3300 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 3301 | mPointerController->clearSpots(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3302 | } |
| 3303 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3304 | InputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3305 | } |
| 3306 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3307 | void TouchInputMapper::process(const RawEvent* rawEvent) { |
| 3308 | mCursorButtonAccumulator.process(rawEvent); |
| 3309 | mCursorScrollAccumulator.process(rawEvent); |
| 3310 | mTouchButtonAccumulator.process(rawEvent); |
| 3311 | |
| 3312 | if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) { |
| 3313 | sync(rawEvent->when); |
| 3314 | } |
| 3315 | } |
| 3316 | |
| 3317 | void TouchInputMapper::sync(nsecs_t when) { |
| 3318 | // Sync button state. |
| 3319 | mCurrentButtonState = mTouchButtonAccumulator.getButtonState() |
| 3320 | | mCursorButtonAccumulator.getButtonState(); |
| 3321 | |
| 3322 | // Sync scroll state. |
| 3323 | mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel(); |
| 3324 | mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel(); |
| 3325 | mCursorScrollAccumulator.finishSync(); |
| 3326 | |
| 3327 | // Sync touch state. |
| 3328 | bool havePointerIds = true; |
| 3329 | mCurrentRawPointerData.clear(); |
| 3330 | syncTouch(when, &havePointerIds); |
| 3331 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 3332 | #if DEBUG_RAW_EVENTS |
| 3333 | if (!havePointerIds) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3334 | ALOGD("syncTouch: pointerCount %d -> %d, no pointer ids", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3335 | mLastRawPointerData.pointerCount, |
| 3336 | mCurrentRawPointerData.pointerCount); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 3337 | } else { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3338 | ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, " |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3339 | "hovering ids 0x%08x -> 0x%08x", |
| 3340 | mLastRawPointerData.pointerCount, |
| 3341 | mCurrentRawPointerData.pointerCount, |
| 3342 | mLastRawPointerData.touchingIdBits.value, |
| 3343 | mCurrentRawPointerData.touchingIdBits.value, |
| 3344 | mLastRawPointerData.hoveringIdBits.value, |
| 3345 | mCurrentRawPointerData.hoveringIdBits.value); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 3346 | } |
| 3347 | #endif |
| 3348 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3349 | // Reset state that we will compute below. |
| 3350 | mCurrentFingerIdBits.clear(); |
| 3351 | mCurrentStylusIdBits.clear(); |
| 3352 | mCurrentMouseIdBits.clear(); |
| 3353 | mCurrentCookedPointerData.clear(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3354 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3355 | if (mDeviceMode == DEVICE_MODE_DISABLED) { |
| 3356 | // Drop all input if the device is disabled. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3357 | mCurrentRawPointerData.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3358 | mCurrentButtonState = 0; |
| 3359 | } else { |
| 3360 | // Preprocess pointer data. |
| 3361 | if (!havePointerIds) { |
| 3362 | assignPointerIds(); |
| 3363 | } |
| 3364 | |
| 3365 | // Handle policy on initial down or hover events. |
| 3366 | uint32_t policyFlags = 0; |
Jeff Brown | c28306a | 2011-08-23 21:32:42 -0700 | [diff] [blame] | 3367 | bool initialDown = mLastRawPointerData.pointerCount == 0 |
| 3368 | && mCurrentRawPointerData.pointerCount != 0; |
| 3369 | bool buttonsPressed = mCurrentButtonState & ~mLastButtonState; |
| 3370 | if (initialDown || buttonsPressed) { |
| 3371 | // If this is a touch screen, hide the pointer on an initial down. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3372 | if (mDeviceMode == DEVICE_MODE_DIRECT) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3373 | getContext()->fadePointer(); |
| 3374 | } |
| 3375 | |
| 3376 | // Initial downs on external touch devices should wake the device. |
| 3377 | // We don't do this for internal touch screens to prevent them from waking |
| 3378 | // up in your pocket. |
| 3379 | // TODO: Use the input device configuration to control this behavior more finely. |
| 3380 | if (getDevice()->isExternal()) { |
| 3381 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 3382 | } |
| 3383 | } |
| 3384 | |
| 3385 | // Synthesize key down from raw buttons if needed. |
| 3386 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource, |
| 3387 | policyFlags, mLastButtonState, mCurrentButtonState); |
| 3388 | |
| 3389 | // Consume raw off-screen touches before cooking pointer data. |
| 3390 | // If touches are consumed, subsequent code will not receive any pointer data. |
| 3391 | if (consumeRawTouches(when, policyFlags)) { |
| 3392 | mCurrentRawPointerData.clear(); |
| 3393 | } |
| 3394 | |
| 3395 | // Cook pointer data. This call populates the mCurrentCookedPointerData structure |
| 3396 | // with cooked pointer data that has the same ids and indices as the raw data. |
| 3397 | // The following code can use either the raw or cooked data, as needed. |
| 3398 | cookPointerData(); |
| 3399 | |
| 3400 | // Dispatch the touches either directly or by translation through a pointer on screen. |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 3401 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3402 | for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) { |
| 3403 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 3404 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3405 | if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS |
| 3406 | || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) { |
| 3407 | mCurrentStylusIdBits.markBit(id); |
| 3408 | } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER |
| 3409 | || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 3410 | mCurrentFingerIdBits.markBit(id); |
| 3411 | } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) { |
| 3412 | mCurrentMouseIdBits.markBit(id); |
| 3413 | } |
| 3414 | } |
| 3415 | for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) { |
| 3416 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 3417 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3418 | if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS |
| 3419 | || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) { |
| 3420 | mCurrentStylusIdBits.markBit(id); |
| 3421 | } |
| 3422 | } |
| 3423 | |
| 3424 | // Stylus takes precedence over all tools, then mouse, then finger. |
| 3425 | PointerUsage pointerUsage = mPointerUsage; |
| 3426 | if (!mCurrentStylusIdBits.isEmpty()) { |
| 3427 | mCurrentMouseIdBits.clear(); |
| 3428 | mCurrentFingerIdBits.clear(); |
| 3429 | pointerUsage = POINTER_USAGE_STYLUS; |
| 3430 | } else if (!mCurrentMouseIdBits.isEmpty()) { |
| 3431 | mCurrentFingerIdBits.clear(); |
| 3432 | pointerUsage = POINTER_USAGE_MOUSE; |
| 3433 | } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) { |
| 3434 | pointerUsage = POINTER_USAGE_GESTURES; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3435 | } |
| 3436 | |
| 3437 | dispatchPointerUsage(when, policyFlags, pointerUsage); |
| 3438 | } else { |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 3439 | if (mDeviceMode == DEVICE_MODE_DIRECT |
| 3440 | && mConfig.showTouches && mPointerController != NULL) { |
| 3441 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT); |
| 3442 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 3443 | |
| 3444 | mPointerController->setButtonState(mCurrentButtonState); |
| 3445 | mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords, |
| 3446 | mCurrentCookedPointerData.idToIndex, |
| 3447 | mCurrentCookedPointerData.touchingIdBits); |
| 3448 | } |
| 3449 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3450 | dispatchHoverExit(when, policyFlags); |
| 3451 | dispatchTouches(when, policyFlags); |
| 3452 | dispatchHoverEnterAndMove(when, policyFlags); |
| 3453 | } |
| 3454 | |
| 3455 | // Synthesize key up from raw buttons if needed. |
| 3456 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource, |
| 3457 | policyFlags, mLastButtonState, mCurrentButtonState); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3458 | } |
| 3459 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3460 | // Copy current touch to last touch in preparation for the next cycle. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3461 | mLastRawPointerData.copyFrom(mCurrentRawPointerData); |
| 3462 | mLastCookedPointerData.copyFrom(mCurrentCookedPointerData); |
| 3463 | mLastButtonState = mCurrentButtonState; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3464 | mLastFingerIdBits = mCurrentFingerIdBits; |
| 3465 | mLastStylusIdBits = mCurrentStylusIdBits; |
| 3466 | mLastMouseIdBits = mCurrentMouseIdBits; |
| 3467 | |
| 3468 | // Clear some transient state. |
| 3469 | mCurrentRawVScroll = 0; |
| 3470 | mCurrentRawHScroll = 0; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3471 | } |
| 3472 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3473 | void TouchInputMapper::timeoutExpired(nsecs_t when) { |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 3474 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3475 | if (mPointerUsage == POINTER_USAGE_GESTURES) { |
| 3476 | dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/); |
| 3477 | } |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3478 | } |
| 3479 | } |
| 3480 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3481 | bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) { |
| 3482 | // Check for release of a virtual key. |
| 3483 | if (mCurrentVirtualKey.down) { |
| 3484 | if (mCurrentRawPointerData.touchingIdBits.isEmpty()) { |
| 3485 | // Pointer went up while virtual key was down. |
| 3486 | mCurrentVirtualKey.down = false; |
| 3487 | if (!mCurrentVirtualKey.ignored) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3488 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3489 | ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3490 | mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3491 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3492 | dispatchVirtualKey(when, policyFlags, |
| 3493 | AKEY_EVENT_ACTION_UP, |
| 3494 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3495 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3496 | return true; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3497 | } |
| 3498 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3499 | if (mCurrentRawPointerData.touchingIdBits.count() == 1) { |
| 3500 | uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit(); |
| 3501 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3502 | const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y); |
| 3503 | if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) { |
| 3504 | // Pointer is still within the space of the virtual key. |
| 3505 | return true; |
| 3506 | } |
| 3507 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3508 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3509 | // Pointer left virtual key area or another pointer also went down. |
| 3510 | // Send key cancellation but do not consume the touch yet. |
| 3511 | // This is useful when the user swipes through from the virtual key area |
| 3512 | // into the main display surface. |
| 3513 | mCurrentVirtualKey.down = false; |
| 3514 | if (!mCurrentVirtualKey.ignored) { |
| 3515 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3516 | ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3517 | mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode); |
| 3518 | #endif |
| 3519 | dispatchVirtualKey(when, policyFlags, |
| 3520 | AKEY_EVENT_ACTION_UP, |
| 3521 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
| 3522 | | AKEY_EVENT_FLAG_CANCELED); |
| 3523 | } |
| 3524 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3525 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3526 | if (mLastRawPointerData.touchingIdBits.isEmpty() |
| 3527 | && !mCurrentRawPointerData.touchingIdBits.isEmpty()) { |
| 3528 | // Pointer just went down. Check for virtual key press or off-screen touches. |
| 3529 | uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit(); |
| 3530 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3531 | if (!isPointInsideSurface(pointer.x, pointer.y)) { |
| 3532 | // If exactly one pointer went down, check for virtual key hit. |
| 3533 | // Otherwise we will drop the entire stroke. |
| 3534 | if (mCurrentRawPointerData.touchingIdBits.count() == 1) { |
| 3535 | const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y); |
| 3536 | if (virtualKey) { |
| 3537 | mCurrentVirtualKey.down = true; |
| 3538 | mCurrentVirtualKey.downTime = when; |
| 3539 | mCurrentVirtualKey.keyCode = virtualKey->keyCode; |
| 3540 | mCurrentVirtualKey.scanCode = virtualKey->scanCode; |
| 3541 | mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey( |
| 3542 | when, getDevice(), virtualKey->keyCode, virtualKey->scanCode); |
| 3543 | |
| 3544 | if (!mCurrentVirtualKey.ignored) { |
| 3545 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3546 | ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3547 | mCurrentVirtualKey.keyCode, |
| 3548 | mCurrentVirtualKey.scanCode); |
| 3549 | #endif |
| 3550 | dispatchVirtualKey(when, policyFlags, |
| 3551 | AKEY_EVENT_ACTION_DOWN, |
| 3552 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY); |
| 3553 | } |
| 3554 | } |
| 3555 | } |
| 3556 | return true; |
| 3557 | } |
| 3558 | } |
| 3559 | |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3560 | // Disable all virtual key touches that happen within a short time interval of the |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3561 | // most recent touch within the screen area. The idea is to filter out stray |
| 3562 | // virtual key presses when interacting with the touch screen. |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3563 | // |
| 3564 | // Problems we're trying to solve: |
| 3565 | // |
| 3566 | // 1. While scrolling a list or dragging the window shade, the user swipes down into a |
| 3567 | // virtual key area that is implemented by a separate touch panel and accidentally |
| 3568 | // triggers a virtual key. |
| 3569 | // |
| 3570 | // 2. While typing in the on screen keyboard, the user taps slightly outside the screen |
| 3571 | // area and accidentally triggers a virtual key. This often happens when virtual keys |
| 3572 | // are layed out below the screen near to where the on screen keyboard's space bar |
| 3573 | // is displayed. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3574 | if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 3575 | mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime); |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3576 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3577 | return false; |
| 3578 | } |
| 3579 | |
| 3580 | void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags, |
| 3581 | int32_t keyEventAction, int32_t keyEventFlags) { |
| 3582 | int32_t keyCode = mCurrentVirtualKey.keyCode; |
| 3583 | int32_t scanCode = mCurrentVirtualKey.scanCode; |
| 3584 | nsecs_t downTime = mCurrentVirtualKey.downTime; |
| 3585 | int32_t metaState = mContext->getGlobalMetaState(); |
| 3586 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3587 | |
| 3588 | NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags, |
| 3589 | keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime); |
| 3590 | getListener()->notifyKey(&args); |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3591 | } |
| 3592 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3593 | void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3594 | BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits; |
| 3595 | BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3596 | int32_t metaState = getContext()->getGlobalMetaState(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3597 | int32_t buttonState = mCurrentButtonState; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3598 | |
| 3599 | if (currentIdBits == lastIdBits) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3600 | if (!currentIdBits.isEmpty()) { |
| 3601 | // No pointer id changes so this is a move event. |
| 3602 | // The listener takes care of batching moves so we don't have to deal with that here. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3603 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3604 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, |
| 3605 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 3606 | mCurrentCookedPointerData.pointerProperties, |
| 3607 | mCurrentCookedPointerData.pointerCoords, |
| 3608 | mCurrentCookedPointerData.idToIndex, |
| 3609 | currentIdBits, -1, |
| 3610 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 3611 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3612 | } else { |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3613 | // There may be pointers going up and pointers going down and pointers moving |
| 3614 | // all at the same time. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3615 | BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value); |
| 3616 | BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3617 | BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3618 | BitSet32 dispatchedIdBits(lastIdBits.value); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3619 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3620 | // Update last coordinates of pointers that have moved so that we observe the new |
| 3621 | // 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] | 3622 | bool moveNeeded = updateMovedPointers( |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3623 | mCurrentCookedPointerData.pointerProperties, |
| 3624 | mCurrentCookedPointerData.pointerCoords, |
| 3625 | mCurrentCookedPointerData.idToIndex, |
| 3626 | mLastCookedPointerData.pointerProperties, |
| 3627 | mLastCookedPointerData.pointerCoords, |
| 3628 | mLastCookedPointerData.idToIndex, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3629 | moveIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3630 | if (buttonState != mLastButtonState) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3631 | moveNeeded = true; |
| 3632 | } |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3633 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3634 | // Dispatch pointer up events. |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3635 | while (!upIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3636 | uint32_t upId = upIdBits.clearFirstMarkedBit(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3637 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3638 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3639 | AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3640 | mLastCookedPointerData.pointerProperties, |
| 3641 | mLastCookedPointerData.pointerCoords, |
| 3642 | mLastCookedPointerData.idToIndex, |
| 3643 | dispatchedIdBits, upId, |
| 3644 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3645 | dispatchedIdBits.clearBit(upId); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3646 | } |
| 3647 | |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3648 | // Dispatch move events if any of the remaining pointers moved from their old locations. |
| 3649 | // Although applications receive new locations as part of individual pointer up |
| 3650 | // events, they do not generally handle them except when presented in a move event. |
| 3651 | if (moveNeeded) { |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3652 | ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3653 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3654 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3655 | mCurrentCookedPointerData.pointerProperties, |
| 3656 | mCurrentCookedPointerData.pointerCoords, |
| 3657 | mCurrentCookedPointerData.idToIndex, |
| 3658 | dispatchedIdBits, -1, |
| 3659 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3660 | } |
| 3661 | |
| 3662 | // Dispatch pointer down events using the new pointer locations. |
| 3663 | while (!downIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3664 | uint32_t downId = downIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3665 | dispatchedIdBits.markBit(downId); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3666 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3667 | if (dispatchedIdBits.count() == 1) { |
| 3668 | // First pointer is going down. Set down time. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3669 | mDownTime = when; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3670 | } |
| 3671 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3672 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | a611137 | 2011-07-14 21:48:23 -0700 | [diff] [blame] | 3673 | AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3674 | mCurrentCookedPointerData.pointerProperties, |
| 3675 | mCurrentCookedPointerData.pointerCoords, |
| 3676 | mCurrentCookedPointerData.idToIndex, |
| 3677 | dispatchedIdBits, downId, |
| 3678 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3679 | } |
| 3680 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3681 | } |
| 3682 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3683 | void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) { |
| 3684 | if (mSentHoverEnter && |
| 3685 | (mCurrentCookedPointerData.hoveringIdBits.isEmpty() |
| 3686 | || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) { |
| 3687 | int32_t metaState = getContext()->getGlobalMetaState(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3688 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3689 | AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0, |
| 3690 | mLastCookedPointerData.pointerProperties, |
| 3691 | mLastCookedPointerData.pointerCoords, |
| 3692 | mLastCookedPointerData.idToIndex, |
| 3693 | mLastCookedPointerData.hoveringIdBits, -1, |
| 3694 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 3695 | mSentHoverEnter = false; |
| 3696 | } |
| 3697 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3698 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3699 | void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) { |
| 3700 | if (mCurrentCookedPointerData.touchingIdBits.isEmpty() |
| 3701 | && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) { |
| 3702 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 3703 | if (!mSentHoverEnter) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3704 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3705 | AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0, |
| 3706 | mCurrentCookedPointerData.pointerProperties, |
| 3707 | mCurrentCookedPointerData.pointerCoords, |
| 3708 | mCurrentCookedPointerData.idToIndex, |
| 3709 | mCurrentCookedPointerData.hoveringIdBits, -1, |
| 3710 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 3711 | mSentHoverEnter = true; |
| 3712 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3713 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3714 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3715 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0, |
| 3716 | mCurrentCookedPointerData.pointerProperties, |
| 3717 | mCurrentCookedPointerData.pointerCoords, |
| 3718 | mCurrentCookedPointerData.idToIndex, |
| 3719 | mCurrentCookedPointerData.hoveringIdBits, -1, |
| 3720 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 3721 | } |
| 3722 | } |
| 3723 | |
| 3724 | void TouchInputMapper::cookPointerData() { |
| 3725 | uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount; |
| 3726 | |
| 3727 | mCurrentCookedPointerData.clear(); |
| 3728 | mCurrentCookedPointerData.pointerCount = currentPointerCount; |
| 3729 | mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits; |
| 3730 | mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits; |
| 3731 | |
| 3732 | // Walk through the the active pointers and map device coordinates onto |
| 3733 | // surface coordinates and adjust for display orientation. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3734 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3735 | const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3736 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3737 | // Size |
| 3738 | float touchMajor, touchMinor, toolMajor, toolMinor, size; |
| 3739 | switch (mCalibration.sizeCalibration) { |
| 3740 | case Calibration::SIZE_CALIBRATION_GEOMETRIC: |
| 3741 | case Calibration::SIZE_CALIBRATION_DIAMETER: |
| 3742 | case Calibration::SIZE_CALIBRATION_AREA: |
| 3743 | if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) { |
| 3744 | touchMajor = in.touchMajor; |
| 3745 | touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor; |
| 3746 | toolMajor = in.toolMajor; |
| 3747 | toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor; |
| 3748 | size = mRawPointerAxes.touchMinor.valid |
| 3749 | ? avg(in.touchMajor, in.touchMinor) : in.touchMajor; |
| 3750 | } else if (mRawPointerAxes.touchMajor.valid) { |
| 3751 | toolMajor = touchMajor = in.touchMajor; |
| 3752 | toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid |
| 3753 | ? in.touchMinor : in.touchMajor; |
| 3754 | size = mRawPointerAxes.touchMinor.valid |
| 3755 | ? avg(in.touchMajor, in.touchMinor) : in.touchMajor; |
| 3756 | } else if (mRawPointerAxes.toolMajor.valid) { |
| 3757 | touchMajor = toolMajor = in.toolMajor; |
| 3758 | touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid |
| 3759 | ? in.toolMinor : in.toolMajor; |
| 3760 | size = mRawPointerAxes.toolMinor.valid |
| 3761 | ? avg(in.toolMajor, in.toolMinor) : in.toolMajor; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3762 | } else { |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3763 | ALOG_ASSERT(false, "No touch or tool axes. " |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3764 | "Size calibration should have been resolved to NONE."); |
| 3765 | touchMajor = 0; |
| 3766 | touchMinor = 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3767 | toolMajor = 0; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3768 | toolMinor = 0; |
| 3769 | size = 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3770 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3771 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3772 | if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) { |
| 3773 | uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count(); |
| 3774 | if (touchingCount > 1) { |
| 3775 | touchMajor /= touchingCount; |
| 3776 | touchMinor /= touchingCount; |
| 3777 | toolMajor /= touchingCount; |
| 3778 | toolMinor /= touchingCount; |
| 3779 | size /= touchingCount; |
| 3780 | } |
| 3781 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3782 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3783 | if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) { |
| 3784 | touchMajor *= mGeometricScale; |
| 3785 | touchMinor *= mGeometricScale; |
| 3786 | toolMajor *= mGeometricScale; |
| 3787 | toolMinor *= mGeometricScale; |
| 3788 | } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) { |
| 3789 | touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3790 | touchMinor = touchMajor; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3791 | toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0; |
| 3792 | toolMinor = toolMajor; |
| 3793 | } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) { |
| 3794 | touchMinor = touchMajor; |
| 3795 | toolMinor = toolMajor; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3796 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3797 | |
| 3798 | mCalibration.applySizeScaleAndBias(&touchMajor); |
| 3799 | mCalibration.applySizeScaleAndBias(&touchMinor); |
| 3800 | mCalibration.applySizeScaleAndBias(&toolMajor); |
| 3801 | mCalibration.applySizeScaleAndBias(&toolMinor); |
| 3802 | size *= mSizeScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3803 | break; |
| 3804 | default: |
| 3805 | touchMajor = 0; |
| 3806 | touchMinor = 0; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3807 | toolMajor = 0; |
| 3808 | toolMinor = 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3809 | size = 0; |
| 3810 | break; |
| 3811 | } |
| 3812 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3813 | // Pressure |
| 3814 | float pressure; |
| 3815 | switch (mCalibration.pressureCalibration) { |
| 3816 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: |
| 3817 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: |
| 3818 | pressure = in.pressure * mPressureScale; |
| 3819 | break; |
| 3820 | default: |
| 3821 | pressure = in.isHovering ? 0 : 1; |
| 3822 | break; |
| 3823 | } |
| 3824 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3825 | // Tilt and Orientation |
| 3826 | float tilt; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3827 | float orientation; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3828 | if (mHaveTilt) { |
| 3829 | float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale; |
| 3830 | float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale; |
| 3831 | orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle)); |
| 3832 | tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle)); |
| 3833 | } else { |
| 3834 | tilt = 0; |
| 3835 | |
| 3836 | switch (mCalibration.orientationCalibration) { |
| 3837 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: |
| 3838 | orientation = (in.orientation - mOrientationCenter) * mOrientationScale; |
| 3839 | break; |
| 3840 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: { |
| 3841 | int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4); |
| 3842 | int32_t c2 = signExtendNybble(in.orientation & 0x0f); |
| 3843 | if (c1 != 0 || c2 != 0) { |
| 3844 | orientation = atan2f(c1, c2) * 0.5f; |
| 3845 | float confidence = hypotf(c1, c2); |
| 3846 | float scale = 1.0f + confidence / 16.0f; |
| 3847 | touchMajor *= scale; |
| 3848 | touchMinor /= scale; |
| 3849 | toolMajor *= scale; |
| 3850 | toolMinor /= scale; |
| 3851 | } else { |
| 3852 | orientation = 0; |
| 3853 | } |
| 3854 | break; |
| 3855 | } |
| 3856 | default: |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3857 | orientation = 0; |
| 3858 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3859 | } |
| 3860 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3861 | // Distance |
| 3862 | float distance; |
| 3863 | switch (mCalibration.distanceCalibration) { |
| 3864 | case Calibration::DISTANCE_CALIBRATION_SCALED: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3865 | distance = in.distance * mDistanceScale; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3866 | break; |
| 3867 | default: |
| 3868 | distance = 0; |
| 3869 | } |
| 3870 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3871 | // X and Y |
| 3872 | // Adjust coords for surface orientation. |
| 3873 | float x, y; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3874 | switch (mSurfaceOrientation) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3875 | case DISPLAY_ORIENTATION_90: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3876 | x = float(in.y - mRawPointerAxes.y.minValue) * mYScale; |
| 3877 | y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3878 | orientation -= M_PI_2; |
| 3879 | if (orientation < - M_PI_2) { |
| 3880 | orientation += M_PI; |
| 3881 | } |
| 3882 | break; |
| 3883 | case DISPLAY_ORIENTATION_180: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3884 | x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale; |
| 3885 | y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3886 | break; |
| 3887 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3888 | x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale; |
| 3889 | y = float(in.x - mRawPointerAxes.x.minValue) * mXScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3890 | orientation += M_PI_2; |
| 3891 | if (orientation > M_PI_2) { |
| 3892 | orientation -= M_PI; |
| 3893 | } |
| 3894 | break; |
| 3895 | default: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3896 | x = float(in.x - mRawPointerAxes.x.minValue) * mXScale; |
| 3897 | y = float(in.y - mRawPointerAxes.y.minValue) * mYScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3898 | break; |
| 3899 | } |
| 3900 | |
| 3901 | // Write output coords. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3902 | PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3903 | out.clear(); |
| 3904 | out.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 3905 | out.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 3906 | out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure); |
| 3907 | out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size); |
| 3908 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor); |
| 3909 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor); |
| 3910 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor); |
| 3911 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor); |
| 3912 | out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3913 | out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3914 | out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3915 | |
| 3916 | // Write output properties. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3917 | PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i]; |
| 3918 | uint32_t id = in.id; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3919 | properties.clear(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3920 | properties.id = id; |
| 3921 | properties.toolType = in.toolType; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3922 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3923 | // Write id index. |
| 3924 | mCurrentCookedPointerData.idToIndex[id] = i; |
| 3925 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3926 | } |
| 3927 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3928 | void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, |
| 3929 | PointerUsage pointerUsage) { |
| 3930 | if (pointerUsage != mPointerUsage) { |
| 3931 | abortPointerUsage(when, policyFlags); |
| 3932 | mPointerUsage = pointerUsage; |
| 3933 | } |
| 3934 | |
| 3935 | switch (mPointerUsage) { |
| 3936 | case POINTER_USAGE_GESTURES: |
| 3937 | dispatchPointerGestures(when, policyFlags, false /*isTimeout*/); |
| 3938 | break; |
| 3939 | case POINTER_USAGE_STYLUS: |
| 3940 | dispatchPointerStylus(when, policyFlags); |
| 3941 | break; |
| 3942 | case POINTER_USAGE_MOUSE: |
| 3943 | dispatchPointerMouse(when, policyFlags); |
| 3944 | break; |
| 3945 | default: |
| 3946 | break; |
| 3947 | } |
| 3948 | } |
| 3949 | |
| 3950 | void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) { |
| 3951 | switch (mPointerUsage) { |
| 3952 | case POINTER_USAGE_GESTURES: |
| 3953 | abortPointerGestures(when, policyFlags); |
| 3954 | break; |
| 3955 | case POINTER_USAGE_STYLUS: |
| 3956 | abortPointerStylus(when, policyFlags); |
| 3957 | break; |
| 3958 | case POINTER_USAGE_MOUSE: |
| 3959 | abortPointerMouse(when, policyFlags); |
| 3960 | break; |
| 3961 | default: |
| 3962 | break; |
| 3963 | } |
| 3964 | |
| 3965 | mPointerUsage = POINTER_USAGE_NONE; |
| 3966 | } |
| 3967 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3968 | void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, |
| 3969 | bool isTimeout) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3970 | // Update current gesture coordinates. |
| 3971 | bool cancelPreviousGesture, finishPreviousGesture; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3972 | bool sendEvents = preparePointerGestures(when, |
| 3973 | &cancelPreviousGesture, &finishPreviousGesture, isTimeout); |
| 3974 | if (!sendEvents) { |
| 3975 | return; |
| 3976 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 3977 | if (finishPreviousGesture) { |
| 3978 | cancelPreviousGesture = false; |
| 3979 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3980 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 3981 | // Update the pointer presentation and spots. |
| 3982 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 3983 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT); |
| 3984 | if (finishPreviousGesture || cancelPreviousGesture) { |
| 3985 | mPointerController->clearSpots(); |
| 3986 | } |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 3987 | mPointerController->setSpots(mPointerGesture.currentGestureCoords, |
| 3988 | mPointerGesture.currentGestureIdToIndex, |
| 3989 | mPointerGesture.currentGestureIdBits); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 3990 | } else { |
| 3991 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER); |
| 3992 | } |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 3993 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 3994 | // Show or hide the pointer if needed. |
| 3995 | switch (mPointerGesture.currentGestureMode) { |
| 3996 | case PointerGesture::NEUTRAL: |
| 3997 | case PointerGesture::QUIET: |
| 3998 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS |
| 3999 | && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE |
| 4000 | || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) { |
| 4001 | // Remind the user of where the pointer is after finishing a gesture with spots. |
| 4002 | mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 4003 | } |
| 4004 | break; |
| 4005 | case PointerGesture::TAP: |
| 4006 | case PointerGesture::TAP_DRAG: |
| 4007 | case PointerGesture::BUTTON_CLICK_OR_DRAG: |
| 4008 | case PointerGesture::HOVER: |
| 4009 | case PointerGesture::PRESS: |
| 4010 | // Unfade the pointer when the current gesture manipulates the |
| 4011 | // area directly under the pointer. |
| 4012 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 4013 | break; |
| 4014 | case PointerGesture::SWIPE: |
| 4015 | case PointerGesture::FREEFORM: |
| 4016 | // Fade the pointer when the current gesture manipulates a different |
| 4017 | // area and there are spots to guide the user experience. |
| 4018 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 4019 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 4020 | } else { |
| 4021 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 4022 | } |
| 4023 | break; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4024 | } |
| 4025 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4026 | // Send events! |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4027 | int32_t metaState = getContext()->getGlobalMetaState(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4028 | int32_t buttonState = mCurrentButtonState; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4029 | |
| 4030 | // Update last coordinates of pointers that have moved so that we observe the new |
| 4031 | // 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] | 4032 | bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP |
| 4033 | || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG |
| 4034 | || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4035 | || mPointerGesture.currentGestureMode == PointerGesture::PRESS |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4036 | || mPointerGesture.currentGestureMode == PointerGesture::SWIPE |
| 4037 | || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM; |
| 4038 | bool moveNeeded = false; |
| 4039 | if (down && !cancelPreviousGesture && !finishPreviousGesture |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4040 | && !mPointerGesture.lastGestureIdBits.isEmpty() |
| 4041 | && !mPointerGesture.currentGestureIdBits.isEmpty()) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4042 | BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value |
| 4043 | & mPointerGesture.lastGestureIdBits.value); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4044 | moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4045 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4046 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4047 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4048 | movedGestureIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4049 | if (buttonState != mLastButtonState) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4050 | moveNeeded = true; |
| 4051 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4052 | } |
| 4053 | |
| 4054 | // Send motion events for all pointers that went up or were canceled. |
| 4055 | BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits); |
| 4056 | if (!dispatchedGestureIdBits.isEmpty()) { |
| 4057 | if (cancelPreviousGesture) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4058 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4059 | AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState, |
| 4060 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4061 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4062 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4063 | dispatchedGestureIdBits, -1, |
| 4064 | 0, 0, mPointerGesture.downTime); |
| 4065 | |
| 4066 | dispatchedGestureIdBits.clear(); |
| 4067 | } else { |
| 4068 | BitSet32 upGestureIdBits; |
| 4069 | if (finishPreviousGesture) { |
| 4070 | upGestureIdBits = dispatchedGestureIdBits; |
| 4071 | } else { |
| 4072 | upGestureIdBits.value = dispatchedGestureIdBits.value |
| 4073 | & ~mPointerGesture.currentGestureIdBits.value; |
| 4074 | } |
| 4075 | while (!upGestureIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4076 | uint32_t id = upGestureIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4077 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4078 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4079 | AMOTION_EVENT_ACTION_POINTER_UP, 0, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4080 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4081 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4082 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4083 | dispatchedGestureIdBits, id, |
| 4084 | 0, 0, mPointerGesture.downTime); |
| 4085 | |
| 4086 | dispatchedGestureIdBits.clearBit(id); |
| 4087 | } |
| 4088 | } |
| 4089 | } |
| 4090 | |
| 4091 | // Send motion events for all pointers that moved. |
| 4092 | if (moveNeeded) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4093 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4094 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4095 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4096 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 4097 | dispatchedGestureIdBits, -1, |
| 4098 | 0, 0, mPointerGesture.downTime); |
| 4099 | } |
| 4100 | |
| 4101 | // Send motion events for all pointers that went down. |
| 4102 | if (down) { |
| 4103 | BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value |
| 4104 | & ~dispatchedGestureIdBits.value); |
| 4105 | while (!downGestureIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4106 | uint32_t id = downGestureIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4107 | dispatchedGestureIdBits.markBit(id); |
| 4108 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4109 | if (dispatchedGestureIdBits.count() == 1) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4110 | mPointerGesture.downTime = when; |
| 4111 | } |
| 4112 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4113 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | a611137 | 2011-07-14 21:48:23 -0700 | [diff] [blame] | 4114 | AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4115 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4116 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 4117 | dispatchedGestureIdBits, id, |
| 4118 | 0, 0, mPointerGesture.downTime); |
| 4119 | } |
| 4120 | } |
| 4121 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4122 | // Send motion events for hover. |
| 4123 | if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4124 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4125 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 4126 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4127 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4128 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 4129 | mPointerGesture.currentGestureIdBits, -1, |
| 4130 | 0, 0, mPointerGesture.downTime); |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4131 | } else if (dispatchedGestureIdBits.isEmpty() |
| 4132 | && !mPointerGesture.lastGestureIdBits.isEmpty()) { |
| 4133 | // Synthesize a hover move event after all pointers go up to indicate that |
| 4134 | // the pointer is hovering again even if the user is not currently touching |
| 4135 | // the touch pad. This ensures that a view will receive a fresh hover enter |
| 4136 | // event after a tap. |
| 4137 | float x, y; |
| 4138 | mPointerController->getPosition(&x, &y); |
| 4139 | |
| 4140 | PointerProperties pointerProperties; |
| 4141 | pointerProperties.clear(); |
| 4142 | pointerProperties.id = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4143 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4144 | |
| 4145 | PointerCoords pointerCoords; |
| 4146 | pointerCoords.clear(); |
| 4147 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4148 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 4149 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4150 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4151 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 4152 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4153 | 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4154 | getListener()->notifyMotion(&args); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4155 | } |
| 4156 | |
| 4157 | // Update state. |
| 4158 | mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode; |
| 4159 | if (!down) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4160 | mPointerGesture.lastGestureIdBits.clear(); |
| 4161 | } else { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4162 | mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits; |
| 4163 | for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4164 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4165 | uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4166 | mPointerGesture.lastGestureProperties[index].copyFrom( |
| 4167 | mPointerGesture.currentGestureProperties[index]); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4168 | mPointerGesture.lastGestureCoords[index].copyFrom( |
| 4169 | mPointerGesture.currentGestureCoords[index]); |
| 4170 | mPointerGesture.lastGestureIdToIndex[id] = index; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4171 | } |
| 4172 | } |
| 4173 | } |
| 4174 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4175 | void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) { |
| 4176 | // Cancel previously dispatches pointers. |
| 4177 | if (!mPointerGesture.lastGestureIdBits.isEmpty()) { |
| 4178 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 4179 | int32_t buttonState = mCurrentButtonState; |
| 4180 | dispatchMotion(when, policyFlags, mSource, |
| 4181 | AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState, |
| 4182 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4183 | mPointerGesture.lastGestureProperties, |
| 4184 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4185 | mPointerGesture.lastGestureIdBits, -1, |
| 4186 | 0, 0, mPointerGesture.downTime); |
| 4187 | } |
| 4188 | |
| 4189 | // Reset the current pointer gesture. |
| 4190 | mPointerGesture.reset(); |
| 4191 | mPointerVelocityControl.reset(); |
| 4192 | |
| 4193 | // Remove any current spots. |
| 4194 | if (mPointerController != NULL) { |
| 4195 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 4196 | mPointerController->clearSpots(); |
| 4197 | } |
| 4198 | } |
| 4199 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4200 | bool TouchInputMapper::preparePointerGestures(nsecs_t when, |
| 4201 | bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4202 | *outCancelPreviousGesture = false; |
| 4203 | *outFinishPreviousGesture = false; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4204 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4205 | // Handle TAP timeout. |
| 4206 | if (isTimeout) { |
| 4207 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4208 | ALOGD("Gestures: Processing timeout"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4209 | #endif |
| 4210 | |
| 4211 | if (mPointerGesture.lastGestureMode == PointerGesture::TAP) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4212 | if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4213 | // The tap/drag timeout has not yet expired. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4214 | getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4215 | + mConfig.pointerGestureTapDragInterval); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4216 | } else { |
| 4217 | // The tap is finished. |
| 4218 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4219 | ALOGD("Gestures: TAP finished"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4220 | #endif |
| 4221 | *outFinishPreviousGesture = true; |
| 4222 | |
| 4223 | mPointerGesture.activeGestureId = -1; |
| 4224 | mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL; |
| 4225 | mPointerGesture.currentGestureIdBits.clear(); |
| 4226 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4227 | mPointerVelocityControl.reset(); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4228 | return true; |
| 4229 | } |
| 4230 | } |
| 4231 | |
| 4232 | // We did not handle this timeout. |
| 4233 | return false; |
| 4234 | } |
| 4235 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4236 | const uint32_t currentFingerCount = mCurrentFingerIdBits.count(); |
| 4237 | const uint32_t lastFingerCount = mLastFingerIdBits.count(); |
| 4238 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4239 | // Update the velocity tracker. |
| 4240 | { |
| 4241 | VelocityTracker::Position positions[MAX_POINTERS]; |
| 4242 | uint32_t count = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4243 | for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4244 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 4245 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4246 | positions[count].x = pointer.x * mPointerXMovementScale; |
| 4247 | positions[count].y = pointer.y * mPointerYMovementScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4248 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4249 | mPointerGesture.velocityTracker.addMovement(when, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4250 | mCurrentFingerIdBits, positions); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4251 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4252 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4253 | // Pick a new active touch id if needed. |
| 4254 | // Choose an arbitrary pointer that just went down, if there is one. |
| 4255 | // Otherwise choose an arbitrary remaining pointer. |
| 4256 | // 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] | 4257 | // We keep the same active touch id for as long as possible. |
| 4258 | bool activeTouchChanged = false; |
| 4259 | int32_t lastActiveTouchId = mPointerGesture.activeTouchId; |
| 4260 | int32_t activeTouchId = lastActiveTouchId; |
| 4261 | if (activeTouchId < 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4262 | if (!mCurrentFingerIdBits.isEmpty()) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4263 | activeTouchChanged = true; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4264 | activeTouchId = mPointerGesture.activeTouchId = |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4265 | mCurrentFingerIdBits.firstMarkedBit(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4266 | mPointerGesture.firstTouchTime = when; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4267 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4268 | } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4269 | activeTouchChanged = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4270 | if (!mCurrentFingerIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4271 | activeTouchId = mPointerGesture.activeTouchId = |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4272 | mCurrentFingerIdBits.firstMarkedBit(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4273 | } else { |
| 4274 | activeTouchId = mPointerGesture.activeTouchId = -1; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4275 | } |
| 4276 | } |
| 4277 | |
| 4278 | // Determine whether we are in quiet time. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4279 | bool isQuietTime = false; |
| 4280 | if (activeTouchId < 0) { |
| 4281 | mPointerGesture.resetQuietTime(); |
| 4282 | } else { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4283 | isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4284 | if (!isQuietTime) { |
| 4285 | if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS |
| 4286 | || mPointerGesture.lastGestureMode == PointerGesture::SWIPE |
| 4287 | || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4288 | && currentFingerCount < 2) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4289 | // Enter quiet time when exiting swipe or freeform state. |
| 4290 | // This is to prevent accidentally entering the hover state and flinging the |
| 4291 | // pointer when finishing a swipe and there is still one pointer left onscreen. |
| 4292 | isQuietTime = true; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4293 | } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4294 | && currentFingerCount >= 2 |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4295 | && !isPointerDown(mCurrentButtonState)) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4296 | // Enter quiet time when releasing the button and there are still two or more |
| 4297 | // fingers down. This may indicate that one finger was used to press the button |
| 4298 | // but it has not gone up yet. |
| 4299 | isQuietTime = true; |
| 4300 | } |
| 4301 | if (isQuietTime) { |
| 4302 | mPointerGesture.quietTime = when; |
| 4303 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4304 | } |
| 4305 | } |
| 4306 | |
| 4307 | // Switch states based on button and pointer state. |
| 4308 | if (isQuietTime) { |
| 4309 | // Case 1: Quiet time. (QUIET) |
| 4310 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4311 | ALOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4312 | + mConfig.pointerGestureQuietInterval - when) * 0.000001f); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4313 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4314 | if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) { |
| 4315 | *outFinishPreviousGesture = true; |
| 4316 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4317 | |
| 4318 | mPointerGesture.activeGestureId = -1; |
| 4319 | mPointerGesture.currentGestureMode = PointerGesture::QUIET; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4320 | mPointerGesture.currentGestureIdBits.clear(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4321 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4322 | mPointerVelocityControl.reset(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4323 | } else if (isPointerDown(mCurrentButtonState)) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4324 | // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG) |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4325 | // The pointer follows the active touch point. |
| 4326 | // Emit DOWN, MOVE, UP events at the pointer location. |
| 4327 | // |
| 4328 | // Only the active touch matters; other fingers are ignored. This policy helps |
| 4329 | // to handle the case where the user places a second finger on the touch pad |
| 4330 | // to apply the necessary force to depress an integrated button below the surface. |
| 4331 | // We don't want the second finger to be delivered to applications. |
| 4332 | // |
| 4333 | // For this to work well, we need to make sure to track the pointer that is really |
| 4334 | // active. If the user first puts one finger down to click then adds another |
| 4335 | // finger to drag then the active pointer should switch to the finger that is |
| 4336 | // being dragged. |
| 4337 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4338 | ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4339 | "currentFingerCount=%d", activeTouchId, currentFingerCount); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4340 | #endif |
| 4341 | // Reset state when just starting. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4342 | if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4343 | *outFinishPreviousGesture = true; |
| 4344 | mPointerGesture.activeGestureId = 0; |
| 4345 | } |
| 4346 | |
| 4347 | // Switch pointers if needed. |
| 4348 | // Find the fastest pointer and follow it. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4349 | if (activeTouchId >= 0 && currentFingerCount > 1) { |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4350 | int32_t bestId = -1; |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4351 | float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4352 | for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4353 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4354 | float vx, vy; |
| 4355 | if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) { |
| 4356 | float speed = hypotf(vx, vy); |
| 4357 | if (speed > bestSpeed) { |
| 4358 | bestId = id; |
| 4359 | bestSpeed = speed; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4360 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 4361 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4362 | } |
| 4363 | if (bestId >= 0 && bestId != activeTouchId) { |
| 4364 | mPointerGesture.activeTouchId = activeTouchId = bestId; |
| 4365 | activeTouchChanged = true; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4366 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4367 | ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, " |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4368 | "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4369 | #endif |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4370 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4371 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4372 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4373 | if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4374 | const RawPointerData::Pointer& currentPointer = |
| 4375 | mCurrentRawPointerData.pointerForId(activeTouchId); |
| 4376 | const RawPointerData::Pointer& lastPointer = |
| 4377 | mLastRawPointerData.pointerForId(activeTouchId); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4378 | float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale; |
| 4379 | float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4380 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4381 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4382 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4383 | |
| 4384 | // Move the pointer using a relative motion. |
| 4385 | // When using spots, the click will occur at the position of the anchor |
| 4386 | // spot and all other spots will move there. |
| 4387 | mPointerController->move(deltaX, deltaY); |
| 4388 | } else { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4389 | mPointerVelocityControl.reset(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4390 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4391 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4392 | float x, y; |
| 4393 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 4394 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4395 | mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4396 | mPointerGesture.currentGestureIdBits.clear(); |
| 4397 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4398 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4399 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4400 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4401 | mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4402 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4403 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4404 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 4405 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4406 | } else if (currentFingerCount == 0) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4407 | // Case 3. No fingers down and button is not pressed. (NEUTRAL) |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4408 | if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) { |
| 4409 | *outFinishPreviousGesture = true; |
| 4410 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4411 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4412 | // Watch for taps coming out of HOVER or TAP_DRAG mode. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4413 | // Checking for taps after TAP_DRAG allows us to detect double-taps. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4414 | bool tapped = false; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4415 | if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER |
| 4416 | || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4417 | && lastFingerCount == 1) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4418 | if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4419 | float x, y; |
| 4420 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4421 | if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop |
| 4422 | && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4423 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4424 | ALOGD("Gestures: TAP"); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4425 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4426 | |
| 4427 | mPointerGesture.tapUpTime = when; |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4428 | getContext()->requestTimeoutAtTime(when |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4429 | + mConfig.pointerGestureTapDragInterval); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4430 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4431 | mPointerGesture.activeGestureId = 0; |
| 4432 | mPointerGesture.currentGestureMode = PointerGesture::TAP; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4433 | mPointerGesture.currentGestureIdBits.clear(); |
| 4434 | mPointerGesture.currentGestureIdBits.markBit( |
| 4435 | mPointerGesture.activeGestureId); |
| 4436 | mPointerGesture.currentGestureIdToIndex[ |
| 4437 | mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4438 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4439 | mPointerGesture.currentGestureProperties[0].id = |
| 4440 | mPointerGesture.activeGestureId; |
| 4441 | mPointerGesture.currentGestureProperties[0].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4442 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4443 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4444 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4445 | AMOTION_EVENT_AXIS_X, mPointerGesture.tapX); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4446 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4447 | AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4448 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
| 4449 | AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4450 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4451 | tapped = true; |
| 4452 | } else { |
| 4453 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4454 | ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f", |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4455 | x - mPointerGesture.tapX, |
| 4456 | y - mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4457 | #endif |
| 4458 | } |
| 4459 | } else { |
| 4460 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4461 | ALOGD("Gestures: Not a TAP, %0.3fms since down", |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4462 | (when - mPointerGesture.tapDownTime) * 0.000001f); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4463 | #endif |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4464 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4465 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4466 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4467 | mPointerVelocityControl.reset(); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4468 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4469 | if (!tapped) { |
| 4470 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4471 | ALOGD("Gestures: NEUTRAL"); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4472 | #endif |
| 4473 | mPointerGesture.activeGestureId = -1; |
| 4474 | mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4475 | mPointerGesture.currentGestureIdBits.clear(); |
| 4476 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4477 | } else if (currentFingerCount == 1) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4478 | // 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] | 4479 | // The pointer follows the active touch point. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4480 | // When in HOVER, emit HOVER_MOVE events at the pointer location. |
| 4481 | // When in TAP_DRAG, emit MOVE events at the pointer location. |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 4482 | ALOG_ASSERT(activeTouchId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4483 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4484 | mPointerGesture.currentGestureMode = PointerGesture::HOVER; |
| 4485 | if (mPointerGesture.lastGestureMode == PointerGesture::TAP) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4486 | if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4487 | float x, y; |
| 4488 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4489 | if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop |
| 4490 | && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4491 | mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG; |
| 4492 | } else { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4493 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4494 | ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f", |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4495 | x - mPointerGesture.tapX, |
| 4496 | y - mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4497 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4498 | } |
| 4499 | } else { |
| 4500 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4501 | ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up", |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4502 | (when - mPointerGesture.tapUpTime) * 0.000001f); |
| 4503 | #endif |
| 4504 | } |
| 4505 | } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) { |
| 4506 | mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG; |
| 4507 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4508 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4509 | if (mLastFingerIdBits.hasBit(activeTouchId)) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4510 | const RawPointerData::Pointer& currentPointer = |
| 4511 | mCurrentRawPointerData.pointerForId(activeTouchId); |
| 4512 | const RawPointerData::Pointer& lastPointer = |
| 4513 | mLastRawPointerData.pointerForId(activeTouchId); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4514 | float deltaX = (currentPointer.x - lastPointer.x) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4515 | * mPointerXMovementScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4516 | float deltaY = (currentPointer.y - lastPointer.y) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4517 | * mPointerYMovementScale; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4518 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4519 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4520 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4521 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4522 | // Move the pointer using a relative motion. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4523 | // 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] | 4524 | mPointerController->move(deltaX, deltaY); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4525 | } else { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4526 | mPointerVelocityControl.reset(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4527 | } |
| 4528 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4529 | bool down; |
| 4530 | if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) { |
| 4531 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4532 | ALOGD("Gestures: TAP_DRAG"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4533 | #endif |
| 4534 | down = true; |
| 4535 | } else { |
| 4536 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4537 | ALOGD("Gestures: HOVER"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4538 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4539 | if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) { |
| 4540 | *outFinishPreviousGesture = true; |
| 4541 | } |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4542 | mPointerGesture.activeGestureId = 0; |
| 4543 | down = false; |
| 4544 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4545 | |
| 4546 | float x, y; |
| 4547 | mPointerController->getPosition(&x, &y); |
| 4548 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4549 | mPointerGesture.currentGestureIdBits.clear(); |
| 4550 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4551 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4552 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4553 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
| 4554 | mPointerGesture.currentGestureProperties[0].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4555 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4556 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4557 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4558 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4559 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, |
| 4560 | down ? 1.0f : 0.0f); |
| 4561 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4562 | if (lastFingerCount == 0 && currentFingerCount != 0) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4563 | mPointerGesture.resetTap(); |
| 4564 | mPointerGesture.tapDownTime = when; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4565 | mPointerGesture.tapX = x; |
| 4566 | mPointerGesture.tapY = y; |
| 4567 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4568 | } else { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4569 | // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM) |
| 4570 | // We need to provide feedback for each finger that goes down so we cannot wait |
| 4571 | // for the fingers to move before deciding what to do. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4572 | // |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4573 | // The ambiguous case is deciding what to do when there are two fingers down but they |
| 4574 | // have not moved enough to determine whether they are part of a drag or part of a |
| 4575 | // freeform gesture, or just a press or long-press at the pointer location. |
| 4576 | // |
| 4577 | // When there are two fingers we start with the PRESS hypothesis and we generate a |
| 4578 | // down at the pointer location. |
| 4579 | // |
| 4580 | // When the two fingers move enough or when additional fingers are added, we make |
| 4581 | // a decision to transition into SWIPE or FREEFORM mode accordingly. |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 4582 | ALOG_ASSERT(activeTouchId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4583 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4584 | bool settled = when >= mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4585 | + mConfig.pointerGestureMultitouchSettleInterval; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4586 | if (mPointerGesture.lastGestureMode != PointerGesture::PRESS |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4587 | && mPointerGesture.lastGestureMode != PointerGesture::SWIPE |
| 4588 | && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4589 | *outFinishPreviousGesture = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4590 | } else if (!settled && currentFingerCount > lastFingerCount) { |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4591 | // Additional pointers have gone down but not yet settled. |
| 4592 | // Reset the gesture. |
| 4593 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4594 | ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, " |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4595 | "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4596 | + mConfig.pointerGestureMultitouchSettleInterval - when) |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4597 | * 0.000001f); |
| 4598 | #endif |
| 4599 | *outCancelPreviousGesture = true; |
| 4600 | } else { |
| 4601 | // Continue previous gesture. |
| 4602 | mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode; |
| 4603 | } |
| 4604 | |
| 4605 | if (*outFinishPreviousGesture || *outCancelPreviousGesture) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4606 | mPointerGesture.currentGestureMode = PointerGesture::PRESS; |
| 4607 | mPointerGesture.activeGestureId = 0; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4608 | mPointerGesture.referenceIdBits.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4609 | mPointerVelocityControl.reset(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4610 | |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4611 | // 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] | 4612 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4613 | ALOGD("Gestures: Using centroid as reference for MULTITOUCH, " |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4614 | "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4615 | + mConfig.pointerGestureMultitouchSettleInterval - when) |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4616 | * 0.000001f); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4617 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4618 | mCurrentRawPointerData.getCentroidOfTouchingPointers( |
| 4619 | &mPointerGesture.referenceTouchX, |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4620 | &mPointerGesture.referenceTouchY); |
| 4621 | mPointerController->getPosition(&mPointerGesture.referenceGestureX, |
| 4622 | &mPointerGesture.referenceGestureY); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4623 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4624 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4625 | // Clear the reference deltas for fingers not yet included in the reference calculation. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4626 | for (BitSet32 idBits(mCurrentFingerIdBits.value |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4627 | & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) { |
| 4628 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4629 | mPointerGesture.referenceDeltas[id].dx = 0; |
| 4630 | mPointerGesture.referenceDeltas[id].dy = 0; |
| 4631 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4632 | mPointerGesture.referenceIdBits = mCurrentFingerIdBits; |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4633 | |
| 4634 | // Add delta for all fingers and calculate a common movement delta. |
| 4635 | float commonDeltaX = 0, commonDeltaY = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4636 | BitSet32 commonIdBits(mLastFingerIdBits.value |
| 4637 | & mCurrentFingerIdBits.value); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4638 | for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) { |
| 4639 | bool first = (idBits == commonIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4640 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 4641 | const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id); |
| 4642 | const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4643 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
| 4644 | delta.dx += cpd.x - lpd.x; |
| 4645 | delta.dy += cpd.y - lpd.y; |
| 4646 | |
| 4647 | if (first) { |
| 4648 | commonDeltaX = delta.dx; |
| 4649 | commonDeltaY = delta.dy; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4650 | } else { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4651 | commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx); |
| 4652 | commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy); |
| 4653 | } |
| 4654 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4655 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4656 | // Consider transitions from PRESS to SWIPE or MULTITOUCH. |
| 4657 | if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) { |
| 4658 | float dist[MAX_POINTER_ID + 1]; |
| 4659 | int32_t distOverThreshold = 0; |
| 4660 | for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4661 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4662 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4663 | dist[id] = hypotf(delta.dx * mPointerXZoomScale, |
| 4664 | delta.dy * mPointerYZoomScale); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4665 | if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4666 | distOverThreshold += 1; |
| 4667 | } |
| 4668 | } |
| 4669 | |
| 4670 | // Only transition when at least two pointers have moved further than |
| 4671 | // the minimum distance threshold. |
| 4672 | if (distOverThreshold >= 2) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4673 | if (currentFingerCount > 2) { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4674 | // There are more than two pointers, switch to FREEFORM. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4675 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4676 | ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4677 | currentFingerCount); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4678 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4679 | *outCancelPreviousGesture = true; |
| 4680 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 4681 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4682 | // There are exactly two pointers. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4683 | BitSet32 idBits(mCurrentFingerIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4684 | uint32_t id1 = idBits.clearFirstMarkedBit(); |
| 4685 | uint32_t id2 = idBits.firstMarkedBit(); |
| 4686 | const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1); |
| 4687 | const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2); |
| 4688 | float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y); |
| 4689 | if (mutualDistance > mPointerGestureMaxSwipeWidth) { |
| 4690 | // There are two pointers but they are too far apart for a SWIPE, |
| 4691 | // switch to FREEFORM. |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4692 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4693 | ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4694 | mutualDistance, mPointerGestureMaxSwipeWidth); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4695 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4696 | *outCancelPreviousGesture = true; |
| 4697 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 4698 | } else { |
| 4699 | // There are two pointers. Wait for both pointers to start moving |
| 4700 | // before deciding whether this is a SWIPE or FREEFORM gesture. |
| 4701 | float dist1 = dist[id1]; |
| 4702 | float dist2 = dist[id2]; |
| 4703 | if (dist1 >= mConfig.pointerGestureMultitouchMinDistance |
| 4704 | && dist2 >= mConfig.pointerGestureMultitouchMinDistance) { |
| 4705 | // Calculate the dot product of the displacement vectors. |
| 4706 | // When the vectors are oriented in approximately the same direction, |
| 4707 | // the angle betweeen them is near zero and the cosine of the angle |
| 4708 | // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2). |
| 4709 | PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1]; |
| 4710 | PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4711 | float dx1 = delta1.dx * mPointerXZoomScale; |
| 4712 | float dy1 = delta1.dy * mPointerYZoomScale; |
| 4713 | float dx2 = delta2.dx * mPointerXZoomScale; |
| 4714 | float dy2 = delta2.dy * mPointerYZoomScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4715 | float dot = dx1 * dx2 + dy1 * dy2; |
| 4716 | float cosine = dot / (dist1 * dist2); // denominator always > 0 |
| 4717 | if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) { |
| 4718 | // Pointers are moving in the same direction. Switch to SWIPE. |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4719 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4720 | ALOGD("Gestures: PRESS transitioned to SWIPE, " |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4721 | "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, " |
| 4722 | "cosine %0.3f >= %0.3f", |
| 4723 | dist1, mConfig.pointerGestureMultitouchMinDistance, |
| 4724 | dist2, mConfig.pointerGestureMultitouchMinDistance, |
| 4725 | cosine, mConfig.pointerGestureSwipeTransitionAngleCosine); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4726 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4727 | mPointerGesture.currentGestureMode = PointerGesture::SWIPE; |
| 4728 | } else { |
| 4729 | // Pointers are moving in different directions. Switch to FREEFORM. |
| 4730 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4731 | ALOGD("Gestures: PRESS transitioned to FREEFORM, " |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4732 | "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, " |
| 4733 | "cosine %0.3f < %0.3f", |
| 4734 | dist1, mConfig.pointerGestureMultitouchMinDistance, |
| 4735 | dist2, mConfig.pointerGestureMultitouchMinDistance, |
| 4736 | cosine, mConfig.pointerGestureSwipeTransitionAngleCosine); |
| 4737 | #endif |
| 4738 | *outCancelPreviousGesture = true; |
| 4739 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 4740 | } |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4741 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4742 | } |
| 4743 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4744 | } |
| 4745 | } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4746 | // Switch from SWIPE to FREEFORM if additional pointers go down. |
| 4747 | // Cancel previous gesture. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4748 | if (currentFingerCount > 2) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4749 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4750 | ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4751 | currentFingerCount); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4752 | #endif |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4753 | *outCancelPreviousGesture = true; |
| 4754 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4755 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4756 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4757 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4758 | // Move the reference points based on the overall group motion of the fingers |
| 4759 | // except in PRESS mode while waiting for a transition to occur. |
| 4760 | if (mPointerGesture.currentGestureMode != PointerGesture::PRESS |
| 4761 | && (commonDeltaX || commonDeltaY)) { |
| 4762 | for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4763 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4764 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4765 | delta.dx = 0; |
| 4766 | delta.dy = 0; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4767 | } |
| 4768 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4769 | mPointerGesture.referenceTouchX += commonDeltaX; |
| 4770 | mPointerGesture.referenceTouchY += commonDeltaY; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4771 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4772 | commonDeltaX *= mPointerXMovementScale; |
| 4773 | commonDeltaY *= mPointerYMovementScale; |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 4774 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4775 | rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4776 | mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4777 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4778 | mPointerGesture.referenceGestureX += commonDeltaX; |
| 4779 | mPointerGesture.referenceGestureY += commonDeltaY; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4780 | } |
| 4781 | |
| 4782 | // Report gestures. |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 4783 | if (mPointerGesture.currentGestureMode == PointerGesture::PRESS |
| 4784 | || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) { |
| 4785 | // PRESS or SWIPE mode. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4786 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4787 | ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d," |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4788 | "activeGestureId=%d, currentTouchPointerCount=%d", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4789 | activeTouchId, mPointerGesture.activeGestureId, currentFingerCount); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4790 | #endif |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 4791 | ALOG_ASSERT(mPointerGesture.activeGestureId >= 0); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4792 | |
| 4793 | mPointerGesture.currentGestureIdBits.clear(); |
| 4794 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4795 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4796 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4797 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
| 4798 | mPointerGesture.currentGestureProperties[0].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4799 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4800 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4801 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 4802 | mPointerGesture.referenceGestureX); |
| 4803 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, |
| 4804 | mPointerGesture.referenceGestureY); |
| 4805 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4806 | } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) { |
| 4807 | // FREEFORM mode. |
| 4808 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4809 | ALOGD("Gestures: FREEFORM activeTouchId=%d," |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4810 | "activeGestureId=%d, currentTouchPointerCount=%d", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4811 | activeTouchId, mPointerGesture.activeGestureId, currentFingerCount); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4812 | #endif |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 4813 | ALOG_ASSERT(mPointerGesture.activeGestureId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4814 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4815 | mPointerGesture.currentGestureIdBits.clear(); |
| 4816 | |
| 4817 | BitSet32 mappedTouchIdBits; |
| 4818 | BitSet32 usedGestureIdBits; |
| 4819 | if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) { |
| 4820 | // Initially, assign the active gesture id to the active touch point |
| 4821 | // if there is one. No other touch id bits are mapped yet. |
| 4822 | if (!*outCancelPreviousGesture) { |
| 4823 | mappedTouchIdBits.markBit(activeTouchId); |
| 4824 | usedGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4825 | mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] = |
| 4826 | mPointerGesture.activeGestureId; |
| 4827 | } else { |
| 4828 | mPointerGesture.activeGestureId = -1; |
| 4829 | } |
| 4830 | } else { |
| 4831 | // Otherwise, assume we mapped all touches from the previous frame. |
| 4832 | // Reuse all mappings that are still applicable. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4833 | mappedTouchIdBits.value = mLastFingerIdBits.value |
| 4834 | & mCurrentFingerIdBits.value; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4835 | usedGestureIdBits = mPointerGesture.lastGestureIdBits; |
| 4836 | |
| 4837 | // Check whether we need to choose a new active gesture id because the |
| 4838 | // current went went up. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4839 | for (BitSet32 upTouchIdBits(mLastFingerIdBits.value |
| 4840 | & ~mCurrentFingerIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4841 | !upTouchIdBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4842 | uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4843 | uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId]; |
| 4844 | if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) { |
| 4845 | mPointerGesture.activeGestureId = -1; |
| 4846 | break; |
| 4847 | } |
| 4848 | } |
| 4849 | } |
| 4850 | |
| 4851 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4852 | ALOGD("Gestures: FREEFORM follow up " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4853 | "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, " |
| 4854 | "activeGestureId=%d", |
| 4855 | mappedTouchIdBits.value, usedGestureIdBits.value, |
| 4856 | mPointerGesture.activeGestureId); |
| 4857 | #endif |
| 4858 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4859 | BitSet32 idBits(mCurrentFingerIdBits); |
| 4860 | for (uint32_t i = 0; i < currentFingerCount; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4861 | uint32_t touchId = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4862 | uint32_t gestureId; |
| 4863 | if (!mappedTouchIdBits.hasBit(touchId)) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4864 | gestureId = usedGestureIdBits.markFirstUnmarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4865 | mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId; |
| 4866 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4867 | ALOGD("Gestures: FREEFORM " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4868 | "new mapping for touch id %d -> gesture id %d", |
| 4869 | touchId, gestureId); |
| 4870 | #endif |
| 4871 | } else { |
| 4872 | gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId]; |
| 4873 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4874 | ALOGD("Gestures: FREEFORM " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4875 | "existing mapping for touch id %d -> gesture id %d", |
| 4876 | touchId, gestureId); |
| 4877 | #endif |
| 4878 | } |
| 4879 | mPointerGesture.currentGestureIdBits.markBit(gestureId); |
| 4880 | mPointerGesture.currentGestureIdToIndex[gestureId] = i; |
| 4881 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4882 | const RawPointerData::Pointer& pointer = |
| 4883 | mCurrentRawPointerData.pointerForId(touchId); |
| 4884 | float deltaX = (pointer.x - mPointerGesture.referenceTouchX) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4885 | * mPointerXZoomScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4886 | float deltaY = (pointer.y - mPointerGesture.referenceTouchY) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4887 | * mPointerYZoomScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4888 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4889 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4890 | mPointerGesture.currentGestureProperties[i].clear(); |
| 4891 | mPointerGesture.currentGestureProperties[i].id = gestureId; |
| 4892 | mPointerGesture.currentGestureProperties[i].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4893 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4894 | mPointerGesture.currentGestureCoords[i].clear(); |
| 4895 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 4896 | AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4897 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 4898 | AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4899 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
| 4900 | AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
| 4901 | } |
| 4902 | |
| 4903 | if (mPointerGesture.activeGestureId < 0) { |
| 4904 | mPointerGesture.activeGestureId = |
| 4905 | mPointerGesture.currentGestureIdBits.firstMarkedBit(); |
| 4906 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4907 | ALOGD("Gestures: FREEFORM new " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4908 | "activeGestureId=%d", mPointerGesture.activeGestureId); |
| 4909 | #endif |
| 4910 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4911 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4912 | } |
| 4913 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4914 | mPointerController->setButtonState(mCurrentButtonState); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4915 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4916 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4917 | ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, " |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4918 | "currentGestureMode=%d, currentGestureIdBits=0x%08x, " |
| 4919 | "lastGestureMode=%d, lastGestureIdBits=0x%08x", |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4920 | toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture), |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4921 | mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value, |
| 4922 | mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4923 | for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4924 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4925 | uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4926 | const PointerProperties& properties = mPointerGesture.currentGestureProperties[index]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4927 | const PointerCoords& coords = mPointerGesture.currentGestureCoords[index]; |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4928 | ALOGD(" currentGesture[%d]: index=%d, toolType=%d, " |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4929 | "x=%0.3f, y=%0.3f, pressure=%0.3f", |
| 4930 | id, index, properties.toolType, |
| 4931 | coords.getAxisValue(AMOTION_EVENT_AXIS_X), |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4932 | coords.getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4933 | coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); |
| 4934 | } |
| 4935 | for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4936 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4937 | uint32_t index = mPointerGesture.lastGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4938 | const PointerProperties& properties = mPointerGesture.lastGestureProperties[index]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4939 | const PointerCoords& coords = mPointerGesture.lastGestureCoords[index]; |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4940 | ALOGD(" lastGesture[%d]: index=%d, toolType=%d, " |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4941 | "x=%0.3f, y=%0.3f, pressure=%0.3f", |
| 4942 | id, index, properties.toolType, |
| 4943 | coords.getAxisValue(AMOTION_EVENT_AXIS_X), |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4944 | coords.getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4945 | coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); |
| 4946 | } |
| 4947 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4948 | return true; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4949 | } |
| 4950 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4951 | void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) { |
| 4952 | mPointerSimple.currentCoords.clear(); |
| 4953 | mPointerSimple.currentProperties.clear(); |
| 4954 | |
| 4955 | bool down, hovering; |
| 4956 | if (!mCurrentStylusIdBits.isEmpty()) { |
| 4957 | uint32_t id = mCurrentStylusIdBits.firstMarkedBit(); |
| 4958 | uint32_t index = mCurrentCookedPointerData.idToIndex[id]; |
| 4959 | float x = mCurrentCookedPointerData.pointerCoords[index].getX(); |
| 4960 | float y = mCurrentCookedPointerData.pointerCoords[index].getY(); |
| 4961 | mPointerController->setPosition(x, y); |
| 4962 | |
| 4963 | hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id); |
| 4964 | down = !hovering; |
| 4965 | |
| 4966 | mPointerController->getPosition(&x, &y); |
| 4967 | mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]); |
| 4968 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4969 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 4970 | mPointerSimple.currentProperties.id = 0; |
| 4971 | mPointerSimple.currentProperties.toolType = |
| 4972 | mCurrentCookedPointerData.pointerProperties[index].toolType; |
| 4973 | } else { |
| 4974 | down = false; |
| 4975 | hovering = false; |
| 4976 | } |
| 4977 | |
| 4978 | dispatchPointerSimple(when, policyFlags, down, hovering); |
| 4979 | } |
| 4980 | |
| 4981 | void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) { |
| 4982 | abortPointerSimple(when, policyFlags); |
| 4983 | } |
| 4984 | |
| 4985 | void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) { |
| 4986 | mPointerSimple.currentCoords.clear(); |
| 4987 | mPointerSimple.currentProperties.clear(); |
| 4988 | |
| 4989 | bool down, hovering; |
| 4990 | if (!mCurrentMouseIdBits.isEmpty()) { |
| 4991 | uint32_t id = mCurrentMouseIdBits.firstMarkedBit(); |
| 4992 | uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id]; |
| 4993 | if (mLastMouseIdBits.hasBit(id)) { |
| 4994 | uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id]; |
| 4995 | float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x |
| 4996 | - mLastRawPointerData.pointers[lastIndex].x) |
| 4997 | * mPointerXMovementScale; |
| 4998 | float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y |
| 4999 | - mLastRawPointerData.pointers[lastIndex].y) |
| 5000 | * mPointerYMovementScale; |
| 5001 | |
| 5002 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
| 5003 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
| 5004 | |
| 5005 | mPointerController->move(deltaX, deltaY); |
| 5006 | } else { |
| 5007 | mPointerVelocityControl.reset(); |
| 5008 | } |
| 5009 | |
| 5010 | down = isPointerDown(mCurrentButtonState); |
| 5011 | hovering = !down; |
| 5012 | |
| 5013 | float x, y; |
| 5014 | mPointerController->getPosition(&x, &y); |
| 5015 | mPointerSimple.currentCoords.copyFrom( |
| 5016 | mCurrentCookedPointerData.pointerCoords[currentIndex]); |
| 5017 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 5018 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 5019 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, |
| 5020 | hovering ? 0.0f : 1.0f); |
| 5021 | mPointerSimple.currentProperties.id = 0; |
| 5022 | mPointerSimple.currentProperties.toolType = |
| 5023 | mCurrentCookedPointerData.pointerProperties[currentIndex].toolType; |
| 5024 | } else { |
| 5025 | mPointerVelocityControl.reset(); |
| 5026 | |
| 5027 | down = false; |
| 5028 | hovering = false; |
| 5029 | } |
| 5030 | |
| 5031 | dispatchPointerSimple(when, policyFlags, down, hovering); |
| 5032 | } |
| 5033 | |
| 5034 | void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) { |
| 5035 | abortPointerSimple(when, policyFlags); |
| 5036 | |
| 5037 | mPointerVelocityControl.reset(); |
| 5038 | } |
| 5039 | |
| 5040 | void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags, |
| 5041 | bool down, bool hovering) { |
| 5042 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 5043 | |
| 5044 | if (mPointerController != NULL) { |
| 5045 | if (down || hovering) { |
| 5046 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER); |
| 5047 | mPointerController->clearSpots(); |
| 5048 | mPointerController->setButtonState(mCurrentButtonState); |
| 5049 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 5050 | } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) { |
| 5051 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 5052 | } |
| 5053 | } |
| 5054 | |
| 5055 | if (mPointerSimple.down && !down) { |
| 5056 | mPointerSimple.down = false; |
| 5057 | |
| 5058 | // Send up. |
| 5059 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5060 | AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0, |
| 5061 | 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords, |
| 5062 | mOrientedXPrecision, mOrientedYPrecision, |
| 5063 | mPointerSimple.downTime); |
| 5064 | getListener()->notifyMotion(&args); |
| 5065 | } |
| 5066 | |
| 5067 | if (mPointerSimple.hovering && !hovering) { |
| 5068 | mPointerSimple.hovering = false; |
| 5069 | |
| 5070 | // Send hover exit. |
| 5071 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5072 | AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0, |
| 5073 | 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords, |
| 5074 | mOrientedXPrecision, mOrientedYPrecision, |
| 5075 | mPointerSimple.downTime); |
| 5076 | getListener()->notifyMotion(&args); |
| 5077 | } |
| 5078 | |
| 5079 | if (down) { |
| 5080 | if (!mPointerSimple.down) { |
| 5081 | mPointerSimple.down = true; |
| 5082 | mPointerSimple.downTime = when; |
| 5083 | |
| 5084 | // Send down. |
| 5085 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5086 | AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0, |
| 5087 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5088 | mOrientedXPrecision, mOrientedYPrecision, |
| 5089 | mPointerSimple.downTime); |
| 5090 | getListener()->notifyMotion(&args); |
| 5091 | } |
| 5092 | |
| 5093 | // Send move. |
| 5094 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5095 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0, |
| 5096 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5097 | mOrientedXPrecision, mOrientedYPrecision, |
| 5098 | mPointerSimple.downTime); |
| 5099 | getListener()->notifyMotion(&args); |
| 5100 | } |
| 5101 | |
| 5102 | if (hovering) { |
| 5103 | if (!mPointerSimple.hovering) { |
| 5104 | mPointerSimple.hovering = true; |
| 5105 | |
| 5106 | // Send hover enter. |
| 5107 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5108 | AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0, |
| 5109 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5110 | mOrientedXPrecision, mOrientedYPrecision, |
| 5111 | mPointerSimple.downTime); |
| 5112 | getListener()->notifyMotion(&args); |
| 5113 | } |
| 5114 | |
| 5115 | // Send hover move. |
| 5116 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5117 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0, |
| 5118 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5119 | mOrientedXPrecision, mOrientedYPrecision, |
| 5120 | mPointerSimple.downTime); |
| 5121 | getListener()->notifyMotion(&args); |
| 5122 | } |
| 5123 | |
| 5124 | if (mCurrentRawVScroll || mCurrentRawHScroll) { |
| 5125 | float vscroll = mCurrentRawVScroll; |
| 5126 | float hscroll = mCurrentRawHScroll; |
| 5127 | mWheelYVelocityControl.move(when, NULL, &vscroll); |
| 5128 | mWheelXVelocityControl.move(when, &hscroll, NULL); |
| 5129 | |
| 5130 | // Send scroll. |
| 5131 | PointerCoords pointerCoords; |
| 5132 | pointerCoords.copyFrom(mPointerSimple.currentCoords); |
| 5133 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll); |
| 5134 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll); |
| 5135 | |
| 5136 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5137 | AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0, |
| 5138 | 1, &mPointerSimple.currentProperties, &pointerCoords, |
| 5139 | mOrientedXPrecision, mOrientedYPrecision, |
| 5140 | mPointerSimple.downTime); |
| 5141 | getListener()->notifyMotion(&args); |
| 5142 | } |
| 5143 | |
| 5144 | // Save state. |
| 5145 | if (down || hovering) { |
| 5146 | mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords); |
| 5147 | mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties); |
| 5148 | } else { |
| 5149 | mPointerSimple.reset(); |
| 5150 | } |
| 5151 | } |
| 5152 | |
| 5153 | void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) { |
| 5154 | mPointerSimple.currentCoords.clear(); |
| 5155 | mPointerSimple.currentProperties.clear(); |
| 5156 | |
| 5157 | dispatchPointerSimple(when, policyFlags, false, false); |
| 5158 | } |
| 5159 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5160 | void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5161 | int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags, |
| 5162 | const PointerProperties* properties, const PointerCoords* coords, |
| 5163 | const uint32_t* idToIndex, BitSet32 idBits, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5164 | int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) { |
| 5165 | PointerCoords pointerCoords[MAX_POINTERS]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5166 | PointerProperties pointerProperties[MAX_POINTERS]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5167 | uint32_t pointerCount = 0; |
| 5168 | while (!idBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5169 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5170 | uint32_t index = idToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5171 | pointerProperties[pointerCount].copyFrom(properties[index]); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5172 | pointerCoords[pointerCount].copyFrom(coords[index]); |
| 5173 | |
| 5174 | if (changedId >= 0 && id == uint32_t(changedId)) { |
| 5175 | action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
| 5176 | } |
| 5177 | |
| 5178 | pointerCount += 1; |
| 5179 | } |
| 5180 | |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 5181 | ALOG_ASSERT(pointerCount != 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5182 | |
| 5183 | if (changedId >= 0 && pointerCount == 1) { |
| 5184 | // Replace initial down and final up action. |
| 5185 | // We can compare the action without masking off the changed pointer index |
| 5186 | // because we know the index is 0. |
| 5187 | if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 5188 | action = AMOTION_EVENT_ACTION_DOWN; |
| 5189 | } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 5190 | action = AMOTION_EVENT_ACTION_UP; |
| 5191 | } else { |
| 5192 | // Can't happen. |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 5193 | ALOG_ASSERT(false); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5194 | } |
| 5195 | } |
| 5196 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5197 | NotifyMotionArgs args(when, getDeviceId(), source, policyFlags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5198 | action, flags, metaState, buttonState, edgeFlags, |
| 5199 | pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5200 | getListener()->notifyMotion(&args); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5201 | } |
| 5202 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5203 | bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5204 | const PointerCoords* inCoords, const uint32_t* inIdToIndex, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5205 | PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex, |
| 5206 | BitSet32 idBits) const { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5207 | bool changed = false; |
| 5208 | while (!idBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5209 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5210 | uint32_t inIndex = inIdToIndex[id]; |
| 5211 | uint32_t outIndex = outIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5212 | |
| 5213 | const PointerProperties& curInProperties = inProperties[inIndex]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5214 | const PointerCoords& curInCoords = inCoords[inIndex]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5215 | PointerProperties& curOutProperties = outProperties[outIndex]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5216 | PointerCoords& curOutCoords = outCoords[outIndex]; |
| 5217 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5218 | if (curInProperties != curOutProperties) { |
| 5219 | curOutProperties.copyFrom(curInProperties); |
| 5220 | changed = true; |
| 5221 | } |
| 5222 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5223 | if (curInCoords != curOutCoords) { |
| 5224 | curOutCoords.copyFrom(curInCoords); |
| 5225 | changed = true; |
| 5226 | } |
| 5227 | } |
| 5228 | return changed; |
| 5229 | } |
| 5230 | |
| 5231 | void TouchInputMapper::fadePointer() { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5232 | if (mPointerController != NULL) { |
| 5233 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 5234 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5235 | } |
| 5236 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5237 | bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) { |
| 5238 | return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue |
| 5239 | && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5240 | } |
| 5241 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5242 | const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit( |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 5243 | int32_t x, int32_t y) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5244 | size_t numVirtualKeys = mVirtualKeys.size(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 5245 | for (size_t i = 0; i < numVirtualKeys; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5246 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5247 | |
| 5248 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5249 | ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, " |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5250 | "left=%d, top=%d, right=%d, bottom=%d", |
| 5251 | x, y, |
| 5252 | virtualKey.keyCode, virtualKey.scanCode, |
| 5253 | virtualKey.hitLeft, virtualKey.hitTop, |
| 5254 | virtualKey.hitRight, virtualKey.hitBottom); |
| 5255 | #endif |
| 5256 | |
| 5257 | if (virtualKey.isHit(x, y)) { |
| 5258 | return & virtualKey; |
| 5259 | } |
| 5260 | } |
| 5261 | |
| 5262 | return NULL; |
| 5263 | } |
| 5264 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5265 | void TouchInputMapper::assignPointerIds() { |
| 5266 | uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount; |
| 5267 | uint32_t lastPointerCount = mLastRawPointerData.pointerCount; |
| 5268 | |
| 5269 | mCurrentRawPointerData.clearIdBits(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5270 | |
| 5271 | if (currentPointerCount == 0) { |
| 5272 | // No pointers to assign. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5273 | return; |
| 5274 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5275 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5276 | if (lastPointerCount == 0) { |
| 5277 | // All pointers are new. |
| 5278 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
| 5279 | uint32_t id = i; |
| 5280 | mCurrentRawPointerData.pointers[i].id = id; |
| 5281 | mCurrentRawPointerData.idToIndex[id] = i; |
| 5282 | mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i)); |
| 5283 | } |
| 5284 | return; |
| 5285 | } |
| 5286 | |
| 5287 | if (currentPointerCount == 1 && lastPointerCount == 1 |
| 5288 | && mCurrentRawPointerData.pointers[0].toolType |
| 5289 | == mLastRawPointerData.pointers[0].toolType) { |
| 5290 | // Only one pointer and no change in count so it must have the same id as before. |
| 5291 | uint32_t id = mLastRawPointerData.pointers[0].id; |
| 5292 | mCurrentRawPointerData.pointers[0].id = id; |
| 5293 | mCurrentRawPointerData.idToIndex[id] = 0; |
| 5294 | mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0)); |
| 5295 | return; |
| 5296 | } |
| 5297 | |
| 5298 | // General case. |
| 5299 | // We build a heap of squared euclidean distances between current and last pointers |
| 5300 | // associated with the current and last pointer indices. Then, we find the best |
| 5301 | // match (by distance) for each current pointer. |
| 5302 | // The pointers must have the same tool type but it is possible for them to |
| 5303 | // transition from hovering to touching or vice-versa while retaining the same id. |
| 5304 | PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS]; |
| 5305 | |
| 5306 | uint32_t heapSize = 0; |
| 5307 | for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount; |
| 5308 | currentPointerIndex++) { |
| 5309 | for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount; |
| 5310 | lastPointerIndex++) { |
| 5311 | const RawPointerData::Pointer& currentPointer = |
| 5312 | mCurrentRawPointerData.pointers[currentPointerIndex]; |
| 5313 | const RawPointerData::Pointer& lastPointer = |
| 5314 | mLastRawPointerData.pointers[lastPointerIndex]; |
| 5315 | if (currentPointer.toolType == lastPointer.toolType) { |
| 5316 | int64_t deltaX = currentPointer.x - lastPointer.x; |
| 5317 | int64_t deltaY = currentPointer.y - lastPointer.y; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5318 | |
| 5319 | uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY); |
| 5320 | |
| 5321 | // Insert new element into the heap (sift up). |
| 5322 | heap[heapSize].currentPointerIndex = currentPointerIndex; |
| 5323 | heap[heapSize].lastPointerIndex = lastPointerIndex; |
| 5324 | heap[heapSize].distance = distance; |
| 5325 | heapSize += 1; |
| 5326 | } |
| 5327 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5328 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5329 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5330 | // Heapify |
| 5331 | for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) { |
| 5332 | startIndex -= 1; |
| 5333 | for (uint32_t parentIndex = startIndex; ;) { |
| 5334 | uint32_t childIndex = parentIndex * 2 + 1; |
| 5335 | if (childIndex >= heapSize) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5336 | break; |
| 5337 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5338 | |
| 5339 | if (childIndex + 1 < heapSize |
| 5340 | && heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 5341 | childIndex += 1; |
| 5342 | } |
| 5343 | |
| 5344 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 5345 | break; |
| 5346 | } |
| 5347 | |
| 5348 | swap(heap[parentIndex], heap[childIndex]); |
| 5349 | parentIndex = childIndex; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5350 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5351 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5352 | |
| 5353 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5354 | ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5355 | for (size_t i = 0; i < heapSize; i++) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5356 | ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5357 | i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, |
| 5358 | heap[i].distance); |
| 5359 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5360 | #endif |
| 5361 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5362 | // Pull matches out by increasing order of distance. |
| 5363 | // To avoid reassigning pointers that have already been matched, the loop keeps track |
| 5364 | // of which last and current pointers have been matched using the matchedXXXBits variables. |
| 5365 | // It also tracks the used pointer id bits. |
| 5366 | BitSet32 matchedLastBits(0); |
| 5367 | BitSet32 matchedCurrentBits(0); |
| 5368 | BitSet32 usedIdBits(0); |
| 5369 | bool first = true; |
| 5370 | for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) { |
| 5371 | while (heapSize > 0) { |
| 5372 | if (first) { |
| 5373 | // The first time through the loop, we just consume the root element of |
| 5374 | // the heap (the one with smallest distance). |
| 5375 | first = false; |
| 5376 | } else { |
| 5377 | // Previous iterations consumed the root element of the heap. |
| 5378 | // Pop root element off of the heap (sift down). |
| 5379 | heap[0] = heap[heapSize]; |
| 5380 | for (uint32_t parentIndex = 0; ;) { |
| 5381 | uint32_t childIndex = parentIndex * 2 + 1; |
| 5382 | if (childIndex >= heapSize) { |
| 5383 | break; |
| 5384 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5385 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5386 | if (childIndex + 1 < heapSize |
| 5387 | && heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 5388 | childIndex += 1; |
| 5389 | } |
| 5390 | |
| 5391 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 5392 | break; |
| 5393 | } |
| 5394 | |
| 5395 | swap(heap[parentIndex], heap[childIndex]); |
| 5396 | parentIndex = childIndex; |
| 5397 | } |
| 5398 | |
| 5399 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5400 | ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5401 | for (size_t i = 0; i < heapSize; i++) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5402 | ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5403 | i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, |
| 5404 | heap[i].distance); |
| 5405 | } |
| 5406 | #endif |
| 5407 | } |
| 5408 | |
| 5409 | heapSize -= 1; |
| 5410 | |
| 5411 | uint32_t currentPointerIndex = heap[0].currentPointerIndex; |
| 5412 | if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched |
| 5413 | |
| 5414 | uint32_t lastPointerIndex = heap[0].lastPointerIndex; |
| 5415 | if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched |
| 5416 | |
| 5417 | matchedCurrentBits.markBit(currentPointerIndex); |
| 5418 | matchedLastBits.markBit(lastPointerIndex); |
| 5419 | |
| 5420 | uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id; |
| 5421 | mCurrentRawPointerData.pointers[currentPointerIndex].id = id; |
| 5422 | mCurrentRawPointerData.idToIndex[id] = currentPointerIndex; |
| 5423 | mCurrentRawPointerData.markIdBit(id, |
| 5424 | mCurrentRawPointerData.isHovering(currentPointerIndex)); |
| 5425 | usedIdBits.markBit(id); |
| 5426 | |
| 5427 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5428 | ALOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5429 | lastPointerIndex, currentPointerIndex, id, heap[0].distance); |
| 5430 | #endif |
| 5431 | break; |
| 5432 | } |
| 5433 | } |
| 5434 | |
| 5435 | // Assign fresh ids to pointers that were not matched in the process. |
| 5436 | for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) { |
| 5437 | uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit(); |
| 5438 | uint32_t id = usedIdBits.markFirstUnmarkedBit(); |
| 5439 | |
| 5440 | mCurrentRawPointerData.pointers[currentPointerIndex].id = id; |
| 5441 | mCurrentRawPointerData.idToIndex[id] = currentPointerIndex; |
| 5442 | mCurrentRawPointerData.markIdBit(id, |
| 5443 | mCurrentRawPointerData.isHovering(currentPointerIndex)); |
| 5444 | |
| 5445 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5446 | ALOGD("assignPointerIds - assigned: cur=%d, id=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5447 | currentPointerIndex, id); |
| 5448 | #endif |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5449 | } |
| 5450 | } |
| 5451 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5452 | int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5453 | if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) { |
| 5454 | return AKEY_STATE_VIRTUAL; |
| 5455 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5456 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5457 | size_t numVirtualKeys = mVirtualKeys.size(); |
| 5458 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 5459 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
| 5460 | if (virtualKey.keyCode == keyCode) { |
| 5461 | return AKEY_STATE_UP; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5462 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5463 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5464 | |
| 5465 | return AKEY_STATE_UNKNOWN; |
| 5466 | } |
| 5467 | |
| 5468 | int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5469 | if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) { |
| 5470 | return AKEY_STATE_VIRTUAL; |
| 5471 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5472 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5473 | size_t numVirtualKeys = mVirtualKeys.size(); |
| 5474 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 5475 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
| 5476 | if (virtualKey.scanCode == scanCode) { |
| 5477 | return AKEY_STATE_UP; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5478 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5479 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5480 | |
| 5481 | return AKEY_STATE_UNKNOWN; |
| 5482 | } |
| 5483 | |
| 5484 | bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 5485 | const int32_t* keyCodes, uint8_t* outFlags) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5486 | size_t numVirtualKeys = mVirtualKeys.size(); |
| 5487 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 5488 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5489 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5490 | for (size_t i = 0; i < numCodes; i++) { |
| 5491 | if (virtualKey.keyCode == keyCodes[i]) { |
| 5492 | outFlags[i] = 1; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5493 | } |
| 5494 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5495 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5496 | |
| 5497 | return true; |
| 5498 | } |
| 5499 | |
| 5500 | |
| 5501 | // --- SingleTouchInputMapper --- |
| 5502 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 5503 | SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) : |
| 5504 | TouchInputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5505 | } |
| 5506 | |
| 5507 | SingleTouchInputMapper::~SingleTouchInputMapper() { |
| 5508 | } |
| 5509 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5510 | void SingleTouchInputMapper::reset(nsecs_t when) { |
| 5511 | mSingleTouchMotionAccumulator.reset(getDevice()); |
| 5512 | |
| 5513 | TouchInputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5514 | } |
| 5515 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5516 | void SingleTouchInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5517 | TouchInputMapper::process(rawEvent); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5518 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5519 | mSingleTouchMotionAccumulator.process(rawEvent); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5520 | } |
| 5521 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5522 | void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) { |
Jeff Brown | d87c6d5 | 2011-08-10 14:55:59 -0700 | [diff] [blame] | 5523 | if (mTouchButtonAccumulator.isToolActive()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5524 | mCurrentRawPointerData.pointerCount = 1; |
| 5525 | mCurrentRawPointerData.idToIndex[0] = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5526 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5527 | bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE |
| 5528 | && (mTouchButtonAccumulator.isHovering() |
| 5529 | || (mRawPointerAxes.pressure.valid |
| 5530 | && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0)); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5531 | mCurrentRawPointerData.markIdBit(0, isHovering); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5532 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5533 | RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0]; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5534 | outPointer.id = 0; |
| 5535 | outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX(); |
| 5536 | outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY(); |
| 5537 | outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure(); |
| 5538 | outPointer.touchMajor = 0; |
| 5539 | outPointer.touchMinor = 0; |
| 5540 | outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth(); |
| 5541 | outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth(); |
| 5542 | outPointer.orientation = 0; |
| 5543 | outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5544 | outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX(); |
| 5545 | outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY(); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5546 | outPointer.toolType = mTouchButtonAccumulator.getToolType(); |
| 5547 | if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 5548 | outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 5549 | } |
| 5550 | outPointer.isHovering = isHovering; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5551 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5552 | } |
| 5553 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5554 | void SingleTouchInputMapper::configureRawPointerAxes() { |
| 5555 | TouchInputMapper::configureRawPointerAxes(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5556 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5557 | getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x); |
| 5558 | getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y); |
| 5559 | getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure); |
| 5560 | getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor); |
| 5561 | getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5562 | getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX); |
| 5563 | getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5564 | } |
| 5565 | |
| 5566 | |
| 5567 | // --- MultiTouchInputMapper --- |
| 5568 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 5569 | MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) : |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5570 | TouchInputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5571 | } |
| 5572 | |
| 5573 | MultiTouchInputMapper::~MultiTouchInputMapper() { |
| 5574 | } |
| 5575 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5576 | void MultiTouchInputMapper::reset(nsecs_t when) { |
| 5577 | mMultiTouchMotionAccumulator.reset(getDevice()); |
| 5578 | |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5579 | mPointerIdBits.clear(); |
Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 5580 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5581 | TouchInputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5582 | } |
| 5583 | |
| 5584 | void MultiTouchInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5585 | TouchInputMapper::process(rawEvent); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5586 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5587 | mMultiTouchMotionAccumulator.process(rawEvent); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5588 | } |
| 5589 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5590 | void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5591 | size_t inCount = mMultiTouchMotionAccumulator.getSlotCount(); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5592 | size_t outCount = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5593 | BitSet32 newPointerIdBits; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5594 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5595 | for (size_t inIndex = 0; inIndex < inCount; inIndex++) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5596 | const MultiTouchMotionAccumulator::Slot* inSlot = |
| 5597 | mMultiTouchMotionAccumulator.getSlot(inIndex); |
| 5598 | if (!inSlot->isInUse()) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5599 | continue; |
| 5600 | } |
| 5601 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5602 | if (outCount >= MAX_POINTERS) { |
| 5603 | #if DEBUG_POINTERS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5604 | ALOGD("MultiTouch device %s emitted more than maximum of %d pointers; " |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5605 | "ignoring the rest.", |
| 5606 | getDeviceName().string(), MAX_POINTERS); |
| 5607 | #endif |
| 5608 | break; // too many fingers! |
| 5609 | } |
| 5610 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5611 | RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount]; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5612 | outPointer.x = inSlot->getX(); |
| 5613 | outPointer.y = inSlot->getY(); |
| 5614 | outPointer.pressure = inSlot->getPressure(); |
| 5615 | outPointer.touchMajor = inSlot->getTouchMajor(); |
| 5616 | outPointer.touchMinor = inSlot->getTouchMinor(); |
| 5617 | outPointer.toolMajor = inSlot->getToolMajor(); |
| 5618 | outPointer.toolMinor = inSlot->getToolMinor(); |
| 5619 | outPointer.orientation = inSlot->getOrientation(); |
| 5620 | outPointer.distance = inSlot->getDistance(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5621 | outPointer.tiltX = 0; |
| 5622 | outPointer.tiltY = 0; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5623 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5624 | outPointer.toolType = inSlot->getToolType(); |
| 5625 | if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 5626 | outPointer.toolType = mTouchButtonAccumulator.getToolType(); |
| 5627 | if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 5628 | outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 5629 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5630 | } |
| 5631 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5632 | bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE |
| 5633 | && (mTouchButtonAccumulator.isHovering() |
| 5634 | || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0)); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5635 | outPointer.isHovering = isHovering; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5636 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5637 | // Assign pointer id using tracking id if available. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5638 | if (*outHavePointerIds) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5639 | int32_t trackingId = inSlot->getTrackingId(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5640 | int32_t id = -1; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5641 | if (trackingId >= 0) { |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5642 | for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5643 | uint32_t n = idBits.clearFirstMarkedBit(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5644 | if (mPointerTrackingIdMap[n] == trackingId) { |
| 5645 | id = n; |
| 5646 | } |
| 5647 | } |
| 5648 | |
| 5649 | if (id < 0 && !mPointerIdBits.isFull()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5650 | id = mPointerIdBits.markFirstUnmarkedBit(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5651 | mPointerTrackingIdMap[id] = trackingId; |
| 5652 | } |
| 5653 | } |
| 5654 | if (id < 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5655 | *outHavePointerIds = false; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5656 | mCurrentRawPointerData.clearIdBits(); |
| 5657 | newPointerIdBits.clear(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5658 | } else { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5659 | outPointer.id = id; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5660 | mCurrentRawPointerData.idToIndex[id] = outCount; |
| 5661 | mCurrentRawPointerData.markIdBit(id, isHovering); |
| 5662 | newPointerIdBits.markBit(id); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5663 | } |
| 5664 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5665 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5666 | outCount += 1; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5667 | } |
| 5668 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5669 | mCurrentRawPointerData.pointerCount = outCount; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5670 | mPointerIdBits = newPointerIdBits; |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5671 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5672 | mMultiTouchMotionAccumulator.finishSync(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5673 | } |
| 5674 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5675 | void MultiTouchInputMapper::configureRawPointerAxes() { |
| 5676 | TouchInputMapper::configureRawPointerAxes(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5677 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5678 | getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x); |
| 5679 | getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y); |
| 5680 | getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor); |
| 5681 | getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor); |
| 5682 | getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor); |
| 5683 | getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor); |
| 5684 | getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation); |
| 5685 | getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure); |
| 5686 | getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance); |
| 5687 | getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId); |
| 5688 | getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5689 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5690 | if (mRawPointerAxes.trackingId.valid |
| 5691 | && mRawPointerAxes.slot.valid |
| 5692 | && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) { |
| 5693 | size_t slotCount = mRawPointerAxes.slot.maxValue + 1; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5694 | if (slotCount > MAX_SLOTS) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5695 | ALOGW("MultiTouch Device %s reported %d slots but the framework " |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5696 | "only supports a maximum of %d slots at this time.", |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5697 | getDeviceName().string(), slotCount, MAX_SLOTS); |
| 5698 | slotCount = MAX_SLOTS; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5699 | } |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5700 | mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5701 | } else { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5702 | mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5703 | } |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 5704 | } |
| 5705 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5706 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5707 | // --- JoystickInputMapper --- |
| 5708 | |
| 5709 | JoystickInputMapper::JoystickInputMapper(InputDevice* device) : |
| 5710 | InputMapper(device) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5711 | } |
| 5712 | |
| 5713 | JoystickInputMapper::~JoystickInputMapper() { |
| 5714 | } |
| 5715 | |
| 5716 | uint32_t JoystickInputMapper::getSources() { |
| 5717 | return AINPUT_SOURCE_JOYSTICK; |
| 5718 | } |
| 5719 | |
| 5720 | void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 5721 | InputMapper::populateDeviceInfo(info); |
| 5722 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5723 | for (size_t i = 0; i < mAxes.size(); i++) { |
| 5724 | const Axis& axis = mAxes.valueAt(i); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 5725 | info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK, |
| 5726 | axis.min, axis.max, axis.flat, axis.fuzz); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5727 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 5728 | info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK, |
| 5729 | axis.min, axis.max, axis.flat, axis.fuzz); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5730 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5731 | } |
| 5732 | } |
| 5733 | |
| 5734 | void JoystickInputMapper::dump(String8& dump) { |
| 5735 | dump.append(INDENT2 "Joystick Input Mapper:\n"); |
| 5736 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5737 | dump.append(INDENT3 "Axes:\n"); |
| 5738 | size_t numAxes = mAxes.size(); |
| 5739 | for (size_t i = 0; i < numAxes; i++) { |
| 5740 | const Axis& axis = mAxes.valueAt(i); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5741 | const char* label = getAxisLabel(axis.axisInfo.axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5742 | if (label) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5743 | dump.appendFormat(INDENT4 "%s", label); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5744 | } else { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5745 | dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5746 | } |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5747 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 5748 | label = getAxisLabel(axis.axisInfo.highAxis); |
| 5749 | if (label) { |
| 5750 | dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue); |
| 5751 | } else { |
| 5752 | dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis, |
| 5753 | axis.axisInfo.splitValue); |
| 5754 | } |
| 5755 | } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) { |
| 5756 | dump.append(" (invert)"); |
| 5757 | } |
| 5758 | |
| 5759 | dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n", |
| 5760 | axis.min, axis.max, axis.flat, axis.fuzz); |
| 5761 | dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, " |
| 5762 | "highScale=%0.5f, highOffset=%0.5f\n", |
| 5763 | axis.scale, axis.offset, axis.highScale, axis.highOffset); |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 5764 | dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, " |
| 5765 | "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n", |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5766 | mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue, |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 5767 | axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5768 | } |
| 5769 | } |
| 5770 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5771 | void JoystickInputMapper::configure(nsecs_t when, |
| 5772 | const InputReaderConfiguration* config, uint32_t changes) { |
| 5773 | InputMapper::configure(when, config, changes); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5774 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5775 | if (!changes) { // first time only |
| 5776 | // Collect all axes. |
| 5777 | for (int32_t abs = 0; abs <= ABS_MAX; abs++) { |
Jeff Brown | 9ee285af | 2011-08-31 12:56:34 -0700 | [diff] [blame] | 5778 | if (!(getAbsAxisUsage(abs, getDevice()->getClasses()) |
| 5779 | & INPUT_DEVICE_CLASS_JOYSTICK)) { |
| 5780 | continue; // axis must be claimed by a different device |
| 5781 | } |
| 5782 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5783 | RawAbsoluteAxisInfo rawAxisInfo; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5784 | getAbsoluteAxisInfo(abs, &rawAxisInfo); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5785 | if (rawAxisInfo.valid) { |
| 5786 | // Map axis. |
| 5787 | AxisInfo axisInfo; |
| 5788 | bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo); |
| 5789 | if (!explicitlyMapped) { |
| 5790 | // Axis is not explicitly mapped, will choose a generic axis later. |
| 5791 | axisInfo.mode = AxisInfo::MODE_NORMAL; |
| 5792 | axisInfo.axis = -1; |
| 5793 | } |
| 5794 | |
| 5795 | // Apply flat override. |
| 5796 | int32_t rawFlat = axisInfo.flatOverride < 0 |
| 5797 | ? rawAxisInfo.flat : axisInfo.flatOverride; |
| 5798 | |
| 5799 | // Calculate scaling factors and limits. |
| 5800 | Axis axis; |
| 5801 | if (axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 5802 | float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue); |
| 5803 | float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue); |
| 5804 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 5805 | scale, 0.0f, highScale, 0.0f, |
| 5806 | 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
| 5807 | } else if (isCenteredAxis(axisInfo.axis)) { |
| 5808 | float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); |
| 5809 | float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale; |
| 5810 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 5811 | scale, offset, scale, offset, |
| 5812 | -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
| 5813 | } else { |
| 5814 | float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); |
| 5815 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 5816 | scale, 0.0f, scale, 0.0f, |
| 5817 | 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
| 5818 | } |
| 5819 | |
| 5820 | // To eliminate noise while the joystick is at rest, filter out small variations |
| 5821 | // in axis values up front. |
| 5822 | axis.filter = axis.flat * 0.25f; |
| 5823 | |
| 5824 | mAxes.add(abs, axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5825 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5826 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5827 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5828 | // If there are too many axes, start dropping them. |
| 5829 | // Prefer to keep explicitly mapped axes. |
| 5830 | if (mAxes.size() > PointerCoords::MAX_AXES) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 5831 | ALOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.", |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5832 | getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES); |
| 5833 | pruneAxes(true); |
| 5834 | pruneAxes(false); |
| 5835 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5836 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5837 | // Assign generic axis ids to remaining axes. |
| 5838 | int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1; |
| 5839 | size_t numAxes = mAxes.size(); |
| 5840 | for (size_t i = 0; i < numAxes; i++) { |
| 5841 | Axis& axis = mAxes.editValueAt(i); |
| 5842 | if (axis.axisInfo.axis < 0) { |
| 5843 | while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16 |
| 5844 | && haveAxis(nextGenericAxisId)) { |
| 5845 | nextGenericAxisId += 1; |
| 5846 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5847 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5848 | if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) { |
| 5849 | axis.axisInfo.axis = nextGenericAxisId; |
| 5850 | nextGenericAxisId += 1; |
| 5851 | } else { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 5852 | ALOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids " |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5853 | "have already been assigned to other axes.", |
| 5854 | getDeviceName().string(), mAxes.keyAt(i)); |
| 5855 | mAxes.removeItemsAt(i--); |
| 5856 | numAxes -= 1; |
| 5857 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5858 | } |
| 5859 | } |
| 5860 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5861 | } |
| 5862 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5863 | bool JoystickInputMapper::haveAxis(int32_t axisId) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5864 | size_t numAxes = mAxes.size(); |
| 5865 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5866 | const Axis& axis = mAxes.valueAt(i); |
| 5867 | if (axis.axisInfo.axis == axisId |
| 5868 | || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT |
| 5869 | && axis.axisInfo.highAxis == axisId)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5870 | return true; |
| 5871 | } |
| 5872 | } |
| 5873 | return false; |
| 5874 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5875 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5876 | void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) { |
| 5877 | size_t i = mAxes.size(); |
| 5878 | while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) { |
| 5879 | if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) { |
| 5880 | continue; |
| 5881 | } |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 5882 | ALOGI("Discarding joystick '%s' axis %d because there are too many axes.", |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5883 | getDeviceName().string(), mAxes.keyAt(i)); |
| 5884 | mAxes.removeItemsAt(i); |
| 5885 | } |
| 5886 | } |
| 5887 | |
| 5888 | bool JoystickInputMapper::isCenteredAxis(int32_t axis) { |
| 5889 | switch (axis) { |
| 5890 | case AMOTION_EVENT_AXIS_X: |
| 5891 | case AMOTION_EVENT_AXIS_Y: |
| 5892 | case AMOTION_EVENT_AXIS_Z: |
| 5893 | case AMOTION_EVENT_AXIS_RX: |
| 5894 | case AMOTION_EVENT_AXIS_RY: |
| 5895 | case AMOTION_EVENT_AXIS_RZ: |
| 5896 | case AMOTION_EVENT_AXIS_HAT_X: |
| 5897 | case AMOTION_EVENT_AXIS_HAT_Y: |
| 5898 | case AMOTION_EVENT_AXIS_ORIENTATION: |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5899 | case AMOTION_EVENT_AXIS_RUDDER: |
| 5900 | case AMOTION_EVENT_AXIS_WHEEL: |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5901 | return true; |
| 5902 | default: |
| 5903 | return false; |
| 5904 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5905 | } |
| 5906 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5907 | void JoystickInputMapper::reset(nsecs_t when) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5908 | // Recenter all axes. |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5909 | size_t numAxes = mAxes.size(); |
| 5910 | for (size_t i = 0; i < numAxes; i++) { |
| 5911 | Axis& axis = mAxes.editValueAt(i); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5912 | axis.resetValue(); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5913 | } |
| 5914 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5915 | InputMapper::reset(when); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5916 | } |
| 5917 | |
| 5918 | void JoystickInputMapper::process(const RawEvent* rawEvent) { |
| 5919 | switch (rawEvent->type) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5920 | case EV_ABS: { |
| 5921 | ssize_t index = mAxes.indexOfKey(rawEvent->scanCode); |
| 5922 | if (index >= 0) { |
| 5923 | Axis& axis = mAxes.editValueAt(index); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5924 | float newValue, highNewValue; |
| 5925 | switch (axis.axisInfo.mode) { |
| 5926 | case AxisInfo::MODE_INVERT: |
| 5927 | newValue = (axis.rawAxisInfo.maxValue - rawEvent->value) |
| 5928 | * axis.scale + axis.offset; |
| 5929 | highNewValue = 0.0f; |
| 5930 | break; |
| 5931 | case AxisInfo::MODE_SPLIT: |
| 5932 | if (rawEvent->value < axis.axisInfo.splitValue) { |
| 5933 | newValue = (axis.axisInfo.splitValue - rawEvent->value) |
| 5934 | * axis.scale + axis.offset; |
| 5935 | highNewValue = 0.0f; |
| 5936 | } else if (rawEvent->value > axis.axisInfo.splitValue) { |
| 5937 | newValue = 0.0f; |
| 5938 | highNewValue = (rawEvent->value - axis.axisInfo.splitValue) |
| 5939 | * axis.highScale + axis.highOffset; |
| 5940 | } else { |
| 5941 | newValue = 0.0f; |
| 5942 | highNewValue = 0.0f; |
| 5943 | } |
| 5944 | break; |
| 5945 | default: |
| 5946 | newValue = rawEvent->value * axis.scale + axis.offset; |
| 5947 | highNewValue = 0.0f; |
| 5948 | break; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5949 | } |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5950 | axis.newValue = newValue; |
| 5951 | axis.highNewValue = highNewValue; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5952 | } |
| 5953 | break; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5954 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5955 | |
| 5956 | case EV_SYN: |
| 5957 | switch (rawEvent->scanCode) { |
| 5958 | case SYN_REPORT: |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5959 | sync(rawEvent->when, false /*force*/); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5960 | break; |
| 5961 | } |
| 5962 | break; |
| 5963 | } |
| 5964 | } |
| 5965 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5966 | void JoystickInputMapper::sync(nsecs_t when, bool force) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5967 | if (!filterAxes(force)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5968 | return; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5969 | } |
| 5970 | |
| 5971 | int32_t metaState = mContext->getGlobalMetaState(); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5972 | int32_t buttonState = 0; |
| 5973 | |
| 5974 | PointerProperties pointerProperties; |
| 5975 | pointerProperties.clear(); |
| 5976 | pointerProperties.id = 0; |
| 5977 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5978 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5979 | PointerCoords pointerCoords; |
| 5980 | pointerCoords.clear(); |
| 5981 | |
| 5982 | size_t numAxes = mAxes.size(); |
| 5983 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5984 | const Axis& axis = mAxes.valueAt(i); |
| 5985 | pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue); |
| 5986 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 5987 | pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue); |
| 5988 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5989 | } |
| 5990 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 5991 | // Moving a joystick axis should not wake the devide because joysticks can |
| 5992 | // be fairly noisy even when not in use. On the other hand, pushing a gamepad |
| 5993 | // button will likely wake the device. |
| 5994 | // TODO: Use the input device configuration to control this behavior more finely. |
| 5995 | uint32_t policyFlags = 0; |
| 5996 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5997 | NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5998 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 5999 | 1, &pointerProperties, &pointerCoords, 0, 0, 0); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 6000 | getListener()->notifyMotion(&args); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6001 | } |
| 6002 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6003 | bool JoystickInputMapper::filterAxes(bool force) { |
| 6004 | bool atLeastOneSignificantChange = force; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6005 | size_t numAxes = mAxes.size(); |
| 6006 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6007 | Axis& axis = mAxes.editValueAt(i); |
| 6008 | if (force || hasValueChangedSignificantly(axis.filter, |
| 6009 | axis.newValue, axis.currentValue, axis.min, axis.max)) { |
| 6010 | axis.currentValue = axis.newValue; |
| 6011 | atLeastOneSignificantChange = true; |
| 6012 | } |
| 6013 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 6014 | if (force || hasValueChangedSignificantly(axis.filter, |
| 6015 | axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) { |
| 6016 | axis.highCurrentValue = axis.highNewValue; |
| 6017 | atLeastOneSignificantChange = true; |
| 6018 | } |
| 6019 | } |
| 6020 | } |
| 6021 | return atLeastOneSignificantChange; |
| 6022 | } |
| 6023 | |
| 6024 | bool JoystickInputMapper::hasValueChangedSignificantly( |
| 6025 | float filter, float newValue, float currentValue, float min, float max) { |
| 6026 | if (newValue != currentValue) { |
| 6027 | // Filter out small changes in value unless the value is converging on the axis |
| 6028 | // bounds or center point. This is intended to reduce the amount of information |
| 6029 | // sent to applications by particularly noisy joysticks (such as PS3). |
| 6030 | if (fabs(newValue - currentValue) > filter |
| 6031 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min) |
| 6032 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max) |
| 6033 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) { |
| 6034 | return true; |
| 6035 | } |
| 6036 | } |
| 6037 | return false; |
| 6038 | } |
| 6039 | |
| 6040 | bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange( |
| 6041 | float filter, float newValue, float currentValue, float thresholdValue) { |
| 6042 | float newDistance = fabs(newValue - thresholdValue); |
| 6043 | if (newDistance < filter) { |
| 6044 | float oldDistance = fabs(currentValue - thresholdValue); |
| 6045 | if (newDistance < oldDistance) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6046 | return true; |
| 6047 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6048 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6049 | return false; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6050 | } |
| 6051 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 6052 | } // namespace android |