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