Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 17 | #define LOG_TAG "InputReader" |
| 18 | |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
| 21 | // Log debug messages for each raw event received from the EventHub. |
| 22 | #define DEBUG_RAW_EVENTS 0 |
| 23 | |
| 24 | // Log debug messages about touch screen filtering hacks. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 25 | #define DEBUG_HACKS 0 |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 26 | |
| 27 | // Log debug messages about virtual key processing. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 28 | #define DEBUG_VIRTUAL_KEYS 0 |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 29 | |
| 30 | // Log debug messages about pointers. |
Jeff Brown | 9f2106f | 2011-05-24 14:40:35 -0700 | [diff] [blame] | 31 | #define DEBUG_POINTERS 0 |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 32 | |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 33 | // Log debug messages about pointer assignment calculations. |
| 34 | #define DEBUG_POINTER_ASSIGNMENT 0 |
| 35 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 36 | // Log debug messages about gesture detection. |
| 37 | #define DEBUG_GESTURES 0 |
| 38 | |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 39 | // Log debug messages about the vibrator. |
| 40 | #define DEBUG_VIBRATOR 0 |
| 41 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 42 | #include "InputReader.h" |
| 43 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 44 | #include <cutils/log.h> |
Mathias Agopian | b93a03f8 | 2012-02-17 15:34:57 -0800 | [diff] [blame] | 45 | #include <androidfw/Keyboard.h> |
| 46 | #include <androidfw/VirtualKeyMap.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 47 | |
| 48 | #include <stddef.h> |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 49 | #include <stdlib.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 50 | #include <unistd.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 51 | #include <errno.h> |
| 52 | #include <limits.h> |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 53 | #include <math.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 54 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 55 | #define INDENT " " |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 56 | #define INDENT2 " " |
| 57 | #define INDENT3 " " |
| 58 | #define INDENT4 " " |
Jeff Brown | aba321a | 2011-06-28 20:34:40 -0700 | [diff] [blame] | 59 | #define INDENT5 " " |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 60 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 61 | namespace android { |
| 62 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 63 | // --- Constants --- |
| 64 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 65 | // Maximum number of slots supported when using the slot-based Multitouch Protocol B. |
| 66 | static const size_t MAX_SLOTS = 32; |
| 67 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 68 | // --- Static Functions --- |
| 69 | |
| 70 | template<typename T> |
| 71 | inline static T abs(const T& value) { |
| 72 | return value < 0 ? - value : value; |
| 73 | } |
| 74 | |
| 75 | template<typename T> |
| 76 | inline static T min(const T& a, const T& b) { |
| 77 | return a < b ? a : b; |
| 78 | } |
| 79 | |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 80 | template<typename T> |
| 81 | inline static void swap(T& a, T& b) { |
| 82 | T temp = a; |
| 83 | a = b; |
| 84 | b = temp; |
| 85 | } |
| 86 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 87 | inline static float avg(float x, float y) { |
| 88 | return (x + y) / 2; |
| 89 | } |
| 90 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 91 | inline static float distance(float x1, float y1, float x2, float y2) { |
| 92 | return hypotf(x1 - x2, y1 - y2); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 95 | inline static int32_t signExtendNybble(int32_t value) { |
| 96 | return value >= 8 ? value - 16 : value; |
| 97 | } |
| 98 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 99 | static inline const char* toString(bool value) { |
| 100 | return value ? "true" : "false"; |
| 101 | } |
| 102 | |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 103 | static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation, |
| 104 | const int32_t map[][4], size_t mapSize) { |
| 105 | if (orientation != DISPLAY_ORIENTATION_0) { |
| 106 | for (size_t i = 0; i < mapSize; i++) { |
| 107 | if (value == map[i][0]) { |
| 108 | return map[i][orientation]; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | return value; |
| 113 | } |
| 114 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 115 | static const int32_t keyCodeRotationMap[][4] = { |
| 116 | // key codes enumerated counter-clockwise with the original (unrotated) key first |
| 117 | // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 118 | { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT }, |
| 119 | { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN }, |
| 120 | { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT }, |
| 121 | { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP }, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 122 | }; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 123 | static const size_t keyCodeRotationMapSize = |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 124 | sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]); |
| 125 | |
Jeff Brown | 6069139 | 2011-07-15 19:08:26 -0700 | [diff] [blame] | 126 | static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) { |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 127 | return rotateValueUsingRotationMap(keyCode, orientation, |
| 128 | keyCodeRotationMap, keyCodeRotationMapSize); |
| 129 | } |
| 130 | |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 131 | static void rotateDelta(int32_t orientation, float* deltaX, float* deltaY) { |
| 132 | float temp; |
| 133 | switch (orientation) { |
| 134 | case DISPLAY_ORIENTATION_90: |
| 135 | temp = *deltaX; |
| 136 | *deltaX = *deltaY; |
| 137 | *deltaY = -temp; |
| 138 | break; |
| 139 | |
| 140 | case DISPLAY_ORIENTATION_180: |
| 141 | *deltaX = -*deltaX; |
| 142 | *deltaY = -*deltaY; |
| 143 | break; |
| 144 | |
| 145 | case DISPLAY_ORIENTATION_270: |
| 146 | temp = *deltaX; |
| 147 | *deltaX = -*deltaY; |
| 148 | *deltaY = temp; |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 153 | static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) { |
| 154 | return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0; |
| 155 | } |
| 156 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 157 | // 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] | 158 | // button states. This determines whether the event is reported as a touch event. |
| 159 | static bool isPointerDown(int32_t buttonState) { |
| 160 | return buttonState & |
| 161 | (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY |
Jeff Brown | 53ca3f1 | 2011-06-27 18:36:00 -0700 | [diff] [blame] | 162 | | AMOTION_EVENT_BUTTON_TERTIARY); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 165 | static float calculateCommonVector(float a, float b) { |
| 166 | if (a > 0 && b > 0) { |
| 167 | return a < b ? a : b; |
| 168 | } else if (a < 0 && b < 0) { |
| 169 | return a > b ? a : b; |
| 170 | } else { |
| 171 | return 0; |
| 172 | } |
| 173 | } |
| 174 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 175 | static void synthesizeButtonKey(InputReaderContext* context, int32_t action, |
| 176 | nsecs_t when, int32_t deviceId, uint32_t source, |
| 177 | uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState, |
| 178 | int32_t buttonState, int32_t keyCode) { |
| 179 | if ( |
| 180 | (action == AKEY_EVENT_ACTION_DOWN |
| 181 | && !(lastButtonState & buttonState) |
| 182 | && (currentButtonState & buttonState)) |
| 183 | || (action == AKEY_EVENT_ACTION_UP |
| 184 | && (lastButtonState & buttonState) |
| 185 | && !(currentButtonState & buttonState))) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 186 | NotifyKeyArgs args(when, deviceId, source, policyFlags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 187 | action, 0, keyCode, 0, context->getGlobalMetaState(), when); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 188 | context->getListener()->notifyKey(&args); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | static void synthesizeButtonKeys(InputReaderContext* context, int32_t action, |
| 193 | nsecs_t when, int32_t deviceId, uint32_t source, |
| 194 | uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState) { |
| 195 | synthesizeButtonKey(context, action, when, deviceId, source, policyFlags, |
| 196 | lastButtonState, currentButtonState, |
| 197 | AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK); |
| 198 | synthesizeButtonKey(context, action, when, deviceId, source, policyFlags, |
| 199 | lastButtonState, currentButtonState, |
| 200 | AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD); |
| 201 | } |
| 202 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 203 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 204 | // --- InputReaderConfiguration --- |
| 205 | |
| 206 | bool InputReaderConfiguration::getDisplayInfo(int32_t displayId, bool external, |
| 207 | int32_t* width, int32_t* height, int32_t* orientation) const { |
| 208 | if (displayId == 0) { |
| 209 | const DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay; |
| 210 | if (info.width > 0 && info.height > 0) { |
| 211 | if (width) { |
| 212 | *width = info.width; |
| 213 | } |
| 214 | if (height) { |
| 215 | *height = info.height; |
| 216 | } |
| 217 | if (orientation) { |
| 218 | *orientation = info.orientation; |
| 219 | } |
| 220 | return true; |
| 221 | } |
| 222 | } |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | void InputReaderConfiguration::setDisplayInfo(int32_t displayId, bool external, |
| 227 | int32_t width, int32_t height, int32_t orientation) { |
| 228 | if (displayId == 0) { |
| 229 | DisplayInfo& info = external ? mExternalDisplay : mInternalDisplay; |
| 230 | info.width = width; |
| 231 | info.height = height; |
| 232 | info.orientation = orientation; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 237 | // --- InputReader --- |
| 238 | |
| 239 | InputReader::InputReader(const sp<EventHubInterface>& eventHub, |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 240 | const sp<InputReaderPolicyInterface>& policy, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 241 | const sp<InputListenerInterface>& listener) : |
| 242 | mContext(this), mEventHub(eventHub), mPolicy(policy), |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 243 | mGlobalMetaState(0), mGeneration(1), |
| 244 | mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX), |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 245 | mConfigurationChangesToRefresh(0) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 246 | mQueuedListener = new QueuedInputListener(listener); |
| 247 | |
| 248 | { // acquire lock |
| 249 | AutoMutex _l(mLock); |
| 250 | |
| 251 | refreshConfigurationLocked(0); |
| 252 | updateGlobalMetaStateLocked(); |
| 253 | updateInputConfigurationLocked(); |
| 254 | } // release lock |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | InputReader::~InputReader() { |
| 258 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 259 | delete mDevices.valueAt(i); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | void InputReader::loopOnce() { |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 264 | int32_t oldGeneration; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 265 | int32_t timeoutMillis; |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 266 | bool inputDevicesChanged = false; |
| 267 | Vector<InputDeviceInfo> inputDevices; |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 268 | { // acquire lock |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 269 | AutoMutex _l(mLock); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 270 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 271 | oldGeneration = mGeneration; |
| 272 | timeoutMillis = -1; |
| 273 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 274 | uint32_t changes = mConfigurationChangesToRefresh; |
| 275 | if (changes) { |
| 276 | mConfigurationChangesToRefresh = 0; |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 277 | timeoutMillis = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 278 | refreshConfigurationLocked(changes); |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 279 | } else if (mNextTimeout != LLONG_MAX) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 280 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 281 | timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout); |
| 282 | } |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 283 | } // release lock |
| 284 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 285 | size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 286 | |
| 287 | { // acquire lock |
| 288 | AutoMutex _l(mLock); |
Jeff Brown | 112b5f5 | 2012-01-27 17:32:06 -0800 | [diff] [blame] | 289 | mReaderIsAliveCondition.broadcast(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 290 | |
| 291 | if (count) { |
| 292 | processEventsLocked(mEventBuffer, count); |
| 293 | } |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 294 | |
| 295 | if (mNextTimeout != LLONG_MAX) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 296 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
Jeff Brown | 112b5f5 | 2012-01-27 17:32:06 -0800 | [diff] [blame] | 297 | if (now >= mNextTimeout) { |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 298 | #if DEBUG_RAW_EVENTS |
Jeff Brown | 112b5f5 | 2012-01-27 17:32:06 -0800 | [diff] [blame] | 299 | ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 300 | #endif |
Jeff Brown | 112b5f5 | 2012-01-27 17:32:06 -0800 | [diff] [blame] | 301 | mNextTimeout = LLONG_MAX; |
| 302 | timeoutExpiredLocked(now); |
| 303 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 304 | } |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 305 | |
| 306 | if (oldGeneration != mGeneration) { |
| 307 | inputDevicesChanged = true; |
| 308 | getInputDevicesLocked(inputDevices); |
| 309 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 310 | } // release lock |
| 311 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 312 | // Send out a message that the describes the changed input devices. |
| 313 | if (inputDevicesChanged) { |
| 314 | mPolicy->notifyInputDevicesChanged(inputDevices); |
| 315 | } |
| 316 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 317 | // Flush queued events out to the listener. |
| 318 | // This must happen outside of the lock because the listener could potentially call |
| 319 | // back into the InputReader's methods, such as getScanCodeState, or become blocked |
| 320 | // on another thread similarly waiting to acquire the InputReader lock thereby |
| 321 | // resulting in a deadlock. This situation is actually quite plausible because the |
| 322 | // listener is actually the input dispatcher, which calls into the window manager, |
| 323 | // which occasionally calls into the input reader. |
| 324 | mQueuedListener->flush(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 327 | void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) { |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 328 | for (const RawEvent* rawEvent = rawEvents; count;) { |
| 329 | int32_t type = rawEvent->type; |
| 330 | size_t batchSize = 1; |
| 331 | if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) { |
| 332 | int32_t deviceId = rawEvent->deviceId; |
| 333 | while (batchSize < count) { |
| 334 | if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT |
| 335 | || rawEvent[batchSize].deviceId != deviceId) { |
| 336 | break; |
| 337 | } |
| 338 | batchSize += 1; |
| 339 | } |
| 340 | #if DEBUG_RAW_EVENTS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 341 | ALOGD("BatchSize: %d Count: %d", batchSize, count); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 342 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 343 | processEventsForDeviceLocked(deviceId, rawEvent, batchSize); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 344 | } else { |
| 345 | switch (rawEvent->type) { |
| 346 | case EventHubInterface::DEVICE_ADDED: |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 347 | addDeviceLocked(rawEvent->when, rawEvent->deviceId); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 348 | break; |
| 349 | case EventHubInterface::DEVICE_REMOVED: |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 350 | removeDeviceLocked(rawEvent->when, rawEvent->deviceId); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 351 | break; |
| 352 | case EventHubInterface::FINISHED_DEVICE_SCAN: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 353 | handleConfigurationChangedLocked(rawEvent->when); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 354 | break; |
| 355 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 356 | ALOG_ASSERT(false); // can't happen |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 357 | break; |
| 358 | } |
| 359 | } |
| 360 | count -= batchSize; |
| 361 | rawEvent += batchSize; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 365 | void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) { |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 366 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 367 | if (deviceIndex >= 0) { |
| 368 | ALOGW("Ignoring spurious device added event for deviceId %d.", deviceId); |
| 369 | return; |
| 370 | } |
| 371 | |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 372 | InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(deviceId); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 373 | uint32_t classes = mEventHub->getDeviceClasses(deviceId); |
| 374 | |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 375 | InputDevice* device = createDeviceLocked(deviceId, identifier, classes); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 376 | device->configure(when, &mConfig, 0); |
| 377 | device->reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 378 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 379 | if (device->isIgnored()) { |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 380 | ALOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, |
| 381 | identifier.name.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 382 | } else { |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 383 | ALOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, |
| 384 | identifier.name.string(), device->getSources()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 387 | mDevices.add(deviceId, device); |
| 388 | bumpGenerationLocked(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 391 | void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 392 | InputDevice* device = NULL; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 393 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 394 | if (deviceIndex < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 395 | ALOGW("Ignoring spurious device removed event for deviceId %d.", deviceId); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 396 | return; |
| 397 | } |
| 398 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 399 | device = mDevices.valueAt(deviceIndex); |
| 400 | mDevices.removeItemsAt(deviceIndex, 1); |
| 401 | bumpGenerationLocked(); |
| 402 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 403 | if (device->isIgnored()) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 404 | ALOGI("Device removed: id=%d, name='%s' (ignored non-input device)", |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 405 | device->getId(), device->getName().string()); |
| 406 | } else { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 407 | ALOGI("Device removed: id=%d, name='%s', sources=0x%08x", |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 408 | device->getId(), device->getName().string(), device->getSources()); |
| 409 | } |
| 410 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 411 | device->reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 412 | delete device; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 415 | InputDevice* InputReader::createDeviceLocked(int32_t deviceId, |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 416 | const InputDeviceIdentifier& identifier, uint32_t classes) { |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 417 | InputDevice* device = new InputDevice(&mContext, deviceId, bumpGenerationLocked(), |
| 418 | identifier, classes); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 419 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 420 | // External devices. |
| 421 | if (classes & INPUT_DEVICE_CLASS_EXTERNAL) { |
| 422 | device->setExternal(true); |
| 423 | } |
| 424 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 425 | // Switch-like devices. |
| 426 | if (classes & INPUT_DEVICE_CLASS_SWITCH) { |
| 427 | device->addMapper(new SwitchInputMapper(device)); |
| 428 | } |
| 429 | |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 430 | // Vibrator-like devices. |
| 431 | if (classes & INPUT_DEVICE_CLASS_VIBRATOR) { |
| 432 | device->addMapper(new VibratorInputMapper(device)); |
| 433 | } |
| 434 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 435 | // Keyboard-like devices. |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 436 | uint32_t keyboardSource = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 437 | int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC; |
| 438 | if (classes & INPUT_DEVICE_CLASS_KEYBOARD) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 439 | keyboardSource |= AINPUT_SOURCE_KEYBOARD; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 440 | } |
| 441 | if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) { |
| 442 | keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC; |
| 443 | } |
| 444 | if (classes & INPUT_DEVICE_CLASS_DPAD) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 445 | keyboardSource |= AINPUT_SOURCE_DPAD; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 446 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 447 | if (classes & INPUT_DEVICE_CLASS_GAMEPAD) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 448 | keyboardSource |= AINPUT_SOURCE_GAMEPAD; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 449 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 450 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 451 | if (keyboardSource != 0) { |
| 452 | device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 455 | // Cursor-like devices. |
| 456 | if (classes & INPUT_DEVICE_CLASS_CURSOR) { |
| 457 | device->addMapper(new CursorInputMapper(device)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 460 | // Touchscreens and touchpad devices. |
| 461 | if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 462 | device->addMapper(new MultiTouchInputMapper(device)); |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 463 | } else if (classes & INPUT_DEVICE_CLASS_TOUCH) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 464 | device->addMapper(new SingleTouchInputMapper(device)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 467 | // Joystick-like devices. |
| 468 | if (classes & INPUT_DEVICE_CLASS_JOYSTICK) { |
| 469 | device->addMapper(new JoystickInputMapper(device)); |
| 470 | } |
| 471 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 472 | return device; |
| 473 | } |
| 474 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 475 | void InputReader::processEventsForDeviceLocked(int32_t deviceId, |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 476 | const RawEvent* rawEvents, size_t count) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 477 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 478 | if (deviceIndex < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 479 | ALOGW("Discarding event for unknown deviceId %d.", deviceId); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 480 | return; |
| 481 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 482 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 483 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 484 | if (device->isIgnored()) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 485 | //ALOGD("Discarding event for ignored deviceId %d.", deviceId); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 486 | return; |
| 487 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 488 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 489 | device->process(rawEvents, count); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 492 | void InputReader::timeoutExpiredLocked(nsecs_t when) { |
| 493 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 494 | InputDevice* device = mDevices.valueAt(i); |
| 495 | if (!device->isIgnored()) { |
| 496 | device->timeoutExpired(when); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 497 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 498 | } |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 501 | void InputReader::handleConfigurationChangedLocked(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 502 | // 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] | 503 | updateGlobalMetaStateLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 504 | |
| 505 | // Update input configuration. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 506 | updateInputConfigurationLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 507 | |
| 508 | // Enqueue configuration changed. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 509 | NotifyConfigurationChangedArgs args(when); |
| 510 | mQueuedListener->notifyConfigurationChanged(&args); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 513 | void InputReader::refreshConfigurationLocked(uint32_t changes) { |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 514 | mPolicy->getReaderConfiguration(&mConfig); |
| 515 | mEventHub->setExcludedDevices(mConfig.excludedDeviceNames); |
| 516 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 517 | if (changes) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 518 | ALOGI("Reconfiguring input devices. changes=0x%08x", changes); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 519 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 520 | |
| 521 | if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) { |
| 522 | mEventHub->requestReopenDevices(); |
| 523 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 524 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 525 | InputDevice* device = mDevices.valueAt(i); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 526 | device->configure(now, &mConfig, changes); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 527 | } |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 528 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 532 | void InputReader::updateGlobalMetaStateLocked() { |
| 533 | mGlobalMetaState = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 534 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 535 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 536 | InputDevice* device = mDevices.valueAt(i); |
| 537 | mGlobalMetaState |= device->getMetaState(); |
| 538 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 541 | int32_t InputReader::getGlobalMetaStateLocked() { |
| 542 | return mGlobalMetaState; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 545 | void InputReader::updateInputConfigurationLocked() { |
| 546 | int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH; |
| 547 | int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS; |
| 548 | int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV; |
| 549 | InputDeviceInfo deviceInfo; |
| 550 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 551 | InputDevice* device = mDevices.valueAt(i); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 552 | if (!(device->getClasses() & INPUT_DEVICE_CLASS_VIRTUAL)) { |
| 553 | device->getDeviceInfo(& deviceInfo); |
| 554 | uint32_t sources = deviceInfo.getSources(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 555 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 556 | if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) { |
| 557 | touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER; |
| 558 | } |
| 559 | if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) { |
| 560 | navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL; |
| 561 | } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) { |
| 562 | navigationConfig = InputConfiguration::NAVIGATION_DPAD; |
| 563 | } |
| 564 | if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) { |
| 565 | keyboardConfig = InputConfiguration::KEYBOARD_QWERTY; |
| 566 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 567 | } |
| 568 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 569 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 570 | mInputConfiguration.touchScreen = touchScreenConfig; |
| 571 | mInputConfiguration.keyboard = keyboardConfig; |
| 572 | mInputConfiguration.navigation = navigationConfig; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 575 | void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) { |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 576 | mDisableVirtualKeysTimeout = time; |
| 577 | } |
| 578 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 579 | bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 580 | InputDevice* device, int32_t keyCode, int32_t scanCode) { |
| 581 | if (now < mDisableVirtualKeysTimeout) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 582 | ALOGI("Dropping virtual key from device %s because virtual keys are " |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 583 | "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d", |
| 584 | device->getName().string(), |
| 585 | (mDisableVirtualKeysTimeout - now) * 0.000001, |
| 586 | keyCode, scanCode); |
| 587 | return true; |
| 588 | } else { |
| 589 | return false; |
| 590 | } |
| 591 | } |
| 592 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 593 | void InputReader::fadePointerLocked() { |
| 594 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 595 | InputDevice* device = mDevices.valueAt(i); |
| 596 | device->fadePointer(); |
| 597 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 598 | } |
| 599 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 600 | void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) { |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 601 | if (when < mNextTimeout) { |
| 602 | mNextTimeout = when; |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 603 | mEventHub->wake(); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 607 | int32_t InputReader::bumpGenerationLocked() { |
| 608 | return ++mGeneration; |
| 609 | } |
| 610 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 611 | void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 612 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 613 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 614 | *outConfiguration = mInputConfiguration; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 617 | void InputReader::getInputDevices(Vector<InputDeviceInfo>& outInputDevices) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 618 | AutoMutex _l(mLock); |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 619 | getInputDevicesLocked(outInputDevices); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 622 | void InputReader::getInputDevicesLocked(Vector<InputDeviceInfo>& outInputDevices) { |
| 623 | outInputDevices.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 624 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 625 | size_t numDevices = mDevices.size(); |
| 626 | for (size_t i = 0; i < numDevices; i++) { |
| 627 | InputDevice* device = mDevices.valueAt(i); |
| 628 | if (!device->isIgnored()) { |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 629 | outInputDevices.push(); |
| 630 | device->getDeviceInfo(&outInputDevices.editTop()); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 631 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 632 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, |
| 636 | int32_t keyCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 637 | AutoMutex _l(mLock); |
| 638 | |
| 639 | return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, |
| 643 | int32_t scanCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 644 | AutoMutex _l(mLock); |
| 645 | |
| 646 | return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | 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] | 650 | AutoMutex _l(mLock); |
| 651 | |
| 652 | return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 655 | 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] | 656 | GetStateFunc getStateFunc) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 657 | int32_t result = AKEY_STATE_UNKNOWN; |
| 658 | if (deviceId >= 0) { |
| 659 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 660 | if (deviceIndex >= 0) { |
| 661 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 662 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 663 | result = (device->*getStateFunc)(sourceMask, code); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 664 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 665 | } |
| 666 | } else { |
| 667 | size_t numDevices = mDevices.size(); |
| 668 | for (size_t i = 0; i < numDevices; i++) { |
| 669 | InputDevice* device = mDevices.valueAt(i); |
| 670 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
David Deephanphongs | fbca596 | 2011-11-14 14:50:45 -0800 | [diff] [blame] | 671 | // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that |
| 672 | // value. Otherwise, return AKEY_STATE_UP as long as one device reports it. |
| 673 | int32_t currentResult = (device->*getStateFunc)(sourceMask, code); |
| 674 | if (currentResult >= AKEY_STATE_DOWN) { |
| 675 | return currentResult; |
| 676 | } else if (currentResult == AKEY_STATE_UP) { |
| 677 | result = currentResult; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 681 | } |
| 682 | return result; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, |
| 686 | size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 687 | AutoMutex _l(mLock); |
| 688 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 689 | memset(outFlags, 0, numCodes); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 690 | return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 691 | } |
| 692 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 693 | bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, |
| 694 | size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) { |
| 695 | bool result = false; |
| 696 | if (deviceId >= 0) { |
| 697 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 698 | if (deviceIndex >= 0) { |
| 699 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 700 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 701 | result = device->markSupportedKeyCodes(sourceMask, |
| 702 | numCodes, keyCodes, outFlags); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 703 | } |
| 704 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 705 | } else { |
| 706 | size_t numDevices = mDevices.size(); |
| 707 | for (size_t i = 0; i < numDevices; i++) { |
| 708 | InputDevice* device = mDevices.valueAt(i); |
| 709 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 710 | result |= device->markSupportedKeyCodes(sourceMask, |
| 711 | numCodes, keyCodes, outFlags); |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | return result; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 716 | } |
| 717 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 718 | void InputReader::requestRefreshConfiguration(uint32_t changes) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 719 | AutoMutex _l(mLock); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 720 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 721 | if (changes) { |
| 722 | bool needWake = !mConfigurationChangesToRefresh; |
| 723 | mConfigurationChangesToRefresh |= changes; |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 724 | |
| 725 | if (needWake) { |
| 726 | mEventHub->wake(); |
| 727 | } |
| 728 | } |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 729 | } |
| 730 | |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 731 | void InputReader::vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize, |
| 732 | ssize_t repeat, int32_t token) { |
| 733 | AutoMutex _l(mLock); |
| 734 | |
| 735 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 736 | if (deviceIndex >= 0) { |
| 737 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 738 | device->vibrate(pattern, patternSize, repeat, token); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | void InputReader::cancelVibrate(int32_t deviceId, int32_t token) { |
| 743 | AutoMutex _l(mLock); |
| 744 | |
| 745 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 746 | if (deviceIndex >= 0) { |
| 747 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 748 | device->cancelVibrate(token); |
| 749 | } |
| 750 | } |
| 751 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 752 | void InputReader::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 753 | AutoMutex _l(mLock); |
| 754 | |
Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 755 | mEventHub->dump(dump); |
| 756 | dump.append("\n"); |
| 757 | |
| 758 | dump.append("Input Reader State:\n"); |
| 759 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 760 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 761 | mDevices.valueAt(i)->dump(dump); |
| 762 | } |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 763 | |
| 764 | dump.append(INDENT "Configuration:\n"); |
| 765 | dump.append(INDENT2 "ExcludedDeviceNames: ["); |
| 766 | for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) { |
| 767 | if (i != 0) { |
| 768 | dump.append(", "); |
| 769 | } |
| 770 | dump.append(mConfig.excludedDeviceNames.itemAt(i).string()); |
| 771 | } |
| 772 | dump.append("]\n"); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 773 | dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n", |
| 774 | mConfig.virtualKeyQuietTime * 0.000001f); |
| 775 | |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 776 | dump.appendFormat(INDENT2 "PointerVelocityControlParameters: " |
| 777 | "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n", |
| 778 | mConfig.pointerVelocityControlParameters.scale, |
| 779 | mConfig.pointerVelocityControlParameters.lowThreshold, |
| 780 | mConfig.pointerVelocityControlParameters.highThreshold, |
| 781 | mConfig.pointerVelocityControlParameters.acceleration); |
| 782 | |
| 783 | dump.appendFormat(INDENT2 "WheelVelocityControlParameters: " |
| 784 | "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n", |
| 785 | mConfig.wheelVelocityControlParameters.scale, |
| 786 | mConfig.wheelVelocityControlParameters.lowThreshold, |
| 787 | mConfig.wheelVelocityControlParameters.highThreshold, |
| 788 | mConfig.wheelVelocityControlParameters.acceleration); |
| 789 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 790 | dump.appendFormat(INDENT2 "PointerGesture:\n"); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 791 | dump.appendFormat(INDENT3 "Enabled: %s\n", |
| 792 | toString(mConfig.pointerGesturesEnabled)); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 793 | dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n", |
| 794 | mConfig.pointerGestureQuietInterval * 0.000001f); |
| 795 | dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n", |
| 796 | mConfig.pointerGestureDragMinSwitchSpeed); |
| 797 | dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n", |
| 798 | mConfig.pointerGestureTapInterval * 0.000001f); |
| 799 | dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n", |
| 800 | mConfig.pointerGestureTapDragInterval * 0.000001f); |
| 801 | dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n", |
| 802 | mConfig.pointerGestureTapSlop); |
| 803 | dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n", |
| 804 | mConfig.pointerGestureMultitouchSettleInterval * 0.000001f); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 805 | dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n", |
| 806 | mConfig.pointerGestureMultitouchMinDistance); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 807 | dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n", |
| 808 | mConfig.pointerGestureSwipeTransitionAngleCosine); |
| 809 | dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n", |
| 810 | mConfig.pointerGestureSwipeMaxWidthRatio); |
| 811 | dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n", |
| 812 | mConfig.pointerGestureMovementSpeedRatio); |
| 813 | dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n", |
| 814 | mConfig.pointerGestureZoomSpeedRatio); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Jeff Brown | 89ef072 | 2011-08-10 16:25:21 -0700 | [diff] [blame] | 817 | void InputReader::monitor() { |
| 818 | // Acquire and release the lock to ensure that the reader has not deadlocked. |
| 819 | mLock.lock(); |
Jeff Brown | 112b5f5 | 2012-01-27 17:32:06 -0800 | [diff] [blame] | 820 | mEventHub->wake(); |
| 821 | mReaderIsAliveCondition.wait(mLock); |
Jeff Brown | 89ef072 | 2011-08-10 16:25:21 -0700 | [diff] [blame] | 822 | mLock.unlock(); |
| 823 | |
| 824 | // Check the EventHub |
| 825 | mEventHub->monitor(); |
| 826 | } |
| 827 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 828 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 829 | // --- InputReader::ContextImpl --- |
| 830 | |
| 831 | InputReader::ContextImpl::ContextImpl(InputReader* reader) : |
| 832 | mReader(reader) { |
| 833 | } |
| 834 | |
| 835 | void InputReader::ContextImpl::updateGlobalMetaState() { |
| 836 | // lock is already held by the input loop |
| 837 | mReader->updateGlobalMetaStateLocked(); |
| 838 | } |
| 839 | |
| 840 | int32_t InputReader::ContextImpl::getGlobalMetaState() { |
| 841 | // lock is already held by the input loop |
| 842 | return mReader->getGlobalMetaStateLocked(); |
| 843 | } |
| 844 | |
| 845 | void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) { |
| 846 | // lock is already held by the input loop |
| 847 | mReader->disableVirtualKeysUntilLocked(time); |
| 848 | } |
| 849 | |
| 850 | bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, |
| 851 | InputDevice* device, int32_t keyCode, int32_t scanCode) { |
| 852 | // lock is already held by the input loop |
| 853 | return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode); |
| 854 | } |
| 855 | |
| 856 | void InputReader::ContextImpl::fadePointer() { |
| 857 | // lock is already held by the input loop |
| 858 | mReader->fadePointerLocked(); |
| 859 | } |
| 860 | |
| 861 | void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) { |
| 862 | // lock is already held by the input loop |
| 863 | mReader->requestTimeoutAtTimeLocked(when); |
| 864 | } |
| 865 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 866 | int32_t InputReader::ContextImpl::bumpGeneration() { |
| 867 | // lock is already held by the input loop |
| 868 | return mReader->bumpGenerationLocked(); |
| 869 | } |
| 870 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 871 | InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() { |
| 872 | return mReader->mPolicy.get(); |
| 873 | } |
| 874 | |
| 875 | InputListenerInterface* InputReader::ContextImpl::getListener() { |
| 876 | return mReader->mQueuedListener.get(); |
| 877 | } |
| 878 | |
| 879 | EventHubInterface* InputReader::ContextImpl::getEventHub() { |
| 880 | return mReader->mEventHub.get(); |
| 881 | } |
| 882 | |
| 883 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 884 | // --- InputReaderThread --- |
| 885 | |
| 886 | InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) : |
| 887 | Thread(/*canCallJava*/ true), mReader(reader) { |
| 888 | } |
| 889 | |
| 890 | InputReaderThread::~InputReaderThread() { |
| 891 | } |
| 892 | |
| 893 | bool InputReaderThread::threadLoop() { |
| 894 | mReader->loopOnce(); |
| 895 | return true; |
| 896 | } |
| 897 | |
| 898 | |
| 899 | // --- InputDevice --- |
| 900 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 901 | InputDevice::InputDevice(InputReaderContext* context, int32_t id, int32_t generation, |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 902 | const InputDeviceIdentifier& identifier, uint32_t classes) : |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 903 | mContext(context), mId(id), mGeneration(generation), |
| 904 | mIdentifier(identifier), mClasses(classes), |
Jeff Brown | 9ee285af | 2011-08-31 12:56:34 -0700 | [diff] [blame] | 905 | mSources(0), mIsExternal(false), mDropUntilNextSync(false) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | InputDevice::~InputDevice() { |
| 909 | size_t numMappers = mMappers.size(); |
| 910 | for (size_t i = 0; i < numMappers; i++) { |
| 911 | delete mMappers[i]; |
| 912 | } |
| 913 | mMappers.clear(); |
| 914 | } |
| 915 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 916 | void InputDevice::dump(String8& dump) { |
| 917 | InputDeviceInfo deviceInfo; |
| 918 | getDeviceInfo(& deviceInfo); |
| 919 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 920 | dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(), |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame^] | 921 | deviceInfo.getDisplayName().string()); |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 922 | dump.appendFormat(INDENT2 "Generation: %d\n", mGeneration); |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 923 | dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal)); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 924 | dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources()); |
| 925 | dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType()); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 926 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 927 | const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges(); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 928 | if (!ranges.isEmpty()) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 929 | dump.append(INDENT2 "Motion Ranges:\n"); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 930 | for (size_t i = 0; i < ranges.size(); i++) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 931 | const InputDeviceInfo::MotionRange& range = ranges.itemAt(i); |
| 932 | const char* label = getAxisLabel(range.axis); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 933 | char name[32]; |
| 934 | if (label) { |
| 935 | strncpy(name, label, sizeof(name)); |
| 936 | name[sizeof(name) - 1] = '\0'; |
| 937 | } else { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 938 | snprintf(name, sizeof(name), "%d", range.axis); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 939 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 940 | dump.appendFormat(INDENT3 "%s: source=0x%08x, " |
| 941 | "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n", |
| 942 | name, range.source, range.min, range.max, range.flat, range.fuzz); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 943 | } |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | size_t numMappers = mMappers.size(); |
| 947 | for (size_t i = 0; i < numMappers; i++) { |
| 948 | InputMapper* mapper = mMappers[i]; |
| 949 | mapper->dump(dump); |
| 950 | } |
| 951 | } |
| 952 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 953 | void InputDevice::addMapper(InputMapper* mapper) { |
| 954 | mMappers.add(mapper); |
| 955 | } |
| 956 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 957 | void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 958 | mSources = 0; |
| 959 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 960 | if (!isIgnored()) { |
| 961 | if (!changes) { // first time only |
| 962 | mContext->getEventHub()->getConfiguration(mId, &mConfiguration); |
| 963 | } |
| 964 | |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 965 | if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) { |
Jeff Brown | 61c0824 | 2012-04-19 11:14:33 -0700 | [diff] [blame] | 966 | if (!(mClasses & INPUT_DEVICE_CLASS_VIRTUAL)) { |
| 967 | sp<KeyCharacterMap> keyboardLayout = |
| 968 | mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier.descriptor); |
| 969 | if (mContext->getEventHub()->setKeyboardLayoutOverlay(mId, keyboardLayout)) { |
| 970 | bumpGeneration(); |
| 971 | } |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame^] | 975 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) { |
| 976 | if (!(mClasses & INPUT_DEVICE_CLASS_VIRTUAL)) { |
| 977 | String8 alias = mContext->getPolicy()->getDeviceAlias(mIdentifier); |
| 978 | if (mAlias != alias) { |
| 979 | mAlias = alias; |
| 980 | bumpGeneration(); |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 985 | size_t numMappers = mMappers.size(); |
| 986 | for (size_t i = 0; i < numMappers; i++) { |
| 987 | InputMapper* mapper = mMappers[i]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 988 | mapper->configure(when, config, changes); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 989 | mSources |= mapper->getSources(); |
| 990 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 991 | } |
| 992 | } |
| 993 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 994 | void InputDevice::reset(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 995 | size_t numMappers = mMappers.size(); |
| 996 | for (size_t i = 0; i < numMappers; i++) { |
| 997 | InputMapper* mapper = mMappers[i]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 998 | mapper->reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 999 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1000 | |
| 1001 | mContext->updateGlobalMetaState(); |
| 1002 | |
| 1003 | notifyReset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1004 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1005 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 1006 | void InputDevice::process(const RawEvent* rawEvents, size_t count) { |
| 1007 | // Process all of the events in order for each mapper. |
| 1008 | // We cannot simply ask each mapper to process them in bulk because mappers may |
| 1009 | // have side-effects that must be interleaved. For example, joystick movement events and |
| 1010 | // gamepad button presses are handled by different mappers but they should be dispatched |
| 1011 | // in the order received. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1012 | size_t numMappers = mMappers.size(); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 1013 | for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) { |
| 1014 | #if DEBUG_RAW_EVENTS |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1015 | ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x", |
| 1016 | rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 1017 | #endif |
| 1018 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 1019 | if (mDropUntilNextSync) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1020 | if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 1021 | mDropUntilNextSync = false; |
| 1022 | #if DEBUG_RAW_EVENTS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1023 | ALOGD("Recovered from input event buffer overrun."); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 1024 | #endif |
| 1025 | } else { |
| 1026 | #if DEBUG_RAW_EVENTS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1027 | ALOGD("Dropped input event while waiting for next input sync."); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 1028 | #endif |
| 1029 | } |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1030 | } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) { |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 1031 | ALOGI("Detected input event buffer overrun for device %s.", getName().string()); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 1032 | mDropUntilNextSync = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1033 | reset(rawEvent->when); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 1034 | } else { |
| 1035 | for (size_t i = 0; i < numMappers; i++) { |
| 1036 | InputMapper* mapper = mMappers[i]; |
| 1037 | mapper->process(rawEvent); |
| 1038 | } |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 1039 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1040 | } |
| 1041 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1042 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 1043 | void InputDevice::timeoutExpired(nsecs_t when) { |
| 1044 | size_t numMappers = mMappers.size(); |
| 1045 | for (size_t i = 0; i < numMappers; i++) { |
| 1046 | InputMapper* mapper = mMappers[i]; |
| 1047 | mapper->timeoutExpired(when); |
| 1048 | } |
| 1049 | } |
| 1050 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1051 | void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) { |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame^] | 1052 | outDeviceInfo->initialize(mId, mGeneration, mIdentifier, mAlias); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1053 | |
| 1054 | size_t numMappers = mMappers.size(); |
| 1055 | for (size_t i = 0; i < numMappers; i++) { |
| 1056 | InputMapper* mapper = mMappers[i]; |
| 1057 | mapper->populateDeviceInfo(outDeviceInfo); |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 1062 | return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState); |
| 1063 | } |
| 1064 | |
| 1065 | int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 1066 | return getState(sourceMask, scanCode, & InputMapper::getScanCodeState); |
| 1067 | } |
| 1068 | |
| 1069 | int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 1070 | return getState(sourceMask, switchCode, & InputMapper::getSwitchState); |
| 1071 | } |
| 1072 | |
| 1073 | int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) { |
| 1074 | int32_t result = AKEY_STATE_UNKNOWN; |
| 1075 | size_t numMappers = mMappers.size(); |
| 1076 | for (size_t i = 0; i < numMappers; i++) { |
| 1077 | InputMapper* mapper = mMappers[i]; |
| 1078 | if (sourcesMatchMask(mapper->getSources(), sourceMask)) { |
David Deephanphongs | fbca596 | 2011-11-14 14:50:45 -0800 | [diff] [blame] | 1079 | // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that |
| 1080 | // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it. |
| 1081 | int32_t currentResult = (mapper->*getStateFunc)(sourceMask, code); |
| 1082 | if (currentResult >= AKEY_STATE_DOWN) { |
| 1083 | return currentResult; |
| 1084 | } else if (currentResult == AKEY_STATE_UP) { |
| 1085 | result = currentResult; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1086 | } |
| 1087 | } |
| 1088 | } |
| 1089 | return result; |
| 1090 | } |
| 1091 | |
| 1092 | bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 1093 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 1094 | bool result = false; |
| 1095 | size_t numMappers = mMappers.size(); |
| 1096 | for (size_t i = 0; i < numMappers; i++) { |
| 1097 | InputMapper* mapper = mMappers[i]; |
| 1098 | if (sourcesMatchMask(mapper->getSources(), sourceMask)) { |
| 1099 | result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags); |
| 1100 | } |
| 1101 | } |
| 1102 | return result; |
| 1103 | } |
| 1104 | |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 1105 | void InputDevice::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, |
| 1106 | int32_t token) { |
| 1107 | size_t numMappers = mMappers.size(); |
| 1108 | for (size_t i = 0; i < numMappers; i++) { |
| 1109 | InputMapper* mapper = mMappers[i]; |
| 1110 | mapper->vibrate(pattern, patternSize, repeat, token); |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | void InputDevice::cancelVibrate(int32_t token) { |
| 1115 | size_t numMappers = mMappers.size(); |
| 1116 | for (size_t i = 0; i < numMappers; i++) { |
| 1117 | InputMapper* mapper = mMappers[i]; |
| 1118 | mapper->cancelVibrate(token); |
| 1119 | } |
| 1120 | } |
| 1121 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1122 | int32_t InputDevice::getMetaState() { |
| 1123 | int32_t result = 0; |
| 1124 | size_t numMappers = mMappers.size(); |
| 1125 | for (size_t i = 0; i < numMappers; i++) { |
| 1126 | InputMapper* mapper = mMappers[i]; |
| 1127 | result |= mapper->getMetaState(); |
| 1128 | } |
| 1129 | return result; |
| 1130 | } |
| 1131 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1132 | void InputDevice::fadePointer() { |
| 1133 | size_t numMappers = mMappers.size(); |
| 1134 | for (size_t i = 0; i < numMappers; i++) { |
| 1135 | InputMapper* mapper = mMappers[i]; |
| 1136 | mapper->fadePointer(); |
| 1137 | } |
| 1138 | } |
| 1139 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 1140 | void InputDevice::bumpGeneration() { |
| 1141 | mGeneration = mContext->bumpGeneration(); |
| 1142 | } |
| 1143 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1144 | void InputDevice::notifyReset(nsecs_t when) { |
| 1145 | NotifyDeviceResetArgs args(when, mId); |
| 1146 | mContext->getListener()->notifyDeviceReset(&args); |
| 1147 | } |
| 1148 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1149 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1150 | // --- CursorButtonAccumulator --- |
| 1151 | |
| 1152 | CursorButtonAccumulator::CursorButtonAccumulator() { |
| 1153 | clearButtons(); |
| 1154 | } |
| 1155 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1156 | void CursorButtonAccumulator::reset(InputDevice* device) { |
| 1157 | mBtnLeft = device->isKeyPressed(BTN_LEFT); |
| 1158 | mBtnRight = device->isKeyPressed(BTN_RIGHT); |
| 1159 | mBtnMiddle = device->isKeyPressed(BTN_MIDDLE); |
| 1160 | mBtnBack = device->isKeyPressed(BTN_BACK); |
| 1161 | mBtnSide = device->isKeyPressed(BTN_SIDE); |
| 1162 | mBtnForward = device->isKeyPressed(BTN_FORWARD); |
| 1163 | mBtnExtra = device->isKeyPressed(BTN_EXTRA); |
| 1164 | mBtnTask = device->isKeyPressed(BTN_TASK); |
| 1165 | } |
| 1166 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1167 | void CursorButtonAccumulator::clearButtons() { |
| 1168 | mBtnLeft = 0; |
| 1169 | mBtnRight = 0; |
| 1170 | mBtnMiddle = 0; |
| 1171 | mBtnBack = 0; |
| 1172 | mBtnSide = 0; |
| 1173 | mBtnForward = 0; |
| 1174 | mBtnExtra = 0; |
| 1175 | mBtnTask = 0; |
| 1176 | } |
| 1177 | |
| 1178 | void CursorButtonAccumulator::process(const RawEvent* rawEvent) { |
| 1179 | if (rawEvent->type == EV_KEY) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1180 | switch (rawEvent->code) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1181 | case BTN_LEFT: |
| 1182 | mBtnLeft = rawEvent->value; |
| 1183 | break; |
| 1184 | case BTN_RIGHT: |
| 1185 | mBtnRight = rawEvent->value; |
| 1186 | break; |
| 1187 | case BTN_MIDDLE: |
| 1188 | mBtnMiddle = rawEvent->value; |
| 1189 | break; |
| 1190 | case BTN_BACK: |
| 1191 | mBtnBack = rawEvent->value; |
| 1192 | break; |
| 1193 | case BTN_SIDE: |
| 1194 | mBtnSide = rawEvent->value; |
| 1195 | break; |
| 1196 | case BTN_FORWARD: |
| 1197 | mBtnForward = rawEvent->value; |
| 1198 | break; |
| 1199 | case BTN_EXTRA: |
| 1200 | mBtnExtra = rawEvent->value; |
| 1201 | break; |
| 1202 | case BTN_TASK: |
| 1203 | mBtnTask = rawEvent->value; |
| 1204 | break; |
| 1205 | } |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | uint32_t CursorButtonAccumulator::getButtonState() const { |
| 1210 | uint32_t result = 0; |
| 1211 | if (mBtnLeft) { |
| 1212 | result |= AMOTION_EVENT_BUTTON_PRIMARY; |
| 1213 | } |
| 1214 | if (mBtnRight) { |
| 1215 | result |= AMOTION_EVENT_BUTTON_SECONDARY; |
| 1216 | } |
| 1217 | if (mBtnMiddle) { |
| 1218 | result |= AMOTION_EVENT_BUTTON_TERTIARY; |
| 1219 | } |
| 1220 | if (mBtnBack || mBtnSide) { |
| 1221 | result |= AMOTION_EVENT_BUTTON_BACK; |
| 1222 | } |
| 1223 | if (mBtnForward || mBtnExtra) { |
| 1224 | result |= AMOTION_EVENT_BUTTON_FORWARD; |
| 1225 | } |
| 1226 | return result; |
| 1227 | } |
| 1228 | |
| 1229 | |
| 1230 | // --- CursorMotionAccumulator --- |
| 1231 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1232 | CursorMotionAccumulator::CursorMotionAccumulator() { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1233 | clearRelativeAxes(); |
| 1234 | } |
| 1235 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1236 | void CursorMotionAccumulator::reset(InputDevice* device) { |
| 1237 | clearRelativeAxes(); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | void CursorMotionAccumulator::clearRelativeAxes() { |
| 1241 | mRelX = 0; |
| 1242 | mRelY = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | void CursorMotionAccumulator::process(const RawEvent* rawEvent) { |
| 1246 | if (rawEvent->type == EV_REL) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1247 | switch (rawEvent->code) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1248 | case REL_X: |
| 1249 | mRelX = rawEvent->value; |
| 1250 | break; |
| 1251 | case REL_Y: |
| 1252 | mRelY = rawEvent->value; |
| 1253 | break; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1254 | } |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | void CursorMotionAccumulator::finishSync() { |
| 1259 | clearRelativeAxes(); |
| 1260 | } |
| 1261 | |
| 1262 | |
| 1263 | // --- CursorScrollAccumulator --- |
| 1264 | |
| 1265 | CursorScrollAccumulator::CursorScrollAccumulator() : |
| 1266 | mHaveRelWheel(false), mHaveRelHWheel(false) { |
| 1267 | clearRelativeAxes(); |
| 1268 | } |
| 1269 | |
| 1270 | void CursorScrollAccumulator::configure(InputDevice* device) { |
| 1271 | mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL); |
| 1272 | mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL); |
| 1273 | } |
| 1274 | |
| 1275 | void CursorScrollAccumulator::reset(InputDevice* device) { |
| 1276 | clearRelativeAxes(); |
| 1277 | } |
| 1278 | |
| 1279 | void CursorScrollAccumulator::clearRelativeAxes() { |
| 1280 | mRelWheel = 0; |
| 1281 | mRelHWheel = 0; |
| 1282 | } |
| 1283 | |
| 1284 | void CursorScrollAccumulator::process(const RawEvent* rawEvent) { |
| 1285 | if (rawEvent->type == EV_REL) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1286 | switch (rawEvent->code) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1287 | case REL_WHEEL: |
| 1288 | mRelWheel = rawEvent->value; |
| 1289 | break; |
| 1290 | case REL_HWHEEL: |
| 1291 | mRelHWheel = rawEvent->value; |
| 1292 | break; |
| 1293 | } |
| 1294 | } |
| 1295 | } |
| 1296 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1297 | void CursorScrollAccumulator::finishSync() { |
| 1298 | clearRelativeAxes(); |
| 1299 | } |
| 1300 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1301 | |
| 1302 | // --- TouchButtonAccumulator --- |
| 1303 | |
| 1304 | TouchButtonAccumulator::TouchButtonAccumulator() : |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 1305 | mHaveBtnTouch(false), mHaveStylus(false) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1306 | clearButtons(); |
| 1307 | } |
| 1308 | |
| 1309 | void TouchButtonAccumulator::configure(InputDevice* device) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1310 | mHaveBtnTouch = device->hasKey(BTN_TOUCH); |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 1311 | mHaveStylus = device->hasKey(BTN_TOOL_PEN) |
| 1312 | || device->hasKey(BTN_TOOL_RUBBER) |
| 1313 | || device->hasKey(BTN_TOOL_BRUSH) |
| 1314 | || device->hasKey(BTN_TOOL_PENCIL) |
| 1315 | || device->hasKey(BTN_TOOL_AIRBRUSH); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | void TouchButtonAccumulator::reset(InputDevice* device) { |
| 1319 | mBtnTouch = device->isKeyPressed(BTN_TOUCH); |
| 1320 | mBtnStylus = device->isKeyPressed(BTN_STYLUS); |
| 1321 | mBtnStylus2 = device->isKeyPressed(BTN_STYLUS); |
| 1322 | mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER); |
| 1323 | mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN); |
| 1324 | mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER); |
| 1325 | mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH); |
| 1326 | mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL); |
| 1327 | mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH); |
| 1328 | mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE); |
| 1329 | mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS); |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1330 | mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP); |
| 1331 | mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP); |
| 1332 | mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | void TouchButtonAccumulator::clearButtons() { |
| 1336 | mBtnTouch = 0; |
| 1337 | mBtnStylus = 0; |
| 1338 | mBtnStylus2 = 0; |
| 1339 | mBtnToolFinger = 0; |
| 1340 | mBtnToolPen = 0; |
| 1341 | mBtnToolRubber = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1342 | mBtnToolBrush = 0; |
| 1343 | mBtnToolPencil = 0; |
| 1344 | mBtnToolAirbrush = 0; |
| 1345 | mBtnToolMouse = 0; |
| 1346 | mBtnToolLens = 0; |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1347 | mBtnToolDoubleTap = 0; |
| 1348 | mBtnToolTripleTap = 0; |
| 1349 | mBtnToolQuadTap = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | void TouchButtonAccumulator::process(const RawEvent* rawEvent) { |
| 1353 | if (rawEvent->type == EV_KEY) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1354 | switch (rawEvent->code) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1355 | case BTN_TOUCH: |
| 1356 | mBtnTouch = rawEvent->value; |
| 1357 | break; |
| 1358 | case BTN_STYLUS: |
| 1359 | mBtnStylus = rawEvent->value; |
| 1360 | break; |
| 1361 | case BTN_STYLUS2: |
| 1362 | mBtnStylus2 = rawEvent->value; |
| 1363 | break; |
| 1364 | case BTN_TOOL_FINGER: |
| 1365 | mBtnToolFinger = rawEvent->value; |
| 1366 | break; |
| 1367 | case BTN_TOOL_PEN: |
| 1368 | mBtnToolPen = rawEvent->value; |
| 1369 | break; |
| 1370 | case BTN_TOOL_RUBBER: |
| 1371 | mBtnToolRubber = rawEvent->value; |
| 1372 | break; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1373 | case BTN_TOOL_BRUSH: |
| 1374 | mBtnToolBrush = rawEvent->value; |
| 1375 | break; |
| 1376 | case BTN_TOOL_PENCIL: |
| 1377 | mBtnToolPencil = rawEvent->value; |
| 1378 | break; |
| 1379 | case BTN_TOOL_AIRBRUSH: |
| 1380 | mBtnToolAirbrush = rawEvent->value; |
| 1381 | break; |
| 1382 | case BTN_TOOL_MOUSE: |
| 1383 | mBtnToolMouse = rawEvent->value; |
| 1384 | break; |
| 1385 | case BTN_TOOL_LENS: |
| 1386 | mBtnToolLens = rawEvent->value; |
| 1387 | break; |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1388 | case BTN_TOOL_DOUBLETAP: |
| 1389 | mBtnToolDoubleTap = rawEvent->value; |
| 1390 | break; |
| 1391 | case BTN_TOOL_TRIPLETAP: |
| 1392 | mBtnToolTripleTap = rawEvent->value; |
| 1393 | break; |
| 1394 | case BTN_TOOL_QUADTAP: |
| 1395 | mBtnToolQuadTap = rawEvent->value; |
| 1396 | break; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1397 | } |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | uint32_t TouchButtonAccumulator::getButtonState() const { |
| 1402 | uint32_t result = 0; |
| 1403 | if (mBtnStylus) { |
| 1404 | result |= AMOTION_EVENT_BUTTON_SECONDARY; |
| 1405 | } |
| 1406 | if (mBtnStylus2) { |
| 1407 | result |= AMOTION_EVENT_BUTTON_TERTIARY; |
| 1408 | } |
| 1409 | return result; |
| 1410 | } |
| 1411 | |
| 1412 | int32_t TouchButtonAccumulator::getToolType() const { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1413 | if (mBtnToolMouse || mBtnToolLens) { |
| 1414 | return AMOTION_EVENT_TOOL_TYPE_MOUSE; |
| 1415 | } |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1416 | if (mBtnToolRubber) { |
| 1417 | return AMOTION_EVENT_TOOL_TYPE_ERASER; |
| 1418 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1419 | if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1420 | return AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 1421 | } |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1422 | if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1423 | return AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 1424 | } |
| 1425 | return AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
| 1426 | } |
| 1427 | |
Jeff Brown | d87c6d5 | 2011-08-10 14:55:59 -0700 | [diff] [blame] | 1428 | bool TouchButtonAccumulator::isToolActive() const { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1429 | return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber |
| 1430 | || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1431 | || mBtnToolMouse || mBtnToolLens |
| 1432 | || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | bool TouchButtonAccumulator::isHovering() const { |
| 1436 | return mHaveBtnTouch && !mBtnTouch; |
| 1437 | } |
| 1438 | |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 1439 | bool TouchButtonAccumulator::hasStylus() const { |
| 1440 | return mHaveStylus; |
| 1441 | } |
| 1442 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1443 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1444 | // --- RawPointerAxes --- |
| 1445 | |
| 1446 | RawPointerAxes::RawPointerAxes() { |
| 1447 | clear(); |
| 1448 | } |
| 1449 | |
| 1450 | void RawPointerAxes::clear() { |
| 1451 | x.clear(); |
| 1452 | y.clear(); |
| 1453 | pressure.clear(); |
| 1454 | touchMajor.clear(); |
| 1455 | touchMinor.clear(); |
| 1456 | toolMajor.clear(); |
| 1457 | toolMinor.clear(); |
| 1458 | orientation.clear(); |
| 1459 | distance.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1460 | tiltX.clear(); |
| 1461 | tiltY.clear(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1462 | trackingId.clear(); |
| 1463 | slot.clear(); |
| 1464 | } |
| 1465 | |
| 1466 | |
| 1467 | // --- RawPointerData --- |
| 1468 | |
| 1469 | RawPointerData::RawPointerData() { |
| 1470 | clear(); |
| 1471 | } |
| 1472 | |
| 1473 | void RawPointerData::clear() { |
| 1474 | pointerCount = 0; |
| 1475 | clearIdBits(); |
| 1476 | } |
| 1477 | |
| 1478 | void RawPointerData::copyFrom(const RawPointerData& other) { |
| 1479 | pointerCount = other.pointerCount; |
| 1480 | hoveringIdBits = other.hoveringIdBits; |
| 1481 | touchingIdBits = other.touchingIdBits; |
| 1482 | |
| 1483 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 1484 | pointers[i] = other.pointers[i]; |
| 1485 | |
| 1486 | int id = pointers[i].id; |
| 1487 | idToIndex[id] = other.idToIndex[id]; |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const { |
| 1492 | float x = 0, y = 0; |
| 1493 | uint32_t count = touchingIdBits.count(); |
| 1494 | if (count) { |
| 1495 | for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) { |
| 1496 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 1497 | const Pointer& pointer = pointerForId(id); |
| 1498 | x += pointer.x; |
| 1499 | y += pointer.y; |
| 1500 | } |
| 1501 | x /= count; |
| 1502 | y /= count; |
| 1503 | } |
| 1504 | *outX = x; |
| 1505 | *outY = y; |
| 1506 | } |
| 1507 | |
| 1508 | |
| 1509 | // --- CookedPointerData --- |
| 1510 | |
| 1511 | CookedPointerData::CookedPointerData() { |
| 1512 | clear(); |
| 1513 | } |
| 1514 | |
| 1515 | void CookedPointerData::clear() { |
| 1516 | pointerCount = 0; |
| 1517 | hoveringIdBits.clear(); |
| 1518 | touchingIdBits.clear(); |
| 1519 | } |
| 1520 | |
| 1521 | void CookedPointerData::copyFrom(const CookedPointerData& other) { |
| 1522 | pointerCount = other.pointerCount; |
| 1523 | hoveringIdBits = other.hoveringIdBits; |
| 1524 | touchingIdBits = other.touchingIdBits; |
| 1525 | |
| 1526 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 1527 | pointerProperties[i].copyFrom(other.pointerProperties[i]); |
| 1528 | pointerCoords[i].copyFrom(other.pointerCoords[i]); |
| 1529 | |
| 1530 | int id = pointerProperties[i].id; |
| 1531 | idToIndex[id] = other.idToIndex[id]; |
| 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1536 | // --- SingleTouchMotionAccumulator --- |
| 1537 | |
| 1538 | SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() { |
| 1539 | clearAbsoluteAxes(); |
| 1540 | } |
| 1541 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1542 | void SingleTouchMotionAccumulator::reset(InputDevice* device) { |
| 1543 | mAbsX = device->getAbsoluteAxisValue(ABS_X); |
| 1544 | mAbsY = device->getAbsoluteAxisValue(ABS_Y); |
| 1545 | mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE); |
| 1546 | mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH); |
| 1547 | mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE); |
| 1548 | mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X); |
| 1549 | mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y); |
| 1550 | } |
| 1551 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1552 | void SingleTouchMotionAccumulator::clearAbsoluteAxes() { |
| 1553 | mAbsX = 0; |
| 1554 | mAbsY = 0; |
| 1555 | mAbsPressure = 0; |
| 1556 | mAbsToolWidth = 0; |
| 1557 | mAbsDistance = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1558 | mAbsTiltX = 0; |
| 1559 | mAbsTiltY = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) { |
| 1563 | if (rawEvent->type == EV_ABS) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1564 | switch (rawEvent->code) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1565 | case ABS_X: |
| 1566 | mAbsX = rawEvent->value; |
| 1567 | break; |
| 1568 | case ABS_Y: |
| 1569 | mAbsY = rawEvent->value; |
| 1570 | break; |
| 1571 | case ABS_PRESSURE: |
| 1572 | mAbsPressure = rawEvent->value; |
| 1573 | break; |
| 1574 | case ABS_TOOL_WIDTH: |
| 1575 | mAbsToolWidth = rawEvent->value; |
| 1576 | break; |
| 1577 | case ABS_DISTANCE: |
| 1578 | mAbsDistance = rawEvent->value; |
| 1579 | break; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1580 | case ABS_TILT_X: |
| 1581 | mAbsTiltX = rawEvent->value; |
| 1582 | break; |
| 1583 | case ABS_TILT_Y: |
| 1584 | mAbsTiltY = rawEvent->value; |
| 1585 | break; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1586 | } |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | |
| 1591 | // --- MultiTouchMotionAccumulator --- |
| 1592 | |
| 1593 | MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() : |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 1594 | mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false), |
| 1595 | mHaveStylus(false) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() { |
| 1599 | delete[] mSlots; |
| 1600 | } |
| 1601 | |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 1602 | void MultiTouchMotionAccumulator::configure(InputDevice* device, |
| 1603 | size_t slotCount, bool usingSlotsProtocol) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1604 | mSlotCount = slotCount; |
| 1605 | mUsingSlotsProtocol = usingSlotsProtocol; |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 1606 | mHaveStylus = device->hasAbsoluteAxis(ABS_MT_TOOL_TYPE); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1607 | |
| 1608 | delete[] mSlots; |
| 1609 | mSlots = new Slot[slotCount]; |
| 1610 | } |
| 1611 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1612 | void MultiTouchMotionAccumulator::reset(InputDevice* device) { |
| 1613 | // Unfortunately there is no way to read the initial contents of the slots. |
| 1614 | // So when we reset the accumulator, we must assume they are all zeroes. |
| 1615 | if (mUsingSlotsProtocol) { |
| 1616 | // Query the driver for the current slot index and use it as the initial slot |
| 1617 | // before we start reading events from the device. It is possible that the |
| 1618 | // current slot index will not be the same as it was when the first event was |
| 1619 | // written into the evdev buffer, which means the input mapper could start |
| 1620 | // out of sync with the initial state of the events in the evdev buffer. |
| 1621 | // In the extremely unlikely case that this happens, the data from |
| 1622 | // two slots will be confused until the next ABS_MT_SLOT event is received. |
| 1623 | // This can cause the touch point to "jump", but at least there will be |
| 1624 | // no stuck touches. |
| 1625 | int32_t initialSlot; |
| 1626 | status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(), |
| 1627 | ABS_MT_SLOT, &initialSlot); |
| 1628 | if (status) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1629 | ALOGD("Could not retrieve current multitouch slot index. status=%d", status); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1630 | initialSlot = -1; |
| 1631 | } |
| 1632 | clearSlots(initialSlot); |
| 1633 | } else { |
| 1634 | clearSlots(-1); |
| 1635 | } |
| 1636 | } |
| 1637 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1638 | void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1639 | if (mSlots) { |
| 1640 | for (size_t i = 0; i < mSlotCount; i++) { |
| 1641 | mSlots[i].clear(); |
| 1642 | } |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1643 | } |
| 1644 | mCurrentSlot = initialSlot; |
| 1645 | } |
| 1646 | |
| 1647 | void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) { |
| 1648 | if (rawEvent->type == EV_ABS) { |
| 1649 | bool newSlot = false; |
| 1650 | if (mUsingSlotsProtocol) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1651 | if (rawEvent->code == ABS_MT_SLOT) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1652 | mCurrentSlot = rawEvent->value; |
| 1653 | newSlot = true; |
| 1654 | } |
| 1655 | } else if (mCurrentSlot < 0) { |
| 1656 | mCurrentSlot = 0; |
| 1657 | } |
| 1658 | |
| 1659 | if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) { |
| 1660 | #if DEBUG_POINTERS |
| 1661 | if (newSlot) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1662 | ALOGW("MultiTouch device emitted invalid slot index %d but it " |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1663 | "should be between 0 and %d; ignoring this slot.", |
| 1664 | mCurrentSlot, mSlotCount - 1); |
| 1665 | } |
| 1666 | #endif |
| 1667 | } else { |
| 1668 | Slot* slot = &mSlots[mCurrentSlot]; |
| 1669 | |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1670 | switch (rawEvent->code) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1671 | case ABS_MT_POSITION_X: |
| 1672 | slot->mInUse = true; |
| 1673 | slot->mAbsMTPositionX = rawEvent->value; |
| 1674 | break; |
| 1675 | case ABS_MT_POSITION_Y: |
| 1676 | slot->mInUse = true; |
| 1677 | slot->mAbsMTPositionY = rawEvent->value; |
| 1678 | break; |
| 1679 | case ABS_MT_TOUCH_MAJOR: |
| 1680 | slot->mInUse = true; |
| 1681 | slot->mAbsMTTouchMajor = rawEvent->value; |
| 1682 | break; |
| 1683 | case ABS_MT_TOUCH_MINOR: |
| 1684 | slot->mInUse = true; |
| 1685 | slot->mAbsMTTouchMinor = rawEvent->value; |
| 1686 | slot->mHaveAbsMTTouchMinor = true; |
| 1687 | break; |
| 1688 | case ABS_MT_WIDTH_MAJOR: |
| 1689 | slot->mInUse = true; |
| 1690 | slot->mAbsMTWidthMajor = rawEvent->value; |
| 1691 | break; |
| 1692 | case ABS_MT_WIDTH_MINOR: |
| 1693 | slot->mInUse = true; |
| 1694 | slot->mAbsMTWidthMinor = rawEvent->value; |
| 1695 | slot->mHaveAbsMTWidthMinor = true; |
| 1696 | break; |
| 1697 | case ABS_MT_ORIENTATION: |
| 1698 | slot->mInUse = true; |
| 1699 | slot->mAbsMTOrientation = rawEvent->value; |
| 1700 | break; |
| 1701 | case ABS_MT_TRACKING_ID: |
| 1702 | if (mUsingSlotsProtocol && rawEvent->value < 0) { |
Jeff Brown | 8bcbbef | 2011-08-11 15:49:09 -0700 | [diff] [blame] | 1703 | // The slot is no longer in use but it retains its previous contents, |
| 1704 | // which may be reused for subsequent touches. |
| 1705 | slot->mInUse = false; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1706 | } else { |
| 1707 | slot->mInUse = true; |
| 1708 | slot->mAbsMTTrackingId = rawEvent->value; |
| 1709 | } |
| 1710 | break; |
| 1711 | case ABS_MT_PRESSURE: |
| 1712 | slot->mInUse = true; |
| 1713 | slot->mAbsMTPressure = rawEvent->value; |
| 1714 | break; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1715 | case ABS_MT_DISTANCE: |
| 1716 | slot->mInUse = true; |
| 1717 | slot->mAbsMTDistance = rawEvent->value; |
| 1718 | break; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1719 | case ABS_MT_TOOL_TYPE: |
| 1720 | slot->mInUse = true; |
| 1721 | slot->mAbsMTToolType = rawEvent->value; |
| 1722 | slot->mHaveAbsMTToolType = true; |
| 1723 | break; |
| 1724 | } |
| 1725 | } |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1726 | } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_MT_REPORT) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1727 | // MultiTouch Sync: The driver has returned all data for *one* of the pointers. |
| 1728 | mCurrentSlot += 1; |
| 1729 | } |
| 1730 | } |
| 1731 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1732 | void MultiTouchMotionAccumulator::finishSync() { |
| 1733 | if (!mUsingSlotsProtocol) { |
| 1734 | clearSlots(-1); |
| 1735 | } |
| 1736 | } |
| 1737 | |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 1738 | bool MultiTouchMotionAccumulator::hasStylus() const { |
| 1739 | return mHaveStylus; |
| 1740 | } |
| 1741 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1742 | |
| 1743 | // --- MultiTouchMotionAccumulator::Slot --- |
| 1744 | |
| 1745 | MultiTouchMotionAccumulator::Slot::Slot() { |
| 1746 | clear(); |
| 1747 | } |
| 1748 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1749 | void MultiTouchMotionAccumulator::Slot::clear() { |
| 1750 | mInUse = false; |
| 1751 | mHaveAbsMTTouchMinor = false; |
| 1752 | mHaveAbsMTWidthMinor = false; |
| 1753 | mHaveAbsMTToolType = false; |
| 1754 | mAbsMTPositionX = 0; |
| 1755 | mAbsMTPositionY = 0; |
| 1756 | mAbsMTTouchMajor = 0; |
| 1757 | mAbsMTTouchMinor = 0; |
| 1758 | mAbsMTWidthMajor = 0; |
| 1759 | mAbsMTWidthMinor = 0; |
| 1760 | mAbsMTOrientation = 0; |
| 1761 | mAbsMTTrackingId = -1; |
| 1762 | mAbsMTPressure = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1763 | mAbsMTDistance = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1764 | mAbsMTToolType = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | int32_t MultiTouchMotionAccumulator::Slot::getToolType() const { |
| 1768 | if (mHaveAbsMTToolType) { |
| 1769 | switch (mAbsMTToolType) { |
| 1770 | case MT_TOOL_FINGER: |
| 1771 | return AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 1772 | case MT_TOOL_PEN: |
| 1773 | return AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 1774 | } |
| 1775 | } |
| 1776 | return AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
| 1777 | } |
| 1778 | |
| 1779 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1780 | // --- InputMapper --- |
| 1781 | |
| 1782 | InputMapper::InputMapper(InputDevice* device) : |
| 1783 | mDevice(device), mContext(device->getContext()) { |
| 1784 | } |
| 1785 | |
| 1786 | InputMapper::~InputMapper() { |
| 1787 | } |
| 1788 | |
| 1789 | void InputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 1790 | info->addSource(getSources()); |
| 1791 | } |
| 1792 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1793 | void InputMapper::dump(String8& dump) { |
| 1794 | } |
| 1795 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1796 | void InputMapper::configure(nsecs_t when, |
| 1797 | const InputReaderConfiguration* config, uint32_t changes) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1798 | } |
| 1799 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1800 | void InputMapper::reset(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1801 | } |
| 1802 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 1803 | void InputMapper::timeoutExpired(nsecs_t when) { |
| 1804 | } |
| 1805 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1806 | int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 1807 | return AKEY_STATE_UNKNOWN; |
| 1808 | } |
| 1809 | |
| 1810 | int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 1811 | return AKEY_STATE_UNKNOWN; |
| 1812 | } |
| 1813 | |
| 1814 | int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 1815 | return AKEY_STATE_UNKNOWN; |
| 1816 | } |
| 1817 | |
| 1818 | bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 1819 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 1820 | return false; |
| 1821 | } |
| 1822 | |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 1823 | void InputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, |
| 1824 | int32_t token) { |
| 1825 | } |
| 1826 | |
| 1827 | void InputMapper::cancelVibrate(int32_t token) { |
| 1828 | } |
| 1829 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1830 | int32_t InputMapper::getMetaState() { |
| 1831 | return 0; |
| 1832 | } |
| 1833 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1834 | void InputMapper::fadePointer() { |
| 1835 | } |
| 1836 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1837 | status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) { |
| 1838 | return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo); |
| 1839 | } |
| 1840 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 1841 | void InputMapper::bumpGeneration() { |
| 1842 | mDevice->bumpGeneration(); |
| 1843 | } |
| 1844 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1845 | void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump, |
| 1846 | const RawAbsoluteAxisInfo& axis, const char* name) { |
| 1847 | if (axis.valid) { |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 1848 | dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n", |
| 1849 | name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1850 | } else { |
| 1851 | dump.appendFormat(INDENT4 "%s: unknown range\n", name); |
| 1852 | } |
| 1853 | } |
| 1854 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1855 | |
| 1856 | // --- SwitchInputMapper --- |
| 1857 | |
| 1858 | SwitchInputMapper::SwitchInputMapper(InputDevice* device) : |
| 1859 | InputMapper(device) { |
| 1860 | } |
| 1861 | |
| 1862 | SwitchInputMapper::~SwitchInputMapper() { |
| 1863 | } |
| 1864 | |
| 1865 | uint32_t SwitchInputMapper::getSources() { |
Jeff Brown | 89de57a | 2011-01-19 18:41:38 -0800 | [diff] [blame] | 1866 | return AINPUT_SOURCE_SWITCH; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | void SwitchInputMapper::process(const RawEvent* rawEvent) { |
| 1870 | switch (rawEvent->type) { |
| 1871 | case EV_SW: |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1872 | processSwitch(rawEvent->when, rawEvent->code, rawEvent->value); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1873 | break; |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1878 | NotifySwitchArgs args(when, 0, switchCode, switchValue); |
| 1879 | getListener()->notifySwitch(&args); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 1883 | return getEventHub()->getSwitchState(getDeviceId(), switchCode); |
| 1884 | } |
| 1885 | |
| 1886 | |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 1887 | // --- VibratorInputMapper --- |
| 1888 | |
| 1889 | VibratorInputMapper::VibratorInputMapper(InputDevice* device) : |
| 1890 | InputMapper(device), mVibrating(false) { |
| 1891 | } |
| 1892 | |
| 1893 | VibratorInputMapper::~VibratorInputMapper() { |
| 1894 | } |
| 1895 | |
| 1896 | uint32_t VibratorInputMapper::getSources() { |
| 1897 | return 0; |
| 1898 | } |
| 1899 | |
| 1900 | void VibratorInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 1901 | InputMapper::populateDeviceInfo(info); |
| 1902 | |
| 1903 | info->setVibrator(true); |
| 1904 | } |
| 1905 | |
| 1906 | void VibratorInputMapper::process(const RawEvent* rawEvent) { |
| 1907 | // TODO: Handle FF_STATUS, although it does not seem to be widely supported. |
| 1908 | } |
| 1909 | |
| 1910 | void VibratorInputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, |
| 1911 | int32_t token) { |
| 1912 | #if DEBUG_VIBRATOR |
| 1913 | String8 patternStr; |
| 1914 | for (size_t i = 0; i < patternSize; i++) { |
| 1915 | if (i != 0) { |
| 1916 | patternStr.append(", "); |
| 1917 | } |
| 1918 | patternStr.appendFormat("%lld", pattern[i]); |
| 1919 | } |
| 1920 | ALOGD("vibrate: deviceId=%d, pattern=[%s], repeat=%ld, token=%d", |
| 1921 | getDeviceId(), patternStr.string(), repeat, token); |
| 1922 | #endif |
| 1923 | |
| 1924 | mVibrating = true; |
| 1925 | memcpy(mPattern, pattern, patternSize * sizeof(nsecs_t)); |
| 1926 | mPatternSize = patternSize; |
| 1927 | mRepeat = repeat; |
| 1928 | mToken = token; |
| 1929 | mIndex = -1; |
| 1930 | |
| 1931 | nextStep(); |
| 1932 | } |
| 1933 | |
| 1934 | void VibratorInputMapper::cancelVibrate(int32_t token) { |
| 1935 | #if DEBUG_VIBRATOR |
| 1936 | ALOGD("cancelVibrate: deviceId=%d, token=%d", getDeviceId(), token); |
| 1937 | #endif |
| 1938 | |
| 1939 | if (mVibrating && mToken == token) { |
| 1940 | stopVibrating(); |
| 1941 | } |
| 1942 | } |
| 1943 | |
| 1944 | void VibratorInputMapper::timeoutExpired(nsecs_t when) { |
| 1945 | if (mVibrating) { |
| 1946 | if (when >= mNextStepTime) { |
| 1947 | nextStep(); |
| 1948 | } else { |
| 1949 | getContext()->requestTimeoutAtTime(mNextStepTime); |
| 1950 | } |
| 1951 | } |
| 1952 | } |
| 1953 | |
| 1954 | void VibratorInputMapper::nextStep() { |
| 1955 | mIndex += 1; |
| 1956 | if (size_t(mIndex) >= mPatternSize) { |
| 1957 | if (mRepeat < 0) { |
| 1958 | // We are done. |
| 1959 | stopVibrating(); |
| 1960 | return; |
| 1961 | } |
| 1962 | mIndex = mRepeat; |
| 1963 | } |
| 1964 | |
| 1965 | bool vibratorOn = mIndex & 1; |
| 1966 | nsecs_t duration = mPattern[mIndex]; |
| 1967 | if (vibratorOn) { |
| 1968 | #if DEBUG_VIBRATOR |
| 1969 | ALOGD("nextStep: sending vibrate deviceId=%d, duration=%lld", |
| 1970 | getDeviceId(), duration); |
| 1971 | #endif |
| 1972 | getEventHub()->vibrate(getDeviceId(), duration); |
| 1973 | } else { |
| 1974 | #if DEBUG_VIBRATOR |
| 1975 | ALOGD("nextStep: sending cancel vibrate deviceId=%d", getDeviceId()); |
| 1976 | #endif |
| 1977 | getEventHub()->cancelVibrate(getDeviceId()); |
| 1978 | } |
| 1979 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1980 | mNextStepTime = now + duration; |
| 1981 | getContext()->requestTimeoutAtTime(mNextStepTime); |
| 1982 | #if DEBUG_VIBRATOR |
| 1983 | ALOGD("nextStep: scheduled timeout in %0.3fms", duration * 0.000001f); |
| 1984 | #endif |
| 1985 | } |
| 1986 | |
| 1987 | void VibratorInputMapper::stopVibrating() { |
| 1988 | mVibrating = false; |
| 1989 | #if DEBUG_VIBRATOR |
| 1990 | ALOGD("stopVibrating: sending cancel vibrate deviceId=%d", getDeviceId()); |
| 1991 | #endif |
| 1992 | getEventHub()->cancelVibrate(getDeviceId()); |
| 1993 | } |
| 1994 | |
| 1995 | void VibratorInputMapper::dump(String8& dump) { |
| 1996 | dump.append(INDENT2 "Vibrator Input Mapper:\n"); |
| 1997 | dump.appendFormat(INDENT3 "Vibrating: %s\n", toString(mVibrating)); |
| 1998 | } |
| 1999 | |
| 2000 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2001 | // --- KeyboardInputMapper --- |
| 2002 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2003 | KeyboardInputMapper::KeyboardInputMapper(InputDevice* device, |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2004 | uint32_t source, int32_t keyboardType) : |
| 2005 | InputMapper(device), mSource(source), |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2006 | mKeyboardType(keyboardType) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2007 | } |
| 2008 | |
| 2009 | KeyboardInputMapper::~KeyboardInputMapper() { |
| 2010 | } |
| 2011 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2012 | uint32_t KeyboardInputMapper::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 | |
| 2016 | void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 2017 | InputMapper::populateDeviceInfo(info); |
| 2018 | |
| 2019 | info->setKeyboardType(mKeyboardType); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 2020 | info->setKeyCharacterMap(getEventHub()->getKeyCharacterMap(getDeviceId())); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2021 | } |
| 2022 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2023 | void KeyboardInputMapper::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2024 | dump.append(INDENT2 "Keyboard Input Mapper:\n"); |
| 2025 | dumpParameters(dump); |
| 2026 | dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2027 | dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2028 | dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size()); |
| 2029 | dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState); |
| 2030 | dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2031 | } |
| 2032 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2033 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2034 | void KeyboardInputMapper::configure(nsecs_t when, |
| 2035 | const InputReaderConfiguration* config, uint32_t changes) { |
| 2036 | InputMapper::configure(when, config, changes); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2037 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2038 | if (!changes) { // first time only |
| 2039 | // Configure basic parameters. |
| 2040 | configureParameters(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2041 | } |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2042 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2043 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) { |
| 2044 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) { |
| 2045 | if (!config->getDisplayInfo(mParameters.associatedDisplayId, |
| 2046 | false /*external*/, NULL, NULL, &mOrientation)) { |
| 2047 | mOrientation = DISPLAY_ORIENTATION_0; |
| 2048 | } |
| 2049 | } else { |
| 2050 | mOrientation = DISPLAY_ORIENTATION_0; |
| 2051 | } |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2052 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | void KeyboardInputMapper::configureParameters() { |
| 2056 | mParameters.orientationAware = false; |
| 2057 | getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"), |
| 2058 | mParameters.orientationAware); |
| 2059 | |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2060 | mParameters.associatedDisplayId = -1; |
| 2061 | if (mParameters.orientationAware) { |
| 2062 | mParameters.associatedDisplayId = 0; |
| 2063 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | void KeyboardInputMapper::dumpParameters(String8& dump) { |
| 2067 | dump.append(INDENT3 "Parameters:\n"); |
| 2068 | dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n", |
| 2069 | mParameters.associatedDisplayId); |
| 2070 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 2071 | toString(mParameters.orientationAware)); |
| 2072 | } |
| 2073 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2074 | void KeyboardInputMapper::reset(nsecs_t when) { |
| 2075 | mMetaState = AMETA_NONE; |
| 2076 | mDownTime = 0; |
| 2077 | mKeyDowns.clear(); |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 2078 | mCurrentHidUsage = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2079 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2080 | resetLedState(); |
| 2081 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2082 | InputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2083 | } |
| 2084 | |
| 2085 | void KeyboardInputMapper::process(const RawEvent* rawEvent) { |
| 2086 | switch (rawEvent->type) { |
| 2087 | case EV_KEY: { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 2088 | int32_t scanCode = rawEvent->code; |
| 2089 | int32_t usageCode = mCurrentHidUsage; |
| 2090 | mCurrentHidUsage = 0; |
| 2091 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2092 | if (isKeyboardOrGamepadKey(scanCode)) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 2093 | int32_t keyCode; |
| 2094 | uint32_t flags; |
| 2095 | if (getEventHub()->mapKey(getDeviceId(), scanCode, usageCode, &keyCode, &flags)) { |
| 2096 | keyCode = AKEYCODE_UNKNOWN; |
| 2097 | flags = 0; |
| 2098 | } |
| 2099 | processKey(rawEvent->when, rawEvent->value != 0, keyCode, scanCode, flags); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2100 | } |
| 2101 | break; |
| 2102 | } |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 2103 | case EV_MSC: { |
| 2104 | if (rawEvent->code == MSC_SCAN) { |
| 2105 | mCurrentHidUsage = rawEvent->value; |
| 2106 | } |
| 2107 | break; |
| 2108 | } |
| 2109 | case EV_SYN: { |
| 2110 | if (rawEvent->code == SYN_REPORT) { |
| 2111 | mCurrentHidUsage = 0; |
| 2112 | } |
| 2113 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2114 | } |
| 2115 | } |
| 2116 | |
| 2117 | bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) { |
| 2118 | return scanCode < BTN_MOUSE |
| 2119 | || scanCode >= KEY_OK |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 2120 | || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE) |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2121 | || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2122 | } |
| 2123 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2124 | void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode, |
| 2125 | int32_t scanCode, uint32_t policyFlags) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2126 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2127 | if (down) { |
| 2128 | // Rotate key codes according to orientation if needed. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2129 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2130 | keyCode = rotateKeyCode(keyCode, mOrientation); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2131 | } |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 2132 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2133 | // Add key down. |
| 2134 | ssize_t keyDownIndex = findKeyDown(scanCode); |
| 2135 | if (keyDownIndex >= 0) { |
| 2136 | // key repeat, be sure to use same keycode as before in case of rotation |
| 2137 | keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2138 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2139 | // key down |
| 2140 | if ((policyFlags & POLICY_FLAG_VIRTUAL) |
| 2141 | && mContext->shouldDropVirtualKey(when, |
| 2142 | getDevice(), keyCode, scanCode)) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2143 | return; |
| 2144 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2145 | |
| 2146 | mKeyDowns.push(); |
| 2147 | KeyDown& keyDown = mKeyDowns.editTop(); |
| 2148 | keyDown.keyCode = keyCode; |
| 2149 | keyDown.scanCode = scanCode; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2150 | } |
| 2151 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2152 | mDownTime = when; |
| 2153 | } else { |
| 2154 | // Remove key down. |
| 2155 | ssize_t keyDownIndex = findKeyDown(scanCode); |
| 2156 | if (keyDownIndex >= 0) { |
| 2157 | // key up, be sure to use same keycode as before in case of rotation |
| 2158 | keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode; |
| 2159 | mKeyDowns.removeAt(size_t(keyDownIndex)); |
| 2160 | } else { |
| 2161 | // key was not actually down |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2162 | 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] | 2163 | "keyCode=%d, scanCode=%d", |
| 2164 | getDeviceName().string(), keyCode, scanCode); |
| 2165 | return; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2166 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2167 | } |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 2168 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2169 | bool metaStateChanged = false; |
| 2170 | int32_t oldMetaState = mMetaState; |
| 2171 | int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState); |
| 2172 | if (oldMetaState != newMetaState) { |
| 2173 | mMetaState = newMetaState; |
| 2174 | metaStateChanged = true; |
| 2175 | updateLedState(false); |
| 2176 | } |
| 2177 | |
| 2178 | nsecs_t downTime = mDownTime; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2179 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2180 | // Key down on external an keyboard should wake the device. |
| 2181 | // We don't do this for internal keyboards to prevent them from waking up in your pocket. |
| 2182 | // For internal keyboards, the key layout file should specify the policy flags for |
| 2183 | // each wake key individually. |
| 2184 | // TODO: Use the input device configuration to control this behavior more finely. |
| 2185 | if (down && getDevice()->isExternal() |
| 2186 | && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) { |
| 2187 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 2188 | } |
| 2189 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2190 | if (metaStateChanged) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2191 | getContext()->updateGlobalMetaState(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2192 | } |
| 2193 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2194 | if (down && !isMetaKey(keyCode)) { |
| 2195 | getContext()->fadePointer(); |
| 2196 | } |
| 2197 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2198 | NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2199 | down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, |
| 2200 | AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2201 | getListener()->notifyKey(&args); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2202 | } |
| 2203 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2204 | ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) { |
| 2205 | size_t n = mKeyDowns.size(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2206 | for (size_t i = 0; i < n; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2207 | if (mKeyDowns[i].scanCode == scanCode) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2208 | return i; |
| 2209 | } |
| 2210 | } |
| 2211 | return -1; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2212 | } |
| 2213 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2214 | int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 2215 | return getEventHub()->getKeyCodeState(getDeviceId(), keyCode); |
| 2216 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2217 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2218 | int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 2219 | return getEventHub()->getScanCodeState(getDeviceId(), scanCode); |
| 2220 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2221 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2222 | bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 2223 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 2224 | return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags); |
| 2225 | } |
| 2226 | |
| 2227 | int32_t KeyboardInputMapper::getMetaState() { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2228 | return mMetaState; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2229 | } |
| 2230 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2231 | void KeyboardInputMapper::resetLedState() { |
| 2232 | initializeLedState(mCapsLockLedState, LED_CAPSL); |
| 2233 | initializeLedState(mNumLockLedState, LED_NUML); |
| 2234 | initializeLedState(mScrollLockLedState, LED_SCROLLL); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2235 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2236 | updateLedState(true); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2237 | } |
| 2238 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2239 | void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) { |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2240 | ledState.avail = getEventHub()->hasLed(getDeviceId(), led); |
| 2241 | ledState.on = false; |
| 2242 | } |
| 2243 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2244 | void KeyboardInputMapper::updateLedState(bool reset) { |
| 2245 | updateLedStateForModifier(mCapsLockLedState, LED_CAPSL, |
Jeff Brown | 51e7fe75 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 2246 | AMETA_CAPS_LOCK_ON, reset); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2247 | updateLedStateForModifier(mNumLockLedState, LED_NUML, |
Jeff Brown | 51e7fe75 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 2248 | AMETA_NUM_LOCK_ON, reset); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2249 | updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL, |
Jeff Brown | 51e7fe75 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 2250 | AMETA_SCROLL_LOCK_ON, reset); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 2251 | } |
| 2252 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2253 | void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState, |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 2254 | int32_t led, int32_t modifier, bool reset) { |
| 2255 | if (ledState.avail) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2256 | bool desiredState = (mMetaState & modifier) != 0; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2257 | if (reset || ledState.on != desiredState) { |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 2258 | getEventHub()->setLedState(getDeviceId(), led, desiredState); |
| 2259 | ledState.on = desiredState; |
| 2260 | } |
| 2261 | } |
| 2262 | } |
| 2263 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2264 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2265 | // --- CursorInputMapper --- |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2266 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2267 | CursorInputMapper::CursorInputMapper(InputDevice* device) : |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2268 | InputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2269 | } |
| 2270 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2271 | CursorInputMapper::~CursorInputMapper() { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2272 | } |
| 2273 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2274 | uint32_t CursorInputMapper::getSources() { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2275 | return mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2276 | } |
| 2277 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2278 | void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2279 | InputMapper::populateDeviceInfo(info); |
| 2280 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2281 | if (mParameters.mode == Parameters::MODE_POINTER) { |
| 2282 | float minX, minY, maxX, maxY; |
| 2283 | if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2284 | info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f); |
| 2285 | 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] | 2286 | } |
| 2287 | } else { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2288 | info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale); |
| 2289 | 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] | 2290 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2291 | 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] | 2292 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2293 | if (mCursorScrollAccumulator.haveRelativeVWheel()) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2294 | 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] | 2295 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2296 | if (mCursorScrollAccumulator.haveRelativeHWheel()) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2297 | 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] | 2298 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2299 | } |
| 2300 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2301 | void CursorInputMapper::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2302 | dump.append(INDENT2 "Cursor Input Mapper:\n"); |
| 2303 | dumpParameters(dump); |
| 2304 | dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale); |
| 2305 | dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale); |
| 2306 | dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision); |
| 2307 | dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision); |
| 2308 | dump.appendFormat(INDENT3 "HaveVWheel: %s\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2309 | toString(mCursorScrollAccumulator.haveRelativeVWheel())); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2310 | dump.appendFormat(INDENT3 "HaveHWheel: %s\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2311 | toString(mCursorScrollAccumulator.haveRelativeHWheel())); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2312 | dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale); |
| 2313 | dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2314 | dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2315 | dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState); |
| 2316 | dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState))); |
| 2317 | dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2318 | } |
| 2319 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2320 | void CursorInputMapper::configure(nsecs_t when, |
| 2321 | const InputReaderConfiguration* config, uint32_t changes) { |
| 2322 | InputMapper::configure(when, config, changes); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2323 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2324 | if (!changes) { // first time only |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2325 | mCursorScrollAccumulator.configure(getDevice()); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2326 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2327 | // Configure basic parameters. |
| 2328 | configureParameters(); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2329 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2330 | // Configure device mode. |
| 2331 | switch (mParameters.mode) { |
| 2332 | case Parameters::MODE_POINTER: |
| 2333 | mSource = AINPUT_SOURCE_MOUSE; |
| 2334 | mXPrecision = 1.0f; |
| 2335 | mYPrecision = 1.0f; |
| 2336 | mXScale = 1.0f; |
| 2337 | mYScale = 1.0f; |
| 2338 | mPointerController = getPolicy()->obtainPointerController(getDeviceId()); |
| 2339 | break; |
| 2340 | case Parameters::MODE_NAVIGATION: |
| 2341 | mSource = AINPUT_SOURCE_TRACKBALL; |
| 2342 | mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 2343 | mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 2344 | mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 2345 | mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 2346 | break; |
| 2347 | } |
| 2348 | |
| 2349 | mVWheelScale = 1.0f; |
| 2350 | mHWheelScale = 1.0f; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2351 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 2352 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2353 | if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) { |
| 2354 | mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters); |
| 2355 | mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters); |
| 2356 | mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters); |
| 2357 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2358 | |
| 2359 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) { |
| 2360 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) { |
| 2361 | if (!config->getDisplayInfo(mParameters.associatedDisplayId, |
| 2362 | false /*external*/, NULL, NULL, &mOrientation)) { |
| 2363 | mOrientation = DISPLAY_ORIENTATION_0; |
| 2364 | } |
| 2365 | } else { |
| 2366 | mOrientation = DISPLAY_ORIENTATION_0; |
| 2367 | } |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 2368 | bumpGeneration(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2369 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2370 | } |
| 2371 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2372 | void CursorInputMapper::configureParameters() { |
| 2373 | mParameters.mode = Parameters::MODE_POINTER; |
| 2374 | String8 cursorModeString; |
| 2375 | if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) { |
| 2376 | if (cursorModeString == "navigation") { |
| 2377 | mParameters.mode = Parameters::MODE_NAVIGATION; |
| 2378 | } else if (cursorModeString != "pointer" && cursorModeString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2379 | ALOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string()); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2380 | } |
| 2381 | } |
| 2382 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2383 | mParameters.orientationAware = false; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2384 | getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"), |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2385 | mParameters.orientationAware); |
| 2386 | |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2387 | mParameters.associatedDisplayId = -1; |
| 2388 | if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) { |
| 2389 | mParameters.associatedDisplayId = 0; |
| 2390 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2391 | } |
| 2392 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2393 | void CursorInputMapper::dumpParameters(String8& dump) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2394 | dump.append(INDENT3 "Parameters:\n"); |
| 2395 | dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n", |
| 2396 | mParameters.associatedDisplayId); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2397 | |
| 2398 | switch (mParameters.mode) { |
| 2399 | case Parameters::MODE_POINTER: |
| 2400 | dump.append(INDENT4 "Mode: pointer\n"); |
| 2401 | break; |
| 2402 | case Parameters::MODE_NAVIGATION: |
| 2403 | dump.append(INDENT4 "Mode: navigation\n"); |
| 2404 | break; |
| 2405 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 2406 | ALOG_ASSERT(false); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2407 | } |
| 2408 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2409 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 2410 | toString(mParameters.orientationAware)); |
| 2411 | } |
| 2412 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2413 | void CursorInputMapper::reset(nsecs_t when) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2414 | mButtonState = 0; |
| 2415 | mDownTime = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2416 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2417 | mPointerVelocityControl.reset(); |
| 2418 | mWheelXVelocityControl.reset(); |
| 2419 | mWheelYVelocityControl.reset(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2420 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2421 | mCursorButtonAccumulator.reset(getDevice()); |
| 2422 | mCursorMotionAccumulator.reset(getDevice()); |
| 2423 | mCursorScrollAccumulator.reset(getDevice()); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2424 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2425 | InputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2426 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2427 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2428 | void CursorInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2429 | mCursorButtonAccumulator.process(rawEvent); |
| 2430 | mCursorMotionAccumulator.process(rawEvent); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2431 | mCursorScrollAccumulator.process(rawEvent); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2432 | |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 2433 | if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2434 | sync(rawEvent->when); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2435 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2436 | } |
| 2437 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2438 | void CursorInputMapper::sync(nsecs_t when) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2439 | int32_t lastButtonState = mButtonState; |
| 2440 | int32_t currentButtonState = mCursorButtonAccumulator.getButtonState(); |
| 2441 | mButtonState = currentButtonState; |
| 2442 | |
| 2443 | bool wasDown = isPointerDown(lastButtonState); |
| 2444 | bool down = isPointerDown(currentButtonState); |
| 2445 | bool downChanged; |
| 2446 | if (!wasDown && down) { |
| 2447 | mDownTime = when; |
| 2448 | downChanged = true; |
| 2449 | } else if (wasDown && !down) { |
| 2450 | downChanged = true; |
| 2451 | } else { |
| 2452 | downChanged = false; |
| 2453 | } |
| 2454 | nsecs_t downTime = mDownTime; |
| 2455 | bool buttonsChanged = currentButtonState != lastButtonState; |
Jeff Brown | c28306a | 2011-08-23 21:32:42 -0700 | [diff] [blame] | 2456 | bool buttonsPressed = currentButtonState & ~lastButtonState; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2457 | |
| 2458 | float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale; |
| 2459 | float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale; |
| 2460 | bool moved = deltaX != 0 || deltaY != 0; |
| 2461 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2462 | // Rotate delta according to orientation if needed. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2463 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0 |
| 2464 | && (deltaX != 0.0f || deltaY != 0.0f)) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2465 | rotateDelta(mOrientation, &deltaX, &deltaY); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2466 | } |
| 2467 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2468 | // Move the pointer. |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2469 | PointerProperties pointerProperties; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2470 | pointerProperties.clear(); |
| 2471 | pointerProperties.id = 0; |
| 2472 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE; |
| 2473 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2474 | PointerCoords pointerCoords; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2475 | pointerCoords.clear(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2476 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2477 | float vscroll = mCursorScrollAccumulator.getRelativeVWheel(); |
| 2478 | float hscroll = mCursorScrollAccumulator.getRelativeHWheel(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2479 | bool scrolled = vscroll != 0 || hscroll != 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2480 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2481 | mWheelYVelocityControl.move(when, NULL, &vscroll); |
| 2482 | mWheelXVelocityControl.move(when, &hscroll, NULL); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2483 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2484 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2485 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2486 | if (mPointerController != NULL) { |
| 2487 | if (moved || scrolled || buttonsChanged) { |
| 2488 | mPointerController->setPresentation( |
| 2489 | PointerControllerInterface::PRESENTATION_POINTER); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2490 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2491 | if (moved) { |
| 2492 | mPointerController->move(deltaX, deltaY); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2493 | } |
| 2494 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2495 | if (buttonsChanged) { |
| 2496 | mPointerController->setButtonState(currentButtonState); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2497 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2498 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2499 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2500 | } |
| 2501 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2502 | float x, y; |
| 2503 | mPointerController->getPosition(&x, &y); |
| 2504 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 2505 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 2506 | } else { |
| 2507 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX); |
| 2508 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY); |
| 2509 | } |
| 2510 | |
| 2511 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2512 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2513 | // Moving an external trackball or mouse should wake the device. |
| 2514 | // We don't do this for internal cursor devices to prevent them from waking up |
| 2515 | // the device in your pocket. |
| 2516 | // TODO: Use the input device configuration to control this behavior more finely. |
| 2517 | uint32_t policyFlags = 0; |
Jeff Brown | c28306a | 2011-08-23 21:32:42 -0700 | [diff] [blame] | 2518 | if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) { |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2519 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 2520 | } |
| 2521 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2522 | // Synthesize key down from buttons if needed. |
| 2523 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource, |
| 2524 | policyFlags, lastButtonState, currentButtonState); |
| 2525 | |
| 2526 | // Send motion event. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2527 | if (downChanged || moved || scrolled || buttonsChanged) { |
| 2528 | int32_t metaState = mContext->getGlobalMetaState(); |
| 2529 | int32_t motionEventAction; |
| 2530 | if (downChanged) { |
| 2531 | motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP; |
| 2532 | } else if (down || mPointerController == NULL) { |
| 2533 | motionEventAction = AMOTION_EVENT_ACTION_MOVE; |
| 2534 | } else { |
| 2535 | motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE; |
| 2536 | } |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2537 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2538 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 2539 | motionEventAction, 0, metaState, currentButtonState, 0, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2540 | 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2541 | getListener()->notifyMotion(&args); |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2542 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2543 | // Send hover move after UP to tell the application that the mouse is hovering now. |
| 2544 | if (motionEventAction == AMOTION_EVENT_ACTION_UP |
| 2545 | && mPointerController != NULL) { |
| 2546 | NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags, |
| 2547 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 2548 | metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 2549 | 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime); |
| 2550 | getListener()->notifyMotion(&hoverArgs); |
| 2551 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2552 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2553 | // Send scroll events. |
| 2554 | if (scrolled) { |
| 2555 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll); |
| 2556 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll); |
| 2557 | |
| 2558 | NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags, |
| 2559 | AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState, |
| 2560 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 2561 | 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime); |
| 2562 | getListener()->notifyMotion(&scrollArgs); |
| 2563 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2564 | } |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2565 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2566 | // Synthesize key up from buttons if needed. |
| 2567 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource, |
| 2568 | policyFlags, lastButtonState, currentButtonState); |
| 2569 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2570 | mCursorMotionAccumulator.finishSync(); |
| 2571 | mCursorScrollAccumulator.finishSync(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2572 | } |
| 2573 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2574 | int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Jeff Brown | c3fc2d0 | 2010-08-10 15:47:53 -0700 | [diff] [blame] | 2575 | if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) { |
| 2576 | return getEventHub()->getScanCodeState(getDeviceId(), scanCode); |
| 2577 | } else { |
| 2578 | return AKEY_STATE_UNKNOWN; |
| 2579 | } |
| 2580 | } |
| 2581 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2582 | void CursorInputMapper::fadePointer() { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2583 | if (mPointerController != NULL) { |
| 2584 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 2585 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2586 | } |
| 2587 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2588 | |
| 2589 | // --- TouchInputMapper --- |
| 2590 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2591 | TouchInputMapper::TouchInputMapper(InputDevice* device) : |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2592 | InputMapper(device), |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2593 | mSource(0), mDeviceMode(DEVICE_MODE_DISABLED), |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2594 | mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | TouchInputMapper::~TouchInputMapper() { |
| 2598 | } |
| 2599 | |
| 2600 | uint32_t TouchInputMapper::getSources() { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2601 | return mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2602 | } |
| 2603 | |
| 2604 | void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 2605 | InputMapper::populateDeviceInfo(info); |
| 2606 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2607 | if (mDeviceMode != DEVICE_MODE_DISABLED) { |
| 2608 | info->addMotionRange(mOrientedRanges.x); |
| 2609 | info->addMotionRange(mOrientedRanges.y); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2610 | info->addMotionRange(mOrientedRanges.pressure); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2611 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2612 | if (mOrientedRanges.haveSize) { |
| 2613 | info->addMotionRange(mOrientedRanges.size); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2614 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2615 | |
| 2616 | if (mOrientedRanges.haveTouchSize) { |
| 2617 | info->addMotionRange(mOrientedRanges.touchMajor); |
| 2618 | info->addMotionRange(mOrientedRanges.touchMinor); |
| 2619 | } |
| 2620 | |
| 2621 | if (mOrientedRanges.haveToolSize) { |
| 2622 | info->addMotionRange(mOrientedRanges.toolMajor); |
| 2623 | info->addMotionRange(mOrientedRanges.toolMinor); |
| 2624 | } |
| 2625 | |
| 2626 | if (mOrientedRanges.haveOrientation) { |
| 2627 | info->addMotionRange(mOrientedRanges.orientation); |
| 2628 | } |
| 2629 | |
| 2630 | if (mOrientedRanges.haveDistance) { |
| 2631 | info->addMotionRange(mOrientedRanges.distance); |
| 2632 | } |
| 2633 | |
| 2634 | if (mOrientedRanges.haveTilt) { |
| 2635 | info->addMotionRange(mOrientedRanges.tilt); |
| 2636 | } |
| 2637 | |
| 2638 | if (mCursorScrollAccumulator.haveRelativeVWheel()) { |
| 2639 | info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f); |
| 2640 | } |
| 2641 | if (mCursorScrollAccumulator.haveRelativeHWheel()) { |
| 2642 | info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f); |
| 2643 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2644 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2645 | } |
| 2646 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2647 | void TouchInputMapper::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2648 | dump.append(INDENT2 "Touch Input Mapper:\n"); |
| 2649 | dumpParameters(dump); |
| 2650 | dumpVirtualKeys(dump); |
| 2651 | dumpRawPointerAxes(dump); |
| 2652 | dumpCalibration(dump); |
| 2653 | dumpSurface(dump); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2654 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2655 | dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n"); |
| 2656 | dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale); |
| 2657 | dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale); |
| 2658 | dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision); |
| 2659 | dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision); |
| 2660 | dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2661 | dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale); |
| 2662 | dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2663 | dump.appendFormat(INDENT4 "OrientationCenter: %0.3f\n", mOrientationCenter); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2664 | dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale); |
| 2665 | dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2666 | dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt)); |
| 2667 | dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter); |
| 2668 | dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale); |
| 2669 | dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter); |
| 2670 | dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2671 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2672 | dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2673 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2674 | dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n", |
| 2675 | mLastRawPointerData.pointerCount); |
| 2676 | for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) { |
| 2677 | const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i]; |
| 2678 | dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, " |
| 2679 | "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2680 | "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, " |
| 2681 | "toolType=%d, isHovering=%s\n", i, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2682 | pointer.id, pointer.x, pointer.y, pointer.pressure, |
| 2683 | pointer.touchMajor, pointer.touchMinor, |
| 2684 | pointer.toolMajor, pointer.toolMinor, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2685 | pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2686 | pointer.toolType, toString(pointer.isHovering)); |
| 2687 | } |
| 2688 | |
| 2689 | dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n", |
| 2690 | mLastCookedPointerData.pointerCount); |
| 2691 | for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) { |
| 2692 | const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i]; |
| 2693 | const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i]; |
| 2694 | dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, " |
| 2695 | "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2696 | "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, " |
| 2697 | "toolType=%d, isHovering=%s\n", i, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2698 | pointerProperties.id, |
| 2699 | pointerCoords.getX(), |
| 2700 | pointerCoords.getY(), |
| 2701 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 2702 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 2703 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 2704 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 2705 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 2706 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2707 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT), |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2708 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), |
| 2709 | pointerProperties.toolType, |
| 2710 | toString(mLastCookedPointerData.isHovering(i))); |
| 2711 | } |
| 2712 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2713 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2714 | dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n"); |
| 2715 | dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2716 | mPointerXMovementScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2717 | dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2718 | mPointerYMovementScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2719 | dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2720 | mPointerXZoomScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2721 | dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2722 | mPointerYZoomScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2723 | dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n", |
| 2724 | mPointerGestureMaxSwipeWidth); |
| 2725 | } |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2726 | } |
| 2727 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2728 | void TouchInputMapper::configure(nsecs_t when, |
| 2729 | const InputReaderConfiguration* config, uint32_t changes) { |
| 2730 | InputMapper::configure(when, config, changes); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2731 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2732 | mConfig = *config; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2733 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2734 | if (!changes) { // first time only |
| 2735 | // Configure basic parameters. |
| 2736 | configureParameters(); |
| 2737 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2738 | // Configure common accumulators. |
| 2739 | mCursorScrollAccumulator.configure(getDevice()); |
| 2740 | mTouchButtonAccumulator.configure(getDevice()); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2741 | |
| 2742 | // Configure absolute axis information. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2743 | configureRawPointerAxes(); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2744 | |
| 2745 | // Prepare input device calibration. |
| 2746 | parseCalibration(); |
| 2747 | resolveCalibration(); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2748 | } |
| 2749 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2750 | if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2751 | // Update pointer speed. |
| 2752 | mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters); |
| 2753 | mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters); |
| 2754 | mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2755 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2756 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2757 | bool resetNeeded = false; |
| 2758 | if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 2759 | | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT |
| 2760 | | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2761 | // Configure device sources, surface dimensions, orientation and |
| 2762 | // scaling factors. |
| 2763 | configureSurface(when, &resetNeeded); |
| 2764 | } |
| 2765 | |
| 2766 | if (changes && resetNeeded) { |
| 2767 | // Send reset, unless this is the first time the device has been configured, |
| 2768 | // in which case the reader will call reset itself after all mappers are ready. |
| 2769 | getDevice()->notifyReset(when); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2770 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2771 | } |
| 2772 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2773 | void TouchInputMapper::configureParameters() { |
Jeff Brown | b126822 | 2011-06-03 17:06:16 -0700 | [diff] [blame] | 2774 | // Use the pointer presentation mode for devices that do not support distinct |
| 2775 | // multitouch. The spot-based presentation relies on being able to accurately |
| 2776 | // locate two or more fingers on the touch pad. |
| 2777 | mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT) |
| 2778 | ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2779 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2780 | String8 gestureModeString; |
| 2781 | if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"), |
| 2782 | gestureModeString)) { |
| 2783 | if (gestureModeString == "pointer") { |
| 2784 | mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER; |
| 2785 | } else if (gestureModeString == "spots") { |
| 2786 | mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS; |
| 2787 | } else if (gestureModeString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2788 | ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string()); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2789 | } |
| 2790 | } |
| 2791 | |
Jeff Brown | deffe07 | 2011-08-26 18:38:46 -0700 | [diff] [blame] | 2792 | if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) { |
| 2793 | // The device is a touch screen. |
| 2794 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
| 2795 | } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) { |
| 2796 | // The device is a pointing device like a track pad. |
| 2797 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
| 2798 | } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X) |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2799 | || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) { |
| 2800 | // The device is a cursor device with a touch pad attached. |
| 2801 | // By default don't use the touch pad to move the pointer. |
| 2802 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; |
| 2803 | } else { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2804 | // The device is a touch pad of unknown purpose. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2805 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
| 2806 | } |
| 2807 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2808 | String8 deviceTypeString; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2809 | if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"), |
| 2810 | deviceTypeString)) { |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 2811 | if (deviceTypeString == "touchScreen") { |
| 2812 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2813 | } else if (deviceTypeString == "touchPad") { |
| 2814 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2815 | } else if (deviceTypeString == "pointer") { |
| 2816 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2817 | } else if (deviceTypeString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2818 | ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string()); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2819 | } |
| 2820 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2821 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2822 | mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2823 | getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"), |
| 2824 | mParameters.orientationAware); |
| 2825 | |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2826 | mParameters.associatedDisplayId = -1; |
| 2827 | mParameters.associatedDisplayIsExternal = false; |
| 2828 | if (mParameters.orientationAware |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2829 | || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2830 | || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) { |
| 2831 | mParameters.associatedDisplayIsExternal = |
| 2832 | mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN |
| 2833 | && getDevice()->isExternal(); |
| 2834 | mParameters.associatedDisplayId = 0; |
| 2835 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2836 | } |
| 2837 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2838 | void TouchInputMapper::dumpParameters(String8& dump) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2839 | dump.append(INDENT3 "Parameters:\n"); |
| 2840 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2841 | switch (mParameters.gestureMode) { |
| 2842 | case Parameters::GESTURE_MODE_POINTER: |
| 2843 | dump.append(INDENT4 "GestureMode: pointer\n"); |
| 2844 | break; |
| 2845 | case Parameters::GESTURE_MODE_SPOTS: |
| 2846 | dump.append(INDENT4 "GestureMode: spots\n"); |
| 2847 | break; |
| 2848 | default: |
| 2849 | assert(false); |
| 2850 | } |
| 2851 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2852 | switch (mParameters.deviceType) { |
| 2853 | case Parameters::DEVICE_TYPE_TOUCH_SCREEN: |
| 2854 | dump.append(INDENT4 "DeviceType: touchScreen\n"); |
| 2855 | break; |
| 2856 | case Parameters::DEVICE_TYPE_TOUCH_PAD: |
| 2857 | dump.append(INDENT4 "DeviceType: touchPad\n"); |
| 2858 | break; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2859 | case Parameters::DEVICE_TYPE_POINTER: |
| 2860 | dump.append(INDENT4 "DeviceType: pointer\n"); |
| 2861 | break; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2862 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 2863 | ALOG_ASSERT(false); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2864 | } |
| 2865 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2866 | dump.appendFormat(INDENT4 "AssociatedDisplay: id=%d, isExternal=%s\n", |
| 2867 | mParameters.associatedDisplayId, toString(mParameters.associatedDisplayIsExternal)); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2868 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 2869 | toString(mParameters.orientationAware)); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2870 | } |
| 2871 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2872 | void TouchInputMapper::configureRawPointerAxes() { |
| 2873 | mRawPointerAxes.clear(); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2874 | } |
| 2875 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2876 | void TouchInputMapper::dumpRawPointerAxes(String8& dump) { |
| 2877 | dump.append(INDENT3 "Raw Touch Axes:\n"); |
| 2878 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X"); |
| 2879 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y"); |
| 2880 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure"); |
| 2881 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor"); |
| 2882 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor"); |
| 2883 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor"); |
| 2884 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor"); |
| 2885 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation"); |
| 2886 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance"); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2887 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX"); |
| 2888 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY"); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2889 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId"); |
| 2890 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot"); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2891 | } |
| 2892 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2893 | void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) { |
| 2894 | int32_t oldDeviceMode = mDeviceMode; |
| 2895 | |
| 2896 | // Determine device mode. |
| 2897 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER |
| 2898 | && mConfig.pointerGesturesEnabled) { |
| 2899 | mSource = AINPUT_SOURCE_MOUSE; |
| 2900 | mDeviceMode = DEVICE_MODE_POINTER; |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 2901 | if (hasStylus()) { |
| 2902 | mSource |= AINPUT_SOURCE_STYLUS; |
| 2903 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2904 | } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN |
| 2905 | && mParameters.associatedDisplayId >= 0) { |
| 2906 | mSource = AINPUT_SOURCE_TOUCHSCREEN; |
| 2907 | mDeviceMode = DEVICE_MODE_DIRECT; |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 2908 | if (hasStylus()) { |
| 2909 | mSource |= AINPUT_SOURCE_STYLUS; |
| 2910 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2911 | } else { |
| 2912 | mSource = AINPUT_SOURCE_TOUCHPAD; |
| 2913 | mDeviceMode = DEVICE_MODE_UNSCALED; |
| 2914 | } |
| 2915 | |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2916 | // Ensure we have valid X and Y axes. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2917 | if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2918 | 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] | 2919 | "The device will be inoperable.", getDeviceName().string()); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2920 | mDeviceMode = DEVICE_MODE_DISABLED; |
| 2921 | return; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2922 | } |
| 2923 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2924 | // Get associated display dimensions. |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2925 | if (mParameters.associatedDisplayId >= 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2926 | if (!mConfig.getDisplayInfo(mParameters.associatedDisplayId, |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2927 | mParameters.associatedDisplayIsExternal, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2928 | &mAssociatedDisplayWidth, &mAssociatedDisplayHeight, |
| 2929 | &mAssociatedDisplayOrientation)) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2930 | 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] | 2931 | "display %d. The device will be inoperable until the display size " |
| 2932 | "becomes available.", |
| 2933 | getDeviceName().string(), mParameters.associatedDisplayId); |
| 2934 | mDeviceMode = DEVICE_MODE_DISABLED; |
| 2935 | return; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2936 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2937 | } |
| 2938 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2939 | // Configure dimensions. |
| 2940 | int32_t width, height, orientation; |
| 2941 | if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) { |
| 2942 | width = mAssociatedDisplayWidth; |
| 2943 | height = mAssociatedDisplayHeight; |
| 2944 | orientation = mParameters.orientationAware ? |
| 2945 | mAssociatedDisplayOrientation : DISPLAY_ORIENTATION_0; |
| 2946 | } else { |
| 2947 | width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1; |
| 2948 | height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1; |
| 2949 | orientation = DISPLAY_ORIENTATION_0; |
| 2950 | } |
| 2951 | |
| 2952 | // If moving between pointer modes, need to reset some state. |
| 2953 | bool deviceModeChanged; |
| 2954 | if (mDeviceMode != oldDeviceMode) { |
| 2955 | deviceModeChanged = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2956 | mOrientedRanges.clear(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2957 | } |
| 2958 | |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 2959 | // Create pointer controller if needed. |
| 2960 | if (mDeviceMode == DEVICE_MODE_POINTER || |
| 2961 | (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) { |
| 2962 | if (mPointerController == NULL) { |
| 2963 | mPointerController = getPolicy()->obtainPointerController(getDeviceId()); |
| 2964 | } |
| 2965 | } else { |
| 2966 | mPointerController.clear(); |
| 2967 | } |
| 2968 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2969 | bool orientationChanged = mSurfaceOrientation != orientation; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2970 | if (orientationChanged) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2971 | mSurfaceOrientation = orientation; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2972 | } |
| 2973 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2974 | bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2975 | if (sizeChanged || deviceModeChanged) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2976 | 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] | 2977 | getDeviceId(), getDeviceName().string(), width, height, mDeviceMode); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2978 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2979 | mSurfaceWidth = width; |
| 2980 | mSurfaceHeight = height; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2981 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2982 | // Configure X and Y factors. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2983 | mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1); |
| 2984 | mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1); |
| 2985 | mXPrecision = 1.0f / mXScale; |
| 2986 | mYPrecision = 1.0f / mYScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2987 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2988 | mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2989 | mOrientedRanges.x.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2990 | mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2991 | mOrientedRanges.y.source = mSource; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2992 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2993 | configureVirtualKeys(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2994 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2995 | // Scale factor for terms that are not oriented in a particular axis. |
| 2996 | // If the pixels are square then xScale == yScale otherwise we fake it |
| 2997 | // by choosing an average. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2998 | mGeometricScale = avg(mXScale, mYScale); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2999 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3000 | // Size of diagonal axis. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3001 | float diagonalSize = hypotf(width, height); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3002 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3003 | // Size factors. |
| 3004 | if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) { |
| 3005 | if (mRawPointerAxes.touchMajor.valid |
| 3006 | && mRawPointerAxes.touchMajor.maxValue != 0) { |
| 3007 | mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue; |
| 3008 | } else if (mRawPointerAxes.toolMajor.valid |
| 3009 | && mRawPointerAxes.toolMajor.maxValue != 0) { |
| 3010 | mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue; |
| 3011 | } else { |
| 3012 | mSizeScale = 0.0f; |
| 3013 | } |
| 3014 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3015 | mOrientedRanges.haveTouchSize = true; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3016 | mOrientedRanges.haveToolSize = true; |
| 3017 | mOrientedRanges.haveSize = true; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 3018 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3019 | mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3020 | mOrientedRanges.touchMajor.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3021 | mOrientedRanges.touchMajor.min = 0; |
| 3022 | mOrientedRanges.touchMajor.max = diagonalSize; |
| 3023 | mOrientedRanges.touchMajor.flat = 0; |
| 3024 | mOrientedRanges.touchMajor.fuzz = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 3025 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3026 | mOrientedRanges.touchMinor = mOrientedRanges.touchMajor; |
| 3027 | mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 3028 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3029 | mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3030 | mOrientedRanges.toolMajor.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3031 | mOrientedRanges.toolMajor.min = 0; |
| 3032 | mOrientedRanges.toolMajor.max = diagonalSize; |
| 3033 | mOrientedRanges.toolMajor.flat = 0; |
| 3034 | mOrientedRanges.toolMajor.fuzz = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 3035 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3036 | mOrientedRanges.toolMinor = mOrientedRanges.toolMajor; |
| 3037 | mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3038 | |
| 3039 | mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3040 | mOrientedRanges.size.source = mSource; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3041 | mOrientedRanges.size.min = 0; |
| 3042 | mOrientedRanges.size.max = 1.0; |
| 3043 | mOrientedRanges.size.flat = 0; |
| 3044 | mOrientedRanges.size.fuzz = 0; |
| 3045 | } else { |
| 3046 | mSizeScale = 0.0f; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3047 | } |
| 3048 | |
| 3049 | // Pressure factors. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3050 | mPressureScale = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3051 | if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL |
| 3052 | || mCalibration.pressureCalibration |
| 3053 | == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) { |
| 3054 | if (mCalibration.havePressureScale) { |
| 3055 | mPressureScale = mCalibration.pressureScale; |
| 3056 | } else if (mRawPointerAxes.pressure.valid |
| 3057 | && mRawPointerAxes.pressure.maxValue != 0) { |
| 3058 | mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3059 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3060 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3061 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3062 | mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE; |
| 3063 | mOrientedRanges.pressure.source = mSource; |
| 3064 | mOrientedRanges.pressure.min = 0; |
| 3065 | mOrientedRanges.pressure.max = 1.0; |
| 3066 | mOrientedRanges.pressure.flat = 0; |
| 3067 | mOrientedRanges.pressure.fuzz = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 3068 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3069 | // Tilt |
| 3070 | mTiltXCenter = 0; |
| 3071 | mTiltXScale = 0; |
| 3072 | mTiltYCenter = 0; |
| 3073 | mTiltYScale = 0; |
| 3074 | mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid; |
| 3075 | if (mHaveTilt) { |
| 3076 | mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue, |
| 3077 | mRawPointerAxes.tiltX.maxValue); |
| 3078 | mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue, |
| 3079 | mRawPointerAxes.tiltY.maxValue); |
| 3080 | mTiltXScale = M_PI / 180; |
| 3081 | mTiltYScale = M_PI / 180; |
| 3082 | |
| 3083 | mOrientedRanges.haveTilt = true; |
| 3084 | |
| 3085 | mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT; |
| 3086 | mOrientedRanges.tilt.source = mSource; |
| 3087 | mOrientedRanges.tilt.min = 0; |
| 3088 | mOrientedRanges.tilt.max = M_PI_2; |
| 3089 | mOrientedRanges.tilt.flat = 0; |
| 3090 | mOrientedRanges.tilt.fuzz = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3091 | } |
| 3092 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3093 | // Orientation |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3094 | mOrientationCenter = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3095 | mOrientationScale = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3096 | if (mHaveTilt) { |
| 3097 | mOrientedRanges.haveOrientation = true; |
| 3098 | |
| 3099 | mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION; |
| 3100 | mOrientedRanges.orientation.source = mSource; |
| 3101 | mOrientedRanges.orientation.min = -M_PI; |
| 3102 | mOrientedRanges.orientation.max = M_PI; |
| 3103 | mOrientedRanges.orientation.flat = 0; |
| 3104 | mOrientedRanges.orientation.fuzz = 0; |
| 3105 | } else if (mCalibration.orientationCalibration != |
| 3106 | Calibration::ORIENTATION_CALIBRATION_NONE) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3107 | if (mCalibration.orientationCalibration |
| 3108 | == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3109 | if (mRawPointerAxes.orientation.valid) { |
| 3110 | mOrientationCenter = avg(mRawPointerAxes.orientation.minValue, |
| 3111 | mRawPointerAxes.orientation.maxValue); |
| 3112 | mOrientationScale = M_PI / (mRawPointerAxes.orientation.maxValue - |
| 3113 | mRawPointerAxes.orientation.minValue); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3114 | } |
| 3115 | } |
| 3116 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3117 | mOrientedRanges.haveOrientation = true; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3118 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3119 | mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3120 | mOrientedRanges.orientation.source = mSource; |
| 3121 | mOrientedRanges.orientation.min = -M_PI_2; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3122 | mOrientedRanges.orientation.max = M_PI_2; |
| 3123 | mOrientedRanges.orientation.flat = 0; |
| 3124 | mOrientedRanges.orientation.fuzz = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3125 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3126 | |
| 3127 | // Distance |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3128 | mDistanceScale = 0; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3129 | if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) { |
| 3130 | if (mCalibration.distanceCalibration |
| 3131 | == Calibration::DISTANCE_CALIBRATION_SCALED) { |
| 3132 | if (mCalibration.haveDistanceScale) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3133 | mDistanceScale = mCalibration.distanceScale; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3134 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3135 | mDistanceScale = 1.0f; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3136 | } |
| 3137 | } |
| 3138 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3139 | mOrientedRanges.haveDistance = true; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3140 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3141 | mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3142 | mOrientedRanges.distance.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3143 | mOrientedRanges.distance.min = |
| 3144 | mRawPointerAxes.distance.minValue * mDistanceScale; |
| 3145 | mOrientedRanges.distance.max = |
| 3146 | mRawPointerAxes.distance.minValue * mDistanceScale; |
| 3147 | mOrientedRanges.distance.flat = 0; |
| 3148 | mOrientedRanges.distance.fuzz = |
| 3149 | mRawPointerAxes.distance.fuzz * mDistanceScale; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3150 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3151 | } |
| 3152 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3153 | if (orientationChanged || sizeChanged || deviceModeChanged) { |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 3154 | // Compute oriented surface dimensions, precision, scales and ranges. |
| 3155 | // Note that the maximum value reported is an inclusive maximum value so it is one |
| 3156 | // unit less than the total width or height of surface. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3157 | switch (mSurfaceOrientation) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 3158 | case DISPLAY_ORIENTATION_90: |
| 3159 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3160 | mOrientedSurfaceWidth = mSurfaceHeight; |
| 3161 | mOrientedSurfaceHeight = mSurfaceWidth; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 3162 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3163 | mOrientedXPrecision = mYPrecision; |
| 3164 | mOrientedYPrecision = mXPrecision; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 3165 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3166 | mOrientedRanges.x.min = 0; |
| 3167 | mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue) |
| 3168 | * mYScale; |
| 3169 | mOrientedRanges.x.flat = 0; |
| 3170 | mOrientedRanges.x.fuzz = mYScale; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 3171 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3172 | mOrientedRanges.y.min = 0; |
| 3173 | mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue) |
| 3174 | * mXScale; |
| 3175 | mOrientedRanges.y.flat = 0; |
| 3176 | mOrientedRanges.y.fuzz = mXScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3177 | break; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 3178 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3179 | default: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3180 | mOrientedSurfaceWidth = mSurfaceWidth; |
| 3181 | mOrientedSurfaceHeight = mSurfaceHeight; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 3182 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3183 | mOrientedXPrecision = mXPrecision; |
| 3184 | mOrientedYPrecision = mYPrecision; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 3185 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3186 | mOrientedRanges.x.min = 0; |
| 3187 | mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue) |
| 3188 | * mXScale; |
| 3189 | mOrientedRanges.x.flat = 0; |
| 3190 | mOrientedRanges.x.fuzz = mXScale; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 3191 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3192 | mOrientedRanges.y.min = 0; |
| 3193 | mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue) |
| 3194 | * mYScale; |
| 3195 | mOrientedRanges.y.flat = 0; |
| 3196 | mOrientedRanges.y.fuzz = mYScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3197 | break; |
| 3198 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3199 | |
| 3200 | // Compute pointer gesture detection parameters. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3201 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3202 | int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1; |
| 3203 | int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3204 | float rawDiagonal = hypotf(rawWidth, rawHeight); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3205 | float displayDiagonal = hypotf(mAssociatedDisplayWidth, |
| 3206 | mAssociatedDisplayHeight); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3207 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3208 | // 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] | 3209 | // given area relative to the diagonal size of the display when no acceleration |
| 3210 | // is applied. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3211 | // Assume that the touch pad has a square aspect ratio such that movements in |
| 3212 | // 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] | 3213 | mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3214 | * displayDiagonal / rawDiagonal; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3215 | mPointerYMovementScale = mPointerXMovementScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3216 | |
| 3217 | // Scale zooms to cover a smaller range of the display than movements do. |
| 3218 | // This value determines the area around the pointer that is affected by freeform |
| 3219 | // pointer gestures. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3220 | mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3221 | * displayDiagonal / rawDiagonal; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3222 | mPointerYZoomScale = mPointerXZoomScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3223 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3224 | // Max width between pointers to detect a swipe gesture is more than some fraction |
| 3225 | // of the diagonal axis of the touch pad. Touches that are wider than this are |
| 3226 | // translated into freeform gestures. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3227 | mPointerGestureMaxSwipeWidth = |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 3228 | mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3229 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3230 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3231 | // Abort current pointer usages because the state has changed. |
| 3232 | abortPointerUsage(when, 0 /*policyFlags*/); |
| 3233 | |
| 3234 | // Inform the dispatcher about the changes. |
| 3235 | *outResetNeeded = true; |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 3236 | bumpGeneration(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3237 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3238 | } |
| 3239 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3240 | void TouchInputMapper::dumpSurface(String8& dump) { |
| 3241 | dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth); |
| 3242 | dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight); |
| 3243 | dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3244 | } |
| 3245 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3246 | void TouchInputMapper::configureVirtualKeys() { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3247 | Vector<VirtualKeyDefinition> virtualKeyDefinitions; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 3248 | getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3249 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3250 | mVirtualKeys.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3251 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3252 | if (virtualKeyDefinitions.size() == 0) { |
| 3253 | return; |
| 3254 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3255 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3256 | mVirtualKeys.setCapacity(virtualKeyDefinitions.size()); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3257 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3258 | int32_t touchScreenLeft = mRawPointerAxes.x.minValue; |
| 3259 | int32_t touchScreenTop = mRawPointerAxes.y.minValue; |
| 3260 | int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1; |
| 3261 | int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3262 | |
| 3263 | for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3264 | const VirtualKeyDefinition& virtualKeyDefinition = |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3265 | virtualKeyDefinitions[i]; |
| 3266 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3267 | mVirtualKeys.add(); |
| 3268 | VirtualKey& virtualKey = mVirtualKeys.editTop(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3269 | |
| 3270 | virtualKey.scanCode = virtualKeyDefinition.scanCode; |
| 3271 | int32_t keyCode; |
| 3272 | uint32_t flags; |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 3273 | if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, 0, &keyCode, &flags)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3274 | ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3275 | virtualKey.scanCode); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3276 | mVirtualKeys.pop(); // drop the key |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3277 | continue; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3278 | } |
| 3279 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3280 | virtualKey.keyCode = keyCode; |
| 3281 | virtualKey.flags = flags; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3282 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3283 | // convert the key definition's display coordinates into touch coordinates for a hit box |
| 3284 | int32_t halfWidth = virtualKeyDefinition.width / 2; |
| 3285 | int32_t halfHeight = virtualKeyDefinition.height / 2; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3286 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3287 | virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3288 | * touchScreenWidth / mSurfaceWidth + touchScreenLeft; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3289 | virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3290 | * touchScreenWidth / mSurfaceWidth + touchScreenLeft; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3291 | virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3292 | * touchScreenHeight / mSurfaceHeight + touchScreenTop; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3293 | virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3294 | * touchScreenHeight / mSurfaceHeight + touchScreenTop; |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3295 | } |
| 3296 | } |
| 3297 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3298 | void TouchInputMapper::dumpVirtualKeys(String8& dump) { |
| 3299 | if (!mVirtualKeys.isEmpty()) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3300 | dump.append(INDENT3 "Virtual Keys:\n"); |
| 3301 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3302 | for (size_t i = 0; i < mVirtualKeys.size(); i++) { |
| 3303 | const VirtualKey& virtualKey = mVirtualKeys.itemAt(i); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3304 | dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, " |
| 3305 | "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n", |
| 3306 | i, virtualKey.scanCode, virtualKey.keyCode, |
| 3307 | virtualKey.hitLeft, virtualKey.hitRight, |
| 3308 | virtualKey.hitTop, virtualKey.hitBottom); |
| 3309 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3310 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3311 | } |
| 3312 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3313 | void TouchInputMapper::parseCalibration() { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 3314 | const PropertyMap& in = getDevice()->getConfiguration(); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3315 | Calibration& out = mCalibration; |
| 3316 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3317 | // Size |
| 3318 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT; |
| 3319 | String8 sizeCalibrationString; |
| 3320 | if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) { |
| 3321 | if (sizeCalibrationString == "none") { |
| 3322 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; |
| 3323 | } else if (sizeCalibrationString == "geometric") { |
| 3324 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC; |
| 3325 | } else if (sizeCalibrationString == "diameter") { |
| 3326 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER; |
| 3327 | } else if (sizeCalibrationString == "area") { |
| 3328 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA; |
| 3329 | } else if (sizeCalibrationString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3330 | ALOGW("Invalid value for touch.size.calibration: '%s'", |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3331 | sizeCalibrationString.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3332 | } |
| 3333 | } |
| 3334 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3335 | out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"), |
| 3336 | out.sizeScale); |
| 3337 | out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"), |
| 3338 | out.sizeBias); |
| 3339 | out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"), |
| 3340 | out.sizeIsSummed); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3341 | |
| 3342 | // Pressure |
| 3343 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT; |
| 3344 | String8 pressureCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 3345 | if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3346 | if (pressureCalibrationString == "none") { |
| 3347 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; |
| 3348 | } else if (pressureCalibrationString == "physical") { |
| 3349 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL; |
| 3350 | } else if (pressureCalibrationString == "amplitude") { |
| 3351 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE; |
| 3352 | } else if (pressureCalibrationString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3353 | ALOGW("Invalid value for touch.pressure.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3354 | pressureCalibrationString.string()); |
| 3355 | } |
| 3356 | } |
| 3357 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3358 | out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"), |
| 3359 | out.pressureScale); |
| 3360 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3361 | // Orientation |
| 3362 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT; |
| 3363 | String8 orientationCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 3364 | if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3365 | if (orientationCalibrationString == "none") { |
| 3366 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; |
| 3367 | } else if (orientationCalibrationString == "interpolated") { |
| 3368 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 3369 | } else if (orientationCalibrationString == "vector") { |
| 3370 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3371 | } else if (orientationCalibrationString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3372 | ALOGW("Invalid value for touch.orientation.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3373 | orientationCalibrationString.string()); |
| 3374 | } |
| 3375 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3376 | |
| 3377 | // Distance |
| 3378 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT; |
| 3379 | String8 distanceCalibrationString; |
| 3380 | if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) { |
| 3381 | if (distanceCalibrationString == "none") { |
| 3382 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE; |
| 3383 | } else if (distanceCalibrationString == "scaled") { |
| 3384 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED; |
| 3385 | } else if (distanceCalibrationString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3386 | ALOGW("Invalid value for touch.distance.calibration: '%s'", |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3387 | distanceCalibrationString.string()); |
| 3388 | } |
| 3389 | } |
| 3390 | |
| 3391 | out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"), |
| 3392 | out.distanceScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3393 | } |
| 3394 | |
| 3395 | void TouchInputMapper::resolveCalibration() { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3396 | // Size |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3397 | if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) { |
| 3398 | if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) { |
| 3399 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3400 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3401 | } else { |
| 3402 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; |
| 3403 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3404 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3405 | // Pressure |
| 3406 | if (mRawPointerAxes.pressure.valid) { |
| 3407 | if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) { |
| 3408 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL; |
| 3409 | } |
| 3410 | } else { |
| 3411 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3412 | } |
| 3413 | |
| 3414 | // Orientation |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3415 | if (mRawPointerAxes.orientation.valid) { |
| 3416 | if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3417 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3418 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3419 | } else { |
| 3420 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3421 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3422 | |
| 3423 | // Distance |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3424 | if (mRawPointerAxes.distance.valid) { |
| 3425 | if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3426 | mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3427 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3428 | } else { |
| 3429 | mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3430 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3431 | } |
| 3432 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3433 | void TouchInputMapper::dumpCalibration(String8& dump) { |
| 3434 | dump.append(INDENT3 "Calibration:\n"); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3435 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3436 | // Size |
| 3437 | switch (mCalibration.sizeCalibration) { |
| 3438 | case Calibration::SIZE_CALIBRATION_NONE: |
| 3439 | dump.append(INDENT4 "touch.size.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3440 | break; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3441 | case Calibration::SIZE_CALIBRATION_GEOMETRIC: |
| 3442 | dump.append(INDENT4 "touch.size.calibration: geometric\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3443 | break; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3444 | case Calibration::SIZE_CALIBRATION_DIAMETER: |
| 3445 | dump.append(INDENT4 "touch.size.calibration: diameter\n"); |
| 3446 | break; |
| 3447 | case Calibration::SIZE_CALIBRATION_AREA: |
| 3448 | dump.append(INDENT4 "touch.size.calibration: area\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3449 | break; |
| 3450 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3451 | ALOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3452 | } |
| 3453 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3454 | if (mCalibration.haveSizeScale) { |
| 3455 | dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n", |
| 3456 | mCalibration.sizeScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3457 | } |
| 3458 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3459 | if (mCalibration.haveSizeBias) { |
| 3460 | dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n", |
| 3461 | mCalibration.sizeBias); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3462 | } |
| 3463 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3464 | if (mCalibration.haveSizeIsSummed) { |
| 3465 | dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n", |
| 3466 | toString(mCalibration.sizeIsSummed)); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3467 | } |
| 3468 | |
| 3469 | // Pressure |
| 3470 | switch (mCalibration.pressureCalibration) { |
| 3471 | case Calibration::PRESSURE_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3472 | dump.append(INDENT4 "touch.pressure.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3473 | break; |
| 3474 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3475 | dump.append(INDENT4 "touch.pressure.calibration: physical\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3476 | break; |
| 3477 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3478 | dump.append(INDENT4 "touch.pressure.calibration: amplitude\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3479 | break; |
| 3480 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3481 | ALOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3482 | } |
| 3483 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3484 | if (mCalibration.havePressureScale) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3485 | dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n", |
| 3486 | mCalibration.pressureScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3487 | } |
| 3488 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3489 | // Orientation |
| 3490 | switch (mCalibration.orientationCalibration) { |
| 3491 | case Calibration::ORIENTATION_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3492 | dump.append(INDENT4 "touch.orientation.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3493 | break; |
| 3494 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3495 | dump.append(INDENT4 "touch.orientation.calibration: interpolated\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3496 | break; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 3497 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: |
| 3498 | dump.append(INDENT4 "touch.orientation.calibration: vector\n"); |
| 3499 | break; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3500 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3501 | ALOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3502 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3503 | |
| 3504 | // Distance |
| 3505 | switch (mCalibration.distanceCalibration) { |
| 3506 | case Calibration::DISTANCE_CALIBRATION_NONE: |
| 3507 | dump.append(INDENT4 "touch.distance.calibration: none\n"); |
| 3508 | break; |
| 3509 | case Calibration::DISTANCE_CALIBRATION_SCALED: |
| 3510 | dump.append(INDENT4 "touch.distance.calibration: scaled\n"); |
| 3511 | break; |
| 3512 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3513 | ALOG_ASSERT(false); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3514 | } |
| 3515 | |
| 3516 | if (mCalibration.haveDistanceScale) { |
| 3517 | dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n", |
| 3518 | mCalibration.distanceScale); |
| 3519 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3520 | } |
| 3521 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3522 | void TouchInputMapper::reset(nsecs_t when) { |
| 3523 | mCursorButtonAccumulator.reset(getDevice()); |
| 3524 | mCursorScrollAccumulator.reset(getDevice()); |
| 3525 | mTouchButtonAccumulator.reset(getDevice()); |
| 3526 | |
| 3527 | mPointerVelocityControl.reset(); |
| 3528 | mWheelXVelocityControl.reset(); |
| 3529 | mWheelYVelocityControl.reset(); |
| 3530 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3531 | mCurrentRawPointerData.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3532 | mLastRawPointerData.clear(); |
| 3533 | mCurrentCookedPointerData.clear(); |
| 3534 | mLastCookedPointerData.clear(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3535 | mCurrentButtonState = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3536 | mLastButtonState = 0; |
| 3537 | mCurrentRawVScroll = 0; |
| 3538 | mCurrentRawHScroll = 0; |
| 3539 | mCurrentFingerIdBits.clear(); |
| 3540 | mLastFingerIdBits.clear(); |
| 3541 | mCurrentStylusIdBits.clear(); |
| 3542 | mLastStylusIdBits.clear(); |
| 3543 | mCurrentMouseIdBits.clear(); |
| 3544 | mLastMouseIdBits.clear(); |
| 3545 | mPointerUsage = POINTER_USAGE_NONE; |
| 3546 | mSentHoverEnter = false; |
| 3547 | mDownTime = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3548 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3549 | mCurrentVirtualKey.down = false; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3550 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3551 | mPointerGesture.reset(); |
| 3552 | mPointerSimple.reset(); |
| 3553 | |
| 3554 | if (mPointerController != NULL) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3555 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 3556 | mPointerController->clearSpots(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3557 | } |
| 3558 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3559 | InputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3560 | } |
| 3561 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3562 | void TouchInputMapper::process(const RawEvent* rawEvent) { |
| 3563 | mCursorButtonAccumulator.process(rawEvent); |
| 3564 | mCursorScrollAccumulator.process(rawEvent); |
| 3565 | mTouchButtonAccumulator.process(rawEvent); |
| 3566 | |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 3567 | if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3568 | sync(rawEvent->when); |
| 3569 | } |
| 3570 | } |
| 3571 | |
| 3572 | void TouchInputMapper::sync(nsecs_t when) { |
| 3573 | // Sync button state. |
| 3574 | mCurrentButtonState = mTouchButtonAccumulator.getButtonState() |
| 3575 | | mCursorButtonAccumulator.getButtonState(); |
| 3576 | |
| 3577 | // Sync scroll state. |
| 3578 | mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel(); |
| 3579 | mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel(); |
| 3580 | mCursorScrollAccumulator.finishSync(); |
| 3581 | |
| 3582 | // Sync touch state. |
| 3583 | bool havePointerIds = true; |
| 3584 | mCurrentRawPointerData.clear(); |
| 3585 | syncTouch(when, &havePointerIds); |
| 3586 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 3587 | #if DEBUG_RAW_EVENTS |
| 3588 | if (!havePointerIds) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3589 | ALOGD("syncTouch: pointerCount %d -> %d, no pointer ids", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3590 | mLastRawPointerData.pointerCount, |
| 3591 | mCurrentRawPointerData.pointerCount); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 3592 | } else { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3593 | ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, " |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3594 | "hovering ids 0x%08x -> 0x%08x", |
| 3595 | mLastRawPointerData.pointerCount, |
| 3596 | mCurrentRawPointerData.pointerCount, |
| 3597 | mLastRawPointerData.touchingIdBits.value, |
| 3598 | mCurrentRawPointerData.touchingIdBits.value, |
| 3599 | mLastRawPointerData.hoveringIdBits.value, |
| 3600 | mCurrentRawPointerData.hoveringIdBits.value); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 3601 | } |
| 3602 | #endif |
| 3603 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3604 | // Reset state that we will compute below. |
| 3605 | mCurrentFingerIdBits.clear(); |
| 3606 | mCurrentStylusIdBits.clear(); |
| 3607 | mCurrentMouseIdBits.clear(); |
| 3608 | mCurrentCookedPointerData.clear(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3609 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3610 | if (mDeviceMode == DEVICE_MODE_DISABLED) { |
| 3611 | // Drop all input if the device is disabled. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3612 | mCurrentRawPointerData.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3613 | mCurrentButtonState = 0; |
| 3614 | } else { |
| 3615 | // Preprocess pointer data. |
| 3616 | if (!havePointerIds) { |
| 3617 | assignPointerIds(); |
| 3618 | } |
| 3619 | |
| 3620 | // Handle policy on initial down or hover events. |
| 3621 | uint32_t policyFlags = 0; |
Jeff Brown | c28306a | 2011-08-23 21:32:42 -0700 | [diff] [blame] | 3622 | bool initialDown = mLastRawPointerData.pointerCount == 0 |
| 3623 | && mCurrentRawPointerData.pointerCount != 0; |
| 3624 | bool buttonsPressed = mCurrentButtonState & ~mLastButtonState; |
| 3625 | if (initialDown || buttonsPressed) { |
| 3626 | // 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] | 3627 | if (mDeviceMode == DEVICE_MODE_DIRECT) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3628 | getContext()->fadePointer(); |
| 3629 | } |
| 3630 | |
| 3631 | // Initial downs on external touch devices should wake the device. |
| 3632 | // We don't do this for internal touch screens to prevent them from waking |
| 3633 | // up in your pocket. |
| 3634 | // TODO: Use the input device configuration to control this behavior more finely. |
| 3635 | if (getDevice()->isExternal()) { |
| 3636 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 3637 | } |
| 3638 | } |
| 3639 | |
| 3640 | // Synthesize key down from raw buttons if needed. |
| 3641 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource, |
| 3642 | policyFlags, mLastButtonState, mCurrentButtonState); |
| 3643 | |
| 3644 | // Consume raw off-screen touches before cooking pointer data. |
| 3645 | // If touches are consumed, subsequent code will not receive any pointer data. |
| 3646 | if (consumeRawTouches(when, policyFlags)) { |
| 3647 | mCurrentRawPointerData.clear(); |
| 3648 | } |
| 3649 | |
| 3650 | // Cook pointer data. This call populates the mCurrentCookedPointerData structure |
| 3651 | // with cooked pointer data that has the same ids and indices as the raw data. |
| 3652 | // The following code can use either the raw or cooked data, as needed. |
| 3653 | cookPointerData(); |
| 3654 | |
| 3655 | // 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] | 3656 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3657 | for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) { |
| 3658 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 3659 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3660 | if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS |
| 3661 | || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) { |
| 3662 | mCurrentStylusIdBits.markBit(id); |
| 3663 | } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER |
| 3664 | || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 3665 | mCurrentFingerIdBits.markBit(id); |
| 3666 | } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) { |
| 3667 | mCurrentMouseIdBits.markBit(id); |
| 3668 | } |
| 3669 | } |
| 3670 | for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) { |
| 3671 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 3672 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3673 | if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS |
| 3674 | || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) { |
| 3675 | mCurrentStylusIdBits.markBit(id); |
| 3676 | } |
| 3677 | } |
| 3678 | |
| 3679 | // Stylus takes precedence over all tools, then mouse, then finger. |
| 3680 | PointerUsage pointerUsage = mPointerUsage; |
| 3681 | if (!mCurrentStylusIdBits.isEmpty()) { |
| 3682 | mCurrentMouseIdBits.clear(); |
| 3683 | mCurrentFingerIdBits.clear(); |
| 3684 | pointerUsage = POINTER_USAGE_STYLUS; |
| 3685 | } else if (!mCurrentMouseIdBits.isEmpty()) { |
| 3686 | mCurrentFingerIdBits.clear(); |
| 3687 | pointerUsage = POINTER_USAGE_MOUSE; |
| 3688 | } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) { |
| 3689 | pointerUsage = POINTER_USAGE_GESTURES; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3690 | } |
| 3691 | |
| 3692 | dispatchPointerUsage(when, policyFlags, pointerUsage); |
| 3693 | } else { |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 3694 | if (mDeviceMode == DEVICE_MODE_DIRECT |
| 3695 | && mConfig.showTouches && mPointerController != NULL) { |
| 3696 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT); |
| 3697 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 3698 | |
| 3699 | mPointerController->setButtonState(mCurrentButtonState); |
| 3700 | mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords, |
| 3701 | mCurrentCookedPointerData.idToIndex, |
| 3702 | mCurrentCookedPointerData.touchingIdBits); |
| 3703 | } |
| 3704 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3705 | dispatchHoverExit(when, policyFlags); |
| 3706 | dispatchTouches(when, policyFlags); |
| 3707 | dispatchHoverEnterAndMove(when, policyFlags); |
| 3708 | } |
| 3709 | |
| 3710 | // Synthesize key up from raw buttons if needed. |
| 3711 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource, |
| 3712 | policyFlags, mLastButtonState, mCurrentButtonState); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3713 | } |
| 3714 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3715 | // Copy current touch to last touch in preparation for the next cycle. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3716 | mLastRawPointerData.copyFrom(mCurrentRawPointerData); |
| 3717 | mLastCookedPointerData.copyFrom(mCurrentCookedPointerData); |
| 3718 | mLastButtonState = mCurrentButtonState; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3719 | mLastFingerIdBits = mCurrentFingerIdBits; |
| 3720 | mLastStylusIdBits = mCurrentStylusIdBits; |
| 3721 | mLastMouseIdBits = mCurrentMouseIdBits; |
| 3722 | |
| 3723 | // Clear some transient state. |
| 3724 | mCurrentRawVScroll = 0; |
| 3725 | mCurrentRawHScroll = 0; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3726 | } |
| 3727 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3728 | void TouchInputMapper::timeoutExpired(nsecs_t when) { |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 3729 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3730 | if (mPointerUsage == POINTER_USAGE_GESTURES) { |
| 3731 | dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/); |
| 3732 | } |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3733 | } |
| 3734 | } |
| 3735 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3736 | bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) { |
| 3737 | // Check for release of a virtual key. |
| 3738 | if (mCurrentVirtualKey.down) { |
| 3739 | if (mCurrentRawPointerData.touchingIdBits.isEmpty()) { |
| 3740 | // Pointer went up while virtual key was down. |
| 3741 | mCurrentVirtualKey.down = false; |
| 3742 | if (!mCurrentVirtualKey.ignored) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3743 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3744 | ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3745 | mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3746 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3747 | dispatchVirtualKey(when, policyFlags, |
| 3748 | AKEY_EVENT_ACTION_UP, |
| 3749 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3750 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3751 | return true; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3752 | } |
| 3753 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3754 | if (mCurrentRawPointerData.touchingIdBits.count() == 1) { |
| 3755 | uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit(); |
| 3756 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3757 | const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y); |
| 3758 | if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) { |
| 3759 | // Pointer is still within the space of the virtual key. |
| 3760 | return true; |
| 3761 | } |
| 3762 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3763 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3764 | // Pointer left virtual key area or another pointer also went down. |
| 3765 | // Send key cancellation but do not consume the touch yet. |
| 3766 | // This is useful when the user swipes through from the virtual key area |
| 3767 | // into the main display surface. |
| 3768 | mCurrentVirtualKey.down = false; |
| 3769 | if (!mCurrentVirtualKey.ignored) { |
| 3770 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3771 | ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3772 | mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode); |
| 3773 | #endif |
| 3774 | dispatchVirtualKey(when, policyFlags, |
| 3775 | AKEY_EVENT_ACTION_UP, |
| 3776 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
| 3777 | | AKEY_EVENT_FLAG_CANCELED); |
| 3778 | } |
| 3779 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3780 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3781 | if (mLastRawPointerData.touchingIdBits.isEmpty() |
| 3782 | && !mCurrentRawPointerData.touchingIdBits.isEmpty()) { |
| 3783 | // Pointer just went down. Check for virtual key press or off-screen touches. |
| 3784 | uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit(); |
| 3785 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3786 | if (!isPointInsideSurface(pointer.x, pointer.y)) { |
| 3787 | // If exactly one pointer went down, check for virtual key hit. |
| 3788 | // Otherwise we will drop the entire stroke. |
| 3789 | if (mCurrentRawPointerData.touchingIdBits.count() == 1) { |
| 3790 | const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y); |
| 3791 | if (virtualKey) { |
| 3792 | mCurrentVirtualKey.down = true; |
| 3793 | mCurrentVirtualKey.downTime = when; |
| 3794 | mCurrentVirtualKey.keyCode = virtualKey->keyCode; |
| 3795 | mCurrentVirtualKey.scanCode = virtualKey->scanCode; |
| 3796 | mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey( |
| 3797 | when, getDevice(), virtualKey->keyCode, virtualKey->scanCode); |
| 3798 | |
| 3799 | if (!mCurrentVirtualKey.ignored) { |
| 3800 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3801 | ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3802 | mCurrentVirtualKey.keyCode, |
| 3803 | mCurrentVirtualKey.scanCode); |
| 3804 | #endif |
| 3805 | dispatchVirtualKey(when, policyFlags, |
| 3806 | AKEY_EVENT_ACTION_DOWN, |
| 3807 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY); |
| 3808 | } |
| 3809 | } |
| 3810 | } |
| 3811 | return true; |
| 3812 | } |
| 3813 | } |
| 3814 | |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3815 | // 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] | 3816 | // most recent touch within the screen area. The idea is to filter out stray |
| 3817 | // virtual key presses when interacting with the touch screen. |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3818 | // |
| 3819 | // Problems we're trying to solve: |
| 3820 | // |
| 3821 | // 1. While scrolling a list or dragging the window shade, the user swipes down into a |
| 3822 | // virtual key area that is implemented by a separate touch panel and accidentally |
| 3823 | // triggers a virtual key. |
| 3824 | // |
| 3825 | // 2. While typing in the on screen keyboard, the user taps slightly outside the screen |
| 3826 | // area and accidentally triggers a virtual key. This often happens when virtual keys |
| 3827 | // are layed out below the screen near to where the on screen keyboard's space bar |
| 3828 | // is displayed. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3829 | if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 3830 | mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime); |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3831 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3832 | return false; |
| 3833 | } |
| 3834 | |
| 3835 | void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags, |
| 3836 | int32_t keyEventAction, int32_t keyEventFlags) { |
| 3837 | int32_t keyCode = mCurrentVirtualKey.keyCode; |
| 3838 | int32_t scanCode = mCurrentVirtualKey.scanCode; |
| 3839 | nsecs_t downTime = mCurrentVirtualKey.downTime; |
| 3840 | int32_t metaState = mContext->getGlobalMetaState(); |
| 3841 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3842 | |
| 3843 | NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags, |
| 3844 | keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime); |
| 3845 | getListener()->notifyKey(&args); |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3846 | } |
| 3847 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3848 | void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3849 | BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits; |
| 3850 | BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3851 | int32_t metaState = getContext()->getGlobalMetaState(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3852 | int32_t buttonState = mCurrentButtonState; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3853 | |
| 3854 | if (currentIdBits == lastIdBits) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3855 | if (!currentIdBits.isEmpty()) { |
| 3856 | // No pointer id changes so this is a move event. |
| 3857 | // 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] | 3858 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3859 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, |
| 3860 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 3861 | mCurrentCookedPointerData.pointerProperties, |
| 3862 | mCurrentCookedPointerData.pointerCoords, |
| 3863 | mCurrentCookedPointerData.idToIndex, |
| 3864 | currentIdBits, -1, |
| 3865 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 3866 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3867 | } else { |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3868 | // There may be pointers going up and pointers going down and pointers moving |
| 3869 | // all at the same time. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3870 | BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value); |
| 3871 | BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3872 | BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3873 | BitSet32 dispatchedIdBits(lastIdBits.value); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3874 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3875 | // Update last coordinates of pointers that have moved so that we observe the new |
| 3876 | // 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] | 3877 | bool moveNeeded = updateMovedPointers( |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3878 | mCurrentCookedPointerData.pointerProperties, |
| 3879 | mCurrentCookedPointerData.pointerCoords, |
| 3880 | mCurrentCookedPointerData.idToIndex, |
| 3881 | mLastCookedPointerData.pointerProperties, |
| 3882 | mLastCookedPointerData.pointerCoords, |
| 3883 | mLastCookedPointerData.idToIndex, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3884 | moveIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3885 | if (buttonState != mLastButtonState) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3886 | moveNeeded = true; |
| 3887 | } |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3888 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3889 | // Dispatch pointer up events. |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3890 | while (!upIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3891 | uint32_t upId = upIdBits.clearFirstMarkedBit(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3892 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3893 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3894 | AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3895 | mLastCookedPointerData.pointerProperties, |
| 3896 | mLastCookedPointerData.pointerCoords, |
| 3897 | mLastCookedPointerData.idToIndex, |
| 3898 | dispatchedIdBits, upId, |
| 3899 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3900 | dispatchedIdBits.clearBit(upId); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3901 | } |
| 3902 | |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3903 | // Dispatch move events if any of the remaining pointers moved from their old locations. |
| 3904 | // Although applications receive new locations as part of individual pointer up |
| 3905 | // events, they do not generally handle them except when presented in a move event. |
| 3906 | if (moveNeeded) { |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3907 | ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3908 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3909 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3910 | mCurrentCookedPointerData.pointerProperties, |
| 3911 | mCurrentCookedPointerData.pointerCoords, |
| 3912 | mCurrentCookedPointerData.idToIndex, |
| 3913 | dispatchedIdBits, -1, |
| 3914 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3915 | } |
| 3916 | |
| 3917 | // Dispatch pointer down events using the new pointer locations. |
| 3918 | while (!downIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3919 | uint32_t downId = downIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3920 | dispatchedIdBits.markBit(downId); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3921 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3922 | if (dispatchedIdBits.count() == 1) { |
| 3923 | // First pointer is going down. Set down time. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3924 | mDownTime = when; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3925 | } |
| 3926 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3927 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | a611137 | 2011-07-14 21:48:23 -0700 | [diff] [blame] | 3928 | AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3929 | mCurrentCookedPointerData.pointerProperties, |
| 3930 | mCurrentCookedPointerData.pointerCoords, |
| 3931 | mCurrentCookedPointerData.idToIndex, |
| 3932 | dispatchedIdBits, downId, |
| 3933 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3934 | } |
| 3935 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3936 | } |
| 3937 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3938 | void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) { |
| 3939 | if (mSentHoverEnter && |
| 3940 | (mCurrentCookedPointerData.hoveringIdBits.isEmpty() |
| 3941 | || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) { |
| 3942 | int32_t metaState = getContext()->getGlobalMetaState(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3943 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3944 | AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0, |
| 3945 | mLastCookedPointerData.pointerProperties, |
| 3946 | mLastCookedPointerData.pointerCoords, |
| 3947 | mLastCookedPointerData.idToIndex, |
| 3948 | mLastCookedPointerData.hoveringIdBits, -1, |
| 3949 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 3950 | mSentHoverEnter = false; |
| 3951 | } |
| 3952 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3953 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3954 | void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) { |
| 3955 | if (mCurrentCookedPointerData.touchingIdBits.isEmpty() |
| 3956 | && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) { |
| 3957 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 3958 | if (!mSentHoverEnter) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3959 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3960 | AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0, |
| 3961 | mCurrentCookedPointerData.pointerProperties, |
| 3962 | mCurrentCookedPointerData.pointerCoords, |
| 3963 | mCurrentCookedPointerData.idToIndex, |
| 3964 | mCurrentCookedPointerData.hoveringIdBits, -1, |
| 3965 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 3966 | mSentHoverEnter = true; |
| 3967 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3968 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3969 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3970 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0, |
| 3971 | mCurrentCookedPointerData.pointerProperties, |
| 3972 | mCurrentCookedPointerData.pointerCoords, |
| 3973 | mCurrentCookedPointerData.idToIndex, |
| 3974 | mCurrentCookedPointerData.hoveringIdBits, -1, |
| 3975 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 3976 | } |
| 3977 | } |
| 3978 | |
| 3979 | void TouchInputMapper::cookPointerData() { |
| 3980 | uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount; |
| 3981 | |
| 3982 | mCurrentCookedPointerData.clear(); |
| 3983 | mCurrentCookedPointerData.pointerCount = currentPointerCount; |
| 3984 | mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits; |
| 3985 | mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits; |
| 3986 | |
| 3987 | // Walk through the the active pointers and map device coordinates onto |
| 3988 | // surface coordinates and adjust for display orientation. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3989 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3990 | const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3991 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3992 | // Size |
| 3993 | float touchMajor, touchMinor, toolMajor, toolMinor, size; |
| 3994 | switch (mCalibration.sizeCalibration) { |
| 3995 | case Calibration::SIZE_CALIBRATION_GEOMETRIC: |
| 3996 | case Calibration::SIZE_CALIBRATION_DIAMETER: |
| 3997 | case Calibration::SIZE_CALIBRATION_AREA: |
| 3998 | if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) { |
| 3999 | touchMajor = in.touchMajor; |
| 4000 | touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor; |
| 4001 | toolMajor = in.toolMajor; |
| 4002 | toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor; |
| 4003 | size = mRawPointerAxes.touchMinor.valid |
| 4004 | ? avg(in.touchMajor, in.touchMinor) : in.touchMajor; |
| 4005 | } else if (mRawPointerAxes.touchMajor.valid) { |
| 4006 | toolMajor = touchMajor = in.touchMajor; |
| 4007 | toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid |
| 4008 | ? in.touchMinor : in.touchMajor; |
| 4009 | size = mRawPointerAxes.touchMinor.valid |
| 4010 | ? avg(in.touchMajor, in.touchMinor) : in.touchMajor; |
| 4011 | } else if (mRawPointerAxes.toolMajor.valid) { |
| 4012 | touchMajor = toolMajor = in.toolMajor; |
| 4013 | touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid |
| 4014 | ? in.toolMinor : in.toolMajor; |
| 4015 | size = mRawPointerAxes.toolMinor.valid |
| 4016 | ? avg(in.toolMajor, in.toolMinor) : in.toolMajor; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4017 | } else { |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 4018 | ALOG_ASSERT(false, "No touch or tool axes. " |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4019 | "Size calibration should have been resolved to NONE."); |
| 4020 | touchMajor = 0; |
| 4021 | touchMinor = 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4022 | toolMajor = 0; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4023 | toolMinor = 0; |
| 4024 | size = 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4025 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4026 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4027 | if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) { |
| 4028 | uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count(); |
| 4029 | if (touchingCount > 1) { |
| 4030 | touchMajor /= touchingCount; |
| 4031 | touchMinor /= touchingCount; |
| 4032 | toolMajor /= touchingCount; |
| 4033 | toolMinor /= touchingCount; |
| 4034 | size /= touchingCount; |
| 4035 | } |
| 4036 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4037 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4038 | if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) { |
| 4039 | touchMajor *= mGeometricScale; |
| 4040 | touchMinor *= mGeometricScale; |
| 4041 | toolMajor *= mGeometricScale; |
| 4042 | toolMinor *= mGeometricScale; |
| 4043 | } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) { |
| 4044 | touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4045 | touchMinor = touchMajor; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4046 | toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0; |
| 4047 | toolMinor = toolMajor; |
| 4048 | } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) { |
| 4049 | touchMinor = touchMajor; |
| 4050 | toolMinor = toolMajor; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4051 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4052 | |
| 4053 | mCalibration.applySizeScaleAndBias(&touchMajor); |
| 4054 | mCalibration.applySizeScaleAndBias(&touchMinor); |
| 4055 | mCalibration.applySizeScaleAndBias(&toolMajor); |
| 4056 | mCalibration.applySizeScaleAndBias(&toolMinor); |
| 4057 | size *= mSizeScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4058 | break; |
| 4059 | default: |
| 4060 | touchMajor = 0; |
| 4061 | touchMinor = 0; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4062 | toolMajor = 0; |
| 4063 | toolMinor = 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4064 | size = 0; |
| 4065 | break; |
| 4066 | } |
| 4067 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4068 | // Pressure |
| 4069 | float pressure; |
| 4070 | switch (mCalibration.pressureCalibration) { |
| 4071 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: |
| 4072 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: |
| 4073 | pressure = in.pressure * mPressureScale; |
| 4074 | break; |
| 4075 | default: |
| 4076 | pressure = in.isHovering ? 0 : 1; |
| 4077 | break; |
| 4078 | } |
| 4079 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4080 | // Tilt and Orientation |
| 4081 | float tilt; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4082 | float orientation; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4083 | if (mHaveTilt) { |
| 4084 | float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale; |
| 4085 | float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale; |
| 4086 | orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle)); |
| 4087 | tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle)); |
| 4088 | } else { |
| 4089 | tilt = 0; |
| 4090 | |
| 4091 | switch (mCalibration.orientationCalibration) { |
| 4092 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: |
| 4093 | orientation = (in.orientation - mOrientationCenter) * mOrientationScale; |
| 4094 | break; |
| 4095 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: { |
| 4096 | int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4); |
| 4097 | int32_t c2 = signExtendNybble(in.orientation & 0x0f); |
| 4098 | if (c1 != 0 || c2 != 0) { |
| 4099 | orientation = atan2f(c1, c2) * 0.5f; |
| 4100 | float confidence = hypotf(c1, c2); |
| 4101 | float scale = 1.0f + confidence / 16.0f; |
| 4102 | touchMajor *= scale; |
| 4103 | touchMinor /= scale; |
| 4104 | toolMajor *= scale; |
| 4105 | toolMinor /= scale; |
| 4106 | } else { |
| 4107 | orientation = 0; |
| 4108 | } |
| 4109 | break; |
| 4110 | } |
| 4111 | default: |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4112 | orientation = 0; |
| 4113 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4114 | } |
| 4115 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 4116 | // Distance |
| 4117 | float distance; |
| 4118 | switch (mCalibration.distanceCalibration) { |
| 4119 | case Calibration::DISTANCE_CALIBRATION_SCALED: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4120 | distance = in.distance * mDistanceScale; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 4121 | break; |
| 4122 | default: |
| 4123 | distance = 0; |
| 4124 | } |
| 4125 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4126 | // X and Y |
| 4127 | // Adjust coords for surface orientation. |
| 4128 | float x, y; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4129 | switch (mSurfaceOrientation) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4130 | case DISPLAY_ORIENTATION_90: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4131 | x = float(in.y - mRawPointerAxes.y.minValue) * mYScale; |
| 4132 | y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4133 | orientation -= M_PI_2; |
| 4134 | if (orientation < - M_PI_2) { |
| 4135 | orientation += M_PI; |
| 4136 | } |
| 4137 | break; |
| 4138 | case DISPLAY_ORIENTATION_180: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4139 | x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale; |
| 4140 | y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4141 | break; |
| 4142 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4143 | x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale; |
| 4144 | y = float(in.x - mRawPointerAxes.x.minValue) * mXScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4145 | orientation += M_PI_2; |
| 4146 | if (orientation > M_PI_2) { |
| 4147 | orientation -= M_PI; |
| 4148 | } |
| 4149 | break; |
| 4150 | default: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4151 | x = float(in.x - mRawPointerAxes.x.minValue) * mXScale; |
| 4152 | y = float(in.y - mRawPointerAxes.y.minValue) * mYScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4153 | break; |
| 4154 | } |
| 4155 | |
| 4156 | // Write output coords. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4157 | PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4158 | out.clear(); |
| 4159 | out.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4160 | out.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 4161 | out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure); |
| 4162 | out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size); |
| 4163 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor); |
| 4164 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor); |
| 4165 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor); |
| 4166 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor); |
| 4167 | out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4168 | out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4169 | out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4170 | |
| 4171 | // Write output properties. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4172 | PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i]; |
| 4173 | uint32_t id = in.id; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4174 | properties.clear(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4175 | properties.id = id; |
| 4176 | properties.toolType = in.toolType; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4177 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4178 | // Write id index. |
| 4179 | mCurrentCookedPointerData.idToIndex[id] = i; |
| 4180 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4181 | } |
| 4182 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4183 | void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, |
| 4184 | PointerUsage pointerUsage) { |
| 4185 | if (pointerUsage != mPointerUsage) { |
| 4186 | abortPointerUsage(when, policyFlags); |
| 4187 | mPointerUsage = pointerUsage; |
| 4188 | } |
| 4189 | |
| 4190 | switch (mPointerUsage) { |
| 4191 | case POINTER_USAGE_GESTURES: |
| 4192 | dispatchPointerGestures(when, policyFlags, false /*isTimeout*/); |
| 4193 | break; |
| 4194 | case POINTER_USAGE_STYLUS: |
| 4195 | dispatchPointerStylus(when, policyFlags); |
| 4196 | break; |
| 4197 | case POINTER_USAGE_MOUSE: |
| 4198 | dispatchPointerMouse(when, policyFlags); |
| 4199 | break; |
| 4200 | default: |
| 4201 | break; |
| 4202 | } |
| 4203 | } |
| 4204 | |
| 4205 | void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) { |
| 4206 | switch (mPointerUsage) { |
| 4207 | case POINTER_USAGE_GESTURES: |
| 4208 | abortPointerGestures(when, policyFlags); |
| 4209 | break; |
| 4210 | case POINTER_USAGE_STYLUS: |
| 4211 | abortPointerStylus(when, policyFlags); |
| 4212 | break; |
| 4213 | case POINTER_USAGE_MOUSE: |
| 4214 | abortPointerMouse(when, policyFlags); |
| 4215 | break; |
| 4216 | default: |
| 4217 | break; |
| 4218 | } |
| 4219 | |
| 4220 | mPointerUsage = POINTER_USAGE_NONE; |
| 4221 | } |
| 4222 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4223 | void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, |
| 4224 | bool isTimeout) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4225 | // Update current gesture coordinates. |
| 4226 | bool cancelPreviousGesture, finishPreviousGesture; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4227 | bool sendEvents = preparePointerGestures(when, |
| 4228 | &cancelPreviousGesture, &finishPreviousGesture, isTimeout); |
| 4229 | if (!sendEvents) { |
| 4230 | return; |
| 4231 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4232 | if (finishPreviousGesture) { |
| 4233 | cancelPreviousGesture = false; |
| 4234 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4235 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4236 | // Update the pointer presentation and spots. |
| 4237 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 4238 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT); |
| 4239 | if (finishPreviousGesture || cancelPreviousGesture) { |
| 4240 | mPointerController->clearSpots(); |
| 4241 | } |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4242 | mPointerController->setSpots(mPointerGesture.currentGestureCoords, |
| 4243 | mPointerGesture.currentGestureIdToIndex, |
| 4244 | mPointerGesture.currentGestureIdBits); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4245 | } else { |
| 4246 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER); |
| 4247 | } |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4248 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4249 | // Show or hide the pointer if needed. |
| 4250 | switch (mPointerGesture.currentGestureMode) { |
| 4251 | case PointerGesture::NEUTRAL: |
| 4252 | case PointerGesture::QUIET: |
| 4253 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS |
| 4254 | && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE |
| 4255 | || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) { |
| 4256 | // Remind the user of where the pointer is after finishing a gesture with spots. |
| 4257 | mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 4258 | } |
| 4259 | break; |
| 4260 | case PointerGesture::TAP: |
| 4261 | case PointerGesture::TAP_DRAG: |
| 4262 | case PointerGesture::BUTTON_CLICK_OR_DRAG: |
| 4263 | case PointerGesture::HOVER: |
| 4264 | case PointerGesture::PRESS: |
| 4265 | // Unfade the pointer when the current gesture manipulates the |
| 4266 | // area directly under the pointer. |
| 4267 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 4268 | break; |
| 4269 | case PointerGesture::SWIPE: |
| 4270 | case PointerGesture::FREEFORM: |
| 4271 | // Fade the pointer when the current gesture manipulates a different |
| 4272 | // area and there are spots to guide the user experience. |
| 4273 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 4274 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 4275 | } else { |
| 4276 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 4277 | } |
| 4278 | break; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4279 | } |
| 4280 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4281 | // Send events! |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4282 | int32_t metaState = getContext()->getGlobalMetaState(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4283 | int32_t buttonState = mCurrentButtonState; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4284 | |
| 4285 | // Update last coordinates of pointers that have moved so that we observe the new |
| 4286 | // 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] | 4287 | bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP |
| 4288 | || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG |
| 4289 | || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4290 | || mPointerGesture.currentGestureMode == PointerGesture::PRESS |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4291 | || mPointerGesture.currentGestureMode == PointerGesture::SWIPE |
| 4292 | || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM; |
| 4293 | bool moveNeeded = false; |
| 4294 | if (down && !cancelPreviousGesture && !finishPreviousGesture |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4295 | && !mPointerGesture.lastGestureIdBits.isEmpty() |
| 4296 | && !mPointerGesture.currentGestureIdBits.isEmpty()) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4297 | BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value |
| 4298 | & mPointerGesture.lastGestureIdBits.value); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4299 | moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4300 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4301 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4302 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4303 | movedGestureIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4304 | if (buttonState != mLastButtonState) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4305 | moveNeeded = true; |
| 4306 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4307 | } |
| 4308 | |
| 4309 | // Send motion events for all pointers that went up or were canceled. |
| 4310 | BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits); |
| 4311 | if (!dispatchedGestureIdBits.isEmpty()) { |
| 4312 | if (cancelPreviousGesture) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4313 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4314 | AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState, |
| 4315 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4316 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4317 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4318 | dispatchedGestureIdBits, -1, |
| 4319 | 0, 0, mPointerGesture.downTime); |
| 4320 | |
| 4321 | dispatchedGestureIdBits.clear(); |
| 4322 | } else { |
| 4323 | BitSet32 upGestureIdBits; |
| 4324 | if (finishPreviousGesture) { |
| 4325 | upGestureIdBits = dispatchedGestureIdBits; |
| 4326 | } else { |
| 4327 | upGestureIdBits.value = dispatchedGestureIdBits.value |
| 4328 | & ~mPointerGesture.currentGestureIdBits.value; |
| 4329 | } |
| 4330 | while (!upGestureIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4331 | uint32_t id = upGestureIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4332 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4333 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4334 | AMOTION_EVENT_ACTION_POINTER_UP, 0, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4335 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4336 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4337 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4338 | dispatchedGestureIdBits, id, |
| 4339 | 0, 0, mPointerGesture.downTime); |
| 4340 | |
| 4341 | dispatchedGestureIdBits.clearBit(id); |
| 4342 | } |
| 4343 | } |
| 4344 | } |
| 4345 | |
| 4346 | // Send motion events for all pointers that moved. |
| 4347 | if (moveNeeded) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4348 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4349 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4350 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4351 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 4352 | dispatchedGestureIdBits, -1, |
| 4353 | 0, 0, mPointerGesture.downTime); |
| 4354 | } |
| 4355 | |
| 4356 | // Send motion events for all pointers that went down. |
| 4357 | if (down) { |
| 4358 | BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value |
| 4359 | & ~dispatchedGestureIdBits.value); |
| 4360 | while (!downGestureIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4361 | uint32_t id = downGestureIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4362 | dispatchedGestureIdBits.markBit(id); |
| 4363 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4364 | if (dispatchedGestureIdBits.count() == 1) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4365 | mPointerGesture.downTime = when; |
| 4366 | } |
| 4367 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4368 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | a611137 | 2011-07-14 21:48:23 -0700 | [diff] [blame] | 4369 | AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4370 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4371 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 4372 | dispatchedGestureIdBits, id, |
| 4373 | 0, 0, mPointerGesture.downTime); |
| 4374 | } |
| 4375 | } |
| 4376 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4377 | // Send motion events for hover. |
| 4378 | if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4379 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4380 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 4381 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4382 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4383 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 4384 | mPointerGesture.currentGestureIdBits, -1, |
| 4385 | 0, 0, mPointerGesture.downTime); |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4386 | } else if (dispatchedGestureIdBits.isEmpty() |
| 4387 | && !mPointerGesture.lastGestureIdBits.isEmpty()) { |
| 4388 | // Synthesize a hover move event after all pointers go up to indicate that |
| 4389 | // the pointer is hovering again even if the user is not currently touching |
| 4390 | // the touch pad. This ensures that a view will receive a fresh hover enter |
| 4391 | // event after a tap. |
| 4392 | float x, y; |
| 4393 | mPointerController->getPosition(&x, &y); |
| 4394 | |
| 4395 | PointerProperties pointerProperties; |
| 4396 | pointerProperties.clear(); |
| 4397 | pointerProperties.id = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4398 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4399 | |
| 4400 | PointerCoords pointerCoords; |
| 4401 | pointerCoords.clear(); |
| 4402 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4403 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 4404 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4405 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4406 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 4407 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4408 | 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4409 | getListener()->notifyMotion(&args); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | // Update state. |
| 4413 | mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode; |
| 4414 | if (!down) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4415 | mPointerGesture.lastGestureIdBits.clear(); |
| 4416 | } else { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4417 | mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits; |
| 4418 | for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4419 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4420 | uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4421 | mPointerGesture.lastGestureProperties[index].copyFrom( |
| 4422 | mPointerGesture.currentGestureProperties[index]); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4423 | mPointerGesture.lastGestureCoords[index].copyFrom( |
| 4424 | mPointerGesture.currentGestureCoords[index]); |
| 4425 | mPointerGesture.lastGestureIdToIndex[id] = index; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4426 | } |
| 4427 | } |
| 4428 | } |
| 4429 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4430 | void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) { |
| 4431 | // Cancel previously dispatches pointers. |
| 4432 | if (!mPointerGesture.lastGestureIdBits.isEmpty()) { |
| 4433 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 4434 | int32_t buttonState = mCurrentButtonState; |
| 4435 | dispatchMotion(when, policyFlags, mSource, |
| 4436 | AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState, |
| 4437 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4438 | mPointerGesture.lastGestureProperties, |
| 4439 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4440 | mPointerGesture.lastGestureIdBits, -1, |
| 4441 | 0, 0, mPointerGesture.downTime); |
| 4442 | } |
| 4443 | |
| 4444 | // Reset the current pointer gesture. |
| 4445 | mPointerGesture.reset(); |
| 4446 | mPointerVelocityControl.reset(); |
| 4447 | |
| 4448 | // Remove any current spots. |
| 4449 | if (mPointerController != NULL) { |
| 4450 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 4451 | mPointerController->clearSpots(); |
| 4452 | } |
| 4453 | } |
| 4454 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4455 | bool TouchInputMapper::preparePointerGestures(nsecs_t when, |
| 4456 | bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4457 | *outCancelPreviousGesture = false; |
| 4458 | *outFinishPreviousGesture = false; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4459 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4460 | // Handle TAP timeout. |
| 4461 | if (isTimeout) { |
| 4462 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4463 | ALOGD("Gestures: Processing timeout"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4464 | #endif |
| 4465 | |
| 4466 | if (mPointerGesture.lastGestureMode == PointerGesture::TAP) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4467 | if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4468 | // The tap/drag timeout has not yet expired. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4469 | getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4470 | + mConfig.pointerGestureTapDragInterval); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4471 | } else { |
| 4472 | // The tap is finished. |
| 4473 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4474 | ALOGD("Gestures: TAP finished"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4475 | #endif |
| 4476 | *outFinishPreviousGesture = true; |
| 4477 | |
| 4478 | mPointerGesture.activeGestureId = -1; |
| 4479 | mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL; |
| 4480 | mPointerGesture.currentGestureIdBits.clear(); |
| 4481 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4482 | mPointerVelocityControl.reset(); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4483 | return true; |
| 4484 | } |
| 4485 | } |
| 4486 | |
| 4487 | // We did not handle this timeout. |
| 4488 | return false; |
| 4489 | } |
| 4490 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4491 | const uint32_t currentFingerCount = mCurrentFingerIdBits.count(); |
| 4492 | const uint32_t lastFingerCount = mLastFingerIdBits.count(); |
| 4493 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4494 | // Update the velocity tracker. |
| 4495 | { |
| 4496 | VelocityTracker::Position positions[MAX_POINTERS]; |
| 4497 | uint32_t count = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4498 | for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4499 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 4500 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4501 | positions[count].x = pointer.x * mPointerXMovementScale; |
| 4502 | positions[count].y = pointer.y * mPointerYMovementScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4503 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4504 | mPointerGesture.velocityTracker.addMovement(when, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4505 | mCurrentFingerIdBits, positions); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4506 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4507 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4508 | // Pick a new active touch id if needed. |
| 4509 | // Choose an arbitrary pointer that just went down, if there is one. |
| 4510 | // Otherwise choose an arbitrary remaining pointer. |
| 4511 | // 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] | 4512 | // We keep the same active touch id for as long as possible. |
| 4513 | bool activeTouchChanged = false; |
| 4514 | int32_t lastActiveTouchId = mPointerGesture.activeTouchId; |
| 4515 | int32_t activeTouchId = lastActiveTouchId; |
| 4516 | if (activeTouchId < 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4517 | if (!mCurrentFingerIdBits.isEmpty()) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4518 | activeTouchChanged = true; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4519 | activeTouchId = mPointerGesture.activeTouchId = |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4520 | mCurrentFingerIdBits.firstMarkedBit(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4521 | mPointerGesture.firstTouchTime = when; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4522 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4523 | } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4524 | activeTouchChanged = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4525 | if (!mCurrentFingerIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4526 | activeTouchId = mPointerGesture.activeTouchId = |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4527 | mCurrentFingerIdBits.firstMarkedBit(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4528 | } else { |
| 4529 | activeTouchId = mPointerGesture.activeTouchId = -1; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4530 | } |
| 4531 | } |
| 4532 | |
| 4533 | // Determine whether we are in quiet time. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4534 | bool isQuietTime = false; |
| 4535 | if (activeTouchId < 0) { |
| 4536 | mPointerGesture.resetQuietTime(); |
| 4537 | } else { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4538 | isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4539 | if (!isQuietTime) { |
| 4540 | if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS |
| 4541 | || mPointerGesture.lastGestureMode == PointerGesture::SWIPE |
| 4542 | || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4543 | && currentFingerCount < 2) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4544 | // Enter quiet time when exiting swipe or freeform state. |
| 4545 | // This is to prevent accidentally entering the hover state and flinging the |
| 4546 | // pointer when finishing a swipe and there is still one pointer left onscreen. |
| 4547 | isQuietTime = true; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4548 | } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4549 | && currentFingerCount >= 2 |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4550 | && !isPointerDown(mCurrentButtonState)) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4551 | // Enter quiet time when releasing the button and there are still two or more |
| 4552 | // fingers down. This may indicate that one finger was used to press the button |
| 4553 | // but it has not gone up yet. |
| 4554 | isQuietTime = true; |
| 4555 | } |
| 4556 | if (isQuietTime) { |
| 4557 | mPointerGesture.quietTime = when; |
| 4558 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4559 | } |
| 4560 | } |
| 4561 | |
| 4562 | // Switch states based on button and pointer state. |
| 4563 | if (isQuietTime) { |
| 4564 | // Case 1: Quiet time. (QUIET) |
| 4565 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4566 | ALOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4567 | + mConfig.pointerGestureQuietInterval - when) * 0.000001f); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4568 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4569 | if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) { |
| 4570 | *outFinishPreviousGesture = true; |
| 4571 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4572 | |
| 4573 | mPointerGesture.activeGestureId = -1; |
| 4574 | mPointerGesture.currentGestureMode = PointerGesture::QUIET; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4575 | mPointerGesture.currentGestureIdBits.clear(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4576 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4577 | mPointerVelocityControl.reset(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4578 | } else if (isPointerDown(mCurrentButtonState)) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4579 | // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG) |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4580 | // The pointer follows the active touch point. |
| 4581 | // Emit DOWN, MOVE, UP events at the pointer location. |
| 4582 | // |
| 4583 | // Only the active touch matters; other fingers are ignored. This policy helps |
| 4584 | // to handle the case where the user places a second finger on the touch pad |
| 4585 | // to apply the necessary force to depress an integrated button below the surface. |
| 4586 | // We don't want the second finger to be delivered to applications. |
| 4587 | // |
| 4588 | // For this to work well, we need to make sure to track the pointer that is really |
| 4589 | // active. If the user first puts one finger down to click then adds another |
| 4590 | // finger to drag then the active pointer should switch to the finger that is |
| 4591 | // being dragged. |
| 4592 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4593 | ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4594 | "currentFingerCount=%d", activeTouchId, currentFingerCount); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4595 | #endif |
| 4596 | // Reset state when just starting. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4597 | if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4598 | *outFinishPreviousGesture = true; |
| 4599 | mPointerGesture.activeGestureId = 0; |
| 4600 | } |
| 4601 | |
| 4602 | // Switch pointers if needed. |
| 4603 | // Find the fastest pointer and follow it. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4604 | if (activeTouchId >= 0 && currentFingerCount > 1) { |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4605 | int32_t bestId = -1; |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4606 | float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4607 | for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4608 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4609 | float vx, vy; |
| 4610 | if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) { |
| 4611 | float speed = hypotf(vx, vy); |
| 4612 | if (speed > bestSpeed) { |
| 4613 | bestId = id; |
| 4614 | bestSpeed = speed; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4615 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 4616 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4617 | } |
| 4618 | if (bestId >= 0 && bestId != activeTouchId) { |
| 4619 | mPointerGesture.activeTouchId = activeTouchId = bestId; |
| 4620 | activeTouchChanged = true; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4621 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4622 | ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, " |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4623 | "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4624 | #endif |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4625 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4626 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4627 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4628 | if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4629 | const RawPointerData::Pointer& currentPointer = |
| 4630 | mCurrentRawPointerData.pointerForId(activeTouchId); |
| 4631 | const RawPointerData::Pointer& lastPointer = |
| 4632 | mLastRawPointerData.pointerForId(activeTouchId); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4633 | float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale; |
| 4634 | float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4635 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4636 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4637 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4638 | |
| 4639 | // Move the pointer using a relative motion. |
| 4640 | // When using spots, the click will occur at the position of the anchor |
| 4641 | // spot and all other spots will move there. |
| 4642 | mPointerController->move(deltaX, deltaY); |
| 4643 | } else { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4644 | mPointerVelocityControl.reset(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4645 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4646 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4647 | float x, y; |
| 4648 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 4649 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4650 | mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4651 | mPointerGesture.currentGestureIdBits.clear(); |
| 4652 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4653 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4654 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4655 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4656 | mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4657 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4658 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4659 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 4660 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4661 | } else if (currentFingerCount == 0) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4662 | // Case 3. No fingers down and button is not pressed. (NEUTRAL) |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4663 | if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) { |
| 4664 | *outFinishPreviousGesture = true; |
| 4665 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4666 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4667 | // Watch for taps coming out of HOVER or TAP_DRAG mode. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4668 | // Checking for taps after TAP_DRAG allows us to detect double-taps. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4669 | bool tapped = false; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4670 | if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER |
| 4671 | || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4672 | && lastFingerCount == 1) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4673 | if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4674 | float x, y; |
| 4675 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4676 | if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop |
| 4677 | && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4678 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4679 | ALOGD("Gestures: TAP"); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4680 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4681 | |
| 4682 | mPointerGesture.tapUpTime = when; |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4683 | getContext()->requestTimeoutAtTime(when |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4684 | + mConfig.pointerGestureTapDragInterval); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4685 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4686 | mPointerGesture.activeGestureId = 0; |
| 4687 | mPointerGesture.currentGestureMode = PointerGesture::TAP; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4688 | mPointerGesture.currentGestureIdBits.clear(); |
| 4689 | mPointerGesture.currentGestureIdBits.markBit( |
| 4690 | mPointerGesture.activeGestureId); |
| 4691 | mPointerGesture.currentGestureIdToIndex[ |
| 4692 | mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4693 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4694 | mPointerGesture.currentGestureProperties[0].id = |
| 4695 | mPointerGesture.activeGestureId; |
| 4696 | mPointerGesture.currentGestureProperties[0].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4697 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4698 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4699 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4700 | AMOTION_EVENT_AXIS_X, mPointerGesture.tapX); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4701 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4702 | AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4703 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
| 4704 | AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4705 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4706 | tapped = true; |
| 4707 | } else { |
| 4708 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4709 | ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f", |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4710 | x - mPointerGesture.tapX, |
| 4711 | y - mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4712 | #endif |
| 4713 | } |
| 4714 | } else { |
| 4715 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4716 | ALOGD("Gestures: Not a TAP, %0.3fms since down", |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4717 | (when - mPointerGesture.tapDownTime) * 0.000001f); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4718 | #endif |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4719 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4720 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4721 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4722 | mPointerVelocityControl.reset(); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4723 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4724 | if (!tapped) { |
| 4725 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4726 | ALOGD("Gestures: NEUTRAL"); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4727 | #endif |
| 4728 | mPointerGesture.activeGestureId = -1; |
| 4729 | mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4730 | mPointerGesture.currentGestureIdBits.clear(); |
| 4731 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4732 | } else if (currentFingerCount == 1) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4733 | // 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] | 4734 | // The pointer follows the active touch point. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4735 | // When in HOVER, emit HOVER_MOVE events at the pointer location. |
| 4736 | // When in TAP_DRAG, emit MOVE events at the pointer location. |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 4737 | ALOG_ASSERT(activeTouchId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4738 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4739 | mPointerGesture.currentGestureMode = PointerGesture::HOVER; |
| 4740 | if (mPointerGesture.lastGestureMode == PointerGesture::TAP) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4741 | if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4742 | float x, y; |
| 4743 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4744 | if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop |
| 4745 | && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4746 | mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG; |
| 4747 | } else { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4748 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4749 | ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f", |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4750 | x - mPointerGesture.tapX, |
| 4751 | y - mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4752 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4753 | } |
| 4754 | } else { |
| 4755 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4756 | ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up", |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4757 | (when - mPointerGesture.tapUpTime) * 0.000001f); |
| 4758 | #endif |
| 4759 | } |
| 4760 | } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) { |
| 4761 | mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG; |
| 4762 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4763 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4764 | if (mLastFingerIdBits.hasBit(activeTouchId)) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4765 | const RawPointerData::Pointer& currentPointer = |
| 4766 | mCurrentRawPointerData.pointerForId(activeTouchId); |
| 4767 | const RawPointerData::Pointer& lastPointer = |
| 4768 | mLastRawPointerData.pointerForId(activeTouchId); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4769 | float deltaX = (currentPointer.x - lastPointer.x) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4770 | * mPointerXMovementScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4771 | float deltaY = (currentPointer.y - lastPointer.y) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4772 | * mPointerYMovementScale; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4773 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4774 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4775 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4776 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4777 | // Move the pointer using a relative motion. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4778 | // 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] | 4779 | mPointerController->move(deltaX, deltaY); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4780 | } else { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4781 | mPointerVelocityControl.reset(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4782 | } |
| 4783 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4784 | bool down; |
| 4785 | if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) { |
| 4786 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4787 | ALOGD("Gestures: TAP_DRAG"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4788 | #endif |
| 4789 | down = true; |
| 4790 | } else { |
| 4791 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4792 | ALOGD("Gestures: HOVER"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4793 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4794 | if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) { |
| 4795 | *outFinishPreviousGesture = true; |
| 4796 | } |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4797 | mPointerGesture.activeGestureId = 0; |
| 4798 | down = false; |
| 4799 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4800 | |
| 4801 | float x, y; |
| 4802 | mPointerController->getPosition(&x, &y); |
| 4803 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4804 | mPointerGesture.currentGestureIdBits.clear(); |
| 4805 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4806 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4807 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4808 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
| 4809 | mPointerGesture.currentGestureProperties[0].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4810 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4811 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4812 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4813 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4814 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, |
| 4815 | down ? 1.0f : 0.0f); |
| 4816 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4817 | if (lastFingerCount == 0 && currentFingerCount != 0) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4818 | mPointerGesture.resetTap(); |
| 4819 | mPointerGesture.tapDownTime = when; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4820 | mPointerGesture.tapX = x; |
| 4821 | mPointerGesture.tapY = y; |
| 4822 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4823 | } else { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4824 | // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM) |
| 4825 | // We need to provide feedback for each finger that goes down so we cannot wait |
| 4826 | // for the fingers to move before deciding what to do. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4827 | // |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4828 | // The ambiguous case is deciding what to do when there are two fingers down but they |
| 4829 | // have not moved enough to determine whether they are part of a drag or part of a |
| 4830 | // freeform gesture, or just a press or long-press at the pointer location. |
| 4831 | // |
| 4832 | // When there are two fingers we start with the PRESS hypothesis and we generate a |
| 4833 | // down at the pointer location. |
| 4834 | // |
| 4835 | // When the two fingers move enough or when additional fingers are added, we make |
| 4836 | // a decision to transition into SWIPE or FREEFORM mode accordingly. |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 4837 | ALOG_ASSERT(activeTouchId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4838 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4839 | bool settled = when >= mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4840 | + mConfig.pointerGestureMultitouchSettleInterval; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4841 | if (mPointerGesture.lastGestureMode != PointerGesture::PRESS |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4842 | && mPointerGesture.lastGestureMode != PointerGesture::SWIPE |
| 4843 | && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4844 | *outFinishPreviousGesture = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4845 | } else if (!settled && currentFingerCount > lastFingerCount) { |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4846 | // Additional pointers have gone down but not yet settled. |
| 4847 | // Reset the gesture. |
| 4848 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4849 | ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, " |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4850 | "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4851 | + mConfig.pointerGestureMultitouchSettleInterval - when) |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4852 | * 0.000001f); |
| 4853 | #endif |
| 4854 | *outCancelPreviousGesture = true; |
| 4855 | } else { |
| 4856 | // Continue previous gesture. |
| 4857 | mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode; |
| 4858 | } |
| 4859 | |
| 4860 | if (*outFinishPreviousGesture || *outCancelPreviousGesture) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4861 | mPointerGesture.currentGestureMode = PointerGesture::PRESS; |
| 4862 | mPointerGesture.activeGestureId = 0; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4863 | mPointerGesture.referenceIdBits.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4864 | mPointerVelocityControl.reset(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4865 | |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4866 | // 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] | 4867 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4868 | ALOGD("Gestures: Using centroid as reference for MULTITOUCH, " |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4869 | "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4870 | + mConfig.pointerGestureMultitouchSettleInterval - when) |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4871 | * 0.000001f); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4872 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4873 | mCurrentRawPointerData.getCentroidOfTouchingPointers( |
| 4874 | &mPointerGesture.referenceTouchX, |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4875 | &mPointerGesture.referenceTouchY); |
| 4876 | mPointerController->getPosition(&mPointerGesture.referenceGestureX, |
| 4877 | &mPointerGesture.referenceGestureY); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4878 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4879 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4880 | // 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] | 4881 | for (BitSet32 idBits(mCurrentFingerIdBits.value |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4882 | & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) { |
| 4883 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4884 | mPointerGesture.referenceDeltas[id].dx = 0; |
| 4885 | mPointerGesture.referenceDeltas[id].dy = 0; |
| 4886 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4887 | mPointerGesture.referenceIdBits = mCurrentFingerIdBits; |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4888 | |
| 4889 | // Add delta for all fingers and calculate a common movement delta. |
| 4890 | float commonDeltaX = 0, commonDeltaY = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4891 | BitSet32 commonIdBits(mLastFingerIdBits.value |
| 4892 | & mCurrentFingerIdBits.value); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4893 | for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) { |
| 4894 | bool first = (idBits == commonIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4895 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 4896 | const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id); |
| 4897 | const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4898 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
| 4899 | delta.dx += cpd.x - lpd.x; |
| 4900 | delta.dy += cpd.y - lpd.y; |
| 4901 | |
| 4902 | if (first) { |
| 4903 | commonDeltaX = delta.dx; |
| 4904 | commonDeltaY = delta.dy; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4905 | } else { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4906 | commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx); |
| 4907 | commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy); |
| 4908 | } |
| 4909 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4910 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4911 | // Consider transitions from PRESS to SWIPE or MULTITOUCH. |
| 4912 | if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) { |
| 4913 | float dist[MAX_POINTER_ID + 1]; |
| 4914 | int32_t distOverThreshold = 0; |
| 4915 | for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4916 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4917 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4918 | dist[id] = hypotf(delta.dx * mPointerXZoomScale, |
| 4919 | delta.dy * mPointerYZoomScale); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4920 | if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4921 | distOverThreshold += 1; |
| 4922 | } |
| 4923 | } |
| 4924 | |
| 4925 | // Only transition when at least two pointers have moved further than |
| 4926 | // the minimum distance threshold. |
| 4927 | if (distOverThreshold >= 2) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4928 | if (currentFingerCount > 2) { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4929 | // There are more than two pointers, switch to FREEFORM. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4930 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4931 | ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4932 | currentFingerCount); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4933 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4934 | *outCancelPreviousGesture = true; |
| 4935 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 4936 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4937 | // There are exactly two pointers. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4938 | BitSet32 idBits(mCurrentFingerIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4939 | uint32_t id1 = idBits.clearFirstMarkedBit(); |
| 4940 | uint32_t id2 = idBits.firstMarkedBit(); |
| 4941 | const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1); |
| 4942 | const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2); |
| 4943 | float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y); |
| 4944 | if (mutualDistance > mPointerGestureMaxSwipeWidth) { |
| 4945 | // There are two pointers but they are too far apart for a SWIPE, |
| 4946 | // switch to FREEFORM. |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4947 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4948 | ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4949 | mutualDistance, mPointerGestureMaxSwipeWidth); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4950 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4951 | *outCancelPreviousGesture = true; |
| 4952 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 4953 | } else { |
| 4954 | // There are two pointers. Wait for both pointers to start moving |
| 4955 | // before deciding whether this is a SWIPE or FREEFORM gesture. |
| 4956 | float dist1 = dist[id1]; |
| 4957 | float dist2 = dist[id2]; |
| 4958 | if (dist1 >= mConfig.pointerGestureMultitouchMinDistance |
| 4959 | && dist2 >= mConfig.pointerGestureMultitouchMinDistance) { |
| 4960 | // Calculate the dot product of the displacement vectors. |
| 4961 | // When the vectors are oriented in approximately the same direction, |
| 4962 | // the angle betweeen them is near zero and the cosine of the angle |
| 4963 | // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2). |
| 4964 | PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1]; |
| 4965 | PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4966 | float dx1 = delta1.dx * mPointerXZoomScale; |
| 4967 | float dy1 = delta1.dy * mPointerYZoomScale; |
| 4968 | float dx2 = delta2.dx * mPointerXZoomScale; |
| 4969 | float dy2 = delta2.dy * mPointerYZoomScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4970 | float dot = dx1 * dx2 + dy1 * dy2; |
| 4971 | float cosine = dot / (dist1 * dist2); // denominator always > 0 |
| 4972 | if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) { |
| 4973 | // Pointers are moving in the same direction. Switch to SWIPE. |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4974 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4975 | ALOGD("Gestures: PRESS transitioned to SWIPE, " |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4976 | "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, " |
| 4977 | "cosine %0.3f >= %0.3f", |
| 4978 | dist1, mConfig.pointerGestureMultitouchMinDistance, |
| 4979 | dist2, mConfig.pointerGestureMultitouchMinDistance, |
| 4980 | cosine, mConfig.pointerGestureSwipeTransitionAngleCosine); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4981 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4982 | mPointerGesture.currentGestureMode = PointerGesture::SWIPE; |
| 4983 | } else { |
| 4984 | // Pointers are moving in different directions. Switch to FREEFORM. |
| 4985 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4986 | ALOGD("Gestures: PRESS transitioned to FREEFORM, " |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4987 | "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, " |
| 4988 | "cosine %0.3f < %0.3f", |
| 4989 | dist1, mConfig.pointerGestureMultitouchMinDistance, |
| 4990 | dist2, mConfig.pointerGestureMultitouchMinDistance, |
| 4991 | cosine, mConfig.pointerGestureSwipeTransitionAngleCosine); |
| 4992 | #endif |
| 4993 | *outCancelPreviousGesture = true; |
| 4994 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 4995 | } |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4996 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4997 | } |
| 4998 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4999 | } |
| 5000 | } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5001 | // Switch from SWIPE to FREEFORM if additional pointers go down. |
| 5002 | // Cancel previous gesture. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5003 | if (currentFingerCount > 2) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5004 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5005 | ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5006 | currentFingerCount); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5007 | #endif |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5008 | *outCancelPreviousGesture = true; |
| 5009 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 5010 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5011 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 5012 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5013 | // Move the reference points based on the overall group motion of the fingers |
| 5014 | // except in PRESS mode while waiting for a transition to occur. |
| 5015 | if (mPointerGesture.currentGestureMode != PointerGesture::PRESS |
| 5016 | && (commonDeltaX || commonDeltaY)) { |
| 5017 | for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5018 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 5019 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5020 | delta.dx = 0; |
| 5021 | delta.dy = 0; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5022 | } |
| 5023 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5024 | mPointerGesture.referenceTouchX += commonDeltaX; |
| 5025 | mPointerGesture.referenceTouchY += commonDeltaY; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 5026 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5027 | commonDeltaX *= mPointerXMovementScale; |
| 5028 | commonDeltaY *= mPointerYMovementScale; |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 5029 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5030 | rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5031 | mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 5032 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5033 | mPointerGesture.referenceGestureX += commonDeltaX; |
| 5034 | mPointerGesture.referenceGestureY += commonDeltaY; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5035 | } |
| 5036 | |
| 5037 | // Report gestures. |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 5038 | if (mPointerGesture.currentGestureMode == PointerGesture::PRESS |
| 5039 | || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) { |
| 5040 | // PRESS or SWIPE mode. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5041 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5042 | ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d," |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5043 | "activeGestureId=%d, currentTouchPointerCount=%d", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5044 | activeTouchId, mPointerGesture.activeGestureId, currentFingerCount); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5045 | #endif |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 5046 | ALOG_ASSERT(mPointerGesture.activeGestureId >= 0); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5047 | |
| 5048 | mPointerGesture.currentGestureIdBits.clear(); |
| 5049 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 5050 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5051 | mPointerGesture.currentGestureProperties[0].clear(); |
| 5052 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
| 5053 | mPointerGesture.currentGestureProperties[0].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5054 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5055 | mPointerGesture.currentGestureCoords[0].clear(); |
| 5056 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 5057 | mPointerGesture.referenceGestureX); |
| 5058 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, |
| 5059 | mPointerGesture.referenceGestureY); |
| 5060 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5061 | } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) { |
| 5062 | // FREEFORM mode. |
| 5063 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5064 | ALOGD("Gestures: FREEFORM activeTouchId=%d," |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5065 | "activeGestureId=%d, currentTouchPointerCount=%d", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5066 | activeTouchId, mPointerGesture.activeGestureId, currentFingerCount); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5067 | #endif |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 5068 | ALOG_ASSERT(mPointerGesture.activeGestureId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5069 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5070 | mPointerGesture.currentGestureIdBits.clear(); |
| 5071 | |
| 5072 | BitSet32 mappedTouchIdBits; |
| 5073 | BitSet32 usedGestureIdBits; |
| 5074 | if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) { |
| 5075 | // Initially, assign the active gesture id to the active touch point |
| 5076 | // if there is one. No other touch id bits are mapped yet. |
| 5077 | if (!*outCancelPreviousGesture) { |
| 5078 | mappedTouchIdBits.markBit(activeTouchId); |
| 5079 | usedGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 5080 | mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] = |
| 5081 | mPointerGesture.activeGestureId; |
| 5082 | } else { |
| 5083 | mPointerGesture.activeGestureId = -1; |
| 5084 | } |
| 5085 | } else { |
| 5086 | // Otherwise, assume we mapped all touches from the previous frame. |
| 5087 | // Reuse all mappings that are still applicable. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5088 | mappedTouchIdBits.value = mLastFingerIdBits.value |
| 5089 | & mCurrentFingerIdBits.value; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5090 | usedGestureIdBits = mPointerGesture.lastGestureIdBits; |
| 5091 | |
| 5092 | // Check whether we need to choose a new active gesture id because the |
| 5093 | // current went went up. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5094 | for (BitSet32 upTouchIdBits(mLastFingerIdBits.value |
| 5095 | & ~mCurrentFingerIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5096 | !upTouchIdBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5097 | uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5098 | uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId]; |
| 5099 | if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) { |
| 5100 | mPointerGesture.activeGestureId = -1; |
| 5101 | break; |
| 5102 | } |
| 5103 | } |
| 5104 | } |
| 5105 | |
| 5106 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5107 | ALOGD("Gestures: FREEFORM follow up " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5108 | "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, " |
| 5109 | "activeGestureId=%d", |
| 5110 | mappedTouchIdBits.value, usedGestureIdBits.value, |
| 5111 | mPointerGesture.activeGestureId); |
| 5112 | #endif |
| 5113 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5114 | BitSet32 idBits(mCurrentFingerIdBits); |
| 5115 | for (uint32_t i = 0; i < currentFingerCount; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5116 | uint32_t touchId = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5117 | uint32_t gestureId; |
| 5118 | if (!mappedTouchIdBits.hasBit(touchId)) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5119 | gestureId = usedGestureIdBits.markFirstUnmarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5120 | mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId; |
| 5121 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5122 | ALOGD("Gestures: FREEFORM " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5123 | "new mapping for touch id %d -> gesture id %d", |
| 5124 | touchId, gestureId); |
| 5125 | #endif |
| 5126 | } else { |
| 5127 | gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId]; |
| 5128 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5129 | ALOGD("Gestures: FREEFORM " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5130 | "existing mapping for touch id %d -> gesture id %d", |
| 5131 | touchId, gestureId); |
| 5132 | #endif |
| 5133 | } |
| 5134 | mPointerGesture.currentGestureIdBits.markBit(gestureId); |
| 5135 | mPointerGesture.currentGestureIdToIndex[gestureId] = i; |
| 5136 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5137 | const RawPointerData::Pointer& pointer = |
| 5138 | mCurrentRawPointerData.pointerForId(touchId); |
| 5139 | float deltaX = (pointer.x - mPointerGesture.referenceTouchX) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5140 | * mPointerXZoomScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5141 | float deltaY = (pointer.y - mPointerGesture.referenceTouchY) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5142 | * mPointerYZoomScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5143 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5144 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5145 | mPointerGesture.currentGestureProperties[i].clear(); |
| 5146 | mPointerGesture.currentGestureProperties[i].id = gestureId; |
| 5147 | mPointerGesture.currentGestureProperties[i].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5148 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5149 | mPointerGesture.currentGestureCoords[i].clear(); |
| 5150 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 5151 | AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5152 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 5153 | AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5154 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
| 5155 | AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
| 5156 | } |
| 5157 | |
| 5158 | if (mPointerGesture.activeGestureId < 0) { |
| 5159 | mPointerGesture.activeGestureId = |
| 5160 | mPointerGesture.currentGestureIdBits.firstMarkedBit(); |
| 5161 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5162 | ALOGD("Gestures: FREEFORM new " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5163 | "activeGestureId=%d", mPointerGesture.activeGestureId); |
| 5164 | #endif |
| 5165 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5166 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5167 | } |
| 5168 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5169 | mPointerController->setButtonState(mCurrentButtonState); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5170 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5171 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5172 | ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, " |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5173 | "currentGestureMode=%d, currentGestureIdBits=0x%08x, " |
| 5174 | "lastGestureMode=%d, lastGestureIdBits=0x%08x", |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5175 | toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture), |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5176 | mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value, |
| 5177 | mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5178 | for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5179 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5180 | uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5181 | const PointerProperties& properties = mPointerGesture.currentGestureProperties[index]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5182 | const PointerCoords& coords = mPointerGesture.currentGestureCoords[index]; |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5183 | ALOGD(" currentGesture[%d]: index=%d, toolType=%d, " |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5184 | "x=%0.3f, y=%0.3f, pressure=%0.3f", |
| 5185 | id, index, properties.toolType, |
| 5186 | coords.getAxisValue(AMOTION_EVENT_AXIS_X), |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5187 | coords.getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 5188 | coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); |
| 5189 | } |
| 5190 | for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5191 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5192 | uint32_t index = mPointerGesture.lastGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5193 | const PointerProperties& properties = mPointerGesture.lastGestureProperties[index]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5194 | const PointerCoords& coords = mPointerGesture.lastGestureCoords[index]; |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5195 | ALOGD(" lastGesture[%d]: index=%d, toolType=%d, " |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5196 | "x=%0.3f, y=%0.3f, pressure=%0.3f", |
| 5197 | id, index, properties.toolType, |
| 5198 | coords.getAxisValue(AMOTION_EVENT_AXIS_X), |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5199 | coords.getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 5200 | coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); |
| 5201 | } |
| 5202 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 5203 | return true; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5204 | } |
| 5205 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5206 | void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) { |
| 5207 | mPointerSimple.currentCoords.clear(); |
| 5208 | mPointerSimple.currentProperties.clear(); |
| 5209 | |
| 5210 | bool down, hovering; |
| 5211 | if (!mCurrentStylusIdBits.isEmpty()) { |
| 5212 | uint32_t id = mCurrentStylusIdBits.firstMarkedBit(); |
| 5213 | uint32_t index = mCurrentCookedPointerData.idToIndex[id]; |
| 5214 | float x = mCurrentCookedPointerData.pointerCoords[index].getX(); |
| 5215 | float y = mCurrentCookedPointerData.pointerCoords[index].getY(); |
| 5216 | mPointerController->setPosition(x, y); |
| 5217 | |
| 5218 | hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id); |
| 5219 | down = !hovering; |
| 5220 | |
| 5221 | mPointerController->getPosition(&x, &y); |
| 5222 | mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]); |
| 5223 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 5224 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 5225 | mPointerSimple.currentProperties.id = 0; |
| 5226 | mPointerSimple.currentProperties.toolType = |
| 5227 | mCurrentCookedPointerData.pointerProperties[index].toolType; |
| 5228 | } else { |
| 5229 | down = false; |
| 5230 | hovering = false; |
| 5231 | } |
| 5232 | |
| 5233 | dispatchPointerSimple(when, policyFlags, down, hovering); |
| 5234 | } |
| 5235 | |
| 5236 | void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) { |
| 5237 | abortPointerSimple(when, policyFlags); |
| 5238 | } |
| 5239 | |
| 5240 | void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) { |
| 5241 | mPointerSimple.currentCoords.clear(); |
| 5242 | mPointerSimple.currentProperties.clear(); |
| 5243 | |
| 5244 | bool down, hovering; |
| 5245 | if (!mCurrentMouseIdBits.isEmpty()) { |
| 5246 | uint32_t id = mCurrentMouseIdBits.firstMarkedBit(); |
| 5247 | uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id]; |
| 5248 | if (mLastMouseIdBits.hasBit(id)) { |
| 5249 | uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id]; |
| 5250 | float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x |
| 5251 | - mLastRawPointerData.pointers[lastIndex].x) |
| 5252 | * mPointerXMovementScale; |
| 5253 | float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y |
| 5254 | - mLastRawPointerData.pointers[lastIndex].y) |
| 5255 | * mPointerYMovementScale; |
| 5256 | |
| 5257 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
| 5258 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
| 5259 | |
| 5260 | mPointerController->move(deltaX, deltaY); |
| 5261 | } else { |
| 5262 | mPointerVelocityControl.reset(); |
| 5263 | } |
| 5264 | |
| 5265 | down = isPointerDown(mCurrentButtonState); |
| 5266 | hovering = !down; |
| 5267 | |
| 5268 | float x, y; |
| 5269 | mPointerController->getPosition(&x, &y); |
| 5270 | mPointerSimple.currentCoords.copyFrom( |
| 5271 | mCurrentCookedPointerData.pointerCoords[currentIndex]); |
| 5272 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 5273 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 5274 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, |
| 5275 | hovering ? 0.0f : 1.0f); |
| 5276 | mPointerSimple.currentProperties.id = 0; |
| 5277 | mPointerSimple.currentProperties.toolType = |
| 5278 | mCurrentCookedPointerData.pointerProperties[currentIndex].toolType; |
| 5279 | } else { |
| 5280 | mPointerVelocityControl.reset(); |
| 5281 | |
| 5282 | down = false; |
| 5283 | hovering = false; |
| 5284 | } |
| 5285 | |
| 5286 | dispatchPointerSimple(when, policyFlags, down, hovering); |
| 5287 | } |
| 5288 | |
| 5289 | void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) { |
| 5290 | abortPointerSimple(when, policyFlags); |
| 5291 | |
| 5292 | mPointerVelocityControl.reset(); |
| 5293 | } |
| 5294 | |
| 5295 | void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags, |
| 5296 | bool down, bool hovering) { |
| 5297 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 5298 | |
| 5299 | if (mPointerController != NULL) { |
| 5300 | if (down || hovering) { |
| 5301 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER); |
| 5302 | mPointerController->clearSpots(); |
| 5303 | mPointerController->setButtonState(mCurrentButtonState); |
| 5304 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 5305 | } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) { |
| 5306 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 5307 | } |
| 5308 | } |
| 5309 | |
| 5310 | if (mPointerSimple.down && !down) { |
| 5311 | mPointerSimple.down = false; |
| 5312 | |
| 5313 | // Send up. |
| 5314 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5315 | AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0, |
| 5316 | 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords, |
| 5317 | mOrientedXPrecision, mOrientedYPrecision, |
| 5318 | mPointerSimple.downTime); |
| 5319 | getListener()->notifyMotion(&args); |
| 5320 | } |
| 5321 | |
| 5322 | if (mPointerSimple.hovering && !hovering) { |
| 5323 | mPointerSimple.hovering = false; |
| 5324 | |
| 5325 | // Send hover exit. |
| 5326 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5327 | AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0, |
| 5328 | 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords, |
| 5329 | mOrientedXPrecision, mOrientedYPrecision, |
| 5330 | mPointerSimple.downTime); |
| 5331 | getListener()->notifyMotion(&args); |
| 5332 | } |
| 5333 | |
| 5334 | if (down) { |
| 5335 | if (!mPointerSimple.down) { |
| 5336 | mPointerSimple.down = true; |
| 5337 | mPointerSimple.downTime = when; |
| 5338 | |
| 5339 | // Send down. |
| 5340 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5341 | AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0, |
| 5342 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5343 | mOrientedXPrecision, mOrientedYPrecision, |
| 5344 | mPointerSimple.downTime); |
| 5345 | getListener()->notifyMotion(&args); |
| 5346 | } |
| 5347 | |
| 5348 | // Send move. |
| 5349 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5350 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0, |
| 5351 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5352 | mOrientedXPrecision, mOrientedYPrecision, |
| 5353 | mPointerSimple.downTime); |
| 5354 | getListener()->notifyMotion(&args); |
| 5355 | } |
| 5356 | |
| 5357 | if (hovering) { |
| 5358 | if (!mPointerSimple.hovering) { |
| 5359 | mPointerSimple.hovering = true; |
| 5360 | |
| 5361 | // Send hover enter. |
| 5362 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5363 | AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0, |
| 5364 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5365 | mOrientedXPrecision, mOrientedYPrecision, |
| 5366 | mPointerSimple.downTime); |
| 5367 | getListener()->notifyMotion(&args); |
| 5368 | } |
| 5369 | |
| 5370 | // Send hover move. |
| 5371 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5372 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0, |
| 5373 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5374 | mOrientedXPrecision, mOrientedYPrecision, |
| 5375 | mPointerSimple.downTime); |
| 5376 | getListener()->notifyMotion(&args); |
| 5377 | } |
| 5378 | |
| 5379 | if (mCurrentRawVScroll || mCurrentRawHScroll) { |
| 5380 | float vscroll = mCurrentRawVScroll; |
| 5381 | float hscroll = mCurrentRawHScroll; |
| 5382 | mWheelYVelocityControl.move(when, NULL, &vscroll); |
| 5383 | mWheelXVelocityControl.move(when, &hscroll, NULL); |
| 5384 | |
| 5385 | // Send scroll. |
| 5386 | PointerCoords pointerCoords; |
| 5387 | pointerCoords.copyFrom(mPointerSimple.currentCoords); |
| 5388 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll); |
| 5389 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll); |
| 5390 | |
| 5391 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5392 | AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0, |
| 5393 | 1, &mPointerSimple.currentProperties, &pointerCoords, |
| 5394 | mOrientedXPrecision, mOrientedYPrecision, |
| 5395 | mPointerSimple.downTime); |
| 5396 | getListener()->notifyMotion(&args); |
| 5397 | } |
| 5398 | |
| 5399 | // Save state. |
| 5400 | if (down || hovering) { |
| 5401 | mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords); |
| 5402 | mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties); |
| 5403 | } else { |
| 5404 | mPointerSimple.reset(); |
| 5405 | } |
| 5406 | } |
| 5407 | |
| 5408 | void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) { |
| 5409 | mPointerSimple.currentCoords.clear(); |
| 5410 | mPointerSimple.currentProperties.clear(); |
| 5411 | |
| 5412 | dispatchPointerSimple(when, policyFlags, false, false); |
| 5413 | } |
| 5414 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5415 | void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5416 | int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags, |
| 5417 | const PointerProperties* properties, const PointerCoords* coords, |
| 5418 | const uint32_t* idToIndex, BitSet32 idBits, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5419 | int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) { |
| 5420 | PointerCoords pointerCoords[MAX_POINTERS]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5421 | PointerProperties pointerProperties[MAX_POINTERS]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5422 | uint32_t pointerCount = 0; |
| 5423 | while (!idBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5424 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5425 | uint32_t index = idToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5426 | pointerProperties[pointerCount].copyFrom(properties[index]); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5427 | pointerCoords[pointerCount].copyFrom(coords[index]); |
| 5428 | |
| 5429 | if (changedId >= 0 && id == uint32_t(changedId)) { |
| 5430 | action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
| 5431 | } |
| 5432 | |
| 5433 | pointerCount += 1; |
| 5434 | } |
| 5435 | |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 5436 | ALOG_ASSERT(pointerCount != 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5437 | |
| 5438 | if (changedId >= 0 && pointerCount == 1) { |
| 5439 | // Replace initial down and final up action. |
| 5440 | // We can compare the action without masking off the changed pointer index |
| 5441 | // because we know the index is 0. |
| 5442 | if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 5443 | action = AMOTION_EVENT_ACTION_DOWN; |
| 5444 | } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 5445 | action = AMOTION_EVENT_ACTION_UP; |
| 5446 | } else { |
| 5447 | // Can't happen. |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 5448 | ALOG_ASSERT(false); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5449 | } |
| 5450 | } |
| 5451 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5452 | NotifyMotionArgs args(when, getDeviceId(), source, policyFlags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5453 | action, flags, metaState, buttonState, edgeFlags, |
| 5454 | pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5455 | getListener()->notifyMotion(&args); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5456 | } |
| 5457 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5458 | bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5459 | const PointerCoords* inCoords, const uint32_t* inIdToIndex, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5460 | PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex, |
| 5461 | BitSet32 idBits) const { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5462 | bool changed = false; |
| 5463 | while (!idBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5464 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5465 | uint32_t inIndex = inIdToIndex[id]; |
| 5466 | uint32_t outIndex = outIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5467 | |
| 5468 | const PointerProperties& curInProperties = inProperties[inIndex]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5469 | const PointerCoords& curInCoords = inCoords[inIndex]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5470 | PointerProperties& curOutProperties = outProperties[outIndex]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5471 | PointerCoords& curOutCoords = outCoords[outIndex]; |
| 5472 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5473 | if (curInProperties != curOutProperties) { |
| 5474 | curOutProperties.copyFrom(curInProperties); |
| 5475 | changed = true; |
| 5476 | } |
| 5477 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5478 | if (curInCoords != curOutCoords) { |
| 5479 | curOutCoords.copyFrom(curInCoords); |
| 5480 | changed = true; |
| 5481 | } |
| 5482 | } |
| 5483 | return changed; |
| 5484 | } |
| 5485 | |
| 5486 | void TouchInputMapper::fadePointer() { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5487 | if (mPointerController != NULL) { |
| 5488 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 5489 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5490 | } |
| 5491 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5492 | bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) { |
| 5493 | return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue |
| 5494 | && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5495 | } |
| 5496 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5497 | const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit( |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 5498 | int32_t x, int32_t y) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5499 | size_t numVirtualKeys = mVirtualKeys.size(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 5500 | for (size_t i = 0; i < numVirtualKeys; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5501 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5502 | |
| 5503 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5504 | ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, " |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5505 | "left=%d, top=%d, right=%d, bottom=%d", |
| 5506 | x, y, |
| 5507 | virtualKey.keyCode, virtualKey.scanCode, |
| 5508 | virtualKey.hitLeft, virtualKey.hitTop, |
| 5509 | virtualKey.hitRight, virtualKey.hitBottom); |
| 5510 | #endif |
| 5511 | |
| 5512 | if (virtualKey.isHit(x, y)) { |
| 5513 | return & virtualKey; |
| 5514 | } |
| 5515 | } |
| 5516 | |
| 5517 | return NULL; |
| 5518 | } |
| 5519 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5520 | void TouchInputMapper::assignPointerIds() { |
| 5521 | uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount; |
| 5522 | uint32_t lastPointerCount = mLastRawPointerData.pointerCount; |
| 5523 | |
| 5524 | mCurrentRawPointerData.clearIdBits(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5525 | |
| 5526 | if (currentPointerCount == 0) { |
| 5527 | // No pointers to assign. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5528 | return; |
| 5529 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5530 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5531 | if (lastPointerCount == 0) { |
| 5532 | // All pointers are new. |
| 5533 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
| 5534 | uint32_t id = i; |
| 5535 | mCurrentRawPointerData.pointers[i].id = id; |
| 5536 | mCurrentRawPointerData.idToIndex[id] = i; |
| 5537 | mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i)); |
| 5538 | } |
| 5539 | return; |
| 5540 | } |
| 5541 | |
| 5542 | if (currentPointerCount == 1 && lastPointerCount == 1 |
| 5543 | && mCurrentRawPointerData.pointers[0].toolType |
| 5544 | == mLastRawPointerData.pointers[0].toolType) { |
| 5545 | // Only one pointer and no change in count so it must have the same id as before. |
| 5546 | uint32_t id = mLastRawPointerData.pointers[0].id; |
| 5547 | mCurrentRawPointerData.pointers[0].id = id; |
| 5548 | mCurrentRawPointerData.idToIndex[id] = 0; |
| 5549 | mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0)); |
| 5550 | return; |
| 5551 | } |
| 5552 | |
| 5553 | // General case. |
| 5554 | // We build a heap of squared euclidean distances between current and last pointers |
| 5555 | // associated with the current and last pointer indices. Then, we find the best |
| 5556 | // match (by distance) for each current pointer. |
| 5557 | // The pointers must have the same tool type but it is possible for them to |
| 5558 | // transition from hovering to touching or vice-versa while retaining the same id. |
| 5559 | PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS]; |
| 5560 | |
| 5561 | uint32_t heapSize = 0; |
| 5562 | for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount; |
| 5563 | currentPointerIndex++) { |
| 5564 | for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount; |
| 5565 | lastPointerIndex++) { |
| 5566 | const RawPointerData::Pointer& currentPointer = |
| 5567 | mCurrentRawPointerData.pointers[currentPointerIndex]; |
| 5568 | const RawPointerData::Pointer& lastPointer = |
| 5569 | mLastRawPointerData.pointers[lastPointerIndex]; |
| 5570 | if (currentPointer.toolType == lastPointer.toolType) { |
| 5571 | int64_t deltaX = currentPointer.x - lastPointer.x; |
| 5572 | int64_t deltaY = currentPointer.y - lastPointer.y; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5573 | |
| 5574 | uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY); |
| 5575 | |
| 5576 | // Insert new element into the heap (sift up). |
| 5577 | heap[heapSize].currentPointerIndex = currentPointerIndex; |
| 5578 | heap[heapSize].lastPointerIndex = lastPointerIndex; |
| 5579 | heap[heapSize].distance = distance; |
| 5580 | heapSize += 1; |
| 5581 | } |
| 5582 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5583 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5584 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5585 | // Heapify |
| 5586 | for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) { |
| 5587 | startIndex -= 1; |
| 5588 | for (uint32_t parentIndex = startIndex; ;) { |
| 5589 | uint32_t childIndex = parentIndex * 2 + 1; |
| 5590 | if (childIndex >= heapSize) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5591 | break; |
| 5592 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5593 | |
| 5594 | if (childIndex + 1 < heapSize |
| 5595 | && heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 5596 | childIndex += 1; |
| 5597 | } |
| 5598 | |
| 5599 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 5600 | break; |
| 5601 | } |
| 5602 | |
| 5603 | swap(heap[parentIndex], heap[childIndex]); |
| 5604 | parentIndex = childIndex; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5605 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5606 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5607 | |
| 5608 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5609 | ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5610 | for (size_t i = 0; i < heapSize; i++) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5611 | ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5612 | i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, |
| 5613 | heap[i].distance); |
| 5614 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5615 | #endif |
| 5616 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5617 | // Pull matches out by increasing order of distance. |
| 5618 | // To avoid reassigning pointers that have already been matched, the loop keeps track |
| 5619 | // of which last and current pointers have been matched using the matchedXXXBits variables. |
| 5620 | // It also tracks the used pointer id bits. |
| 5621 | BitSet32 matchedLastBits(0); |
| 5622 | BitSet32 matchedCurrentBits(0); |
| 5623 | BitSet32 usedIdBits(0); |
| 5624 | bool first = true; |
| 5625 | for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) { |
| 5626 | while (heapSize > 0) { |
| 5627 | if (first) { |
| 5628 | // The first time through the loop, we just consume the root element of |
| 5629 | // the heap (the one with smallest distance). |
| 5630 | first = false; |
| 5631 | } else { |
| 5632 | // Previous iterations consumed the root element of the heap. |
| 5633 | // Pop root element off of the heap (sift down). |
| 5634 | heap[0] = heap[heapSize]; |
| 5635 | for (uint32_t parentIndex = 0; ;) { |
| 5636 | uint32_t childIndex = parentIndex * 2 + 1; |
| 5637 | if (childIndex >= heapSize) { |
| 5638 | break; |
| 5639 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5640 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5641 | if (childIndex + 1 < heapSize |
| 5642 | && heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 5643 | childIndex += 1; |
| 5644 | } |
| 5645 | |
| 5646 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 5647 | break; |
| 5648 | } |
| 5649 | |
| 5650 | swap(heap[parentIndex], heap[childIndex]); |
| 5651 | parentIndex = childIndex; |
| 5652 | } |
| 5653 | |
| 5654 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5655 | ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5656 | for (size_t i = 0; i < heapSize; i++) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5657 | ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5658 | i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, |
| 5659 | heap[i].distance); |
| 5660 | } |
| 5661 | #endif |
| 5662 | } |
| 5663 | |
| 5664 | heapSize -= 1; |
| 5665 | |
| 5666 | uint32_t currentPointerIndex = heap[0].currentPointerIndex; |
| 5667 | if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched |
| 5668 | |
| 5669 | uint32_t lastPointerIndex = heap[0].lastPointerIndex; |
| 5670 | if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched |
| 5671 | |
| 5672 | matchedCurrentBits.markBit(currentPointerIndex); |
| 5673 | matchedLastBits.markBit(lastPointerIndex); |
| 5674 | |
| 5675 | uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id; |
| 5676 | mCurrentRawPointerData.pointers[currentPointerIndex].id = id; |
| 5677 | mCurrentRawPointerData.idToIndex[id] = currentPointerIndex; |
| 5678 | mCurrentRawPointerData.markIdBit(id, |
| 5679 | mCurrentRawPointerData.isHovering(currentPointerIndex)); |
| 5680 | usedIdBits.markBit(id); |
| 5681 | |
| 5682 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5683 | ALOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5684 | lastPointerIndex, currentPointerIndex, id, heap[0].distance); |
| 5685 | #endif |
| 5686 | break; |
| 5687 | } |
| 5688 | } |
| 5689 | |
| 5690 | // Assign fresh ids to pointers that were not matched in the process. |
| 5691 | for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) { |
| 5692 | uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit(); |
| 5693 | uint32_t id = usedIdBits.markFirstUnmarkedBit(); |
| 5694 | |
| 5695 | mCurrentRawPointerData.pointers[currentPointerIndex].id = id; |
| 5696 | mCurrentRawPointerData.idToIndex[id] = currentPointerIndex; |
| 5697 | mCurrentRawPointerData.markIdBit(id, |
| 5698 | mCurrentRawPointerData.isHovering(currentPointerIndex)); |
| 5699 | |
| 5700 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5701 | ALOGD("assignPointerIds - assigned: cur=%d, id=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5702 | currentPointerIndex, id); |
| 5703 | #endif |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5704 | } |
| 5705 | } |
| 5706 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5707 | int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5708 | if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) { |
| 5709 | return AKEY_STATE_VIRTUAL; |
| 5710 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5711 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5712 | size_t numVirtualKeys = mVirtualKeys.size(); |
| 5713 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 5714 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
| 5715 | if (virtualKey.keyCode == keyCode) { |
| 5716 | return AKEY_STATE_UP; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5717 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5718 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5719 | |
| 5720 | return AKEY_STATE_UNKNOWN; |
| 5721 | } |
| 5722 | |
| 5723 | int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5724 | if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) { |
| 5725 | return AKEY_STATE_VIRTUAL; |
| 5726 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5727 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5728 | size_t numVirtualKeys = mVirtualKeys.size(); |
| 5729 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 5730 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
| 5731 | if (virtualKey.scanCode == scanCode) { |
| 5732 | return AKEY_STATE_UP; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5733 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5734 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5735 | |
| 5736 | return AKEY_STATE_UNKNOWN; |
| 5737 | } |
| 5738 | |
| 5739 | bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 5740 | const int32_t* keyCodes, uint8_t* outFlags) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5741 | size_t numVirtualKeys = mVirtualKeys.size(); |
| 5742 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 5743 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5744 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5745 | for (size_t i = 0; i < numCodes; i++) { |
| 5746 | if (virtualKey.keyCode == keyCodes[i]) { |
| 5747 | outFlags[i] = 1; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5748 | } |
| 5749 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5750 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5751 | |
| 5752 | return true; |
| 5753 | } |
| 5754 | |
| 5755 | |
| 5756 | // --- SingleTouchInputMapper --- |
| 5757 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 5758 | SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) : |
| 5759 | TouchInputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5760 | } |
| 5761 | |
| 5762 | SingleTouchInputMapper::~SingleTouchInputMapper() { |
| 5763 | } |
| 5764 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5765 | void SingleTouchInputMapper::reset(nsecs_t when) { |
| 5766 | mSingleTouchMotionAccumulator.reset(getDevice()); |
| 5767 | |
| 5768 | TouchInputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5769 | } |
| 5770 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5771 | void SingleTouchInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5772 | TouchInputMapper::process(rawEvent); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5773 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5774 | mSingleTouchMotionAccumulator.process(rawEvent); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5775 | } |
| 5776 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5777 | void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) { |
Jeff Brown | d87c6d5 | 2011-08-10 14:55:59 -0700 | [diff] [blame] | 5778 | if (mTouchButtonAccumulator.isToolActive()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5779 | mCurrentRawPointerData.pointerCount = 1; |
| 5780 | mCurrentRawPointerData.idToIndex[0] = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5781 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5782 | bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE |
| 5783 | && (mTouchButtonAccumulator.isHovering() |
| 5784 | || (mRawPointerAxes.pressure.valid |
| 5785 | && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0)); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5786 | mCurrentRawPointerData.markIdBit(0, isHovering); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5787 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5788 | RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0]; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5789 | outPointer.id = 0; |
| 5790 | outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX(); |
| 5791 | outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY(); |
| 5792 | outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure(); |
| 5793 | outPointer.touchMajor = 0; |
| 5794 | outPointer.touchMinor = 0; |
| 5795 | outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth(); |
| 5796 | outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth(); |
| 5797 | outPointer.orientation = 0; |
| 5798 | outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5799 | outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX(); |
| 5800 | outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY(); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5801 | outPointer.toolType = mTouchButtonAccumulator.getToolType(); |
| 5802 | if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 5803 | outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 5804 | } |
| 5805 | outPointer.isHovering = isHovering; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5806 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5807 | } |
| 5808 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5809 | void SingleTouchInputMapper::configureRawPointerAxes() { |
| 5810 | TouchInputMapper::configureRawPointerAxes(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5811 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5812 | getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x); |
| 5813 | getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y); |
| 5814 | getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure); |
| 5815 | getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor); |
| 5816 | getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5817 | getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX); |
| 5818 | getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5819 | } |
| 5820 | |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 5821 | bool SingleTouchInputMapper::hasStylus() const { |
| 5822 | return mTouchButtonAccumulator.hasStylus(); |
| 5823 | } |
| 5824 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5825 | |
| 5826 | // --- MultiTouchInputMapper --- |
| 5827 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 5828 | MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) : |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5829 | TouchInputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5830 | } |
| 5831 | |
| 5832 | MultiTouchInputMapper::~MultiTouchInputMapper() { |
| 5833 | } |
| 5834 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5835 | void MultiTouchInputMapper::reset(nsecs_t when) { |
| 5836 | mMultiTouchMotionAccumulator.reset(getDevice()); |
| 5837 | |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5838 | mPointerIdBits.clear(); |
Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 5839 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5840 | TouchInputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5841 | } |
| 5842 | |
| 5843 | void MultiTouchInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5844 | TouchInputMapper::process(rawEvent); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5845 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5846 | mMultiTouchMotionAccumulator.process(rawEvent); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5847 | } |
| 5848 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5849 | void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5850 | size_t inCount = mMultiTouchMotionAccumulator.getSlotCount(); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5851 | size_t outCount = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5852 | BitSet32 newPointerIdBits; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5853 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5854 | for (size_t inIndex = 0; inIndex < inCount; inIndex++) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5855 | const MultiTouchMotionAccumulator::Slot* inSlot = |
| 5856 | mMultiTouchMotionAccumulator.getSlot(inIndex); |
| 5857 | if (!inSlot->isInUse()) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5858 | continue; |
| 5859 | } |
| 5860 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5861 | if (outCount >= MAX_POINTERS) { |
| 5862 | #if DEBUG_POINTERS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5863 | ALOGD("MultiTouch device %s emitted more than maximum of %d pointers; " |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5864 | "ignoring the rest.", |
| 5865 | getDeviceName().string(), MAX_POINTERS); |
| 5866 | #endif |
| 5867 | break; // too many fingers! |
| 5868 | } |
| 5869 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5870 | RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount]; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5871 | outPointer.x = inSlot->getX(); |
| 5872 | outPointer.y = inSlot->getY(); |
| 5873 | outPointer.pressure = inSlot->getPressure(); |
| 5874 | outPointer.touchMajor = inSlot->getTouchMajor(); |
| 5875 | outPointer.touchMinor = inSlot->getTouchMinor(); |
| 5876 | outPointer.toolMajor = inSlot->getToolMajor(); |
| 5877 | outPointer.toolMinor = inSlot->getToolMinor(); |
| 5878 | outPointer.orientation = inSlot->getOrientation(); |
| 5879 | outPointer.distance = inSlot->getDistance(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5880 | outPointer.tiltX = 0; |
| 5881 | outPointer.tiltY = 0; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5882 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5883 | outPointer.toolType = inSlot->getToolType(); |
| 5884 | if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 5885 | outPointer.toolType = mTouchButtonAccumulator.getToolType(); |
| 5886 | if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 5887 | outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 5888 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5889 | } |
| 5890 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5891 | bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE |
| 5892 | && (mTouchButtonAccumulator.isHovering() |
| 5893 | || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0)); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5894 | outPointer.isHovering = isHovering; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5895 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5896 | // Assign pointer id using tracking id if available. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5897 | if (*outHavePointerIds) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5898 | int32_t trackingId = inSlot->getTrackingId(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5899 | int32_t id = -1; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5900 | if (trackingId >= 0) { |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5901 | for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5902 | uint32_t n = idBits.clearFirstMarkedBit(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5903 | if (mPointerTrackingIdMap[n] == trackingId) { |
| 5904 | id = n; |
| 5905 | } |
| 5906 | } |
| 5907 | |
| 5908 | if (id < 0 && !mPointerIdBits.isFull()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5909 | id = mPointerIdBits.markFirstUnmarkedBit(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5910 | mPointerTrackingIdMap[id] = trackingId; |
| 5911 | } |
| 5912 | } |
| 5913 | if (id < 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5914 | *outHavePointerIds = false; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5915 | mCurrentRawPointerData.clearIdBits(); |
| 5916 | newPointerIdBits.clear(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5917 | } else { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5918 | outPointer.id = id; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5919 | mCurrentRawPointerData.idToIndex[id] = outCount; |
| 5920 | mCurrentRawPointerData.markIdBit(id, isHovering); |
| 5921 | newPointerIdBits.markBit(id); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5922 | } |
| 5923 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5924 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5925 | outCount += 1; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5926 | } |
| 5927 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5928 | mCurrentRawPointerData.pointerCount = outCount; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5929 | mPointerIdBits = newPointerIdBits; |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5930 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5931 | mMultiTouchMotionAccumulator.finishSync(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5932 | } |
| 5933 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5934 | void MultiTouchInputMapper::configureRawPointerAxes() { |
| 5935 | TouchInputMapper::configureRawPointerAxes(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5936 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5937 | getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x); |
| 5938 | getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y); |
| 5939 | getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor); |
| 5940 | getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor); |
| 5941 | getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor); |
| 5942 | getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor); |
| 5943 | getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation); |
| 5944 | getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure); |
| 5945 | getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance); |
| 5946 | getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId); |
| 5947 | getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5948 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5949 | if (mRawPointerAxes.trackingId.valid |
| 5950 | && mRawPointerAxes.slot.valid |
| 5951 | && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) { |
| 5952 | size_t slotCount = mRawPointerAxes.slot.maxValue + 1; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5953 | if (slotCount > MAX_SLOTS) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 5954 | ALOGW("MultiTouch Device %s reported %d slots but the framework " |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5955 | "only supports a maximum of %d slots at this time.", |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5956 | getDeviceName().string(), slotCount, MAX_SLOTS); |
| 5957 | slotCount = MAX_SLOTS; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5958 | } |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 5959 | mMultiTouchMotionAccumulator.configure(getDevice(), |
| 5960 | slotCount, true /*usingSlotsProtocol*/); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5961 | } else { |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 5962 | mMultiTouchMotionAccumulator.configure(getDevice(), |
| 5963 | MAX_POINTERS, false /*usingSlotsProtocol*/); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5964 | } |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 5965 | } |
| 5966 | |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 5967 | bool MultiTouchInputMapper::hasStylus() const { |
| 5968 | return mMultiTouchMotionAccumulator.hasStylus() |
| 5969 | || mTouchButtonAccumulator.hasStylus(); |
| 5970 | } |
| 5971 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5972 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5973 | // --- JoystickInputMapper --- |
| 5974 | |
| 5975 | JoystickInputMapper::JoystickInputMapper(InputDevice* device) : |
| 5976 | InputMapper(device) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5977 | } |
| 5978 | |
| 5979 | JoystickInputMapper::~JoystickInputMapper() { |
| 5980 | } |
| 5981 | |
| 5982 | uint32_t JoystickInputMapper::getSources() { |
| 5983 | return AINPUT_SOURCE_JOYSTICK; |
| 5984 | } |
| 5985 | |
| 5986 | void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 5987 | InputMapper::populateDeviceInfo(info); |
| 5988 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5989 | for (size_t i = 0; i < mAxes.size(); i++) { |
| 5990 | const Axis& axis = mAxes.valueAt(i); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 5991 | info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK, |
| 5992 | axis.min, axis.max, axis.flat, axis.fuzz); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5993 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 5994 | info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK, |
| 5995 | axis.min, axis.max, axis.flat, axis.fuzz); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5996 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5997 | } |
| 5998 | } |
| 5999 | |
| 6000 | void JoystickInputMapper::dump(String8& dump) { |
| 6001 | dump.append(INDENT2 "Joystick Input Mapper:\n"); |
| 6002 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6003 | dump.append(INDENT3 "Axes:\n"); |
| 6004 | size_t numAxes = mAxes.size(); |
| 6005 | for (size_t i = 0; i < numAxes; i++) { |
| 6006 | const Axis& axis = mAxes.valueAt(i); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6007 | const char* label = getAxisLabel(axis.axisInfo.axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6008 | if (label) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6009 | dump.appendFormat(INDENT4 "%s", label); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6010 | } else { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6011 | dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6012 | } |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6013 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 6014 | label = getAxisLabel(axis.axisInfo.highAxis); |
| 6015 | if (label) { |
| 6016 | dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue); |
| 6017 | } else { |
| 6018 | dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis, |
| 6019 | axis.axisInfo.splitValue); |
| 6020 | } |
| 6021 | } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) { |
| 6022 | dump.append(" (invert)"); |
| 6023 | } |
| 6024 | |
| 6025 | dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n", |
| 6026 | axis.min, axis.max, axis.flat, axis.fuzz); |
| 6027 | dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, " |
| 6028 | "highScale=%0.5f, highOffset=%0.5f\n", |
| 6029 | axis.scale, axis.offset, axis.highScale, axis.highOffset); |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 6030 | dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, " |
| 6031 | "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n", |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6032 | mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue, |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 6033 | axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6034 | } |
| 6035 | } |
| 6036 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 6037 | void JoystickInputMapper::configure(nsecs_t when, |
| 6038 | const InputReaderConfiguration* config, uint32_t changes) { |
| 6039 | InputMapper::configure(when, config, changes); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6040 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 6041 | if (!changes) { // first time only |
| 6042 | // Collect all axes. |
| 6043 | for (int32_t abs = 0; abs <= ABS_MAX; abs++) { |
Jeff Brown | 9ee285af | 2011-08-31 12:56:34 -0700 | [diff] [blame] | 6044 | if (!(getAbsAxisUsage(abs, getDevice()->getClasses()) |
| 6045 | & INPUT_DEVICE_CLASS_JOYSTICK)) { |
| 6046 | continue; // axis must be claimed by a different device |
| 6047 | } |
| 6048 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 6049 | RawAbsoluteAxisInfo rawAxisInfo; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 6050 | getAbsoluteAxisInfo(abs, &rawAxisInfo); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 6051 | if (rawAxisInfo.valid) { |
| 6052 | // Map axis. |
| 6053 | AxisInfo axisInfo; |
| 6054 | bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo); |
| 6055 | if (!explicitlyMapped) { |
| 6056 | // Axis is not explicitly mapped, will choose a generic axis later. |
| 6057 | axisInfo.mode = AxisInfo::MODE_NORMAL; |
| 6058 | axisInfo.axis = -1; |
| 6059 | } |
| 6060 | |
| 6061 | // Apply flat override. |
| 6062 | int32_t rawFlat = axisInfo.flatOverride < 0 |
| 6063 | ? rawAxisInfo.flat : axisInfo.flatOverride; |
| 6064 | |
| 6065 | // Calculate scaling factors and limits. |
| 6066 | Axis axis; |
| 6067 | if (axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 6068 | float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue); |
| 6069 | float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue); |
| 6070 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 6071 | scale, 0.0f, highScale, 0.0f, |
| 6072 | 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
| 6073 | } else if (isCenteredAxis(axisInfo.axis)) { |
| 6074 | float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); |
| 6075 | float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale; |
| 6076 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 6077 | scale, offset, scale, offset, |
| 6078 | -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
| 6079 | } else { |
| 6080 | float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); |
| 6081 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 6082 | scale, 0.0f, scale, 0.0f, |
| 6083 | 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
| 6084 | } |
| 6085 | |
| 6086 | // To eliminate noise while the joystick is at rest, filter out small variations |
| 6087 | // in axis values up front. |
| 6088 | axis.filter = axis.flat * 0.25f; |
| 6089 | |
| 6090 | mAxes.add(abs, axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6091 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6092 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6093 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 6094 | // If there are too many axes, start dropping them. |
| 6095 | // Prefer to keep explicitly mapped axes. |
| 6096 | if (mAxes.size() > PointerCoords::MAX_AXES) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 6097 | 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] | 6098 | getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES); |
| 6099 | pruneAxes(true); |
| 6100 | pruneAxes(false); |
| 6101 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6102 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 6103 | // Assign generic axis ids to remaining axes. |
| 6104 | int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1; |
| 6105 | size_t numAxes = mAxes.size(); |
| 6106 | for (size_t i = 0; i < numAxes; i++) { |
| 6107 | Axis& axis = mAxes.editValueAt(i); |
| 6108 | if (axis.axisInfo.axis < 0) { |
| 6109 | while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16 |
| 6110 | && haveAxis(nextGenericAxisId)) { |
| 6111 | nextGenericAxisId += 1; |
| 6112 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6113 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 6114 | if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) { |
| 6115 | axis.axisInfo.axis = nextGenericAxisId; |
| 6116 | nextGenericAxisId += 1; |
| 6117 | } else { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 6118 | 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] | 6119 | "have already been assigned to other axes.", |
| 6120 | getDeviceName().string(), mAxes.keyAt(i)); |
| 6121 | mAxes.removeItemsAt(i--); |
| 6122 | numAxes -= 1; |
| 6123 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6124 | } |
| 6125 | } |
| 6126 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6127 | } |
| 6128 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6129 | bool JoystickInputMapper::haveAxis(int32_t axisId) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6130 | size_t numAxes = mAxes.size(); |
| 6131 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6132 | const Axis& axis = mAxes.valueAt(i); |
| 6133 | if (axis.axisInfo.axis == axisId |
| 6134 | || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT |
| 6135 | && axis.axisInfo.highAxis == axisId)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6136 | return true; |
| 6137 | } |
| 6138 | } |
| 6139 | return false; |
| 6140 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6141 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6142 | void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) { |
| 6143 | size_t i = mAxes.size(); |
| 6144 | while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) { |
| 6145 | if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) { |
| 6146 | continue; |
| 6147 | } |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 6148 | ALOGI("Discarding joystick '%s' axis %d because there are too many axes.", |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6149 | getDeviceName().string(), mAxes.keyAt(i)); |
| 6150 | mAxes.removeItemsAt(i); |
| 6151 | } |
| 6152 | } |
| 6153 | |
| 6154 | bool JoystickInputMapper::isCenteredAxis(int32_t axis) { |
| 6155 | switch (axis) { |
| 6156 | case AMOTION_EVENT_AXIS_X: |
| 6157 | case AMOTION_EVENT_AXIS_Y: |
| 6158 | case AMOTION_EVENT_AXIS_Z: |
| 6159 | case AMOTION_EVENT_AXIS_RX: |
| 6160 | case AMOTION_EVENT_AXIS_RY: |
| 6161 | case AMOTION_EVENT_AXIS_RZ: |
| 6162 | case AMOTION_EVENT_AXIS_HAT_X: |
| 6163 | case AMOTION_EVENT_AXIS_HAT_Y: |
| 6164 | case AMOTION_EVENT_AXIS_ORIENTATION: |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6165 | case AMOTION_EVENT_AXIS_RUDDER: |
| 6166 | case AMOTION_EVENT_AXIS_WHEEL: |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6167 | return true; |
| 6168 | default: |
| 6169 | return false; |
| 6170 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6171 | } |
| 6172 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 6173 | void JoystickInputMapper::reset(nsecs_t when) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6174 | // Recenter all axes. |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6175 | size_t numAxes = mAxes.size(); |
| 6176 | for (size_t i = 0; i < numAxes; i++) { |
| 6177 | Axis& axis = mAxes.editValueAt(i); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6178 | axis.resetValue(); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6179 | } |
| 6180 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 6181 | InputMapper::reset(when); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6182 | } |
| 6183 | |
| 6184 | void JoystickInputMapper::process(const RawEvent* rawEvent) { |
| 6185 | switch (rawEvent->type) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6186 | case EV_ABS: { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 6187 | ssize_t index = mAxes.indexOfKey(rawEvent->code); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6188 | if (index >= 0) { |
| 6189 | Axis& axis = mAxes.editValueAt(index); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6190 | float newValue, highNewValue; |
| 6191 | switch (axis.axisInfo.mode) { |
| 6192 | case AxisInfo::MODE_INVERT: |
| 6193 | newValue = (axis.rawAxisInfo.maxValue - rawEvent->value) |
| 6194 | * axis.scale + axis.offset; |
| 6195 | highNewValue = 0.0f; |
| 6196 | break; |
| 6197 | case AxisInfo::MODE_SPLIT: |
| 6198 | if (rawEvent->value < axis.axisInfo.splitValue) { |
| 6199 | newValue = (axis.axisInfo.splitValue - rawEvent->value) |
| 6200 | * axis.scale + axis.offset; |
| 6201 | highNewValue = 0.0f; |
| 6202 | } else if (rawEvent->value > axis.axisInfo.splitValue) { |
| 6203 | newValue = 0.0f; |
| 6204 | highNewValue = (rawEvent->value - axis.axisInfo.splitValue) |
| 6205 | * axis.highScale + axis.highOffset; |
| 6206 | } else { |
| 6207 | newValue = 0.0f; |
| 6208 | highNewValue = 0.0f; |
| 6209 | } |
| 6210 | break; |
| 6211 | default: |
| 6212 | newValue = rawEvent->value * axis.scale + axis.offset; |
| 6213 | highNewValue = 0.0f; |
| 6214 | break; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6215 | } |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6216 | axis.newValue = newValue; |
| 6217 | axis.highNewValue = highNewValue; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6218 | } |
| 6219 | break; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6220 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6221 | |
| 6222 | case EV_SYN: |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 6223 | switch (rawEvent->code) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6224 | case SYN_REPORT: |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6225 | sync(rawEvent->when, false /*force*/); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6226 | break; |
| 6227 | } |
| 6228 | break; |
| 6229 | } |
| 6230 | } |
| 6231 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6232 | void JoystickInputMapper::sync(nsecs_t when, bool force) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6233 | if (!filterAxes(force)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6234 | return; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6235 | } |
| 6236 | |
| 6237 | int32_t metaState = mContext->getGlobalMetaState(); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 6238 | int32_t buttonState = 0; |
| 6239 | |
| 6240 | PointerProperties pointerProperties; |
| 6241 | pointerProperties.clear(); |
| 6242 | pointerProperties.id = 0; |
| 6243 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6244 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6245 | PointerCoords pointerCoords; |
| 6246 | pointerCoords.clear(); |
| 6247 | |
| 6248 | size_t numAxes = mAxes.size(); |
| 6249 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6250 | const Axis& axis = mAxes.valueAt(i); |
| 6251 | pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue); |
| 6252 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 6253 | pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue); |
| 6254 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6255 | } |
| 6256 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 6257 | // Moving a joystick axis should not wake the devide because joysticks can |
| 6258 | // be fairly noisy even when not in use. On the other hand, pushing a gamepad |
| 6259 | // button will likely wake the device. |
| 6260 | // TODO: Use the input device configuration to control this behavior more finely. |
| 6261 | uint32_t policyFlags = 0; |
| 6262 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 6263 | NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 6264 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 6265 | 1, &pointerProperties, &pointerCoords, 0, 0, 0); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 6266 | getListener()->notifyMotion(&args); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6267 | } |
| 6268 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6269 | bool JoystickInputMapper::filterAxes(bool force) { |
| 6270 | bool atLeastOneSignificantChange = force; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6271 | size_t numAxes = mAxes.size(); |
| 6272 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6273 | Axis& axis = mAxes.editValueAt(i); |
| 6274 | if (force || hasValueChangedSignificantly(axis.filter, |
| 6275 | axis.newValue, axis.currentValue, axis.min, axis.max)) { |
| 6276 | axis.currentValue = axis.newValue; |
| 6277 | atLeastOneSignificantChange = true; |
| 6278 | } |
| 6279 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 6280 | if (force || hasValueChangedSignificantly(axis.filter, |
| 6281 | axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) { |
| 6282 | axis.highCurrentValue = axis.highNewValue; |
| 6283 | atLeastOneSignificantChange = true; |
| 6284 | } |
| 6285 | } |
| 6286 | } |
| 6287 | return atLeastOneSignificantChange; |
| 6288 | } |
| 6289 | |
| 6290 | bool JoystickInputMapper::hasValueChangedSignificantly( |
| 6291 | float filter, float newValue, float currentValue, float min, float max) { |
| 6292 | if (newValue != currentValue) { |
| 6293 | // Filter out small changes in value unless the value is converging on the axis |
| 6294 | // bounds or center point. This is intended to reduce the amount of information |
| 6295 | // sent to applications by particularly noisy joysticks (such as PS3). |
| 6296 | if (fabs(newValue - currentValue) > filter |
| 6297 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min) |
| 6298 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max) |
| 6299 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) { |
| 6300 | return true; |
| 6301 | } |
| 6302 | } |
| 6303 | return false; |
| 6304 | } |
| 6305 | |
| 6306 | bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange( |
| 6307 | float filter, float newValue, float currentValue, float thresholdValue) { |
| 6308 | float newDistance = fabs(newValue - thresholdValue); |
| 6309 | if (newDistance < filter) { |
| 6310 | float oldDistance = fabs(currentValue - thresholdValue); |
| 6311 | if (newDistance < oldDistance) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6312 | return true; |
| 6313 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6314 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6315 | return false; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6316 | } |
| 6317 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 6318 | } // namespace android |