Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 17 | #define LOG_TAG "InputReader" |
| 18 | |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
| 21 | // Log debug messages for each raw event received from the EventHub. |
| 22 | #define DEBUG_RAW_EVENTS 0 |
| 23 | |
| 24 | // Log debug messages about touch screen filtering hacks. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 25 | #define DEBUG_HACKS 0 |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 26 | |
| 27 | // Log debug messages about virtual key processing. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 28 | #define DEBUG_VIRTUAL_KEYS 0 |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 29 | |
| 30 | // Log debug messages about pointers. |
Jeff Brown | 9f2106f | 2011-05-24 14:40:35 -0700 | [diff] [blame] | 31 | #define DEBUG_POINTERS 0 |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 32 | |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 33 | // Log debug messages about pointer assignment calculations. |
| 34 | #define DEBUG_POINTER_ASSIGNMENT 0 |
| 35 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 36 | // Log debug messages about gesture detection. |
| 37 | #define DEBUG_GESTURES 0 |
| 38 | |
Jeff Brown | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 50 | #include <unistd.h> |
Jeff Brown | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | fd03582 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 206 | bool InputReaderConfiguration::getDisplayInfo(bool external, DisplayViewport* outViewport) const { |
| 207 | const DisplayViewport& viewport = external ? mExternalDisplay : mInternalDisplay; |
| 208 | if (viewport.displayId >= 0) { |
| 209 | *outViewport = viewport; |
| 210 | return true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 211 | } |
| 212 | return false; |
| 213 | } |
| 214 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 215 | void InputReaderConfiguration::setDisplayInfo(bool external, const DisplayViewport& viewport) { |
| 216 | DisplayViewport& v = external ? mExternalDisplay : mInternalDisplay; |
| 217 | v = viewport; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 221 | // --- InputReader --- |
| 222 | |
| 223 | InputReader::InputReader(const sp<EventHubInterface>& eventHub, |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 224 | const sp<InputReaderPolicyInterface>& policy, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 225 | const sp<InputListenerInterface>& listener) : |
| 226 | mContext(this), mEventHub(eventHub), mPolicy(policy), |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 227 | mGlobalMetaState(0), mGeneration(1), |
| 228 | mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX), |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 229 | mConfigurationChangesToRefresh(0) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 230 | mQueuedListener = new QueuedInputListener(listener); |
| 231 | |
| 232 | { // acquire lock |
| 233 | AutoMutex _l(mLock); |
| 234 | |
| 235 | refreshConfigurationLocked(0); |
| 236 | updateGlobalMetaStateLocked(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 237 | } // release lock |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | InputReader::~InputReader() { |
| 241 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 242 | delete mDevices.valueAt(i); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | void InputReader::loopOnce() { |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 247 | int32_t oldGeneration; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 248 | int32_t timeoutMillis; |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 249 | bool inputDevicesChanged = false; |
| 250 | Vector<InputDeviceInfo> inputDevices; |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 251 | { // acquire lock |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 252 | AutoMutex _l(mLock); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 253 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 254 | oldGeneration = mGeneration; |
| 255 | timeoutMillis = -1; |
| 256 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 257 | uint32_t changes = mConfigurationChangesToRefresh; |
| 258 | if (changes) { |
| 259 | mConfigurationChangesToRefresh = 0; |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 260 | timeoutMillis = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 261 | refreshConfigurationLocked(changes); |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 262 | } else if (mNextTimeout != LLONG_MAX) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 263 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 264 | timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout); |
| 265 | } |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 266 | } // release lock |
| 267 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 268 | size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 269 | |
| 270 | { // acquire lock |
| 271 | AutoMutex _l(mLock); |
Jeff Brown | 112b5f5 | 2012-01-27 17:32:06 -0800 | [diff] [blame] | 272 | mReaderIsAliveCondition.broadcast(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 273 | |
| 274 | if (count) { |
| 275 | processEventsLocked(mEventBuffer, count); |
| 276 | } |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 277 | |
| 278 | if (mNextTimeout != LLONG_MAX) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 279 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
Jeff Brown | 112b5f5 | 2012-01-27 17:32:06 -0800 | [diff] [blame] | 280 | if (now >= mNextTimeout) { |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 281 | #if DEBUG_RAW_EVENTS |
Jeff Brown | 112b5f5 | 2012-01-27 17:32:06 -0800 | [diff] [blame] | 282 | ALOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 283 | #endif |
Jeff Brown | 112b5f5 | 2012-01-27 17:32:06 -0800 | [diff] [blame] | 284 | mNextTimeout = LLONG_MAX; |
| 285 | timeoutExpiredLocked(now); |
| 286 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 287 | } |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 288 | |
| 289 | if (oldGeneration != mGeneration) { |
| 290 | inputDevicesChanged = true; |
| 291 | getInputDevicesLocked(inputDevices); |
| 292 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 293 | } // release lock |
| 294 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 295 | // Send out a message that the describes the changed input devices. |
| 296 | if (inputDevicesChanged) { |
| 297 | mPolicy->notifyInputDevicesChanged(inputDevices); |
| 298 | } |
| 299 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 300 | // Flush queued events out to the listener. |
| 301 | // This must happen outside of the lock because the listener could potentially call |
| 302 | // back into the InputReader's methods, such as getScanCodeState, or become blocked |
| 303 | // on another thread similarly waiting to acquire the InputReader lock thereby |
| 304 | // resulting in a deadlock. This situation is actually quite plausible because the |
| 305 | // listener is actually the input dispatcher, which calls into the window manager, |
| 306 | // which occasionally calls into the input reader. |
| 307 | mQueuedListener->flush(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 310 | void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) { |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 311 | for (const RawEvent* rawEvent = rawEvents; count;) { |
| 312 | int32_t type = rawEvent->type; |
| 313 | size_t batchSize = 1; |
| 314 | if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) { |
| 315 | int32_t deviceId = rawEvent->deviceId; |
| 316 | while (batchSize < count) { |
| 317 | if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT |
| 318 | || rawEvent[batchSize].deviceId != deviceId) { |
| 319 | break; |
| 320 | } |
| 321 | batchSize += 1; |
| 322 | } |
| 323 | #if DEBUG_RAW_EVENTS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 324 | ALOGD("BatchSize: %d Count: %d", batchSize, count); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 325 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 326 | processEventsForDeviceLocked(deviceId, rawEvent, batchSize); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 327 | } else { |
| 328 | switch (rawEvent->type) { |
| 329 | case EventHubInterface::DEVICE_ADDED: |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 330 | addDeviceLocked(rawEvent->when, rawEvent->deviceId); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 331 | break; |
| 332 | case EventHubInterface::DEVICE_REMOVED: |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 333 | removeDeviceLocked(rawEvent->when, rawEvent->deviceId); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 334 | break; |
| 335 | case EventHubInterface::FINISHED_DEVICE_SCAN: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 336 | handleConfigurationChangedLocked(rawEvent->when); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 337 | break; |
| 338 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 339 | ALOG_ASSERT(false); // can't happen |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 340 | break; |
| 341 | } |
| 342 | } |
| 343 | count -= batchSize; |
| 344 | rawEvent += batchSize; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 348 | void InputReader::addDeviceLocked(nsecs_t when, int32_t deviceId) { |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 349 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 350 | if (deviceIndex >= 0) { |
| 351 | ALOGW("Ignoring spurious device added event for deviceId %d.", deviceId); |
| 352 | return; |
| 353 | } |
| 354 | |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 355 | InputDeviceIdentifier identifier = mEventHub->getDeviceIdentifier(deviceId); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 356 | uint32_t classes = mEventHub->getDeviceClasses(deviceId); |
| 357 | |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 358 | InputDevice* device = createDeviceLocked(deviceId, identifier, classes); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 359 | device->configure(when, &mConfig, 0); |
| 360 | device->reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 361 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 362 | if (device->isIgnored()) { |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 363 | ALOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, |
| 364 | identifier.name.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 365 | } else { |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 366 | ALOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, |
| 367 | identifier.name.string(), device->getSources()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 370 | mDevices.add(deviceId, device); |
| 371 | bumpGenerationLocked(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 374 | void InputReader::removeDeviceLocked(nsecs_t when, int32_t deviceId) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 375 | InputDevice* device = NULL; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 376 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 377 | if (deviceIndex < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 378 | ALOGW("Ignoring spurious device removed event for deviceId %d.", deviceId); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 379 | return; |
| 380 | } |
| 381 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 382 | device = mDevices.valueAt(deviceIndex); |
| 383 | mDevices.removeItemsAt(deviceIndex, 1); |
| 384 | bumpGenerationLocked(); |
| 385 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 386 | if (device->isIgnored()) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 387 | ALOGI("Device removed: id=%d, name='%s' (ignored non-input device)", |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 388 | device->getId(), device->getName().string()); |
| 389 | } else { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 390 | ALOGI("Device removed: id=%d, name='%s', sources=0x%08x", |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 391 | device->getId(), device->getName().string(), device->getSources()); |
| 392 | } |
| 393 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 394 | device->reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 395 | delete device; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 398 | InputDevice* InputReader::createDeviceLocked(int32_t deviceId, |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 399 | const InputDeviceIdentifier& identifier, uint32_t classes) { |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 400 | InputDevice* device = new InputDevice(&mContext, deviceId, bumpGenerationLocked(), |
| 401 | identifier, classes); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 402 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 403 | // External devices. |
| 404 | if (classes & INPUT_DEVICE_CLASS_EXTERNAL) { |
| 405 | device->setExternal(true); |
| 406 | } |
| 407 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 408 | // Switch-like devices. |
| 409 | if (classes & INPUT_DEVICE_CLASS_SWITCH) { |
| 410 | device->addMapper(new SwitchInputMapper(device)); |
| 411 | } |
| 412 | |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 413 | // Vibrator-like devices. |
| 414 | if (classes & INPUT_DEVICE_CLASS_VIBRATOR) { |
| 415 | device->addMapper(new VibratorInputMapper(device)); |
| 416 | } |
| 417 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 418 | // Keyboard-like devices. |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 419 | uint32_t keyboardSource = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 420 | int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC; |
| 421 | if (classes & INPUT_DEVICE_CLASS_KEYBOARD) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 422 | keyboardSource |= AINPUT_SOURCE_KEYBOARD; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 423 | } |
| 424 | if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) { |
| 425 | keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC; |
| 426 | } |
| 427 | if (classes & INPUT_DEVICE_CLASS_DPAD) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 428 | keyboardSource |= AINPUT_SOURCE_DPAD; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 429 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 430 | if (classes & INPUT_DEVICE_CLASS_GAMEPAD) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 431 | keyboardSource |= AINPUT_SOURCE_GAMEPAD; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 432 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 433 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 434 | if (keyboardSource != 0) { |
| 435 | device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 438 | // Cursor-like devices. |
| 439 | if (classes & INPUT_DEVICE_CLASS_CURSOR) { |
| 440 | device->addMapper(new CursorInputMapper(device)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 443 | // Touchscreens and touchpad devices. |
| 444 | if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 445 | device->addMapper(new MultiTouchInputMapper(device)); |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 446 | } else if (classes & INPUT_DEVICE_CLASS_TOUCH) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 447 | device->addMapper(new SingleTouchInputMapper(device)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 450 | // Joystick-like devices. |
| 451 | if (classes & INPUT_DEVICE_CLASS_JOYSTICK) { |
| 452 | device->addMapper(new JoystickInputMapper(device)); |
| 453 | } |
| 454 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 455 | return device; |
| 456 | } |
| 457 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 458 | void InputReader::processEventsForDeviceLocked(int32_t deviceId, |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 459 | const RawEvent* rawEvents, size_t count) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 460 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 461 | if (deviceIndex < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 462 | ALOGW("Discarding event for unknown deviceId %d.", deviceId); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 463 | return; |
| 464 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 465 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 466 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 467 | if (device->isIgnored()) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 468 | //ALOGD("Discarding event for ignored deviceId %d.", deviceId); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 469 | return; |
| 470 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 471 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 472 | device->process(rawEvents, count); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 475 | void InputReader::timeoutExpiredLocked(nsecs_t when) { |
| 476 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 477 | InputDevice* device = mDevices.valueAt(i); |
| 478 | if (!device->isIgnored()) { |
| 479 | device->timeoutExpired(when); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 480 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 481 | } |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 484 | void InputReader::handleConfigurationChangedLocked(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 485 | // 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] | 486 | updateGlobalMetaStateLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 487 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 488 | // Enqueue configuration changed. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 489 | NotifyConfigurationChangedArgs args(when); |
| 490 | mQueuedListener->notifyConfigurationChanged(&args); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 493 | void InputReader::refreshConfigurationLocked(uint32_t changes) { |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 494 | mPolicy->getReaderConfiguration(&mConfig); |
| 495 | mEventHub->setExcludedDevices(mConfig.excludedDeviceNames); |
| 496 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 497 | if (changes) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 498 | ALOGI("Reconfiguring input devices. changes=0x%08x", changes); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 499 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 500 | |
| 501 | if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) { |
| 502 | mEventHub->requestReopenDevices(); |
| 503 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 504 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 505 | InputDevice* device = mDevices.valueAt(i); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 506 | device->configure(now, &mConfig, changes); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 507 | } |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 508 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 512 | void InputReader::updateGlobalMetaStateLocked() { |
| 513 | mGlobalMetaState = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 514 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 515 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 516 | InputDevice* device = mDevices.valueAt(i); |
| 517 | mGlobalMetaState |= device->getMetaState(); |
| 518 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 521 | int32_t InputReader::getGlobalMetaStateLocked() { |
| 522 | return mGlobalMetaState; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 525 | void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) { |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 526 | mDisableVirtualKeysTimeout = time; |
| 527 | } |
| 528 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 529 | bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now, |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 530 | InputDevice* device, int32_t keyCode, int32_t scanCode) { |
| 531 | if (now < mDisableVirtualKeysTimeout) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 532 | ALOGI("Dropping virtual key from device %s because virtual keys are " |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 533 | "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d", |
| 534 | device->getName().string(), |
| 535 | (mDisableVirtualKeysTimeout - now) * 0.000001, |
| 536 | keyCode, scanCode); |
| 537 | return true; |
| 538 | } else { |
| 539 | return false; |
| 540 | } |
| 541 | } |
| 542 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 543 | void InputReader::fadePointerLocked() { |
| 544 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 545 | InputDevice* device = mDevices.valueAt(i); |
| 546 | device->fadePointer(); |
| 547 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 548 | } |
| 549 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 550 | void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) { |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 551 | if (when < mNextTimeout) { |
| 552 | mNextTimeout = when; |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 553 | mEventHub->wake(); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 557 | int32_t InputReader::bumpGenerationLocked() { |
| 558 | return ++mGeneration; |
| 559 | } |
| 560 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 561 | void InputReader::getInputDevices(Vector<InputDeviceInfo>& outInputDevices) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 562 | AutoMutex _l(mLock); |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 563 | getInputDevicesLocked(outInputDevices); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 566 | void InputReader::getInputDevicesLocked(Vector<InputDeviceInfo>& outInputDevices) { |
| 567 | outInputDevices.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 568 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 569 | size_t numDevices = mDevices.size(); |
| 570 | for (size_t i = 0; i < numDevices; i++) { |
| 571 | InputDevice* device = mDevices.valueAt(i); |
| 572 | if (!device->isIgnored()) { |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 573 | outInputDevices.push(); |
| 574 | device->getDeviceInfo(&outInputDevices.editTop()); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 575 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 576 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, |
| 580 | int32_t keyCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 581 | AutoMutex _l(mLock); |
| 582 | |
| 583 | return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, |
| 587 | int32_t scanCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 588 | AutoMutex _l(mLock); |
| 589 | |
| 590 | return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | 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] | 594 | AutoMutex _l(mLock); |
| 595 | |
| 596 | return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 599 | 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] | 600 | GetStateFunc getStateFunc) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 601 | int32_t result = AKEY_STATE_UNKNOWN; |
| 602 | if (deviceId >= 0) { |
| 603 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 604 | if (deviceIndex >= 0) { |
| 605 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 606 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 607 | result = (device->*getStateFunc)(sourceMask, code); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 608 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 609 | } |
| 610 | } else { |
| 611 | size_t numDevices = mDevices.size(); |
| 612 | for (size_t i = 0; i < numDevices; i++) { |
| 613 | InputDevice* device = mDevices.valueAt(i); |
| 614 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
David Deephanphongs | fbca596 | 2011-11-14 14:50:45 -0800 | [diff] [blame] | 615 | // If any device reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that |
| 616 | // value. Otherwise, return AKEY_STATE_UP as long as one device reports it. |
| 617 | int32_t currentResult = (device->*getStateFunc)(sourceMask, code); |
| 618 | if (currentResult >= AKEY_STATE_DOWN) { |
| 619 | return currentResult; |
| 620 | } else if (currentResult == AKEY_STATE_UP) { |
| 621 | result = currentResult; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 625 | } |
| 626 | return result; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, |
| 630 | size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 631 | AutoMutex _l(mLock); |
| 632 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 633 | memset(outFlags, 0, numCodes); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 634 | return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 637 | bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask, |
| 638 | size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) { |
| 639 | bool result = false; |
| 640 | if (deviceId >= 0) { |
| 641 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 642 | if (deviceIndex >= 0) { |
| 643 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 644 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 645 | result = device->markSupportedKeyCodes(sourceMask, |
| 646 | numCodes, keyCodes, outFlags); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 647 | } |
| 648 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 649 | } else { |
| 650 | size_t numDevices = mDevices.size(); |
| 651 | for (size_t i = 0; i < numDevices; i++) { |
| 652 | InputDevice* device = mDevices.valueAt(i); |
| 653 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 654 | result |= device->markSupportedKeyCodes(sourceMask, |
| 655 | numCodes, keyCodes, outFlags); |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | return result; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 660 | } |
| 661 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 662 | void InputReader::requestRefreshConfiguration(uint32_t changes) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 663 | AutoMutex _l(mLock); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 664 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 665 | if (changes) { |
| 666 | bool needWake = !mConfigurationChangesToRefresh; |
| 667 | mConfigurationChangesToRefresh |= changes; |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 668 | |
| 669 | if (needWake) { |
| 670 | mEventHub->wake(); |
| 671 | } |
| 672 | } |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 675 | void InputReader::vibrate(int32_t deviceId, const nsecs_t* pattern, size_t patternSize, |
| 676 | ssize_t repeat, int32_t token) { |
| 677 | AutoMutex _l(mLock); |
| 678 | |
| 679 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 680 | if (deviceIndex >= 0) { |
| 681 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 682 | device->vibrate(pattern, patternSize, repeat, token); |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | void InputReader::cancelVibrate(int32_t deviceId, int32_t token) { |
| 687 | AutoMutex _l(mLock); |
| 688 | |
| 689 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 690 | if (deviceIndex >= 0) { |
| 691 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 692 | device->cancelVibrate(token); |
| 693 | } |
| 694 | } |
| 695 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 696 | void InputReader::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 697 | AutoMutex _l(mLock); |
| 698 | |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 699 | mEventHub->dump(dump); |
| 700 | dump.append("\n"); |
| 701 | |
| 702 | dump.append("Input Reader State:\n"); |
| 703 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 704 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 705 | mDevices.valueAt(i)->dump(dump); |
| 706 | } |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 707 | |
| 708 | dump.append(INDENT "Configuration:\n"); |
| 709 | dump.append(INDENT2 "ExcludedDeviceNames: ["); |
| 710 | for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) { |
| 711 | if (i != 0) { |
| 712 | dump.append(", "); |
| 713 | } |
| 714 | dump.append(mConfig.excludedDeviceNames.itemAt(i).string()); |
| 715 | } |
| 716 | dump.append("]\n"); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 717 | dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n", |
| 718 | mConfig.virtualKeyQuietTime * 0.000001f); |
| 719 | |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 720 | dump.appendFormat(INDENT2 "PointerVelocityControlParameters: " |
| 721 | "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n", |
| 722 | mConfig.pointerVelocityControlParameters.scale, |
| 723 | mConfig.pointerVelocityControlParameters.lowThreshold, |
| 724 | mConfig.pointerVelocityControlParameters.highThreshold, |
| 725 | mConfig.pointerVelocityControlParameters.acceleration); |
| 726 | |
| 727 | dump.appendFormat(INDENT2 "WheelVelocityControlParameters: " |
| 728 | "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n", |
| 729 | mConfig.wheelVelocityControlParameters.scale, |
| 730 | mConfig.wheelVelocityControlParameters.lowThreshold, |
| 731 | mConfig.wheelVelocityControlParameters.highThreshold, |
| 732 | mConfig.wheelVelocityControlParameters.acceleration); |
| 733 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 734 | dump.appendFormat(INDENT2 "PointerGesture:\n"); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 735 | dump.appendFormat(INDENT3 "Enabled: %s\n", |
| 736 | toString(mConfig.pointerGesturesEnabled)); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 737 | dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n", |
| 738 | mConfig.pointerGestureQuietInterval * 0.000001f); |
| 739 | dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n", |
| 740 | mConfig.pointerGestureDragMinSwitchSpeed); |
| 741 | dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n", |
| 742 | mConfig.pointerGestureTapInterval * 0.000001f); |
| 743 | dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n", |
| 744 | mConfig.pointerGestureTapDragInterval * 0.000001f); |
| 745 | dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n", |
| 746 | mConfig.pointerGestureTapSlop); |
| 747 | dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n", |
| 748 | mConfig.pointerGestureMultitouchSettleInterval * 0.000001f); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 749 | dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n", |
| 750 | mConfig.pointerGestureMultitouchMinDistance); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 751 | dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n", |
| 752 | mConfig.pointerGestureSwipeTransitionAngleCosine); |
| 753 | dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n", |
| 754 | mConfig.pointerGestureSwipeMaxWidthRatio); |
| 755 | dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n", |
| 756 | mConfig.pointerGestureMovementSpeedRatio); |
| 757 | dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n", |
| 758 | mConfig.pointerGestureZoomSpeedRatio); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Jeff Brown | 89ef072 | 2011-08-10 16:25:21 -0700 | [diff] [blame] | 761 | void InputReader::monitor() { |
| 762 | // Acquire and release the lock to ensure that the reader has not deadlocked. |
| 763 | mLock.lock(); |
Jeff Brown | 112b5f5 | 2012-01-27 17:32:06 -0800 | [diff] [blame] | 764 | mEventHub->wake(); |
| 765 | mReaderIsAliveCondition.wait(mLock); |
Jeff Brown | 89ef072 | 2011-08-10 16:25:21 -0700 | [diff] [blame] | 766 | mLock.unlock(); |
| 767 | |
| 768 | // Check the EventHub |
| 769 | mEventHub->monitor(); |
| 770 | } |
| 771 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 772 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 773 | // --- InputReader::ContextImpl --- |
| 774 | |
| 775 | InputReader::ContextImpl::ContextImpl(InputReader* reader) : |
| 776 | mReader(reader) { |
| 777 | } |
| 778 | |
| 779 | void InputReader::ContextImpl::updateGlobalMetaState() { |
| 780 | // lock is already held by the input loop |
| 781 | mReader->updateGlobalMetaStateLocked(); |
| 782 | } |
| 783 | |
| 784 | int32_t InputReader::ContextImpl::getGlobalMetaState() { |
| 785 | // lock is already held by the input loop |
| 786 | return mReader->getGlobalMetaStateLocked(); |
| 787 | } |
| 788 | |
| 789 | void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) { |
| 790 | // lock is already held by the input loop |
| 791 | mReader->disableVirtualKeysUntilLocked(time); |
| 792 | } |
| 793 | |
| 794 | bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now, |
| 795 | InputDevice* device, int32_t keyCode, int32_t scanCode) { |
| 796 | // lock is already held by the input loop |
| 797 | return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode); |
| 798 | } |
| 799 | |
| 800 | void InputReader::ContextImpl::fadePointer() { |
| 801 | // lock is already held by the input loop |
| 802 | mReader->fadePointerLocked(); |
| 803 | } |
| 804 | |
| 805 | void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) { |
| 806 | // lock is already held by the input loop |
| 807 | mReader->requestTimeoutAtTimeLocked(when); |
| 808 | } |
| 809 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 810 | int32_t InputReader::ContextImpl::bumpGeneration() { |
| 811 | // lock is already held by the input loop |
| 812 | return mReader->bumpGenerationLocked(); |
| 813 | } |
| 814 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 815 | InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() { |
| 816 | return mReader->mPolicy.get(); |
| 817 | } |
| 818 | |
| 819 | InputListenerInterface* InputReader::ContextImpl::getListener() { |
| 820 | return mReader->mQueuedListener.get(); |
| 821 | } |
| 822 | |
| 823 | EventHubInterface* InputReader::ContextImpl::getEventHub() { |
| 824 | return mReader->mEventHub.get(); |
| 825 | } |
| 826 | |
| 827 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 828 | // --- InputReaderThread --- |
| 829 | |
| 830 | InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) : |
| 831 | Thread(/*canCallJava*/ true), mReader(reader) { |
| 832 | } |
| 833 | |
| 834 | InputReaderThread::~InputReaderThread() { |
| 835 | } |
| 836 | |
| 837 | bool InputReaderThread::threadLoop() { |
| 838 | mReader->loopOnce(); |
| 839 | return true; |
| 840 | } |
| 841 | |
| 842 | |
| 843 | // --- InputDevice --- |
| 844 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 845 | InputDevice::InputDevice(InputReaderContext* context, int32_t id, int32_t generation, |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 846 | const InputDeviceIdentifier& identifier, uint32_t classes) : |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 847 | mContext(context), mId(id), mGeneration(generation), |
| 848 | mIdentifier(identifier), mClasses(classes), |
Jeff Brown | 9ee285a | 2011-08-31 12:56:34 -0700 | [diff] [blame] | 849 | mSources(0), mIsExternal(false), mDropUntilNextSync(false) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | InputDevice::~InputDevice() { |
| 853 | size_t numMappers = mMappers.size(); |
| 854 | for (size_t i = 0; i < numMappers; i++) { |
| 855 | delete mMappers[i]; |
| 856 | } |
| 857 | mMappers.clear(); |
| 858 | } |
| 859 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 860 | void InputDevice::dump(String8& dump) { |
| 861 | InputDeviceInfo deviceInfo; |
| 862 | getDeviceInfo(& deviceInfo); |
| 863 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 864 | dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(), |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame] | 865 | deviceInfo.getDisplayName().string()); |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 866 | dump.appendFormat(INDENT2 "Generation: %d\n", mGeneration); |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 867 | dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal)); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 868 | dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources()); |
| 869 | dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType()); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 870 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 871 | const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges(); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 872 | if (!ranges.isEmpty()) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 873 | dump.append(INDENT2 "Motion Ranges:\n"); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 874 | for (size_t i = 0; i < ranges.size(); i++) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 875 | const InputDeviceInfo::MotionRange& range = ranges.itemAt(i); |
| 876 | const char* label = getAxisLabel(range.axis); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 877 | char name[32]; |
| 878 | if (label) { |
| 879 | strncpy(name, label, sizeof(name)); |
| 880 | name[sizeof(name) - 1] = '\0'; |
| 881 | } else { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 882 | snprintf(name, sizeof(name), "%d", range.axis); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 883 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 884 | dump.appendFormat(INDENT3 "%s: source=0x%08x, " |
| 885 | "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n", |
| 886 | name, range.source, range.min, range.max, range.flat, range.fuzz); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 887 | } |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | size_t numMappers = mMappers.size(); |
| 891 | for (size_t i = 0; i < numMappers; i++) { |
| 892 | InputMapper* mapper = mMappers[i]; |
| 893 | mapper->dump(dump); |
| 894 | } |
| 895 | } |
| 896 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 897 | void InputDevice::addMapper(InputMapper* mapper) { |
| 898 | mMappers.add(mapper); |
| 899 | } |
| 900 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 901 | void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 902 | mSources = 0; |
| 903 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 904 | if (!isIgnored()) { |
| 905 | if (!changes) { // first time only |
| 906 | mContext->getEventHub()->getConfiguration(mId, &mConfiguration); |
| 907 | } |
| 908 | |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 909 | if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) { |
Jeff Brown | 61c0824 | 2012-04-19 11:14:33 -0700 | [diff] [blame] | 910 | if (!(mClasses & INPUT_DEVICE_CLASS_VIRTUAL)) { |
| 911 | sp<KeyCharacterMap> keyboardLayout = |
| 912 | mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier.descriptor); |
| 913 | if (mContext->getEventHub()->setKeyboardLayoutOverlay(mId, keyboardLayout)) { |
| 914 | bumpGeneration(); |
| 915 | } |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame] | 919 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) { |
| 920 | if (!(mClasses & INPUT_DEVICE_CLASS_VIRTUAL)) { |
| 921 | String8 alias = mContext->getPolicy()->getDeviceAlias(mIdentifier); |
| 922 | if (mAlias != alias) { |
| 923 | mAlias = alias; |
| 924 | bumpGeneration(); |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 929 | size_t numMappers = mMappers.size(); |
| 930 | for (size_t i = 0; i < numMappers; i++) { |
| 931 | InputMapper* mapper = mMappers[i]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 932 | mapper->configure(when, config, changes); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 933 | mSources |= mapper->getSources(); |
| 934 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 935 | } |
| 936 | } |
| 937 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 938 | void InputDevice::reset(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 939 | size_t numMappers = mMappers.size(); |
| 940 | for (size_t i = 0; i < numMappers; i++) { |
| 941 | InputMapper* mapper = mMappers[i]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 942 | mapper->reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 943 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 944 | |
| 945 | mContext->updateGlobalMetaState(); |
| 946 | |
| 947 | notifyReset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 948 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 949 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 950 | void InputDevice::process(const RawEvent* rawEvents, size_t count) { |
| 951 | // Process all of the events in order for each mapper. |
| 952 | // We cannot simply ask each mapper to process them in bulk because mappers may |
| 953 | // have side-effects that must be interleaved. For example, joystick movement events and |
| 954 | // gamepad button presses are handled by different mappers but they should be dispatched |
| 955 | // in the order received. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 956 | size_t numMappers = mMappers.size(); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 957 | for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) { |
| 958 | #if DEBUG_RAW_EVENTS |
Jeff Brown | f33b2b2 | 2012-10-05 17:59:56 -0700 | [diff] [blame] | 959 | ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%lld", |
| 960 | rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value, |
| 961 | rawEvent->when); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 962 | #endif |
| 963 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 964 | if (mDropUntilNextSync) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 965 | if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 966 | mDropUntilNextSync = false; |
| 967 | #if DEBUG_RAW_EVENTS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 968 | ALOGD("Recovered from input event buffer overrun."); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 969 | #endif |
| 970 | } else { |
| 971 | #if DEBUG_RAW_EVENTS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 972 | ALOGD("Dropped input event while waiting for next input sync."); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 973 | #endif |
| 974 | } |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 975 | } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) { |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 976 | ALOGI("Detected input event buffer overrun for device %s.", getName().string()); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 977 | mDropUntilNextSync = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 978 | reset(rawEvent->when); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 979 | } else { |
| 980 | for (size_t i = 0; i < numMappers; i++) { |
| 981 | InputMapper* mapper = mMappers[i]; |
| 982 | mapper->process(rawEvent); |
| 983 | } |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 984 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 985 | } |
| 986 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 987 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 988 | void InputDevice::timeoutExpired(nsecs_t when) { |
| 989 | size_t numMappers = mMappers.size(); |
| 990 | for (size_t i = 0; i < numMappers; i++) { |
| 991 | InputMapper* mapper = mMappers[i]; |
| 992 | mapper->timeoutExpired(when); |
| 993 | } |
| 994 | } |
| 995 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 996 | void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) { |
Jeff Brown | daa3753 | 2012-05-01 15:54:03 -0700 | [diff] [blame] | 997 | outDeviceInfo->initialize(mId, mGeneration, mIdentifier, mAlias, mIsExternal); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 998 | |
| 999 | size_t numMappers = mMappers.size(); |
| 1000 | for (size_t i = 0; i < numMappers; i++) { |
| 1001 | InputMapper* mapper = mMappers[i]; |
| 1002 | mapper->populateDeviceInfo(outDeviceInfo); |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 1007 | return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState); |
| 1008 | } |
| 1009 | |
| 1010 | int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 1011 | return getState(sourceMask, scanCode, & InputMapper::getScanCodeState); |
| 1012 | } |
| 1013 | |
| 1014 | int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 1015 | return getState(sourceMask, switchCode, & InputMapper::getSwitchState); |
| 1016 | } |
| 1017 | |
| 1018 | int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) { |
| 1019 | int32_t result = AKEY_STATE_UNKNOWN; |
| 1020 | size_t numMappers = mMappers.size(); |
| 1021 | for (size_t i = 0; i < numMappers; i++) { |
| 1022 | InputMapper* mapper = mMappers[i]; |
| 1023 | if (sourcesMatchMask(mapper->getSources(), sourceMask)) { |
David Deephanphongs | fbca596 | 2011-11-14 14:50:45 -0800 | [diff] [blame] | 1024 | // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that |
| 1025 | // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it. |
| 1026 | int32_t currentResult = (mapper->*getStateFunc)(sourceMask, code); |
| 1027 | if (currentResult >= AKEY_STATE_DOWN) { |
| 1028 | return currentResult; |
| 1029 | } else if (currentResult == AKEY_STATE_UP) { |
| 1030 | result = currentResult; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | return result; |
| 1035 | } |
| 1036 | |
| 1037 | bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 1038 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 1039 | bool result = false; |
| 1040 | size_t numMappers = mMappers.size(); |
| 1041 | for (size_t i = 0; i < numMappers; i++) { |
| 1042 | InputMapper* mapper = mMappers[i]; |
| 1043 | if (sourcesMatchMask(mapper->getSources(), sourceMask)) { |
| 1044 | result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags); |
| 1045 | } |
| 1046 | } |
| 1047 | return result; |
| 1048 | } |
| 1049 | |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 1050 | void InputDevice::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, |
| 1051 | int32_t token) { |
| 1052 | size_t numMappers = mMappers.size(); |
| 1053 | for (size_t i = 0; i < numMappers; i++) { |
| 1054 | InputMapper* mapper = mMappers[i]; |
| 1055 | mapper->vibrate(pattern, patternSize, repeat, token); |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | void InputDevice::cancelVibrate(int32_t token) { |
| 1060 | size_t numMappers = mMappers.size(); |
| 1061 | for (size_t i = 0; i < numMappers; i++) { |
| 1062 | InputMapper* mapper = mMappers[i]; |
| 1063 | mapper->cancelVibrate(token); |
| 1064 | } |
| 1065 | } |
| 1066 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1067 | int32_t InputDevice::getMetaState() { |
| 1068 | int32_t result = 0; |
| 1069 | size_t numMappers = mMappers.size(); |
| 1070 | for (size_t i = 0; i < numMappers; i++) { |
| 1071 | InputMapper* mapper = mMappers[i]; |
| 1072 | result |= mapper->getMetaState(); |
| 1073 | } |
| 1074 | return result; |
| 1075 | } |
| 1076 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1077 | void InputDevice::fadePointer() { |
| 1078 | size_t numMappers = mMappers.size(); |
| 1079 | for (size_t i = 0; i < numMappers; i++) { |
| 1080 | InputMapper* mapper = mMappers[i]; |
| 1081 | mapper->fadePointer(); |
| 1082 | } |
| 1083 | } |
| 1084 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 1085 | void InputDevice::bumpGeneration() { |
| 1086 | mGeneration = mContext->bumpGeneration(); |
| 1087 | } |
| 1088 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1089 | void InputDevice::notifyReset(nsecs_t when) { |
| 1090 | NotifyDeviceResetArgs args(when, mId); |
| 1091 | mContext->getListener()->notifyDeviceReset(&args); |
| 1092 | } |
| 1093 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1094 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1095 | // --- CursorButtonAccumulator --- |
| 1096 | |
| 1097 | CursorButtonAccumulator::CursorButtonAccumulator() { |
| 1098 | clearButtons(); |
| 1099 | } |
| 1100 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1101 | void CursorButtonAccumulator::reset(InputDevice* device) { |
| 1102 | mBtnLeft = device->isKeyPressed(BTN_LEFT); |
| 1103 | mBtnRight = device->isKeyPressed(BTN_RIGHT); |
| 1104 | mBtnMiddle = device->isKeyPressed(BTN_MIDDLE); |
| 1105 | mBtnBack = device->isKeyPressed(BTN_BACK); |
| 1106 | mBtnSide = device->isKeyPressed(BTN_SIDE); |
| 1107 | mBtnForward = device->isKeyPressed(BTN_FORWARD); |
| 1108 | mBtnExtra = device->isKeyPressed(BTN_EXTRA); |
| 1109 | mBtnTask = device->isKeyPressed(BTN_TASK); |
| 1110 | } |
| 1111 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1112 | void CursorButtonAccumulator::clearButtons() { |
| 1113 | mBtnLeft = 0; |
| 1114 | mBtnRight = 0; |
| 1115 | mBtnMiddle = 0; |
| 1116 | mBtnBack = 0; |
| 1117 | mBtnSide = 0; |
| 1118 | mBtnForward = 0; |
| 1119 | mBtnExtra = 0; |
| 1120 | mBtnTask = 0; |
| 1121 | } |
| 1122 | |
| 1123 | void CursorButtonAccumulator::process(const RawEvent* rawEvent) { |
| 1124 | if (rawEvent->type == EV_KEY) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1125 | switch (rawEvent->code) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1126 | case BTN_LEFT: |
| 1127 | mBtnLeft = rawEvent->value; |
| 1128 | break; |
| 1129 | case BTN_RIGHT: |
| 1130 | mBtnRight = rawEvent->value; |
| 1131 | break; |
| 1132 | case BTN_MIDDLE: |
| 1133 | mBtnMiddle = rawEvent->value; |
| 1134 | break; |
| 1135 | case BTN_BACK: |
| 1136 | mBtnBack = rawEvent->value; |
| 1137 | break; |
| 1138 | case BTN_SIDE: |
| 1139 | mBtnSide = rawEvent->value; |
| 1140 | break; |
| 1141 | case BTN_FORWARD: |
| 1142 | mBtnForward = rawEvent->value; |
| 1143 | break; |
| 1144 | case BTN_EXTRA: |
| 1145 | mBtnExtra = rawEvent->value; |
| 1146 | break; |
| 1147 | case BTN_TASK: |
| 1148 | mBtnTask = rawEvent->value; |
| 1149 | break; |
| 1150 | } |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | uint32_t CursorButtonAccumulator::getButtonState() const { |
| 1155 | uint32_t result = 0; |
| 1156 | if (mBtnLeft) { |
| 1157 | result |= AMOTION_EVENT_BUTTON_PRIMARY; |
| 1158 | } |
| 1159 | if (mBtnRight) { |
| 1160 | result |= AMOTION_EVENT_BUTTON_SECONDARY; |
| 1161 | } |
| 1162 | if (mBtnMiddle) { |
| 1163 | result |= AMOTION_EVENT_BUTTON_TERTIARY; |
| 1164 | } |
| 1165 | if (mBtnBack || mBtnSide) { |
| 1166 | result |= AMOTION_EVENT_BUTTON_BACK; |
| 1167 | } |
| 1168 | if (mBtnForward || mBtnExtra) { |
| 1169 | result |= AMOTION_EVENT_BUTTON_FORWARD; |
| 1170 | } |
| 1171 | return result; |
| 1172 | } |
| 1173 | |
| 1174 | |
| 1175 | // --- CursorMotionAccumulator --- |
| 1176 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1177 | CursorMotionAccumulator::CursorMotionAccumulator() { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1178 | clearRelativeAxes(); |
| 1179 | } |
| 1180 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1181 | void CursorMotionAccumulator::reset(InputDevice* device) { |
| 1182 | clearRelativeAxes(); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | void CursorMotionAccumulator::clearRelativeAxes() { |
| 1186 | mRelX = 0; |
| 1187 | mRelY = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | void CursorMotionAccumulator::process(const RawEvent* rawEvent) { |
| 1191 | if (rawEvent->type == EV_REL) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1192 | switch (rawEvent->code) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1193 | case REL_X: |
| 1194 | mRelX = rawEvent->value; |
| 1195 | break; |
| 1196 | case REL_Y: |
| 1197 | mRelY = rawEvent->value; |
| 1198 | break; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1199 | } |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | void CursorMotionAccumulator::finishSync() { |
| 1204 | clearRelativeAxes(); |
| 1205 | } |
| 1206 | |
| 1207 | |
| 1208 | // --- CursorScrollAccumulator --- |
| 1209 | |
| 1210 | CursorScrollAccumulator::CursorScrollAccumulator() : |
| 1211 | mHaveRelWheel(false), mHaveRelHWheel(false) { |
| 1212 | clearRelativeAxes(); |
| 1213 | } |
| 1214 | |
| 1215 | void CursorScrollAccumulator::configure(InputDevice* device) { |
| 1216 | mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL); |
| 1217 | mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL); |
| 1218 | } |
| 1219 | |
| 1220 | void CursorScrollAccumulator::reset(InputDevice* device) { |
| 1221 | clearRelativeAxes(); |
| 1222 | } |
| 1223 | |
| 1224 | void CursorScrollAccumulator::clearRelativeAxes() { |
| 1225 | mRelWheel = 0; |
| 1226 | mRelHWheel = 0; |
| 1227 | } |
| 1228 | |
| 1229 | void CursorScrollAccumulator::process(const RawEvent* rawEvent) { |
| 1230 | if (rawEvent->type == EV_REL) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1231 | switch (rawEvent->code) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1232 | case REL_WHEEL: |
| 1233 | mRelWheel = rawEvent->value; |
| 1234 | break; |
| 1235 | case REL_HWHEEL: |
| 1236 | mRelHWheel = rawEvent->value; |
| 1237 | break; |
| 1238 | } |
| 1239 | } |
| 1240 | } |
| 1241 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1242 | void CursorScrollAccumulator::finishSync() { |
| 1243 | clearRelativeAxes(); |
| 1244 | } |
| 1245 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1246 | |
| 1247 | // --- TouchButtonAccumulator --- |
| 1248 | |
| 1249 | TouchButtonAccumulator::TouchButtonAccumulator() : |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 1250 | mHaveBtnTouch(false), mHaveStylus(false) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1251 | clearButtons(); |
| 1252 | } |
| 1253 | |
| 1254 | void TouchButtonAccumulator::configure(InputDevice* device) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1255 | mHaveBtnTouch = device->hasKey(BTN_TOUCH); |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 1256 | mHaveStylus = device->hasKey(BTN_TOOL_PEN) |
| 1257 | || device->hasKey(BTN_TOOL_RUBBER) |
| 1258 | || device->hasKey(BTN_TOOL_BRUSH) |
| 1259 | || device->hasKey(BTN_TOOL_PENCIL) |
| 1260 | || device->hasKey(BTN_TOOL_AIRBRUSH); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | void TouchButtonAccumulator::reset(InputDevice* device) { |
| 1264 | mBtnTouch = device->isKeyPressed(BTN_TOUCH); |
| 1265 | mBtnStylus = device->isKeyPressed(BTN_STYLUS); |
| 1266 | mBtnStylus2 = device->isKeyPressed(BTN_STYLUS); |
| 1267 | mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER); |
| 1268 | mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN); |
| 1269 | mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER); |
| 1270 | mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH); |
| 1271 | mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL); |
| 1272 | mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH); |
| 1273 | mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE); |
| 1274 | mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS); |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1275 | mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP); |
| 1276 | mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP); |
| 1277 | mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | void TouchButtonAccumulator::clearButtons() { |
| 1281 | mBtnTouch = 0; |
| 1282 | mBtnStylus = 0; |
| 1283 | mBtnStylus2 = 0; |
| 1284 | mBtnToolFinger = 0; |
| 1285 | mBtnToolPen = 0; |
| 1286 | mBtnToolRubber = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1287 | mBtnToolBrush = 0; |
| 1288 | mBtnToolPencil = 0; |
| 1289 | mBtnToolAirbrush = 0; |
| 1290 | mBtnToolMouse = 0; |
| 1291 | mBtnToolLens = 0; |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1292 | mBtnToolDoubleTap = 0; |
| 1293 | mBtnToolTripleTap = 0; |
| 1294 | mBtnToolQuadTap = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | void TouchButtonAccumulator::process(const RawEvent* rawEvent) { |
| 1298 | if (rawEvent->type == EV_KEY) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1299 | switch (rawEvent->code) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1300 | case BTN_TOUCH: |
| 1301 | mBtnTouch = rawEvent->value; |
| 1302 | break; |
| 1303 | case BTN_STYLUS: |
| 1304 | mBtnStylus = rawEvent->value; |
| 1305 | break; |
| 1306 | case BTN_STYLUS2: |
| 1307 | mBtnStylus2 = rawEvent->value; |
| 1308 | break; |
| 1309 | case BTN_TOOL_FINGER: |
| 1310 | mBtnToolFinger = rawEvent->value; |
| 1311 | break; |
| 1312 | case BTN_TOOL_PEN: |
| 1313 | mBtnToolPen = rawEvent->value; |
| 1314 | break; |
| 1315 | case BTN_TOOL_RUBBER: |
| 1316 | mBtnToolRubber = rawEvent->value; |
| 1317 | break; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1318 | case BTN_TOOL_BRUSH: |
| 1319 | mBtnToolBrush = rawEvent->value; |
| 1320 | break; |
| 1321 | case BTN_TOOL_PENCIL: |
| 1322 | mBtnToolPencil = rawEvent->value; |
| 1323 | break; |
| 1324 | case BTN_TOOL_AIRBRUSH: |
| 1325 | mBtnToolAirbrush = rawEvent->value; |
| 1326 | break; |
| 1327 | case BTN_TOOL_MOUSE: |
| 1328 | mBtnToolMouse = rawEvent->value; |
| 1329 | break; |
| 1330 | case BTN_TOOL_LENS: |
| 1331 | mBtnToolLens = rawEvent->value; |
| 1332 | break; |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1333 | case BTN_TOOL_DOUBLETAP: |
| 1334 | mBtnToolDoubleTap = rawEvent->value; |
| 1335 | break; |
| 1336 | case BTN_TOOL_TRIPLETAP: |
| 1337 | mBtnToolTripleTap = rawEvent->value; |
| 1338 | break; |
| 1339 | case BTN_TOOL_QUADTAP: |
| 1340 | mBtnToolQuadTap = rawEvent->value; |
| 1341 | break; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1342 | } |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | uint32_t TouchButtonAccumulator::getButtonState() const { |
| 1347 | uint32_t result = 0; |
| 1348 | if (mBtnStylus) { |
| 1349 | result |= AMOTION_EVENT_BUTTON_SECONDARY; |
| 1350 | } |
| 1351 | if (mBtnStylus2) { |
| 1352 | result |= AMOTION_EVENT_BUTTON_TERTIARY; |
| 1353 | } |
| 1354 | return result; |
| 1355 | } |
| 1356 | |
| 1357 | int32_t TouchButtonAccumulator::getToolType() const { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1358 | if (mBtnToolMouse || mBtnToolLens) { |
| 1359 | return AMOTION_EVENT_TOOL_TYPE_MOUSE; |
| 1360 | } |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1361 | if (mBtnToolRubber) { |
| 1362 | return AMOTION_EVENT_TOOL_TYPE_ERASER; |
| 1363 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1364 | if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1365 | return AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 1366 | } |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1367 | if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1368 | return AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 1369 | } |
| 1370 | return AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
| 1371 | } |
| 1372 | |
Jeff Brown | d87c6d5 | 2011-08-10 14:55:59 -0700 | [diff] [blame] | 1373 | bool TouchButtonAccumulator::isToolActive() const { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1374 | return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber |
| 1375 | || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1376 | || mBtnToolMouse || mBtnToolLens |
| 1377 | || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | bool TouchButtonAccumulator::isHovering() const { |
| 1381 | return mHaveBtnTouch && !mBtnTouch; |
| 1382 | } |
| 1383 | |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 1384 | bool TouchButtonAccumulator::hasStylus() const { |
| 1385 | return mHaveStylus; |
| 1386 | } |
| 1387 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1388 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1389 | // --- RawPointerAxes --- |
| 1390 | |
| 1391 | RawPointerAxes::RawPointerAxes() { |
| 1392 | clear(); |
| 1393 | } |
| 1394 | |
| 1395 | void RawPointerAxes::clear() { |
| 1396 | x.clear(); |
| 1397 | y.clear(); |
| 1398 | pressure.clear(); |
| 1399 | touchMajor.clear(); |
| 1400 | touchMinor.clear(); |
| 1401 | toolMajor.clear(); |
| 1402 | toolMinor.clear(); |
| 1403 | orientation.clear(); |
| 1404 | distance.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1405 | tiltX.clear(); |
| 1406 | tiltY.clear(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1407 | trackingId.clear(); |
| 1408 | slot.clear(); |
| 1409 | } |
| 1410 | |
| 1411 | |
| 1412 | // --- RawPointerData --- |
| 1413 | |
| 1414 | RawPointerData::RawPointerData() { |
| 1415 | clear(); |
| 1416 | } |
| 1417 | |
| 1418 | void RawPointerData::clear() { |
| 1419 | pointerCount = 0; |
| 1420 | clearIdBits(); |
| 1421 | } |
| 1422 | |
| 1423 | void RawPointerData::copyFrom(const RawPointerData& other) { |
| 1424 | pointerCount = other.pointerCount; |
| 1425 | hoveringIdBits = other.hoveringIdBits; |
| 1426 | touchingIdBits = other.touchingIdBits; |
| 1427 | |
| 1428 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 1429 | pointers[i] = other.pointers[i]; |
| 1430 | |
| 1431 | int id = pointers[i].id; |
| 1432 | idToIndex[id] = other.idToIndex[id]; |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const { |
| 1437 | float x = 0, y = 0; |
| 1438 | uint32_t count = touchingIdBits.count(); |
| 1439 | if (count) { |
| 1440 | for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) { |
| 1441 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 1442 | const Pointer& pointer = pointerForId(id); |
| 1443 | x += pointer.x; |
| 1444 | y += pointer.y; |
| 1445 | } |
| 1446 | x /= count; |
| 1447 | y /= count; |
| 1448 | } |
| 1449 | *outX = x; |
| 1450 | *outY = y; |
| 1451 | } |
| 1452 | |
| 1453 | |
| 1454 | // --- CookedPointerData --- |
| 1455 | |
| 1456 | CookedPointerData::CookedPointerData() { |
| 1457 | clear(); |
| 1458 | } |
| 1459 | |
| 1460 | void CookedPointerData::clear() { |
| 1461 | pointerCount = 0; |
| 1462 | hoveringIdBits.clear(); |
| 1463 | touchingIdBits.clear(); |
| 1464 | } |
| 1465 | |
| 1466 | void CookedPointerData::copyFrom(const CookedPointerData& other) { |
| 1467 | pointerCount = other.pointerCount; |
| 1468 | hoveringIdBits = other.hoveringIdBits; |
| 1469 | touchingIdBits = other.touchingIdBits; |
| 1470 | |
| 1471 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 1472 | pointerProperties[i].copyFrom(other.pointerProperties[i]); |
| 1473 | pointerCoords[i].copyFrom(other.pointerCoords[i]); |
| 1474 | |
| 1475 | int id = pointerProperties[i].id; |
| 1476 | idToIndex[id] = other.idToIndex[id]; |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1481 | // --- SingleTouchMotionAccumulator --- |
| 1482 | |
| 1483 | SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() { |
| 1484 | clearAbsoluteAxes(); |
| 1485 | } |
| 1486 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1487 | void SingleTouchMotionAccumulator::reset(InputDevice* device) { |
| 1488 | mAbsX = device->getAbsoluteAxisValue(ABS_X); |
| 1489 | mAbsY = device->getAbsoluteAxisValue(ABS_Y); |
| 1490 | mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE); |
| 1491 | mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH); |
| 1492 | mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE); |
| 1493 | mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X); |
| 1494 | mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y); |
| 1495 | } |
| 1496 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1497 | void SingleTouchMotionAccumulator::clearAbsoluteAxes() { |
| 1498 | mAbsX = 0; |
| 1499 | mAbsY = 0; |
| 1500 | mAbsPressure = 0; |
| 1501 | mAbsToolWidth = 0; |
| 1502 | mAbsDistance = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1503 | mAbsTiltX = 0; |
| 1504 | mAbsTiltY = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) { |
| 1508 | if (rawEvent->type == EV_ABS) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1509 | switch (rawEvent->code) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1510 | case ABS_X: |
| 1511 | mAbsX = rawEvent->value; |
| 1512 | break; |
| 1513 | case ABS_Y: |
| 1514 | mAbsY = rawEvent->value; |
| 1515 | break; |
| 1516 | case ABS_PRESSURE: |
| 1517 | mAbsPressure = rawEvent->value; |
| 1518 | break; |
| 1519 | case ABS_TOOL_WIDTH: |
| 1520 | mAbsToolWidth = rawEvent->value; |
| 1521 | break; |
| 1522 | case ABS_DISTANCE: |
| 1523 | mAbsDistance = rawEvent->value; |
| 1524 | break; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1525 | case ABS_TILT_X: |
| 1526 | mAbsTiltX = rawEvent->value; |
| 1527 | break; |
| 1528 | case ABS_TILT_Y: |
| 1529 | mAbsTiltY = rawEvent->value; |
| 1530 | break; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1531 | } |
| 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | |
| 1536 | // --- MultiTouchMotionAccumulator --- |
| 1537 | |
| 1538 | MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() : |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 1539 | mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false), |
| 1540 | mHaveStylus(false) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() { |
| 1544 | delete[] mSlots; |
| 1545 | } |
| 1546 | |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 1547 | void MultiTouchMotionAccumulator::configure(InputDevice* device, |
| 1548 | size_t slotCount, bool usingSlotsProtocol) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1549 | mSlotCount = slotCount; |
| 1550 | mUsingSlotsProtocol = usingSlotsProtocol; |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 1551 | mHaveStylus = device->hasAbsoluteAxis(ABS_MT_TOOL_TYPE); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1552 | |
| 1553 | delete[] mSlots; |
| 1554 | mSlots = new Slot[slotCount]; |
| 1555 | } |
| 1556 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1557 | void MultiTouchMotionAccumulator::reset(InputDevice* device) { |
| 1558 | // Unfortunately there is no way to read the initial contents of the slots. |
| 1559 | // So when we reset the accumulator, we must assume they are all zeroes. |
| 1560 | if (mUsingSlotsProtocol) { |
| 1561 | // Query the driver for the current slot index and use it as the initial slot |
| 1562 | // before we start reading events from the device. It is possible that the |
| 1563 | // current slot index will not be the same as it was when the first event was |
| 1564 | // written into the evdev buffer, which means the input mapper could start |
| 1565 | // out of sync with the initial state of the events in the evdev buffer. |
| 1566 | // In the extremely unlikely case that this happens, the data from |
| 1567 | // two slots will be confused until the next ABS_MT_SLOT event is received. |
| 1568 | // This can cause the touch point to "jump", but at least there will be |
| 1569 | // no stuck touches. |
| 1570 | int32_t initialSlot; |
| 1571 | status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(), |
| 1572 | ABS_MT_SLOT, &initialSlot); |
| 1573 | if (status) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1574 | ALOGD("Could not retrieve current multitouch slot index. status=%d", status); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1575 | initialSlot = -1; |
| 1576 | } |
| 1577 | clearSlots(initialSlot); |
| 1578 | } else { |
| 1579 | clearSlots(-1); |
| 1580 | } |
| 1581 | } |
| 1582 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1583 | void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1584 | if (mSlots) { |
| 1585 | for (size_t i = 0; i < mSlotCount; i++) { |
| 1586 | mSlots[i].clear(); |
| 1587 | } |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1588 | } |
| 1589 | mCurrentSlot = initialSlot; |
| 1590 | } |
| 1591 | |
| 1592 | void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) { |
| 1593 | if (rawEvent->type == EV_ABS) { |
| 1594 | bool newSlot = false; |
| 1595 | if (mUsingSlotsProtocol) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1596 | if (rawEvent->code == ABS_MT_SLOT) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1597 | mCurrentSlot = rawEvent->value; |
| 1598 | newSlot = true; |
| 1599 | } |
| 1600 | } else if (mCurrentSlot < 0) { |
| 1601 | mCurrentSlot = 0; |
| 1602 | } |
| 1603 | |
| 1604 | if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) { |
| 1605 | #if DEBUG_POINTERS |
| 1606 | if (newSlot) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1607 | ALOGW("MultiTouch device emitted invalid slot index %d but it " |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1608 | "should be between 0 and %d; ignoring this slot.", |
| 1609 | mCurrentSlot, mSlotCount - 1); |
| 1610 | } |
| 1611 | #endif |
| 1612 | } else { |
| 1613 | Slot* slot = &mSlots[mCurrentSlot]; |
| 1614 | |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1615 | switch (rawEvent->code) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1616 | case ABS_MT_POSITION_X: |
| 1617 | slot->mInUse = true; |
| 1618 | slot->mAbsMTPositionX = rawEvent->value; |
| 1619 | break; |
| 1620 | case ABS_MT_POSITION_Y: |
| 1621 | slot->mInUse = true; |
| 1622 | slot->mAbsMTPositionY = rawEvent->value; |
| 1623 | break; |
| 1624 | case ABS_MT_TOUCH_MAJOR: |
| 1625 | slot->mInUse = true; |
| 1626 | slot->mAbsMTTouchMajor = rawEvent->value; |
| 1627 | break; |
| 1628 | case ABS_MT_TOUCH_MINOR: |
| 1629 | slot->mInUse = true; |
| 1630 | slot->mAbsMTTouchMinor = rawEvent->value; |
| 1631 | slot->mHaveAbsMTTouchMinor = true; |
| 1632 | break; |
| 1633 | case ABS_MT_WIDTH_MAJOR: |
| 1634 | slot->mInUse = true; |
| 1635 | slot->mAbsMTWidthMajor = rawEvent->value; |
| 1636 | break; |
| 1637 | case ABS_MT_WIDTH_MINOR: |
| 1638 | slot->mInUse = true; |
| 1639 | slot->mAbsMTWidthMinor = rawEvent->value; |
| 1640 | slot->mHaveAbsMTWidthMinor = true; |
| 1641 | break; |
| 1642 | case ABS_MT_ORIENTATION: |
| 1643 | slot->mInUse = true; |
| 1644 | slot->mAbsMTOrientation = rawEvent->value; |
| 1645 | break; |
| 1646 | case ABS_MT_TRACKING_ID: |
| 1647 | if (mUsingSlotsProtocol && rawEvent->value < 0) { |
Jeff Brown | 8bcbbef | 2011-08-11 15:49:09 -0700 | [diff] [blame] | 1648 | // The slot is no longer in use but it retains its previous contents, |
| 1649 | // which may be reused for subsequent touches. |
| 1650 | slot->mInUse = false; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1651 | } else { |
| 1652 | slot->mInUse = true; |
| 1653 | slot->mAbsMTTrackingId = rawEvent->value; |
| 1654 | } |
| 1655 | break; |
| 1656 | case ABS_MT_PRESSURE: |
| 1657 | slot->mInUse = true; |
| 1658 | slot->mAbsMTPressure = rawEvent->value; |
| 1659 | break; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1660 | case ABS_MT_DISTANCE: |
| 1661 | slot->mInUse = true; |
| 1662 | slot->mAbsMTDistance = rawEvent->value; |
| 1663 | break; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1664 | case ABS_MT_TOOL_TYPE: |
| 1665 | slot->mInUse = true; |
| 1666 | slot->mAbsMTToolType = rawEvent->value; |
| 1667 | slot->mHaveAbsMTToolType = true; |
| 1668 | break; |
| 1669 | } |
| 1670 | } |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1671 | } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_MT_REPORT) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1672 | // MultiTouch Sync: The driver has returned all data for *one* of the pointers. |
| 1673 | mCurrentSlot += 1; |
| 1674 | } |
| 1675 | } |
| 1676 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1677 | void MultiTouchMotionAccumulator::finishSync() { |
| 1678 | if (!mUsingSlotsProtocol) { |
| 1679 | clearSlots(-1); |
| 1680 | } |
| 1681 | } |
| 1682 | |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 1683 | bool MultiTouchMotionAccumulator::hasStylus() const { |
| 1684 | return mHaveStylus; |
| 1685 | } |
| 1686 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1687 | |
| 1688 | // --- MultiTouchMotionAccumulator::Slot --- |
| 1689 | |
| 1690 | MultiTouchMotionAccumulator::Slot::Slot() { |
| 1691 | clear(); |
| 1692 | } |
| 1693 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1694 | void MultiTouchMotionAccumulator::Slot::clear() { |
| 1695 | mInUse = false; |
| 1696 | mHaveAbsMTTouchMinor = false; |
| 1697 | mHaveAbsMTWidthMinor = false; |
| 1698 | mHaveAbsMTToolType = false; |
| 1699 | mAbsMTPositionX = 0; |
| 1700 | mAbsMTPositionY = 0; |
| 1701 | mAbsMTTouchMajor = 0; |
| 1702 | mAbsMTTouchMinor = 0; |
| 1703 | mAbsMTWidthMajor = 0; |
| 1704 | mAbsMTWidthMinor = 0; |
| 1705 | mAbsMTOrientation = 0; |
| 1706 | mAbsMTTrackingId = -1; |
| 1707 | mAbsMTPressure = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1708 | mAbsMTDistance = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1709 | mAbsMTToolType = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | int32_t MultiTouchMotionAccumulator::Slot::getToolType() const { |
| 1713 | if (mHaveAbsMTToolType) { |
| 1714 | switch (mAbsMTToolType) { |
| 1715 | case MT_TOOL_FINGER: |
| 1716 | return AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 1717 | case MT_TOOL_PEN: |
| 1718 | return AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 1719 | } |
| 1720 | } |
| 1721 | return AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
| 1722 | } |
| 1723 | |
| 1724 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1725 | // --- InputMapper --- |
| 1726 | |
| 1727 | InputMapper::InputMapper(InputDevice* device) : |
| 1728 | mDevice(device), mContext(device->getContext()) { |
| 1729 | } |
| 1730 | |
| 1731 | InputMapper::~InputMapper() { |
| 1732 | } |
| 1733 | |
| 1734 | void InputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 1735 | info->addSource(getSources()); |
| 1736 | } |
| 1737 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1738 | void InputMapper::dump(String8& dump) { |
| 1739 | } |
| 1740 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1741 | void InputMapper::configure(nsecs_t when, |
| 1742 | const InputReaderConfiguration* config, uint32_t changes) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1743 | } |
| 1744 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1745 | void InputMapper::reset(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1746 | } |
| 1747 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 1748 | void InputMapper::timeoutExpired(nsecs_t when) { |
| 1749 | } |
| 1750 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1751 | int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 1752 | return AKEY_STATE_UNKNOWN; |
| 1753 | } |
| 1754 | |
| 1755 | int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 1756 | return AKEY_STATE_UNKNOWN; |
| 1757 | } |
| 1758 | |
| 1759 | int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 1760 | return AKEY_STATE_UNKNOWN; |
| 1761 | } |
| 1762 | |
| 1763 | bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 1764 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 1765 | return false; |
| 1766 | } |
| 1767 | |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 1768 | void InputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, |
| 1769 | int32_t token) { |
| 1770 | } |
| 1771 | |
| 1772 | void InputMapper::cancelVibrate(int32_t token) { |
| 1773 | } |
| 1774 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1775 | int32_t InputMapper::getMetaState() { |
| 1776 | return 0; |
| 1777 | } |
| 1778 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1779 | void InputMapper::fadePointer() { |
| 1780 | } |
| 1781 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1782 | status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) { |
| 1783 | return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo); |
| 1784 | } |
| 1785 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 1786 | void InputMapper::bumpGeneration() { |
| 1787 | mDevice->bumpGeneration(); |
| 1788 | } |
| 1789 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1790 | void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump, |
| 1791 | const RawAbsoluteAxisInfo& axis, const char* name) { |
| 1792 | if (axis.valid) { |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 1793 | dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n", |
| 1794 | name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1795 | } else { |
| 1796 | dump.appendFormat(INDENT4 "%s: unknown range\n", name); |
| 1797 | } |
| 1798 | } |
| 1799 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1800 | |
| 1801 | // --- SwitchInputMapper --- |
| 1802 | |
| 1803 | SwitchInputMapper::SwitchInputMapper(InputDevice* device) : |
Jeff Brown | bcc046a | 2012-09-27 20:46:43 -0700 | [diff] [blame] | 1804 | InputMapper(device), mUpdatedSwitchValues(0), mUpdatedSwitchMask(0) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1805 | } |
| 1806 | |
| 1807 | SwitchInputMapper::~SwitchInputMapper() { |
| 1808 | } |
| 1809 | |
| 1810 | uint32_t SwitchInputMapper::getSources() { |
Jeff Brown | 89de57a | 2011-01-19 18:41:38 -0800 | [diff] [blame] | 1811 | return AINPUT_SOURCE_SWITCH; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1812 | } |
| 1813 | |
| 1814 | void SwitchInputMapper::process(const RawEvent* rawEvent) { |
| 1815 | switch (rawEvent->type) { |
| 1816 | case EV_SW: |
Jeff Brown | bcc046a | 2012-09-27 20:46:43 -0700 | [diff] [blame] | 1817 | processSwitch(rawEvent->code, rawEvent->value); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1818 | break; |
Jeff Brown | bcc046a | 2012-09-27 20:46:43 -0700 | [diff] [blame] | 1819 | |
| 1820 | case EV_SYN: |
| 1821 | if (rawEvent->code == SYN_REPORT) { |
| 1822 | sync(rawEvent->when); |
| 1823 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1824 | } |
| 1825 | } |
| 1826 | |
Jeff Brown | bcc046a | 2012-09-27 20:46:43 -0700 | [diff] [blame] | 1827 | void SwitchInputMapper::processSwitch(int32_t switchCode, int32_t switchValue) { |
| 1828 | if (switchCode >= 0 && switchCode < 32) { |
| 1829 | if (switchValue) { |
| 1830 | mUpdatedSwitchValues |= 1 << switchCode; |
| 1831 | } |
| 1832 | mUpdatedSwitchMask |= 1 << switchCode; |
| 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | void SwitchInputMapper::sync(nsecs_t when) { |
| 1837 | if (mUpdatedSwitchMask) { |
| 1838 | NotifySwitchArgs args(when, 0, mUpdatedSwitchValues, mUpdatedSwitchMask); |
| 1839 | getListener()->notifySwitch(&args); |
| 1840 | |
| 1841 | mUpdatedSwitchValues = 0; |
| 1842 | mUpdatedSwitchMask = 0; |
| 1843 | } |
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)) { |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2008 | if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) { |
| 2009 | DisplayViewport v; |
| 2010 | if (config->getDisplayInfo(false /*external*/, &v)) { |
| 2011 | mOrientation = v.orientation; |
| 2012 | } else { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2013 | mOrientation = DISPLAY_ORIENTATION_0; |
| 2014 | } |
| 2015 | } else { |
| 2016 | mOrientation = DISPLAY_ORIENTATION_0; |
| 2017 | } |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2018 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | void KeyboardInputMapper::configureParameters() { |
| 2022 | mParameters.orientationAware = false; |
| 2023 | getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"), |
| 2024 | mParameters.orientationAware); |
| 2025 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2026 | mParameters.hasAssociatedDisplay = false; |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2027 | if (mParameters.orientationAware) { |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2028 | mParameters.hasAssociatedDisplay = true; |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2029 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | void KeyboardInputMapper::dumpParameters(String8& dump) { |
| 2033 | dump.append(INDENT3 "Parameters:\n"); |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2034 | dump.appendFormat(INDENT4 "HasAssociatedDisplay: %s\n", |
| 2035 | toString(mParameters.hasAssociatedDisplay)); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2036 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 2037 | toString(mParameters.orientationAware)); |
| 2038 | } |
| 2039 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2040 | void KeyboardInputMapper::reset(nsecs_t when) { |
| 2041 | mMetaState = AMETA_NONE; |
| 2042 | mDownTime = 0; |
| 2043 | mKeyDowns.clear(); |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 2044 | mCurrentHidUsage = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2045 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2046 | resetLedState(); |
| 2047 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2048 | InputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | void KeyboardInputMapper::process(const RawEvent* rawEvent) { |
| 2052 | switch (rawEvent->type) { |
| 2053 | case EV_KEY: { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 2054 | int32_t scanCode = rawEvent->code; |
| 2055 | int32_t usageCode = mCurrentHidUsage; |
| 2056 | mCurrentHidUsage = 0; |
| 2057 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2058 | if (isKeyboardOrGamepadKey(scanCode)) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 2059 | int32_t keyCode; |
| 2060 | uint32_t flags; |
| 2061 | if (getEventHub()->mapKey(getDeviceId(), scanCode, usageCode, &keyCode, &flags)) { |
| 2062 | keyCode = AKEYCODE_UNKNOWN; |
| 2063 | flags = 0; |
| 2064 | } |
| 2065 | processKey(rawEvent->when, rawEvent->value != 0, keyCode, scanCode, flags); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2066 | } |
| 2067 | break; |
| 2068 | } |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 2069 | case EV_MSC: { |
| 2070 | if (rawEvent->code == MSC_SCAN) { |
| 2071 | mCurrentHidUsage = rawEvent->value; |
| 2072 | } |
| 2073 | break; |
| 2074 | } |
| 2075 | case EV_SYN: { |
| 2076 | if (rawEvent->code == SYN_REPORT) { |
| 2077 | mCurrentHidUsage = 0; |
| 2078 | } |
| 2079 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) { |
| 2084 | return scanCode < BTN_MOUSE |
| 2085 | || scanCode >= KEY_OK |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 2086 | || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE) |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2087 | || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2088 | } |
| 2089 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2090 | void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode, |
| 2091 | int32_t scanCode, uint32_t policyFlags) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2092 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2093 | if (down) { |
| 2094 | // Rotate key codes according to orientation if needed. |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2095 | if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2096 | keyCode = rotateKeyCode(keyCode, mOrientation); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2097 | } |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 2098 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2099 | // Add key down. |
| 2100 | ssize_t keyDownIndex = findKeyDown(scanCode); |
| 2101 | if (keyDownIndex >= 0) { |
| 2102 | // key repeat, be sure to use same keycode as before in case of rotation |
| 2103 | keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2104 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2105 | // key down |
| 2106 | if ((policyFlags & POLICY_FLAG_VIRTUAL) |
| 2107 | && mContext->shouldDropVirtualKey(when, |
| 2108 | getDevice(), keyCode, scanCode)) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2109 | return; |
| 2110 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2111 | |
| 2112 | mKeyDowns.push(); |
| 2113 | KeyDown& keyDown = mKeyDowns.editTop(); |
| 2114 | keyDown.keyCode = keyCode; |
| 2115 | keyDown.scanCode = scanCode; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2116 | } |
| 2117 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2118 | mDownTime = when; |
| 2119 | } else { |
| 2120 | // Remove key down. |
| 2121 | ssize_t keyDownIndex = findKeyDown(scanCode); |
| 2122 | if (keyDownIndex >= 0) { |
| 2123 | // key up, be sure to use same keycode as before in case of rotation |
| 2124 | keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode; |
| 2125 | mKeyDowns.removeAt(size_t(keyDownIndex)); |
| 2126 | } else { |
| 2127 | // key was not actually down |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2128 | 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] | 2129 | "keyCode=%d, scanCode=%d", |
| 2130 | getDeviceName().string(), keyCode, scanCode); |
| 2131 | return; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2132 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2133 | } |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 2134 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2135 | bool metaStateChanged = false; |
| 2136 | int32_t oldMetaState = mMetaState; |
| 2137 | int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState); |
| 2138 | if (oldMetaState != newMetaState) { |
| 2139 | mMetaState = newMetaState; |
| 2140 | metaStateChanged = true; |
| 2141 | updateLedState(false); |
| 2142 | } |
| 2143 | |
| 2144 | nsecs_t downTime = mDownTime; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2145 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2146 | // Key down on external an keyboard should wake the device. |
| 2147 | // We don't do this for internal keyboards to prevent them from waking up in your pocket. |
| 2148 | // For internal keyboards, the key layout file should specify the policy flags for |
| 2149 | // each wake key individually. |
| 2150 | // TODO: Use the input device configuration to control this behavior more finely. |
| 2151 | if (down && getDevice()->isExternal() |
| 2152 | && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) { |
| 2153 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 2154 | } |
| 2155 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2156 | if (metaStateChanged) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2157 | getContext()->updateGlobalMetaState(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2158 | } |
| 2159 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2160 | if (down && !isMetaKey(keyCode)) { |
| 2161 | getContext()->fadePointer(); |
| 2162 | } |
| 2163 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2164 | NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2165 | down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, |
| 2166 | AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2167 | getListener()->notifyKey(&args); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2168 | } |
| 2169 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2170 | ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) { |
| 2171 | size_t n = mKeyDowns.size(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2172 | for (size_t i = 0; i < n; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2173 | if (mKeyDowns[i].scanCode == scanCode) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2174 | return i; |
| 2175 | } |
| 2176 | } |
| 2177 | return -1; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2178 | } |
| 2179 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2180 | int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 2181 | return getEventHub()->getKeyCodeState(getDeviceId(), keyCode); |
| 2182 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2183 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2184 | int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 2185 | return getEventHub()->getScanCodeState(getDeviceId(), scanCode); |
| 2186 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2187 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2188 | bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 2189 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 2190 | return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags); |
| 2191 | } |
| 2192 | |
| 2193 | int32_t KeyboardInputMapper::getMetaState() { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2194 | return mMetaState; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2195 | } |
| 2196 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2197 | void KeyboardInputMapper::resetLedState() { |
| 2198 | initializeLedState(mCapsLockLedState, LED_CAPSL); |
| 2199 | initializeLedState(mNumLockLedState, LED_NUML); |
| 2200 | initializeLedState(mScrollLockLedState, LED_SCROLLL); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2201 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2202 | updateLedState(true); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2203 | } |
| 2204 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2205 | void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) { |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2206 | ledState.avail = getEventHub()->hasLed(getDeviceId(), led); |
| 2207 | ledState.on = false; |
| 2208 | } |
| 2209 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2210 | void KeyboardInputMapper::updateLedState(bool reset) { |
| 2211 | updateLedStateForModifier(mCapsLockLedState, LED_CAPSL, |
Jeff Brown | 51e7fe7 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 2212 | AMETA_CAPS_LOCK_ON, reset); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2213 | updateLedStateForModifier(mNumLockLedState, LED_NUML, |
Jeff Brown | 51e7fe7 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 2214 | AMETA_NUM_LOCK_ON, reset); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2215 | updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL, |
Jeff Brown | 51e7fe7 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 2216 | AMETA_SCROLL_LOCK_ON, reset); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 2217 | } |
| 2218 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2219 | void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState, |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 2220 | int32_t led, int32_t modifier, bool reset) { |
| 2221 | if (ledState.avail) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2222 | bool desiredState = (mMetaState & modifier) != 0; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2223 | if (reset || ledState.on != desiredState) { |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 2224 | getEventHub()->setLedState(getDeviceId(), led, desiredState); |
| 2225 | ledState.on = desiredState; |
| 2226 | } |
| 2227 | } |
| 2228 | } |
| 2229 | |
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 --- |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2232 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2233 | CursorInputMapper::CursorInputMapper(InputDevice* device) : |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2234 | InputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2235 | } |
| 2236 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2237 | CursorInputMapper::~CursorInputMapper() { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2238 | } |
| 2239 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2240 | uint32_t CursorInputMapper::getSources() { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2241 | return mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2242 | } |
| 2243 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2244 | void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2245 | InputMapper::populateDeviceInfo(info); |
| 2246 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2247 | if (mParameters.mode == Parameters::MODE_POINTER) { |
| 2248 | float minX, minY, maxX, maxY; |
| 2249 | if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2250 | info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f); |
| 2251 | 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] | 2252 | } |
| 2253 | } else { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2254 | info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale); |
| 2255 | 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] | 2256 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2257 | 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] | 2258 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2259 | if (mCursorScrollAccumulator.haveRelativeVWheel()) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2260 | 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] | 2261 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2262 | if (mCursorScrollAccumulator.haveRelativeHWheel()) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2263 | 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] | 2264 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2265 | } |
| 2266 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2267 | void CursorInputMapper::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2268 | dump.append(INDENT2 "Cursor Input Mapper:\n"); |
| 2269 | dumpParameters(dump); |
| 2270 | dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale); |
| 2271 | dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale); |
| 2272 | dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision); |
| 2273 | dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision); |
| 2274 | dump.appendFormat(INDENT3 "HaveVWheel: %s\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2275 | toString(mCursorScrollAccumulator.haveRelativeVWheel())); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2276 | dump.appendFormat(INDENT3 "HaveHWheel: %s\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2277 | toString(mCursorScrollAccumulator.haveRelativeHWheel())); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2278 | dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale); |
| 2279 | dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2280 | dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2281 | dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState); |
| 2282 | dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState))); |
| 2283 | dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2284 | } |
| 2285 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2286 | void CursorInputMapper::configure(nsecs_t when, |
| 2287 | const InputReaderConfiguration* config, uint32_t changes) { |
| 2288 | InputMapper::configure(when, config, changes); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2289 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2290 | if (!changes) { // first time only |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2291 | mCursorScrollAccumulator.configure(getDevice()); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2292 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2293 | // Configure basic parameters. |
| 2294 | configureParameters(); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2295 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2296 | // Configure device mode. |
| 2297 | switch (mParameters.mode) { |
| 2298 | case Parameters::MODE_POINTER: |
| 2299 | mSource = AINPUT_SOURCE_MOUSE; |
| 2300 | mXPrecision = 1.0f; |
| 2301 | mYPrecision = 1.0f; |
| 2302 | mXScale = 1.0f; |
| 2303 | mYScale = 1.0f; |
| 2304 | mPointerController = getPolicy()->obtainPointerController(getDeviceId()); |
| 2305 | break; |
| 2306 | case Parameters::MODE_NAVIGATION: |
| 2307 | mSource = AINPUT_SOURCE_TRACKBALL; |
| 2308 | mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 2309 | mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 2310 | mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 2311 | mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 2312 | break; |
| 2313 | } |
| 2314 | |
| 2315 | mVWheelScale = 1.0f; |
| 2316 | mHWheelScale = 1.0f; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2317 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 2318 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2319 | if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) { |
| 2320 | mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters); |
| 2321 | mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters); |
| 2322 | mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters); |
| 2323 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2324 | |
| 2325 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) { |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2326 | if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) { |
| 2327 | DisplayViewport v; |
| 2328 | if (config->getDisplayInfo(false /*external*/, &v)) { |
| 2329 | mOrientation = v.orientation; |
| 2330 | } else { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2331 | mOrientation = DISPLAY_ORIENTATION_0; |
| 2332 | } |
| 2333 | } else { |
| 2334 | mOrientation = DISPLAY_ORIENTATION_0; |
| 2335 | } |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 2336 | bumpGeneration(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2337 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2338 | } |
| 2339 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2340 | void CursorInputMapper::configureParameters() { |
| 2341 | mParameters.mode = Parameters::MODE_POINTER; |
| 2342 | String8 cursorModeString; |
| 2343 | if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) { |
| 2344 | if (cursorModeString == "navigation") { |
| 2345 | mParameters.mode = Parameters::MODE_NAVIGATION; |
| 2346 | } else if (cursorModeString != "pointer" && cursorModeString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2347 | ALOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string()); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2348 | } |
| 2349 | } |
| 2350 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2351 | mParameters.orientationAware = false; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2352 | getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"), |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2353 | mParameters.orientationAware); |
| 2354 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2355 | mParameters.hasAssociatedDisplay = false; |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2356 | if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) { |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2357 | mParameters.hasAssociatedDisplay = true; |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2358 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2359 | } |
| 2360 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2361 | void CursorInputMapper::dumpParameters(String8& dump) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2362 | dump.append(INDENT3 "Parameters:\n"); |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2363 | dump.appendFormat(INDENT4 "HasAssociatedDisplay: %s\n", |
| 2364 | toString(mParameters.hasAssociatedDisplay)); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2365 | |
| 2366 | switch (mParameters.mode) { |
| 2367 | case Parameters::MODE_POINTER: |
| 2368 | dump.append(INDENT4 "Mode: pointer\n"); |
| 2369 | break; |
| 2370 | case Parameters::MODE_NAVIGATION: |
| 2371 | dump.append(INDENT4 "Mode: navigation\n"); |
| 2372 | break; |
| 2373 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 2374 | ALOG_ASSERT(false); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2375 | } |
| 2376 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2377 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 2378 | toString(mParameters.orientationAware)); |
| 2379 | } |
| 2380 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2381 | void CursorInputMapper::reset(nsecs_t when) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2382 | mButtonState = 0; |
| 2383 | mDownTime = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2384 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2385 | mPointerVelocityControl.reset(); |
| 2386 | mWheelXVelocityControl.reset(); |
| 2387 | mWheelYVelocityControl.reset(); |
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 | mCursorButtonAccumulator.reset(getDevice()); |
| 2390 | mCursorMotionAccumulator.reset(getDevice()); |
| 2391 | mCursorScrollAccumulator.reset(getDevice()); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2392 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2393 | InputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2394 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2395 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2396 | void CursorInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2397 | mCursorButtonAccumulator.process(rawEvent); |
| 2398 | mCursorMotionAccumulator.process(rawEvent); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2399 | mCursorScrollAccumulator.process(rawEvent); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2400 | |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 2401 | if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2402 | sync(rawEvent->when); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2403 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2404 | } |
| 2405 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2406 | void CursorInputMapper::sync(nsecs_t when) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2407 | int32_t lastButtonState = mButtonState; |
| 2408 | int32_t currentButtonState = mCursorButtonAccumulator.getButtonState(); |
| 2409 | mButtonState = currentButtonState; |
| 2410 | |
| 2411 | bool wasDown = isPointerDown(lastButtonState); |
| 2412 | bool down = isPointerDown(currentButtonState); |
| 2413 | bool downChanged; |
| 2414 | if (!wasDown && down) { |
| 2415 | mDownTime = when; |
| 2416 | downChanged = true; |
| 2417 | } else if (wasDown && !down) { |
| 2418 | downChanged = true; |
| 2419 | } else { |
| 2420 | downChanged = false; |
| 2421 | } |
| 2422 | nsecs_t downTime = mDownTime; |
| 2423 | bool buttonsChanged = currentButtonState != lastButtonState; |
Jeff Brown | c28306a | 2011-08-23 21:32:42 -0700 | [diff] [blame] | 2424 | bool buttonsPressed = currentButtonState & ~lastButtonState; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2425 | |
| 2426 | float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale; |
| 2427 | float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale; |
| 2428 | bool moved = deltaX != 0 || deltaY != 0; |
| 2429 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2430 | // Rotate delta according to orientation if needed. |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2431 | if (mParameters.orientationAware && mParameters.hasAssociatedDisplay |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2432 | && (deltaX != 0.0f || deltaY != 0.0f)) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2433 | rotateDelta(mOrientation, &deltaX, &deltaY); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2434 | } |
| 2435 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2436 | // Move the pointer. |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2437 | PointerProperties pointerProperties; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2438 | pointerProperties.clear(); |
| 2439 | pointerProperties.id = 0; |
| 2440 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE; |
| 2441 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2442 | PointerCoords pointerCoords; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2443 | pointerCoords.clear(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2444 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2445 | float vscroll = mCursorScrollAccumulator.getRelativeVWheel(); |
| 2446 | float hscroll = mCursorScrollAccumulator.getRelativeHWheel(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2447 | bool scrolled = vscroll != 0 || hscroll != 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2448 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2449 | mWheelYVelocityControl.move(when, NULL, &vscroll); |
| 2450 | mWheelXVelocityControl.move(when, &hscroll, NULL); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2451 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2452 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2453 | |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2454 | int32_t displayId; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2455 | if (mPointerController != NULL) { |
| 2456 | if (moved || scrolled || buttonsChanged) { |
| 2457 | mPointerController->setPresentation( |
| 2458 | PointerControllerInterface::PRESENTATION_POINTER); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2459 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2460 | if (moved) { |
| 2461 | mPointerController->move(deltaX, deltaY); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2462 | } |
| 2463 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2464 | if (buttonsChanged) { |
| 2465 | mPointerController->setButtonState(currentButtonState); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2466 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2467 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2468 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2469 | } |
| 2470 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2471 | float x, y; |
| 2472 | mPointerController->getPosition(&x, &y); |
| 2473 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 2474 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2475 | displayId = ADISPLAY_ID_DEFAULT; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2476 | } else { |
| 2477 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX); |
| 2478 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY); |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2479 | displayId = ADISPLAY_ID_NONE; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2483 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2484 | // Moving an external trackball or mouse should wake the device. |
| 2485 | // We don't do this for internal cursor devices to prevent them from waking up |
| 2486 | // the device in your pocket. |
| 2487 | // TODO: Use the input device configuration to control this behavior more finely. |
| 2488 | uint32_t policyFlags = 0; |
Jeff Brown | c28306a | 2011-08-23 21:32:42 -0700 | [diff] [blame] | 2489 | if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) { |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2490 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 2491 | } |
| 2492 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2493 | // Synthesize key down from buttons if needed. |
| 2494 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource, |
| 2495 | policyFlags, lastButtonState, currentButtonState); |
| 2496 | |
| 2497 | // Send motion event. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2498 | if (downChanged || moved || scrolled || buttonsChanged) { |
| 2499 | int32_t metaState = mContext->getGlobalMetaState(); |
| 2500 | int32_t motionEventAction; |
| 2501 | if (downChanged) { |
| 2502 | motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP; |
| 2503 | } else if (down || mPointerController == NULL) { |
| 2504 | motionEventAction = AMOTION_EVENT_ACTION_MOVE; |
| 2505 | } else { |
| 2506 | motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE; |
| 2507 | } |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2508 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2509 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 2510 | motionEventAction, 0, metaState, currentButtonState, 0, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2511 | displayId, 1, &pointerProperties, &pointerCoords, |
| 2512 | mXPrecision, mYPrecision, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2513 | getListener()->notifyMotion(&args); |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2514 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2515 | // Send hover move after UP to tell the application that the mouse is hovering now. |
| 2516 | if (motionEventAction == AMOTION_EVENT_ACTION_UP |
| 2517 | && mPointerController != NULL) { |
| 2518 | NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags, |
| 2519 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 2520 | metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2521 | displayId, 1, &pointerProperties, &pointerCoords, |
| 2522 | mXPrecision, mYPrecision, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2523 | getListener()->notifyMotion(&hoverArgs); |
| 2524 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2525 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2526 | // Send scroll events. |
| 2527 | if (scrolled) { |
| 2528 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll); |
| 2529 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll); |
| 2530 | |
| 2531 | NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags, |
| 2532 | AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState, |
| 2533 | AMOTION_EVENT_EDGE_FLAG_NONE, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2534 | displayId, 1, &pointerProperties, &pointerCoords, |
| 2535 | mXPrecision, mYPrecision, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2536 | getListener()->notifyMotion(&scrollArgs); |
| 2537 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2538 | } |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2539 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2540 | // Synthesize key up from buttons if needed. |
| 2541 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource, |
| 2542 | policyFlags, lastButtonState, currentButtonState); |
| 2543 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2544 | mCursorMotionAccumulator.finishSync(); |
| 2545 | mCursorScrollAccumulator.finishSync(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2546 | } |
| 2547 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2548 | int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Jeff Brown | c3fc2d0 | 2010-08-10 15:47:53 -0700 | [diff] [blame] | 2549 | if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) { |
| 2550 | return getEventHub()->getScanCodeState(getDeviceId(), scanCode); |
| 2551 | } else { |
| 2552 | return AKEY_STATE_UNKNOWN; |
| 2553 | } |
| 2554 | } |
| 2555 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2556 | void CursorInputMapper::fadePointer() { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2557 | if (mPointerController != NULL) { |
| 2558 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 2559 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2560 | } |
| 2561 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2562 | |
| 2563 | // --- TouchInputMapper --- |
| 2564 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2565 | TouchInputMapper::TouchInputMapper(InputDevice* device) : |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2566 | InputMapper(device), |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2567 | mSource(0), mDeviceMode(DEVICE_MODE_DISABLED), |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2568 | mSurfaceWidth(-1), mSurfaceHeight(-1), mSurfaceLeft(0), mSurfaceTop(0), |
| 2569 | mSurfaceOrientation(DISPLAY_ORIENTATION_0) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2570 | } |
| 2571 | |
| 2572 | TouchInputMapper::~TouchInputMapper() { |
| 2573 | } |
| 2574 | |
| 2575 | uint32_t TouchInputMapper::getSources() { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2576 | return mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2577 | } |
| 2578 | |
| 2579 | void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 2580 | InputMapper::populateDeviceInfo(info); |
| 2581 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2582 | if (mDeviceMode != DEVICE_MODE_DISABLED) { |
| 2583 | info->addMotionRange(mOrientedRanges.x); |
| 2584 | info->addMotionRange(mOrientedRanges.y); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2585 | info->addMotionRange(mOrientedRanges.pressure); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2586 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2587 | if (mOrientedRanges.haveSize) { |
| 2588 | info->addMotionRange(mOrientedRanges.size); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2589 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2590 | |
| 2591 | if (mOrientedRanges.haveTouchSize) { |
| 2592 | info->addMotionRange(mOrientedRanges.touchMajor); |
| 2593 | info->addMotionRange(mOrientedRanges.touchMinor); |
| 2594 | } |
| 2595 | |
| 2596 | if (mOrientedRanges.haveToolSize) { |
| 2597 | info->addMotionRange(mOrientedRanges.toolMajor); |
| 2598 | info->addMotionRange(mOrientedRanges.toolMinor); |
| 2599 | } |
| 2600 | |
| 2601 | if (mOrientedRanges.haveOrientation) { |
| 2602 | info->addMotionRange(mOrientedRanges.orientation); |
| 2603 | } |
| 2604 | |
| 2605 | if (mOrientedRanges.haveDistance) { |
| 2606 | info->addMotionRange(mOrientedRanges.distance); |
| 2607 | } |
| 2608 | |
| 2609 | if (mOrientedRanges.haveTilt) { |
| 2610 | info->addMotionRange(mOrientedRanges.tilt); |
| 2611 | } |
| 2612 | |
| 2613 | if (mCursorScrollAccumulator.haveRelativeVWheel()) { |
| 2614 | info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f); |
| 2615 | } |
| 2616 | if (mCursorScrollAccumulator.haveRelativeHWheel()) { |
| 2617 | info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f); |
| 2618 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2619 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2620 | } |
| 2621 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2622 | void TouchInputMapper::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2623 | dump.append(INDENT2 "Touch Input Mapper:\n"); |
| 2624 | dumpParameters(dump); |
| 2625 | dumpVirtualKeys(dump); |
| 2626 | dumpRawPointerAxes(dump); |
| 2627 | dumpCalibration(dump); |
| 2628 | dumpSurface(dump); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2629 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2630 | dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n"); |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2631 | dump.appendFormat(INDENT4 "XTranslate: %0.3f\n", mXTranslate); |
| 2632 | dump.appendFormat(INDENT4 "YTranslate: %0.3f\n", mYTranslate); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2633 | dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale); |
| 2634 | dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale); |
| 2635 | dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision); |
| 2636 | dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision); |
| 2637 | dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2638 | dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale); |
| 2639 | dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale); |
| 2640 | dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale); |
| 2641 | dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2642 | dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt)); |
| 2643 | dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter); |
| 2644 | dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale); |
| 2645 | dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter); |
| 2646 | dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2647 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2648 | dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2649 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2650 | dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n", |
| 2651 | mLastRawPointerData.pointerCount); |
| 2652 | for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) { |
| 2653 | const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i]; |
| 2654 | dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, " |
| 2655 | "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2656 | "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, " |
| 2657 | "toolType=%d, isHovering=%s\n", i, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2658 | pointer.id, pointer.x, pointer.y, pointer.pressure, |
| 2659 | pointer.touchMajor, pointer.touchMinor, |
| 2660 | pointer.toolMajor, pointer.toolMinor, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2661 | pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2662 | pointer.toolType, toString(pointer.isHovering)); |
| 2663 | } |
| 2664 | |
| 2665 | dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n", |
| 2666 | mLastCookedPointerData.pointerCount); |
| 2667 | for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) { |
| 2668 | const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i]; |
| 2669 | const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i]; |
| 2670 | dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, " |
| 2671 | "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2672 | "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, " |
| 2673 | "toolType=%d, isHovering=%s\n", i, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2674 | pointerProperties.id, |
| 2675 | pointerCoords.getX(), |
| 2676 | pointerCoords.getY(), |
| 2677 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 2678 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 2679 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 2680 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 2681 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 2682 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2683 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT), |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2684 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), |
| 2685 | pointerProperties.toolType, |
| 2686 | toString(mLastCookedPointerData.isHovering(i))); |
| 2687 | } |
| 2688 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2689 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2690 | dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n"); |
| 2691 | dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2692 | mPointerXMovementScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2693 | dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2694 | mPointerYMovementScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2695 | dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2696 | mPointerXZoomScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2697 | dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2698 | mPointerYZoomScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2699 | dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n", |
| 2700 | mPointerGestureMaxSwipeWidth); |
| 2701 | } |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2702 | } |
| 2703 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2704 | void TouchInputMapper::configure(nsecs_t when, |
| 2705 | const InputReaderConfiguration* config, uint32_t changes) { |
| 2706 | InputMapper::configure(when, config, changes); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2707 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2708 | mConfig = *config; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2709 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2710 | if (!changes) { // first time only |
| 2711 | // Configure basic parameters. |
| 2712 | configureParameters(); |
| 2713 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2714 | // Configure common accumulators. |
| 2715 | mCursorScrollAccumulator.configure(getDevice()); |
| 2716 | mTouchButtonAccumulator.configure(getDevice()); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2717 | |
| 2718 | // Configure absolute axis information. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2719 | configureRawPointerAxes(); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2720 | |
| 2721 | // Prepare input device calibration. |
| 2722 | parseCalibration(); |
| 2723 | resolveCalibration(); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2724 | } |
| 2725 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2726 | if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2727 | // Update pointer speed. |
| 2728 | mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters); |
| 2729 | mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters); |
| 2730 | mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2731 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2732 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2733 | bool resetNeeded = false; |
| 2734 | if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 2735 | | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT |
| 2736 | | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2737 | // Configure device sources, surface dimensions, orientation and |
| 2738 | // scaling factors. |
| 2739 | configureSurface(when, &resetNeeded); |
| 2740 | } |
| 2741 | |
| 2742 | if (changes && resetNeeded) { |
| 2743 | // Send reset, unless this is the first time the device has been configured, |
| 2744 | // in which case the reader will call reset itself after all mappers are ready. |
| 2745 | getDevice()->notifyReset(when); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2746 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2747 | } |
| 2748 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2749 | void TouchInputMapper::configureParameters() { |
Jeff Brown | b126822 | 2011-06-03 17:06:16 -0700 | [diff] [blame] | 2750 | // Use the pointer presentation mode for devices that do not support distinct |
| 2751 | // multitouch. The spot-based presentation relies on being able to accurately |
| 2752 | // locate two or more fingers on the touch pad. |
| 2753 | mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT) |
| 2754 | ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2755 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2756 | String8 gestureModeString; |
| 2757 | if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"), |
| 2758 | gestureModeString)) { |
| 2759 | if (gestureModeString == "pointer") { |
| 2760 | mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER; |
| 2761 | } else if (gestureModeString == "spots") { |
| 2762 | mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS; |
| 2763 | } else if (gestureModeString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2764 | ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string()); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2765 | } |
| 2766 | } |
| 2767 | |
Jeff Brown | deffe07 | 2011-08-26 18:38:46 -0700 | [diff] [blame] | 2768 | if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) { |
| 2769 | // The device is a touch screen. |
| 2770 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
| 2771 | } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) { |
| 2772 | // The device is a pointing device like a track pad. |
| 2773 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
| 2774 | } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X) |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2775 | || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) { |
| 2776 | // The device is a cursor device with a touch pad attached. |
| 2777 | // By default don't use the touch pad to move the pointer. |
| 2778 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; |
| 2779 | } else { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2780 | // The device is a touch pad of unknown purpose. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2781 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
| 2782 | } |
| 2783 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2784 | String8 deviceTypeString; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2785 | if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"), |
| 2786 | deviceTypeString)) { |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 2787 | if (deviceTypeString == "touchScreen") { |
| 2788 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2789 | } else if (deviceTypeString == "touchPad") { |
| 2790 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2791 | } else if (deviceTypeString == "pointer") { |
| 2792 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2793 | } else if (deviceTypeString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2794 | ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string()); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2795 | } |
| 2796 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2797 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2798 | mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2799 | getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"), |
| 2800 | mParameters.orientationAware); |
| 2801 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2802 | mParameters.hasAssociatedDisplay = false; |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2803 | mParameters.associatedDisplayIsExternal = false; |
| 2804 | if (mParameters.orientationAware |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2805 | || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2806 | || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) { |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2807 | mParameters.hasAssociatedDisplay = true; |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2808 | mParameters.associatedDisplayIsExternal = |
| 2809 | mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN |
| 2810 | && getDevice()->isExternal(); |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2811 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2812 | } |
| 2813 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2814 | void TouchInputMapper::dumpParameters(String8& dump) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2815 | dump.append(INDENT3 "Parameters:\n"); |
| 2816 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2817 | switch (mParameters.gestureMode) { |
| 2818 | case Parameters::GESTURE_MODE_POINTER: |
| 2819 | dump.append(INDENT4 "GestureMode: pointer\n"); |
| 2820 | break; |
| 2821 | case Parameters::GESTURE_MODE_SPOTS: |
| 2822 | dump.append(INDENT4 "GestureMode: spots\n"); |
| 2823 | break; |
| 2824 | default: |
| 2825 | assert(false); |
| 2826 | } |
| 2827 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2828 | switch (mParameters.deviceType) { |
| 2829 | case Parameters::DEVICE_TYPE_TOUCH_SCREEN: |
| 2830 | dump.append(INDENT4 "DeviceType: touchScreen\n"); |
| 2831 | break; |
| 2832 | case Parameters::DEVICE_TYPE_TOUCH_PAD: |
| 2833 | dump.append(INDENT4 "DeviceType: touchPad\n"); |
| 2834 | break; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2835 | case Parameters::DEVICE_TYPE_POINTER: |
| 2836 | dump.append(INDENT4 "DeviceType: pointer\n"); |
| 2837 | break; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2838 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 2839 | ALOG_ASSERT(false); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2840 | } |
| 2841 | |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2842 | dump.appendFormat(INDENT4 "AssociatedDisplay: hasAssociatedDisplay=%s, isExternal=%s\n", |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2843 | toString(mParameters.hasAssociatedDisplay), |
| 2844 | toString(mParameters.associatedDisplayIsExternal)); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2845 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 2846 | toString(mParameters.orientationAware)); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2847 | } |
| 2848 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2849 | void TouchInputMapper::configureRawPointerAxes() { |
| 2850 | mRawPointerAxes.clear(); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2851 | } |
| 2852 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2853 | void TouchInputMapper::dumpRawPointerAxes(String8& dump) { |
| 2854 | dump.append(INDENT3 "Raw Touch Axes:\n"); |
| 2855 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X"); |
| 2856 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y"); |
| 2857 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure"); |
| 2858 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor"); |
| 2859 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor"); |
| 2860 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor"); |
| 2861 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor"); |
| 2862 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation"); |
| 2863 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance"); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2864 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX"); |
| 2865 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY"); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2866 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId"); |
| 2867 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot"); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2868 | } |
| 2869 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2870 | void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) { |
| 2871 | int32_t oldDeviceMode = mDeviceMode; |
| 2872 | |
| 2873 | // Determine device mode. |
| 2874 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER |
| 2875 | && mConfig.pointerGesturesEnabled) { |
| 2876 | mSource = AINPUT_SOURCE_MOUSE; |
| 2877 | mDeviceMode = DEVICE_MODE_POINTER; |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 2878 | if (hasStylus()) { |
| 2879 | mSource |= AINPUT_SOURCE_STYLUS; |
| 2880 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2881 | } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2882 | && mParameters.hasAssociatedDisplay) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2883 | mSource = AINPUT_SOURCE_TOUCHSCREEN; |
| 2884 | mDeviceMode = DEVICE_MODE_DIRECT; |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 2885 | if (hasStylus()) { |
| 2886 | mSource |= AINPUT_SOURCE_STYLUS; |
| 2887 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2888 | } else { |
| 2889 | mSource = AINPUT_SOURCE_TOUCHPAD; |
| 2890 | mDeviceMode = DEVICE_MODE_UNSCALED; |
| 2891 | } |
| 2892 | |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2893 | // Ensure we have valid X and Y axes. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2894 | if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2895 | 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] | 2896 | "The device will be inoperable.", getDeviceName().string()); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2897 | mDeviceMode = DEVICE_MODE_DISABLED; |
| 2898 | return; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2899 | } |
| 2900 | |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2901 | // Raw width and height in the natural orientation. |
| 2902 | int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1; |
| 2903 | int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1; |
| 2904 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2905 | // Get associated display dimensions. |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2906 | bool viewportChanged = false; |
| 2907 | DisplayViewport newViewport; |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2908 | if (mParameters.hasAssociatedDisplay) { |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2909 | if (!mConfig.getDisplayInfo(mParameters.associatedDisplayIsExternal, &newViewport)) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2910 | ALOGI(INDENT "Touch device '%s' could not query the properties of its associated " |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2911 | "display. The device will be inoperable until the display size " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2912 | "becomes available.", |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2913 | getDeviceName().string()); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2914 | mDeviceMode = DEVICE_MODE_DISABLED; |
| 2915 | return; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2916 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2917 | } else { |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2918 | newViewport.setNonDisplayViewport(rawWidth, rawHeight); |
| 2919 | } |
| 2920 | if (mViewport != newViewport) { |
| 2921 | mViewport = newViewport; |
| 2922 | viewportChanged = true; |
| 2923 | |
| 2924 | if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) { |
| 2925 | // Convert rotated viewport to natural surface coordinates. |
| 2926 | int32_t naturalLogicalWidth, naturalLogicalHeight; |
| 2927 | int32_t naturalPhysicalWidth, naturalPhysicalHeight; |
| 2928 | int32_t naturalPhysicalLeft, naturalPhysicalTop; |
| 2929 | int32_t naturalDeviceWidth, naturalDeviceHeight; |
| 2930 | switch (mViewport.orientation) { |
| 2931 | case DISPLAY_ORIENTATION_90: |
| 2932 | naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop; |
| 2933 | naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft; |
| 2934 | naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop; |
| 2935 | naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft; |
| 2936 | naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom; |
| 2937 | naturalPhysicalTop = mViewport.physicalLeft; |
| 2938 | naturalDeviceWidth = mViewport.deviceHeight; |
| 2939 | naturalDeviceHeight = mViewport.deviceWidth; |
| 2940 | break; |
| 2941 | case DISPLAY_ORIENTATION_180: |
| 2942 | naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft; |
| 2943 | naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop; |
| 2944 | naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft; |
| 2945 | naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop; |
| 2946 | naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight; |
| 2947 | naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom; |
| 2948 | naturalDeviceWidth = mViewport.deviceWidth; |
| 2949 | naturalDeviceHeight = mViewport.deviceHeight; |
| 2950 | break; |
| 2951 | case DISPLAY_ORIENTATION_270: |
| 2952 | naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop; |
| 2953 | naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft; |
| 2954 | naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop; |
| 2955 | naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft; |
| 2956 | naturalPhysicalLeft = mViewport.physicalTop; |
| 2957 | naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight; |
| 2958 | naturalDeviceWidth = mViewport.deviceHeight; |
| 2959 | naturalDeviceHeight = mViewport.deviceWidth; |
| 2960 | break; |
| 2961 | case DISPLAY_ORIENTATION_0: |
| 2962 | default: |
| 2963 | naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft; |
| 2964 | naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop; |
| 2965 | naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft; |
| 2966 | naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop; |
| 2967 | naturalPhysicalLeft = mViewport.physicalLeft; |
| 2968 | naturalPhysicalTop = mViewport.physicalTop; |
| 2969 | naturalDeviceWidth = mViewport.deviceWidth; |
| 2970 | naturalDeviceHeight = mViewport.deviceHeight; |
| 2971 | break; |
| 2972 | } |
| 2973 | |
| 2974 | mSurfaceWidth = naturalLogicalWidth * naturalDeviceWidth / naturalPhysicalWidth; |
| 2975 | mSurfaceHeight = naturalLogicalHeight * naturalDeviceHeight / naturalPhysicalHeight; |
| 2976 | mSurfaceLeft = naturalPhysicalLeft * naturalLogicalWidth / naturalPhysicalWidth; |
| 2977 | mSurfaceTop = naturalPhysicalTop * naturalLogicalHeight / naturalPhysicalHeight; |
| 2978 | |
| 2979 | mSurfaceOrientation = mParameters.orientationAware ? |
| 2980 | mViewport.orientation : DISPLAY_ORIENTATION_0; |
| 2981 | } else { |
| 2982 | mSurfaceWidth = rawWidth; |
| 2983 | mSurfaceHeight = rawHeight; |
| 2984 | mSurfaceLeft = 0; |
| 2985 | mSurfaceTop = 0; |
| 2986 | mSurfaceOrientation = DISPLAY_ORIENTATION_0; |
| 2987 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2988 | } |
| 2989 | |
| 2990 | // If moving between pointer modes, need to reset some state. |
| 2991 | bool deviceModeChanged; |
| 2992 | if (mDeviceMode != oldDeviceMode) { |
| 2993 | deviceModeChanged = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2994 | mOrientedRanges.clear(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2995 | } |
| 2996 | |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 2997 | // Create pointer controller if needed. |
| 2998 | if (mDeviceMode == DEVICE_MODE_POINTER || |
| 2999 | (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) { |
| 3000 | if (mPointerController == NULL) { |
| 3001 | mPointerController = getPolicy()->obtainPointerController(getDeviceId()); |
| 3002 | } |
| 3003 | } else { |
| 3004 | mPointerController.clear(); |
| 3005 | } |
| 3006 | |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 3007 | if (viewportChanged || deviceModeChanged) { |
| 3008 | ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, " |
| 3009 | "display id %d", |
| 3010 | getDeviceId(), getDeviceName().string(), mSurfaceWidth, mSurfaceHeight, |
| 3011 | mSurfaceOrientation, mDeviceMode, mViewport.displayId); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3012 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3013 | // Configure X and Y factors. |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 3014 | mXScale = float(mSurfaceWidth) / rawWidth; |
| 3015 | mYScale = float(mSurfaceHeight) / rawHeight; |
| 3016 | mXTranslate = -mSurfaceLeft; |
| 3017 | mYTranslate = -mSurfaceTop; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3018 | mXPrecision = 1.0f / mXScale; |
| 3019 | mYPrecision = 1.0f / mYScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3020 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3021 | mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3022 | mOrientedRanges.x.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3023 | mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3024 | mOrientedRanges.y.source = mSource; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 3025 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3026 | configureVirtualKeys(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3027 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3028 | // Scale factor for terms that are not oriented in a particular axis. |
| 3029 | // If the pixels are square then xScale == yScale otherwise we fake it |
| 3030 | // by choosing an average. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3031 | mGeometricScale = avg(mXScale, mYScale); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3032 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3033 | // Size of diagonal axis. |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 3034 | float diagonalSize = hypotf(mSurfaceWidth, mSurfaceHeight); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3035 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3036 | // Size factors. |
| 3037 | if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) { |
| 3038 | if (mRawPointerAxes.touchMajor.valid |
| 3039 | && mRawPointerAxes.touchMajor.maxValue != 0) { |
| 3040 | mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue; |
| 3041 | } else if (mRawPointerAxes.toolMajor.valid |
| 3042 | && mRawPointerAxes.toolMajor.maxValue != 0) { |
| 3043 | mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue; |
| 3044 | } else { |
| 3045 | mSizeScale = 0.0f; |
| 3046 | } |
| 3047 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3048 | mOrientedRanges.haveTouchSize = true; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3049 | mOrientedRanges.haveToolSize = true; |
| 3050 | mOrientedRanges.haveSize = true; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 3051 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3052 | mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3053 | mOrientedRanges.touchMajor.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3054 | mOrientedRanges.touchMajor.min = 0; |
| 3055 | mOrientedRanges.touchMajor.max = diagonalSize; |
| 3056 | mOrientedRanges.touchMajor.flat = 0; |
| 3057 | mOrientedRanges.touchMajor.fuzz = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 3058 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3059 | mOrientedRanges.touchMinor = mOrientedRanges.touchMajor; |
| 3060 | mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 3061 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3062 | mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3063 | mOrientedRanges.toolMajor.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3064 | mOrientedRanges.toolMajor.min = 0; |
| 3065 | mOrientedRanges.toolMajor.max = diagonalSize; |
| 3066 | mOrientedRanges.toolMajor.flat = 0; |
| 3067 | mOrientedRanges.toolMajor.fuzz = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 3068 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3069 | mOrientedRanges.toolMinor = mOrientedRanges.toolMajor; |
| 3070 | mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3071 | |
| 3072 | mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3073 | mOrientedRanges.size.source = mSource; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3074 | mOrientedRanges.size.min = 0; |
| 3075 | mOrientedRanges.size.max = 1.0; |
| 3076 | mOrientedRanges.size.flat = 0; |
| 3077 | mOrientedRanges.size.fuzz = 0; |
| 3078 | } else { |
| 3079 | mSizeScale = 0.0f; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3080 | } |
| 3081 | |
| 3082 | // Pressure factors. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3083 | mPressureScale = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3084 | if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL |
| 3085 | || mCalibration.pressureCalibration |
| 3086 | == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) { |
| 3087 | if (mCalibration.havePressureScale) { |
| 3088 | mPressureScale = mCalibration.pressureScale; |
| 3089 | } else if (mRawPointerAxes.pressure.valid |
| 3090 | && mRawPointerAxes.pressure.maxValue != 0) { |
| 3091 | mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3092 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3093 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3094 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3095 | mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE; |
| 3096 | mOrientedRanges.pressure.source = mSource; |
| 3097 | mOrientedRanges.pressure.min = 0; |
| 3098 | mOrientedRanges.pressure.max = 1.0; |
| 3099 | mOrientedRanges.pressure.flat = 0; |
| 3100 | mOrientedRanges.pressure.fuzz = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 3101 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3102 | // Tilt |
| 3103 | mTiltXCenter = 0; |
| 3104 | mTiltXScale = 0; |
| 3105 | mTiltYCenter = 0; |
| 3106 | mTiltYScale = 0; |
| 3107 | mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid; |
| 3108 | if (mHaveTilt) { |
| 3109 | mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue, |
| 3110 | mRawPointerAxes.tiltX.maxValue); |
| 3111 | mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue, |
| 3112 | mRawPointerAxes.tiltY.maxValue); |
| 3113 | mTiltXScale = M_PI / 180; |
| 3114 | mTiltYScale = M_PI / 180; |
| 3115 | |
| 3116 | mOrientedRanges.haveTilt = true; |
| 3117 | |
| 3118 | mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT; |
| 3119 | mOrientedRanges.tilt.source = mSource; |
| 3120 | mOrientedRanges.tilt.min = 0; |
| 3121 | mOrientedRanges.tilt.max = M_PI_2; |
| 3122 | mOrientedRanges.tilt.flat = 0; |
| 3123 | mOrientedRanges.tilt.fuzz = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3124 | } |
| 3125 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3126 | // Orientation |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3127 | mOrientationScale = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3128 | if (mHaveTilt) { |
| 3129 | mOrientedRanges.haveOrientation = true; |
| 3130 | |
| 3131 | mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION; |
| 3132 | mOrientedRanges.orientation.source = mSource; |
| 3133 | mOrientedRanges.orientation.min = -M_PI; |
| 3134 | mOrientedRanges.orientation.max = M_PI; |
| 3135 | mOrientedRanges.orientation.flat = 0; |
| 3136 | mOrientedRanges.orientation.fuzz = 0; |
| 3137 | } else if (mCalibration.orientationCalibration != |
| 3138 | Calibration::ORIENTATION_CALIBRATION_NONE) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3139 | if (mCalibration.orientationCalibration |
| 3140 | == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3141 | if (mRawPointerAxes.orientation.valid) { |
Jeff Brown | 037f727 | 2012-06-25 17:31:23 -0700 | [diff] [blame] | 3142 | if (mRawPointerAxes.orientation.maxValue > 0) { |
| 3143 | mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue; |
| 3144 | } else if (mRawPointerAxes.orientation.minValue < 0) { |
| 3145 | mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue; |
| 3146 | } else { |
| 3147 | mOrientationScale = 0; |
| 3148 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3149 | } |
| 3150 | } |
| 3151 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3152 | mOrientedRanges.haveOrientation = true; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3153 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3154 | mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3155 | mOrientedRanges.orientation.source = mSource; |
| 3156 | mOrientedRanges.orientation.min = -M_PI_2; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3157 | mOrientedRanges.orientation.max = M_PI_2; |
| 3158 | mOrientedRanges.orientation.flat = 0; |
| 3159 | mOrientedRanges.orientation.fuzz = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3160 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3161 | |
| 3162 | // Distance |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3163 | mDistanceScale = 0; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3164 | if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) { |
| 3165 | if (mCalibration.distanceCalibration |
| 3166 | == Calibration::DISTANCE_CALIBRATION_SCALED) { |
| 3167 | if (mCalibration.haveDistanceScale) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3168 | mDistanceScale = mCalibration.distanceScale; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3169 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3170 | mDistanceScale = 1.0f; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3171 | } |
| 3172 | } |
| 3173 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3174 | mOrientedRanges.haveDistance = true; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3175 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3176 | mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3177 | mOrientedRanges.distance.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3178 | mOrientedRanges.distance.min = |
| 3179 | mRawPointerAxes.distance.minValue * mDistanceScale; |
| 3180 | mOrientedRanges.distance.max = |
Andreas Sandblad | 8239940 | 2012-03-21 14:39:57 +0100 | [diff] [blame] | 3181 | mRawPointerAxes.distance.maxValue * mDistanceScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3182 | mOrientedRanges.distance.flat = 0; |
| 3183 | mOrientedRanges.distance.fuzz = |
| 3184 | mRawPointerAxes.distance.fuzz * mDistanceScale; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3185 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3186 | |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 3187 | // Compute oriented precision, scales and ranges. |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 3188 | // Note that the maximum value reported is an inclusive maximum value so it is one |
| 3189 | // unit less than the total width or height of surface. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3190 | switch (mSurfaceOrientation) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 3191 | case DISPLAY_ORIENTATION_90: |
| 3192 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3193 | mOrientedXPrecision = mYPrecision; |
| 3194 | mOrientedYPrecision = mXPrecision; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 3195 | |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 3196 | mOrientedRanges.x.min = mYTranslate; |
| 3197 | mOrientedRanges.x.max = mSurfaceHeight + mYTranslate - 1; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3198 | mOrientedRanges.x.flat = 0; |
| 3199 | mOrientedRanges.x.fuzz = mYScale; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 3200 | |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 3201 | mOrientedRanges.y.min = mXTranslate; |
| 3202 | mOrientedRanges.y.max = mSurfaceWidth + mXTranslate - 1; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3203 | mOrientedRanges.y.flat = 0; |
| 3204 | mOrientedRanges.y.fuzz = mXScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3205 | break; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 3206 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3207 | default: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3208 | mOrientedXPrecision = mXPrecision; |
| 3209 | mOrientedYPrecision = mYPrecision; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 3210 | |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 3211 | mOrientedRanges.x.min = mXTranslate; |
| 3212 | mOrientedRanges.x.max = mSurfaceWidth + mXTranslate - 1; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3213 | mOrientedRanges.x.flat = 0; |
| 3214 | mOrientedRanges.x.fuzz = mXScale; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 3215 | |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 3216 | mOrientedRanges.y.min = mYTranslate; |
| 3217 | mOrientedRanges.y.max = mSurfaceHeight + mYTranslate - 1; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3218 | mOrientedRanges.y.flat = 0; |
| 3219 | mOrientedRanges.y.fuzz = mYScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3220 | break; |
| 3221 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3222 | |
| 3223 | // Compute pointer gesture detection parameters. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3224 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3225 | float rawDiagonal = hypotf(rawWidth, rawHeight); |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 3226 | float displayDiagonal = hypotf(mSurfaceWidth, mSurfaceHeight); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3227 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3228 | // 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] | 3229 | // given area relative to the diagonal size of the display when no acceleration |
| 3230 | // is applied. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3231 | // Assume that the touch pad has a square aspect ratio such that movements in |
| 3232 | // 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] | 3233 | mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3234 | * displayDiagonal / rawDiagonal; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3235 | mPointerYMovementScale = mPointerXMovementScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3236 | |
| 3237 | // Scale zooms to cover a smaller range of the display than movements do. |
| 3238 | // This value determines the area around the pointer that is affected by freeform |
| 3239 | // pointer gestures. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3240 | mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3241 | * displayDiagonal / rawDiagonal; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3242 | mPointerYZoomScale = mPointerXZoomScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3243 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3244 | // Max width between pointers to detect a swipe gesture is more than some fraction |
| 3245 | // of the diagonal axis of the touch pad. Touches that are wider than this are |
| 3246 | // translated into freeform gestures. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3247 | mPointerGestureMaxSwipeWidth = |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 3248 | mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3249 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3250 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3251 | // Abort current pointer usages because the state has changed. |
| 3252 | abortPointerUsage(when, 0 /*policyFlags*/); |
| 3253 | |
| 3254 | // Inform the dispatcher about the changes. |
| 3255 | *outResetNeeded = true; |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 3256 | bumpGeneration(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3257 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3258 | } |
| 3259 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3260 | void TouchInputMapper::dumpSurface(String8& dump) { |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 3261 | dump.appendFormat(INDENT3 "Viewport: displayId=%d, orientation=%d, " |
| 3262 | "logicalFrame=[%d, %d, %d, %d], " |
| 3263 | "physicalFrame=[%d, %d, %d, %d], " |
| 3264 | "deviceSize=[%d, %d]\n", |
| 3265 | mViewport.displayId, mViewport.orientation, |
| 3266 | mViewport.logicalLeft, mViewport.logicalTop, |
| 3267 | mViewport.logicalRight, mViewport.logicalBottom, |
| 3268 | mViewport.physicalLeft, mViewport.physicalTop, |
| 3269 | mViewport.physicalRight, mViewport.physicalBottom, |
| 3270 | mViewport.deviceWidth, mViewport.deviceHeight); |
| 3271 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3272 | dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth); |
| 3273 | dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight); |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 3274 | dump.appendFormat(INDENT3 "SurfaceLeft: %d\n", mSurfaceLeft); |
| 3275 | dump.appendFormat(INDENT3 "SurfaceTop: %d\n", mSurfaceTop); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3276 | dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3277 | } |
| 3278 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3279 | void TouchInputMapper::configureVirtualKeys() { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3280 | Vector<VirtualKeyDefinition> virtualKeyDefinitions; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 3281 | getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3282 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3283 | mVirtualKeys.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3284 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3285 | if (virtualKeyDefinitions.size() == 0) { |
| 3286 | return; |
| 3287 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3288 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3289 | mVirtualKeys.setCapacity(virtualKeyDefinitions.size()); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3290 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3291 | int32_t touchScreenLeft = mRawPointerAxes.x.minValue; |
| 3292 | int32_t touchScreenTop = mRawPointerAxes.y.minValue; |
| 3293 | int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1; |
| 3294 | int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3295 | |
| 3296 | for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3297 | const VirtualKeyDefinition& virtualKeyDefinition = |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3298 | virtualKeyDefinitions[i]; |
| 3299 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3300 | mVirtualKeys.add(); |
| 3301 | VirtualKey& virtualKey = mVirtualKeys.editTop(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3302 | |
| 3303 | virtualKey.scanCode = virtualKeyDefinition.scanCode; |
| 3304 | int32_t keyCode; |
| 3305 | uint32_t flags; |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 3306 | if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, 0, &keyCode, &flags)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3307 | ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3308 | virtualKey.scanCode); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3309 | mVirtualKeys.pop(); // drop the key |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3310 | continue; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3311 | } |
| 3312 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3313 | virtualKey.keyCode = keyCode; |
| 3314 | virtualKey.flags = flags; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3315 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3316 | // convert the key definition's display coordinates into touch coordinates for a hit box |
| 3317 | int32_t halfWidth = virtualKeyDefinition.width / 2; |
| 3318 | int32_t halfHeight = virtualKeyDefinition.height / 2; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3319 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3320 | virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3321 | * touchScreenWidth / mSurfaceWidth + touchScreenLeft; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3322 | virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3323 | * touchScreenWidth / mSurfaceWidth + touchScreenLeft; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3324 | virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3325 | * touchScreenHeight / mSurfaceHeight + touchScreenTop; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3326 | virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3327 | * touchScreenHeight / mSurfaceHeight + touchScreenTop; |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3328 | } |
| 3329 | } |
| 3330 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3331 | void TouchInputMapper::dumpVirtualKeys(String8& dump) { |
| 3332 | if (!mVirtualKeys.isEmpty()) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3333 | dump.append(INDENT3 "Virtual Keys:\n"); |
| 3334 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3335 | for (size_t i = 0; i < mVirtualKeys.size(); i++) { |
| 3336 | const VirtualKey& virtualKey = mVirtualKeys.itemAt(i); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3337 | dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, " |
| 3338 | "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n", |
| 3339 | i, virtualKey.scanCode, virtualKey.keyCode, |
| 3340 | virtualKey.hitLeft, virtualKey.hitRight, |
| 3341 | virtualKey.hitTop, virtualKey.hitBottom); |
| 3342 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3343 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3344 | } |
| 3345 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3346 | void TouchInputMapper::parseCalibration() { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 3347 | const PropertyMap& in = getDevice()->getConfiguration(); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3348 | Calibration& out = mCalibration; |
| 3349 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3350 | // Size |
| 3351 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT; |
| 3352 | String8 sizeCalibrationString; |
| 3353 | if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) { |
| 3354 | if (sizeCalibrationString == "none") { |
| 3355 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; |
| 3356 | } else if (sizeCalibrationString == "geometric") { |
| 3357 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC; |
| 3358 | } else if (sizeCalibrationString == "diameter") { |
| 3359 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER; |
Jeff Brown | 037f727 | 2012-06-25 17:31:23 -0700 | [diff] [blame] | 3360 | } else if (sizeCalibrationString == "box") { |
| 3361 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_BOX; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3362 | } else if (sizeCalibrationString == "area") { |
| 3363 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA; |
| 3364 | } else if (sizeCalibrationString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3365 | ALOGW("Invalid value for touch.size.calibration: '%s'", |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3366 | sizeCalibrationString.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3367 | } |
| 3368 | } |
| 3369 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3370 | out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"), |
| 3371 | out.sizeScale); |
| 3372 | out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"), |
| 3373 | out.sizeBias); |
| 3374 | out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"), |
| 3375 | out.sizeIsSummed); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3376 | |
| 3377 | // Pressure |
| 3378 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT; |
| 3379 | String8 pressureCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 3380 | if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3381 | if (pressureCalibrationString == "none") { |
| 3382 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; |
| 3383 | } else if (pressureCalibrationString == "physical") { |
| 3384 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL; |
| 3385 | } else if (pressureCalibrationString == "amplitude") { |
| 3386 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE; |
| 3387 | } else if (pressureCalibrationString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3388 | ALOGW("Invalid value for touch.pressure.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3389 | pressureCalibrationString.string()); |
| 3390 | } |
| 3391 | } |
| 3392 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3393 | out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"), |
| 3394 | out.pressureScale); |
| 3395 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3396 | // Orientation |
| 3397 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT; |
| 3398 | String8 orientationCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 3399 | if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3400 | if (orientationCalibrationString == "none") { |
| 3401 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; |
| 3402 | } else if (orientationCalibrationString == "interpolated") { |
| 3403 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 3404 | } else if (orientationCalibrationString == "vector") { |
| 3405 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3406 | } else if (orientationCalibrationString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3407 | ALOGW("Invalid value for touch.orientation.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3408 | orientationCalibrationString.string()); |
| 3409 | } |
| 3410 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3411 | |
| 3412 | // Distance |
| 3413 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT; |
| 3414 | String8 distanceCalibrationString; |
| 3415 | if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) { |
| 3416 | if (distanceCalibrationString == "none") { |
| 3417 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE; |
| 3418 | } else if (distanceCalibrationString == "scaled") { |
| 3419 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED; |
| 3420 | } else if (distanceCalibrationString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3421 | ALOGW("Invalid value for touch.distance.calibration: '%s'", |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3422 | distanceCalibrationString.string()); |
| 3423 | } |
| 3424 | } |
| 3425 | |
| 3426 | out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"), |
| 3427 | out.distanceScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3428 | } |
| 3429 | |
| 3430 | void TouchInputMapper::resolveCalibration() { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3431 | // Size |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3432 | if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) { |
| 3433 | if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) { |
| 3434 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3435 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3436 | } else { |
| 3437 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; |
| 3438 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3439 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3440 | // Pressure |
| 3441 | if (mRawPointerAxes.pressure.valid) { |
| 3442 | if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) { |
| 3443 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL; |
| 3444 | } |
| 3445 | } else { |
| 3446 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3447 | } |
| 3448 | |
| 3449 | // Orientation |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3450 | if (mRawPointerAxes.orientation.valid) { |
| 3451 | if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3452 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3453 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3454 | } else { |
| 3455 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3456 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3457 | |
| 3458 | // Distance |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3459 | if (mRawPointerAxes.distance.valid) { |
| 3460 | if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3461 | mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3462 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3463 | } else { |
| 3464 | mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3465 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3466 | } |
| 3467 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3468 | void TouchInputMapper::dumpCalibration(String8& dump) { |
| 3469 | dump.append(INDENT3 "Calibration:\n"); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3470 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3471 | // Size |
| 3472 | switch (mCalibration.sizeCalibration) { |
| 3473 | case Calibration::SIZE_CALIBRATION_NONE: |
| 3474 | dump.append(INDENT4 "touch.size.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3475 | break; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3476 | case Calibration::SIZE_CALIBRATION_GEOMETRIC: |
| 3477 | dump.append(INDENT4 "touch.size.calibration: geometric\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3478 | break; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3479 | case Calibration::SIZE_CALIBRATION_DIAMETER: |
| 3480 | dump.append(INDENT4 "touch.size.calibration: diameter\n"); |
| 3481 | break; |
Jeff Brown | 037f727 | 2012-06-25 17:31:23 -0700 | [diff] [blame] | 3482 | case Calibration::SIZE_CALIBRATION_BOX: |
| 3483 | dump.append(INDENT4 "touch.size.calibration: box\n"); |
| 3484 | break; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3485 | case Calibration::SIZE_CALIBRATION_AREA: |
| 3486 | dump.append(INDENT4 "touch.size.calibration: area\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3487 | break; |
| 3488 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3489 | ALOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3490 | } |
| 3491 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3492 | if (mCalibration.haveSizeScale) { |
| 3493 | dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n", |
| 3494 | mCalibration.sizeScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3495 | } |
| 3496 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3497 | if (mCalibration.haveSizeBias) { |
| 3498 | dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n", |
| 3499 | mCalibration.sizeBias); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3500 | } |
| 3501 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3502 | if (mCalibration.haveSizeIsSummed) { |
| 3503 | dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n", |
| 3504 | toString(mCalibration.sizeIsSummed)); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3505 | } |
| 3506 | |
| 3507 | // Pressure |
| 3508 | switch (mCalibration.pressureCalibration) { |
| 3509 | case Calibration::PRESSURE_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3510 | dump.append(INDENT4 "touch.pressure.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3511 | break; |
| 3512 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3513 | dump.append(INDENT4 "touch.pressure.calibration: physical\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3514 | break; |
| 3515 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3516 | dump.append(INDENT4 "touch.pressure.calibration: amplitude\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3517 | break; |
| 3518 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3519 | ALOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3520 | } |
| 3521 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3522 | if (mCalibration.havePressureScale) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3523 | dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n", |
| 3524 | mCalibration.pressureScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3525 | } |
| 3526 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3527 | // Orientation |
| 3528 | switch (mCalibration.orientationCalibration) { |
| 3529 | case Calibration::ORIENTATION_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3530 | dump.append(INDENT4 "touch.orientation.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3531 | break; |
| 3532 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3533 | dump.append(INDENT4 "touch.orientation.calibration: interpolated\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3534 | break; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 3535 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: |
| 3536 | dump.append(INDENT4 "touch.orientation.calibration: vector\n"); |
| 3537 | break; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3538 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3539 | ALOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3540 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3541 | |
| 3542 | // Distance |
| 3543 | switch (mCalibration.distanceCalibration) { |
| 3544 | case Calibration::DISTANCE_CALIBRATION_NONE: |
| 3545 | dump.append(INDENT4 "touch.distance.calibration: none\n"); |
| 3546 | break; |
| 3547 | case Calibration::DISTANCE_CALIBRATION_SCALED: |
| 3548 | dump.append(INDENT4 "touch.distance.calibration: scaled\n"); |
| 3549 | break; |
| 3550 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3551 | ALOG_ASSERT(false); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | if (mCalibration.haveDistanceScale) { |
| 3555 | dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n", |
| 3556 | mCalibration.distanceScale); |
| 3557 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3558 | } |
| 3559 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3560 | void TouchInputMapper::reset(nsecs_t when) { |
| 3561 | mCursorButtonAccumulator.reset(getDevice()); |
| 3562 | mCursorScrollAccumulator.reset(getDevice()); |
| 3563 | mTouchButtonAccumulator.reset(getDevice()); |
| 3564 | |
| 3565 | mPointerVelocityControl.reset(); |
| 3566 | mWheelXVelocityControl.reset(); |
| 3567 | mWheelYVelocityControl.reset(); |
| 3568 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3569 | mCurrentRawPointerData.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3570 | mLastRawPointerData.clear(); |
| 3571 | mCurrentCookedPointerData.clear(); |
| 3572 | mLastCookedPointerData.clear(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3573 | mCurrentButtonState = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3574 | mLastButtonState = 0; |
| 3575 | mCurrentRawVScroll = 0; |
| 3576 | mCurrentRawHScroll = 0; |
| 3577 | mCurrentFingerIdBits.clear(); |
| 3578 | mLastFingerIdBits.clear(); |
| 3579 | mCurrentStylusIdBits.clear(); |
| 3580 | mLastStylusIdBits.clear(); |
| 3581 | mCurrentMouseIdBits.clear(); |
| 3582 | mLastMouseIdBits.clear(); |
| 3583 | mPointerUsage = POINTER_USAGE_NONE; |
| 3584 | mSentHoverEnter = false; |
| 3585 | mDownTime = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3586 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3587 | mCurrentVirtualKey.down = false; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3588 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3589 | mPointerGesture.reset(); |
| 3590 | mPointerSimple.reset(); |
| 3591 | |
| 3592 | if (mPointerController != NULL) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3593 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 3594 | mPointerController->clearSpots(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3595 | } |
| 3596 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3597 | InputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3598 | } |
| 3599 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3600 | void TouchInputMapper::process(const RawEvent* rawEvent) { |
| 3601 | mCursorButtonAccumulator.process(rawEvent); |
| 3602 | mCursorScrollAccumulator.process(rawEvent); |
| 3603 | mTouchButtonAccumulator.process(rawEvent); |
| 3604 | |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 3605 | if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3606 | sync(rawEvent->when); |
| 3607 | } |
| 3608 | } |
| 3609 | |
| 3610 | void TouchInputMapper::sync(nsecs_t when) { |
| 3611 | // Sync button state. |
| 3612 | mCurrentButtonState = mTouchButtonAccumulator.getButtonState() |
| 3613 | | mCursorButtonAccumulator.getButtonState(); |
| 3614 | |
| 3615 | // Sync scroll state. |
| 3616 | mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel(); |
| 3617 | mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel(); |
| 3618 | mCursorScrollAccumulator.finishSync(); |
| 3619 | |
| 3620 | // Sync touch state. |
| 3621 | bool havePointerIds = true; |
| 3622 | mCurrentRawPointerData.clear(); |
| 3623 | syncTouch(when, &havePointerIds); |
| 3624 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 3625 | #if DEBUG_RAW_EVENTS |
| 3626 | if (!havePointerIds) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3627 | ALOGD("syncTouch: pointerCount %d -> %d, no pointer ids", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3628 | mLastRawPointerData.pointerCount, |
| 3629 | mCurrentRawPointerData.pointerCount); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 3630 | } else { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3631 | ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, " |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3632 | "hovering ids 0x%08x -> 0x%08x", |
| 3633 | mLastRawPointerData.pointerCount, |
| 3634 | mCurrentRawPointerData.pointerCount, |
| 3635 | mLastRawPointerData.touchingIdBits.value, |
| 3636 | mCurrentRawPointerData.touchingIdBits.value, |
| 3637 | mLastRawPointerData.hoveringIdBits.value, |
| 3638 | mCurrentRawPointerData.hoveringIdBits.value); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 3639 | } |
| 3640 | #endif |
| 3641 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3642 | // Reset state that we will compute below. |
| 3643 | mCurrentFingerIdBits.clear(); |
| 3644 | mCurrentStylusIdBits.clear(); |
| 3645 | mCurrentMouseIdBits.clear(); |
| 3646 | mCurrentCookedPointerData.clear(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3647 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3648 | if (mDeviceMode == DEVICE_MODE_DISABLED) { |
| 3649 | // Drop all input if the device is disabled. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3650 | mCurrentRawPointerData.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3651 | mCurrentButtonState = 0; |
| 3652 | } else { |
| 3653 | // Preprocess pointer data. |
| 3654 | if (!havePointerIds) { |
| 3655 | assignPointerIds(); |
| 3656 | } |
| 3657 | |
| 3658 | // Handle policy on initial down or hover events. |
| 3659 | uint32_t policyFlags = 0; |
Jeff Brown | c28306a | 2011-08-23 21:32:42 -0700 | [diff] [blame] | 3660 | bool initialDown = mLastRawPointerData.pointerCount == 0 |
| 3661 | && mCurrentRawPointerData.pointerCount != 0; |
| 3662 | bool buttonsPressed = mCurrentButtonState & ~mLastButtonState; |
| 3663 | if (initialDown || buttonsPressed) { |
| 3664 | // 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] | 3665 | if (mDeviceMode == DEVICE_MODE_DIRECT) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3666 | getContext()->fadePointer(); |
| 3667 | } |
| 3668 | |
| 3669 | // Initial downs on external touch devices should wake the device. |
| 3670 | // We don't do this for internal touch screens to prevent them from waking |
| 3671 | // up in your pocket. |
| 3672 | // TODO: Use the input device configuration to control this behavior more finely. |
| 3673 | if (getDevice()->isExternal()) { |
| 3674 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 3675 | } |
| 3676 | } |
| 3677 | |
| 3678 | // Synthesize key down from raw buttons if needed. |
| 3679 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource, |
| 3680 | policyFlags, mLastButtonState, mCurrentButtonState); |
| 3681 | |
| 3682 | // Consume raw off-screen touches before cooking pointer data. |
| 3683 | // If touches are consumed, subsequent code will not receive any pointer data. |
| 3684 | if (consumeRawTouches(when, policyFlags)) { |
| 3685 | mCurrentRawPointerData.clear(); |
| 3686 | } |
| 3687 | |
| 3688 | // Cook pointer data. This call populates the mCurrentCookedPointerData structure |
| 3689 | // with cooked pointer data that has the same ids and indices as the raw data. |
| 3690 | // The following code can use either the raw or cooked data, as needed. |
| 3691 | cookPointerData(); |
| 3692 | |
| 3693 | // 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] | 3694 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3695 | for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) { |
| 3696 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 3697 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3698 | if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS |
| 3699 | || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) { |
| 3700 | mCurrentStylusIdBits.markBit(id); |
| 3701 | } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER |
| 3702 | || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 3703 | mCurrentFingerIdBits.markBit(id); |
| 3704 | } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) { |
| 3705 | mCurrentMouseIdBits.markBit(id); |
| 3706 | } |
| 3707 | } |
| 3708 | for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) { |
| 3709 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 3710 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3711 | if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS |
| 3712 | || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) { |
| 3713 | mCurrentStylusIdBits.markBit(id); |
| 3714 | } |
| 3715 | } |
| 3716 | |
| 3717 | // Stylus takes precedence over all tools, then mouse, then finger. |
| 3718 | PointerUsage pointerUsage = mPointerUsage; |
| 3719 | if (!mCurrentStylusIdBits.isEmpty()) { |
| 3720 | mCurrentMouseIdBits.clear(); |
| 3721 | mCurrentFingerIdBits.clear(); |
| 3722 | pointerUsage = POINTER_USAGE_STYLUS; |
| 3723 | } else if (!mCurrentMouseIdBits.isEmpty()) { |
| 3724 | mCurrentFingerIdBits.clear(); |
| 3725 | pointerUsage = POINTER_USAGE_MOUSE; |
| 3726 | } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) { |
| 3727 | pointerUsage = POINTER_USAGE_GESTURES; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3728 | } |
| 3729 | |
| 3730 | dispatchPointerUsage(when, policyFlags, pointerUsage); |
| 3731 | } else { |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 3732 | if (mDeviceMode == DEVICE_MODE_DIRECT |
| 3733 | && mConfig.showTouches && mPointerController != NULL) { |
| 3734 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT); |
| 3735 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 3736 | |
| 3737 | mPointerController->setButtonState(mCurrentButtonState); |
| 3738 | mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords, |
| 3739 | mCurrentCookedPointerData.idToIndex, |
| 3740 | mCurrentCookedPointerData.touchingIdBits); |
| 3741 | } |
| 3742 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3743 | dispatchHoverExit(when, policyFlags); |
| 3744 | dispatchTouches(when, policyFlags); |
| 3745 | dispatchHoverEnterAndMove(when, policyFlags); |
| 3746 | } |
| 3747 | |
| 3748 | // Synthesize key up from raw buttons if needed. |
| 3749 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource, |
| 3750 | policyFlags, mLastButtonState, mCurrentButtonState); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3751 | } |
| 3752 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3753 | // Copy current touch to last touch in preparation for the next cycle. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3754 | mLastRawPointerData.copyFrom(mCurrentRawPointerData); |
| 3755 | mLastCookedPointerData.copyFrom(mCurrentCookedPointerData); |
| 3756 | mLastButtonState = mCurrentButtonState; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3757 | mLastFingerIdBits = mCurrentFingerIdBits; |
| 3758 | mLastStylusIdBits = mCurrentStylusIdBits; |
| 3759 | mLastMouseIdBits = mCurrentMouseIdBits; |
| 3760 | |
| 3761 | // Clear some transient state. |
| 3762 | mCurrentRawVScroll = 0; |
| 3763 | mCurrentRawHScroll = 0; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3764 | } |
| 3765 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3766 | void TouchInputMapper::timeoutExpired(nsecs_t when) { |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 3767 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3768 | if (mPointerUsage == POINTER_USAGE_GESTURES) { |
| 3769 | dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/); |
| 3770 | } |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3771 | } |
| 3772 | } |
| 3773 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3774 | bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) { |
| 3775 | // Check for release of a virtual key. |
| 3776 | if (mCurrentVirtualKey.down) { |
| 3777 | if (mCurrentRawPointerData.touchingIdBits.isEmpty()) { |
| 3778 | // Pointer went up while virtual key was down. |
| 3779 | mCurrentVirtualKey.down = false; |
| 3780 | if (!mCurrentVirtualKey.ignored) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3781 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3782 | ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3783 | mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3784 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3785 | dispatchVirtualKey(when, policyFlags, |
| 3786 | AKEY_EVENT_ACTION_UP, |
| 3787 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3788 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3789 | return true; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3790 | } |
| 3791 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3792 | if (mCurrentRawPointerData.touchingIdBits.count() == 1) { |
| 3793 | uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit(); |
| 3794 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3795 | const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y); |
| 3796 | if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) { |
| 3797 | // Pointer is still within the space of the virtual key. |
| 3798 | return true; |
| 3799 | } |
| 3800 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3801 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3802 | // Pointer left virtual key area or another pointer also went down. |
| 3803 | // Send key cancellation but do not consume the touch yet. |
| 3804 | // This is useful when the user swipes through from the virtual key area |
| 3805 | // into the main display surface. |
| 3806 | mCurrentVirtualKey.down = false; |
| 3807 | if (!mCurrentVirtualKey.ignored) { |
| 3808 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3809 | ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3810 | mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode); |
| 3811 | #endif |
| 3812 | dispatchVirtualKey(when, policyFlags, |
| 3813 | AKEY_EVENT_ACTION_UP, |
| 3814 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
| 3815 | | AKEY_EVENT_FLAG_CANCELED); |
| 3816 | } |
| 3817 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3818 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3819 | if (mLastRawPointerData.touchingIdBits.isEmpty() |
| 3820 | && !mCurrentRawPointerData.touchingIdBits.isEmpty()) { |
| 3821 | // Pointer just went down. Check for virtual key press or off-screen touches. |
| 3822 | uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit(); |
| 3823 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3824 | if (!isPointInsideSurface(pointer.x, pointer.y)) { |
| 3825 | // If exactly one pointer went down, check for virtual key hit. |
| 3826 | // Otherwise we will drop the entire stroke. |
| 3827 | if (mCurrentRawPointerData.touchingIdBits.count() == 1) { |
| 3828 | const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y); |
| 3829 | if (virtualKey) { |
| 3830 | mCurrentVirtualKey.down = true; |
| 3831 | mCurrentVirtualKey.downTime = when; |
| 3832 | mCurrentVirtualKey.keyCode = virtualKey->keyCode; |
| 3833 | mCurrentVirtualKey.scanCode = virtualKey->scanCode; |
| 3834 | mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey( |
| 3835 | when, getDevice(), virtualKey->keyCode, virtualKey->scanCode); |
| 3836 | |
| 3837 | if (!mCurrentVirtualKey.ignored) { |
| 3838 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3839 | ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3840 | mCurrentVirtualKey.keyCode, |
| 3841 | mCurrentVirtualKey.scanCode); |
| 3842 | #endif |
| 3843 | dispatchVirtualKey(when, policyFlags, |
| 3844 | AKEY_EVENT_ACTION_DOWN, |
| 3845 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY); |
| 3846 | } |
| 3847 | } |
| 3848 | } |
| 3849 | return true; |
| 3850 | } |
| 3851 | } |
| 3852 | |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3853 | // 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] | 3854 | // most recent touch within the screen area. The idea is to filter out stray |
| 3855 | // virtual key presses when interacting with the touch screen. |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3856 | // |
| 3857 | // Problems we're trying to solve: |
| 3858 | // |
| 3859 | // 1. While scrolling a list or dragging the window shade, the user swipes down into a |
| 3860 | // virtual key area that is implemented by a separate touch panel and accidentally |
| 3861 | // triggers a virtual key. |
| 3862 | // |
| 3863 | // 2. While typing in the on screen keyboard, the user taps slightly outside the screen |
| 3864 | // area and accidentally triggers a virtual key. This often happens when virtual keys |
| 3865 | // are layed out below the screen near to where the on screen keyboard's space bar |
| 3866 | // is displayed. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3867 | if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 3868 | mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime); |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3869 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3870 | return false; |
| 3871 | } |
| 3872 | |
| 3873 | void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags, |
| 3874 | int32_t keyEventAction, int32_t keyEventFlags) { |
| 3875 | int32_t keyCode = mCurrentVirtualKey.keyCode; |
| 3876 | int32_t scanCode = mCurrentVirtualKey.scanCode; |
| 3877 | nsecs_t downTime = mCurrentVirtualKey.downTime; |
| 3878 | int32_t metaState = mContext->getGlobalMetaState(); |
| 3879 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3880 | |
| 3881 | NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags, |
| 3882 | keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime); |
| 3883 | getListener()->notifyKey(&args); |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3884 | } |
| 3885 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3886 | void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3887 | BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits; |
| 3888 | BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3889 | int32_t metaState = getContext()->getGlobalMetaState(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3890 | int32_t buttonState = mCurrentButtonState; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3891 | |
| 3892 | if (currentIdBits == lastIdBits) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3893 | if (!currentIdBits.isEmpty()) { |
| 3894 | // No pointer id changes so this is a move event. |
| 3895 | // 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] | 3896 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3897 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, |
| 3898 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 3899 | mCurrentCookedPointerData.pointerProperties, |
| 3900 | mCurrentCookedPointerData.pointerCoords, |
| 3901 | mCurrentCookedPointerData.idToIndex, |
| 3902 | currentIdBits, -1, |
| 3903 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 3904 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3905 | } else { |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3906 | // There may be pointers going up and pointers going down and pointers moving |
| 3907 | // all at the same time. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3908 | BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value); |
| 3909 | BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3910 | BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3911 | BitSet32 dispatchedIdBits(lastIdBits.value); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3912 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3913 | // Update last coordinates of pointers that have moved so that we observe the new |
| 3914 | // 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] | 3915 | bool moveNeeded = updateMovedPointers( |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3916 | mCurrentCookedPointerData.pointerProperties, |
| 3917 | mCurrentCookedPointerData.pointerCoords, |
| 3918 | mCurrentCookedPointerData.idToIndex, |
| 3919 | mLastCookedPointerData.pointerProperties, |
| 3920 | mLastCookedPointerData.pointerCoords, |
| 3921 | mLastCookedPointerData.idToIndex, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3922 | moveIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3923 | if (buttonState != mLastButtonState) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3924 | moveNeeded = true; |
| 3925 | } |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3926 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3927 | // Dispatch pointer up events. |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3928 | while (!upIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3929 | uint32_t upId = upIdBits.clearFirstMarkedBit(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3930 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3931 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3932 | AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3933 | mLastCookedPointerData.pointerProperties, |
| 3934 | mLastCookedPointerData.pointerCoords, |
| 3935 | mLastCookedPointerData.idToIndex, |
| 3936 | dispatchedIdBits, upId, |
| 3937 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3938 | dispatchedIdBits.clearBit(upId); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3939 | } |
| 3940 | |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3941 | // Dispatch move events if any of the remaining pointers moved from their old locations. |
| 3942 | // Although applications receive new locations as part of individual pointer up |
| 3943 | // events, they do not generally handle them except when presented in a move event. |
| 3944 | if (moveNeeded) { |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3945 | ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3946 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3947 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3948 | mCurrentCookedPointerData.pointerProperties, |
| 3949 | mCurrentCookedPointerData.pointerCoords, |
| 3950 | mCurrentCookedPointerData.idToIndex, |
| 3951 | dispatchedIdBits, -1, |
| 3952 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3953 | } |
| 3954 | |
| 3955 | // Dispatch pointer down events using the new pointer locations. |
| 3956 | while (!downIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3957 | uint32_t downId = downIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3958 | dispatchedIdBits.markBit(downId); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3959 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3960 | if (dispatchedIdBits.count() == 1) { |
| 3961 | // First pointer is going down. Set down time. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3962 | mDownTime = when; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3963 | } |
| 3964 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3965 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | a611137 | 2011-07-14 21:48:23 -0700 | [diff] [blame] | 3966 | AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3967 | mCurrentCookedPointerData.pointerProperties, |
| 3968 | mCurrentCookedPointerData.pointerCoords, |
| 3969 | mCurrentCookedPointerData.idToIndex, |
| 3970 | dispatchedIdBits, downId, |
| 3971 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3972 | } |
| 3973 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3974 | } |
| 3975 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3976 | void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) { |
| 3977 | if (mSentHoverEnter && |
| 3978 | (mCurrentCookedPointerData.hoveringIdBits.isEmpty() |
| 3979 | || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) { |
| 3980 | int32_t metaState = getContext()->getGlobalMetaState(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3981 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3982 | AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0, |
| 3983 | mLastCookedPointerData.pointerProperties, |
| 3984 | mLastCookedPointerData.pointerCoords, |
| 3985 | mLastCookedPointerData.idToIndex, |
| 3986 | mLastCookedPointerData.hoveringIdBits, -1, |
| 3987 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 3988 | mSentHoverEnter = false; |
| 3989 | } |
| 3990 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3991 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3992 | void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) { |
| 3993 | if (mCurrentCookedPointerData.touchingIdBits.isEmpty() |
| 3994 | && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) { |
| 3995 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 3996 | if (!mSentHoverEnter) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3997 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3998 | AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0, |
| 3999 | mCurrentCookedPointerData.pointerProperties, |
| 4000 | mCurrentCookedPointerData.pointerCoords, |
| 4001 | mCurrentCookedPointerData.idToIndex, |
| 4002 | mCurrentCookedPointerData.hoveringIdBits, -1, |
| 4003 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 4004 | mSentHoverEnter = true; |
| 4005 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4006 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4007 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4008 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0, |
| 4009 | mCurrentCookedPointerData.pointerProperties, |
| 4010 | mCurrentCookedPointerData.pointerCoords, |
| 4011 | mCurrentCookedPointerData.idToIndex, |
| 4012 | mCurrentCookedPointerData.hoveringIdBits, -1, |
| 4013 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 4014 | } |
| 4015 | } |
| 4016 | |
| 4017 | void TouchInputMapper::cookPointerData() { |
| 4018 | uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount; |
| 4019 | |
| 4020 | mCurrentCookedPointerData.clear(); |
| 4021 | mCurrentCookedPointerData.pointerCount = currentPointerCount; |
| 4022 | mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits; |
| 4023 | mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits; |
| 4024 | |
| 4025 | // Walk through the the active pointers and map device coordinates onto |
| 4026 | // surface coordinates and adjust for display orientation. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4027 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4028 | const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4029 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4030 | // Size |
| 4031 | float touchMajor, touchMinor, toolMajor, toolMinor, size; |
| 4032 | switch (mCalibration.sizeCalibration) { |
| 4033 | case Calibration::SIZE_CALIBRATION_GEOMETRIC: |
| 4034 | case Calibration::SIZE_CALIBRATION_DIAMETER: |
Jeff Brown | 037f727 | 2012-06-25 17:31:23 -0700 | [diff] [blame] | 4035 | case Calibration::SIZE_CALIBRATION_BOX: |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4036 | case Calibration::SIZE_CALIBRATION_AREA: |
| 4037 | if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) { |
| 4038 | touchMajor = in.touchMajor; |
| 4039 | touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor; |
| 4040 | toolMajor = in.toolMajor; |
| 4041 | toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor; |
| 4042 | size = mRawPointerAxes.touchMinor.valid |
| 4043 | ? avg(in.touchMajor, in.touchMinor) : in.touchMajor; |
| 4044 | } else if (mRawPointerAxes.touchMajor.valid) { |
| 4045 | toolMajor = touchMajor = in.touchMajor; |
| 4046 | toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid |
| 4047 | ? in.touchMinor : in.touchMajor; |
| 4048 | size = mRawPointerAxes.touchMinor.valid |
| 4049 | ? avg(in.touchMajor, in.touchMinor) : in.touchMajor; |
| 4050 | } else if (mRawPointerAxes.toolMajor.valid) { |
| 4051 | touchMajor = toolMajor = in.toolMajor; |
| 4052 | touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid |
| 4053 | ? in.toolMinor : in.toolMajor; |
| 4054 | size = mRawPointerAxes.toolMinor.valid |
| 4055 | ? avg(in.toolMajor, in.toolMinor) : in.toolMajor; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4056 | } else { |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 4057 | ALOG_ASSERT(false, "No touch or tool axes. " |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4058 | "Size calibration should have been resolved to NONE."); |
| 4059 | touchMajor = 0; |
| 4060 | touchMinor = 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4061 | toolMajor = 0; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4062 | toolMinor = 0; |
| 4063 | size = 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4064 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4065 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4066 | if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) { |
| 4067 | uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count(); |
| 4068 | if (touchingCount > 1) { |
| 4069 | touchMajor /= touchingCount; |
| 4070 | touchMinor /= touchingCount; |
| 4071 | toolMajor /= touchingCount; |
| 4072 | toolMinor /= touchingCount; |
| 4073 | size /= touchingCount; |
| 4074 | } |
| 4075 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4076 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4077 | if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) { |
| 4078 | touchMajor *= mGeometricScale; |
| 4079 | touchMinor *= mGeometricScale; |
| 4080 | toolMajor *= mGeometricScale; |
| 4081 | toolMinor *= mGeometricScale; |
| 4082 | } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) { |
| 4083 | touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4084 | touchMinor = touchMajor; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4085 | toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0; |
| 4086 | toolMinor = toolMajor; |
| 4087 | } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) { |
| 4088 | touchMinor = touchMajor; |
| 4089 | toolMinor = toolMajor; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4090 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4091 | |
| 4092 | mCalibration.applySizeScaleAndBias(&touchMajor); |
| 4093 | mCalibration.applySizeScaleAndBias(&touchMinor); |
| 4094 | mCalibration.applySizeScaleAndBias(&toolMajor); |
| 4095 | mCalibration.applySizeScaleAndBias(&toolMinor); |
| 4096 | size *= mSizeScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4097 | break; |
| 4098 | default: |
| 4099 | touchMajor = 0; |
| 4100 | touchMinor = 0; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4101 | toolMajor = 0; |
| 4102 | toolMinor = 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4103 | size = 0; |
| 4104 | break; |
| 4105 | } |
| 4106 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4107 | // Pressure |
| 4108 | float pressure; |
| 4109 | switch (mCalibration.pressureCalibration) { |
| 4110 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: |
| 4111 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: |
| 4112 | pressure = in.pressure * mPressureScale; |
| 4113 | break; |
| 4114 | default: |
| 4115 | pressure = in.isHovering ? 0 : 1; |
| 4116 | break; |
| 4117 | } |
| 4118 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4119 | // Tilt and Orientation |
| 4120 | float tilt; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4121 | float orientation; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4122 | if (mHaveTilt) { |
| 4123 | float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale; |
| 4124 | float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale; |
| 4125 | orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle)); |
| 4126 | tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle)); |
| 4127 | } else { |
| 4128 | tilt = 0; |
| 4129 | |
| 4130 | switch (mCalibration.orientationCalibration) { |
| 4131 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: |
Jeff Brown | 037f727 | 2012-06-25 17:31:23 -0700 | [diff] [blame] | 4132 | orientation = in.orientation * mOrientationScale; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4133 | break; |
| 4134 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: { |
| 4135 | int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4); |
| 4136 | int32_t c2 = signExtendNybble(in.orientation & 0x0f); |
| 4137 | if (c1 != 0 || c2 != 0) { |
| 4138 | orientation = atan2f(c1, c2) * 0.5f; |
| 4139 | float confidence = hypotf(c1, c2); |
| 4140 | float scale = 1.0f + confidence / 16.0f; |
| 4141 | touchMajor *= scale; |
| 4142 | touchMinor /= scale; |
| 4143 | toolMajor *= scale; |
| 4144 | toolMinor /= scale; |
| 4145 | } else { |
| 4146 | orientation = 0; |
| 4147 | } |
| 4148 | break; |
| 4149 | } |
| 4150 | default: |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4151 | orientation = 0; |
| 4152 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4153 | } |
| 4154 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 4155 | // Distance |
| 4156 | float distance; |
| 4157 | switch (mCalibration.distanceCalibration) { |
| 4158 | case Calibration::DISTANCE_CALIBRATION_SCALED: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4159 | distance = in.distance * mDistanceScale; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 4160 | break; |
| 4161 | default: |
| 4162 | distance = 0; |
| 4163 | } |
| 4164 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4165 | // X and Y |
| 4166 | // Adjust coords for surface orientation. |
| 4167 | float x, y; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4168 | switch (mSurfaceOrientation) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4169 | case DISPLAY_ORIENTATION_90: |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 4170 | x = float(in.y - mRawPointerAxes.y.minValue) * mYScale + mYTranslate; |
| 4171 | y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale + mXTranslate; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4172 | orientation -= M_PI_2; |
Jason Gerecke | 70bca4c | 2013-03-01 11:49:07 -0800 | [diff] [blame^] | 4173 | if (orientation < mOrientedRanges.orientation.min) { |
| 4174 | orientation += (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4175 | } |
| 4176 | break; |
| 4177 | case DISPLAY_ORIENTATION_180: |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 4178 | x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale + mXTranslate; |
| 4179 | y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale + mYTranslate; |
Jason Gerecke | 70bca4c | 2013-03-01 11:49:07 -0800 | [diff] [blame^] | 4180 | orientation -= M_PI; |
| 4181 | if (orientation < mOrientedRanges.orientation.min) { |
| 4182 | orientation += (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min); |
| 4183 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4184 | break; |
| 4185 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 4186 | x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale + mYTranslate; |
| 4187 | y = float(in.x - mRawPointerAxes.x.minValue) * mXScale + mXTranslate; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4188 | orientation += M_PI_2; |
Jason Gerecke | 70bca4c | 2013-03-01 11:49:07 -0800 | [diff] [blame^] | 4189 | if (orientation > mOrientedRanges.orientation.max) { |
| 4190 | orientation -= (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4191 | } |
| 4192 | break; |
| 4193 | default: |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 4194 | x = float(in.x - mRawPointerAxes.x.minValue) * mXScale + mXTranslate; |
| 4195 | y = float(in.y - mRawPointerAxes.y.minValue) * mYScale + mYTranslate; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4196 | break; |
| 4197 | } |
| 4198 | |
| 4199 | // Write output coords. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4200 | PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4201 | out.clear(); |
| 4202 | out.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4203 | out.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 4204 | out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure); |
| 4205 | out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size); |
| 4206 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor); |
| 4207 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor); |
| 4208 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor); |
| 4209 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor); |
| 4210 | out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4211 | out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4212 | out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4213 | |
| 4214 | // Write output properties. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4215 | PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i]; |
| 4216 | uint32_t id = in.id; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4217 | properties.clear(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4218 | properties.id = id; |
| 4219 | properties.toolType = in.toolType; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4220 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4221 | // Write id index. |
| 4222 | mCurrentCookedPointerData.idToIndex[id] = i; |
| 4223 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4224 | } |
| 4225 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4226 | void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, |
| 4227 | PointerUsage pointerUsage) { |
| 4228 | if (pointerUsage != mPointerUsage) { |
| 4229 | abortPointerUsage(when, policyFlags); |
| 4230 | mPointerUsage = pointerUsage; |
| 4231 | } |
| 4232 | |
| 4233 | switch (mPointerUsage) { |
| 4234 | case POINTER_USAGE_GESTURES: |
| 4235 | dispatchPointerGestures(when, policyFlags, false /*isTimeout*/); |
| 4236 | break; |
| 4237 | case POINTER_USAGE_STYLUS: |
| 4238 | dispatchPointerStylus(when, policyFlags); |
| 4239 | break; |
| 4240 | case POINTER_USAGE_MOUSE: |
| 4241 | dispatchPointerMouse(when, policyFlags); |
| 4242 | break; |
| 4243 | default: |
| 4244 | break; |
| 4245 | } |
| 4246 | } |
| 4247 | |
| 4248 | void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) { |
| 4249 | switch (mPointerUsage) { |
| 4250 | case POINTER_USAGE_GESTURES: |
| 4251 | abortPointerGestures(when, policyFlags); |
| 4252 | break; |
| 4253 | case POINTER_USAGE_STYLUS: |
| 4254 | abortPointerStylus(when, policyFlags); |
| 4255 | break; |
| 4256 | case POINTER_USAGE_MOUSE: |
| 4257 | abortPointerMouse(when, policyFlags); |
| 4258 | break; |
| 4259 | default: |
| 4260 | break; |
| 4261 | } |
| 4262 | |
| 4263 | mPointerUsage = POINTER_USAGE_NONE; |
| 4264 | } |
| 4265 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4266 | void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, |
| 4267 | bool isTimeout) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4268 | // Update current gesture coordinates. |
| 4269 | bool cancelPreviousGesture, finishPreviousGesture; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4270 | bool sendEvents = preparePointerGestures(when, |
| 4271 | &cancelPreviousGesture, &finishPreviousGesture, isTimeout); |
| 4272 | if (!sendEvents) { |
| 4273 | return; |
| 4274 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4275 | if (finishPreviousGesture) { |
| 4276 | cancelPreviousGesture = false; |
| 4277 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4278 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4279 | // Update the pointer presentation and spots. |
| 4280 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 4281 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT); |
| 4282 | if (finishPreviousGesture || cancelPreviousGesture) { |
| 4283 | mPointerController->clearSpots(); |
| 4284 | } |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4285 | mPointerController->setSpots(mPointerGesture.currentGestureCoords, |
| 4286 | mPointerGesture.currentGestureIdToIndex, |
| 4287 | mPointerGesture.currentGestureIdBits); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4288 | } else { |
| 4289 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER); |
| 4290 | } |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4291 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4292 | // Show or hide the pointer if needed. |
| 4293 | switch (mPointerGesture.currentGestureMode) { |
| 4294 | case PointerGesture::NEUTRAL: |
| 4295 | case PointerGesture::QUIET: |
| 4296 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS |
| 4297 | && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE |
| 4298 | || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) { |
| 4299 | // Remind the user of where the pointer is after finishing a gesture with spots. |
| 4300 | mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 4301 | } |
| 4302 | break; |
| 4303 | case PointerGesture::TAP: |
| 4304 | case PointerGesture::TAP_DRAG: |
| 4305 | case PointerGesture::BUTTON_CLICK_OR_DRAG: |
| 4306 | case PointerGesture::HOVER: |
| 4307 | case PointerGesture::PRESS: |
| 4308 | // Unfade the pointer when the current gesture manipulates the |
| 4309 | // area directly under the pointer. |
| 4310 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 4311 | break; |
| 4312 | case PointerGesture::SWIPE: |
| 4313 | case PointerGesture::FREEFORM: |
| 4314 | // Fade the pointer when the current gesture manipulates a different |
| 4315 | // area and there are spots to guide the user experience. |
| 4316 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 4317 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 4318 | } else { |
| 4319 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 4320 | } |
| 4321 | break; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4322 | } |
| 4323 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4324 | // Send events! |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4325 | int32_t metaState = getContext()->getGlobalMetaState(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4326 | int32_t buttonState = mCurrentButtonState; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4327 | |
| 4328 | // Update last coordinates of pointers that have moved so that we observe the new |
| 4329 | // 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] | 4330 | bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP |
| 4331 | || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG |
| 4332 | || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4333 | || mPointerGesture.currentGestureMode == PointerGesture::PRESS |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4334 | || mPointerGesture.currentGestureMode == PointerGesture::SWIPE |
| 4335 | || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM; |
| 4336 | bool moveNeeded = false; |
| 4337 | if (down && !cancelPreviousGesture && !finishPreviousGesture |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4338 | && !mPointerGesture.lastGestureIdBits.isEmpty() |
| 4339 | && !mPointerGesture.currentGestureIdBits.isEmpty()) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4340 | BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value |
| 4341 | & mPointerGesture.lastGestureIdBits.value); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4342 | moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4343 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4344 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4345 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4346 | movedGestureIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4347 | if (buttonState != mLastButtonState) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4348 | moveNeeded = true; |
| 4349 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4350 | } |
| 4351 | |
| 4352 | // Send motion events for all pointers that went up or were canceled. |
| 4353 | BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits); |
| 4354 | if (!dispatchedGestureIdBits.isEmpty()) { |
| 4355 | if (cancelPreviousGesture) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4356 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4357 | AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState, |
| 4358 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4359 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4360 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4361 | dispatchedGestureIdBits, -1, |
| 4362 | 0, 0, mPointerGesture.downTime); |
| 4363 | |
| 4364 | dispatchedGestureIdBits.clear(); |
| 4365 | } else { |
| 4366 | BitSet32 upGestureIdBits; |
| 4367 | if (finishPreviousGesture) { |
| 4368 | upGestureIdBits = dispatchedGestureIdBits; |
| 4369 | } else { |
| 4370 | upGestureIdBits.value = dispatchedGestureIdBits.value |
| 4371 | & ~mPointerGesture.currentGestureIdBits.value; |
| 4372 | } |
| 4373 | while (!upGestureIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4374 | uint32_t id = upGestureIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4375 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4376 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4377 | AMOTION_EVENT_ACTION_POINTER_UP, 0, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4378 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4379 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4380 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4381 | dispatchedGestureIdBits, id, |
| 4382 | 0, 0, mPointerGesture.downTime); |
| 4383 | |
| 4384 | dispatchedGestureIdBits.clearBit(id); |
| 4385 | } |
| 4386 | } |
| 4387 | } |
| 4388 | |
| 4389 | // Send motion events for all pointers that moved. |
| 4390 | if (moveNeeded) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4391 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4392 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4393 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4394 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 4395 | dispatchedGestureIdBits, -1, |
| 4396 | 0, 0, mPointerGesture.downTime); |
| 4397 | } |
| 4398 | |
| 4399 | // Send motion events for all pointers that went down. |
| 4400 | if (down) { |
| 4401 | BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value |
| 4402 | & ~dispatchedGestureIdBits.value); |
| 4403 | while (!downGestureIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4404 | uint32_t id = downGestureIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4405 | dispatchedGestureIdBits.markBit(id); |
| 4406 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4407 | if (dispatchedGestureIdBits.count() == 1) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4408 | mPointerGesture.downTime = when; |
| 4409 | } |
| 4410 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4411 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | a611137 | 2011-07-14 21:48:23 -0700 | [diff] [blame] | 4412 | AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4413 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4414 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 4415 | dispatchedGestureIdBits, id, |
| 4416 | 0, 0, mPointerGesture.downTime); |
| 4417 | } |
| 4418 | } |
| 4419 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4420 | // Send motion events for hover. |
| 4421 | if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4422 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4423 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 4424 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4425 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4426 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 4427 | mPointerGesture.currentGestureIdBits, -1, |
| 4428 | 0, 0, mPointerGesture.downTime); |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4429 | } else if (dispatchedGestureIdBits.isEmpty() |
| 4430 | && !mPointerGesture.lastGestureIdBits.isEmpty()) { |
| 4431 | // Synthesize a hover move event after all pointers go up to indicate that |
| 4432 | // the pointer is hovering again even if the user is not currently touching |
| 4433 | // the touch pad. This ensures that a view will receive a fresh hover enter |
| 4434 | // event after a tap. |
| 4435 | float x, y; |
| 4436 | mPointerController->getPosition(&x, &y); |
| 4437 | |
| 4438 | PointerProperties pointerProperties; |
| 4439 | pointerProperties.clear(); |
| 4440 | pointerProperties.id = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4441 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4442 | |
| 4443 | PointerCoords pointerCoords; |
| 4444 | pointerCoords.clear(); |
| 4445 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4446 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 4447 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4448 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4449 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 4450 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 4451 | mViewport.displayId, 1, &pointerProperties, &pointerCoords, |
| 4452 | 0, 0, mPointerGesture.downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4453 | getListener()->notifyMotion(&args); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4454 | } |
| 4455 | |
| 4456 | // Update state. |
| 4457 | mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode; |
| 4458 | if (!down) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4459 | mPointerGesture.lastGestureIdBits.clear(); |
| 4460 | } else { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4461 | mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits; |
| 4462 | for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4463 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4464 | uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4465 | mPointerGesture.lastGestureProperties[index].copyFrom( |
| 4466 | mPointerGesture.currentGestureProperties[index]); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4467 | mPointerGesture.lastGestureCoords[index].copyFrom( |
| 4468 | mPointerGesture.currentGestureCoords[index]); |
| 4469 | mPointerGesture.lastGestureIdToIndex[id] = index; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4470 | } |
| 4471 | } |
| 4472 | } |
| 4473 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4474 | void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) { |
| 4475 | // Cancel previously dispatches pointers. |
| 4476 | if (!mPointerGesture.lastGestureIdBits.isEmpty()) { |
| 4477 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 4478 | int32_t buttonState = mCurrentButtonState; |
| 4479 | dispatchMotion(when, policyFlags, mSource, |
| 4480 | AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState, |
| 4481 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4482 | mPointerGesture.lastGestureProperties, |
| 4483 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4484 | mPointerGesture.lastGestureIdBits, -1, |
| 4485 | 0, 0, mPointerGesture.downTime); |
| 4486 | } |
| 4487 | |
| 4488 | // Reset the current pointer gesture. |
| 4489 | mPointerGesture.reset(); |
| 4490 | mPointerVelocityControl.reset(); |
| 4491 | |
| 4492 | // Remove any current spots. |
| 4493 | if (mPointerController != NULL) { |
| 4494 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 4495 | mPointerController->clearSpots(); |
| 4496 | } |
| 4497 | } |
| 4498 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4499 | bool TouchInputMapper::preparePointerGestures(nsecs_t when, |
| 4500 | bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4501 | *outCancelPreviousGesture = false; |
| 4502 | *outFinishPreviousGesture = false; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4503 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4504 | // Handle TAP timeout. |
| 4505 | if (isTimeout) { |
| 4506 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4507 | ALOGD("Gestures: Processing timeout"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4508 | #endif |
| 4509 | |
| 4510 | if (mPointerGesture.lastGestureMode == PointerGesture::TAP) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4511 | if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4512 | // The tap/drag timeout has not yet expired. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4513 | getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4514 | + mConfig.pointerGestureTapDragInterval); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4515 | } else { |
| 4516 | // The tap is finished. |
| 4517 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4518 | ALOGD("Gestures: TAP finished"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4519 | #endif |
| 4520 | *outFinishPreviousGesture = true; |
| 4521 | |
| 4522 | mPointerGesture.activeGestureId = -1; |
| 4523 | mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL; |
| 4524 | mPointerGesture.currentGestureIdBits.clear(); |
| 4525 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4526 | mPointerVelocityControl.reset(); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4527 | return true; |
| 4528 | } |
| 4529 | } |
| 4530 | |
| 4531 | // We did not handle this timeout. |
| 4532 | return false; |
| 4533 | } |
| 4534 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4535 | const uint32_t currentFingerCount = mCurrentFingerIdBits.count(); |
| 4536 | const uint32_t lastFingerCount = mLastFingerIdBits.count(); |
| 4537 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4538 | // Update the velocity tracker. |
| 4539 | { |
| 4540 | VelocityTracker::Position positions[MAX_POINTERS]; |
| 4541 | uint32_t count = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4542 | for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4543 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 4544 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4545 | positions[count].x = pointer.x * mPointerXMovementScale; |
| 4546 | positions[count].y = pointer.y * mPointerYMovementScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4547 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4548 | mPointerGesture.velocityTracker.addMovement(when, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4549 | mCurrentFingerIdBits, positions); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4550 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4551 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4552 | // Pick a new active touch id if needed. |
| 4553 | // Choose an arbitrary pointer that just went down, if there is one. |
| 4554 | // Otherwise choose an arbitrary remaining pointer. |
| 4555 | // 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] | 4556 | // We keep the same active touch id for as long as possible. |
| 4557 | bool activeTouchChanged = false; |
| 4558 | int32_t lastActiveTouchId = mPointerGesture.activeTouchId; |
| 4559 | int32_t activeTouchId = lastActiveTouchId; |
| 4560 | if (activeTouchId < 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4561 | if (!mCurrentFingerIdBits.isEmpty()) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4562 | activeTouchChanged = true; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4563 | activeTouchId = mPointerGesture.activeTouchId = |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4564 | mCurrentFingerIdBits.firstMarkedBit(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4565 | mPointerGesture.firstTouchTime = when; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4566 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4567 | } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4568 | activeTouchChanged = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4569 | if (!mCurrentFingerIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4570 | activeTouchId = mPointerGesture.activeTouchId = |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4571 | mCurrentFingerIdBits.firstMarkedBit(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4572 | } else { |
| 4573 | activeTouchId = mPointerGesture.activeTouchId = -1; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4574 | } |
| 4575 | } |
| 4576 | |
| 4577 | // Determine whether we are in quiet time. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4578 | bool isQuietTime = false; |
| 4579 | if (activeTouchId < 0) { |
| 4580 | mPointerGesture.resetQuietTime(); |
| 4581 | } else { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4582 | isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4583 | if (!isQuietTime) { |
| 4584 | if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS |
| 4585 | || mPointerGesture.lastGestureMode == PointerGesture::SWIPE |
| 4586 | || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4587 | && currentFingerCount < 2) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4588 | // Enter quiet time when exiting swipe or freeform state. |
| 4589 | // This is to prevent accidentally entering the hover state and flinging the |
| 4590 | // pointer when finishing a swipe and there is still one pointer left onscreen. |
| 4591 | isQuietTime = true; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4592 | } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4593 | && currentFingerCount >= 2 |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4594 | && !isPointerDown(mCurrentButtonState)) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4595 | // Enter quiet time when releasing the button and there are still two or more |
| 4596 | // fingers down. This may indicate that one finger was used to press the button |
| 4597 | // but it has not gone up yet. |
| 4598 | isQuietTime = true; |
| 4599 | } |
| 4600 | if (isQuietTime) { |
| 4601 | mPointerGesture.quietTime = when; |
| 4602 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4603 | } |
| 4604 | } |
| 4605 | |
| 4606 | // Switch states based on button and pointer state. |
| 4607 | if (isQuietTime) { |
| 4608 | // Case 1: Quiet time. (QUIET) |
| 4609 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4610 | ALOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4611 | + mConfig.pointerGestureQuietInterval - when) * 0.000001f); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4612 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4613 | if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) { |
| 4614 | *outFinishPreviousGesture = true; |
| 4615 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4616 | |
| 4617 | mPointerGesture.activeGestureId = -1; |
| 4618 | mPointerGesture.currentGestureMode = PointerGesture::QUIET; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4619 | mPointerGesture.currentGestureIdBits.clear(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4620 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4621 | mPointerVelocityControl.reset(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4622 | } else if (isPointerDown(mCurrentButtonState)) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4623 | // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG) |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4624 | // The pointer follows the active touch point. |
| 4625 | // Emit DOWN, MOVE, UP events at the pointer location. |
| 4626 | // |
| 4627 | // Only the active touch matters; other fingers are ignored. This policy helps |
| 4628 | // to handle the case where the user places a second finger on the touch pad |
| 4629 | // to apply the necessary force to depress an integrated button below the surface. |
| 4630 | // We don't want the second finger to be delivered to applications. |
| 4631 | // |
| 4632 | // For this to work well, we need to make sure to track the pointer that is really |
| 4633 | // active. If the user first puts one finger down to click then adds another |
| 4634 | // finger to drag then the active pointer should switch to the finger that is |
| 4635 | // being dragged. |
| 4636 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4637 | ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4638 | "currentFingerCount=%d", activeTouchId, currentFingerCount); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4639 | #endif |
| 4640 | // Reset state when just starting. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4641 | if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4642 | *outFinishPreviousGesture = true; |
| 4643 | mPointerGesture.activeGestureId = 0; |
| 4644 | } |
| 4645 | |
| 4646 | // Switch pointers if needed. |
| 4647 | // Find the fastest pointer and follow it. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4648 | if (activeTouchId >= 0 && currentFingerCount > 1) { |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4649 | int32_t bestId = -1; |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4650 | float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4651 | for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4652 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4653 | float vx, vy; |
| 4654 | if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) { |
| 4655 | float speed = hypotf(vx, vy); |
| 4656 | if (speed > bestSpeed) { |
| 4657 | bestId = id; |
| 4658 | bestSpeed = speed; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4659 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 4660 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4661 | } |
| 4662 | if (bestId >= 0 && bestId != activeTouchId) { |
| 4663 | mPointerGesture.activeTouchId = activeTouchId = bestId; |
| 4664 | activeTouchChanged = true; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4665 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4666 | ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, " |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4667 | "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4668 | #endif |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4669 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4670 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4671 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4672 | if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4673 | const RawPointerData::Pointer& currentPointer = |
| 4674 | mCurrentRawPointerData.pointerForId(activeTouchId); |
| 4675 | const RawPointerData::Pointer& lastPointer = |
| 4676 | mLastRawPointerData.pointerForId(activeTouchId); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4677 | float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale; |
| 4678 | float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4679 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4680 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4681 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4682 | |
| 4683 | // Move the pointer using a relative motion. |
| 4684 | // When using spots, the click will occur at the position of the anchor |
| 4685 | // spot and all other spots will move there. |
| 4686 | mPointerController->move(deltaX, deltaY); |
| 4687 | } else { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4688 | mPointerVelocityControl.reset(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4689 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4690 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4691 | float x, y; |
| 4692 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 4693 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4694 | mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4695 | mPointerGesture.currentGestureIdBits.clear(); |
| 4696 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4697 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4698 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4699 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4700 | mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4701 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4702 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4703 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 4704 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4705 | } else if (currentFingerCount == 0) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4706 | // Case 3. No fingers down and button is not pressed. (NEUTRAL) |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4707 | if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) { |
| 4708 | *outFinishPreviousGesture = true; |
| 4709 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4710 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4711 | // Watch for taps coming out of HOVER or TAP_DRAG mode. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4712 | // Checking for taps after TAP_DRAG allows us to detect double-taps. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4713 | bool tapped = false; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4714 | if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER |
| 4715 | || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4716 | && lastFingerCount == 1) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4717 | if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4718 | float x, y; |
| 4719 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4720 | if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop |
| 4721 | && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4722 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4723 | ALOGD("Gestures: TAP"); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4724 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4725 | |
| 4726 | mPointerGesture.tapUpTime = when; |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4727 | getContext()->requestTimeoutAtTime(when |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4728 | + mConfig.pointerGestureTapDragInterval); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4729 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4730 | mPointerGesture.activeGestureId = 0; |
| 4731 | mPointerGesture.currentGestureMode = PointerGesture::TAP; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4732 | mPointerGesture.currentGestureIdBits.clear(); |
| 4733 | mPointerGesture.currentGestureIdBits.markBit( |
| 4734 | mPointerGesture.activeGestureId); |
| 4735 | mPointerGesture.currentGestureIdToIndex[ |
| 4736 | mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4737 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4738 | mPointerGesture.currentGestureProperties[0].id = |
| 4739 | mPointerGesture.activeGestureId; |
| 4740 | mPointerGesture.currentGestureProperties[0].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4741 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4742 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4743 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4744 | AMOTION_EVENT_AXIS_X, mPointerGesture.tapX); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4745 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4746 | AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4747 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
| 4748 | AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4749 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4750 | tapped = true; |
| 4751 | } else { |
| 4752 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4753 | ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f", |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4754 | x - mPointerGesture.tapX, |
| 4755 | y - mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4756 | #endif |
| 4757 | } |
| 4758 | } else { |
| 4759 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4760 | ALOGD("Gestures: Not a TAP, %0.3fms since down", |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4761 | (when - mPointerGesture.tapDownTime) * 0.000001f); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4762 | #endif |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4763 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4764 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4765 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4766 | mPointerVelocityControl.reset(); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4767 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4768 | if (!tapped) { |
| 4769 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4770 | ALOGD("Gestures: NEUTRAL"); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4771 | #endif |
| 4772 | mPointerGesture.activeGestureId = -1; |
| 4773 | mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4774 | mPointerGesture.currentGestureIdBits.clear(); |
| 4775 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4776 | } else if (currentFingerCount == 1) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4777 | // 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] | 4778 | // The pointer follows the active touch point. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4779 | // When in HOVER, emit HOVER_MOVE events at the pointer location. |
| 4780 | // When in TAP_DRAG, emit MOVE events at the pointer location. |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 4781 | ALOG_ASSERT(activeTouchId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4782 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4783 | mPointerGesture.currentGestureMode = PointerGesture::HOVER; |
| 4784 | if (mPointerGesture.lastGestureMode == PointerGesture::TAP) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4785 | if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4786 | float x, y; |
| 4787 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4788 | if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop |
| 4789 | && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4790 | mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG; |
| 4791 | } else { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4792 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4793 | ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f", |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4794 | x - mPointerGesture.tapX, |
| 4795 | y - mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4796 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4797 | } |
| 4798 | } else { |
| 4799 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4800 | ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up", |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4801 | (when - mPointerGesture.tapUpTime) * 0.000001f); |
| 4802 | #endif |
| 4803 | } |
| 4804 | } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) { |
| 4805 | mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG; |
| 4806 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4807 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4808 | if (mLastFingerIdBits.hasBit(activeTouchId)) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4809 | const RawPointerData::Pointer& currentPointer = |
| 4810 | mCurrentRawPointerData.pointerForId(activeTouchId); |
| 4811 | const RawPointerData::Pointer& lastPointer = |
| 4812 | mLastRawPointerData.pointerForId(activeTouchId); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4813 | float deltaX = (currentPointer.x - lastPointer.x) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4814 | * mPointerXMovementScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4815 | float deltaY = (currentPointer.y - lastPointer.y) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4816 | * mPointerYMovementScale; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4817 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4818 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4819 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4820 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4821 | // Move the pointer using a relative motion. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4822 | // 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] | 4823 | mPointerController->move(deltaX, deltaY); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4824 | } else { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4825 | mPointerVelocityControl.reset(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4826 | } |
| 4827 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4828 | bool down; |
| 4829 | if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) { |
| 4830 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4831 | ALOGD("Gestures: TAP_DRAG"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4832 | #endif |
| 4833 | down = true; |
| 4834 | } else { |
| 4835 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4836 | ALOGD("Gestures: HOVER"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4837 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4838 | if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) { |
| 4839 | *outFinishPreviousGesture = true; |
| 4840 | } |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4841 | mPointerGesture.activeGestureId = 0; |
| 4842 | down = false; |
| 4843 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4844 | |
| 4845 | float x, y; |
| 4846 | mPointerController->getPosition(&x, &y); |
| 4847 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4848 | mPointerGesture.currentGestureIdBits.clear(); |
| 4849 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4850 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4851 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4852 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
| 4853 | mPointerGesture.currentGestureProperties[0].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4854 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4855 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4856 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4857 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4858 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, |
| 4859 | down ? 1.0f : 0.0f); |
| 4860 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4861 | if (lastFingerCount == 0 && currentFingerCount != 0) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4862 | mPointerGesture.resetTap(); |
| 4863 | mPointerGesture.tapDownTime = when; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4864 | mPointerGesture.tapX = x; |
| 4865 | mPointerGesture.tapY = y; |
| 4866 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4867 | } else { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4868 | // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM) |
| 4869 | // We need to provide feedback for each finger that goes down so we cannot wait |
| 4870 | // for the fingers to move before deciding what to do. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4871 | // |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4872 | // The ambiguous case is deciding what to do when there are two fingers down but they |
| 4873 | // have not moved enough to determine whether they are part of a drag or part of a |
| 4874 | // freeform gesture, or just a press or long-press at the pointer location. |
| 4875 | // |
| 4876 | // When there are two fingers we start with the PRESS hypothesis and we generate a |
| 4877 | // down at the pointer location. |
| 4878 | // |
| 4879 | // When the two fingers move enough or when additional fingers are added, we make |
| 4880 | // a decision to transition into SWIPE or FREEFORM mode accordingly. |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 4881 | ALOG_ASSERT(activeTouchId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4882 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4883 | bool settled = when >= mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4884 | + mConfig.pointerGestureMultitouchSettleInterval; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4885 | if (mPointerGesture.lastGestureMode != PointerGesture::PRESS |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4886 | && mPointerGesture.lastGestureMode != PointerGesture::SWIPE |
| 4887 | && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4888 | *outFinishPreviousGesture = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4889 | } else if (!settled && currentFingerCount > lastFingerCount) { |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4890 | // Additional pointers have gone down but not yet settled. |
| 4891 | // Reset the gesture. |
| 4892 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4893 | ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, " |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4894 | "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4895 | + mConfig.pointerGestureMultitouchSettleInterval - when) |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4896 | * 0.000001f); |
| 4897 | #endif |
| 4898 | *outCancelPreviousGesture = true; |
| 4899 | } else { |
| 4900 | // Continue previous gesture. |
| 4901 | mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode; |
| 4902 | } |
| 4903 | |
| 4904 | if (*outFinishPreviousGesture || *outCancelPreviousGesture) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4905 | mPointerGesture.currentGestureMode = PointerGesture::PRESS; |
| 4906 | mPointerGesture.activeGestureId = 0; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4907 | mPointerGesture.referenceIdBits.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4908 | mPointerVelocityControl.reset(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4909 | |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4910 | // 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] | 4911 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4912 | ALOGD("Gestures: Using centroid as reference for MULTITOUCH, " |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4913 | "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4914 | + mConfig.pointerGestureMultitouchSettleInterval - when) |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4915 | * 0.000001f); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4916 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4917 | mCurrentRawPointerData.getCentroidOfTouchingPointers( |
| 4918 | &mPointerGesture.referenceTouchX, |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4919 | &mPointerGesture.referenceTouchY); |
| 4920 | mPointerController->getPosition(&mPointerGesture.referenceGestureX, |
| 4921 | &mPointerGesture.referenceGestureY); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4922 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4923 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4924 | // 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] | 4925 | for (BitSet32 idBits(mCurrentFingerIdBits.value |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4926 | & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) { |
| 4927 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4928 | mPointerGesture.referenceDeltas[id].dx = 0; |
| 4929 | mPointerGesture.referenceDeltas[id].dy = 0; |
| 4930 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4931 | mPointerGesture.referenceIdBits = mCurrentFingerIdBits; |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4932 | |
| 4933 | // Add delta for all fingers and calculate a common movement delta. |
| 4934 | float commonDeltaX = 0, commonDeltaY = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4935 | BitSet32 commonIdBits(mLastFingerIdBits.value |
| 4936 | & mCurrentFingerIdBits.value); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4937 | for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) { |
| 4938 | bool first = (idBits == commonIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4939 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 4940 | const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id); |
| 4941 | const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4942 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
| 4943 | delta.dx += cpd.x - lpd.x; |
| 4944 | delta.dy += cpd.y - lpd.y; |
| 4945 | |
| 4946 | if (first) { |
| 4947 | commonDeltaX = delta.dx; |
| 4948 | commonDeltaY = delta.dy; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4949 | } else { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4950 | commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx); |
| 4951 | commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy); |
| 4952 | } |
| 4953 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4954 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4955 | // Consider transitions from PRESS to SWIPE or MULTITOUCH. |
| 4956 | if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) { |
| 4957 | float dist[MAX_POINTER_ID + 1]; |
| 4958 | int32_t distOverThreshold = 0; |
| 4959 | for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4960 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4961 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4962 | dist[id] = hypotf(delta.dx * mPointerXZoomScale, |
| 4963 | delta.dy * mPointerYZoomScale); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4964 | if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4965 | distOverThreshold += 1; |
| 4966 | } |
| 4967 | } |
| 4968 | |
| 4969 | // Only transition when at least two pointers have moved further than |
| 4970 | // the minimum distance threshold. |
| 4971 | if (distOverThreshold >= 2) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4972 | if (currentFingerCount > 2) { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4973 | // There are more than two pointers, switch to FREEFORM. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4974 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4975 | ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4976 | currentFingerCount); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4977 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4978 | *outCancelPreviousGesture = true; |
| 4979 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 4980 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4981 | // There are exactly two pointers. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4982 | BitSet32 idBits(mCurrentFingerIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4983 | uint32_t id1 = idBits.clearFirstMarkedBit(); |
| 4984 | uint32_t id2 = idBits.firstMarkedBit(); |
| 4985 | const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1); |
| 4986 | const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2); |
| 4987 | float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y); |
| 4988 | if (mutualDistance > mPointerGestureMaxSwipeWidth) { |
| 4989 | // There are two pointers but they are too far apart for a SWIPE, |
| 4990 | // switch to FREEFORM. |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4991 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4992 | ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4993 | mutualDistance, mPointerGestureMaxSwipeWidth); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4994 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4995 | *outCancelPreviousGesture = true; |
| 4996 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 4997 | } else { |
| 4998 | // There are two pointers. Wait for both pointers to start moving |
| 4999 | // before deciding whether this is a SWIPE or FREEFORM gesture. |
| 5000 | float dist1 = dist[id1]; |
| 5001 | float dist2 = dist[id2]; |
| 5002 | if (dist1 >= mConfig.pointerGestureMultitouchMinDistance |
| 5003 | && dist2 >= mConfig.pointerGestureMultitouchMinDistance) { |
| 5004 | // Calculate the dot product of the displacement vectors. |
| 5005 | // When the vectors are oriented in approximately the same direction, |
| 5006 | // the angle betweeen them is near zero and the cosine of the angle |
| 5007 | // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2). |
| 5008 | PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1]; |
| 5009 | PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5010 | float dx1 = delta1.dx * mPointerXZoomScale; |
| 5011 | float dy1 = delta1.dy * mPointerYZoomScale; |
| 5012 | float dx2 = delta2.dx * mPointerXZoomScale; |
| 5013 | float dy2 = delta2.dy * mPointerYZoomScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5014 | float dot = dx1 * dx2 + dy1 * dy2; |
| 5015 | float cosine = dot / (dist1 * dist2); // denominator always > 0 |
| 5016 | if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) { |
| 5017 | // Pointers are moving in the same direction. Switch to SWIPE. |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5018 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5019 | ALOGD("Gestures: PRESS transitioned to SWIPE, " |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5020 | "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, " |
| 5021 | "cosine %0.3f >= %0.3f", |
| 5022 | dist1, mConfig.pointerGestureMultitouchMinDistance, |
| 5023 | dist2, mConfig.pointerGestureMultitouchMinDistance, |
| 5024 | cosine, mConfig.pointerGestureSwipeTransitionAngleCosine); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5025 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5026 | mPointerGesture.currentGestureMode = PointerGesture::SWIPE; |
| 5027 | } else { |
| 5028 | // Pointers are moving in different directions. Switch to FREEFORM. |
| 5029 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5030 | ALOGD("Gestures: PRESS transitioned to FREEFORM, " |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5031 | "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, " |
| 5032 | "cosine %0.3f < %0.3f", |
| 5033 | dist1, mConfig.pointerGestureMultitouchMinDistance, |
| 5034 | dist2, mConfig.pointerGestureMultitouchMinDistance, |
| 5035 | cosine, mConfig.pointerGestureSwipeTransitionAngleCosine); |
| 5036 | #endif |
| 5037 | *outCancelPreviousGesture = true; |
| 5038 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 5039 | } |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5040 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5041 | } |
| 5042 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5043 | } |
| 5044 | } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5045 | // Switch from SWIPE to FREEFORM if additional pointers go down. |
| 5046 | // Cancel previous gesture. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5047 | if (currentFingerCount > 2) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5048 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5049 | ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5050 | currentFingerCount); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5051 | #endif |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5052 | *outCancelPreviousGesture = true; |
| 5053 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 5054 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5055 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 5056 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5057 | // Move the reference points based on the overall group motion of the fingers |
| 5058 | // except in PRESS mode while waiting for a transition to occur. |
| 5059 | if (mPointerGesture.currentGestureMode != PointerGesture::PRESS |
| 5060 | && (commonDeltaX || commonDeltaY)) { |
| 5061 | for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5062 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 5063 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5064 | delta.dx = 0; |
| 5065 | delta.dy = 0; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5066 | } |
| 5067 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5068 | mPointerGesture.referenceTouchX += commonDeltaX; |
| 5069 | mPointerGesture.referenceTouchY += commonDeltaY; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 5070 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5071 | commonDeltaX *= mPointerXMovementScale; |
| 5072 | commonDeltaY *= mPointerYMovementScale; |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 5073 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5074 | rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5075 | mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 5076 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5077 | mPointerGesture.referenceGestureX += commonDeltaX; |
| 5078 | mPointerGesture.referenceGestureY += commonDeltaY; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5079 | } |
| 5080 | |
| 5081 | // Report gestures. |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 5082 | if (mPointerGesture.currentGestureMode == PointerGesture::PRESS |
| 5083 | || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) { |
| 5084 | // PRESS or SWIPE mode. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5085 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5086 | ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d," |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5087 | "activeGestureId=%d, currentTouchPointerCount=%d", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5088 | activeTouchId, mPointerGesture.activeGestureId, currentFingerCount); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5089 | #endif |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 5090 | ALOG_ASSERT(mPointerGesture.activeGestureId >= 0); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5091 | |
| 5092 | mPointerGesture.currentGestureIdBits.clear(); |
| 5093 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 5094 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5095 | mPointerGesture.currentGestureProperties[0].clear(); |
| 5096 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
| 5097 | mPointerGesture.currentGestureProperties[0].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5098 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5099 | mPointerGesture.currentGestureCoords[0].clear(); |
| 5100 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 5101 | mPointerGesture.referenceGestureX); |
| 5102 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, |
| 5103 | mPointerGesture.referenceGestureY); |
| 5104 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5105 | } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) { |
| 5106 | // FREEFORM mode. |
| 5107 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5108 | ALOGD("Gestures: FREEFORM activeTouchId=%d," |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5109 | "activeGestureId=%d, currentTouchPointerCount=%d", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5110 | activeTouchId, mPointerGesture.activeGestureId, currentFingerCount); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5111 | #endif |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 5112 | ALOG_ASSERT(mPointerGesture.activeGestureId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5113 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5114 | mPointerGesture.currentGestureIdBits.clear(); |
| 5115 | |
| 5116 | BitSet32 mappedTouchIdBits; |
| 5117 | BitSet32 usedGestureIdBits; |
| 5118 | if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) { |
| 5119 | // Initially, assign the active gesture id to the active touch point |
| 5120 | // if there is one. No other touch id bits are mapped yet. |
| 5121 | if (!*outCancelPreviousGesture) { |
| 5122 | mappedTouchIdBits.markBit(activeTouchId); |
| 5123 | usedGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 5124 | mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] = |
| 5125 | mPointerGesture.activeGestureId; |
| 5126 | } else { |
| 5127 | mPointerGesture.activeGestureId = -1; |
| 5128 | } |
| 5129 | } else { |
| 5130 | // Otherwise, assume we mapped all touches from the previous frame. |
| 5131 | // Reuse all mappings that are still applicable. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5132 | mappedTouchIdBits.value = mLastFingerIdBits.value |
| 5133 | & mCurrentFingerIdBits.value; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5134 | usedGestureIdBits = mPointerGesture.lastGestureIdBits; |
| 5135 | |
| 5136 | // Check whether we need to choose a new active gesture id because the |
| 5137 | // current went went up. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5138 | for (BitSet32 upTouchIdBits(mLastFingerIdBits.value |
| 5139 | & ~mCurrentFingerIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5140 | !upTouchIdBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5141 | uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5142 | uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId]; |
| 5143 | if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) { |
| 5144 | mPointerGesture.activeGestureId = -1; |
| 5145 | break; |
| 5146 | } |
| 5147 | } |
| 5148 | } |
| 5149 | |
| 5150 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5151 | ALOGD("Gestures: FREEFORM follow up " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5152 | "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, " |
| 5153 | "activeGestureId=%d", |
| 5154 | mappedTouchIdBits.value, usedGestureIdBits.value, |
| 5155 | mPointerGesture.activeGestureId); |
| 5156 | #endif |
| 5157 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5158 | BitSet32 idBits(mCurrentFingerIdBits); |
| 5159 | for (uint32_t i = 0; i < currentFingerCount; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5160 | uint32_t touchId = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5161 | uint32_t gestureId; |
| 5162 | if (!mappedTouchIdBits.hasBit(touchId)) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5163 | gestureId = usedGestureIdBits.markFirstUnmarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5164 | mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId; |
| 5165 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5166 | ALOGD("Gestures: FREEFORM " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5167 | "new mapping for touch id %d -> gesture id %d", |
| 5168 | touchId, gestureId); |
| 5169 | #endif |
| 5170 | } else { |
| 5171 | gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId]; |
| 5172 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5173 | ALOGD("Gestures: FREEFORM " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5174 | "existing mapping for touch id %d -> gesture id %d", |
| 5175 | touchId, gestureId); |
| 5176 | #endif |
| 5177 | } |
| 5178 | mPointerGesture.currentGestureIdBits.markBit(gestureId); |
| 5179 | mPointerGesture.currentGestureIdToIndex[gestureId] = i; |
| 5180 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5181 | const RawPointerData::Pointer& pointer = |
| 5182 | mCurrentRawPointerData.pointerForId(touchId); |
| 5183 | float deltaX = (pointer.x - mPointerGesture.referenceTouchX) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5184 | * mPointerXZoomScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5185 | float deltaY = (pointer.y - mPointerGesture.referenceTouchY) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5186 | * mPointerYZoomScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5187 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5188 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5189 | mPointerGesture.currentGestureProperties[i].clear(); |
| 5190 | mPointerGesture.currentGestureProperties[i].id = gestureId; |
| 5191 | mPointerGesture.currentGestureProperties[i].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5192 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5193 | mPointerGesture.currentGestureCoords[i].clear(); |
| 5194 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 5195 | AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5196 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 5197 | AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5198 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
| 5199 | AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
| 5200 | } |
| 5201 | |
| 5202 | if (mPointerGesture.activeGestureId < 0) { |
| 5203 | mPointerGesture.activeGestureId = |
| 5204 | mPointerGesture.currentGestureIdBits.firstMarkedBit(); |
| 5205 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5206 | ALOGD("Gestures: FREEFORM new " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5207 | "activeGestureId=%d", mPointerGesture.activeGestureId); |
| 5208 | #endif |
| 5209 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5210 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5211 | } |
| 5212 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5213 | mPointerController->setButtonState(mCurrentButtonState); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5214 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5215 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5216 | ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, " |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5217 | "currentGestureMode=%d, currentGestureIdBits=0x%08x, " |
| 5218 | "lastGestureMode=%d, lastGestureIdBits=0x%08x", |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5219 | toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture), |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5220 | mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value, |
| 5221 | mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5222 | for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5223 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5224 | uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5225 | const PointerProperties& properties = mPointerGesture.currentGestureProperties[index]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5226 | const PointerCoords& coords = mPointerGesture.currentGestureCoords[index]; |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5227 | ALOGD(" currentGesture[%d]: index=%d, toolType=%d, " |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5228 | "x=%0.3f, y=%0.3f, pressure=%0.3f", |
| 5229 | id, index, properties.toolType, |
| 5230 | coords.getAxisValue(AMOTION_EVENT_AXIS_X), |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5231 | coords.getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 5232 | coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); |
| 5233 | } |
| 5234 | for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5235 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5236 | uint32_t index = mPointerGesture.lastGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5237 | const PointerProperties& properties = mPointerGesture.lastGestureProperties[index]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5238 | const PointerCoords& coords = mPointerGesture.lastGestureCoords[index]; |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5239 | ALOGD(" lastGesture[%d]: index=%d, toolType=%d, " |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5240 | "x=%0.3f, y=%0.3f, pressure=%0.3f", |
| 5241 | id, index, properties.toolType, |
| 5242 | coords.getAxisValue(AMOTION_EVENT_AXIS_X), |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5243 | coords.getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 5244 | coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); |
| 5245 | } |
| 5246 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 5247 | return true; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5248 | } |
| 5249 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5250 | void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) { |
| 5251 | mPointerSimple.currentCoords.clear(); |
| 5252 | mPointerSimple.currentProperties.clear(); |
| 5253 | |
| 5254 | bool down, hovering; |
| 5255 | if (!mCurrentStylusIdBits.isEmpty()) { |
| 5256 | uint32_t id = mCurrentStylusIdBits.firstMarkedBit(); |
| 5257 | uint32_t index = mCurrentCookedPointerData.idToIndex[id]; |
| 5258 | float x = mCurrentCookedPointerData.pointerCoords[index].getX(); |
| 5259 | float y = mCurrentCookedPointerData.pointerCoords[index].getY(); |
| 5260 | mPointerController->setPosition(x, y); |
| 5261 | |
| 5262 | hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id); |
| 5263 | down = !hovering; |
| 5264 | |
| 5265 | mPointerController->getPosition(&x, &y); |
| 5266 | mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]); |
| 5267 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 5268 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 5269 | mPointerSimple.currentProperties.id = 0; |
| 5270 | mPointerSimple.currentProperties.toolType = |
| 5271 | mCurrentCookedPointerData.pointerProperties[index].toolType; |
| 5272 | } else { |
| 5273 | down = false; |
| 5274 | hovering = false; |
| 5275 | } |
| 5276 | |
| 5277 | dispatchPointerSimple(when, policyFlags, down, hovering); |
| 5278 | } |
| 5279 | |
| 5280 | void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) { |
| 5281 | abortPointerSimple(when, policyFlags); |
| 5282 | } |
| 5283 | |
| 5284 | void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) { |
| 5285 | mPointerSimple.currentCoords.clear(); |
| 5286 | mPointerSimple.currentProperties.clear(); |
| 5287 | |
| 5288 | bool down, hovering; |
| 5289 | if (!mCurrentMouseIdBits.isEmpty()) { |
| 5290 | uint32_t id = mCurrentMouseIdBits.firstMarkedBit(); |
| 5291 | uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id]; |
| 5292 | if (mLastMouseIdBits.hasBit(id)) { |
| 5293 | uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id]; |
| 5294 | float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x |
| 5295 | - mLastRawPointerData.pointers[lastIndex].x) |
| 5296 | * mPointerXMovementScale; |
| 5297 | float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y |
| 5298 | - mLastRawPointerData.pointers[lastIndex].y) |
| 5299 | * mPointerYMovementScale; |
| 5300 | |
| 5301 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
| 5302 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
| 5303 | |
| 5304 | mPointerController->move(deltaX, deltaY); |
| 5305 | } else { |
| 5306 | mPointerVelocityControl.reset(); |
| 5307 | } |
| 5308 | |
| 5309 | down = isPointerDown(mCurrentButtonState); |
| 5310 | hovering = !down; |
| 5311 | |
| 5312 | float x, y; |
| 5313 | mPointerController->getPosition(&x, &y); |
| 5314 | mPointerSimple.currentCoords.copyFrom( |
| 5315 | mCurrentCookedPointerData.pointerCoords[currentIndex]); |
| 5316 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 5317 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 5318 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, |
| 5319 | hovering ? 0.0f : 1.0f); |
| 5320 | mPointerSimple.currentProperties.id = 0; |
| 5321 | mPointerSimple.currentProperties.toolType = |
| 5322 | mCurrentCookedPointerData.pointerProperties[currentIndex].toolType; |
| 5323 | } else { |
| 5324 | mPointerVelocityControl.reset(); |
| 5325 | |
| 5326 | down = false; |
| 5327 | hovering = false; |
| 5328 | } |
| 5329 | |
| 5330 | dispatchPointerSimple(when, policyFlags, down, hovering); |
| 5331 | } |
| 5332 | |
| 5333 | void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) { |
| 5334 | abortPointerSimple(when, policyFlags); |
| 5335 | |
| 5336 | mPointerVelocityControl.reset(); |
| 5337 | } |
| 5338 | |
| 5339 | void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags, |
| 5340 | bool down, bool hovering) { |
| 5341 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 5342 | |
| 5343 | if (mPointerController != NULL) { |
| 5344 | if (down || hovering) { |
| 5345 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER); |
| 5346 | mPointerController->clearSpots(); |
| 5347 | mPointerController->setButtonState(mCurrentButtonState); |
| 5348 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 5349 | } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) { |
| 5350 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 5351 | } |
| 5352 | } |
| 5353 | |
| 5354 | if (mPointerSimple.down && !down) { |
| 5355 | mPointerSimple.down = false; |
| 5356 | |
| 5357 | // Send up. |
| 5358 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5359 | AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 5360 | mViewport.displayId, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5361 | 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords, |
| 5362 | mOrientedXPrecision, mOrientedYPrecision, |
| 5363 | mPointerSimple.downTime); |
| 5364 | getListener()->notifyMotion(&args); |
| 5365 | } |
| 5366 | |
| 5367 | if (mPointerSimple.hovering && !hovering) { |
| 5368 | mPointerSimple.hovering = false; |
| 5369 | |
| 5370 | // Send hover exit. |
| 5371 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5372 | AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 5373 | mViewport.displayId, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5374 | 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords, |
| 5375 | mOrientedXPrecision, mOrientedYPrecision, |
| 5376 | mPointerSimple.downTime); |
| 5377 | getListener()->notifyMotion(&args); |
| 5378 | } |
| 5379 | |
| 5380 | if (down) { |
| 5381 | if (!mPointerSimple.down) { |
| 5382 | mPointerSimple.down = true; |
| 5383 | mPointerSimple.downTime = when; |
| 5384 | |
| 5385 | // Send down. |
| 5386 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5387 | AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 5388 | mViewport.displayId, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5389 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5390 | mOrientedXPrecision, mOrientedYPrecision, |
| 5391 | mPointerSimple.downTime); |
| 5392 | getListener()->notifyMotion(&args); |
| 5393 | } |
| 5394 | |
| 5395 | // Send move. |
| 5396 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5397 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 5398 | mViewport.displayId, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5399 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5400 | mOrientedXPrecision, mOrientedYPrecision, |
| 5401 | mPointerSimple.downTime); |
| 5402 | getListener()->notifyMotion(&args); |
| 5403 | } |
| 5404 | |
| 5405 | if (hovering) { |
| 5406 | if (!mPointerSimple.hovering) { |
| 5407 | mPointerSimple.hovering = true; |
| 5408 | |
| 5409 | // Send hover enter. |
| 5410 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5411 | AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 5412 | mViewport.displayId, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5413 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5414 | mOrientedXPrecision, mOrientedYPrecision, |
| 5415 | mPointerSimple.downTime); |
| 5416 | getListener()->notifyMotion(&args); |
| 5417 | } |
| 5418 | |
| 5419 | // Send hover move. |
| 5420 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5421 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 5422 | mViewport.displayId, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5423 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5424 | mOrientedXPrecision, mOrientedYPrecision, |
| 5425 | mPointerSimple.downTime); |
| 5426 | getListener()->notifyMotion(&args); |
| 5427 | } |
| 5428 | |
| 5429 | if (mCurrentRawVScroll || mCurrentRawHScroll) { |
| 5430 | float vscroll = mCurrentRawVScroll; |
| 5431 | float hscroll = mCurrentRawHScroll; |
| 5432 | mWheelYVelocityControl.move(when, NULL, &vscroll); |
| 5433 | mWheelXVelocityControl.move(when, &hscroll, NULL); |
| 5434 | |
| 5435 | // Send scroll. |
| 5436 | PointerCoords pointerCoords; |
| 5437 | pointerCoords.copyFrom(mPointerSimple.currentCoords); |
| 5438 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll); |
| 5439 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll); |
| 5440 | |
| 5441 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5442 | AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 5443 | mViewport.displayId, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5444 | 1, &mPointerSimple.currentProperties, &pointerCoords, |
| 5445 | mOrientedXPrecision, mOrientedYPrecision, |
| 5446 | mPointerSimple.downTime); |
| 5447 | getListener()->notifyMotion(&args); |
| 5448 | } |
| 5449 | |
| 5450 | // Save state. |
| 5451 | if (down || hovering) { |
| 5452 | mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords); |
| 5453 | mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties); |
| 5454 | } else { |
| 5455 | mPointerSimple.reset(); |
| 5456 | } |
| 5457 | } |
| 5458 | |
| 5459 | void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) { |
| 5460 | mPointerSimple.currentCoords.clear(); |
| 5461 | mPointerSimple.currentProperties.clear(); |
| 5462 | |
| 5463 | dispatchPointerSimple(when, policyFlags, false, false); |
| 5464 | } |
| 5465 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5466 | void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5467 | int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags, |
| 5468 | const PointerProperties* properties, const PointerCoords* coords, |
| 5469 | const uint32_t* idToIndex, BitSet32 idBits, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5470 | int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) { |
| 5471 | PointerCoords pointerCoords[MAX_POINTERS]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5472 | PointerProperties pointerProperties[MAX_POINTERS]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5473 | uint32_t pointerCount = 0; |
| 5474 | while (!idBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5475 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5476 | uint32_t index = idToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5477 | pointerProperties[pointerCount].copyFrom(properties[index]); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5478 | pointerCoords[pointerCount].copyFrom(coords[index]); |
| 5479 | |
| 5480 | if (changedId >= 0 && id == uint32_t(changedId)) { |
| 5481 | action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
| 5482 | } |
| 5483 | |
| 5484 | pointerCount += 1; |
| 5485 | } |
| 5486 | |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 5487 | ALOG_ASSERT(pointerCount != 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5488 | |
| 5489 | if (changedId >= 0 && pointerCount == 1) { |
| 5490 | // Replace initial down and final up action. |
| 5491 | // We can compare the action without masking off the changed pointer index |
| 5492 | // because we know the index is 0. |
| 5493 | if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 5494 | action = AMOTION_EVENT_ACTION_DOWN; |
| 5495 | } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 5496 | action = AMOTION_EVENT_ACTION_UP; |
| 5497 | } else { |
| 5498 | // Can't happen. |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 5499 | ALOG_ASSERT(false); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5500 | } |
| 5501 | } |
| 5502 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5503 | NotifyMotionArgs args(when, getDeviceId(), source, policyFlags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5504 | action, flags, metaState, buttonState, edgeFlags, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 5505 | mViewport.displayId, pointerCount, pointerProperties, pointerCoords, |
| 5506 | xPrecision, yPrecision, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5507 | getListener()->notifyMotion(&args); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5508 | } |
| 5509 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5510 | bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5511 | const PointerCoords* inCoords, const uint32_t* inIdToIndex, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5512 | PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex, |
| 5513 | BitSet32 idBits) const { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5514 | bool changed = false; |
| 5515 | while (!idBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5516 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5517 | uint32_t inIndex = inIdToIndex[id]; |
| 5518 | uint32_t outIndex = outIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5519 | |
| 5520 | const PointerProperties& curInProperties = inProperties[inIndex]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5521 | const PointerCoords& curInCoords = inCoords[inIndex]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5522 | PointerProperties& curOutProperties = outProperties[outIndex]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5523 | PointerCoords& curOutCoords = outCoords[outIndex]; |
| 5524 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5525 | if (curInProperties != curOutProperties) { |
| 5526 | curOutProperties.copyFrom(curInProperties); |
| 5527 | changed = true; |
| 5528 | } |
| 5529 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5530 | if (curInCoords != curOutCoords) { |
| 5531 | curOutCoords.copyFrom(curInCoords); |
| 5532 | changed = true; |
| 5533 | } |
| 5534 | } |
| 5535 | return changed; |
| 5536 | } |
| 5537 | |
| 5538 | void TouchInputMapper::fadePointer() { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5539 | if (mPointerController != NULL) { |
| 5540 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 5541 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5542 | } |
| 5543 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5544 | bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) { |
| 5545 | return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue |
| 5546 | && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5547 | } |
| 5548 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5549 | const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit( |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 5550 | int32_t x, int32_t y) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5551 | size_t numVirtualKeys = mVirtualKeys.size(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 5552 | for (size_t i = 0; i < numVirtualKeys; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5553 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5554 | |
| 5555 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5556 | ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, " |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5557 | "left=%d, top=%d, right=%d, bottom=%d", |
| 5558 | x, y, |
| 5559 | virtualKey.keyCode, virtualKey.scanCode, |
| 5560 | virtualKey.hitLeft, virtualKey.hitTop, |
| 5561 | virtualKey.hitRight, virtualKey.hitBottom); |
| 5562 | #endif |
| 5563 | |
| 5564 | if (virtualKey.isHit(x, y)) { |
| 5565 | return & virtualKey; |
| 5566 | } |
| 5567 | } |
| 5568 | |
| 5569 | return NULL; |
| 5570 | } |
| 5571 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5572 | void TouchInputMapper::assignPointerIds() { |
| 5573 | uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount; |
| 5574 | uint32_t lastPointerCount = mLastRawPointerData.pointerCount; |
| 5575 | |
| 5576 | mCurrentRawPointerData.clearIdBits(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5577 | |
| 5578 | if (currentPointerCount == 0) { |
| 5579 | // No pointers to assign. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5580 | return; |
| 5581 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5582 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5583 | if (lastPointerCount == 0) { |
| 5584 | // All pointers are new. |
| 5585 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
| 5586 | uint32_t id = i; |
| 5587 | mCurrentRawPointerData.pointers[i].id = id; |
| 5588 | mCurrentRawPointerData.idToIndex[id] = i; |
| 5589 | mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i)); |
| 5590 | } |
| 5591 | return; |
| 5592 | } |
| 5593 | |
| 5594 | if (currentPointerCount == 1 && lastPointerCount == 1 |
| 5595 | && mCurrentRawPointerData.pointers[0].toolType |
| 5596 | == mLastRawPointerData.pointers[0].toolType) { |
| 5597 | // Only one pointer and no change in count so it must have the same id as before. |
| 5598 | uint32_t id = mLastRawPointerData.pointers[0].id; |
| 5599 | mCurrentRawPointerData.pointers[0].id = id; |
| 5600 | mCurrentRawPointerData.idToIndex[id] = 0; |
| 5601 | mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0)); |
| 5602 | return; |
| 5603 | } |
| 5604 | |
| 5605 | // General case. |
| 5606 | // We build a heap of squared euclidean distances between current and last pointers |
| 5607 | // associated with the current and last pointer indices. Then, we find the best |
| 5608 | // match (by distance) for each current pointer. |
| 5609 | // The pointers must have the same tool type but it is possible for them to |
| 5610 | // transition from hovering to touching or vice-versa while retaining the same id. |
| 5611 | PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS]; |
| 5612 | |
| 5613 | uint32_t heapSize = 0; |
| 5614 | for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount; |
| 5615 | currentPointerIndex++) { |
| 5616 | for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount; |
| 5617 | lastPointerIndex++) { |
| 5618 | const RawPointerData::Pointer& currentPointer = |
| 5619 | mCurrentRawPointerData.pointers[currentPointerIndex]; |
| 5620 | const RawPointerData::Pointer& lastPointer = |
| 5621 | mLastRawPointerData.pointers[lastPointerIndex]; |
| 5622 | if (currentPointer.toolType == lastPointer.toolType) { |
| 5623 | int64_t deltaX = currentPointer.x - lastPointer.x; |
| 5624 | int64_t deltaY = currentPointer.y - lastPointer.y; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5625 | |
| 5626 | uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY); |
| 5627 | |
| 5628 | // Insert new element into the heap (sift up). |
| 5629 | heap[heapSize].currentPointerIndex = currentPointerIndex; |
| 5630 | heap[heapSize].lastPointerIndex = lastPointerIndex; |
| 5631 | heap[heapSize].distance = distance; |
| 5632 | heapSize += 1; |
| 5633 | } |
| 5634 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5635 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5636 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5637 | // Heapify |
| 5638 | for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) { |
| 5639 | startIndex -= 1; |
| 5640 | for (uint32_t parentIndex = startIndex; ;) { |
| 5641 | uint32_t childIndex = parentIndex * 2 + 1; |
| 5642 | if (childIndex >= heapSize) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5643 | break; |
| 5644 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5645 | |
| 5646 | if (childIndex + 1 < heapSize |
| 5647 | && heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 5648 | childIndex += 1; |
| 5649 | } |
| 5650 | |
| 5651 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 5652 | break; |
| 5653 | } |
| 5654 | |
| 5655 | swap(heap[parentIndex], heap[childIndex]); |
| 5656 | parentIndex = childIndex; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5657 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5658 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5659 | |
| 5660 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5661 | ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5662 | for (size_t i = 0; i < heapSize; i++) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5663 | ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5664 | i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, |
| 5665 | heap[i].distance); |
| 5666 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5667 | #endif |
| 5668 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5669 | // Pull matches out by increasing order of distance. |
| 5670 | // To avoid reassigning pointers that have already been matched, the loop keeps track |
| 5671 | // of which last and current pointers have been matched using the matchedXXXBits variables. |
| 5672 | // It also tracks the used pointer id bits. |
| 5673 | BitSet32 matchedLastBits(0); |
| 5674 | BitSet32 matchedCurrentBits(0); |
| 5675 | BitSet32 usedIdBits(0); |
| 5676 | bool first = true; |
| 5677 | for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) { |
| 5678 | while (heapSize > 0) { |
| 5679 | if (first) { |
| 5680 | // The first time through the loop, we just consume the root element of |
| 5681 | // the heap (the one with smallest distance). |
| 5682 | first = false; |
| 5683 | } else { |
| 5684 | // Previous iterations consumed the root element of the heap. |
| 5685 | // Pop root element off of the heap (sift down). |
| 5686 | heap[0] = heap[heapSize]; |
| 5687 | for (uint32_t parentIndex = 0; ;) { |
| 5688 | uint32_t childIndex = parentIndex * 2 + 1; |
| 5689 | if (childIndex >= heapSize) { |
| 5690 | break; |
| 5691 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5692 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5693 | if (childIndex + 1 < heapSize |
| 5694 | && heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 5695 | childIndex += 1; |
| 5696 | } |
| 5697 | |
| 5698 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 5699 | break; |
| 5700 | } |
| 5701 | |
| 5702 | swap(heap[parentIndex], heap[childIndex]); |
| 5703 | parentIndex = childIndex; |
| 5704 | } |
| 5705 | |
| 5706 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5707 | ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5708 | for (size_t i = 0; i < heapSize; i++) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5709 | ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5710 | i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, |
| 5711 | heap[i].distance); |
| 5712 | } |
| 5713 | #endif |
| 5714 | } |
| 5715 | |
| 5716 | heapSize -= 1; |
| 5717 | |
| 5718 | uint32_t currentPointerIndex = heap[0].currentPointerIndex; |
| 5719 | if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched |
| 5720 | |
| 5721 | uint32_t lastPointerIndex = heap[0].lastPointerIndex; |
| 5722 | if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched |
| 5723 | |
| 5724 | matchedCurrentBits.markBit(currentPointerIndex); |
| 5725 | matchedLastBits.markBit(lastPointerIndex); |
| 5726 | |
| 5727 | uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id; |
| 5728 | mCurrentRawPointerData.pointers[currentPointerIndex].id = id; |
| 5729 | mCurrentRawPointerData.idToIndex[id] = currentPointerIndex; |
| 5730 | mCurrentRawPointerData.markIdBit(id, |
| 5731 | mCurrentRawPointerData.isHovering(currentPointerIndex)); |
| 5732 | usedIdBits.markBit(id); |
| 5733 | |
| 5734 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5735 | ALOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5736 | lastPointerIndex, currentPointerIndex, id, heap[0].distance); |
| 5737 | #endif |
| 5738 | break; |
| 5739 | } |
| 5740 | } |
| 5741 | |
| 5742 | // Assign fresh ids to pointers that were not matched in the process. |
| 5743 | for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) { |
| 5744 | uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit(); |
| 5745 | uint32_t id = usedIdBits.markFirstUnmarkedBit(); |
| 5746 | |
| 5747 | mCurrentRawPointerData.pointers[currentPointerIndex].id = id; |
| 5748 | mCurrentRawPointerData.idToIndex[id] = currentPointerIndex; |
| 5749 | mCurrentRawPointerData.markIdBit(id, |
| 5750 | mCurrentRawPointerData.isHovering(currentPointerIndex)); |
| 5751 | |
| 5752 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5753 | ALOGD("assignPointerIds - assigned: cur=%d, id=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5754 | currentPointerIndex, id); |
| 5755 | #endif |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5756 | } |
| 5757 | } |
| 5758 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5759 | int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5760 | if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) { |
| 5761 | return AKEY_STATE_VIRTUAL; |
| 5762 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5763 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5764 | size_t numVirtualKeys = mVirtualKeys.size(); |
| 5765 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 5766 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
| 5767 | if (virtualKey.keyCode == keyCode) { |
| 5768 | return AKEY_STATE_UP; |
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 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5771 | |
| 5772 | return AKEY_STATE_UNKNOWN; |
| 5773 | } |
| 5774 | |
| 5775 | int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5776 | if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) { |
| 5777 | return AKEY_STATE_VIRTUAL; |
| 5778 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5779 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5780 | size_t numVirtualKeys = mVirtualKeys.size(); |
| 5781 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 5782 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
| 5783 | if (virtualKey.scanCode == scanCode) { |
| 5784 | return AKEY_STATE_UP; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5785 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5786 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5787 | |
| 5788 | return AKEY_STATE_UNKNOWN; |
| 5789 | } |
| 5790 | |
| 5791 | bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 5792 | const int32_t* keyCodes, uint8_t* outFlags) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5793 | size_t numVirtualKeys = mVirtualKeys.size(); |
| 5794 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 5795 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5796 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5797 | for (size_t i = 0; i < numCodes; i++) { |
| 5798 | if (virtualKey.keyCode == keyCodes[i]) { |
| 5799 | outFlags[i] = 1; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5800 | } |
| 5801 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5802 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5803 | |
| 5804 | return true; |
| 5805 | } |
| 5806 | |
| 5807 | |
| 5808 | // --- SingleTouchInputMapper --- |
| 5809 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 5810 | SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) : |
| 5811 | TouchInputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5812 | } |
| 5813 | |
| 5814 | SingleTouchInputMapper::~SingleTouchInputMapper() { |
| 5815 | } |
| 5816 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5817 | void SingleTouchInputMapper::reset(nsecs_t when) { |
| 5818 | mSingleTouchMotionAccumulator.reset(getDevice()); |
| 5819 | |
| 5820 | TouchInputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5821 | } |
| 5822 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5823 | void SingleTouchInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5824 | TouchInputMapper::process(rawEvent); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5825 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5826 | mSingleTouchMotionAccumulator.process(rawEvent); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5827 | } |
| 5828 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5829 | void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) { |
Jeff Brown | d87c6d5 | 2011-08-10 14:55:59 -0700 | [diff] [blame] | 5830 | if (mTouchButtonAccumulator.isToolActive()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5831 | mCurrentRawPointerData.pointerCount = 1; |
| 5832 | mCurrentRawPointerData.idToIndex[0] = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5833 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5834 | bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE |
| 5835 | && (mTouchButtonAccumulator.isHovering() |
| 5836 | || (mRawPointerAxes.pressure.valid |
| 5837 | && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0)); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5838 | mCurrentRawPointerData.markIdBit(0, isHovering); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5839 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5840 | RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0]; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5841 | outPointer.id = 0; |
| 5842 | outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX(); |
| 5843 | outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY(); |
| 5844 | outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure(); |
| 5845 | outPointer.touchMajor = 0; |
| 5846 | outPointer.touchMinor = 0; |
| 5847 | outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth(); |
| 5848 | outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth(); |
| 5849 | outPointer.orientation = 0; |
| 5850 | outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5851 | outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX(); |
| 5852 | outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY(); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5853 | outPointer.toolType = mTouchButtonAccumulator.getToolType(); |
| 5854 | if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 5855 | outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 5856 | } |
| 5857 | outPointer.isHovering = isHovering; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5858 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5859 | } |
| 5860 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5861 | void SingleTouchInputMapper::configureRawPointerAxes() { |
| 5862 | TouchInputMapper::configureRawPointerAxes(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5863 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5864 | getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x); |
| 5865 | getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y); |
| 5866 | getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure); |
| 5867 | getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor); |
| 5868 | getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5869 | getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX); |
| 5870 | getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5871 | } |
| 5872 | |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 5873 | bool SingleTouchInputMapper::hasStylus() const { |
| 5874 | return mTouchButtonAccumulator.hasStylus(); |
| 5875 | } |
| 5876 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5877 | |
| 5878 | // --- MultiTouchInputMapper --- |
| 5879 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 5880 | MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) : |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5881 | TouchInputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5882 | } |
| 5883 | |
| 5884 | MultiTouchInputMapper::~MultiTouchInputMapper() { |
| 5885 | } |
| 5886 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5887 | void MultiTouchInputMapper::reset(nsecs_t when) { |
| 5888 | mMultiTouchMotionAccumulator.reset(getDevice()); |
| 5889 | |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5890 | mPointerIdBits.clear(); |
Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 5891 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5892 | TouchInputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5893 | } |
| 5894 | |
| 5895 | void MultiTouchInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5896 | TouchInputMapper::process(rawEvent); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5897 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5898 | mMultiTouchMotionAccumulator.process(rawEvent); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5899 | } |
| 5900 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5901 | void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5902 | size_t inCount = mMultiTouchMotionAccumulator.getSlotCount(); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5903 | size_t outCount = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5904 | BitSet32 newPointerIdBits; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5905 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5906 | for (size_t inIndex = 0; inIndex < inCount; inIndex++) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5907 | const MultiTouchMotionAccumulator::Slot* inSlot = |
| 5908 | mMultiTouchMotionAccumulator.getSlot(inIndex); |
| 5909 | if (!inSlot->isInUse()) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5910 | continue; |
| 5911 | } |
| 5912 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5913 | if (outCount >= MAX_POINTERS) { |
| 5914 | #if DEBUG_POINTERS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5915 | ALOGD("MultiTouch device %s emitted more than maximum of %d pointers; " |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5916 | "ignoring the rest.", |
| 5917 | getDeviceName().string(), MAX_POINTERS); |
| 5918 | #endif |
| 5919 | break; // too many fingers! |
| 5920 | } |
| 5921 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5922 | RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount]; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5923 | outPointer.x = inSlot->getX(); |
| 5924 | outPointer.y = inSlot->getY(); |
| 5925 | outPointer.pressure = inSlot->getPressure(); |
| 5926 | outPointer.touchMajor = inSlot->getTouchMajor(); |
| 5927 | outPointer.touchMinor = inSlot->getTouchMinor(); |
| 5928 | outPointer.toolMajor = inSlot->getToolMajor(); |
| 5929 | outPointer.toolMinor = inSlot->getToolMinor(); |
| 5930 | outPointer.orientation = inSlot->getOrientation(); |
| 5931 | outPointer.distance = inSlot->getDistance(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5932 | outPointer.tiltX = 0; |
| 5933 | outPointer.tiltY = 0; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5934 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5935 | outPointer.toolType = inSlot->getToolType(); |
| 5936 | if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 5937 | outPointer.toolType = mTouchButtonAccumulator.getToolType(); |
| 5938 | if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 5939 | outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 5940 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5941 | } |
| 5942 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5943 | bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE |
| 5944 | && (mTouchButtonAccumulator.isHovering() |
| 5945 | || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0)); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5946 | outPointer.isHovering = isHovering; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5947 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5948 | // Assign pointer id using tracking id if available. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5949 | if (*outHavePointerIds) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5950 | int32_t trackingId = inSlot->getTrackingId(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5951 | int32_t id = -1; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5952 | if (trackingId >= 0) { |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5953 | for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5954 | uint32_t n = idBits.clearFirstMarkedBit(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5955 | if (mPointerTrackingIdMap[n] == trackingId) { |
| 5956 | id = n; |
| 5957 | } |
| 5958 | } |
| 5959 | |
| 5960 | if (id < 0 && !mPointerIdBits.isFull()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5961 | id = mPointerIdBits.markFirstUnmarkedBit(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5962 | mPointerTrackingIdMap[id] = trackingId; |
| 5963 | } |
| 5964 | } |
| 5965 | if (id < 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5966 | *outHavePointerIds = false; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5967 | mCurrentRawPointerData.clearIdBits(); |
| 5968 | newPointerIdBits.clear(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5969 | } else { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5970 | outPointer.id = id; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5971 | mCurrentRawPointerData.idToIndex[id] = outCount; |
| 5972 | mCurrentRawPointerData.markIdBit(id, isHovering); |
| 5973 | newPointerIdBits.markBit(id); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5974 | } |
| 5975 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5976 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5977 | outCount += 1; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5978 | } |
| 5979 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5980 | mCurrentRawPointerData.pointerCount = outCount; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5981 | mPointerIdBits = newPointerIdBits; |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5982 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5983 | mMultiTouchMotionAccumulator.finishSync(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5984 | } |
| 5985 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5986 | void MultiTouchInputMapper::configureRawPointerAxes() { |
| 5987 | TouchInputMapper::configureRawPointerAxes(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5988 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5989 | getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x); |
| 5990 | getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y); |
| 5991 | getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor); |
| 5992 | getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor); |
| 5993 | getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor); |
| 5994 | getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor); |
| 5995 | getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation); |
| 5996 | getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure); |
| 5997 | getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance); |
| 5998 | getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId); |
| 5999 | getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 6000 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 6001 | if (mRawPointerAxes.trackingId.valid |
| 6002 | && mRawPointerAxes.slot.valid |
| 6003 | && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) { |
| 6004 | size_t slotCount = mRawPointerAxes.slot.maxValue + 1; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 6005 | if (slotCount > MAX_SLOTS) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6006 | ALOGW("MultiTouch Device %s reported %d slots but the framework " |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 6007 | "only supports a maximum of %d slots at this time.", |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 6008 | getDeviceName().string(), slotCount, MAX_SLOTS); |
| 6009 | slotCount = MAX_SLOTS; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 6010 | } |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 6011 | mMultiTouchMotionAccumulator.configure(getDevice(), |
| 6012 | slotCount, true /*usingSlotsProtocol*/); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 6013 | } else { |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 6014 | mMultiTouchMotionAccumulator.configure(getDevice(), |
| 6015 | MAX_POINTERS, false /*usingSlotsProtocol*/); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 6016 | } |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 6017 | } |
| 6018 | |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 6019 | bool MultiTouchInputMapper::hasStylus() const { |
| 6020 | return mMultiTouchMotionAccumulator.hasStylus() |
| 6021 | || mTouchButtonAccumulator.hasStylus(); |
| 6022 | } |
| 6023 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 6024 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6025 | // --- JoystickInputMapper --- |
| 6026 | |
| 6027 | JoystickInputMapper::JoystickInputMapper(InputDevice* device) : |
| 6028 | InputMapper(device) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6029 | } |
| 6030 | |
| 6031 | JoystickInputMapper::~JoystickInputMapper() { |
| 6032 | } |
| 6033 | |
| 6034 | uint32_t JoystickInputMapper::getSources() { |
| 6035 | return AINPUT_SOURCE_JOYSTICK; |
| 6036 | } |
| 6037 | |
| 6038 | void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 6039 | InputMapper::populateDeviceInfo(info); |
| 6040 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6041 | for (size_t i = 0; i < mAxes.size(); i++) { |
| 6042 | const Axis& axis = mAxes.valueAt(i); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 6043 | info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK, |
| 6044 | axis.min, axis.max, axis.flat, axis.fuzz); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6045 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 6046 | info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK, |
| 6047 | axis.min, axis.max, axis.flat, axis.fuzz); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6048 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6049 | } |
| 6050 | } |
| 6051 | |
| 6052 | void JoystickInputMapper::dump(String8& dump) { |
| 6053 | dump.append(INDENT2 "Joystick Input Mapper:\n"); |
| 6054 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6055 | dump.append(INDENT3 "Axes:\n"); |
| 6056 | size_t numAxes = mAxes.size(); |
| 6057 | for (size_t i = 0; i < numAxes; i++) { |
| 6058 | const Axis& axis = mAxes.valueAt(i); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6059 | const char* label = getAxisLabel(axis.axisInfo.axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6060 | if (label) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6061 | dump.appendFormat(INDENT4 "%s", label); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6062 | } else { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6063 | dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6064 | } |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6065 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 6066 | label = getAxisLabel(axis.axisInfo.highAxis); |
| 6067 | if (label) { |
| 6068 | dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue); |
| 6069 | } else { |
| 6070 | dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis, |
| 6071 | axis.axisInfo.splitValue); |
| 6072 | } |
| 6073 | } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) { |
| 6074 | dump.append(" (invert)"); |
| 6075 | } |
| 6076 | |
| 6077 | dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n", |
| 6078 | axis.min, axis.max, axis.flat, axis.fuzz); |
| 6079 | dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, " |
| 6080 | "highScale=%0.5f, highOffset=%0.5f\n", |
| 6081 | axis.scale, axis.offset, axis.highScale, axis.highOffset); |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 6082 | dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, " |
| 6083 | "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n", |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6084 | mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue, |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 6085 | axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6086 | } |
| 6087 | } |
| 6088 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 6089 | void JoystickInputMapper::configure(nsecs_t when, |
| 6090 | const InputReaderConfiguration* config, uint32_t changes) { |
| 6091 | InputMapper::configure(when, config, changes); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6092 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 6093 | if (!changes) { // first time only |
| 6094 | // Collect all axes. |
| 6095 | for (int32_t abs = 0; abs <= ABS_MAX; abs++) { |
Jeff Brown | 9ee285a | 2011-08-31 12:56:34 -0700 | [diff] [blame] | 6096 | if (!(getAbsAxisUsage(abs, getDevice()->getClasses()) |
| 6097 | & INPUT_DEVICE_CLASS_JOYSTICK)) { |
| 6098 | continue; // axis must be claimed by a different device |
| 6099 | } |
| 6100 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 6101 | RawAbsoluteAxisInfo rawAxisInfo; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 6102 | getAbsoluteAxisInfo(abs, &rawAxisInfo); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 6103 | if (rawAxisInfo.valid) { |
| 6104 | // Map axis. |
| 6105 | AxisInfo axisInfo; |
| 6106 | bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo); |
| 6107 | if (!explicitlyMapped) { |
| 6108 | // Axis is not explicitly mapped, will choose a generic axis later. |
| 6109 | axisInfo.mode = AxisInfo::MODE_NORMAL; |
| 6110 | axisInfo.axis = -1; |
| 6111 | } |
| 6112 | |
| 6113 | // Apply flat override. |
| 6114 | int32_t rawFlat = axisInfo.flatOverride < 0 |
| 6115 | ? rawAxisInfo.flat : axisInfo.flatOverride; |
| 6116 | |
| 6117 | // Calculate scaling factors and limits. |
| 6118 | Axis axis; |
| 6119 | if (axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 6120 | float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue); |
| 6121 | float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue); |
| 6122 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 6123 | scale, 0.0f, highScale, 0.0f, |
| 6124 | 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
| 6125 | } else if (isCenteredAxis(axisInfo.axis)) { |
| 6126 | float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); |
| 6127 | float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale; |
| 6128 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 6129 | scale, offset, scale, offset, |
| 6130 | -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
| 6131 | } else { |
| 6132 | float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); |
| 6133 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 6134 | scale, 0.0f, scale, 0.0f, |
| 6135 | 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
| 6136 | } |
| 6137 | |
| 6138 | // To eliminate noise while the joystick is at rest, filter out small variations |
| 6139 | // in axis values up front. |
| 6140 | axis.filter = axis.flat * 0.25f; |
| 6141 | |
| 6142 | mAxes.add(abs, axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6143 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6144 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6145 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 6146 | // If there are too many axes, start dropping them. |
| 6147 | // Prefer to keep explicitly mapped axes. |
| 6148 | if (mAxes.size() > PointerCoords::MAX_AXES) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 6149 | 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] | 6150 | getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES); |
| 6151 | pruneAxes(true); |
| 6152 | pruneAxes(false); |
| 6153 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6154 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 6155 | // Assign generic axis ids to remaining axes. |
| 6156 | int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1; |
| 6157 | size_t numAxes = mAxes.size(); |
| 6158 | for (size_t i = 0; i < numAxes; i++) { |
| 6159 | Axis& axis = mAxes.editValueAt(i); |
| 6160 | if (axis.axisInfo.axis < 0) { |
| 6161 | while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16 |
| 6162 | && haveAxis(nextGenericAxisId)) { |
| 6163 | nextGenericAxisId += 1; |
| 6164 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6165 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 6166 | if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) { |
| 6167 | axis.axisInfo.axis = nextGenericAxisId; |
| 6168 | nextGenericAxisId += 1; |
| 6169 | } else { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 6170 | 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] | 6171 | "have already been assigned to other axes.", |
| 6172 | getDeviceName().string(), mAxes.keyAt(i)); |
| 6173 | mAxes.removeItemsAt(i--); |
| 6174 | numAxes -= 1; |
| 6175 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6176 | } |
| 6177 | } |
| 6178 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6179 | } |
| 6180 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6181 | bool JoystickInputMapper::haveAxis(int32_t axisId) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6182 | size_t numAxes = mAxes.size(); |
| 6183 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6184 | const Axis& axis = mAxes.valueAt(i); |
| 6185 | if (axis.axisInfo.axis == axisId |
| 6186 | || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT |
| 6187 | && axis.axisInfo.highAxis == axisId)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6188 | return true; |
| 6189 | } |
| 6190 | } |
| 6191 | return false; |
| 6192 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6193 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6194 | void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) { |
| 6195 | size_t i = mAxes.size(); |
| 6196 | while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) { |
| 6197 | if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) { |
| 6198 | continue; |
| 6199 | } |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 6200 | ALOGI("Discarding joystick '%s' axis %d because there are too many axes.", |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6201 | getDeviceName().string(), mAxes.keyAt(i)); |
| 6202 | mAxes.removeItemsAt(i); |
| 6203 | } |
| 6204 | } |
| 6205 | |
| 6206 | bool JoystickInputMapper::isCenteredAxis(int32_t axis) { |
| 6207 | switch (axis) { |
| 6208 | case AMOTION_EVENT_AXIS_X: |
| 6209 | case AMOTION_EVENT_AXIS_Y: |
| 6210 | case AMOTION_EVENT_AXIS_Z: |
| 6211 | case AMOTION_EVENT_AXIS_RX: |
| 6212 | case AMOTION_EVENT_AXIS_RY: |
| 6213 | case AMOTION_EVENT_AXIS_RZ: |
| 6214 | case AMOTION_EVENT_AXIS_HAT_X: |
| 6215 | case AMOTION_EVENT_AXIS_HAT_Y: |
| 6216 | case AMOTION_EVENT_AXIS_ORIENTATION: |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6217 | case AMOTION_EVENT_AXIS_RUDDER: |
| 6218 | case AMOTION_EVENT_AXIS_WHEEL: |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6219 | return true; |
| 6220 | default: |
| 6221 | return false; |
| 6222 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6223 | } |
| 6224 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 6225 | void JoystickInputMapper::reset(nsecs_t when) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6226 | // Recenter all axes. |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6227 | size_t numAxes = mAxes.size(); |
| 6228 | for (size_t i = 0; i < numAxes; i++) { |
| 6229 | Axis& axis = mAxes.editValueAt(i); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6230 | axis.resetValue(); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6231 | } |
| 6232 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 6233 | InputMapper::reset(when); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6234 | } |
| 6235 | |
| 6236 | void JoystickInputMapper::process(const RawEvent* rawEvent) { |
| 6237 | switch (rawEvent->type) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6238 | case EV_ABS: { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 6239 | ssize_t index = mAxes.indexOfKey(rawEvent->code); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6240 | if (index >= 0) { |
| 6241 | Axis& axis = mAxes.editValueAt(index); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6242 | float newValue, highNewValue; |
| 6243 | switch (axis.axisInfo.mode) { |
| 6244 | case AxisInfo::MODE_INVERT: |
| 6245 | newValue = (axis.rawAxisInfo.maxValue - rawEvent->value) |
| 6246 | * axis.scale + axis.offset; |
| 6247 | highNewValue = 0.0f; |
| 6248 | break; |
| 6249 | case AxisInfo::MODE_SPLIT: |
| 6250 | if (rawEvent->value < axis.axisInfo.splitValue) { |
| 6251 | newValue = (axis.axisInfo.splitValue - rawEvent->value) |
| 6252 | * axis.scale + axis.offset; |
| 6253 | highNewValue = 0.0f; |
| 6254 | } else if (rawEvent->value > axis.axisInfo.splitValue) { |
| 6255 | newValue = 0.0f; |
| 6256 | highNewValue = (rawEvent->value - axis.axisInfo.splitValue) |
| 6257 | * axis.highScale + axis.highOffset; |
| 6258 | } else { |
| 6259 | newValue = 0.0f; |
| 6260 | highNewValue = 0.0f; |
| 6261 | } |
| 6262 | break; |
| 6263 | default: |
| 6264 | newValue = rawEvent->value * axis.scale + axis.offset; |
| 6265 | highNewValue = 0.0f; |
| 6266 | break; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6267 | } |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6268 | axis.newValue = newValue; |
| 6269 | axis.highNewValue = highNewValue; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6270 | } |
| 6271 | break; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6272 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6273 | |
| 6274 | case EV_SYN: |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 6275 | switch (rawEvent->code) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6276 | case SYN_REPORT: |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6277 | sync(rawEvent->when, false /*force*/); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6278 | break; |
| 6279 | } |
| 6280 | break; |
| 6281 | } |
| 6282 | } |
| 6283 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6284 | void JoystickInputMapper::sync(nsecs_t when, bool force) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6285 | if (!filterAxes(force)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6286 | return; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6287 | } |
| 6288 | |
| 6289 | int32_t metaState = mContext->getGlobalMetaState(); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 6290 | int32_t buttonState = 0; |
| 6291 | |
| 6292 | PointerProperties pointerProperties; |
| 6293 | pointerProperties.clear(); |
| 6294 | pointerProperties.id = 0; |
| 6295 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6296 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6297 | PointerCoords pointerCoords; |
| 6298 | pointerCoords.clear(); |
| 6299 | |
| 6300 | size_t numAxes = mAxes.size(); |
| 6301 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6302 | const Axis& axis = mAxes.valueAt(i); |
| 6303 | pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue); |
| 6304 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 6305 | pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue); |
| 6306 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6307 | } |
| 6308 | |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 6309 | // Moving a joystick axis should not wake the device because joysticks can |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 6310 | // be fairly noisy even when not in use. On the other hand, pushing a gamepad |
| 6311 | // button will likely wake the device. |
| 6312 | // TODO: Use the input device configuration to control this behavior more finely. |
| 6313 | uint32_t policyFlags = 0; |
| 6314 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 6315 | NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 6316 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 6317 | ADISPLAY_ID_NONE, 1, &pointerProperties, &pointerCoords, 0, 0, 0); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 6318 | getListener()->notifyMotion(&args); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6319 | } |
| 6320 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6321 | bool JoystickInputMapper::filterAxes(bool force) { |
| 6322 | bool atLeastOneSignificantChange = force; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6323 | size_t numAxes = mAxes.size(); |
| 6324 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6325 | Axis& axis = mAxes.editValueAt(i); |
| 6326 | if (force || hasValueChangedSignificantly(axis.filter, |
| 6327 | axis.newValue, axis.currentValue, axis.min, axis.max)) { |
| 6328 | axis.currentValue = axis.newValue; |
| 6329 | atLeastOneSignificantChange = true; |
| 6330 | } |
| 6331 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 6332 | if (force || hasValueChangedSignificantly(axis.filter, |
| 6333 | axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) { |
| 6334 | axis.highCurrentValue = axis.highNewValue; |
| 6335 | atLeastOneSignificantChange = true; |
| 6336 | } |
| 6337 | } |
| 6338 | } |
| 6339 | return atLeastOneSignificantChange; |
| 6340 | } |
| 6341 | |
| 6342 | bool JoystickInputMapper::hasValueChangedSignificantly( |
| 6343 | float filter, float newValue, float currentValue, float min, float max) { |
| 6344 | if (newValue != currentValue) { |
| 6345 | // Filter out small changes in value unless the value is converging on the axis |
| 6346 | // bounds or center point. This is intended to reduce the amount of information |
| 6347 | // sent to applications by particularly noisy joysticks (such as PS3). |
| 6348 | if (fabs(newValue - currentValue) > filter |
| 6349 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min) |
| 6350 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max) |
| 6351 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) { |
| 6352 | return true; |
| 6353 | } |
| 6354 | } |
| 6355 | return false; |
| 6356 | } |
| 6357 | |
| 6358 | bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange( |
| 6359 | float filter, float newValue, float currentValue, float thresholdValue) { |
| 6360 | float newDistance = fabs(newValue - thresholdValue); |
| 6361 | if (newDistance < filter) { |
| 6362 | float oldDistance = fabs(currentValue - thresholdValue); |
| 6363 | if (newDistance < oldDistance) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6364 | return true; |
| 6365 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6366 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6367 | return false; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6368 | } |
| 6369 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 6370 | } // namespace android |