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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 47 | #include <unistd.h> |
Jeff Brown | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | fd03582 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 288 | ALOGD("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 | 46b9ac0 | 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 |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 319 | ALOGD("BatchSize: %d Count: %d", batchSize, count); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 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: |
Steve Block | f68633d | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 334 | ALOG_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 | 46b9ac0 | 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()) { |
Steve Block | 933e856 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 352 | ALOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 353 | } else { |
Steve Block | 933e856 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 354 | ALOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(), |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 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 { |
Steve Block | a51f0e7 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 362 | ALOGW("Ignoring spurious device added event for deviceId %d.", deviceId); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 363 | delete device; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 364 | return; |
| 365 | } |
Jeff Brown | 46b9ac0 | 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 { |
Steve Block | a51f0e7 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 375 | ALOGW("Ignoring spurious device removed event for deviceId %d.", deviceId); |
Jeff Brown | 46b9ac0 | 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()) { |
Steve Block | 933e856 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 380 | ALOGI("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 { |
Steve Block | 933e856 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 383 | ALOGI("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 | 46b9ac0 | 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) { |
Jeff Brown | 9ee285a | 2011-08-31 12:56:34 -0700 | [diff] [blame] | 393 | InputDevice* device = new InputDevice(&mContext, deviceId, name, classes); |
Jeff Brown | 46b9ac0 | 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) { |
Steve Block | a51f0e7 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 449 | ALOGW("Discarding event for unknown deviceId %d.", deviceId); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 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()) { |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 455 | //ALOGD("Discarding event for ignored deviceId %d.", deviceId); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 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) { |
Steve Block | 933e856 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 488 | ALOGI("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) { |
Steve Block | 933e856 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 550 | ALOGI("Dropping virtual key from device %s because virtual keys are " |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 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 | 46b9ac0 | 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 | f2f4871 | 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 | |
Jeff Brown | 9ee285a | 2011-08-31 12:56:34 -0700 | [diff] [blame] | 845 | InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name, |
| 846 | uint32_t classes) : |
| 847 | mContext(context), mId(id), mName(name), mClasses(classes), |
| 848 | mSources(0), mIsExternal(false), mDropUntilNextSync(false) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | InputDevice::~InputDevice() { |
| 852 | size_t numMappers = mMappers.size(); |
| 853 | for (size_t i = 0; i < numMappers; i++) { |
| 854 | delete mMappers[i]; |
| 855 | } |
| 856 | mMappers.clear(); |
| 857 | } |
| 858 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 859 | void InputDevice::dump(String8& dump) { |
| 860 | InputDeviceInfo deviceInfo; |
| 861 | getDeviceInfo(& deviceInfo); |
| 862 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 863 | dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(), |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 864 | deviceInfo.getName().string()); |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 865 | dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal)); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 866 | dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources()); |
| 867 | dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType()); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 868 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 869 | const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges(); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 870 | if (!ranges.isEmpty()) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 871 | dump.append(INDENT2 "Motion Ranges:\n"); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 872 | for (size_t i = 0; i < ranges.size(); i++) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 873 | const InputDeviceInfo::MotionRange& range = ranges.itemAt(i); |
| 874 | const char* label = getAxisLabel(range.axis); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 875 | char name[32]; |
| 876 | if (label) { |
| 877 | strncpy(name, label, sizeof(name)); |
| 878 | name[sizeof(name) - 1] = '\0'; |
| 879 | } else { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 880 | snprintf(name, sizeof(name), "%d", range.axis); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 881 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 882 | dump.appendFormat(INDENT3 "%s: source=0x%08x, " |
| 883 | "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n", |
| 884 | name, range.source, range.min, range.max, range.flat, range.fuzz); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 885 | } |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | size_t numMappers = mMappers.size(); |
| 889 | for (size_t i = 0; i < numMappers; i++) { |
| 890 | InputMapper* mapper = mMappers[i]; |
| 891 | mapper->dump(dump); |
| 892 | } |
| 893 | } |
| 894 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 895 | void InputDevice::addMapper(InputMapper* mapper) { |
| 896 | mMappers.add(mapper); |
| 897 | } |
| 898 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 899 | void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 900 | mSources = 0; |
| 901 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 902 | if (!isIgnored()) { |
| 903 | if (!changes) { // first time only |
| 904 | mContext->getEventHub()->getConfiguration(mId, &mConfiguration); |
| 905 | } |
| 906 | |
| 907 | size_t numMappers = mMappers.size(); |
| 908 | for (size_t i = 0; i < numMappers; i++) { |
| 909 | InputMapper* mapper = mMappers[i]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 910 | mapper->configure(when, config, changes); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 911 | mSources |= mapper->getSources(); |
| 912 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 916 | void InputDevice::reset(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 917 | size_t numMappers = mMappers.size(); |
| 918 | for (size_t i = 0; i < numMappers; i++) { |
| 919 | InputMapper* mapper = mMappers[i]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 920 | mapper->reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 921 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 922 | |
| 923 | mContext->updateGlobalMetaState(); |
| 924 | |
| 925 | notifyReset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 926 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 927 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 928 | void InputDevice::process(const RawEvent* rawEvents, size_t count) { |
| 929 | // Process all of the events in order for each mapper. |
| 930 | // We cannot simply ask each mapper to process them in bulk because mappers may |
| 931 | // have side-effects that must be interleaved. For example, joystick movement events and |
| 932 | // gamepad button presses are handled by different mappers but they should be dispatched |
| 933 | // in the order received. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 934 | size_t numMappers = mMappers.size(); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 935 | for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) { |
| 936 | #if DEBUG_RAW_EVENTS |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 937 | ALOGD("Input event: device=%d type=0x%04x scancode=0x%04x " |
Jeff Brown | 2e45fb6 | 2011-06-29 21:19:05 -0700 | [diff] [blame] | 938 | "keycode=0x%04x value=0x%08x flags=0x%08x", |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 939 | rawEvent->deviceId, rawEvent->type, rawEvent->scanCode, rawEvent->keyCode, |
| 940 | rawEvent->value, rawEvent->flags); |
| 941 | #endif |
| 942 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 943 | if (mDropUntilNextSync) { |
| 944 | if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) { |
| 945 | mDropUntilNextSync = false; |
| 946 | #if DEBUG_RAW_EVENTS |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 947 | ALOGD("Recovered from input event buffer overrun."); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 948 | #endif |
| 949 | } else { |
| 950 | #if DEBUG_RAW_EVENTS |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 951 | ALOGD("Dropped input event while waiting for next input sync."); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 952 | #endif |
| 953 | } |
| 954 | } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_DROPPED) { |
Steve Block | 933e856 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 955 | ALOGI("Detected input event buffer overrun for device %s.", mName.string()); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 956 | mDropUntilNextSync = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 957 | reset(rawEvent->when); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 958 | } else { |
| 959 | for (size_t i = 0; i < numMappers; i++) { |
| 960 | InputMapper* mapper = mMappers[i]; |
| 961 | mapper->process(rawEvent); |
| 962 | } |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 963 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 964 | } |
| 965 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 966 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 967 | void InputDevice::timeoutExpired(nsecs_t when) { |
| 968 | size_t numMappers = mMappers.size(); |
| 969 | for (size_t i = 0; i < numMappers; i++) { |
| 970 | InputMapper* mapper = mMappers[i]; |
| 971 | mapper->timeoutExpired(when); |
| 972 | } |
| 973 | } |
| 974 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 975 | void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) { |
| 976 | outDeviceInfo->initialize(mId, mName); |
| 977 | |
| 978 | size_t numMappers = mMappers.size(); |
| 979 | for (size_t i = 0; i < numMappers; i++) { |
| 980 | InputMapper* mapper = mMappers[i]; |
| 981 | mapper->populateDeviceInfo(outDeviceInfo); |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 986 | return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState); |
| 987 | } |
| 988 | |
| 989 | int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 990 | return getState(sourceMask, scanCode, & InputMapper::getScanCodeState); |
| 991 | } |
| 992 | |
| 993 | int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 994 | return getState(sourceMask, switchCode, & InputMapper::getSwitchState); |
| 995 | } |
| 996 | |
| 997 | int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) { |
| 998 | int32_t result = AKEY_STATE_UNKNOWN; |
| 999 | size_t numMappers = mMappers.size(); |
| 1000 | for (size_t i = 0; i < numMappers; i++) { |
| 1001 | InputMapper* mapper = mMappers[i]; |
| 1002 | if (sourcesMatchMask(mapper->getSources(), sourceMask)) { |
| 1003 | result = (mapper->*getStateFunc)(sourceMask, code); |
| 1004 | if (result >= AKEY_STATE_DOWN) { |
| 1005 | return result; |
| 1006 | } |
| 1007 | } |
| 1008 | } |
| 1009 | return result; |
| 1010 | } |
| 1011 | |
| 1012 | bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 1013 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 1014 | bool result = false; |
| 1015 | size_t numMappers = mMappers.size(); |
| 1016 | for (size_t i = 0; i < numMappers; i++) { |
| 1017 | InputMapper* mapper = mMappers[i]; |
| 1018 | if (sourcesMatchMask(mapper->getSources(), sourceMask)) { |
| 1019 | result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags); |
| 1020 | } |
| 1021 | } |
| 1022 | return result; |
| 1023 | } |
| 1024 | |
| 1025 | int32_t InputDevice::getMetaState() { |
| 1026 | int32_t result = 0; |
| 1027 | size_t numMappers = mMappers.size(); |
| 1028 | for (size_t i = 0; i < numMappers; i++) { |
| 1029 | InputMapper* mapper = mMappers[i]; |
| 1030 | result |= mapper->getMetaState(); |
| 1031 | } |
| 1032 | return result; |
| 1033 | } |
| 1034 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1035 | void InputDevice::fadePointer() { |
| 1036 | size_t numMappers = mMappers.size(); |
| 1037 | for (size_t i = 0; i < numMappers; i++) { |
| 1038 | InputMapper* mapper = mMappers[i]; |
| 1039 | mapper->fadePointer(); |
| 1040 | } |
| 1041 | } |
| 1042 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1043 | void InputDevice::notifyReset(nsecs_t when) { |
| 1044 | NotifyDeviceResetArgs args(when, mId); |
| 1045 | mContext->getListener()->notifyDeviceReset(&args); |
| 1046 | } |
| 1047 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1048 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1049 | // --- CursorButtonAccumulator --- |
| 1050 | |
| 1051 | CursorButtonAccumulator::CursorButtonAccumulator() { |
| 1052 | clearButtons(); |
| 1053 | } |
| 1054 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1055 | void CursorButtonAccumulator::reset(InputDevice* device) { |
| 1056 | mBtnLeft = device->isKeyPressed(BTN_LEFT); |
| 1057 | mBtnRight = device->isKeyPressed(BTN_RIGHT); |
| 1058 | mBtnMiddle = device->isKeyPressed(BTN_MIDDLE); |
| 1059 | mBtnBack = device->isKeyPressed(BTN_BACK); |
| 1060 | mBtnSide = device->isKeyPressed(BTN_SIDE); |
| 1061 | mBtnForward = device->isKeyPressed(BTN_FORWARD); |
| 1062 | mBtnExtra = device->isKeyPressed(BTN_EXTRA); |
| 1063 | mBtnTask = device->isKeyPressed(BTN_TASK); |
| 1064 | } |
| 1065 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1066 | void CursorButtonAccumulator::clearButtons() { |
| 1067 | mBtnLeft = 0; |
| 1068 | mBtnRight = 0; |
| 1069 | mBtnMiddle = 0; |
| 1070 | mBtnBack = 0; |
| 1071 | mBtnSide = 0; |
| 1072 | mBtnForward = 0; |
| 1073 | mBtnExtra = 0; |
| 1074 | mBtnTask = 0; |
| 1075 | } |
| 1076 | |
| 1077 | void CursorButtonAccumulator::process(const RawEvent* rawEvent) { |
| 1078 | if (rawEvent->type == EV_KEY) { |
| 1079 | switch (rawEvent->scanCode) { |
| 1080 | case BTN_LEFT: |
| 1081 | mBtnLeft = rawEvent->value; |
| 1082 | break; |
| 1083 | case BTN_RIGHT: |
| 1084 | mBtnRight = rawEvent->value; |
| 1085 | break; |
| 1086 | case BTN_MIDDLE: |
| 1087 | mBtnMiddle = rawEvent->value; |
| 1088 | break; |
| 1089 | case BTN_BACK: |
| 1090 | mBtnBack = rawEvent->value; |
| 1091 | break; |
| 1092 | case BTN_SIDE: |
| 1093 | mBtnSide = rawEvent->value; |
| 1094 | break; |
| 1095 | case BTN_FORWARD: |
| 1096 | mBtnForward = rawEvent->value; |
| 1097 | break; |
| 1098 | case BTN_EXTRA: |
| 1099 | mBtnExtra = rawEvent->value; |
| 1100 | break; |
| 1101 | case BTN_TASK: |
| 1102 | mBtnTask = rawEvent->value; |
| 1103 | break; |
| 1104 | } |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | uint32_t CursorButtonAccumulator::getButtonState() const { |
| 1109 | uint32_t result = 0; |
| 1110 | if (mBtnLeft) { |
| 1111 | result |= AMOTION_EVENT_BUTTON_PRIMARY; |
| 1112 | } |
| 1113 | if (mBtnRight) { |
| 1114 | result |= AMOTION_EVENT_BUTTON_SECONDARY; |
| 1115 | } |
| 1116 | if (mBtnMiddle) { |
| 1117 | result |= AMOTION_EVENT_BUTTON_TERTIARY; |
| 1118 | } |
| 1119 | if (mBtnBack || mBtnSide) { |
| 1120 | result |= AMOTION_EVENT_BUTTON_BACK; |
| 1121 | } |
| 1122 | if (mBtnForward || mBtnExtra) { |
| 1123 | result |= AMOTION_EVENT_BUTTON_FORWARD; |
| 1124 | } |
| 1125 | return result; |
| 1126 | } |
| 1127 | |
| 1128 | |
| 1129 | // --- CursorMotionAccumulator --- |
| 1130 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1131 | CursorMotionAccumulator::CursorMotionAccumulator() { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1132 | clearRelativeAxes(); |
| 1133 | } |
| 1134 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1135 | void CursorMotionAccumulator::reset(InputDevice* device) { |
| 1136 | clearRelativeAxes(); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | void CursorMotionAccumulator::clearRelativeAxes() { |
| 1140 | mRelX = 0; |
| 1141 | mRelY = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | void CursorMotionAccumulator::process(const RawEvent* rawEvent) { |
| 1145 | if (rawEvent->type == EV_REL) { |
| 1146 | switch (rawEvent->scanCode) { |
| 1147 | case REL_X: |
| 1148 | mRelX = rawEvent->value; |
| 1149 | break; |
| 1150 | case REL_Y: |
| 1151 | mRelY = rawEvent->value; |
| 1152 | break; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1153 | } |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | void CursorMotionAccumulator::finishSync() { |
| 1158 | clearRelativeAxes(); |
| 1159 | } |
| 1160 | |
| 1161 | |
| 1162 | // --- CursorScrollAccumulator --- |
| 1163 | |
| 1164 | CursorScrollAccumulator::CursorScrollAccumulator() : |
| 1165 | mHaveRelWheel(false), mHaveRelHWheel(false) { |
| 1166 | clearRelativeAxes(); |
| 1167 | } |
| 1168 | |
| 1169 | void CursorScrollAccumulator::configure(InputDevice* device) { |
| 1170 | mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL); |
| 1171 | mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL); |
| 1172 | } |
| 1173 | |
| 1174 | void CursorScrollAccumulator::reset(InputDevice* device) { |
| 1175 | clearRelativeAxes(); |
| 1176 | } |
| 1177 | |
| 1178 | void CursorScrollAccumulator::clearRelativeAxes() { |
| 1179 | mRelWheel = 0; |
| 1180 | mRelHWheel = 0; |
| 1181 | } |
| 1182 | |
| 1183 | void CursorScrollAccumulator::process(const RawEvent* rawEvent) { |
| 1184 | if (rawEvent->type == EV_REL) { |
| 1185 | switch (rawEvent->scanCode) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1186 | case REL_WHEEL: |
| 1187 | mRelWheel = rawEvent->value; |
| 1188 | break; |
| 1189 | case REL_HWHEEL: |
| 1190 | mRelHWheel = rawEvent->value; |
| 1191 | break; |
| 1192 | } |
| 1193 | } |
| 1194 | } |
| 1195 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1196 | void CursorScrollAccumulator::finishSync() { |
| 1197 | clearRelativeAxes(); |
| 1198 | } |
| 1199 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1200 | |
| 1201 | // --- TouchButtonAccumulator --- |
| 1202 | |
| 1203 | TouchButtonAccumulator::TouchButtonAccumulator() : |
| 1204 | mHaveBtnTouch(false) { |
| 1205 | clearButtons(); |
| 1206 | } |
| 1207 | |
| 1208 | void TouchButtonAccumulator::configure(InputDevice* device) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1209 | mHaveBtnTouch = device->hasKey(BTN_TOUCH); |
| 1210 | } |
| 1211 | |
| 1212 | void TouchButtonAccumulator::reset(InputDevice* device) { |
| 1213 | mBtnTouch = device->isKeyPressed(BTN_TOUCH); |
| 1214 | mBtnStylus = device->isKeyPressed(BTN_STYLUS); |
| 1215 | mBtnStylus2 = device->isKeyPressed(BTN_STYLUS); |
| 1216 | mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER); |
| 1217 | mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN); |
| 1218 | mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER); |
| 1219 | mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH); |
| 1220 | mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL); |
| 1221 | mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH); |
| 1222 | mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE); |
| 1223 | mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS); |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1224 | mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP); |
| 1225 | mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP); |
| 1226 | mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | void TouchButtonAccumulator::clearButtons() { |
| 1230 | mBtnTouch = 0; |
| 1231 | mBtnStylus = 0; |
| 1232 | mBtnStylus2 = 0; |
| 1233 | mBtnToolFinger = 0; |
| 1234 | mBtnToolPen = 0; |
| 1235 | mBtnToolRubber = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1236 | mBtnToolBrush = 0; |
| 1237 | mBtnToolPencil = 0; |
| 1238 | mBtnToolAirbrush = 0; |
| 1239 | mBtnToolMouse = 0; |
| 1240 | mBtnToolLens = 0; |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1241 | mBtnToolDoubleTap = 0; |
| 1242 | mBtnToolTripleTap = 0; |
| 1243 | mBtnToolQuadTap = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | void TouchButtonAccumulator::process(const RawEvent* rawEvent) { |
| 1247 | if (rawEvent->type == EV_KEY) { |
| 1248 | switch (rawEvent->scanCode) { |
| 1249 | case BTN_TOUCH: |
| 1250 | mBtnTouch = rawEvent->value; |
| 1251 | break; |
| 1252 | case BTN_STYLUS: |
| 1253 | mBtnStylus = rawEvent->value; |
| 1254 | break; |
| 1255 | case BTN_STYLUS2: |
| 1256 | mBtnStylus2 = rawEvent->value; |
| 1257 | break; |
| 1258 | case BTN_TOOL_FINGER: |
| 1259 | mBtnToolFinger = rawEvent->value; |
| 1260 | break; |
| 1261 | case BTN_TOOL_PEN: |
| 1262 | mBtnToolPen = rawEvent->value; |
| 1263 | break; |
| 1264 | case BTN_TOOL_RUBBER: |
| 1265 | mBtnToolRubber = rawEvent->value; |
| 1266 | break; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1267 | case BTN_TOOL_BRUSH: |
| 1268 | mBtnToolBrush = rawEvent->value; |
| 1269 | break; |
| 1270 | case BTN_TOOL_PENCIL: |
| 1271 | mBtnToolPencil = rawEvent->value; |
| 1272 | break; |
| 1273 | case BTN_TOOL_AIRBRUSH: |
| 1274 | mBtnToolAirbrush = rawEvent->value; |
| 1275 | break; |
| 1276 | case BTN_TOOL_MOUSE: |
| 1277 | mBtnToolMouse = rawEvent->value; |
| 1278 | break; |
| 1279 | case BTN_TOOL_LENS: |
| 1280 | mBtnToolLens = rawEvent->value; |
| 1281 | break; |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1282 | case BTN_TOOL_DOUBLETAP: |
| 1283 | mBtnToolDoubleTap = rawEvent->value; |
| 1284 | break; |
| 1285 | case BTN_TOOL_TRIPLETAP: |
| 1286 | mBtnToolTripleTap = rawEvent->value; |
| 1287 | break; |
| 1288 | case BTN_TOOL_QUADTAP: |
| 1289 | mBtnToolQuadTap = rawEvent->value; |
| 1290 | break; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1291 | } |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | uint32_t TouchButtonAccumulator::getButtonState() const { |
| 1296 | uint32_t result = 0; |
| 1297 | if (mBtnStylus) { |
| 1298 | result |= AMOTION_EVENT_BUTTON_SECONDARY; |
| 1299 | } |
| 1300 | if (mBtnStylus2) { |
| 1301 | result |= AMOTION_EVENT_BUTTON_TERTIARY; |
| 1302 | } |
| 1303 | return result; |
| 1304 | } |
| 1305 | |
| 1306 | int32_t TouchButtonAccumulator::getToolType() const { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1307 | if (mBtnToolMouse || mBtnToolLens) { |
| 1308 | return AMOTION_EVENT_TOOL_TYPE_MOUSE; |
| 1309 | } |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1310 | if (mBtnToolRubber) { |
| 1311 | return AMOTION_EVENT_TOOL_TYPE_ERASER; |
| 1312 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1313 | if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1314 | return AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 1315 | } |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1316 | if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1317 | return AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 1318 | } |
| 1319 | return AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
| 1320 | } |
| 1321 | |
Jeff Brown | d87c6d5 | 2011-08-10 14:55:59 -0700 | [diff] [blame] | 1322 | bool TouchButtonAccumulator::isToolActive() const { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1323 | return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber |
| 1324 | || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1325 | || mBtnToolMouse || mBtnToolLens |
| 1326 | || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | bool TouchButtonAccumulator::isHovering() const { |
| 1330 | return mHaveBtnTouch && !mBtnTouch; |
| 1331 | } |
| 1332 | |
| 1333 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1334 | // --- RawPointerAxes --- |
| 1335 | |
| 1336 | RawPointerAxes::RawPointerAxes() { |
| 1337 | clear(); |
| 1338 | } |
| 1339 | |
| 1340 | void RawPointerAxes::clear() { |
| 1341 | x.clear(); |
| 1342 | y.clear(); |
| 1343 | pressure.clear(); |
| 1344 | touchMajor.clear(); |
| 1345 | touchMinor.clear(); |
| 1346 | toolMajor.clear(); |
| 1347 | toolMinor.clear(); |
| 1348 | orientation.clear(); |
| 1349 | distance.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1350 | tiltX.clear(); |
| 1351 | tiltY.clear(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1352 | trackingId.clear(); |
| 1353 | slot.clear(); |
| 1354 | } |
| 1355 | |
| 1356 | |
| 1357 | // --- RawPointerData --- |
| 1358 | |
| 1359 | RawPointerData::RawPointerData() { |
| 1360 | clear(); |
| 1361 | } |
| 1362 | |
| 1363 | void RawPointerData::clear() { |
| 1364 | pointerCount = 0; |
| 1365 | clearIdBits(); |
| 1366 | } |
| 1367 | |
| 1368 | void RawPointerData::copyFrom(const RawPointerData& other) { |
| 1369 | pointerCount = other.pointerCount; |
| 1370 | hoveringIdBits = other.hoveringIdBits; |
| 1371 | touchingIdBits = other.touchingIdBits; |
| 1372 | |
| 1373 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 1374 | pointers[i] = other.pointers[i]; |
| 1375 | |
| 1376 | int id = pointers[i].id; |
| 1377 | idToIndex[id] = other.idToIndex[id]; |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const { |
| 1382 | float x = 0, y = 0; |
| 1383 | uint32_t count = touchingIdBits.count(); |
| 1384 | if (count) { |
| 1385 | for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) { |
| 1386 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 1387 | const Pointer& pointer = pointerForId(id); |
| 1388 | x += pointer.x; |
| 1389 | y += pointer.y; |
| 1390 | } |
| 1391 | x /= count; |
| 1392 | y /= count; |
| 1393 | } |
| 1394 | *outX = x; |
| 1395 | *outY = y; |
| 1396 | } |
| 1397 | |
| 1398 | |
| 1399 | // --- CookedPointerData --- |
| 1400 | |
| 1401 | CookedPointerData::CookedPointerData() { |
| 1402 | clear(); |
| 1403 | } |
| 1404 | |
| 1405 | void CookedPointerData::clear() { |
| 1406 | pointerCount = 0; |
| 1407 | hoveringIdBits.clear(); |
| 1408 | touchingIdBits.clear(); |
| 1409 | } |
| 1410 | |
| 1411 | void CookedPointerData::copyFrom(const CookedPointerData& other) { |
| 1412 | pointerCount = other.pointerCount; |
| 1413 | hoveringIdBits = other.hoveringIdBits; |
| 1414 | touchingIdBits = other.touchingIdBits; |
| 1415 | |
| 1416 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 1417 | pointerProperties[i].copyFrom(other.pointerProperties[i]); |
| 1418 | pointerCoords[i].copyFrom(other.pointerCoords[i]); |
| 1419 | |
| 1420 | int id = pointerProperties[i].id; |
| 1421 | idToIndex[id] = other.idToIndex[id]; |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1426 | // --- SingleTouchMotionAccumulator --- |
| 1427 | |
| 1428 | SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() { |
| 1429 | clearAbsoluteAxes(); |
| 1430 | } |
| 1431 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1432 | void SingleTouchMotionAccumulator::reset(InputDevice* device) { |
| 1433 | mAbsX = device->getAbsoluteAxisValue(ABS_X); |
| 1434 | mAbsY = device->getAbsoluteAxisValue(ABS_Y); |
| 1435 | mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE); |
| 1436 | mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH); |
| 1437 | mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE); |
| 1438 | mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X); |
| 1439 | mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y); |
| 1440 | } |
| 1441 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1442 | void SingleTouchMotionAccumulator::clearAbsoluteAxes() { |
| 1443 | mAbsX = 0; |
| 1444 | mAbsY = 0; |
| 1445 | mAbsPressure = 0; |
| 1446 | mAbsToolWidth = 0; |
| 1447 | mAbsDistance = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1448 | mAbsTiltX = 0; |
| 1449 | mAbsTiltY = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) { |
| 1453 | if (rawEvent->type == EV_ABS) { |
| 1454 | switch (rawEvent->scanCode) { |
| 1455 | case ABS_X: |
| 1456 | mAbsX = rawEvent->value; |
| 1457 | break; |
| 1458 | case ABS_Y: |
| 1459 | mAbsY = rawEvent->value; |
| 1460 | break; |
| 1461 | case ABS_PRESSURE: |
| 1462 | mAbsPressure = rawEvent->value; |
| 1463 | break; |
| 1464 | case ABS_TOOL_WIDTH: |
| 1465 | mAbsToolWidth = rawEvent->value; |
| 1466 | break; |
| 1467 | case ABS_DISTANCE: |
| 1468 | mAbsDistance = rawEvent->value; |
| 1469 | break; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1470 | case ABS_TILT_X: |
| 1471 | mAbsTiltX = rawEvent->value; |
| 1472 | break; |
| 1473 | case ABS_TILT_Y: |
| 1474 | mAbsTiltY = rawEvent->value; |
| 1475 | break; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1476 | } |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | |
| 1481 | // --- MultiTouchMotionAccumulator --- |
| 1482 | |
| 1483 | MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() : |
| 1484 | mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false) { |
| 1485 | } |
| 1486 | |
| 1487 | MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() { |
| 1488 | delete[] mSlots; |
| 1489 | } |
| 1490 | |
| 1491 | void MultiTouchMotionAccumulator::configure(size_t slotCount, bool usingSlotsProtocol) { |
| 1492 | mSlotCount = slotCount; |
| 1493 | mUsingSlotsProtocol = usingSlotsProtocol; |
| 1494 | |
| 1495 | delete[] mSlots; |
| 1496 | mSlots = new Slot[slotCount]; |
| 1497 | } |
| 1498 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1499 | void MultiTouchMotionAccumulator::reset(InputDevice* device) { |
| 1500 | // Unfortunately there is no way to read the initial contents of the slots. |
| 1501 | // So when we reset the accumulator, we must assume they are all zeroes. |
| 1502 | if (mUsingSlotsProtocol) { |
| 1503 | // Query the driver for the current slot index and use it as the initial slot |
| 1504 | // before we start reading events from the device. It is possible that the |
| 1505 | // current slot index will not be the same as it was when the first event was |
| 1506 | // written into the evdev buffer, which means the input mapper could start |
| 1507 | // out of sync with the initial state of the events in the evdev buffer. |
| 1508 | // In the extremely unlikely case that this happens, the data from |
| 1509 | // two slots will be confused until the next ABS_MT_SLOT event is received. |
| 1510 | // This can cause the touch point to "jump", but at least there will be |
| 1511 | // no stuck touches. |
| 1512 | int32_t initialSlot; |
| 1513 | status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(), |
| 1514 | ABS_MT_SLOT, &initialSlot); |
| 1515 | if (status) { |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1516 | ALOGD("Could not retrieve current multitouch slot index. status=%d", status); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1517 | initialSlot = -1; |
| 1518 | } |
| 1519 | clearSlots(initialSlot); |
| 1520 | } else { |
| 1521 | clearSlots(-1); |
| 1522 | } |
| 1523 | } |
| 1524 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1525 | void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1526 | if (mSlots) { |
| 1527 | for (size_t i = 0; i < mSlotCount; i++) { |
| 1528 | mSlots[i].clear(); |
| 1529 | } |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1530 | } |
| 1531 | mCurrentSlot = initialSlot; |
| 1532 | } |
| 1533 | |
| 1534 | void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) { |
| 1535 | if (rawEvent->type == EV_ABS) { |
| 1536 | bool newSlot = false; |
| 1537 | if (mUsingSlotsProtocol) { |
| 1538 | if (rawEvent->scanCode == ABS_MT_SLOT) { |
| 1539 | mCurrentSlot = rawEvent->value; |
| 1540 | newSlot = true; |
| 1541 | } |
| 1542 | } else if (mCurrentSlot < 0) { |
| 1543 | mCurrentSlot = 0; |
| 1544 | } |
| 1545 | |
| 1546 | if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) { |
| 1547 | #if DEBUG_POINTERS |
| 1548 | if (newSlot) { |
Steve Block | a51f0e7 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1549 | ALOGW("MultiTouch device emitted invalid slot index %d but it " |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1550 | "should be between 0 and %d; ignoring this slot.", |
| 1551 | mCurrentSlot, mSlotCount - 1); |
| 1552 | } |
| 1553 | #endif |
| 1554 | } else { |
| 1555 | Slot* slot = &mSlots[mCurrentSlot]; |
| 1556 | |
| 1557 | switch (rawEvent->scanCode) { |
| 1558 | case ABS_MT_POSITION_X: |
| 1559 | slot->mInUse = true; |
| 1560 | slot->mAbsMTPositionX = rawEvent->value; |
| 1561 | break; |
| 1562 | case ABS_MT_POSITION_Y: |
| 1563 | slot->mInUse = true; |
| 1564 | slot->mAbsMTPositionY = rawEvent->value; |
| 1565 | break; |
| 1566 | case ABS_MT_TOUCH_MAJOR: |
| 1567 | slot->mInUse = true; |
| 1568 | slot->mAbsMTTouchMajor = rawEvent->value; |
| 1569 | break; |
| 1570 | case ABS_MT_TOUCH_MINOR: |
| 1571 | slot->mInUse = true; |
| 1572 | slot->mAbsMTTouchMinor = rawEvent->value; |
| 1573 | slot->mHaveAbsMTTouchMinor = true; |
| 1574 | break; |
| 1575 | case ABS_MT_WIDTH_MAJOR: |
| 1576 | slot->mInUse = true; |
| 1577 | slot->mAbsMTWidthMajor = rawEvent->value; |
| 1578 | break; |
| 1579 | case ABS_MT_WIDTH_MINOR: |
| 1580 | slot->mInUse = true; |
| 1581 | slot->mAbsMTWidthMinor = rawEvent->value; |
| 1582 | slot->mHaveAbsMTWidthMinor = true; |
| 1583 | break; |
| 1584 | case ABS_MT_ORIENTATION: |
| 1585 | slot->mInUse = true; |
| 1586 | slot->mAbsMTOrientation = rawEvent->value; |
| 1587 | break; |
| 1588 | case ABS_MT_TRACKING_ID: |
| 1589 | if (mUsingSlotsProtocol && rawEvent->value < 0) { |
Jeff Brown | 8bcbbef | 2011-08-11 15:49:09 -0700 | [diff] [blame] | 1590 | // The slot is no longer in use but it retains its previous contents, |
| 1591 | // which may be reused for subsequent touches. |
| 1592 | slot->mInUse = false; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1593 | } else { |
| 1594 | slot->mInUse = true; |
| 1595 | slot->mAbsMTTrackingId = rawEvent->value; |
| 1596 | } |
| 1597 | break; |
| 1598 | case ABS_MT_PRESSURE: |
| 1599 | slot->mInUse = true; |
| 1600 | slot->mAbsMTPressure = rawEvent->value; |
| 1601 | break; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1602 | case ABS_MT_DISTANCE: |
| 1603 | slot->mInUse = true; |
| 1604 | slot->mAbsMTDistance = rawEvent->value; |
| 1605 | break; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1606 | case ABS_MT_TOOL_TYPE: |
| 1607 | slot->mInUse = true; |
| 1608 | slot->mAbsMTToolType = rawEvent->value; |
| 1609 | slot->mHaveAbsMTToolType = true; |
| 1610 | break; |
| 1611 | } |
| 1612 | } |
| 1613 | } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_MT_REPORT) { |
| 1614 | // MultiTouch Sync: The driver has returned all data for *one* of the pointers. |
| 1615 | mCurrentSlot += 1; |
| 1616 | } |
| 1617 | } |
| 1618 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1619 | void MultiTouchMotionAccumulator::finishSync() { |
| 1620 | if (!mUsingSlotsProtocol) { |
| 1621 | clearSlots(-1); |
| 1622 | } |
| 1623 | } |
| 1624 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1625 | |
| 1626 | // --- MultiTouchMotionAccumulator::Slot --- |
| 1627 | |
| 1628 | MultiTouchMotionAccumulator::Slot::Slot() { |
| 1629 | clear(); |
| 1630 | } |
| 1631 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1632 | void MultiTouchMotionAccumulator::Slot::clear() { |
| 1633 | mInUse = false; |
| 1634 | mHaveAbsMTTouchMinor = false; |
| 1635 | mHaveAbsMTWidthMinor = false; |
| 1636 | mHaveAbsMTToolType = false; |
| 1637 | mAbsMTPositionX = 0; |
| 1638 | mAbsMTPositionY = 0; |
| 1639 | mAbsMTTouchMajor = 0; |
| 1640 | mAbsMTTouchMinor = 0; |
| 1641 | mAbsMTWidthMajor = 0; |
| 1642 | mAbsMTWidthMinor = 0; |
| 1643 | mAbsMTOrientation = 0; |
| 1644 | mAbsMTTrackingId = -1; |
| 1645 | mAbsMTPressure = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1646 | mAbsMTDistance = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1647 | mAbsMTToolType = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | int32_t MultiTouchMotionAccumulator::Slot::getToolType() const { |
| 1651 | if (mHaveAbsMTToolType) { |
| 1652 | switch (mAbsMTToolType) { |
| 1653 | case MT_TOOL_FINGER: |
| 1654 | return AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 1655 | case MT_TOOL_PEN: |
| 1656 | return AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 1657 | } |
| 1658 | } |
| 1659 | return AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
| 1660 | } |
| 1661 | |
| 1662 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1663 | // --- InputMapper --- |
| 1664 | |
| 1665 | InputMapper::InputMapper(InputDevice* device) : |
| 1666 | mDevice(device), mContext(device->getContext()) { |
| 1667 | } |
| 1668 | |
| 1669 | InputMapper::~InputMapper() { |
| 1670 | } |
| 1671 | |
| 1672 | void InputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 1673 | info->addSource(getSources()); |
| 1674 | } |
| 1675 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1676 | void InputMapper::dump(String8& dump) { |
| 1677 | } |
| 1678 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1679 | void InputMapper::configure(nsecs_t when, |
| 1680 | const InputReaderConfiguration* config, uint32_t changes) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1683 | void InputMapper::reset(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1684 | } |
| 1685 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 1686 | void InputMapper::timeoutExpired(nsecs_t when) { |
| 1687 | } |
| 1688 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1689 | int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 1690 | return AKEY_STATE_UNKNOWN; |
| 1691 | } |
| 1692 | |
| 1693 | int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 1694 | return AKEY_STATE_UNKNOWN; |
| 1695 | } |
| 1696 | |
| 1697 | int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 1698 | return AKEY_STATE_UNKNOWN; |
| 1699 | } |
| 1700 | |
| 1701 | bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 1702 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 1703 | return false; |
| 1704 | } |
| 1705 | |
| 1706 | int32_t InputMapper::getMetaState() { |
| 1707 | return 0; |
| 1708 | } |
| 1709 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1710 | void InputMapper::fadePointer() { |
| 1711 | } |
| 1712 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1713 | status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) { |
| 1714 | return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo); |
| 1715 | } |
| 1716 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1717 | void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump, |
| 1718 | const RawAbsoluteAxisInfo& axis, const char* name) { |
| 1719 | if (axis.valid) { |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 1720 | dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n", |
| 1721 | name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1722 | } else { |
| 1723 | dump.appendFormat(INDENT4 "%s: unknown range\n", name); |
| 1724 | } |
| 1725 | } |
| 1726 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1727 | |
| 1728 | // --- SwitchInputMapper --- |
| 1729 | |
| 1730 | SwitchInputMapper::SwitchInputMapper(InputDevice* device) : |
| 1731 | InputMapper(device) { |
| 1732 | } |
| 1733 | |
| 1734 | SwitchInputMapper::~SwitchInputMapper() { |
| 1735 | } |
| 1736 | |
| 1737 | uint32_t SwitchInputMapper::getSources() { |
Jeff Brown | 89de57a | 2011-01-19 18:41:38 -0800 | [diff] [blame] | 1738 | return AINPUT_SOURCE_SWITCH; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | void SwitchInputMapper::process(const RawEvent* rawEvent) { |
| 1742 | switch (rawEvent->type) { |
| 1743 | case EV_SW: |
| 1744 | processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value); |
| 1745 | break; |
| 1746 | } |
| 1747 | } |
| 1748 | |
| 1749 | void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1750 | NotifySwitchArgs args(when, 0, switchCode, switchValue); |
| 1751 | getListener()->notifySwitch(&args); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1752 | } |
| 1753 | |
| 1754 | int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 1755 | return getEventHub()->getSwitchState(getDeviceId(), switchCode); |
| 1756 | } |
| 1757 | |
| 1758 | |
| 1759 | // --- KeyboardInputMapper --- |
| 1760 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1761 | KeyboardInputMapper::KeyboardInputMapper(InputDevice* device, |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1762 | uint32_t source, int32_t keyboardType) : |
| 1763 | InputMapper(device), mSource(source), |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1764 | mKeyboardType(keyboardType) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | KeyboardInputMapper::~KeyboardInputMapper() { |
| 1768 | } |
| 1769 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1770 | uint32_t KeyboardInputMapper::getSources() { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1771 | return mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 1775 | InputMapper::populateDeviceInfo(info); |
| 1776 | |
| 1777 | info->setKeyboardType(mKeyboardType); |
Jeff Brown | 1e08fe9 | 2011-11-15 17:48:10 -0800 | [diff] [blame] | 1778 | info->setKeyCharacterMapFile(getEventHub()->getKeyCharacterMapFile(getDeviceId())); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1779 | } |
| 1780 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1781 | void KeyboardInputMapper::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1782 | dump.append(INDENT2 "Keyboard Input Mapper:\n"); |
| 1783 | dumpParameters(dump); |
| 1784 | dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1785 | dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1786 | dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size()); |
| 1787 | dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState); |
| 1788 | dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1789 | } |
| 1790 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1791 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1792 | void KeyboardInputMapper::configure(nsecs_t when, |
| 1793 | const InputReaderConfiguration* config, uint32_t changes) { |
| 1794 | InputMapper::configure(when, config, changes); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1795 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 1796 | if (!changes) { // first time only |
| 1797 | // Configure basic parameters. |
| 1798 | configureParameters(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1799 | } |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1800 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1801 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) { |
| 1802 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) { |
| 1803 | if (!config->getDisplayInfo(mParameters.associatedDisplayId, |
| 1804 | false /*external*/, NULL, NULL, &mOrientation)) { |
| 1805 | mOrientation = DISPLAY_ORIENTATION_0; |
| 1806 | } |
| 1807 | } else { |
| 1808 | mOrientation = DISPLAY_ORIENTATION_0; |
| 1809 | } |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1810 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | void KeyboardInputMapper::configureParameters() { |
| 1814 | mParameters.orientationAware = false; |
| 1815 | getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"), |
| 1816 | mParameters.orientationAware); |
| 1817 | |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 1818 | mParameters.associatedDisplayId = -1; |
| 1819 | if (mParameters.orientationAware) { |
| 1820 | mParameters.associatedDisplayId = 0; |
| 1821 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | void KeyboardInputMapper::dumpParameters(String8& dump) { |
| 1825 | dump.append(INDENT3 "Parameters:\n"); |
| 1826 | dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n", |
| 1827 | mParameters.associatedDisplayId); |
| 1828 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 1829 | toString(mParameters.orientationAware)); |
| 1830 | } |
| 1831 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1832 | void KeyboardInputMapper::reset(nsecs_t when) { |
| 1833 | mMetaState = AMETA_NONE; |
| 1834 | mDownTime = 0; |
| 1835 | mKeyDowns.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1836 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1837 | resetLedState(); |
| 1838 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1839 | InputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | void KeyboardInputMapper::process(const RawEvent* rawEvent) { |
| 1843 | switch (rawEvent->type) { |
| 1844 | case EV_KEY: { |
| 1845 | int32_t scanCode = rawEvent->scanCode; |
| 1846 | if (isKeyboardOrGamepadKey(scanCode)) { |
| 1847 | processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode, |
| 1848 | rawEvent->flags); |
| 1849 | } |
| 1850 | break; |
| 1851 | } |
| 1852 | } |
| 1853 | } |
| 1854 | |
| 1855 | bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) { |
| 1856 | return scanCode < BTN_MOUSE |
| 1857 | || scanCode >= KEY_OK |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 1858 | || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE) |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1859 | || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1860 | } |
| 1861 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1862 | void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode, |
| 1863 | int32_t scanCode, uint32_t policyFlags) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1864 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1865 | if (down) { |
| 1866 | // Rotate key codes according to orientation if needed. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1867 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1868 | keyCode = rotateKeyCode(keyCode, mOrientation); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1869 | } |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 1870 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1871 | // Add key down. |
| 1872 | ssize_t keyDownIndex = findKeyDown(scanCode); |
| 1873 | if (keyDownIndex >= 0) { |
| 1874 | // key repeat, be sure to use same keycode as before in case of rotation |
| 1875 | keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1876 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1877 | // key down |
| 1878 | if ((policyFlags & POLICY_FLAG_VIRTUAL) |
| 1879 | && mContext->shouldDropVirtualKey(when, |
| 1880 | getDevice(), keyCode, scanCode)) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1881 | return; |
| 1882 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1883 | |
| 1884 | mKeyDowns.push(); |
| 1885 | KeyDown& keyDown = mKeyDowns.editTop(); |
| 1886 | keyDown.keyCode = keyCode; |
| 1887 | keyDown.scanCode = scanCode; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1888 | } |
| 1889 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1890 | mDownTime = when; |
| 1891 | } else { |
| 1892 | // Remove key down. |
| 1893 | ssize_t keyDownIndex = findKeyDown(scanCode); |
| 1894 | if (keyDownIndex >= 0) { |
| 1895 | // key up, be sure to use same keycode as before in case of rotation |
| 1896 | keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode; |
| 1897 | mKeyDowns.removeAt(size_t(keyDownIndex)); |
| 1898 | } else { |
| 1899 | // key was not actually down |
Steve Block | 933e856 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1900 | ALOGI("Dropping key up from device %s because the key was not down. " |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1901 | "keyCode=%d, scanCode=%d", |
| 1902 | getDeviceName().string(), keyCode, scanCode); |
| 1903 | return; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1904 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1905 | } |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 1906 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1907 | bool metaStateChanged = false; |
| 1908 | int32_t oldMetaState = mMetaState; |
| 1909 | int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState); |
| 1910 | if (oldMetaState != newMetaState) { |
| 1911 | mMetaState = newMetaState; |
| 1912 | metaStateChanged = true; |
| 1913 | updateLedState(false); |
| 1914 | } |
| 1915 | |
| 1916 | nsecs_t downTime = mDownTime; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1917 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1918 | // Key down on external an keyboard should wake the device. |
| 1919 | // We don't do this for internal keyboards to prevent them from waking up in your pocket. |
| 1920 | // For internal keyboards, the key layout file should specify the policy flags for |
| 1921 | // each wake key individually. |
| 1922 | // TODO: Use the input device configuration to control this behavior more finely. |
| 1923 | if (down && getDevice()->isExternal() |
| 1924 | && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) { |
| 1925 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 1926 | } |
| 1927 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1928 | if (metaStateChanged) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1929 | getContext()->updateGlobalMetaState(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1930 | } |
| 1931 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1932 | if (down && !isMetaKey(keyCode)) { |
| 1933 | getContext()->fadePointer(); |
| 1934 | } |
| 1935 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1936 | NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1937 | down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, |
| 1938 | AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1939 | getListener()->notifyKey(&args); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1940 | } |
| 1941 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1942 | ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) { |
| 1943 | size_t n = mKeyDowns.size(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1944 | for (size_t i = 0; i < n; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1945 | if (mKeyDowns[i].scanCode == scanCode) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1946 | return i; |
| 1947 | } |
| 1948 | } |
| 1949 | return -1; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1950 | } |
| 1951 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1952 | int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 1953 | return getEventHub()->getKeyCodeState(getDeviceId(), keyCode); |
| 1954 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1955 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1956 | int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 1957 | return getEventHub()->getScanCodeState(getDeviceId(), scanCode); |
| 1958 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1959 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1960 | bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 1961 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 1962 | return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags); |
| 1963 | } |
| 1964 | |
| 1965 | int32_t KeyboardInputMapper::getMetaState() { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1966 | return mMetaState; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1967 | } |
| 1968 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1969 | void KeyboardInputMapper::resetLedState() { |
| 1970 | initializeLedState(mCapsLockLedState, LED_CAPSL); |
| 1971 | initializeLedState(mNumLockLedState, LED_NUML); |
| 1972 | initializeLedState(mScrollLockLedState, LED_SCROLLL); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1973 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1974 | updateLedState(true); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1975 | } |
| 1976 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1977 | void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) { |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1978 | ledState.avail = getEventHub()->hasLed(getDeviceId(), led); |
| 1979 | ledState.on = false; |
| 1980 | } |
| 1981 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1982 | void KeyboardInputMapper::updateLedState(bool reset) { |
| 1983 | updateLedStateForModifier(mCapsLockLedState, LED_CAPSL, |
Jeff Brown | 51e7fe7 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 1984 | AMETA_CAPS_LOCK_ON, reset); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1985 | updateLedStateForModifier(mNumLockLedState, LED_NUML, |
Jeff Brown | 51e7fe7 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 1986 | AMETA_NUM_LOCK_ON, reset); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1987 | updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL, |
Jeff Brown | 51e7fe7 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 1988 | AMETA_SCROLL_LOCK_ON, reset); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1989 | } |
| 1990 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1991 | void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState, |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1992 | int32_t led, int32_t modifier, bool reset) { |
| 1993 | if (ledState.avail) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1994 | bool desiredState = (mMetaState & modifier) != 0; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1995 | if (reset || ledState.on != desiredState) { |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1996 | getEventHub()->setLedState(getDeviceId(), led, desiredState); |
| 1997 | ledState.on = desiredState; |
| 1998 | } |
| 1999 | } |
| 2000 | } |
| 2001 | |
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 --- |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2004 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2005 | CursorInputMapper::CursorInputMapper(InputDevice* device) : |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2006 | InputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2007 | } |
| 2008 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2009 | CursorInputMapper::~CursorInputMapper() { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2010 | } |
| 2011 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2012 | uint32_t CursorInputMapper::getSources() { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2013 | return mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2014 | } |
| 2015 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2016 | void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2017 | InputMapper::populateDeviceInfo(info); |
| 2018 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2019 | if (mParameters.mode == Parameters::MODE_POINTER) { |
| 2020 | float minX, minY, maxX, maxY; |
| 2021 | if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2022 | info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f); |
| 2023 | 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] | 2024 | } |
| 2025 | } else { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2026 | info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale); |
| 2027 | 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] | 2028 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2029 | 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] | 2030 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2031 | if (mCursorScrollAccumulator.haveRelativeVWheel()) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2032 | 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] | 2033 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2034 | if (mCursorScrollAccumulator.haveRelativeHWheel()) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2035 | 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] | 2036 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2037 | } |
| 2038 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2039 | void CursorInputMapper::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2040 | dump.append(INDENT2 "Cursor Input Mapper:\n"); |
| 2041 | dumpParameters(dump); |
| 2042 | dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale); |
| 2043 | dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale); |
| 2044 | dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision); |
| 2045 | dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision); |
| 2046 | dump.appendFormat(INDENT3 "HaveVWheel: %s\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2047 | toString(mCursorScrollAccumulator.haveRelativeVWheel())); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2048 | dump.appendFormat(INDENT3 "HaveHWheel: %s\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2049 | toString(mCursorScrollAccumulator.haveRelativeHWheel())); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2050 | dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale); |
| 2051 | dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2052 | dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2053 | dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState); |
| 2054 | dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState))); |
| 2055 | dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2056 | } |
| 2057 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2058 | void CursorInputMapper::configure(nsecs_t when, |
| 2059 | const InputReaderConfiguration* config, uint32_t changes) { |
| 2060 | InputMapper::configure(when, config, changes); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2061 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2062 | if (!changes) { // first time only |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2063 | mCursorScrollAccumulator.configure(getDevice()); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2064 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2065 | // Configure basic parameters. |
| 2066 | configureParameters(); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2067 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2068 | // Configure device mode. |
| 2069 | switch (mParameters.mode) { |
| 2070 | case Parameters::MODE_POINTER: |
| 2071 | mSource = AINPUT_SOURCE_MOUSE; |
| 2072 | mXPrecision = 1.0f; |
| 2073 | mYPrecision = 1.0f; |
| 2074 | mXScale = 1.0f; |
| 2075 | mYScale = 1.0f; |
| 2076 | mPointerController = getPolicy()->obtainPointerController(getDeviceId()); |
| 2077 | break; |
| 2078 | case Parameters::MODE_NAVIGATION: |
| 2079 | mSource = AINPUT_SOURCE_TRACKBALL; |
| 2080 | mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 2081 | mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 2082 | mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 2083 | mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 2084 | break; |
| 2085 | } |
| 2086 | |
| 2087 | mVWheelScale = 1.0f; |
| 2088 | mHWheelScale = 1.0f; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2089 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 2090 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2091 | if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) { |
| 2092 | mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters); |
| 2093 | mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters); |
| 2094 | mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters); |
| 2095 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2096 | |
| 2097 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) { |
| 2098 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) { |
| 2099 | if (!config->getDisplayInfo(mParameters.associatedDisplayId, |
| 2100 | false /*external*/, NULL, NULL, &mOrientation)) { |
| 2101 | mOrientation = DISPLAY_ORIENTATION_0; |
| 2102 | } |
| 2103 | } else { |
| 2104 | mOrientation = DISPLAY_ORIENTATION_0; |
| 2105 | } |
| 2106 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2107 | } |
| 2108 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2109 | void CursorInputMapper::configureParameters() { |
| 2110 | mParameters.mode = Parameters::MODE_POINTER; |
| 2111 | String8 cursorModeString; |
| 2112 | if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) { |
| 2113 | if (cursorModeString == "navigation") { |
| 2114 | mParameters.mode = Parameters::MODE_NAVIGATION; |
| 2115 | } else if (cursorModeString != "pointer" && cursorModeString != "default") { |
Steve Block | a51f0e7 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2116 | ALOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string()); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2117 | } |
| 2118 | } |
| 2119 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2120 | mParameters.orientationAware = false; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2121 | getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"), |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2122 | mParameters.orientationAware); |
| 2123 | |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2124 | mParameters.associatedDisplayId = -1; |
| 2125 | if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) { |
| 2126 | mParameters.associatedDisplayId = 0; |
| 2127 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2128 | } |
| 2129 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2130 | void CursorInputMapper::dumpParameters(String8& dump) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2131 | dump.append(INDENT3 "Parameters:\n"); |
| 2132 | dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n", |
| 2133 | mParameters.associatedDisplayId); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2134 | |
| 2135 | switch (mParameters.mode) { |
| 2136 | case Parameters::MODE_POINTER: |
| 2137 | dump.append(INDENT4 "Mode: pointer\n"); |
| 2138 | break; |
| 2139 | case Parameters::MODE_NAVIGATION: |
| 2140 | dump.append(INDENT4 "Mode: navigation\n"); |
| 2141 | break; |
| 2142 | default: |
Steve Block | f68633d | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 2143 | ALOG_ASSERT(false); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2144 | } |
| 2145 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2146 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 2147 | toString(mParameters.orientationAware)); |
| 2148 | } |
| 2149 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2150 | void CursorInputMapper::reset(nsecs_t when) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2151 | mButtonState = 0; |
| 2152 | mDownTime = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2153 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2154 | mPointerVelocityControl.reset(); |
| 2155 | mWheelXVelocityControl.reset(); |
| 2156 | mWheelYVelocityControl.reset(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2157 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2158 | mCursorButtonAccumulator.reset(getDevice()); |
| 2159 | mCursorMotionAccumulator.reset(getDevice()); |
| 2160 | mCursorScrollAccumulator.reset(getDevice()); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2161 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2162 | InputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2163 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2164 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2165 | void CursorInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2166 | mCursorButtonAccumulator.process(rawEvent); |
| 2167 | mCursorMotionAccumulator.process(rawEvent); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2168 | mCursorScrollAccumulator.process(rawEvent); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2169 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2170 | if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) { |
| 2171 | sync(rawEvent->when); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2172 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2173 | } |
| 2174 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2175 | void CursorInputMapper::sync(nsecs_t when) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2176 | int32_t lastButtonState = mButtonState; |
| 2177 | int32_t currentButtonState = mCursorButtonAccumulator.getButtonState(); |
| 2178 | mButtonState = currentButtonState; |
| 2179 | |
| 2180 | bool wasDown = isPointerDown(lastButtonState); |
| 2181 | bool down = isPointerDown(currentButtonState); |
| 2182 | bool downChanged; |
| 2183 | if (!wasDown && down) { |
| 2184 | mDownTime = when; |
| 2185 | downChanged = true; |
| 2186 | } else if (wasDown && !down) { |
| 2187 | downChanged = true; |
| 2188 | } else { |
| 2189 | downChanged = false; |
| 2190 | } |
| 2191 | nsecs_t downTime = mDownTime; |
| 2192 | bool buttonsChanged = currentButtonState != lastButtonState; |
Jeff Brown | c28306a | 2011-08-23 21:32:42 -0700 | [diff] [blame] | 2193 | bool buttonsPressed = currentButtonState & ~lastButtonState; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2194 | |
| 2195 | float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale; |
| 2196 | float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale; |
| 2197 | bool moved = deltaX != 0 || deltaY != 0; |
| 2198 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2199 | // Rotate delta according to orientation if needed. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2200 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0 |
| 2201 | && (deltaX != 0.0f || deltaY != 0.0f)) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2202 | rotateDelta(mOrientation, &deltaX, &deltaY); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2203 | } |
| 2204 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2205 | // Move the pointer. |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2206 | PointerProperties pointerProperties; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2207 | pointerProperties.clear(); |
| 2208 | pointerProperties.id = 0; |
| 2209 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE; |
| 2210 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2211 | PointerCoords pointerCoords; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2212 | pointerCoords.clear(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2213 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2214 | float vscroll = mCursorScrollAccumulator.getRelativeVWheel(); |
| 2215 | float hscroll = mCursorScrollAccumulator.getRelativeHWheel(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2216 | bool scrolled = vscroll != 0 || hscroll != 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2217 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2218 | mWheelYVelocityControl.move(when, NULL, &vscroll); |
| 2219 | mWheelXVelocityControl.move(when, &hscroll, NULL); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2220 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2221 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2222 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2223 | if (mPointerController != NULL) { |
| 2224 | if (moved || scrolled || buttonsChanged) { |
| 2225 | mPointerController->setPresentation( |
| 2226 | PointerControllerInterface::PRESENTATION_POINTER); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2227 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2228 | if (moved) { |
| 2229 | mPointerController->move(deltaX, deltaY); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2230 | } |
| 2231 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2232 | if (buttonsChanged) { |
| 2233 | mPointerController->setButtonState(currentButtonState); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2234 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2235 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2236 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2237 | } |
| 2238 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2239 | float x, y; |
| 2240 | mPointerController->getPosition(&x, &y); |
| 2241 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 2242 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 2243 | } else { |
| 2244 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX); |
| 2245 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY); |
| 2246 | } |
| 2247 | |
| 2248 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2249 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2250 | // Moving an external trackball or mouse should wake the device. |
| 2251 | // We don't do this for internal cursor devices to prevent them from waking up |
| 2252 | // the device in your pocket. |
| 2253 | // TODO: Use the input device configuration to control this behavior more finely. |
| 2254 | uint32_t policyFlags = 0; |
Jeff Brown | c28306a | 2011-08-23 21:32:42 -0700 | [diff] [blame] | 2255 | if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) { |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2256 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 2257 | } |
| 2258 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2259 | // Synthesize key down from buttons if needed. |
| 2260 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource, |
| 2261 | policyFlags, lastButtonState, currentButtonState); |
| 2262 | |
| 2263 | // Send motion event. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2264 | if (downChanged || moved || scrolled || buttonsChanged) { |
| 2265 | int32_t metaState = mContext->getGlobalMetaState(); |
| 2266 | int32_t motionEventAction; |
| 2267 | if (downChanged) { |
| 2268 | motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP; |
| 2269 | } else if (down || mPointerController == NULL) { |
| 2270 | motionEventAction = AMOTION_EVENT_ACTION_MOVE; |
| 2271 | } else { |
| 2272 | motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE; |
| 2273 | } |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2274 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2275 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 2276 | motionEventAction, 0, metaState, currentButtonState, 0, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2277 | 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2278 | getListener()->notifyMotion(&args); |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2279 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2280 | // Send hover move after UP to tell the application that the mouse is hovering now. |
| 2281 | if (motionEventAction == AMOTION_EVENT_ACTION_UP |
| 2282 | && mPointerController != NULL) { |
| 2283 | NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags, |
| 2284 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 2285 | metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 2286 | 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime); |
| 2287 | getListener()->notifyMotion(&hoverArgs); |
| 2288 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2289 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2290 | // Send scroll events. |
| 2291 | if (scrolled) { |
| 2292 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll); |
| 2293 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll); |
| 2294 | |
| 2295 | NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags, |
| 2296 | AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState, |
| 2297 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 2298 | 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime); |
| 2299 | getListener()->notifyMotion(&scrollArgs); |
| 2300 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2301 | } |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2302 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2303 | // Synthesize key up from buttons if needed. |
| 2304 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource, |
| 2305 | policyFlags, lastButtonState, currentButtonState); |
| 2306 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2307 | mCursorMotionAccumulator.finishSync(); |
| 2308 | mCursorScrollAccumulator.finishSync(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2309 | } |
| 2310 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2311 | int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Jeff Brown | c3fc2d0 | 2010-08-10 15:47:53 -0700 | [diff] [blame] | 2312 | if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) { |
| 2313 | return getEventHub()->getScanCodeState(getDeviceId(), scanCode); |
| 2314 | } else { |
| 2315 | return AKEY_STATE_UNKNOWN; |
| 2316 | } |
| 2317 | } |
| 2318 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2319 | void CursorInputMapper::fadePointer() { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2320 | if (mPointerController != NULL) { |
| 2321 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 2322 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2323 | } |
| 2324 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2325 | |
| 2326 | // --- TouchInputMapper --- |
| 2327 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2328 | TouchInputMapper::TouchInputMapper(InputDevice* device) : |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2329 | InputMapper(device), |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2330 | mSource(0), mDeviceMode(DEVICE_MODE_DISABLED), |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2331 | mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | TouchInputMapper::~TouchInputMapper() { |
| 2335 | } |
| 2336 | |
| 2337 | uint32_t TouchInputMapper::getSources() { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2338 | return mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2339 | } |
| 2340 | |
| 2341 | void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 2342 | InputMapper::populateDeviceInfo(info); |
| 2343 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2344 | if (mDeviceMode != DEVICE_MODE_DISABLED) { |
| 2345 | info->addMotionRange(mOrientedRanges.x); |
| 2346 | info->addMotionRange(mOrientedRanges.y); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2347 | info->addMotionRange(mOrientedRanges.pressure); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2348 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2349 | if (mOrientedRanges.haveSize) { |
| 2350 | info->addMotionRange(mOrientedRanges.size); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2351 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2352 | |
| 2353 | if (mOrientedRanges.haveTouchSize) { |
| 2354 | info->addMotionRange(mOrientedRanges.touchMajor); |
| 2355 | info->addMotionRange(mOrientedRanges.touchMinor); |
| 2356 | } |
| 2357 | |
| 2358 | if (mOrientedRanges.haveToolSize) { |
| 2359 | info->addMotionRange(mOrientedRanges.toolMajor); |
| 2360 | info->addMotionRange(mOrientedRanges.toolMinor); |
| 2361 | } |
| 2362 | |
| 2363 | if (mOrientedRanges.haveOrientation) { |
| 2364 | info->addMotionRange(mOrientedRanges.orientation); |
| 2365 | } |
| 2366 | |
| 2367 | if (mOrientedRanges.haveDistance) { |
| 2368 | info->addMotionRange(mOrientedRanges.distance); |
| 2369 | } |
| 2370 | |
| 2371 | if (mOrientedRanges.haveTilt) { |
| 2372 | info->addMotionRange(mOrientedRanges.tilt); |
| 2373 | } |
| 2374 | |
| 2375 | if (mCursorScrollAccumulator.haveRelativeVWheel()) { |
| 2376 | info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f); |
| 2377 | } |
| 2378 | if (mCursorScrollAccumulator.haveRelativeHWheel()) { |
| 2379 | info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f); |
| 2380 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2381 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2382 | } |
| 2383 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2384 | void TouchInputMapper::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2385 | dump.append(INDENT2 "Touch Input Mapper:\n"); |
| 2386 | dumpParameters(dump); |
| 2387 | dumpVirtualKeys(dump); |
| 2388 | dumpRawPointerAxes(dump); |
| 2389 | dumpCalibration(dump); |
| 2390 | dumpSurface(dump); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2391 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2392 | dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n"); |
| 2393 | dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale); |
| 2394 | dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale); |
| 2395 | dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision); |
| 2396 | dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision); |
| 2397 | dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2398 | dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale); |
| 2399 | dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2400 | dump.appendFormat(INDENT4 "OrientationCenter: %0.3f\n", mOrientationCenter); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2401 | dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale); |
| 2402 | dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2403 | dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt)); |
| 2404 | dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter); |
| 2405 | dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale); |
| 2406 | dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter); |
| 2407 | dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2408 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2409 | dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2410 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2411 | dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n", |
| 2412 | mLastRawPointerData.pointerCount); |
| 2413 | for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) { |
| 2414 | const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i]; |
| 2415 | dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, " |
| 2416 | "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2417 | "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, " |
| 2418 | "toolType=%d, isHovering=%s\n", i, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2419 | pointer.id, pointer.x, pointer.y, pointer.pressure, |
| 2420 | pointer.touchMajor, pointer.touchMinor, |
| 2421 | pointer.toolMajor, pointer.toolMinor, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2422 | pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2423 | pointer.toolType, toString(pointer.isHovering)); |
| 2424 | } |
| 2425 | |
| 2426 | dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n", |
| 2427 | mLastCookedPointerData.pointerCount); |
| 2428 | for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) { |
| 2429 | const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i]; |
| 2430 | const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i]; |
| 2431 | dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, " |
| 2432 | "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2433 | "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, " |
| 2434 | "toolType=%d, isHovering=%s\n", i, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2435 | pointerProperties.id, |
| 2436 | pointerCoords.getX(), |
| 2437 | pointerCoords.getY(), |
| 2438 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 2439 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 2440 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 2441 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 2442 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 2443 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2444 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT), |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2445 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), |
| 2446 | pointerProperties.toolType, |
| 2447 | toString(mLastCookedPointerData.isHovering(i))); |
| 2448 | } |
| 2449 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2450 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2451 | dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n"); |
| 2452 | dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2453 | mPointerXMovementScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2454 | dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2455 | mPointerYMovementScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2456 | dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2457 | mPointerXZoomScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2458 | dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2459 | mPointerYZoomScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2460 | dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n", |
| 2461 | mPointerGestureMaxSwipeWidth); |
| 2462 | } |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2463 | } |
| 2464 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2465 | void TouchInputMapper::configure(nsecs_t when, |
| 2466 | const InputReaderConfiguration* config, uint32_t changes) { |
| 2467 | InputMapper::configure(when, config, changes); |
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 | mConfig = *config; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2470 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2471 | if (!changes) { // first time only |
| 2472 | // Configure basic parameters. |
| 2473 | configureParameters(); |
| 2474 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2475 | // Configure common accumulators. |
| 2476 | mCursorScrollAccumulator.configure(getDevice()); |
| 2477 | mTouchButtonAccumulator.configure(getDevice()); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2478 | |
| 2479 | // Configure absolute axis information. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2480 | configureRawPointerAxes(); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2481 | |
| 2482 | // Prepare input device calibration. |
| 2483 | parseCalibration(); |
| 2484 | resolveCalibration(); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2485 | } |
| 2486 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2487 | if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2488 | // Update pointer speed. |
| 2489 | mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters); |
| 2490 | mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters); |
| 2491 | mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2492 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2493 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2494 | bool resetNeeded = false; |
| 2495 | if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 2496 | | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT |
| 2497 | | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2498 | // Configure device sources, surface dimensions, orientation and |
| 2499 | // scaling factors. |
| 2500 | configureSurface(when, &resetNeeded); |
| 2501 | } |
| 2502 | |
| 2503 | if (changes && resetNeeded) { |
| 2504 | // Send reset, unless this is the first time the device has been configured, |
| 2505 | // in which case the reader will call reset itself after all mappers are ready. |
| 2506 | getDevice()->notifyReset(when); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2507 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2508 | } |
| 2509 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2510 | void TouchInputMapper::configureParameters() { |
Jeff Brown | b126822 | 2011-06-03 17:06:16 -0700 | [diff] [blame] | 2511 | // Use the pointer presentation mode for devices that do not support distinct |
| 2512 | // multitouch. The spot-based presentation relies on being able to accurately |
| 2513 | // locate two or more fingers on the touch pad. |
| 2514 | mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT) |
| 2515 | ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2516 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2517 | String8 gestureModeString; |
| 2518 | if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"), |
| 2519 | gestureModeString)) { |
| 2520 | if (gestureModeString == "pointer") { |
| 2521 | mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER; |
| 2522 | } else if (gestureModeString == "spots") { |
| 2523 | mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS; |
| 2524 | } else if (gestureModeString != "default") { |
Steve Block | a51f0e7 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2525 | ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string()); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2526 | } |
| 2527 | } |
| 2528 | |
Jeff Brown | deffe07 | 2011-08-26 18:38:46 -0700 | [diff] [blame] | 2529 | if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) { |
| 2530 | // The device is a touch screen. |
| 2531 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
| 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()->hasRelativeAxis(getDeviceId(), REL_X) |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2536 | || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) { |
| 2537 | // The device is a cursor device with a touch pad attached. |
| 2538 | // By default don't use the touch pad to move the pointer. |
| 2539 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; |
| 2540 | } else { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2541 | // The device is a touch pad of unknown purpose. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2542 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
| 2543 | } |
| 2544 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2545 | String8 deviceTypeString; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2546 | if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"), |
| 2547 | deviceTypeString)) { |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 2548 | if (deviceTypeString == "touchScreen") { |
| 2549 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2550 | } else if (deviceTypeString == "touchPad") { |
| 2551 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2552 | } else if (deviceTypeString == "pointer") { |
| 2553 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2554 | } else if (deviceTypeString != "default") { |
Steve Block | a51f0e7 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2555 | ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string()); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2556 | } |
| 2557 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2558 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2559 | mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2560 | getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"), |
| 2561 | mParameters.orientationAware); |
| 2562 | |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2563 | mParameters.associatedDisplayId = -1; |
| 2564 | mParameters.associatedDisplayIsExternal = false; |
| 2565 | if (mParameters.orientationAware |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2566 | || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2567 | || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) { |
| 2568 | mParameters.associatedDisplayIsExternal = |
| 2569 | mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN |
| 2570 | && getDevice()->isExternal(); |
| 2571 | mParameters.associatedDisplayId = 0; |
| 2572 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2573 | } |
| 2574 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2575 | void TouchInputMapper::dumpParameters(String8& dump) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2576 | dump.append(INDENT3 "Parameters:\n"); |
| 2577 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2578 | switch (mParameters.gestureMode) { |
| 2579 | case Parameters::GESTURE_MODE_POINTER: |
| 2580 | dump.append(INDENT4 "GestureMode: pointer\n"); |
| 2581 | break; |
| 2582 | case Parameters::GESTURE_MODE_SPOTS: |
| 2583 | dump.append(INDENT4 "GestureMode: spots\n"); |
| 2584 | break; |
| 2585 | default: |
| 2586 | assert(false); |
| 2587 | } |
| 2588 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2589 | switch (mParameters.deviceType) { |
| 2590 | case Parameters::DEVICE_TYPE_TOUCH_SCREEN: |
| 2591 | dump.append(INDENT4 "DeviceType: touchScreen\n"); |
| 2592 | break; |
| 2593 | case Parameters::DEVICE_TYPE_TOUCH_PAD: |
| 2594 | dump.append(INDENT4 "DeviceType: touchPad\n"); |
| 2595 | break; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2596 | case Parameters::DEVICE_TYPE_POINTER: |
| 2597 | dump.append(INDENT4 "DeviceType: pointer\n"); |
| 2598 | break; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2599 | default: |
Steve Block | f68633d | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 2600 | ALOG_ASSERT(false); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2601 | } |
| 2602 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2603 | dump.appendFormat(INDENT4 "AssociatedDisplay: id=%d, isExternal=%s\n", |
| 2604 | mParameters.associatedDisplayId, toString(mParameters.associatedDisplayIsExternal)); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2605 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 2606 | toString(mParameters.orientationAware)); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2607 | } |
| 2608 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2609 | void TouchInputMapper::configureRawPointerAxes() { |
| 2610 | mRawPointerAxes.clear(); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2611 | } |
| 2612 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2613 | void TouchInputMapper::dumpRawPointerAxes(String8& dump) { |
| 2614 | dump.append(INDENT3 "Raw Touch Axes:\n"); |
| 2615 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X"); |
| 2616 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y"); |
| 2617 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure"); |
| 2618 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor"); |
| 2619 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor"); |
| 2620 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor"); |
| 2621 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor"); |
| 2622 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation"); |
| 2623 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance"); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2624 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX"); |
| 2625 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY"); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2626 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId"); |
| 2627 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot"); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2628 | } |
| 2629 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2630 | void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) { |
| 2631 | int32_t oldDeviceMode = mDeviceMode; |
| 2632 | |
| 2633 | // Determine device mode. |
| 2634 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER |
| 2635 | && mConfig.pointerGesturesEnabled) { |
| 2636 | mSource = AINPUT_SOURCE_MOUSE; |
| 2637 | mDeviceMode = DEVICE_MODE_POINTER; |
| 2638 | } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN |
| 2639 | && mParameters.associatedDisplayId >= 0) { |
| 2640 | mSource = AINPUT_SOURCE_TOUCHSCREEN; |
| 2641 | mDeviceMode = DEVICE_MODE_DIRECT; |
| 2642 | } else { |
| 2643 | mSource = AINPUT_SOURCE_TOUCHPAD; |
| 2644 | mDeviceMode = DEVICE_MODE_UNSCALED; |
| 2645 | } |
| 2646 | |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2647 | // Ensure we have valid X and Y axes. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2648 | if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) { |
Steve Block | a51f0e7 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2649 | ALOGW(INDENT "Touch device '%s' did not report support for X or Y axis! " |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2650 | "The device will be inoperable.", getDeviceName().string()); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2651 | mDeviceMode = DEVICE_MODE_DISABLED; |
| 2652 | return; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2653 | } |
| 2654 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2655 | // Get associated display dimensions. |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2656 | if (mParameters.associatedDisplayId >= 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2657 | if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId, |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2658 | mParameters.associatedDisplayIsExternal, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2659 | &mAssociatedDisplayWidth, &mAssociatedDisplayHeight, |
| 2660 | &mAssociatedDisplayOrientation)) { |
Steve Block | 933e856 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2661 | ALOGI(INDENT "Touch device '%s' could not query the properties of its associated " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2662 | "display %d. The device will be inoperable until the display size " |
| 2663 | "becomes available.", |
| 2664 | getDeviceName().string(), mParameters.associatedDisplayId); |
| 2665 | mDeviceMode = DEVICE_MODE_DISABLED; |
| 2666 | return; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2667 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2668 | } |
| 2669 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2670 | // Configure dimensions. |
| 2671 | int32_t width, height, orientation; |
| 2672 | if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) { |
| 2673 | width = mAssociatedDisplayWidth; |
| 2674 | height = mAssociatedDisplayHeight; |
| 2675 | orientation = mParameters.orientationAware ? |
| 2676 | mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0; |
| 2677 | } else { |
| 2678 | width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1; |
| 2679 | height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1; |
| 2680 | orientation = DISPLAY_ORIENTATION_0; |
| 2681 | } |
| 2682 | |
| 2683 | // If moving between pointer modes, need to reset some state. |
| 2684 | bool deviceModeChanged; |
| 2685 | if (mDeviceMode != oldDeviceMode) { |
| 2686 | deviceModeChanged = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2687 | mOrientedRanges.clear(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2688 | } |
| 2689 | |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 2690 | // Create pointer controller if needed. |
| 2691 | if (mDeviceMode == DEVICE_MODE_POINTER || |
| 2692 | (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) { |
| 2693 | if (mPointerController == NULL) { |
| 2694 | mPointerController = getPolicy()->obtainPointerController(getDeviceId()); |
| 2695 | } |
| 2696 | } else { |
| 2697 | mPointerController.clear(); |
| 2698 | } |
| 2699 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2700 | bool orientationChanged = mSurfaceOrientation != orientation; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2701 | if (orientationChanged) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2702 | mSurfaceOrientation = orientation; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2703 | } |
| 2704 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2705 | bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2706 | if (sizeChanged || deviceModeChanged) { |
Steve Block | 933e856 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2707 | ALOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d, mode is %d", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2708 | getDeviceId(), getDeviceName().string(), width, height, mDeviceMode); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2709 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2710 | mSurfaceWidth = width; |
| 2711 | mSurfaceHeight = height; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2712 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2713 | // Configure X and Y factors. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2714 | mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1); |
| 2715 | mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1); |
| 2716 | mXPrecision = 1.0f / mXScale; |
| 2717 | mYPrecision = 1.0f / mYScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2718 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2719 | mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2720 | mOrientedRanges.x.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2721 | mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2722 | mOrientedRanges.y.source = mSource; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2723 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2724 | configureVirtualKeys(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2725 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2726 | // Scale factor for terms that are not oriented in a particular axis. |
| 2727 | // If the pixels are square then xScale == yScale otherwise we fake it |
| 2728 | // by choosing an average. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2729 | mGeometricScale = avg(mXScale, mYScale); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2730 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2731 | // Size of diagonal axis. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2732 | float diagonalSize = hypotf(width, height); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2733 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 2734 | // Size factors. |
| 2735 | if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) { |
| 2736 | if (mRawPointerAxes.touchMajor.valid |
| 2737 | && mRawPointerAxes.touchMajor.maxValue != 0) { |
| 2738 | mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue; |
| 2739 | } else if (mRawPointerAxes.toolMajor.valid |
| 2740 | && mRawPointerAxes.toolMajor.maxValue != 0) { |
| 2741 | mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue; |
| 2742 | } else { |
| 2743 | mSizeScale = 0.0f; |
| 2744 | } |
| 2745 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2746 | mOrientedRanges.haveTouchSize = true; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 2747 | mOrientedRanges.haveToolSize = true; |
| 2748 | mOrientedRanges.haveSize = true; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2749 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2750 | mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2751 | mOrientedRanges.touchMajor.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2752 | mOrientedRanges.touchMajor.min = 0; |
| 2753 | mOrientedRanges.touchMajor.max = diagonalSize; |
| 2754 | mOrientedRanges.touchMajor.flat = 0; |
| 2755 | mOrientedRanges.touchMajor.fuzz = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2756 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2757 | mOrientedRanges.touchMinor = mOrientedRanges.touchMajor; |
| 2758 | mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2759 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2760 | mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2761 | mOrientedRanges.toolMajor.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2762 | mOrientedRanges.toolMajor.min = 0; |
| 2763 | mOrientedRanges.toolMajor.max = diagonalSize; |
| 2764 | mOrientedRanges.toolMajor.flat = 0; |
| 2765 | mOrientedRanges.toolMajor.fuzz = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2766 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2767 | mOrientedRanges.toolMinor = mOrientedRanges.toolMajor; |
| 2768 | mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 2769 | |
| 2770 | mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2771 | mOrientedRanges.size.source = mSource; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 2772 | mOrientedRanges.size.min = 0; |
| 2773 | mOrientedRanges.size.max = 1.0; |
| 2774 | mOrientedRanges.size.flat = 0; |
| 2775 | mOrientedRanges.size.fuzz = 0; |
| 2776 | } else { |
| 2777 | mSizeScale = 0.0f; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2778 | } |
| 2779 | |
| 2780 | // Pressure factors. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2781 | mPressureScale = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2782 | if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL |
| 2783 | || mCalibration.pressureCalibration |
| 2784 | == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) { |
| 2785 | if (mCalibration.havePressureScale) { |
| 2786 | mPressureScale = mCalibration.pressureScale; |
| 2787 | } else if (mRawPointerAxes.pressure.valid |
| 2788 | && mRawPointerAxes.pressure.maxValue != 0) { |
| 2789 | mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue; |
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 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2792 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2793 | mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE; |
| 2794 | mOrientedRanges.pressure.source = mSource; |
| 2795 | mOrientedRanges.pressure.min = 0; |
| 2796 | mOrientedRanges.pressure.max = 1.0; |
| 2797 | mOrientedRanges.pressure.flat = 0; |
| 2798 | mOrientedRanges.pressure.fuzz = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2799 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2800 | // Tilt |
| 2801 | mTiltXCenter = 0; |
| 2802 | mTiltXScale = 0; |
| 2803 | mTiltYCenter = 0; |
| 2804 | mTiltYScale = 0; |
| 2805 | mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid; |
| 2806 | if (mHaveTilt) { |
| 2807 | mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue, |
| 2808 | mRawPointerAxes.tiltX.maxValue); |
| 2809 | mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue, |
| 2810 | mRawPointerAxes.tiltY.maxValue); |
| 2811 | mTiltXScale = M_PI / 180; |
| 2812 | mTiltYScale = M_PI / 180; |
| 2813 | |
| 2814 | mOrientedRanges.haveTilt = true; |
| 2815 | |
| 2816 | mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT; |
| 2817 | mOrientedRanges.tilt.source = mSource; |
| 2818 | mOrientedRanges.tilt.min = 0; |
| 2819 | mOrientedRanges.tilt.max = M_PI_2; |
| 2820 | mOrientedRanges.tilt.flat = 0; |
| 2821 | mOrientedRanges.tilt.fuzz = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2822 | } |
| 2823 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2824 | // Orientation |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2825 | mOrientationCenter = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2826 | mOrientationScale = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2827 | if (mHaveTilt) { |
| 2828 | mOrientedRanges.haveOrientation = true; |
| 2829 | |
| 2830 | mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION; |
| 2831 | mOrientedRanges.orientation.source = mSource; |
| 2832 | mOrientedRanges.orientation.min = -M_PI; |
| 2833 | mOrientedRanges.orientation.max = M_PI; |
| 2834 | mOrientedRanges.orientation.flat = 0; |
| 2835 | mOrientedRanges.orientation.fuzz = 0; |
| 2836 | } else if (mCalibration.orientationCalibration != |
| 2837 | Calibration::ORIENTATION_CALIBRATION_NONE) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2838 | if (mCalibration.orientationCalibration |
| 2839 | == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2840 | if (mRawPointerAxes.orientation.valid) { |
| 2841 | mOrientationCenter = avg(mRawPointerAxes.orientation.minValue, |
| 2842 | mRawPointerAxes.orientation.maxValue); |
| 2843 | mOrientationScale = M_PI / (mRawPointerAxes.orientation.maxValue - |
| 2844 | mRawPointerAxes.orientation.minValue); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2845 | } |
| 2846 | } |
| 2847 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2848 | mOrientedRanges.haveOrientation = true; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2849 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2850 | mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2851 | mOrientedRanges.orientation.source = mSource; |
| 2852 | mOrientedRanges.orientation.min = -M_PI_2; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2853 | mOrientedRanges.orientation.max = M_PI_2; |
| 2854 | mOrientedRanges.orientation.flat = 0; |
| 2855 | mOrientedRanges.orientation.fuzz = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2856 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2857 | |
| 2858 | // Distance |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2859 | mDistanceScale = 0; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2860 | if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) { |
| 2861 | if (mCalibration.distanceCalibration |
| 2862 | == Calibration::DISTANCE_CALIBRATION_SCALED) { |
| 2863 | if (mCalibration.haveDistanceScale) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2864 | mDistanceScale = mCalibration.distanceScale; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2865 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2866 | mDistanceScale = 1.0f; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2867 | } |
| 2868 | } |
| 2869 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2870 | mOrientedRanges.haveDistance = true; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2871 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2872 | mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2873 | mOrientedRanges.distance.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2874 | mOrientedRanges.distance.min = |
| 2875 | mRawPointerAxes.distance.minValue * mDistanceScale; |
| 2876 | mOrientedRanges.distance.max = |
Andreas Sandblad | 8239940 | 2012-03-21 14:39:57 +0100 | [diff] [blame^] | 2877 | mRawPointerAxes.distance.maxValue * mDistanceScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2878 | mOrientedRanges.distance.flat = 0; |
| 2879 | mOrientedRanges.distance.fuzz = |
| 2880 | mRawPointerAxes.distance.fuzz * mDistanceScale; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2881 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2882 | } |
| 2883 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2884 | if (orientationChanged || sizeChanged || deviceModeChanged) { |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2885 | // Compute oriented surface dimensions, precision, scales and ranges. |
| 2886 | // Note that the maximum value reported is an inclusive maximum value so it is one |
| 2887 | // unit less than the total width or height of surface. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2888 | switch (mSurfaceOrientation) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 2889 | case DISPLAY_ORIENTATION_90: |
| 2890 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2891 | mOrientedSurfaceWidth = mSurfaceHeight; |
| 2892 | mOrientedSurfaceHeight = mSurfaceWidth; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2893 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2894 | mOrientedXPrecision = mYPrecision; |
| 2895 | mOrientedYPrecision = mXPrecision; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2896 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2897 | mOrientedRanges.x.min = 0; |
| 2898 | mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue) |
| 2899 | * mYScale; |
| 2900 | mOrientedRanges.x.flat = 0; |
| 2901 | mOrientedRanges.x.fuzz = mYScale; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2902 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2903 | mOrientedRanges.y.min = 0; |
| 2904 | mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue) |
| 2905 | * mXScale; |
| 2906 | mOrientedRanges.y.flat = 0; |
| 2907 | mOrientedRanges.y.fuzz = mXScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2908 | break; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2909 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2910 | default: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2911 | mOrientedSurfaceWidth = mSurfaceWidth; |
| 2912 | mOrientedSurfaceHeight = mSurfaceHeight; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2913 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2914 | mOrientedXPrecision = mXPrecision; |
| 2915 | mOrientedYPrecision = mYPrecision; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2916 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2917 | mOrientedRanges.x.min = 0; |
| 2918 | mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue) |
| 2919 | * mXScale; |
| 2920 | mOrientedRanges.x.flat = 0; |
| 2921 | mOrientedRanges.x.fuzz = mXScale; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2922 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2923 | mOrientedRanges.y.min = 0; |
| 2924 | mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue) |
| 2925 | * mYScale; |
| 2926 | mOrientedRanges.y.flat = 0; |
| 2927 | mOrientedRanges.y.fuzz = mYScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2928 | break; |
| 2929 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2930 | |
| 2931 | // Compute pointer gesture detection parameters. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2932 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2933 | int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1; |
| 2934 | int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2935 | float rawDiagonal = hypotf(rawWidth, rawHeight); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2936 | float displayDiagonal = hypotf(mAssociatedDisplayWidth, |
| 2937 | mAssociatedDisplayHeight); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2938 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2939 | // 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] | 2940 | // given area relative to the diagonal size of the display when no acceleration |
| 2941 | // is applied. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2942 | // Assume that the touch pad has a square aspect ratio such that movements in |
| 2943 | // 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] | 2944 | mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2945 | * displayDiagonal / rawDiagonal; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2946 | mPointerYMovementScale = mPointerXMovementScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2947 | |
| 2948 | // Scale zooms to cover a smaller range of the display than movements do. |
| 2949 | // This value determines the area around the pointer that is affected by freeform |
| 2950 | // pointer gestures. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2951 | mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2952 | * displayDiagonal / rawDiagonal; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2953 | mPointerYZoomScale = mPointerXZoomScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2954 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2955 | // Max width between pointers to detect a swipe gesture is more than some fraction |
| 2956 | // of the diagonal axis of the touch pad. Touches that are wider than this are |
| 2957 | // translated into freeform gestures. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2958 | mPointerGestureMaxSwipeWidth = |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2959 | mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2960 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2961 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2962 | // Abort current pointer usages because the state has changed. |
| 2963 | abortPointerUsage(when, 0 /*policyFlags*/); |
| 2964 | |
| 2965 | // Inform the dispatcher about the changes. |
| 2966 | *outResetNeeded = true; |
| 2967 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2968 | } |
| 2969 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2970 | void TouchInputMapper::dumpSurface(String8& dump) { |
| 2971 | dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth); |
| 2972 | dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight); |
| 2973 | dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2974 | } |
| 2975 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2976 | void TouchInputMapper::configureVirtualKeys() { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2977 | Vector<VirtualKeyDefinition> virtualKeyDefinitions; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 2978 | getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2979 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2980 | mVirtualKeys.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2981 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2982 | if (virtualKeyDefinitions.size() == 0) { |
| 2983 | return; |
| 2984 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2985 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2986 | mVirtualKeys.setCapacity(virtualKeyDefinitions.size()); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2987 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2988 | int32_t touchScreenLeft = mRawPointerAxes.x.minValue; |
| 2989 | int32_t touchScreenTop = mRawPointerAxes.y.minValue; |
| 2990 | int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1; |
| 2991 | int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2992 | |
| 2993 | for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2994 | const VirtualKeyDefinition& virtualKeyDefinition = |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2995 | virtualKeyDefinitions[i]; |
| 2996 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2997 | mVirtualKeys.add(); |
| 2998 | VirtualKey& virtualKey = mVirtualKeys.editTop(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2999 | |
| 3000 | virtualKey.scanCode = virtualKeyDefinition.scanCode; |
| 3001 | int32_t keyCode; |
| 3002 | uint32_t flags; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 3003 | if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3004 | & keyCode, & flags)) { |
Steve Block | a51f0e7 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3005 | ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3006 | virtualKey.scanCode); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3007 | mVirtualKeys.pop(); // drop the key |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3008 | continue; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3009 | } |
| 3010 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3011 | virtualKey.keyCode = keyCode; |
| 3012 | virtualKey.flags = flags; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3013 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3014 | // convert the key definition's display coordinates into touch coordinates for a hit box |
| 3015 | int32_t halfWidth = virtualKeyDefinition.width / 2; |
| 3016 | int32_t halfHeight = virtualKeyDefinition.height / 2; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3017 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3018 | virtualKey.hitLeft = (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.hitRight= (virtualKeyDefinition.centerX + halfWidth) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3021 | * touchScreenWidth / mSurfaceWidth + touchScreenLeft; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3022 | virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3023 | * touchScreenHeight / mSurfaceHeight + touchScreenTop; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3024 | virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3025 | * touchScreenHeight / mSurfaceHeight + touchScreenTop; |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3026 | } |
| 3027 | } |
| 3028 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3029 | void TouchInputMapper::dumpVirtualKeys(String8& dump) { |
| 3030 | if (!mVirtualKeys.isEmpty()) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3031 | dump.append(INDENT3 "Virtual Keys:\n"); |
| 3032 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3033 | for (size_t i = 0; i < mVirtualKeys.size(); i++) { |
| 3034 | const VirtualKey& virtualKey = mVirtualKeys.itemAt(i); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3035 | dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, " |
| 3036 | "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n", |
| 3037 | i, virtualKey.scanCode, virtualKey.keyCode, |
| 3038 | virtualKey.hitLeft, virtualKey.hitRight, |
| 3039 | virtualKey.hitTop, virtualKey.hitBottom); |
| 3040 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3041 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3042 | } |
| 3043 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3044 | void TouchInputMapper::parseCalibration() { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 3045 | const PropertyMap& in = getDevice()->getConfiguration(); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3046 | Calibration& out = mCalibration; |
| 3047 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3048 | // Size |
| 3049 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT; |
| 3050 | String8 sizeCalibrationString; |
| 3051 | if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) { |
| 3052 | if (sizeCalibrationString == "none") { |
| 3053 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; |
| 3054 | } else if (sizeCalibrationString == "geometric") { |
| 3055 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC; |
| 3056 | } else if (sizeCalibrationString == "diameter") { |
| 3057 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER; |
| 3058 | } else if (sizeCalibrationString == "area") { |
| 3059 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA; |
| 3060 | } else if (sizeCalibrationString != "default") { |
Steve Block | a51f0e7 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3061 | ALOGW("Invalid value for touch.size.calibration: '%s'", |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3062 | sizeCalibrationString.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3063 | } |
| 3064 | } |
| 3065 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3066 | out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"), |
| 3067 | out.sizeScale); |
| 3068 | out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"), |
| 3069 | out.sizeBias); |
| 3070 | out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"), |
| 3071 | out.sizeIsSummed); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3072 | |
| 3073 | // Pressure |
| 3074 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT; |
| 3075 | String8 pressureCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 3076 | if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3077 | if (pressureCalibrationString == "none") { |
| 3078 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; |
| 3079 | } else if (pressureCalibrationString == "physical") { |
| 3080 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL; |
| 3081 | } else if (pressureCalibrationString == "amplitude") { |
| 3082 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE; |
| 3083 | } else if (pressureCalibrationString != "default") { |
Steve Block | a51f0e7 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3084 | ALOGW("Invalid value for touch.pressure.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3085 | pressureCalibrationString.string()); |
| 3086 | } |
| 3087 | } |
| 3088 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3089 | out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"), |
| 3090 | out.pressureScale); |
| 3091 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3092 | // Orientation |
| 3093 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT; |
| 3094 | String8 orientationCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 3095 | if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3096 | if (orientationCalibrationString == "none") { |
| 3097 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; |
| 3098 | } else if (orientationCalibrationString == "interpolated") { |
| 3099 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 3100 | } else if (orientationCalibrationString == "vector") { |
| 3101 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3102 | } else if (orientationCalibrationString != "default") { |
Steve Block | a51f0e7 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3103 | ALOGW("Invalid value for touch.orientation.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3104 | orientationCalibrationString.string()); |
| 3105 | } |
| 3106 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3107 | |
| 3108 | // Distance |
| 3109 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT; |
| 3110 | String8 distanceCalibrationString; |
| 3111 | if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) { |
| 3112 | if (distanceCalibrationString == "none") { |
| 3113 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE; |
| 3114 | } else if (distanceCalibrationString == "scaled") { |
| 3115 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED; |
| 3116 | } else if (distanceCalibrationString != "default") { |
Steve Block | a51f0e7 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3117 | ALOGW("Invalid value for touch.distance.calibration: '%s'", |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3118 | distanceCalibrationString.string()); |
| 3119 | } |
| 3120 | } |
| 3121 | |
| 3122 | out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"), |
| 3123 | out.distanceScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3124 | } |
| 3125 | |
| 3126 | void TouchInputMapper::resolveCalibration() { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3127 | // Size |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3128 | if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) { |
| 3129 | if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) { |
| 3130 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3131 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3132 | } else { |
| 3133 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; |
| 3134 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3135 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3136 | // Pressure |
| 3137 | if (mRawPointerAxes.pressure.valid) { |
| 3138 | if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) { |
| 3139 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL; |
| 3140 | } |
| 3141 | } else { |
| 3142 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3143 | } |
| 3144 | |
| 3145 | // Orientation |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3146 | if (mRawPointerAxes.orientation.valid) { |
| 3147 | if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3148 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3149 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3150 | } else { |
| 3151 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3152 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3153 | |
| 3154 | // Distance |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3155 | if (mRawPointerAxes.distance.valid) { |
| 3156 | if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3157 | mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3158 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3159 | } else { |
| 3160 | mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3161 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3162 | } |
| 3163 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3164 | void TouchInputMapper::dumpCalibration(String8& dump) { |
| 3165 | dump.append(INDENT3 "Calibration:\n"); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3166 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3167 | // Size |
| 3168 | switch (mCalibration.sizeCalibration) { |
| 3169 | case Calibration::SIZE_CALIBRATION_NONE: |
| 3170 | dump.append(INDENT4 "touch.size.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3171 | break; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3172 | case Calibration::SIZE_CALIBRATION_GEOMETRIC: |
| 3173 | dump.append(INDENT4 "touch.size.calibration: geometric\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3174 | break; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3175 | case Calibration::SIZE_CALIBRATION_DIAMETER: |
| 3176 | dump.append(INDENT4 "touch.size.calibration: diameter\n"); |
| 3177 | break; |
| 3178 | case Calibration::SIZE_CALIBRATION_AREA: |
| 3179 | dump.append(INDENT4 "touch.size.calibration: area\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3180 | break; |
| 3181 | default: |
Steve Block | f68633d | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3182 | ALOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3183 | } |
| 3184 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3185 | if (mCalibration.haveSizeScale) { |
| 3186 | dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n", |
| 3187 | mCalibration.sizeScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3188 | } |
| 3189 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3190 | if (mCalibration.haveSizeBias) { |
| 3191 | dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n", |
| 3192 | mCalibration.sizeBias); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3193 | } |
| 3194 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3195 | if (mCalibration.haveSizeIsSummed) { |
| 3196 | dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n", |
| 3197 | toString(mCalibration.sizeIsSummed)); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3198 | } |
| 3199 | |
| 3200 | // Pressure |
| 3201 | switch (mCalibration.pressureCalibration) { |
| 3202 | case Calibration::PRESSURE_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3203 | dump.append(INDENT4 "touch.pressure.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3204 | break; |
| 3205 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3206 | dump.append(INDENT4 "touch.pressure.calibration: physical\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3207 | break; |
| 3208 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3209 | dump.append(INDENT4 "touch.pressure.calibration: amplitude\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3210 | break; |
| 3211 | default: |
Steve Block | f68633d | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3212 | ALOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3213 | } |
| 3214 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3215 | if (mCalibration.havePressureScale) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3216 | dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n", |
| 3217 | mCalibration.pressureScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3218 | } |
| 3219 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3220 | // Orientation |
| 3221 | switch (mCalibration.orientationCalibration) { |
| 3222 | case Calibration::ORIENTATION_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3223 | dump.append(INDENT4 "touch.orientation.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3224 | break; |
| 3225 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3226 | dump.append(INDENT4 "touch.orientation.calibration: interpolated\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3227 | break; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 3228 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: |
| 3229 | dump.append(INDENT4 "touch.orientation.calibration: vector\n"); |
| 3230 | break; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3231 | default: |
Steve Block | f68633d | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3232 | ALOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3233 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3234 | |
| 3235 | // Distance |
| 3236 | switch (mCalibration.distanceCalibration) { |
| 3237 | case Calibration::DISTANCE_CALIBRATION_NONE: |
| 3238 | dump.append(INDENT4 "touch.distance.calibration: none\n"); |
| 3239 | break; |
| 3240 | case Calibration::DISTANCE_CALIBRATION_SCALED: |
| 3241 | dump.append(INDENT4 "touch.distance.calibration: scaled\n"); |
| 3242 | break; |
| 3243 | default: |
Steve Block | f68633d | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3244 | ALOG_ASSERT(false); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3245 | } |
| 3246 | |
| 3247 | if (mCalibration.haveDistanceScale) { |
| 3248 | dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n", |
| 3249 | mCalibration.distanceScale); |
| 3250 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3251 | } |
| 3252 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3253 | void TouchInputMapper::reset(nsecs_t when) { |
| 3254 | mCursorButtonAccumulator.reset(getDevice()); |
| 3255 | mCursorScrollAccumulator.reset(getDevice()); |
| 3256 | mTouchButtonAccumulator.reset(getDevice()); |
| 3257 | |
| 3258 | mPointerVelocityControl.reset(); |
| 3259 | mWheelXVelocityControl.reset(); |
| 3260 | mWheelYVelocityControl.reset(); |
| 3261 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3262 | mCurrentRawPointerData.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3263 | mLastRawPointerData.clear(); |
| 3264 | mCurrentCookedPointerData.clear(); |
| 3265 | mLastCookedPointerData.clear(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3266 | mCurrentButtonState = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3267 | mLastButtonState = 0; |
| 3268 | mCurrentRawVScroll = 0; |
| 3269 | mCurrentRawHScroll = 0; |
| 3270 | mCurrentFingerIdBits.clear(); |
| 3271 | mLastFingerIdBits.clear(); |
| 3272 | mCurrentStylusIdBits.clear(); |
| 3273 | mLastStylusIdBits.clear(); |
| 3274 | mCurrentMouseIdBits.clear(); |
| 3275 | mLastMouseIdBits.clear(); |
| 3276 | mPointerUsage = POINTER_USAGE_NONE; |
| 3277 | mSentHoverEnter = false; |
| 3278 | mDownTime = 0; |
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 | mCurrentVirtualKey.down = false; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3281 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3282 | mPointerGesture.reset(); |
| 3283 | mPointerSimple.reset(); |
| 3284 | |
| 3285 | if (mPointerController != NULL) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3286 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 3287 | mPointerController->clearSpots(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3288 | } |
| 3289 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3290 | InputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3291 | } |
| 3292 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3293 | void TouchInputMapper::process(const RawEvent* rawEvent) { |
| 3294 | mCursorButtonAccumulator.process(rawEvent); |
| 3295 | mCursorScrollAccumulator.process(rawEvent); |
| 3296 | mTouchButtonAccumulator.process(rawEvent); |
| 3297 | |
| 3298 | if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) { |
| 3299 | sync(rawEvent->when); |
| 3300 | } |
| 3301 | } |
| 3302 | |
| 3303 | void TouchInputMapper::sync(nsecs_t when) { |
| 3304 | // Sync button state. |
| 3305 | mCurrentButtonState = mTouchButtonAccumulator.getButtonState() |
| 3306 | | mCursorButtonAccumulator.getButtonState(); |
| 3307 | |
| 3308 | // Sync scroll state. |
| 3309 | mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel(); |
| 3310 | mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel(); |
| 3311 | mCursorScrollAccumulator.finishSync(); |
| 3312 | |
| 3313 | // Sync touch state. |
| 3314 | bool havePointerIds = true; |
| 3315 | mCurrentRawPointerData.clear(); |
| 3316 | syncTouch(when, &havePointerIds); |
| 3317 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 3318 | #if DEBUG_RAW_EVENTS |
| 3319 | if (!havePointerIds) { |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3320 | ALOGD("syncTouch: pointerCount %d -> %d, no pointer ids", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3321 | mLastRawPointerData.pointerCount, |
| 3322 | mCurrentRawPointerData.pointerCount); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 3323 | } else { |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3324 | ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, " |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3325 | "hovering ids 0x%08x -> 0x%08x", |
| 3326 | mLastRawPointerData.pointerCount, |
| 3327 | mCurrentRawPointerData.pointerCount, |
| 3328 | mLastRawPointerData.touchingIdBits.value, |
| 3329 | mCurrentRawPointerData.touchingIdBits.value, |
| 3330 | mLastRawPointerData.hoveringIdBits.value, |
| 3331 | mCurrentRawPointerData.hoveringIdBits.value); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 3332 | } |
| 3333 | #endif |
| 3334 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3335 | // Reset state that we will compute below. |
| 3336 | mCurrentFingerIdBits.clear(); |
| 3337 | mCurrentStylusIdBits.clear(); |
| 3338 | mCurrentMouseIdBits.clear(); |
| 3339 | mCurrentCookedPointerData.clear(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3340 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3341 | if (mDeviceMode == DEVICE_MODE_DISABLED) { |
| 3342 | // Drop all input if the device is disabled. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3343 | mCurrentRawPointerData.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3344 | mCurrentButtonState = 0; |
| 3345 | } else { |
| 3346 | // Preprocess pointer data. |
| 3347 | if (!havePointerIds) { |
| 3348 | assignPointerIds(); |
| 3349 | } |
| 3350 | |
| 3351 | // Handle policy on initial down or hover events. |
| 3352 | uint32_t policyFlags = 0; |
Jeff Brown | c28306a | 2011-08-23 21:32:42 -0700 | [diff] [blame] | 3353 | bool initialDown = mLastRawPointerData.pointerCount == 0 |
| 3354 | && mCurrentRawPointerData.pointerCount != 0; |
| 3355 | bool buttonsPressed = mCurrentButtonState & ~mLastButtonState; |
| 3356 | if (initialDown || buttonsPressed) { |
| 3357 | // 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] | 3358 | if (mDeviceMode == DEVICE_MODE_DIRECT) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3359 | getContext()->fadePointer(); |
| 3360 | } |
| 3361 | |
| 3362 | // Initial downs on external touch devices should wake the device. |
| 3363 | // We don't do this for internal touch screens to prevent them from waking |
| 3364 | // up in your pocket. |
| 3365 | // TODO: Use the input device configuration to control this behavior more finely. |
| 3366 | if (getDevice()->isExternal()) { |
| 3367 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 3368 | } |
| 3369 | } |
| 3370 | |
| 3371 | // Synthesize key down from raw buttons if needed. |
| 3372 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource, |
| 3373 | policyFlags, mLastButtonState, mCurrentButtonState); |
| 3374 | |
| 3375 | // Consume raw off-screen touches before cooking pointer data. |
| 3376 | // If touches are consumed, subsequent code will not receive any pointer data. |
| 3377 | if (consumeRawTouches(when, policyFlags)) { |
| 3378 | mCurrentRawPointerData.clear(); |
| 3379 | } |
| 3380 | |
| 3381 | // Cook pointer data. This call populates the mCurrentCookedPointerData structure |
| 3382 | // with cooked pointer data that has the same ids and indices as the raw data. |
| 3383 | // The following code can use either the raw or cooked data, as needed. |
| 3384 | cookPointerData(); |
| 3385 | |
| 3386 | // 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] | 3387 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3388 | for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) { |
| 3389 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 3390 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3391 | if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS |
| 3392 | || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) { |
| 3393 | mCurrentStylusIdBits.markBit(id); |
| 3394 | } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER |
| 3395 | || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 3396 | mCurrentFingerIdBits.markBit(id); |
| 3397 | } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) { |
| 3398 | mCurrentMouseIdBits.markBit(id); |
| 3399 | } |
| 3400 | } |
| 3401 | for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) { |
| 3402 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 3403 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3404 | if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS |
| 3405 | || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) { |
| 3406 | mCurrentStylusIdBits.markBit(id); |
| 3407 | } |
| 3408 | } |
| 3409 | |
| 3410 | // Stylus takes precedence over all tools, then mouse, then finger. |
| 3411 | PointerUsage pointerUsage = mPointerUsage; |
| 3412 | if (!mCurrentStylusIdBits.isEmpty()) { |
| 3413 | mCurrentMouseIdBits.clear(); |
| 3414 | mCurrentFingerIdBits.clear(); |
| 3415 | pointerUsage = POINTER_USAGE_STYLUS; |
| 3416 | } else if (!mCurrentMouseIdBits.isEmpty()) { |
| 3417 | mCurrentFingerIdBits.clear(); |
| 3418 | pointerUsage = POINTER_USAGE_MOUSE; |
| 3419 | } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) { |
| 3420 | pointerUsage = POINTER_USAGE_GESTURES; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3421 | } |
| 3422 | |
| 3423 | dispatchPointerUsage(when, policyFlags, pointerUsage); |
| 3424 | } else { |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 3425 | if (mDeviceMode == DEVICE_MODE_DIRECT |
| 3426 | && mConfig.showTouches && mPointerController != NULL) { |
| 3427 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT); |
| 3428 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 3429 | |
| 3430 | mPointerController->setButtonState(mCurrentButtonState); |
| 3431 | mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords, |
| 3432 | mCurrentCookedPointerData.idToIndex, |
| 3433 | mCurrentCookedPointerData.touchingIdBits); |
| 3434 | } |
| 3435 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3436 | dispatchHoverExit(when, policyFlags); |
| 3437 | dispatchTouches(when, policyFlags); |
| 3438 | dispatchHoverEnterAndMove(when, policyFlags); |
| 3439 | } |
| 3440 | |
| 3441 | // Synthesize key up from raw buttons if needed. |
| 3442 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource, |
| 3443 | policyFlags, mLastButtonState, mCurrentButtonState); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3444 | } |
| 3445 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3446 | // Copy current touch to last touch in preparation for the next cycle. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3447 | mLastRawPointerData.copyFrom(mCurrentRawPointerData); |
| 3448 | mLastCookedPointerData.copyFrom(mCurrentCookedPointerData); |
| 3449 | mLastButtonState = mCurrentButtonState; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3450 | mLastFingerIdBits = mCurrentFingerIdBits; |
| 3451 | mLastStylusIdBits = mCurrentStylusIdBits; |
| 3452 | mLastMouseIdBits = mCurrentMouseIdBits; |
| 3453 | |
| 3454 | // Clear some transient state. |
| 3455 | mCurrentRawVScroll = 0; |
| 3456 | mCurrentRawHScroll = 0; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3457 | } |
| 3458 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3459 | void TouchInputMapper::timeoutExpired(nsecs_t when) { |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 3460 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3461 | if (mPointerUsage == POINTER_USAGE_GESTURES) { |
| 3462 | dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/); |
| 3463 | } |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3464 | } |
| 3465 | } |
| 3466 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3467 | bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) { |
| 3468 | // Check for release of a virtual key. |
| 3469 | if (mCurrentVirtualKey.down) { |
| 3470 | if (mCurrentRawPointerData.touchingIdBits.isEmpty()) { |
| 3471 | // Pointer went up while virtual key was down. |
| 3472 | mCurrentVirtualKey.down = false; |
| 3473 | if (!mCurrentVirtualKey.ignored) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3474 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3475 | ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3476 | mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3477 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3478 | dispatchVirtualKey(when, policyFlags, |
| 3479 | AKEY_EVENT_ACTION_UP, |
| 3480 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3481 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3482 | return true; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3483 | } |
| 3484 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3485 | if (mCurrentRawPointerData.touchingIdBits.count() == 1) { |
| 3486 | uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit(); |
| 3487 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3488 | const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y); |
| 3489 | if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) { |
| 3490 | // Pointer is still within the space of the virtual key. |
| 3491 | return true; |
| 3492 | } |
| 3493 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3494 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3495 | // Pointer left virtual key area or another pointer also went down. |
| 3496 | // Send key cancellation but do not consume the touch yet. |
| 3497 | // This is useful when the user swipes through from the virtual key area |
| 3498 | // into the main display surface. |
| 3499 | mCurrentVirtualKey.down = false; |
| 3500 | if (!mCurrentVirtualKey.ignored) { |
| 3501 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3502 | ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3503 | mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode); |
| 3504 | #endif |
| 3505 | dispatchVirtualKey(when, policyFlags, |
| 3506 | AKEY_EVENT_ACTION_UP, |
| 3507 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
| 3508 | | AKEY_EVENT_FLAG_CANCELED); |
| 3509 | } |
| 3510 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3511 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3512 | if (mLastRawPointerData.touchingIdBits.isEmpty() |
| 3513 | && !mCurrentRawPointerData.touchingIdBits.isEmpty()) { |
| 3514 | // Pointer just went down. Check for virtual key press or off-screen touches. |
| 3515 | uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit(); |
| 3516 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3517 | if (!isPointInsideSurface(pointer.x, pointer.y)) { |
| 3518 | // If exactly one pointer went down, check for virtual key hit. |
| 3519 | // Otherwise we will drop the entire stroke. |
| 3520 | if (mCurrentRawPointerData.touchingIdBits.count() == 1) { |
| 3521 | const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y); |
| 3522 | if (virtualKey) { |
| 3523 | mCurrentVirtualKey.down = true; |
| 3524 | mCurrentVirtualKey.downTime = when; |
| 3525 | mCurrentVirtualKey.keyCode = virtualKey->keyCode; |
| 3526 | mCurrentVirtualKey.scanCode = virtualKey->scanCode; |
| 3527 | mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey( |
| 3528 | when, getDevice(), virtualKey->keyCode, virtualKey->scanCode); |
| 3529 | |
| 3530 | if (!mCurrentVirtualKey.ignored) { |
| 3531 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3532 | ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3533 | mCurrentVirtualKey.keyCode, |
| 3534 | mCurrentVirtualKey.scanCode); |
| 3535 | #endif |
| 3536 | dispatchVirtualKey(when, policyFlags, |
| 3537 | AKEY_EVENT_ACTION_DOWN, |
| 3538 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY); |
| 3539 | } |
| 3540 | } |
| 3541 | } |
| 3542 | return true; |
| 3543 | } |
| 3544 | } |
| 3545 | |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3546 | // 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] | 3547 | // most recent touch within the screen area. The idea is to filter out stray |
| 3548 | // virtual key presses when interacting with the touch screen. |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3549 | // |
| 3550 | // Problems we're trying to solve: |
| 3551 | // |
| 3552 | // 1. While scrolling a list or dragging the window shade, the user swipes down into a |
| 3553 | // virtual key area that is implemented by a separate touch panel and accidentally |
| 3554 | // triggers a virtual key. |
| 3555 | // |
| 3556 | // 2. While typing in the on screen keyboard, the user taps slightly outside the screen |
| 3557 | // area and accidentally triggers a virtual key. This often happens when virtual keys |
| 3558 | // are layed out below the screen near to where the on screen keyboard's space bar |
| 3559 | // is displayed. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3560 | if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 3561 | mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime); |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3562 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3563 | return false; |
| 3564 | } |
| 3565 | |
| 3566 | void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags, |
| 3567 | int32_t keyEventAction, int32_t keyEventFlags) { |
| 3568 | int32_t keyCode = mCurrentVirtualKey.keyCode; |
| 3569 | int32_t scanCode = mCurrentVirtualKey.scanCode; |
| 3570 | nsecs_t downTime = mCurrentVirtualKey.downTime; |
| 3571 | int32_t metaState = mContext->getGlobalMetaState(); |
| 3572 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3573 | |
| 3574 | NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags, |
| 3575 | keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime); |
| 3576 | getListener()->notifyKey(&args); |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3577 | } |
| 3578 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3579 | void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3580 | BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits; |
| 3581 | BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3582 | int32_t metaState = getContext()->getGlobalMetaState(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3583 | int32_t buttonState = mCurrentButtonState; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3584 | |
| 3585 | if (currentIdBits == lastIdBits) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3586 | if (!currentIdBits.isEmpty()) { |
| 3587 | // No pointer id changes so this is a move event. |
| 3588 | // 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] | 3589 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3590 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, |
| 3591 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 3592 | mCurrentCookedPointerData.pointerProperties, |
| 3593 | mCurrentCookedPointerData.pointerCoords, |
| 3594 | mCurrentCookedPointerData.idToIndex, |
| 3595 | currentIdBits, -1, |
| 3596 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 3597 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3598 | } else { |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3599 | // There may be pointers going up and pointers going down and pointers moving |
| 3600 | // all at the same time. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3601 | BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value); |
| 3602 | BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3603 | BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3604 | BitSet32 dispatchedIdBits(lastIdBits.value); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3605 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3606 | // Update last coordinates of pointers that have moved so that we observe the new |
| 3607 | // 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] | 3608 | bool moveNeeded = updateMovedPointers( |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3609 | mCurrentCookedPointerData.pointerProperties, |
| 3610 | mCurrentCookedPointerData.pointerCoords, |
| 3611 | mCurrentCookedPointerData.idToIndex, |
| 3612 | mLastCookedPointerData.pointerProperties, |
| 3613 | mLastCookedPointerData.pointerCoords, |
| 3614 | mLastCookedPointerData.idToIndex, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3615 | moveIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3616 | if (buttonState != mLastButtonState) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3617 | moveNeeded = true; |
| 3618 | } |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3619 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3620 | // Dispatch pointer up events. |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3621 | while (!upIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3622 | uint32_t upId = upIdBits.clearFirstMarkedBit(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3623 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3624 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3625 | AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3626 | mLastCookedPointerData.pointerProperties, |
| 3627 | mLastCookedPointerData.pointerCoords, |
| 3628 | mLastCookedPointerData.idToIndex, |
| 3629 | dispatchedIdBits, upId, |
| 3630 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3631 | dispatchedIdBits.clearBit(upId); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3632 | } |
| 3633 | |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3634 | // Dispatch move events if any of the remaining pointers moved from their old locations. |
| 3635 | // Although applications receive new locations as part of individual pointer up |
| 3636 | // events, they do not generally handle them except when presented in a move event. |
| 3637 | if (moveNeeded) { |
Steve Block | f68633d | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3638 | ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3639 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3640 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3641 | mCurrentCookedPointerData.pointerProperties, |
| 3642 | mCurrentCookedPointerData.pointerCoords, |
| 3643 | mCurrentCookedPointerData.idToIndex, |
| 3644 | dispatchedIdBits, -1, |
| 3645 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3646 | } |
| 3647 | |
| 3648 | // Dispatch pointer down events using the new pointer locations. |
| 3649 | while (!downIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3650 | uint32_t downId = downIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3651 | dispatchedIdBits.markBit(downId); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3652 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3653 | if (dispatchedIdBits.count() == 1) { |
| 3654 | // First pointer is going down. Set down time. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3655 | mDownTime = when; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3656 | } |
| 3657 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3658 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | a611137 | 2011-07-14 21:48:23 -0700 | [diff] [blame] | 3659 | AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3660 | mCurrentCookedPointerData.pointerProperties, |
| 3661 | mCurrentCookedPointerData.pointerCoords, |
| 3662 | mCurrentCookedPointerData.idToIndex, |
| 3663 | dispatchedIdBits, downId, |
| 3664 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3665 | } |
| 3666 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3667 | } |
| 3668 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3669 | void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) { |
| 3670 | if (mSentHoverEnter && |
| 3671 | (mCurrentCookedPointerData.hoveringIdBits.isEmpty() |
| 3672 | || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) { |
| 3673 | int32_t metaState = getContext()->getGlobalMetaState(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3674 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3675 | AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0, |
| 3676 | mLastCookedPointerData.pointerProperties, |
| 3677 | mLastCookedPointerData.pointerCoords, |
| 3678 | mLastCookedPointerData.idToIndex, |
| 3679 | mLastCookedPointerData.hoveringIdBits, -1, |
| 3680 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 3681 | mSentHoverEnter = false; |
| 3682 | } |
| 3683 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3684 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3685 | void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) { |
| 3686 | if (mCurrentCookedPointerData.touchingIdBits.isEmpty() |
| 3687 | && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) { |
| 3688 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 3689 | if (!mSentHoverEnter) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3690 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3691 | AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0, |
| 3692 | mCurrentCookedPointerData.pointerProperties, |
| 3693 | mCurrentCookedPointerData.pointerCoords, |
| 3694 | mCurrentCookedPointerData.idToIndex, |
| 3695 | mCurrentCookedPointerData.hoveringIdBits, -1, |
| 3696 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 3697 | mSentHoverEnter = true; |
| 3698 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3699 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3700 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3701 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0, |
| 3702 | mCurrentCookedPointerData.pointerProperties, |
| 3703 | mCurrentCookedPointerData.pointerCoords, |
| 3704 | mCurrentCookedPointerData.idToIndex, |
| 3705 | mCurrentCookedPointerData.hoveringIdBits, -1, |
| 3706 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 3707 | } |
| 3708 | } |
| 3709 | |
| 3710 | void TouchInputMapper::cookPointerData() { |
| 3711 | uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount; |
| 3712 | |
| 3713 | mCurrentCookedPointerData.clear(); |
| 3714 | mCurrentCookedPointerData.pointerCount = currentPointerCount; |
| 3715 | mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits; |
| 3716 | mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits; |
| 3717 | |
| 3718 | // Walk through the the active pointers and map device coordinates onto |
| 3719 | // surface coordinates and adjust for display orientation. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3720 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3721 | const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3722 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3723 | // Size |
| 3724 | float touchMajor, touchMinor, toolMajor, toolMinor, size; |
| 3725 | switch (mCalibration.sizeCalibration) { |
| 3726 | case Calibration::SIZE_CALIBRATION_GEOMETRIC: |
| 3727 | case Calibration::SIZE_CALIBRATION_DIAMETER: |
| 3728 | case Calibration::SIZE_CALIBRATION_AREA: |
| 3729 | if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) { |
| 3730 | touchMajor = in.touchMajor; |
| 3731 | touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor; |
| 3732 | toolMajor = in.toolMajor; |
| 3733 | toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor; |
| 3734 | size = mRawPointerAxes.touchMinor.valid |
| 3735 | ? avg(in.touchMajor, in.touchMinor) : in.touchMajor; |
| 3736 | } else if (mRawPointerAxes.touchMajor.valid) { |
| 3737 | toolMajor = touchMajor = in.touchMajor; |
| 3738 | toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid |
| 3739 | ? in.touchMinor : in.touchMajor; |
| 3740 | size = mRawPointerAxes.touchMinor.valid |
| 3741 | ? avg(in.touchMajor, in.touchMinor) : in.touchMajor; |
| 3742 | } else if (mRawPointerAxes.toolMajor.valid) { |
| 3743 | touchMajor = toolMajor = in.toolMajor; |
| 3744 | touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid |
| 3745 | ? in.toolMinor : in.toolMajor; |
| 3746 | size = mRawPointerAxes.toolMinor.valid |
| 3747 | ? avg(in.toolMajor, in.toolMinor) : in.toolMajor; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3748 | } else { |
Steve Block | f68633d | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3749 | ALOG_ASSERT(false, "No touch or tool axes. " |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3750 | "Size calibration should have been resolved to NONE."); |
| 3751 | touchMajor = 0; |
| 3752 | touchMinor = 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3753 | toolMajor = 0; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3754 | toolMinor = 0; |
| 3755 | size = 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3756 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3757 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3758 | if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) { |
| 3759 | uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count(); |
| 3760 | if (touchingCount > 1) { |
| 3761 | touchMajor /= touchingCount; |
| 3762 | touchMinor /= touchingCount; |
| 3763 | toolMajor /= touchingCount; |
| 3764 | toolMinor /= touchingCount; |
| 3765 | size /= touchingCount; |
| 3766 | } |
| 3767 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3768 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3769 | if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) { |
| 3770 | touchMajor *= mGeometricScale; |
| 3771 | touchMinor *= mGeometricScale; |
| 3772 | toolMajor *= mGeometricScale; |
| 3773 | toolMinor *= mGeometricScale; |
| 3774 | } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) { |
| 3775 | touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3776 | touchMinor = touchMajor; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3777 | toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0; |
| 3778 | toolMinor = toolMajor; |
| 3779 | } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) { |
| 3780 | touchMinor = touchMajor; |
| 3781 | toolMinor = toolMajor; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3782 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3783 | |
| 3784 | mCalibration.applySizeScaleAndBias(&touchMajor); |
| 3785 | mCalibration.applySizeScaleAndBias(&touchMinor); |
| 3786 | mCalibration.applySizeScaleAndBias(&toolMajor); |
| 3787 | mCalibration.applySizeScaleAndBias(&toolMinor); |
| 3788 | size *= mSizeScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3789 | break; |
| 3790 | default: |
| 3791 | touchMajor = 0; |
| 3792 | touchMinor = 0; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3793 | toolMajor = 0; |
| 3794 | toolMinor = 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3795 | size = 0; |
| 3796 | break; |
| 3797 | } |
| 3798 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3799 | // Pressure |
| 3800 | float pressure; |
| 3801 | switch (mCalibration.pressureCalibration) { |
| 3802 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: |
| 3803 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: |
| 3804 | pressure = in.pressure * mPressureScale; |
| 3805 | break; |
| 3806 | default: |
| 3807 | pressure = in.isHovering ? 0 : 1; |
| 3808 | break; |
| 3809 | } |
| 3810 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3811 | // Tilt and Orientation |
| 3812 | float tilt; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3813 | float orientation; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3814 | if (mHaveTilt) { |
| 3815 | float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale; |
| 3816 | float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale; |
| 3817 | orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle)); |
| 3818 | tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle)); |
| 3819 | } else { |
| 3820 | tilt = 0; |
| 3821 | |
| 3822 | switch (mCalibration.orientationCalibration) { |
| 3823 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: |
| 3824 | orientation = (in.orientation - mOrientationCenter) * mOrientationScale; |
| 3825 | break; |
| 3826 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: { |
| 3827 | int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4); |
| 3828 | int32_t c2 = signExtendNybble(in.orientation & 0x0f); |
| 3829 | if (c1 != 0 || c2 != 0) { |
| 3830 | orientation = atan2f(c1, c2) * 0.5f; |
| 3831 | float confidence = hypotf(c1, c2); |
| 3832 | float scale = 1.0f + confidence / 16.0f; |
| 3833 | touchMajor *= scale; |
| 3834 | touchMinor /= scale; |
| 3835 | toolMajor *= scale; |
| 3836 | toolMinor /= scale; |
| 3837 | } else { |
| 3838 | orientation = 0; |
| 3839 | } |
| 3840 | break; |
| 3841 | } |
| 3842 | default: |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3843 | orientation = 0; |
| 3844 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3845 | } |
| 3846 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3847 | // Distance |
| 3848 | float distance; |
| 3849 | switch (mCalibration.distanceCalibration) { |
| 3850 | case Calibration::DISTANCE_CALIBRATION_SCALED: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3851 | distance = in.distance * mDistanceScale; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3852 | break; |
| 3853 | default: |
| 3854 | distance = 0; |
| 3855 | } |
| 3856 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3857 | // X and Y |
| 3858 | // Adjust coords for surface orientation. |
| 3859 | float x, y; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3860 | switch (mSurfaceOrientation) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3861 | case DISPLAY_ORIENTATION_90: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3862 | x = float(in.y - mRawPointerAxes.y.minValue) * mYScale; |
| 3863 | y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3864 | orientation -= M_PI_2; |
| 3865 | if (orientation < - M_PI_2) { |
| 3866 | orientation += M_PI; |
| 3867 | } |
| 3868 | break; |
| 3869 | case DISPLAY_ORIENTATION_180: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3870 | x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale; |
| 3871 | y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3872 | break; |
| 3873 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3874 | x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale; |
| 3875 | y = float(in.x - mRawPointerAxes.x.minValue) * mXScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3876 | orientation += M_PI_2; |
| 3877 | if (orientation > M_PI_2) { |
| 3878 | orientation -= M_PI; |
| 3879 | } |
| 3880 | break; |
| 3881 | default: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3882 | x = float(in.x - mRawPointerAxes.x.minValue) * mXScale; |
| 3883 | y = float(in.y - mRawPointerAxes.y.minValue) * mYScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3884 | break; |
| 3885 | } |
| 3886 | |
| 3887 | // Write output coords. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3888 | PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3889 | out.clear(); |
| 3890 | out.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 3891 | out.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 3892 | out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure); |
| 3893 | out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size); |
| 3894 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor); |
| 3895 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor); |
| 3896 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor); |
| 3897 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor); |
| 3898 | out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3899 | out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3900 | out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3901 | |
| 3902 | // Write output properties. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3903 | PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i]; |
| 3904 | uint32_t id = in.id; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3905 | properties.clear(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3906 | properties.id = id; |
| 3907 | properties.toolType = in.toolType; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3908 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3909 | // Write id index. |
| 3910 | mCurrentCookedPointerData.idToIndex[id] = i; |
| 3911 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3912 | } |
| 3913 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3914 | void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, |
| 3915 | PointerUsage pointerUsage) { |
| 3916 | if (pointerUsage != mPointerUsage) { |
| 3917 | abortPointerUsage(when, policyFlags); |
| 3918 | mPointerUsage = pointerUsage; |
| 3919 | } |
| 3920 | |
| 3921 | switch (mPointerUsage) { |
| 3922 | case POINTER_USAGE_GESTURES: |
| 3923 | dispatchPointerGestures(when, policyFlags, false /*isTimeout*/); |
| 3924 | break; |
| 3925 | case POINTER_USAGE_STYLUS: |
| 3926 | dispatchPointerStylus(when, policyFlags); |
| 3927 | break; |
| 3928 | case POINTER_USAGE_MOUSE: |
| 3929 | dispatchPointerMouse(when, policyFlags); |
| 3930 | break; |
| 3931 | default: |
| 3932 | break; |
| 3933 | } |
| 3934 | } |
| 3935 | |
| 3936 | void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) { |
| 3937 | switch (mPointerUsage) { |
| 3938 | case POINTER_USAGE_GESTURES: |
| 3939 | abortPointerGestures(when, policyFlags); |
| 3940 | break; |
| 3941 | case POINTER_USAGE_STYLUS: |
| 3942 | abortPointerStylus(when, policyFlags); |
| 3943 | break; |
| 3944 | case POINTER_USAGE_MOUSE: |
| 3945 | abortPointerMouse(when, policyFlags); |
| 3946 | break; |
| 3947 | default: |
| 3948 | break; |
| 3949 | } |
| 3950 | |
| 3951 | mPointerUsage = POINTER_USAGE_NONE; |
| 3952 | } |
| 3953 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3954 | void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, |
| 3955 | bool isTimeout) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3956 | // Update current gesture coordinates. |
| 3957 | bool cancelPreviousGesture, finishPreviousGesture; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3958 | bool sendEvents = preparePointerGestures(when, |
| 3959 | &cancelPreviousGesture, &finishPreviousGesture, isTimeout); |
| 3960 | if (!sendEvents) { |
| 3961 | return; |
| 3962 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 3963 | if (finishPreviousGesture) { |
| 3964 | cancelPreviousGesture = false; |
| 3965 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3966 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 3967 | // Update the pointer presentation and spots. |
| 3968 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 3969 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT); |
| 3970 | if (finishPreviousGesture || cancelPreviousGesture) { |
| 3971 | mPointerController->clearSpots(); |
| 3972 | } |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 3973 | mPointerController->setSpots(mPointerGesture.currentGestureCoords, |
| 3974 | mPointerGesture.currentGestureIdToIndex, |
| 3975 | mPointerGesture.currentGestureIdBits); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 3976 | } else { |
| 3977 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER); |
| 3978 | } |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 3979 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 3980 | // Show or hide the pointer if needed. |
| 3981 | switch (mPointerGesture.currentGestureMode) { |
| 3982 | case PointerGesture::NEUTRAL: |
| 3983 | case PointerGesture::QUIET: |
| 3984 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS |
| 3985 | && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE |
| 3986 | || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) { |
| 3987 | // Remind the user of where the pointer is after finishing a gesture with spots. |
| 3988 | mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 3989 | } |
| 3990 | break; |
| 3991 | case PointerGesture::TAP: |
| 3992 | case PointerGesture::TAP_DRAG: |
| 3993 | case PointerGesture::BUTTON_CLICK_OR_DRAG: |
| 3994 | case PointerGesture::HOVER: |
| 3995 | case PointerGesture::PRESS: |
| 3996 | // Unfade the pointer when the current gesture manipulates the |
| 3997 | // area directly under the pointer. |
| 3998 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 3999 | break; |
| 4000 | case PointerGesture::SWIPE: |
| 4001 | case PointerGesture::FREEFORM: |
| 4002 | // Fade the pointer when the current gesture manipulates a different |
| 4003 | // area and there are spots to guide the user experience. |
| 4004 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 4005 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 4006 | } else { |
| 4007 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 4008 | } |
| 4009 | break; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4010 | } |
| 4011 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4012 | // Send events! |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4013 | int32_t metaState = getContext()->getGlobalMetaState(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4014 | int32_t buttonState = mCurrentButtonState; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4015 | |
| 4016 | // Update last coordinates of pointers that have moved so that we observe the new |
| 4017 | // 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] | 4018 | bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP |
| 4019 | || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG |
| 4020 | || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4021 | || mPointerGesture.currentGestureMode == PointerGesture::PRESS |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4022 | || mPointerGesture.currentGestureMode == PointerGesture::SWIPE |
| 4023 | || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM; |
| 4024 | bool moveNeeded = false; |
| 4025 | if (down && !cancelPreviousGesture && !finishPreviousGesture |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4026 | && !mPointerGesture.lastGestureIdBits.isEmpty() |
| 4027 | && !mPointerGesture.currentGestureIdBits.isEmpty()) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4028 | BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value |
| 4029 | & mPointerGesture.lastGestureIdBits.value); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4030 | moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4031 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4032 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4033 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4034 | movedGestureIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4035 | if (buttonState != mLastButtonState) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4036 | moveNeeded = true; |
| 4037 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4038 | } |
| 4039 | |
| 4040 | // Send motion events for all pointers that went up or were canceled. |
| 4041 | BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits); |
| 4042 | if (!dispatchedGestureIdBits.isEmpty()) { |
| 4043 | if (cancelPreviousGesture) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4044 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4045 | AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState, |
| 4046 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4047 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4048 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4049 | dispatchedGestureIdBits, -1, |
| 4050 | 0, 0, mPointerGesture.downTime); |
| 4051 | |
| 4052 | dispatchedGestureIdBits.clear(); |
| 4053 | } else { |
| 4054 | BitSet32 upGestureIdBits; |
| 4055 | if (finishPreviousGesture) { |
| 4056 | upGestureIdBits = dispatchedGestureIdBits; |
| 4057 | } else { |
| 4058 | upGestureIdBits.value = dispatchedGestureIdBits.value |
| 4059 | & ~mPointerGesture.currentGestureIdBits.value; |
| 4060 | } |
| 4061 | while (!upGestureIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4062 | uint32_t id = upGestureIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4063 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4064 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4065 | AMOTION_EVENT_ACTION_POINTER_UP, 0, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4066 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4067 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4068 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4069 | dispatchedGestureIdBits, id, |
| 4070 | 0, 0, mPointerGesture.downTime); |
| 4071 | |
| 4072 | dispatchedGestureIdBits.clearBit(id); |
| 4073 | } |
| 4074 | } |
| 4075 | } |
| 4076 | |
| 4077 | // Send motion events for all pointers that moved. |
| 4078 | if (moveNeeded) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4079 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4080 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4081 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4082 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 4083 | dispatchedGestureIdBits, -1, |
| 4084 | 0, 0, mPointerGesture.downTime); |
| 4085 | } |
| 4086 | |
| 4087 | // Send motion events for all pointers that went down. |
| 4088 | if (down) { |
| 4089 | BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value |
| 4090 | & ~dispatchedGestureIdBits.value); |
| 4091 | while (!downGestureIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4092 | uint32_t id = downGestureIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4093 | dispatchedGestureIdBits.markBit(id); |
| 4094 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4095 | if (dispatchedGestureIdBits.count() == 1) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4096 | mPointerGesture.downTime = when; |
| 4097 | } |
| 4098 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4099 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | a611137 | 2011-07-14 21:48:23 -0700 | [diff] [blame] | 4100 | AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4101 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4102 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 4103 | dispatchedGestureIdBits, id, |
| 4104 | 0, 0, mPointerGesture.downTime); |
| 4105 | } |
| 4106 | } |
| 4107 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4108 | // Send motion events for hover. |
| 4109 | if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4110 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4111 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 4112 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4113 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4114 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 4115 | mPointerGesture.currentGestureIdBits, -1, |
| 4116 | 0, 0, mPointerGesture.downTime); |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4117 | } else if (dispatchedGestureIdBits.isEmpty() |
| 4118 | && !mPointerGesture.lastGestureIdBits.isEmpty()) { |
| 4119 | // Synthesize a hover move event after all pointers go up to indicate that |
| 4120 | // the pointer is hovering again even if the user is not currently touching |
| 4121 | // the touch pad. This ensures that a view will receive a fresh hover enter |
| 4122 | // event after a tap. |
| 4123 | float x, y; |
| 4124 | mPointerController->getPosition(&x, &y); |
| 4125 | |
| 4126 | PointerProperties pointerProperties; |
| 4127 | pointerProperties.clear(); |
| 4128 | pointerProperties.id = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4129 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4130 | |
| 4131 | PointerCoords pointerCoords; |
| 4132 | pointerCoords.clear(); |
| 4133 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4134 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 4135 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4136 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4137 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 4138 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4139 | 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4140 | getListener()->notifyMotion(&args); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4141 | } |
| 4142 | |
| 4143 | // Update state. |
| 4144 | mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode; |
| 4145 | if (!down) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4146 | mPointerGesture.lastGestureIdBits.clear(); |
| 4147 | } else { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4148 | mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits; |
| 4149 | for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4150 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4151 | uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4152 | mPointerGesture.lastGestureProperties[index].copyFrom( |
| 4153 | mPointerGesture.currentGestureProperties[index]); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4154 | mPointerGesture.lastGestureCoords[index].copyFrom( |
| 4155 | mPointerGesture.currentGestureCoords[index]); |
| 4156 | mPointerGesture.lastGestureIdToIndex[id] = index; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4157 | } |
| 4158 | } |
| 4159 | } |
| 4160 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4161 | void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) { |
| 4162 | // Cancel previously dispatches pointers. |
| 4163 | if (!mPointerGesture.lastGestureIdBits.isEmpty()) { |
| 4164 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 4165 | int32_t buttonState = mCurrentButtonState; |
| 4166 | dispatchMotion(when, policyFlags, mSource, |
| 4167 | AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState, |
| 4168 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4169 | mPointerGesture.lastGestureProperties, |
| 4170 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4171 | mPointerGesture.lastGestureIdBits, -1, |
| 4172 | 0, 0, mPointerGesture.downTime); |
| 4173 | } |
| 4174 | |
| 4175 | // Reset the current pointer gesture. |
| 4176 | mPointerGesture.reset(); |
| 4177 | mPointerVelocityControl.reset(); |
| 4178 | |
| 4179 | // Remove any current spots. |
| 4180 | if (mPointerController != NULL) { |
| 4181 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 4182 | mPointerController->clearSpots(); |
| 4183 | } |
| 4184 | } |
| 4185 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4186 | bool TouchInputMapper::preparePointerGestures(nsecs_t when, |
| 4187 | bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4188 | *outCancelPreviousGesture = false; |
| 4189 | *outFinishPreviousGesture = false; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4190 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4191 | // Handle TAP timeout. |
| 4192 | if (isTimeout) { |
| 4193 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4194 | ALOGD("Gestures: Processing timeout"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4195 | #endif |
| 4196 | |
| 4197 | if (mPointerGesture.lastGestureMode == PointerGesture::TAP) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4198 | if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4199 | // The tap/drag timeout has not yet expired. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4200 | getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4201 | + mConfig.pointerGestureTapDragInterval); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4202 | } else { |
| 4203 | // The tap is finished. |
| 4204 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4205 | ALOGD("Gestures: TAP finished"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4206 | #endif |
| 4207 | *outFinishPreviousGesture = true; |
| 4208 | |
| 4209 | mPointerGesture.activeGestureId = -1; |
| 4210 | mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL; |
| 4211 | mPointerGesture.currentGestureIdBits.clear(); |
| 4212 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4213 | mPointerVelocityControl.reset(); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4214 | return true; |
| 4215 | } |
| 4216 | } |
| 4217 | |
| 4218 | // We did not handle this timeout. |
| 4219 | return false; |
| 4220 | } |
| 4221 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4222 | const uint32_t currentFingerCount = mCurrentFingerIdBits.count(); |
| 4223 | const uint32_t lastFingerCount = mLastFingerIdBits.count(); |
| 4224 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4225 | // Update the velocity tracker. |
| 4226 | { |
| 4227 | VelocityTracker::Position positions[MAX_POINTERS]; |
| 4228 | uint32_t count = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4229 | for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4230 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 4231 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4232 | positions[count].x = pointer.x * mPointerXMovementScale; |
| 4233 | positions[count].y = pointer.y * mPointerYMovementScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4234 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4235 | mPointerGesture.velocityTracker.addMovement(when, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4236 | mCurrentFingerIdBits, positions); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4237 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4238 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4239 | // Pick a new active touch id if needed. |
| 4240 | // Choose an arbitrary pointer that just went down, if there is one. |
| 4241 | // Otherwise choose an arbitrary remaining pointer. |
| 4242 | // 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] | 4243 | // We keep the same active touch id for as long as possible. |
| 4244 | bool activeTouchChanged = false; |
| 4245 | int32_t lastActiveTouchId = mPointerGesture.activeTouchId; |
| 4246 | int32_t activeTouchId = lastActiveTouchId; |
| 4247 | if (activeTouchId < 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4248 | if (!mCurrentFingerIdBits.isEmpty()) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4249 | activeTouchChanged = true; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4250 | activeTouchId = mPointerGesture.activeTouchId = |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4251 | mCurrentFingerIdBits.firstMarkedBit(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4252 | mPointerGesture.firstTouchTime = when; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4253 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4254 | } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4255 | activeTouchChanged = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4256 | if (!mCurrentFingerIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4257 | activeTouchId = mPointerGesture.activeTouchId = |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4258 | mCurrentFingerIdBits.firstMarkedBit(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4259 | } else { |
| 4260 | activeTouchId = mPointerGesture.activeTouchId = -1; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4261 | } |
| 4262 | } |
| 4263 | |
| 4264 | // Determine whether we are in quiet time. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4265 | bool isQuietTime = false; |
| 4266 | if (activeTouchId < 0) { |
| 4267 | mPointerGesture.resetQuietTime(); |
| 4268 | } else { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4269 | isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4270 | if (!isQuietTime) { |
| 4271 | if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS |
| 4272 | || mPointerGesture.lastGestureMode == PointerGesture::SWIPE |
| 4273 | || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4274 | && currentFingerCount < 2) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4275 | // Enter quiet time when exiting swipe or freeform state. |
| 4276 | // This is to prevent accidentally entering the hover state and flinging the |
| 4277 | // pointer when finishing a swipe and there is still one pointer left onscreen. |
| 4278 | isQuietTime = true; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4279 | } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4280 | && currentFingerCount >= 2 |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4281 | && !isPointerDown(mCurrentButtonState)) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4282 | // Enter quiet time when releasing the button and there are still two or more |
| 4283 | // fingers down. This may indicate that one finger was used to press the button |
| 4284 | // but it has not gone up yet. |
| 4285 | isQuietTime = true; |
| 4286 | } |
| 4287 | if (isQuietTime) { |
| 4288 | mPointerGesture.quietTime = when; |
| 4289 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4290 | } |
| 4291 | } |
| 4292 | |
| 4293 | // Switch states based on button and pointer state. |
| 4294 | if (isQuietTime) { |
| 4295 | // Case 1: Quiet time. (QUIET) |
| 4296 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4297 | ALOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4298 | + mConfig.pointerGestureQuietInterval - when) * 0.000001f); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4299 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4300 | if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) { |
| 4301 | *outFinishPreviousGesture = true; |
| 4302 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4303 | |
| 4304 | mPointerGesture.activeGestureId = -1; |
| 4305 | mPointerGesture.currentGestureMode = PointerGesture::QUIET; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4306 | mPointerGesture.currentGestureIdBits.clear(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4307 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4308 | mPointerVelocityControl.reset(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4309 | } else if (isPointerDown(mCurrentButtonState)) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4310 | // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG) |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4311 | // The pointer follows the active touch point. |
| 4312 | // Emit DOWN, MOVE, UP events at the pointer location. |
| 4313 | // |
| 4314 | // Only the active touch matters; other fingers are ignored. This policy helps |
| 4315 | // to handle the case where the user places a second finger on the touch pad |
| 4316 | // to apply the necessary force to depress an integrated button below the surface. |
| 4317 | // We don't want the second finger to be delivered to applications. |
| 4318 | // |
| 4319 | // For this to work well, we need to make sure to track the pointer that is really |
| 4320 | // active. If the user first puts one finger down to click then adds another |
| 4321 | // finger to drag then the active pointer should switch to the finger that is |
| 4322 | // being dragged. |
| 4323 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4324 | ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4325 | "currentFingerCount=%d", activeTouchId, currentFingerCount); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4326 | #endif |
| 4327 | // Reset state when just starting. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4328 | if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4329 | *outFinishPreviousGesture = true; |
| 4330 | mPointerGesture.activeGestureId = 0; |
| 4331 | } |
| 4332 | |
| 4333 | // Switch pointers if needed. |
| 4334 | // Find the fastest pointer and follow it. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4335 | if (activeTouchId >= 0 && currentFingerCount > 1) { |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4336 | int32_t bestId = -1; |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4337 | float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4338 | for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4339 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4340 | float vx, vy; |
| 4341 | if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) { |
| 4342 | float speed = hypotf(vx, vy); |
| 4343 | if (speed > bestSpeed) { |
| 4344 | bestId = id; |
| 4345 | bestSpeed = speed; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4346 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 4347 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4348 | } |
| 4349 | if (bestId >= 0 && bestId != activeTouchId) { |
| 4350 | mPointerGesture.activeTouchId = activeTouchId = bestId; |
| 4351 | activeTouchChanged = true; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4352 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4353 | ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, " |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4354 | "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4355 | #endif |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4356 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4357 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4358 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4359 | if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4360 | const RawPointerData::Pointer& currentPointer = |
| 4361 | mCurrentRawPointerData.pointerForId(activeTouchId); |
| 4362 | const RawPointerData::Pointer& lastPointer = |
| 4363 | mLastRawPointerData.pointerForId(activeTouchId); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4364 | float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale; |
| 4365 | float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4366 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4367 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4368 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4369 | |
| 4370 | // Move the pointer using a relative motion. |
| 4371 | // When using spots, the click will occur at the position of the anchor |
| 4372 | // spot and all other spots will move there. |
| 4373 | mPointerController->move(deltaX, deltaY); |
| 4374 | } else { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4375 | mPointerVelocityControl.reset(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4376 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4377 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4378 | float x, y; |
| 4379 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 4380 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4381 | mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4382 | mPointerGesture.currentGestureIdBits.clear(); |
| 4383 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4384 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4385 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4386 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4387 | mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4388 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4389 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4390 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 4391 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4392 | } else if (currentFingerCount == 0) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4393 | // Case 3. No fingers down and button is not pressed. (NEUTRAL) |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4394 | if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) { |
| 4395 | *outFinishPreviousGesture = true; |
| 4396 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4397 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4398 | // Watch for taps coming out of HOVER or TAP_DRAG mode. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4399 | // Checking for taps after TAP_DRAG allows us to detect double-taps. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4400 | bool tapped = false; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4401 | if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER |
| 4402 | || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4403 | && lastFingerCount == 1) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4404 | if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4405 | float x, y; |
| 4406 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4407 | if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop |
| 4408 | && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4409 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4410 | ALOGD("Gestures: TAP"); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4411 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4412 | |
| 4413 | mPointerGesture.tapUpTime = when; |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4414 | getContext()->requestTimeoutAtTime(when |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4415 | + mConfig.pointerGestureTapDragInterval); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4416 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4417 | mPointerGesture.activeGestureId = 0; |
| 4418 | mPointerGesture.currentGestureMode = PointerGesture::TAP; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4419 | mPointerGesture.currentGestureIdBits.clear(); |
| 4420 | mPointerGesture.currentGestureIdBits.markBit( |
| 4421 | mPointerGesture.activeGestureId); |
| 4422 | mPointerGesture.currentGestureIdToIndex[ |
| 4423 | mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4424 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4425 | mPointerGesture.currentGestureProperties[0].id = |
| 4426 | mPointerGesture.activeGestureId; |
| 4427 | mPointerGesture.currentGestureProperties[0].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4428 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4429 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4430 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4431 | AMOTION_EVENT_AXIS_X, mPointerGesture.tapX); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4432 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4433 | AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4434 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
| 4435 | AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4436 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4437 | tapped = true; |
| 4438 | } else { |
| 4439 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4440 | ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f", |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4441 | x - mPointerGesture.tapX, |
| 4442 | y - mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4443 | #endif |
| 4444 | } |
| 4445 | } else { |
| 4446 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4447 | ALOGD("Gestures: Not a TAP, %0.3fms since down", |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4448 | (when - mPointerGesture.tapDownTime) * 0.000001f); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4449 | #endif |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4450 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4451 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4452 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4453 | mPointerVelocityControl.reset(); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4454 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4455 | if (!tapped) { |
| 4456 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4457 | ALOGD("Gestures: NEUTRAL"); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4458 | #endif |
| 4459 | mPointerGesture.activeGestureId = -1; |
| 4460 | mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4461 | mPointerGesture.currentGestureIdBits.clear(); |
| 4462 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4463 | } else if (currentFingerCount == 1) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4464 | // 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] | 4465 | // The pointer follows the active touch point. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4466 | // When in HOVER, emit HOVER_MOVE events at the pointer location. |
| 4467 | // When in TAP_DRAG, emit MOVE events at the pointer location. |
Steve Block | f68633d | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 4468 | ALOG_ASSERT(activeTouchId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4469 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4470 | mPointerGesture.currentGestureMode = PointerGesture::HOVER; |
| 4471 | if (mPointerGesture.lastGestureMode == PointerGesture::TAP) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4472 | if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4473 | float x, y; |
| 4474 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4475 | if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop |
| 4476 | && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4477 | mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG; |
| 4478 | } else { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4479 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4480 | ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f", |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4481 | x - mPointerGesture.tapX, |
| 4482 | y - mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4483 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4484 | } |
| 4485 | } else { |
| 4486 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4487 | ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up", |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4488 | (when - mPointerGesture.tapUpTime) * 0.000001f); |
| 4489 | #endif |
| 4490 | } |
| 4491 | } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) { |
| 4492 | mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG; |
| 4493 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4494 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4495 | if (mLastFingerIdBits.hasBit(activeTouchId)) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4496 | const RawPointerData::Pointer& currentPointer = |
| 4497 | mCurrentRawPointerData.pointerForId(activeTouchId); |
| 4498 | const RawPointerData::Pointer& lastPointer = |
| 4499 | mLastRawPointerData.pointerForId(activeTouchId); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4500 | float deltaX = (currentPointer.x - lastPointer.x) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4501 | * mPointerXMovementScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4502 | float deltaY = (currentPointer.y - lastPointer.y) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4503 | * mPointerYMovementScale; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4504 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4505 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4506 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4507 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4508 | // Move the pointer using a relative motion. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4509 | // 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] | 4510 | mPointerController->move(deltaX, deltaY); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4511 | } else { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4512 | mPointerVelocityControl.reset(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4513 | } |
| 4514 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4515 | bool down; |
| 4516 | if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) { |
| 4517 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4518 | ALOGD("Gestures: TAP_DRAG"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4519 | #endif |
| 4520 | down = true; |
| 4521 | } else { |
| 4522 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4523 | ALOGD("Gestures: HOVER"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4524 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4525 | if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) { |
| 4526 | *outFinishPreviousGesture = true; |
| 4527 | } |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4528 | mPointerGesture.activeGestureId = 0; |
| 4529 | down = false; |
| 4530 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4531 | |
| 4532 | float x, y; |
| 4533 | mPointerController->getPosition(&x, &y); |
| 4534 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4535 | mPointerGesture.currentGestureIdBits.clear(); |
| 4536 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4537 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4538 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4539 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
| 4540 | mPointerGesture.currentGestureProperties[0].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4541 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4542 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4543 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4544 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4545 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, |
| 4546 | down ? 1.0f : 0.0f); |
| 4547 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4548 | if (lastFingerCount == 0 && currentFingerCount != 0) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4549 | mPointerGesture.resetTap(); |
| 4550 | mPointerGesture.tapDownTime = when; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4551 | mPointerGesture.tapX = x; |
| 4552 | mPointerGesture.tapY = y; |
| 4553 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4554 | } else { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4555 | // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM) |
| 4556 | // We need to provide feedback for each finger that goes down so we cannot wait |
| 4557 | // for the fingers to move before deciding what to do. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4558 | // |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4559 | // The ambiguous case is deciding what to do when there are two fingers down but they |
| 4560 | // have not moved enough to determine whether they are part of a drag or part of a |
| 4561 | // freeform gesture, or just a press or long-press at the pointer location. |
| 4562 | // |
| 4563 | // When there are two fingers we start with the PRESS hypothesis and we generate a |
| 4564 | // down at the pointer location. |
| 4565 | // |
| 4566 | // When the two fingers move enough or when additional fingers are added, we make |
| 4567 | // a decision to transition into SWIPE or FREEFORM mode accordingly. |
Steve Block | f68633d | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 4568 | ALOG_ASSERT(activeTouchId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4569 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4570 | bool settled = when >= mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4571 | + mConfig.pointerGestureMultitouchSettleInterval; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4572 | if (mPointerGesture.lastGestureMode != PointerGesture::PRESS |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4573 | && mPointerGesture.lastGestureMode != PointerGesture::SWIPE |
| 4574 | && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4575 | *outFinishPreviousGesture = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4576 | } else if (!settled && currentFingerCount > lastFingerCount) { |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4577 | // Additional pointers have gone down but not yet settled. |
| 4578 | // Reset the gesture. |
| 4579 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4580 | ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, " |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4581 | "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4582 | + mConfig.pointerGestureMultitouchSettleInterval - when) |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4583 | * 0.000001f); |
| 4584 | #endif |
| 4585 | *outCancelPreviousGesture = true; |
| 4586 | } else { |
| 4587 | // Continue previous gesture. |
| 4588 | mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode; |
| 4589 | } |
| 4590 | |
| 4591 | if (*outFinishPreviousGesture || *outCancelPreviousGesture) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4592 | mPointerGesture.currentGestureMode = PointerGesture::PRESS; |
| 4593 | mPointerGesture.activeGestureId = 0; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4594 | mPointerGesture.referenceIdBits.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4595 | mPointerVelocityControl.reset(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4596 | |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4597 | // 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] | 4598 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4599 | ALOGD("Gestures: Using centroid as reference for MULTITOUCH, " |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4600 | "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4601 | + mConfig.pointerGestureMultitouchSettleInterval - when) |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4602 | * 0.000001f); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4603 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4604 | mCurrentRawPointerData.getCentroidOfTouchingPointers( |
| 4605 | &mPointerGesture.referenceTouchX, |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4606 | &mPointerGesture.referenceTouchY); |
| 4607 | mPointerController->getPosition(&mPointerGesture.referenceGestureX, |
| 4608 | &mPointerGesture.referenceGestureY); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4609 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4610 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4611 | // 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] | 4612 | for (BitSet32 idBits(mCurrentFingerIdBits.value |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4613 | & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) { |
| 4614 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4615 | mPointerGesture.referenceDeltas[id].dx = 0; |
| 4616 | mPointerGesture.referenceDeltas[id].dy = 0; |
| 4617 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4618 | mPointerGesture.referenceIdBits = mCurrentFingerIdBits; |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4619 | |
| 4620 | // Add delta for all fingers and calculate a common movement delta. |
| 4621 | float commonDeltaX = 0, commonDeltaY = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4622 | BitSet32 commonIdBits(mLastFingerIdBits.value |
| 4623 | & mCurrentFingerIdBits.value); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4624 | for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) { |
| 4625 | bool first = (idBits == commonIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4626 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 4627 | const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id); |
| 4628 | const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4629 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
| 4630 | delta.dx += cpd.x - lpd.x; |
| 4631 | delta.dy += cpd.y - lpd.y; |
| 4632 | |
| 4633 | if (first) { |
| 4634 | commonDeltaX = delta.dx; |
| 4635 | commonDeltaY = delta.dy; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4636 | } else { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4637 | commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx); |
| 4638 | commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy); |
| 4639 | } |
| 4640 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4641 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4642 | // Consider transitions from PRESS to SWIPE or MULTITOUCH. |
| 4643 | if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) { |
| 4644 | float dist[MAX_POINTER_ID + 1]; |
| 4645 | int32_t distOverThreshold = 0; |
| 4646 | for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4647 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4648 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4649 | dist[id] = hypotf(delta.dx * mPointerXZoomScale, |
| 4650 | delta.dy * mPointerYZoomScale); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4651 | if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4652 | distOverThreshold += 1; |
| 4653 | } |
| 4654 | } |
| 4655 | |
| 4656 | // Only transition when at least two pointers have moved further than |
| 4657 | // the minimum distance threshold. |
| 4658 | if (distOverThreshold >= 2) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4659 | if (currentFingerCount > 2) { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4660 | // There are more than two pointers, switch to FREEFORM. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4661 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4662 | ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4663 | currentFingerCount); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4664 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4665 | *outCancelPreviousGesture = true; |
| 4666 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 4667 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4668 | // There are exactly two pointers. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4669 | BitSet32 idBits(mCurrentFingerIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4670 | uint32_t id1 = idBits.clearFirstMarkedBit(); |
| 4671 | uint32_t id2 = idBits.firstMarkedBit(); |
| 4672 | const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1); |
| 4673 | const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2); |
| 4674 | float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y); |
| 4675 | if (mutualDistance > mPointerGestureMaxSwipeWidth) { |
| 4676 | // There are two pointers but they are too far apart for a SWIPE, |
| 4677 | // switch to FREEFORM. |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4678 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4679 | ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4680 | mutualDistance, mPointerGestureMaxSwipeWidth); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4681 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4682 | *outCancelPreviousGesture = true; |
| 4683 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 4684 | } else { |
| 4685 | // There are two pointers. Wait for both pointers to start moving |
| 4686 | // before deciding whether this is a SWIPE or FREEFORM gesture. |
| 4687 | float dist1 = dist[id1]; |
| 4688 | float dist2 = dist[id2]; |
| 4689 | if (dist1 >= mConfig.pointerGestureMultitouchMinDistance |
| 4690 | && dist2 >= mConfig.pointerGestureMultitouchMinDistance) { |
| 4691 | // Calculate the dot product of the displacement vectors. |
| 4692 | // When the vectors are oriented in approximately the same direction, |
| 4693 | // the angle betweeen them is near zero and the cosine of the angle |
| 4694 | // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2). |
| 4695 | PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1]; |
| 4696 | PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4697 | float dx1 = delta1.dx * mPointerXZoomScale; |
| 4698 | float dy1 = delta1.dy * mPointerYZoomScale; |
| 4699 | float dx2 = delta2.dx * mPointerXZoomScale; |
| 4700 | float dy2 = delta2.dy * mPointerYZoomScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4701 | float dot = dx1 * dx2 + dy1 * dy2; |
| 4702 | float cosine = dot / (dist1 * dist2); // denominator always > 0 |
| 4703 | if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) { |
| 4704 | // Pointers are moving in the same direction. Switch to SWIPE. |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4705 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4706 | ALOGD("Gestures: PRESS transitioned to SWIPE, " |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4707 | "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, " |
| 4708 | "cosine %0.3f >= %0.3f", |
| 4709 | dist1, mConfig.pointerGestureMultitouchMinDistance, |
| 4710 | dist2, mConfig.pointerGestureMultitouchMinDistance, |
| 4711 | cosine, mConfig.pointerGestureSwipeTransitionAngleCosine); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4712 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4713 | mPointerGesture.currentGestureMode = PointerGesture::SWIPE; |
| 4714 | } else { |
| 4715 | // Pointers are moving in different directions. Switch to FREEFORM. |
| 4716 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4717 | ALOGD("Gestures: PRESS transitioned to FREEFORM, " |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4718 | "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, " |
| 4719 | "cosine %0.3f < %0.3f", |
| 4720 | dist1, mConfig.pointerGestureMultitouchMinDistance, |
| 4721 | dist2, mConfig.pointerGestureMultitouchMinDistance, |
| 4722 | cosine, mConfig.pointerGestureSwipeTransitionAngleCosine); |
| 4723 | #endif |
| 4724 | *outCancelPreviousGesture = true; |
| 4725 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 4726 | } |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4727 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4728 | } |
| 4729 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4730 | } |
| 4731 | } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4732 | // Switch from SWIPE to FREEFORM if additional pointers go down. |
| 4733 | // Cancel previous gesture. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4734 | if (currentFingerCount > 2) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4735 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4736 | ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4737 | currentFingerCount); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4738 | #endif |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4739 | *outCancelPreviousGesture = true; |
| 4740 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4741 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4742 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4743 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4744 | // Move the reference points based on the overall group motion of the fingers |
| 4745 | // except in PRESS mode while waiting for a transition to occur. |
| 4746 | if (mPointerGesture.currentGestureMode != PointerGesture::PRESS |
| 4747 | && (commonDeltaX || commonDeltaY)) { |
| 4748 | for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4749 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4750 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4751 | delta.dx = 0; |
| 4752 | delta.dy = 0; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4753 | } |
| 4754 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4755 | mPointerGesture.referenceTouchX += commonDeltaX; |
| 4756 | mPointerGesture.referenceTouchY += commonDeltaY; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4757 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4758 | commonDeltaX *= mPointerXMovementScale; |
| 4759 | commonDeltaY *= mPointerYMovementScale; |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 4760 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4761 | rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4762 | mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4763 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4764 | mPointerGesture.referenceGestureX += commonDeltaX; |
| 4765 | mPointerGesture.referenceGestureY += commonDeltaY; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4766 | } |
| 4767 | |
| 4768 | // Report gestures. |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 4769 | if (mPointerGesture.currentGestureMode == PointerGesture::PRESS |
| 4770 | || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) { |
| 4771 | // PRESS or SWIPE mode. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4772 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4773 | ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d," |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4774 | "activeGestureId=%d, currentTouchPointerCount=%d", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4775 | activeTouchId, mPointerGesture.activeGestureId, currentFingerCount); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4776 | #endif |
Steve Block | f68633d | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 4777 | ALOG_ASSERT(mPointerGesture.activeGestureId >= 0); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4778 | |
| 4779 | mPointerGesture.currentGestureIdBits.clear(); |
| 4780 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4781 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4782 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4783 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
| 4784 | mPointerGesture.currentGestureProperties[0].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4785 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4786 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4787 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 4788 | mPointerGesture.referenceGestureX); |
| 4789 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, |
| 4790 | mPointerGesture.referenceGestureY); |
| 4791 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4792 | } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) { |
| 4793 | // FREEFORM mode. |
| 4794 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4795 | ALOGD("Gestures: FREEFORM activeTouchId=%d," |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4796 | "activeGestureId=%d, currentTouchPointerCount=%d", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4797 | activeTouchId, mPointerGesture.activeGestureId, currentFingerCount); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4798 | #endif |
Steve Block | f68633d | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 4799 | ALOG_ASSERT(mPointerGesture.activeGestureId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4800 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4801 | mPointerGesture.currentGestureIdBits.clear(); |
| 4802 | |
| 4803 | BitSet32 mappedTouchIdBits; |
| 4804 | BitSet32 usedGestureIdBits; |
| 4805 | if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) { |
| 4806 | // Initially, assign the active gesture id to the active touch point |
| 4807 | // if there is one. No other touch id bits are mapped yet. |
| 4808 | if (!*outCancelPreviousGesture) { |
| 4809 | mappedTouchIdBits.markBit(activeTouchId); |
| 4810 | usedGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4811 | mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] = |
| 4812 | mPointerGesture.activeGestureId; |
| 4813 | } else { |
| 4814 | mPointerGesture.activeGestureId = -1; |
| 4815 | } |
| 4816 | } else { |
| 4817 | // Otherwise, assume we mapped all touches from the previous frame. |
| 4818 | // Reuse all mappings that are still applicable. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4819 | mappedTouchIdBits.value = mLastFingerIdBits.value |
| 4820 | & mCurrentFingerIdBits.value; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4821 | usedGestureIdBits = mPointerGesture.lastGestureIdBits; |
| 4822 | |
| 4823 | // Check whether we need to choose a new active gesture id because the |
| 4824 | // current went went up. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4825 | for (BitSet32 upTouchIdBits(mLastFingerIdBits.value |
| 4826 | & ~mCurrentFingerIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4827 | !upTouchIdBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4828 | uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4829 | uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId]; |
| 4830 | if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) { |
| 4831 | mPointerGesture.activeGestureId = -1; |
| 4832 | break; |
| 4833 | } |
| 4834 | } |
| 4835 | } |
| 4836 | |
| 4837 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4838 | ALOGD("Gestures: FREEFORM follow up " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4839 | "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, " |
| 4840 | "activeGestureId=%d", |
| 4841 | mappedTouchIdBits.value, usedGestureIdBits.value, |
| 4842 | mPointerGesture.activeGestureId); |
| 4843 | #endif |
| 4844 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4845 | BitSet32 idBits(mCurrentFingerIdBits); |
| 4846 | for (uint32_t i = 0; i < currentFingerCount; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4847 | uint32_t touchId = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4848 | uint32_t gestureId; |
| 4849 | if (!mappedTouchIdBits.hasBit(touchId)) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4850 | gestureId = usedGestureIdBits.markFirstUnmarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4851 | mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId; |
| 4852 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4853 | ALOGD("Gestures: FREEFORM " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4854 | "new mapping for touch id %d -> gesture id %d", |
| 4855 | touchId, gestureId); |
| 4856 | #endif |
| 4857 | } else { |
| 4858 | gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId]; |
| 4859 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4860 | ALOGD("Gestures: FREEFORM " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4861 | "existing mapping for touch id %d -> gesture id %d", |
| 4862 | touchId, gestureId); |
| 4863 | #endif |
| 4864 | } |
| 4865 | mPointerGesture.currentGestureIdBits.markBit(gestureId); |
| 4866 | mPointerGesture.currentGestureIdToIndex[gestureId] = i; |
| 4867 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4868 | const RawPointerData::Pointer& pointer = |
| 4869 | mCurrentRawPointerData.pointerForId(touchId); |
| 4870 | float deltaX = (pointer.x - mPointerGesture.referenceTouchX) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4871 | * mPointerXZoomScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4872 | float deltaY = (pointer.y - mPointerGesture.referenceTouchY) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4873 | * mPointerYZoomScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4874 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4875 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4876 | mPointerGesture.currentGestureProperties[i].clear(); |
| 4877 | mPointerGesture.currentGestureProperties[i].id = gestureId; |
| 4878 | mPointerGesture.currentGestureProperties[i].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4879 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4880 | mPointerGesture.currentGestureCoords[i].clear(); |
| 4881 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 4882 | AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4883 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 4884 | AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4885 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
| 4886 | AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
| 4887 | } |
| 4888 | |
| 4889 | if (mPointerGesture.activeGestureId < 0) { |
| 4890 | mPointerGesture.activeGestureId = |
| 4891 | mPointerGesture.currentGestureIdBits.firstMarkedBit(); |
| 4892 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4893 | ALOGD("Gestures: FREEFORM new " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4894 | "activeGestureId=%d", mPointerGesture.activeGestureId); |
| 4895 | #endif |
| 4896 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4897 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4898 | } |
| 4899 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4900 | mPointerController->setButtonState(mCurrentButtonState); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4901 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4902 | #if DEBUG_GESTURES |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4903 | ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, " |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4904 | "currentGestureMode=%d, currentGestureIdBits=0x%08x, " |
| 4905 | "lastGestureMode=%d, lastGestureIdBits=0x%08x", |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4906 | toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture), |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4907 | mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value, |
| 4908 | mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4909 | for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4910 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4911 | uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4912 | const PointerProperties& properties = mPointerGesture.currentGestureProperties[index]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4913 | const PointerCoords& coords = mPointerGesture.currentGestureCoords[index]; |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4914 | ALOGD(" currentGesture[%d]: index=%d, toolType=%d, " |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4915 | "x=%0.3f, y=%0.3f, pressure=%0.3f", |
| 4916 | id, index, properties.toolType, |
| 4917 | coords.getAxisValue(AMOTION_EVENT_AXIS_X), |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4918 | coords.getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4919 | coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); |
| 4920 | } |
| 4921 | for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4922 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4923 | uint32_t index = mPointerGesture.lastGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4924 | const PointerProperties& properties = mPointerGesture.lastGestureProperties[index]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4925 | const PointerCoords& coords = mPointerGesture.lastGestureCoords[index]; |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4926 | ALOGD(" lastGesture[%d]: index=%d, toolType=%d, " |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4927 | "x=%0.3f, y=%0.3f, pressure=%0.3f", |
| 4928 | id, index, properties.toolType, |
| 4929 | coords.getAxisValue(AMOTION_EVENT_AXIS_X), |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4930 | coords.getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4931 | coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); |
| 4932 | } |
| 4933 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4934 | return true; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4935 | } |
| 4936 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4937 | void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) { |
| 4938 | mPointerSimple.currentCoords.clear(); |
| 4939 | mPointerSimple.currentProperties.clear(); |
| 4940 | |
| 4941 | bool down, hovering; |
| 4942 | if (!mCurrentStylusIdBits.isEmpty()) { |
| 4943 | uint32_t id = mCurrentStylusIdBits.firstMarkedBit(); |
| 4944 | uint32_t index = mCurrentCookedPointerData.idToIndex[id]; |
| 4945 | float x = mCurrentCookedPointerData.pointerCoords[index].getX(); |
| 4946 | float y = mCurrentCookedPointerData.pointerCoords[index].getY(); |
| 4947 | mPointerController->setPosition(x, y); |
| 4948 | |
| 4949 | hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id); |
| 4950 | down = !hovering; |
| 4951 | |
| 4952 | mPointerController->getPosition(&x, &y); |
| 4953 | mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]); |
| 4954 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4955 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 4956 | mPointerSimple.currentProperties.id = 0; |
| 4957 | mPointerSimple.currentProperties.toolType = |
| 4958 | mCurrentCookedPointerData.pointerProperties[index].toolType; |
| 4959 | } else { |
| 4960 | down = false; |
| 4961 | hovering = false; |
| 4962 | } |
| 4963 | |
| 4964 | dispatchPointerSimple(when, policyFlags, down, hovering); |
| 4965 | } |
| 4966 | |
| 4967 | void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) { |
| 4968 | abortPointerSimple(when, policyFlags); |
| 4969 | } |
| 4970 | |
| 4971 | void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) { |
| 4972 | mPointerSimple.currentCoords.clear(); |
| 4973 | mPointerSimple.currentProperties.clear(); |
| 4974 | |
| 4975 | bool down, hovering; |
| 4976 | if (!mCurrentMouseIdBits.isEmpty()) { |
| 4977 | uint32_t id = mCurrentMouseIdBits.firstMarkedBit(); |
| 4978 | uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id]; |
| 4979 | if (mLastMouseIdBits.hasBit(id)) { |
| 4980 | uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id]; |
| 4981 | float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x |
| 4982 | - mLastRawPointerData.pointers[lastIndex].x) |
| 4983 | * mPointerXMovementScale; |
| 4984 | float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y |
| 4985 | - mLastRawPointerData.pointers[lastIndex].y) |
| 4986 | * mPointerYMovementScale; |
| 4987 | |
| 4988 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
| 4989 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
| 4990 | |
| 4991 | mPointerController->move(deltaX, deltaY); |
| 4992 | } else { |
| 4993 | mPointerVelocityControl.reset(); |
| 4994 | } |
| 4995 | |
| 4996 | down = isPointerDown(mCurrentButtonState); |
| 4997 | hovering = !down; |
| 4998 | |
| 4999 | float x, y; |
| 5000 | mPointerController->getPosition(&x, &y); |
| 5001 | mPointerSimple.currentCoords.copyFrom( |
| 5002 | mCurrentCookedPointerData.pointerCoords[currentIndex]); |
| 5003 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 5004 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 5005 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, |
| 5006 | hovering ? 0.0f : 1.0f); |
| 5007 | mPointerSimple.currentProperties.id = 0; |
| 5008 | mPointerSimple.currentProperties.toolType = |
| 5009 | mCurrentCookedPointerData.pointerProperties[currentIndex].toolType; |
| 5010 | } else { |
| 5011 | mPointerVelocityControl.reset(); |
| 5012 | |
| 5013 | down = false; |
| 5014 | hovering = false; |
| 5015 | } |
| 5016 | |
| 5017 | dispatchPointerSimple(when, policyFlags, down, hovering); |
| 5018 | } |
| 5019 | |
| 5020 | void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) { |
| 5021 | abortPointerSimple(when, policyFlags); |
| 5022 | |
| 5023 | mPointerVelocityControl.reset(); |
| 5024 | } |
| 5025 | |
| 5026 | void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags, |
| 5027 | bool down, bool hovering) { |
| 5028 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 5029 | |
| 5030 | if (mPointerController != NULL) { |
| 5031 | if (down || hovering) { |
| 5032 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER); |
| 5033 | mPointerController->clearSpots(); |
| 5034 | mPointerController->setButtonState(mCurrentButtonState); |
| 5035 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 5036 | } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) { |
| 5037 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 5038 | } |
| 5039 | } |
| 5040 | |
| 5041 | if (mPointerSimple.down && !down) { |
| 5042 | mPointerSimple.down = false; |
| 5043 | |
| 5044 | // Send up. |
| 5045 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5046 | AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0, |
| 5047 | 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords, |
| 5048 | mOrientedXPrecision, mOrientedYPrecision, |
| 5049 | mPointerSimple.downTime); |
| 5050 | getListener()->notifyMotion(&args); |
| 5051 | } |
| 5052 | |
| 5053 | if (mPointerSimple.hovering && !hovering) { |
| 5054 | mPointerSimple.hovering = false; |
| 5055 | |
| 5056 | // Send hover exit. |
| 5057 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5058 | AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0, |
| 5059 | 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords, |
| 5060 | mOrientedXPrecision, mOrientedYPrecision, |
| 5061 | mPointerSimple.downTime); |
| 5062 | getListener()->notifyMotion(&args); |
| 5063 | } |
| 5064 | |
| 5065 | if (down) { |
| 5066 | if (!mPointerSimple.down) { |
| 5067 | mPointerSimple.down = true; |
| 5068 | mPointerSimple.downTime = when; |
| 5069 | |
| 5070 | // Send down. |
| 5071 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5072 | AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0, |
| 5073 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5074 | mOrientedXPrecision, mOrientedYPrecision, |
| 5075 | mPointerSimple.downTime); |
| 5076 | getListener()->notifyMotion(&args); |
| 5077 | } |
| 5078 | |
| 5079 | // Send move. |
| 5080 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5081 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0, |
| 5082 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5083 | mOrientedXPrecision, mOrientedYPrecision, |
| 5084 | mPointerSimple.downTime); |
| 5085 | getListener()->notifyMotion(&args); |
| 5086 | } |
| 5087 | |
| 5088 | if (hovering) { |
| 5089 | if (!mPointerSimple.hovering) { |
| 5090 | mPointerSimple.hovering = true; |
| 5091 | |
| 5092 | // Send hover enter. |
| 5093 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5094 | AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0, |
| 5095 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5096 | mOrientedXPrecision, mOrientedYPrecision, |
| 5097 | mPointerSimple.downTime); |
| 5098 | getListener()->notifyMotion(&args); |
| 5099 | } |
| 5100 | |
| 5101 | // Send hover move. |
| 5102 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5103 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0, |
| 5104 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5105 | mOrientedXPrecision, mOrientedYPrecision, |
| 5106 | mPointerSimple.downTime); |
| 5107 | getListener()->notifyMotion(&args); |
| 5108 | } |
| 5109 | |
| 5110 | if (mCurrentRawVScroll || mCurrentRawHScroll) { |
| 5111 | float vscroll = mCurrentRawVScroll; |
| 5112 | float hscroll = mCurrentRawHScroll; |
| 5113 | mWheelYVelocityControl.move(when, NULL, &vscroll); |
| 5114 | mWheelXVelocityControl.move(when, &hscroll, NULL); |
| 5115 | |
| 5116 | // Send scroll. |
| 5117 | PointerCoords pointerCoords; |
| 5118 | pointerCoords.copyFrom(mPointerSimple.currentCoords); |
| 5119 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll); |
| 5120 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll); |
| 5121 | |
| 5122 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5123 | AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0, |
| 5124 | 1, &mPointerSimple.currentProperties, &pointerCoords, |
| 5125 | mOrientedXPrecision, mOrientedYPrecision, |
| 5126 | mPointerSimple.downTime); |
| 5127 | getListener()->notifyMotion(&args); |
| 5128 | } |
| 5129 | |
| 5130 | // Save state. |
| 5131 | if (down || hovering) { |
| 5132 | mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords); |
| 5133 | mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties); |
| 5134 | } else { |
| 5135 | mPointerSimple.reset(); |
| 5136 | } |
| 5137 | } |
| 5138 | |
| 5139 | void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) { |
| 5140 | mPointerSimple.currentCoords.clear(); |
| 5141 | mPointerSimple.currentProperties.clear(); |
| 5142 | |
| 5143 | dispatchPointerSimple(when, policyFlags, false, false); |
| 5144 | } |
| 5145 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5146 | void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5147 | int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags, |
| 5148 | const PointerProperties* properties, const PointerCoords* coords, |
| 5149 | const uint32_t* idToIndex, BitSet32 idBits, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5150 | int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) { |
| 5151 | PointerCoords pointerCoords[MAX_POINTERS]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5152 | PointerProperties pointerProperties[MAX_POINTERS]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5153 | uint32_t pointerCount = 0; |
| 5154 | while (!idBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5155 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5156 | uint32_t index = idToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5157 | pointerProperties[pointerCount].copyFrom(properties[index]); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5158 | pointerCoords[pointerCount].copyFrom(coords[index]); |
| 5159 | |
| 5160 | if (changedId >= 0 && id == uint32_t(changedId)) { |
| 5161 | action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
| 5162 | } |
| 5163 | |
| 5164 | pointerCount += 1; |
| 5165 | } |
| 5166 | |
Steve Block | f68633d | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 5167 | ALOG_ASSERT(pointerCount != 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5168 | |
| 5169 | if (changedId >= 0 && pointerCount == 1) { |
| 5170 | // Replace initial down and final up action. |
| 5171 | // We can compare the action without masking off the changed pointer index |
| 5172 | // because we know the index is 0. |
| 5173 | if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 5174 | action = AMOTION_EVENT_ACTION_DOWN; |
| 5175 | } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 5176 | action = AMOTION_EVENT_ACTION_UP; |
| 5177 | } else { |
| 5178 | // Can't happen. |
Steve Block | f68633d | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 5179 | ALOG_ASSERT(false); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5180 | } |
| 5181 | } |
| 5182 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5183 | NotifyMotionArgs args(when, getDeviceId(), source, policyFlags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5184 | action, flags, metaState, buttonState, edgeFlags, |
| 5185 | pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5186 | getListener()->notifyMotion(&args); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5187 | } |
| 5188 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5189 | bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5190 | const PointerCoords* inCoords, const uint32_t* inIdToIndex, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5191 | PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex, |
| 5192 | BitSet32 idBits) const { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5193 | bool changed = false; |
| 5194 | while (!idBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5195 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5196 | uint32_t inIndex = inIdToIndex[id]; |
| 5197 | uint32_t outIndex = outIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5198 | |
| 5199 | const PointerProperties& curInProperties = inProperties[inIndex]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5200 | const PointerCoords& curInCoords = inCoords[inIndex]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5201 | PointerProperties& curOutProperties = outProperties[outIndex]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5202 | PointerCoords& curOutCoords = outCoords[outIndex]; |
| 5203 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5204 | if (curInProperties != curOutProperties) { |
| 5205 | curOutProperties.copyFrom(curInProperties); |
| 5206 | changed = true; |
| 5207 | } |
| 5208 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5209 | if (curInCoords != curOutCoords) { |
| 5210 | curOutCoords.copyFrom(curInCoords); |
| 5211 | changed = true; |
| 5212 | } |
| 5213 | } |
| 5214 | return changed; |
| 5215 | } |
| 5216 | |
| 5217 | void TouchInputMapper::fadePointer() { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5218 | if (mPointerController != NULL) { |
| 5219 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 5220 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5221 | } |
| 5222 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5223 | bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) { |
| 5224 | return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue |
| 5225 | && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5226 | } |
| 5227 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5228 | const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit( |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 5229 | int32_t x, int32_t y) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5230 | size_t numVirtualKeys = mVirtualKeys.size(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 5231 | for (size_t i = 0; i < numVirtualKeys; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5232 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5233 | |
| 5234 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5235 | ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, " |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5236 | "left=%d, top=%d, right=%d, bottom=%d", |
| 5237 | x, y, |
| 5238 | virtualKey.keyCode, virtualKey.scanCode, |
| 5239 | virtualKey.hitLeft, virtualKey.hitTop, |
| 5240 | virtualKey.hitRight, virtualKey.hitBottom); |
| 5241 | #endif |
| 5242 | |
| 5243 | if (virtualKey.isHit(x, y)) { |
| 5244 | return & virtualKey; |
| 5245 | } |
| 5246 | } |
| 5247 | |
| 5248 | return NULL; |
| 5249 | } |
| 5250 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5251 | void TouchInputMapper::assignPointerIds() { |
| 5252 | uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount; |
| 5253 | uint32_t lastPointerCount = mLastRawPointerData.pointerCount; |
| 5254 | |
| 5255 | mCurrentRawPointerData.clearIdBits(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5256 | |
| 5257 | if (currentPointerCount == 0) { |
| 5258 | // No pointers to assign. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5259 | return; |
| 5260 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5261 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5262 | if (lastPointerCount == 0) { |
| 5263 | // All pointers are new. |
| 5264 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
| 5265 | uint32_t id = i; |
| 5266 | mCurrentRawPointerData.pointers[i].id = id; |
| 5267 | mCurrentRawPointerData.idToIndex[id] = i; |
| 5268 | mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i)); |
| 5269 | } |
| 5270 | return; |
| 5271 | } |
| 5272 | |
| 5273 | if (currentPointerCount == 1 && lastPointerCount == 1 |
| 5274 | && mCurrentRawPointerData.pointers[0].toolType |
| 5275 | == mLastRawPointerData.pointers[0].toolType) { |
| 5276 | // Only one pointer and no change in count so it must have the same id as before. |
| 5277 | uint32_t id = mLastRawPointerData.pointers[0].id; |
| 5278 | mCurrentRawPointerData.pointers[0].id = id; |
| 5279 | mCurrentRawPointerData.idToIndex[id] = 0; |
| 5280 | mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0)); |
| 5281 | return; |
| 5282 | } |
| 5283 | |
| 5284 | // General case. |
| 5285 | // We build a heap of squared euclidean distances between current and last pointers |
| 5286 | // associated with the current and last pointer indices. Then, we find the best |
| 5287 | // match (by distance) for each current pointer. |
| 5288 | // The pointers must have the same tool type but it is possible for them to |
| 5289 | // transition from hovering to touching or vice-versa while retaining the same id. |
| 5290 | PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS]; |
| 5291 | |
| 5292 | uint32_t heapSize = 0; |
| 5293 | for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount; |
| 5294 | currentPointerIndex++) { |
| 5295 | for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount; |
| 5296 | lastPointerIndex++) { |
| 5297 | const RawPointerData::Pointer& currentPointer = |
| 5298 | mCurrentRawPointerData.pointers[currentPointerIndex]; |
| 5299 | const RawPointerData::Pointer& lastPointer = |
| 5300 | mLastRawPointerData.pointers[lastPointerIndex]; |
| 5301 | if (currentPointer.toolType == lastPointer.toolType) { |
| 5302 | int64_t deltaX = currentPointer.x - lastPointer.x; |
| 5303 | int64_t deltaY = currentPointer.y - lastPointer.y; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5304 | |
| 5305 | uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY); |
| 5306 | |
| 5307 | // Insert new element into the heap (sift up). |
| 5308 | heap[heapSize].currentPointerIndex = currentPointerIndex; |
| 5309 | heap[heapSize].lastPointerIndex = lastPointerIndex; |
| 5310 | heap[heapSize].distance = distance; |
| 5311 | heapSize += 1; |
| 5312 | } |
| 5313 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5314 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5315 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5316 | // Heapify |
| 5317 | for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) { |
| 5318 | startIndex -= 1; |
| 5319 | for (uint32_t parentIndex = startIndex; ;) { |
| 5320 | uint32_t childIndex = parentIndex * 2 + 1; |
| 5321 | if (childIndex >= heapSize) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5322 | break; |
| 5323 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5324 | |
| 5325 | if (childIndex + 1 < heapSize |
| 5326 | && heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 5327 | childIndex += 1; |
| 5328 | } |
| 5329 | |
| 5330 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 5331 | break; |
| 5332 | } |
| 5333 | |
| 5334 | swap(heap[parentIndex], heap[childIndex]); |
| 5335 | parentIndex = childIndex; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5336 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5337 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5338 | |
| 5339 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5340 | ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5341 | for (size_t i = 0; i < heapSize; i++) { |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5342 | ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5343 | i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, |
| 5344 | heap[i].distance); |
| 5345 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5346 | #endif |
| 5347 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5348 | // Pull matches out by increasing order of distance. |
| 5349 | // To avoid reassigning pointers that have already been matched, the loop keeps track |
| 5350 | // of which last and current pointers have been matched using the matchedXXXBits variables. |
| 5351 | // It also tracks the used pointer id bits. |
| 5352 | BitSet32 matchedLastBits(0); |
| 5353 | BitSet32 matchedCurrentBits(0); |
| 5354 | BitSet32 usedIdBits(0); |
| 5355 | bool first = true; |
| 5356 | for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) { |
| 5357 | while (heapSize > 0) { |
| 5358 | if (first) { |
| 5359 | // The first time through the loop, we just consume the root element of |
| 5360 | // the heap (the one with smallest distance). |
| 5361 | first = false; |
| 5362 | } else { |
| 5363 | // Previous iterations consumed the root element of the heap. |
| 5364 | // Pop root element off of the heap (sift down). |
| 5365 | heap[0] = heap[heapSize]; |
| 5366 | for (uint32_t parentIndex = 0; ;) { |
| 5367 | uint32_t childIndex = parentIndex * 2 + 1; |
| 5368 | if (childIndex >= heapSize) { |
| 5369 | break; |
| 5370 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5371 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5372 | if (childIndex + 1 < heapSize |
| 5373 | && heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 5374 | childIndex += 1; |
| 5375 | } |
| 5376 | |
| 5377 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 5378 | break; |
| 5379 | } |
| 5380 | |
| 5381 | swap(heap[parentIndex], heap[childIndex]); |
| 5382 | parentIndex = childIndex; |
| 5383 | } |
| 5384 | |
| 5385 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5386 | ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5387 | for (size_t i = 0; i < heapSize; i++) { |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5388 | ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5389 | i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, |
| 5390 | heap[i].distance); |
| 5391 | } |
| 5392 | #endif |
| 5393 | } |
| 5394 | |
| 5395 | heapSize -= 1; |
| 5396 | |
| 5397 | uint32_t currentPointerIndex = heap[0].currentPointerIndex; |
| 5398 | if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched |
| 5399 | |
| 5400 | uint32_t lastPointerIndex = heap[0].lastPointerIndex; |
| 5401 | if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched |
| 5402 | |
| 5403 | matchedCurrentBits.markBit(currentPointerIndex); |
| 5404 | matchedLastBits.markBit(lastPointerIndex); |
| 5405 | |
| 5406 | uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id; |
| 5407 | mCurrentRawPointerData.pointers[currentPointerIndex].id = id; |
| 5408 | mCurrentRawPointerData.idToIndex[id] = currentPointerIndex; |
| 5409 | mCurrentRawPointerData.markIdBit(id, |
| 5410 | mCurrentRawPointerData.isHovering(currentPointerIndex)); |
| 5411 | usedIdBits.markBit(id); |
| 5412 | |
| 5413 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5414 | ALOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5415 | lastPointerIndex, currentPointerIndex, id, heap[0].distance); |
| 5416 | #endif |
| 5417 | break; |
| 5418 | } |
| 5419 | } |
| 5420 | |
| 5421 | // Assign fresh ids to pointers that were not matched in the process. |
| 5422 | for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) { |
| 5423 | uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit(); |
| 5424 | uint32_t id = usedIdBits.markFirstUnmarkedBit(); |
| 5425 | |
| 5426 | mCurrentRawPointerData.pointers[currentPointerIndex].id = id; |
| 5427 | mCurrentRawPointerData.idToIndex[id] = currentPointerIndex; |
| 5428 | mCurrentRawPointerData.markIdBit(id, |
| 5429 | mCurrentRawPointerData.isHovering(currentPointerIndex)); |
| 5430 | |
| 5431 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5432 | ALOGD("assignPointerIds - assigned: cur=%d, id=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5433 | currentPointerIndex, id); |
| 5434 | #endif |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5435 | } |
| 5436 | } |
| 5437 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5438 | int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5439 | if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) { |
| 5440 | return AKEY_STATE_VIRTUAL; |
| 5441 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5442 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5443 | size_t numVirtualKeys = mVirtualKeys.size(); |
| 5444 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 5445 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
| 5446 | if (virtualKey.keyCode == keyCode) { |
| 5447 | return AKEY_STATE_UP; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5448 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5449 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5450 | |
| 5451 | return AKEY_STATE_UNKNOWN; |
| 5452 | } |
| 5453 | |
| 5454 | int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5455 | if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) { |
| 5456 | return AKEY_STATE_VIRTUAL; |
| 5457 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5458 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5459 | size_t numVirtualKeys = mVirtualKeys.size(); |
| 5460 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 5461 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
| 5462 | if (virtualKey.scanCode == scanCode) { |
| 5463 | return AKEY_STATE_UP; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5464 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5465 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5466 | |
| 5467 | return AKEY_STATE_UNKNOWN; |
| 5468 | } |
| 5469 | |
| 5470 | bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 5471 | const int32_t* keyCodes, uint8_t* outFlags) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5472 | size_t numVirtualKeys = mVirtualKeys.size(); |
| 5473 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 5474 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5475 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5476 | for (size_t i = 0; i < numCodes; i++) { |
| 5477 | if (virtualKey.keyCode == keyCodes[i]) { |
| 5478 | outFlags[i] = 1; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5479 | } |
| 5480 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5481 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5482 | |
| 5483 | return true; |
| 5484 | } |
| 5485 | |
| 5486 | |
| 5487 | // --- SingleTouchInputMapper --- |
| 5488 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 5489 | SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) : |
| 5490 | TouchInputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5491 | } |
| 5492 | |
| 5493 | SingleTouchInputMapper::~SingleTouchInputMapper() { |
| 5494 | } |
| 5495 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5496 | void SingleTouchInputMapper::reset(nsecs_t when) { |
| 5497 | mSingleTouchMotionAccumulator.reset(getDevice()); |
| 5498 | |
| 5499 | TouchInputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5500 | } |
| 5501 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5502 | void SingleTouchInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5503 | TouchInputMapper::process(rawEvent); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5504 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5505 | mSingleTouchMotionAccumulator.process(rawEvent); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5506 | } |
| 5507 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5508 | void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) { |
Jeff Brown | d87c6d5 | 2011-08-10 14:55:59 -0700 | [diff] [blame] | 5509 | if (mTouchButtonAccumulator.isToolActive()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5510 | mCurrentRawPointerData.pointerCount = 1; |
| 5511 | mCurrentRawPointerData.idToIndex[0] = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5512 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5513 | bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE |
| 5514 | && (mTouchButtonAccumulator.isHovering() |
| 5515 | || (mRawPointerAxes.pressure.valid |
| 5516 | && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0)); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5517 | mCurrentRawPointerData.markIdBit(0, isHovering); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5518 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5519 | RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0]; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5520 | outPointer.id = 0; |
| 5521 | outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX(); |
| 5522 | outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY(); |
| 5523 | outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure(); |
| 5524 | outPointer.touchMajor = 0; |
| 5525 | outPointer.touchMinor = 0; |
| 5526 | outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth(); |
| 5527 | outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth(); |
| 5528 | outPointer.orientation = 0; |
| 5529 | outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5530 | outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX(); |
| 5531 | outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY(); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5532 | outPointer.toolType = mTouchButtonAccumulator.getToolType(); |
| 5533 | if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 5534 | outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 5535 | } |
| 5536 | outPointer.isHovering = isHovering; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5537 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5538 | } |
| 5539 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5540 | void SingleTouchInputMapper::configureRawPointerAxes() { |
| 5541 | TouchInputMapper::configureRawPointerAxes(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5542 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5543 | getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x); |
| 5544 | getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y); |
| 5545 | getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure); |
| 5546 | getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor); |
| 5547 | getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5548 | getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX); |
| 5549 | getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5550 | } |
| 5551 | |
| 5552 | |
| 5553 | // --- MultiTouchInputMapper --- |
| 5554 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 5555 | MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) : |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5556 | TouchInputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5557 | } |
| 5558 | |
| 5559 | MultiTouchInputMapper::~MultiTouchInputMapper() { |
| 5560 | } |
| 5561 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5562 | void MultiTouchInputMapper::reset(nsecs_t when) { |
| 5563 | mMultiTouchMotionAccumulator.reset(getDevice()); |
| 5564 | |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5565 | mPointerIdBits.clear(); |
Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 5566 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5567 | TouchInputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5568 | } |
| 5569 | |
| 5570 | void MultiTouchInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5571 | TouchInputMapper::process(rawEvent); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5572 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5573 | mMultiTouchMotionAccumulator.process(rawEvent); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5574 | } |
| 5575 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5576 | void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5577 | size_t inCount = mMultiTouchMotionAccumulator.getSlotCount(); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5578 | size_t outCount = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5579 | BitSet32 newPointerIdBits; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5580 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5581 | for (size_t inIndex = 0; inIndex < inCount; inIndex++) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5582 | const MultiTouchMotionAccumulator::Slot* inSlot = |
| 5583 | mMultiTouchMotionAccumulator.getSlot(inIndex); |
| 5584 | if (!inSlot->isInUse()) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5585 | continue; |
| 5586 | } |
| 5587 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5588 | if (outCount >= MAX_POINTERS) { |
| 5589 | #if DEBUG_POINTERS |
Steve Block | 1afd5ba | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5590 | ALOGD("MultiTouch device %s emitted more than maximum of %d pointers; " |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5591 | "ignoring the rest.", |
| 5592 | getDeviceName().string(), MAX_POINTERS); |
| 5593 | #endif |
| 5594 | break; // too many fingers! |
| 5595 | } |
| 5596 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5597 | RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount]; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5598 | outPointer.x = inSlot->getX(); |
| 5599 | outPointer.y = inSlot->getY(); |
| 5600 | outPointer.pressure = inSlot->getPressure(); |
| 5601 | outPointer.touchMajor = inSlot->getTouchMajor(); |
| 5602 | outPointer.touchMinor = inSlot->getTouchMinor(); |
| 5603 | outPointer.toolMajor = inSlot->getToolMajor(); |
| 5604 | outPointer.toolMinor = inSlot->getToolMinor(); |
| 5605 | outPointer.orientation = inSlot->getOrientation(); |
| 5606 | outPointer.distance = inSlot->getDistance(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5607 | outPointer.tiltX = 0; |
| 5608 | outPointer.tiltY = 0; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5609 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5610 | outPointer.toolType = inSlot->getToolType(); |
| 5611 | if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 5612 | outPointer.toolType = mTouchButtonAccumulator.getToolType(); |
| 5613 | if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 5614 | outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 5615 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5616 | } |
| 5617 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5618 | bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE |
| 5619 | && (mTouchButtonAccumulator.isHovering() |
| 5620 | || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0)); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5621 | outPointer.isHovering = isHovering; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5622 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5623 | // Assign pointer id using tracking id if available. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5624 | if (*outHavePointerIds) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5625 | int32_t trackingId = inSlot->getTrackingId(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5626 | int32_t id = -1; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5627 | if (trackingId >= 0) { |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5628 | for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5629 | uint32_t n = idBits.clearFirstMarkedBit(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5630 | if (mPointerTrackingIdMap[n] == trackingId) { |
| 5631 | id = n; |
| 5632 | } |
| 5633 | } |
| 5634 | |
| 5635 | if (id < 0 && !mPointerIdBits.isFull()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5636 | id = mPointerIdBits.markFirstUnmarkedBit(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5637 | mPointerTrackingIdMap[id] = trackingId; |
| 5638 | } |
| 5639 | } |
| 5640 | if (id < 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5641 | *outHavePointerIds = false; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5642 | mCurrentRawPointerData.clearIdBits(); |
| 5643 | newPointerIdBits.clear(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5644 | } else { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5645 | outPointer.id = id; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5646 | mCurrentRawPointerData.idToIndex[id] = outCount; |
| 5647 | mCurrentRawPointerData.markIdBit(id, isHovering); |
| 5648 | newPointerIdBits.markBit(id); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5649 | } |
| 5650 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5651 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5652 | outCount += 1; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5653 | } |
| 5654 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5655 | mCurrentRawPointerData.pointerCount = outCount; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5656 | mPointerIdBits = newPointerIdBits; |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5657 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5658 | mMultiTouchMotionAccumulator.finishSync(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5659 | } |
| 5660 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5661 | void MultiTouchInputMapper::configureRawPointerAxes() { |
| 5662 | TouchInputMapper::configureRawPointerAxes(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5663 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5664 | getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x); |
| 5665 | getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y); |
| 5666 | getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor); |
| 5667 | getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor); |
| 5668 | getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor); |
| 5669 | getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor); |
| 5670 | getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation); |
| 5671 | getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure); |
| 5672 | getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance); |
| 5673 | getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId); |
| 5674 | getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5675 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5676 | if (mRawPointerAxes.trackingId.valid |
| 5677 | && mRawPointerAxes.slot.valid |
| 5678 | && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) { |
| 5679 | size_t slotCount = mRawPointerAxes.slot.maxValue + 1; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5680 | if (slotCount > MAX_SLOTS) { |
Steve Block | a51f0e7 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5681 | ALOGW("MultiTouch Device %s reported %d slots but the framework " |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5682 | "only supports a maximum of %d slots at this time.", |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5683 | getDeviceName().string(), slotCount, MAX_SLOTS); |
| 5684 | slotCount = MAX_SLOTS; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5685 | } |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5686 | mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5687 | } else { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5688 | mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5689 | } |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 5690 | } |
| 5691 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5692 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5693 | // --- JoystickInputMapper --- |
| 5694 | |
| 5695 | JoystickInputMapper::JoystickInputMapper(InputDevice* device) : |
| 5696 | InputMapper(device) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5697 | } |
| 5698 | |
| 5699 | JoystickInputMapper::~JoystickInputMapper() { |
| 5700 | } |
| 5701 | |
| 5702 | uint32_t JoystickInputMapper::getSources() { |
| 5703 | return AINPUT_SOURCE_JOYSTICK; |
| 5704 | } |
| 5705 | |
| 5706 | void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 5707 | InputMapper::populateDeviceInfo(info); |
| 5708 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5709 | for (size_t i = 0; i < mAxes.size(); i++) { |
| 5710 | const Axis& axis = mAxes.valueAt(i); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 5711 | info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK, |
| 5712 | axis.min, axis.max, axis.flat, axis.fuzz); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5713 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 5714 | info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK, |
| 5715 | axis.min, axis.max, axis.flat, axis.fuzz); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5716 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5717 | } |
| 5718 | } |
| 5719 | |
| 5720 | void JoystickInputMapper::dump(String8& dump) { |
| 5721 | dump.append(INDENT2 "Joystick Input Mapper:\n"); |
| 5722 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5723 | dump.append(INDENT3 "Axes:\n"); |
| 5724 | size_t numAxes = mAxes.size(); |
| 5725 | for (size_t i = 0; i < numAxes; i++) { |
| 5726 | const Axis& axis = mAxes.valueAt(i); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5727 | const char* label = getAxisLabel(axis.axisInfo.axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5728 | if (label) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5729 | dump.appendFormat(INDENT4 "%s", label); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5730 | } else { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5731 | dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5732 | } |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5733 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 5734 | label = getAxisLabel(axis.axisInfo.highAxis); |
| 5735 | if (label) { |
| 5736 | dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue); |
| 5737 | } else { |
| 5738 | dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis, |
| 5739 | axis.axisInfo.splitValue); |
| 5740 | } |
| 5741 | } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) { |
| 5742 | dump.append(" (invert)"); |
| 5743 | } |
| 5744 | |
| 5745 | dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n", |
| 5746 | axis.min, axis.max, axis.flat, axis.fuzz); |
| 5747 | dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, " |
| 5748 | "highScale=%0.5f, highOffset=%0.5f\n", |
| 5749 | axis.scale, axis.offset, axis.highScale, axis.highOffset); |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 5750 | dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, " |
| 5751 | "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n", |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5752 | mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue, |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 5753 | axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5754 | } |
| 5755 | } |
| 5756 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5757 | void JoystickInputMapper::configure(nsecs_t when, |
| 5758 | const InputReaderConfiguration* config, uint32_t changes) { |
| 5759 | InputMapper::configure(when, config, changes); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5760 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5761 | if (!changes) { // first time only |
| 5762 | // Collect all axes. |
| 5763 | for (int32_t abs = 0; abs <= ABS_MAX; abs++) { |
Jeff Brown | 9ee285a | 2011-08-31 12:56:34 -0700 | [diff] [blame] | 5764 | if (!(getAbsAxisUsage(abs, getDevice()->getClasses()) |
| 5765 | & INPUT_DEVICE_CLASS_JOYSTICK)) { |
| 5766 | continue; // axis must be claimed by a different device |
| 5767 | } |
| 5768 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5769 | RawAbsoluteAxisInfo rawAxisInfo; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5770 | getAbsoluteAxisInfo(abs, &rawAxisInfo); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5771 | if (rawAxisInfo.valid) { |
| 5772 | // Map axis. |
| 5773 | AxisInfo axisInfo; |
| 5774 | bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo); |
| 5775 | if (!explicitlyMapped) { |
| 5776 | // Axis is not explicitly mapped, will choose a generic axis later. |
| 5777 | axisInfo.mode = AxisInfo::MODE_NORMAL; |
| 5778 | axisInfo.axis = -1; |
| 5779 | } |
| 5780 | |
| 5781 | // Apply flat override. |
| 5782 | int32_t rawFlat = axisInfo.flatOverride < 0 |
| 5783 | ? rawAxisInfo.flat : axisInfo.flatOverride; |
| 5784 | |
| 5785 | // Calculate scaling factors and limits. |
| 5786 | Axis axis; |
| 5787 | if (axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 5788 | float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue); |
| 5789 | float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue); |
| 5790 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 5791 | scale, 0.0f, highScale, 0.0f, |
| 5792 | 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
| 5793 | } else if (isCenteredAxis(axisInfo.axis)) { |
| 5794 | float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); |
| 5795 | float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale; |
| 5796 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 5797 | scale, offset, scale, offset, |
| 5798 | -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
| 5799 | } else { |
| 5800 | float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); |
| 5801 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 5802 | scale, 0.0f, scale, 0.0f, |
| 5803 | 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
| 5804 | } |
| 5805 | |
| 5806 | // To eliminate noise while the joystick is at rest, filter out small variations |
| 5807 | // in axis values up front. |
| 5808 | axis.filter = axis.flat * 0.25f; |
| 5809 | |
| 5810 | mAxes.add(abs, axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5811 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5812 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5813 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5814 | // If there are too many axes, start dropping them. |
| 5815 | // Prefer to keep explicitly mapped axes. |
| 5816 | if (mAxes.size() > PointerCoords::MAX_AXES) { |
Steve Block | 933e856 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 5817 | ALOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.", |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5818 | getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES); |
| 5819 | pruneAxes(true); |
| 5820 | pruneAxes(false); |
| 5821 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5822 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5823 | // Assign generic axis ids to remaining axes. |
| 5824 | int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1; |
| 5825 | size_t numAxes = mAxes.size(); |
| 5826 | for (size_t i = 0; i < numAxes; i++) { |
| 5827 | Axis& axis = mAxes.editValueAt(i); |
| 5828 | if (axis.axisInfo.axis < 0) { |
| 5829 | while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16 |
| 5830 | && haveAxis(nextGenericAxisId)) { |
| 5831 | nextGenericAxisId += 1; |
| 5832 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5833 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5834 | if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) { |
| 5835 | axis.axisInfo.axis = nextGenericAxisId; |
| 5836 | nextGenericAxisId += 1; |
| 5837 | } else { |
Steve Block | 933e856 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 5838 | ALOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids " |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5839 | "have already been assigned to other axes.", |
| 5840 | getDeviceName().string(), mAxes.keyAt(i)); |
| 5841 | mAxes.removeItemsAt(i--); |
| 5842 | numAxes -= 1; |
| 5843 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5844 | } |
| 5845 | } |
| 5846 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5847 | } |
| 5848 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5849 | bool JoystickInputMapper::haveAxis(int32_t axisId) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5850 | size_t numAxes = mAxes.size(); |
| 5851 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5852 | const Axis& axis = mAxes.valueAt(i); |
| 5853 | if (axis.axisInfo.axis == axisId |
| 5854 | || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT |
| 5855 | && axis.axisInfo.highAxis == axisId)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5856 | return true; |
| 5857 | } |
| 5858 | } |
| 5859 | return false; |
| 5860 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5861 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5862 | void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) { |
| 5863 | size_t i = mAxes.size(); |
| 5864 | while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) { |
| 5865 | if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) { |
| 5866 | continue; |
| 5867 | } |
Steve Block | 933e856 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 5868 | ALOGI("Discarding joystick '%s' axis %d because there are too many axes.", |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5869 | getDeviceName().string(), mAxes.keyAt(i)); |
| 5870 | mAxes.removeItemsAt(i); |
| 5871 | } |
| 5872 | } |
| 5873 | |
| 5874 | bool JoystickInputMapper::isCenteredAxis(int32_t axis) { |
| 5875 | switch (axis) { |
| 5876 | case AMOTION_EVENT_AXIS_X: |
| 5877 | case AMOTION_EVENT_AXIS_Y: |
| 5878 | case AMOTION_EVENT_AXIS_Z: |
| 5879 | case AMOTION_EVENT_AXIS_RX: |
| 5880 | case AMOTION_EVENT_AXIS_RY: |
| 5881 | case AMOTION_EVENT_AXIS_RZ: |
| 5882 | case AMOTION_EVENT_AXIS_HAT_X: |
| 5883 | case AMOTION_EVENT_AXIS_HAT_Y: |
| 5884 | case AMOTION_EVENT_AXIS_ORIENTATION: |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5885 | case AMOTION_EVENT_AXIS_RUDDER: |
| 5886 | case AMOTION_EVENT_AXIS_WHEEL: |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5887 | return true; |
| 5888 | default: |
| 5889 | return false; |
| 5890 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5891 | } |
| 5892 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5893 | void JoystickInputMapper::reset(nsecs_t when) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5894 | // Recenter all axes. |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5895 | size_t numAxes = mAxes.size(); |
| 5896 | for (size_t i = 0; i < numAxes; i++) { |
| 5897 | Axis& axis = mAxes.editValueAt(i); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5898 | axis.resetValue(); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5899 | } |
| 5900 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5901 | InputMapper::reset(when); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5902 | } |
| 5903 | |
| 5904 | void JoystickInputMapper::process(const RawEvent* rawEvent) { |
| 5905 | switch (rawEvent->type) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5906 | case EV_ABS: { |
| 5907 | ssize_t index = mAxes.indexOfKey(rawEvent->scanCode); |
| 5908 | if (index >= 0) { |
| 5909 | Axis& axis = mAxes.editValueAt(index); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5910 | float newValue, highNewValue; |
| 5911 | switch (axis.axisInfo.mode) { |
| 5912 | case AxisInfo::MODE_INVERT: |
| 5913 | newValue = (axis.rawAxisInfo.maxValue - rawEvent->value) |
| 5914 | * axis.scale + axis.offset; |
| 5915 | highNewValue = 0.0f; |
| 5916 | break; |
| 5917 | case AxisInfo::MODE_SPLIT: |
| 5918 | if (rawEvent->value < axis.axisInfo.splitValue) { |
| 5919 | newValue = (axis.axisInfo.splitValue - rawEvent->value) |
| 5920 | * axis.scale + axis.offset; |
| 5921 | highNewValue = 0.0f; |
| 5922 | } else if (rawEvent->value > axis.axisInfo.splitValue) { |
| 5923 | newValue = 0.0f; |
| 5924 | highNewValue = (rawEvent->value - axis.axisInfo.splitValue) |
| 5925 | * axis.highScale + axis.highOffset; |
| 5926 | } else { |
| 5927 | newValue = 0.0f; |
| 5928 | highNewValue = 0.0f; |
| 5929 | } |
| 5930 | break; |
| 5931 | default: |
| 5932 | newValue = rawEvent->value * axis.scale + axis.offset; |
| 5933 | highNewValue = 0.0f; |
| 5934 | break; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5935 | } |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5936 | axis.newValue = newValue; |
| 5937 | axis.highNewValue = highNewValue; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5938 | } |
| 5939 | break; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5940 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5941 | |
| 5942 | case EV_SYN: |
| 5943 | switch (rawEvent->scanCode) { |
| 5944 | case SYN_REPORT: |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5945 | sync(rawEvent->when, false /*force*/); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5946 | break; |
| 5947 | } |
| 5948 | break; |
| 5949 | } |
| 5950 | } |
| 5951 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5952 | void JoystickInputMapper::sync(nsecs_t when, bool force) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5953 | if (!filterAxes(force)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5954 | return; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5955 | } |
| 5956 | |
| 5957 | int32_t metaState = mContext->getGlobalMetaState(); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5958 | int32_t buttonState = 0; |
| 5959 | |
| 5960 | PointerProperties pointerProperties; |
| 5961 | pointerProperties.clear(); |
| 5962 | pointerProperties.id = 0; |
| 5963 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5964 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5965 | PointerCoords pointerCoords; |
| 5966 | pointerCoords.clear(); |
| 5967 | |
| 5968 | size_t numAxes = mAxes.size(); |
| 5969 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5970 | const Axis& axis = mAxes.valueAt(i); |
| 5971 | pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue); |
| 5972 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 5973 | pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue); |
| 5974 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5975 | } |
| 5976 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 5977 | // Moving a joystick axis should not wake the devide because joysticks can |
| 5978 | // be fairly noisy even when not in use. On the other hand, pushing a gamepad |
| 5979 | // button will likely wake the device. |
| 5980 | // TODO: Use the input device configuration to control this behavior more finely. |
| 5981 | uint32_t policyFlags = 0; |
| 5982 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5983 | NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5984 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 5985 | 1, &pointerProperties, &pointerCoords, 0, 0, 0); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5986 | getListener()->notifyMotion(&args); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5987 | } |
| 5988 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5989 | bool JoystickInputMapper::filterAxes(bool force) { |
| 5990 | bool atLeastOneSignificantChange = force; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5991 | size_t numAxes = mAxes.size(); |
| 5992 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5993 | Axis& axis = mAxes.editValueAt(i); |
| 5994 | if (force || hasValueChangedSignificantly(axis.filter, |
| 5995 | axis.newValue, axis.currentValue, axis.min, axis.max)) { |
| 5996 | axis.currentValue = axis.newValue; |
| 5997 | atLeastOneSignificantChange = true; |
| 5998 | } |
| 5999 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 6000 | if (force || hasValueChangedSignificantly(axis.filter, |
| 6001 | axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) { |
| 6002 | axis.highCurrentValue = axis.highNewValue; |
| 6003 | atLeastOneSignificantChange = true; |
| 6004 | } |
| 6005 | } |
| 6006 | } |
| 6007 | return atLeastOneSignificantChange; |
| 6008 | } |
| 6009 | |
| 6010 | bool JoystickInputMapper::hasValueChangedSignificantly( |
| 6011 | float filter, float newValue, float currentValue, float min, float max) { |
| 6012 | if (newValue != currentValue) { |
| 6013 | // Filter out small changes in value unless the value is converging on the axis |
| 6014 | // bounds or center point. This is intended to reduce the amount of information |
| 6015 | // sent to applications by particularly noisy joysticks (such as PS3). |
| 6016 | if (fabs(newValue - currentValue) > filter |
| 6017 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min) |
| 6018 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max) |
| 6019 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) { |
| 6020 | return true; |
| 6021 | } |
| 6022 | } |
| 6023 | return false; |
| 6024 | } |
| 6025 | |
| 6026 | bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange( |
| 6027 | float filter, float newValue, float currentValue, float thresholdValue) { |
| 6028 | float newDistance = fabs(newValue - thresholdValue); |
| 6029 | if (newDistance < filter) { |
| 6030 | float oldDistance = fabs(currentValue - thresholdValue); |
| 6031 | if (newDistance < oldDistance) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6032 | return true; |
| 6033 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6034 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6035 | return false; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6036 | } |
| 6037 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 6038 | } // namespace android |