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