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 | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 347 | String8 name = mEventHub->getDeviceName(deviceId); |
| 348 | uint32_t classes = mEventHub->getDeviceClasses(deviceId); |
| 349 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 350 | InputDevice* device = createDeviceLocked(deviceId, name, 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()) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 355 | ALOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 356 | } else { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 357 | ALOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(), |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 358 | device->getSources()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 361 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 362 | if (deviceIndex < 0) { |
| 363 | mDevices.add(deviceId, device); |
| 364 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 365 | ALOGW("Ignoring spurious device added event for deviceId %d.", deviceId); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 366 | delete device; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 367 | return; |
| 368 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 371 | void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 372 | InputDevice* device = NULL; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 373 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 374 | if (deviceIndex >= 0) { |
| 375 | device = mDevices.valueAt(deviceIndex); |
| 376 | mDevices.removeItemsAt(deviceIndex, 1); |
| 377 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 378 | ALOGW("Ignoring spurious device removed event for deviceId %d.", deviceId); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 379 | return; |
| 380 | } |
| 381 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 382 | if (device->isIgnored()) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 383 | ALOGI("Device removed: id=%d, name='%s' (ignored non-input device)", |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 384 | device->getId(), device->getName().string()); |
| 385 | } else { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 386 | ALOGI("Device removed: id=%d, name='%s', sources=0x%08x", |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 387 | device->getId(), device->getName().string(), device->getSources()); |
| 388 | } |
| 389 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 390 | device->reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 391 | delete device; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 394 | InputDevice* InputReader::createDeviceLocked(int32_t deviceId, |
| 395 | const String8& name, uint32_t classes) { |
Jeff Brown | 9ee285af | 2011-08-31 12:56:34 -0700 | [diff] [blame] | 396 | InputDevice* device = new InputDevice(&mContext, deviceId, name, classes); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 397 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 398 | // External devices. |
| 399 | if (classes & INPUT_DEVICE_CLASS_EXTERNAL) { |
| 400 | device->setExternal(true); |
| 401 | } |
| 402 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 403 | // Switch-like devices. |
| 404 | if (classes & INPUT_DEVICE_CLASS_SWITCH) { |
| 405 | device->addMapper(new SwitchInputMapper(device)); |
| 406 | } |
| 407 | |
| 408 | // Keyboard-like devices. |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 409 | uint32_t keyboardSource = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 410 | int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC; |
| 411 | if (classes & INPUT_DEVICE_CLASS_KEYBOARD) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 412 | keyboardSource |= AINPUT_SOURCE_KEYBOARD; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 413 | } |
| 414 | if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) { |
| 415 | keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC; |
| 416 | } |
| 417 | if (classes & INPUT_DEVICE_CLASS_DPAD) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 418 | keyboardSource |= AINPUT_SOURCE_DPAD; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 419 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 420 | if (classes & INPUT_DEVICE_CLASS_GAMEPAD) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 421 | keyboardSource |= AINPUT_SOURCE_GAMEPAD; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 422 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 423 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 424 | if (keyboardSource != 0) { |
| 425 | device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 428 | // Cursor-like devices. |
| 429 | if (classes & INPUT_DEVICE_CLASS_CURSOR) { |
| 430 | device->addMapper(new CursorInputMapper(device)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 433 | // Touchscreens and touchpad devices. |
| 434 | if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 435 | device->addMapper(new MultiTouchInputMapper(device)); |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 436 | } else if (classes & INPUT_DEVICE_CLASS_TOUCH) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 437 | device->addMapper(new SingleTouchInputMapper(device)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 440 | // Joystick-like devices. |
| 441 | if (classes & INPUT_DEVICE_CLASS_JOYSTICK) { |
| 442 | device->addMapper(new JoystickInputMapper(device)); |
| 443 | } |
| 444 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 445 | return device; |
| 446 | } |
| 447 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 448 | void InputReader::processEventsForDeviceLocked(int32_t deviceId, |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 449 | const RawEvent* rawEvents, size_t count) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 450 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 451 | if (deviceIndex < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 452 | ALOGW("Discarding event for unknown deviceId %d.", deviceId); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 453 | return; |
| 454 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 455 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 456 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 457 | if (device->isIgnored()) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 458 | //ALOGD("Discarding event for ignored deviceId %d.", deviceId); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 459 | return; |
| 460 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 461 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 462 | device->process(rawEvents, count); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 465 | void InputReader::timeoutExpiredLocked(nsecs_t when) { |
| 466 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 467 | InputDevice* device = mDevices.valueAt(i); |
| 468 | if (!device->isIgnored()) { |
| 469 | device->timeoutExpired(when); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 470 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 471 | } |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 474 | void InputReader::handleConfigurationChangedLocked(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 475 | // 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] | 476 | updateGlobalMetaStateLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 477 | |
| 478 | // Update input configuration. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 479 | updateInputConfigurationLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 480 | |
| 481 | // Enqueue configuration changed. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 482 | NotifyConfigurationChangedArgs args(when); |
| 483 | mQueuedListener->notifyConfigurationChanged(&args); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 486 | void InputReader::refreshConfigurationLocked(uint32_t changes) { |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 487 | mPolicy->getReaderConfiguration(&mConfig); |
| 488 | mEventHub->setExcludedDevices(mConfig.excludedDeviceNames); |
| 489 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 490 | if (changes) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 491 | ALOGI("Reconfiguring input devices. changes=0x%08x", changes); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 492 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 493 | |
| 494 | if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) { |
| 495 | mEventHub->requestReopenDevices(); |
| 496 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 497 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 498 | InputDevice* device = mDevices.valueAt(i); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 499 | device->configure(now, &mConfig, changes); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 500 | } |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 501 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 505 | void InputReader::updateGlobalMetaStateLocked() { |
| 506 | mGlobalMetaState = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 507 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 508 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 509 | InputDevice* device = mDevices.valueAt(i); |
| 510 | mGlobalMetaState |= device->getMetaState(); |
| 511 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 514 | int32_t InputReader::getGlobalMetaStateLocked() { |
| 515 | return mGlobalMetaState; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 518 | void InputReader::updateInputConfigurationLocked() { |
| 519 | int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH; |
| 520 | int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS; |
| 521 | int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV; |
| 522 | InputDeviceInfo deviceInfo; |
| 523 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 524 | InputDevice* device = mDevices.valueAt(i); |
| 525 | device->getDeviceInfo(& deviceInfo); |
| 526 | uint32_t sources = deviceInfo.getSources(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 527 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 528 | if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) { |
| 529 | touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER; |
| 530 | } |
| 531 | if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) { |
| 532 | navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL; |
| 533 | } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) { |
| 534 | navigationConfig = InputConfiguration::NAVIGATION_DPAD; |
| 535 | } |
| 536 | if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) { |
| 537 | keyboardConfig = InputConfiguration::KEYBOARD_QWERTY; |
| 538 | } |
| 539 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 540 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 541 | mInputConfiguration.touchScreen = touchScreenConfig; |
| 542 | mInputConfiguration.keyboard = keyboardConfig; |
| 543 | mInputConfiguration.navigation = navigationConfig; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 546 | void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) { |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 547 | mDisableVirtualKeysTimeout = time; |
| 548 | } |
| 549 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 550 | bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 551 | InputDevice* device, int32_t keyCode, int32_t scanCode) { |
| 552 | if (now < mDisableVirtualKeysTimeout) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 553 | ALOGI("Dropping virtual key from device %s because virtual keys are " |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 554 | "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d", |
| 555 | device->getName().string(), |
| 556 | (mDisableVirtualKeysTimeout - now) * 0.000001, |
| 557 | keyCode, scanCode); |
| 558 | return true; |
| 559 | } else { |
| 560 | return false; |
| 561 | } |
| 562 | } |
| 563 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 564 | void InputReader::fadePointerLocked() { |
| 565 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 566 | InputDevice* device = mDevices.valueAt(i); |
| 567 | device->fadePointer(); |
| 568 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 569 | } |
| 570 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 571 | void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) { |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 572 | if (when < mNextTimeout) { |
| 573 | mNextTimeout = when; |
| 574 | } |
| 575 | } |
| 576 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 577 | void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 578 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 579 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 580 | *outConfiguration = mInputConfiguration; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 584 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 585 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 586 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 587 | if (deviceIndex < 0) { |
| 588 | return NAME_NOT_FOUND; |
| 589 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 590 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 591 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 592 | if (device->isIgnored()) { |
| 593 | return NAME_NOT_FOUND; |
| 594 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 595 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 596 | device->getDeviceInfo(outDeviceInfo); |
| 597 | return OK; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 601 | AutoMutex _l(mLock); |
| 602 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 603 | outDeviceIds.clear(); |
| 604 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 605 | size_t numDevices = mDevices.size(); |
| 606 | for (size_t i = 0; i < numDevices; i++) { |
| 607 | InputDevice* device = mDevices.valueAt(i); |
| 608 | if (!device->isIgnored()) { |
| 609 | outDeviceIds.add(device->getId()); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 610 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 611 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, |
| 615 | int32_t keyCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 616 | AutoMutex _l(mLock); |
| 617 | |
| 618 | return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, |
| 622 | int32_t scanCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 623 | AutoMutex _l(mLock); |
| 624 | |
| 625 | return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | 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] | 629 | AutoMutex _l(mLock); |
| 630 | |
| 631 | return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 634 | 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] | 635 | GetStateFunc getStateFunc) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 636 | int32_t result = AKEY_STATE_UNKNOWN; |
| 637 | if (deviceId >= 0) { |
| 638 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 639 | if (deviceIndex >= 0) { |
| 640 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 641 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 642 | result = (device->*getStateFunc)(sourceMask, code); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 643 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 644 | } |
| 645 | } else { |
| 646 | size_t numDevices = mDevices.size(); |
| 647 | for (size_t i = 0; i < numDevices; i++) { |
| 648 | InputDevice* device = mDevices.valueAt(i); |
| 649 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
David Deephanphongs | fbca596 | 2011-11-14 14:50:45 -0800 | [diff] [blame] | 650 | // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that |
| 651 | // value. Otherwise, return AKEY_STATE_UP as long as one device reports it. |
| 652 | int32_t currentResult = (device->*getStateFunc)(sourceMask, code); |
| 653 | if (currentResult >= AKEY_STATE_DOWN) { |
| 654 | return currentResult; |
| 655 | } else if (currentResult == AKEY_STATE_UP) { |
| 656 | result = currentResult; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 660 | } |
| 661 | return result; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, |
| 665 | size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 666 | AutoMutex _l(mLock); |
| 667 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 668 | memset(outFlags, 0, numCodes); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 669 | return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 672 | bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, |
| 673 | size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) { |
| 674 | bool result = false; |
| 675 | if (deviceId >= 0) { |
| 676 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 677 | if (deviceIndex >= 0) { |
| 678 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 679 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 680 | result = device->markSupportedKeyCodes(sourceMask, |
| 681 | numCodes, keyCodes, outFlags); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 682 | } |
| 683 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 684 | } else { |
| 685 | size_t numDevices = mDevices.size(); |
| 686 | for (size_t i = 0; i < numDevices; i++) { |
| 687 | InputDevice* device = mDevices.valueAt(i); |
| 688 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 689 | result |= device->markSupportedKeyCodes(sourceMask, |
| 690 | numCodes, keyCodes, outFlags); |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | return result; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 695 | } |
| 696 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 697 | void InputReader::requestRefreshConfiguration(uint32_t changes) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 698 | AutoMutex _l(mLock); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 699 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 700 | if (changes) { |
| 701 | bool needWake = !mConfigurationChangesToRefresh; |
| 702 | mConfigurationChangesToRefresh |= changes; |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 703 | |
| 704 | if (needWake) { |
| 705 | mEventHub->wake(); |
| 706 | } |
| 707 | } |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 710 | void InputReader::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 711 | AutoMutex _l(mLock); |
| 712 | |
Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 713 | mEventHub->dump(dump); |
| 714 | dump.append("\n"); |
| 715 | |
| 716 | dump.append("Input Reader State:\n"); |
| 717 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 718 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 719 | mDevices.valueAt(i)->dump(dump); |
| 720 | } |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 721 | |
| 722 | dump.append(INDENT "Configuration:\n"); |
| 723 | dump.append(INDENT2 "ExcludedDeviceNames: ["); |
| 724 | for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) { |
| 725 | if (i != 0) { |
| 726 | dump.append(", "); |
| 727 | } |
| 728 | dump.append(mConfig.excludedDeviceNames.itemAt(i).string()); |
| 729 | } |
| 730 | dump.append("]\n"); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 731 | dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n", |
| 732 | mConfig.virtualKeyQuietTime * 0.000001f); |
| 733 | |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 734 | dump.appendFormat(INDENT2 "PointerVelocityControlParameters: " |
| 735 | "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n", |
| 736 | mConfig.pointerVelocityControlParameters.scale, |
| 737 | mConfig.pointerVelocityControlParameters.lowThreshold, |
| 738 | mConfig.pointerVelocityControlParameters.highThreshold, |
| 739 | mConfig.pointerVelocityControlParameters.acceleration); |
| 740 | |
| 741 | dump.appendFormat(INDENT2 "WheelVelocityControlParameters: " |
| 742 | "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n", |
| 743 | mConfig.wheelVelocityControlParameters.scale, |
| 744 | mConfig.wheelVelocityControlParameters.lowThreshold, |
| 745 | mConfig.wheelVelocityControlParameters.highThreshold, |
| 746 | mConfig.wheelVelocityControlParameters.acceleration); |
| 747 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 748 | dump.appendFormat(INDENT2 "PointerGesture:\n"); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 749 | dump.appendFormat(INDENT3 "Enabled: %s\n", |
| 750 | toString(mConfig.pointerGesturesEnabled)); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 751 | dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n", |
| 752 | mConfig.pointerGestureQuietInterval * 0.000001f); |
| 753 | dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n", |
| 754 | mConfig.pointerGestureDragMinSwitchSpeed); |
| 755 | dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n", |
| 756 | mConfig.pointerGestureTapInterval * 0.000001f); |
| 757 | dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n", |
| 758 | mConfig.pointerGestureTapDragInterval * 0.000001f); |
| 759 | dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n", |
| 760 | mConfig.pointerGestureTapSlop); |
| 761 | dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n", |
| 762 | mConfig.pointerGestureMultitouchSettleInterval * 0.000001f); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 763 | dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n", |
| 764 | mConfig.pointerGestureMultitouchMinDistance); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 765 | dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n", |
| 766 | mConfig.pointerGestureSwipeTransitionAngleCosine); |
| 767 | dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n", |
| 768 | mConfig.pointerGestureSwipeMaxWidthRatio); |
| 769 | dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n", |
| 770 | mConfig.pointerGestureMovementSpeedRatio); |
| 771 | dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n", |
| 772 | mConfig.pointerGestureZoomSpeedRatio); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Jeff Brown | 89ef072 | 2011-08-10 16:25:21 -0700 | [diff] [blame] | 775 | void InputReader::monitor() { |
| 776 | // Acquire and release the lock to ensure that the reader has not deadlocked. |
| 777 | mLock.lock(); |
Jeff Brown | 112b5f5 | 2012-01-27 17:32:06 -0800 | [diff] [blame] | 778 | mEventHub->wake(); |
| 779 | mReaderIsAliveCondition.wait(mLock); |
Jeff Brown | 89ef072 | 2011-08-10 16:25:21 -0700 | [diff] [blame] | 780 | mLock.unlock(); |
| 781 | |
| 782 | // Check the EventHub |
| 783 | mEventHub->monitor(); |
| 784 | } |
| 785 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 786 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 787 | // --- InputReader::ContextImpl --- |
| 788 | |
| 789 | InputReader::ContextImpl::ContextImpl(InputReader* reader) : |
| 790 | mReader(reader) { |
| 791 | } |
| 792 | |
| 793 | void InputReader::ContextImpl::updateGlobalMetaState() { |
| 794 | // lock is already held by the input loop |
| 795 | mReader->updateGlobalMetaStateLocked(); |
| 796 | } |
| 797 | |
| 798 | int32_t InputReader::ContextImpl::getGlobalMetaState() { |
| 799 | // lock is already held by the input loop |
| 800 | return mReader->getGlobalMetaStateLocked(); |
| 801 | } |
| 802 | |
| 803 | void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) { |
| 804 | // lock is already held by the input loop |
| 805 | mReader->disableVirtualKeysUntilLocked(time); |
| 806 | } |
| 807 | |
| 808 | bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, |
| 809 | InputDevice* device, int32_t keyCode, int32_t scanCode) { |
| 810 | // lock is already held by the input loop |
| 811 | return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode); |
| 812 | } |
| 813 | |
| 814 | void InputReader::ContextImpl::fadePointer() { |
| 815 | // lock is already held by the input loop |
| 816 | mReader->fadePointerLocked(); |
| 817 | } |
| 818 | |
| 819 | void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) { |
| 820 | // lock is already held by the input loop |
| 821 | mReader->requestTimeoutAtTimeLocked(when); |
| 822 | } |
| 823 | |
| 824 | InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() { |
| 825 | return mReader->mPolicy.get(); |
| 826 | } |
| 827 | |
| 828 | InputListenerInterface* InputReader::ContextImpl::getListener() { |
| 829 | return mReader->mQueuedListener.get(); |
| 830 | } |
| 831 | |
| 832 | EventHubInterface* InputReader::ContextImpl::getEventHub() { |
| 833 | return mReader->mEventHub.get(); |
| 834 | } |
| 835 | |
| 836 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 837 | // --- InputReaderThread --- |
| 838 | |
| 839 | InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) : |
| 840 | Thread(/*canCallJava*/ true), mReader(reader) { |
| 841 | } |
| 842 | |
| 843 | InputReaderThread::~InputReaderThread() { |
| 844 | } |
| 845 | |
| 846 | bool InputReaderThread::threadLoop() { |
| 847 | mReader->loopOnce(); |
| 848 | return true; |
| 849 | } |
| 850 | |
| 851 | |
| 852 | // --- InputDevice --- |
| 853 | |
Jeff Brown | 9ee285af | 2011-08-31 12:56:34 -0700 | [diff] [blame] | 854 | InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name, |
| 855 | uint32_t classes) : |
| 856 | mContext(context), mId(id), mName(name), mClasses(classes), |
| 857 | mSources(0), mIsExternal(false), mDropUntilNextSync(false) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | InputDevice::~InputDevice() { |
| 861 | size_t numMappers = mMappers.size(); |
| 862 | for (size_t i = 0; i < numMappers; i++) { |
| 863 | delete mMappers[i]; |
| 864 | } |
| 865 | mMappers.clear(); |
| 866 | } |
| 867 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 868 | void InputDevice::dump(String8& dump) { |
| 869 | InputDeviceInfo deviceInfo; |
| 870 | getDeviceInfo(& deviceInfo); |
| 871 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 872 | dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(), |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 873 | deviceInfo.getName().string()); |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 874 | dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal)); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 875 | dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources()); |
| 876 | dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType()); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 877 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 878 | const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges(); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 879 | if (!ranges.isEmpty()) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 880 | dump.append(INDENT2 "Motion Ranges:\n"); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 881 | for (size_t i = 0; i < ranges.size(); i++) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 882 | const InputDeviceInfo::MotionRange& range = ranges.itemAt(i); |
| 883 | const char* label = getAxisLabel(range.axis); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 884 | char name[32]; |
| 885 | if (label) { |
| 886 | strncpy(name, label, sizeof(name)); |
| 887 | name[sizeof(name) - 1] = '\0'; |
| 888 | } else { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 889 | snprintf(name, sizeof(name), "%d", range.axis); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 890 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 891 | dump.appendFormat(INDENT3 "%s: source=0x%08x, " |
| 892 | "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n", |
| 893 | name, range.source, range.min, range.max, range.flat, range.fuzz); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 894 | } |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | size_t numMappers = mMappers.size(); |
| 898 | for (size_t i = 0; i < numMappers; i++) { |
| 899 | InputMapper* mapper = mMappers[i]; |
| 900 | mapper->dump(dump); |
| 901 | } |
| 902 | } |
| 903 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 904 | void InputDevice::addMapper(InputMapper* mapper) { |
| 905 | mMappers.add(mapper); |
| 906 | } |
| 907 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 908 | void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 909 | mSources = 0; |
| 910 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 911 | if (!isIgnored()) { |
| 912 | if (!changes) { // first time only |
| 913 | mContext->getEventHub()->getConfiguration(mId, &mConfiguration); |
| 914 | } |
| 915 | |
| 916 | size_t numMappers = mMappers.size(); |
| 917 | for (size_t i = 0; i < numMappers; i++) { |
| 918 | InputMapper* mapper = mMappers[i]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 919 | mapper->configure(when, config, changes); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 920 | mSources |= mapper->getSources(); |
| 921 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 925 | void InputDevice::reset(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 926 | size_t numMappers = mMappers.size(); |
| 927 | for (size_t i = 0; i < numMappers; i++) { |
| 928 | InputMapper* mapper = mMappers[i]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 929 | mapper->reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 930 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 931 | |
| 932 | mContext->updateGlobalMetaState(); |
| 933 | |
| 934 | notifyReset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 935 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 936 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 937 | void InputDevice::process(const RawEvent* rawEvents, size_t count) { |
| 938 | // Process all of the events in order for each mapper. |
| 939 | // We cannot simply ask each mapper to process them in bulk because mappers may |
| 940 | // have side-effects that must be interleaved. For example, joystick movement events and |
| 941 | // gamepad button presses are handled by different mappers but they should be dispatched |
| 942 | // in the order received. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 943 | size_t numMappers = mMappers.size(); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 944 | for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) { |
| 945 | #if DEBUG_RAW_EVENTS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 946 | ALOGD("Input event: device=%d type=0x%04x scancode=0x%04x " |
Jeff Brown | 2e45fb6 | 2011-06-29 21:19:05 -0700 | [diff] [blame] | 947 | "keycode=0x%04x value=0x%08x flags=0x%08x", |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 948 | rawEvent->deviceId, rawEvent->type, rawEvent->scanCode, rawEvent->keyCode, |
| 949 | rawEvent->value, rawEvent->flags); |
| 950 | #endif |
| 951 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 952 | if (mDropUntilNextSync) { |
| 953 | if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) { |
| 954 | mDropUntilNextSync = false; |
| 955 | #if DEBUG_RAW_EVENTS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 956 | ALOGD("Recovered from input event buffer overrun."); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 957 | #endif |
| 958 | } else { |
| 959 | #if DEBUG_RAW_EVENTS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 960 | ALOGD("Dropped input event while waiting for next input sync."); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 961 | #endif |
| 962 | } |
| 963 | } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_DROPPED) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 964 | ALOGI("Detected input event buffer overrun for device %s.", mName.string()); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 965 | mDropUntilNextSync = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 966 | reset(rawEvent->when); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 967 | } else { |
| 968 | for (size_t i = 0; i < numMappers; i++) { |
| 969 | InputMapper* mapper = mMappers[i]; |
| 970 | mapper->process(rawEvent); |
| 971 | } |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 972 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 973 | } |
| 974 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 975 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 976 | void InputDevice::timeoutExpired(nsecs_t when) { |
| 977 | size_t numMappers = mMappers.size(); |
| 978 | for (size_t i = 0; i < numMappers; i++) { |
| 979 | InputMapper* mapper = mMappers[i]; |
| 980 | mapper->timeoutExpired(when); |
| 981 | } |
| 982 | } |
| 983 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 984 | void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) { |
| 985 | outDeviceInfo->initialize(mId, mName); |
| 986 | |
| 987 | size_t numMappers = mMappers.size(); |
| 988 | for (size_t i = 0; i < numMappers; i++) { |
| 989 | InputMapper* mapper = mMappers[i]; |
| 990 | mapper->populateDeviceInfo(outDeviceInfo); |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 995 | return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState); |
| 996 | } |
| 997 | |
| 998 | int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 999 | return getState(sourceMask, scanCode, & InputMapper::getScanCodeState); |
| 1000 | } |
| 1001 | |
| 1002 | int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 1003 | return getState(sourceMask, switchCode, & InputMapper::getSwitchState); |
| 1004 | } |
| 1005 | |
| 1006 | int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) { |
| 1007 | int32_t result = AKEY_STATE_UNKNOWN; |
| 1008 | size_t numMappers = mMappers.size(); |
| 1009 | for (size_t i = 0; i < numMappers; i++) { |
| 1010 | InputMapper* mapper = mMappers[i]; |
| 1011 | if (sourcesMatchMask(mapper->getSources(), sourceMask)) { |
David Deephanphongs | fbca596 | 2011-11-14 14:50:45 -0800 | [diff] [blame] | 1012 | // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that |
| 1013 | // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it. |
| 1014 | int32_t currentResult = (mapper->*getStateFunc)(sourceMask, code); |
| 1015 | if (currentResult >= AKEY_STATE_DOWN) { |
| 1016 | return currentResult; |
| 1017 | } else if (currentResult == AKEY_STATE_UP) { |
| 1018 | result = currentResult; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | return result; |
| 1023 | } |
| 1024 | |
| 1025 | bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 1026 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 1027 | bool result = false; |
| 1028 | size_t numMappers = mMappers.size(); |
| 1029 | for (size_t i = 0; i < numMappers; i++) { |
| 1030 | InputMapper* mapper = mMappers[i]; |
| 1031 | if (sourcesMatchMask(mapper->getSources(), sourceMask)) { |
| 1032 | result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags); |
| 1033 | } |
| 1034 | } |
| 1035 | return result; |
| 1036 | } |
| 1037 | |
| 1038 | int32_t InputDevice::getMetaState() { |
| 1039 | int32_t result = 0; |
| 1040 | size_t numMappers = mMappers.size(); |
| 1041 | for (size_t i = 0; i < numMappers; i++) { |
| 1042 | InputMapper* mapper = mMappers[i]; |
| 1043 | result |= mapper->getMetaState(); |
| 1044 | } |
| 1045 | return result; |
| 1046 | } |
| 1047 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1048 | void InputDevice::fadePointer() { |
| 1049 | size_t numMappers = mMappers.size(); |
| 1050 | for (size_t i = 0; i < numMappers; i++) { |
| 1051 | InputMapper* mapper = mMappers[i]; |
| 1052 | mapper->fadePointer(); |
| 1053 | } |
| 1054 | } |
| 1055 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1056 | void InputDevice::notifyReset(nsecs_t when) { |
| 1057 | NotifyDeviceResetArgs args(when, mId); |
| 1058 | mContext->getListener()->notifyDeviceReset(&args); |
| 1059 | } |
| 1060 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1061 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1062 | // --- CursorButtonAccumulator --- |
| 1063 | |
| 1064 | CursorButtonAccumulator::CursorButtonAccumulator() { |
| 1065 | clearButtons(); |
| 1066 | } |
| 1067 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1068 | void CursorButtonAccumulator::reset(InputDevice* device) { |
| 1069 | mBtnLeft = device->isKeyPressed(BTN_LEFT); |
| 1070 | mBtnRight = device->isKeyPressed(BTN_RIGHT); |
| 1071 | mBtnMiddle = device->isKeyPressed(BTN_MIDDLE); |
| 1072 | mBtnBack = device->isKeyPressed(BTN_BACK); |
| 1073 | mBtnSide = device->isKeyPressed(BTN_SIDE); |
| 1074 | mBtnForward = device->isKeyPressed(BTN_FORWARD); |
| 1075 | mBtnExtra = device->isKeyPressed(BTN_EXTRA); |
| 1076 | mBtnTask = device->isKeyPressed(BTN_TASK); |
| 1077 | } |
| 1078 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1079 | void CursorButtonAccumulator::clearButtons() { |
| 1080 | mBtnLeft = 0; |
| 1081 | mBtnRight = 0; |
| 1082 | mBtnMiddle = 0; |
| 1083 | mBtnBack = 0; |
| 1084 | mBtnSide = 0; |
| 1085 | mBtnForward = 0; |
| 1086 | mBtnExtra = 0; |
| 1087 | mBtnTask = 0; |
| 1088 | } |
| 1089 | |
| 1090 | void CursorButtonAccumulator::process(const RawEvent* rawEvent) { |
| 1091 | if (rawEvent->type == EV_KEY) { |
| 1092 | switch (rawEvent->scanCode) { |
| 1093 | case BTN_LEFT: |
| 1094 | mBtnLeft = rawEvent->value; |
| 1095 | break; |
| 1096 | case BTN_RIGHT: |
| 1097 | mBtnRight = rawEvent->value; |
| 1098 | break; |
| 1099 | case BTN_MIDDLE: |
| 1100 | mBtnMiddle = rawEvent->value; |
| 1101 | break; |
| 1102 | case BTN_BACK: |
| 1103 | mBtnBack = rawEvent->value; |
| 1104 | break; |
| 1105 | case BTN_SIDE: |
| 1106 | mBtnSide = rawEvent->value; |
| 1107 | break; |
| 1108 | case BTN_FORWARD: |
| 1109 | mBtnForward = rawEvent->value; |
| 1110 | break; |
| 1111 | case BTN_EXTRA: |
| 1112 | mBtnExtra = rawEvent->value; |
| 1113 | break; |
| 1114 | case BTN_TASK: |
| 1115 | mBtnTask = rawEvent->value; |
| 1116 | break; |
| 1117 | } |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | uint32_t CursorButtonAccumulator::getButtonState() const { |
| 1122 | uint32_t result = 0; |
| 1123 | if (mBtnLeft) { |
| 1124 | result |= AMOTION_EVENT_BUTTON_PRIMARY; |
| 1125 | } |
| 1126 | if (mBtnRight) { |
| 1127 | result |= AMOTION_EVENT_BUTTON_SECONDARY; |
| 1128 | } |
| 1129 | if (mBtnMiddle) { |
| 1130 | result |= AMOTION_EVENT_BUTTON_TERTIARY; |
| 1131 | } |
| 1132 | if (mBtnBack || mBtnSide) { |
| 1133 | result |= AMOTION_EVENT_BUTTON_BACK; |
| 1134 | } |
| 1135 | if (mBtnForward || mBtnExtra) { |
| 1136 | result |= AMOTION_EVENT_BUTTON_FORWARD; |
| 1137 | } |
| 1138 | return result; |
| 1139 | } |
| 1140 | |
| 1141 | |
| 1142 | // --- CursorMotionAccumulator --- |
| 1143 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1144 | CursorMotionAccumulator::CursorMotionAccumulator() { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1145 | clearRelativeAxes(); |
| 1146 | } |
| 1147 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1148 | void CursorMotionAccumulator::reset(InputDevice* device) { |
| 1149 | clearRelativeAxes(); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | void CursorMotionAccumulator::clearRelativeAxes() { |
| 1153 | mRelX = 0; |
| 1154 | mRelY = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | void CursorMotionAccumulator::process(const RawEvent* rawEvent) { |
| 1158 | if (rawEvent->type == EV_REL) { |
| 1159 | switch (rawEvent->scanCode) { |
| 1160 | case REL_X: |
| 1161 | mRelX = rawEvent->value; |
| 1162 | break; |
| 1163 | case REL_Y: |
| 1164 | mRelY = rawEvent->value; |
| 1165 | break; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1166 | } |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | void CursorMotionAccumulator::finishSync() { |
| 1171 | clearRelativeAxes(); |
| 1172 | } |
| 1173 | |
| 1174 | |
| 1175 | // --- CursorScrollAccumulator --- |
| 1176 | |
| 1177 | CursorScrollAccumulator::CursorScrollAccumulator() : |
| 1178 | mHaveRelWheel(false), mHaveRelHWheel(false) { |
| 1179 | clearRelativeAxes(); |
| 1180 | } |
| 1181 | |
| 1182 | void CursorScrollAccumulator::configure(InputDevice* device) { |
| 1183 | mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL); |
| 1184 | mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL); |
| 1185 | } |
| 1186 | |
| 1187 | void CursorScrollAccumulator::reset(InputDevice* device) { |
| 1188 | clearRelativeAxes(); |
| 1189 | } |
| 1190 | |
| 1191 | void CursorScrollAccumulator::clearRelativeAxes() { |
| 1192 | mRelWheel = 0; |
| 1193 | mRelHWheel = 0; |
| 1194 | } |
| 1195 | |
| 1196 | void CursorScrollAccumulator::process(const RawEvent* rawEvent) { |
| 1197 | if (rawEvent->type == EV_REL) { |
| 1198 | switch (rawEvent->scanCode) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1199 | case REL_WHEEL: |
| 1200 | mRelWheel = rawEvent->value; |
| 1201 | break; |
| 1202 | case REL_HWHEEL: |
| 1203 | mRelHWheel = rawEvent->value; |
| 1204 | break; |
| 1205 | } |
| 1206 | } |
| 1207 | } |
| 1208 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1209 | void CursorScrollAccumulator::finishSync() { |
| 1210 | clearRelativeAxes(); |
| 1211 | } |
| 1212 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1213 | |
| 1214 | // --- TouchButtonAccumulator --- |
| 1215 | |
| 1216 | TouchButtonAccumulator::TouchButtonAccumulator() : |
| 1217 | mHaveBtnTouch(false) { |
| 1218 | clearButtons(); |
| 1219 | } |
| 1220 | |
| 1221 | void TouchButtonAccumulator::configure(InputDevice* device) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1222 | mHaveBtnTouch = device->hasKey(BTN_TOUCH); |
| 1223 | } |
| 1224 | |
| 1225 | void TouchButtonAccumulator::reset(InputDevice* device) { |
| 1226 | mBtnTouch = device->isKeyPressed(BTN_TOUCH); |
| 1227 | mBtnStylus = device->isKeyPressed(BTN_STYLUS); |
| 1228 | mBtnStylus2 = device->isKeyPressed(BTN_STYLUS); |
| 1229 | mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER); |
| 1230 | mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN); |
| 1231 | mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER); |
| 1232 | mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH); |
| 1233 | mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL); |
| 1234 | mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH); |
| 1235 | mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE); |
| 1236 | mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS); |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1237 | mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP); |
| 1238 | mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP); |
| 1239 | mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | void TouchButtonAccumulator::clearButtons() { |
| 1243 | mBtnTouch = 0; |
| 1244 | mBtnStylus = 0; |
| 1245 | mBtnStylus2 = 0; |
| 1246 | mBtnToolFinger = 0; |
| 1247 | mBtnToolPen = 0; |
| 1248 | mBtnToolRubber = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1249 | mBtnToolBrush = 0; |
| 1250 | mBtnToolPencil = 0; |
| 1251 | mBtnToolAirbrush = 0; |
| 1252 | mBtnToolMouse = 0; |
| 1253 | mBtnToolLens = 0; |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1254 | mBtnToolDoubleTap = 0; |
| 1255 | mBtnToolTripleTap = 0; |
| 1256 | mBtnToolQuadTap = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | void TouchButtonAccumulator::process(const RawEvent* rawEvent) { |
| 1260 | if (rawEvent->type == EV_KEY) { |
| 1261 | switch (rawEvent->scanCode) { |
| 1262 | case BTN_TOUCH: |
| 1263 | mBtnTouch = rawEvent->value; |
| 1264 | break; |
| 1265 | case BTN_STYLUS: |
| 1266 | mBtnStylus = rawEvent->value; |
| 1267 | break; |
| 1268 | case BTN_STYLUS2: |
| 1269 | mBtnStylus2 = rawEvent->value; |
| 1270 | break; |
| 1271 | case BTN_TOOL_FINGER: |
| 1272 | mBtnToolFinger = rawEvent->value; |
| 1273 | break; |
| 1274 | case BTN_TOOL_PEN: |
| 1275 | mBtnToolPen = rawEvent->value; |
| 1276 | break; |
| 1277 | case BTN_TOOL_RUBBER: |
| 1278 | mBtnToolRubber = rawEvent->value; |
| 1279 | break; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1280 | case BTN_TOOL_BRUSH: |
| 1281 | mBtnToolBrush = rawEvent->value; |
| 1282 | break; |
| 1283 | case BTN_TOOL_PENCIL: |
| 1284 | mBtnToolPencil = rawEvent->value; |
| 1285 | break; |
| 1286 | case BTN_TOOL_AIRBRUSH: |
| 1287 | mBtnToolAirbrush = rawEvent->value; |
| 1288 | break; |
| 1289 | case BTN_TOOL_MOUSE: |
| 1290 | mBtnToolMouse = rawEvent->value; |
| 1291 | break; |
| 1292 | case BTN_TOOL_LENS: |
| 1293 | mBtnToolLens = rawEvent->value; |
| 1294 | break; |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1295 | case BTN_TOOL_DOUBLETAP: |
| 1296 | mBtnToolDoubleTap = rawEvent->value; |
| 1297 | break; |
| 1298 | case BTN_TOOL_TRIPLETAP: |
| 1299 | mBtnToolTripleTap = rawEvent->value; |
| 1300 | break; |
| 1301 | case BTN_TOOL_QUADTAP: |
| 1302 | mBtnToolQuadTap = rawEvent->value; |
| 1303 | break; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1304 | } |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | uint32_t TouchButtonAccumulator::getButtonState() const { |
| 1309 | uint32_t result = 0; |
| 1310 | if (mBtnStylus) { |
| 1311 | result |= AMOTION_EVENT_BUTTON_SECONDARY; |
| 1312 | } |
| 1313 | if (mBtnStylus2) { |
| 1314 | result |= AMOTION_EVENT_BUTTON_TERTIARY; |
| 1315 | } |
| 1316 | return result; |
| 1317 | } |
| 1318 | |
| 1319 | int32_t TouchButtonAccumulator::getToolType() const { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1320 | if (mBtnToolMouse || mBtnToolLens) { |
| 1321 | return AMOTION_EVENT_TOOL_TYPE_MOUSE; |
| 1322 | } |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1323 | if (mBtnToolRubber) { |
| 1324 | return AMOTION_EVENT_TOOL_TYPE_ERASER; |
| 1325 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1326 | if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1327 | return AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 1328 | } |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1329 | if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1330 | return AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 1331 | } |
| 1332 | return AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
| 1333 | } |
| 1334 | |
Jeff Brown | d87c6d5 | 2011-08-10 14:55:59 -0700 | [diff] [blame] | 1335 | bool TouchButtonAccumulator::isToolActive() const { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1336 | return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber |
| 1337 | || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1338 | || mBtnToolMouse || mBtnToolLens |
| 1339 | || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | bool TouchButtonAccumulator::isHovering() const { |
| 1343 | return mHaveBtnTouch && !mBtnTouch; |
| 1344 | } |
| 1345 | |
| 1346 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1347 | // --- RawPointerAxes --- |
| 1348 | |
| 1349 | RawPointerAxes::RawPointerAxes() { |
| 1350 | clear(); |
| 1351 | } |
| 1352 | |
| 1353 | void RawPointerAxes::clear() { |
| 1354 | x.clear(); |
| 1355 | y.clear(); |
| 1356 | pressure.clear(); |
| 1357 | touchMajor.clear(); |
| 1358 | touchMinor.clear(); |
| 1359 | toolMajor.clear(); |
| 1360 | toolMinor.clear(); |
| 1361 | orientation.clear(); |
| 1362 | distance.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1363 | tiltX.clear(); |
| 1364 | tiltY.clear(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1365 | trackingId.clear(); |
| 1366 | slot.clear(); |
| 1367 | } |
| 1368 | |
| 1369 | |
| 1370 | // --- RawPointerData --- |
| 1371 | |
| 1372 | RawPointerData::RawPointerData() { |
| 1373 | clear(); |
| 1374 | } |
| 1375 | |
| 1376 | void RawPointerData::clear() { |
| 1377 | pointerCount = 0; |
| 1378 | clearIdBits(); |
| 1379 | } |
| 1380 | |
| 1381 | void RawPointerData::copyFrom(const RawPointerData& other) { |
| 1382 | pointerCount = other.pointerCount; |
| 1383 | hoveringIdBits = other.hoveringIdBits; |
| 1384 | touchingIdBits = other.touchingIdBits; |
| 1385 | |
| 1386 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 1387 | pointers[i] = other.pointers[i]; |
| 1388 | |
| 1389 | int id = pointers[i].id; |
| 1390 | idToIndex[id] = other.idToIndex[id]; |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const { |
| 1395 | float x = 0, y = 0; |
| 1396 | uint32_t count = touchingIdBits.count(); |
| 1397 | if (count) { |
| 1398 | for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) { |
| 1399 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 1400 | const Pointer& pointer = pointerForId(id); |
| 1401 | x += pointer.x; |
| 1402 | y += pointer.y; |
| 1403 | } |
| 1404 | x /= count; |
| 1405 | y /= count; |
| 1406 | } |
| 1407 | *outX = x; |
| 1408 | *outY = y; |
| 1409 | } |
| 1410 | |
| 1411 | |
| 1412 | // --- CookedPointerData --- |
| 1413 | |
| 1414 | CookedPointerData::CookedPointerData() { |
| 1415 | clear(); |
| 1416 | } |
| 1417 | |
| 1418 | void CookedPointerData::clear() { |
| 1419 | pointerCount = 0; |
| 1420 | hoveringIdBits.clear(); |
| 1421 | touchingIdBits.clear(); |
| 1422 | } |
| 1423 | |
| 1424 | void CookedPointerData::copyFrom(const CookedPointerData& other) { |
| 1425 | pointerCount = other.pointerCount; |
| 1426 | hoveringIdBits = other.hoveringIdBits; |
| 1427 | touchingIdBits = other.touchingIdBits; |
| 1428 | |
| 1429 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 1430 | pointerProperties[i].copyFrom(other.pointerProperties[i]); |
| 1431 | pointerCoords[i].copyFrom(other.pointerCoords[i]); |
| 1432 | |
| 1433 | int id = pointerProperties[i].id; |
| 1434 | idToIndex[id] = other.idToIndex[id]; |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1439 | // --- SingleTouchMotionAccumulator --- |
| 1440 | |
| 1441 | SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() { |
| 1442 | clearAbsoluteAxes(); |
| 1443 | } |
| 1444 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1445 | void SingleTouchMotionAccumulator::reset(InputDevice* device) { |
| 1446 | mAbsX = device->getAbsoluteAxisValue(ABS_X); |
| 1447 | mAbsY = device->getAbsoluteAxisValue(ABS_Y); |
| 1448 | mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE); |
| 1449 | mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH); |
| 1450 | mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE); |
| 1451 | mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X); |
| 1452 | mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y); |
| 1453 | } |
| 1454 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1455 | void SingleTouchMotionAccumulator::clearAbsoluteAxes() { |
| 1456 | mAbsX = 0; |
| 1457 | mAbsY = 0; |
| 1458 | mAbsPressure = 0; |
| 1459 | mAbsToolWidth = 0; |
| 1460 | mAbsDistance = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1461 | mAbsTiltX = 0; |
| 1462 | mAbsTiltY = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) { |
| 1466 | if (rawEvent->type == EV_ABS) { |
| 1467 | switch (rawEvent->scanCode) { |
| 1468 | case ABS_X: |
| 1469 | mAbsX = rawEvent->value; |
| 1470 | break; |
| 1471 | case ABS_Y: |
| 1472 | mAbsY = rawEvent->value; |
| 1473 | break; |
| 1474 | case ABS_PRESSURE: |
| 1475 | mAbsPressure = rawEvent->value; |
| 1476 | break; |
| 1477 | case ABS_TOOL_WIDTH: |
| 1478 | mAbsToolWidth = rawEvent->value; |
| 1479 | break; |
| 1480 | case ABS_DISTANCE: |
| 1481 | mAbsDistance = rawEvent->value; |
| 1482 | break; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1483 | case ABS_TILT_X: |
| 1484 | mAbsTiltX = rawEvent->value; |
| 1485 | break; |
| 1486 | case ABS_TILT_Y: |
| 1487 | mAbsTiltY = rawEvent->value; |
| 1488 | break; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1489 | } |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | |
| 1494 | // --- MultiTouchMotionAccumulator --- |
| 1495 | |
| 1496 | MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() : |
| 1497 | mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false) { |
| 1498 | } |
| 1499 | |
| 1500 | MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() { |
| 1501 | delete[] mSlots; |
| 1502 | } |
| 1503 | |
| 1504 | void MultiTouchMotionAccumulator::configure(size_t slotCount, bool usingSlotsProtocol) { |
| 1505 | mSlotCount = slotCount; |
| 1506 | mUsingSlotsProtocol = usingSlotsProtocol; |
| 1507 | |
| 1508 | delete[] mSlots; |
| 1509 | mSlots = new Slot[slotCount]; |
| 1510 | } |
| 1511 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1512 | void MultiTouchMotionAccumulator::reset(InputDevice* device) { |
| 1513 | // Unfortunately there is no way to read the initial contents of the slots. |
| 1514 | // So when we reset the accumulator, we must assume they are all zeroes. |
| 1515 | if (mUsingSlotsProtocol) { |
| 1516 | // Query the driver for the current slot index and use it as the initial slot |
| 1517 | // before we start reading events from the device. It is possible that the |
| 1518 | // current slot index will not be the same as it was when the first event was |
| 1519 | // written into the evdev buffer, which means the input mapper could start |
| 1520 | // out of sync with the initial state of the events in the evdev buffer. |
| 1521 | // In the extremely unlikely case that this happens, the data from |
| 1522 | // two slots will be confused until the next ABS_MT_SLOT event is received. |
| 1523 | // This can cause the touch point to "jump", but at least there will be |
| 1524 | // no stuck touches. |
| 1525 | int32_t initialSlot; |
| 1526 | status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(), |
| 1527 | ABS_MT_SLOT, &initialSlot); |
| 1528 | if (status) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1529 | ALOGD("Could not retrieve current multitouch slot index. status=%d", status); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1530 | initialSlot = -1; |
| 1531 | } |
| 1532 | clearSlots(initialSlot); |
| 1533 | } else { |
| 1534 | clearSlots(-1); |
| 1535 | } |
| 1536 | } |
| 1537 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1538 | void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1539 | if (mSlots) { |
| 1540 | for (size_t i = 0; i < mSlotCount; i++) { |
| 1541 | mSlots[i].clear(); |
| 1542 | } |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1543 | } |
| 1544 | mCurrentSlot = initialSlot; |
| 1545 | } |
| 1546 | |
| 1547 | void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) { |
| 1548 | if (rawEvent->type == EV_ABS) { |
| 1549 | bool newSlot = false; |
| 1550 | if (mUsingSlotsProtocol) { |
| 1551 | if (rawEvent->scanCode == ABS_MT_SLOT) { |
| 1552 | mCurrentSlot = rawEvent->value; |
| 1553 | newSlot = true; |
| 1554 | } |
| 1555 | } else if (mCurrentSlot < 0) { |
| 1556 | mCurrentSlot = 0; |
| 1557 | } |
| 1558 | |
| 1559 | if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) { |
| 1560 | #if DEBUG_POINTERS |
| 1561 | if (newSlot) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1562 | ALOGW("MultiTouch device emitted invalid slot index %d but it " |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1563 | "should be between 0 and %d; ignoring this slot.", |
| 1564 | mCurrentSlot, mSlotCount - 1); |
| 1565 | } |
| 1566 | #endif |
| 1567 | } else { |
| 1568 | Slot* slot = &mSlots[mCurrentSlot]; |
| 1569 | |
| 1570 | switch (rawEvent->scanCode) { |
| 1571 | case ABS_MT_POSITION_X: |
| 1572 | slot->mInUse = true; |
| 1573 | slot->mAbsMTPositionX = rawEvent->value; |
| 1574 | break; |
| 1575 | case ABS_MT_POSITION_Y: |
| 1576 | slot->mInUse = true; |
| 1577 | slot->mAbsMTPositionY = rawEvent->value; |
| 1578 | break; |
| 1579 | case ABS_MT_TOUCH_MAJOR: |
| 1580 | slot->mInUse = true; |
| 1581 | slot->mAbsMTTouchMajor = rawEvent->value; |
| 1582 | break; |
| 1583 | case ABS_MT_TOUCH_MINOR: |
| 1584 | slot->mInUse = true; |
| 1585 | slot->mAbsMTTouchMinor = rawEvent->value; |
| 1586 | slot->mHaveAbsMTTouchMinor = true; |
| 1587 | break; |
| 1588 | case ABS_MT_WIDTH_MAJOR: |
| 1589 | slot->mInUse = true; |
| 1590 | slot->mAbsMTWidthMajor = rawEvent->value; |
| 1591 | break; |
| 1592 | case ABS_MT_WIDTH_MINOR: |
| 1593 | slot->mInUse = true; |
| 1594 | slot->mAbsMTWidthMinor = rawEvent->value; |
| 1595 | slot->mHaveAbsMTWidthMinor = true; |
| 1596 | break; |
| 1597 | case ABS_MT_ORIENTATION: |
| 1598 | slot->mInUse = true; |
| 1599 | slot->mAbsMTOrientation = rawEvent->value; |
| 1600 | break; |
| 1601 | case ABS_MT_TRACKING_ID: |
| 1602 | if (mUsingSlotsProtocol && rawEvent->value < 0) { |
Jeff Brown | 8bcbbef | 2011-08-11 15:49:09 -0700 | [diff] [blame] | 1603 | // The slot is no longer in use but it retains its previous contents, |
| 1604 | // which may be reused for subsequent touches. |
| 1605 | slot->mInUse = false; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1606 | } else { |
| 1607 | slot->mInUse = true; |
| 1608 | slot->mAbsMTTrackingId = rawEvent->value; |
| 1609 | } |
| 1610 | break; |
| 1611 | case ABS_MT_PRESSURE: |
| 1612 | slot->mInUse = true; |
| 1613 | slot->mAbsMTPressure = rawEvent->value; |
| 1614 | break; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1615 | case ABS_MT_DISTANCE: |
| 1616 | slot->mInUse = true; |
| 1617 | slot->mAbsMTDistance = rawEvent->value; |
| 1618 | break; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1619 | case ABS_MT_TOOL_TYPE: |
| 1620 | slot->mInUse = true; |
| 1621 | slot->mAbsMTToolType = rawEvent->value; |
| 1622 | slot->mHaveAbsMTToolType = true; |
| 1623 | break; |
| 1624 | } |
| 1625 | } |
| 1626 | } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_MT_REPORT) { |
| 1627 | // MultiTouch Sync: The driver has returned all data for *one* of the pointers. |
| 1628 | mCurrentSlot += 1; |
| 1629 | } |
| 1630 | } |
| 1631 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1632 | void MultiTouchMotionAccumulator::finishSync() { |
| 1633 | if (!mUsingSlotsProtocol) { |
| 1634 | clearSlots(-1); |
| 1635 | } |
| 1636 | } |
| 1637 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1638 | |
| 1639 | // --- MultiTouchMotionAccumulator::Slot --- |
| 1640 | |
| 1641 | MultiTouchMotionAccumulator::Slot::Slot() { |
| 1642 | clear(); |
| 1643 | } |
| 1644 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1645 | void MultiTouchMotionAccumulator::Slot::clear() { |
| 1646 | mInUse = false; |
| 1647 | mHaveAbsMTTouchMinor = false; |
| 1648 | mHaveAbsMTWidthMinor = false; |
| 1649 | mHaveAbsMTToolType = false; |
| 1650 | mAbsMTPositionX = 0; |
| 1651 | mAbsMTPositionY = 0; |
| 1652 | mAbsMTTouchMajor = 0; |
| 1653 | mAbsMTTouchMinor = 0; |
| 1654 | mAbsMTWidthMajor = 0; |
| 1655 | mAbsMTWidthMinor = 0; |
| 1656 | mAbsMTOrientation = 0; |
| 1657 | mAbsMTTrackingId = -1; |
| 1658 | mAbsMTPressure = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1659 | mAbsMTDistance = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1660 | mAbsMTToolType = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | int32_t MultiTouchMotionAccumulator::Slot::getToolType() const { |
| 1664 | if (mHaveAbsMTToolType) { |
| 1665 | switch (mAbsMTToolType) { |
| 1666 | case MT_TOOL_FINGER: |
| 1667 | return AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 1668 | case MT_TOOL_PEN: |
| 1669 | return AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 1670 | } |
| 1671 | } |
| 1672 | return AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
| 1673 | } |
| 1674 | |
| 1675 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1676 | // --- InputMapper --- |
| 1677 | |
| 1678 | InputMapper::InputMapper(InputDevice* device) : |
| 1679 | mDevice(device), mContext(device->getContext()) { |
| 1680 | } |
| 1681 | |
| 1682 | InputMapper::~InputMapper() { |
| 1683 | } |
| 1684 | |
| 1685 | void InputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 1686 | info->addSource(getSources()); |
| 1687 | } |
| 1688 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1689 | void InputMapper::dump(String8& dump) { |
| 1690 | } |
| 1691 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1692 | void InputMapper::configure(nsecs_t when, |
| 1693 | const InputReaderConfiguration* config, uint32_t changes) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1694 | } |
| 1695 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1696 | void InputMapper::reset(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1697 | } |
| 1698 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 1699 | void InputMapper::timeoutExpired(nsecs_t when) { |
| 1700 | } |
| 1701 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1702 | int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 1703 | return AKEY_STATE_UNKNOWN; |
| 1704 | } |
| 1705 | |
| 1706 | int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 1707 | return AKEY_STATE_UNKNOWN; |
| 1708 | } |
| 1709 | |
| 1710 | int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 1711 | return AKEY_STATE_UNKNOWN; |
| 1712 | } |
| 1713 | |
| 1714 | bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 1715 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 1716 | return false; |
| 1717 | } |
| 1718 | |
| 1719 | int32_t InputMapper::getMetaState() { |
| 1720 | return 0; |
| 1721 | } |
| 1722 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1723 | void InputMapper::fadePointer() { |
| 1724 | } |
| 1725 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1726 | status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) { |
| 1727 | return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo); |
| 1728 | } |
| 1729 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1730 | void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump, |
| 1731 | const RawAbsoluteAxisInfo& axis, const char* name) { |
| 1732 | if (axis.valid) { |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 1733 | dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n", |
| 1734 | name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1735 | } else { |
| 1736 | dump.appendFormat(INDENT4 "%s: unknown range\n", name); |
| 1737 | } |
| 1738 | } |
| 1739 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1740 | |
| 1741 | // --- SwitchInputMapper --- |
| 1742 | |
| 1743 | SwitchInputMapper::SwitchInputMapper(InputDevice* device) : |
| 1744 | InputMapper(device) { |
| 1745 | } |
| 1746 | |
| 1747 | SwitchInputMapper::~SwitchInputMapper() { |
| 1748 | } |
| 1749 | |
| 1750 | uint32_t SwitchInputMapper::getSources() { |
Jeff Brown | 89de57a | 2011-01-19 18:41:38 -0800 | [diff] [blame] | 1751 | return AINPUT_SOURCE_SWITCH; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1752 | } |
| 1753 | |
| 1754 | void SwitchInputMapper::process(const RawEvent* rawEvent) { |
| 1755 | switch (rawEvent->type) { |
| 1756 | case EV_SW: |
| 1757 | processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value); |
| 1758 | break; |
| 1759 | } |
| 1760 | } |
| 1761 | |
| 1762 | void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1763 | NotifySwitchArgs args(when, 0, switchCode, switchValue); |
| 1764 | getListener()->notifySwitch(&args); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 1768 | return getEventHub()->getSwitchState(getDeviceId(), switchCode); |
| 1769 | } |
| 1770 | |
| 1771 | |
| 1772 | // --- KeyboardInputMapper --- |
| 1773 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1774 | KeyboardInputMapper::KeyboardInputMapper(InputDevice* device, |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1775 | uint32_t source, int32_t keyboardType) : |
| 1776 | InputMapper(device), mSource(source), |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1777 | mKeyboardType(keyboardType) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | KeyboardInputMapper::~KeyboardInputMapper() { |
| 1781 | } |
| 1782 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1783 | uint32_t KeyboardInputMapper::getSources() { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1784 | return mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 1788 | InputMapper::populateDeviceInfo(info); |
| 1789 | |
| 1790 | info->setKeyboardType(mKeyboardType); |
Jeff Brown | 1e08fe9 | 2011-11-15 17:48:10 -0800 | [diff] [blame] | 1791 | info->setKeyCharacterMapFile(getEventHub()->getKeyCharacterMapFile(getDeviceId())); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1792 | } |
| 1793 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1794 | void KeyboardInputMapper::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1795 | dump.append(INDENT2 "Keyboard Input Mapper:\n"); |
| 1796 | dumpParameters(dump); |
| 1797 | dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1798 | dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1799 | dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size()); |
| 1800 | dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState); |
| 1801 | dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1802 | } |
| 1803 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1804 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1805 | void KeyboardInputMapper::configure(nsecs_t when, |
| 1806 | const InputReaderConfiguration* config, uint32_t changes) { |
| 1807 | InputMapper::configure(when, config, changes); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1808 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 1809 | if (!changes) { // first time only |
| 1810 | // Configure basic parameters. |
| 1811 | configureParameters(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1812 | } |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1813 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1814 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) { |
| 1815 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) { |
| 1816 | if (!config->getDisplayInfo(mParameters.associatedDisplayId, |
| 1817 | false /*external*/, NULL, NULL, &mOrientation)) { |
| 1818 | mOrientation = DISPLAY_ORIENTATION_0; |
| 1819 | } |
| 1820 | } else { |
| 1821 | mOrientation = DISPLAY_ORIENTATION_0; |
| 1822 | } |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1823 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1824 | } |
| 1825 | |
| 1826 | void KeyboardInputMapper::configureParameters() { |
| 1827 | mParameters.orientationAware = false; |
| 1828 | getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"), |
| 1829 | mParameters.orientationAware); |
| 1830 | |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 1831 | mParameters.associatedDisplayId = -1; |
| 1832 | if (mParameters.orientationAware) { |
| 1833 | mParameters.associatedDisplayId = 0; |
| 1834 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | void KeyboardInputMapper::dumpParameters(String8& dump) { |
| 1838 | dump.append(INDENT3 "Parameters:\n"); |
| 1839 | dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n", |
| 1840 | mParameters.associatedDisplayId); |
| 1841 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 1842 | toString(mParameters.orientationAware)); |
| 1843 | } |
| 1844 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1845 | void KeyboardInputMapper::reset(nsecs_t when) { |
| 1846 | mMetaState = AMETA_NONE; |
| 1847 | mDownTime = 0; |
| 1848 | mKeyDowns.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1849 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1850 | resetLedState(); |
| 1851 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1852 | InputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | void KeyboardInputMapper::process(const RawEvent* rawEvent) { |
| 1856 | switch (rawEvent->type) { |
| 1857 | case EV_KEY: { |
| 1858 | int32_t scanCode = rawEvent->scanCode; |
| 1859 | if (isKeyboardOrGamepadKey(scanCode)) { |
| 1860 | processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode, |
| 1861 | rawEvent->flags); |
| 1862 | } |
| 1863 | break; |
| 1864 | } |
| 1865 | } |
| 1866 | } |
| 1867 | |
| 1868 | bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) { |
| 1869 | return scanCode < BTN_MOUSE |
| 1870 | || scanCode >= KEY_OK |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 1871 | || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE) |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1872 | || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1873 | } |
| 1874 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1875 | void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode, |
| 1876 | int32_t scanCode, uint32_t policyFlags) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1877 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1878 | if (down) { |
| 1879 | // Rotate key codes according to orientation if needed. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1880 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1881 | keyCode = rotateKeyCode(keyCode, mOrientation); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1882 | } |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 1883 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1884 | // Add key down. |
| 1885 | ssize_t keyDownIndex = findKeyDown(scanCode); |
| 1886 | if (keyDownIndex >= 0) { |
| 1887 | // key repeat, be sure to use same keycode as before in case of rotation |
| 1888 | keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1889 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1890 | // key down |
| 1891 | if ((policyFlags & POLICY_FLAG_VIRTUAL) |
| 1892 | && mContext->shouldDropVirtualKey(when, |
| 1893 | getDevice(), keyCode, scanCode)) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1894 | return; |
| 1895 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1896 | |
| 1897 | mKeyDowns.push(); |
| 1898 | KeyDown& keyDown = mKeyDowns.editTop(); |
| 1899 | keyDown.keyCode = keyCode; |
| 1900 | keyDown.scanCode = scanCode; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1901 | } |
| 1902 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1903 | mDownTime = when; |
| 1904 | } else { |
| 1905 | // Remove key down. |
| 1906 | ssize_t keyDownIndex = findKeyDown(scanCode); |
| 1907 | if (keyDownIndex >= 0) { |
| 1908 | // key up, be sure to use same keycode as before in case of rotation |
| 1909 | keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode; |
| 1910 | mKeyDowns.removeAt(size_t(keyDownIndex)); |
| 1911 | } else { |
| 1912 | // key was not actually down |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1913 | 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] | 1914 | "keyCode=%d, scanCode=%d", |
| 1915 | getDeviceName().string(), keyCode, scanCode); |
| 1916 | return; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1917 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1918 | } |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 1919 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1920 | bool metaStateChanged = false; |
| 1921 | int32_t oldMetaState = mMetaState; |
| 1922 | int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState); |
| 1923 | if (oldMetaState != newMetaState) { |
| 1924 | mMetaState = newMetaState; |
| 1925 | metaStateChanged = true; |
| 1926 | updateLedState(false); |
| 1927 | } |
| 1928 | |
| 1929 | nsecs_t downTime = mDownTime; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1930 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1931 | // Key down on external an keyboard should wake the device. |
| 1932 | // We don't do this for internal keyboards to prevent them from waking up in your pocket. |
| 1933 | // For internal keyboards, the key layout file should specify the policy flags for |
| 1934 | // each wake key individually. |
| 1935 | // TODO: Use the input device configuration to control this behavior more finely. |
| 1936 | if (down && getDevice()->isExternal() |
| 1937 | && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) { |
| 1938 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 1939 | } |
| 1940 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1941 | if (metaStateChanged) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1942 | getContext()->updateGlobalMetaState(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1943 | } |
| 1944 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1945 | if (down && !isMetaKey(keyCode)) { |
| 1946 | getContext()->fadePointer(); |
| 1947 | } |
| 1948 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1949 | NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1950 | down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, |
| 1951 | AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1952 | getListener()->notifyKey(&args); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1953 | } |
| 1954 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1955 | ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) { |
| 1956 | size_t n = mKeyDowns.size(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1957 | for (size_t i = 0; i < n; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1958 | if (mKeyDowns[i].scanCode == scanCode) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1959 | return i; |
| 1960 | } |
| 1961 | } |
| 1962 | return -1; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1963 | } |
| 1964 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1965 | int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 1966 | return getEventHub()->getKeyCodeState(getDeviceId(), keyCode); |
| 1967 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1968 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1969 | int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 1970 | return getEventHub()->getScanCodeState(getDeviceId(), scanCode); |
| 1971 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1972 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1973 | bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 1974 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 1975 | return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags); |
| 1976 | } |
| 1977 | |
| 1978 | int32_t KeyboardInputMapper::getMetaState() { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1979 | return mMetaState; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1980 | } |
| 1981 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1982 | void KeyboardInputMapper::resetLedState() { |
| 1983 | initializeLedState(mCapsLockLedState, LED_CAPSL); |
| 1984 | initializeLedState(mNumLockLedState, LED_NUML); |
| 1985 | initializeLedState(mScrollLockLedState, LED_SCROLLL); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1986 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1987 | updateLedState(true); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1988 | } |
| 1989 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1990 | void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) { |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1991 | ledState.avail = getEventHub()->hasLed(getDeviceId(), led); |
| 1992 | ledState.on = false; |
| 1993 | } |
| 1994 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1995 | void KeyboardInputMapper::updateLedState(bool reset) { |
| 1996 | updateLedStateForModifier(mCapsLockLedState, LED_CAPSL, |
Jeff Brown | 51e7fe75 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 1997 | AMETA_CAPS_LOCK_ON, reset); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1998 | updateLedStateForModifier(mNumLockLedState, LED_NUML, |
Jeff Brown | 51e7fe75 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 1999 | AMETA_NUM_LOCK_ON, reset); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2000 | updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL, |
Jeff Brown | 51e7fe75 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 2001 | AMETA_SCROLL_LOCK_ON, reset); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 2002 | } |
| 2003 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2004 | void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState, |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 2005 | int32_t led, int32_t modifier, bool reset) { |
| 2006 | if (ledState.avail) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2007 | bool desiredState = (mMetaState & modifier) != 0; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2008 | if (reset || ledState.on != desiredState) { |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 2009 | getEventHub()->setLedState(getDeviceId(), led, desiredState); |
| 2010 | ledState.on = desiredState; |
| 2011 | } |
| 2012 | } |
| 2013 | } |
| 2014 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2015 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2016 | // --- CursorInputMapper --- |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2017 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2018 | CursorInputMapper::CursorInputMapper(InputDevice* device) : |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2019 | InputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2020 | } |
| 2021 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2022 | CursorInputMapper::~CursorInputMapper() { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2023 | } |
| 2024 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2025 | uint32_t CursorInputMapper::getSources() { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2026 | return mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2027 | } |
| 2028 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2029 | void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2030 | InputMapper::populateDeviceInfo(info); |
| 2031 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2032 | if (mParameters.mode == Parameters::MODE_POINTER) { |
| 2033 | float minX, minY, maxX, maxY; |
| 2034 | if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2035 | info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f); |
| 2036 | 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] | 2037 | } |
| 2038 | } else { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2039 | info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale); |
| 2040 | 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] | 2041 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2042 | 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] | 2043 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2044 | if (mCursorScrollAccumulator.haveRelativeVWheel()) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2045 | 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] | 2046 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2047 | if (mCursorScrollAccumulator.haveRelativeHWheel()) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2048 | 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] | 2049 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2050 | } |
| 2051 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2052 | void CursorInputMapper::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2053 | dump.append(INDENT2 "Cursor Input Mapper:\n"); |
| 2054 | dumpParameters(dump); |
| 2055 | dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale); |
| 2056 | dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale); |
| 2057 | dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision); |
| 2058 | dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision); |
| 2059 | dump.appendFormat(INDENT3 "HaveVWheel: %s\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2060 | toString(mCursorScrollAccumulator.haveRelativeVWheel())); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2061 | dump.appendFormat(INDENT3 "HaveHWheel: %s\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2062 | toString(mCursorScrollAccumulator.haveRelativeHWheel())); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2063 | dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale); |
| 2064 | dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2065 | dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2066 | dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState); |
| 2067 | dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState))); |
| 2068 | dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2069 | } |
| 2070 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2071 | void CursorInputMapper::configure(nsecs_t when, |
| 2072 | const InputReaderConfiguration* config, uint32_t changes) { |
| 2073 | InputMapper::configure(when, config, changes); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2074 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2075 | if (!changes) { // first time only |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2076 | mCursorScrollAccumulator.configure(getDevice()); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2077 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2078 | // Configure basic parameters. |
| 2079 | configureParameters(); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2080 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2081 | // Configure device mode. |
| 2082 | switch (mParameters.mode) { |
| 2083 | case Parameters::MODE_POINTER: |
| 2084 | mSource = AINPUT_SOURCE_MOUSE; |
| 2085 | mXPrecision = 1.0f; |
| 2086 | mYPrecision = 1.0f; |
| 2087 | mXScale = 1.0f; |
| 2088 | mYScale = 1.0f; |
| 2089 | mPointerController = getPolicy()->obtainPointerController(getDeviceId()); |
| 2090 | break; |
| 2091 | case Parameters::MODE_NAVIGATION: |
| 2092 | mSource = AINPUT_SOURCE_TRACKBALL; |
| 2093 | mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 2094 | mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 2095 | mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 2096 | mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 2097 | break; |
| 2098 | } |
| 2099 | |
| 2100 | mVWheelScale = 1.0f; |
| 2101 | mHWheelScale = 1.0f; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2102 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 2103 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2104 | if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) { |
| 2105 | mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters); |
| 2106 | mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters); |
| 2107 | mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters); |
| 2108 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2109 | |
| 2110 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) { |
| 2111 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) { |
| 2112 | if (!config->getDisplayInfo(mParameters.associatedDisplayId, |
| 2113 | false /*external*/, NULL, NULL, &mOrientation)) { |
| 2114 | mOrientation = DISPLAY_ORIENTATION_0; |
| 2115 | } |
| 2116 | } else { |
| 2117 | mOrientation = DISPLAY_ORIENTATION_0; |
| 2118 | } |
| 2119 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2120 | } |
| 2121 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2122 | void CursorInputMapper::configureParameters() { |
| 2123 | mParameters.mode = Parameters::MODE_POINTER; |
| 2124 | String8 cursorModeString; |
| 2125 | if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) { |
| 2126 | if (cursorModeString == "navigation") { |
| 2127 | mParameters.mode = Parameters::MODE_NAVIGATION; |
| 2128 | } else if (cursorModeString != "pointer" && cursorModeString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2129 | ALOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string()); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2130 | } |
| 2131 | } |
| 2132 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2133 | mParameters.orientationAware = false; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2134 | getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"), |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2135 | mParameters.orientationAware); |
| 2136 | |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2137 | mParameters.associatedDisplayId = -1; |
| 2138 | if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) { |
| 2139 | mParameters.associatedDisplayId = 0; |
| 2140 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2141 | } |
| 2142 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2143 | void CursorInputMapper::dumpParameters(String8& dump) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2144 | dump.append(INDENT3 "Parameters:\n"); |
| 2145 | dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n", |
| 2146 | mParameters.associatedDisplayId); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2147 | |
| 2148 | switch (mParameters.mode) { |
| 2149 | case Parameters::MODE_POINTER: |
| 2150 | dump.append(INDENT4 "Mode: pointer\n"); |
| 2151 | break; |
| 2152 | case Parameters::MODE_NAVIGATION: |
| 2153 | dump.append(INDENT4 "Mode: navigation\n"); |
| 2154 | break; |
| 2155 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 2156 | ALOG_ASSERT(false); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2157 | } |
| 2158 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2159 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 2160 | toString(mParameters.orientationAware)); |
| 2161 | } |
| 2162 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2163 | void CursorInputMapper::reset(nsecs_t when) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2164 | mButtonState = 0; |
| 2165 | mDownTime = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2166 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2167 | mPointerVelocityControl.reset(); |
| 2168 | mWheelXVelocityControl.reset(); |
| 2169 | mWheelYVelocityControl.reset(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2170 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2171 | mCursorButtonAccumulator.reset(getDevice()); |
| 2172 | mCursorMotionAccumulator.reset(getDevice()); |
| 2173 | mCursorScrollAccumulator.reset(getDevice()); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2174 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2175 | InputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2176 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2177 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2178 | void CursorInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2179 | mCursorButtonAccumulator.process(rawEvent); |
| 2180 | mCursorMotionAccumulator.process(rawEvent); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2181 | mCursorScrollAccumulator.process(rawEvent); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2182 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2183 | if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) { |
| 2184 | sync(rawEvent->when); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2185 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2186 | } |
| 2187 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2188 | void CursorInputMapper::sync(nsecs_t when) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2189 | int32_t lastButtonState = mButtonState; |
| 2190 | int32_t currentButtonState = mCursorButtonAccumulator.getButtonState(); |
| 2191 | mButtonState = currentButtonState; |
| 2192 | |
| 2193 | bool wasDown = isPointerDown(lastButtonState); |
| 2194 | bool down = isPointerDown(currentButtonState); |
| 2195 | bool downChanged; |
| 2196 | if (!wasDown && down) { |
| 2197 | mDownTime = when; |
| 2198 | downChanged = true; |
| 2199 | } else if (wasDown && !down) { |
| 2200 | downChanged = true; |
| 2201 | } else { |
| 2202 | downChanged = false; |
| 2203 | } |
| 2204 | nsecs_t downTime = mDownTime; |
| 2205 | bool buttonsChanged = currentButtonState != lastButtonState; |
Jeff Brown | c28306a | 2011-08-23 21:32:42 -0700 | [diff] [blame] | 2206 | bool buttonsPressed = currentButtonState & ~lastButtonState; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2207 | |
| 2208 | float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale; |
| 2209 | float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale; |
| 2210 | bool moved = deltaX != 0 || deltaY != 0; |
| 2211 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2212 | // Rotate delta according to orientation if needed. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2213 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0 |
| 2214 | && (deltaX != 0.0f || deltaY != 0.0f)) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2215 | rotateDelta(mOrientation, &deltaX, &deltaY); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2216 | } |
| 2217 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2218 | // Move the pointer. |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2219 | PointerProperties pointerProperties; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2220 | pointerProperties.clear(); |
| 2221 | pointerProperties.id = 0; |
| 2222 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE; |
| 2223 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2224 | PointerCoords pointerCoords; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2225 | pointerCoords.clear(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2226 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2227 | float vscroll = mCursorScrollAccumulator.getRelativeVWheel(); |
| 2228 | float hscroll = mCursorScrollAccumulator.getRelativeHWheel(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2229 | bool scrolled = vscroll != 0 || hscroll != 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2230 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2231 | mWheelYVelocityControl.move(when, NULL, &vscroll); |
| 2232 | mWheelXVelocityControl.move(when, &hscroll, NULL); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2233 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2234 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2235 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2236 | if (mPointerController != NULL) { |
| 2237 | if (moved || scrolled || buttonsChanged) { |
| 2238 | mPointerController->setPresentation( |
| 2239 | PointerControllerInterface::PRESENTATION_POINTER); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2240 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2241 | if (moved) { |
| 2242 | mPointerController->move(deltaX, deltaY); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2243 | } |
| 2244 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2245 | if (buttonsChanged) { |
| 2246 | mPointerController->setButtonState(currentButtonState); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2247 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2248 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2249 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2250 | } |
| 2251 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2252 | float x, y; |
| 2253 | mPointerController->getPosition(&x, &y); |
| 2254 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 2255 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 2256 | } else { |
| 2257 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX); |
| 2258 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY); |
| 2259 | } |
| 2260 | |
| 2261 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2262 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2263 | // Moving an external trackball or mouse should wake the device. |
| 2264 | // We don't do this for internal cursor devices to prevent them from waking up |
| 2265 | // the device in your pocket. |
| 2266 | // TODO: Use the input device configuration to control this behavior more finely. |
| 2267 | uint32_t policyFlags = 0; |
Jeff Brown | c28306a | 2011-08-23 21:32:42 -0700 | [diff] [blame] | 2268 | if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) { |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2269 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 2270 | } |
| 2271 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2272 | // Synthesize key down from buttons if needed. |
| 2273 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource, |
| 2274 | policyFlags, lastButtonState, currentButtonState); |
| 2275 | |
| 2276 | // Send motion event. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2277 | if (downChanged || moved || scrolled || buttonsChanged) { |
| 2278 | int32_t metaState = mContext->getGlobalMetaState(); |
| 2279 | int32_t motionEventAction; |
| 2280 | if (downChanged) { |
| 2281 | motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP; |
| 2282 | } else if (down || mPointerController == NULL) { |
| 2283 | motionEventAction = AMOTION_EVENT_ACTION_MOVE; |
| 2284 | } else { |
| 2285 | motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE; |
| 2286 | } |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2287 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2288 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 2289 | motionEventAction, 0, metaState, currentButtonState, 0, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2290 | 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2291 | getListener()->notifyMotion(&args); |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2292 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2293 | // Send hover move after UP to tell the application that the mouse is hovering now. |
| 2294 | if (motionEventAction == AMOTION_EVENT_ACTION_UP |
| 2295 | && mPointerController != NULL) { |
| 2296 | NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags, |
| 2297 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 2298 | metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 2299 | 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime); |
| 2300 | getListener()->notifyMotion(&hoverArgs); |
| 2301 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2302 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2303 | // Send scroll events. |
| 2304 | if (scrolled) { |
| 2305 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll); |
| 2306 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll); |
| 2307 | |
| 2308 | NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags, |
| 2309 | AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState, |
| 2310 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 2311 | 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime); |
| 2312 | getListener()->notifyMotion(&scrollArgs); |
| 2313 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2314 | } |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2315 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2316 | // Synthesize key up from buttons if needed. |
| 2317 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource, |
| 2318 | policyFlags, lastButtonState, currentButtonState); |
| 2319 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2320 | mCursorMotionAccumulator.finishSync(); |
| 2321 | mCursorScrollAccumulator.finishSync(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2322 | } |
| 2323 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2324 | int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Jeff Brown | c3fc2d0 | 2010-08-10 15:47:53 -0700 | [diff] [blame] | 2325 | if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) { |
| 2326 | return getEventHub()->getScanCodeState(getDeviceId(), scanCode); |
| 2327 | } else { |
| 2328 | return AKEY_STATE_UNKNOWN; |
| 2329 | } |
| 2330 | } |
| 2331 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2332 | void CursorInputMapper::fadePointer() { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2333 | if (mPointerController != NULL) { |
| 2334 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 2335 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2336 | } |
| 2337 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2338 | |
| 2339 | // --- TouchInputMapper --- |
| 2340 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2341 | TouchInputMapper::TouchInputMapper(InputDevice* device) : |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2342 | InputMapper(device), |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2343 | mSource(0), mDeviceMode(DEVICE_MODE_DISABLED), |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2344 | mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2345 | } |
| 2346 | |
| 2347 | TouchInputMapper::~TouchInputMapper() { |
| 2348 | } |
| 2349 | |
| 2350 | uint32_t TouchInputMapper::getSources() { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2351 | return mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2352 | } |
| 2353 | |
| 2354 | void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 2355 | InputMapper::populateDeviceInfo(info); |
| 2356 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2357 | if (mDeviceMode != DEVICE_MODE_DISABLED) { |
| 2358 | info->addMotionRange(mOrientedRanges.x); |
| 2359 | info->addMotionRange(mOrientedRanges.y); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2360 | info->addMotionRange(mOrientedRanges.pressure); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2361 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2362 | if (mOrientedRanges.haveSize) { |
| 2363 | info->addMotionRange(mOrientedRanges.size); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2364 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2365 | |
| 2366 | if (mOrientedRanges.haveTouchSize) { |
| 2367 | info->addMotionRange(mOrientedRanges.touchMajor); |
| 2368 | info->addMotionRange(mOrientedRanges.touchMinor); |
| 2369 | } |
| 2370 | |
| 2371 | if (mOrientedRanges.haveToolSize) { |
| 2372 | info->addMotionRange(mOrientedRanges.toolMajor); |
| 2373 | info->addMotionRange(mOrientedRanges.toolMinor); |
| 2374 | } |
| 2375 | |
| 2376 | if (mOrientedRanges.haveOrientation) { |
| 2377 | info->addMotionRange(mOrientedRanges.orientation); |
| 2378 | } |
| 2379 | |
| 2380 | if (mOrientedRanges.haveDistance) { |
| 2381 | info->addMotionRange(mOrientedRanges.distance); |
| 2382 | } |
| 2383 | |
| 2384 | if (mOrientedRanges.haveTilt) { |
| 2385 | info->addMotionRange(mOrientedRanges.tilt); |
| 2386 | } |
| 2387 | |
| 2388 | if (mCursorScrollAccumulator.haveRelativeVWheel()) { |
| 2389 | info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f); |
| 2390 | } |
| 2391 | if (mCursorScrollAccumulator.haveRelativeHWheel()) { |
| 2392 | info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f); |
| 2393 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2394 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2395 | } |
| 2396 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2397 | void TouchInputMapper::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2398 | dump.append(INDENT2 "Touch Input Mapper:\n"); |
| 2399 | dumpParameters(dump); |
| 2400 | dumpVirtualKeys(dump); |
| 2401 | dumpRawPointerAxes(dump); |
| 2402 | dumpCalibration(dump); |
| 2403 | dumpSurface(dump); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2404 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2405 | dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n"); |
| 2406 | dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale); |
| 2407 | dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale); |
| 2408 | dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision); |
| 2409 | dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision); |
| 2410 | dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2411 | dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale); |
| 2412 | dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2413 | dump.appendFormat(INDENT4 "OrientationCenter: %0.3f\n", mOrientationCenter); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2414 | dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale); |
| 2415 | dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2416 | dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt)); |
| 2417 | dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter); |
| 2418 | dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale); |
| 2419 | dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter); |
| 2420 | dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2421 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2422 | dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2423 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2424 | dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n", |
| 2425 | mLastRawPointerData.pointerCount); |
| 2426 | for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) { |
| 2427 | const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i]; |
| 2428 | dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, " |
| 2429 | "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2430 | "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, " |
| 2431 | "toolType=%d, isHovering=%s\n", i, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2432 | pointer.id, pointer.x, pointer.y, pointer.pressure, |
| 2433 | pointer.touchMajor, pointer.touchMinor, |
| 2434 | pointer.toolMajor, pointer.toolMinor, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2435 | pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2436 | pointer.toolType, toString(pointer.isHovering)); |
| 2437 | } |
| 2438 | |
| 2439 | dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n", |
| 2440 | mLastCookedPointerData.pointerCount); |
| 2441 | for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) { |
| 2442 | const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i]; |
| 2443 | const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i]; |
| 2444 | dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, " |
| 2445 | "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2446 | "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, " |
| 2447 | "toolType=%d, isHovering=%s\n", i, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2448 | pointerProperties.id, |
| 2449 | pointerCoords.getX(), |
| 2450 | pointerCoords.getY(), |
| 2451 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 2452 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 2453 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 2454 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 2455 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 2456 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2457 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT), |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2458 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), |
| 2459 | pointerProperties.toolType, |
| 2460 | toString(mLastCookedPointerData.isHovering(i))); |
| 2461 | } |
| 2462 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2463 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2464 | dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n"); |
| 2465 | dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2466 | mPointerXMovementScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2467 | dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2468 | mPointerYMovementScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2469 | dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2470 | mPointerXZoomScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2471 | dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2472 | mPointerYZoomScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2473 | dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n", |
| 2474 | mPointerGestureMaxSwipeWidth); |
| 2475 | } |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2476 | } |
| 2477 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2478 | void TouchInputMapper::configure(nsecs_t when, |
| 2479 | const InputReaderConfiguration* config, uint32_t changes) { |
| 2480 | InputMapper::configure(when, config, changes); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2481 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2482 | mConfig = *config; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2483 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2484 | if (!changes) { // first time only |
| 2485 | // Configure basic parameters. |
| 2486 | configureParameters(); |
| 2487 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2488 | // Configure common accumulators. |
| 2489 | mCursorScrollAccumulator.configure(getDevice()); |
| 2490 | mTouchButtonAccumulator.configure(getDevice()); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2491 | |
| 2492 | // Configure absolute axis information. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2493 | configureRawPointerAxes(); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2494 | |
| 2495 | // Prepare input device calibration. |
| 2496 | parseCalibration(); |
| 2497 | resolveCalibration(); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2498 | } |
| 2499 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2500 | if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2501 | // Update pointer speed. |
| 2502 | mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters); |
| 2503 | mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters); |
| 2504 | mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2505 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2506 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2507 | bool resetNeeded = false; |
| 2508 | if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 2509 | | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT |
| 2510 | | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2511 | // Configure device sources, surface dimensions, orientation and |
| 2512 | // scaling factors. |
| 2513 | configureSurface(when, &resetNeeded); |
| 2514 | } |
| 2515 | |
| 2516 | if (changes && resetNeeded) { |
| 2517 | // Send reset, unless this is the first time the device has been configured, |
| 2518 | // in which case the reader will call reset itself after all mappers are ready. |
| 2519 | getDevice()->notifyReset(when); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2520 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2521 | } |
| 2522 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2523 | void TouchInputMapper::configureParameters() { |
Jeff Brown | b126822 | 2011-06-03 17:06:16 -0700 | [diff] [blame] | 2524 | // Use the pointer presentation mode for devices that do not support distinct |
| 2525 | // multitouch. The spot-based presentation relies on being able to accurately |
| 2526 | // locate two or more fingers on the touch pad. |
| 2527 | mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT) |
| 2528 | ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2529 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2530 | String8 gestureModeString; |
| 2531 | if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"), |
| 2532 | gestureModeString)) { |
| 2533 | if (gestureModeString == "pointer") { |
| 2534 | mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER; |
| 2535 | } else if (gestureModeString == "spots") { |
| 2536 | mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS; |
| 2537 | } else if (gestureModeString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2538 | ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string()); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2539 | } |
| 2540 | } |
| 2541 | |
Jeff Brown | deffe07 | 2011-08-26 18:38:46 -0700 | [diff] [blame] | 2542 | if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) { |
| 2543 | // The device is a touch screen. |
| 2544 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
| 2545 | } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) { |
| 2546 | // The device is a pointing device like a track pad. |
| 2547 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
| 2548 | } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X) |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2549 | || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) { |
| 2550 | // The device is a cursor device with a touch pad attached. |
| 2551 | // By default don't use the touch pad to move the pointer. |
| 2552 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; |
| 2553 | } else { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2554 | // The device is a touch pad of unknown purpose. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2555 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
| 2556 | } |
| 2557 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2558 | String8 deviceTypeString; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2559 | if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"), |
| 2560 | deviceTypeString)) { |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 2561 | if (deviceTypeString == "touchScreen") { |
| 2562 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2563 | } else if (deviceTypeString == "touchPad") { |
| 2564 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2565 | } else if (deviceTypeString == "pointer") { |
| 2566 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2567 | } else if (deviceTypeString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2568 | ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string()); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2569 | } |
| 2570 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2571 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2572 | mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2573 | getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"), |
| 2574 | mParameters.orientationAware); |
| 2575 | |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2576 | mParameters.associatedDisplayId = -1; |
| 2577 | mParameters.associatedDisplayIsExternal = false; |
| 2578 | if (mParameters.orientationAware |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2579 | || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2580 | || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) { |
| 2581 | mParameters.associatedDisplayIsExternal = |
| 2582 | mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN |
| 2583 | && getDevice()->isExternal(); |
| 2584 | mParameters.associatedDisplayId = 0; |
| 2585 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2586 | } |
| 2587 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2588 | void TouchInputMapper::dumpParameters(String8& dump) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2589 | dump.append(INDENT3 "Parameters:\n"); |
| 2590 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2591 | switch (mParameters.gestureMode) { |
| 2592 | case Parameters::GESTURE_MODE_POINTER: |
| 2593 | dump.append(INDENT4 "GestureMode: pointer\n"); |
| 2594 | break; |
| 2595 | case Parameters::GESTURE_MODE_SPOTS: |
| 2596 | dump.append(INDENT4 "GestureMode: spots\n"); |
| 2597 | break; |
| 2598 | default: |
| 2599 | assert(false); |
| 2600 | } |
| 2601 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2602 | switch (mParameters.deviceType) { |
| 2603 | case Parameters::DEVICE_TYPE_TOUCH_SCREEN: |
| 2604 | dump.append(INDENT4 "DeviceType: touchScreen\n"); |
| 2605 | break; |
| 2606 | case Parameters::DEVICE_TYPE_TOUCH_PAD: |
| 2607 | dump.append(INDENT4 "DeviceType: touchPad\n"); |
| 2608 | break; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2609 | case Parameters::DEVICE_TYPE_POINTER: |
| 2610 | dump.append(INDENT4 "DeviceType: pointer\n"); |
| 2611 | break; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2612 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 2613 | ALOG_ASSERT(false); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2614 | } |
| 2615 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2616 | dump.appendFormat(INDENT4 "AssociatedDisplay: id=%d, isExternal=%s\n", |
| 2617 | mParameters.associatedDisplayId, toString(mParameters.associatedDisplayIsExternal)); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2618 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 2619 | toString(mParameters.orientationAware)); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2620 | } |
| 2621 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2622 | void TouchInputMapper::configureRawPointerAxes() { |
| 2623 | mRawPointerAxes.clear(); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2624 | } |
| 2625 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2626 | void TouchInputMapper::dumpRawPointerAxes(String8& dump) { |
| 2627 | dump.append(INDENT3 "Raw Touch Axes:\n"); |
| 2628 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X"); |
| 2629 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y"); |
| 2630 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure"); |
| 2631 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor"); |
| 2632 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor"); |
| 2633 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor"); |
| 2634 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor"); |
| 2635 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation"); |
| 2636 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance"); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2637 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX"); |
| 2638 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY"); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2639 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId"); |
| 2640 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot"); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2641 | } |
| 2642 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2643 | void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) { |
| 2644 | int32_t oldDeviceMode = mDeviceMode; |
| 2645 | |
| 2646 | // Determine device mode. |
| 2647 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER |
| 2648 | && mConfig.pointerGesturesEnabled) { |
| 2649 | mSource = AINPUT_SOURCE_MOUSE; |
| 2650 | mDeviceMode = DEVICE_MODE_POINTER; |
| 2651 | } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN |
| 2652 | && mParameters.associatedDisplayId >= 0) { |
| 2653 | mSource = AINPUT_SOURCE_TOUCHSCREEN; |
| 2654 | mDeviceMode = DEVICE_MODE_DIRECT; |
| 2655 | } else { |
| 2656 | mSource = AINPUT_SOURCE_TOUCHPAD; |
| 2657 | mDeviceMode = DEVICE_MODE_UNSCALED; |
| 2658 | } |
| 2659 | |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2660 | // Ensure we have valid X and Y axes. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2661 | if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2662 | 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] | 2663 | "The device will be inoperable.", getDeviceName().string()); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2664 | mDeviceMode = DEVICE_MODE_DISABLED; |
| 2665 | return; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2666 | } |
| 2667 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2668 | // Get associated display dimensions. |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2669 | if (mParameters.associatedDisplayId >= 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2670 | if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId, |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2671 | mParameters.associatedDisplayIsExternal, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2672 | &mAssociatedDisplayWidth, &mAssociatedDisplayHeight, |
| 2673 | &mAssociatedDisplayOrientation)) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2674 | 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] | 2675 | "display %d. The device will be inoperable until the display size " |
| 2676 | "becomes available.", |
| 2677 | getDeviceName().string(), mParameters.associatedDisplayId); |
| 2678 | mDeviceMode = DEVICE_MODE_DISABLED; |
| 2679 | return; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2680 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2681 | } |
| 2682 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2683 | // Configure dimensions. |
| 2684 | int32_t width, height, orientation; |
| 2685 | if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) { |
| 2686 | width = mAssociatedDisplayWidth; |
| 2687 | height = mAssociatedDisplayHeight; |
| 2688 | orientation = mParameters.orientationAware ? |
| 2689 | mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0; |
| 2690 | } else { |
| 2691 | width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1; |
| 2692 | height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1; |
| 2693 | orientation = DISPLAY_ORIENTATION_0; |
| 2694 | } |
| 2695 | |
| 2696 | // If moving between pointer modes, need to reset some state. |
| 2697 | bool deviceModeChanged; |
| 2698 | if (mDeviceMode != oldDeviceMode) { |
| 2699 | deviceModeChanged = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2700 | mOrientedRanges.clear(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2701 | } |
| 2702 | |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 2703 | // Create pointer controller if needed. |
| 2704 | if (mDeviceMode == DEVICE_MODE_POINTER || |
| 2705 | (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) { |
| 2706 | if (mPointerController == NULL) { |
| 2707 | mPointerController = getPolicy()->obtainPointerController(getDeviceId()); |
| 2708 | } |
| 2709 | } else { |
| 2710 | mPointerController.clear(); |
| 2711 | } |
| 2712 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2713 | bool orientationChanged = mSurfaceOrientation != orientation; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2714 | if (orientationChanged) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2715 | mSurfaceOrientation = orientation; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2716 | } |
| 2717 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2718 | bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2719 | if (sizeChanged || deviceModeChanged) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2720 | 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] | 2721 | getDeviceId(), getDeviceName().string(), width, height, mDeviceMode); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2722 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2723 | mSurfaceWidth = width; |
| 2724 | mSurfaceHeight = height; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2725 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2726 | // Configure X and Y factors. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2727 | mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1); |
| 2728 | mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1); |
| 2729 | mXPrecision = 1.0f / mXScale; |
| 2730 | mYPrecision = 1.0f / mYScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2731 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2732 | mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2733 | mOrientedRanges.x.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2734 | mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2735 | mOrientedRanges.y.source = mSource; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2736 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2737 | configureVirtualKeys(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2738 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2739 | // Scale factor for terms that are not oriented in a particular axis. |
| 2740 | // If the pixels are square then xScale == yScale otherwise we fake it |
| 2741 | // by choosing an average. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2742 | mGeometricScale = avg(mXScale, mYScale); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2743 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2744 | // Size of diagonal axis. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2745 | float diagonalSize = hypotf(width, height); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2746 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 2747 | // Size factors. |
| 2748 | if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) { |
| 2749 | if (mRawPointerAxes.touchMajor.valid |
| 2750 | && mRawPointerAxes.touchMajor.maxValue != 0) { |
| 2751 | mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue; |
| 2752 | } else if (mRawPointerAxes.toolMajor.valid |
| 2753 | && mRawPointerAxes.toolMajor.maxValue != 0) { |
| 2754 | mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue; |
| 2755 | } else { |
| 2756 | mSizeScale = 0.0f; |
| 2757 | } |
| 2758 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2759 | mOrientedRanges.haveTouchSize = true; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 2760 | mOrientedRanges.haveToolSize = true; |
| 2761 | mOrientedRanges.haveSize = true; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2762 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2763 | mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2764 | mOrientedRanges.touchMajor.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2765 | mOrientedRanges.touchMajor.min = 0; |
| 2766 | mOrientedRanges.touchMajor.max = diagonalSize; |
| 2767 | mOrientedRanges.touchMajor.flat = 0; |
| 2768 | mOrientedRanges.touchMajor.fuzz = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2769 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2770 | mOrientedRanges.touchMinor = mOrientedRanges.touchMajor; |
| 2771 | mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2772 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2773 | mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2774 | mOrientedRanges.toolMajor.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2775 | mOrientedRanges.toolMajor.min = 0; |
| 2776 | mOrientedRanges.toolMajor.max = diagonalSize; |
| 2777 | mOrientedRanges.toolMajor.flat = 0; |
| 2778 | mOrientedRanges.toolMajor.fuzz = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2779 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2780 | mOrientedRanges.toolMinor = mOrientedRanges.toolMajor; |
| 2781 | mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 2782 | |
| 2783 | mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2784 | mOrientedRanges.size.source = mSource; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 2785 | mOrientedRanges.size.min = 0; |
| 2786 | mOrientedRanges.size.max = 1.0; |
| 2787 | mOrientedRanges.size.flat = 0; |
| 2788 | mOrientedRanges.size.fuzz = 0; |
| 2789 | } else { |
| 2790 | mSizeScale = 0.0f; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2791 | } |
| 2792 | |
| 2793 | // Pressure factors. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2794 | mPressureScale = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2795 | if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL |
| 2796 | || mCalibration.pressureCalibration |
| 2797 | == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) { |
| 2798 | if (mCalibration.havePressureScale) { |
| 2799 | mPressureScale = mCalibration.pressureScale; |
| 2800 | } else if (mRawPointerAxes.pressure.valid |
| 2801 | && mRawPointerAxes.pressure.maxValue != 0) { |
| 2802 | mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2803 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2804 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2805 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2806 | mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE; |
| 2807 | mOrientedRanges.pressure.source = mSource; |
| 2808 | mOrientedRanges.pressure.min = 0; |
| 2809 | mOrientedRanges.pressure.max = 1.0; |
| 2810 | mOrientedRanges.pressure.flat = 0; |
| 2811 | mOrientedRanges.pressure.fuzz = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2812 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2813 | // Tilt |
| 2814 | mTiltXCenter = 0; |
| 2815 | mTiltXScale = 0; |
| 2816 | mTiltYCenter = 0; |
| 2817 | mTiltYScale = 0; |
| 2818 | mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid; |
| 2819 | if (mHaveTilt) { |
| 2820 | mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue, |
| 2821 | mRawPointerAxes.tiltX.maxValue); |
| 2822 | mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue, |
| 2823 | mRawPointerAxes.tiltY.maxValue); |
| 2824 | mTiltXScale = M_PI / 180; |
| 2825 | mTiltYScale = M_PI / 180; |
| 2826 | |
| 2827 | mOrientedRanges.haveTilt = true; |
| 2828 | |
| 2829 | mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT; |
| 2830 | mOrientedRanges.tilt.source = mSource; |
| 2831 | mOrientedRanges.tilt.min = 0; |
| 2832 | mOrientedRanges.tilt.max = M_PI_2; |
| 2833 | mOrientedRanges.tilt.flat = 0; |
| 2834 | mOrientedRanges.tilt.fuzz = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2835 | } |
| 2836 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2837 | // Orientation |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2838 | mOrientationCenter = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2839 | mOrientationScale = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2840 | if (mHaveTilt) { |
| 2841 | mOrientedRanges.haveOrientation = true; |
| 2842 | |
| 2843 | mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION; |
| 2844 | mOrientedRanges.orientation.source = mSource; |
| 2845 | mOrientedRanges.orientation.min = -M_PI; |
| 2846 | mOrientedRanges.orientation.max = M_PI; |
| 2847 | mOrientedRanges.orientation.flat = 0; |
| 2848 | mOrientedRanges.orientation.fuzz = 0; |
| 2849 | } else if (mCalibration.orientationCalibration != |
| 2850 | Calibration::ORIENTATION_CALIBRATION_NONE) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2851 | if (mCalibration.orientationCalibration |
| 2852 | == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2853 | if (mRawPointerAxes.orientation.valid) { |
| 2854 | mOrientationCenter = avg(mRawPointerAxes.orientation.minValue, |
| 2855 | mRawPointerAxes.orientation.maxValue); |
| 2856 | mOrientationScale = M_PI / (mRawPointerAxes.orientation.maxValue - |
| 2857 | mRawPointerAxes.orientation.minValue); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2858 | } |
| 2859 | } |
| 2860 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2861 | mOrientedRanges.haveOrientation = true; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2862 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2863 | mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2864 | mOrientedRanges.orientation.source = mSource; |
| 2865 | mOrientedRanges.orientation.min = -M_PI_2; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2866 | mOrientedRanges.orientation.max = M_PI_2; |
| 2867 | mOrientedRanges.orientation.flat = 0; |
| 2868 | mOrientedRanges.orientation.fuzz = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2869 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2870 | |
| 2871 | // Distance |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2872 | mDistanceScale = 0; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2873 | if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) { |
| 2874 | if (mCalibration.distanceCalibration |
| 2875 | == Calibration::DISTANCE_CALIBRATION_SCALED) { |
| 2876 | if (mCalibration.haveDistanceScale) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2877 | mDistanceScale = mCalibration.distanceScale; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2878 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2879 | mDistanceScale = 1.0f; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2880 | } |
| 2881 | } |
| 2882 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2883 | mOrientedRanges.haveDistance = true; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2884 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2885 | mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2886 | mOrientedRanges.distance.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2887 | mOrientedRanges.distance.min = |
| 2888 | mRawPointerAxes.distance.minValue * mDistanceScale; |
| 2889 | mOrientedRanges.distance.max = |
| 2890 | mRawPointerAxes.distance.minValue * mDistanceScale; |
| 2891 | mOrientedRanges.distance.flat = 0; |
| 2892 | mOrientedRanges.distance.fuzz = |
| 2893 | mRawPointerAxes.distance.fuzz * mDistanceScale; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2894 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2895 | } |
| 2896 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2897 | if (orientationChanged || sizeChanged || deviceModeChanged) { |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2898 | // Compute oriented surface dimensions, precision, scales and ranges. |
| 2899 | // Note that the maximum value reported is an inclusive maximum value so it is one |
| 2900 | // unit less than the total width or height of surface. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2901 | switch (mSurfaceOrientation) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 2902 | case DISPLAY_ORIENTATION_90: |
| 2903 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2904 | mOrientedSurfaceWidth = mSurfaceHeight; |
| 2905 | mOrientedSurfaceHeight = mSurfaceWidth; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2906 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2907 | mOrientedXPrecision = mYPrecision; |
| 2908 | mOrientedYPrecision = mXPrecision; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2909 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2910 | mOrientedRanges.x.min = 0; |
| 2911 | mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue) |
| 2912 | * mYScale; |
| 2913 | mOrientedRanges.x.flat = 0; |
| 2914 | mOrientedRanges.x.fuzz = mYScale; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2915 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2916 | mOrientedRanges.y.min = 0; |
| 2917 | mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue) |
| 2918 | * mXScale; |
| 2919 | mOrientedRanges.y.flat = 0; |
| 2920 | mOrientedRanges.y.fuzz = mXScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2921 | break; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2922 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2923 | default: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2924 | mOrientedSurfaceWidth = mSurfaceWidth; |
| 2925 | mOrientedSurfaceHeight = mSurfaceHeight; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2926 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2927 | mOrientedXPrecision = mXPrecision; |
| 2928 | mOrientedYPrecision = mYPrecision; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2929 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2930 | mOrientedRanges.x.min = 0; |
| 2931 | mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue) |
| 2932 | * mXScale; |
| 2933 | mOrientedRanges.x.flat = 0; |
| 2934 | mOrientedRanges.x.fuzz = mXScale; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2935 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2936 | mOrientedRanges.y.min = 0; |
| 2937 | mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue) |
| 2938 | * mYScale; |
| 2939 | mOrientedRanges.y.flat = 0; |
| 2940 | mOrientedRanges.y.fuzz = mYScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2941 | break; |
| 2942 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2943 | |
| 2944 | // Compute pointer gesture detection parameters. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2945 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2946 | int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1; |
| 2947 | int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2948 | float rawDiagonal = hypotf(rawWidth, rawHeight); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2949 | float displayDiagonal = hypotf(mAssociatedDisplayWidth, |
| 2950 | mAssociatedDisplayHeight); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2951 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2952 | // 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] | 2953 | // given area relative to the diagonal size of the display when no acceleration |
| 2954 | // is applied. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2955 | // Assume that the touch pad has a square aspect ratio such that movements in |
| 2956 | // 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] | 2957 | mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2958 | * displayDiagonal / rawDiagonal; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2959 | mPointerYMovementScale = mPointerXMovementScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2960 | |
| 2961 | // Scale zooms to cover a smaller range of the display than movements do. |
| 2962 | // This value determines the area around the pointer that is affected by freeform |
| 2963 | // pointer gestures. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2964 | mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2965 | * displayDiagonal / rawDiagonal; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2966 | mPointerYZoomScale = mPointerXZoomScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2967 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2968 | // Max width between pointers to detect a swipe gesture is more than some fraction |
| 2969 | // of the diagonal axis of the touch pad. Touches that are wider than this are |
| 2970 | // translated into freeform gestures. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2971 | mPointerGestureMaxSwipeWidth = |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2972 | mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2973 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2974 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2975 | // Abort current pointer usages because the state has changed. |
| 2976 | abortPointerUsage(when, 0 /*policyFlags*/); |
| 2977 | |
| 2978 | // Inform the dispatcher about the changes. |
| 2979 | *outResetNeeded = true; |
| 2980 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2981 | } |
| 2982 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2983 | void TouchInputMapper::dumpSurface(String8& dump) { |
| 2984 | dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth); |
| 2985 | dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight); |
| 2986 | dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2987 | } |
| 2988 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2989 | void TouchInputMapper::configureVirtualKeys() { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2990 | Vector<VirtualKeyDefinition> virtualKeyDefinitions; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 2991 | getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2992 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2993 | mVirtualKeys.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2994 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2995 | if (virtualKeyDefinitions.size() == 0) { |
| 2996 | return; |
| 2997 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2998 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2999 | mVirtualKeys.setCapacity(virtualKeyDefinitions.size()); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3000 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3001 | int32_t touchScreenLeft = mRawPointerAxes.x.minValue; |
| 3002 | int32_t touchScreenTop = mRawPointerAxes.y.minValue; |
| 3003 | int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1; |
| 3004 | int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3005 | |
| 3006 | for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3007 | const VirtualKeyDefinition& virtualKeyDefinition = |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3008 | virtualKeyDefinitions[i]; |
| 3009 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3010 | mVirtualKeys.add(); |
| 3011 | VirtualKey& virtualKey = mVirtualKeys.editTop(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3012 | |
| 3013 | virtualKey.scanCode = virtualKeyDefinition.scanCode; |
| 3014 | int32_t keyCode; |
| 3015 | uint32_t flags; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 3016 | if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3017 | & keyCode, & flags)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3018 | ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3019 | virtualKey.scanCode); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3020 | mVirtualKeys.pop(); // drop the key |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3021 | continue; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3022 | } |
| 3023 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3024 | virtualKey.keyCode = keyCode; |
| 3025 | virtualKey.flags = flags; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3026 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3027 | // convert the key definition's display coordinates into touch coordinates for a hit box |
| 3028 | int32_t halfWidth = virtualKeyDefinition.width / 2; |
| 3029 | int32_t halfHeight = virtualKeyDefinition.height / 2; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3030 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3031 | virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3032 | * touchScreenWidth / mSurfaceWidth + touchScreenLeft; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3033 | virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3034 | * touchScreenWidth / mSurfaceWidth + touchScreenLeft; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3035 | virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3036 | * touchScreenHeight / mSurfaceHeight + touchScreenTop; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3037 | virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3038 | * touchScreenHeight / mSurfaceHeight + touchScreenTop; |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3039 | } |
| 3040 | } |
| 3041 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3042 | void TouchInputMapper::dumpVirtualKeys(String8& dump) { |
| 3043 | if (!mVirtualKeys.isEmpty()) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3044 | dump.append(INDENT3 "Virtual Keys:\n"); |
| 3045 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3046 | for (size_t i = 0; i < mVirtualKeys.size(); i++) { |
| 3047 | const VirtualKey& virtualKey = mVirtualKeys.itemAt(i); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3048 | dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, " |
| 3049 | "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n", |
| 3050 | i, virtualKey.scanCode, virtualKey.keyCode, |
| 3051 | virtualKey.hitLeft, virtualKey.hitRight, |
| 3052 | virtualKey.hitTop, virtualKey.hitBottom); |
| 3053 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3054 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3055 | } |
| 3056 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3057 | void TouchInputMapper::parseCalibration() { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 3058 | const PropertyMap& in = getDevice()->getConfiguration(); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3059 | Calibration& out = mCalibration; |
| 3060 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3061 | // Size |
| 3062 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT; |
| 3063 | String8 sizeCalibrationString; |
| 3064 | if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) { |
| 3065 | if (sizeCalibrationString == "none") { |
| 3066 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; |
| 3067 | } else if (sizeCalibrationString == "geometric") { |
| 3068 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC; |
| 3069 | } else if (sizeCalibrationString == "diameter") { |
| 3070 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER; |
| 3071 | } else if (sizeCalibrationString == "area") { |
| 3072 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA; |
| 3073 | } else if (sizeCalibrationString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3074 | ALOGW("Invalid value for touch.size.calibration: '%s'", |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3075 | sizeCalibrationString.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3076 | } |
| 3077 | } |
| 3078 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3079 | out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"), |
| 3080 | out.sizeScale); |
| 3081 | out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"), |
| 3082 | out.sizeBias); |
| 3083 | out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"), |
| 3084 | out.sizeIsSummed); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3085 | |
| 3086 | // Pressure |
| 3087 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT; |
| 3088 | String8 pressureCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 3089 | if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3090 | if (pressureCalibrationString == "none") { |
| 3091 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; |
| 3092 | } else if (pressureCalibrationString == "physical") { |
| 3093 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL; |
| 3094 | } else if (pressureCalibrationString == "amplitude") { |
| 3095 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE; |
| 3096 | } else if (pressureCalibrationString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3097 | ALOGW("Invalid value for touch.pressure.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3098 | pressureCalibrationString.string()); |
| 3099 | } |
| 3100 | } |
| 3101 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3102 | out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"), |
| 3103 | out.pressureScale); |
| 3104 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3105 | // Orientation |
| 3106 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT; |
| 3107 | String8 orientationCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 3108 | if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3109 | if (orientationCalibrationString == "none") { |
| 3110 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; |
| 3111 | } else if (orientationCalibrationString == "interpolated") { |
| 3112 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 3113 | } else if (orientationCalibrationString == "vector") { |
| 3114 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3115 | } else if (orientationCalibrationString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3116 | ALOGW("Invalid value for touch.orientation.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3117 | orientationCalibrationString.string()); |
| 3118 | } |
| 3119 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3120 | |
| 3121 | // Distance |
| 3122 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT; |
| 3123 | String8 distanceCalibrationString; |
| 3124 | if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) { |
| 3125 | if (distanceCalibrationString == "none") { |
| 3126 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE; |
| 3127 | } else if (distanceCalibrationString == "scaled") { |
| 3128 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED; |
| 3129 | } else if (distanceCalibrationString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3130 | ALOGW("Invalid value for touch.distance.calibration: '%s'", |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3131 | distanceCalibrationString.string()); |
| 3132 | } |
| 3133 | } |
| 3134 | |
| 3135 | out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"), |
| 3136 | out.distanceScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3137 | } |
| 3138 | |
| 3139 | void TouchInputMapper::resolveCalibration() { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3140 | // Size |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3141 | if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) { |
| 3142 | if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) { |
| 3143 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3144 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3145 | } else { |
| 3146 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; |
| 3147 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3148 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3149 | // Pressure |
| 3150 | if (mRawPointerAxes.pressure.valid) { |
| 3151 | if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) { |
| 3152 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL; |
| 3153 | } |
| 3154 | } else { |
| 3155 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3156 | } |
| 3157 | |
| 3158 | // Orientation |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3159 | if (mRawPointerAxes.orientation.valid) { |
| 3160 | if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3161 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3162 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3163 | } else { |
| 3164 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3165 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3166 | |
| 3167 | // Distance |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3168 | if (mRawPointerAxes.distance.valid) { |
| 3169 | if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3170 | mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3171 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3172 | } else { |
| 3173 | mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3174 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3175 | } |
| 3176 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3177 | void TouchInputMapper::dumpCalibration(String8& dump) { |
| 3178 | dump.append(INDENT3 "Calibration:\n"); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3179 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3180 | // Size |
| 3181 | switch (mCalibration.sizeCalibration) { |
| 3182 | case Calibration::SIZE_CALIBRATION_NONE: |
| 3183 | dump.append(INDENT4 "touch.size.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3184 | break; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3185 | case Calibration::SIZE_CALIBRATION_GEOMETRIC: |
| 3186 | dump.append(INDENT4 "touch.size.calibration: geometric\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3187 | break; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3188 | case Calibration::SIZE_CALIBRATION_DIAMETER: |
| 3189 | dump.append(INDENT4 "touch.size.calibration: diameter\n"); |
| 3190 | break; |
| 3191 | case Calibration::SIZE_CALIBRATION_AREA: |
| 3192 | dump.append(INDENT4 "touch.size.calibration: area\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3193 | break; |
| 3194 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3195 | ALOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3196 | } |
| 3197 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3198 | if (mCalibration.haveSizeScale) { |
| 3199 | dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n", |
| 3200 | mCalibration.sizeScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3201 | } |
| 3202 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3203 | if (mCalibration.haveSizeBias) { |
| 3204 | dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n", |
| 3205 | mCalibration.sizeBias); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3206 | } |
| 3207 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3208 | if (mCalibration.haveSizeIsSummed) { |
| 3209 | dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n", |
| 3210 | toString(mCalibration.sizeIsSummed)); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3211 | } |
| 3212 | |
| 3213 | // Pressure |
| 3214 | switch (mCalibration.pressureCalibration) { |
| 3215 | case Calibration::PRESSURE_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3216 | dump.append(INDENT4 "touch.pressure.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3217 | break; |
| 3218 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3219 | dump.append(INDENT4 "touch.pressure.calibration: physical\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3220 | break; |
| 3221 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3222 | dump.append(INDENT4 "touch.pressure.calibration: amplitude\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3223 | break; |
| 3224 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3225 | ALOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3226 | } |
| 3227 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3228 | if (mCalibration.havePressureScale) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3229 | dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n", |
| 3230 | mCalibration.pressureScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3231 | } |
| 3232 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3233 | // Orientation |
| 3234 | switch (mCalibration.orientationCalibration) { |
| 3235 | case Calibration::ORIENTATION_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3236 | dump.append(INDENT4 "touch.orientation.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3237 | break; |
| 3238 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3239 | dump.append(INDENT4 "touch.orientation.calibration: interpolated\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3240 | break; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 3241 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: |
| 3242 | dump.append(INDENT4 "touch.orientation.calibration: vector\n"); |
| 3243 | break; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3244 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3245 | ALOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3246 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3247 | |
| 3248 | // Distance |
| 3249 | switch (mCalibration.distanceCalibration) { |
| 3250 | case Calibration::DISTANCE_CALIBRATION_NONE: |
| 3251 | dump.append(INDENT4 "touch.distance.calibration: none\n"); |
| 3252 | break; |
| 3253 | case Calibration::DISTANCE_CALIBRATION_SCALED: |
| 3254 | dump.append(INDENT4 "touch.distance.calibration: scaled\n"); |
| 3255 | break; |
| 3256 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3257 | ALOG_ASSERT(false); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3258 | } |
| 3259 | |
| 3260 | if (mCalibration.haveDistanceScale) { |
| 3261 | dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n", |
| 3262 | mCalibration.distanceScale); |
| 3263 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3264 | } |
| 3265 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3266 | void TouchInputMapper::reset(nsecs_t when) { |
| 3267 | mCursorButtonAccumulator.reset(getDevice()); |
| 3268 | mCursorScrollAccumulator.reset(getDevice()); |
| 3269 | mTouchButtonAccumulator.reset(getDevice()); |
| 3270 | |
| 3271 | mPointerVelocityControl.reset(); |
| 3272 | mWheelXVelocityControl.reset(); |
| 3273 | mWheelYVelocityControl.reset(); |
| 3274 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3275 | mCurrentRawPointerData.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3276 | mLastRawPointerData.clear(); |
| 3277 | mCurrentCookedPointerData.clear(); |
| 3278 | mLastCookedPointerData.clear(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3279 | mCurrentButtonState = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3280 | mLastButtonState = 0; |
| 3281 | mCurrentRawVScroll = 0; |
| 3282 | mCurrentRawHScroll = 0; |
| 3283 | mCurrentFingerIdBits.clear(); |
| 3284 | mLastFingerIdBits.clear(); |
| 3285 | mCurrentStylusIdBits.clear(); |
| 3286 | mLastStylusIdBits.clear(); |
| 3287 | mCurrentMouseIdBits.clear(); |
| 3288 | mLastMouseIdBits.clear(); |
| 3289 | mPointerUsage = POINTER_USAGE_NONE; |
| 3290 | mSentHoverEnter = false; |
| 3291 | mDownTime = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3292 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3293 | mCurrentVirtualKey.down = false; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3294 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3295 | mPointerGesture.reset(); |
| 3296 | mPointerSimple.reset(); |
| 3297 | |
| 3298 | if (mPointerController != NULL) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3299 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 3300 | mPointerController->clearSpots(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3301 | } |
| 3302 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3303 | InputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3304 | } |
| 3305 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3306 | void TouchInputMapper::process(const RawEvent* rawEvent) { |
| 3307 | mCursorButtonAccumulator.process(rawEvent); |
| 3308 | mCursorScrollAccumulator.process(rawEvent); |
| 3309 | mTouchButtonAccumulator.process(rawEvent); |
| 3310 | |
| 3311 | if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) { |
| 3312 | sync(rawEvent->when); |
| 3313 | } |
| 3314 | } |
| 3315 | |
| 3316 | void TouchInputMapper::sync(nsecs_t when) { |
| 3317 | // Sync button state. |
| 3318 | mCurrentButtonState = mTouchButtonAccumulator.getButtonState() |
| 3319 | | mCursorButtonAccumulator.getButtonState(); |
| 3320 | |
| 3321 | // Sync scroll state. |
| 3322 | mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel(); |
| 3323 | mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel(); |
| 3324 | mCursorScrollAccumulator.finishSync(); |
| 3325 | |
| 3326 | // Sync touch state. |
| 3327 | bool havePointerIds = true; |
| 3328 | mCurrentRawPointerData.clear(); |
| 3329 | syncTouch(when, &havePointerIds); |
| 3330 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 3331 | #if DEBUG_RAW_EVENTS |
| 3332 | if (!havePointerIds) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3333 | ALOGD("syncTouch: pointerCount %d -> %d, no pointer ids", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3334 | mLastRawPointerData.pointerCount, |
| 3335 | mCurrentRawPointerData.pointerCount); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 3336 | } else { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3337 | ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, " |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3338 | "hovering ids 0x%08x -> 0x%08x", |
| 3339 | mLastRawPointerData.pointerCount, |
| 3340 | mCurrentRawPointerData.pointerCount, |
| 3341 | mLastRawPointerData.touchingIdBits.value, |
| 3342 | mCurrentRawPointerData.touchingIdBits.value, |
| 3343 | mLastRawPointerData.hoveringIdBits.value, |
| 3344 | mCurrentRawPointerData.hoveringIdBits.value); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 3345 | } |
| 3346 | #endif |
| 3347 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3348 | // Reset state that we will compute below. |
| 3349 | mCurrentFingerIdBits.clear(); |
| 3350 | mCurrentStylusIdBits.clear(); |
| 3351 | mCurrentMouseIdBits.clear(); |
| 3352 | mCurrentCookedPointerData.clear(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3353 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3354 | if (mDeviceMode == DEVICE_MODE_DISABLED) { |
| 3355 | // Drop all input if the device is disabled. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3356 | mCurrentRawPointerData.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3357 | mCurrentButtonState = 0; |
| 3358 | } else { |
| 3359 | // Preprocess pointer data. |
| 3360 | if (!havePointerIds) { |
| 3361 | assignPointerIds(); |
| 3362 | } |
| 3363 | |
| 3364 | // Handle policy on initial down or hover events. |
| 3365 | uint32_t policyFlags = 0; |
Jeff Brown | c28306a | 2011-08-23 21:32:42 -0700 | [diff] [blame] | 3366 | bool initialDown = mLastRawPointerData.pointerCount == 0 |
| 3367 | && mCurrentRawPointerData.pointerCount != 0; |
| 3368 | bool buttonsPressed = mCurrentButtonState & ~mLastButtonState; |
| 3369 | if (initialDown || buttonsPressed) { |
| 3370 | // 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] | 3371 | if (mDeviceMode == DEVICE_MODE_DIRECT) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3372 | getContext()->fadePointer(); |
| 3373 | } |
| 3374 | |
| 3375 | // Initial downs on external touch devices should wake the device. |
| 3376 | // We don't do this for internal touch screens to prevent them from waking |
| 3377 | // up in your pocket. |
| 3378 | // TODO: Use the input device configuration to control this behavior more finely. |
| 3379 | if (getDevice()->isExternal()) { |
| 3380 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 3381 | } |
| 3382 | } |
| 3383 | |
| 3384 | // Synthesize key down from raw buttons if needed. |
| 3385 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource, |
| 3386 | policyFlags, mLastButtonState, mCurrentButtonState); |
| 3387 | |
| 3388 | // Consume raw off-screen touches before cooking pointer data. |
| 3389 | // If touches are consumed, subsequent code will not receive any pointer data. |
| 3390 | if (consumeRawTouches(when, policyFlags)) { |
| 3391 | mCurrentRawPointerData.clear(); |
| 3392 | } |
| 3393 | |
| 3394 | // Cook pointer data. This call populates the mCurrentCookedPointerData structure |
| 3395 | // with cooked pointer data that has the same ids and indices as the raw data. |
| 3396 | // The following code can use either the raw or cooked data, as needed. |
| 3397 | cookPointerData(); |
| 3398 | |
| 3399 | // 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] | 3400 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3401 | for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) { |
| 3402 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 3403 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3404 | if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS |
| 3405 | || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) { |
| 3406 | mCurrentStylusIdBits.markBit(id); |
| 3407 | } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER |
| 3408 | || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 3409 | mCurrentFingerIdBits.markBit(id); |
| 3410 | } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) { |
| 3411 | mCurrentMouseIdBits.markBit(id); |
| 3412 | } |
| 3413 | } |
| 3414 | for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) { |
| 3415 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 3416 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3417 | if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS |
| 3418 | || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) { |
| 3419 | mCurrentStylusIdBits.markBit(id); |
| 3420 | } |
| 3421 | } |
| 3422 | |
| 3423 | // Stylus takes precedence over all tools, then mouse, then finger. |
| 3424 | PointerUsage pointerUsage = mPointerUsage; |
| 3425 | if (!mCurrentStylusIdBits.isEmpty()) { |
| 3426 | mCurrentMouseIdBits.clear(); |
| 3427 | mCurrentFingerIdBits.clear(); |
| 3428 | pointerUsage = POINTER_USAGE_STYLUS; |
| 3429 | } else if (!mCurrentMouseIdBits.isEmpty()) { |
| 3430 | mCurrentFingerIdBits.clear(); |
| 3431 | pointerUsage = POINTER_USAGE_MOUSE; |
| 3432 | } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) { |
| 3433 | pointerUsage = POINTER_USAGE_GESTURES; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3434 | } |
| 3435 | |
| 3436 | dispatchPointerUsage(when, policyFlags, pointerUsage); |
| 3437 | } else { |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 3438 | if (mDeviceMode == DEVICE_MODE_DIRECT |
| 3439 | && mConfig.showTouches && mPointerController != NULL) { |
| 3440 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT); |
| 3441 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 3442 | |
| 3443 | mPointerController->setButtonState(mCurrentButtonState); |
| 3444 | mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords, |
| 3445 | mCurrentCookedPointerData.idToIndex, |
| 3446 | mCurrentCookedPointerData.touchingIdBits); |
| 3447 | } |
| 3448 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3449 | dispatchHoverExit(when, policyFlags); |
| 3450 | dispatchTouches(when, policyFlags); |
| 3451 | dispatchHoverEnterAndMove(when, policyFlags); |
| 3452 | } |
| 3453 | |
| 3454 | // Synthesize key up from raw buttons if needed. |
| 3455 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource, |
| 3456 | policyFlags, mLastButtonState, mCurrentButtonState); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3457 | } |
| 3458 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3459 | // Copy current touch to last touch in preparation for the next cycle. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3460 | mLastRawPointerData.copyFrom(mCurrentRawPointerData); |
| 3461 | mLastCookedPointerData.copyFrom(mCurrentCookedPointerData); |
| 3462 | mLastButtonState = mCurrentButtonState; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3463 | mLastFingerIdBits = mCurrentFingerIdBits; |
| 3464 | mLastStylusIdBits = mCurrentStylusIdBits; |
| 3465 | mLastMouseIdBits = mCurrentMouseIdBits; |
| 3466 | |
| 3467 | // Clear some transient state. |
| 3468 | mCurrentRawVScroll = 0; |
| 3469 | mCurrentRawHScroll = 0; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3470 | } |
| 3471 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3472 | void TouchInputMapper::timeoutExpired(nsecs_t when) { |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 3473 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3474 | if (mPointerUsage == POINTER_USAGE_GESTURES) { |
| 3475 | dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/); |
| 3476 | } |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3477 | } |
| 3478 | } |
| 3479 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3480 | bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) { |
| 3481 | // Check for release of a virtual key. |
| 3482 | if (mCurrentVirtualKey.down) { |
| 3483 | if (mCurrentRawPointerData.touchingIdBits.isEmpty()) { |
| 3484 | // Pointer went up while virtual key was down. |
| 3485 | mCurrentVirtualKey.down = false; |
| 3486 | if (!mCurrentVirtualKey.ignored) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3487 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3488 | ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3489 | mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3490 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3491 | dispatchVirtualKey(when, policyFlags, |
| 3492 | AKEY_EVENT_ACTION_UP, |
| 3493 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3494 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3495 | return true; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3496 | } |
| 3497 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3498 | if (mCurrentRawPointerData.touchingIdBits.count() == 1) { |
| 3499 | uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit(); |
| 3500 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3501 | const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y); |
| 3502 | if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) { |
| 3503 | // Pointer is still within the space of the virtual key. |
| 3504 | return true; |
| 3505 | } |
| 3506 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3507 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3508 | // Pointer left virtual key area or another pointer also went down. |
| 3509 | // Send key cancellation but do not consume the touch yet. |
| 3510 | // This is useful when the user swipes through from the virtual key area |
| 3511 | // into the main display surface. |
| 3512 | mCurrentVirtualKey.down = false; |
| 3513 | if (!mCurrentVirtualKey.ignored) { |
| 3514 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3515 | ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3516 | mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode); |
| 3517 | #endif |
| 3518 | dispatchVirtualKey(when, policyFlags, |
| 3519 | AKEY_EVENT_ACTION_UP, |
| 3520 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
| 3521 | | AKEY_EVENT_FLAG_CANCELED); |
| 3522 | } |
| 3523 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3524 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3525 | if (mLastRawPointerData.touchingIdBits.isEmpty() |
| 3526 | && !mCurrentRawPointerData.touchingIdBits.isEmpty()) { |
| 3527 | // Pointer just went down. Check for virtual key press or off-screen touches. |
| 3528 | uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit(); |
| 3529 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3530 | if (!isPointInsideSurface(pointer.x, pointer.y)) { |
| 3531 | // If exactly one pointer went down, check for virtual key hit. |
| 3532 | // Otherwise we will drop the entire stroke. |
| 3533 | if (mCurrentRawPointerData.touchingIdBits.count() == 1) { |
| 3534 | const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y); |
| 3535 | if (virtualKey) { |
| 3536 | mCurrentVirtualKey.down = true; |
| 3537 | mCurrentVirtualKey.downTime = when; |
| 3538 | mCurrentVirtualKey.keyCode = virtualKey->keyCode; |
| 3539 | mCurrentVirtualKey.scanCode = virtualKey->scanCode; |
| 3540 | mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey( |
| 3541 | when, getDevice(), virtualKey->keyCode, virtualKey->scanCode); |
| 3542 | |
| 3543 | if (!mCurrentVirtualKey.ignored) { |
| 3544 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3545 | ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3546 | mCurrentVirtualKey.keyCode, |
| 3547 | mCurrentVirtualKey.scanCode); |
| 3548 | #endif |
| 3549 | dispatchVirtualKey(when, policyFlags, |
| 3550 | AKEY_EVENT_ACTION_DOWN, |
| 3551 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY); |
| 3552 | } |
| 3553 | } |
| 3554 | } |
| 3555 | return true; |
| 3556 | } |
| 3557 | } |
| 3558 | |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3559 | // 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] | 3560 | // most recent touch within the screen area. The idea is to filter out stray |
| 3561 | // virtual key presses when interacting with the touch screen. |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3562 | // |
| 3563 | // Problems we're trying to solve: |
| 3564 | // |
| 3565 | // 1. While scrolling a list or dragging the window shade, the user swipes down into a |
| 3566 | // virtual key area that is implemented by a separate touch panel and accidentally |
| 3567 | // triggers a virtual key. |
| 3568 | // |
| 3569 | // 2. While typing in the on screen keyboard, the user taps slightly outside the screen |
| 3570 | // area and accidentally triggers a virtual key. This often happens when virtual keys |
| 3571 | // are layed out below the screen near to where the on screen keyboard's space bar |
| 3572 | // is displayed. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3573 | if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 3574 | mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime); |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3575 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3576 | return false; |
| 3577 | } |
| 3578 | |
| 3579 | void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags, |
| 3580 | int32_t keyEventAction, int32_t keyEventFlags) { |
| 3581 | int32_t keyCode = mCurrentVirtualKey.keyCode; |
| 3582 | int32_t scanCode = mCurrentVirtualKey.scanCode; |
| 3583 | nsecs_t downTime = mCurrentVirtualKey.downTime; |
| 3584 | int32_t metaState = mContext->getGlobalMetaState(); |
| 3585 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3586 | |
| 3587 | NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags, |
| 3588 | keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime); |
| 3589 | getListener()->notifyKey(&args); |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3590 | } |
| 3591 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3592 | void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3593 | BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits; |
| 3594 | BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3595 | int32_t metaState = getContext()->getGlobalMetaState(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3596 | int32_t buttonState = mCurrentButtonState; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3597 | |
| 3598 | if (currentIdBits == lastIdBits) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3599 | if (!currentIdBits.isEmpty()) { |
| 3600 | // No pointer id changes so this is a move event. |
| 3601 | // 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] | 3602 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3603 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, |
| 3604 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 3605 | mCurrentCookedPointerData.pointerProperties, |
| 3606 | mCurrentCookedPointerData.pointerCoords, |
| 3607 | mCurrentCookedPointerData.idToIndex, |
| 3608 | currentIdBits, -1, |
| 3609 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 3610 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3611 | } else { |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3612 | // There may be pointers going up and pointers going down and pointers moving |
| 3613 | // all at the same time. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3614 | BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value); |
| 3615 | BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3616 | BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3617 | BitSet32 dispatchedIdBits(lastIdBits.value); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3618 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3619 | // Update last coordinates of pointers that have moved so that we observe the new |
| 3620 | // 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] | 3621 | bool moveNeeded = updateMovedPointers( |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3622 | mCurrentCookedPointerData.pointerProperties, |
| 3623 | mCurrentCookedPointerData.pointerCoords, |
| 3624 | mCurrentCookedPointerData.idToIndex, |
| 3625 | mLastCookedPointerData.pointerProperties, |
| 3626 | mLastCookedPointerData.pointerCoords, |
| 3627 | mLastCookedPointerData.idToIndex, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3628 | moveIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3629 | if (buttonState != mLastButtonState) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3630 | moveNeeded = true; |
| 3631 | } |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3632 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3633 | // Dispatch pointer up events. |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3634 | while (!upIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3635 | uint32_t upId = upIdBits.clearFirstMarkedBit(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3636 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3637 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3638 | AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3639 | mLastCookedPointerData.pointerProperties, |
| 3640 | mLastCookedPointerData.pointerCoords, |
| 3641 | mLastCookedPointerData.idToIndex, |
| 3642 | dispatchedIdBits, upId, |
| 3643 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3644 | dispatchedIdBits.clearBit(upId); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3645 | } |
| 3646 | |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3647 | // Dispatch move events if any of the remaining pointers moved from their old locations. |
| 3648 | // Although applications receive new locations as part of individual pointer up |
| 3649 | // events, they do not generally handle them except when presented in a move event. |
| 3650 | if (moveNeeded) { |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3651 | ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3652 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3653 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3654 | mCurrentCookedPointerData.pointerProperties, |
| 3655 | mCurrentCookedPointerData.pointerCoords, |
| 3656 | mCurrentCookedPointerData.idToIndex, |
| 3657 | dispatchedIdBits, -1, |
| 3658 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3659 | } |
| 3660 | |
| 3661 | // Dispatch pointer down events using the new pointer locations. |
| 3662 | while (!downIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3663 | uint32_t downId = downIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3664 | dispatchedIdBits.markBit(downId); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3665 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3666 | if (dispatchedIdBits.count() == 1) { |
| 3667 | // First pointer is going down. Set down time. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3668 | mDownTime = when; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3669 | } |
| 3670 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3671 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | a611137 | 2011-07-14 21:48:23 -0700 | [diff] [blame] | 3672 | AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3673 | mCurrentCookedPointerData.pointerProperties, |
| 3674 | mCurrentCookedPointerData.pointerCoords, |
| 3675 | mCurrentCookedPointerData.idToIndex, |
| 3676 | dispatchedIdBits, downId, |
| 3677 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3678 | } |
| 3679 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3680 | } |
| 3681 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3682 | void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) { |
| 3683 | if (mSentHoverEnter && |
| 3684 | (mCurrentCookedPointerData.hoveringIdBits.isEmpty() |
| 3685 | || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) { |
| 3686 | int32_t metaState = getContext()->getGlobalMetaState(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3687 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3688 | AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0, |
| 3689 | mLastCookedPointerData.pointerProperties, |
| 3690 | mLastCookedPointerData.pointerCoords, |
| 3691 | mLastCookedPointerData.idToIndex, |
| 3692 | mLastCookedPointerData.hoveringIdBits, -1, |
| 3693 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 3694 | mSentHoverEnter = false; |
| 3695 | } |
| 3696 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3697 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3698 | void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) { |
| 3699 | if (mCurrentCookedPointerData.touchingIdBits.isEmpty() |
| 3700 | && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) { |
| 3701 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 3702 | if (!mSentHoverEnter) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3703 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3704 | AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0, |
| 3705 | mCurrentCookedPointerData.pointerProperties, |
| 3706 | mCurrentCookedPointerData.pointerCoords, |
| 3707 | mCurrentCookedPointerData.idToIndex, |
| 3708 | mCurrentCookedPointerData.hoveringIdBits, -1, |
| 3709 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 3710 | mSentHoverEnter = true; |
| 3711 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3712 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3713 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3714 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0, |
| 3715 | mCurrentCookedPointerData.pointerProperties, |
| 3716 | mCurrentCookedPointerData.pointerCoords, |
| 3717 | mCurrentCookedPointerData.idToIndex, |
| 3718 | mCurrentCookedPointerData.hoveringIdBits, -1, |
| 3719 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 3720 | } |
| 3721 | } |
| 3722 | |
| 3723 | void TouchInputMapper::cookPointerData() { |
| 3724 | uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount; |
| 3725 | |
| 3726 | mCurrentCookedPointerData.clear(); |
| 3727 | mCurrentCookedPointerData.pointerCount = currentPointerCount; |
| 3728 | mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits; |
| 3729 | mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits; |
| 3730 | |
| 3731 | // Walk through the the active pointers and map device coordinates onto |
| 3732 | // surface coordinates and adjust for display orientation. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3733 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3734 | const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3735 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3736 | // Size |
| 3737 | float touchMajor, touchMinor, toolMajor, toolMinor, size; |
| 3738 | switch (mCalibration.sizeCalibration) { |
| 3739 | case Calibration::SIZE_CALIBRATION_GEOMETRIC: |
| 3740 | case Calibration::SIZE_CALIBRATION_DIAMETER: |
| 3741 | case Calibration::SIZE_CALIBRATION_AREA: |
| 3742 | if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) { |
| 3743 | touchMajor = in.touchMajor; |
| 3744 | touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor; |
| 3745 | toolMajor = in.toolMajor; |
| 3746 | toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor; |
| 3747 | size = mRawPointerAxes.touchMinor.valid |
| 3748 | ? avg(in.touchMajor, in.touchMinor) : in.touchMajor; |
| 3749 | } else if (mRawPointerAxes.touchMajor.valid) { |
| 3750 | toolMajor = touchMajor = in.touchMajor; |
| 3751 | toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid |
| 3752 | ? in.touchMinor : in.touchMajor; |
| 3753 | size = mRawPointerAxes.touchMinor.valid |
| 3754 | ? avg(in.touchMajor, in.touchMinor) : in.touchMajor; |
| 3755 | } else if (mRawPointerAxes.toolMajor.valid) { |
| 3756 | touchMajor = toolMajor = in.toolMajor; |
| 3757 | touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid |
| 3758 | ? in.toolMinor : in.toolMajor; |
| 3759 | size = mRawPointerAxes.toolMinor.valid |
| 3760 | ? avg(in.toolMajor, in.toolMinor) : in.toolMajor; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3761 | } else { |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3762 | ALOG_ASSERT(false, "No touch or tool axes. " |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3763 | "Size calibration should have been resolved to NONE."); |
| 3764 | touchMajor = 0; |
| 3765 | touchMinor = 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3766 | toolMajor = 0; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3767 | toolMinor = 0; |
| 3768 | size = 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3769 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3770 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3771 | if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) { |
| 3772 | uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count(); |
| 3773 | if (touchingCount > 1) { |
| 3774 | touchMajor /= touchingCount; |
| 3775 | touchMinor /= touchingCount; |
| 3776 | toolMajor /= touchingCount; |
| 3777 | toolMinor /= touchingCount; |
| 3778 | size /= touchingCount; |
| 3779 | } |
| 3780 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3781 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3782 | if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) { |
| 3783 | touchMajor *= mGeometricScale; |
| 3784 | touchMinor *= mGeometricScale; |
| 3785 | toolMajor *= mGeometricScale; |
| 3786 | toolMinor *= mGeometricScale; |
| 3787 | } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) { |
| 3788 | touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3789 | touchMinor = touchMajor; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3790 | toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0; |
| 3791 | toolMinor = toolMajor; |
| 3792 | } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) { |
| 3793 | touchMinor = touchMajor; |
| 3794 | toolMinor = toolMajor; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3795 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3796 | |
| 3797 | mCalibration.applySizeScaleAndBias(&touchMajor); |
| 3798 | mCalibration.applySizeScaleAndBias(&touchMinor); |
| 3799 | mCalibration.applySizeScaleAndBias(&toolMajor); |
| 3800 | mCalibration.applySizeScaleAndBias(&toolMinor); |
| 3801 | size *= mSizeScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3802 | break; |
| 3803 | default: |
| 3804 | touchMajor = 0; |
| 3805 | touchMinor = 0; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3806 | toolMajor = 0; |
| 3807 | toolMinor = 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3808 | size = 0; |
| 3809 | break; |
| 3810 | } |
| 3811 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3812 | // Pressure |
| 3813 | float pressure; |
| 3814 | switch (mCalibration.pressureCalibration) { |
| 3815 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: |
| 3816 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: |
| 3817 | pressure = in.pressure * mPressureScale; |
| 3818 | break; |
| 3819 | default: |
| 3820 | pressure = in.isHovering ? 0 : 1; |
| 3821 | break; |
| 3822 | } |
| 3823 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3824 | // Tilt and Orientation |
| 3825 | float tilt; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3826 | float orientation; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3827 | if (mHaveTilt) { |
| 3828 | float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale; |
| 3829 | float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale; |
| 3830 | orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle)); |
| 3831 | tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle)); |
| 3832 | } else { |
| 3833 | tilt = 0; |
| 3834 | |
| 3835 | switch (mCalibration.orientationCalibration) { |
| 3836 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: |
| 3837 | orientation = (in.orientation - mOrientationCenter) * mOrientationScale; |
| 3838 | break; |
| 3839 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: { |
| 3840 | int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4); |
| 3841 | int32_t c2 = signExtendNybble(in.orientation & 0x0f); |
| 3842 | if (c1 != 0 || c2 != 0) { |
| 3843 | orientation = atan2f(c1, c2) * 0.5f; |
| 3844 | float confidence = hypotf(c1, c2); |
| 3845 | float scale = 1.0f + confidence / 16.0f; |
| 3846 | touchMajor *= scale; |
| 3847 | touchMinor /= scale; |
| 3848 | toolMajor *= scale; |
| 3849 | toolMinor /= scale; |
| 3850 | } else { |
| 3851 | orientation = 0; |
| 3852 | } |
| 3853 | break; |
| 3854 | } |
| 3855 | default: |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3856 | orientation = 0; |
| 3857 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3858 | } |
| 3859 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3860 | // Distance |
| 3861 | float distance; |
| 3862 | switch (mCalibration.distanceCalibration) { |
| 3863 | case Calibration::DISTANCE_CALIBRATION_SCALED: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3864 | distance = in.distance * mDistanceScale; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3865 | break; |
| 3866 | default: |
| 3867 | distance = 0; |
| 3868 | } |
| 3869 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3870 | // X and Y |
| 3871 | // Adjust coords for surface orientation. |
| 3872 | float x, y; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3873 | switch (mSurfaceOrientation) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3874 | case DISPLAY_ORIENTATION_90: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3875 | x = float(in.y - mRawPointerAxes.y.minValue) * mYScale; |
| 3876 | y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3877 | orientation -= M_PI_2; |
| 3878 | if (orientation < - M_PI_2) { |
| 3879 | orientation += M_PI; |
| 3880 | } |
| 3881 | break; |
| 3882 | case DISPLAY_ORIENTATION_180: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3883 | x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale; |
| 3884 | y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3885 | break; |
| 3886 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3887 | x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale; |
| 3888 | y = float(in.x - mRawPointerAxes.x.minValue) * mXScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3889 | orientation += M_PI_2; |
| 3890 | if (orientation > M_PI_2) { |
| 3891 | orientation -= M_PI; |
| 3892 | } |
| 3893 | break; |
| 3894 | default: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3895 | x = float(in.x - mRawPointerAxes.x.minValue) * mXScale; |
| 3896 | y = float(in.y - mRawPointerAxes.y.minValue) * mYScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3897 | break; |
| 3898 | } |
| 3899 | |
| 3900 | // Write output coords. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3901 | PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3902 | out.clear(); |
| 3903 | out.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 3904 | out.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 3905 | out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure); |
| 3906 | out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size); |
| 3907 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor); |
| 3908 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor); |
| 3909 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor); |
| 3910 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor); |
| 3911 | out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3912 | out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3913 | out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3914 | |
| 3915 | // Write output properties. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3916 | PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i]; |
| 3917 | uint32_t id = in.id; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3918 | properties.clear(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3919 | properties.id = id; |
| 3920 | properties.toolType = in.toolType; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3921 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3922 | // Write id index. |
| 3923 | mCurrentCookedPointerData.idToIndex[id] = i; |
| 3924 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3925 | } |
| 3926 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3927 | void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, |
| 3928 | PointerUsage pointerUsage) { |
| 3929 | if (pointerUsage != mPointerUsage) { |
| 3930 | abortPointerUsage(when, policyFlags); |
| 3931 | mPointerUsage = pointerUsage; |
| 3932 | } |
| 3933 | |
| 3934 | switch (mPointerUsage) { |
| 3935 | case POINTER_USAGE_GESTURES: |
| 3936 | dispatchPointerGestures(when, policyFlags, false /*isTimeout*/); |
| 3937 | break; |
| 3938 | case POINTER_USAGE_STYLUS: |
| 3939 | dispatchPointerStylus(when, policyFlags); |
| 3940 | break; |
| 3941 | case POINTER_USAGE_MOUSE: |
| 3942 | dispatchPointerMouse(when, policyFlags); |
| 3943 | break; |
| 3944 | default: |
| 3945 | break; |
| 3946 | } |
| 3947 | } |
| 3948 | |
| 3949 | void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) { |
| 3950 | switch (mPointerUsage) { |
| 3951 | case POINTER_USAGE_GESTURES: |
| 3952 | abortPointerGestures(when, policyFlags); |
| 3953 | break; |
| 3954 | case POINTER_USAGE_STYLUS: |
| 3955 | abortPointerStylus(when, policyFlags); |
| 3956 | break; |
| 3957 | case POINTER_USAGE_MOUSE: |
| 3958 | abortPointerMouse(when, policyFlags); |
| 3959 | break; |
| 3960 | default: |
| 3961 | break; |
| 3962 | } |
| 3963 | |
| 3964 | mPointerUsage = POINTER_USAGE_NONE; |
| 3965 | } |
| 3966 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3967 | void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, |
| 3968 | bool isTimeout) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3969 | // Update current gesture coordinates. |
| 3970 | bool cancelPreviousGesture, finishPreviousGesture; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3971 | bool sendEvents = preparePointerGestures(when, |
| 3972 | &cancelPreviousGesture, &finishPreviousGesture, isTimeout); |
| 3973 | if (!sendEvents) { |
| 3974 | return; |
| 3975 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 3976 | if (finishPreviousGesture) { |
| 3977 | cancelPreviousGesture = false; |
| 3978 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3979 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 3980 | // Update the pointer presentation and spots. |
| 3981 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 3982 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT); |
| 3983 | if (finishPreviousGesture || cancelPreviousGesture) { |
| 3984 | mPointerController->clearSpots(); |
| 3985 | } |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 3986 | mPointerController->setSpots(mPointerGesture.currentGestureCoords, |
| 3987 | mPointerGesture.currentGestureIdToIndex, |
| 3988 | mPointerGesture.currentGestureIdBits); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 3989 | } else { |
| 3990 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER); |
| 3991 | } |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 3992 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 3993 | // Show or hide the pointer if needed. |
| 3994 | switch (mPointerGesture.currentGestureMode) { |
| 3995 | case PointerGesture::NEUTRAL: |
| 3996 | case PointerGesture::QUIET: |
| 3997 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS |
| 3998 | && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE |
| 3999 | || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) { |
| 4000 | // Remind the user of where the pointer is after finishing a gesture with spots. |
| 4001 | mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 4002 | } |
| 4003 | break; |
| 4004 | case PointerGesture::TAP: |
| 4005 | case PointerGesture::TAP_DRAG: |
| 4006 | case PointerGesture::BUTTON_CLICK_OR_DRAG: |
| 4007 | case PointerGesture::HOVER: |
| 4008 | case PointerGesture::PRESS: |
| 4009 | // Unfade the pointer when the current gesture manipulates the |
| 4010 | // area directly under the pointer. |
| 4011 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 4012 | break; |
| 4013 | case PointerGesture::SWIPE: |
| 4014 | case PointerGesture::FREEFORM: |
| 4015 | // Fade the pointer when the current gesture manipulates a different |
| 4016 | // area and there are spots to guide the user experience. |
| 4017 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 4018 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 4019 | } else { |
| 4020 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 4021 | } |
| 4022 | break; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4023 | } |
| 4024 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4025 | // Send events! |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4026 | int32_t metaState = getContext()->getGlobalMetaState(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4027 | int32_t buttonState = mCurrentButtonState; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4028 | |
| 4029 | // Update last coordinates of pointers that have moved so that we observe the new |
| 4030 | // 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] | 4031 | bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP |
| 4032 | || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG |
| 4033 | || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4034 | || mPointerGesture.currentGestureMode == PointerGesture::PRESS |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4035 | || mPointerGesture.currentGestureMode == PointerGesture::SWIPE |
| 4036 | || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM; |
| 4037 | bool moveNeeded = false; |
| 4038 | if (down && !cancelPreviousGesture && !finishPreviousGesture |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4039 | && !mPointerGesture.lastGestureIdBits.isEmpty() |
| 4040 | && !mPointerGesture.currentGestureIdBits.isEmpty()) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4041 | BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value |
| 4042 | & mPointerGesture.lastGestureIdBits.value); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4043 | moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4044 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4045 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4046 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4047 | movedGestureIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4048 | if (buttonState != mLastButtonState) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4049 | moveNeeded = true; |
| 4050 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4051 | } |
| 4052 | |
| 4053 | // Send motion events for all pointers that went up or were canceled. |
| 4054 | BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits); |
| 4055 | if (!dispatchedGestureIdBits.isEmpty()) { |
| 4056 | if (cancelPreviousGesture) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4057 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4058 | AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState, |
| 4059 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4060 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4061 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4062 | dispatchedGestureIdBits, -1, |
| 4063 | 0, 0, mPointerGesture.downTime); |
| 4064 | |
| 4065 | dispatchedGestureIdBits.clear(); |
| 4066 | } else { |
| 4067 | BitSet32 upGestureIdBits; |
| 4068 | if (finishPreviousGesture) { |
| 4069 | upGestureIdBits = dispatchedGestureIdBits; |
| 4070 | } else { |
| 4071 | upGestureIdBits.value = dispatchedGestureIdBits.value |
| 4072 | & ~mPointerGesture.currentGestureIdBits.value; |
| 4073 | } |
| 4074 | while (!upGestureIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4075 | uint32_t id = upGestureIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4076 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4077 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4078 | AMOTION_EVENT_ACTION_POINTER_UP, 0, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4079 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4080 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4081 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4082 | dispatchedGestureIdBits, id, |
| 4083 | 0, 0, mPointerGesture.downTime); |
| 4084 | |
| 4085 | dispatchedGestureIdBits.clearBit(id); |
| 4086 | } |
| 4087 | } |
| 4088 | } |
| 4089 | |
| 4090 | // Send motion events for all pointers that moved. |
| 4091 | if (moveNeeded) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4092 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4093 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4094 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4095 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 4096 | dispatchedGestureIdBits, -1, |
| 4097 | 0, 0, mPointerGesture.downTime); |
| 4098 | } |
| 4099 | |
| 4100 | // Send motion events for all pointers that went down. |
| 4101 | if (down) { |
| 4102 | BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value |
| 4103 | & ~dispatchedGestureIdBits.value); |
| 4104 | while (!downGestureIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4105 | uint32_t id = downGestureIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4106 | dispatchedGestureIdBits.markBit(id); |
| 4107 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4108 | if (dispatchedGestureIdBits.count() == 1) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4109 | mPointerGesture.downTime = when; |
| 4110 | } |
| 4111 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4112 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | a611137 | 2011-07-14 21:48:23 -0700 | [diff] [blame] | 4113 | AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4114 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4115 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 4116 | dispatchedGestureIdBits, id, |
| 4117 | 0, 0, mPointerGesture.downTime); |
| 4118 | } |
| 4119 | } |
| 4120 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4121 | // Send motion events for hover. |
| 4122 | if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4123 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4124 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 4125 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4126 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4127 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 4128 | mPointerGesture.currentGestureIdBits, -1, |
| 4129 | 0, 0, mPointerGesture.downTime); |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4130 | } else if (dispatchedGestureIdBits.isEmpty() |
| 4131 | && !mPointerGesture.lastGestureIdBits.isEmpty()) { |
| 4132 | // Synthesize a hover move event after all pointers go up to indicate that |
| 4133 | // the pointer is hovering again even if the user is not currently touching |
| 4134 | // the touch pad. This ensures that a view will receive a fresh hover enter |
| 4135 | // event after a tap. |
| 4136 | float x, y; |
| 4137 | mPointerController->getPosition(&x, &y); |
| 4138 | |
| 4139 | PointerProperties pointerProperties; |
| 4140 | pointerProperties.clear(); |
| 4141 | pointerProperties.id = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4142 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4143 | |
| 4144 | PointerCoords pointerCoords; |
| 4145 | pointerCoords.clear(); |
| 4146 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4147 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 4148 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4149 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4150 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 4151 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4152 | 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4153 | getListener()->notifyMotion(&args); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4154 | } |
| 4155 | |
| 4156 | // Update state. |
| 4157 | mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode; |
| 4158 | if (!down) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4159 | mPointerGesture.lastGestureIdBits.clear(); |
| 4160 | } else { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4161 | mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits; |
| 4162 | for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4163 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4164 | uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4165 | mPointerGesture.lastGestureProperties[index].copyFrom( |
| 4166 | mPointerGesture.currentGestureProperties[index]); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4167 | mPointerGesture.lastGestureCoords[index].copyFrom( |
| 4168 | mPointerGesture.currentGestureCoords[index]); |
| 4169 | mPointerGesture.lastGestureIdToIndex[id] = index; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4170 | } |
| 4171 | } |
| 4172 | } |
| 4173 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4174 | void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) { |
| 4175 | // Cancel previously dispatches pointers. |
| 4176 | if (!mPointerGesture.lastGestureIdBits.isEmpty()) { |
| 4177 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 4178 | int32_t buttonState = mCurrentButtonState; |
| 4179 | dispatchMotion(when, policyFlags, mSource, |
| 4180 | AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState, |
| 4181 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4182 | mPointerGesture.lastGestureProperties, |
| 4183 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4184 | mPointerGesture.lastGestureIdBits, -1, |
| 4185 | 0, 0, mPointerGesture.downTime); |
| 4186 | } |
| 4187 | |
| 4188 | // Reset the current pointer gesture. |
| 4189 | mPointerGesture.reset(); |
| 4190 | mPointerVelocityControl.reset(); |
| 4191 | |
| 4192 | // Remove any current spots. |
| 4193 | if (mPointerController != NULL) { |
| 4194 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 4195 | mPointerController->clearSpots(); |
| 4196 | } |
| 4197 | } |
| 4198 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4199 | bool TouchInputMapper::preparePointerGestures(nsecs_t when, |
| 4200 | bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4201 | *outCancelPreviousGesture = false; |
| 4202 | *outFinishPreviousGesture = false; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4203 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4204 | // Handle TAP timeout. |
| 4205 | if (isTimeout) { |
| 4206 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4207 | ALOGD("Gestures: Processing timeout"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4208 | #endif |
| 4209 | |
| 4210 | if (mPointerGesture.lastGestureMode == PointerGesture::TAP) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4211 | if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4212 | // The tap/drag timeout has not yet expired. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4213 | getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4214 | + mConfig.pointerGestureTapDragInterval); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4215 | } else { |
| 4216 | // The tap is finished. |
| 4217 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4218 | ALOGD("Gestures: TAP finished"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4219 | #endif |
| 4220 | *outFinishPreviousGesture = true; |
| 4221 | |
| 4222 | mPointerGesture.activeGestureId = -1; |
| 4223 | mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL; |
| 4224 | mPointerGesture.currentGestureIdBits.clear(); |
| 4225 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4226 | mPointerVelocityControl.reset(); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4227 | return true; |
| 4228 | } |
| 4229 | } |
| 4230 | |
| 4231 | // We did not handle this timeout. |
| 4232 | return false; |
| 4233 | } |
| 4234 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4235 | const uint32_t currentFingerCount = mCurrentFingerIdBits.count(); |
| 4236 | const uint32_t lastFingerCount = mLastFingerIdBits.count(); |
| 4237 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4238 | // Update the velocity tracker. |
| 4239 | { |
| 4240 | VelocityTracker::Position positions[MAX_POINTERS]; |
| 4241 | uint32_t count = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4242 | for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4243 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 4244 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4245 | positions[count].x = pointer.x * mPointerXMovementScale; |
| 4246 | positions[count].y = pointer.y * mPointerYMovementScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4247 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4248 | mPointerGesture.velocityTracker.addMovement(when, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4249 | mCurrentFingerIdBits, positions); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4250 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4251 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4252 | // Pick a new active touch id if needed. |
| 4253 | // Choose an arbitrary pointer that just went down, if there is one. |
| 4254 | // Otherwise choose an arbitrary remaining pointer. |
| 4255 | // 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] | 4256 | // We keep the same active touch id for as long as possible. |
| 4257 | bool activeTouchChanged = false; |
| 4258 | int32_t lastActiveTouchId = mPointerGesture.activeTouchId; |
| 4259 | int32_t activeTouchId = lastActiveTouchId; |
| 4260 | if (activeTouchId < 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4261 | if (!mCurrentFingerIdBits.isEmpty()) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4262 | activeTouchChanged = true; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4263 | activeTouchId = mPointerGesture.activeTouchId = |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4264 | mCurrentFingerIdBits.firstMarkedBit(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4265 | mPointerGesture.firstTouchTime = when; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4266 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4267 | } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4268 | activeTouchChanged = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4269 | if (!mCurrentFingerIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4270 | activeTouchId = mPointerGesture.activeTouchId = |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4271 | mCurrentFingerIdBits.firstMarkedBit(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4272 | } else { |
| 4273 | activeTouchId = mPointerGesture.activeTouchId = -1; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4274 | } |
| 4275 | } |
| 4276 | |
| 4277 | // Determine whether we are in quiet time. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4278 | bool isQuietTime = false; |
| 4279 | if (activeTouchId < 0) { |
| 4280 | mPointerGesture.resetQuietTime(); |
| 4281 | } else { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4282 | isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4283 | if (!isQuietTime) { |
| 4284 | if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS |
| 4285 | || mPointerGesture.lastGestureMode == PointerGesture::SWIPE |
| 4286 | || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4287 | && currentFingerCount < 2) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4288 | // Enter quiet time when exiting swipe or freeform state. |
| 4289 | // This is to prevent accidentally entering the hover state and flinging the |
| 4290 | // pointer when finishing a swipe and there is still one pointer left onscreen. |
| 4291 | isQuietTime = true; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4292 | } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4293 | && currentFingerCount >= 2 |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4294 | && !isPointerDown(mCurrentButtonState)) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4295 | // Enter quiet time when releasing the button and there are still two or more |
| 4296 | // fingers down. This may indicate that one finger was used to press the button |
| 4297 | // but it has not gone up yet. |
| 4298 | isQuietTime = true; |
| 4299 | } |
| 4300 | if (isQuietTime) { |
| 4301 | mPointerGesture.quietTime = when; |
| 4302 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4303 | } |
| 4304 | } |
| 4305 | |
| 4306 | // Switch states based on button and pointer state. |
| 4307 | if (isQuietTime) { |
| 4308 | // Case 1: Quiet time. (QUIET) |
| 4309 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4310 | ALOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4311 | + mConfig.pointerGestureQuietInterval - when) * 0.000001f); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4312 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4313 | if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) { |
| 4314 | *outFinishPreviousGesture = true; |
| 4315 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4316 | |
| 4317 | mPointerGesture.activeGestureId = -1; |
| 4318 | mPointerGesture.currentGestureMode = PointerGesture::QUIET; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4319 | mPointerGesture.currentGestureIdBits.clear(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4320 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4321 | mPointerVelocityControl.reset(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4322 | } else if (isPointerDown(mCurrentButtonState)) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4323 | // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG) |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4324 | // The pointer follows the active touch point. |
| 4325 | // Emit DOWN, MOVE, UP events at the pointer location. |
| 4326 | // |
| 4327 | // Only the active touch matters; other fingers are ignored. This policy helps |
| 4328 | // to handle the case where the user places a second finger on the touch pad |
| 4329 | // to apply the necessary force to depress an integrated button below the surface. |
| 4330 | // We don't want the second finger to be delivered to applications. |
| 4331 | // |
| 4332 | // For this to work well, we need to make sure to track the pointer that is really |
| 4333 | // active. If the user first puts one finger down to click then adds another |
| 4334 | // finger to drag then the active pointer should switch to the finger that is |
| 4335 | // being dragged. |
| 4336 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4337 | ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4338 | "currentFingerCount=%d", activeTouchId, currentFingerCount); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4339 | #endif |
| 4340 | // Reset state when just starting. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4341 | if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4342 | *outFinishPreviousGesture = true; |
| 4343 | mPointerGesture.activeGestureId = 0; |
| 4344 | } |
| 4345 | |
| 4346 | // Switch pointers if needed. |
| 4347 | // Find the fastest pointer and follow it. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4348 | if (activeTouchId >= 0 && currentFingerCount > 1) { |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4349 | int32_t bestId = -1; |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4350 | float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4351 | for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4352 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4353 | float vx, vy; |
| 4354 | if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) { |
| 4355 | float speed = hypotf(vx, vy); |
| 4356 | if (speed > bestSpeed) { |
| 4357 | bestId = id; |
| 4358 | bestSpeed = speed; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4359 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 4360 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4361 | } |
| 4362 | if (bestId >= 0 && bestId != activeTouchId) { |
| 4363 | mPointerGesture.activeTouchId = activeTouchId = bestId; |
| 4364 | activeTouchChanged = true; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4365 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4366 | ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, " |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4367 | "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4368 | #endif |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4369 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4370 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4371 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4372 | if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4373 | const RawPointerData::Pointer& currentPointer = |
| 4374 | mCurrentRawPointerData.pointerForId(activeTouchId); |
| 4375 | const RawPointerData::Pointer& lastPointer = |
| 4376 | mLastRawPointerData.pointerForId(activeTouchId); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4377 | float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale; |
| 4378 | float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4379 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4380 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4381 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4382 | |
| 4383 | // Move the pointer using a relative motion. |
| 4384 | // When using spots, the click will occur at the position of the anchor |
| 4385 | // spot and all other spots will move there. |
| 4386 | mPointerController->move(deltaX, deltaY); |
| 4387 | } else { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4388 | mPointerVelocityControl.reset(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4389 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4390 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4391 | float x, y; |
| 4392 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 4393 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4394 | mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4395 | mPointerGesture.currentGestureIdBits.clear(); |
| 4396 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4397 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4398 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4399 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4400 | mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4401 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4402 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4403 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 4404 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4405 | } else if (currentFingerCount == 0) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4406 | // Case 3. No fingers down and button is not pressed. (NEUTRAL) |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4407 | if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) { |
| 4408 | *outFinishPreviousGesture = true; |
| 4409 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4410 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4411 | // Watch for taps coming out of HOVER or TAP_DRAG mode. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4412 | // Checking for taps after TAP_DRAG allows us to detect double-taps. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4413 | bool tapped = false; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4414 | if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER |
| 4415 | || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4416 | && lastFingerCount == 1) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4417 | if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4418 | float x, y; |
| 4419 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4420 | if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop |
| 4421 | && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4422 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4423 | ALOGD("Gestures: TAP"); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4424 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4425 | |
| 4426 | mPointerGesture.tapUpTime = when; |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4427 | getContext()->requestTimeoutAtTime(when |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4428 | + mConfig.pointerGestureTapDragInterval); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4429 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4430 | mPointerGesture.activeGestureId = 0; |
| 4431 | mPointerGesture.currentGestureMode = PointerGesture::TAP; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4432 | mPointerGesture.currentGestureIdBits.clear(); |
| 4433 | mPointerGesture.currentGestureIdBits.markBit( |
| 4434 | mPointerGesture.activeGestureId); |
| 4435 | mPointerGesture.currentGestureIdToIndex[ |
| 4436 | mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4437 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4438 | mPointerGesture.currentGestureProperties[0].id = |
| 4439 | mPointerGesture.activeGestureId; |
| 4440 | mPointerGesture.currentGestureProperties[0].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4441 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4442 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4443 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4444 | AMOTION_EVENT_AXIS_X, mPointerGesture.tapX); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4445 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4446 | AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4447 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
| 4448 | AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4449 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4450 | tapped = true; |
| 4451 | } else { |
| 4452 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4453 | ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f", |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4454 | x - mPointerGesture.tapX, |
| 4455 | y - mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4456 | #endif |
| 4457 | } |
| 4458 | } else { |
| 4459 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4460 | ALOGD("Gestures: Not a TAP, %0.3fms since down", |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4461 | (when - mPointerGesture.tapDownTime) * 0.000001f); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4462 | #endif |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4463 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4464 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4465 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4466 | mPointerVelocityControl.reset(); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4467 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4468 | if (!tapped) { |
| 4469 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4470 | ALOGD("Gestures: NEUTRAL"); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4471 | #endif |
| 4472 | mPointerGesture.activeGestureId = -1; |
| 4473 | mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4474 | mPointerGesture.currentGestureIdBits.clear(); |
| 4475 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4476 | } else if (currentFingerCount == 1) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4477 | // 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] | 4478 | // The pointer follows the active touch point. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4479 | // When in HOVER, emit HOVER_MOVE events at the pointer location. |
| 4480 | // When in TAP_DRAG, emit MOVE events at the pointer location. |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 4481 | ALOG_ASSERT(activeTouchId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4482 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4483 | mPointerGesture.currentGestureMode = PointerGesture::HOVER; |
| 4484 | if (mPointerGesture.lastGestureMode == PointerGesture::TAP) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4485 | if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4486 | float x, y; |
| 4487 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4488 | if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop |
| 4489 | && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4490 | mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG; |
| 4491 | } else { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4492 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4493 | ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f", |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4494 | x - mPointerGesture.tapX, |
| 4495 | y - mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4496 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4497 | } |
| 4498 | } else { |
| 4499 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4500 | ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up", |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4501 | (when - mPointerGesture.tapUpTime) * 0.000001f); |
| 4502 | #endif |
| 4503 | } |
| 4504 | } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) { |
| 4505 | mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG; |
| 4506 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4507 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4508 | if (mLastFingerIdBits.hasBit(activeTouchId)) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4509 | const RawPointerData::Pointer& currentPointer = |
| 4510 | mCurrentRawPointerData.pointerForId(activeTouchId); |
| 4511 | const RawPointerData::Pointer& lastPointer = |
| 4512 | mLastRawPointerData.pointerForId(activeTouchId); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4513 | float deltaX = (currentPointer.x - lastPointer.x) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4514 | * mPointerXMovementScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4515 | float deltaY = (currentPointer.y - lastPointer.y) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4516 | * mPointerYMovementScale; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4517 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4518 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4519 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4520 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4521 | // Move the pointer using a relative motion. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4522 | // 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] | 4523 | mPointerController->move(deltaX, deltaY); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4524 | } else { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4525 | mPointerVelocityControl.reset(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4526 | } |
| 4527 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4528 | bool down; |
| 4529 | if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) { |
| 4530 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4531 | ALOGD("Gestures: TAP_DRAG"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4532 | #endif |
| 4533 | down = true; |
| 4534 | } else { |
| 4535 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4536 | ALOGD("Gestures: HOVER"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4537 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4538 | if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) { |
| 4539 | *outFinishPreviousGesture = true; |
| 4540 | } |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4541 | mPointerGesture.activeGestureId = 0; |
| 4542 | down = false; |
| 4543 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4544 | |
| 4545 | float x, y; |
| 4546 | mPointerController->getPosition(&x, &y); |
| 4547 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4548 | mPointerGesture.currentGestureIdBits.clear(); |
| 4549 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4550 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4551 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4552 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
| 4553 | mPointerGesture.currentGestureProperties[0].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4554 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4555 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4556 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4557 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4558 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, |
| 4559 | down ? 1.0f : 0.0f); |
| 4560 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4561 | if (lastFingerCount == 0 && currentFingerCount != 0) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4562 | mPointerGesture.resetTap(); |
| 4563 | mPointerGesture.tapDownTime = when; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4564 | mPointerGesture.tapX = x; |
| 4565 | mPointerGesture.tapY = y; |
| 4566 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4567 | } else { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4568 | // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM) |
| 4569 | // We need to provide feedback for each finger that goes down so we cannot wait |
| 4570 | // for the fingers to move before deciding what to do. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4571 | // |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4572 | // The ambiguous case is deciding what to do when there are two fingers down but they |
| 4573 | // have not moved enough to determine whether they are part of a drag or part of a |
| 4574 | // freeform gesture, or just a press or long-press at the pointer location. |
| 4575 | // |
| 4576 | // When there are two fingers we start with the PRESS hypothesis and we generate a |
| 4577 | // down at the pointer location. |
| 4578 | // |
| 4579 | // When the two fingers move enough or when additional fingers are added, we make |
| 4580 | // a decision to transition into SWIPE or FREEFORM mode accordingly. |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 4581 | ALOG_ASSERT(activeTouchId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4582 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4583 | bool settled = when >= mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4584 | + mConfig.pointerGestureMultitouchSettleInterval; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4585 | if (mPointerGesture.lastGestureMode != PointerGesture::PRESS |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4586 | && mPointerGesture.lastGestureMode != PointerGesture::SWIPE |
| 4587 | && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4588 | *outFinishPreviousGesture = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4589 | } else if (!settled && currentFingerCount > lastFingerCount) { |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4590 | // Additional pointers have gone down but not yet settled. |
| 4591 | // Reset the gesture. |
| 4592 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4593 | ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, " |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4594 | "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4595 | + mConfig.pointerGestureMultitouchSettleInterval - when) |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4596 | * 0.000001f); |
| 4597 | #endif |
| 4598 | *outCancelPreviousGesture = true; |
| 4599 | } else { |
| 4600 | // Continue previous gesture. |
| 4601 | mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode; |
| 4602 | } |
| 4603 | |
| 4604 | if (*outFinishPreviousGesture || *outCancelPreviousGesture) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4605 | mPointerGesture.currentGestureMode = PointerGesture::PRESS; |
| 4606 | mPointerGesture.activeGestureId = 0; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4607 | mPointerGesture.referenceIdBits.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4608 | mPointerVelocityControl.reset(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4609 | |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4610 | // 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] | 4611 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4612 | ALOGD("Gestures: Using centroid as reference for MULTITOUCH, " |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4613 | "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4614 | + mConfig.pointerGestureMultitouchSettleInterval - when) |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4615 | * 0.000001f); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4616 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4617 | mCurrentRawPointerData.getCentroidOfTouchingPointers( |
| 4618 | &mPointerGesture.referenceTouchX, |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4619 | &mPointerGesture.referenceTouchY); |
| 4620 | mPointerController->getPosition(&mPointerGesture.referenceGestureX, |
| 4621 | &mPointerGesture.referenceGestureY); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4622 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4623 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4624 | // 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] | 4625 | for (BitSet32 idBits(mCurrentFingerIdBits.value |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4626 | & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) { |
| 4627 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4628 | mPointerGesture.referenceDeltas[id].dx = 0; |
| 4629 | mPointerGesture.referenceDeltas[id].dy = 0; |
| 4630 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4631 | mPointerGesture.referenceIdBits = mCurrentFingerIdBits; |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4632 | |
| 4633 | // Add delta for all fingers and calculate a common movement delta. |
| 4634 | float commonDeltaX = 0, commonDeltaY = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4635 | BitSet32 commonIdBits(mLastFingerIdBits.value |
| 4636 | & mCurrentFingerIdBits.value); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4637 | for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) { |
| 4638 | bool first = (idBits == commonIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4639 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 4640 | const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id); |
| 4641 | const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4642 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
| 4643 | delta.dx += cpd.x - lpd.x; |
| 4644 | delta.dy += cpd.y - lpd.y; |
| 4645 | |
| 4646 | if (first) { |
| 4647 | commonDeltaX = delta.dx; |
| 4648 | commonDeltaY = delta.dy; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4649 | } else { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4650 | commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx); |
| 4651 | commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy); |
| 4652 | } |
| 4653 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4654 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4655 | // Consider transitions from PRESS to SWIPE or MULTITOUCH. |
| 4656 | if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) { |
| 4657 | float dist[MAX_POINTER_ID + 1]; |
| 4658 | int32_t distOverThreshold = 0; |
| 4659 | for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4660 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4661 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4662 | dist[id] = hypotf(delta.dx * mPointerXZoomScale, |
| 4663 | delta.dy * mPointerYZoomScale); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4664 | if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4665 | distOverThreshold += 1; |
| 4666 | } |
| 4667 | } |
| 4668 | |
| 4669 | // Only transition when at least two pointers have moved further than |
| 4670 | // the minimum distance threshold. |
| 4671 | if (distOverThreshold >= 2) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4672 | if (currentFingerCount > 2) { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4673 | // There are more than two pointers, switch to FREEFORM. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4674 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4675 | ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4676 | currentFingerCount); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4677 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4678 | *outCancelPreviousGesture = true; |
| 4679 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 4680 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4681 | // There are exactly two pointers. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4682 | BitSet32 idBits(mCurrentFingerIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4683 | uint32_t id1 = idBits.clearFirstMarkedBit(); |
| 4684 | uint32_t id2 = idBits.firstMarkedBit(); |
| 4685 | const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1); |
| 4686 | const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2); |
| 4687 | float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y); |
| 4688 | if (mutualDistance > mPointerGestureMaxSwipeWidth) { |
| 4689 | // There are two pointers but they are too far apart for a SWIPE, |
| 4690 | // switch to FREEFORM. |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4691 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4692 | ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4693 | mutualDistance, mPointerGestureMaxSwipeWidth); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4694 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4695 | *outCancelPreviousGesture = true; |
| 4696 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 4697 | } else { |
| 4698 | // There are two pointers. Wait for both pointers to start moving |
| 4699 | // before deciding whether this is a SWIPE or FREEFORM gesture. |
| 4700 | float dist1 = dist[id1]; |
| 4701 | float dist2 = dist[id2]; |
| 4702 | if (dist1 >= mConfig.pointerGestureMultitouchMinDistance |
| 4703 | && dist2 >= mConfig.pointerGestureMultitouchMinDistance) { |
| 4704 | // Calculate the dot product of the displacement vectors. |
| 4705 | // When the vectors are oriented in approximately the same direction, |
| 4706 | // the angle betweeen them is near zero and the cosine of the angle |
| 4707 | // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2). |
| 4708 | PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1]; |
| 4709 | PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4710 | float dx1 = delta1.dx * mPointerXZoomScale; |
| 4711 | float dy1 = delta1.dy * mPointerYZoomScale; |
| 4712 | float dx2 = delta2.dx * mPointerXZoomScale; |
| 4713 | float dy2 = delta2.dy * mPointerYZoomScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4714 | float dot = dx1 * dx2 + dy1 * dy2; |
| 4715 | float cosine = dot / (dist1 * dist2); // denominator always > 0 |
| 4716 | if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) { |
| 4717 | // Pointers are moving in the same direction. Switch to SWIPE. |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4718 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4719 | ALOGD("Gestures: PRESS transitioned to SWIPE, " |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4720 | "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, " |
| 4721 | "cosine %0.3f >= %0.3f", |
| 4722 | dist1, mConfig.pointerGestureMultitouchMinDistance, |
| 4723 | dist2, mConfig.pointerGestureMultitouchMinDistance, |
| 4724 | cosine, mConfig.pointerGestureSwipeTransitionAngleCosine); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4725 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4726 | mPointerGesture.currentGestureMode = PointerGesture::SWIPE; |
| 4727 | } else { |
| 4728 | // Pointers are moving in different directions. Switch to FREEFORM. |
| 4729 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4730 | ALOGD("Gestures: PRESS transitioned to FREEFORM, " |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4731 | "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, " |
| 4732 | "cosine %0.3f < %0.3f", |
| 4733 | dist1, mConfig.pointerGestureMultitouchMinDistance, |
| 4734 | dist2, mConfig.pointerGestureMultitouchMinDistance, |
| 4735 | cosine, mConfig.pointerGestureSwipeTransitionAngleCosine); |
| 4736 | #endif |
| 4737 | *outCancelPreviousGesture = true; |
| 4738 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 4739 | } |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4740 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4741 | } |
| 4742 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4743 | } |
| 4744 | } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4745 | // Switch from SWIPE to FREEFORM if additional pointers go down. |
| 4746 | // Cancel previous gesture. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4747 | if (currentFingerCount > 2) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4748 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4749 | ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4750 | currentFingerCount); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4751 | #endif |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4752 | *outCancelPreviousGesture = true; |
| 4753 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4754 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4755 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4756 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4757 | // Move the reference points based on the overall group motion of the fingers |
| 4758 | // except in PRESS mode while waiting for a transition to occur. |
| 4759 | if (mPointerGesture.currentGestureMode != PointerGesture::PRESS |
| 4760 | && (commonDeltaX || commonDeltaY)) { |
| 4761 | for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4762 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4763 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4764 | delta.dx = 0; |
| 4765 | delta.dy = 0; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4766 | } |
| 4767 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4768 | mPointerGesture.referenceTouchX += commonDeltaX; |
| 4769 | mPointerGesture.referenceTouchY += commonDeltaY; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4770 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4771 | commonDeltaX *= mPointerXMovementScale; |
| 4772 | commonDeltaY *= mPointerYMovementScale; |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 4773 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4774 | rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4775 | mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4776 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4777 | mPointerGesture.referenceGestureX += commonDeltaX; |
| 4778 | mPointerGesture.referenceGestureY += commonDeltaY; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4779 | } |
| 4780 | |
| 4781 | // Report gestures. |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 4782 | if (mPointerGesture.currentGestureMode == PointerGesture::PRESS |
| 4783 | || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) { |
| 4784 | // PRESS or SWIPE mode. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4785 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4786 | ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d," |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4787 | "activeGestureId=%d, currentTouchPointerCount=%d", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4788 | activeTouchId, mPointerGesture.activeGestureId, currentFingerCount); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4789 | #endif |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 4790 | ALOG_ASSERT(mPointerGesture.activeGestureId >= 0); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4791 | |
| 4792 | mPointerGesture.currentGestureIdBits.clear(); |
| 4793 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4794 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4795 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4796 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
| 4797 | mPointerGesture.currentGestureProperties[0].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4798 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4799 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4800 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 4801 | mPointerGesture.referenceGestureX); |
| 4802 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, |
| 4803 | mPointerGesture.referenceGestureY); |
| 4804 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4805 | } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) { |
| 4806 | // FREEFORM mode. |
| 4807 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4808 | ALOGD("Gestures: FREEFORM activeTouchId=%d," |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4809 | "activeGestureId=%d, currentTouchPointerCount=%d", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4810 | activeTouchId, mPointerGesture.activeGestureId, currentFingerCount); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4811 | #endif |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 4812 | ALOG_ASSERT(mPointerGesture.activeGestureId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4813 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4814 | mPointerGesture.currentGestureIdBits.clear(); |
| 4815 | |
| 4816 | BitSet32 mappedTouchIdBits; |
| 4817 | BitSet32 usedGestureIdBits; |
| 4818 | if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) { |
| 4819 | // Initially, assign the active gesture id to the active touch point |
| 4820 | // if there is one. No other touch id bits are mapped yet. |
| 4821 | if (!*outCancelPreviousGesture) { |
| 4822 | mappedTouchIdBits.markBit(activeTouchId); |
| 4823 | usedGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4824 | mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] = |
| 4825 | mPointerGesture.activeGestureId; |
| 4826 | } else { |
| 4827 | mPointerGesture.activeGestureId = -1; |
| 4828 | } |
| 4829 | } else { |
| 4830 | // Otherwise, assume we mapped all touches from the previous frame. |
| 4831 | // Reuse all mappings that are still applicable. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4832 | mappedTouchIdBits.value = mLastFingerIdBits.value |
| 4833 | & mCurrentFingerIdBits.value; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4834 | usedGestureIdBits = mPointerGesture.lastGestureIdBits; |
| 4835 | |
| 4836 | // Check whether we need to choose a new active gesture id because the |
| 4837 | // current went went up. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4838 | for (BitSet32 upTouchIdBits(mLastFingerIdBits.value |
| 4839 | & ~mCurrentFingerIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4840 | !upTouchIdBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4841 | uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4842 | uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId]; |
| 4843 | if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) { |
| 4844 | mPointerGesture.activeGestureId = -1; |
| 4845 | break; |
| 4846 | } |
| 4847 | } |
| 4848 | } |
| 4849 | |
| 4850 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4851 | ALOGD("Gestures: FREEFORM follow up " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4852 | "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, " |
| 4853 | "activeGestureId=%d", |
| 4854 | mappedTouchIdBits.value, usedGestureIdBits.value, |
| 4855 | mPointerGesture.activeGestureId); |
| 4856 | #endif |
| 4857 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4858 | BitSet32 idBits(mCurrentFingerIdBits); |
| 4859 | for (uint32_t i = 0; i < currentFingerCount; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4860 | uint32_t touchId = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4861 | uint32_t gestureId; |
| 4862 | if (!mappedTouchIdBits.hasBit(touchId)) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4863 | gestureId = usedGestureIdBits.markFirstUnmarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4864 | mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId; |
| 4865 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4866 | ALOGD("Gestures: FREEFORM " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4867 | "new mapping for touch id %d -> gesture id %d", |
| 4868 | touchId, gestureId); |
| 4869 | #endif |
| 4870 | } else { |
| 4871 | gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId]; |
| 4872 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4873 | ALOGD("Gestures: FREEFORM " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4874 | "existing mapping for touch id %d -> gesture id %d", |
| 4875 | touchId, gestureId); |
| 4876 | #endif |
| 4877 | } |
| 4878 | mPointerGesture.currentGestureIdBits.markBit(gestureId); |
| 4879 | mPointerGesture.currentGestureIdToIndex[gestureId] = i; |
| 4880 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4881 | const RawPointerData::Pointer& pointer = |
| 4882 | mCurrentRawPointerData.pointerForId(touchId); |
| 4883 | float deltaX = (pointer.x - mPointerGesture.referenceTouchX) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4884 | * mPointerXZoomScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4885 | float deltaY = (pointer.y - mPointerGesture.referenceTouchY) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4886 | * mPointerYZoomScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4887 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4888 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4889 | mPointerGesture.currentGestureProperties[i].clear(); |
| 4890 | mPointerGesture.currentGestureProperties[i].id = gestureId; |
| 4891 | mPointerGesture.currentGestureProperties[i].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4892 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4893 | mPointerGesture.currentGestureCoords[i].clear(); |
| 4894 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 4895 | AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4896 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 4897 | AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4898 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
| 4899 | AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
| 4900 | } |
| 4901 | |
| 4902 | if (mPointerGesture.activeGestureId < 0) { |
| 4903 | mPointerGesture.activeGestureId = |
| 4904 | mPointerGesture.currentGestureIdBits.firstMarkedBit(); |
| 4905 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4906 | ALOGD("Gestures: FREEFORM new " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4907 | "activeGestureId=%d", mPointerGesture.activeGestureId); |
| 4908 | #endif |
| 4909 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4910 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4911 | } |
| 4912 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4913 | mPointerController->setButtonState(mCurrentButtonState); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4914 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4915 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4916 | ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, " |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4917 | "currentGestureMode=%d, currentGestureIdBits=0x%08x, " |
| 4918 | "lastGestureMode=%d, lastGestureIdBits=0x%08x", |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4919 | toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture), |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4920 | mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value, |
| 4921 | mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4922 | for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4923 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4924 | uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4925 | const PointerProperties& properties = mPointerGesture.currentGestureProperties[index]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4926 | const PointerCoords& coords = mPointerGesture.currentGestureCoords[index]; |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4927 | ALOGD(" currentGesture[%d]: index=%d, toolType=%d, " |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4928 | "x=%0.3f, y=%0.3f, pressure=%0.3f", |
| 4929 | id, index, properties.toolType, |
| 4930 | coords.getAxisValue(AMOTION_EVENT_AXIS_X), |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4931 | coords.getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4932 | coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); |
| 4933 | } |
| 4934 | for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4935 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4936 | uint32_t index = mPointerGesture.lastGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4937 | const PointerProperties& properties = mPointerGesture.lastGestureProperties[index]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4938 | const PointerCoords& coords = mPointerGesture.lastGestureCoords[index]; |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4939 | ALOGD(" lastGesture[%d]: index=%d, toolType=%d, " |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4940 | "x=%0.3f, y=%0.3f, pressure=%0.3f", |
| 4941 | id, index, properties.toolType, |
| 4942 | coords.getAxisValue(AMOTION_EVENT_AXIS_X), |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4943 | coords.getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4944 | coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); |
| 4945 | } |
| 4946 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4947 | return true; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4948 | } |
| 4949 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4950 | void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) { |
| 4951 | mPointerSimple.currentCoords.clear(); |
| 4952 | mPointerSimple.currentProperties.clear(); |
| 4953 | |
| 4954 | bool down, hovering; |
| 4955 | if (!mCurrentStylusIdBits.isEmpty()) { |
| 4956 | uint32_t id = mCurrentStylusIdBits.firstMarkedBit(); |
| 4957 | uint32_t index = mCurrentCookedPointerData.idToIndex[id]; |
| 4958 | float x = mCurrentCookedPointerData.pointerCoords[index].getX(); |
| 4959 | float y = mCurrentCookedPointerData.pointerCoords[index].getY(); |
| 4960 | mPointerController->setPosition(x, y); |
| 4961 | |
| 4962 | hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id); |
| 4963 | down = !hovering; |
| 4964 | |
| 4965 | mPointerController->getPosition(&x, &y); |
| 4966 | mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]); |
| 4967 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4968 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 4969 | mPointerSimple.currentProperties.id = 0; |
| 4970 | mPointerSimple.currentProperties.toolType = |
| 4971 | mCurrentCookedPointerData.pointerProperties[index].toolType; |
| 4972 | } else { |
| 4973 | down = false; |
| 4974 | hovering = false; |
| 4975 | } |
| 4976 | |
| 4977 | dispatchPointerSimple(when, policyFlags, down, hovering); |
| 4978 | } |
| 4979 | |
| 4980 | void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) { |
| 4981 | abortPointerSimple(when, policyFlags); |
| 4982 | } |
| 4983 | |
| 4984 | void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) { |
| 4985 | mPointerSimple.currentCoords.clear(); |
| 4986 | mPointerSimple.currentProperties.clear(); |
| 4987 | |
| 4988 | bool down, hovering; |
| 4989 | if (!mCurrentMouseIdBits.isEmpty()) { |
| 4990 | uint32_t id = mCurrentMouseIdBits.firstMarkedBit(); |
| 4991 | uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id]; |
| 4992 | if (mLastMouseIdBits.hasBit(id)) { |
| 4993 | uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id]; |
| 4994 | float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x |
| 4995 | - mLastRawPointerData.pointers[lastIndex].x) |
| 4996 | * mPointerXMovementScale; |
| 4997 | float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y |
| 4998 | - mLastRawPointerData.pointers[lastIndex].y) |
| 4999 | * mPointerYMovementScale; |
| 5000 | |
| 5001 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
| 5002 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
| 5003 | |
| 5004 | mPointerController->move(deltaX, deltaY); |
| 5005 | } else { |
| 5006 | mPointerVelocityControl.reset(); |
| 5007 | } |
| 5008 | |
| 5009 | down = isPointerDown(mCurrentButtonState); |
| 5010 | hovering = !down; |
| 5011 | |
| 5012 | float x, y; |
| 5013 | mPointerController->getPosition(&x, &y); |
| 5014 | mPointerSimple.currentCoords.copyFrom( |
| 5015 | mCurrentCookedPointerData.pointerCoords[currentIndex]); |
| 5016 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 5017 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 5018 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, |
| 5019 | hovering ? 0.0f : 1.0f); |
| 5020 | mPointerSimple.currentProperties.id = 0; |
| 5021 | mPointerSimple.currentProperties.toolType = |
| 5022 | mCurrentCookedPointerData.pointerProperties[currentIndex].toolType; |
| 5023 | } else { |
| 5024 | mPointerVelocityControl.reset(); |
| 5025 | |
| 5026 | down = false; |
| 5027 | hovering = false; |
| 5028 | } |
| 5029 | |
| 5030 | dispatchPointerSimple(when, policyFlags, down, hovering); |
| 5031 | } |
| 5032 | |
| 5033 | void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) { |
| 5034 | abortPointerSimple(when, policyFlags); |
| 5035 | |
| 5036 | mPointerVelocityControl.reset(); |
| 5037 | } |
| 5038 | |
| 5039 | void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags, |
| 5040 | bool down, bool hovering) { |
| 5041 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 5042 | |
| 5043 | if (mPointerController != NULL) { |
| 5044 | if (down || hovering) { |
| 5045 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER); |
| 5046 | mPointerController->clearSpots(); |
| 5047 | mPointerController->setButtonState(mCurrentButtonState); |
| 5048 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 5049 | } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) { |
| 5050 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 5051 | } |
| 5052 | } |
| 5053 | |
| 5054 | if (mPointerSimple.down && !down) { |
| 5055 | mPointerSimple.down = false; |
| 5056 | |
| 5057 | // Send up. |
| 5058 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5059 | AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0, |
| 5060 | 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords, |
| 5061 | mOrientedXPrecision, mOrientedYPrecision, |
| 5062 | mPointerSimple.downTime); |
| 5063 | getListener()->notifyMotion(&args); |
| 5064 | } |
| 5065 | |
| 5066 | if (mPointerSimple.hovering && !hovering) { |
| 5067 | mPointerSimple.hovering = false; |
| 5068 | |
| 5069 | // Send hover exit. |
| 5070 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5071 | AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0, |
| 5072 | 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords, |
| 5073 | mOrientedXPrecision, mOrientedYPrecision, |
| 5074 | mPointerSimple.downTime); |
| 5075 | getListener()->notifyMotion(&args); |
| 5076 | } |
| 5077 | |
| 5078 | if (down) { |
| 5079 | if (!mPointerSimple.down) { |
| 5080 | mPointerSimple.down = true; |
| 5081 | mPointerSimple.downTime = when; |
| 5082 | |
| 5083 | // Send down. |
| 5084 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5085 | AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0, |
| 5086 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5087 | mOrientedXPrecision, mOrientedYPrecision, |
| 5088 | mPointerSimple.downTime); |
| 5089 | getListener()->notifyMotion(&args); |
| 5090 | } |
| 5091 | |
| 5092 | // Send move. |
| 5093 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5094 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0, |
| 5095 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5096 | mOrientedXPrecision, mOrientedYPrecision, |
| 5097 | mPointerSimple.downTime); |
| 5098 | getListener()->notifyMotion(&args); |
| 5099 | } |
| 5100 | |
| 5101 | if (hovering) { |
| 5102 | if (!mPointerSimple.hovering) { |
| 5103 | mPointerSimple.hovering = true; |
| 5104 | |
| 5105 | // Send hover enter. |
| 5106 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5107 | AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0, |
| 5108 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5109 | mOrientedXPrecision, mOrientedYPrecision, |
| 5110 | mPointerSimple.downTime); |
| 5111 | getListener()->notifyMotion(&args); |
| 5112 | } |
| 5113 | |
| 5114 | // Send hover move. |
| 5115 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5116 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0, |
| 5117 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5118 | mOrientedXPrecision, mOrientedYPrecision, |
| 5119 | mPointerSimple.downTime); |
| 5120 | getListener()->notifyMotion(&args); |
| 5121 | } |
| 5122 | |
| 5123 | if (mCurrentRawVScroll || mCurrentRawHScroll) { |
| 5124 | float vscroll = mCurrentRawVScroll; |
| 5125 | float hscroll = mCurrentRawHScroll; |
| 5126 | mWheelYVelocityControl.move(when, NULL, &vscroll); |
| 5127 | mWheelXVelocityControl.move(when, &hscroll, NULL); |
| 5128 | |
| 5129 | // Send scroll. |
| 5130 | PointerCoords pointerCoords; |
| 5131 | pointerCoords.copyFrom(mPointerSimple.currentCoords); |
| 5132 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll); |
| 5133 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll); |
| 5134 | |
| 5135 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5136 | AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0, |
| 5137 | 1, &mPointerSimple.currentProperties, &pointerCoords, |
| 5138 | mOrientedXPrecision, mOrientedYPrecision, |
| 5139 | mPointerSimple.downTime); |
| 5140 | getListener()->notifyMotion(&args); |
| 5141 | } |
| 5142 | |
| 5143 | // Save state. |
| 5144 | if (down || hovering) { |
| 5145 | mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords); |
| 5146 | mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties); |
| 5147 | } else { |
| 5148 | mPointerSimple.reset(); |
| 5149 | } |
| 5150 | } |
| 5151 | |
| 5152 | void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) { |
| 5153 | mPointerSimple.currentCoords.clear(); |
| 5154 | mPointerSimple.currentProperties.clear(); |
| 5155 | |
| 5156 | dispatchPointerSimple(when, policyFlags, false, false); |
| 5157 | } |
| 5158 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5159 | void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5160 | int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags, |
| 5161 | const PointerProperties* properties, const PointerCoords* coords, |
| 5162 | const uint32_t* idToIndex, BitSet32 idBits, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5163 | int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) { |
| 5164 | PointerCoords pointerCoords[MAX_POINTERS]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5165 | PointerProperties pointerProperties[MAX_POINTERS]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5166 | uint32_t pointerCount = 0; |
| 5167 | while (!idBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5168 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5169 | uint32_t index = idToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5170 | pointerProperties[pointerCount].copyFrom(properties[index]); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5171 | pointerCoords[pointerCount].copyFrom(coords[index]); |
| 5172 | |
| 5173 | if (changedId >= 0 && id == uint32_t(changedId)) { |
| 5174 | action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
| 5175 | } |
| 5176 | |
| 5177 | pointerCount += 1; |
| 5178 | } |
| 5179 | |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 5180 | ALOG_ASSERT(pointerCount != 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5181 | |
| 5182 | if (changedId >= 0 && pointerCount == 1) { |
| 5183 | // Replace initial down and final up action. |
| 5184 | // We can compare the action without masking off the changed pointer index |
| 5185 | // because we know the index is 0. |
| 5186 | if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 5187 | action = AMOTION_EVENT_ACTION_DOWN; |
| 5188 | } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 5189 | action = AMOTION_EVENT_ACTION_UP; |
| 5190 | } else { |
| 5191 | // Can't happen. |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 5192 | ALOG_ASSERT(false); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5193 | } |
| 5194 | } |
| 5195 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5196 | NotifyMotionArgs args(when, getDeviceId(), source, policyFlags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5197 | action, flags, metaState, buttonState, edgeFlags, |
| 5198 | pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5199 | getListener()->notifyMotion(&args); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5200 | } |
| 5201 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5202 | bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5203 | const PointerCoords* inCoords, const uint32_t* inIdToIndex, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5204 | PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex, |
| 5205 | BitSet32 idBits) const { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5206 | bool changed = false; |
| 5207 | while (!idBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5208 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5209 | uint32_t inIndex = inIdToIndex[id]; |
| 5210 | uint32_t outIndex = outIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5211 | |
| 5212 | const PointerProperties& curInProperties = inProperties[inIndex]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5213 | const PointerCoords& curInCoords = inCoords[inIndex]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5214 | PointerProperties& curOutProperties = outProperties[outIndex]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5215 | PointerCoords& curOutCoords = outCoords[outIndex]; |
| 5216 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5217 | if (curInProperties != curOutProperties) { |
| 5218 | curOutProperties.copyFrom(curInProperties); |
| 5219 | changed = true; |
| 5220 | } |
| 5221 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5222 | if (curInCoords != curOutCoords) { |
| 5223 | curOutCoords.copyFrom(curInCoords); |
| 5224 | changed = true; |
| 5225 | } |
| 5226 | } |
| 5227 | return changed; |
| 5228 | } |
| 5229 | |
| 5230 | void TouchInputMapper::fadePointer() { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5231 | if (mPointerController != NULL) { |
| 5232 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 5233 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5234 | } |
| 5235 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5236 | bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) { |
| 5237 | return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue |
| 5238 | && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5239 | } |
| 5240 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5241 | const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit( |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 5242 | int32_t x, int32_t y) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5243 | size_t numVirtualKeys = mVirtualKeys.size(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 5244 | for (size_t i = 0; i < numVirtualKeys; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5245 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5246 | |
| 5247 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5248 | ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, " |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5249 | "left=%d, top=%d, right=%d, bottom=%d", |
| 5250 | x, y, |
| 5251 | virtualKey.keyCode, virtualKey.scanCode, |
| 5252 | virtualKey.hitLeft, virtualKey.hitTop, |
| 5253 | virtualKey.hitRight, virtualKey.hitBottom); |
| 5254 | #endif |
| 5255 | |
| 5256 | if (virtualKey.isHit(x, y)) { |
| 5257 | return & virtualKey; |
| 5258 | } |
| 5259 | } |
| 5260 | |
| 5261 | return NULL; |
| 5262 | } |
| 5263 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5264 | void TouchInputMapper::assignPointerIds() { |
| 5265 | uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount; |
| 5266 | uint32_t lastPointerCount = mLastRawPointerData.pointerCount; |
| 5267 | |
| 5268 | mCurrentRawPointerData.clearIdBits(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5269 | |
| 5270 | if (currentPointerCount == 0) { |
| 5271 | // No pointers to assign. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5272 | return; |
| 5273 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5274 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5275 | if (lastPointerCount == 0) { |
| 5276 | // All pointers are new. |
| 5277 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
| 5278 | uint32_t id = i; |
| 5279 | mCurrentRawPointerData.pointers[i].id = id; |
| 5280 | mCurrentRawPointerData.idToIndex[id] = i; |
| 5281 | mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i)); |
| 5282 | } |
| 5283 | return; |
| 5284 | } |
| 5285 | |
| 5286 | if (currentPointerCount == 1 && lastPointerCount == 1 |
| 5287 | && mCurrentRawPointerData.pointers[0].toolType |
| 5288 | == mLastRawPointerData.pointers[0].toolType) { |
| 5289 | // Only one pointer and no change in count so it must have the same id as before. |
| 5290 | uint32_t id = mLastRawPointerData.pointers[0].id; |
| 5291 | mCurrentRawPointerData.pointers[0].id = id; |
| 5292 | mCurrentRawPointerData.idToIndex[id] = 0; |
| 5293 | mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0)); |
| 5294 | return; |
| 5295 | } |
| 5296 | |
| 5297 | // General case. |
| 5298 | // We build a heap of squared euclidean distances between current and last pointers |
| 5299 | // associated with the current and last pointer indices. Then, we find the best |
| 5300 | // match (by distance) for each current pointer. |
| 5301 | // The pointers must have the same tool type but it is possible for them to |
| 5302 | // transition from hovering to touching or vice-versa while retaining the same id. |
| 5303 | PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS]; |
| 5304 | |
| 5305 | uint32_t heapSize = 0; |
| 5306 | for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount; |
| 5307 | currentPointerIndex++) { |
| 5308 | for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount; |
| 5309 | lastPointerIndex++) { |
| 5310 | const RawPointerData::Pointer& currentPointer = |
| 5311 | mCurrentRawPointerData.pointers[currentPointerIndex]; |
| 5312 | const RawPointerData::Pointer& lastPointer = |
| 5313 | mLastRawPointerData.pointers[lastPointerIndex]; |
| 5314 | if (currentPointer.toolType == lastPointer.toolType) { |
| 5315 | int64_t deltaX = currentPointer.x - lastPointer.x; |
| 5316 | int64_t deltaY = currentPointer.y - lastPointer.y; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5317 | |
| 5318 | uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY); |
| 5319 | |
| 5320 | // Insert new element into the heap (sift up). |
| 5321 | heap[heapSize].currentPointerIndex = currentPointerIndex; |
| 5322 | heap[heapSize].lastPointerIndex = lastPointerIndex; |
| 5323 | heap[heapSize].distance = distance; |
| 5324 | heapSize += 1; |
| 5325 | } |
| 5326 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5327 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5328 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5329 | // Heapify |
| 5330 | for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) { |
| 5331 | startIndex -= 1; |
| 5332 | for (uint32_t parentIndex = startIndex; ;) { |
| 5333 | uint32_t childIndex = parentIndex * 2 + 1; |
| 5334 | if (childIndex >= heapSize) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5335 | break; |
| 5336 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5337 | |
| 5338 | if (childIndex + 1 < heapSize |
| 5339 | && heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 5340 | childIndex += 1; |
| 5341 | } |
| 5342 | |
| 5343 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 5344 | break; |
| 5345 | } |
| 5346 | |
| 5347 | swap(heap[parentIndex], heap[childIndex]); |
| 5348 | parentIndex = childIndex; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5349 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5350 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5351 | |
| 5352 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5353 | ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5354 | for (size_t i = 0; i < heapSize; i++) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5355 | ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5356 | i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, |
| 5357 | heap[i].distance); |
| 5358 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5359 | #endif |
| 5360 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5361 | // Pull matches out by increasing order of distance. |
| 5362 | // To avoid reassigning pointers that have already been matched, the loop keeps track |
| 5363 | // of which last and current pointers have been matched using the matchedXXXBits variables. |
| 5364 | // It also tracks the used pointer id bits. |
| 5365 | BitSet32 matchedLastBits(0); |
| 5366 | BitSet32 matchedCurrentBits(0); |
| 5367 | BitSet32 usedIdBits(0); |
| 5368 | bool first = true; |
| 5369 | for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) { |
| 5370 | while (heapSize > 0) { |
| 5371 | if (first) { |
| 5372 | // The first time through the loop, we just consume the root element of |
| 5373 | // the heap (the one with smallest distance). |
| 5374 | first = false; |
| 5375 | } else { |
| 5376 | // Previous iterations consumed the root element of the heap. |
| 5377 | // Pop root element off of the heap (sift down). |
| 5378 | heap[0] = heap[heapSize]; |
| 5379 | for (uint32_t parentIndex = 0; ;) { |
| 5380 | uint32_t childIndex = parentIndex * 2 + 1; |
| 5381 | if (childIndex >= heapSize) { |
| 5382 | break; |
| 5383 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5384 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5385 | if (childIndex + 1 < heapSize |
| 5386 | && heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 5387 | childIndex += 1; |
| 5388 | } |
| 5389 | |
| 5390 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 5391 | break; |
| 5392 | } |
| 5393 | |
| 5394 | swap(heap[parentIndex], heap[childIndex]); |
| 5395 | parentIndex = childIndex; |
| 5396 | } |
| 5397 | |
| 5398 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5399 | ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5400 | for (size_t i = 0; i < heapSize; i++) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5401 | ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5402 | i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, |
| 5403 | heap[i].distance); |
| 5404 | } |
| 5405 | #endif |
| 5406 | } |
| 5407 | |
| 5408 | heapSize -= 1; |
| 5409 | |
| 5410 | uint32_t currentPointerIndex = heap[0].currentPointerIndex; |
| 5411 | if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched |
| 5412 | |
| 5413 | uint32_t lastPointerIndex = heap[0].lastPointerIndex; |
| 5414 | if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched |
| 5415 | |
| 5416 | matchedCurrentBits.markBit(currentPointerIndex); |
| 5417 | matchedLastBits.markBit(lastPointerIndex); |
| 5418 | |
| 5419 | uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id; |
| 5420 | mCurrentRawPointerData.pointers[currentPointerIndex].id = id; |
| 5421 | mCurrentRawPointerData.idToIndex[id] = currentPointerIndex; |
| 5422 | mCurrentRawPointerData.markIdBit(id, |
| 5423 | mCurrentRawPointerData.isHovering(currentPointerIndex)); |
| 5424 | usedIdBits.markBit(id); |
| 5425 | |
| 5426 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5427 | ALOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5428 | lastPointerIndex, currentPointerIndex, id, heap[0].distance); |
| 5429 | #endif |
| 5430 | break; |
| 5431 | } |
| 5432 | } |
| 5433 | |
| 5434 | // Assign fresh ids to pointers that were not matched in the process. |
| 5435 | for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) { |
| 5436 | uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit(); |
| 5437 | uint32_t id = usedIdBits.markFirstUnmarkedBit(); |
| 5438 | |
| 5439 | mCurrentRawPointerData.pointers[currentPointerIndex].id = id; |
| 5440 | mCurrentRawPointerData.idToIndex[id] = currentPointerIndex; |
| 5441 | mCurrentRawPointerData.markIdBit(id, |
| 5442 | mCurrentRawPointerData.isHovering(currentPointerIndex)); |
| 5443 | |
| 5444 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5445 | ALOGD("assignPointerIds - assigned: cur=%d, id=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5446 | currentPointerIndex, id); |
| 5447 | #endif |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5448 | } |
| 5449 | } |
| 5450 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5451 | int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5452 | if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) { |
| 5453 | return AKEY_STATE_VIRTUAL; |
| 5454 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5455 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5456 | size_t numVirtualKeys = mVirtualKeys.size(); |
| 5457 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 5458 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
| 5459 | if (virtualKey.keyCode == keyCode) { |
| 5460 | return AKEY_STATE_UP; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5461 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5462 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5463 | |
| 5464 | return AKEY_STATE_UNKNOWN; |
| 5465 | } |
| 5466 | |
| 5467 | int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5468 | if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) { |
| 5469 | return AKEY_STATE_VIRTUAL; |
| 5470 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5471 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5472 | size_t numVirtualKeys = mVirtualKeys.size(); |
| 5473 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 5474 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
| 5475 | if (virtualKey.scanCode == scanCode) { |
| 5476 | return AKEY_STATE_UP; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5477 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5478 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5479 | |
| 5480 | return AKEY_STATE_UNKNOWN; |
| 5481 | } |
| 5482 | |
| 5483 | bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 5484 | const int32_t* keyCodes, uint8_t* outFlags) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5485 | size_t numVirtualKeys = mVirtualKeys.size(); |
| 5486 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 5487 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5488 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5489 | for (size_t i = 0; i < numCodes; i++) { |
| 5490 | if (virtualKey.keyCode == keyCodes[i]) { |
| 5491 | outFlags[i] = 1; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5492 | } |
| 5493 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5494 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5495 | |
| 5496 | return true; |
| 5497 | } |
| 5498 | |
| 5499 | |
| 5500 | // --- SingleTouchInputMapper --- |
| 5501 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 5502 | SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) : |
| 5503 | TouchInputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5504 | } |
| 5505 | |
| 5506 | SingleTouchInputMapper::~SingleTouchInputMapper() { |
| 5507 | } |
| 5508 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5509 | void SingleTouchInputMapper::reset(nsecs_t when) { |
| 5510 | mSingleTouchMotionAccumulator.reset(getDevice()); |
| 5511 | |
| 5512 | TouchInputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5513 | } |
| 5514 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5515 | void SingleTouchInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5516 | TouchInputMapper::process(rawEvent); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5517 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5518 | mSingleTouchMotionAccumulator.process(rawEvent); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5519 | } |
| 5520 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5521 | void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) { |
Jeff Brown | d87c6d5 | 2011-08-10 14:55:59 -0700 | [diff] [blame] | 5522 | if (mTouchButtonAccumulator.isToolActive()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5523 | mCurrentRawPointerData.pointerCount = 1; |
| 5524 | mCurrentRawPointerData.idToIndex[0] = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5525 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5526 | bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE |
| 5527 | && (mTouchButtonAccumulator.isHovering() |
| 5528 | || (mRawPointerAxes.pressure.valid |
| 5529 | && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0)); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5530 | mCurrentRawPointerData.markIdBit(0, isHovering); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5531 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5532 | RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0]; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5533 | outPointer.id = 0; |
| 5534 | outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX(); |
| 5535 | outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY(); |
| 5536 | outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure(); |
| 5537 | outPointer.touchMajor = 0; |
| 5538 | outPointer.touchMinor = 0; |
| 5539 | outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth(); |
| 5540 | outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth(); |
| 5541 | outPointer.orientation = 0; |
| 5542 | outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5543 | outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX(); |
| 5544 | outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY(); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5545 | outPointer.toolType = mTouchButtonAccumulator.getToolType(); |
| 5546 | if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 5547 | outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 5548 | } |
| 5549 | outPointer.isHovering = isHovering; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5550 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5551 | } |
| 5552 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5553 | void SingleTouchInputMapper::configureRawPointerAxes() { |
| 5554 | TouchInputMapper::configureRawPointerAxes(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5555 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5556 | getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x); |
| 5557 | getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y); |
| 5558 | getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure); |
| 5559 | getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor); |
| 5560 | getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5561 | getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX); |
| 5562 | getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5563 | } |
| 5564 | |
| 5565 | |
| 5566 | // --- MultiTouchInputMapper --- |
| 5567 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 5568 | MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) : |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5569 | TouchInputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5570 | } |
| 5571 | |
| 5572 | MultiTouchInputMapper::~MultiTouchInputMapper() { |
| 5573 | } |
| 5574 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5575 | void MultiTouchInputMapper::reset(nsecs_t when) { |
| 5576 | mMultiTouchMotionAccumulator.reset(getDevice()); |
| 5577 | |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5578 | mPointerIdBits.clear(); |
Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 5579 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5580 | TouchInputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5581 | } |
| 5582 | |
| 5583 | void MultiTouchInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5584 | TouchInputMapper::process(rawEvent); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5585 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5586 | mMultiTouchMotionAccumulator.process(rawEvent); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5587 | } |
| 5588 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5589 | void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5590 | size_t inCount = mMultiTouchMotionAccumulator.getSlotCount(); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5591 | size_t outCount = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5592 | BitSet32 newPointerIdBits; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5593 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5594 | for (size_t inIndex = 0; inIndex < inCount; inIndex++) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5595 | const MultiTouchMotionAccumulator::Slot* inSlot = |
| 5596 | mMultiTouchMotionAccumulator.getSlot(inIndex); |
| 5597 | if (!inSlot->isInUse()) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5598 | continue; |
| 5599 | } |
| 5600 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5601 | if (outCount >= MAX_POINTERS) { |
| 5602 | #if DEBUG_POINTERS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5603 | ALOGD("MultiTouch device %s emitted more than maximum of %d pointers; " |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5604 | "ignoring the rest.", |
| 5605 | getDeviceName().string(), MAX_POINTERS); |
| 5606 | #endif |
| 5607 | break; // too many fingers! |
| 5608 | } |
| 5609 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5610 | RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount]; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5611 | outPointer.x = inSlot->getX(); |
| 5612 | outPointer.y = inSlot->getY(); |
| 5613 | outPointer.pressure = inSlot->getPressure(); |
| 5614 | outPointer.touchMajor = inSlot->getTouchMajor(); |
| 5615 | outPointer.touchMinor = inSlot->getTouchMinor(); |
| 5616 | outPointer.toolMajor = inSlot->getToolMajor(); |
| 5617 | outPointer.toolMinor = inSlot->getToolMinor(); |
| 5618 | outPointer.orientation = inSlot->getOrientation(); |
| 5619 | outPointer.distance = inSlot->getDistance(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5620 | outPointer.tiltX = 0; |
| 5621 | outPointer.tiltY = 0; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5622 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5623 | outPointer.toolType = inSlot->getToolType(); |
| 5624 | if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 5625 | outPointer.toolType = mTouchButtonAccumulator.getToolType(); |
| 5626 | if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 5627 | outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 5628 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5629 | } |
| 5630 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5631 | bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE |
| 5632 | && (mTouchButtonAccumulator.isHovering() |
| 5633 | || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0)); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5634 | outPointer.isHovering = isHovering; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5635 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5636 | // Assign pointer id using tracking id if available. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5637 | if (*outHavePointerIds) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5638 | int32_t trackingId = inSlot->getTrackingId(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5639 | int32_t id = -1; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5640 | if (trackingId >= 0) { |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5641 | for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5642 | uint32_t n = idBits.clearFirstMarkedBit(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5643 | if (mPointerTrackingIdMap[n] == trackingId) { |
| 5644 | id = n; |
| 5645 | } |
| 5646 | } |
| 5647 | |
| 5648 | if (id < 0 && !mPointerIdBits.isFull()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5649 | id = mPointerIdBits.markFirstUnmarkedBit(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5650 | mPointerTrackingIdMap[id] = trackingId; |
| 5651 | } |
| 5652 | } |
| 5653 | if (id < 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5654 | *outHavePointerIds = false; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5655 | mCurrentRawPointerData.clearIdBits(); |
| 5656 | newPointerIdBits.clear(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5657 | } else { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5658 | outPointer.id = id; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5659 | mCurrentRawPointerData.idToIndex[id] = outCount; |
| 5660 | mCurrentRawPointerData.markIdBit(id, isHovering); |
| 5661 | newPointerIdBits.markBit(id); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5662 | } |
| 5663 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5664 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5665 | outCount += 1; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5666 | } |
| 5667 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5668 | mCurrentRawPointerData.pointerCount = outCount; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5669 | mPointerIdBits = newPointerIdBits; |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5670 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5671 | mMultiTouchMotionAccumulator.finishSync(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5672 | } |
| 5673 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5674 | void MultiTouchInputMapper::configureRawPointerAxes() { |
| 5675 | TouchInputMapper::configureRawPointerAxes(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5676 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5677 | getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x); |
| 5678 | getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y); |
| 5679 | getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor); |
| 5680 | getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor); |
| 5681 | getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor); |
| 5682 | getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor); |
| 5683 | getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation); |
| 5684 | getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure); |
| 5685 | getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance); |
| 5686 | getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId); |
| 5687 | getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5688 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5689 | if (mRawPointerAxes.trackingId.valid |
| 5690 | && mRawPointerAxes.slot.valid |
| 5691 | && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) { |
| 5692 | size_t slotCount = mRawPointerAxes.slot.maxValue + 1; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5693 | if (slotCount > MAX_SLOTS) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5694 | ALOGW("MultiTouch Device %s reported %d slots but the framework " |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5695 | "only supports a maximum of %d slots at this time.", |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5696 | getDeviceName().string(), slotCount, MAX_SLOTS); |
| 5697 | slotCount = MAX_SLOTS; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5698 | } |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5699 | mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5700 | } else { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5701 | mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5702 | } |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 5703 | } |
| 5704 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5705 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5706 | // --- JoystickInputMapper --- |
| 5707 | |
| 5708 | JoystickInputMapper::JoystickInputMapper(InputDevice* device) : |
| 5709 | InputMapper(device) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5710 | } |
| 5711 | |
| 5712 | JoystickInputMapper::~JoystickInputMapper() { |
| 5713 | } |
| 5714 | |
| 5715 | uint32_t JoystickInputMapper::getSources() { |
| 5716 | return AINPUT_SOURCE_JOYSTICK; |
| 5717 | } |
| 5718 | |
| 5719 | void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 5720 | InputMapper::populateDeviceInfo(info); |
| 5721 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5722 | for (size_t i = 0; i < mAxes.size(); i++) { |
| 5723 | const Axis& axis = mAxes.valueAt(i); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 5724 | info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK, |
| 5725 | axis.min, axis.max, axis.flat, axis.fuzz); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5726 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 5727 | info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK, |
| 5728 | axis.min, axis.max, axis.flat, axis.fuzz); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5729 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5730 | } |
| 5731 | } |
| 5732 | |
| 5733 | void JoystickInputMapper::dump(String8& dump) { |
| 5734 | dump.append(INDENT2 "Joystick Input Mapper:\n"); |
| 5735 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5736 | dump.append(INDENT3 "Axes:\n"); |
| 5737 | size_t numAxes = mAxes.size(); |
| 5738 | for (size_t i = 0; i < numAxes; i++) { |
| 5739 | const Axis& axis = mAxes.valueAt(i); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5740 | const char* label = getAxisLabel(axis.axisInfo.axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5741 | if (label) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5742 | dump.appendFormat(INDENT4 "%s", label); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5743 | } else { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5744 | dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5745 | } |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5746 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 5747 | label = getAxisLabel(axis.axisInfo.highAxis); |
| 5748 | if (label) { |
| 5749 | dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue); |
| 5750 | } else { |
| 5751 | dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis, |
| 5752 | axis.axisInfo.splitValue); |
| 5753 | } |
| 5754 | } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) { |
| 5755 | dump.append(" (invert)"); |
| 5756 | } |
| 5757 | |
| 5758 | dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n", |
| 5759 | axis.min, axis.max, axis.flat, axis.fuzz); |
| 5760 | dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, " |
| 5761 | "highScale=%0.5f, highOffset=%0.5f\n", |
| 5762 | axis.scale, axis.offset, axis.highScale, axis.highOffset); |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 5763 | dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, " |
| 5764 | "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n", |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5765 | mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue, |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 5766 | axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5767 | } |
| 5768 | } |
| 5769 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5770 | void JoystickInputMapper::configure(nsecs_t when, |
| 5771 | const InputReaderConfiguration* config, uint32_t changes) { |
| 5772 | InputMapper::configure(when, config, changes); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5773 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5774 | if (!changes) { // first time only |
| 5775 | // Collect all axes. |
| 5776 | for (int32_t abs = 0; abs <= ABS_MAX; abs++) { |
Jeff Brown | 9ee285af | 2011-08-31 12:56:34 -0700 | [diff] [blame] | 5777 | if (!(getAbsAxisUsage(abs, getDevice()->getClasses()) |
| 5778 | & INPUT_DEVICE_CLASS_JOYSTICK)) { |
| 5779 | continue; // axis must be claimed by a different device |
| 5780 | } |
| 5781 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5782 | RawAbsoluteAxisInfo rawAxisInfo; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5783 | getAbsoluteAxisInfo(abs, &rawAxisInfo); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5784 | if (rawAxisInfo.valid) { |
| 5785 | // Map axis. |
| 5786 | AxisInfo axisInfo; |
| 5787 | bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo); |
| 5788 | if (!explicitlyMapped) { |
| 5789 | // Axis is not explicitly mapped, will choose a generic axis later. |
| 5790 | axisInfo.mode = AxisInfo::MODE_NORMAL; |
| 5791 | axisInfo.axis = -1; |
| 5792 | } |
| 5793 | |
| 5794 | // Apply flat override. |
| 5795 | int32_t rawFlat = axisInfo.flatOverride < 0 |
| 5796 | ? rawAxisInfo.flat : axisInfo.flatOverride; |
| 5797 | |
| 5798 | // Calculate scaling factors and limits. |
| 5799 | Axis axis; |
| 5800 | if (axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 5801 | float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue); |
| 5802 | float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue); |
| 5803 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 5804 | scale, 0.0f, highScale, 0.0f, |
| 5805 | 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
| 5806 | } else if (isCenteredAxis(axisInfo.axis)) { |
| 5807 | float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); |
| 5808 | float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale; |
| 5809 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 5810 | scale, offset, scale, offset, |
| 5811 | -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
| 5812 | } else { |
| 5813 | float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); |
| 5814 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 5815 | scale, 0.0f, scale, 0.0f, |
| 5816 | 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
| 5817 | } |
| 5818 | |
| 5819 | // To eliminate noise while the joystick is at rest, filter out small variations |
| 5820 | // in axis values up front. |
| 5821 | axis.filter = axis.flat * 0.25f; |
| 5822 | |
| 5823 | mAxes.add(abs, axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5824 | } |
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 | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5827 | // If there are too many axes, start dropping them. |
| 5828 | // Prefer to keep explicitly mapped axes. |
| 5829 | if (mAxes.size() > PointerCoords::MAX_AXES) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 5830 | 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] | 5831 | getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES); |
| 5832 | pruneAxes(true); |
| 5833 | pruneAxes(false); |
| 5834 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5835 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5836 | // Assign generic axis ids to remaining axes. |
| 5837 | int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1; |
| 5838 | size_t numAxes = mAxes.size(); |
| 5839 | for (size_t i = 0; i < numAxes; i++) { |
| 5840 | Axis& axis = mAxes.editValueAt(i); |
| 5841 | if (axis.axisInfo.axis < 0) { |
| 5842 | while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16 |
| 5843 | && haveAxis(nextGenericAxisId)) { |
| 5844 | nextGenericAxisId += 1; |
| 5845 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5846 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5847 | if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) { |
| 5848 | axis.axisInfo.axis = nextGenericAxisId; |
| 5849 | nextGenericAxisId += 1; |
| 5850 | } else { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 5851 | 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] | 5852 | "have already been assigned to other axes.", |
| 5853 | getDeviceName().string(), mAxes.keyAt(i)); |
| 5854 | mAxes.removeItemsAt(i--); |
| 5855 | numAxes -= 1; |
| 5856 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5857 | } |
| 5858 | } |
| 5859 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5860 | } |
| 5861 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5862 | bool JoystickInputMapper::haveAxis(int32_t axisId) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5863 | size_t numAxes = mAxes.size(); |
| 5864 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5865 | const Axis& axis = mAxes.valueAt(i); |
| 5866 | if (axis.axisInfo.axis == axisId |
| 5867 | || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT |
| 5868 | && axis.axisInfo.highAxis == axisId)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5869 | return true; |
| 5870 | } |
| 5871 | } |
| 5872 | return false; |
| 5873 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5874 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5875 | void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) { |
| 5876 | size_t i = mAxes.size(); |
| 5877 | while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) { |
| 5878 | if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) { |
| 5879 | continue; |
| 5880 | } |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 5881 | ALOGI("Discarding joystick '%s' axis %d because there are too many axes.", |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5882 | getDeviceName().string(), mAxes.keyAt(i)); |
| 5883 | mAxes.removeItemsAt(i); |
| 5884 | } |
| 5885 | } |
| 5886 | |
| 5887 | bool JoystickInputMapper::isCenteredAxis(int32_t axis) { |
| 5888 | switch (axis) { |
| 5889 | case AMOTION_EVENT_AXIS_X: |
| 5890 | case AMOTION_EVENT_AXIS_Y: |
| 5891 | case AMOTION_EVENT_AXIS_Z: |
| 5892 | case AMOTION_EVENT_AXIS_RX: |
| 5893 | case AMOTION_EVENT_AXIS_RY: |
| 5894 | case AMOTION_EVENT_AXIS_RZ: |
| 5895 | case AMOTION_EVENT_AXIS_HAT_X: |
| 5896 | case AMOTION_EVENT_AXIS_HAT_Y: |
| 5897 | case AMOTION_EVENT_AXIS_ORIENTATION: |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5898 | case AMOTION_EVENT_AXIS_RUDDER: |
| 5899 | case AMOTION_EVENT_AXIS_WHEEL: |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5900 | return true; |
| 5901 | default: |
| 5902 | return false; |
| 5903 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5904 | } |
| 5905 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5906 | void JoystickInputMapper::reset(nsecs_t when) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5907 | // Recenter all axes. |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5908 | size_t numAxes = mAxes.size(); |
| 5909 | for (size_t i = 0; i < numAxes; i++) { |
| 5910 | Axis& axis = mAxes.editValueAt(i); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5911 | axis.resetValue(); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5912 | } |
| 5913 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5914 | InputMapper::reset(when); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5915 | } |
| 5916 | |
| 5917 | void JoystickInputMapper::process(const RawEvent* rawEvent) { |
| 5918 | switch (rawEvent->type) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5919 | case EV_ABS: { |
| 5920 | ssize_t index = mAxes.indexOfKey(rawEvent->scanCode); |
| 5921 | if (index >= 0) { |
| 5922 | Axis& axis = mAxes.editValueAt(index); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5923 | float newValue, highNewValue; |
| 5924 | switch (axis.axisInfo.mode) { |
| 5925 | case AxisInfo::MODE_INVERT: |
| 5926 | newValue = (axis.rawAxisInfo.maxValue - rawEvent->value) |
| 5927 | * axis.scale + axis.offset; |
| 5928 | highNewValue = 0.0f; |
| 5929 | break; |
| 5930 | case AxisInfo::MODE_SPLIT: |
| 5931 | if (rawEvent->value < axis.axisInfo.splitValue) { |
| 5932 | newValue = (axis.axisInfo.splitValue - rawEvent->value) |
| 5933 | * axis.scale + axis.offset; |
| 5934 | highNewValue = 0.0f; |
| 5935 | } else if (rawEvent->value > axis.axisInfo.splitValue) { |
| 5936 | newValue = 0.0f; |
| 5937 | highNewValue = (rawEvent->value - axis.axisInfo.splitValue) |
| 5938 | * axis.highScale + axis.highOffset; |
| 5939 | } else { |
| 5940 | newValue = 0.0f; |
| 5941 | highNewValue = 0.0f; |
| 5942 | } |
| 5943 | break; |
| 5944 | default: |
| 5945 | newValue = rawEvent->value * axis.scale + axis.offset; |
| 5946 | highNewValue = 0.0f; |
| 5947 | break; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5948 | } |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5949 | axis.newValue = newValue; |
| 5950 | axis.highNewValue = highNewValue; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5951 | } |
| 5952 | break; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5953 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5954 | |
| 5955 | case EV_SYN: |
| 5956 | switch (rawEvent->scanCode) { |
| 5957 | case SYN_REPORT: |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5958 | sync(rawEvent->when, false /*force*/); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5959 | break; |
| 5960 | } |
| 5961 | break; |
| 5962 | } |
| 5963 | } |
| 5964 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5965 | void JoystickInputMapper::sync(nsecs_t when, bool force) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5966 | if (!filterAxes(force)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5967 | return; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5968 | } |
| 5969 | |
| 5970 | int32_t metaState = mContext->getGlobalMetaState(); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5971 | int32_t buttonState = 0; |
| 5972 | |
| 5973 | PointerProperties pointerProperties; |
| 5974 | pointerProperties.clear(); |
| 5975 | pointerProperties.id = 0; |
| 5976 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5977 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5978 | PointerCoords pointerCoords; |
| 5979 | pointerCoords.clear(); |
| 5980 | |
| 5981 | size_t numAxes = mAxes.size(); |
| 5982 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5983 | const Axis& axis = mAxes.valueAt(i); |
| 5984 | pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue); |
| 5985 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 5986 | pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue); |
| 5987 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5988 | } |
| 5989 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 5990 | // Moving a joystick axis should not wake the devide because joysticks can |
| 5991 | // be fairly noisy even when not in use. On the other hand, pushing a gamepad |
| 5992 | // button will likely wake the device. |
| 5993 | // TODO: Use the input device configuration to control this behavior more finely. |
| 5994 | uint32_t policyFlags = 0; |
| 5995 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5996 | NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5997 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 5998 | 1, &pointerProperties, &pointerCoords, 0, 0, 0); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5999 | getListener()->notifyMotion(&args); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6000 | } |
| 6001 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6002 | bool JoystickInputMapper::filterAxes(bool force) { |
| 6003 | bool atLeastOneSignificantChange = force; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6004 | size_t numAxes = mAxes.size(); |
| 6005 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6006 | Axis& axis = mAxes.editValueAt(i); |
| 6007 | if (force || hasValueChangedSignificantly(axis.filter, |
| 6008 | axis.newValue, axis.currentValue, axis.min, axis.max)) { |
| 6009 | axis.currentValue = axis.newValue; |
| 6010 | atLeastOneSignificantChange = true; |
| 6011 | } |
| 6012 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 6013 | if (force || hasValueChangedSignificantly(axis.filter, |
| 6014 | axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) { |
| 6015 | axis.highCurrentValue = axis.highNewValue; |
| 6016 | atLeastOneSignificantChange = true; |
| 6017 | } |
| 6018 | } |
| 6019 | } |
| 6020 | return atLeastOneSignificantChange; |
| 6021 | } |
| 6022 | |
| 6023 | bool JoystickInputMapper::hasValueChangedSignificantly( |
| 6024 | float filter, float newValue, float currentValue, float min, float max) { |
| 6025 | if (newValue != currentValue) { |
| 6026 | // Filter out small changes in value unless the value is converging on the axis |
| 6027 | // bounds or center point. This is intended to reduce the amount of information |
| 6028 | // sent to applications by particularly noisy joysticks (such as PS3). |
| 6029 | if (fabs(newValue - currentValue) > filter |
| 6030 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min) |
| 6031 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max) |
| 6032 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) { |
| 6033 | return true; |
| 6034 | } |
| 6035 | } |
| 6036 | return false; |
| 6037 | } |
| 6038 | |
| 6039 | bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange( |
| 6040 | float filter, float newValue, float currentValue, float thresholdValue) { |
| 6041 | float newDistance = fabs(newValue - thresholdValue); |
| 6042 | if (newDistance < filter) { |
| 6043 | float oldDistance = fabs(currentValue - thresholdValue); |
| 6044 | if (newDistance < oldDistance) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6045 | return true; |
| 6046 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6047 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6048 | return false; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6049 | } |
| 6050 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 6051 | } // namespace android |