Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 17 | #define LOG_TAG "InputReader" |
| 18 | |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
| 21 | // Log debug messages for each raw event received from the EventHub. |
| 22 | #define DEBUG_RAW_EVENTS 0 |
| 23 | |
| 24 | // Log debug messages about touch screen filtering hacks. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 25 | #define DEBUG_HACKS 0 |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 26 | |
| 27 | // Log debug messages about virtual key processing. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 28 | #define DEBUG_VIRTUAL_KEYS 0 |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 29 | |
| 30 | // Log debug messages about pointers. |
Jeff Brown | 9f2106f | 2011-05-24 14:40:35 -0700 | [diff] [blame] | 31 | #define DEBUG_POINTERS 0 |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 32 | |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 33 | // Log debug messages about pointer assignment calculations. |
| 34 | #define DEBUG_POINTER_ASSIGNMENT 0 |
| 35 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 36 | // Log debug messages about gesture detection. |
| 37 | #define DEBUG_GESTURES 0 |
| 38 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 39 | #include "InputReader.h" |
| 40 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 41 | #include <cutils/log.h> |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 42 | #include <ui/Keyboard.h> |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 43 | #include <ui/VirtualKeyMap.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 44 | |
| 45 | #include <stddef.h> |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 46 | #include <stdlib.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 47 | #include <unistd.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 48 | #include <errno.h> |
| 49 | #include <limits.h> |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 50 | #include <math.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 51 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 52 | #define INDENT " " |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 53 | #define INDENT2 " " |
| 54 | #define INDENT3 " " |
| 55 | #define INDENT4 " " |
Jeff Brown | aba321a | 2011-06-28 20:34:40 -0700 | [diff] [blame] | 56 | #define INDENT5 " " |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 57 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 58 | namespace android { |
| 59 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 60 | // --- Constants --- |
| 61 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 62 | // Maximum number of slots supported when using the slot-based Multitouch Protocol B. |
| 63 | static const size_t MAX_SLOTS = 32; |
| 64 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 65 | // --- Static Functions --- |
| 66 | |
| 67 | template<typename T> |
| 68 | inline static T abs(const T& value) { |
| 69 | return value < 0 ? - value : value; |
| 70 | } |
| 71 | |
| 72 | template<typename T> |
| 73 | inline static T min(const T& a, const T& b) { |
| 74 | return a < b ? a : b; |
| 75 | } |
| 76 | |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 77 | template<typename T> |
| 78 | inline static void swap(T& a, T& b) { |
| 79 | T temp = a; |
| 80 | a = b; |
| 81 | b = temp; |
| 82 | } |
| 83 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 84 | inline static float avg(float x, float y) { |
| 85 | return (x + y) / 2; |
| 86 | } |
| 87 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 88 | inline static float distance(float x1, float y1, float x2, float y2) { |
| 89 | return hypotf(x1 - x2, y1 - y2); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 92 | inline static int32_t signExtendNybble(int32_t value) { |
| 93 | return value >= 8 ? value - 16 : value; |
| 94 | } |
| 95 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 96 | static inline const char* toString(bool value) { |
| 97 | return value ? "true" : "false"; |
| 98 | } |
| 99 | |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 100 | static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation, |
| 101 | const int32_t map[][4], size_t mapSize) { |
| 102 | if (orientation != DISPLAY_ORIENTATION_0) { |
| 103 | for (size_t i = 0; i < mapSize; i++) { |
| 104 | if (value == map[i][0]) { |
| 105 | return map[i][orientation]; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | return value; |
| 110 | } |
| 111 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 112 | static const int32_t keyCodeRotationMap[][4] = { |
| 113 | // key codes enumerated counter-clockwise with the original (unrotated) key first |
| 114 | // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 115 | { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT }, |
| 116 | { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN }, |
| 117 | { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT }, |
| 118 | { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP }, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 119 | }; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 120 | static const size_t keyCodeRotationMapSize = |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 121 | sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]); |
| 122 | |
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); |
| 281 | |
| 282 | if (count) { |
| 283 | processEventsLocked(mEventBuffer, count); |
| 284 | } |
| 285 | if (!count || timeoutMillis == 0) { |
| 286 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 287 | #if DEBUG_RAW_EVENTS |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 288 | LOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 289 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 290 | mNextTimeout = LLONG_MAX; |
| 291 | timeoutExpiredLocked(now); |
| 292 | } |
| 293 | } // release lock |
| 294 | |
| 295 | // Flush queued events out to the listener. |
| 296 | // This must happen outside of the lock because the listener could potentially call |
| 297 | // back into the InputReader's methods, such as getScanCodeState, or become blocked |
| 298 | // on another thread similarly waiting to acquire the InputReader lock thereby |
| 299 | // resulting in a deadlock. This situation is actually quite plausible because the |
| 300 | // listener is actually the input dispatcher, which calls into the window manager, |
| 301 | // which occasionally calls into the input reader. |
| 302 | mQueuedListener->flush(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 305 | void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) { |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 306 | for (const RawEvent* rawEvent = rawEvents; count;) { |
| 307 | int32_t type = rawEvent->type; |
| 308 | size_t batchSize = 1; |
| 309 | if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) { |
| 310 | int32_t deviceId = rawEvent->deviceId; |
| 311 | while (batchSize < count) { |
| 312 | if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT |
| 313 | || rawEvent[batchSize].deviceId != deviceId) { |
| 314 | break; |
| 315 | } |
| 316 | batchSize += 1; |
| 317 | } |
| 318 | #if DEBUG_RAW_EVENTS |
| 319 | LOGD("BatchSize: %d Count: %d", batchSize, count); |
| 320 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 321 | processEventsForDeviceLocked(deviceId, rawEvent, batchSize); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 322 | } else { |
| 323 | switch (rawEvent->type) { |
| 324 | case EventHubInterface::DEVICE_ADDED: |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 325 | addDeviceLocked(rawEvent->when, rawEvent->deviceId); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 326 | break; |
| 327 | case EventHubInterface::DEVICE_REMOVED: |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 328 | removeDeviceLocked(rawEvent->when, rawEvent->deviceId); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 329 | break; |
| 330 | case EventHubInterface::FINISHED_DEVICE_SCAN: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 331 | handleConfigurationChangedLocked(rawEvent->when); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 332 | break; |
| 333 | default: |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 334 | LOG_ASSERT(false); // can't happen |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 335 | break; |
| 336 | } |
| 337 | } |
| 338 | count -= batchSize; |
| 339 | rawEvent += batchSize; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 343 | void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 344 | String8 name = mEventHub->getDeviceName(deviceId); |
| 345 | uint32_t classes = mEventHub->getDeviceClasses(deviceId); |
| 346 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 347 | InputDevice* device = createDeviceLocked(deviceId, name, classes); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 348 | device->configure(when, &mConfig, 0); |
| 349 | device->reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 350 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 351 | if (device->isIgnored()) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 352 | LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 353 | } else { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 354 | LOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(), |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 355 | device->getSources()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 358 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 359 | if (deviceIndex < 0) { |
| 360 | mDevices.add(deviceId, device); |
| 361 | } else { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 362 | LOGW("Ignoring spurious device added event for deviceId %d.", deviceId); |
| 363 | delete device; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 364 | return; |
| 365 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 368 | void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 369 | InputDevice* device = NULL; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 370 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 371 | if (deviceIndex >= 0) { |
| 372 | device = mDevices.valueAt(deviceIndex); |
| 373 | mDevices.removeItemsAt(deviceIndex, 1); |
| 374 | } else { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 375 | LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 376 | return; |
| 377 | } |
| 378 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 379 | if (device->isIgnored()) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 380 | LOGI("Device removed: id=%d, name='%s' (ignored non-input device)", |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 381 | device->getId(), device->getName().string()); |
| 382 | } else { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 383 | LOGI("Device removed: id=%d, name='%s', sources=0x%08x", |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 384 | device->getId(), device->getName().string(), device->getSources()); |
| 385 | } |
| 386 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 387 | device->reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 388 | delete device; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 391 | InputDevice* InputReader::createDeviceLocked(int32_t deviceId, |
| 392 | const String8& name, uint32_t classes) { |
| 393 | InputDevice* device = new InputDevice(&mContext, deviceId, name); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 394 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 395 | // External devices. |
| 396 | if (classes & INPUT_DEVICE_CLASS_EXTERNAL) { |
| 397 | device->setExternal(true); |
| 398 | } |
| 399 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 400 | // Switch-like devices. |
| 401 | if (classes & INPUT_DEVICE_CLASS_SWITCH) { |
| 402 | device->addMapper(new SwitchInputMapper(device)); |
| 403 | } |
| 404 | |
| 405 | // Keyboard-like devices. |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 406 | uint32_t keyboardSource = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 407 | int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC; |
| 408 | if (classes & INPUT_DEVICE_CLASS_KEYBOARD) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 409 | keyboardSource |= AINPUT_SOURCE_KEYBOARD; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 410 | } |
| 411 | if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) { |
| 412 | keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC; |
| 413 | } |
| 414 | if (classes & INPUT_DEVICE_CLASS_DPAD) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 415 | keyboardSource |= AINPUT_SOURCE_DPAD; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 416 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 417 | if (classes & INPUT_DEVICE_CLASS_GAMEPAD) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 418 | keyboardSource |= AINPUT_SOURCE_GAMEPAD; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 419 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 420 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 421 | if (keyboardSource != 0) { |
| 422 | device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 425 | // Cursor-like devices. |
| 426 | if (classes & INPUT_DEVICE_CLASS_CURSOR) { |
| 427 | device->addMapper(new CursorInputMapper(device)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 430 | // Touchscreens and touchpad devices. |
| 431 | if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 432 | device->addMapper(new MultiTouchInputMapper(device)); |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 433 | } else if (classes & INPUT_DEVICE_CLASS_TOUCH) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 434 | device->addMapper(new SingleTouchInputMapper(device)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 437 | // Joystick-like devices. |
| 438 | if (classes & INPUT_DEVICE_CLASS_JOYSTICK) { |
| 439 | device->addMapper(new JoystickInputMapper(device)); |
| 440 | } |
| 441 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 442 | return device; |
| 443 | } |
| 444 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 445 | void InputReader::processEventsForDeviceLocked(int32_t deviceId, |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 446 | const RawEvent* rawEvents, size_t count) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 447 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 448 | if (deviceIndex < 0) { |
| 449 | LOGW("Discarding event for unknown deviceId %d.", deviceId); |
| 450 | return; |
| 451 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 452 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 453 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 454 | if (device->isIgnored()) { |
| 455 | //LOGD("Discarding event for ignored deviceId %d.", deviceId); |
| 456 | return; |
| 457 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 458 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 459 | device->process(rawEvents, count); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 462 | void InputReader::timeoutExpiredLocked(nsecs_t when) { |
| 463 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 464 | InputDevice* device = mDevices.valueAt(i); |
| 465 | if (!device->isIgnored()) { |
| 466 | device->timeoutExpired(when); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 467 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 468 | } |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 471 | void InputReader::handleConfigurationChangedLocked(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 472 | // 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] | 473 | updateGlobalMetaStateLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 474 | |
| 475 | // Update input configuration. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 476 | updateInputConfigurationLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 477 | |
| 478 | // Enqueue configuration changed. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 479 | NotifyConfigurationChangedArgs args(when); |
| 480 | mQueuedListener->notifyConfigurationChanged(&args); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 483 | void InputReader::refreshConfigurationLocked(uint32_t changes) { |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 484 | mPolicy->getReaderConfiguration(&mConfig); |
| 485 | mEventHub->setExcludedDevices(mConfig.excludedDeviceNames); |
| 486 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 487 | if (changes) { |
| 488 | LOGI("Reconfiguring input devices. changes=0x%08x", changes); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 489 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 490 | |
| 491 | if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) { |
| 492 | mEventHub->requestReopenDevices(); |
| 493 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 494 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 495 | InputDevice* device = mDevices.valueAt(i); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 496 | device->configure(now, &mConfig, changes); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 497 | } |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 498 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 502 | void InputReader::updateGlobalMetaStateLocked() { |
| 503 | mGlobalMetaState = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 504 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 505 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 506 | InputDevice* device = mDevices.valueAt(i); |
| 507 | mGlobalMetaState |= device->getMetaState(); |
| 508 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 511 | int32_t InputReader::getGlobalMetaStateLocked() { |
| 512 | return mGlobalMetaState; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 515 | void InputReader::updateInputConfigurationLocked() { |
| 516 | int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH; |
| 517 | int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS; |
| 518 | int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV; |
| 519 | InputDeviceInfo deviceInfo; |
| 520 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 521 | InputDevice* device = mDevices.valueAt(i); |
| 522 | device->getDeviceInfo(& deviceInfo); |
| 523 | uint32_t sources = deviceInfo.getSources(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 524 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 525 | if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) { |
| 526 | touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER; |
| 527 | } |
| 528 | if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) { |
| 529 | navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL; |
| 530 | } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) { |
| 531 | navigationConfig = InputConfiguration::NAVIGATION_DPAD; |
| 532 | } |
| 533 | if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) { |
| 534 | keyboardConfig = InputConfiguration::KEYBOARD_QWERTY; |
| 535 | } |
| 536 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 537 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 538 | mInputConfiguration.touchScreen = touchScreenConfig; |
| 539 | mInputConfiguration.keyboard = keyboardConfig; |
| 540 | mInputConfiguration.navigation = navigationConfig; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 543 | void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) { |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 544 | mDisableVirtualKeysTimeout = time; |
| 545 | } |
| 546 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 547 | bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 548 | InputDevice* device, int32_t keyCode, int32_t scanCode) { |
| 549 | if (now < mDisableVirtualKeysTimeout) { |
| 550 | LOGI("Dropping virtual key from device %s because virtual keys are " |
| 551 | "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d", |
| 552 | device->getName().string(), |
| 553 | (mDisableVirtualKeysTimeout - now) * 0.000001, |
| 554 | keyCode, scanCode); |
| 555 | return true; |
| 556 | } else { |
| 557 | return false; |
| 558 | } |
| 559 | } |
| 560 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 561 | void InputReader::fadePointerLocked() { |
| 562 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 563 | InputDevice* device = mDevices.valueAt(i); |
| 564 | device->fadePointer(); |
| 565 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 566 | } |
| 567 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 568 | void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) { |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 569 | if (when < mNextTimeout) { |
| 570 | mNextTimeout = when; |
| 571 | } |
| 572 | } |
| 573 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 574 | void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 575 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 576 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 577 | *outConfiguration = mInputConfiguration; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 581 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 582 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 583 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 584 | if (deviceIndex < 0) { |
| 585 | return NAME_NOT_FOUND; |
| 586 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 587 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 588 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 589 | if (device->isIgnored()) { |
| 590 | return NAME_NOT_FOUND; |
| 591 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 592 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 593 | device->getDeviceInfo(outDeviceInfo); |
| 594 | return OK; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 598 | AutoMutex _l(mLock); |
| 599 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 600 | outDeviceIds.clear(); |
| 601 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 602 | size_t numDevices = mDevices.size(); |
| 603 | for (size_t i = 0; i < numDevices; i++) { |
| 604 | InputDevice* device = mDevices.valueAt(i); |
| 605 | if (!device->isIgnored()) { |
| 606 | outDeviceIds.add(device->getId()); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 607 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 608 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, |
| 612 | int32_t keyCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 613 | AutoMutex _l(mLock); |
| 614 | |
| 615 | return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, |
| 619 | int32_t scanCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 620 | AutoMutex _l(mLock); |
| 621 | |
| 622 | return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | 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] | 626 | AutoMutex _l(mLock); |
| 627 | |
| 628 | return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 631 | 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] | 632 | GetStateFunc getStateFunc) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 633 | int32_t result = AKEY_STATE_UNKNOWN; |
| 634 | if (deviceId >= 0) { |
| 635 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 636 | if (deviceIndex >= 0) { |
| 637 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 638 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 639 | result = (device->*getStateFunc)(sourceMask, code); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 640 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 641 | } |
| 642 | } else { |
| 643 | size_t numDevices = mDevices.size(); |
| 644 | for (size_t i = 0; i < numDevices; i++) { |
| 645 | InputDevice* device = mDevices.valueAt(i); |
| 646 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 647 | result = (device->*getStateFunc)(sourceMask, code); |
| 648 | if (result >= AKEY_STATE_DOWN) { |
| 649 | return result; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 653 | } |
| 654 | return result; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, |
| 658 | size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 659 | AutoMutex _l(mLock); |
| 660 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 661 | memset(outFlags, 0, numCodes); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 662 | return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 665 | bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, |
| 666 | size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) { |
| 667 | bool result = false; |
| 668 | if (deviceId >= 0) { |
| 669 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 670 | if (deviceIndex >= 0) { |
| 671 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 672 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 673 | result = device->markSupportedKeyCodes(sourceMask, |
| 674 | numCodes, keyCodes, outFlags); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 675 | } |
| 676 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 677 | } else { |
| 678 | size_t numDevices = mDevices.size(); |
| 679 | for (size_t i = 0; i < numDevices; i++) { |
| 680 | InputDevice* device = mDevices.valueAt(i); |
| 681 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 682 | result |= device->markSupportedKeyCodes(sourceMask, |
| 683 | numCodes, keyCodes, outFlags); |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | return result; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 690 | void InputReader::requestRefreshConfiguration(uint32_t changes) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 691 | AutoMutex _l(mLock); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 692 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 693 | if (changes) { |
| 694 | bool needWake = !mConfigurationChangesToRefresh; |
| 695 | mConfigurationChangesToRefresh |= changes; |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 696 | |
| 697 | if (needWake) { |
| 698 | mEventHub->wake(); |
| 699 | } |
| 700 | } |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 703 | void InputReader::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 704 | AutoMutex _l(mLock); |
| 705 | |
Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 706 | mEventHub->dump(dump); |
| 707 | dump.append("\n"); |
| 708 | |
| 709 | dump.append("Input Reader State:\n"); |
| 710 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 711 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 712 | mDevices.valueAt(i)->dump(dump); |
| 713 | } |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 714 | |
| 715 | dump.append(INDENT "Configuration:\n"); |
| 716 | dump.append(INDENT2 "ExcludedDeviceNames: ["); |
| 717 | for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) { |
| 718 | if (i != 0) { |
| 719 | dump.append(", "); |
| 720 | } |
| 721 | dump.append(mConfig.excludedDeviceNames.itemAt(i).string()); |
| 722 | } |
| 723 | dump.append("]\n"); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 724 | dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n", |
| 725 | mConfig.virtualKeyQuietTime * 0.000001f); |
| 726 | |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 727 | dump.appendFormat(INDENT2 "PointerVelocityControlParameters: " |
| 728 | "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n", |
| 729 | mConfig.pointerVelocityControlParameters.scale, |
| 730 | mConfig.pointerVelocityControlParameters.lowThreshold, |
| 731 | mConfig.pointerVelocityControlParameters.highThreshold, |
| 732 | mConfig.pointerVelocityControlParameters.acceleration); |
| 733 | |
| 734 | dump.appendFormat(INDENT2 "WheelVelocityControlParameters: " |
| 735 | "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n", |
| 736 | mConfig.wheelVelocityControlParameters.scale, |
| 737 | mConfig.wheelVelocityControlParameters.lowThreshold, |
| 738 | mConfig.wheelVelocityControlParameters.highThreshold, |
| 739 | mConfig.wheelVelocityControlParameters.acceleration); |
| 740 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 741 | dump.appendFormat(INDENT2 "PointerGesture:\n"); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 742 | dump.appendFormat(INDENT3 "Enabled: %s\n", |
| 743 | toString(mConfig.pointerGesturesEnabled)); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 744 | dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n", |
| 745 | mConfig.pointerGestureQuietInterval * 0.000001f); |
| 746 | dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n", |
| 747 | mConfig.pointerGestureDragMinSwitchSpeed); |
| 748 | dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n", |
| 749 | mConfig.pointerGestureTapInterval * 0.000001f); |
| 750 | dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n", |
| 751 | mConfig.pointerGestureTapDragInterval * 0.000001f); |
| 752 | dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n", |
| 753 | mConfig.pointerGestureTapSlop); |
| 754 | dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n", |
| 755 | mConfig.pointerGestureMultitouchSettleInterval * 0.000001f); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 756 | dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n", |
| 757 | mConfig.pointerGestureMultitouchMinDistance); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 758 | dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n", |
| 759 | mConfig.pointerGestureSwipeTransitionAngleCosine); |
| 760 | dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n", |
| 761 | mConfig.pointerGestureSwipeMaxWidthRatio); |
| 762 | dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n", |
| 763 | mConfig.pointerGestureMovementSpeedRatio); |
| 764 | dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n", |
| 765 | mConfig.pointerGestureZoomSpeedRatio); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Jeff Brown | 89ef072 | 2011-08-10 16:25:21 -0700 | [diff] [blame] | 768 | void InputReader::monitor() { |
| 769 | // Acquire and release the lock to ensure that the reader has not deadlocked. |
| 770 | mLock.lock(); |
| 771 | mLock.unlock(); |
| 772 | |
| 773 | // Check the EventHub |
| 774 | mEventHub->monitor(); |
| 775 | } |
| 776 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 777 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 778 | // --- InputReader::ContextImpl --- |
| 779 | |
| 780 | InputReader::ContextImpl::ContextImpl(InputReader* reader) : |
| 781 | mReader(reader) { |
| 782 | } |
| 783 | |
| 784 | void InputReader::ContextImpl::updateGlobalMetaState() { |
| 785 | // lock is already held by the input loop |
| 786 | mReader->updateGlobalMetaStateLocked(); |
| 787 | } |
| 788 | |
| 789 | int32_t InputReader::ContextImpl::getGlobalMetaState() { |
| 790 | // lock is already held by the input loop |
| 791 | return mReader->getGlobalMetaStateLocked(); |
| 792 | } |
| 793 | |
| 794 | void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) { |
| 795 | // lock is already held by the input loop |
| 796 | mReader->disableVirtualKeysUntilLocked(time); |
| 797 | } |
| 798 | |
| 799 | bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, |
| 800 | InputDevice* device, int32_t keyCode, int32_t scanCode) { |
| 801 | // lock is already held by the input loop |
| 802 | return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode); |
| 803 | } |
| 804 | |
| 805 | void InputReader::ContextImpl::fadePointer() { |
| 806 | // lock is already held by the input loop |
| 807 | mReader->fadePointerLocked(); |
| 808 | } |
| 809 | |
| 810 | void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) { |
| 811 | // lock is already held by the input loop |
| 812 | mReader->requestTimeoutAtTimeLocked(when); |
| 813 | } |
| 814 | |
| 815 | InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() { |
| 816 | return mReader->mPolicy.get(); |
| 817 | } |
| 818 | |
| 819 | InputListenerInterface* InputReader::ContextImpl::getListener() { |
| 820 | return mReader->mQueuedListener.get(); |
| 821 | } |
| 822 | |
| 823 | EventHubInterface* InputReader::ContextImpl::getEventHub() { |
| 824 | return mReader->mEventHub.get(); |
| 825 | } |
| 826 | |
| 827 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 828 | // --- InputReaderThread --- |
| 829 | |
| 830 | InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) : |
| 831 | Thread(/*canCallJava*/ true), mReader(reader) { |
| 832 | } |
| 833 | |
| 834 | InputReaderThread::~InputReaderThread() { |
| 835 | } |
| 836 | |
| 837 | bool InputReaderThread::threadLoop() { |
| 838 | mReader->loopOnce(); |
| 839 | return true; |
| 840 | } |
| 841 | |
| 842 | |
| 843 | // --- InputDevice --- |
| 844 | |
| 845 | InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) : |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 846 | mContext(context), mId(id), mName(name), mSources(0), |
| 847 | mIsExternal(false), mDropUntilNextSync(false) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | InputDevice::~InputDevice() { |
| 851 | size_t numMappers = mMappers.size(); |
| 852 | for (size_t i = 0; i < numMappers; i++) { |
| 853 | delete mMappers[i]; |
| 854 | } |
| 855 | mMappers.clear(); |
| 856 | } |
| 857 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 858 | void InputDevice::dump(String8& dump) { |
| 859 | InputDeviceInfo deviceInfo; |
| 860 | getDeviceInfo(& deviceInfo); |
| 861 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 862 | dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(), |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 863 | deviceInfo.getName().string()); |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 864 | dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal)); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 865 | dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources()); |
| 866 | dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType()); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 867 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 868 | const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges(); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 869 | if (!ranges.isEmpty()) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 870 | dump.append(INDENT2 "Motion Ranges:\n"); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 871 | for (size_t i = 0; i < ranges.size(); i++) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 872 | const InputDeviceInfo::MotionRange& range = ranges.itemAt(i); |
| 873 | const char* label = getAxisLabel(range.axis); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 874 | char name[32]; |
| 875 | if (label) { |
| 876 | strncpy(name, label, sizeof(name)); |
| 877 | name[sizeof(name) - 1] = '\0'; |
| 878 | } else { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 879 | snprintf(name, sizeof(name), "%d", range.axis); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 880 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 881 | dump.appendFormat(INDENT3 "%s: source=0x%08x, " |
| 882 | "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n", |
| 883 | name, range.source, range.min, range.max, range.flat, range.fuzz); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 884 | } |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | size_t numMappers = mMappers.size(); |
| 888 | for (size_t i = 0; i < numMappers; i++) { |
| 889 | InputMapper* mapper = mMappers[i]; |
| 890 | mapper->dump(dump); |
| 891 | } |
| 892 | } |
| 893 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 894 | void InputDevice::addMapper(InputMapper* mapper) { |
| 895 | mMappers.add(mapper); |
| 896 | } |
| 897 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 898 | void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 899 | mSources = 0; |
| 900 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 901 | if (!isIgnored()) { |
| 902 | if (!changes) { // first time only |
| 903 | mContext->getEventHub()->getConfiguration(mId, &mConfiguration); |
| 904 | } |
| 905 | |
| 906 | size_t numMappers = mMappers.size(); |
| 907 | for (size_t i = 0; i < numMappers; i++) { |
| 908 | InputMapper* mapper = mMappers[i]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 909 | mapper->configure(when, config, changes); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 910 | mSources |= mapper->getSources(); |
| 911 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 912 | } |
| 913 | } |
| 914 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 915 | void InputDevice::reset(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 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->reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 920 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 921 | |
| 922 | mContext->updateGlobalMetaState(); |
| 923 | |
| 924 | notifyReset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 925 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 926 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 927 | void InputDevice::process(const RawEvent* rawEvents, size_t count) { |
| 928 | // Process all of the events in order for each mapper. |
| 929 | // We cannot simply ask each mapper to process them in bulk because mappers may |
| 930 | // have side-effects that must be interleaved. For example, joystick movement events and |
| 931 | // gamepad button presses are handled by different mappers but they should be dispatched |
| 932 | // in the order received. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 933 | size_t numMappers = mMappers.size(); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 934 | for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) { |
| 935 | #if DEBUG_RAW_EVENTS |
| 936 | LOGD("Input event: device=%d type=0x%04x scancode=0x%04x " |
Jeff Brown | 2e45fb6 | 2011-06-29 21:19:05 -0700 | [diff] [blame] | 937 | "keycode=0x%04x value=0x%08x flags=0x%08x", |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 938 | rawEvent->deviceId, rawEvent->type, rawEvent->scanCode, rawEvent->keyCode, |
| 939 | rawEvent->value, rawEvent->flags); |
| 940 | #endif |
| 941 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 942 | if (mDropUntilNextSync) { |
| 943 | if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) { |
| 944 | mDropUntilNextSync = false; |
| 945 | #if DEBUG_RAW_EVENTS |
| 946 | LOGD("Recovered from input event buffer overrun."); |
| 947 | #endif |
| 948 | } else { |
| 949 | #if DEBUG_RAW_EVENTS |
| 950 | LOGD("Dropped input event while waiting for next input sync."); |
| 951 | #endif |
| 952 | } |
| 953 | } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_DROPPED) { |
| 954 | LOGI("Detected input event buffer overrun for device %s.", mName.string()); |
| 955 | mDropUntilNextSync = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 956 | reset(rawEvent->when); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 957 | } else { |
| 958 | for (size_t i = 0; i < numMappers; i++) { |
| 959 | InputMapper* mapper = mMappers[i]; |
| 960 | mapper->process(rawEvent); |
| 961 | } |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 962 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 963 | } |
| 964 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 965 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 966 | void InputDevice::timeoutExpired(nsecs_t when) { |
| 967 | size_t numMappers = mMappers.size(); |
| 968 | for (size_t i = 0; i < numMappers; i++) { |
| 969 | InputMapper* mapper = mMappers[i]; |
| 970 | mapper->timeoutExpired(when); |
| 971 | } |
| 972 | } |
| 973 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 974 | void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) { |
| 975 | outDeviceInfo->initialize(mId, mName); |
| 976 | |
| 977 | size_t numMappers = mMappers.size(); |
| 978 | for (size_t i = 0; i < numMappers; i++) { |
| 979 | InputMapper* mapper = mMappers[i]; |
| 980 | mapper->populateDeviceInfo(outDeviceInfo); |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 985 | return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState); |
| 986 | } |
| 987 | |
| 988 | int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 989 | return getState(sourceMask, scanCode, & InputMapper::getScanCodeState); |
| 990 | } |
| 991 | |
| 992 | int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 993 | return getState(sourceMask, switchCode, & InputMapper::getSwitchState); |
| 994 | } |
| 995 | |
| 996 | int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) { |
| 997 | int32_t result = AKEY_STATE_UNKNOWN; |
| 998 | size_t numMappers = mMappers.size(); |
| 999 | for (size_t i = 0; i < numMappers; i++) { |
| 1000 | InputMapper* mapper = mMappers[i]; |
| 1001 | if (sourcesMatchMask(mapper->getSources(), sourceMask)) { |
| 1002 | result = (mapper->*getStateFunc)(sourceMask, code); |
| 1003 | if (result >= AKEY_STATE_DOWN) { |
| 1004 | return result; |
| 1005 | } |
| 1006 | } |
| 1007 | } |
| 1008 | return result; |
| 1009 | } |
| 1010 | |
| 1011 | bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 1012 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 1013 | bool result = false; |
| 1014 | size_t numMappers = mMappers.size(); |
| 1015 | for (size_t i = 0; i < numMappers; i++) { |
| 1016 | InputMapper* mapper = mMappers[i]; |
| 1017 | if (sourcesMatchMask(mapper->getSources(), sourceMask)) { |
| 1018 | result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags); |
| 1019 | } |
| 1020 | } |
| 1021 | return result; |
| 1022 | } |
| 1023 | |
| 1024 | int32_t InputDevice::getMetaState() { |
| 1025 | int32_t result = 0; |
| 1026 | size_t numMappers = mMappers.size(); |
| 1027 | for (size_t i = 0; i < numMappers; i++) { |
| 1028 | InputMapper* mapper = mMappers[i]; |
| 1029 | result |= mapper->getMetaState(); |
| 1030 | } |
| 1031 | return result; |
| 1032 | } |
| 1033 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1034 | void InputDevice::fadePointer() { |
| 1035 | size_t numMappers = mMappers.size(); |
| 1036 | for (size_t i = 0; i < numMappers; i++) { |
| 1037 | InputMapper* mapper = mMappers[i]; |
| 1038 | mapper->fadePointer(); |
| 1039 | } |
| 1040 | } |
| 1041 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1042 | void InputDevice::notifyReset(nsecs_t when) { |
| 1043 | NotifyDeviceResetArgs args(when, mId); |
| 1044 | mContext->getListener()->notifyDeviceReset(&args); |
| 1045 | } |
| 1046 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1047 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1048 | // --- CursorButtonAccumulator --- |
| 1049 | |
| 1050 | CursorButtonAccumulator::CursorButtonAccumulator() { |
| 1051 | clearButtons(); |
| 1052 | } |
| 1053 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1054 | void CursorButtonAccumulator::reset(InputDevice* device) { |
| 1055 | mBtnLeft = device->isKeyPressed(BTN_LEFT); |
| 1056 | mBtnRight = device->isKeyPressed(BTN_RIGHT); |
| 1057 | mBtnMiddle = device->isKeyPressed(BTN_MIDDLE); |
| 1058 | mBtnBack = device->isKeyPressed(BTN_BACK); |
| 1059 | mBtnSide = device->isKeyPressed(BTN_SIDE); |
| 1060 | mBtnForward = device->isKeyPressed(BTN_FORWARD); |
| 1061 | mBtnExtra = device->isKeyPressed(BTN_EXTRA); |
| 1062 | mBtnTask = device->isKeyPressed(BTN_TASK); |
| 1063 | } |
| 1064 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1065 | void CursorButtonAccumulator::clearButtons() { |
| 1066 | mBtnLeft = 0; |
| 1067 | mBtnRight = 0; |
| 1068 | mBtnMiddle = 0; |
| 1069 | mBtnBack = 0; |
| 1070 | mBtnSide = 0; |
| 1071 | mBtnForward = 0; |
| 1072 | mBtnExtra = 0; |
| 1073 | mBtnTask = 0; |
| 1074 | } |
| 1075 | |
| 1076 | void CursorButtonAccumulator::process(const RawEvent* rawEvent) { |
| 1077 | if (rawEvent->type == EV_KEY) { |
| 1078 | switch (rawEvent->scanCode) { |
| 1079 | case BTN_LEFT: |
| 1080 | mBtnLeft = rawEvent->value; |
| 1081 | break; |
| 1082 | case BTN_RIGHT: |
| 1083 | mBtnRight = rawEvent->value; |
| 1084 | break; |
| 1085 | case BTN_MIDDLE: |
| 1086 | mBtnMiddle = rawEvent->value; |
| 1087 | break; |
| 1088 | case BTN_BACK: |
| 1089 | mBtnBack = rawEvent->value; |
| 1090 | break; |
| 1091 | case BTN_SIDE: |
| 1092 | mBtnSide = rawEvent->value; |
| 1093 | break; |
| 1094 | case BTN_FORWARD: |
| 1095 | mBtnForward = rawEvent->value; |
| 1096 | break; |
| 1097 | case BTN_EXTRA: |
| 1098 | mBtnExtra = rawEvent->value; |
| 1099 | break; |
| 1100 | case BTN_TASK: |
| 1101 | mBtnTask = rawEvent->value; |
| 1102 | break; |
| 1103 | } |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | uint32_t CursorButtonAccumulator::getButtonState() const { |
| 1108 | uint32_t result = 0; |
| 1109 | if (mBtnLeft) { |
| 1110 | result |= AMOTION_EVENT_BUTTON_PRIMARY; |
| 1111 | } |
| 1112 | if (mBtnRight) { |
| 1113 | result |= AMOTION_EVENT_BUTTON_SECONDARY; |
| 1114 | } |
| 1115 | if (mBtnMiddle) { |
| 1116 | result |= AMOTION_EVENT_BUTTON_TERTIARY; |
| 1117 | } |
| 1118 | if (mBtnBack || mBtnSide) { |
| 1119 | result |= AMOTION_EVENT_BUTTON_BACK; |
| 1120 | } |
| 1121 | if (mBtnForward || mBtnExtra) { |
| 1122 | result |= AMOTION_EVENT_BUTTON_FORWARD; |
| 1123 | } |
| 1124 | return result; |
| 1125 | } |
| 1126 | |
| 1127 | |
| 1128 | // --- CursorMotionAccumulator --- |
| 1129 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1130 | CursorMotionAccumulator::CursorMotionAccumulator() { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1131 | clearRelativeAxes(); |
| 1132 | } |
| 1133 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1134 | void CursorMotionAccumulator::reset(InputDevice* device) { |
| 1135 | clearRelativeAxes(); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | void CursorMotionAccumulator::clearRelativeAxes() { |
| 1139 | mRelX = 0; |
| 1140 | mRelY = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | void CursorMotionAccumulator::process(const RawEvent* rawEvent) { |
| 1144 | if (rawEvent->type == EV_REL) { |
| 1145 | switch (rawEvent->scanCode) { |
| 1146 | case REL_X: |
| 1147 | mRelX = rawEvent->value; |
| 1148 | break; |
| 1149 | case REL_Y: |
| 1150 | mRelY = rawEvent->value; |
| 1151 | break; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1152 | } |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | void CursorMotionAccumulator::finishSync() { |
| 1157 | clearRelativeAxes(); |
| 1158 | } |
| 1159 | |
| 1160 | |
| 1161 | // --- CursorScrollAccumulator --- |
| 1162 | |
| 1163 | CursorScrollAccumulator::CursorScrollAccumulator() : |
| 1164 | mHaveRelWheel(false), mHaveRelHWheel(false) { |
| 1165 | clearRelativeAxes(); |
| 1166 | } |
| 1167 | |
| 1168 | void CursorScrollAccumulator::configure(InputDevice* device) { |
| 1169 | mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL); |
| 1170 | mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL); |
| 1171 | } |
| 1172 | |
| 1173 | void CursorScrollAccumulator::reset(InputDevice* device) { |
| 1174 | clearRelativeAxes(); |
| 1175 | } |
| 1176 | |
| 1177 | void CursorScrollAccumulator::clearRelativeAxes() { |
| 1178 | mRelWheel = 0; |
| 1179 | mRelHWheel = 0; |
| 1180 | } |
| 1181 | |
| 1182 | void CursorScrollAccumulator::process(const RawEvent* rawEvent) { |
| 1183 | if (rawEvent->type == EV_REL) { |
| 1184 | switch (rawEvent->scanCode) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1185 | case REL_WHEEL: |
| 1186 | mRelWheel = rawEvent->value; |
| 1187 | break; |
| 1188 | case REL_HWHEEL: |
| 1189 | mRelHWheel = rawEvent->value; |
| 1190 | break; |
| 1191 | } |
| 1192 | } |
| 1193 | } |
| 1194 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1195 | void CursorScrollAccumulator::finishSync() { |
| 1196 | clearRelativeAxes(); |
| 1197 | } |
| 1198 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1199 | |
| 1200 | // --- TouchButtonAccumulator --- |
| 1201 | |
| 1202 | TouchButtonAccumulator::TouchButtonAccumulator() : |
| 1203 | mHaveBtnTouch(false) { |
| 1204 | clearButtons(); |
| 1205 | } |
| 1206 | |
| 1207 | void TouchButtonAccumulator::configure(InputDevice* device) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1208 | mHaveBtnTouch = device->hasKey(BTN_TOUCH); |
| 1209 | } |
| 1210 | |
| 1211 | void TouchButtonAccumulator::reset(InputDevice* device) { |
| 1212 | mBtnTouch = device->isKeyPressed(BTN_TOUCH); |
| 1213 | mBtnStylus = device->isKeyPressed(BTN_STYLUS); |
| 1214 | mBtnStylus2 = device->isKeyPressed(BTN_STYLUS); |
| 1215 | mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER); |
| 1216 | mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN); |
| 1217 | mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER); |
| 1218 | mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH); |
| 1219 | mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL); |
| 1220 | mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH); |
| 1221 | mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE); |
| 1222 | mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS); |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1223 | mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP); |
| 1224 | mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP); |
| 1225 | mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | void TouchButtonAccumulator::clearButtons() { |
| 1229 | mBtnTouch = 0; |
| 1230 | mBtnStylus = 0; |
| 1231 | mBtnStylus2 = 0; |
| 1232 | mBtnToolFinger = 0; |
| 1233 | mBtnToolPen = 0; |
| 1234 | mBtnToolRubber = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1235 | mBtnToolBrush = 0; |
| 1236 | mBtnToolPencil = 0; |
| 1237 | mBtnToolAirbrush = 0; |
| 1238 | mBtnToolMouse = 0; |
| 1239 | mBtnToolLens = 0; |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1240 | mBtnToolDoubleTap = 0; |
| 1241 | mBtnToolTripleTap = 0; |
| 1242 | mBtnToolQuadTap = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | void TouchButtonAccumulator::process(const RawEvent* rawEvent) { |
| 1246 | if (rawEvent->type == EV_KEY) { |
| 1247 | switch (rawEvent->scanCode) { |
| 1248 | case BTN_TOUCH: |
| 1249 | mBtnTouch = rawEvent->value; |
| 1250 | break; |
| 1251 | case BTN_STYLUS: |
| 1252 | mBtnStylus = rawEvent->value; |
| 1253 | break; |
| 1254 | case BTN_STYLUS2: |
| 1255 | mBtnStylus2 = rawEvent->value; |
| 1256 | break; |
| 1257 | case BTN_TOOL_FINGER: |
| 1258 | mBtnToolFinger = rawEvent->value; |
| 1259 | break; |
| 1260 | case BTN_TOOL_PEN: |
| 1261 | mBtnToolPen = rawEvent->value; |
| 1262 | break; |
| 1263 | case BTN_TOOL_RUBBER: |
| 1264 | mBtnToolRubber = rawEvent->value; |
| 1265 | break; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1266 | case BTN_TOOL_BRUSH: |
| 1267 | mBtnToolBrush = rawEvent->value; |
| 1268 | break; |
| 1269 | case BTN_TOOL_PENCIL: |
| 1270 | mBtnToolPencil = rawEvent->value; |
| 1271 | break; |
| 1272 | case BTN_TOOL_AIRBRUSH: |
| 1273 | mBtnToolAirbrush = rawEvent->value; |
| 1274 | break; |
| 1275 | case BTN_TOOL_MOUSE: |
| 1276 | mBtnToolMouse = rawEvent->value; |
| 1277 | break; |
| 1278 | case BTN_TOOL_LENS: |
| 1279 | mBtnToolLens = rawEvent->value; |
| 1280 | break; |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1281 | case BTN_TOOL_DOUBLETAP: |
| 1282 | mBtnToolDoubleTap = rawEvent->value; |
| 1283 | break; |
| 1284 | case BTN_TOOL_TRIPLETAP: |
| 1285 | mBtnToolTripleTap = rawEvent->value; |
| 1286 | break; |
| 1287 | case BTN_TOOL_QUADTAP: |
| 1288 | mBtnToolQuadTap = rawEvent->value; |
| 1289 | break; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1290 | } |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | uint32_t TouchButtonAccumulator::getButtonState() const { |
| 1295 | uint32_t result = 0; |
| 1296 | if (mBtnStylus) { |
| 1297 | result |= AMOTION_EVENT_BUTTON_SECONDARY; |
| 1298 | } |
| 1299 | if (mBtnStylus2) { |
| 1300 | result |= AMOTION_EVENT_BUTTON_TERTIARY; |
| 1301 | } |
| 1302 | return result; |
| 1303 | } |
| 1304 | |
| 1305 | int32_t TouchButtonAccumulator::getToolType() const { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1306 | if (mBtnToolMouse || mBtnToolLens) { |
| 1307 | return AMOTION_EVENT_TOOL_TYPE_MOUSE; |
| 1308 | } |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1309 | if (mBtnToolRubber) { |
| 1310 | return AMOTION_EVENT_TOOL_TYPE_ERASER; |
| 1311 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1312 | if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1313 | return AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 1314 | } |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1315 | if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1316 | return AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 1317 | } |
| 1318 | return AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
| 1319 | } |
| 1320 | |
Jeff Brown | d87c6d5 | 2011-08-10 14:55:59 -0700 | [diff] [blame] | 1321 | bool TouchButtonAccumulator::isToolActive() const { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1322 | return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber |
| 1323 | || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1324 | || mBtnToolMouse || mBtnToolLens |
| 1325 | || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | bool TouchButtonAccumulator::isHovering() const { |
| 1329 | return mHaveBtnTouch && !mBtnTouch; |
| 1330 | } |
| 1331 | |
| 1332 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1333 | // --- RawPointerAxes --- |
| 1334 | |
| 1335 | RawPointerAxes::RawPointerAxes() { |
| 1336 | clear(); |
| 1337 | } |
| 1338 | |
| 1339 | void RawPointerAxes::clear() { |
| 1340 | x.clear(); |
| 1341 | y.clear(); |
| 1342 | pressure.clear(); |
| 1343 | touchMajor.clear(); |
| 1344 | touchMinor.clear(); |
| 1345 | toolMajor.clear(); |
| 1346 | toolMinor.clear(); |
| 1347 | orientation.clear(); |
| 1348 | distance.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1349 | tiltX.clear(); |
| 1350 | tiltY.clear(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1351 | trackingId.clear(); |
| 1352 | slot.clear(); |
| 1353 | } |
| 1354 | |
| 1355 | |
| 1356 | // --- RawPointerData --- |
| 1357 | |
| 1358 | RawPointerData::RawPointerData() { |
| 1359 | clear(); |
| 1360 | } |
| 1361 | |
| 1362 | void RawPointerData::clear() { |
| 1363 | pointerCount = 0; |
| 1364 | clearIdBits(); |
| 1365 | } |
| 1366 | |
| 1367 | void RawPointerData::copyFrom(const RawPointerData& other) { |
| 1368 | pointerCount = other.pointerCount; |
| 1369 | hoveringIdBits = other.hoveringIdBits; |
| 1370 | touchingIdBits = other.touchingIdBits; |
| 1371 | |
| 1372 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 1373 | pointers[i] = other.pointers[i]; |
| 1374 | |
| 1375 | int id = pointers[i].id; |
| 1376 | idToIndex[id] = other.idToIndex[id]; |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const { |
| 1381 | float x = 0, y = 0; |
| 1382 | uint32_t count = touchingIdBits.count(); |
| 1383 | if (count) { |
| 1384 | for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) { |
| 1385 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 1386 | const Pointer& pointer = pointerForId(id); |
| 1387 | x += pointer.x; |
| 1388 | y += pointer.y; |
| 1389 | } |
| 1390 | x /= count; |
| 1391 | y /= count; |
| 1392 | } |
| 1393 | *outX = x; |
| 1394 | *outY = y; |
| 1395 | } |
| 1396 | |
| 1397 | |
| 1398 | // --- CookedPointerData --- |
| 1399 | |
| 1400 | CookedPointerData::CookedPointerData() { |
| 1401 | clear(); |
| 1402 | } |
| 1403 | |
| 1404 | void CookedPointerData::clear() { |
| 1405 | pointerCount = 0; |
| 1406 | hoveringIdBits.clear(); |
| 1407 | touchingIdBits.clear(); |
| 1408 | } |
| 1409 | |
| 1410 | void CookedPointerData::copyFrom(const CookedPointerData& other) { |
| 1411 | pointerCount = other.pointerCount; |
| 1412 | hoveringIdBits = other.hoveringIdBits; |
| 1413 | touchingIdBits = other.touchingIdBits; |
| 1414 | |
| 1415 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 1416 | pointerProperties[i].copyFrom(other.pointerProperties[i]); |
| 1417 | pointerCoords[i].copyFrom(other.pointerCoords[i]); |
| 1418 | |
| 1419 | int id = pointerProperties[i].id; |
| 1420 | idToIndex[id] = other.idToIndex[id]; |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1425 | // --- SingleTouchMotionAccumulator --- |
| 1426 | |
| 1427 | SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() { |
| 1428 | clearAbsoluteAxes(); |
| 1429 | } |
| 1430 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1431 | void SingleTouchMotionAccumulator::reset(InputDevice* device) { |
| 1432 | mAbsX = device->getAbsoluteAxisValue(ABS_X); |
| 1433 | mAbsY = device->getAbsoluteAxisValue(ABS_Y); |
| 1434 | mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE); |
| 1435 | mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH); |
| 1436 | mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE); |
| 1437 | mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X); |
| 1438 | mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y); |
| 1439 | } |
| 1440 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1441 | void SingleTouchMotionAccumulator::clearAbsoluteAxes() { |
| 1442 | mAbsX = 0; |
| 1443 | mAbsY = 0; |
| 1444 | mAbsPressure = 0; |
| 1445 | mAbsToolWidth = 0; |
| 1446 | mAbsDistance = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1447 | mAbsTiltX = 0; |
| 1448 | mAbsTiltY = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) { |
| 1452 | if (rawEvent->type == EV_ABS) { |
| 1453 | switch (rawEvent->scanCode) { |
| 1454 | case ABS_X: |
| 1455 | mAbsX = rawEvent->value; |
| 1456 | break; |
| 1457 | case ABS_Y: |
| 1458 | mAbsY = rawEvent->value; |
| 1459 | break; |
| 1460 | case ABS_PRESSURE: |
| 1461 | mAbsPressure = rawEvent->value; |
| 1462 | break; |
| 1463 | case ABS_TOOL_WIDTH: |
| 1464 | mAbsToolWidth = rawEvent->value; |
| 1465 | break; |
| 1466 | case ABS_DISTANCE: |
| 1467 | mAbsDistance = rawEvent->value; |
| 1468 | break; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1469 | case ABS_TILT_X: |
| 1470 | mAbsTiltX = rawEvent->value; |
| 1471 | break; |
| 1472 | case ABS_TILT_Y: |
| 1473 | mAbsTiltY = rawEvent->value; |
| 1474 | break; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1475 | } |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | |
| 1480 | // --- MultiTouchMotionAccumulator --- |
| 1481 | |
| 1482 | MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() : |
| 1483 | mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false) { |
| 1484 | } |
| 1485 | |
| 1486 | MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() { |
| 1487 | delete[] mSlots; |
| 1488 | } |
| 1489 | |
| 1490 | void MultiTouchMotionAccumulator::configure(size_t slotCount, bool usingSlotsProtocol) { |
| 1491 | mSlotCount = slotCount; |
| 1492 | mUsingSlotsProtocol = usingSlotsProtocol; |
| 1493 | |
| 1494 | delete[] mSlots; |
| 1495 | mSlots = new Slot[slotCount]; |
| 1496 | } |
| 1497 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1498 | void MultiTouchMotionAccumulator::reset(InputDevice* device) { |
| 1499 | // Unfortunately there is no way to read the initial contents of the slots. |
| 1500 | // So when we reset the accumulator, we must assume they are all zeroes. |
| 1501 | if (mUsingSlotsProtocol) { |
| 1502 | // Query the driver for the current slot index and use it as the initial slot |
| 1503 | // before we start reading events from the device. It is possible that the |
| 1504 | // current slot index will not be the same as it was when the first event was |
| 1505 | // written into the evdev buffer, which means the input mapper could start |
| 1506 | // out of sync with the initial state of the events in the evdev buffer. |
| 1507 | // In the extremely unlikely case that this happens, the data from |
| 1508 | // two slots will be confused until the next ABS_MT_SLOT event is received. |
| 1509 | // This can cause the touch point to "jump", but at least there will be |
| 1510 | // no stuck touches. |
| 1511 | int32_t initialSlot; |
| 1512 | status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(), |
| 1513 | ABS_MT_SLOT, &initialSlot); |
| 1514 | if (status) { |
| 1515 | LOGD("Could not retrieve current multitouch slot index. status=%d", status); |
| 1516 | initialSlot = -1; |
| 1517 | } |
| 1518 | clearSlots(initialSlot); |
| 1519 | } else { |
| 1520 | clearSlots(-1); |
| 1521 | } |
| 1522 | } |
| 1523 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1524 | void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1525 | if (mSlots) { |
| 1526 | for (size_t i = 0; i < mSlotCount; i++) { |
| 1527 | mSlots[i].clear(); |
| 1528 | } |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1529 | } |
| 1530 | mCurrentSlot = initialSlot; |
| 1531 | } |
| 1532 | |
| 1533 | void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) { |
| 1534 | if (rawEvent->type == EV_ABS) { |
| 1535 | bool newSlot = false; |
| 1536 | if (mUsingSlotsProtocol) { |
| 1537 | if (rawEvent->scanCode == ABS_MT_SLOT) { |
| 1538 | mCurrentSlot = rawEvent->value; |
| 1539 | newSlot = true; |
| 1540 | } |
| 1541 | } else if (mCurrentSlot < 0) { |
| 1542 | mCurrentSlot = 0; |
| 1543 | } |
| 1544 | |
| 1545 | if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) { |
| 1546 | #if DEBUG_POINTERS |
| 1547 | if (newSlot) { |
| 1548 | LOGW("MultiTouch device emitted invalid slot index %d but it " |
| 1549 | "should be between 0 and %d; ignoring this slot.", |
| 1550 | mCurrentSlot, mSlotCount - 1); |
| 1551 | } |
| 1552 | #endif |
| 1553 | } else { |
| 1554 | Slot* slot = &mSlots[mCurrentSlot]; |
| 1555 | |
| 1556 | switch (rawEvent->scanCode) { |
| 1557 | case ABS_MT_POSITION_X: |
| 1558 | slot->mInUse = true; |
| 1559 | slot->mAbsMTPositionX = rawEvent->value; |
| 1560 | break; |
| 1561 | case ABS_MT_POSITION_Y: |
| 1562 | slot->mInUse = true; |
| 1563 | slot->mAbsMTPositionY = rawEvent->value; |
| 1564 | break; |
| 1565 | case ABS_MT_TOUCH_MAJOR: |
| 1566 | slot->mInUse = true; |
| 1567 | slot->mAbsMTTouchMajor = rawEvent->value; |
| 1568 | break; |
| 1569 | case ABS_MT_TOUCH_MINOR: |
| 1570 | slot->mInUse = true; |
| 1571 | slot->mAbsMTTouchMinor = rawEvent->value; |
| 1572 | slot->mHaveAbsMTTouchMinor = true; |
| 1573 | break; |
| 1574 | case ABS_MT_WIDTH_MAJOR: |
| 1575 | slot->mInUse = true; |
| 1576 | slot->mAbsMTWidthMajor = rawEvent->value; |
| 1577 | break; |
| 1578 | case ABS_MT_WIDTH_MINOR: |
| 1579 | slot->mInUse = true; |
| 1580 | slot->mAbsMTWidthMinor = rawEvent->value; |
| 1581 | slot->mHaveAbsMTWidthMinor = true; |
| 1582 | break; |
| 1583 | case ABS_MT_ORIENTATION: |
| 1584 | slot->mInUse = true; |
| 1585 | slot->mAbsMTOrientation = rawEvent->value; |
| 1586 | break; |
| 1587 | case ABS_MT_TRACKING_ID: |
| 1588 | if (mUsingSlotsProtocol && rawEvent->value < 0) { |
Jeff Brown | 8bcbbef | 2011-08-11 15:49:09 -0700 | [diff] [blame] | 1589 | // The slot is no longer in use but it retains its previous contents, |
| 1590 | // which may be reused for subsequent touches. |
| 1591 | slot->mInUse = false; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1592 | } else { |
| 1593 | slot->mInUse = true; |
| 1594 | slot->mAbsMTTrackingId = rawEvent->value; |
| 1595 | } |
| 1596 | break; |
| 1597 | case ABS_MT_PRESSURE: |
| 1598 | slot->mInUse = true; |
| 1599 | slot->mAbsMTPressure = rawEvent->value; |
| 1600 | break; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1601 | case ABS_MT_DISTANCE: |
| 1602 | slot->mInUse = true; |
| 1603 | slot->mAbsMTDistance = rawEvent->value; |
| 1604 | break; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1605 | case ABS_MT_TOOL_TYPE: |
| 1606 | slot->mInUse = true; |
| 1607 | slot->mAbsMTToolType = rawEvent->value; |
| 1608 | slot->mHaveAbsMTToolType = true; |
| 1609 | break; |
| 1610 | } |
| 1611 | } |
| 1612 | } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_MT_REPORT) { |
| 1613 | // MultiTouch Sync: The driver has returned all data for *one* of the pointers. |
| 1614 | mCurrentSlot += 1; |
| 1615 | } |
| 1616 | } |
| 1617 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1618 | void MultiTouchMotionAccumulator::finishSync() { |
| 1619 | if (!mUsingSlotsProtocol) { |
| 1620 | clearSlots(-1); |
| 1621 | } |
| 1622 | } |
| 1623 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1624 | |
| 1625 | // --- MultiTouchMotionAccumulator::Slot --- |
| 1626 | |
| 1627 | MultiTouchMotionAccumulator::Slot::Slot() { |
| 1628 | clear(); |
| 1629 | } |
| 1630 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1631 | void MultiTouchMotionAccumulator::Slot::clear() { |
| 1632 | mInUse = false; |
| 1633 | mHaveAbsMTTouchMinor = false; |
| 1634 | mHaveAbsMTWidthMinor = false; |
| 1635 | mHaveAbsMTToolType = false; |
| 1636 | mAbsMTPositionX = 0; |
| 1637 | mAbsMTPositionY = 0; |
| 1638 | mAbsMTTouchMajor = 0; |
| 1639 | mAbsMTTouchMinor = 0; |
| 1640 | mAbsMTWidthMajor = 0; |
| 1641 | mAbsMTWidthMinor = 0; |
| 1642 | mAbsMTOrientation = 0; |
| 1643 | mAbsMTTrackingId = -1; |
| 1644 | mAbsMTPressure = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1645 | mAbsMTDistance = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1646 | mAbsMTToolType = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | int32_t MultiTouchMotionAccumulator::Slot::getToolType() const { |
| 1650 | if (mHaveAbsMTToolType) { |
| 1651 | switch (mAbsMTToolType) { |
| 1652 | case MT_TOOL_FINGER: |
| 1653 | return AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 1654 | case MT_TOOL_PEN: |
| 1655 | return AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 1656 | } |
| 1657 | } |
| 1658 | return AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
| 1659 | } |
| 1660 | |
| 1661 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1662 | // --- InputMapper --- |
| 1663 | |
| 1664 | InputMapper::InputMapper(InputDevice* device) : |
| 1665 | mDevice(device), mContext(device->getContext()) { |
| 1666 | } |
| 1667 | |
| 1668 | InputMapper::~InputMapper() { |
| 1669 | } |
| 1670 | |
| 1671 | void InputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 1672 | info->addSource(getSources()); |
| 1673 | } |
| 1674 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1675 | void InputMapper::dump(String8& dump) { |
| 1676 | } |
| 1677 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1678 | void InputMapper::configure(nsecs_t when, |
| 1679 | const InputReaderConfiguration* config, uint32_t changes) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1680 | } |
| 1681 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1682 | void InputMapper::reset(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1683 | } |
| 1684 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 1685 | void InputMapper::timeoutExpired(nsecs_t when) { |
| 1686 | } |
| 1687 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1688 | int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 1689 | return AKEY_STATE_UNKNOWN; |
| 1690 | } |
| 1691 | |
| 1692 | int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 1693 | return AKEY_STATE_UNKNOWN; |
| 1694 | } |
| 1695 | |
| 1696 | int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 1697 | return AKEY_STATE_UNKNOWN; |
| 1698 | } |
| 1699 | |
| 1700 | bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 1701 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 1702 | return false; |
| 1703 | } |
| 1704 | |
| 1705 | int32_t InputMapper::getMetaState() { |
| 1706 | return 0; |
| 1707 | } |
| 1708 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1709 | void InputMapper::fadePointer() { |
| 1710 | } |
| 1711 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1712 | status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) { |
| 1713 | return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo); |
| 1714 | } |
| 1715 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1716 | void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump, |
| 1717 | const RawAbsoluteAxisInfo& axis, const char* name) { |
| 1718 | if (axis.valid) { |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 1719 | dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n", |
| 1720 | name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1721 | } else { |
| 1722 | dump.appendFormat(INDENT4 "%s: unknown range\n", name); |
| 1723 | } |
| 1724 | } |
| 1725 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1726 | |
| 1727 | // --- SwitchInputMapper --- |
| 1728 | |
| 1729 | SwitchInputMapper::SwitchInputMapper(InputDevice* device) : |
| 1730 | InputMapper(device) { |
| 1731 | } |
| 1732 | |
| 1733 | SwitchInputMapper::~SwitchInputMapper() { |
| 1734 | } |
| 1735 | |
| 1736 | uint32_t SwitchInputMapper::getSources() { |
Jeff Brown | 89de57a | 2011-01-19 18:41:38 -0800 | [diff] [blame] | 1737 | return AINPUT_SOURCE_SWITCH; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | void SwitchInputMapper::process(const RawEvent* rawEvent) { |
| 1741 | switch (rawEvent->type) { |
| 1742 | case EV_SW: |
| 1743 | processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value); |
| 1744 | break; |
| 1745 | } |
| 1746 | } |
| 1747 | |
| 1748 | void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1749 | NotifySwitchArgs args(when, 0, switchCode, switchValue); |
| 1750 | getListener()->notifySwitch(&args); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 1754 | return getEventHub()->getSwitchState(getDeviceId(), switchCode); |
| 1755 | } |
| 1756 | |
| 1757 | |
| 1758 | // --- KeyboardInputMapper --- |
| 1759 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1760 | KeyboardInputMapper::KeyboardInputMapper(InputDevice* device, |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1761 | uint32_t source, int32_t keyboardType) : |
| 1762 | InputMapper(device), mSource(source), |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1763 | mKeyboardType(keyboardType) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | KeyboardInputMapper::~KeyboardInputMapper() { |
| 1767 | } |
| 1768 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1769 | uint32_t KeyboardInputMapper::getSources() { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1770 | return mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 1774 | InputMapper::populateDeviceInfo(info); |
| 1775 | |
| 1776 | info->setKeyboardType(mKeyboardType); |
| 1777 | } |
| 1778 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1779 | void KeyboardInputMapper::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1780 | dump.append(INDENT2 "Keyboard Input Mapper:\n"); |
| 1781 | dumpParameters(dump); |
| 1782 | dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1783 | dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1784 | dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size()); |
| 1785 | dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState); |
| 1786 | dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1787 | } |
| 1788 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1789 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1790 | void KeyboardInputMapper::configure(nsecs_t when, |
| 1791 | const InputReaderConfiguration* config, uint32_t changes) { |
| 1792 | InputMapper::configure(when, config, changes); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1793 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 1794 | if (!changes) { // first time only |
| 1795 | // Configure basic parameters. |
| 1796 | configureParameters(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1797 | } |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1798 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1799 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) { |
| 1800 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) { |
| 1801 | if (!config->getDisplayInfo(mParameters.associatedDisplayId, |
| 1802 | false /*external*/, NULL, NULL, &mOrientation)) { |
| 1803 | mOrientation = DISPLAY_ORIENTATION_0; |
| 1804 | } |
| 1805 | } else { |
| 1806 | mOrientation = DISPLAY_ORIENTATION_0; |
| 1807 | } |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1808 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | void KeyboardInputMapper::configureParameters() { |
| 1812 | mParameters.orientationAware = false; |
| 1813 | getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"), |
| 1814 | mParameters.orientationAware); |
| 1815 | |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 1816 | mParameters.associatedDisplayId = -1; |
| 1817 | if (mParameters.orientationAware) { |
| 1818 | mParameters.associatedDisplayId = 0; |
| 1819 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | void KeyboardInputMapper::dumpParameters(String8& dump) { |
| 1823 | dump.append(INDENT3 "Parameters:\n"); |
| 1824 | dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n", |
| 1825 | mParameters.associatedDisplayId); |
| 1826 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 1827 | toString(mParameters.orientationAware)); |
| 1828 | } |
| 1829 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1830 | void KeyboardInputMapper::reset(nsecs_t when) { |
| 1831 | mMetaState = AMETA_NONE; |
| 1832 | mDownTime = 0; |
| 1833 | mKeyDowns.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1834 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1835 | resetLedState(); |
| 1836 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1837 | InputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | void KeyboardInputMapper::process(const RawEvent* rawEvent) { |
| 1841 | switch (rawEvent->type) { |
| 1842 | case EV_KEY: { |
| 1843 | int32_t scanCode = rawEvent->scanCode; |
| 1844 | if (isKeyboardOrGamepadKey(scanCode)) { |
| 1845 | processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode, |
| 1846 | rawEvent->flags); |
| 1847 | } |
| 1848 | break; |
| 1849 | } |
| 1850 | } |
| 1851 | } |
| 1852 | |
| 1853 | bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) { |
| 1854 | return scanCode < BTN_MOUSE |
| 1855 | || scanCode >= KEY_OK |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 1856 | || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE) |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1857 | || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1860 | void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode, |
| 1861 | int32_t scanCode, uint32_t policyFlags) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1862 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1863 | if (down) { |
| 1864 | // Rotate key codes according to orientation if needed. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1865 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1866 | keyCode = rotateKeyCode(keyCode, mOrientation); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1867 | } |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 1868 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1869 | // Add key down. |
| 1870 | ssize_t keyDownIndex = findKeyDown(scanCode); |
| 1871 | if (keyDownIndex >= 0) { |
| 1872 | // key repeat, be sure to use same keycode as before in case of rotation |
| 1873 | keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1874 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1875 | // key down |
| 1876 | if ((policyFlags & POLICY_FLAG_VIRTUAL) |
| 1877 | && mContext->shouldDropVirtualKey(when, |
| 1878 | getDevice(), keyCode, scanCode)) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1879 | return; |
| 1880 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1881 | |
| 1882 | mKeyDowns.push(); |
| 1883 | KeyDown& keyDown = mKeyDowns.editTop(); |
| 1884 | keyDown.keyCode = keyCode; |
| 1885 | keyDown.scanCode = scanCode; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1886 | } |
| 1887 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1888 | mDownTime = when; |
| 1889 | } else { |
| 1890 | // Remove key down. |
| 1891 | ssize_t keyDownIndex = findKeyDown(scanCode); |
| 1892 | if (keyDownIndex >= 0) { |
| 1893 | // key up, be sure to use same keycode as before in case of rotation |
| 1894 | keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode; |
| 1895 | mKeyDowns.removeAt(size_t(keyDownIndex)); |
| 1896 | } else { |
| 1897 | // key was not actually down |
| 1898 | LOGI("Dropping key up from device %s because the key was not down. " |
| 1899 | "keyCode=%d, scanCode=%d", |
| 1900 | getDeviceName().string(), keyCode, scanCode); |
| 1901 | return; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1902 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1903 | } |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 1904 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1905 | bool metaStateChanged = false; |
| 1906 | int32_t oldMetaState = mMetaState; |
| 1907 | int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState); |
| 1908 | if (oldMetaState != newMetaState) { |
| 1909 | mMetaState = newMetaState; |
| 1910 | metaStateChanged = true; |
| 1911 | updateLedState(false); |
| 1912 | } |
| 1913 | |
| 1914 | nsecs_t downTime = mDownTime; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1915 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1916 | // Key down on external an keyboard should wake the device. |
| 1917 | // We don't do this for internal keyboards to prevent them from waking up in your pocket. |
| 1918 | // For internal keyboards, the key layout file should specify the policy flags for |
| 1919 | // each wake key individually. |
| 1920 | // TODO: Use the input device configuration to control this behavior more finely. |
| 1921 | if (down && getDevice()->isExternal() |
| 1922 | && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) { |
| 1923 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 1924 | } |
| 1925 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1926 | if (metaStateChanged) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1927 | getContext()->updateGlobalMetaState(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1928 | } |
| 1929 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1930 | if (down && !isMetaKey(keyCode)) { |
| 1931 | getContext()->fadePointer(); |
| 1932 | } |
| 1933 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1934 | NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1935 | down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, |
| 1936 | AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1937 | getListener()->notifyKey(&args); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1938 | } |
| 1939 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1940 | ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) { |
| 1941 | size_t n = mKeyDowns.size(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1942 | for (size_t i = 0; i < n; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1943 | if (mKeyDowns[i].scanCode == scanCode) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1944 | return i; |
| 1945 | } |
| 1946 | } |
| 1947 | return -1; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1948 | } |
| 1949 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1950 | int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 1951 | return getEventHub()->getKeyCodeState(getDeviceId(), keyCode); |
| 1952 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1953 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1954 | int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 1955 | return getEventHub()->getScanCodeState(getDeviceId(), scanCode); |
| 1956 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1957 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1958 | bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 1959 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 1960 | return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags); |
| 1961 | } |
| 1962 | |
| 1963 | int32_t KeyboardInputMapper::getMetaState() { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1964 | return mMetaState; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1965 | } |
| 1966 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1967 | void KeyboardInputMapper::resetLedState() { |
| 1968 | initializeLedState(mCapsLockLedState, LED_CAPSL); |
| 1969 | initializeLedState(mNumLockLedState, LED_NUML); |
| 1970 | initializeLedState(mScrollLockLedState, LED_SCROLLL); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1971 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1972 | updateLedState(true); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1973 | } |
| 1974 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1975 | void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) { |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1976 | ledState.avail = getEventHub()->hasLed(getDeviceId(), led); |
| 1977 | ledState.on = false; |
| 1978 | } |
| 1979 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1980 | void KeyboardInputMapper::updateLedState(bool reset) { |
| 1981 | updateLedStateForModifier(mCapsLockLedState, LED_CAPSL, |
Jeff Brown | 51e7fe75 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 1982 | AMETA_CAPS_LOCK_ON, reset); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1983 | updateLedStateForModifier(mNumLockLedState, LED_NUML, |
Jeff Brown | 51e7fe75 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 1984 | AMETA_NUM_LOCK_ON, reset); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1985 | updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL, |
Jeff Brown | 51e7fe75 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 1986 | AMETA_SCROLL_LOCK_ON, reset); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1987 | } |
| 1988 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1989 | void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState, |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1990 | int32_t led, int32_t modifier, bool reset) { |
| 1991 | if (ledState.avail) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1992 | bool desiredState = (mMetaState & modifier) != 0; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1993 | if (reset || ledState.on != desiredState) { |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1994 | getEventHub()->setLedState(getDeviceId(), led, desiredState); |
| 1995 | ledState.on = desiredState; |
| 1996 | } |
| 1997 | } |
| 1998 | } |
| 1999 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2000 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2001 | // --- CursorInputMapper --- |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2002 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2003 | CursorInputMapper::CursorInputMapper(InputDevice* device) : |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2004 | InputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2005 | } |
| 2006 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2007 | CursorInputMapper::~CursorInputMapper() { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2008 | } |
| 2009 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2010 | uint32_t CursorInputMapper::getSources() { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2011 | return mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2012 | } |
| 2013 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2014 | void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2015 | InputMapper::populateDeviceInfo(info); |
| 2016 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2017 | if (mParameters.mode == Parameters::MODE_POINTER) { |
| 2018 | float minX, minY, maxX, maxY; |
| 2019 | if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2020 | info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f); |
| 2021 | 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] | 2022 | } |
| 2023 | } else { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2024 | info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale); |
| 2025 | 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] | 2026 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2027 | 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] | 2028 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2029 | if (mCursorScrollAccumulator.haveRelativeVWheel()) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2030 | 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] | 2031 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2032 | if (mCursorScrollAccumulator.haveRelativeHWheel()) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2033 | 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] | 2034 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2035 | } |
| 2036 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2037 | void CursorInputMapper::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2038 | dump.append(INDENT2 "Cursor Input Mapper:\n"); |
| 2039 | dumpParameters(dump); |
| 2040 | dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale); |
| 2041 | dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale); |
| 2042 | dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision); |
| 2043 | dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision); |
| 2044 | dump.appendFormat(INDENT3 "HaveVWheel: %s\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2045 | toString(mCursorScrollAccumulator.haveRelativeVWheel())); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2046 | dump.appendFormat(INDENT3 "HaveHWheel: %s\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2047 | toString(mCursorScrollAccumulator.haveRelativeHWheel())); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2048 | dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale); |
| 2049 | dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2050 | dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2051 | dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState); |
| 2052 | dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState))); |
| 2053 | dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2054 | } |
| 2055 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2056 | void CursorInputMapper::configure(nsecs_t when, |
| 2057 | const InputReaderConfiguration* config, uint32_t changes) { |
| 2058 | InputMapper::configure(when, config, changes); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2059 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2060 | if (!changes) { // first time only |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2061 | mCursorScrollAccumulator.configure(getDevice()); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2062 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2063 | // Configure basic parameters. |
| 2064 | configureParameters(); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2065 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2066 | // Configure device mode. |
| 2067 | switch (mParameters.mode) { |
| 2068 | case Parameters::MODE_POINTER: |
| 2069 | mSource = AINPUT_SOURCE_MOUSE; |
| 2070 | mXPrecision = 1.0f; |
| 2071 | mYPrecision = 1.0f; |
| 2072 | mXScale = 1.0f; |
| 2073 | mYScale = 1.0f; |
| 2074 | mPointerController = getPolicy()->obtainPointerController(getDeviceId()); |
| 2075 | break; |
| 2076 | case Parameters::MODE_NAVIGATION: |
| 2077 | mSource = AINPUT_SOURCE_TRACKBALL; |
| 2078 | mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 2079 | mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 2080 | mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 2081 | mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 2082 | break; |
| 2083 | } |
| 2084 | |
| 2085 | mVWheelScale = 1.0f; |
| 2086 | mHWheelScale = 1.0f; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2087 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 2088 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2089 | if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) { |
| 2090 | mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters); |
| 2091 | mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters); |
| 2092 | mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters); |
| 2093 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2094 | |
| 2095 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) { |
| 2096 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) { |
| 2097 | if (!config->getDisplayInfo(mParameters.associatedDisplayId, |
| 2098 | false /*external*/, NULL, NULL, &mOrientation)) { |
| 2099 | mOrientation = DISPLAY_ORIENTATION_0; |
| 2100 | } |
| 2101 | } else { |
| 2102 | mOrientation = DISPLAY_ORIENTATION_0; |
| 2103 | } |
| 2104 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2105 | } |
| 2106 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2107 | void CursorInputMapper::configureParameters() { |
| 2108 | mParameters.mode = Parameters::MODE_POINTER; |
| 2109 | String8 cursorModeString; |
| 2110 | if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) { |
| 2111 | if (cursorModeString == "navigation") { |
| 2112 | mParameters.mode = Parameters::MODE_NAVIGATION; |
| 2113 | } else if (cursorModeString != "pointer" && cursorModeString != "default") { |
| 2114 | LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string()); |
| 2115 | } |
| 2116 | } |
| 2117 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2118 | mParameters.orientationAware = false; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2119 | getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"), |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2120 | mParameters.orientationAware); |
| 2121 | |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2122 | mParameters.associatedDisplayId = -1; |
| 2123 | if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) { |
| 2124 | mParameters.associatedDisplayId = 0; |
| 2125 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2126 | } |
| 2127 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2128 | void CursorInputMapper::dumpParameters(String8& dump) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2129 | dump.append(INDENT3 "Parameters:\n"); |
| 2130 | dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n", |
| 2131 | mParameters.associatedDisplayId); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2132 | |
| 2133 | switch (mParameters.mode) { |
| 2134 | case Parameters::MODE_POINTER: |
| 2135 | dump.append(INDENT4 "Mode: pointer\n"); |
| 2136 | break; |
| 2137 | case Parameters::MODE_NAVIGATION: |
| 2138 | dump.append(INDENT4 "Mode: navigation\n"); |
| 2139 | break; |
| 2140 | default: |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 2141 | LOG_ASSERT(false); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2142 | } |
| 2143 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2144 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 2145 | toString(mParameters.orientationAware)); |
| 2146 | } |
| 2147 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2148 | void CursorInputMapper::reset(nsecs_t when) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2149 | mButtonState = 0; |
| 2150 | mDownTime = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2151 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2152 | mPointerVelocityControl.reset(); |
| 2153 | mWheelXVelocityControl.reset(); |
| 2154 | mWheelYVelocityControl.reset(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2155 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2156 | mCursorButtonAccumulator.reset(getDevice()); |
| 2157 | mCursorMotionAccumulator.reset(getDevice()); |
| 2158 | mCursorScrollAccumulator.reset(getDevice()); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2159 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2160 | InputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2161 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2162 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2163 | void CursorInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2164 | mCursorButtonAccumulator.process(rawEvent); |
| 2165 | mCursorMotionAccumulator.process(rawEvent); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2166 | mCursorScrollAccumulator.process(rawEvent); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2167 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2168 | if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) { |
| 2169 | sync(rawEvent->when); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2170 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2171 | } |
| 2172 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2173 | void CursorInputMapper::sync(nsecs_t when) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2174 | int32_t lastButtonState = mButtonState; |
| 2175 | int32_t currentButtonState = mCursorButtonAccumulator.getButtonState(); |
| 2176 | mButtonState = currentButtonState; |
| 2177 | |
| 2178 | bool wasDown = isPointerDown(lastButtonState); |
| 2179 | bool down = isPointerDown(currentButtonState); |
| 2180 | bool downChanged; |
| 2181 | if (!wasDown && down) { |
| 2182 | mDownTime = when; |
| 2183 | downChanged = true; |
| 2184 | } else if (wasDown && !down) { |
| 2185 | downChanged = true; |
| 2186 | } else { |
| 2187 | downChanged = false; |
| 2188 | } |
| 2189 | nsecs_t downTime = mDownTime; |
| 2190 | bool buttonsChanged = currentButtonState != lastButtonState; |
Jeff Brown | c28306a | 2011-08-23 21:32:42 -0700 | [diff] [blame] | 2191 | bool buttonsPressed = currentButtonState & ~lastButtonState; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2192 | |
| 2193 | float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale; |
| 2194 | float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale; |
| 2195 | bool moved = deltaX != 0 || deltaY != 0; |
| 2196 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2197 | // Rotate delta according to orientation if needed. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2198 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0 |
| 2199 | && (deltaX != 0.0f || deltaY != 0.0f)) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2200 | rotateDelta(mOrientation, &deltaX, &deltaY); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2201 | } |
| 2202 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2203 | // Move the pointer. |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2204 | PointerProperties pointerProperties; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2205 | pointerProperties.clear(); |
| 2206 | pointerProperties.id = 0; |
| 2207 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE; |
| 2208 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2209 | PointerCoords pointerCoords; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2210 | pointerCoords.clear(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2211 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2212 | float vscroll = mCursorScrollAccumulator.getRelativeVWheel(); |
| 2213 | float hscroll = mCursorScrollAccumulator.getRelativeHWheel(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2214 | bool scrolled = vscroll != 0 || hscroll != 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2215 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2216 | mWheelYVelocityControl.move(when, NULL, &vscroll); |
| 2217 | mWheelXVelocityControl.move(when, &hscroll, NULL); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2218 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2219 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2220 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2221 | if (mPointerController != NULL) { |
| 2222 | if (moved || scrolled || buttonsChanged) { |
| 2223 | mPointerController->setPresentation( |
| 2224 | PointerControllerInterface::PRESENTATION_POINTER); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2225 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2226 | if (moved) { |
| 2227 | mPointerController->move(deltaX, deltaY); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2228 | } |
| 2229 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2230 | if (buttonsChanged) { |
| 2231 | mPointerController->setButtonState(currentButtonState); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2232 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2233 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2234 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2235 | } |
| 2236 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2237 | float x, y; |
| 2238 | mPointerController->getPosition(&x, &y); |
| 2239 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 2240 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 2241 | } else { |
| 2242 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX); |
| 2243 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY); |
| 2244 | } |
| 2245 | |
| 2246 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2247 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2248 | // Moving an external trackball or mouse should wake the device. |
| 2249 | // We don't do this for internal cursor devices to prevent them from waking up |
| 2250 | // the device in your pocket. |
| 2251 | // TODO: Use the input device configuration to control this behavior more finely. |
| 2252 | uint32_t policyFlags = 0; |
Jeff Brown | c28306a | 2011-08-23 21:32:42 -0700 | [diff] [blame] | 2253 | if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) { |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2254 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 2255 | } |
| 2256 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2257 | // Synthesize key down from buttons if needed. |
| 2258 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource, |
| 2259 | policyFlags, lastButtonState, currentButtonState); |
| 2260 | |
| 2261 | // Send motion event. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2262 | if (downChanged || moved || scrolled || buttonsChanged) { |
| 2263 | int32_t metaState = mContext->getGlobalMetaState(); |
| 2264 | int32_t motionEventAction; |
| 2265 | if (downChanged) { |
| 2266 | motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP; |
| 2267 | } else if (down || mPointerController == NULL) { |
| 2268 | motionEventAction = AMOTION_EVENT_ACTION_MOVE; |
| 2269 | } else { |
| 2270 | motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE; |
| 2271 | } |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2272 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2273 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 2274 | motionEventAction, 0, metaState, currentButtonState, 0, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2275 | 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2276 | getListener()->notifyMotion(&args); |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2277 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2278 | // Send hover move after UP to tell the application that the mouse is hovering now. |
| 2279 | if (motionEventAction == AMOTION_EVENT_ACTION_UP |
| 2280 | && mPointerController != NULL) { |
| 2281 | NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags, |
| 2282 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 2283 | metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 2284 | 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime); |
| 2285 | getListener()->notifyMotion(&hoverArgs); |
| 2286 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2287 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2288 | // Send scroll events. |
| 2289 | if (scrolled) { |
| 2290 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll); |
| 2291 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll); |
| 2292 | |
| 2293 | NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags, |
| 2294 | AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState, |
| 2295 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 2296 | 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime); |
| 2297 | getListener()->notifyMotion(&scrollArgs); |
| 2298 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2299 | } |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2300 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2301 | // Synthesize key up from buttons if needed. |
| 2302 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource, |
| 2303 | policyFlags, lastButtonState, currentButtonState); |
| 2304 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2305 | mCursorMotionAccumulator.finishSync(); |
| 2306 | mCursorScrollAccumulator.finishSync(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2307 | } |
| 2308 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2309 | int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Jeff Brown | c3fc2d0 | 2010-08-10 15:47:53 -0700 | [diff] [blame] | 2310 | if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) { |
| 2311 | return getEventHub()->getScanCodeState(getDeviceId(), scanCode); |
| 2312 | } else { |
| 2313 | return AKEY_STATE_UNKNOWN; |
| 2314 | } |
| 2315 | } |
| 2316 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2317 | void CursorInputMapper::fadePointer() { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2318 | if (mPointerController != NULL) { |
| 2319 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 2320 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2321 | } |
| 2322 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2323 | |
| 2324 | // --- TouchInputMapper --- |
| 2325 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2326 | TouchInputMapper::TouchInputMapper(InputDevice* device) : |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2327 | InputMapper(device), |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2328 | mSource(0), mDeviceMode(DEVICE_MODE_DISABLED), |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2329 | mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2330 | } |
| 2331 | |
| 2332 | TouchInputMapper::~TouchInputMapper() { |
| 2333 | } |
| 2334 | |
| 2335 | uint32_t TouchInputMapper::getSources() { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2336 | return mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 2340 | InputMapper::populateDeviceInfo(info); |
| 2341 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2342 | if (mDeviceMode != DEVICE_MODE_DISABLED) { |
| 2343 | info->addMotionRange(mOrientedRanges.x); |
| 2344 | info->addMotionRange(mOrientedRanges.y); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2345 | info->addMotionRange(mOrientedRanges.pressure); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2346 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2347 | if (mOrientedRanges.haveSize) { |
| 2348 | info->addMotionRange(mOrientedRanges.size); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2349 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2350 | |
| 2351 | if (mOrientedRanges.haveTouchSize) { |
| 2352 | info->addMotionRange(mOrientedRanges.touchMajor); |
| 2353 | info->addMotionRange(mOrientedRanges.touchMinor); |
| 2354 | } |
| 2355 | |
| 2356 | if (mOrientedRanges.haveToolSize) { |
| 2357 | info->addMotionRange(mOrientedRanges.toolMajor); |
| 2358 | info->addMotionRange(mOrientedRanges.toolMinor); |
| 2359 | } |
| 2360 | |
| 2361 | if (mOrientedRanges.haveOrientation) { |
| 2362 | info->addMotionRange(mOrientedRanges.orientation); |
| 2363 | } |
| 2364 | |
| 2365 | if (mOrientedRanges.haveDistance) { |
| 2366 | info->addMotionRange(mOrientedRanges.distance); |
| 2367 | } |
| 2368 | |
| 2369 | if (mOrientedRanges.haveTilt) { |
| 2370 | info->addMotionRange(mOrientedRanges.tilt); |
| 2371 | } |
| 2372 | |
| 2373 | if (mCursorScrollAccumulator.haveRelativeVWheel()) { |
| 2374 | info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f); |
| 2375 | } |
| 2376 | if (mCursorScrollAccumulator.haveRelativeHWheel()) { |
| 2377 | info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f); |
| 2378 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2379 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2380 | } |
| 2381 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2382 | void TouchInputMapper::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2383 | dump.append(INDENT2 "Touch Input Mapper:\n"); |
| 2384 | dumpParameters(dump); |
| 2385 | dumpVirtualKeys(dump); |
| 2386 | dumpRawPointerAxes(dump); |
| 2387 | dumpCalibration(dump); |
| 2388 | dumpSurface(dump); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2389 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2390 | dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n"); |
| 2391 | dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale); |
| 2392 | dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale); |
| 2393 | dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision); |
| 2394 | dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision); |
| 2395 | dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2396 | dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale); |
| 2397 | dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2398 | dump.appendFormat(INDENT4 "OrientationCenter: %0.3f\n", mOrientationCenter); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2399 | dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale); |
| 2400 | dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2401 | dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt)); |
| 2402 | dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter); |
| 2403 | dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale); |
| 2404 | dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter); |
| 2405 | dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2406 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2407 | dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2408 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2409 | dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n", |
| 2410 | mLastRawPointerData.pointerCount); |
| 2411 | for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) { |
| 2412 | const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i]; |
| 2413 | dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, " |
| 2414 | "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2415 | "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, " |
| 2416 | "toolType=%d, isHovering=%s\n", i, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2417 | pointer.id, pointer.x, pointer.y, pointer.pressure, |
| 2418 | pointer.touchMajor, pointer.touchMinor, |
| 2419 | pointer.toolMajor, pointer.toolMinor, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2420 | pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2421 | pointer.toolType, toString(pointer.isHovering)); |
| 2422 | } |
| 2423 | |
| 2424 | dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n", |
| 2425 | mLastCookedPointerData.pointerCount); |
| 2426 | for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) { |
| 2427 | const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i]; |
| 2428 | const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i]; |
| 2429 | dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, " |
| 2430 | "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2431 | "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, " |
| 2432 | "toolType=%d, isHovering=%s\n", i, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2433 | pointerProperties.id, |
| 2434 | pointerCoords.getX(), |
| 2435 | pointerCoords.getY(), |
| 2436 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 2437 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 2438 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 2439 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 2440 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 2441 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2442 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT), |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2443 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), |
| 2444 | pointerProperties.toolType, |
| 2445 | toString(mLastCookedPointerData.isHovering(i))); |
| 2446 | } |
| 2447 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2448 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2449 | dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n"); |
| 2450 | dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2451 | mPointerXMovementScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2452 | dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2453 | mPointerYMovementScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2454 | dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2455 | mPointerXZoomScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2456 | dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2457 | mPointerYZoomScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2458 | dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n", |
| 2459 | mPointerGestureMaxSwipeWidth); |
| 2460 | } |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2461 | } |
| 2462 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2463 | void TouchInputMapper::configure(nsecs_t when, |
| 2464 | const InputReaderConfiguration* config, uint32_t changes) { |
| 2465 | InputMapper::configure(when, config, changes); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2466 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2467 | mConfig = *config; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2468 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2469 | if (!changes) { // first time only |
| 2470 | // Configure basic parameters. |
| 2471 | configureParameters(); |
| 2472 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2473 | // Configure common accumulators. |
| 2474 | mCursorScrollAccumulator.configure(getDevice()); |
| 2475 | mTouchButtonAccumulator.configure(getDevice()); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2476 | |
| 2477 | // Configure absolute axis information. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2478 | configureRawPointerAxes(); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2479 | |
| 2480 | // Prepare input device calibration. |
| 2481 | parseCalibration(); |
| 2482 | resolveCalibration(); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2483 | } |
| 2484 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2485 | if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2486 | // Update pointer speed. |
| 2487 | mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters); |
| 2488 | mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters); |
| 2489 | mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2490 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2491 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2492 | bool resetNeeded = false; |
| 2493 | if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame^] | 2494 | | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT |
| 2495 | | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2496 | // Configure device sources, surface dimensions, orientation and |
| 2497 | // scaling factors. |
| 2498 | configureSurface(when, &resetNeeded); |
| 2499 | } |
| 2500 | |
| 2501 | if (changes && resetNeeded) { |
| 2502 | // Send reset, unless this is the first time the device has been configured, |
| 2503 | // in which case the reader will call reset itself after all mappers are ready. |
| 2504 | getDevice()->notifyReset(when); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2505 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2506 | } |
| 2507 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2508 | void TouchInputMapper::configureParameters() { |
Jeff Brown | b126822 | 2011-06-03 17:06:16 -0700 | [diff] [blame] | 2509 | // Use the pointer presentation mode for devices that do not support distinct |
| 2510 | // multitouch. The spot-based presentation relies on being able to accurately |
| 2511 | // locate two or more fingers on the touch pad. |
| 2512 | mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT) |
| 2513 | ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2514 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2515 | String8 gestureModeString; |
| 2516 | if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"), |
| 2517 | gestureModeString)) { |
| 2518 | if (gestureModeString == "pointer") { |
| 2519 | mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER; |
| 2520 | } else if (gestureModeString == "spots") { |
| 2521 | mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS; |
| 2522 | } else if (gestureModeString != "default") { |
| 2523 | LOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string()); |
| 2524 | } |
| 2525 | } |
| 2526 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2527 | if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X) |
| 2528 | || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) { |
| 2529 | // The device is a cursor device with a touch pad attached. |
| 2530 | // By default don't use the touch pad to move the pointer. |
| 2531 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2532 | } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) { |
| 2533 | // The device is a pointing device like a track pad. |
| 2534 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
| 2535 | } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) { |
| 2536 | // The device is a touch screen. |
| 2537 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2538 | } else { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2539 | // The device is a touch pad of unknown purpose. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2540 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
| 2541 | } |
| 2542 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2543 | String8 deviceTypeString; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2544 | if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"), |
| 2545 | deviceTypeString)) { |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 2546 | if (deviceTypeString == "touchScreen") { |
| 2547 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2548 | } else if (deviceTypeString == "touchPad") { |
| 2549 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2550 | } else if (deviceTypeString == "pointer") { |
| 2551 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2552 | } else if (deviceTypeString != "default") { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2553 | LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string()); |
| 2554 | } |
| 2555 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2556 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2557 | mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2558 | getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"), |
| 2559 | mParameters.orientationAware); |
| 2560 | |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2561 | mParameters.associatedDisplayId = -1; |
| 2562 | mParameters.associatedDisplayIsExternal = false; |
| 2563 | if (mParameters.orientationAware |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2564 | || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2565 | || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) { |
| 2566 | mParameters.associatedDisplayIsExternal = |
| 2567 | mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN |
| 2568 | && getDevice()->isExternal(); |
| 2569 | mParameters.associatedDisplayId = 0; |
| 2570 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2571 | } |
| 2572 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2573 | void TouchInputMapper::dumpParameters(String8& dump) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2574 | dump.append(INDENT3 "Parameters:\n"); |
| 2575 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2576 | switch (mParameters.gestureMode) { |
| 2577 | case Parameters::GESTURE_MODE_POINTER: |
| 2578 | dump.append(INDENT4 "GestureMode: pointer\n"); |
| 2579 | break; |
| 2580 | case Parameters::GESTURE_MODE_SPOTS: |
| 2581 | dump.append(INDENT4 "GestureMode: spots\n"); |
| 2582 | break; |
| 2583 | default: |
| 2584 | assert(false); |
| 2585 | } |
| 2586 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2587 | switch (mParameters.deviceType) { |
| 2588 | case Parameters::DEVICE_TYPE_TOUCH_SCREEN: |
| 2589 | dump.append(INDENT4 "DeviceType: touchScreen\n"); |
| 2590 | break; |
| 2591 | case Parameters::DEVICE_TYPE_TOUCH_PAD: |
| 2592 | dump.append(INDENT4 "DeviceType: touchPad\n"); |
| 2593 | break; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2594 | case Parameters::DEVICE_TYPE_POINTER: |
| 2595 | dump.append(INDENT4 "DeviceType: pointer\n"); |
| 2596 | break; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2597 | default: |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 2598 | LOG_ASSERT(false); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2599 | } |
| 2600 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2601 | dump.appendFormat(INDENT4 "AssociatedDisplay: id=%d, isExternal=%s\n", |
| 2602 | mParameters.associatedDisplayId, toString(mParameters.associatedDisplayIsExternal)); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2603 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 2604 | toString(mParameters.orientationAware)); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2605 | } |
| 2606 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2607 | void TouchInputMapper::configureRawPointerAxes() { |
| 2608 | mRawPointerAxes.clear(); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2609 | } |
| 2610 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2611 | void TouchInputMapper::dumpRawPointerAxes(String8& dump) { |
| 2612 | dump.append(INDENT3 "Raw Touch Axes:\n"); |
| 2613 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X"); |
| 2614 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y"); |
| 2615 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure"); |
| 2616 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor"); |
| 2617 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor"); |
| 2618 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor"); |
| 2619 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor"); |
| 2620 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation"); |
| 2621 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance"); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2622 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX"); |
| 2623 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY"); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2624 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId"); |
| 2625 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot"); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2626 | } |
| 2627 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2628 | void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) { |
| 2629 | int32_t oldDeviceMode = mDeviceMode; |
| 2630 | |
| 2631 | // Determine device mode. |
| 2632 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER |
| 2633 | && mConfig.pointerGesturesEnabled) { |
| 2634 | mSource = AINPUT_SOURCE_MOUSE; |
| 2635 | mDeviceMode = DEVICE_MODE_POINTER; |
| 2636 | } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN |
| 2637 | && mParameters.associatedDisplayId >= 0) { |
| 2638 | mSource = AINPUT_SOURCE_TOUCHSCREEN; |
| 2639 | mDeviceMode = DEVICE_MODE_DIRECT; |
| 2640 | } else { |
| 2641 | mSource = AINPUT_SOURCE_TOUCHPAD; |
| 2642 | mDeviceMode = DEVICE_MODE_UNSCALED; |
| 2643 | } |
| 2644 | |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2645 | // Ensure we have valid X and Y axes. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2646 | if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) { |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2647 | LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! " |
| 2648 | "The device will be inoperable.", getDeviceName().string()); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2649 | mDeviceMode = DEVICE_MODE_DISABLED; |
| 2650 | return; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2651 | } |
| 2652 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2653 | // Get associated display dimensions. |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2654 | if (mParameters.associatedDisplayId >= 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2655 | if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId, |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2656 | mParameters.associatedDisplayIsExternal, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2657 | &mAssociatedDisplayWidth, &mAssociatedDisplayHeight, |
| 2658 | &mAssociatedDisplayOrientation)) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2659 | LOGI(INDENT "Touch device '%s' could not query the properties of its associated " |
| 2660 | "display %d. The device will be inoperable until the display size " |
| 2661 | "becomes available.", |
| 2662 | getDeviceName().string(), mParameters.associatedDisplayId); |
| 2663 | mDeviceMode = DEVICE_MODE_DISABLED; |
| 2664 | return; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2665 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2666 | } |
| 2667 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2668 | // Configure dimensions. |
| 2669 | int32_t width, height, orientation; |
| 2670 | if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) { |
| 2671 | width = mAssociatedDisplayWidth; |
| 2672 | height = mAssociatedDisplayHeight; |
| 2673 | orientation = mParameters.orientationAware ? |
| 2674 | mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0; |
| 2675 | } else { |
| 2676 | width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1; |
| 2677 | height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1; |
| 2678 | orientation = DISPLAY_ORIENTATION_0; |
| 2679 | } |
| 2680 | |
| 2681 | // If moving between pointer modes, need to reset some state. |
| 2682 | bool deviceModeChanged; |
| 2683 | if (mDeviceMode != oldDeviceMode) { |
| 2684 | deviceModeChanged = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2685 | mOrientedRanges.clear(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2686 | } |
| 2687 | |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame^] | 2688 | // Create pointer controller if needed. |
| 2689 | if (mDeviceMode == DEVICE_MODE_POINTER || |
| 2690 | (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) { |
| 2691 | if (mPointerController == NULL) { |
| 2692 | mPointerController = getPolicy()->obtainPointerController(getDeviceId()); |
| 2693 | } |
| 2694 | } else { |
| 2695 | mPointerController.clear(); |
| 2696 | } |
| 2697 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2698 | bool orientationChanged = mSurfaceOrientation != orientation; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2699 | if (orientationChanged) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2700 | mSurfaceOrientation = orientation; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2701 | } |
| 2702 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2703 | bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2704 | if (sizeChanged || deviceModeChanged) { |
| 2705 | LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d, mode is %d", |
| 2706 | getDeviceId(), getDeviceName().string(), width, height, mDeviceMode); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2707 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2708 | mSurfaceWidth = width; |
| 2709 | mSurfaceHeight = height; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2710 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2711 | // Configure X and Y factors. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2712 | mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1); |
| 2713 | mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1); |
| 2714 | mXPrecision = 1.0f / mXScale; |
| 2715 | mYPrecision = 1.0f / mYScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2716 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2717 | mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2718 | mOrientedRanges.x.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2719 | mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2720 | mOrientedRanges.y.source = mSource; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2721 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2722 | configureVirtualKeys(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2723 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2724 | // Scale factor for terms that are not oriented in a particular axis. |
| 2725 | // If the pixels are square then xScale == yScale otherwise we fake it |
| 2726 | // by choosing an average. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2727 | mGeometricScale = avg(mXScale, mYScale); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2728 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2729 | // Size of diagonal axis. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2730 | float diagonalSize = hypotf(width, height); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2731 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 2732 | // Size factors. |
| 2733 | if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) { |
| 2734 | if (mRawPointerAxes.touchMajor.valid |
| 2735 | && mRawPointerAxes.touchMajor.maxValue != 0) { |
| 2736 | mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue; |
| 2737 | } else if (mRawPointerAxes.toolMajor.valid |
| 2738 | && mRawPointerAxes.toolMajor.maxValue != 0) { |
| 2739 | mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue; |
| 2740 | } else { |
| 2741 | mSizeScale = 0.0f; |
| 2742 | } |
| 2743 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2744 | mOrientedRanges.haveTouchSize = true; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 2745 | mOrientedRanges.haveToolSize = true; |
| 2746 | mOrientedRanges.haveSize = true; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2747 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2748 | mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2749 | mOrientedRanges.touchMajor.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2750 | mOrientedRanges.touchMajor.min = 0; |
| 2751 | mOrientedRanges.touchMajor.max = diagonalSize; |
| 2752 | mOrientedRanges.touchMajor.flat = 0; |
| 2753 | mOrientedRanges.touchMajor.fuzz = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2754 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2755 | mOrientedRanges.touchMinor = mOrientedRanges.touchMajor; |
| 2756 | mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2757 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2758 | mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2759 | mOrientedRanges.toolMajor.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2760 | mOrientedRanges.toolMajor.min = 0; |
| 2761 | mOrientedRanges.toolMajor.max = diagonalSize; |
| 2762 | mOrientedRanges.toolMajor.flat = 0; |
| 2763 | mOrientedRanges.toolMajor.fuzz = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2764 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2765 | mOrientedRanges.toolMinor = mOrientedRanges.toolMajor; |
| 2766 | mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 2767 | |
| 2768 | mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2769 | mOrientedRanges.size.source = mSource; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 2770 | mOrientedRanges.size.min = 0; |
| 2771 | mOrientedRanges.size.max = 1.0; |
| 2772 | mOrientedRanges.size.flat = 0; |
| 2773 | mOrientedRanges.size.fuzz = 0; |
| 2774 | } else { |
| 2775 | mSizeScale = 0.0f; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2776 | } |
| 2777 | |
| 2778 | // Pressure factors. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2779 | mPressureScale = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2780 | if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL |
| 2781 | || mCalibration.pressureCalibration |
| 2782 | == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) { |
| 2783 | if (mCalibration.havePressureScale) { |
| 2784 | mPressureScale = mCalibration.pressureScale; |
| 2785 | } else if (mRawPointerAxes.pressure.valid |
| 2786 | && mRawPointerAxes.pressure.maxValue != 0) { |
| 2787 | mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2788 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2789 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2790 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2791 | mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE; |
| 2792 | mOrientedRanges.pressure.source = mSource; |
| 2793 | mOrientedRanges.pressure.min = 0; |
| 2794 | mOrientedRanges.pressure.max = 1.0; |
| 2795 | mOrientedRanges.pressure.flat = 0; |
| 2796 | mOrientedRanges.pressure.fuzz = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2797 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2798 | // Tilt |
| 2799 | mTiltXCenter = 0; |
| 2800 | mTiltXScale = 0; |
| 2801 | mTiltYCenter = 0; |
| 2802 | mTiltYScale = 0; |
| 2803 | mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid; |
| 2804 | if (mHaveTilt) { |
| 2805 | mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue, |
| 2806 | mRawPointerAxes.tiltX.maxValue); |
| 2807 | mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue, |
| 2808 | mRawPointerAxes.tiltY.maxValue); |
| 2809 | mTiltXScale = M_PI / 180; |
| 2810 | mTiltYScale = M_PI / 180; |
| 2811 | |
| 2812 | mOrientedRanges.haveTilt = true; |
| 2813 | |
| 2814 | mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT; |
| 2815 | mOrientedRanges.tilt.source = mSource; |
| 2816 | mOrientedRanges.tilt.min = 0; |
| 2817 | mOrientedRanges.tilt.max = M_PI_2; |
| 2818 | mOrientedRanges.tilt.flat = 0; |
| 2819 | mOrientedRanges.tilt.fuzz = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2820 | } |
| 2821 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2822 | // Orientation |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2823 | mOrientationCenter = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2824 | mOrientationScale = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2825 | if (mHaveTilt) { |
| 2826 | mOrientedRanges.haveOrientation = true; |
| 2827 | |
| 2828 | mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION; |
| 2829 | mOrientedRanges.orientation.source = mSource; |
| 2830 | mOrientedRanges.orientation.min = -M_PI; |
| 2831 | mOrientedRanges.orientation.max = M_PI; |
| 2832 | mOrientedRanges.orientation.flat = 0; |
| 2833 | mOrientedRanges.orientation.fuzz = 0; |
| 2834 | } else if (mCalibration.orientationCalibration != |
| 2835 | Calibration::ORIENTATION_CALIBRATION_NONE) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2836 | if (mCalibration.orientationCalibration |
| 2837 | == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2838 | if (mRawPointerAxes.orientation.valid) { |
| 2839 | mOrientationCenter = avg(mRawPointerAxes.orientation.minValue, |
| 2840 | mRawPointerAxes.orientation.maxValue); |
| 2841 | mOrientationScale = M_PI / (mRawPointerAxes.orientation.maxValue - |
| 2842 | mRawPointerAxes.orientation.minValue); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2843 | } |
| 2844 | } |
| 2845 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2846 | mOrientedRanges.haveOrientation = true; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2847 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2848 | mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2849 | mOrientedRanges.orientation.source = mSource; |
| 2850 | mOrientedRanges.orientation.min = -M_PI_2; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2851 | mOrientedRanges.orientation.max = M_PI_2; |
| 2852 | mOrientedRanges.orientation.flat = 0; |
| 2853 | mOrientedRanges.orientation.fuzz = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2854 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2855 | |
| 2856 | // Distance |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2857 | mDistanceScale = 0; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2858 | if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) { |
| 2859 | if (mCalibration.distanceCalibration |
| 2860 | == Calibration::DISTANCE_CALIBRATION_SCALED) { |
| 2861 | if (mCalibration.haveDistanceScale) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2862 | mDistanceScale = mCalibration.distanceScale; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2863 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2864 | mDistanceScale = 1.0f; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2865 | } |
| 2866 | } |
| 2867 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2868 | mOrientedRanges.haveDistance = true; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2869 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2870 | mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2871 | mOrientedRanges.distance.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2872 | mOrientedRanges.distance.min = |
| 2873 | mRawPointerAxes.distance.minValue * mDistanceScale; |
| 2874 | mOrientedRanges.distance.max = |
| 2875 | mRawPointerAxes.distance.minValue * mDistanceScale; |
| 2876 | mOrientedRanges.distance.flat = 0; |
| 2877 | mOrientedRanges.distance.fuzz = |
| 2878 | mRawPointerAxes.distance.fuzz * mDistanceScale; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2879 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2880 | } |
| 2881 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2882 | if (orientationChanged || sizeChanged || deviceModeChanged) { |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2883 | // Compute oriented surface dimensions, precision, scales and ranges. |
| 2884 | // Note that the maximum value reported is an inclusive maximum value so it is one |
| 2885 | // unit less than the total width or height of surface. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2886 | switch (mSurfaceOrientation) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 2887 | case DISPLAY_ORIENTATION_90: |
| 2888 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2889 | mOrientedSurfaceWidth = mSurfaceHeight; |
| 2890 | mOrientedSurfaceHeight = mSurfaceWidth; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2891 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2892 | mOrientedXPrecision = mYPrecision; |
| 2893 | mOrientedYPrecision = mXPrecision; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2894 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2895 | mOrientedRanges.x.min = 0; |
| 2896 | mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue) |
| 2897 | * mYScale; |
| 2898 | mOrientedRanges.x.flat = 0; |
| 2899 | mOrientedRanges.x.fuzz = mYScale; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2900 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2901 | mOrientedRanges.y.min = 0; |
| 2902 | mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue) |
| 2903 | * mXScale; |
| 2904 | mOrientedRanges.y.flat = 0; |
| 2905 | mOrientedRanges.y.fuzz = mXScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2906 | break; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2907 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2908 | default: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2909 | mOrientedSurfaceWidth = mSurfaceWidth; |
| 2910 | mOrientedSurfaceHeight = mSurfaceHeight; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2911 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2912 | mOrientedXPrecision = mXPrecision; |
| 2913 | mOrientedYPrecision = mYPrecision; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2914 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2915 | mOrientedRanges.x.min = 0; |
| 2916 | mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue) |
| 2917 | * mXScale; |
| 2918 | mOrientedRanges.x.flat = 0; |
| 2919 | mOrientedRanges.x.fuzz = mXScale; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2920 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2921 | mOrientedRanges.y.min = 0; |
| 2922 | mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue) |
| 2923 | * mYScale; |
| 2924 | mOrientedRanges.y.flat = 0; |
| 2925 | mOrientedRanges.y.fuzz = mYScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2926 | break; |
| 2927 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2928 | |
| 2929 | // Compute pointer gesture detection parameters. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2930 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2931 | int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1; |
| 2932 | int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2933 | float rawDiagonal = hypotf(rawWidth, rawHeight); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2934 | float displayDiagonal = hypotf(mAssociatedDisplayWidth, |
| 2935 | mAssociatedDisplayHeight); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2936 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2937 | // 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] | 2938 | // given area relative to the diagonal size of the display when no acceleration |
| 2939 | // is applied. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2940 | // Assume that the touch pad has a square aspect ratio such that movements in |
| 2941 | // 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] | 2942 | mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2943 | * displayDiagonal / rawDiagonal; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2944 | mPointerYMovementScale = mPointerXMovementScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2945 | |
| 2946 | // Scale zooms to cover a smaller range of the display than movements do. |
| 2947 | // This value determines the area around the pointer that is affected by freeform |
| 2948 | // pointer gestures. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2949 | mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2950 | * displayDiagonal / rawDiagonal; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2951 | mPointerYZoomScale = mPointerXZoomScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2952 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2953 | // Max width between pointers to detect a swipe gesture is more than some fraction |
| 2954 | // of the diagonal axis of the touch pad. Touches that are wider than this are |
| 2955 | // translated into freeform gestures. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2956 | mPointerGestureMaxSwipeWidth = |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2957 | mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2958 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2959 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2960 | // Abort current pointer usages because the state has changed. |
| 2961 | abortPointerUsage(when, 0 /*policyFlags*/); |
| 2962 | |
| 2963 | // Inform the dispatcher about the changes. |
| 2964 | *outResetNeeded = true; |
| 2965 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2966 | } |
| 2967 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2968 | void TouchInputMapper::dumpSurface(String8& dump) { |
| 2969 | dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth); |
| 2970 | dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight); |
| 2971 | dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2972 | } |
| 2973 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2974 | void TouchInputMapper::configureVirtualKeys() { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2975 | Vector<VirtualKeyDefinition> virtualKeyDefinitions; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 2976 | getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2977 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2978 | mVirtualKeys.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2979 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2980 | if (virtualKeyDefinitions.size() == 0) { |
| 2981 | return; |
| 2982 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2983 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2984 | mVirtualKeys.setCapacity(virtualKeyDefinitions.size()); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2985 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2986 | int32_t touchScreenLeft = mRawPointerAxes.x.minValue; |
| 2987 | int32_t touchScreenTop = mRawPointerAxes.y.minValue; |
| 2988 | int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1; |
| 2989 | int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2990 | |
| 2991 | for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2992 | const VirtualKeyDefinition& virtualKeyDefinition = |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2993 | virtualKeyDefinitions[i]; |
| 2994 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2995 | mVirtualKeys.add(); |
| 2996 | VirtualKey& virtualKey = mVirtualKeys.editTop(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2997 | |
| 2998 | virtualKey.scanCode = virtualKeyDefinition.scanCode; |
| 2999 | int32_t keyCode; |
| 3000 | uint32_t flags; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 3001 | if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3002 | & keyCode, & flags)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3003 | LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", |
| 3004 | virtualKey.scanCode); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3005 | mVirtualKeys.pop(); // drop the key |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3006 | continue; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3007 | } |
| 3008 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3009 | virtualKey.keyCode = keyCode; |
| 3010 | virtualKey.flags = flags; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3011 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3012 | // convert the key definition's display coordinates into touch coordinates for a hit box |
| 3013 | int32_t halfWidth = virtualKeyDefinition.width / 2; |
| 3014 | int32_t halfHeight = virtualKeyDefinition.height / 2; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3015 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3016 | virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3017 | * touchScreenWidth / mSurfaceWidth + touchScreenLeft; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3018 | virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3019 | * touchScreenWidth / mSurfaceWidth + touchScreenLeft; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3020 | virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3021 | * touchScreenHeight / mSurfaceHeight + touchScreenTop; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3022 | virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3023 | * touchScreenHeight / mSurfaceHeight + touchScreenTop; |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3024 | } |
| 3025 | } |
| 3026 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3027 | void TouchInputMapper::dumpVirtualKeys(String8& dump) { |
| 3028 | if (!mVirtualKeys.isEmpty()) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3029 | dump.append(INDENT3 "Virtual Keys:\n"); |
| 3030 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3031 | for (size_t i = 0; i < mVirtualKeys.size(); i++) { |
| 3032 | const VirtualKey& virtualKey = mVirtualKeys.itemAt(i); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3033 | dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, " |
| 3034 | "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n", |
| 3035 | i, virtualKey.scanCode, virtualKey.keyCode, |
| 3036 | virtualKey.hitLeft, virtualKey.hitRight, |
| 3037 | virtualKey.hitTop, virtualKey.hitBottom); |
| 3038 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3039 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3040 | } |
| 3041 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3042 | void TouchInputMapper::parseCalibration() { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 3043 | const PropertyMap& in = getDevice()->getConfiguration(); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3044 | Calibration& out = mCalibration; |
| 3045 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3046 | // Size |
| 3047 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT; |
| 3048 | String8 sizeCalibrationString; |
| 3049 | if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) { |
| 3050 | if (sizeCalibrationString == "none") { |
| 3051 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; |
| 3052 | } else if (sizeCalibrationString == "geometric") { |
| 3053 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC; |
| 3054 | } else if (sizeCalibrationString == "diameter") { |
| 3055 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER; |
| 3056 | } else if (sizeCalibrationString == "area") { |
| 3057 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA; |
| 3058 | } else if (sizeCalibrationString != "default") { |
| 3059 | LOGW("Invalid value for touch.size.calibration: '%s'", |
| 3060 | sizeCalibrationString.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3061 | } |
| 3062 | } |
| 3063 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3064 | out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"), |
| 3065 | out.sizeScale); |
| 3066 | out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"), |
| 3067 | out.sizeBias); |
| 3068 | out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"), |
| 3069 | out.sizeIsSummed); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3070 | |
| 3071 | // Pressure |
| 3072 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT; |
| 3073 | String8 pressureCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 3074 | if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3075 | if (pressureCalibrationString == "none") { |
| 3076 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; |
| 3077 | } else if (pressureCalibrationString == "physical") { |
| 3078 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL; |
| 3079 | } else if (pressureCalibrationString == "amplitude") { |
| 3080 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE; |
| 3081 | } else if (pressureCalibrationString != "default") { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 3082 | LOGW("Invalid value for touch.pressure.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3083 | pressureCalibrationString.string()); |
| 3084 | } |
| 3085 | } |
| 3086 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3087 | out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"), |
| 3088 | out.pressureScale); |
| 3089 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3090 | // Orientation |
| 3091 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT; |
| 3092 | String8 orientationCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 3093 | if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3094 | if (orientationCalibrationString == "none") { |
| 3095 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; |
| 3096 | } else if (orientationCalibrationString == "interpolated") { |
| 3097 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 3098 | } else if (orientationCalibrationString == "vector") { |
| 3099 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3100 | } else if (orientationCalibrationString != "default") { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 3101 | LOGW("Invalid value for touch.orientation.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3102 | orientationCalibrationString.string()); |
| 3103 | } |
| 3104 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3105 | |
| 3106 | // Distance |
| 3107 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT; |
| 3108 | String8 distanceCalibrationString; |
| 3109 | if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) { |
| 3110 | if (distanceCalibrationString == "none") { |
| 3111 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE; |
| 3112 | } else if (distanceCalibrationString == "scaled") { |
| 3113 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED; |
| 3114 | } else if (distanceCalibrationString != "default") { |
| 3115 | LOGW("Invalid value for touch.distance.calibration: '%s'", |
| 3116 | distanceCalibrationString.string()); |
| 3117 | } |
| 3118 | } |
| 3119 | |
| 3120 | out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"), |
| 3121 | out.distanceScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3122 | } |
| 3123 | |
| 3124 | void TouchInputMapper::resolveCalibration() { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3125 | // Size |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3126 | if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) { |
| 3127 | if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) { |
| 3128 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3129 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3130 | } else { |
| 3131 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; |
| 3132 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3133 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3134 | // Pressure |
| 3135 | if (mRawPointerAxes.pressure.valid) { |
| 3136 | if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) { |
| 3137 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL; |
| 3138 | } |
| 3139 | } else { |
| 3140 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3141 | } |
| 3142 | |
| 3143 | // Orientation |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3144 | if (mRawPointerAxes.orientation.valid) { |
| 3145 | if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3146 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3147 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3148 | } else { |
| 3149 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3150 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3151 | |
| 3152 | // Distance |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3153 | if (mRawPointerAxes.distance.valid) { |
| 3154 | if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3155 | mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3156 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3157 | } else { |
| 3158 | mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3159 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3160 | } |
| 3161 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3162 | void TouchInputMapper::dumpCalibration(String8& dump) { |
| 3163 | dump.append(INDENT3 "Calibration:\n"); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3164 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3165 | // Size |
| 3166 | switch (mCalibration.sizeCalibration) { |
| 3167 | case Calibration::SIZE_CALIBRATION_NONE: |
| 3168 | dump.append(INDENT4 "touch.size.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3169 | break; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3170 | case Calibration::SIZE_CALIBRATION_GEOMETRIC: |
| 3171 | dump.append(INDENT4 "touch.size.calibration: geometric\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3172 | break; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3173 | case Calibration::SIZE_CALIBRATION_DIAMETER: |
| 3174 | dump.append(INDENT4 "touch.size.calibration: diameter\n"); |
| 3175 | break; |
| 3176 | case Calibration::SIZE_CALIBRATION_AREA: |
| 3177 | dump.append(INDENT4 "touch.size.calibration: area\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3178 | break; |
| 3179 | default: |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 3180 | LOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3181 | } |
| 3182 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3183 | if (mCalibration.haveSizeScale) { |
| 3184 | dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n", |
| 3185 | mCalibration.sizeScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3186 | } |
| 3187 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3188 | if (mCalibration.haveSizeBias) { |
| 3189 | dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n", |
| 3190 | mCalibration.sizeBias); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3191 | } |
| 3192 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3193 | if (mCalibration.haveSizeIsSummed) { |
| 3194 | dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n", |
| 3195 | toString(mCalibration.sizeIsSummed)); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3196 | } |
| 3197 | |
| 3198 | // Pressure |
| 3199 | switch (mCalibration.pressureCalibration) { |
| 3200 | case Calibration::PRESSURE_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3201 | dump.append(INDENT4 "touch.pressure.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3202 | break; |
| 3203 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3204 | dump.append(INDENT4 "touch.pressure.calibration: physical\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3205 | break; |
| 3206 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3207 | dump.append(INDENT4 "touch.pressure.calibration: amplitude\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3208 | break; |
| 3209 | default: |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 3210 | LOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3211 | } |
| 3212 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3213 | if (mCalibration.havePressureScale) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3214 | dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n", |
| 3215 | mCalibration.pressureScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3216 | } |
| 3217 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3218 | // Orientation |
| 3219 | switch (mCalibration.orientationCalibration) { |
| 3220 | case Calibration::ORIENTATION_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3221 | dump.append(INDENT4 "touch.orientation.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3222 | break; |
| 3223 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3224 | dump.append(INDENT4 "touch.orientation.calibration: interpolated\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3225 | break; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 3226 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: |
| 3227 | dump.append(INDENT4 "touch.orientation.calibration: vector\n"); |
| 3228 | break; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3229 | default: |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 3230 | LOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3231 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3232 | |
| 3233 | // Distance |
| 3234 | switch (mCalibration.distanceCalibration) { |
| 3235 | case Calibration::DISTANCE_CALIBRATION_NONE: |
| 3236 | dump.append(INDENT4 "touch.distance.calibration: none\n"); |
| 3237 | break; |
| 3238 | case Calibration::DISTANCE_CALIBRATION_SCALED: |
| 3239 | dump.append(INDENT4 "touch.distance.calibration: scaled\n"); |
| 3240 | break; |
| 3241 | default: |
| 3242 | LOG_ASSERT(false); |
| 3243 | } |
| 3244 | |
| 3245 | if (mCalibration.haveDistanceScale) { |
| 3246 | dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n", |
| 3247 | mCalibration.distanceScale); |
| 3248 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3249 | } |
| 3250 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3251 | void TouchInputMapper::reset(nsecs_t when) { |
| 3252 | mCursorButtonAccumulator.reset(getDevice()); |
| 3253 | mCursorScrollAccumulator.reset(getDevice()); |
| 3254 | mTouchButtonAccumulator.reset(getDevice()); |
| 3255 | |
| 3256 | mPointerVelocityControl.reset(); |
| 3257 | mWheelXVelocityControl.reset(); |
| 3258 | mWheelYVelocityControl.reset(); |
| 3259 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3260 | mCurrentRawPointerData.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3261 | mLastRawPointerData.clear(); |
| 3262 | mCurrentCookedPointerData.clear(); |
| 3263 | mLastCookedPointerData.clear(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3264 | mCurrentButtonState = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3265 | mLastButtonState = 0; |
| 3266 | mCurrentRawVScroll = 0; |
| 3267 | mCurrentRawHScroll = 0; |
| 3268 | mCurrentFingerIdBits.clear(); |
| 3269 | mLastFingerIdBits.clear(); |
| 3270 | mCurrentStylusIdBits.clear(); |
| 3271 | mLastStylusIdBits.clear(); |
| 3272 | mCurrentMouseIdBits.clear(); |
| 3273 | mLastMouseIdBits.clear(); |
| 3274 | mPointerUsage = POINTER_USAGE_NONE; |
| 3275 | mSentHoverEnter = false; |
| 3276 | mDownTime = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3277 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3278 | mCurrentVirtualKey.down = false; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3279 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3280 | mPointerGesture.reset(); |
| 3281 | mPointerSimple.reset(); |
| 3282 | |
| 3283 | if (mPointerController != NULL) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3284 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 3285 | mPointerController->clearSpots(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3286 | } |
| 3287 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3288 | InputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3289 | } |
| 3290 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3291 | void TouchInputMapper::process(const RawEvent* rawEvent) { |
| 3292 | mCursorButtonAccumulator.process(rawEvent); |
| 3293 | mCursorScrollAccumulator.process(rawEvent); |
| 3294 | mTouchButtonAccumulator.process(rawEvent); |
| 3295 | |
| 3296 | if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) { |
| 3297 | sync(rawEvent->when); |
| 3298 | } |
| 3299 | } |
| 3300 | |
| 3301 | void TouchInputMapper::sync(nsecs_t when) { |
| 3302 | // Sync button state. |
| 3303 | mCurrentButtonState = mTouchButtonAccumulator.getButtonState() |
| 3304 | | mCursorButtonAccumulator.getButtonState(); |
| 3305 | |
| 3306 | // Sync scroll state. |
| 3307 | mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel(); |
| 3308 | mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel(); |
| 3309 | mCursorScrollAccumulator.finishSync(); |
| 3310 | |
| 3311 | // Sync touch state. |
| 3312 | bool havePointerIds = true; |
| 3313 | mCurrentRawPointerData.clear(); |
| 3314 | syncTouch(when, &havePointerIds); |
| 3315 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 3316 | #if DEBUG_RAW_EVENTS |
| 3317 | if (!havePointerIds) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3318 | LOGD("syncTouch: pointerCount %d -> %d, no pointer ids", |
| 3319 | mLastRawPointerData.pointerCount, |
| 3320 | mCurrentRawPointerData.pointerCount); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 3321 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3322 | LOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, " |
| 3323 | "hovering ids 0x%08x -> 0x%08x", |
| 3324 | mLastRawPointerData.pointerCount, |
| 3325 | mCurrentRawPointerData.pointerCount, |
| 3326 | mLastRawPointerData.touchingIdBits.value, |
| 3327 | mCurrentRawPointerData.touchingIdBits.value, |
| 3328 | mLastRawPointerData.hoveringIdBits.value, |
| 3329 | mCurrentRawPointerData.hoveringIdBits.value); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 3330 | } |
| 3331 | #endif |
| 3332 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3333 | // Reset state that we will compute below. |
| 3334 | mCurrentFingerIdBits.clear(); |
| 3335 | mCurrentStylusIdBits.clear(); |
| 3336 | mCurrentMouseIdBits.clear(); |
| 3337 | mCurrentCookedPointerData.clear(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3338 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3339 | if (mDeviceMode == DEVICE_MODE_DISABLED) { |
| 3340 | // Drop all input if the device is disabled. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3341 | mCurrentRawPointerData.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3342 | mCurrentButtonState = 0; |
| 3343 | } else { |
| 3344 | // Preprocess pointer data. |
| 3345 | if (!havePointerIds) { |
| 3346 | assignPointerIds(); |
| 3347 | } |
| 3348 | |
| 3349 | // Handle policy on initial down or hover events. |
| 3350 | uint32_t policyFlags = 0; |
Jeff Brown | c28306a | 2011-08-23 21:32:42 -0700 | [diff] [blame] | 3351 | bool initialDown = mLastRawPointerData.pointerCount == 0 |
| 3352 | && mCurrentRawPointerData.pointerCount != 0; |
| 3353 | bool buttonsPressed = mCurrentButtonState & ~mLastButtonState; |
| 3354 | if (initialDown || buttonsPressed) { |
| 3355 | // 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] | 3356 | if (mDeviceMode == DEVICE_MODE_DIRECT) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3357 | getContext()->fadePointer(); |
| 3358 | } |
| 3359 | |
| 3360 | // Initial downs on external touch devices should wake the device. |
| 3361 | // We don't do this for internal touch screens to prevent them from waking |
| 3362 | // up in your pocket. |
| 3363 | // TODO: Use the input device configuration to control this behavior more finely. |
| 3364 | if (getDevice()->isExternal()) { |
| 3365 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 3366 | } |
| 3367 | } |
| 3368 | |
| 3369 | // Synthesize key down from raw buttons if needed. |
| 3370 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource, |
| 3371 | policyFlags, mLastButtonState, mCurrentButtonState); |
| 3372 | |
| 3373 | // Consume raw off-screen touches before cooking pointer data. |
| 3374 | // If touches are consumed, subsequent code will not receive any pointer data. |
| 3375 | if (consumeRawTouches(when, policyFlags)) { |
| 3376 | mCurrentRawPointerData.clear(); |
| 3377 | } |
| 3378 | |
| 3379 | // Cook pointer data. This call populates the mCurrentCookedPointerData structure |
| 3380 | // with cooked pointer data that has the same ids and indices as the raw data. |
| 3381 | // The following code can use either the raw or cooked data, as needed. |
| 3382 | cookPointerData(); |
| 3383 | |
| 3384 | // 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^] | 3385 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3386 | for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) { |
| 3387 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 3388 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3389 | if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS |
| 3390 | || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) { |
| 3391 | mCurrentStylusIdBits.markBit(id); |
| 3392 | } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER |
| 3393 | || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 3394 | mCurrentFingerIdBits.markBit(id); |
| 3395 | } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) { |
| 3396 | mCurrentMouseIdBits.markBit(id); |
| 3397 | } |
| 3398 | } |
| 3399 | for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) { |
| 3400 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 3401 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3402 | if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS |
| 3403 | || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) { |
| 3404 | mCurrentStylusIdBits.markBit(id); |
| 3405 | } |
| 3406 | } |
| 3407 | |
| 3408 | // Stylus takes precedence over all tools, then mouse, then finger. |
| 3409 | PointerUsage pointerUsage = mPointerUsage; |
| 3410 | if (!mCurrentStylusIdBits.isEmpty()) { |
| 3411 | mCurrentMouseIdBits.clear(); |
| 3412 | mCurrentFingerIdBits.clear(); |
| 3413 | pointerUsage = POINTER_USAGE_STYLUS; |
| 3414 | } else if (!mCurrentMouseIdBits.isEmpty()) { |
| 3415 | mCurrentFingerIdBits.clear(); |
| 3416 | pointerUsage = POINTER_USAGE_MOUSE; |
| 3417 | } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) { |
| 3418 | pointerUsage = POINTER_USAGE_GESTURES; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3419 | } |
| 3420 | |
| 3421 | dispatchPointerUsage(when, policyFlags, pointerUsage); |
| 3422 | } else { |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame^] | 3423 | if (mDeviceMode == DEVICE_MODE_DIRECT |
| 3424 | && mConfig.showTouches && mPointerController != NULL) { |
| 3425 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT); |
| 3426 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 3427 | |
| 3428 | mPointerController->setButtonState(mCurrentButtonState); |
| 3429 | mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords, |
| 3430 | mCurrentCookedPointerData.idToIndex, |
| 3431 | mCurrentCookedPointerData.touchingIdBits); |
| 3432 | } |
| 3433 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3434 | dispatchHoverExit(when, policyFlags); |
| 3435 | dispatchTouches(when, policyFlags); |
| 3436 | dispatchHoverEnterAndMove(when, policyFlags); |
| 3437 | } |
| 3438 | |
| 3439 | // Synthesize key up from raw buttons if needed. |
| 3440 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource, |
| 3441 | policyFlags, mLastButtonState, mCurrentButtonState); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3442 | } |
| 3443 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3444 | // Copy current touch to last touch in preparation for the next cycle. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3445 | mLastRawPointerData.copyFrom(mCurrentRawPointerData); |
| 3446 | mLastCookedPointerData.copyFrom(mCurrentCookedPointerData); |
| 3447 | mLastButtonState = mCurrentButtonState; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3448 | mLastFingerIdBits = mCurrentFingerIdBits; |
| 3449 | mLastStylusIdBits = mCurrentStylusIdBits; |
| 3450 | mLastMouseIdBits = mCurrentMouseIdBits; |
| 3451 | |
| 3452 | // Clear some transient state. |
| 3453 | mCurrentRawVScroll = 0; |
| 3454 | mCurrentRawHScroll = 0; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3455 | } |
| 3456 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3457 | void TouchInputMapper::timeoutExpired(nsecs_t when) { |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame^] | 3458 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3459 | if (mPointerUsage == POINTER_USAGE_GESTURES) { |
| 3460 | dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/); |
| 3461 | } |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3462 | } |
| 3463 | } |
| 3464 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3465 | bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) { |
| 3466 | // Check for release of a virtual key. |
| 3467 | if (mCurrentVirtualKey.down) { |
| 3468 | if (mCurrentRawPointerData.touchingIdBits.isEmpty()) { |
| 3469 | // Pointer went up while virtual key was down. |
| 3470 | mCurrentVirtualKey.down = false; |
| 3471 | if (!mCurrentVirtualKey.ignored) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3472 | #if DEBUG_VIRTUAL_KEYS |
| 3473 | LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3474 | mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3475 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3476 | dispatchVirtualKey(when, policyFlags, |
| 3477 | AKEY_EVENT_ACTION_UP, |
| 3478 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3479 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3480 | return true; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3481 | } |
| 3482 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3483 | if (mCurrentRawPointerData.touchingIdBits.count() == 1) { |
| 3484 | uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit(); |
| 3485 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3486 | const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y); |
| 3487 | if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) { |
| 3488 | // Pointer is still within the space of the virtual key. |
| 3489 | return true; |
| 3490 | } |
| 3491 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3492 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3493 | // Pointer left virtual key area or another pointer also went down. |
| 3494 | // Send key cancellation but do not consume the touch yet. |
| 3495 | // This is useful when the user swipes through from the virtual key area |
| 3496 | // into the main display surface. |
| 3497 | mCurrentVirtualKey.down = false; |
| 3498 | if (!mCurrentVirtualKey.ignored) { |
| 3499 | #if DEBUG_VIRTUAL_KEYS |
| 3500 | LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d", |
| 3501 | mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode); |
| 3502 | #endif |
| 3503 | dispatchVirtualKey(when, policyFlags, |
| 3504 | AKEY_EVENT_ACTION_UP, |
| 3505 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
| 3506 | | AKEY_EVENT_FLAG_CANCELED); |
| 3507 | } |
| 3508 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3509 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3510 | if (mLastRawPointerData.touchingIdBits.isEmpty() |
| 3511 | && !mCurrentRawPointerData.touchingIdBits.isEmpty()) { |
| 3512 | // Pointer just went down. Check for virtual key press or off-screen touches. |
| 3513 | uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit(); |
| 3514 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3515 | if (!isPointInsideSurface(pointer.x, pointer.y)) { |
| 3516 | // If exactly one pointer went down, check for virtual key hit. |
| 3517 | // Otherwise we will drop the entire stroke. |
| 3518 | if (mCurrentRawPointerData.touchingIdBits.count() == 1) { |
| 3519 | const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y); |
| 3520 | if (virtualKey) { |
| 3521 | mCurrentVirtualKey.down = true; |
| 3522 | mCurrentVirtualKey.downTime = when; |
| 3523 | mCurrentVirtualKey.keyCode = virtualKey->keyCode; |
| 3524 | mCurrentVirtualKey.scanCode = virtualKey->scanCode; |
| 3525 | mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey( |
| 3526 | when, getDevice(), virtualKey->keyCode, virtualKey->scanCode); |
| 3527 | |
| 3528 | if (!mCurrentVirtualKey.ignored) { |
| 3529 | #if DEBUG_VIRTUAL_KEYS |
| 3530 | LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d", |
| 3531 | mCurrentVirtualKey.keyCode, |
| 3532 | mCurrentVirtualKey.scanCode); |
| 3533 | #endif |
| 3534 | dispatchVirtualKey(when, policyFlags, |
| 3535 | AKEY_EVENT_ACTION_DOWN, |
| 3536 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY); |
| 3537 | } |
| 3538 | } |
| 3539 | } |
| 3540 | return true; |
| 3541 | } |
| 3542 | } |
| 3543 | |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3544 | // 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] | 3545 | // most recent touch within the screen area. The idea is to filter out stray |
| 3546 | // virtual key presses when interacting with the touch screen. |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3547 | // |
| 3548 | // Problems we're trying to solve: |
| 3549 | // |
| 3550 | // 1. While scrolling a list or dragging the window shade, the user swipes down into a |
| 3551 | // virtual key area that is implemented by a separate touch panel and accidentally |
| 3552 | // triggers a virtual key. |
| 3553 | // |
| 3554 | // 2. While typing in the on screen keyboard, the user taps slightly outside the screen |
| 3555 | // area and accidentally triggers a virtual key. This often happens when virtual keys |
| 3556 | // are layed out below the screen near to where the on screen keyboard's space bar |
| 3557 | // is displayed. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3558 | if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 3559 | mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime); |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3560 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3561 | return false; |
| 3562 | } |
| 3563 | |
| 3564 | void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags, |
| 3565 | int32_t keyEventAction, int32_t keyEventFlags) { |
| 3566 | int32_t keyCode = mCurrentVirtualKey.keyCode; |
| 3567 | int32_t scanCode = mCurrentVirtualKey.scanCode; |
| 3568 | nsecs_t downTime = mCurrentVirtualKey.downTime; |
| 3569 | int32_t metaState = mContext->getGlobalMetaState(); |
| 3570 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3571 | |
| 3572 | NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags, |
| 3573 | keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime); |
| 3574 | getListener()->notifyKey(&args); |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3575 | } |
| 3576 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3577 | void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3578 | BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits; |
| 3579 | BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3580 | int32_t metaState = getContext()->getGlobalMetaState(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3581 | int32_t buttonState = mCurrentButtonState; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3582 | |
| 3583 | if (currentIdBits == lastIdBits) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3584 | if (!currentIdBits.isEmpty()) { |
| 3585 | // No pointer id changes so this is a move event. |
| 3586 | // 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] | 3587 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3588 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, |
| 3589 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 3590 | mCurrentCookedPointerData.pointerProperties, |
| 3591 | mCurrentCookedPointerData.pointerCoords, |
| 3592 | mCurrentCookedPointerData.idToIndex, |
| 3593 | currentIdBits, -1, |
| 3594 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 3595 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3596 | } else { |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3597 | // There may be pointers going up and pointers going down and pointers moving |
| 3598 | // all at the same time. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3599 | BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value); |
| 3600 | BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3601 | BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3602 | BitSet32 dispatchedIdBits(lastIdBits.value); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3603 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3604 | // Update last coordinates of pointers that have moved so that we observe the new |
| 3605 | // 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] | 3606 | bool moveNeeded = updateMovedPointers( |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3607 | mCurrentCookedPointerData.pointerProperties, |
| 3608 | mCurrentCookedPointerData.pointerCoords, |
| 3609 | mCurrentCookedPointerData.idToIndex, |
| 3610 | mLastCookedPointerData.pointerProperties, |
| 3611 | mLastCookedPointerData.pointerCoords, |
| 3612 | mLastCookedPointerData.idToIndex, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3613 | moveIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3614 | if (buttonState != mLastButtonState) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3615 | moveNeeded = true; |
| 3616 | } |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3617 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3618 | // Dispatch pointer up events. |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3619 | while (!upIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3620 | uint32_t upId = upIdBits.clearFirstMarkedBit(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3621 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3622 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3623 | AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3624 | mLastCookedPointerData.pointerProperties, |
| 3625 | mLastCookedPointerData.pointerCoords, |
| 3626 | mLastCookedPointerData.idToIndex, |
| 3627 | dispatchedIdBits, upId, |
| 3628 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3629 | dispatchedIdBits.clearBit(upId); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3630 | } |
| 3631 | |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3632 | // Dispatch move events if any of the remaining pointers moved from their old locations. |
| 3633 | // Although applications receive new locations as part of individual pointer up |
| 3634 | // events, they do not generally handle them except when presented in a move event. |
| 3635 | if (moveNeeded) { |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 3636 | LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value); |
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_MOVE, 0, metaState, buttonState, 0, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3639 | mCurrentCookedPointerData.pointerProperties, |
| 3640 | mCurrentCookedPointerData.pointerCoords, |
| 3641 | mCurrentCookedPointerData.idToIndex, |
| 3642 | dispatchedIdBits, -1, |
| 3643 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3644 | } |
| 3645 | |
| 3646 | // Dispatch pointer down events using the new pointer locations. |
| 3647 | while (!downIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3648 | uint32_t downId = downIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3649 | dispatchedIdBits.markBit(downId); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3650 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3651 | if (dispatchedIdBits.count() == 1) { |
| 3652 | // First pointer is going down. Set down time. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3653 | mDownTime = when; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3654 | } |
| 3655 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3656 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | a611137 | 2011-07-14 21:48:23 -0700 | [diff] [blame] | 3657 | AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3658 | mCurrentCookedPointerData.pointerProperties, |
| 3659 | mCurrentCookedPointerData.pointerCoords, |
| 3660 | mCurrentCookedPointerData.idToIndex, |
| 3661 | dispatchedIdBits, downId, |
| 3662 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3663 | } |
| 3664 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3665 | } |
| 3666 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3667 | void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) { |
| 3668 | if (mSentHoverEnter && |
| 3669 | (mCurrentCookedPointerData.hoveringIdBits.isEmpty() |
| 3670 | || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) { |
| 3671 | int32_t metaState = getContext()->getGlobalMetaState(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3672 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3673 | AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0, |
| 3674 | mLastCookedPointerData.pointerProperties, |
| 3675 | mLastCookedPointerData.pointerCoords, |
| 3676 | mLastCookedPointerData.idToIndex, |
| 3677 | mLastCookedPointerData.hoveringIdBits, -1, |
| 3678 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 3679 | mSentHoverEnter = false; |
| 3680 | } |
| 3681 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3682 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3683 | void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) { |
| 3684 | if (mCurrentCookedPointerData.touchingIdBits.isEmpty() |
| 3685 | && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) { |
| 3686 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 3687 | if (!mSentHoverEnter) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3688 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3689 | AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0, |
| 3690 | mCurrentCookedPointerData.pointerProperties, |
| 3691 | mCurrentCookedPointerData.pointerCoords, |
| 3692 | mCurrentCookedPointerData.idToIndex, |
| 3693 | mCurrentCookedPointerData.hoveringIdBits, -1, |
| 3694 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 3695 | mSentHoverEnter = true; |
| 3696 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3697 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3698 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3699 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0, |
| 3700 | mCurrentCookedPointerData.pointerProperties, |
| 3701 | mCurrentCookedPointerData.pointerCoords, |
| 3702 | mCurrentCookedPointerData.idToIndex, |
| 3703 | mCurrentCookedPointerData.hoveringIdBits, -1, |
| 3704 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 3705 | } |
| 3706 | } |
| 3707 | |
| 3708 | void TouchInputMapper::cookPointerData() { |
| 3709 | uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount; |
| 3710 | |
| 3711 | mCurrentCookedPointerData.clear(); |
| 3712 | mCurrentCookedPointerData.pointerCount = currentPointerCount; |
| 3713 | mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits; |
| 3714 | mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits; |
| 3715 | |
| 3716 | // Walk through the the active pointers and map device coordinates onto |
| 3717 | // surface coordinates and adjust for display orientation. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3718 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3719 | const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3720 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3721 | // Size |
| 3722 | float touchMajor, touchMinor, toolMajor, toolMinor, size; |
| 3723 | switch (mCalibration.sizeCalibration) { |
| 3724 | case Calibration::SIZE_CALIBRATION_GEOMETRIC: |
| 3725 | case Calibration::SIZE_CALIBRATION_DIAMETER: |
| 3726 | case Calibration::SIZE_CALIBRATION_AREA: |
| 3727 | if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) { |
| 3728 | touchMajor = in.touchMajor; |
| 3729 | touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor; |
| 3730 | toolMajor = in.toolMajor; |
| 3731 | toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor; |
| 3732 | size = mRawPointerAxes.touchMinor.valid |
| 3733 | ? avg(in.touchMajor, in.touchMinor) : in.touchMajor; |
| 3734 | } else if (mRawPointerAxes.touchMajor.valid) { |
| 3735 | toolMajor = touchMajor = in.touchMajor; |
| 3736 | toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid |
| 3737 | ? in.touchMinor : in.touchMajor; |
| 3738 | size = mRawPointerAxes.touchMinor.valid |
| 3739 | ? avg(in.touchMajor, in.touchMinor) : in.touchMajor; |
| 3740 | } else if (mRawPointerAxes.toolMajor.valid) { |
| 3741 | touchMajor = toolMajor = in.toolMajor; |
| 3742 | touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid |
| 3743 | ? in.toolMinor : in.toolMajor; |
| 3744 | size = mRawPointerAxes.toolMinor.valid |
| 3745 | ? avg(in.toolMajor, in.toolMinor) : in.toolMajor; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3746 | } else { |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3747 | LOG_ASSERT(false, "No touch or tool axes. " |
| 3748 | "Size calibration should have been resolved to NONE."); |
| 3749 | touchMajor = 0; |
| 3750 | touchMinor = 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3751 | toolMajor = 0; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3752 | toolMinor = 0; |
| 3753 | size = 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3754 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3755 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3756 | if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) { |
| 3757 | uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count(); |
| 3758 | if (touchingCount > 1) { |
| 3759 | touchMajor /= touchingCount; |
| 3760 | touchMinor /= touchingCount; |
| 3761 | toolMajor /= touchingCount; |
| 3762 | toolMinor /= touchingCount; |
| 3763 | size /= touchingCount; |
| 3764 | } |
| 3765 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3766 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3767 | if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) { |
| 3768 | touchMajor *= mGeometricScale; |
| 3769 | touchMinor *= mGeometricScale; |
| 3770 | toolMajor *= mGeometricScale; |
| 3771 | toolMinor *= mGeometricScale; |
| 3772 | } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) { |
| 3773 | touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3774 | touchMinor = touchMajor; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3775 | toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0; |
| 3776 | toolMinor = toolMajor; |
| 3777 | } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) { |
| 3778 | touchMinor = touchMajor; |
| 3779 | toolMinor = toolMajor; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3780 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3781 | |
| 3782 | mCalibration.applySizeScaleAndBias(&touchMajor); |
| 3783 | mCalibration.applySizeScaleAndBias(&touchMinor); |
| 3784 | mCalibration.applySizeScaleAndBias(&toolMajor); |
| 3785 | mCalibration.applySizeScaleAndBias(&toolMinor); |
| 3786 | size *= mSizeScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3787 | break; |
| 3788 | default: |
| 3789 | touchMajor = 0; |
| 3790 | touchMinor = 0; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3791 | toolMajor = 0; |
| 3792 | toolMinor = 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3793 | size = 0; |
| 3794 | break; |
| 3795 | } |
| 3796 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3797 | // Pressure |
| 3798 | float pressure; |
| 3799 | switch (mCalibration.pressureCalibration) { |
| 3800 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: |
| 3801 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: |
| 3802 | pressure = in.pressure * mPressureScale; |
| 3803 | break; |
| 3804 | default: |
| 3805 | pressure = in.isHovering ? 0 : 1; |
| 3806 | break; |
| 3807 | } |
| 3808 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3809 | // Tilt and Orientation |
| 3810 | float tilt; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3811 | float orientation; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3812 | if (mHaveTilt) { |
| 3813 | float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale; |
| 3814 | float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale; |
| 3815 | orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle)); |
| 3816 | tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle)); |
| 3817 | } else { |
| 3818 | tilt = 0; |
| 3819 | |
| 3820 | switch (mCalibration.orientationCalibration) { |
| 3821 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: |
| 3822 | orientation = (in.orientation - mOrientationCenter) * mOrientationScale; |
| 3823 | break; |
| 3824 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: { |
| 3825 | int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4); |
| 3826 | int32_t c2 = signExtendNybble(in.orientation & 0x0f); |
| 3827 | if (c1 != 0 || c2 != 0) { |
| 3828 | orientation = atan2f(c1, c2) * 0.5f; |
| 3829 | float confidence = hypotf(c1, c2); |
| 3830 | float scale = 1.0f + confidence / 16.0f; |
| 3831 | touchMajor *= scale; |
| 3832 | touchMinor /= scale; |
| 3833 | toolMajor *= scale; |
| 3834 | toolMinor /= scale; |
| 3835 | } else { |
| 3836 | orientation = 0; |
| 3837 | } |
| 3838 | break; |
| 3839 | } |
| 3840 | default: |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3841 | orientation = 0; |
| 3842 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3843 | } |
| 3844 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3845 | // Distance |
| 3846 | float distance; |
| 3847 | switch (mCalibration.distanceCalibration) { |
| 3848 | case Calibration::DISTANCE_CALIBRATION_SCALED: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3849 | distance = in.distance * mDistanceScale; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3850 | break; |
| 3851 | default: |
| 3852 | distance = 0; |
| 3853 | } |
| 3854 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3855 | // X and Y |
| 3856 | // Adjust coords for surface orientation. |
| 3857 | float x, y; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3858 | switch (mSurfaceOrientation) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3859 | case DISPLAY_ORIENTATION_90: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3860 | x = float(in.y - mRawPointerAxes.y.minValue) * mYScale; |
| 3861 | y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3862 | orientation -= M_PI_2; |
| 3863 | if (orientation < - M_PI_2) { |
| 3864 | orientation += M_PI; |
| 3865 | } |
| 3866 | break; |
| 3867 | case DISPLAY_ORIENTATION_180: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3868 | x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale; |
| 3869 | y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3870 | break; |
| 3871 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3872 | x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale; |
| 3873 | y = float(in.x - mRawPointerAxes.x.minValue) * mXScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3874 | orientation += M_PI_2; |
| 3875 | if (orientation > M_PI_2) { |
| 3876 | orientation -= M_PI; |
| 3877 | } |
| 3878 | break; |
| 3879 | default: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3880 | x = float(in.x - mRawPointerAxes.x.minValue) * mXScale; |
| 3881 | y = float(in.y - mRawPointerAxes.y.minValue) * mYScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3882 | break; |
| 3883 | } |
| 3884 | |
| 3885 | // Write output coords. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3886 | PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3887 | out.clear(); |
| 3888 | out.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 3889 | out.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 3890 | out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure); |
| 3891 | out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size); |
| 3892 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor); |
| 3893 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor); |
| 3894 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor); |
| 3895 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor); |
| 3896 | out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3897 | out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3898 | out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3899 | |
| 3900 | // Write output properties. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3901 | PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i]; |
| 3902 | uint32_t id = in.id; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3903 | properties.clear(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3904 | properties.id = id; |
| 3905 | properties.toolType = in.toolType; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3906 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3907 | // Write id index. |
| 3908 | mCurrentCookedPointerData.idToIndex[id] = i; |
| 3909 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3910 | } |
| 3911 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3912 | void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, |
| 3913 | PointerUsage pointerUsage) { |
| 3914 | if (pointerUsage != mPointerUsage) { |
| 3915 | abortPointerUsage(when, policyFlags); |
| 3916 | mPointerUsage = pointerUsage; |
| 3917 | } |
| 3918 | |
| 3919 | switch (mPointerUsage) { |
| 3920 | case POINTER_USAGE_GESTURES: |
| 3921 | dispatchPointerGestures(when, policyFlags, false /*isTimeout*/); |
| 3922 | break; |
| 3923 | case POINTER_USAGE_STYLUS: |
| 3924 | dispatchPointerStylus(when, policyFlags); |
| 3925 | break; |
| 3926 | case POINTER_USAGE_MOUSE: |
| 3927 | dispatchPointerMouse(when, policyFlags); |
| 3928 | break; |
| 3929 | default: |
| 3930 | break; |
| 3931 | } |
| 3932 | } |
| 3933 | |
| 3934 | void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) { |
| 3935 | switch (mPointerUsage) { |
| 3936 | case POINTER_USAGE_GESTURES: |
| 3937 | abortPointerGestures(when, policyFlags); |
| 3938 | break; |
| 3939 | case POINTER_USAGE_STYLUS: |
| 3940 | abortPointerStylus(when, policyFlags); |
| 3941 | break; |
| 3942 | case POINTER_USAGE_MOUSE: |
| 3943 | abortPointerMouse(when, policyFlags); |
| 3944 | break; |
| 3945 | default: |
| 3946 | break; |
| 3947 | } |
| 3948 | |
| 3949 | mPointerUsage = POINTER_USAGE_NONE; |
| 3950 | } |
| 3951 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3952 | void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, |
| 3953 | bool isTimeout) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3954 | // Update current gesture coordinates. |
| 3955 | bool cancelPreviousGesture, finishPreviousGesture; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3956 | bool sendEvents = preparePointerGestures(when, |
| 3957 | &cancelPreviousGesture, &finishPreviousGesture, isTimeout); |
| 3958 | if (!sendEvents) { |
| 3959 | return; |
| 3960 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 3961 | if (finishPreviousGesture) { |
| 3962 | cancelPreviousGesture = false; |
| 3963 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3964 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 3965 | // Update the pointer presentation and spots. |
| 3966 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 3967 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT); |
| 3968 | if (finishPreviousGesture || cancelPreviousGesture) { |
| 3969 | mPointerController->clearSpots(); |
| 3970 | } |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 3971 | mPointerController->setSpots(mPointerGesture.currentGestureCoords, |
| 3972 | mPointerGesture.currentGestureIdToIndex, |
| 3973 | mPointerGesture.currentGestureIdBits); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 3974 | } else { |
| 3975 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER); |
| 3976 | } |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 3977 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 3978 | // Show or hide the pointer if needed. |
| 3979 | switch (mPointerGesture.currentGestureMode) { |
| 3980 | case PointerGesture::NEUTRAL: |
| 3981 | case PointerGesture::QUIET: |
| 3982 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS |
| 3983 | && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE |
| 3984 | || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) { |
| 3985 | // Remind the user of where the pointer is after finishing a gesture with spots. |
| 3986 | mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 3987 | } |
| 3988 | break; |
| 3989 | case PointerGesture::TAP: |
| 3990 | case PointerGesture::TAP_DRAG: |
| 3991 | case PointerGesture::BUTTON_CLICK_OR_DRAG: |
| 3992 | case PointerGesture::HOVER: |
| 3993 | case PointerGesture::PRESS: |
| 3994 | // Unfade the pointer when the current gesture manipulates the |
| 3995 | // area directly under the pointer. |
| 3996 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 3997 | break; |
| 3998 | case PointerGesture::SWIPE: |
| 3999 | case PointerGesture::FREEFORM: |
| 4000 | // Fade the pointer when the current gesture manipulates a different |
| 4001 | // area and there are spots to guide the user experience. |
| 4002 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 4003 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 4004 | } else { |
| 4005 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 4006 | } |
| 4007 | break; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4008 | } |
| 4009 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4010 | // Send events! |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4011 | int32_t metaState = getContext()->getGlobalMetaState(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4012 | int32_t buttonState = mCurrentButtonState; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4013 | |
| 4014 | // Update last coordinates of pointers that have moved so that we observe the new |
| 4015 | // 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] | 4016 | bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP |
| 4017 | || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG |
| 4018 | || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4019 | || mPointerGesture.currentGestureMode == PointerGesture::PRESS |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4020 | || mPointerGesture.currentGestureMode == PointerGesture::SWIPE |
| 4021 | || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM; |
| 4022 | bool moveNeeded = false; |
| 4023 | if (down && !cancelPreviousGesture && !finishPreviousGesture |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4024 | && !mPointerGesture.lastGestureIdBits.isEmpty() |
| 4025 | && !mPointerGesture.currentGestureIdBits.isEmpty()) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4026 | BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value |
| 4027 | & mPointerGesture.lastGestureIdBits.value); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4028 | moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4029 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4030 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4031 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4032 | movedGestureIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4033 | if (buttonState != mLastButtonState) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4034 | moveNeeded = true; |
| 4035 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4036 | } |
| 4037 | |
| 4038 | // Send motion events for all pointers that went up or were canceled. |
| 4039 | BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits); |
| 4040 | if (!dispatchedGestureIdBits.isEmpty()) { |
| 4041 | if (cancelPreviousGesture) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4042 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4043 | AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState, |
| 4044 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4045 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4046 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4047 | dispatchedGestureIdBits, -1, |
| 4048 | 0, 0, mPointerGesture.downTime); |
| 4049 | |
| 4050 | dispatchedGestureIdBits.clear(); |
| 4051 | } else { |
| 4052 | BitSet32 upGestureIdBits; |
| 4053 | if (finishPreviousGesture) { |
| 4054 | upGestureIdBits = dispatchedGestureIdBits; |
| 4055 | } else { |
| 4056 | upGestureIdBits.value = dispatchedGestureIdBits.value |
| 4057 | & ~mPointerGesture.currentGestureIdBits.value; |
| 4058 | } |
| 4059 | while (!upGestureIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4060 | uint32_t id = upGestureIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4061 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4062 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4063 | AMOTION_EVENT_ACTION_POINTER_UP, 0, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4064 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4065 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4066 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4067 | dispatchedGestureIdBits, id, |
| 4068 | 0, 0, mPointerGesture.downTime); |
| 4069 | |
| 4070 | dispatchedGestureIdBits.clearBit(id); |
| 4071 | } |
| 4072 | } |
| 4073 | } |
| 4074 | |
| 4075 | // Send motion events for all pointers that moved. |
| 4076 | if (moveNeeded) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4077 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4078 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4079 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4080 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 4081 | dispatchedGestureIdBits, -1, |
| 4082 | 0, 0, mPointerGesture.downTime); |
| 4083 | } |
| 4084 | |
| 4085 | // Send motion events for all pointers that went down. |
| 4086 | if (down) { |
| 4087 | BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value |
| 4088 | & ~dispatchedGestureIdBits.value); |
| 4089 | while (!downGestureIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4090 | uint32_t id = downGestureIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4091 | dispatchedGestureIdBits.markBit(id); |
| 4092 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4093 | if (dispatchedGestureIdBits.count() == 1) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4094 | mPointerGesture.downTime = when; |
| 4095 | } |
| 4096 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4097 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | a611137 | 2011-07-14 21:48:23 -0700 | [diff] [blame] | 4098 | AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4099 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4100 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 4101 | dispatchedGestureIdBits, id, |
| 4102 | 0, 0, mPointerGesture.downTime); |
| 4103 | } |
| 4104 | } |
| 4105 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4106 | // Send motion events for hover. |
| 4107 | if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4108 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4109 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 4110 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4111 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4112 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 4113 | mPointerGesture.currentGestureIdBits, -1, |
| 4114 | 0, 0, mPointerGesture.downTime); |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4115 | } else if (dispatchedGestureIdBits.isEmpty() |
| 4116 | && !mPointerGesture.lastGestureIdBits.isEmpty()) { |
| 4117 | // Synthesize a hover move event after all pointers go up to indicate that |
| 4118 | // the pointer is hovering again even if the user is not currently touching |
| 4119 | // the touch pad. This ensures that a view will receive a fresh hover enter |
| 4120 | // event after a tap. |
| 4121 | float x, y; |
| 4122 | mPointerController->getPosition(&x, &y); |
| 4123 | |
| 4124 | PointerProperties pointerProperties; |
| 4125 | pointerProperties.clear(); |
| 4126 | pointerProperties.id = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4127 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4128 | |
| 4129 | PointerCoords pointerCoords; |
| 4130 | pointerCoords.clear(); |
| 4131 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4132 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 4133 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4134 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4135 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 4136 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4137 | 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4138 | getListener()->notifyMotion(&args); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4139 | } |
| 4140 | |
| 4141 | // Update state. |
| 4142 | mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode; |
| 4143 | if (!down) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4144 | mPointerGesture.lastGestureIdBits.clear(); |
| 4145 | } else { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4146 | mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits; |
| 4147 | for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4148 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4149 | uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4150 | mPointerGesture.lastGestureProperties[index].copyFrom( |
| 4151 | mPointerGesture.currentGestureProperties[index]); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4152 | mPointerGesture.lastGestureCoords[index].copyFrom( |
| 4153 | mPointerGesture.currentGestureCoords[index]); |
| 4154 | mPointerGesture.lastGestureIdToIndex[id] = index; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4155 | } |
| 4156 | } |
| 4157 | } |
| 4158 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4159 | void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) { |
| 4160 | // Cancel previously dispatches pointers. |
| 4161 | if (!mPointerGesture.lastGestureIdBits.isEmpty()) { |
| 4162 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 4163 | int32_t buttonState = mCurrentButtonState; |
| 4164 | dispatchMotion(when, policyFlags, mSource, |
| 4165 | AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState, |
| 4166 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4167 | mPointerGesture.lastGestureProperties, |
| 4168 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4169 | mPointerGesture.lastGestureIdBits, -1, |
| 4170 | 0, 0, mPointerGesture.downTime); |
| 4171 | } |
| 4172 | |
| 4173 | // Reset the current pointer gesture. |
| 4174 | mPointerGesture.reset(); |
| 4175 | mPointerVelocityControl.reset(); |
| 4176 | |
| 4177 | // Remove any current spots. |
| 4178 | if (mPointerController != NULL) { |
| 4179 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 4180 | mPointerController->clearSpots(); |
| 4181 | } |
| 4182 | } |
| 4183 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4184 | bool TouchInputMapper::preparePointerGestures(nsecs_t when, |
| 4185 | bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4186 | *outCancelPreviousGesture = false; |
| 4187 | *outFinishPreviousGesture = false; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4188 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4189 | // Handle TAP timeout. |
| 4190 | if (isTimeout) { |
| 4191 | #if DEBUG_GESTURES |
| 4192 | LOGD("Gestures: Processing timeout"); |
| 4193 | #endif |
| 4194 | |
| 4195 | if (mPointerGesture.lastGestureMode == PointerGesture::TAP) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4196 | if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4197 | // The tap/drag timeout has not yet expired. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4198 | getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4199 | + mConfig.pointerGestureTapDragInterval); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4200 | } else { |
| 4201 | // The tap is finished. |
| 4202 | #if DEBUG_GESTURES |
| 4203 | LOGD("Gestures: TAP finished"); |
| 4204 | #endif |
| 4205 | *outFinishPreviousGesture = true; |
| 4206 | |
| 4207 | mPointerGesture.activeGestureId = -1; |
| 4208 | mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL; |
| 4209 | mPointerGesture.currentGestureIdBits.clear(); |
| 4210 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4211 | mPointerVelocityControl.reset(); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4212 | return true; |
| 4213 | } |
| 4214 | } |
| 4215 | |
| 4216 | // We did not handle this timeout. |
| 4217 | return false; |
| 4218 | } |
| 4219 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4220 | const uint32_t currentFingerCount = mCurrentFingerIdBits.count(); |
| 4221 | const uint32_t lastFingerCount = mLastFingerIdBits.count(); |
| 4222 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4223 | // Update the velocity tracker. |
| 4224 | { |
| 4225 | VelocityTracker::Position positions[MAX_POINTERS]; |
| 4226 | uint32_t count = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4227 | for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4228 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 4229 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4230 | positions[count].x = pointer.x * mPointerXMovementScale; |
| 4231 | positions[count].y = pointer.y * mPointerYMovementScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4232 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4233 | mPointerGesture.velocityTracker.addMovement(when, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4234 | mCurrentFingerIdBits, positions); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4235 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4236 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4237 | // Pick a new active touch id if needed. |
| 4238 | // Choose an arbitrary pointer that just went down, if there is one. |
| 4239 | // Otherwise choose an arbitrary remaining pointer. |
| 4240 | // 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] | 4241 | // We keep the same active touch id for as long as possible. |
| 4242 | bool activeTouchChanged = false; |
| 4243 | int32_t lastActiveTouchId = mPointerGesture.activeTouchId; |
| 4244 | int32_t activeTouchId = lastActiveTouchId; |
| 4245 | if (activeTouchId < 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4246 | if (!mCurrentFingerIdBits.isEmpty()) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4247 | activeTouchChanged = true; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4248 | activeTouchId = mPointerGesture.activeTouchId = |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4249 | mCurrentFingerIdBits.firstMarkedBit(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4250 | mPointerGesture.firstTouchTime = when; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4251 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4252 | } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4253 | activeTouchChanged = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4254 | if (!mCurrentFingerIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4255 | activeTouchId = mPointerGesture.activeTouchId = |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4256 | mCurrentFingerIdBits.firstMarkedBit(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4257 | } else { |
| 4258 | activeTouchId = mPointerGesture.activeTouchId = -1; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4259 | } |
| 4260 | } |
| 4261 | |
| 4262 | // Determine whether we are in quiet time. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4263 | bool isQuietTime = false; |
| 4264 | if (activeTouchId < 0) { |
| 4265 | mPointerGesture.resetQuietTime(); |
| 4266 | } else { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4267 | isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4268 | if (!isQuietTime) { |
| 4269 | if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS |
| 4270 | || mPointerGesture.lastGestureMode == PointerGesture::SWIPE |
| 4271 | || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4272 | && currentFingerCount < 2) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4273 | // Enter quiet time when exiting swipe or freeform state. |
| 4274 | // This is to prevent accidentally entering the hover state and flinging the |
| 4275 | // pointer when finishing a swipe and there is still one pointer left onscreen. |
| 4276 | isQuietTime = true; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4277 | } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4278 | && currentFingerCount >= 2 |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4279 | && !isPointerDown(mCurrentButtonState)) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4280 | // Enter quiet time when releasing the button and there are still two or more |
| 4281 | // fingers down. This may indicate that one finger was used to press the button |
| 4282 | // but it has not gone up yet. |
| 4283 | isQuietTime = true; |
| 4284 | } |
| 4285 | if (isQuietTime) { |
| 4286 | mPointerGesture.quietTime = when; |
| 4287 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4288 | } |
| 4289 | } |
| 4290 | |
| 4291 | // Switch states based on button and pointer state. |
| 4292 | if (isQuietTime) { |
| 4293 | // Case 1: Quiet time. (QUIET) |
| 4294 | #if DEBUG_GESTURES |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4295 | LOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4296 | + mConfig.pointerGestureQuietInterval - when) * 0.000001f); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4297 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4298 | if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) { |
| 4299 | *outFinishPreviousGesture = true; |
| 4300 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4301 | |
| 4302 | mPointerGesture.activeGestureId = -1; |
| 4303 | mPointerGesture.currentGestureMode = PointerGesture::QUIET; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4304 | mPointerGesture.currentGestureIdBits.clear(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4305 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4306 | mPointerVelocityControl.reset(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4307 | } else if (isPointerDown(mCurrentButtonState)) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4308 | // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG) |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4309 | // The pointer follows the active touch point. |
| 4310 | // Emit DOWN, MOVE, UP events at the pointer location. |
| 4311 | // |
| 4312 | // Only the active touch matters; other fingers are ignored. This policy helps |
| 4313 | // to handle the case where the user places a second finger on the touch pad |
| 4314 | // to apply the necessary force to depress an integrated button below the surface. |
| 4315 | // We don't want the second finger to be delivered to applications. |
| 4316 | // |
| 4317 | // For this to work well, we need to make sure to track the pointer that is really |
| 4318 | // active. If the user first puts one finger down to click then adds another |
| 4319 | // finger to drag then the active pointer should switch to the finger that is |
| 4320 | // being dragged. |
| 4321 | #if DEBUG_GESTURES |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4322 | LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4323 | "currentFingerCount=%d", activeTouchId, currentFingerCount); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4324 | #endif |
| 4325 | // Reset state when just starting. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4326 | if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4327 | *outFinishPreviousGesture = true; |
| 4328 | mPointerGesture.activeGestureId = 0; |
| 4329 | } |
| 4330 | |
| 4331 | // Switch pointers if needed. |
| 4332 | // Find the fastest pointer and follow it. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4333 | if (activeTouchId >= 0 && currentFingerCount > 1) { |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4334 | int32_t bestId = -1; |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4335 | float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4336 | for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4337 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4338 | float vx, vy; |
| 4339 | if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) { |
| 4340 | float speed = hypotf(vx, vy); |
| 4341 | if (speed > bestSpeed) { |
| 4342 | bestId = id; |
| 4343 | bestSpeed = speed; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4344 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 4345 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4346 | } |
| 4347 | if (bestId >= 0 && bestId != activeTouchId) { |
| 4348 | mPointerGesture.activeTouchId = activeTouchId = bestId; |
| 4349 | activeTouchChanged = true; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4350 | #if DEBUG_GESTURES |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4351 | LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, " |
| 4352 | "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4353 | #endif |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4354 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4355 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4356 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4357 | if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4358 | const RawPointerData::Pointer& currentPointer = |
| 4359 | mCurrentRawPointerData.pointerForId(activeTouchId); |
| 4360 | const RawPointerData::Pointer& lastPointer = |
| 4361 | mLastRawPointerData.pointerForId(activeTouchId); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4362 | float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale; |
| 4363 | float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4364 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4365 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4366 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4367 | |
| 4368 | // Move the pointer using a relative motion. |
| 4369 | // When using spots, the click will occur at the position of the anchor |
| 4370 | // spot and all other spots will move there. |
| 4371 | mPointerController->move(deltaX, deltaY); |
| 4372 | } else { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4373 | mPointerVelocityControl.reset(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4374 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4375 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4376 | float x, y; |
| 4377 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 4378 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4379 | mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4380 | mPointerGesture.currentGestureIdBits.clear(); |
| 4381 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4382 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4383 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4384 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4385 | mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4386 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4387 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4388 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 4389 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4390 | } else if (currentFingerCount == 0) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4391 | // Case 3. No fingers down and button is not pressed. (NEUTRAL) |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4392 | if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) { |
| 4393 | *outFinishPreviousGesture = true; |
| 4394 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4395 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4396 | // Watch for taps coming out of HOVER or TAP_DRAG mode. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4397 | // Checking for taps after TAP_DRAG allows us to detect double-taps. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4398 | bool tapped = false; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4399 | if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER |
| 4400 | || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4401 | && lastFingerCount == 1) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4402 | if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4403 | float x, y; |
| 4404 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4405 | if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop |
| 4406 | && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4407 | #if DEBUG_GESTURES |
| 4408 | LOGD("Gestures: TAP"); |
| 4409 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4410 | |
| 4411 | mPointerGesture.tapUpTime = when; |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4412 | getContext()->requestTimeoutAtTime(when |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4413 | + mConfig.pointerGestureTapDragInterval); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4414 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4415 | mPointerGesture.activeGestureId = 0; |
| 4416 | mPointerGesture.currentGestureMode = PointerGesture::TAP; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4417 | mPointerGesture.currentGestureIdBits.clear(); |
| 4418 | mPointerGesture.currentGestureIdBits.markBit( |
| 4419 | mPointerGesture.activeGestureId); |
| 4420 | mPointerGesture.currentGestureIdToIndex[ |
| 4421 | mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4422 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4423 | mPointerGesture.currentGestureProperties[0].id = |
| 4424 | mPointerGesture.activeGestureId; |
| 4425 | mPointerGesture.currentGestureProperties[0].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4426 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4427 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4428 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4429 | AMOTION_EVENT_AXIS_X, mPointerGesture.tapX); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4430 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4431 | AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4432 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
| 4433 | AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4434 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4435 | tapped = true; |
| 4436 | } else { |
| 4437 | #if DEBUG_GESTURES |
| 4438 | LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f", |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4439 | x - mPointerGesture.tapX, |
| 4440 | y - mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4441 | #endif |
| 4442 | } |
| 4443 | } else { |
| 4444 | #if DEBUG_GESTURES |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4445 | LOGD("Gestures: Not a TAP, %0.3fms since down", |
| 4446 | (when - mPointerGesture.tapDownTime) * 0.000001f); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4447 | #endif |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4448 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4449 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4450 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4451 | mPointerVelocityControl.reset(); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4452 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4453 | if (!tapped) { |
| 4454 | #if DEBUG_GESTURES |
| 4455 | LOGD("Gestures: NEUTRAL"); |
| 4456 | #endif |
| 4457 | mPointerGesture.activeGestureId = -1; |
| 4458 | mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4459 | mPointerGesture.currentGestureIdBits.clear(); |
| 4460 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4461 | } else if (currentFingerCount == 1) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4462 | // 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] | 4463 | // The pointer follows the active touch point. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4464 | // When in HOVER, emit HOVER_MOVE events at the pointer location. |
| 4465 | // When in TAP_DRAG, emit MOVE events at the pointer location. |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 4466 | LOG_ASSERT(activeTouchId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4467 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4468 | mPointerGesture.currentGestureMode = PointerGesture::HOVER; |
| 4469 | if (mPointerGesture.lastGestureMode == PointerGesture::TAP) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4470 | if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4471 | float x, y; |
| 4472 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4473 | if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop |
| 4474 | && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4475 | mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG; |
| 4476 | } else { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4477 | #if DEBUG_GESTURES |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4478 | LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f", |
| 4479 | x - mPointerGesture.tapX, |
| 4480 | y - mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4481 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4482 | } |
| 4483 | } else { |
| 4484 | #if DEBUG_GESTURES |
| 4485 | LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up", |
| 4486 | (when - mPointerGesture.tapUpTime) * 0.000001f); |
| 4487 | #endif |
| 4488 | } |
| 4489 | } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) { |
| 4490 | mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG; |
| 4491 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4492 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4493 | if (mLastFingerIdBits.hasBit(activeTouchId)) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4494 | const RawPointerData::Pointer& currentPointer = |
| 4495 | mCurrentRawPointerData.pointerForId(activeTouchId); |
| 4496 | const RawPointerData::Pointer& lastPointer = |
| 4497 | mLastRawPointerData.pointerForId(activeTouchId); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4498 | float deltaX = (currentPointer.x - lastPointer.x) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4499 | * mPointerXMovementScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4500 | float deltaY = (currentPointer.y - lastPointer.y) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4501 | * mPointerYMovementScale; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4502 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4503 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4504 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4505 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4506 | // Move the pointer using a relative motion. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4507 | // 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] | 4508 | mPointerController->move(deltaX, deltaY); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4509 | } else { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4510 | mPointerVelocityControl.reset(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4511 | } |
| 4512 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4513 | bool down; |
| 4514 | if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) { |
| 4515 | #if DEBUG_GESTURES |
| 4516 | LOGD("Gestures: TAP_DRAG"); |
| 4517 | #endif |
| 4518 | down = true; |
| 4519 | } else { |
| 4520 | #if DEBUG_GESTURES |
| 4521 | LOGD("Gestures: HOVER"); |
| 4522 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4523 | if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) { |
| 4524 | *outFinishPreviousGesture = true; |
| 4525 | } |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4526 | mPointerGesture.activeGestureId = 0; |
| 4527 | down = false; |
| 4528 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4529 | |
| 4530 | float x, y; |
| 4531 | mPointerController->getPosition(&x, &y); |
| 4532 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4533 | mPointerGesture.currentGestureIdBits.clear(); |
| 4534 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4535 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4536 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4537 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
| 4538 | mPointerGesture.currentGestureProperties[0].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4539 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4540 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4541 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4542 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4543 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, |
| 4544 | down ? 1.0f : 0.0f); |
| 4545 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4546 | if (lastFingerCount == 0 && currentFingerCount != 0) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4547 | mPointerGesture.resetTap(); |
| 4548 | mPointerGesture.tapDownTime = when; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4549 | mPointerGesture.tapX = x; |
| 4550 | mPointerGesture.tapY = y; |
| 4551 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4552 | } else { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4553 | // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM) |
| 4554 | // We need to provide feedback for each finger that goes down so we cannot wait |
| 4555 | // for the fingers to move before deciding what to do. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4556 | // |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4557 | // The ambiguous case is deciding what to do when there are two fingers down but they |
| 4558 | // have not moved enough to determine whether they are part of a drag or part of a |
| 4559 | // freeform gesture, or just a press or long-press at the pointer location. |
| 4560 | // |
| 4561 | // When there are two fingers we start with the PRESS hypothesis and we generate a |
| 4562 | // down at the pointer location. |
| 4563 | // |
| 4564 | // When the two fingers move enough or when additional fingers are added, we make |
| 4565 | // a decision to transition into SWIPE or FREEFORM mode accordingly. |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 4566 | LOG_ASSERT(activeTouchId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4567 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4568 | bool settled = when >= mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4569 | + mConfig.pointerGestureMultitouchSettleInterval; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4570 | if (mPointerGesture.lastGestureMode != PointerGesture::PRESS |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4571 | && mPointerGesture.lastGestureMode != PointerGesture::SWIPE |
| 4572 | && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4573 | *outFinishPreviousGesture = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4574 | } else if (!settled && currentFingerCount > lastFingerCount) { |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4575 | // Additional pointers have gone down but not yet settled. |
| 4576 | // Reset the gesture. |
| 4577 | #if DEBUG_GESTURES |
| 4578 | LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, " |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4579 | "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4580 | + mConfig.pointerGestureMultitouchSettleInterval - when) |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4581 | * 0.000001f); |
| 4582 | #endif |
| 4583 | *outCancelPreviousGesture = true; |
| 4584 | } else { |
| 4585 | // Continue previous gesture. |
| 4586 | mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode; |
| 4587 | } |
| 4588 | |
| 4589 | if (*outFinishPreviousGesture || *outCancelPreviousGesture) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4590 | mPointerGesture.currentGestureMode = PointerGesture::PRESS; |
| 4591 | mPointerGesture.activeGestureId = 0; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4592 | mPointerGesture.referenceIdBits.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4593 | mPointerVelocityControl.reset(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4594 | |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4595 | // 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] | 4596 | #if DEBUG_GESTURES |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4597 | LOGD("Gestures: Using centroid as reference for MULTITOUCH, " |
| 4598 | "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4599 | + mConfig.pointerGestureMultitouchSettleInterval - when) |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4600 | * 0.000001f); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4601 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4602 | mCurrentRawPointerData.getCentroidOfTouchingPointers( |
| 4603 | &mPointerGesture.referenceTouchX, |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4604 | &mPointerGesture.referenceTouchY); |
| 4605 | mPointerController->getPosition(&mPointerGesture.referenceGestureX, |
| 4606 | &mPointerGesture.referenceGestureY); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4607 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4608 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4609 | // 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] | 4610 | for (BitSet32 idBits(mCurrentFingerIdBits.value |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4611 | & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) { |
| 4612 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4613 | mPointerGesture.referenceDeltas[id].dx = 0; |
| 4614 | mPointerGesture.referenceDeltas[id].dy = 0; |
| 4615 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4616 | mPointerGesture.referenceIdBits = mCurrentFingerIdBits; |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4617 | |
| 4618 | // Add delta for all fingers and calculate a common movement delta. |
| 4619 | float commonDeltaX = 0, commonDeltaY = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4620 | BitSet32 commonIdBits(mLastFingerIdBits.value |
| 4621 | & mCurrentFingerIdBits.value); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4622 | for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) { |
| 4623 | bool first = (idBits == commonIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4624 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 4625 | const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id); |
| 4626 | const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4627 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
| 4628 | delta.dx += cpd.x - lpd.x; |
| 4629 | delta.dy += cpd.y - lpd.y; |
| 4630 | |
| 4631 | if (first) { |
| 4632 | commonDeltaX = delta.dx; |
| 4633 | commonDeltaY = delta.dy; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4634 | } else { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4635 | commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx); |
| 4636 | commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy); |
| 4637 | } |
| 4638 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4639 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4640 | // Consider transitions from PRESS to SWIPE or MULTITOUCH. |
| 4641 | if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) { |
| 4642 | float dist[MAX_POINTER_ID + 1]; |
| 4643 | int32_t distOverThreshold = 0; |
| 4644 | for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4645 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4646 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4647 | dist[id] = hypotf(delta.dx * mPointerXZoomScale, |
| 4648 | delta.dy * mPointerYZoomScale); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4649 | if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4650 | distOverThreshold += 1; |
| 4651 | } |
| 4652 | } |
| 4653 | |
| 4654 | // Only transition when at least two pointers have moved further than |
| 4655 | // the minimum distance threshold. |
| 4656 | if (distOverThreshold >= 2) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4657 | if (currentFingerCount > 2) { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4658 | // There are more than two pointers, switch to FREEFORM. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4659 | #if DEBUG_GESTURES |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4660 | LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4661 | currentFingerCount); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4662 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4663 | *outCancelPreviousGesture = true; |
| 4664 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 4665 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4666 | // There are exactly two pointers. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4667 | BitSet32 idBits(mCurrentFingerIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4668 | uint32_t id1 = idBits.clearFirstMarkedBit(); |
| 4669 | uint32_t id2 = idBits.firstMarkedBit(); |
| 4670 | const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1); |
| 4671 | const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2); |
| 4672 | float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y); |
| 4673 | if (mutualDistance > mPointerGestureMaxSwipeWidth) { |
| 4674 | // There are two pointers but they are too far apart for a SWIPE, |
| 4675 | // switch to FREEFORM. |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4676 | #if DEBUG_GESTURES |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4677 | LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f", |
| 4678 | mutualDistance, mPointerGestureMaxSwipeWidth); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4679 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4680 | *outCancelPreviousGesture = true; |
| 4681 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 4682 | } else { |
| 4683 | // There are two pointers. Wait for both pointers to start moving |
| 4684 | // before deciding whether this is a SWIPE or FREEFORM gesture. |
| 4685 | float dist1 = dist[id1]; |
| 4686 | float dist2 = dist[id2]; |
| 4687 | if (dist1 >= mConfig.pointerGestureMultitouchMinDistance |
| 4688 | && dist2 >= mConfig.pointerGestureMultitouchMinDistance) { |
| 4689 | // Calculate the dot product of the displacement vectors. |
| 4690 | // When the vectors are oriented in approximately the same direction, |
| 4691 | // the angle betweeen them is near zero and the cosine of the angle |
| 4692 | // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2). |
| 4693 | PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1]; |
| 4694 | PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4695 | float dx1 = delta1.dx * mPointerXZoomScale; |
| 4696 | float dy1 = delta1.dy * mPointerYZoomScale; |
| 4697 | float dx2 = delta2.dx * mPointerXZoomScale; |
| 4698 | float dy2 = delta2.dy * mPointerYZoomScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4699 | float dot = dx1 * dx2 + dy1 * dy2; |
| 4700 | float cosine = dot / (dist1 * dist2); // denominator always > 0 |
| 4701 | if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) { |
| 4702 | // Pointers are moving in the same direction. Switch to SWIPE. |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4703 | #if DEBUG_GESTURES |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4704 | LOGD("Gestures: PRESS transitioned to SWIPE, " |
| 4705 | "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, " |
| 4706 | "cosine %0.3f >= %0.3f", |
| 4707 | dist1, mConfig.pointerGestureMultitouchMinDistance, |
| 4708 | dist2, mConfig.pointerGestureMultitouchMinDistance, |
| 4709 | cosine, mConfig.pointerGestureSwipeTransitionAngleCosine); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4710 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4711 | mPointerGesture.currentGestureMode = PointerGesture::SWIPE; |
| 4712 | } else { |
| 4713 | // Pointers are moving in different directions. Switch to FREEFORM. |
| 4714 | #if DEBUG_GESTURES |
| 4715 | LOGD("Gestures: PRESS transitioned to FREEFORM, " |
| 4716 | "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, " |
| 4717 | "cosine %0.3f < %0.3f", |
| 4718 | dist1, mConfig.pointerGestureMultitouchMinDistance, |
| 4719 | dist2, mConfig.pointerGestureMultitouchMinDistance, |
| 4720 | cosine, mConfig.pointerGestureSwipeTransitionAngleCosine); |
| 4721 | #endif |
| 4722 | *outCancelPreviousGesture = true; |
| 4723 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 4724 | } |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4725 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4726 | } |
| 4727 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4728 | } |
| 4729 | } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4730 | // Switch from SWIPE to FREEFORM if additional pointers go down. |
| 4731 | // Cancel previous gesture. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4732 | if (currentFingerCount > 2) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4733 | #if DEBUG_GESTURES |
| 4734 | LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4735 | currentFingerCount); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4736 | #endif |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4737 | *outCancelPreviousGesture = true; |
| 4738 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4739 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4740 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4741 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4742 | // Move the reference points based on the overall group motion of the fingers |
| 4743 | // except in PRESS mode while waiting for a transition to occur. |
| 4744 | if (mPointerGesture.currentGestureMode != PointerGesture::PRESS |
| 4745 | && (commonDeltaX || commonDeltaY)) { |
| 4746 | for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4747 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4748 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4749 | delta.dx = 0; |
| 4750 | delta.dy = 0; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4751 | } |
| 4752 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4753 | mPointerGesture.referenceTouchX += commonDeltaX; |
| 4754 | mPointerGesture.referenceTouchY += commonDeltaY; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4755 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4756 | commonDeltaX *= mPointerXMovementScale; |
| 4757 | commonDeltaY *= mPointerYMovementScale; |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 4758 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4759 | rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4760 | mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4761 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4762 | mPointerGesture.referenceGestureX += commonDeltaX; |
| 4763 | mPointerGesture.referenceGestureY += commonDeltaY; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4764 | } |
| 4765 | |
| 4766 | // Report gestures. |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 4767 | if (mPointerGesture.currentGestureMode == PointerGesture::PRESS |
| 4768 | || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) { |
| 4769 | // PRESS or SWIPE mode. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4770 | #if DEBUG_GESTURES |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 4771 | LOGD("Gestures: PRESS or SWIPE activeTouchId=%d," |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4772 | "activeGestureId=%d, currentTouchPointerCount=%d", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4773 | activeTouchId, mPointerGesture.activeGestureId, currentFingerCount); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4774 | #endif |
| 4775 | LOG_ASSERT(mPointerGesture.activeGestureId >= 0); |
| 4776 | |
| 4777 | mPointerGesture.currentGestureIdBits.clear(); |
| 4778 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4779 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4780 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4781 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
| 4782 | mPointerGesture.currentGestureProperties[0].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4783 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4784 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4785 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 4786 | mPointerGesture.referenceGestureX); |
| 4787 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, |
| 4788 | mPointerGesture.referenceGestureY); |
| 4789 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4790 | } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) { |
| 4791 | // FREEFORM mode. |
| 4792 | #if DEBUG_GESTURES |
| 4793 | LOGD("Gestures: FREEFORM activeTouchId=%d," |
| 4794 | "activeGestureId=%d, currentTouchPointerCount=%d", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4795 | activeTouchId, mPointerGesture.activeGestureId, currentFingerCount); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4796 | #endif |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 4797 | LOG_ASSERT(mPointerGesture.activeGestureId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4798 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4799 | mPointerGesture.currentGestureIdBits.clear(); |
| 4800 | |
| 4801 | BitSet32 mappedTouchIdBits; |
| 4802 | BitSet32 usedGestureIdBits; |
| 4803 | if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) { |
| 4804 | // Initially, assign the active gesture id to the active touch point |
| 4805 | // if there is one. No other touch id bits are mapped yet. |
| 4806 | if (!*outCancelPreviousGesture) { |
| 4807 | mappedTouchIdBits.markBit(activeTouchId); |
| 4808 | usedGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4809 | mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] = |
| 4810 | mPointerGesture.activeGestureId; |
| 4811 | } else { |
| 4812 | mPointerGesture.activeGestureId = -1; |
| 4813 | } |
| 4814 | } else { |
| 4815 | // Otherwise, assume we mapped all touches from the previous frame. |
| 4816 | // Reuse all mappings that are still applicable. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4817 | mappedTouchIdBits.value = mLastFingerIdBits.value |
| 4818 | & mCurrentFingerIdBits.value; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4819 | usedGestureIdBits = mPointerGesture.lastGestureIdBits; |
| 4820 | |
| 4821 | // Check whether we need to choose a new active gesture id because the |
| 4822 | // current went went up. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4823 | for (BitSet32 upTouchIdBits(mLastFingerIdBits.value |
| 4824 | & ~mCurrentFingerIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4825 | !upTouchIdBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4826 | uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4827 | uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId]; |
| 4828 | if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) { |
| 4829 | mPointerGesture.activeGestureId = -1; |
| 4830 | break; |
| 4831 | } |
| 4832 | } |
| 4833 | } |
| 4834 | |
| 4835 | #if DEBUG_GESTURES |
| 4836 | LOGD("Gestures: FREEFORM follow up " |
| 4837 | "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, " |
| 4838 | "activeGestureId=%d", |
| 4839 | mappedTouchIdBits.value, usedGestureIdBits.value, |
| 4840 | mPointerGesture.activeGestureId); |
| 4841 | #endif |
| 4842 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4843 | BitSet32 idBits(mCurrentFingerIdBits); |
| 4844 | for (uint32_t i = 0; i < currentFingerCount; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4845 | uint32_t touchId = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4846 | uint32_t gestureId; |
| 4847 | if (!mappedTouchIdBits.hasBit(touchId)) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4848 | gestureId = usedGestureIdBits.markFirstUnmarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4849 | mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId; |
| 4850 | #if DEBUG_GESTURES |
| 4851 | LOGD("Gestures: FREEFORM " |
| 4852 | "new mapping for touch id %d -> gesture id %d", |
| 4853 | touchId, gestureId); |
| 4854 | #endif |
| 4855 | } else { |
| 4856 | gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId]; |
| 4857 | #if DEBUG_GESTURES |
| 4858 | LOGD("Gestures: FREEFORM " |
| 4859 | "existing mapping for touch id %d -> gesture id %d", |
| 4860 | touchId, gestureId); |
| 4861 | #endif |
| 4862 | } |
| 4863 | mPointerGesture.currentGestureIdBits.markBit(gestureId); |
| 4864 | mPointerGesture.currentGestureIdToIndex[gestureId] = i; |
| 4865 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4866 | const RawPointerData::Pointer& pointer = |
| 4867 | mCurrentRawPointerData.pointerForId(touchId); |
| 4868 | float deltaX = (pointer.x - mPointerGesture.referenceTouchX) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4869 | * mPointerXZoomScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4870 | float deltaY = (pointer.y - mPointerGesture.referenceTouchY) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4871 | * mPointerYZoomScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4872 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4873 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4874 | mPointerGesture.currentGestureProperties[i].clear(); |
| 4875 | mPointerGesture.currentGestureProperties[i].id = gestureId; |
| 4876 | mPointerGesture.currentGestureProperties[i].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4877 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4878 | mPointerGesture.currentGestureCoords[i].clear(); |
| 4879 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 4880 | AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4881 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 4882 | AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4883 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
| 4884 | AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
| 4885 | } |
| 4886 | |
| 4887 | if (mPointerGesture.activeGestureId < 0) { |
| 4888 | mPointerGesture.activeGestureId = |
| 4889 | mPointerGesture.currentGestureIdBits.firstMarkedBit(); |
| 4890 | #if DEBUG_GESTURES |
| 4891 | LOGD("Gestures: FREEFORM new " |
| 4892 | "activeGestureId=%d", mPointerGesture.activeGestureId); |
| 4893 | #endif |
| 4894 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4895 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4896 | } |
| 4897 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4898 | mPointerController->setButtonState(mCurrentButtonState); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4899 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4900 | #if DEBUG_GESTURES |
| 4901 | LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, " |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4902 | "currentGestureMode=%d, currentGestureIdBits=0x%08x, " |
| 4903 | "lastGestureMode=%d, lastGestureIdBits=0x%08x", |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4904 | toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture), |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4905 | mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value, |
| 4906 | mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4907 | for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4908 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4909 | uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4910 | const PointerProperties& properties = mPointerGesture.currentGestureProperties[index]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4911 | const PointerCoords& coords = mPointerGesture.currentGestureCoords[index]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4912 | LOGD(" currentGesture[%d]: index=%d, toolType=%d, " |
| 4913 | "x=%0.3f, y=%0.3f, pressure=%0.3f", |
| 4914 | id, index, properties.toolType, |
| 4915 | coords.getAxisValue(AMOTION_EVENT_AXIS_X), |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4916 | coords.getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4917 | coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); |
| 4918 | } |
| 4919 | for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4920 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4921 | uint32_t index = mPointerGesture.lastGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4922 | const PointerProperties& properties = mPointerGesture.lastGestureProperties[index]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4923 | const PointerCoords& coords = mPointerGesture.lastGestureCoords[index]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4924 | LOGD(" lastGesture[%d]: index=%d, toolType=%d, " |
| 4925 | "x=%0.3f, y=%0.3f, pressure=%0.3f", |
| 4926 | id, index, properties.toolType, |
| 4927 | coords.getAxisValue(AMOTION_EVENT_AXIS_X), |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4928 | coords.getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4929 | coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); |
| 4930 | } |
| 4931 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4932 | return true; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4933 | } |
| 4934 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4935 | void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) { |
| 4936 | mPointerSimple.currentCoords.clear(); |
| 4937 | mPointerSimple.currentProperties.clear(); |
| 4938 | |
| 4939 | bool down, hovering; |
| 4940 | if (!mCurrentStylusIdBits.isEmpty()) { |
| 4941 | uint32_t id = mCurrentStylusIdBits.firstMarkedBit(); |
| 4942 | uint32_t index = mCurrentCookedPointerData.idToIndex[id]; |
| 4943 | float x = mCurrentCookedPointerData.pointerCoords[index].getX(); |
| 4944 | float y = mCurrentCookedPointerData.pointerCoords[index].getY(); |
| 4945 | mPointerController->setPosition(x, y); |
| 4946 | |
| 4947 | hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id); |
| 4948 | down = !hovering; |
| 4949 | |
| 4950 | mPointerController->getPosition(&x, &y); |
| 4951 | mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]); |
| 4952 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4953 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 4954 | mPointerSimple.currentProperties.id = 0; |
| 4955 | mPointerSimple.currentProperties.toolType = |
| 4956 | mCurrentCookedPointerData.pointerProperties[index].toolType; |
| 4957 | } else { |
| 4958 | down = false; |
| 4959 | hovering = false; |
| 4960 | } |
| 4961 | |
| 4962 | dispatchPointerSimple(when, policyFlags, down, hovering); |
| 4963 | } |
| 4964 | |
| 4965 | void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) { |
| 4966 | abortPointerSimple(when, policyFlags); |
| 4967 | } |
| 4968 | |
| 4969 | void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) { |
| 4970 | mPointerSimple.currentCoords.clear(); |
| 4971 | mPointerSimple.currentProperties.clear(); |
| 4972 | |
| 4973 | bool down, hovering; |
| 4974 | if (!mCurrentMouseIdBits.isEmpty()) { |
| 4975 | uint32_t id = mCurrentMouseIdBits.firstMarkedBit(); |
| 4976 | uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id]; |
| 4977 | if (mLastMouseIdBits.hasBit(id)) { |
| 4978 | uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id]; |
| 4979 | float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x |
| 4980 | - mLastRawPointerData.pointers[lastIndex].x) |
| 4981 | * mPointerXMovementScale; |
| 4982 | float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y |
| 4983 | - mLastRawPointerData.pointers[lastIndex].y) |
| 4984 | * mPointerYMovementScale; |
| 4985 | |
| 4986 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
| 4987 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
| 4988 | |
| 4989 | mPointerController->move(deltaX, deltaY); |
| 4990 | } else { |
| 4991 | mPointerVelocityControl.reset(); |
| 4992 | } |
| 4993 | |
| 4994 | down = isPointerDown(mCurrentButtonState); |
| 4995 | hovering = !down; |
| 4996 | |
| 4997 | float x, y; |
| 4998 | mPointerController->getPosition(&x, &y); |
| 4999 | mPointerSimple.currentCoords.copyFrom( |
| 5000 | mCurrentCookedPointerData.pointerCoords[currentIndex]); |
| 5001 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 5002 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 5003 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, |
| 5004 | hovering ? 0.0f : 1.0f); |
| 5005 | mPointerSimple.currentProperties.id = 0; |
| 5006 | mPointerSimple.currentProperties.toolType = |
| 5007 | mCurrentCookedPointerData.pointerProperties[currentIndex].toolType; |
| 5008 | } else { |
| 5009 | mPointerVelocityControl.reset(); |
| 5010 | |
| 5011 | down = false; |
| 5012 | hovering = false; |
| 5013 | } |
| 5014 | |
| 5015 | dispatchPointerSimple(when, policyFlags, down, hovering); |
| 5016 | } |
| 5017 | |
| 5018 | void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) { |
| 5019 | abortPointerSimple(when, policyFlags); |
| 5020 | |
| 5021 | mPointerVelocityControl.reset(); |
| 5022 | } |
| 5023 | |
| 5024 | void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags, |
| 5025 | bool down, bool hovering) { |
| 5026 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 5027 | |
| 5028 | if (mPointerController != NULL) { |
| 5029 | if (down || hovering) { |
| 5030 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER); |
| 5031 | mPointerController->clearSpots(); |
| 5032 | mPointerController->setButtonState(mCurrentButtonState); |
| 5033 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 5034 | } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) { |
| 5035 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 5036 | } |
| 5037 | } |
| 5038 | |
| 5039 | if (mPointerSimple.down && !down) { |
| 5040 | mPointerSimple.down = false; |
| 5041 | |
| 5042 | // Send up. |
| 5043 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5044 | AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0, |
| 5045 | 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords, |
| 5046 | mOrientedXPrecision, mOrientedYPrecision, |
| 5047 | mPointerSimple.downTime); |
| 5048 | getListener()->notifyMotion(&args); |
| 5049 | } |
| 5050 | |
| 5051 | if (mPointerSimple.hovering && !hovering) { |
| 5052 | mPointerSimple.hovering = false; |
| 5053 | |
| 5054 | // Send hover exit. |
| 5055 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5056 | AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0, |
| 5057 | 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords, |
| 5058 | mOrientedXPrecision, mOrientedYPrecision, |
| 5059 | mPointerSimple.downTime); |
| 5060 | getListener()->notifyMotion(&args); |
| 5061 | } |
| 5062 | |
| 5063 | if (down) { |
| 5064 | if (!mPointerSimple.down) { |
| 5065 | mPointerSimple.down = true; |
| 5066 | mPointerSimple.downTime = when; |
| 5067 | |
| 5068 | // Send down. |
| 5069 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5070 | AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0, |
| 5071 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5072 | mOrientedXPrecision, mOrientedYPrecision, |
| 5073 | mPointerSimple.downTime); |
| 5074 | getListener()->notifyMotion(&args); |
| 5075 | } |
| 5076 | |
| 5077 | // Send move. |
| 5078 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5079 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0, |
| 5080 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5081 | mOrientedXPrecision, mOrientedYPrecision, |
| 5082 | mPointerSimple.downTime); |
| 5083 | getListener()->notifyMotion(&args); |
| 5084 | } |
| 5085 | |
| 5086 | if (hovering) { |
| 5087 | if (!mPointerSimple.hovering) { |
| 5088 | mPointerSimple.hovering = true; |
| 5089 | |
| 5090 | // Send hover enter. |
| 5091 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5092 | AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0, |
| 5093 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5094 | mOrientedXPrecision, mOrientedYPrecision, |
| 5095 | mPointerSimple.downTime); |
| 5096 | getListener()->notifyMotion(&args); |
| 5097 | } |
| 5098 | |
| 5099 | // Send hover move. |
| 5100 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5101 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0, |
| 5102 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5103 | mOrientedXPrecision, mOrientedYPrecision, |
| 5104 | mPointerSimple.downTime); |
| 5105 | getListener()->notifyMotion(&args); |
| 5106 | } |
| 5107 | |
| 5108 | if (mCurrentRawVScroll || mCurrentRawHScroll) { |
| 5109 | float vscroll = mCurrentRawVScroll; |
| 5110 | float hscroll = mCurrentRawHScroll; |
| 5111 | mWheelYVelocityControl.move(when, NULL, &vscroll); |
| 5112 | mWheelXVelocityControl.move(when, &hscroll, NULL); |
| 5113 | |
| 5114 | // Send scroll. |
| 5115 | PointerCoords pointerCoords; |
| 5116 | pointerCoords.copyFrom(mPointerSimple.currentCoords); |
| 5117 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll); |
| 5118 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll); |
| 5119 | |
| 5120 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5121 | AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0, |
| 5122 | 1, &mPointerSimple.currentProperties, &pointerCoords, |
| 5123 | mOrientedXPrecision, mOrientedYPrecision, |
| 5124 | mPointerSimple.downTime); |
| 5125 | getListener()->notifyMotion(&args); |
| 5126 | } |
| 5127 | |
| 5128 | // Save state. |
| 5129 | if (down || hovering) { |
| 5130 | mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords); |
| 5131 | mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties); |
| 5132 | } else { |
| 5133 | mPointerSimple.reset(); |
| 5134 | } |
| 5135 | } |
| 5136 | |
| 5137 | void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) { |
| 5138 | mPointerSimple.currentCoords.clear(); |
| 5139 | mPointerSimple.currentProperties.clear(); |
| 5140 | |
| 5141 | dispatchPointerSimple(when, policyFlags, false, false); |
| 5142 | } |
| 5143 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5144 | void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5145 | int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags, |
| 5146 | const PointerProperties* properties, const PointerCoords* coords, |
| 5147 | const uint32_t* idToIndex, BitSet32 idBits, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5148 | int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) { |
| 5149 | PointerCoords pointerCoords[MAX_POINTERS]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5150 | PointerProperties pointerProperties[MAX_POINTERS]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5151 | uint32_t pointerCount = 0; |
| 5152 | while (!idBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5153 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5154 | uint32_t index = idToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5155 | pointerProperties[pointerCount].copyFrom(properties[index]); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5156 | pointerCoords[pointerCount].copyFrom(coords[index]); |
| 5157 | |
| 5158 | if (changedId >= 0 && id == uint32_t(changedId)) { |
| 5159 | action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
| 5160 | } |
| 5161 | |
| 5162 | pointerCount += 1; |
| 5163 | } |
| 5164 | |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 5165 | LOG_ASSERT(pointerCount != 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5166 | |
| 5167 | if (changedId >= 0 && pointerCount == 1) { |
| 5168 | // Replace initial down and final up action. |
| 5169 | // We can compare the action without masking off the changed pointer index |
| 5170 | // because we know the index is 0. |
| 5171 | if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 5172 | action = AMOTION_EVENT_ACTION_DOWN; |
| 5173 | } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 5174 | action = AMOTION_EVENT_ACTION_UP; |
| 5175 | } else { |
| 5176 | // Can't happen. |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 5177 | LOG_ASSERT(false); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5178 | } |
| 5179 | } |
| 5180 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5181 | NotifyMotionArgs args(when, getDeviceId(), source, policyFlags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5182 | action, flags, metaState, buttonState, edgeFlags, |
| 5183 | pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5184 | getListener()->notifyMotion(&args); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5185 | } |
| 5186 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5187 | bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5188 | const PointerCoords* inCoords, const uint32_t* inIdToIndex, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5189 | PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex, |
| 5190 | BitSet32 idBits) const { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5191 | bool changed = false; |
| 5192 | while (!idBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5193 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5194 | uint32_t inIndex = inIdToIndex[id]; |
| 5195 | uint32_t outIndex = outIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5196 | |
| 5197 | const PointerProperties& curInProperties = inProperties[inIndex]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5198 | const PointerCoords& curInCoords = inCoords[inIndex]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5199 | PointerProperties& curOutProperties = outProperties[outIndex]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5200 | PointerCoords& curOutCoords = outCoords[outIndex]; |
| 5201 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5202 | if (curInProperties != curOutProperties) { |
| 5203 | curOutProperties.copyFrom(curInProperties); |
| 5204 | changed = true; |
| 5205 | } |
| 5206 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5207 | if (curInCoords != curOutCoords) { |
| 5208 | curOutCoords.copyFrom(curInCoords); |
| 5209 | changed = true; |
| 5210 | } |
| 5211 | } |
| 5212 | return changed; |
| 5213 | } |
| 5214 | |
| 5215 | void TouchInputMapper::fadePointer() { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5216 | if (mPointerController != NULL) { |
| 5217 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 5218 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5219 | } |
| 5220 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5221 | bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) { |
| 5222 | return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue |
| 5223 | && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5224 | } |
| 5225 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5226 | const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit( |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 5227 | int32_t x, int32_t y) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5228 | size_t numVirtualKeys = mVirtualKeys.size(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 5229 | for (size_t i = 0; i < numVirtualKeys; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5230 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5231 | |
| 5232 | #if DEBUG_VIRTUAL_KEYS |
| 5233 | LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, " |
| 5234 | "left=%d, top=%d, right=%d, bottom=%d", |
| 5235 | x, y, |
| 5236 | virtualKey.keyCode, virtualKey.scanCode, |
| 5237 | virtualKey.hitLeft, virtualKey.hitTop, |
| 5238 | virtualKey.hitRight, virtualKey.hitBottom); |
| 5239 | #endif |
| 5240 | |
| 5241 | if (virtualKey.isHit(x, y)) { |
| 5242 | return & virtualKey; |
| 5243 | } |
| 5244 | } |
| 5245 | |
| 5246 | return NULL; |
| 5247 | } |
| 5248 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5249 | void TouchInputMapper::assignPointerIds() { |
| 5250 | uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount; |
| 5251 | uint32_t lastPointerCount = mLastRawPointerData.pointerCount; |
| 5252 | |
| 5253 | mCurrentRawPointerData.clearIdBits(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5254 | |
| 5255 | if (currentPointerCount == 0) { |
| 5256 | // No pointers to assign. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5257 | return; |
| 5258 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5259 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5260 | if (lastPointerCount == 0) { |
| 5261 | // All pointers are new. |
| 5262 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
| 5263 | uint32_t id = i; |
| 5264 | mCurrentRawPointerData.pointers[i].id = id; |
| 5265 | mCurrentRawPointerData.idToIndex[id] = i; |
| 5266 | mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i)); |
| 5267 | } |
| 5268 | return; |
| 5269 | } |
| 5270 | |
| 5271 | if (currentPointerCount == 1 && lastPointerCount == 1 |
| 5272 | && mCurrentRawPointerData.pointers[0].toolType |
| 5273 | == mLastRawPointerData.pointers[0].toolType) { |
| 5274 | // Only one pointer and no change in count so it must have the same id as before. |
| 5275 | uint32_t id = mLastRawPointerData.pointers[0].id; |
| 5276 | mCurrentRawPointerData.pointers[0].id = id; |
| 5277 | mCurrentRawPointerData.idToIndex[id] = 0; |
| 5278 | mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0)); |
| 5279 | return; |
| 5280 | } |
| 5281 | |
| 5282 | // General case. |
| 5283 | // We build a heap of squared euclidean distances between current and last pointers |
| 5284 | // associated with the current and last pointer indices. Then, we find the best |
| 5285 | // match (by distance) for each current pointer. |
| 5286 | // The pointers must have the same tool type but it is possible for them to |
| 5287 | // transition from hovering to touching or vice-versa while retaining the same id. |
| 5288 | PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS]; |
| 5289 | |
| 5290 | uint32_t heapSize = 0; |
| 5291 | for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount; |
| 5292 | currentPointerIndex++) { |
| 5293 | for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount; |
| 5294 | lastPointerIndex++) { |
| 5295 | const RawPointerData::Pointer& currentPointer = |
| 5296 | mCurrentRawPointerData.pointers[currentPointerIndex]; |
| 5297 | const RawPointerData::Pointer& lastPointer = |
| 5298 | mLastRawPointerData.pointers[lastPointerIndex]; |
| 5299 | if (currentPointer.toolType == lastPointer.toolType) { |
| 5300 | int64_t deltaX = currentPointer.x - lastPointer.x; |
| 5301 | int64_t deltaY = currentPointer.y - lastPointer.y; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5302 | |
| 5303 | uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY); |
| 5304 | |
| 5305 | // Insert new element into the heap (sift up). |
| 5306 | heap[heapSize].currentPointerIndex = currentPointerIndex; |
| 5307 | heap[heapSize].lastPointerIndex = lastPointerIndex; |
| 5308 | heap[heapSize].distance = distance; |
| 5309 | heapSize += 1; |
| 5310 | } |
| 5311 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5312 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5313 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5314 | // Heapify |
| 5315 | for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) { |
| 5316 | startIndex -= 1; |
| 5317 | for (uint32_t parentIndex = startIndex; ;) { |
| 5318 | uint32_t childIndex = parentIndex * 2 + 1; |
| 5319 | if (childIndex >= heapSize) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5320 | break; |
| 5321 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5322 | |
| 5323 | if (childIndex + 1 < heapSize |
| 5324 | && heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 5325 | childIndex += 1; |
| 5326 | } |
| 5327 | |
| 5328 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 5329 | break; |
| 5330 | } |
| 5331 | |
| 5332 | swap(heap[parentIndex], heap[childIndex]); |
| 5333 | parentIndex = childIndex; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5334 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5335 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5336 | |
| 5337 | #if DEBUG_POINTER_ASSIGNMENT |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5338 | LOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize); |
| 5339 | for (size_t i = 0; i < heapSize; i++) { |
| 5340 | LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", |
| 5341 | i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, |
| 5342 | heap[i].distance); |
| 5343 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5344 | #endif |
| 5345 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5346 | // Pull matches out by increasing order of distance. |
| 5347 | // To avoid reassigning pointers that have already been matched, the loop keeps track |
| 5348 | // of which last and current pointers have been matched using the matchedXXXBits variables. |
| 5349 | // It also tracks the used pointer id bits. |
| 5350 | BitSet32 matchedLastBits(0); |
| 5351 | BitSet32 matchedCurrentBits(0); |
| 5352 | BitSet32 usedIdBits(0); |
| 5353 | bool first = true; |
| 5354 | for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) { |
| 5355 | while (heapSize > 0) { |
| 5356 | if (first) { |
| 5357 | // The first time through the loop, we just consume the root element of |
| 5358 | // the heap (the one with smallest distance). |
| 5359 | first = false; |
| 5360 | } else { |
| 5361 | // Previous iterations consumed the root element of the heap. |
| 5362 | // Pop root element off of the heap (sift down). |
| 5363 | heap[0] = heap[heapSize]; |
| 5364 | for (uint32_t parentIndex = 0; ;) { |
| 5365 | uint32_t childIndex = parentIndex * 2 + 1; |
| 5366 | if (childIndex >= heapSize) { |
| 5367 | break; |
| 5368 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5369 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5370 | if (childIndex + 1 < heapSize |
| 5371 | && heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 5372 | childIndex += 1; |
| 5373 | } |
| 5374 | |
| 5375 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 5376 | break; |
| 5377 | } |
| 5378 | |
| 5379 | swap(heap[parentIndex], heap[childIndex]); |
| 5380 | parentIndex = childIndex; |
| 5381 | } |
| 5382 | |
| 5383 | #if DEBUG_POINTER_ASSIGNMENT |
| 5384 | LOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize); |
| 5385 | for (size_t i = 0; i < heapSize; i++) { |
| 5386 | LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", |
| 5387 | i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, |
| 5388 | heap[i].distance); |
| 5389 | } |
| 5390 | #endif |
| 5391 | } |
| 5392 | |
| 5393 | heapSize -= 1; |
| 5394 | |
| 5395 | uint32_t currentPointerIndex = heap[0].currentPointerIndex; |
| 5396 | if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched |
| 5397 | |
| 5398 | uint32_t lastPointerIndex = heap[0].lastPointerIndex; |
| 5399 | if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched |
| 5400 | |
| 5401 | matchedCurrentBits.markBit(currentPointerIndex); |
| 5402 | matchedLastBits.markBit(lastPointerIndex); |
| 5403 | |
| 5404 | uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id; |
| 5405 | mCurrentRawPointerData.pointers[currentPointerIndex].id = id; |
| 5406 | mCurrentRawPointerData.idToIndex[id] = currentPointerIndex; |
| 5407 | mCurrentRawPointerData.markIdBit(id, |
| 5408 | mCurrentRawPointerData.isHovering(currentPointerIndex)); |
| 5409 | usedIdBits.markBit(id); |
| 5410 | |
| 5411 | #if DEBUG_POINTER_ASSIGNMENT |
| 5412 | LOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld", |
| 5413 | lastPointerIndex, currentPointerIndex, id, heap[0].distance); |
| 5414 | #endif |
| 5415 | break; |
| 5416 | } |
| 5417 | } |
| 5418 | |
| 5419 | // Assign fresh ids to pointers that were not matched in the process. |
| 5420 | for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) { |
| 5421 | uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit(); |
| 5422 | uint32_t id = usedIdBits.markFirstUnmarkedBit(); |
| 5423 | |
| 5424 | mCurrentRawPointerData.pointers[currentPointerIndex].id = id; |
| 5425 | mCurrentRawPointerData.idToIndex[id] = currentPointerIndex; |
| 5426 | mCurrentRawPointerData.markIdBit(id, |
| 5427 | mCurrentRawPointerData.isHovering(currentPointerIndex)); |
| 5428 | |
| 5429 | #if DEBUG_POINTER_ASSIGNMENT |
| 5430 | LOGD("assignPointerIds - assigned: cur=%d, id=%d", |
| 5431 | currentPointerIndex, id); |
| 5432 | #endif |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5433 | } |
| 5434 | } |
| 5435 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5436 | int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5437 | if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) { |
| 5438 | return AKEY_STATE_VIRTUAL; |
| 5439 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5440 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5441 | size_t numVirtualKeys = mVirtualKeys.size(); |
| 5442 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 5443 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
| 5444 | if (virtualKey.keyCode == keyCode) { |
| 5445 | return AKEY_STATE_UP; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5446 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5447 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5448 | |
| 5449 | return AKEY_STATE_UNKNOWN; |
| 5450 | } |
| 5451 | |
| 5452 | int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5453 | if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) { |
| 5454 | return AKEY_STATE_VIRTUAL; |
| 5455 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5456 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5457 | size_t numVirtualKeys = mVirtualKeys.size(); |
| 5458 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 5459 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
| 5460 | if (virtualKey.scanCode == scanCode) { |
| 5461 | return AKEY_STATE_UP; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5462 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5463 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5464 | |
| 5465 | return AKEY_STATE_UNKNOWN; |
| 5466 | } |
| 5467 | |
| 5468 | bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 5469 | const int32_t* keyCodes, uint8_t* outFlags) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5470 | size_t numVirtualKeys = mVirtualKeys.size(); |
| 5471 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 5472 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5473 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5474 | for (size_t i = 0; i < numCodes; i++) { |
| 5475 | if (virtualKey.keyCode == keyCodes[i]) { |
| 5476 | outFlags[i] = 1; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5477 | } |
| 5478 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5479 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5480 | |
| 5481 | return true; |
| 5482 | } |
| 5483 | |
| 5484 | |
| 5485 | // --- SingleTouchInputMapper --- |
| 5486 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 5487 | SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) : |
| 5488 | TouchInputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5489 | } |
| 5490 | |
| 5491 | SingleTouchInputMapper::~SingleTouchInputMapper() { |
| 5492 | } |
| 5493 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5494 | void SingleTouchInputMapper::reset(nsecs_t when) { |
| 5495 | mSingleTouchMotionAccumulator.reset(getDevice()); |
| 5496 | |
| 5497 | TouchInputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5498 | } |
| 5499 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5500 | void SingleTouchInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5501 | TouchInputMapper::process(rawEvent); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5502 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5503 | mSingleTouchMotionAccumulator.process(rawEvent); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5504 | } |
| 5505 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5506 | void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) { |
Jeff Brown | d87c6d5 | 2011-08-10 14:55:59 -0700 | [diff] [blame] | 5507 | if (mTouchButtonAccumulator.isToolActive()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5508 | mCurrentRawPointerData.pointerCount = 1; |
| 5509 | mCurrentRawPointerData.idToIndex[0] = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5510 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5511 | bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE |
| 5512 | && (mTouchButtonAccumulator.isHovering() |
| 5513 | || (mRawPointerAxes.pressure.valid |
| 5514 | && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0)); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5515 | mCurrentRawPointerData.markIdBit(0, isHovering); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5516 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5517 | RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0]; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5518 | outPointer.id = 0; |
| 5519 | outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX(); |
| 5520 | outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY(); |
| 5521 | outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure(); |
| 5522 | outPointer.touchMajor = 0; |
| 5523 | outPointer.touchMinor = 0; |
| 5524 | outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth(); |
| 5525 | outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth(); |
| 5526 | outPointer.orientation = 0; |
| 5527 | outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5528 | outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX(); |
| 5529 | outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY(); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5530 | outPointer.toolType = mTouchButtonAccumulator.getToolType(); |
| 5531 | if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 5532 | outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 5533 | } |
| 5534 | outPointer.isHovering = isHovering; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5535 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5536 | } |
| 5537 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5538 | void SingleTouchInputMapper::configureRawPointerAxes() { |
| 5539 | TouchInputMapper::configureRawPointerAxes(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5540 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5541 | getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x); |
| 5542 | getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y); |
| 5543 | getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure); |
| 5544 | getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor); |
| 5545 | getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5546 | getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX); |
| 5547 | getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5548 | } |
| 5549 | |
| 5550 | |
| 5551 | // --- MultiTouchInputMapper --- |
| 5552 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 5553 | MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) : |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5554 | TouchInputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5555 | } |
| 5556 | |
| 5557 | MultiTouchInputMapper::~MultiTouchInputMapper() { |
| 5558 | } |
| 5559 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5560 | void MultiTouchInputMapper::reset(nsecs_t when) { |
| 5561 | mMultiTouchMotionAccumulator.reset(getDevice()); |
| 5562 | |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5563 | mPointerIdBits.clear(); |
Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 5564 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5565 | TouchInputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5566 | } |
| 5567 | |
| 5568 | void MultiTouchInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5569 | TouchInputMapper::process(rawEvent); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5570 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5571 | mMultiTouchMotionAccumulator.process(rawEvent); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5572 | } |
| 5573 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5574 | void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5575 | size_t inCount = mMultiTouchMotionAccumulator.getSlotCount(); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5576 | size_t outCount = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5577 | BitSet32 newPointerIdBits; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5578 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5579 | for (size_t inIndex = 0; inIndex < inCount; inIndex++) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5580 | const MultiTouchMotionAccumulator::Slot* inSlot = |
| 5581 | mMultiTouchMotionAccumulator.getSlot(inIndex); |
| 5582 | if (!inSlot->isInUse()) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5583 | continue; |
| 5584 | } |
| 5585 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5586 | if (outCount >= MAX_POINTERS) { |
| 5587 | #if DEBUG_POINTERS |
| 5588 | LOGD("MultiTouch device %s emitted more than maximum of %d pointers; " |
| 5589 | "ignoring the rest.", |
| 5590 | getDeviceName().string(), MAX_POINTERS); |
| 5591 | #endif |
| 5592 | break; // too many fingers! |
| 5593 | } |
| 5594 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5595 | RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount]; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5596 | outPointer.x = inSlot->getX(); |
| 5597 | outPointer.y = inSlot->getY(); |
| 5598 | outPointer.pressure = inSlot->getPressure(); |
| 5599 | outPointer.touchMajor = inSlot->getTouchMajor(); |
| 5600 | outPointer.touchMinor = inSlot->getTouchMinor(); |
| 5601 | outPointer.toolMajor = inSlot->getToolMajor(); |
| 5602 | outPointer.toolMinor = inSlot->getToolMinor(); |
| 5603 | outPointer.orientation = inSlot->getOrientation(); |
| 5604 | outPointer.distance = inSlot->getDistance(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5605 | outPointer.tiltX = 0; |
| 5606 | outPointer.tiltY = 0; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5607 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5608 | outPointer.toolType = inSlot->getToolType(); |
| 5609 | if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 5610 | outPointer.toolType = mTouchButtonAccumulator.getToolType(); |
| 5611 | if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 5612 | outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 5613 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5614 | } |
| 5615 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5616 | bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE |
| 5617 | && (mTouchButtonAccumulator.isHovering() |
| 5618 | || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0)); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5619 | outPointer.isHovering = isHovering; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5620 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5621 | // Assign pointer id using tracking id if available. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5622 | if (*outHavePointerIds) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5623 | int32_t trackingId = inSlot->getTrackingId(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5624 | int32_t id = -1; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5625 | if (trackingId >= 0) { |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5626 | for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5627 | uint32_t n = idBits.clearFirstMarkedBit(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5628 | if (mPointerTrackingIdMap[n] == trackingId) { |
| 5629 | id = n; |
| 5630 | } |
| 5631 | } |
| 5632 | |
| 5633 | if (id < 0 && !mPointerIdBits.isFull()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5634 | id = mPointerIdBits.markFirstUnmarkedBit(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5635 | mPointerTrackingIdMap[id] = trackingId; |
| 5636 | } |
| 5637 | } |
| 5638 | if (id < 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5639 | *outHavePointerIds = false; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5640 | mCurrentRawPointerData.clearIdBits(); |
| 5641 | newPointerIdBits.clear(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5642 | } else { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5643 | outPointer.id = id; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5644 | mCurrentRawPointerData.idToIndex[id] = outCount; |
| 5645 | mCurrentRawPointerData.markIdBit(id, isHovering); |
| 5646 | newPointerIdBits.markBit(id); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5647 | } |
| 5648 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5649 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5650 | outCount += 1; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5651 | } |
| 5652 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5653 | mCurrentRawPointerData.pointerCount = outCount; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5654 | mPointerIdBits = newPointerIdBits; |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5655 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5656 | mMultiTouchMotionAccumulator.finishSync(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5657 | } |
| 5658 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5659 | void MultiTouchInputMapper::configureRawPointerAxes() { |
| 5660 | TouchInputMapper::configureRawPointerAxes(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5661 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5662 | getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x); |
| 5663 | getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y); |
| 5664 | getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor); |
| 5665 | getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor); |
| 5666 | getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor); |
| 5667 | getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor); |
| 5668 | getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation); |
| 5669 | getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure); |
| 5670 | getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance); |
| 5671 | getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId); |
| 5672 | getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5673 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5674 | if (mRawPointerAxes.trackingId.valid |
| 5675 | && mRawPointerAxes.slot.valid |
| 5676 | && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) { |
| 5677 | size_t slotCount = mRawPointerAxes.slot.maxValue + 1; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5678 | if (slotCount > MAX_SLOTS) { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5679 | LOGW("MultiTouch Device %s reported %d slots but the framework " |
| 5680 | "only supports a maximum of %d slots at this time.", |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5681 | getDeviceName().string(), slotCount, MAX_SLOTS); |
| 5682 | slotCount = MAX_SLOTS; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5683 | } |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5684 | mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5685 | } else { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5686 | mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5687 | } |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 5688 | } |
| 5689 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5690 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5691 | // --- JoystickInputMapper --- |
| 5692 | |
| 5693 | JoystickInputMapper::JoystickInputMapper(InputDevice* device) : |
| 5694 | InputMapper(device) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5695 | } |
| 5696 | |
| 5697 | JoystickInputMapper::~JoystickInputMapper() { |
| 5698 | } |
| 5699 | |
| 5700 | uint32_t JoystickInputMapper::getSources() { |
| 5701 | return AINPUT_SOURCE_JOYSTICK; |
| 5702 | } |
| 5703 | |
| 5704 | void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 5705 | InputMapper::populateDeviceInfo(info); |
| 5706 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5707 | for (size_t i = 0; i < mAxes.size(); i++) { |
| 5708 | const Axis& axis = mAxes.valueAt(i); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 5709 | info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK, |
| 5710 | axis.min, axis.max, axis.flat, axis.fuzz); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5711 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 5712 | info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK, |
| 5713 | axis.min, axis.max, axis.flat, axis.fuzz); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5714 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5715 | } |
| 5716 | } |
| 5717 | |
| 5718 | void JoystickInputMapper::dump(String8& dump) { |
| 5719 | dump.append(INDENT2 "Joystick Input Mapper:\n"); |
| 5720 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5721 | dump.append(INDENT3 "Axes:\n"); |
| 5722 | size_t numAxes = mAxes.size(); |
| 5723 | for (size_t i = 0; i < numAxes; i++) { |
| 5724 | const Axis& axis = mAxes.valueAt(i); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5725 | const char* label = getAxisLabel(axis.axisInfo.axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5726 | if (label) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5727 | dump.appendFormat(INDENT4 "%s", label); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5728 | } else { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5729 | dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5730 | } |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5731 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 5732 | label = getAxisLabel(axis.axisInfo.highAxis); |
| 5733 | if (label) { |
| 5734 | dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue); |
| 5735 | } else { |
| 5736 | dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis, |
| 5737 | axis.axisInfo.splitValue); |
| 5738 | } |
| 5739 | } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) { |
| 5740 | dump.append(" (invert)"); |
| 5741 | } |
| 5742 | |
| 5743 | dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n", |
| 5744 | axis.min, axis.max, axis.flat, axis.fuzz); |
| 5745 | dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, " |
| 5746 | "highScale=%0.5f, highOffset=%0.5f\n", |
| 5747 | axis.scale, axis.offset, axis.highScale, axis.highOffset); |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 5748 | dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, " |
| 5749 | "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n", |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5750 | mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue, |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 5751 | axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5752 | } |
| 5753 | } |
| 5754 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5755 | void JoystickInputMapper::configure(nsecs_t when, |
| 5756 | const InputReaderConfiguration* config, uint32_t changes) { |
| 5757 | InputMapper::configure(when, config, changes); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5758 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5759 | if (!changes) { // first time only |
| 5760 | // Collect all axes. |
| 5761 | for (int32_t abs = 0; abs <= ABS_MAX; abs++) { |
| 5762 | RawAbsoluteAxisInfo rawAxisInfo; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5763 | getAbsoluteAxisInfo(abs, &rawAxisInfo); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5764 | if (rawAxisInfo.valid) { |
| 5765 | // Map axis. |
| 5766 | AxisInfo axisInfo; |
| 5767 | bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo); |
| 5768 | if (!explicitlyMapped) { |
| 5769 | // Axis is not explicitly mapped, will choose a generic axis later. |
| 5770 | axisInfo.mode = AxisInfo::MODE_NORMAL; |
| 5771 | axisInfo.axis = -1; |
| 5772 | } |
| 5773 | |
| 5774 | // Apply flat override. |
| 5775 | int32_t rawFlat = axisInfo.flatOverride < 0 |
| 5776 | ? rawAxisInfo.flat : axisInfo.flatOverride; |
| 5777 | |
| 5778 | // Calculate scaling factors and limits. |
| 5779 | Axis axis; |
| 5780 | if (axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 5781 | float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue); |
| 5782 | float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue); |
| 5783 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 5784 | scale, 0.0f, highScale, 0.0f, |
| 5785 | 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
| 5786 | } else if (isCenteredAxis(axisInfo.axis)) { |
| 5787 | float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); |
| 5788 | float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale; |
| 5789 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 5790 | scale, offset, scale, offset, |
| 5791 | -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
| 5792 | } else { |
| 5793 | float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); |
| 5794 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 5795 | scale, 0.0f, scale, 0.0f, |
| 5796 | 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
| 5797 | } |
| 5798 | |
| 5799 | // To eliminate noise while the joystick is at rest, filter out small variations |
| 5800 | // in axis values up front. |
| 5801 | axis.filter = axis.flat * 0.25f; |
| 5802 | |
| 5803 | mAxes.add(abs, axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5804 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5805 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5806 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5807 | // If there are too many axes, start dropping them. |
| 5808 | // Prefer to keep explicitly mapped axes. |
| 5809 | if (mAxes.size() > PointerCoords::MAX_AXES) { |
| 5810 | LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.", |
| 5811 | getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES); |
| 5812 | pruneAxes(true); |
| 5813 | pruneAxes(false); |
| 5814 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5815 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5816 | // Assign generic axis ids to remaining axes. |
| 5817 | int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1; |
| 5818 | size_t numAxes = mAxes.size(); |
| 5819 | for (size_t i = 0; i < numAxes; i++) { |
| 5820 | Axis& axis = mAxes.editValueAt(i); |
| 5821 | if (axis.axisInfo.axis < 0) { |
| 5822 | while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16 |
| 5823 | && haveAxis(nextGenericAxisId)) { |
| 5824 | nextGenericAxisId += 1; |
| 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 (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) { |
| 5828 | axis.axisInfo.axis = nextGenericAxisId; |
| 5829 | nextGenericAxisId += 1; |
| 5830 | } else { |
| 5831 | LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids " |
| 5832 | "have already been assigned to other axes.", |
| 5833 | getDeviceName().string(), mAxes.keyAt(i)); |
| 5834 | mAxes.removeItemsAt(i--); |
| 5835 | numAxes -= 1; |
| 5836 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5837 | } |
| 5838 | } |
| 5839 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5840 | } |
| 5841 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5842 | bool JoystickInputMapper::haveAxis(int32_t axisId) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5843 | size_t numAxes = mAxes.size(); |
| 5844 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5845 | const Axis& axis = mAxes.valueAt(i); |
| 5846 | if (axis.axisInfo.axis == axisId |
| 5847 | || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT |
| 5848 | && axis.axisInfo.highAxis == axisId)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5849 | return true; |
| 5850 | } |
| 5851 | } |
| 5852 | return false; |
| 5853 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5854 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5855 | void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) { |
| 5856 | size_t i = mAxes.size(); |
| 5857 | while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) { |
| 5858 | if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) { |
| 5859 | continue; |
| 5860 | } |
| 5861 | LOGI("Discarding joystick '%s' axis %d because there are too many axes.", |
| 5862 | getDeviceName().string(), mAxes.keyAt(i)); |
| 5863 | mAxes.removeItemsAt(i); |
| 5864 | } |
| 5865 | } |
| 5866 | |
| 5867 | bool JoystickInputMapper::isCenteredAxis(int32_t axis) { |
| 5868 | switch (axis) { |
| 5869 | case AMOTION_EVENT_AXIS_X: |
| 5870 | case AMOTION_EVENT_AXIS_Y: |
| 5871 | case AMOTION_EVENT_AXIS_Z: |
| 5872 | case AMOTION_EVENT_AXIS_RX: |
| 5873 | case AMOTION_EVENT_AXIS_RY: |
| 5874 | case AMOTION_EVENT_AXIS_RZ: |
| 5875 | case AMOTION_EVENT_AXIS_HAT_X: |
| 5876 | case AMOTION_EVENT_AXIS_HAT_Y: |
| 5877 | case AMOTION_EVENT_AXIS_ORIENTATION: |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5878 | case AMOTION_EVENT_AXIS_RUDDER: |
| 5879 | case AMOTION_EVENT_AXIS_WHEEL: |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5880 | return true; |
| 5881 | default: |
| 5882 | return false; |
| 5883 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5884 | } |
| 5885 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5886 | void JoystickInputMapper::reset(nsecs_t when) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5887 | // Recenter all axes. |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5888 | size_t numAxes = mAxes.size(); |
| 5889 | for (size_t i = 0; i < numAxes; i++) { |
| 5890 | Axis& axis = mAxes.editValueAt(i); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5891 | axis.resetValue(); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5892 | } |
| 5893 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5894 | InputMapper::reset(when); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5895 | } |
| 5896 | |
| 5897 | void JoystickInputMapper::process(const RawEvent* rawEvent) { |
| 5898 | switch (rawEvent->type) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5899 | case EV_ABS: { |
| 5900 | ssize_t index = mAxes.indexOfKey(rawEvent->scanCode); |
| 5901 | if (index >= 0) { |
| 5902 | Axis& axis = mAxes.editValueAt(index); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5903 | float newValue, highNewValue; |
| 5904 | switch (axis.axisInfo.mode) { |
| 5905 | case AxisInfo::MODE_INVERT: |
| 5906 | newValue = (axis.rawAxisInfo.maxValue - rawEvent->value) |
| 5907 | * axis.scale + axis.offset; |
| 5908 | highNewValue = 0.0f; |
| 5909 | break; |
| 5910 | case AxisInfo::MODE_SPLIT: |
| 5911 | if (rawEvent->value < axis.axisInfo.splitValue) { |
| 5912 | newValue = (axis.axisInfo.splitValue - rawEvent->value) |
| 5913 | * axis.scale + axis.offset; |
| 5914 | highNewValue = 0.0f; |
| 5915 | } else if (rawEvent->value > axis.axisInfo.splitValue) { |
| 5916 | newValue = 0.0f; |
| 5917 | highNewValue = (rawEvent->value - axis.axisInfo.splitValue) |
| 5918 | * axis.highScale + axis.highOffset; |
| 5919 | } else { |
| 5920 | newValue = 0.0f; |
| 5921 | highNewValue = 0.0f; |
| 5922 | } |
| 5923 | break; |
| 5924 | default: |
| 5925 | newValue = rawEvent->value * axis.scale + axis.offset; |
| 5926 | highNewValue = 0.0f; |
| 5927 | break; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5928 | } |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5929 | axis.newValue = newValue; |
| 5930 | axis.highNewValue = highNewValue; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5931 | } |
| 5932 | break; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5933 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5934 | |
| 5935 | case EV_SYN: |
| 5936 | switch (rawEvent->scanCode) { |
| 5937 | case SYN_REPORT: |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5938 | sync(rawEvent->when, false /*force*/); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5939 | break; |
| 5940 | } |
| 5941 | break; |
| 5942 | } |
| 5943 | } |
| 5944 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5945 | void JoystickInputMapper::sync(nsecs_t when, bool force) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5946 | if (!filterAxes(force)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5947 | return; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5948 | } |
| 5949 | |
| 5950 | int32_t metaState = mContext->getGlobalMetaState(); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5951 | int32_t buttonState = 0; |
| 5952 | |
| 5953 | PointerProperties pointerProperties; |
| 5954 | pointerProperties.clear(); |
| 5955 | pointerProperties.id = 0; |
| 5956 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5957 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5958 | PointerCoords pointerCoords; |
| 5959 | pointerCoords.clear(); |
| 5960 | |
| 5961 | size_t numAxes = mAxes.size(); |
| 5962 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5963 | const Axis& axis = mAxes.valueAt(i); |
| 5964 | pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue); |
| 5965 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 5966 | pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue); |
| 5967 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5968 | } |
| 5969 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 5970 | // Moving a joystick axis should not wake the devide because joysticks can |
| 5971 | // be fairly noisy even when not in use. On the other hand, pushing a gamepad |
| 5972 | // button will likely wake the device. |
| 5973 | // TODO: Use the input device configuration to control this behavior more finely. |
| 5974 | uint32_t policyFlags = 0; |
| 5975 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5976 | NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5977 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 5978 | 1, &pointerProperties, &pointerCoords, 0, 0, 0); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5979 | getListener()->notifyMotion(&args); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5980 | } |
| 5981 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5982 | bool JoystickInputMapper::filterAxes(bool force) { |
| 5983 | bool atLeastOneSignificantChange = force; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5984 | size_t numAxes = mAxes.size(); |
| 5985 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5986 | Axis& axis = mAxes.editValueAt(i); |
| 5987 | if (force || hasValueChangedSignificantly(axis.filter, |
| 5988 | axis.newValue, axis.currentValue, axis.min, axis.max)) { |
| 5989 | axis.currentValue = axis.newValue; |
| 5990 | atLeastOneSignificantChange = true; |
| 5991 | } |
| 5992 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 5993 | if (force || hasValueChangedSignificantly(axis.filter, |
| 5994 | axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) { |
| 5995 | axis.highCurrentValue = axis.highNewValue; |
| 5996 | atLeastOneSignificantChange = true; |
| 5997 | } |
| 5998 | } |
| 5999 | } |
| 6000 | return atLeastOneSignificantChange; |
| 6001 | } |
| 6002 | |
| 6003 | bool JoystickInputMapper::hasValueChangedSignificantly( |
| 6004 | float filter, float newValue, float currentValue, float min, float max) { |
| 6005 | if (newValue != currentValue) { |
| 6006 | // Filter out small changes in value unless the value is converging on the axis |
| 6007 | // bounds or center point. This is intended to reduce the amount of information |
| 6008 | // sent to applications by particularly noisy joysticks (such as PS3). |
| 6009 | if (fabs(newValue - currentValue) > filter |
| 6010 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min) |
| 6011 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max) |
| 6012 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) { |
| 6013 | return true; |
| 6014 | } |
| 6015 | } |
| 6016 | return false; |
| 6017 | } |
| 6018 | |
| 6019 | bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange( |
| 6020 | float filter, float newValue, float currentValue, float thresholdValue) { |
| 6021 | float newDistance = fabs(newValue - thresholdValue); |
| 6022 | if (newDistance < filter) { |
| 6023 | float oldDistance = fabs(currentValue - thresholdValue); |
| 6024 | if (newDistance < oldDistance) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6025 | return true; |
| 6026 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6027 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6028 | return false; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6029 | } |
| 6030 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 6031 | } // namespace android |