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> |
Jeff Brown | 9d3b1a4 | 2013-07-01 19:07:15 -0700 | [diff] [blame] | 45 | #include <input/Keyboard.h> |
| 46 | #include <input/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, " |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 885 | "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n", |
| 886 | name, range.source, range.min, range.max, range.flat, range.fuzz, |
| 887 | range.resolution); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 888 | } |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | size_t numMappers = mMappers.size(); |
| 892 | for (size_t i = 0; i < numMappers; i++) { |
| 893 | InputMapper* mapper = mMappers[i]; |
| 894 | mapper->dump(dump); |
| 895 | } |
| 896 | } |
| 897 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 898 | void InputDevice::addMapper(InputMapper* mapper) { |
| 899 | mMappers.add(mapper); |
| 900 | } |
| 901 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 902 | void InputDevice::configure(nsecs_t when, const InputReaderConfiguration* config, uint32_t changes) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 903 | mSources = 0; |
| 904 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 905 | if (!isIgnored()) { |
| 906 | if (!changes) { // first time only |
| 907 | mContext->getEventHub()->getConfiguration(mId, &mConfiguration); |
| 908 | } |
| 909 | |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 910 | if (!changes || (changes & InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUTS)) { |
Jeff Brown | 61c0824 | 2012-04-19 11:14:33 -0700 | [diff] [blame] | 911 | if (!(mClasses & INPUT_DEVICE_CLASS_VIRTUAL)) { |
| 912 | sp<KeyCharacterMap> keyboardLayout = |
| 913 | mContext->getPolicy()->getKeyboardLayoutOverlay(mIdentifier.descriptor); |
| 914 | if (mContext->getEventHub()->setKeyboardLayoutOverlay(mId, keyboardLayout)) { |
| 915 | bumpGeneration(); |
| 916 | } |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame] | 920 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DEVICE_ALIAS)) { |
| 921 | if (!(mClasses & INPUT_DEVICE_CLASS_VIRTUAL)) { |
| 922 | String8 alias = mContext->getPolicy()->getDeviceAlias(mIdentifier); |
| 923 | if (mAlias != alias) { |
| 924 | mAlias = alias; |
| 925 | bumpGeneration(); |
| 926 | } |
| 927 | } |
| 928 | } |
| 929 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 930 | size_t numMappers = mMappers.size(); |
| 931 | for (size_t i = 0; i < numMappers; i++) { |
| 932 | InputMapper* mapper = mMappers[i]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 933 | mapper->configure(when, config, changes); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 934 | mSources |= mapper->getSources(); |
| 935 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 936 | } |
| 937 | } |
| 938 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 939 | void InputDevice::reset(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 940 | size_t numMappers = mMappers.size(); |
| 941 | for (size_t i = 0; i < numMappers; i++) { |
| 942 | InputMapper* mapper = mMappers[i]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 943 | mapper->reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 944 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 945 | |
| 946 | mContext->updateGlobalMetaState(); |
| 947 | |
| 948 | notifyReset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 949 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 950 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 951 | void InputDevice::process(const RawEvent* rawEvents, size_t count) { |
| 952 | // Process all of the events in order for each mapper. |
| 953 | // We cannot simply ask each mapper to process them in bulk because mappers may |
| 954 | // have side-effects that must be interleaved. For example, joystick movement events and |
| 955 | // gamepad button presses are handled by different mappers but they should be dispatched |
| 956 | // in the order received. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 957 | size_t numMappers = mMappers.size(); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 958 | for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) { |
| 959 | #if DEBUG_RAW_EVENTS |
Jeff Brown | f33b2b2 | 2012-10-05 17:59:56 -0700 | [diff] [blame] | 960 | ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%lld", |
| 961 | rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value, |
| 962 | rawEvent->when); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 963 | #endif |
| 964 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 965 | if (mDropUntilNextSync) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 966 | if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 967 | mDropUntilNextSync = false; |
| 968 | #if DEBUG_RAW_EVENTS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 969 | ALOGD("Recovered from input event buffer overrun."); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 970 | #endif |
| 971 | } else { |
| 972 | #if DEBUG_RAW_EVENTS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 973 | ALOGD("Dropped input event while waiting for next input sync."); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 974 | #endif |
| 975 | } |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 976 | } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_DROPPED) { |
Jeff Brown | e38fdfa | 2012-04-06 14:51:01 -0700 | [diff] [blame] | 977 | ALOGI("Detected input event buffer overrun for device %s.", getName().string()); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 978 | mDropUntilNextSync = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 979 | reset(rawEvent->when); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 980 | } else { |
| 981 | for (size_t i = 0; i < numMappers; i++) { |
| 982 | InputMapper* mapper = mMappers[i]; |
| 983 | mapper->process(rawEvent); |
| 984 | } |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 985 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 986 | } |
| 987 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 988 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 989 | void InputDevice::timeoutExpired(nsecs_t when) { |
| 990 | size_t numMappers = mMappers.size(); |
| 991 | for (size_t i = 0; i < numMappers; i++) { |
| 992 | InputMapper* mapper = mMappers[i]; |
| 993 | mapper->timeoutExpired(when); |
| 994 | } |
| 995 | } |
| 996 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 997 | void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) { |
Jeff Brown | daa3753 | 2012-05-01 15:54:03 -0700 | [diff] [blame] | 998 | outDeviceInfo->initialize(mId, mGeneration, mIdentifier, mAlias, mIsExternal); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 999 | |
| 1000 | size_t numMappers = mMappers.size(); |
| 1001 | for (size_t i = 0; i < numMappers; i++) { |
| 1002 | InputMapper* mapper = mMappers[i]; |
| 1003 | mapper->populateDeviceInfo(outDeviceInfo); |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 1008 | return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState); |
| 1009 | } |
| 1010 | |
| 1011 | int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 1012 | return getState(sourceMask, scanCode, & InputMapper::getScanCodeState); |
| 1013 | } |
| 1014 | |
| 1015 | int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 1016 | return getState(sourceMask, switchCode, & InputMapper::getSwitchState); |
| 1017 | } |
| 1018 | |
| 1019 | int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) { |
| 1020 | int32_t result = AKEY_STATE_UNKNOWN; |
| 1021 | size_t numMappers = mMappers.size(); |
| 1022 | for (size_t i = 0; i < numMappers; i++) { |
| 1023 | InputMapper* mapper = mMappers[i]; |
| 1024 | if (sourcesMatchMask(mapper->getSources(), sourceMask)) { |
David Deephanphongs | fbca596 | 2011-11-14 14:50:45 -0800 | [diff] [blame] | 1025 | // If any mapper reports AKEY_STATE_DOWN or AKEY_STATE_VIRTUAL, return that |
| 1026 | // value. Otherwise, return AKEY_STATE_UP as long as one mapper reports it. |
| 1027 | int32_t currentResult = (mapper->*getStateFunc)(sourceMask, code); |
| 1028 | if (currentResult >= AKEY_STATE_DOWN) { |
| 1029 | return currentResult; |
| 1030 | } else if (currentResult == AKEY_STATE_UP) { |
| 1031 | result = currentResult; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1032 | } |
| 1033 | } |
| 1034 | } |
| 1035 | return result; |
| 1036 | } |
| 1037 | |
| 1038 | bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 1039 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 1040 | bool result = false; |
| 1041 | size_t numMappers = mMappers.size(); |
| 1042 | for (size_t i = 0; i < numMappers; i++) { |
| 1043 | InputMapper* mapper = mMappers[i]; |
| 1044 | if (sourcesMatchMask(mapper->getSources(), sourceMask)) { |
| 1045 | result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags); |
| 1046 | } |
| 1047 | } |
| 1048 | return result; |
| 1049 | } |
| 1050 | |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 1051 | void InputDevice::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, |
| 1052 | int32_t token) { |
| 1053 | size_t numMappers = mMappers.size(); |
| 1054 | for (size_t i = 0; i < numMappers; i++) { |
| 1055 | InputMapper* mapper = mMappers[i]; |
| 1056 | mapper->vibrate(pattern, patternSize, repeat, token); |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | void InputDevice::cancelVibrate(int32_t token) { |
| 1061 | size_t numMappers = mMappers.size(); |
| 1062 | for (size_t i = 0; i < numMappers; i++) { |
| 1063 | InputMapper* mapper = mMappers[i]; |
| 1064 | mapper->cancelVibrate(token); |
| 1065 | } |
| 1066 | } |
| 1067 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1068 | int32_t InputDevice::getMetaState() { |
| 1069 | int32_t result = 0; |
| 1070 | size_t numMappers = mMappers.size(); |
| 1071 | for (size_t i = 0; i < numMappers; i++) { |
| 1072 | InputMapper* mapper = mMappers[i]; |
| 1073 | result |= mapper->getMetaState(); |
| 1074 | } |
| 1075 | return result; |
| 1076 | } |
| 1077 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1078 | void InputDevice::fadePointer() { |
| 1079 | size_t numMappers = mMappers.size(); |
| 1080 | for (size_t i = 0; i < numMappers; i++) { |
| 1081 | InputMapper* mapper = mMappers[i]; |
| 1082 | mapper->fadePointer(); |
| 1083 | } |
| 1084 | } |
| 1085 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 1086 | void InputDevice::bumpGeneration() { |
| 1087 | mGeneration = mContext->bumpGeneration(); |
| 1088 | } |
| 1089 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1090 | void InputDevice::notifyReset(nsecs_t when) { |
| 1091 | NotifyDeviceResetArgs args(when, mId); |
| 1092 | mContext->getListener()->notifyDeviceReset(&args); |
| 1093 | } |
| 1094 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1095 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1096 | // --- CursorButtonAccumulator --- |
| 1097 | |
| 1098 | CursorButtonAccumulator::CursorButtonAccumulator() { |
| 1099 | clearButtons(); |
| 1100 | } |
| 1101 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1102 | void CursorButtonAccumulator::reset(InputDevice* device) { |
| 1103 | mBtnLeft = device->isKeyPressed(BTN_LEFT); |
| 1104 | mBtnRight = device->isKeyPressed(BTN_RIGHT); |
| 1105 | mBtnMiddle = device->isKeyPressed(BTN_MIDDLE); |
| 1106 | mBtnBack = device->isKeyPressed(BTN_BACK); |
| 1107 | mBtnSide = device->isKeyPressed(BTN_SIDE); |
| 1108 | mBtnForward = device->isKeyPressed(BTN_FORWARD); |
| 1109 | mBtnExtra = device->isKeyPressed(BTN_EXTRA); |
| 1110 | mBtnTask = device->isKeyPressed(BTN_TASK); |
| 1111 | } |
| 1112 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1113 | void CursorButtonAccumulator::clearButtons() { |
| 1114 | mBtnLeft = 0; |
| 1115 | mBtnRight = 0; |
| 1116 | mBtnMiddle = 0; |
| 1117 | mBtnBack = 0; |
| 1118 | mBtnSide = 0; |
| 1119 | mBtnForward = 0; |
| 1120 | mBtnExtra = 0; |
| 1121 | mBtnTask = 0; |
| 1122 | } |
| 1123 | |
| 1124 | void CursorButtonAccumulator::process(const RawEvent* rawEvent) { |
| 1125 | if (rawEvent->type == EV_KEY) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1126 | switch (rawEvent->code) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1127 | case BTN_LEFT: |
| 1128 | mBtnLeft = rawEvent->value; |
| 1129 | break; |
| 1130 | case BTN_RIGHT: |
| 1131 | mBtnRight = rawEvent->value; |
| 1132 | break; |
| 1133 | case BTN_MIDDLE: |
| 1134 | mBtnMiddle = rawEvent->value; |
| 1135 | break; |
| 1136 | case BTN_BACK: |
| 1137 | mBtnBack = rawEvent->value; |
| 1138 | break; |
| 1139 | case BTN_SIDE: |
| 1140 | mBtnSide = rawEvent->value; |
| 1141 | break; |
| 1142 | case BTN_FORWARD: |
| 1143 | mBtnForward = rawEvent->value; |
| 1144 | break; |
| 1145 | case BTN_EXTRA: |
| 1146 | mBtnExtra = rawEvent->value; |
| 1147 | break; |
| 1148 | case BTN_TASK: |
| 1149 | mBtnTask = rawEvent->value; |
| 1150 | break; |
| 1151 | } |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | uint32_t CursorButtonAccumulator::getButtonState() const { |
| 1156 | uint32_t result = 0; |
| 1157 | if (mBtnLeft) { |
| 1158 | result |= AMOTION_EVENT_BUTTON_PRIMARY; |
| 1159 | } |
| 1160 | if (mBtnRight) { |
| 1161 | result |= AMOTION_EVENT_BUTTON_SECONDARY; |
| 1162 | } |
| 1163 | if (mBtnMiddle) { |
| 1164 | result |= AMOTION_EVENT_BUTTON_TERTIARY; |
| 1165 | } |
| 1166 | if (mBtnBack || mBtnSide) { |
| 1167 | result |= AMOTION_EVENT_BUTTON_BACK; |
| 1168 | } |
| 1169 | if (mBtnForward || mBtnExtra) { |
| 1170 | result |= AMOTION_EVENT_BUTTON_FORWARD; |
| 1171 | } |
| 1172 | return result; |
| 1173 | } |
| 1174 | |
| 1175 | |
| 1176 | // --- CursorMotionAccumulator --- |
| 1177 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1178 | CursorMotionAccumulator::CursorMotionAccumulator() { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1179 | clearRelativeAxes(); |
| 1180 | } |
| 1181 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1182 | void CursorMotionAccumulator::reset(InputDevice* device) { |
| 1183 | clearRelativeAxes(); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | void CursorMotionAccumulator::clearRelativeAxes() { |
| 1187 | mRelX = 0; |
| 1188 | mRelY = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | void CursorMotionAccumulator::process(const RawEvent* rawEvent) { |
| 1192 | if (rawEvent->type == EV_REL) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1193 | switch (rawEvent->code) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1194 | case REL_X: |
| 1195 | mRelX = rawEvent->value; |
| 1196 | break; |
| 1197 | case REL_Y: |
| 1198 | mRelY = rawEvent->value; |
| 1199 | break; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | void CursorMotionAccumulator::finishSync() { |
| 1205 | clearRelativeAxes(); |
| 1206 | } |
| 1207 | |
| 1208 | |
| 1209 | // --- CursorScrollAccumulator --- |
| 1210 | |
| 1211 | CursorScrollAccumulator::CursorScrollAccumulator() : |
| 1212 | mHaveRelWheel(false), mHaveRelHWheel(false) { |
| 1213 | clearRelativeAxes(); |
| 1214 | } |
| 1215 | |
| 1216 | void CursorScrollAccumulator::configure(InputDevice* device) { |
| 1217 | mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL); |
| 1218 | mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL); |
| 1219 | } |
| 1220 | |
| 1221 | void CursorScrollAccumulator::reset(InputDevice* device) { |
| 1222 | clearRelativeAxes(); |
| 1223 | } |
| 1224 | |
| 1225 | void CursorScrollAccumulator::clearRelativeAxes() { |
| 1226 | mRelWheel = 0; |
| 1227 | mRelHWheel = 0; |
| 1228 | } |
| 1229 | |
| 1230 | void CursorScrollAccumulator::process(const RawEvent* rawEvent) { |
| 1231 | if (rawEvent->type == EV_REL) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1232 | switch (rawEvent->code) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1233 | case REL_WHEEL: |
| 1234 | mRelWheel = rawEvent->value; |
| 1235 | break; |
| 1236 | case REL_HWHEEL: |
| 1237 | mRelHWheel = rawEvent->value; |
| 1238 | break; |
| 1239 | } |
| 1240 | } |
| 1241 | } |
| 1242 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1243 | void CursorScrollAccumulator::finishSync() { |
| 1244 | clearRelativeAxes(); |
| 1245 | } |
| 1246 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1247 | |
| 1248 | // --- TouchButtonAccumulator --- |
| 1249 | |
| 1250 | TouchButtonAccumulator::TouchButtonAccumulator() : |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 1251 | mHaveBtnTouch(false), mHaveStylus(false) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1252 | clearButtons(); |
| 1253 | } |
| 1254 | |
| 1255 | void TouchButtonAccumulator::configure(InputDevice* device) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1256 | mHaveBtnTouch = device->hasKey(BTN_TOUCH); |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 1257 | mHaveStylus = device->hasKey(BTN_TOOL_PEN) |
| 1258 | || device->hasKey(BTN_TOOL_RUBBER) |
| 1259 | || device->hasKey(BTN_TOOL_BRUSH) |
| 1260 | || device->hasKey(BTN_TOOL_PENCIL) |
| 1261 | || device->hasKey(BTN_TOOL_AIRBRUSH); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | void TouchButtonAccumulator::reset(InputDevice* device) { |
| 1265 | mBtnTouch = device->isKeyPressed(BTN_TOUCH); |
| 1266 | mBtnStylus = device->isKeyPressed(BTN_STYLUS); |
| 1267 | mBtnStylus2 = device->isKeyPressed(BTN_STYLUS); |
| 1268 | mBtnToolFinger = device->isKeyPressed(BTN_TOOL_FINGER); |
| 1269 | mBtnToolPen = device->isKeyPressed(BTN_TOOL_PEN); |
| 1270 | mBtnToolRubber = device->isKeyPressed(BTN_TOOL_RUBBER); |
| 1271 | mBtnToolBrush = device->isKeyPressed(BTN_TOOL_BRUSH); |
| 1272 | mBtnToolPencil = device->isKeyPressed(BTN_TOOL_PENCIL); |
| 1273 | mBtnToolAirbrush = device->isKeyPressed(BTN_TOOL_AIRBRUSH); |
| 1274 | mBtnToolMouse = device->isKeyPressed(BTN_TOOL_MOUSE); |
| 1275 | mBtnToolLens = device->isKeyPressed(BTN_TOOL_LENS); |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1276 | mBtnToolDoubleTap = device->isKeyPressed(BTN_TOOL_DOUBLETAP); |
| 1277 | mBtnToolTripleTap = device->isKeyPressed(BTN_TOOL_TRIPLETAP); |
| 1278 | mBtnToolQuadTap = device->isKeyPressed(BTN_TOOL_QUADTAP); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | void TouchButtonAccumulator::clearButtons() { |
| 1282 | mBtnTouch = 0; |
| 1283 | mBtnStylus = 0; |
| 1284 | mBtnStylus2 = 0; |
| 1285 | mBtnToolFinger = 0; |
| 1286 | mBtnToolPen = 0; |
| 1287 | mBtnToolRubber = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1288 | mBtnToolBrush = 0; |
| 1289 | mBtnToolPencil = 0; |
| 1290 | mBtnToolAirbrush = 0; |
| 1291 | mBtnToolMouse = 0; |
| 1292 | mBtnToolLens = 0; |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1293 | mBtnToolDoubleTap = 0; |
| 1294 | mBtnToolTripleTap = 0; |
| 1295 | mBtnToolQuadTap = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | void TouchButtonAccumulator::process(const RawEvent* rawEvent) { |
| 1299 | if (rawEvent->type == EV_KEY) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1300 | switch (rawEvent->code) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1301 | case BTN_TOUCH: |
| 1302 | mBtnTouch = rawEvent->value; |
| 1303 | break; |
| 1304 | case BTN_STYLUS: |
| 1305 | mBtnStylus = rawEvent->value; |
| 1306 | break; |
| 1307 | case BTN_STYLUS2: |
| 1308 | mBtnStylus2 = rawEvent->value; |
| 1309 | break; |
| 1310 | case BTN_TOOL_FINGER: |
| 1311 | mBtnToolFinger = rawEvent->value; |
| 1312 | break; |
| 1313 | case BTN_TOOL_PEN: |
| 1314 | mBtnToolPen = rawEvent->value; |
| 1315 | break; |
| 1316 | case BTN_TOOL_RUBBER: |
| 1317 | mBtnToolRubber = rawEvent->value; |
| 1318 | break; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1319 | case BTN_TOOL_BRUSH: |
| 1320 | mBtnToolBrush = rawEvent->value; |
| 1321 | break; |
| 1322 | case BTN_TOOL_PENCIL: |
| 1323 | mBtnToolPencil = rawEvent->value; |
| 1324 | break; |
| 1325 | case BTN_TOOL_AIRBRUSH: |
| 1326 | mBtnToolAirbrush = rawEvent->value; |
| 1327 | break; |
| 1328 | case BTN_TOOL_MOUSE: |
| 1329 | mBtnToolMouse = rawEvent->value; |
| 1330 | break; |
| 1331 | case BTN_TOOL_LENS: |
| 1332 | mBtnToolLens = rawEvent->value; |
| 1333 | break; |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1334 | case BTN_TOOL_DOUBLETAP: |
| 1335 | mBtnToolDoubleTap = rawEvent->value; |
| 1336 | break; |
| 1337 | case BTN_TOOL_TRIPLETAP: |
| 1338 | mBtnToolTripleTap = rawEvent->value; |
| 1339 | break; |
| 1340 | case BTN_TOOL_QUADTAP: |
| 1341 | mBtnToolQuadTap = rawEvent->value; |
| 1342 | break; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1343 | } |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | uint32_t TouchButtonAccumulator::getButtonState() const { |
| 1348 | uint32_t result = 0; |
| 1349 | if (mBtnStylus) { |
| 1350 | result |= AMOTION_EVENT_BUTTON_SECONDARY; |
| 1351 | } |
| 1352 | if (mBtnStylus2) { |
| 1353 | result |= AMOTION_EVENT_BUTTON_TERTIARY; |
| 1354 | } |
| 1355 | return result; |
| 1356 | } |
| 1357 | |
| 1358 | int32_t TouchButtonAccumulator::getToolType() const { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1359 | if (mBtnToolMouse || mBtnToolLens) { |
| 1360 | return AMOTION_EVENT_TOOL_TYPE_MOUSE; |
| 1361 | } |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1362 | if (mBtnToolRubber) { |
| 1363 | return AMOTION_EVENT_TOOL_TYPE_ERASER; |
| 1364 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1365 | if (mBtnToolPen || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1366 | return AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 1367 | } |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1368 | if (mBtnToolFinger || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1369 | return AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 1370 | } |
| 1371 | return AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
| 1372 | } |
| 1373 | |
Jeff Brown | d87c6d5 | 2011-08-10 14:55:59 -0700 | [diff] [blame] | 1374 | bool TouchButtonAccumulator::isToolActive() const { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1375 | return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber |
| 1376 | || mBtnToolBrush || mBtnToolPencil || mBtnToolAirbrush |
Jeff Brown | ea6892e | 2011-08-23 17:31:25 -0700 | [diff] [blame] | 1377 | || mBtnToolMouse || mBtnToolLens |
| 1378 | || mBtnToolDoubleTap || mBtnToolTripleTap || mBtnToolQuadTap; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | bool TouchButtonAccumulator::isHovering() const { |
| 1382 | return mHaveBtnTouch && !mBtnTouch; |
| 1383 | } |
| 1384 | |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 1385 | bool TouchButtonAccumulator::hasStylus() const { |
| 1386 | return mHaveStylus; |
| 1387 | } |
| 1388 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1389 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1390 | // --- RawPointerAxes --- |
| 1391 | |
| 1392 | RawPointerAxes::RawPointerAxes() { |
| 1393 | clear(); |
| 1394 | } |
| 1395 | |
| 1396 | void RawPointerAxes::clear() { |
| 1397 | x.clear(); |
| 1398 | y.clear(); |
| 1399 | pressure.clear(); |
| 1400 | touchMajor.clear(); |
| 1401 | touchMinor.clear(); |
| 1402 | toolMajor.clear(); |
| 1403 | toolMinor.clear(); |
| 1404 | orientation.clear(); |
| 1405 | distance.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1406 | tiltX.clear(); |
| 1407 | tiltY.clear(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1408 | trackingId.clear(); |
| 1409 | slot.clear(); |
| 1410 | } |
| 1411 | |
| 1412 | |
| 1413 | // --- RawPointerData --- |
| 1414 | |
| 1415 | RawPointerData::RawPointerData() { |
| 1416 | clear(); |
| 1417 | } |
| 1418 | |
| 1419 | void RawPointerData::clear() { |
| 1420 | pointerCount = 0; |
| 1421 | clearIdBits(); |
| 1422 | } |
| 1423 | |
| 1424 | void RawPointerData::copyFrom(const RawPointerData& other) { |
| 1425 | pointerCount = other.pointerCount; |
| 1426 | hoveringIdBits = other.hoveringIdBits; |
| 1427 | touchingIdBits = other.touchingIdBits; |
| 1428 | |
| 1429 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 1430 | pointers[i] = other.pointers[i]; |
| 1431 | |
| 1432 | int id = pointers[i].id; |
| 1433 | idToIndex[id] = other.idToIndex[id]; |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const { |
| 1438 | float x = 0, y = 0; |
| 1439 | uint32_t count = touchingIdBits.count(); |
| 1440 | if (count) { |
| 1441 | for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) { |
| 1442 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 1443 | const Pointer& pointer = pointerForId(id); |
| 1444 | x += pointer.x; |
| 1445 | y += pointer.y; |
| 1446 | } |
| 1447 | x /= count; |
| 1448 | y /= count; |
| 1449 | } |
| 1450 | *outX = x; |
| 1451 | *outY = y; |
| 1452 | } |
| 1453 | |
| 1454 | |
| 1455 | // --- CookedPointerData --- |
| 1456 | |
| 1457 | CookedPointerData::CookedPointerData() { |
| 1458 | clear(); |
| 1459 | } |
| 1460 | |
| 1461 | void CookedPointerData::clear() { |
| 1462 | pointerCount = 0; |
| 1463 | hoveringIdBits.clear(); |
| 1464 | touchingIdBits.clear(); |
| 1465 | } |
| 1466 | |
| 1467 | void CookedPointerData::copyFrom(const CookedPointerData& other) { |
| 1468 | pointerCount = other.pointerCount; |
| 1469 | hoveringIdBits = other.hoveringIdBits; |
| 1470 | touchingIdBits = other.touchingIdBits; |
| 1471 | |
| 1472 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 1473 | pointerProperties[i].copyFrom(other.pointerProperties[i]); |
| 1474 | pointerCoords[i].copyFrom(other.pointerCoords[i]); |
| 1475 | |
| 1476 | int id = pointerProperties[i].id; |
| 1477 | idToIndex[id] = other.idToIndex[id]; |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1482 | // --- SingleTouchMotionAccumulator --- |
| 1483 | |
| 1484 | SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() { |
| 1485 | clearAbsoluteAxes(); |
| 1486 | } |
| 1487 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1488 | void SingleTouchMotionAccumulator::reset(InputDevice* device) { |
| 1489 | mAbsX = device->getAbsoluteAxisValue(ABS_X); |
| 1490 | mAbsY = device->getAbsoluteAxisValue(ABS_Y); |
| 1491 | mAbsPressure = device->getAbsoluteAxisValue(ABS_PRESSURE); |
| 1492 | mAbsToolWidth = device->getAbsoluteAxisValue(ABS_TOOL_WIDTH); |
| 1493 | mAbsDistance = device->getAbsoluteAxisValue(ABS_DISTANCE); |
| 1494 | mAbsTiltX = device->getAbsoluteAxisValue(ABS_TILT_X); |
| 1495 | mAbsTiltY = device->getAbsoluteAxisValue(ABS_TILT_Y); |
| 1496 | } |
| 1497 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1498 | void SingleTouchMotionAccumulator::clearAbsoluteAxes() { |
| 1499 | mAbsX = 0; |
| 1500 | mAbsY = 0; |
| 1501 | mAbsPressure = 0; |
| 1502 | mAbsToolWidth = 0; |
| 1503 | mAbsDistance = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1504 | mAbsTiltX = 0; |
| 1505 | mAbsTiltY = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) { |
| 1509 | if (rawEvent->type == EV_ABS) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1510 | switch (rawEvent->code) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1511 | case ABS_X: |
| 1512 | mAbsX = rawEvent->value; |
| 1513 | break; |
| 1514 | case ABS_Y: |
| 1515 | mAbsY = rawEvent->value; |
| 1516 | break; |
| 1517 | case ABS_PRESSURE: |
| 1518 | mAbsPressure = rawEvent->value; |
| 1519 | break; |
| 1520 | case ABS_TOOL_WIDTH: |
| 1521 | mAbsToolWidth = rawEvent->value; |
| 1522 | break; |
| 1523 | case ABS_DISTANCE: |
| 1524 | mAbsDistance = rawEvent->value; |
| 1525 | break; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1526 | case ABS_TILT_X: |
| 1527 | mAbsTiltX = rawEvent->value; |
| 1528 | break; |
| 1529 | case ABS_TILT_Y: |
| 1530 | mAbsTiltY = rawEvent->value; |
| 1531 | break; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1532 | } |
| 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | |
| 1537 | // --- MultiTouchMotionAccumulator --- |
| 1538 | |
| 1539 | MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() : |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 1540 | mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false), |
| 1541 | mHaveStylus(false) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() { |
| 1545 | delete[] mSlots; |
| 1546 | } |
| 1547 | |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 1548 | void MultiTouchMotionAccumulator::configure(InputDevice* device, |
| 1549 | size_t slotCount, bool usingSlotsProtocol) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1550 | mSlotCount = slotCount; |
| 1551 | mUsingSlotsProtocol = usingSlotsProtocol; |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 1552 | mHaveStylus = device->hasAbsoluteAxis(ABS_MT_TOOL_TYPE); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1553 | |
| 1554 | delete[] mSlots; |
| 1555 | mSlots = new Slot[slotCount]; |
| 1556 | } |
| 1557 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1558 | void MultiTouchMotionAccumulator::reset(InputDevice* device) { |
| 1559 | // Unfortunately there is no way to read the initial contents of the slots. |
| 1560 | // So when we reset the accumulator, we must assume they are all zeroes. |
| 1561 | if (mUsingSlotsProtocol) { |
| 1562 | // Query the driver for the current slot index and use it as the initial slot |
| 1563 | // before we start reading events from the device. It is possible that the |
| 1564 | // current slot index will not be the same as it was when the first event was |
| 1565 | // written into the evdev buffer, which means the input mapper could start |
| 1566 | // out of sync with the initial state of the events in the evdev buffer. |
| 1567 | // In the extremely unlikely case that this happens, the data from |
| 1568 | // two slots will be confused until the next ABS_MT_SLOT event is received. |
| 1569 | // This can cause the touch point to "jump", but at least there will be |
| 1570 | // no stuck touches. |
| 1571 | int32_t initialSlot; |
| 1572 | status_t status = device->getEventHub()->getAbsoluteAxisValue(device->getId(), |
| 1573 | ABS_MT_SLOT, &initialSlot); |
| 1574 | if (status) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1575 | ALOGD("Could not retrieve current multitouch slot index. status=%d", status); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1576 | initialSlot = -1; |
| 1577 | } |
| 1578 | clearSlots(initialSlot); |
| 1579 | } else { |
| 1580 | clearSlots(-1); |
| 1581 | } |
| 1582 | } |
| 1583 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1584 | void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1585 | if (mSlots) { |
| 1586 | for (size_t i = 0; i < mSlotCount; i++) { |
| 1587 | mSlots[i].clear(); |
| 1588 | } |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1589 | } |
| 1590 | mCurrentSlot = initialSlot; |
| 1591 | } |
| 1592 | |
| 1593 | void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) { |
| 1594 | if (rawEvent->type == EV_ABS) { |
| 1595 | bool newSlot = false; |
| 1596 | if (mUsingSlotsProtocol) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1597 | if (rawEvent->code == ABS_MT_SLOT) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1598 | mCurrentSlot = rawEvent->value; |
| 1599 | newSlot = true; |
| 1600 | } |
| 1601 | } else if (mCurrentSlot < 0) { |
| 1602 | mCurrentSlot = 0; |
| 1603 | } |
| 1604 | |
| 1605 | if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) { |
| 1606 | #if DEBUG_POINTERS |
| 1607 | if (newSlot) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1608 | ALOGW("MultiTouch device emitted invalid slot index %d but it " |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1609 | "should be between 0 and %d; ignoring this slot.", |
| 1610 | mCurrentSlot, mSlotCount - 1); |
| 1611 | } |
| 1612 | #endif |
| 1613 | } else { |
| 1614 | Slot* slot = &mSlots[mCurrentSlot]; |
| 1615 | |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1616 | switch (rawEvent->code) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1617 | case ABS_MT_POSITION_X: |
| 1618 | slot->mInUse = true; |
| 1619 | slot->mAbsMTPositionX = rawEvent->value; |
| 1620 | break; |
| 1621 | case ABS_MT_POSITION_Y: |
| 1622 | slot->mInUse = true; |
| 1623 | slot->mAbsMTPositionY = rawEvent->value; |
| 1624 | break; |
| 1625 | case ABS_MT_TOUCH_MAJOR: |
| 1626 | slot->mInUse = true; |
| 1627 | slot->mAbsMTTouchMajor = rawEvent->value; |
| 1628 | break; |
| 1629 | case ABS_MT_TOUCH_MINOR: |
| 1630 | slot->mInUse = true; |
| 1631 | slot->mAbsMTTouchMinor = rawEvent->value; |
| 1632 | slot->mHaveAbsMTTouchMinor = true; |
| 1633 | break; |
| 1634 | case ABS_MT_WIDTH_MAJOR: |
| 1635 | slot->mInUse = true; |
| 1636 | slot->mAbsMTWidthMajor = rawEvent->value; |
| 1637 | break; |
| 1638 | case ABS_MT_WIDTH_MINOR: |
| 1639 | slot->mInUse = true; |
| 1640 | slot->mAbsMTWidthMinor = rawEvent->value; |
| 1641 | slot->mHaveAbsMTWidthMinor = true; |
| 1642 | break; |
| 1643 | case ABS_MT_ORIENTATION: |
| 1644 | slot->mInUse = true; |
| 1645 | slot->mAbsMTOrientation = rawEvent->value; |
| 1646 | break; |
| 1647 | case ABS_MT_TRACKING_ID: |
| 1648 | if (mUsingSlotsProtocol && rawEvent->value < 0) { |
Jeff Brown | 8bcbbef | 2011-08-11 15:49:09 -0700 | [diff] [blame] | 1649 | // The slot is no longer in use but it retains its previous contents, |
| 1650 | // which may be reused for subsequent touches. |
| 1651 | slot->mInUse = false; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1652 | } else { |
| 1653 | slot->mInUse = true; |
| 1654 | slot->mAbsMTTrackingId = rawEvent->value; |
| 1655 | } |
| 1656 | break; |
| 1657 | case ABS_MT_PRESSURE: |
| 1658 | slot->mInUse = true; |
| 1659 | slot->mAbsMTPressure = rawEvent->value; |
| 1660 | break; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1661 | case ABS_MT_DISTANCE: |
| 1662 | slot->mInUse = true; |
| 1663 | slot->mAbsMTDistance = rawEvent->value; |
| 1664 | break; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1665 | case ABS_MT_TOOL_TYPE: |
| 1666 | slot->mInUse = true; |
| 1667 | slot->mAbsMTToolType = rawEvent->value; |
| 1668 | slot->mHaveAbsMTToolType = true; |
| 1669 | break; |
| 1670 | } |
| 1671 | } |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 1672 | } else if (rawEvent->type == EV_SYN && rawEvent->code == SYN_MT_REPORT) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1673 | // MultiTouch Sync: The driver has returned all data for *one* of the pointers. |
| 1674 | mCurrentSlot += 1; |
| 1675 | } |
| 1676 | } |
| 1677 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1678 | void MultiTouchMotionAccumulator::finishSync() { |
| 1679 | if (!mUsingSlotsProtocol) { |
| 1680 | clearSlots(-1); |
| 1681 | } |
| 1682 | } |
| 1683 | |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 1684 | bool MultiTouchMotionAccumulator::hasStylus() const { |
| 1685 | return mHaveStylus; |
| 1686 | } |
| 1687 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1688 | |
| 1689 | // --- MultiTouchMotionAccumulator::Slot --- |
| 1690 | |
| 1691 | MultiTouchMotionAccumulator::Slot::Slot() { |
| 1692 | clear(); |
| 1693 | } |
| 1694 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1695 | void MultiTouchMotionAccumulator::Slot::clear() { |
| 1696 | mInUse = false; |
| 1697 | mHaveAbsMTTouchMinor = false; |
| 1698 | mHaveAbsMTWidthMinor = false; |
| 1699 | mHaveAbsMTToolType = false; |
| 1700 | mAbsMTPositionX = 0; |
| 1701 | mAbsMTPositionY = 0; |
| 1702 | mAbsMTTouchMajor = 0; |
| 1703 | mAbsMTTouchMinor = 0; |
| 1704 | mAbsMTWidthMajor = 0; |
| 1705 | mAbsMTWidthMinor = 0; |
| 1706 | mAbsMTOrientation = 0; |
| 1707 | mAbsMTTrackingId = -1; |
| 1708 | mAbsMTPressure = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1709 | mAbsMTDistance = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1710 | mAbsMTToolType = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | int32_t MultiTouchMotionAccumulator::Slot::getToolType() const { |
| 1714 | if (mHaveAbsMTToolType) { |
| 1715 | switch (mAbsMTToolType) { |
| 1716 | case MT_TOOL_FINGER: |
| 1717 | return AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 1718 | case MT_TOOL_PEN: |
| 1719 | return AMOTION_EVENT_TOOL_TYPE_STYLUS; |
| 1720 | } |
| 1721 | } |
| 1722 | return AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
| 1723 | } |
| 1724 | |
| 1725 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1726 | // --- InputMapper --- |
| 1727 | |
| 1728 | InputMapper::InputMapper(InputDevice* device) : |
| 1729 | mDevice(device), mContext(device->getContext()) { |
| 1730 | } |
| 1731 | |
| 1732 | InputMapper::~InputMapper() { |
| 1733 | } |
| 1734 | |
| 1735 | void InputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 1736 | info->addSource(getSources()); |
| 1737 | } |
| 1738 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1739 | void InputMapper::dump(String8& dump) { |
| 1740 | } |
| 1741 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1742 | void InputMapper::configure(nsecs_t when, |
| 1743 | const InputReaderConfiguration* config, uint32_t changes) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1744 | } |
| 1745 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1746 | void InputMapper::reset(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1747 | } |
| 1748 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 1749 | void InputMapper::timeoutExpired(nsecs_t when) { |
| 1750 | } |
| 1751 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1752 | int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 1753 | return AKEY_STATE_UNKNOWN; |
| 1754 | } |
| 1755 | |
| 1756 | int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 1757 | return AKEY_STATE_UNKNOWN; |
| 1758 | } |
| 1759 | |
| 1760 | int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 1761 | return AKEY_STATE_UNKNOWN; |
| 1762 | } |
| 1763 | |
| 1764 | bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 1765 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 1766 | return false; |
| 1767 | } |
| 1768 | |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 1769 | void InputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, |
| 1770 | int32_t token) { |
| 1771 | } |
| 1772 | |
| 1773 | void InputMapper::cancelVibrate(int32_t token) { |
| 1774 | } |
| 1775 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1776 | int32_t InputMapper::getMetaState() { |
| 1777 | return 0; |
| 1778 | } |
| 1779 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1780 | void InputMapper::fadePointer() { |
| 1781 | } |
| 1782 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1783 | status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) { |
| 1784 | return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo); |
| 1785 | } |
| 1786 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 1787 | void InputMapper::bumpGeneration() { |
| 1788 | mDevice->bumpGeneration(); |
| 1789 | } |
| 1790 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1791 | void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump, |
| 1792 | const RawAbsoluteAxisInfo& axis, const char* name) { |
| 1793 | if (axis.valid) { |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 1794 | dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n", |
| 1795 | name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1796 | } else { |
| 1797 | dump.appendFormat(INDENT4 "%s: unknown range\n", name); |
| 1798 | } |
| 1799 | } |
| 1800 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1801 | |
| 1802 | // --- SwitchInputMapper --- |
| 1803 | |
| 1804 | SwitchInputMapper::SwitchInputMapper(InputDevice* device) : |
Jeff Brown | bcc046a | 2012-09-27 20:46:43 -0700 | [diff] [blame] | 1805 | InputMapper(device), mUpdatedSwitchValues(0), mUpdatedSwitchMask(0) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1806 | } |
| 1807 | |
| 1808 | SwitchInputMapper::~SwitchInputMapper() { |
| 1809 | } |
| 1810 | |
| 1811 | uint32_t SwitchInputMapper::getSources() { |
Jeff Brown | 89de57a | 2011-01-19 18:41:38 -0800 | [diff] [blame] | 1812 | return AINPUT_SOURCE_SWITCH; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | void SwitchInputMapper::process(const RawEvent* rawEvent) { |
| 1816 | switch (rawEvent->type) { |
| 1817 | case EV_SW: |
Jeff Brown | bcc046a | 2012-09-27 20:46:43 -0700 | [diff] [blame] | 1818 | processSwitch(rawEvent->code, rawEvent->value); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1819 | break; |
Jeff Brown | bcc046a | 2012-09-27 20:46:43 -0700 | [diff] [blame] | 1820 | |
| 1821 | case EV_SYN: |
| 1822 | if (rawEvent->code == SYN_REPORT) { |
| 1823 | sync(rawEvent->when); |
| 1824 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1825 | } |
| 1826 | } |
| 1827 | |
Jeff Brown | bcc046a | 2012-09-27 20:46:43 -0700 | [diff] [blame] | 1828 | void SwitchInputMapper::processSwitch(int32_t switchCode, int32_t switchValue) { |
| 1829 | if (switchCode >= 0 && switchCode < 32) { |
| 1830 | if (switchValue) { |
| 1831 | mUpdatedSwitchValues |= 1 << switchCode; |
| 1832 | } |
| 1833 | mUpdatedSwitchMask |= 1 << switchCode; |
| 1834 | } |
| 1835 | } |
| 1836 | |
| 1837 | void SwitchInputMapper::sync(nsecs_t when) { |
| 1838 | if (mUpdatedSwitchMask) { |
| 1839 | NotifySwitchArgs args(when, 0, mUpdatedSwitchValues, mUpdatedSwitchMask); |
| 1840 | getListener()->notifySwitch(&args); |
| 1841 | |
| 1842 | mUpdatedSwitchValues = 0; |
| 1843 | mUpdatedSwitchMask = 0; |
| 1844 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 1848 | return getEventHub()->getSwitchState(getDeviceId(), switchCode); |
| 1849 | } |
| 1850 | |
| 1851 | |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 1852 | // --- VibratorInputMapper --- |
| 1853 | |
| 1854 | VibratorInputMapper::VibratorInputMapper(InputDevice* device) : |
| 1855 | InputMapper(device), mVibrating(false) { |
| 1856 | } |
| 1857 | |
| 1858 | VibratorInputMapper::~VibratorInputMapper() { |
| 1859 | } |
| 1860 | |
| 1861 | uint32_t VibratorInputMapper::getSources() { |
| 1862 | return 0; |
| 1863 | } |
| 1864 | |
| 1865 | void VibratorInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 1866 | InputMapper::populateDeviceInfo(info); |
| 1867 | |
| 1868 | info->setVibrator(true); |
| 1869 | } |
| 1870 | |
| 1871 | void VibratorInputMapper::process(const RawEvent* rawEvent) { |
| 1872 | // TODO: Handle FF_STATUS, although it does not seem to be widely supported. |
| 1873 | } |
| 1874 | |
| 1875 | void VibratorInputMapper::vibrate(const nsecs_t* pattern, size_t patternSize, ssize_t repeat, |
| 1876 | int32_t token) { |
| 1877 | #if DEBUG_VIBRATOR |
| 1878 | String8 patternStr; |
| 1879 | for (size_t i = 0; i < patternSize; i++) { |
| 1880 | if (i != 0) { |
| 1881 | patternStr.append(", "); |
| 1882 | } |
| 1883 | patternStr.appendFormat("%lld", pattern[i]); |
| 1884 | } |
| 1885 | ALOGD("vibrate: deviceId=%d, pattern=[%s], repeat=%ld, token=%d", |
| 1886 | getDeviceId(), patternStr.string(), repeat, token); |
| 1887 | #endif |
| 1888 | |
| 1889 | mVibrating = true; |
| 1890 | memcpy(mPattern, pattern, patternSize * sizeof(nsecs_t)); |
| 1891 | mPatternSize = patternSize; |
| 1892 | mRepeat = repeat; |
| 1893 | mToken = token; |
| 1894 | mIndex = -1; |
| 1895 | |
| 1896 | nextStep(); |
| 1897 | } |
| 1898 | |
| 1899 | void VibratorInputMapper::cancelVibrate(int32_t token) { |
| 1900 | #if DEBUG_VIBRATOR |
| 1901 | ALOGD("cancelVibrate: deviceId=%d, token=%d", getDeviceId(), token); |
| 1902 | #endif |
| 1903 | |
| 1904 | if (mVibrating && mToken == token) { |
| 1905 | stopVibrating(); |
| 1906 | } |
| 1907 | } |
| 1908 | |
| 1909 | void VibratorInputMapper::timeoutExpired(nsecs_t when) { |
| 1910 | if (mVibrating) { |
| 1911 | if (when >= mNextStepTime) { |
| 1912 | nextStep(); |
| 1913 | } else { |
| 1914 | getContext()->requestTimeoutAtTime(mNextStepTime); |
| 1915 | } |
| 1916 | } |
| 1917 | } |
| 1918 | |
| 1919 | void VibratorInputMapper::nextStep() { |
| 1920 | mIndex += 1; |
| 1921 | if (size_t(mIndex) >= mPatternSize) { |
| 1922 | if (mRepeat < 0) { |
| 1923 | // We are done. |
| 1924 | stopVibrating(); |
| 1925 | return; |
| 1926 | } |
| 1927 | mIndex = mRepeat; |
| 1928 | } |
| 1929 | |
| 1930 | bool vibratorOn = mIndex & 1; |
| 1931 | nsecs_t duration = mPattern[mIndex]; |
| 1932 | if (vibratorOn) { |
| 1933 | #if DEBUG_VIBRATOR |
| 1934 | ALOGD("nextStep: sending vibrate deviceId=%d, duration=%lld", |
| 1935 | getDeviceId(), duration); |
| 1936 | #endif |
| 1937 | getEventHub()->vibrate(getDeviceId(), duration); |
| 1938 | } else { |
| 1939 | #if DEBUG_VIBRATOR |
| 1940 | ALOGD("nextStep: sending cancel vibrate deviceId=%d", getDeviceId()); |
| 1941 | #endif |
| 1942 | getEventHub()->cancelVibrate(getDeviceId()); |
| 1943 | } |
| 1944 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 1945 | mNextStepTime = now + duration; |
| 1946 | getContext()->requestTimeoutAtTime(mNextStepTime); |
| 1947 | #if DEBUG_VIBRATOR |
| 1948 | ALOGD("nextStep: scheduled timeout in %0.3fms", duration * 0.000001f); |
| 1949 | #endif |
| 1950 | } |
| 1951 | |
| 1952 | void VibratorInputMapper::stopVibrating() { |
| 1953 | mVibrating = false; |
| 1954 | #if DEBUG_VIBRATOR |
| 1955 | ALOGD("stopVibrating: sending cancel vibrate deviceId=%d", getDeviceId()); |
| 1956 | #endif |
| 1957 | getEventHub()->cancelVibrate(getDeviceId()); |
| 1958 | } |
| 1959 | |
| 1960 | void VibratorInputMapper::dump(String8& dump) { |
| 1961 | dump.append(INDENT2 "Vibrator Input Mapper:\n"); |
| 1962 | dump.appendFormat(INDENT3 "Vibrating: %s\n", toString(mVibrating)); |
| 1963 | } |
| 1964 | |
| 1965 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1966 | // --- KeyboardInputMapper --- |
| 1967 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1968 | KeyboardInputMapper::KeyboardInputMapper(InputDevice* device, |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1969 | uint32_t source, int32_t keyboardType) : |
| 1970 | InputMapper(device), mSource(source), |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1971 | mKeyboardType(keyboardType) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | KeyboardInputMapper::~KeyboardInputMapper() { |
| 1975 | } |
| 1976 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1977 | uint32_t KeyboardInputMapper::getSources() { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1978 | return mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 1982 | InputMapper::populateDeviceInfo(info); |
| 1983 | |
| 1984 | info->setKeyboardType(mKeyboardType); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 1985 | info->setKeyCharacterMap(getEventHub()->getKeyCharacterMap(getDeviceId())); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1986 | } |
| 1987 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1988 | void KeyboardInputMapper::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1989 | dump.append(INDENT2 "Keyboard Input Mapper:\n"); |
| 1990 | dumpParameters(dump); |
| 1991 | dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1992 | dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 1993 | dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size()); |
| 1994 | dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState); |
| 1995 | dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1996 | } |
| 1997 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1998 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 1999 | void KeyboardInputMapper::configure(nsecs_t when, |
| 2000 | const InputReaderConfiguration* config, uint32_t changes) { |
| 2001 | InputMapper::configure(when, config, changes); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2002 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2003 | if (!changes) { // first time only |
| 2004 | // Configure basic parameters. |
| 2005 | configureParameters(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2006 | } |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2007 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2008 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) { |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2009 | if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) { |
| 2010 | DisplayViewport v; |
| 2011 | if (config->getDisplayInfo(false /*external*/, &v)) { |
| 2012 | mOrientation = v.orientation; |
| 2013 | } else { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2014 | mOrientation = DISPLAY_ORIENTATION_0; |
| 2015 | } |
| 2016 | } else { |
| 2017 | mOrientation = DISPLAY_ORIENTATION_0; |
| 2018 | } |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2019 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | void KeyboardInputMapper::configureParameters() { |
| 2023 | mParameters.orientationAware = false; |
| 2024 | getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"), |
| 2025 | mParameters.orientationAware); |
| 2026 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2027 | mParameters.hasAssociatedDisplay = false; |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2028 | if (mParameters.orientationAware) { |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2029 | mParameters.hasAssociatedDisplay = true; |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2030 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | void KeyboardInputMapper::dumpParameters(String8& dump) { |
| 2034 | dump.append(INDENT3 "Parameters:\n"); |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2035 | dump.appendFormat(INDENT4 "HasAssociatedDisplay: %s\n", |
| 2036 | toString(mParameters.hasAssociatedDisplay)); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2037 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 2038 | toString(mParameters.orientationAware)); |
| 2039 | } |
| 2040 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2041 | void KeyboardInputMapper::reset(nsecs_t when) { |
| 2042 | mMetaState = AMETA_NONE; |
| 2043 | mDownTime = 0; |
| 2044 | mKeyDowns.clear(); |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 2045 | mCurrentHidUsage = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2046 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2047 | resetLedState(); |
| 2048 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2049 | InputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | void KeyboardInputMapper::process(const RawEvent* rawEvent) { |
| 2053 | switch (rawEvent->type) { |
| 2054 | case EV_KEY: { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 2055 | int32_t scanCode = rawEvent->code; |
| 2056 | int32_t usageCode = mCurrentHidUsage; |
| 2057 | mCurrentHidUsage = 0; |
| 2058 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2059 | if (isKeyboardOrGamepadKey(scanCode)) { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 2060 | int32_t keyCode; |
| 2061 | uint32_t flags; |
| 2062 | if (getEventHub()->mapKey(getDeviceId(), scanCode, usageCode, &keyCode, &flags)) { |
| 2063 | keyCode = AKEYCODE_UNKNOWN; |
| 2064 | flags = 0; |
| 2065 | } |
| 2066 | processKey(rawEvent->when, rawEvent->value != 0, keyCode, scanCode, flags); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2067 | } |
| 2068 | break; |
| 2069 | } |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 2070 | case EV_MSC: { |
| 2071 | if (rawEvent->code == MSC_SCAN) { |
| 2072 | mCurrentHidUsage = rawEvent->value; |
| 2073 | } |
| 2074 | break; |
| 2075 | } |
| 2076 | case EV_SYN: { |
| 2077 | if (rawEvent->code == SYN_REPORT) { |
| 2078 | mCurrentHidUsage = 0; |
| 2079 | } |
| 2080 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2081 | } |
| 2082 | } |
| 2083 | |
| 2084 | bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) { |
| 2085 | return scanCode < BTN_MOUSE |
| 2086 | || scanCode >= KEY_OK |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 2087 | || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE) |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2088 | || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2089 | } |
| 2090 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2091 | void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode, |
| 2092 | int32_t scanCode, uint32_t policyFlags) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2093 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2094 | if (down) { |
| 2095 | // Rotate key codes according to orientation if needed. |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2096 | if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2097 | keyCode = rotateKeyCode(keyCode, mOrientation); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2098 | } |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 2099 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2100 | // Add key down. |
| 2101 | ssize_t keyDownIndex = findKeyDown(scanCode); |
| 2102 | if (keyDownIndex >= 0) { |
| 2103 | // key repeat, be sure to use same keycode as before in case of rotation |
| 2104 | keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2105 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2106 | // key down |
| 2107 | if ((policyFlags & POLICY_FLAG_VIRTUAL) |
| 2108 | && mContext->shouldDropVirtualKey(when, |
| 2109 | getDevice(), keyCode, scanCode)) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2110 | return; |
| 2111 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2112 | |
| 2113 | mKeyDowns.push(); |
| 2114 | KeyDown& keyDown = mKeyDowns.editTop(); |
| 2115 | keyDown.keyCode = keyCode; |
| 2116 | keyDown.scanCode = scanCode; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2117 | } |
| 2118 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2119 | mDownTime = when; |
| 2120 | } else { |
| 2121 | // Remove key down. |
| 2122 | ssize_t keyDownIndex = findKeyDown(scanCode); |
| 2123 | if (keyDownIndex >= 0) { |
| 2124 | // key up, be sure to use same keycode as before in case of rotation |
| 2125 | keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode; |
| 2126 | mKeyDowns.removeAt(size_t(keyDownIndex)); |
| 2127 | } else { |
| 2128 | // key was not actually down |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2129 | 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] | 2130 | "keyCode=%d, scanCode=%d", |
| 2131 | getDeviceName().string(), keyCode, scanCode); |
| 2132 | return; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2133 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2134 | } |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 2135 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2136 | bool metaStateChanged = false; |
| 2137 | int32_t oldMetaState = mMetaState; |
| 2138 | int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState); |
| 2139 | if (oldMetaState != newMetaState) { |
| 2140 | mMetaState = newMetaState; |
| 2141 | metaStateChanged = true; |
| 2142 | updateLedState(false); |
| 2143 | } |
| 2144 | |
| 2145 | nsecs_t downTime = mDownTime; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2146 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2147 | // Key down on external an keyboard should wake the device. |
| 2148 | // We don't do this for internal keyboards to prevent them from waking up in your pocket. |
| 2149 | // For internal keyboards, the key layout file should specify the policy flags for |
| 2150 | // each wake key individually. |
| 2151 | // TODO: Use the input device configuration to control this behavior more finely. |
| 2152 | if (down && getDevice()->isExternal() |
| 2153 | && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) { |
| 2154 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 2155 | } |
| 2156 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2157 | if (metaStateChanged) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2158 | getContext()->updateGlobalMetaState(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2159 | } |
| 2160 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2161 | if (down && !isMetaKey(keyCode)) { |
| 2162 | getContext()->fadePointer(); |
| 2163 | } |
| 2164 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2165 | NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2166 | down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, |
| 2167 | AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2168 | getListener()->notifyKey(&args); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2169 | } |
| 2170 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2171 | ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) { |
| 2172 | size_t n = mKeyDowns.size(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2173 | for (size_t i = 0; i < n; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2174 | if (mKeyDowns[i].scanCode == scanCode) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2175 | return i; |
| 2176 | } |
| 2177 | } |
| 2178 | return -1; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2179 | } |
| 2180 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2181 | int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 2182 | return getEventHub()->getKeyCodeState(getDeviceId(), keyCode); |
| 2183 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2184 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2185 | int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 2186 | return getEventHub()->getScanCodeState(getDeviceId(), scanCode); |
| 2187 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2188 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2189 | bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 2190 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 2191 | return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags); |
| 2192 | } |
| 2193 | |
| 2194 | int32_t KeyboardInputMapper::getMetaState() { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2195 | return mMetaState; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2196 | } |
| 2197 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2198 | void KeyboardInputMapper::resetLedState() { |
| 2199 | initializeLedState(mCapsLockLedState, LED_CAPSL); |
| 2200 | initializeLedState(mNumLockLedState, LED_NUML); |
| 2201 | initializeLedState(mScrollLockLedState, LED_SCROLLL); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2202 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2203 | updateLedState(true); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2204 | } |
| 2205 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2206 | void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) { |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2207 | ledState.avail = getEventHub()->hasLed(getDeviceId(), led); |
| 2208 | ledState.on = false; |
| 2209 | } |
| 2210 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2211 | void KeyboardInputMapper::updateLedState(bool reset) { |
| 2212 | updateLedStateForModifier(mCapsLockLedState, LED_CAPSL, |
Jeff Brown | 51e7fe7 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 2213 | AMETA_CAPS_LOCK_ON, reset); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2214 | updateLedStateForModifier(mNumLockLedState, LED_NUML, |
Jeff Brown | 51e7fe7 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 2215 | AMETA_NUM_LOCK_ON, reset); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2216 | updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL, |
Jeff Brown | 51e7fe7 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 2217 | AMETA_SCROLL_LOCK_ON, reset); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 2218 | } |
| 2219 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2220 | void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState, |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 2221 | int32_t led, int32_t modifier, bool reset) { |
| 2222 | if (ledState.avail) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2223 | bool desiredState = (mMetaState & modifier) != 0; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2224 | if (reset || ledState.on != desiredState) { |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 2225 | getEventHub()->setLedState(getDeviceId(), led, desiredState); |
| 2226 | ledState.on = desiredState; |
| 2227 | } |
| 2228 | } |
| 2229 | } |
| 2230 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2231 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2232 | // --- CursorInputMapper --- |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2233 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2234 | CursorInputMapper::CursorInputMapper(InputDevice* device) : |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2235 | InputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2236 | } |
| 2237 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2238 | CursorInputMapper::~CursorInputMapper() { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2239 | } |
| 2240 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2241 | uint32_t CursorInputMapper::getSources() { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2242 | return mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2243 | } |
| 2244 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2245 | void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2246 | InputMapper::populateDeviceInfo(info); |
| 2247 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2248 | if (mParameters.mode == Parameters::MODE_POINTER) { |
| 2249 | float minX, minY, maxX, maxY; |
| 2250 | if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) { |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 2251 | info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f, 0.0f); |
| 2252 | info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f, 0.0f); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2253 | } |
| 2254 | } else { |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 2255 | info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale, 0.0f); |
| 2256 | info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale, 0.0f); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2257 | } |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 2258 | info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 2259 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2260 | if (mCursorScrollAccumulator.haveRelativeVWheel()) { |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 2261 | info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 2262 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2263 | if (mCursorScrollAccumulator.haveRelativeHWheel()) { |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 2264 | info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 2265 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2266 | } |
| 2267 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2268 | void CursorInputMapper::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2269 | dump.append(INDENT2 "Cursor Input Mapper:\n"); |
| 2270 | dumpParameters(dump); |
| 2271 | dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale); |
| 2272 | dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale); |
| 2273 | dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision); |
| 2274 | dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision); |
| 2275 | dump.appendFormat(INDENT3 "HaveVWheel: %s\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2276 | toString(mCursorScrollAccumulator.haveRelativeVWheel())); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2277 | dump.appendFormat(INDENT3 "HaveHWheel: %s\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2278 | toString(mCursorScrollAccumulator.haveRelativeHWheel())); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2279 | dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale); |
| 2280 | dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2281 | dump.appendFormat(INDENT3 "Orientation: %d\n", mOrientation); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2282 | dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState); |
| 2283 | dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState))); |
| 2284 | dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2285 | } |
| 2286 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2287 | void CursorInputMapper::configure(nsecs_t when, |
| 2288 | const InputReaderConfiguration* config, uint32_t changes) { |
| 2289 | InputMapper::configure(when, config, changes); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2290 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2291 | if (!changes) { // first time only |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2292 | mCursorScrollAccumulator.configure(getDevice()); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2293 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2294 | // Configure basic parameters. |
| 2295 | configureParameters(); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2296 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2297 | // Configure device mode. |
| 2298 | switch (mParameters.mode) { |
| 2299 | case Parameters::MODE_POINTER: |
| 2300 | mSource = AINPUT_SOURCE_MOUSE; |
| 2301 | mXPrecision = 1.0f; |
| 2302 | mYPrecision = 1.0f; |
| 2303 | mXScale = 1.0f; |
| 2304 | mYScale = 1.0f; |
| 2305 | mPointerController = getPolicy()->obtainPointerController(getDeviceId()); |
| 2306 | break; |
| 2307 | case Parameters::MODE_NAVIGATION: |
| 2308 | mSource = AINPUT_SOURCE_TRACKBALL; |
| 2309 | mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 2310 | mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 2311 | mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 2312 | mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 2313 | break; |
| 2314 | } |
| 2315 | |
| 2316 | mVWheelScale = 1.0f; |
| 2317 | mHWheelScale = 1.0f; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2318 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 2319 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2320 | if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) { |
| 2321 | mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters); |
| 2322 | mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters); |
| 2323 | mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters); |
| 2324 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2325 | |
| 2326 | if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) { |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2327 | if (mParameters.orientationAware && mParameters.hasAssociatedDisplay) { |
| 2328 | DisplayViewport v; |
| 2329 | if (config->getDisplayInfo(false /*external*/, &v)) { |
| 2330 | mOrientation = v.orientation; |
| 2331 | } else { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2332 | mOrientation = DISPLAY_ORIENTATION_0; |
| 2333 | } |
| 2334 | } else { |
| 2335 | mOrientation = DISPLAY_ORIENTATION_0; |
| 2336 | } |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 2337 | bumpGeneration(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2338 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2339 | } |
| 2340 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2341 | void CursorInputMapper::configureParameters() { |
| 2342 | mParameters.mode = Parameters::MODE_POINTER; |
| 2343 | String8 cursorModeString; |
| 2344 | if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) { |
| 2345 | if (cursorModeString == "navigation") { |
| 2346 | mParameters.mode = Parameters::MODE_NAVIGATION; |
| 2347 | } else if (cursorModeString != "pointer" && cursorModeString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2348 | ALOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string()); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2349 | } |
| 2350 | } |
| 2351 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2352 | mParameters.orientationAware = false; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2353 | getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"), |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2354 | mParameters.orientationAware); |
| 2355 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2356 | mParameters.hasAssociatedDisplay = false; |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2357 | if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) { |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2358 | mParameters.hasAssociatedDisplay = true; |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2359 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2360 | } |
| 2361 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2362 | void CursorInputMapper::dumpParameters(String8& dump) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2363 | dump.append(INDENT3 "Parameters:\n"); |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2364 | dump.appendFormat(INDENT4 "HasAssociatedDisplay: %s\n", |
| 2365 | toString(mParameters.hasAssociatedDisplay)); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2366 | |
| 2367 | switch (mParameters.mode) { |
| 2368 | case Parameters::MODE_POINTER: |
| 2369 | dump.append(INDENT4 "Mode: pointer\n"); |
| 2370 | break; |
| 2371 | case Parameters::MODE_NAVIGATION: |
| 2372 | dump.append(INDENT4 "Mode: navigation\n"); |
| 2373 | break; |
| 2374 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 2375 | ALOG_ASSERT(false); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2376 | } |
| 2377 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2378 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 2379 | toString(mParameters.orientationAware)); |
| 2380 | } |
| 2381 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2382 | void CursorInputMapper::reset(nsecs_t when) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2383 | mButtonState = 0; |
| 2384 | mDownTime = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2385 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2386 | mPointerVelocityControl.reset(); |
| 2387 | mWheelXVelocityControl.reset(); |
| 2388 | mWheelYVelocityControl.reset(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2389 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2390 | mCursorButtonAccumulator.reset(getDevice()); |
| 2391 | mCursorMotionAccumulator.reset(getDevice()); |
| 2392 | mCursorScrollAccumulator.reset(getDevice()); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2393 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2394 | InputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2395 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2396 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2397 | void CursorInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2398 | mCursorButtonAccumulator.process(rawEvent); |
| 2399 | mCursorMotionAccumulator.process(rawEvent); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2400 | mCursorScrollAccumulator.process(rawEvent); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2401 | |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 2402 | if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2403 | sync(rawEvent->when); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2404 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2405 | } |
| 2406 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2407 | void CursorInputMapper::sync(nsecs_t when) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2408 | int32_t lastButtonState = mButtonState; |
| 2409 | int32_t currentButtonState = mCursorButtonAccumulator.getButtonState(); |
| 2410 | mButtonState = currentButtonState; |
| 2411 | |
| 2412 | bool wasDown = isPointerDown(lastButtonState); |
| 2413 | bool down = isPointerDown(currentButtonState); |
| 2414 | bool downChanged; |
| 2415 | if (!wasDown && down) { |
| 2416 | mDownTime = when; |
| 2417 | downChanged = true; |
| 2418 | } else if (wasDown && !down) { |
| 2419 | downChanged = true; |
| 2420 | } else { |
| 2421 | downChanged = false; |
| 2422 | } |
| 2423 | nsecs_t downTime = mDownTime; |
| 2424 | bool buttonsChanged = currentButtonState != lastButtonState; |
Jeff Brown | c28306a | 2011-08-23 21:32:42 -0700 | [diff] [blame] | 2425 | bool buttonsPressed = currentButtonState & ~lastButtonState; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2426 | |
| 2427 | float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale; |
| 2428 | float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale; |
| 2429 | bool moved = deltaX != 0 || deltaY != 0; |
| 2430 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2431 | // Rotate delta according to orientation if needed. |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2432 | if (mParameters.orientationAware && mParameters.hasAssociatedDisplay |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2433 | && (deltaX != 0.0f || deltaY != 0.0f)) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2434 | rotateDelta(mOrientation, &deltaX, &deltaY); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2435 | } |
| 2436 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2437 | // Move the pointer. |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2438 | PointerProperties pointerProperties; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2439 | pointerProperties.clear(); |
| 2440 | pointerProperties.id = 0; |
| 2441 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE; |
| 2442 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2443 | PointerCoords pointerCoords; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2444 | pointerCoords.clear(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2445 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2446 | float vscroll = mCursorScrollAccumulator.getRelativeVWheel(); |
| 2447 | float hscroll = mCursorScrollAccumulator.getRelativeHWheel(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2448 | bool scrolled = vscroll != 0 || hscroll != 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2449 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2450 | mWheelYVelocityControl.move(when, NULL, &vscroll); |
| 2451 | mWheelXVelocityControl.move(when, &hscroll, NULL); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2452 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2453 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2454 | |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2455 | int32_t displayId; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2456 | if (mPointerController != NULL) { |
| 2457 | if (moved || scrolled || buttonsChanged) { |
| 2458 | mPointerController->setPresentation( |
| 2459 | PointerControllerInterface::PRESENTATION_POINTER); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 2460 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2461 | if (moved) { |
| 2462 | mPointerController->move(deltaX, deltaY); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2463 | } |
| 2464 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2465 | if (buttonsChanged) { |
| 2466 | mPointerController->setButtonState(currentButtonState); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2467 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2468 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2469 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2470 | } |
| 2471 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2472 | float x, y; |
| 2473 | mPointerController->getPosition(&x, &y); |
| 2474 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 2475 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2476 | displayId = ADISPLAY_ID_DEFAULT; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2477 | } else { |
| 2478 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX); |
| 2479 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY); |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2480 | displayId = ADISPLAY_ID_NONE; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2481 | } |
| 2482 | |
| 2483 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2484 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2485 | // Moving an external trackball or mouse should wake the device. |
| 2486 | // We don't do this for internal cursor devices to prevent them from waking up |
| 2487 | // the device in your pocket. |
| 2488 | // TODO: Use the input device configuration to control this behavior more finely. |
| 2489 | uint32_t policyFlags = 0; |
Jeff Brown | c28306a | 2011-08-23 21:32:42 -0700 | [diff] [blame] | 2490 | if ((buttonsPressed || moved || scrolled) && getDevice()->isExternal()) { |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2491 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 2492 | } |
| 2493 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2494 | // Synthesize key down from buttons if needed. |
| 2495 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource, |
| 2496 | policyFlags, lastButtonState, currentButtonState); |
| 2497 | |
| 2498 | // Send motion event. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2499 | if (downChanged || moved || scrolled || buttonsChanged) { |
| 2500 | int32_t metaState = mContext->getGlobalMetaState(); |
| 2501 | int32_t motionEventAction; |
| 2502 | if (downChanged) { |
| 2503 | motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP; |
| 2504 | } else if (down || mPointerController == NULL) { |
| 2505 | motionEventAction = AMOTION_EVENT_ACTION_MOVE; |
| 2506 | } else { |
| 2507 | motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE; |
| 2508 | } |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2509 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2510 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 2511 | motionEventAction, 0, metaState, currentButtonState, 0, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2512 | displayId, 1, &pointerProperties, &pointerCoords, |
| 2513 | mXPrecision, mYPrecision, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2514 | getListener()->notifyMotion(&args); |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2515 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2516 | // Send hover move after UP to tell the application that the mouse is hovering now. |
| 2517 | if (motionEventAction == AMOTION_EVENT_ACTION_UP |
| 2518 | && mPointerController != NULL) { |
| 2519 | NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags, |
| 2520 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 2521 | metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2522 | displayId, 1, &pointerProperties, &pointerCoords, |
| 2523 | mXPrecision, mYPrecision, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2524 | getListener()->notifyMotion(&hoverArgs); |
| 2525 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2526 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2527 | // Send scroll events. |
| 2528 | if (scrolled) { |
| 2529 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll); |
| 2530 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll); |
| 2531 | |
| 2532 | NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags, |
| 2533 | AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState, |
| 2534 | AMOTION_EVENT_EDGE_FLAG_NONE, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2535 | displayId, 1, &pointerProperties, &pointerCoords, |
| 2536 | mXPrecision, mYPrecision, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2537 | getListener()->notifyMotion(&scrollArgs); |
| 2538 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2539 | } |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2540 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2541 | // Synthesize key up from buttons if needed. |
| 2542 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource, |
| 2543 | policyFlags, lastButtonState, currentButtonState); |
| 2544 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2545 | mCursorMotionAccumulator.finishSync(); |
| 2546 | mCursorScrollAccumulator.finishSync(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2547 | } |
| 2548 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2549 | int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Jeff Brown | c3fc2d0 | 2010-08-10 15:47:53 -0700 | [diff] [blame] | 2550 | if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) { |
| 2551 | return getEventHub()->getScanCodeState(getDeviceId(), scanCode); |
| 2552 | } else { |
| 2553 | return AKEY_STATE_UNKNOWN; |
| 2554 | } |
| 2555 | } |
| 2556 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2557 | void CursorInputMapper::fadePointer() { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2558 | if (mPointerController != NULL) { |
| 2559 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 2560 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2561 | } |
| 2562 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2563 | |
| 2564 | // --- TouchInputMapper --- |
| 2565 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2566 | TouchInputMapper::TouchInputMapper(InputDevice* device) : |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2567 | InputMapper(device), |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2568 | mSource(0), mDeviceMode(DEVICE_MODE_DISABLED), |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2569 | mSurfaceWidth(-1), mSurfaceHeight(-1), mSurfaceLeft(0), mSurfaceTop(0), |
| 2570 | mSurfaceOrientation(DISPLAY_ORIENTATION_0) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2571 | } |
| 2572 | |
| 2573 | TouchInputMapper::~TouchInputMapper() { |
| 2574 | } |
| 2575 | |
| 2576 | uint32_t TouchInputMapper::getSources() { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2577 | return mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2578 | } |
| 2579 | |
| 2580 | void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 2581 | InputMapper::populateDeviceInfo(info); |
| 2582 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2583 | if (mDeviceMode != DEVICE_MODE_DISABLED) { |
| 2584 | info->addMotionRange(mOrientedRanges.x); |
| 2585 | info->addMotionRange(mOrientedRanges.y); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2586 | info->addMotionRange(mOrientedRanges.pressure); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2587 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2588 | if (mOrientedRanges.haveSize) { |
| 2589 | info->addMotionRange(mOrientedRanges.size); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2590 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2591 | |
| 2592 | if (mOrientedRanges.haveTouchSize) { |
| 2593 | info->addMotionRange(mOrientedRanges.touchMajor); |
| 2594 | info->addMotionRange(mOrientedRanges.touchMinor); |
| 2595 | } |
| 2596 | |
| 2597 | if (mOrientedRanges.haveToolSize) { |
| 2598 | info->addMotionRange(mOrientedRanges.toolMajor); |
| 2599 | info->addMotionRange(mOrientedRanges.toolMinor); |
| 2600 | } |
| 2601 | |
| 2602 | if (mOrientedRanges.haveOrientation) { |
| 2603 | info->addMotionRange(mOrientedRanges.orientation); |
| 2604 | } |
| 2605 | |
| 2606 | if (mOrientedRanges.haveDistance) { |
| 2607 | info->addMotionRange(mOrientedRanges.distance); |
| 2608 | } |
| 2609 | |
| 2610 | if (mOrientedRanges.haveTilt) { |
| 2611 | info->addMotionRange(mOrientedRanges.tilt); |
| 2612 | } |
| 2613 | |
| 2614 | if (mCursorScrollAccumulator.haveRelativeVWheel()) { |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 2615 | info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, |
| 2616 | 0.0f); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2617 | } |
| 2618 | if (mCursorScrollAccumulator.haveRelativeHWheel()) { |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 2619 | info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f, |
| 2620 | 0.0f); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2621 | } |
Michael Wright | 5e025eb | 2013-05-15 23:16:54 -0700 | [diff] [blame] | 2622 | if (mCalibration.coverageCalibration == Calibration::COVERAGE_CALIBRATION_BOX) { |
| 2623 | const InputDeviceInfo::MotionRange& x = mOrientedRanges.x; |
| 2624 | const InputDeviceInfo::MotionRange& y = mOrientedRanges.y; |
| 2625 | info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_1, mSource, x.min, x.max, x.flat, |
| 2626 | x.fuzz, x.resolution); |
| 2627 | info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_2, mSource, y.min, y.max, y.flat, |
| 2628 | y.fuzz, y.resolution); |
| 2629 | info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_3, mSource, x.min, x.max, x.flat, |
| 2630 | x.fuzz, x.resolution); |
| 2631 | info->addMotionRange(AMOTION_EVENT_AXIS_GENERIC_4, mSource, y.min, y.max, y.flat, |
| 2632 | y.fuzz, y.resolution); |
| 2633 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2634 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2635 | } |
| 2636 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2637 | void TouchInputMapper::dump(String8& dump) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2638 | dump.append(INDENT2 "Touch Input Mapper:\n"); |
| 2639 | dumpParameters(dump); |
| 2640 | dumpVirtualKeys(dump); |
| 2641 | dumpRawPointerAxes(dump); |
| 2642 | dumpCalibration(dump); |
| 2643 | dumpSurface(dump); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2644 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2645 | dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n"); |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2646 | dump.appendFormat(INDENT4 "XTranslate: %0.3f\n", mXTranslate); |
| 2647 | dump.appendFormat(INDENT4 "YTranslate: %0.3f\n", mYTranslate); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2648 | dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale); |
| 2649 | dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale); |
| 2650 | dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision); |
| 2651 | dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision); |
| 2652 | dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2653 | dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale); |
| 2654 | dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale); |
| 2655 | dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale); |
| 2656 | dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2657 | dump.appendFormat(INDENT4 "HaveTilt: %s\n", toString(mHaveTilt)); |
| 2658 | dump.appendFormat(INDENT4 "TiltXCenter: %0.3f\n", mTiltXCenter); |
| 2659 | dump.appendFormat(INDENT4 "TiltXScale: %0.3f\n", mTiltXScale); |
| 2660 | dump.appendFormat(INDENT4 "TiltYCenter: %0.3f\n", mTiltYCenter); |
| 2661 | dump.appendFormat(INDENT4 "TiltYScale: %0.3f\n", mTiltYScale); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2662 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2663 | dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2664 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2665 | dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n", |
| 2666 | mLastRawPointerData.pointerCount); |
| 2667 | for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) { |
| 2668 | const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i]; |
| 2669 | dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, " |
| 2670 | "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2671 | "orientation=%d, tiltX=%d, tiltY=%d, distance=%d, " |
| 2672 | "toolType=%d, isHovering=%s\n", i, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2673 | pointer.id, pointer.x, pointer.y, pointer.pressure, |
| 2674 | pointer.touchMajor, pointer.touchMinor, |
| 2675 | pointer.toolMajor, pointer.toolMinor, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2676 | pointer.orientation, pointer.tiltX, pointer.tiltY, pointer.distance, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2677 | pointer.toolType, toString(pointer.isHovering)); |
| 2678 | } |
| 2679 | |
| 2680 | dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n", |
| 2681 | mLastCookedPointerData.pointerCount); |
| 2682 | for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) { |
| 2683 | const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i]; |
| 2684 | const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i]; |
| 2685 | dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, " |
| 2686 | "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2687 | "orientation=%0.3f, tilt=%0.3f, distance=%0.3f, " |
| 2688 | "toolType=%d, isHovering=%s\n", i, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2689 | pointerProperties.id, |
| 2690 | pointerCoords.getX(), |
| 2691 | pointerCoords.getY(), |
| 2692 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 2693 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 2694 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 2695 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 2696 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 2697 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2698 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TILT), |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2699 | pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), |
| 2700 | pointerProperties.toolType, |
| 2701 | toString(mLastCookedPointerData.isHovering(i))); |
| 2702 | } |
| 2703 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2704 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2705 | dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n"); |
| 2706 | dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2707 | mPointerXMovementScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2708 | dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2709 | mPointerYMovementScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2710 | dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2711 | mPointerXZoomScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2712 | dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2713 | mPointerYZoomScale); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2714 | dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n", |
| 2715 | mPointerGestureMaxSwipeWidth); |
| 2716 | } |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2717 | } |
| 2718 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2719 | void TouchInputMapper::configure(nsecs_t when, |
| 2720 | const InputReaderConfiguration* config, uint32_t changes) { |
| 2721 | InputMapper::configure(when, config, changes); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2722 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2723 | mConfig = *config; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2724 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2725 | if (!changes) { // first time only |
| 2726 | // Configure basic parameters. |
| 2727 | configureParameters(); |
| 2728 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2729 | // Configure common accumulators. |
| 2730 | mCursorScrollAccumulator.configure(getDevice()); |
| 2731 | mTouchButtonAccumulator.configure(getDevice()); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2732 | |
| 2733 | // Configure absolute axis information. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2734 | configureRawPointerAxes(); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2735 | |
| 2736 | // Prepare input device calibration. |
| 2737 | parseCalibration(); |
| 2738 | resolveCalibration(); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2739 | } |
| 2740 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2741 | if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2742 | // Update pointer speed. |
| 2743 | mPointerVelocityControl.setParameters(mConfig.pointerVelocityControlParameters); |
| 2744 | mWheelXVelocityControl.setParameters(mConfig.wheelVelocityControlParameters); |
| 2745 | mWheelYVelocityControl.setParameters(mConfig.wheelVelocityControlParameters); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2746 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2747 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2748 | bool resetNeeded = false; |
| 2749 | if (!changes || (changes & (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 2750 | | InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT |
| 2751 | | InputReaderConfiguration::CHANGE_SHOW_TOUCHES))) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2752 | // Configure device sources, surface dimensions, orientation and |
| 2753 | // scaling factors. |
| 2754 | configureSurface(when, &resetNeeded); |
| 2755 | } |
| 2756 | |
| 2757 | if (changes && resetNeeded) { |
| 2758 | // Send reset, unless this is the first time the device has been configured, |
| 2759 | // in which case the reader will call reset itself after all mappers are ready. |
| 2760 | getDevice()->notifyReset(when); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2761 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2762 | } |
| 2763 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2764 | void TouchInputMapper::configureParameters() { |
Jeff Brown | b126822 | 2011-06-03 17:06:16 -0700 | [diff] [blame] | 2765 | // Use the pointer presentation mode for devices that do not support distinct |
| 2766 | // multitouch. The spot-based presentation relies on being able to accurately |
| 2767 | // locate two or more fingers on the touch pad. |
| 2768 | mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT) |
| 2769 | ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2770 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2771 | String8 gestureModeString; |
| 2772 | if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"), |
| 2773 | gestureModeString)) { |
| 2774 | if (gestureModeString == "pointer") { |
| 2775 | mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER; |
| 2776 | } else if (gestureModeString == "spots") { |
| 2777 | mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS; |
| 2778 | } else if (gestureModeString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2779 | ALOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string()); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2780 | } |
| 2781 | } |
| 2782 | |
Jeff Brown | deffe07 | 2011-08-26 18:38:46 -0700 | [diff] [blame] | 2783 | if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) { |
| 2784 | // The device is a touch screen. |
| 2785 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
| 2786 | } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) { |
| 2787 | // The device is a pointing device like a track pad. |
| 2788 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
| 2789 | } else if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X) |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2790 | || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) { |
| 2791 | // The device is a cursor device with a touch pad attached. |
| 2792 | // By default don't use the touch pad to move the pointer. |
| 2793 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; |
| 2794 | } else { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2795 | // The device is a touch pad of unknown purpose. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2796 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
| 2797 | } |
| 2798 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2799 | String8 deviceTypeString; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2800 | if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"), |
| 2801 | deviceTypeString)) { |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 2802 | if (deviceTypeString == "touchScreen") { |
| 2803 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2804 | } else if (deviceTypeString == "touchPad") { |
| 2805 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; |
Michael Wright | e7a9ae8 | 2013-03-08 15:19:19 -0800 | [diff] [blame] | 2806 | } else if (deviceTypeString == "touchNavigation") { |
| 2807 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_NAVIGATION; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2808 | } else if (deviceTypeString == "pointer") { |
| 2809 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2810 | } else if (deviceTypeString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2811 | ALOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string()); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2812 | } |
| 2813 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2814 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2815 | mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2816 | getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"), |
| 2817 | mParameters.orientationAware); |
| 2818 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2819 | mParameters.hasAssociatedDisplay = false; |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2820 | mParameters.associatedDisplayIsExternal = false; |
| 2821 | if (mParameters.orientationAware |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2822 | || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2823 | || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) { |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2824 | mParameters.hasAssociatedDisplay = true; |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2825 | mParameters.associatedDisplayIsExternal = |
| 2826 | mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN |
| 2827 | && getDevice()->isExternal(); |
Jeff Brown | bc68a59 | 2011-07-25 12:58:12 -0700 | [diff] [blame] | 2828 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2829 | } |
| 2830 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2831 | void TouchInputMapper::dumpParameters(String8& dump) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2832 | dump.append(INDENT3 "Parameters:\n"); |
| 2833 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2834 | switch (mParameters.gestureMode) { |
| 2835 | case Parameters::GESTURE_MODE_POINTER: |
| 2836 | dump.append(INDENT4 "GestureMode: pointer\n"); |
| 2837 | break; |
| 2838 | case Parameters::GESTURE_MODE_SPOTS: |
| 2839 | dump.append(INDENT4 "GestureMode: spots\n"); |
| 2840 | break; |
| 2841 | default: |
| 2842 | assert(false); |
| 2843 | } |
| 2844 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2845 | switch (mParameters.deviceType) { |
| 2846 | case Parameters::DEVICE_TYPE_TOUCH_SCREEN: |
| 2847 | dump.append(INDENT4 "DeviceType: touchScreen\n"); |
| 2848 | break; |
| 2849 | case Parameters::DEVICE_TYPE_TOUCH_PAD: |
| 2850 | dump.append(INDENT4 "DeviceType: touchPad\n"); |
| 2851 | break; |
Michael Wright | e7a9ae8 | 2013-03-08 15:19:19 -0800 | [diff] [blame] | 2852 | case Parameters::DEVICE_TYPE_TOUCH_NAVIGATION: |
| 2853 | dump.append(INDENT4 "DeviceType: touchNavigation\n"); |
| 2854 | break; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2855 | case Parameters::DEVICE_TYPE_POINTER: |
| 2856 | dump.append(INDENT4 "DeviceType: pointer\n"); |
| 2857 | break; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2858 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 2859 | ALOG_ASSERT(false); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2860 | } |
| 2861 | |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2862 | dump.appendFormat(INDENT4 "AssociatedDisplay: hasAssociatedDisplay=%s, isExternal=%s\n", |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2863 | toString(mParameters.hasAssociatedDisplay), |
| 2864 | toString(mParameters.associatedDisplayIsExternal)); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2865 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 2866 | toString(mParameters.orientationAware)); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2867 | } |
| 2868 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2869 | void TouchInputMapper::configureRawPointerAxes() { |
| 2870 | mRawPointerAxes.clear(); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2871 | } |
| 2872 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2873 | void TouchInputMapper::dumpRawPointerAxes(String8& dump) { |
| 2874 | dump.append(INDENT3 "Raw Touch Axes:\n"); |
| 2875 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X"); |
| 2876 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y"); |
| 2877 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure"); |
| 2878 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor"); |
| 2879 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor"); |
| 2880 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor"); |
| 2881 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor"); |
| 2882 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation"); |
| 2883 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance"); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2884 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltX, "TiltX"); |
| 2885 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.tiltY, "TiltY"); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2886 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId"); |
| 2887 | dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot"); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2888 | } |
| 2889 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2890 | void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) { |
| 2891 | int32_t oldDeviceMode = mDeviceMode; |
| 2892 | |
| 2893 | // Determine device mode. |
| 2894 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER |
| 2895 | && mConfig.pointerGesturesEnabled) { |
| 2896 | mSource = AINPUT_SOURCE_MOUSE; |
| 2897 | mDeviceMode = DEVICE_MODE_POINTER; |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 2898 | if (hasStylus()) { |
| 2899 | mSource |= AINPUT_SOURCE_STYLUS; |
| 2900 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2901 | } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2902 | && mParameters.hasAssociatedDisplay) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2903 | mSource = AINPUT_SOURCE_TOUCHSCREEN; |
| 2904 | mDeviceMode = DEVICE_MODE_DIRECT; |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 2905 | if (hasStylus()) { |
| 2906 | mSource |= AINPUT_SOURCE_STYLUS; |
| 2907 | } |
Michael Wright | e7a9ae8 | 2013-03-08 15:19:19 -0800 | [diff] [blame] | 2908 | } else if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_NAVIGATION) { |
| 2909 | mSource = AINPUT_SOURCE_TOUCH_NAVIGATION; |
Jeff Brown | 4dac901 | 2013-04-10 01:03:19 -0700 | [diff] [blame] | 2910 | mDeviceMode = DEVICE_MODE_NAVIGATION; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2911 | } else { |
| 2912 | mSource = AINPUT_SOURCE_TOUCHPAD; |
| 2913 | mDeviceMode = DEVICE_MODE_UNSCALED; |
| 2914 | } |
| 2915 | |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2916 | // Ensure we have valid X and Y axes. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2917 | if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 2918 | ALOGW(INDENT "Touch device '%s' did not report support for X or Y axis! " |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2919 | "The device will be inoperable.", getDeviceName().string()); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2920 | mDeviceMode = DEVICE_MODE_DISABLED; |
| 2921 | return; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2922 | } |
| 2923 | |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2924 | // Raw width and height in the natural orientation. |
| 2925 | int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1; |
| 2926 | int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1; |
| 2927 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2928 | // Get associated display dimensions. |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2929 | bool viewportChanged = false; |
| 2930 | DisplayViewport newViewport; |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2931 | if (mParameters.hasAssociatedDisplay) { |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2932 | if (!mConfig.getDisplayInfo(mParameters.associatedDisplayIsExternal, &newViewport)) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 2933 | 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] | 2934 | "display. The device will be inoperable until the display size " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2935 | "becomes available.", |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 2936 | getDeviceName().string()); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2937 | mDeviceMode = DEVICE_MODE_DISABLED; |
| 2938 | return; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2939 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 2940 | } else { |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 2941 | newViewport.setNonDisplayViewport(rawWidth, rawHeight); |
| 2942 | } |
| 2943 | if (mViewport != newViewport) { |
| 2944 | mViewport = newViewport; |
| 2945 | viewportChanged = true; |
| 2946 | |
| 2947 | if (mDeviceMode == DEVICE_MODE_DIRECT || mDeviceMode == DEVICE_MODE_POINTER) { |
| 2948 | // Convert rotated viewport to natural surface coordinates. |
| 2949 | int32_t naturalLogicalWidth, naturalLogicalHeight; |
| 2950 | int32_t naturalPhysicalWidth, naturalPhysicalHeight; |
| 2951 | int32_t naturalPhysicalLeft, naturalPhysicalTop; |
| 2952 | int32_t naturalDeviceWidth, naturalDeviceHeight; |
| 2953 | switch (mViewport.orientation) { |
| 2954 | case DISPLAY_ORIENTATION_90: |
| 2955 | naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop; |
| 2956 | naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft; |
| 2957 | naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop; |
| 2958 | naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft; |
| 2959 | naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom; |
| 2960 | naturalPhysicalTop = mViewport.physicalLeft; |
| 2961 | naturalDeviceWidth = mViewport.deviceHeight; |
| 2962 | naturalDeviceHeight = mViewport.deviceWidth; |
| 2963 | break; |
| 2964 | case DISPLAY_ORIENTATION_180: |
| 2965 | naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft; |
| 2966 | naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop; |
| 2967 | naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft; |
| 2968 | naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop; |
| 2969 | naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight; |
| 2970 | naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom; |
| 2971 | naturalDeviceWidth = mViewport.deviceWidth; |
| 2972 | naturalDeviceHeight = mViewport.deviceHeight; |
| 2973 | break; |
| 2974 | case DISPLAY_ORIENTATION_270: |
| 2975 | naturalLogicalWidth = mViewport.logicalBottom - mViewport.logicalTop; |
| 2976 | naturalLogicalHeight = mViewport.logicalRight - mViewport.logicalLeft; |
| 2977 | naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop; |
| 2978 | naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft; |
| 2979 | naturalPhysicalLeft = mViewport.physicalTop; |
| 2980 | naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight; |
| 2981 | naturalDeviceWidth = mViewport.deviceHeight; |
| 2982 | naturalDeviceHeight = mViewport.deviceWidth; |
| 2983 | break; |
| 2984 | case DISPLAY_ORIENTATION_0: |
| 2985 | default: |
| 2986 | naturalLogicalWidth = mViewport.logicalRight - mViewport.logicalLeft; |
| 2987 | naturalLogicalHeight = mViewport.logicalBottom - mViewport.logicalTop; |
| 2988 | naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft; |
| 2989 | naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop; |
| 2990 | naturalPhysicalLeft = mViewport.physicalLeft; |
| 2991 | naturalPhysicalTop = mViewport.physicalTop; |
| 2992 | naturalDeviceWidth = mViewport.deviceWidth; |
| 2993 | naturalDeviceHeight = mViewport.deviceHeight; |
| 2994 | break; |
| 2995 | } |
| 2996 | |
| 2997 | mSurfaceWidth = naturalLogicalWidth * naturalDeviceWidth / naturalPhysicalWidth; |
| 2998 | mSurfaceHeight = naturalLogicalHeight * naturalDeviceHeight / naturalPhysicalHeight; |
| 2999 | mSurfaceLeft = naturalPhysicalLeft * naturalLogicalWidth / naturalPhysicalWidth; |
| 3000 | mSurfaceTop = naturalPhysicalTop * naturalLogicalHeight / naturalPhysicalHeight; |
| 3001 | |
| 3002 | mSurfaceOrientation = mParameters.orientationAware ? |
| 3003 | mViewport.orientation : DISPLAY_ORIENTATION_0; |
| 3004 | } else { |
| 3005 | mSurfaceWidth = rawWidth; |
| 3006 | mSurfaceHeight = rawHeight; |
| 3007 | mSurfaceLeft = 0; |
| 3008 | mSurfaceTop = 0; |
| 3009 | mSurfaceOrientation = DISPLAY_ORIENTATION_0; |
| 3010 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | // If moving between pointer modes, need to reset some state. |
| 3014 | bool deviceModeChanged; |
| 3015 | if (mDeviceMode != oldDeviceMode) { |
| 3016 | deviceModeChanged = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3017 | mOrientedRanges.clear(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3018 | } |
| 3019 | |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 3020 | // Create pointer controller if needed. |
| 3021 | if (mDeviceMode == DEVICE_MODE_POINTER || |
| 3022 | (mDeviceMode == DEVICE_MODE_DIRECT && mConfig.showTouches)) { |
| 3023 | if (mPointerController == NULL) { |
| 3024 | mPointerController = getPolicy()->obtainPointerController(getDeviceId()); |
| 3025 | } |
| 3026 | } else { |
| 3027 | mPointerController.clear(); |
| 3028 | } |
| 3029 | |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 3030 | if (viewportChanged || deviceModeChanged) { |
| 3031 | ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, " |
| 3032 | "display id %d", |
| 3033 | getDeviceId(), getDeviceName().string(), mSurfaceWidth, mSurfaceHeight, |
| 3034 | mSurfaceOrientation, mDeviceMode, mViewport.displayId); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3035 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3036 | // Configure X and Y factors. |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 3037 | mXScale = float(mSurfaceWidth) / rawWidth; |
| 3038 | mYScale = float(mSurfaceHeight) / rawHeight; |
| 3039 | mXTranslate = -mSurfaceLeft; |
| 3040 | mYTranslate = -mSurfaceTop; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3041 | mXPrecision = 1.0f / mXScale; |
| 3042 | mYPrecision = 1.0f / mYScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3043 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3044 | mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3045 | mOrientedRanges.x.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3046 | mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3047 | mOrientedRanges.y.source = mSource; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 3048 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3049 | configureVirtualKeys(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3050 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3051 | // Scale factor for terms that are not oriented in a particular axis. |
| 3052 | // If the pixels are square then xScale == yScale otherwise we fake it |
| 3053 | // by choosing an average. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3054 | mGeometricScale = avg(mXScale, mYScale); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3055 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3056 | // Size of diagonal axis. |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 3057 | float diagonalSize = hypotf(mSurfaceWidth, mSurfaceHeight); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3058 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3059 | // Size factors. |
| 3060 | if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) { |
| 3061 | if (mRawPointerAxes.touchMajor.valid |
| 3062 | && mRawPointerAxes.touchMajor.maxValue != 0) { |
| 3063 | mSizeScale = 1.0f / mRawPointerAxes.touchMajor.maxValue; |
| 3064 | } else if (mRawPointerAxes.toolMajor.valid |
| 3065 | && mRawPointerAxes.toolMajor.maxValue != 0) { |
| 3066 | mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue; |
| 3067 | } else { |
| 3068 | mSizeScale = 0.0f; |
| 3069 | } |
| 3070 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3071 | mOrientedRanges.haveTouchSize = true; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3072 | mOrientedRanges.haveToolSize = true; |
| 3073 | mOrientedRanges.haveSize = true; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 3074 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3075 | mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3076 | mOrientedRanges.touchMajor.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3077 | mOrientedRanges.touchMajor.min = 0; |
| 3078 | mOrientedRanges.touchMajor.max = diagonalSize; |
| 3079 | mOrientedRanges.touchMajor.flat = 0; |
| 3080 | mOrientedRanges.touchMajor.fuzz = 0; |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 3081 | mOrientedRanges.touchMajor.resolution = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 3082 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3083 | mOrientedRanges.touchMinor = mOrientedRanges.touchMajor; |
| 3084 | mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 3085 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3086 | mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3087 | mOrientedRanges.toolMajor.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3088 | mOrientedRanges.toolMajor.min = 0; |
| 3089 | mOrientedRanges.toolMajor.max = diagonalSize; |
| 3090 | mOrientedRanges.toolMajor.flat = 0; |
| 3091 | mOrientedRanges.toolMajor.fuzz = 0; |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 3092 | mOrientedRanges.toolMajor.resolution = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 3093 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3094 | mOrientedRanges.toolMinor = mOrientedRanges.toolMajor; |
| 3095 | mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3096 | |
| 3097 | mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3098 | mOrientedRanges.size.source = mSource; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3099 | mOrientedRanges.size.min = 0; |
| 3100 | mOrientedRanges.size.max = 1.0; |
| 3101 | mOrientedRanges.size.flat = 0; |
| 3102 | mOrientedRanges.size.fuzz = 0; |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 3103 | mOrientedRanges.size.resolution = 0; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3104 | } else { |
| 3105 | mSizeScale = 0.0f; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3106 | } |
| 3107 | |
| 3108 | // Pressure factors. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3109 | mPressureScale = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3110 | if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL |
| 3111 | || mCalibration.pressureCalibration |
| 3112 | == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) { |
| 3113 | if (mCalibration.havePressureScale) { |
| 3114 | mPressureScale = mCalibration.pressureScale; |
| 3115 | } else if (mRawPointerAxes.pressure.valid |
| 3116 | && mRawPointerAxes.pressure.maxValue != 0) { |
| 3117 | mPressureScale = 1.0f / mRawPointerAxes.pressure.maxValue; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3118 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3119 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3120 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3121 | mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE; |
| 3122 | mOrientedRanges.pressure.source = mSource; |
| 3123 | mOrientedRanges.pressure.min = 0; |
| 3124 | mOrientedRanges.pressure.max = 1.0; |
| 3125 | mOrientedRanges.pressure.flat = 0; |
| 3126 | mOrientedRanges.pressure.fuzz = 0; |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 3127 | mOrientedRanges.pressure.resolution = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 3128 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3129 | // Tilt |
| 3130 | mTiltXCenter = 0; |
| 3131 | mTiltXScale = 0; |
| 3132 | mTiltYCenter = 0; |
| 3133 | mTiltYScale = 0; |
| 3134 | mHaveTilt = mRawPointerAxes.tiltX.valid && mRawPointerAxes.tiltY.valid; |
| 3135 | if (mHaveTilt) { |
| 3136 | mTiltXCenter = avg(mRawPointerAxes.tiltX.minValue, |
| 3137 | mRawPointerAxes.tiltX.maxValue); |
| 3138 | mTiltYCenter = avg(mRawPointerAxes.tiltY.minValue, |
| 3139 | mRawPointerAxes.tiltY.maxValue); |
| 3140 | mTiltXScale = M_PI / 180; |
| 3141 | mTiltYScale = M_PI / 180; |
| 3142 | |
| 3143 | mOrientedRanges.haveTilt = true; |
| 3144 | |
| 3145 | mOrientedRanges.tilt.axis = AMOTION_EVENT_AXIS_TILT; |
| 3146 | mOrientedRanges.tilt.source = mSource; |
| 3147 | mOrientedRanges.tilt.min = 0; |
| 3148 | mOrientedRanges.tilt.max = M_PI_2; |
| 3149 | mOrientedRanges.tilt.flat = 0; |
| 3150 | mOrientedRanges.tilt.fuzz = 0; |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 3151 | mOrientedRanges.tilt.resolution = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3152 | } |
| 3153 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3154 | // Orientation |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3155 | mOrientationScale = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3156 | if (mHaveTilt) { |
| 3157 | mOrientedRanges.haveOrientation = true; |
| 3158 | |
| 3159 | mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION; |
| 3160 | mOrientedRanges.orientation.source = mSource; |
| 3161 | mOrientedRanges.orientation.min = -M_PI; |
| 3162 | mOrientedRanges.orientation.max = M_PI; |
| 3163 | mOrientedRanges.orientation.flat = 0; |
| 3164 | mOrientedRanges.orientation.fuzz = 0; |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 3165 | mOrientedRanges.orientation.resolution = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3166 | } else if (mCalibration.orientationCalibration != |
| 3167 | Calibration::ORIENTATION_CALIBRATION_NONE) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3168 | if (mCalibration.orientationCalibration |
| 3169 | == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3170 | if (mRawPointerAxes.orientation.valid) { |
Jeff Brown | 037f727 | 2012-06-25 17:31:23 -0700 | [diff] [blame] | 3171 | if (mRawPointerAxes.orientation.maxValue > 0) { |
| 3172 | mOrientationScale = M_PI_2 / mRawPointerAxes.orientation.maxValue; |
| 3173 | } else if (mRawPointerAxes.orientation.minValue < 0) { |
| 3174 | mOrientationScale = -M_PI_2 / mRawPointerAxes.orientation.minValue; |
| 3175 | } else { |
| 3176 | mOrientationScale = 0; |
| 3177 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3178 | } |
| 3179 | } |
| 3180 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3181 | mOrientedRanges.haveOrientation = true; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3182 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3183 | mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3184 | mOrientedRanges.orientation.source = mSource; |
| 3185 | mOrientedRanges.orientation.min = -M_PI_2; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3186 | mOrientedRanges.orientation.max = M_PI_2; |
| 3187 | mOrientedRanges.orientation.flat = 0; |
| 3188 | mOrientedRanges.orientation.fuzz = 0; |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 3189 | mOrientedRanges.orientation.resolution = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3190 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3191 | |
| 3192 | // Distance |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3193 | mDistanceScale = 0; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3194 | if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) { |
| 3195 | if (mCalibration.distanceCalibration |
| 3196 | == Calibration::DISTANCE_CALIBRATION_SCALED) { |
| 3197 | if (mCalibration.haveDistanceScale) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3198 | mDistanceScale = mCalibration.distanceScale; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3199 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3200 | mDistanceScale = 1.0f; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3201 | } |
| 3202 | } |
| 3203 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3204 | mOrientedRanges.haveDistance = true; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3205 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3206 | mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3207 | mOrientedRanges.distance.source = mSource; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3208 | mOrientedRanges.distance.min = |
| 3209 | mRawPointerAxes.distance.minValue * mDistanceScale; |
| 3210 | mOrientedRanges.distance.max = |
Andreas Sandblad | 8239940 | 2012-03-21 14:39:57 +0100 | [diff] [blame] | 3211 | mRawPointerAxes.distance.maxValue * mDistanceScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3212 | mOrientedRanges.distance.flat = 0; |
| 3213 | mOrientedRanges.distance.fuzz = |
| 3214 | mRawPointerAxes.distance.fuzz * mDistanceScale; |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 3215 | mOrientedRanges.distance.resolution = 0; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3216 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3217 | |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 3218 | // Compute oriented precision, scales and ranges. |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 3219 | // Note that the maximum value reported is an inclusive maximum value so it is one |
| 3220 | // unit less than the total width or height of surface. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3221 | switch (mSurfaceOrientation) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 3222 | case DISPLAY_ORIENTATION_90: |
| 3223 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3224 | mOrientedXPrecision = mYPrecision; |
| 3225 | mOrientedYPrecision = mXPrecision; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 3226 | |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 3227 | mOrientedRanges.x.min = mYTranslate; |
| 3228 | mOrientedRanges.x.max = mSurfaceHeight + mYTranslate - 1; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3229 | mOrientedRanges.x.flat = 0; |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 3230 | mOrientedRanges.x.fuzz = 0; |
| 3231 | mOrientedRanges.x.resolution = mRawPointerAxes.y.resolution * mYScale; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 3232 | |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 3233 | mOrientedRanges.y.min = mXTranslate; |
| 3234 | mOrientedRanges.y.max = mSurfaceWidth + mXTranslate - 1; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3235 | mOrientedRanges.y.flat = 0; |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 3236 | mOrientedRanges.y.fuzz = 0; |
| 3237 | mOrientedRanges.y.resolution = mRawPointerAxes.x.resolution * mXScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3238 | break; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 3239 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3240 | default: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3241 | mOrientedXPrecision = mXPrecision; |
| 3242 | mOrientedYPrecision = mYPrecision; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 3243 | |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 3244 | mOrientedRanges.x.min = mXTranslate; |
| 3245 | mOrientedRanges.x.max = mSurfaceWidth + mXTranslate - 1; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3246 | mOrientedRanges.x.flat = 0; |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 3247 | mOrientedRanges.x.fuzz = 0; |
| 3248 | mOrientedRanges.x.resolution = mRawPointerAxes.x.resolution * mXScale; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 3249 | |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 3250 | mOrientedRanges.y.min = mYTranslate; |
| 3251 | mOrientedRanges.y.max = mSurfaceHeight + mYTranslate - 1; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3252 | mOrientedRanges.y.flat = 0; |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 3253 | mOrientedRanges.y.fuzz = 0; |
| 3254 | mOrientedRanges.y.resolution = mRawPointerAxes.y.resolution * mYScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3255 | break; |
| 3256 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3257 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3258 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | 4dac901 | 2013-04-10 01:03:19 -0700 | [diff] [blame] | 3259 | // Compute pointer gesture detection parameters. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3260 | float rawDiagonal = hypotf(rawWidth, rawHeight); |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 3261 | float displayDiagonal = hypotf(mSurfaceWidth, mSurfaceHeight); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3262 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3263 | // 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] | 3264 | // given area relative to the diagonal size of the display when no acceleration |
| 3265 | // is applied. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3266 | // Assume that the touch pad has a square aspect ratio such that movements in |
| 3267 | // 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] | 3268 | mPointerXMovementScale = mConfig.pointerGestureMovementSpeedRatio |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3269 | * displayDiagonal / rawDiagonal; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3270 | mPointerYMovementScale = mPointerXMovementScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3271 | |
| 3272 | // Scale zooms to cover a smaller range of the display than movements do. |
| 3273 | // This value determines the area around the pointer that is affected by freeform |
| 3274 | // pointer gestures. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3275 | mPointerXZoomScale = mConfig.pointerGestureZoomSpeedRatio |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3276 | * displayDiagonal / rawDiagonal; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3277 | mPointerYZoomScale = mPointerXZoomScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3278 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3279 | // Max width between pointers to detect a swipe gesture is more than some fraction |
| 3280 | // of the diagonal axis of the touch pad. Touches that are wider than this are |
| 3281 | // translated into freeform gestures. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3282 | mPointerGestureMaxSwipeWidth = |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 3283 | mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3284 | |
Jeff Brown | 4dac901 | 2013-04-10 01:03:19 -0700 | [diff] [blame] | 3285 | // Abort current pointer usages because the state has changed. |
| 3286 | abortPointerUsage(when, 0 /*policyFlags*/); |
Jeff Brown | 4dac901 | 2013-04-10 01:03:19 -0700 | [diff] [blame] | 3287 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3288 | |
| 3289 | // Inform the dispatcher about the changes. |
| 3290 | *outResetNeeded = true; |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 3291 | bumpGeneration(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3292 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3293 | } |
| 3294 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3295 | void TouchInputMapper::dumpSurface(String8& dump) { |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 3296 | dump.appendFormat(INDENT3 "Viewport: displayId=%d, orientation=%d, " |
| 3297 | "logicalFrame=[%d, %d, %d, %d], " |
| 3298 | "physicalFrame=[%d, %d, %d, %d], " |
| 3299 | "deviceSize=[%d, %d]\n", |
| 3300 | mViewport.displayId, mViewport.orientation, |
| 3301 | mViewport.logicalLeft, mViewport.logicalTop, |
| 3302 | mViewport.logicalRight, mViewport.logicalBottom, |
| 3303 | mViewport.physicalLeft, mViewport.physicalTop, |
| 3304 | mViewport.physicalRight, mViewport.physicalBottom, |
| 3305 | mViewport.deviceWidth, mViewport.deviceHeight); |
| 3306 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3307 | dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth); |
| 3308 | dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight); |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 3309 | dump.appendFormat(INDENT3 "SurfaceLeft: %d\n", mSurfaceLeft); |
| 3310 | dump.appendFormat(INDENT3 "SurfaceTop: %d\n", mSurfaceTop); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3311 | dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3312 | } |
| 3313 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3314 | void TouchInputMapper::configureVirtualKeys() { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3315 | Vector<VirtualKeyDefinition> virtualKeyDefinitions; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 3316 | getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3317 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3318 | mVirtualKeys.clear(); |
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 | if (virtualKeyDefinitions.size() == 0) { |
| 3321 | return; |
| 3322 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3323 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3324 | mVirtualKeys.setCapacity(virtualKeyDefinitions.size()); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3325 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3326 | int32_t touchScreenLeft = mRawPointerAxes.x.minValue; |
| 3327 | int32_t touchScreenTop = mRawPointerAxes.y.minValue; |
| 3328 | int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1; |
| 3329 | int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3330 | |
| 3331 | for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3332 | const VirtualKeyDefinition& virtualKeyDefinition = |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3333 | virtualKeyDefinitions[i]; |
| 3334 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3335 | mVirtualKeys.add(); |
| 3336 | VirtualKey& virtualKey = mVirtualKeys.editTop(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3337 | |
| 3338 | virtualKey.scanCode = virtualKeyDefinition.scanCode; |
| 3339 | int32_t keyCode; |
| 3340 | uint32_t flags; |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 3341 | if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, 0, &keyCode, &flags)) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3342 | ALOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3343 | virtualKey.scanCode); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3344 | mVirtualKeys.pop(); // drop the key |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3345 | continue; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3346 | } |
| 3347 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3348 | virtualKey.keyCode = keyCode; |
| 3349 | virtualKey.flags = flags; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3350 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3351 | // convert the key definition's display coordinates into touch coordinates for a hit box |
| 3352 | int32_t halfWidth = virtualKeyDefinition.width / 2; |
| 3353 | int32_t halfHeight = virtualKeyDefinition.height / 2; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3354 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3355 | virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3356 | * touchScreenWidth / mSurfaceWidth + touchScreenLeft; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3357 | virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3358 | * touchScreenWidth / mSurfaceWidth + touchScreenLeft; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3359 | virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3360 | * touchScreenHeight / mSurfaceHeight + touchScreenTop; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3361 | virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight) |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3362 | * touchScreenHeight / mSurfaceHeight + touchScreenTop; |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3363 | } |
| 3364 | } |
| 3365 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3366 | void TouchInputMapper::dumpVirtualKeys(String8& dump) { |
| 3367 | if (!mVirtualKeys.isEmpty()) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3368 | dump.append(INDENT3 "Virtual Keys:\n"); |
| 3369 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3370 | for (size_t i = 0; i < mVirtualKeys.size(); i++) { |
| 3371 | const VirtualKey& virtualKey = mVirtualKeys.itemAt(i); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3372 | dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, " |
| 3373 | "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n", |
| 3374 | i, virtualKey.scanCode, virtualKey.keyCode, |
| 3375 | virtualKey.hitLeft, virtualKey.hitRight, |
| 3376 | virtualKey.hitTop, virtualKey.hitBottom); |
| 3377 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3378 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3379 | } |
| 3380 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3381 | void TouchInputMapper::parseCalibration() { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 3382 | const PropertyMap& in = getDevice()->getConfiguration(); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3383 | Calibration& out = mCalibration; |
| 3384 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3385 | // Size |
| 3386 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT; |
| 3387 | String8 sizeCalibrationString; |
| 3388 | if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) { |
| 3389 | if (sizeCalibrationString == "none") { |
| 3390 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; |
| 3391 | } else if (sizeCalibrationString == "geometric") { |
| 3392 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC; |
| 3393 | } else if (sizeCalibrationString == "diameter") { |
| 3394 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_DIAMETER; |
Jeff Brown | 037f727 | 2012-06-25 17:31:23 -0700 | [diff] [blame] | 3395 | } else if (sizeCalibrationString == "box") { |
| 3396 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_BOX; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3397 | } else if (sizeCalibrationString == "area") { |
| 3398 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_AREA; |
| 3399 | } else if (sizeCalibrationString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3400 | ALOGW("Invalid value for touch.size.calibration: '%s'", |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3401 | sizeCalibrationString.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3402 | } |
| 3403 | } |
| 3404 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3405 | out.haveSizeScale = in.tryGetProperty(String8("touch.size.scale"), |
| 3406 | out.sizeScale); |
| 3407 | out.haveSizeBias = in.tryGetProperty(String8("touch.size.bias"), |
| 3408 | out.sizeBias); |
| 3409 | out.haveSizeIsSummed = in.tryGetProperty(String8("touch.size.isSummed"), |
| 3410 | out.sizeIsSummed); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3411 | |
| 3412 | // Pressure |
| 3413 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT; |
| 3414 | String8 pressureCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 3415 | if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3416 | if (pressureCalibrationString == "none") { |
| 3417 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; |
| 3418 | } else if (pressureCalibrationString == "physical") { |
| 3419 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL; |
| 3420 | } else if (pressureCalibrationString == "amplitude") { |
| 3421 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE; |
| 3422 | } else if (pressureCalibrationString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3423 | ALOGW("Invalid value for touch.pressure.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3424 | pressureCalibrationString.string()); |
| 3425 | } |
| 3426 | } |
| 3427 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3428 | out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"), |
| 3429 | out.pressureScale); |
| 3430 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3431 | // Orientation |
| 3432 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT; |
| 3433 | String8 orientationCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 3434 | if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3435 | if (orientationCalibrationString == "none") { |
| 3436 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; |
| 3437 | } else if (orientationCalibrationString == "interpolated") { |
| 3438 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 3439 | } else if (orientationCalibrationString == "vector") { |
| 3440 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3441 | } else if (orientationCalibrationString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3442 | ALOGW("Invalid value for touch.orientation.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3443 | orientationCalibrationString.string()); |
| 3444 | } |
| 3445 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3446 | |
| 3447 | // Distance |
| 3448 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT; |
| 3449 | String8 distanceCalibrationString; |
| 3450 | if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) { |
| 3451 | if (distanceCalibrationString == "none") { |
| 3452 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE; |
| 3453 | } else if (distanceCalibrationString == "scaled") { |
| 3454 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED; |
| 3455 | } else if (distanceCalibrationString != "default") { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 3456 | ALOGW("Invalid value for touch.distance.calibration: '%s'", |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3457 | distanceCalibrationString.string()); |
| 3458 | } |
| 3459 | } |
| 3460 | |
| 3461 | out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"), |
| 3462 | out.distanceScale); |
Michael Wright | 5e025eb | 2013-05-15 23:16:54 -0700 | [diff] [blame] | 3463 | |
| 3464 | out.coverageCalibration = Calibration::COVERAGE_CALIBRATION_DEFAULT; |
| 3465 | String8 coverageCalibrationString; |
| 3466 | if (in.tryGetProperty(String8("touch.coverage.calibration"), coverageCalibrationString)) { |
| 3467 | if (coverageCalibrationString == "none") { |
| 3468 | out.coverageCalibration = Calibration::COVERAGE_CALIBRATION_NONE; |
| 3469 | } else if (coverageCalibrationString == "box") { |
| 3470 | out.coverageCalibration = Calibration::COVERAGE_CALIBRATION_BOX; |
| 3471 | } else if (coverageCalibrationString != "default") { |
| 3472 | ALOGW("Invalid value for touch.coverage.calibration: '%s'", |
| 3473 | coverageCalibrationString.string()); |
| 3474 | } |
| 3475 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3476 | } |
| 3477 | |
| 3478 | void TouchInputMapper::resolveCalibration() { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3479 | // Size |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3480 | if (mRawPointerAxes.touchMajor.valid || mRawPointerAxes.toolMajor.valid) { |
| 3481 | if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DEFAULT) { |
| 3482 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_GEOMETRIC; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3483 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3484 | } else { |
| 3485 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; |
| 3486 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3487 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3488 | // Pressure |
| 3489 | if (mRawPointerAxes.pressure.valid) { |
| 3490 | if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_DEFAULT) { |
| 3491 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL; |
| 3492 | } |
| 3493 | } else { |
| 3494 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3495 | } |
| 3496 | |
| 3497 | // Orientation |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3498 | if (mRawPointerAxes.orientation.valid) { |
| 3499 | if (mCalibration.orientationCalibration == Calibration::ORIENTATION_CALIBRATION_DEFAULT) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3500 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3501 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3502 | } else { |
| 3503 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3504 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3505 | |
| 3506 | // Distance |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3507 | if (mRawPointerAxes.distance.valid) { |
| 3508 | if (mCalibration.distanceCalibration == Calibration::DISTANCE_CALIBRATION_DEFAULT) { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3509 | mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3510 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3511 | } else { |
| 3512 | mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3513 | } |
Michael Wright | 5e025eb | 2013-05-15 23:16:54 -0700 | [diff] [blame] | 3514 | |
| 3515 | // Coverage |
| 3516 | if (mCalibration.coverageCalibration == Calibration::COVERAGE_CALIBRATION_DEFAULT) { |
| 3517 | mCalibration.coverageCalibration = Calibration::COVERAGE_CALIBRATION_NONE; |
| 3518 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3519 | } |
| 3520 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3521 | void TouchInputMapper::dumpCalibration(String8& dump) { |
| 3522 | dump.append(INDENT3 "Calibration:\n"); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3523 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3524 | // Size |
| 3525 | switch (mCalibration.sizeCalibration) { |
| 3526 | case Calibration::SIZE_CALIBRATION_NONE: |
| 3527 | dump.append(INDENT4 "touch.size.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3528 | break; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3529 | case Calibration::SIZE_CALIBRATION_GEOMETRIC: |
| 3530 | dump.append(INDENT4 "touch.size.calibration: geometric\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3531 | break; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3532 | case Calibration::SIZE_CALIBRATION_DIAMETER: |
| 3533 | dump.append(INDENT4 "touch.size.calibration: diameter\n"); |
| 3534 | break; |
Jeff Brown | 037f727 | 2012-06-25 17:31:23 -0700 | [diff] [blame] | 3535 | case Calibration::SIZE_CALIBRATION_BOX: |
| 3536 | dump.append(INDENT4 "touch.size.calibration: box\n"); |
| 3537 | break; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3538 | case Calibration::SIZE_CALIBRATION_AREA: |
| 3539 | dump.append(INDENT4 "touch.size.calibration: area\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3540 | break; |
| 3541 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3542 | ALOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3543 | } |
| 3544 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3545 | if (mCalibration.haveSizeScale) { |
| 3546 | dump.appendFormat(INDENT4 "touch.size.scale: %0.3f\n", |
| 3547 | mCalibration.sizeScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3548 | } |
| 3549 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3550 | if (mCalibration.haveSizeBias) { |
| 3551 | dump.appendFormat(INDENT4 "touch.size.bias: %0.3f\n", |
| 3552 | mCalibration.sizeBias); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3553 | } |
| 3554 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 3555 | if (mCalibration.haveSizeIsSummed) { |
| 3556 | dump.appendFormat(INDENT4 "touch.size.isSummed: %s\n", |
| 3557 | toString(mCalibration.sizeIsSummed)); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3558 | } |
| 3559 | |
| 3560 | // Pressure |
| 3561 | switch (mCalibration.pressureCalibration) { |
| 3562 | case Calibration::PRESSURE_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3563 | dump.append(INDENT4 "touch.pressure.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3564 | break; |
| 3565 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3566 | dump.append(INDENT4 "touch.pressure.calibration: physical\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3567 | break; |
| 3568 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3569 | dump.append(INDENT4 "touch.pressure.calibration: amplitude\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3570 | break; |
| 3571 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3572 | ALOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3573 | } |
| 3574 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3575 | if (mCalibration.havePressureScale) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3576 | dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n", |
| 3577 | mCalibration.pressureScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3578 | } |
| 3579 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3580 | // Orientation |
| 3581 | switch (mCalibration.orientationCalibration) { |
| 3582 | case Calibration::ORIENTATION_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3583 | dump.append(INDENT4 "touch.orientation.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3584 | break; |
| 3585 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 3586 | dump.append(INDENT4 "touch.orientation.calibration: interpolated\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3587 | break; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 3588 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: |
| 3589 | dump.append(INDENT4 "touch.orientation.calibration: vector\n"); |
| 3590 | break; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3591 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3592 | ALOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3593 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3594 | |
| 3595 | // Distance |
| 3596 | switch (mCalibration.distanceCalibration) { |
| 3597 | case Calibration::DISTANCE_CALIBRATION_NONE: |
| 3598 | dump.append(INDENT4 "touch.distance.calibration: none\n"); |
| 3599 | break; |
| 3600 | case Calibration::DISTANCE_CALIBRATION_SCALED: |
| 3601 | dump.append(INDENT4 "touch.distance.calibration: scaled\n"); |
| 3602 | break; |
| 3603 | default: |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 3604 | ALOG_ASSERT(false); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3605 | } |
| 3606 | |
| 3607 | if (mCalibration.haveDistanceScale) { |
| 3608 | dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n", |
| 3609 | mCalibration.distanceScale); |
| 3610 | } |
Michael Wright | 5e025eb | 2013-05-15 23:16:54 -0700 | [diff] [blame] | 3611 | |
| 3612 | switch (mCalibration.coverageCalibration) { |
| 3613 | case Calibration::COVERAGE_CALIBRATION_NONE: |
| 3614 | dump.append(INDENT4 "touch.coverage.calibration: none\n"); |
| 3615 | break; |
| 3616 | case Calibration::COVERAGE_CALIBRATION_BOX: |
| 3617 | dump.append(INDENT4 "touch.coverage.calibration: box\n"); |
| 3618 | break; |
| 3619 | default: |
| 3620 | ALOG_ASSERT(false); |
| 3621 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3622 | } |
| 3623 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3624 | void TouchInputMapper::reset(nsecs_t when) { |
| 3625 | mCursorButtonAccumulator.reset(getDevice()); |
| 3626 | mCursorScrollAccumulator.reset(getDevice()); |
| 3627 | mTouchButtonAccumulator.reset(getDevice()); |
| 3628 | |
| 3629 | mPointerVelocityControl.reset(); |
| 3630 | mWheelXVelocityControl.reset(); |
| 3631 | mWheelYVelocityControl.reset(); |
| 3632 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3633 | mCurrentRawPointerData.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3634 | mLastRawPointerData.clear(); |
| 3635 | mCurrentCookedPointerData.clear(); |
| 3636 | mLastCookedPointerData.clear(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3637 | mCurrentButtonState = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3638 | mLastButtonState = 0; |
| 3639 | mCurrentRawVScroll = 0; |
| 3640 | mCurrentRawHScroll = 0; |
| 3641 | mCurrentFingerIdBits.clear(); |
| 3642 | mLastFingerIdBits.clear(); |
| 3643 | mCurrentStylusIdBits.clear(); |
| 3644 | mLastStylusIdBits.clear(); |
| 3645 | mCurrentMouseIdBits.clear(); |
| 3646 | mLastMouseIdBits.clear(); |
| 3647 | mPointerUsage = POINTER_USAGE_NONE; |
| 3648 | mSentHoverEnter = false; |
| 3649 | mDownTime = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3650 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3651 | mCurrentVirtualKey.down = false; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3652 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3653 | mPointerGesture.reset(); |
| 3654 | mPointerSimple.reset(); |
| 3655 | |
| 3656 | if (mPointerController != NULL) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3657 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 3658 | mPointerController->clearSpots(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3659 | } |
| 3660 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3661 | InputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3662 | } |
| 3663 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3664 | void TouchInputMapper::process(const RawEvent* rawEvent) { |
| 3665 | mCursorButtonAccumulator.process(rawEvent); |
| 3666 | mCursorScrollAccumulator.process(rawEvent); |
| 3667 | mTouchButtonAccumulator.process(rawEvent); |
| 3668 | |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 3669 | if (rawEvent->type == EV_SYN && rawEvent->code == SYN_REPORT) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3670 | sync(rawEvent->when); |
| 3671 | } |
| 3672 | } |
| 3673 | |
| 3674 | void TouchInputMapper::sync(nsecs_t when) { |
| 3675 | // Sync button state. |
| 3676 | mCurrentButtonState = mTouchButtonAccumulator.getButtonState() |
| 3677 | | mCursorButtonAccumulator.getButtonState(); |
| 3678 | |
| 3679 | // Sync scroll state. |
| 3680 | mCurrentRawVScroll = mCursorScrollAccumulator.getRelativeVWheel(); |
| 3681 | mCurrentRawHScroll = mCursorScrollAccumulator.getRelativeHWheel(); |
| 3682 | mCursorScrollAccumulator.finishSync(); |
| 3683 | |
| 3684 | // Sync touch state. |
| 3685 | bool havePointerIds = true; |
| 3686 | mCurrentRawPointerData.clear(); |
| 3687 | syncTouch(when, &havePointerIds); |
| 3688 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 3689 | #if DEBUG_RAW_EVENTS |
| 3690 | if (!havePointerIds) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3691 | ALOGD("syncTouch: pointerCount %d -> %d, no pointer ids", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3692 | mLastRawPointerData.pointerCount, |
| 3693 | mCurrentRawPointerData.pointerCount); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 3694 | } else { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3695 | ALOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, " |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3696 | "hovering ids 0x%08x -> 0x%08x", |
| 3697 | mLastRawPointerData.pointerCount, |
| 3698 | mCurrentRawPointerData.pointerCount, |
| 3699 | mLastRawPointerData.touchingIdBits.value, |
| 3700 | mCurrentRawPointerData.touchingIdBits.value, |
| 3701 | mLastRawPointerData.hoveringIdBits.value, |
| 3702 | mCurrentRawPointerData.hoveringIdBits.value); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 3703 | } |
| 3704 | #endif |
| 3705 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3706 | // Reset state that we will compute below. |
| 3707 | mCurrentFingerIdBits.clear(); |
| 3708 | mCurrentStylusIdBits.clear(); |
| 3709 | mCurrentMouseIdBits.clear(); |
| 3710 | mCurrentCookedPointerData.clear(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3711 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3712 | if (mDeviceMode == DEVICE_MODE_DISABLED) { |
| 3713 | // Drop all input if the device is disabled. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3714 | mCurrentRawPointerData.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3715 | mCurrentButtonState = 0; |
| 3716 | } else { |
| 3717 | // Preprocess pointer data. |
| 3718 | if (!havePointerIds) { |
| 3719 | assignPointerIds(); |
| 3720 | } |
| 3721 | |
| 3722 | // Handle policy on initial down or hover events. |
| 3723 | uint32_t policyFlags = 0; |
Jeff Brown | c28306a | 2011-08-23 21:32:42 -0700 | [diff] [blame] | 3724 | bool initialDown = mLastRawPointerData.pointerCount == 0 |
| 3725 | && mCurrentRawPointerData.pointerCount != 0; |
| 3726 | bool buttonsPressed = mCurrentButtonState & ~mLastButtonState; |
| 3727 | if (initialDown || buttonsPressed) { |
| 3728 | // 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] | 3729 | if (mDeviceMode == DEVICE_MODE_DIRECT) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3730 | getContext()->fadePointer(); |
| 3731 | } |
| 3732 | |
| 3733 | // Initial downs on external touch devices should wake the device. |
| 3734 | // We don't do this for internal touch screens to prevent them from waking |
| 3735 | // up in your pocket. |
| 3736 | // TODO: Use the input device configuration to control this behavior more finely. |
| 3737 | if (getDevice()->isExternal()) { |
| 3738 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 3739 | } |
| 3740 | } |
| 3741 | |
| 3742 | // Synthesize key down from raw buttons if needed. |
| 3743 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource, |
| 3744 | policyFlags, mLastButtonState, mCurrentButtonState); |
| 3745 | |
| 3746 | // Consume raw off-screen touches before cooking pointer data. |
| 3747 | // If touches are consumed, subsequent code will not receive any pointer data. |
| 3748 | if (consumeRawTouches(when, policyFlags)) { |
| 3749 | mCurrentRawPointerData.clear(); |
| 3750 | } |
| 3751 | |
| 3752 | // Cook pointer data. This call populates the mCurrentCookedPointerData structure |
| 3753 | // with cooked pointer data that has the same ids and indices as the raw data. |
| 3754 | // The following code can use either the raw or cooked data, as needed. |
| 3755 | cookPointerData(); |
| 3756 | |
| 3757 | // 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] | 3758 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3759 | for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) { |
| 3760 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 3761 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3762 | if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS |
| 3763 | || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) { |
| 3764 | mCurrentStylusIdBits.markBit(id); |
| 3765 | } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER |
| 3766 | || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 3767 | mCurrentFingerIdBits.markBit(id); |
| 3768 | } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_MOUSE) { |
| 3769 | mCurrentMouseIdBits.markBit(id); |
| 3770 | } |
| 3771 | } |
| 3772 | for (BitSet32 idBits(mCurrentRawPointerData.hoveringIdBits); !idBits.isEmpty(); ) { |
| 3773 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 3774 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3775 | if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS |
| 3776 | || pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) { |
| 3777 | mCurrentStylusIdBits.markBit(id); |
| 3778 | } |
| 3779 | } |
| 3780 | |
| 3781 | // Stylus takes precedence over all tools, then mouse, then finger. |
| 3782 | PointerUsage pointerUsage = mPointerUsage; |
| 3783 | if (!mCurrentStylusIdBits.isEmpty()) { |
| 3784 | mCurrentMouseIdBits.clear(); |
| 3785 | mCurrentFingerIdBits.clear(); |
| 3786 | pointerUsage = POINTER_USAGE_STYLUS; |
| 3787 | } else if (!mCurrentMouseIdBits.isEmpty()) { |
| 3788 | mCurrentFingerIdBits.clear(); |
| 3789 | pointerUsage = POINTER_USAGE_MOUSE; |
| 3790 | } else if (!mCurrentFingerIdBits.isEmpty() || isPointerDown(mCurrentButtonState)) { |
| 3791 | pointerUsage = POINTER_USAGE_GESTURES; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3792 | } |
| 3793 | |
| 3794 | dispatchPointerUsage(when, policyFlags, pointerUsage); |
| 3795 | } else { |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 3796 | if (mDeviceMode == DEVICE_MODE_DIRECT |
| 3797 | && mConfig.showTouches && mPointerController != NULL) { |
| 3798 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT); |
| 3799 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 3800 | |
| 3801 | mPointerController->setButtonState(mCurrentButtonState); |
| 3802 | mPointerController->setSpots(mCurrentCookedPointerData.pointerCoords, |
| 3803 | mCurrentCookedPointerData.idToIndex, |
| 3804 | mCurrentCookedPointerData.touchingIdBits); |
| 3805 | } |
| 3806 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3807 | dispatchHoverExit(when, policyFlags); |
| 3808 | dispatchTouches(when, policyFlags); |
| 3809 | dispatchHoverEnterAndMove(when, policyFlags); |
| 3810 | } |
| 3811 | |
| 3812 | // Synthesize key up from raw buttons if needed. |
| 3813 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource, |
| 3814 | policyFlags, mLastButtonState, mCurrentButtonState); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3815 | } |
| 3816 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3817 | // Copy current touch to last touch in preparation for the next cycle. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3818 | mLastRawPointerData.copyFrom(mCurrentRawPointerData); |
| 3819 | mLastCookedPointerData.copyFrom(mCurrentCookedPointerData); |
| 3820 | mLastButtonState = mCurrentButtonState; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3821 | mLastFingerIdBits = mCurrentFingerIdBits; |
| 3822 | mLastStylusIdBits = mCurrentStylusIdBits; |
| 3823 | mLastMouseIdBits = mCurrentMouseIdBits; |
| 3824 | |
| 3825 | // Clear some transient state. |
| 3826 | mCurrentRawVScroll = 0; |
| 3827 | mCurrentRawHScroll = 0; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3828 | } |
| 3829 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3830 | void TouchInputMapper::timeoutExpired(nsecs_t when) { |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 3831 | if (mDeviceMode == DEVICE_MODE_POINTER) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3832 | if (mPointerUsage == POINTER_USAGE_GESTURES) { |
| 3833 | dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/); |
| 3834 | } |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3835 | } |
| 3836 | } |
| 3837 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3838 | bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) { |
| 3839 | // Check for release of a virtual key. |
| 3840 | if (mCurrentVirtualKey.down) { |
| 3841 | if (mCurrentRawPointerData.touchingIdBits.isEmpty()) { |
| 3842 | // Pointer went up while virtual key was down. |
| 3843 | mCurrentVirtualKey.down = false; |
| 3844 | if (!mCurrentVirtualKey.ignored) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3845 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3846 | ALOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3847 | mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3848 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3849 | dispatchVirtualKey(when, policyFlags, |
| 3850 | AKEY_EVENT_ACTION_UP, |
| 3851 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3852 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3853 | return true; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3854 | } |
| 3855 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3856 | if (mCurrentRawPointerData.touchingIdBits.count() == 1) { |
| 3857 | uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit(); |
| 3858 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3859 | const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y); |
| 3860 | if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) { |
| 3861 | // Pointer is still within the space of the virtual key. |
| 3862 | return true; |
| 3863 | } |
| 3864 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3865 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3866 | // Pointer left virtual key area or another pointer also went down. |
| 3867 | // Send key cancellation but do not consume the touch yet. |
| 3868 | // This is useful when the user swipes through from the virtual key area |
| 3869 | // into the main display surface. |
| 3870 | mCurrentVirtualKey.down = false; |
| 3871 | if (!mCurrentVirtualKey.ignored) { |
| 3872 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3873 | ALOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3874 | mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode); |
| 3875 | #endif |
| 3876 | dispatchVirtualKey(when, policyFlags, |
| 3877 | AKEY_EVENT_ACTION_UP, |
| 3878 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
| 3879 | | AKEY_EVENT_FLAG_CANCELED); |
| 3880 | } |
| 3881 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3882 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3883 | if (mLastRawPointerData.touchingIdBits.isEmpty() |
| 3884 | && !mCurrentRawPointerData.touchingIdBits.isEmpty()) { |
| 3885 | // Pointer just went down. Check for virtual key press or off-screen touches. |
| 3886 | uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit(); |
| 3887 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
| 3888 | if (!isPointInsideSurface(pointer.x, pointer.y)) { |
| 3889 | // If exactly one pointer went down, check for virtual key hit. |
| 3890 | // Otherwise we will drop the entire stroke. |
| 3891 | if (mCurrentRawPointerData.touchingIdBits.count() == 1) { |
| 3892 | const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y); |
| 3893 | if (virtualKey) { |
| 3894 | mCurrentVirtualKey.down = true; |
| 3895 | mCurrentVirtualKey.downTime = when; |
| 3896 | mCurrentVirtualKey.keyCode = virtualKey->keyCode; |
| 3897 | mCurrentVirtualKey.scanCode = virtualKey->scanCode; |
| 3898 | mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey( |
| 3899 | when, getDevice(), virtualKey->keyCode, virtualKey->scanCode); |
| 3900 | |
| 3901 | if (!mCurrentVirtualKey.ignored) { |
| 3902 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 3903 | ALOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3904 | mCurrentVirtualKey.keyCode, |
| 3905 | mCurrentVirtualKey.scanCode); |
| 3906 | #endif |
| 3907 | dispatchVirtualKey(when, policyFlags, |
| 3908 | AKEY_EVENT_ACTION_DOWN, |
| 3909 | AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY); |
| 3910 | } |
| 3911 | } |
| 3912 | } |
| 3913 | return true; |
| 3914 | } |
| 3915 | } |
| 3916 | |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3917 | // 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] | 3918 | // most recent touch within the screen area. The idea is to filter out stray |
| 3919 | // virtual key presses when interacting with the touch screen. |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3920 | // |
| 3921 | // Problems we're trying to solve: |
| 3922 | // |
| 3923 | // 1. While scrolling a list or dragging the window shade, the user swipes down into a |
| 3924 | // virtual key area that is implemented by a separate touch panel and accidentally |
| 3925 | // triggers a virtual key. |
| 3926 | // |
| 3927 | // 2. While typing in the on screen keyboard, the user taps slightly outside the screen |
| 3928 | // area and accidentally triggers a virtual key. This often happens when virtual keys |
| 3929 | // are layed out below the screen near to where the on screen keyboard's space bar |
| 3930 | // is displayed. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3931 | if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 3932 | mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime); |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3933 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3934 | return false; |
| 3935 | } |
| 3936 | |
| 3937 | void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags, |
| 3938 | int32_t keyEventAction, int32_t keyEventFlags) { |
| 3939 | int32_t keyCode = mCurrentVirtualKey.keyCode; |
| 3940 | int32_t scanCode = mCurrentVirtualKey.scanCode; |
| 3941 | nsecs_t downTime = mCurrentVirtualKey.downTime; |
| 3942 | int32_t metaState = mContext->getGlobalMetaState(); |
| 3943 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3944 | |
| 3945 | NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags, |
| 3946 | keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime); |
| 3947 | getListener()->notifyKey(&args); |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3948 | } |
| 3949 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3950 | void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3951 | BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits; |
| 3952 | BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3953 | int32_t metaState = getContext()->getGlobalMetaState(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3954 | int32_t buttonState = mCurrentButtonState; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3955 | |
| 3956 | if (currentIdBits == lastIdBits) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3957 | if (!currentIdBits.isEmpty()) { |
| 3958 | // No pointer id changes so this is a move event. |
| 3959 | // 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] | 3960 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3961 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, |
| 3962 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 3963 | mCurrentCookedPointerData.pointerProperties, |
| 3964 | mCurrentCookedPointerData.pointerCoords, |
| 3965 | mCurrentCookedPointerData.idToIndex, |
| 3966 | currentIdBits, -1, |
| 3967 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 3968 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3969 | } else { |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3970 | // There may be pointers going up and pointers going down and pointers moving |
| 3971 | // all at the same time. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3972 | BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value); |
| 3973 | BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3974 | BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3975 | BitSet32 dispatchedIdBits(lastIdBits.value); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3976 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3977 | // Update last coordinates of pointers that have moved so that we observe the new |
| 3978 | // 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] | 3979 | bool moveNeeded = updateMovedPointers( |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3980 | mCurrentCookedPointerData.pointerProperties, |
| 3981 | mCurrentCookedPointerData.pointerCoords, |
| 3982 | mCurrentCookedPointerData.idToIndex, |
| 3983 | mLastCookedPointerData.pointerProperties, |
| 3984 | mLastCookedPointerData.pointerCoords, |
| 3985 | mLastCookedPointerData.idToIndex, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3986 | moveIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3987 | if (buttonState != mLastButtonState) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3988 | moveNeeded = true; |
| 3989 | } |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3990 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3991 | // Dispatch pointer up events. |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3992 | while (!upIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3993 | uint32_t upId = upIdBits.clearFirstMarkedBit(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3994 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3995 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3996 | AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3997 | mLastCookedPointerData.pointerProperties, |
| 3998 | mLastCookedPointerData.pointerCoords, |
| 3999 | mLastCookedPointerData.idToIndex, |
| 4000 | dispatchedIdBits, upId, |
| 4001 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4002 | dispatchedIdBits.clearBit(upId); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4003 | } |
| 4004 | |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 4005 | // Dispatch move events if any of the remaining pointers moved from their old locations. |
| 4006 | // Although applications receive new locations as part of individual pointer up |
| 4007 | // events, they do not generally handle them except when presented in a move event. |
| 4008 | if (moveNeeded) { |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 4009 | ALOG_ASSERT(moveIdBits.value == dispatchedIdBits.value); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4010 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4011 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4012 | mCurrentCookedPointerData.pointerProperties, |
| 4013 | mCurrentCookedPointerData.pointerCoords, |
| 4014 | mCurrentCookedPointerData.idToIndex, |
| 4015 | dispatchedIdBits, -1, |
| 4016 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 4017 | } |
| 4018 | |
| 4019 | // Dispatch pointer down events using the new pointer locations. |
| 4020 | while (!downIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4021 | uint32_t downId = downIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4022 | dispatchedIdBits.markBit(downId); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4023 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4024 | if (dispatchedIdBits.count() == 1) { |
| 4025 | // First pointer is going down. Set down time. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4026 | mDownTime = when; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4027 | } |
| 4028 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4029 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | a611137 | 2011-07-14 21:48:23 -0700 | [diff] [blame] | 4030 | AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4031 | mCurrentCookedPointerData.pointerProperties, |
| 4032 | mCurrentCookedPointerData.pointerCoords, |
| 4033 | mCurrentCookedPointerData.idToIndex, |
| 4034 | dispatchedIdBits, downId, |
| 4035 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4036 | } |
| 4037 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4038 | } |
| 4039 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4040 | void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) { |
| 4041 | if (mSentHoverEnter && |
| 4042 | (mCurrentCookedPointerData.hoveringIdBits.isEmpty() |
| 4043 | || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) { |
| 4044 | int32_t metaState = getContext()->getGlobalMetaState(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4045 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4046 | AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0, |
| 4047 | mLastCookedPointerData.pointerProperties, |
| 4048 | mLastCookedPointerData.pointerCoords, |
| 4049 | mLastCookedPointerData.idToIndex, |
| 4050 | mLastCookedPointerData.hoveringIdBits, -1, |
| 4051 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 4052 | mSentHoverEnter = false; |
| 4053 | } |
| 4054 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4055 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4056 | void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) { |
| 4057 | if (mCurrentCookedPointerData.touchingIdBits.isEmpty() |
| 4058 | && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) { |
| 4059 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 4060 | if (!mSentHoverEnter) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4061 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4062 | AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0, |
| 4063 | mCurrentCookedPointerData.pointerProperties, |
| 4064 | mCurrentCookedPointerData.pointerCoords, |
| 4065 | mCurrentCookedPointerData.idToIndex, |
| 4066 | mCurrentCookedPointerData.hoveringIdBits, -1, |
| 4067 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 4068 | mSentHoverEnter = true; |
| 4069 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4070 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4071 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4072 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0, |
| 4073 | mCurrentCookedPointerData.pointerProperties, |
| 4074 | mCurrentCookedPointerData.pointerCoords, |
| 4075 | mCurrentCookedPointerData.idToIndex, |
| 4076 | mCurrentCookedPointerData.hoveringIdBits, -1, |
| 4077 | mOrientedXPrecision, mOrientedYPrecision, mDownTime); |
| 4078 | } |
| 4079 | } |
| 4080 | |
| 4081 | void TouchInputMapper::cookPointerData() { |
| 4082 | uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount; |
| 4083 | |
| 4084 | mCurrentCookedPointerData.clear(); |
| 4085 | mCurrentCookedPointerData.pointerCount = currentPointerCount; |
| 4086 | mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits; |
| 4087 | mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits; |
| 4088 | |
| 4089 | // Walk through the the active pointers and map device coordinates onto |
| 4090 | // surface coordinates and adjust for display orientation. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4091 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4092 | const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4093 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4094 | // Size |
| 4095 | float touchMajor, touchMinor, toolMajor, toolMinor, size; |
| 4096 | switch (mCalibration.sizeCalibration) { |
| 4097 | case Calibration::SIZE_CALIBRATION_GEOMETRIC: |
| 4098 | case Calibration::SIZE_CALIBRATION_DIAMETER: |
Jeff Brown | 037f727 | 2012-06-25 17:31:23 -0700 | [diff] [blame] | 4099 | case Calibration::SIZE_CALIBRATION_BOX: |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4100 | case Calibration::SIZE_CALIBRATION_AREA: |
| 4101 | if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.toolMajor.valid) { |
| 4102 | touchMajor = in.touchMajor; |
| 4103 | touchMinor = mRawPointerAxes.touchMinor.valid ? in.touchMinor : in.touchMajor; |
| 4104 | toolMajor = in.toolMajor; |
| 4105 | toolMinor = mRawPointerAxes.toolMinor.valid ? in.toolMinor : in.toolMajor; |
| 4106 | size = mRawPointerAxes.touchMinor.valid |
| 4107 | ? avg(in.touchMajor, in.touchMinor) : in.touchMajor; |
| 4108 | } else if (mRawPointerAxes.touchMajor.valid) { |
| 4109 | toolMajor = touchMajor = in.touchMajor; |
| 4110 | toolMinor = touchMinor = mRawPointerAxes.touchMinor.valid |
| 4111 | ? in.touchMinor : in.touchMajor; |
| 4112 | size = mRawPointerAxes.touchMinor.valid |
| 4113 | ? avg(in.touchMajor, in.touchMinor) : in.touchMajor; |
| 4114 | } else if (mRawPointerAxes.toolMajor.valid) { |
| 4115 | touchMajor = toolMajor = in.toolMajor; |
| 4116 | touchMinor = toolMinor = mRawPointerAxes.toolMinor.valid |
| 4117 | ? in.toolMinor : in.toolMajor; |
| 4118 | size = mRawPointerAxes.toolMinor.valid |
| 4119 | ? avg(in.toolMajor, in.toolMinor) : in.toolMajor; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4120 | } else { |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 4121 | ALOG_ASSERT(false, "No touch or tool axes. " |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4122 | "Size calibration should have been resolved to NONE."); |
| 4123 | touchMajor = 0; |
| 4124 | touchMinor = 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4125 | toolMajor = 0; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4126 | toolMinor = 0; |
| 4127 | size = 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4128 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4129 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4130 | if (mCalibration.haveSizeIsSummed && mCalibration.sizeIsSummed) { |
| 4131 | uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count(); |
| 4132 | if (touchingCount > 1) { |
| 4133 | touchMajor /= touchingCount; |
| 4134 | touchMinor /= touchingCount; |
| 4135 | toolMajor /= touchingCount; |
| 4136 | toolMinor /= touchingCount; |
| 4137 | size /= touchingCount; |
| 4138 | } |
| 4139 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4140 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4141 | if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_GEOMETRIC) { |
| 4142 | touchMajor *= mGeometricScale; |
| 4143 | touchMinor *= mGeometricScale; |
| 4144 | toolMajor *= mGeometricScale; |
| 4145 | toolMinor *= mGeometricScale; |
| 4146 | } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_AREA) { |
| 4147 | touchMajor = touchMajor > 0 ? sqrtf(touchMajor) : 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4148 | touchMinor = touchMajor; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4149 | toolMajor = toolMajor > 0 ? sqrtf(toolMajor) : 0; |
| 4150 | toolMinor = toolMajor; |
| 4151 | } else if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_DIAMETER) { |
| 4152 | touchMinor = touchMajor; |
| 4153 | toolMinor = toolMajor; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4154 | } |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4155 | |
| 4156 | mCalibration.applySizeScaleAndBias(&touchMajor); |
| 4157 | mCalibration.applySizeScaleAndBias(&touchMinor); |
| 4158 | mCalibration.applySizeScaleAndBias(&toolMajor); |
| 4159 | mCalibration.applySizeScaleAndBias(&toolMinor); |
| 4160 | size *= mSizeScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4161 | break; |
| 4162 | default: |
| 4163 | touchMajor = 0; |
| 4164 | touchMinor = 0; |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4165 | toolMajor = 0; |
| 4166 | toolMinor = 0; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4167 | size = 0; |
| 4168 | break; |
| 4169 | } |
| 4170 | |
Jeff Brown | a1f89ce | 2011-08-11 00:05:01 -0700 | [diff] [blame] | 4171 | // Pressure |
| 4172 | float pressure; |
| 4173 | switch (mCalibration.pressureCalibration) { |
| 4174 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: |
| 4175 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: |
| 4176 | pressure = in.pressure * mPressureScale; |
| 4177 | break; |
| 4178 | default: |
| 4179 | pressure = in.isHovering ? 0 : 1; |
| 4180 | break; |
| 4181 | } |
| 4182 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4183 | // Tilt and Orientation |
| 4184 | float tilt; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4185 | float orientation; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4186 | if (mHaveTilt) { |
| 4187 | float tiltXAngle = (in.tiltX - mTiltXCenter) * mTiltXScale; |
| 4188 | float tiltYAngle = (in.tiltY - mTiltYCenter) * mTiltYScale; |
| 4189 | orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle)); |
| 4190 | tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle)); |
| 4191 | } else { |
| 4192 | tilt = 0; |
| 4193 | |
| 4194 | switch (mCalibration.orientationCalibration) { |
| 4195 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: |
Jeff Brown | 037f727 | 2012-06-25 17:31:23 -0700 | [diff] [blame] | 4196 | orientation = in.orientation * mOrientationScale; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4197 | break; |
| 4198 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: { |
| 4199 | int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4); |
| 4200 | int32_t c2 = signExtendNybble(in.orientation & 0x0f); |
| 4201 | if (c1 != 0 || c2 != 0) { |
| 4202 | orientation = atan2f(c1, c2) * 0.5f; |
| 4203 | float confidence = hypotf(c1, c2); |
| 4204 | float scale = 1.0f + confidence / 16.0f; |
| 4205 | touchMajor *= scale; |
| 4206 | touchMinor /= scale; |
| 4207 | toolMajor *= scale; |
| 4208 | toolMinor /= scale; |
| 4209 | } else { |
| 4210 | orientation = 0; |
| 4211 | } |
| 4212 | break; |
| 4213 | } |
| 4214 | default: |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4215 | orientation = 0; |
| 4216 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4217 | } |
| 4218 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 4219 | // Distance |
| 4220 | float distance; |
| 4221 | switch (mCalibration.distanceCalibration) { |
| 4222 | case Calibration::DISTANCE_CALIBRATION_SCALED: |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4223 | distance = in.distance * mDistanceScale; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 4224 | break; |
| 4225 | default: |
| 4226 | distance = 0; |
| 4227 | } |
| 4228 | |
Michael Wright | 5e025eb | 2013-05-15 23:16:54 -0700 | [diff] [blame] | 4229 | // Coverage |
| 4230 | int32_t rawLeft, rawTop, rawRight, rawBottom; |
| 4231 | switch (mCalibration.coverageCalibration) { |
| 4232 | case Calibration::COVERAGE_CALIBRATION_BOX: |
| 4233 | rawLeft = (in.toolMinor & 0xffff0000) >> 16; |
| 4234 | rawRight = in.toolMinor & 0x0000ffff; |
| 4235 | rawBottom = in.toolMajor & 0x0000ffff; |
| 4236 | rawTop = (in.toolMajor & 0xffff0000) >> 16; |
| 4237 | break; |
| 4238 | default: |
| 4239 | rawLeft = rawTop = rawRight = rawBottom = 0; |
| 4240 | break; |
| 4241 | } |
| 4242 | |
| 4243 | // X, Y, and the bounding box for coverage information |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4244 | // Adjust coords for surface orientation. |
Michael Wright | 5e025eb | 2013-05-15 23:16:54 -0700 | [diff] [blame] | 4245 | float x, y, left, top, right, bottom; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4246 | switch (mSurfaceOrientation) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4247 | case DISPLAY_ORIENTATION_90: |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 4248 | x = float(in.y - mRawPointerAxes.y.minValue) * mYScale + mYTranslate; |
| 4249 | y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale + mXTranslate; |
Michael Wright | 5e025eb | 2013-05-15 23:16:54 -0700 | [diff] [blame] | 4250 | left = float(rawTop - mRawPointerAxes.y.minValue) * mYScale + mYTranslate; |
| 4251 | right = float(rawBottom- mRawPointerAxes.y.minValue) * mYScale + mYTranslate; |
| 4252 | bottom = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale + mXTranslate; |
| 4253 | top = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale + mXTranslate; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4254 | orientation -= M_PI_2; |
Jason Gerecke | 70bca4c | 2013-03-01 11:49:07 -0800 | [diff] [blame] | 4255 | if (orientation < mOrientedRanges.orientation.min) { |
| 4256 | orientation += (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4257 | } |
| 4258 | break; |
| 4259 | case DISPLAY_ORIENTATION_180: |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 4260 | x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale + mXTranslate; |
| 4261 | y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale + mYTranslate; |
Michael Wright | 5e025eb | 2013-05-15 23:16:54 -0700 | [diff] [blame] | 4262 | left = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale + mXTranslate; |
| 4263 | right = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale + mXTranslate; |
| 4264 | bottom = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale + mYTranslate; |
| 4265 | top = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale + mYTranslate; |
Jason Gerecke | 70bca4c | 2013-03-01 11:49:07 -0800 | [diff] [blame] | 4266 | orientation -= M_PI; |
| 4267 | if (orientation < mOrientedRanges.orientation.min) { |
| 4268 | orientation += (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min); |
| 4269 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4270 | break; |
| 4271 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 4272 | x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale + mYTranslate; |
| 4273 | y = float(in.x - mRawPointerAxes.x.minValue) * mXScale + mXTranslate; |
Michael Wright | 5e025eb | 2013-05-15 23:16:54 -0700 | [diff] [blame] | 4274 | left = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale + mYTranslate; |
| 4275 | right = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale + mYTranslate; |
| 4276 | bottom = float(rawRight - mRawPointerAxes.x.minValue) * mXScale + mXTranslate; |
| 4277 | top = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale + mXTranslate; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4278 | orientation += M_PI_2; |
Jason Gerecke | 70bca4c | 2013-03-01 11:49:07 -0800 | [diff] [blame] | 4279 | if (orientation > mOrientedRanges.orientation.max) { |
| 4280 | orientation -= (mOrientedRanges.orientation.max - mOrientedRanges.orientation.min); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4281 | } |
| 4282 | break; |
| 4283 | default: |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 4284 | x = float(in.x - mRawPointerAxes.x.minValue) * mXScale + mXTranslate; |
| 4285 | y = float(in.y - mRawPointerAxes.y.minValue) * mYScale + mYTranslate; |
Michael Wright | 5e025eb | 2013-05-15 23:16:54 -0700 | [diff] [blame] | 4286 | left = float(rawLeft - mRawPointerAxes.x.minValue) * mXScale + mXTranslate; |
| 4287 | right = float(rawRight - mRawPointerAxes.x.minValue) * mXScale + mXTranslate; |
| 4288 | bottom = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale + mYTranslate; |
| 4289 | top = float(rawTop - mRawPointerAxes.y.minValue) * mYScale + mYTranslate; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4290 | break; |
| 4291 | } |
| 4292 | |
| 4293 | // Write output coords. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4294 | PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4295 | out.clear(); |
| 4296 | out.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4297 | out.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 4298 | out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure); |
| 4299 | out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size); |
| 4300 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor); |
| 4301 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4302 | out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4303 | out.setAxisValue(AMOTION_EVENT_AXIS_TILT, tilt); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4304 | out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance); |
Michael Wright | 5e025eb | 2013-05-15 23:16:54 -0700 | [diff] [blame] | 4305 | if (mCalibration.coverageCalibration == Calibration::COVERAGE_CALIBRATION_BOX) { |
| 4306 | out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_1, left); |
| 4307 | out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_2, top); |
| 4308 | out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_3, right); |
| 4309 | out.setAxisValue(AMOTION_EVENT_AXIS_GENERIC_4, bottom); |
| 4310 | } else { |
| 4311 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor); |
| 4312 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor); |
| 4313 | } |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4314 | |
| 4315 | // Write output properties. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4316 | PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i]; |
| 4317 | uint32_t id = in.id; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4318 | properties.clear(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4319 | properties.id = id; |
| 4320 | properties.toolType = in.toolType; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4321 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4322 | // Write id index. |
| 4323 | mCurrentCookedPointerData.idToIndex[id] = i; |
| 4324 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4325 | } |
| 4326 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4327 | void TouchInputMapper::dispatchPointerUsage(nsecs_t when, uint32_t policyFlags, |
| 4328 | PointerUsage pointerUsage) { |
| 4329 | if (pointerUsage != mPointerUsage) { |
| 4330 | abortPointerUsage(when, policyFlags); |
| 4331 | mPointerUsage = pointerUsage; |
| 4332 | } |
| 4333 | |
| 4334 | switch (mPointerUsage) { |
| 4335 | case POINTER_USAGE_GESTURES: |
| 4336 | dispatchPointerGestures(when, policyFlags, false /*isTimeout*/); |
| 4337 | break; |
| 4338 | case POINTER_USAGE_STYLUS: |
| 4339 | dispatchPointerStylus(when, policyFlags); |
| 4340 | break; |
| 4341 | case POINTER_USAGE_MOUSE: |
| 4342 | dispatchPointerMouse(when, policyFlags); |
| 4343 | break; |
| 4344 | default: |
| 4345 | break; |
| 4346 | } |
| 4347 | } |
| 4348 | |
| 4349 | void TouchInputMapper::abortPointerUsage(nsecs_t when, uint32_t policyFlags) { |
| 4350 | switch (mPointerUsage) { |
| 4351 | case POINTER_USAGE_GESTURES: |
| 4352 | abortPointerGestures(when, policyFlags); |
| 4353 | break; |
| 4354 | case POINTER_USAGE_STYLUS: |
| 4355 | abortPointerStylus(when, policyFlags); |
| 4356 | break; |
| 4357 | case POINTER_USAGE_MOUSE: |
| 4358 | abortPointerMouse(when, policyFlags); |
| 4359 | break; |
| 4360 | default: |
| 4361 | break; |
| 4362 | } |
| 4363 | |
| 4364 | mPointerUsage = POINTER_USAGE_NONE; |
| 4365 | } |
| 4366 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4367 | void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, |
| 4368 | bool isTimeout) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4369 | // Update current gesture coordinates. |
| 4370 | bool cancelPreviousGesture, finishPreviousGesture; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4371 | bool sendEvents = preparePointerGestures(when, |
| 4372 | &cancelPreviousGesture, &finishPreviousGesture, isTimeout); |
| 4373 | if (!sendEvents) { |
| 4374 | return; |
| 4375 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4376 | if (finishPreviousGesture) { |
| 4377 | cancelPreviousGesture = false; |
| 4378 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4379 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4380 | // Update the pointer presentation and spots. |
| 4381 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 4382 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT); |
| 4383 | if (finishPreviousGesture || cancelPreviousGesture) { |
| 4384 | mPointerController->clearSpots(); |
| 4385 | } |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4386 | mPointerController->setSpots(mPointerGesture.currentGestureCoords, |
| 4387 | mPointerGesture.currentGestureIdToIndex, |
| 4388 | mPointerGesture.currentGestureIdBits); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4389 | } else { |
| 4390 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER); |
| 4391 | } |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4392 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4393 | // Show or hide the pointer if needed. |
| 4394 | switch (mPointerGesture.currentGestureMode) { |
| 4395 | case PointerGesture::NEUTRAL: |
| 4396 | case PointerGesture::QUIET: |
| 4397 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS |
| 4398 | && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE |
| 4399 | || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) { |
| 4400 | // Remind the user of where the pointer is after finishing a gesture with spots. |
| 4401 | mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 4402 | } |
| 4403 | break; |
| 4404 | case PointerGesture::TAP: |
| 4405 | case PointerGesture::TAP_DRAG: |
| 4406 | case PointerGesture::BUTTON_CLICK_OR_DRAG: |
| 4407 | case PointerGesture::HOVER: |
| 4408 | case PointerGesture::PRESS: |
| 4409 | // Unfade the pointer when the current gesture manipulates the |
| 4410 | // area directly under the pointer. |
| 4411 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 4412 | break; |
| 4413 | case PointerGesture::SWIPE: |
| 4414 | case PointerGesture::FREEFORM: |
| 4415 | // Fade the pointer when the current gesture manipulates a different |
| 4416 | // area and there are spots to guide the user experience. |
| 4417 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 4418 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 4419 | } else { |
| 4420 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 4421 | } |
| 4422 | break; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4423 | } |
| 4424 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4425 | // Send events! |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4426 | int32_t metaState = getContext()->getGlobalMetaState(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4427 | int32_t buttonState = mCurrentButtonState; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4428 | |
| 4429 | // Update last coordinates of pointers that have moved so that we observe the new |
| 4430 | // 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] | 4431 | bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP |
| 4432 | || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG |
| 4433 | || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4434 | || mPointerGesture.currentGestureMode == PointerGesture::PRESS |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4435 | || mPointerGesture.currentGestureMode == PointerGesture::SWIPE |
| 4436 | || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM; |
| 4437 | bool moveNeeded = false; |
| 4438 | if (down && !cancelPreviousGesture && !finishPreviousGesture |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4439 | && !mPointerGesture.lastGestureIdBits.isEmpty() |
| 4440 | && !mPointerGesture.currentGestureIdBits.isEmpty()) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4441 | BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value |
| 4442 | & mPointerGesture.lastGestureIdBits.value); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4443 | moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4444 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4445 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4446 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4447 | movedGestureIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4448 | if (buttonState != mLastButtonState) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4449 | moveNeeded = true; |
| 4450 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4451 | } |
| 4452 | |
| 4453 | // Send motion events for all pointers that went up or were canceled. |
| 4454 | BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits); |
| 4455 | if (!dispatchedGestureIdBits.isEmpty()) { |
| 4456 | if (cancelPreviousGesture) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4457 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4458 | AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState, |
| 4459 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4460 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4461 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4462 | dispatchedGestureIdBits, -1, |
| 4463 | 0, 0, mPointerGesture.downTime); |
| 4464 | |
| 4465 | dispatchedGestureIdBits.clear(); |
| 4466 | } else { |
| 4467 | BitSet32 upGestureIdBits; |
| 4468 | if (finishPreviousGesture) { |
| 4469 | upGestureIdBits = dispatchedGestureIdBits; |
| 4470 | } else { |
| 4471 | upGestureIdBits.value = dispatchedGestureIdBits.value |
| 4472 | & ~mPointerGesture.currentGestureIdBits.value; |
| 4473 | } |
| 4474 | while (!upGestureIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4475 | uint32_t id = upGestureIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4476 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4477 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4478 | AMOTION_EVENT_ACTION_POINTER_UP, 0, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4479 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4480 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4481 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4482 | dispatchedGestureIdBits, id, |
| 4483 | 0, 0, mPointerGesture.downTime); |
| 4484 | |
| 4485 | dispatchedGestureIdBits.clearBit(id); |
| 4486 | } |
| 4487 | } |
| 4488 | } |
| 4489 | |
| 4490 | // Send motion events for all pointers that moved. |
| 4491 | if (moveNeeded) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4492 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4493 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4494 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4495 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 4496 | dispatchedGestureIdBits, -1, |
| 4497 | 0, 0, mPointerGesture.downTime); |
| 4498 | } |
| 4499 | |
| 4500 | // Send motion events for all pointers that went down. |
| 4501 | if (down) { |
| 4502 | BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value |
| 4503 | & ~dispatchedGestureIdBits.value); |
| 4504 | while (!downGestureIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4505 | uint32_t id = downGestureIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4506 | dispatchedGestureIdBits.markBit(id); |
| 4507 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4508 | if (dispatchedGestureIdBits.count() == 1) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4509 | mPointerGesture.downTime = when; |
| 4510 | } |
| 4511 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4512 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | a611137 | 2011-07-14 21:48:23 -0700 | [diff] [blame] | 4513 | AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4514 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4515 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 4516 | dispatchedGestureIdBits, id, |
| 4517 | 0, 0, mPointerGesture.downTime); |
| 4518 | } |
| 4519 | } |
| 4520 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4521 | // Send motion events for hover. |
| 4522 | if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4523 | dispatchMotion(when, policyFlags, mSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4524 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 4525 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4526 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4527 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 4528 | mPointerGesture.currentGestureIdBits, -1, |
| 4529 | 0, 0, mPointerGesture.downTime); |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4530 | } else if (dispatchedGestureIdBits.isEmpty() |
| 4531 | && !mPointerGesture.lastGestureIdBits.isEmpty()) { |
| 4532 | // Synthesize a hover move event after all pointers go up to indicate that |
| 4533 | // the pointer is hovering again even if the user is not currently touching |
| 4534 | // the touch pad. This ensures that a view will receive a fresh hover enter |
| 4535 | // event after a tap. |
| 4536 | float x, y; |
| 4537 | mPointerController->getPosition(&x, &y); |
| 4538 | |
| 4539 | PointerProperties pointerProperties; |
| 4540 | pointerProperties.clear(); |
| 4541 | pointerProperties.id = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4542 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4543 | |
| 4544 | PointerCoords pointerCoords; |
| 4545 | pointerCoords.clear(); |
| 4546 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4547 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 4548 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4549 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4550 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 4551 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 4552 | mViewport.displayId, 1, &pointerProperties, &pointerCoords, |
| 4553 | 0, 0, mPointerGesture.downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4554 | getListener()->notifyMotion(&args); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4555 | } |
| 4556 | |
| 4557 | // Update state. |
| 4558 | mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode; |
| 4559 | if (!down) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4560 | mPointerGesture.lastGestureIdBits.clear(); |
| 4561 | } else { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4562 | mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits; |
| 4563 | for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4564 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4565 | uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4566 | mPointerGesture.lastGestureProperties[index].copyFrom( |
| 4567 | mPointerGesture.currentGestureProperties[index]); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4568 | mPointerGesture.lastGestureCoords[index].copyFrom( |
| 4569 | mPointerGesture.currentGestureCoords[index]); |
| 4570 | mPointerGesture.lastGestureIdToIndex[id] = index; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4571 | } |
| 4572 | } |
| 4573 | } |
| 4574 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4575 | void TouchInputMapper::abortPointerGestures(nsecs_t when, uint32_t policyFlags) { |
| 4576 | // Cancel previously dispatches pointers. |
| 4577 | if (!mPointerGesture.lastGestureIdBits.isEmpty()) { |
| 4578 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 4579 | int32_t buttonState = mCurrentButtonState; |
| 4580 | dispatchMotion(when, policyFlags, mSource, |
| 4581 | AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState, |
| 4582 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 4583 | mPointerGesture.lastGestureProperties, |
| 4584 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 4585 | mPointerGesture.lastGestureIdBits, -1, |
| 4586 | 0, 0, mPointerGesture.downTime); |
| 4587 | } |
| 4588 | |
| 4589 | // Reset the current pointer gesture. |
| 4590 | mPointerGesture.reset(); |
| 4591 | mPointerVelocityControl.reset(); |
| 4592 | |
| 4593 | // Remove any current spots. |
| 4594 | if (mPointerController != NULL) { |
| 4595 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 4596 | mPointerController->clearSpots(); |
| 4597 | } |
| 4598 | } |
| 4599 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4600 | bool TouchInputMapper::preparePointerGestures(nsecs_t when, |
| 4601 | bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4602 | *outCancelPreviousGesture = false; |
| 4603 | *outFinishPreviousGesture = false; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4604 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4605 | // Handle TAP timeout. |
| 4606 | if (isTimeout) { |
| 4607 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4608 | ALOGD("Gestures: Processing timeout"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4609 | #endif |
| 4610 | |
| 4611 | if (mPointerGesture.lastGestureMode == PointerGesture::TAP) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4612 | if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4613 | // The tap/drag timeout has not yet expired. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4614 | getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4615 | + mConfig.pointerGestureTapDragInterval); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4616 | } else { |
| 4617 | // The tap is finished. |
| 4618 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4619 | ALOGD("Gestures: TAP finished"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4620 | #endif |
| 4621 | *outFinishPreviousGesture = true; |
| 4622 | |
| 4623 | mPointerGesture.activeGestureId = -1; |
| 4624 | mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL; |
| 4625 | mPointerGesture.currentGestureIdBits.clear(); |
| 4626 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4627 | mPointerVelocityControl.reset(); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4628 | return true; |
| 4629 | } |
| 4630 | } |
| 4631 | |
| 4632 | // We did not handle this timeout. |
| 4633 | return false; |
| 4634 | } |
| 4635 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4636 | const uint32_t currentFingerCount = mCurrentFingerIdBits.count(); |
| 4637 | const uint32_t lastFingerCount = mLastFingerIdBits.count(); |
| 4638 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4639 | // Update the velocity tracker. |
| 4640 | { |
| 4641 | VelocityTracker::Position positions[MAX_POINTERS]; |
| 4642 | uint32_t count = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4643 | for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); count++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4644 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 4645 | const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4646 | positions[count].x = pointer.x * mPointerXMovementScale; |
| 4647 | positions[count].y = pointer.y * mPointerYMovementScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4648 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4649 | mPointerGesture.velocityTracker.addMovement(when, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4650 | mCurrentFingerIdBits, positions); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4651 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4652 | |
Michael Wright | 398d309 | 2013-07-02 17:35:00 -0700 | [diff] [blame^] | 4653 | // If the gesture ever enters a mode other than TAP, HOVER or TAP_DRAG, without first returning |
| 4654 | // to NEUTRAL, then we should not generate tap event. |
| 4655 | if (mPointerGesture.lastGestureMode != PointerGesture::HOVER |
| 4656 | && mPointerGesture.lastGestureMode != PointerGesture::TAP |
| 4657 | && mPointerGesture.lastGestureMode != PointerGesture::TAP_DRAG) { |
| 4658 | mPointerGesture.resetTap(); |
| 4659 | } |
| 4660 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4661 | // Pick a new active touch id if needed. |
| 4662 | // Choose an arbitrary pointer that just went down, if there is one. |
| 4663 | // Otherwise choose an arbitrary remaining pointer. |
| 4664 | // 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] | 4665 | // We keep the same active touch id for as long as possible. |
| 4666 | bool activeTouchChanged = false; |
| 4667 | int32_t lastActiveTouchId = mPointerGesture.activeTouchId; |
| 4668 | int32_t activeTouchId = lastActiveTouchId; |
| 4669 | if (activeTouchId < 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4670 | if (!mCurrentFingerIdBits.isEmpty()) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4671 | activeTouchChanged = true; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4672 | activeTouchId = mPointerGesture.activeTouchId = |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4673 | mCurrentFingerIdBits.firstMarkedBit(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4674 | mPointerGesture.firstTouchTime = when; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4675 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4676 | } else if (!mCurrentFingerIdBits.hasBit(activeTouchId)) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4677 | activeTouchChanged = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4678 | if (!mCurrentFingerIdBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4679 | activeTouchId = mPointerGesture.activeTouchId = |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4680 | mCurrentFingerIdBits.firstMarkedBit(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4681 | } else { |
| 4682 | activeTouchId = mPointerGesture.activeTouchId = -1; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4683 | } |
| 4684 | } |
| 4685 | |
| 4686 | // Determine whether we are in quiet time. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4687 | bool isQuietTime = false; |
| 4688 | if (activeTouchId < 0) { |
| 4689 | mPointerGesture.resetQuietTime(); |
| 4690 | } else { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4691 | isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4692 | if (!isQuietTime) { |
| 4693 | if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS |
| 4694 | || mPointerGesture.lastGestureMode == PointerGesture::SWIPE |
| 4695 | || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4696 | && currentFingerCount < 2) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4697 | // Enter quiet time when exiting swipe or freeform state. |
| 4698 | // This is to prevent accidentally entering the hover state and flinging the |
| 4699 | // pointer when finishing a swipe and there is still one pointer left onscreen. |
| 4700 | isQuietTime = true; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4701 | } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4702 | && currentFingerCount >= 2 |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4703 | && !isPointerDown(mCurrentButtonState)) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4704 | // Enter quiet time when releasing the button and there are still two or more |
| 4705 | // fingers down. This may indicate that one finger was used to press the button |
| 4706 | // but it has not gone up yet. |
| 4707 | isQuietTime = true; |
| 4708 | } |
| 4709 | if (isQuietTime) { |
| 4710 | mPointerGesture.quietTime = when; |
| 4711 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4712 | } |
| 4713 | } |
| 4714 | |
| 4715 | // Switch states based on button and pointer state. |
| 4716 | if (isQuietTime) { |
| 4717 | // Case 1: Quiet time. (QUIET) |
| 4718 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4719 | ALOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4720 | + mConfig.pointerGestureQuietInterval - when) * 0.000001f); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4721 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4722 | if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) { |
| 4723 | *outFinishPreviousGesture = true; |
| 4724 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4725 | |
| 4726 | mPointerGesture.activeGestureId = -1; |
| 4727 | mPointerGesture.currentGestureMode = PointerGesture::QUIET; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4728 | mPointerGesture.currentGestureIdBits.clear(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4729 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4730 | mPointerVelocityControl.reset(); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4731 | } else if (isPointerDown(mCurrentButtonState)) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4732 | // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG) |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4733 | // The pointer follows the active touch point. |
| 4734 | // Emit DOWN, MOVE, UP events at the pointer location. |
| 4735 | // |
| 4736 | // Only the active touch matters; other fingers are ignored. This policy helps |
| 4737 | // to handle the case where the user places a second finger on the touch pad |
| 4738 | // to apply the necessary force to depress an integrated button below the surface. |
| 4739 | // We don't want the second finger to be delivered to applications. |
| 4740 | // |
| 4741 | // For this to work well, we need to make sure to track the pointer that is really |
| 4742 | // active. If the user first puts one finger down to click then adds another |
| 4743 | // finger to drag then the active pointer should switch to the finger that is |
| 4744 | // being dragged. |
| 4745 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4746 | ALOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, " |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4747 | "currentFingerCount=%d", activeTouchId, currentFingerCount); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4748 | #endif |
| 4749 | // Reset state when just starting. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4750 | if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4751 | *outFinishPreviousGesture = true; |
| 4752 | mPointerGesture.activeGestureId = 0; |
| 4753 | } |
| 4754 | |
| 4755 | // Switch pointers if needed. |
| 4756 | // Find the fastest pointer and follow it. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4757 | if (activeTouchId >= 0 && currentFingerCount > 1) { |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4758 | int32_t bestId = -1; |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4759 | float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4760 | for (BitSet32 idBits(mCurrentFingerIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4761 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4762 | float vx, vy; |
| 4763 | if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) { |
| 4764 | float speed = hypotf(vx, vy); |
| 4765 | if (speed > bestSpeed) { |
| 4766 | bestId = id; |
| 4767 | bestSpeed = speed; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4768 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 4769 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4770 | } |
| 4771 | if (bestId >= 0 && bestId != activeTouchId) { |
| 4772 | mPointerGesture.activeTouchId = activeTouchId = bestId; |
| 4773 | activeTouchChanged = true; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4774 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4775 | ALOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, " |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4776 | "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4777 | #endif |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4778 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4779 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4780 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4781 | if (activeTouchId >= 0 && mLastFingerIdBits.hasBit(activeTouchId)) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4782 | const RawPointerData::Pointer& currentPointer = |
| 4783 | mCurrentRawPointerData.pointerForId(activeTouchId); |
| 4784 | const RawPointerData::Pointer& lastPointer = |
| 4785 | mLastRawPointerData.pointerForId(activeTouchId); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4786 | float deltaX = (currentPointer.x - lastPointer.x) * mPointerXMovementScale; |
| 4787 | float deltaY = (currentPointer.y - lastPointer.y) * mPointerYMovementScale; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4788 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4789 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4790 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4791 | |
| 4792 | // Move the pointer using a relative motion. |
| 4793 | // When using spots, the click will occur at the position of the anchor |
| 4794 | // spot and all other spots will move there. |
| 4795 | mPointerController->move(deltaX, deltaY); |
| 4796 | } else { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4797 | mPointerVelocityControl.reset(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4798 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4799 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4800 | float x, y; |
| 4801 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 4802 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4803 | mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4804 | mPointerGesture.currentGestureIdBits.clear(); |
| 4805 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4806 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4807 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4808 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4809 | mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4810 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4811 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4812 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 4813 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4814 | } else if (currentFingerCount == 0) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4815 | // Case 3. No fingers down and button is not pressed. (NEUTRAL) |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4816 | if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) { |
| 4817 | *outFinishPreviousGesture = true; |
| 4818 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4819 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4820 | // Watch for taps coming out of HOVER or TAP_DRAG mode. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4821 | // Checking for taps after TAP_DRAG allows us to detect double-taps. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4822 | bool tapped = false; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4823 | if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER |
| 4824 | || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4825 | && lastFingerCount == 1) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4826 | if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4827 | float x, y; |
| 4828 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4829 | if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop |
| 4830 | && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4831 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4832 | ALOGD("Gestures: TAP"); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4833 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4834 | |
| 4835 | mPointerGesture.tapUpTime = when; |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4836 | getContext()->requestTimeoutAtTime(when |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4837 | + mConfig.pointerGestureTapDragInterval); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4838 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4839 | mPointerGesture.activeGestureId = 0; |
| 4840 | mPointerGesture.currentGestureMode = PointerGesture::TAP; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4841 | mPointerGesture.currentGestureIdBits.clear(); |
| 4842 | mPointerGesture.currentGestureIdBits.markBit( |
| 4843 | mPointerGesture.activeGestureId); |
| 4844 | mPointerGesture.currentGestureIdToIndex[ |
| 4845 | mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4846 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4847 | mPointerGesture.currentGestureProperties[0].id = |
| 4848 | mPointerGesture.activeGestureId; |
| 4849 | mPointerGesture.currentGestureProperties[0].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4850 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4851 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4852 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4853 | AMOTION_EVENT_AXIS_X, mPointerGesture.tapX); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4854 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4855 | AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4856 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
| 4857 | AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4858 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4859 | tapped = true; |
| 4860 | } else { |
| 4861 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4862 | ALOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f", |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4863 | x - mPointerGesture.tapX, |
| 4864 | y - mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4865 | #endif |
| 4866 | } |
| 4867 | } else { |
| 4868 | #if DEBUG_GESTURES |
Michael Wright | 398d309 | 2013-07-02 17:35:00 -0700 | [diff] [blame^] | 4869 | if (mPointerGesture.tapDownTime != LLONG_MIN) { |
| 4870 | ALOGD("Gestures: Not a TAP, %0.3fms since down", |
| 4871 | (when - mPointerGesture.tapDownTime) * 0.000001f); |
| 4872 | } else { |
| 4873 | ALOGD("Gestures: Not a TAP, incompatible mode transitions"); |
| 4874 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4875 | #endif |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4876 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4877 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4878 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4879 | mPointerVelocityControl.reset(); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4880 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4881 | if (!tapped) { |
| 4882 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4883 | ALOGD("Gestures: NEUTRAL"); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4884 | #endif |
| 4885 | mPointerGesture.activeGestureId = -1; |
| 4886 | mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4887 | mPointerGesture.currentGestureIdBits.clear(); |
| 4888 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4889 | } else if (currentFingerCount == 1) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4890 | // 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] | 4891 | // The pointer follows the active touch point. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4892 | // When in HOVER, emit HOVER_MOVE events at the pointer location. |
| 4893 | // When in TAP_DRAG, emit MOVE events at the pointer location. |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 4894 | ALOG_ASSERT(activeTouchId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4895 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4896 | mPointerGesture.currentGestureMode = PointerGesture::HOVER; |
| 4897 | if (mPointerGesture.lastGestureMode == PointerGesture::TAP) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4898 | if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4899 | float x, y; |
| 4900 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4901 | if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop |
| 4902 | && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4903 | mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG; |
| 4904 | } else { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4905 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4906 | ALOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f", |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4907 | x - mPointerGesture.tapX, |
| 4908 | y - mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4909 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4910 | } |
| 4911 | } else { |
| 4912 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4913 | ALOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up", |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4914 | (when - mPointerGesture.tapUpTime) * 0.000001f); |
| 4915 | #endif |
| 4916 | } |
| 4917 | } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) { |
| 4918 | mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG; |
| 4919 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4920 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4921 | if (mLastFingerIdBits.hasBit(activeTouchId)) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4922 | const RawPointerData::Pointer& currentPointer = |
| 4923 | mCurrentRawPointerData.pointerForId(activeTouchId); |
| 4924 | const RawPointerData::Pointer& lastPointer = |
| 4925 | mLastRawPointerData.pointerForId(activeTouchId); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4926 | float deltaX = (currentPointer.x - lastPointer.x) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4927 | * mPointerXMovementScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4928 | float deltaY = (currentPointer.y - lastPointer.y) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4929 | * mPointerYMovementScale; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4930 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 4931 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4932 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4933 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4934 | // Move the pointer using a relative motion. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4935 | // 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] | 4936 | mPointerController->move(deltaX, deltaY); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4937 | } else { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4938 | mPointerVelocityControl.reset(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4939 | } |
| 4940 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4941 | bool down; |
| 4942 | if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) { |
| 4943 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4944 | ALOGD("Gestures: TAP_DRAG"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4945 | #endif |
| 4946 | down = true; |
| 4947 | } else { |
| 4948 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 4949 | ALOGD("Gestures: HOVER"); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4950 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4951 | if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) { |
| 4952 | *outFinishPreviousGesture = true; |
| 4953 | } |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4954 | mPointerGesture.activeGestureId = 0; |
| 4955 | down = false; |
| 4956 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4957 | |
| 4958 | float x, y; |
| 4959 | mPointerController->getPosition(&x, &y); |
| 4960 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4961 | mPointerGesture.currentGestureIdBits.clear(); |
| 4962 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4963 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4964 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4965 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
| 4966 | mPointerGesture.currentGestureProperties[0].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 4967 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4968 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4969 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 4970 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4971 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, |
| 4972 | down ? 1.0f : 0.0f); |
| 4973 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4974 | if (lastFingerCount == 0 && currentFingerCount != 0) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4975 | mPointerGesture.resetTap(); |
| 4976 | mPointerGesture.tapDownTime = when; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4977 | mPointerGesture.tapX = x; |
| 4978 | mPointerGesture.tapY = y; |
| 4979 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4980 | } else { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4981 | // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM) |
| 4982 | // We need to provide feedback for each finger that goes down so we cannot wait |
| 4983 | // for the fingers to move before deciding what to do. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4984 | // |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4985 | // The ambiguous case is deciding what to do when there are two fingers down but they |
| 4986 | // have not moved enough to determine whether they are part of a drag or part of a |
| 4987 | // freeform gesture, or just a press or long-press at the pointer location. |
| 4988 | // |
| 4989 | // When there are two fingers we start with the PRESS hypothesis and we generate a |
| 4990 | // down at the pointer location. |
| 4991 | // |
| 4992 | // When the two fingers move enough or when additional fingers are added, we make |
| 4993 | // a decision to transition into SWIPE or FREEFORM mode accordingly. |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 4994 | ALOG_ASSERT(activeTouchId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4995 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4996 | bool settled = when >= mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4997 | + mConfig.pointerGestureMultitouchSettleInterval; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4998 | if (mPointerGesture.lastGestureMode != PointerGesture::PRESS |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4999 | && mPointerGesture.lastGestureMode != PointerGesture::SWIPE |
| 5000 | && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5001 | *outFinishPreviousGesture = true; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5002 | } else if (!settled && currentFingerCount > lastFingerCount) { |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 5003 | // Additional pointers have gone down but not yet settled. |
| 5004 | // Reset the gesture. |
| 5005 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5006 | ALOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, " |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5007 | "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5008 | + mConfig.pointerGestureMultitouchSettleInterval - when) |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 5009 | * 0.000001f); |
| 5010 | #endif |
| 5011 | *outCancelPreviousGesture = true; |
| 5012 | } else { |
| 5013 | // Continue previous gesture. |
| 5014 | mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode; |
| 5015 | } |
| 5016 | |
| 5017 | if (*outFinishPreviousGesture || *outCancelPreviousGesture) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5018 | mPointerGesture.currentGestureMode = PointerGesture::PRESS; |
| 5019 | mPointerGesture.activeGestureId = 0; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 5020 | mPointerGesture.referenceIdBits.clear(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5021 | mPointerVelocityControl.reset(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5022 | |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 5023 | // 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] | 5024 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5025 | ALOGD("Gestures: Using centroid as reference for MULTITOUCH, " |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 5026 | "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5027 | + mConfig.pointerGestureMultitouchSettleInterval - when) |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 5028 | * 0.000001f); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5029 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5030 | mCurrentRawPointerData.getCentroidOfTouchingPointers( |
| 5031 | &mPointerGesture.referenceTouchX, |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 5032 | &mPointerGesture.referenceTouchY); |
| 5033 | mPointerController->getPosition(&mPointerGesture.referenceGestureX, |
| 5034 | &mPointerGesture.referenceGestureY); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5035 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5036 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5037 | // 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] | 5038 | for (BitSet32 idBits(mCurrentFingerIdBits.value |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5039 | & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) { |
| 5040 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5041 | mPointerGesture.referenceDeltas[id].dx = 0; |
| 5042 | mPointerGesture.referenceDeltas[id].dy = 0; |
| 5043 | } |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5044 | mPointerGesture.referenceIdBits = mCurrentFingerIdBits; |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5045 | |
| 5046 | // Add delta for all fingers and calculate a common movement delta. |
| 5047 | float commonDeltaX = 0, commonDeltaY = 0; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5048 | BitSet32 commonIdBits(mLastFingerIdBits.value |
| 5049 | & mCurrentFingerIdBits.value); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5050 | for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) { |
| 5051 | bool first = (idBits == commonIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5052 | uint32_t id = idBits.clearFirstMarkedBit(); |
| 5053 | const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id); |
| 5054 | const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5055 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
| 5056 | delta.dx += cpd.x - lpd.x; |
| 5057 | delta.dy += cpd.y - lpd.y; |
| 5058 | |
| 5059 | if (first) { |
| 5060 | commonDeltaX = delta.dx; |
| 5061 | commonDeltaY = delta.dy; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5062 | } else { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5063 | commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx); |
| 5064 | commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy); |
| 5065 | } |
| 5066 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5067 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5068 | // Consider transitions from PRESS to SWIPE or MULTITOUCH. |
| 5069 | if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) { |
| 5070 | float dist[MAX_POINTER_ID + 1]; |
| 5071 | int32_t distOverThreshold = 0; |
| 5072 | for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5073 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5074 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5075 | dist[id] = hypotf(delta.dx * mPointerXZoomScale, |
| 5076 | delta.dy * mPointerYZoomScale); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5077 | if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5078 | distOverThreshold += 1; |
| 5079 | } |
| 5080 | } |
| 5081 | |
| 5082 | // Only transition when at least two pointers have moved further than |
| 5083 | // the minimum distance threshold. |
| 5084 | if (distOverThreshold >= 2) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5085 | if (currentFingerCount > 2) { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5086 | // There are more than two pointers, switch to FREEFORM. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5087 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5088 | ALOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5089 | currentFingerCount); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5090 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5091 | *outCancelPreviousGesture = true; |
| 5092 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 5093 | } else { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5094 | // There are exactly two pointers. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5095 | BitSet32 idBits(mCurrentFingerIdBits); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5096 | uint32_t id1 = idBits.clearFirstMarkedBit(); |
| 5097 | uint32_t id2 = idBits.firstMarkedBit(); |
| 5098 | const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1); |
| 5099 | const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2); |
| 5100 | float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y); |
| 5101 | if (mutualDistance > mPointerGestureMaxSwipeWidth) { |
| 5102 | // There are two pointers but they are too far apart for a SWIPE, |
| 5103 | // switch to FREEFORM. |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5104 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5105 | ALOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5106 | mutualDistance, mPointerGestureMaxSwipeWidth); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5107 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5108 | *outCancelPreviousGesture = true; |
| 5109 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 5110 | } else { |
| 5111 | // There are two pointers. Wait for both pointers to start moving |
| 5112 | // before deciding whether this is a SWIPE or FREEFORM gesture. |
| 5113 | float dist1 = dist[id1]; |
| 5114 | float dist2 = dist[id2]; |
| 5115 | if (dist1 >= mConfig.pointerGestureMultitouchMinDistance |
| 5116 | && dist2 >= mConfig.pointerGestureMultitouchMinDistance) { |
| 5117 | // Calculate the dot product of the displacement vectors. |
| 5118 | // When the vectors are oriented in approximately the same direction, |
| 5119 | // the angle betweeen them is near zero and the cosine of the angle |
| 5120 | // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2). |
| 5121 | PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1]; |
| 5122 | PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2]; |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5123 | float dx1 = delta1.dx * mPointerXZoomScale; |
| 5124 | float dy1 = delta1.dy * mPointerYZoomScale; |
| 5125 | float dx2 = delta2.dx * mPointerXZoomScale; |
| 5126 | float dy2 = delta2.dy * mPointerYZoomScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5127 | float dot = dx1 * dx2 + dy1 * dy2; |
| 5128 | float cosine = dot / (dist1 * dist2); // denominator always > 0 |
| 5129 | if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) { |
| 5130 | // Pointers are moving in the same direction. Switch to SWIPE. |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5131 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5132 | ALOGD("Gestures: PRESS transitioned to SWIPE, " |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5133 | "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, " |
| 5134 | "cosine %0.3f >= %0.3f", |
| 5135 | dist1, mConfig.pointerGestureMultitouchMinDistance, |
| 5136 | dist2, mConfig.pointerGestureMultitouchMinDistance, |
| 5137 | cosine, mConfig.pointerGestureSwipeTransitionAngleCosine); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5138 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5139 | mPointerGesture.currentGestureMode = PointerGesture::SWIPE; |
| 5140 | } else { |
| 5141 | // Pointers are moving in different directions. Switch to FREEFORM. |
| 5142 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5143 | ALOGD("Gestures: PRESS transitioned to FREEFORM, " |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5144 | "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, " |
| 5145 | "cosine %0.3f < %0.3f", |
| 5146 | dist1, mConfig.pointerGestureMultitouchMinDistance, |
| 5147 | dist2, mConfig.pointerGestureMultitouchMinDistance, |
| 5148 | cosine, mConfig.pointerGestureSwipeTransitionAngleCosine); |
| 5149 | #endif |
| 5150 | *outCancelPreviousGesture = true; |
| 5151 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 5152 | } |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5153 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5154 | } |
| 5155 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5156 | } |
| 5157 | } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5158 | // Switch from SWIPE to FREEFORM if additional pointers go down. |
| 5159 | // Cancel previous gesture. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5160 | if (currentFingerCount > 2) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5161 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5162 | ALOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5163 | currentFingerCount); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5164 | #endif |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5165 | *outCancelPreviousGesture = true; |
| 5166 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 5167 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5168 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 5169 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5170 | // Move the reference points based on the overall group motion of the fingers |
| 5171 | // except in PRESS mode while waiting for a transition to occur. |
| 5172 | if (mPointerGesture.currentGestureMode != PointerGesture::PRESS |
| 5173 | && (commonDeltaX || commonDeltaY)) { |
| 5174 | for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5175 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 5176 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5177 | delta.dx = 0; |
| 5178 | delta.dy = 0; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5179 | } |
| 5180 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5181 | mPointerGesture.referenceTouchX += commonDeltaX; |
| 5182 | mPointerGesture.referenceTouchY += commonDeltaY; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 5183 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5184 | commonDeltaX *= mPointerXMovementScale; |
| 5185 | commonDeltaY *= mPointerYMovementScale; |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 5186 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5187 | rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5188 | mPointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 5189 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 5190 | mPointerGesture.referenceGestureX += commonDeltaX; |
| 5191 | mPointerGesture.referenceGestureY += commonDeltaY; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5192 | } |
| 5193 | |
| 5194 | // Report gestures. |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 5195 | if (mPointerGesture.currentGestureMode == PointerGesture::PRESS |
| 5196 | || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) { |
| 5197 | // PRESS or SWIPE mode. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5198 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5199 | ALOGD("Gestures: PRESS or SWIPE activeTouchId=%d," |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5200 | "activeGestureId=%d, currentTouchPointerCount=%d", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5201 | activeTouchId, mPointerGesture.activeGestureId, currentFingerCount); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5202 | #endif |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 5203 | ALOG_ASSERT(mPointerGesture.activeGestureId >= 0); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5204 | |
| 5205 | mPointerGesture.currentGestureIdBits.clear(); |
| 5206 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 5207 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5208 | mPointerGesture.currentGestureProperties[0].clear(); |
| 5209 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
| 5210 | mPointerGesture.currentGestureProperties[0].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5211 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5212 | mPointerGesture.currentGestureCoords[0].clear(); |
| 5213 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 5214 | mPointerGesture.referenceGestureX); |
| 5215 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, |
| 5216 | mPointerGesture.referenceGestureY); |
| 5217 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5218 | } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) { |
| 5219 | // FREEFORM mode. |
| 5220 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5221 | ALOGD("Gestures: FREEFORM activeTouchId=%d," |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5222 | "activeGestureId=%d, currentTouchPointerCount=%d", |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5223 | activeTouchId, mPointerGesture.activeGestureId, currentFingerCount); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5224 | #endif |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 5225 | ALOG_ASSERT(mPointerGesture.activeGestureId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5226 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5227 | mPointerGesture.currentGestureIdBits.clear(); |
| 5228 | |
| 5229 | BitSet32 mappedTouchIdBits; |
| 5230 | BitSet32 usedGestureIdBits; |
| 5231 | if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) { |
| 5232 | // Initially, assign the active gesture id to the active touch point |
| 5233 | // if there is one. No other touch id bits are mapped yet. |
| 5234 | if (!*outCancelPreviousGesture) { |
| 5235 | mappedTouchIdBits.markBit(activeTouchId); |
| 5236 | usedGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 5237 | mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] = |
| 5238 | mPointerGesture.activeGestureId; |
| 5239 | } else { |
| 5240 | mPointerGesture.activeGestureId = -1; |
| 5241 | } |
| 5242 | } else { |
| 5243 | // Otherwise, assume we mapped all touches from the previous frame. |
| 5244 | // Reuse all mappings that are still applicable. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5245 | mappedTouchIdBits.value = mLastFingerIdBits.value |
| 5246 | & mCurrentFingerIdBits.value; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5247 | usedGestureIdBits = mPointerGesture.lastGestureIdBits; |
| 5248 | |
| 5249 | // Check whether we need to choose a new active gesture id because the |
| 5250 | // current went went up. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5251 | for (BitSet32 upTouchIdBits(mLastFingerIdBits.value |
| 5252 | & ~mCurrentFingerIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5253 | !upTouchIdBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5254 | uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5255 | uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId]; |
| 5256 | if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) { |
| 5257 | mPointerGesture.activeGestureId = -1; |
| 5258 | break; |
| 5259 | } |
| 5260 | } |
| 5261 | } |
| 5262 | |
| 5263 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5264 | ALOGD("Gestures: FREEFORM follow up " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5265 | "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, " |
| 5266 | "activeGestureId=%d", |
| 5267 | mappedTouchIdBits.value, usedGestureIdBits.value, |
| 5268 | mPointerGesture.activeGestureId); |
| 5269 | #endif |
| 5270 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5271 | BitSet32 idBits(mCurrentFingerIdBits); |
| 5272 | for (uint32_t i = 0; i < currentFingerCount; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5273 | uint32_t touchId = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5274 | uint32_t gestureId; |
| 5275 | if (!mappedTouchIdBits.hasBit(touchId)) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5276 | gestureId = usedGestureIdBits.markFirstUnmarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5277 | mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId; |
| 5278 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5279 | ALOGD("Gestures: FREEFORM " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5280 | "new mapping for touch id %d -> gesture id %d", |
| 5281 | touchId, gestureId); |
| 5282 | #endif |
| 5283 | } else { |
| 5284 | gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId]; |
| 5285 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5286 | ALOGD("Gestures: FREEFORM " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5287 | "existing mapping for touch id %d -> gesture id %d", |
| 5288 | touchId, gestureId); |
| 5289 | #endif |
| 5290 | } |
| 5291 | mPointerGesture.currentGestureIdBits.markBit(gestureId); |
| 5292 | mPointerGesture.currentGestureIdToIndex[gestureId] = i; |
| 5293 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5294 | const RawPointerData::Pointer& pointer = |
| 5295 | mCurrentRawPointerData.pointerForId(touchId); |
| 5296 | float deltaX = (pointer.x - mPointerGesture.referenceTouchX) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5297 | * mPointerXZoomScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5298 | float deltaY = (pointer.y - mPointerGesture.referenceTouchY) |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5299 | * mPointerYZoomScale; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5300 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5301 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5302 | mPointerGesture.currentGestureProperties[i].clear(); |
| 5303 | mPointerGesture.currentGestureProperties[i].id = gestureId; |
| 5304 | mPointerGesture.currentGestureProperties[i].toolType = |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5305 | AMOTION_EVENT_TOOL_TYPE_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5306 | mPointerGesture.currentGestureCoords[i].clear(); |
| 5307 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 5308 | AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5309 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
Jeff Brown | 612891e | 2011-07-15 20:44:17 -0700 | [diff] [blame] | 5310 | AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5311 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
| 5312 | AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
| 5313 | } |
| 5314 | |
| 5315 | if (mPointerGesture.activeGestureId < 0) { |
| 5316 | mPointerGesture.activeGestureId = |
| 5317 | mPointerGesture.currentGestureIdBits.firstMarkedBit(); |
| 5318 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5319 | ALOGD("Gestures: FREEFORM new " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5320 | "activeGestureId=%d", mPointerGesture.activeGestureId); |
| 5321 | #endif |
| 5322 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5323 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5324 | } |
| 5325 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5326 | mPointerController->setButtonState(mCurrentButtonState); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5327 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5328 | #if DEBUG_GESTURES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5329 | ALOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, " |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5330 | "currentGestureMode=%d, currentGestureIdBits=0x%08x, " |
| 5331 | "lastGestureMode=%d, lastGestureIdBits=0x%08x", |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5332 | toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture), |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 5333 | mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value, |
| 5334 | mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5335 | for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5336 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5337 | uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5338 | const PointerProperties& properties = mPointerGesture.currentGestureProperties[index]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5339 | const PointerCoords& coords = mPointerGesture.currentGestureCoords[index]; |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5340 | ALOGD(" currentGesture[%d]: index=%d, toolType=%d, " |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5341 | "x=%0.3f, y=%0.3f, pressure=%0.3f", |
| 5342 | id, index, properties.toolType, |
| 5343 | coords.getAxisValue(AMOTION_EVENT_AXIS_X), |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5344 | coords.getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 5345 | coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); |
| 5346 | } |
| 5347 | for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5348 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5349 | uint32_t index = mPointerGesture.lastGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5350 | const PointerProperties& properties = mPointerGesture.lastGestureProperties[index]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5351 | const PointerCoords& coords = mPointerGesture.lastGestureCoords[index]; |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5352 | ALOGD(" lastGesture[%d]: index=%d, toolType=%d, " |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5353 | "x=%0.3f, y=%0.3f, pressure=%0.3f", |
| 5354 | id, index, properties.toolType, |
| 5355 | coords.getAxisValue(AMOTION_EVENT_AXIS_X), |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5356 | coords.getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 5357 | coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); |
| 5358 | } |
| 5359 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 5360 | return true; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5361 | } |
| 5362 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5363 | void TouchInputMapper::dispatchPointerStylus(nsecs_t when, uint32_t policyFlags) { |
| 5364 | mPointerSimple.currentCoords.clear(); |
| 5365 | mPointerSimple.currentProperties.clear(); |
| 5366 | |
| 5367 | bool down, hovering; |
| 5368 | if (!mCurrentStylusIdBits.isEmpty()) { |
| 5369 | uint32_t id = mCurrentStylusIdBits.firstMarkedBit(); |
| 5370 | uint32_t index = mCurrentCookedPointerData.idToIndex[id]; |
| 5371 | float x = mCurrentCookedPointerData.pointerCoords[index].getX(); |
| 5372 | float y = mCurrentCookedPointerData.pointerCoords[index].getY(); |
| 5373 | mPointerController->setPosition(x, y); |
| 5374 | |
| 5375 | hovering = mCurrentCookedPointerData.hoveringIdBits.hasBit(id); |
| 5376 | down = !hovering; |
| 5377 | |
| 5378 | mPointerController->getPosition(&x, &y); |
| 5379 | mPointerSimple.currentCoords.copyFrom(mCurrentCookedPointerData.pointerCoords[index]); |
| 5380 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 5381 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 5382 | mPointerSimple.currentProperties.id = 0; |
| 5383 | mPointerSimple.currentProperties.toolType = |
| 5384 | mCurrentCookedPointerData.pointerProperties[index].toolType; |
| 5385 | } else { |
| 5386 | down = false; |
| 5387 | hovering = false; |
| 5388 | } |
| 5389 | |
| 5390 | dispatchPointerSimple(when, policyFlags, down, hovering); |
| 5391 | } |
| 5392 | |
| 5393 | void TouchInputMapper::abortPointerStylus(nsecs_t when, uint32_t policyFlags) { |
| 5394 | abortPointerSimple(when, policyFlags); |
| 5395 | } |
| 5396 | |
| 5397 | void TouchInputMapper::dispatchPointerMouse(nsecs_t when, uint32_t policyFlags) { |
| 5398 | mPointerSimple.currentCoords.clear(); |
| 5399 | mPointerSimple.currentProperties.clear(); |
| 5400 | |
| 5401 | bool down, hovering; |
| 5402 | if (!mCurrentMouseIdBits.isEmpty()) { |
| 5403 | uint32_t id = mCurrentMouseIdBits.firstMarkedBit(); |
| 5404 | uint32_t currentIndex = mCurrentRawPointerData.idToIndex[id]; |
| 5405 | if (mLastMouseIdBits.hasBit(id)) { |
| 5406 | uint32_t lastIndex = mCurrentRawPointerData.idToIndex[id]; |
| 5407 | float deltaX = (mCurrentRawPointerData.pointers[currentIndex].x |
| 5408 | - mLastRawPointerData.pointers[lastIndex].x) |
| 5409 | * mPointerXMovementScale; |
| 5410 | float deltaY = (mCurrentRawPointerData.pointers[currentIndex].y |
| 5411 | - mLastRawPointerData.pointers[lastIndex].y) |
| 5412 | * mPointerYMovementScale; |
| 5413 | |
| 5414 | rotateDelta(mSurfaceOrientation, &deltaX, &deltaY); |
| 5415 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
| 5416 | |
| 5417 | mPointerController->move(deltaX, deltaY); |
| 5418 | } else { |
| 5419 | mPointerVelocityControl.reset(); |
| 5420 | } |
| 5421 | |
| 5422 | down = isPointerDown(mCurrentButtonState); |
| 5423 | hovering = !down; |
| 5424 | |
| 5425 | float x, y; |
| 5426 | mPointerController->getPosition(&x, &y); |
| 5427 | mPointerSimple.currentCoords.copyFrom( |
| 5428 | mCurrentCookedPointerData.pointerCoords[currentIndex]); |
| 5429 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 5430 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 5431 | mPointerSimple.currentCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, |
| 5432 | hovering ? 0.0f : 1.0f); |
| 5433 | mPointerSimple.currentProperties.id = 0; |
| 5434 | mPointerSimple.currentProperties.toolType = |
| 5435 | mCurrentCookedPointerData.pointerProperties[currentIndex].toolType; |
| 5436 | } else { |
| 5437 | mPointerVelocityControl.reset(); |
| 5438 | |
| 5439 | down = false; |
| 5440 | hovering = false; |
| 5441 | } |
| 5442 | |
| 5443 | dispatchPointerSimple(when, policyFlags, down, hovering); |
| 5444 | } |
| 5445 | |
| 5446 | void TouchInputMapper::abortPointerMouse(nsecs_t when, uint32_t policyFlags) { |
| 5447 | abortPointerSimple(when, policyFlags); |
| 5448 | |
| 5449 | mPointerVelocityControl.reset(); |
| 5450 | } |
| 5451 | |
| 5452 | void TouchInputMapper::dispatchPointerSimple(nsecs_t when, uint32_t policyFlags, |
| 5453 | bool down, bool hovering) { |
| 5454 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 5455 | |
| 5456 | if (mPointerController != NULL) { |
| 5457 | if (down || hovering) { |
| 5458 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER); |
| 5459 | mPointerController->clearSpots(); |
| 5460 | mPointerController->setButtonState(mCurrentButtonState); |
| 5461 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 5462 | } else if (!down && !hovering && (mPointerSimple.down || mPointerSimple.hovering)) { |
| 5463 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 5464 | } |
| 5465 | } |
| 5466 | |
| 5467 | if (mPointerSimple.down && !down) { |
| 5468 | mPointerSimple.down = false; |
| 5469 | |
| 5470 | // Send up. |
| 5471 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5472 | AMOTION_EVENT_ACTION_UP, 0, metaState, mLastButtonState, 0, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 5473 | mViewport.displayId, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5474 | 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords, |
| 5475 | mOrientedXPrecision, mOrientedYPrecision, |
| 5476 | mPointerSimple.downTime); |
| 5477 | getListener()->notifyMotion(&args); |
| 5478 | } |
| 5479 | |
| 5480 | if (mPointerSimple.hovering && !hovering) { |
| 5481 | mPointerSimple.hovering = false; |
| 5482 | |
| 5483 | // Send hover exit. |
| 5484 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5485 | AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 5486 | mViewport.displayId, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5487 | 1, &mPointerSimple.lastProperties, &mPointerSimple.lastCoords, |
| 5488 | mOrientedXPrecision, mOrientedYPrecision, |
| 5489 | mPointerSimple.downTime); |
| 5490 | getListener()->notifyMotion(&args); |
| 5491 | } |
| 5492 | |
| 5493 | if (down) { |
| 5494 | if (!mPointerSimple.down) { |
| 5495 | mPointerSimple.down = true; |
| 5496 | mPointerSimple.downTime = when; |
| 5497 | |
| 5498 | // Send down. |
| 5499 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5500 | AMOTION_EVENT_ACTION_DOWN, 0, metaState, mCurrentButtonState, 0, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 5501 | mViewport.displayId, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5502 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5503 | mOrientedXPrecision, mOrientedYPrecision, |
| 5504 | mPointerSimple.downTime); |
| 5505 | getListener()->notifyMotion(&args); |
| 5506 | } |
| 5507 | |
| 5508 | // Send move. |
| 5509 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5510 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, mCurrentButtonState, 0, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 5511 | mViewport.displayId, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5512 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5513 | mOrientedXPrecision, mOrientedYPrecision, |
| 5514 | mPointerSimple.downTime); |
| 5515 | getListener()->notifyMotion(&args); |
| 5516 | } |
| 5517 | |
| 5518 | if (hovering) { |
| 5519 | if (!mPointerSimple.hovering) { |
| 5520 | mPointerSimple.hovering = true; |
| 5521 | |
| 5522 | // Send hover enter. |
| 5523 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5524 | AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 5525 | mViewport.displayId, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5526 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5527 | mOrientedXPrecision, mOrientedYPrecision, |
| 5528 | mPointerSimple.downTime); |
| 5529 | getListener()->notifyMotion(&args); |
| 5530 | } |
| 5531 | |
| 5532 | // Send hover move. |
| 5533 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5534 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 5535 | mViewport.displayId, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5536 | 1, &mPointerSimple.currentProperties, &mPointerSimple.currentCoords, |
| 5537 | mOrientedXPrecision, mOrientedYPrecision, |
| 5538 | mPointerSimple.downTime); |
| 5539 | getListener()->notifyMotion(&args); |
| 5540 | } |
| 5541 | |
| 5542 | if (mCurrentRawVScroll || mCurrentRawHScroll) { |
| 5543 | float vscroll = mCurrentRawVScroll; |
| 5544 | float hscroll = mCurrentRawHScroll; |
| 5545 | mWheelYVelocityControl.move(when, NULL, &vscroll); |
| 5546 | mWheelXVelocityControl.move(when, &hscroll, NULL); |
| 5547 | |
| 5548 | // Send scroll. |
| 5549 | PointerCoords pointerCoords; |
| 5550 | pointerCoords.copyFrom(mPointerSimple.currentCoords); |
| 5551 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll); |
| 5552 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll); |
| 5553 | |
| 5554 | NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags, |
| 5555 | AMOTION_EVENT_ACTION_SCROLL, 0, metaState, mCurrentButtonState, 0, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 5556 | mViewport.displayId, |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5557 | 1, &mPointerSimple.currentProperties, &pointerCoords, |
| 5558 | mOrientedXPrecision, mOrientedYPrecision, |
| 5559 | mPointerSimple.downTime); |
| 5560 | getListener()->notifyMotion(&args); |
| 5561 | } |
| 5562 | |
| 5563 | // Save state. |
| 5564 | if (down || hovering) { |
| 5565 | mPointerSimple.lastCoords.copyFrom(mPointerSimple.currentCoords); |
| 5566 | mPointerSimple.lastProperties.copyFrom(mPointerSimple.currentProperties); |
| 5567 | } else { |
| 5568 | mPointerSimple.reset(); |
| 5569 | } |
| 5570 | } |
| 5571 | |
| 5572 | void TouchInputMapper::abortPointerSimple(nsecs_t when, uint32_t policyFlags) { |
| 5573 | mPointerSimple.currentCoords.clear(); |
| 5574 | mPointerSimple.currentProperties.clear(); |
| 5575 | |
| 5576 | dispatchPointerSimple(when, policyFlags, false, false); |
| 5577 | } |
| 5578 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5579 | void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5580 | int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags, |
| 5581 | const PointerProperties* properties, const PointerCoords* coords, |
| 5582 | const uint32_t* idToIndex, BitSet32 idBits, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5583 | int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) { |
| 5584 | PointerCoords pointerCoords[MAX_POINTERS]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5585 | PointerProperties pointerProperties[MAX_POINTERS]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5586 | uint32_t pointerCount = 0; |
| 5587 | while (!idBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5588 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5589 | uint32_t index = idToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5590 | pointerProperties[pointerCount].copyFrom(properties[index]); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5591 | pointerCoords[pointerCount].copyFrom(coords[index]); |
| 5592 | |
| 5593 | if (changedId >= 0 && id == uint32_t(changedId)) { |
| 5594 | action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
| 5595 | } |
| 5596 | |
| 5597 | pointerCount += 1; |
| 5598 | } |
| 5599 | |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 5600 | ALOG_ASSERT(pointerCount != 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5601 | |
| 5602 | if (changedId >= 0 && pointerCount == 1) { |
| 5603 | // Replace initial down and final up action. |
| 5604 | // We can compare the action without masking off the changed pointer index |
| 5605 | // because we know the index is 0. |
| 5606 | if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 5607 | action = AMOTION_EVENT_ACTION_DOWN; |
| 5608 | } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 5609 | action = AMOTION_EVENT_ACTION_UP; |
| 5610 | } else { |
| 5611 | // Can't happen. |
Steve Block | ec193de | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 5612 | ALOG_ASSERT(false); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5613 | } |
| 5614 | } |
| 5615 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5616 | NotifyMotionArgs args(when, getDeviceId(), source, policyFlags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5617 | action, flags, metaState, buttonState, edgeFlags, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 5618 | mViewport.displayId, pointerCount, pointerProperties, pointerCoords, |
| 5619 | xPrecision, yPrecision, downTime); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5620 | getListener()->notifyMotion(&args); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5621 | } |
| 5622 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5623 | bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5624 | const PointerCoords* inCoords, const uint32_t* inIdToIndex, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5625 | PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex, |
| 5626 | BitSet32 idBits) const { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5627 | bool changed = false; |
| 5628 | while (!idBits.isEmpty()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5629 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5630 | uint32_t inIndex = inIdToIndex[id]; |
| 5631 | uint32_t outIndex = outIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5632 | |
| 5633 | const PointerProperties& curInProperties = inProperties[inIndex]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5634 | const PointerCoords& curInCoords = inCoords[inIndex]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5635 | PointerProperties& curOutProperties = outProperties[outIndex]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5636 | PointerCoords& curOutCoords = outCoords[outIndex]; |
| 5637 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5638 | if (curInProperties != curOutProperties) { |
| 5639 | curOutProperties.copyFrom(curInProperties); |
| 5640 | changed = true; |
| 5641 | } |
| 5642 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5643 | if (curInCoords != curOutCoords) { |
| 5644 | curOutCoords.copyFrom(curInCoords); |
| 5645 | changed = true; |
| 5646 | } |
| 5647 | } |
| 5648 | return changed; |
| 5649 | } |
| 5650 | |
| 5651 | void TouchInputMapper::fadePointer() { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5652 | if (mPointerController != NULL) { |
| 5653 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 5654 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5655 | } |
| 5656 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5657 | bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) { |
| 5658 | return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue |
| 5659 | && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5660 | } |
| 5661 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5662 | const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit( |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 5663 | int32_t x, int32_t y) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5664 | size_t numVirtualKeys = mVirtualKeys.size(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 5665 | for (size_t i = 0; i < numVirtualKeys; i++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5666 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5667 | |
| 5668 | #if DEBUG_VIRTUAL_KEYS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5669 | ALOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, " |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5670 | "left=%d, top=%d, right=%d, bottom=%d", |
| 5671 | x, y, |
| 5672 | virtualKey.keyCode, virtualKey.scanCode, |
| 5673 | virtualKey.hitLeft, virtualKey.hitTop, |
| 5674 | virtualKey.hitRight, virtualKey.hitBottom); |
| 5675 | #endif |
| 5676 | |
| 5677 | if (virtualKey.isHit(x, y)) { |
| 5678 | return & virtualKey; |
| 5679 | } |
| 5680 | } |
| 5681 | |
| 5682 | return NULL; |
| 5683 | } |
| 5684 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5685 | void TouchInputMapper::assignPointerIds() { |
| 5686 | uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount; |
| 5687 | uint32_t lastPointerCount = mLastRawPointerData.pointerCount; |
| 5688 | |
| 5689 | mCurrentRawPointerData.clearIdBits(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5690 | |
| 5691 | if (currentPointerCount == 0) { |
| 5692 | // No pointers to assign. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5693 | return; |
| 5694 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5695 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5696 | if (lastPointerCount == 0) { |
| 5697 | // All pointers are new. |
| 5698 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
| 5699 | uint32_t id = i; |
| 5700 | mCurrentRawPointerData.pointers[i].id = id; |
| 5701 | mCurrentRawPointerData.idToIndex[id] = i; |
| 5702 | mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i)); |
| 5703 | } |
| 5704 | return; |
| 5705 | } |
| 5706 | |
| 5707 | if (currentPointerCount == 1 && lastPointerCount == 1 |
| 5708 | && mCurrentRawPointerData.pointers[0].toolType |
| 5709 | == mLastRawPointerData.pointers[0].toolType) { |
| 5710 | // Only one pointer and no change in count so it must have the same id as before. |
| 5711 | uint32_t id = mLastRawPointerData.pointers[0].id; |
| 5712 | mCurrentRawPointerData.pointers[0].id = id; |
| 5713 | mCurrentRawPointerData.idToIndex[id] = 0; |
| 5714 | mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0)); |
| 5715 | return; |
| 5716 | } |
| 5717 | |
| 5718 | // General case. |
| 5719 | // We build a heap of squared euclidean distances between current and last pointers |
| 5720 | // associated with the current and last pointer indices. Then, we find the best |
| 5721 | // match (by distance) for each current pointer. |
| 5722 | // The pointers must have the same tool type but it is possible for them to |
| 5723 | // transition from hovering to touching or vice-versa while retaining the same id. |
| 5724 | PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS]; |
| 5725 | |
| 5726 | uint32_t heapSize = 0; |
| 5727 | for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount; |
| 5728 | currentPointerIndex++) { |
| 5729 | for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount; |
| 5730 | lastPointerIndex++) { |
| 5731 | const RawPointerData::Pointer& currentPointer = |
| 5732 | mCurrentRawPointerData.pointers[currentPointerIndex]; |
| 5733 | const RawPointerData::Pointer& lastPointer = |
| 5734 | mLastRawPointerData.pointers[lastPointerIndex]; |
| 5735 | if (currentPointer.toolType == lastPointer.toolType) { |
| 5736 | int64_t deltaX = currentPointer.x - lastPointer.x; |
| 5737 | int64_t deltaY = currentPointer.y - lastPointer.y; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5738 | |
| 5739 | uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY); |
| 5740 | |
| 5741 | // Insert new element into the heap (sift up). |
| 5742 | heap[heapSize].currentPointerIndex = currentPointerIndex; |
| 5743 | heap[heapSize].lastPointerIndex = lastPointerIndex; |
| 5744 | heap[heapSize].distance = distance; |
| 5745 | heapSize += 1; |
| 5746 | } |
| 5747 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5748 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5749 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5750 | // Heapify |
| 5751 | for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) { |
| 5752 | startIndex -= 1; |
| 5753 | for (uint32_t parentIndex = startIndex; ;) { |
| 5754 | uint32_t childIndex = parentIndex * 2 + 1; |
| 5755 | if (childIndex >= heapSize) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5756 | break; |
| 5757 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5758 | |
| 5759 | if (childIndex + 1 < heapSize |
| 5760 | && heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 5761 | childIndex += 1; |
| 5762 | } |
| 5763 | |
| 5764 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 5765 | break; |
| 5766 | } |
| 5767 | |
| 5768 | swap(heap[parentIndex], heap[childIndex]); |
| 5769 | parentIndex = childIndex; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5770 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5771 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5772 | |
| 5773 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5774 | ALOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5775 | for (size_t i = 0; i < heapSize; i++) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5776 | ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5777 | i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, |
| 5778 | heap[i].distance); |
| 5779 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5780 | #endif |
| 5781 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5782 | // Pull matches out by increasing order of distance. |
| 5783 | // To avoid reassigning pointers that have already been matched, the loop keeps track |
| 5784 | // of which last and current pointers have been matched using the matchedXXXBits variables. |
| 5785 | // It also tracks the used pointer id bits. |
| 5786 | BitSet32 matchedLastBits(0); |
| 5787 | BitSet32 matchedCurrentBits(0); |
| 5788 | BitSet32 usedIdBits(0); |
| 5789 | bool first = true; |
| 5790 | for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) { |
| 5791 | while (heapSize > 0) { |
| 5792 | if (first) { |
| 5793 | // The first time through the loop, we just consume the root element of |
| 5794 | // the heap (the one with smallest distance). |
| 5795 | first = false; |
| 5796 | } else { |
| 5797 | // Previous iterations consumed the root element of the heap. |
| 5798 | // Pop root element off of the heap (sift down). |
| 5799 | heap[0] = heap[heapSize]; |
| 5800 | for (uint32_t parentIndex = 0; ;) { |
| 5801 | uint32_t childIndex = parentIndex * 2 + 1; |
| 5802 | if (childIndex >= heapSize) { |
| 5803 | break; |
| 5804 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5805 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5806 | if (childIndex + 1 < heapSize |
| 5807 | && heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 5808 | childIndex += 1; |
| 5809 | } |
| 5810 | |
| 5811 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 5812 | break; |
| 5813 | } |
| 5814 | |
| 5815 | swap(heap[parentIndex], heap[childIndex]); |
| 5816 | parentIndex = childIndex; |
| 5817 | } |
| 5818 | |
| 5819 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5820 | ALOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5821 | for (size_t i = 0; i < heapSize; i++) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5822 | ALOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5823 | i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, |
| 5824 | heap[i].distance); |
| 5825 | } |
| 5826 | #endif |
| 5827 | } |
| 5828 | |
| 5829 | heapSize -= 1; |
| 5830 | |
| 5831 | uint32_t currentPointerIndex = heap[0].currentPointerIndex; |
| 5832 | if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched |
| 5833 | |
| 5834 | uint32_t lastPointerIndex = heap[0].lastPointerIndex; |
| 5835 | if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched |
| 5836 | |
| 5837 | matchedCurrentBits.markBit(currentPointerIndex); |
| 5838 | matchedLastBits.markBit(lastPointerIndex); |
| 5839 | |
| 5840 | uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id; |
| 5841 | mCurrentRawPointerData.pointers[currentPointerIndex].id = id; |
| 5842 | mCurrentRawPointerData.idToIndex[id] = currentPointerIndex; |
| 5843 | mCurrentRawPointerData.markIdBit(id, |
| 5844 | mCurrentRawPointerData.isHovering(currentPointerIndex)); |
| 5845 | usedIdBits.markBit(id); |
| 5846 | |
| 5847 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5848 | ALOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5849 | lastPointerIndex, currentPointerIndex, id, heap[0].distance); |
| 5850 | #endif |
| 5851 | break; |
| 5852 | } |
| 5853 | } |
| 5854 | |
| 5855 | // Assign fresh ids to pointers that were not matched in the process. |
| 5856 | for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) { |
| 5857 | uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit(); |
| 5858 | uint32_t id = usedIdBits.markFirstUnmarkedBit(); |
| 5859 | |
| 5860 | mCurrentRawPointerData.pointers[currentPointerIndex].id = id; |
| 5861 | mCurrentRawPointerData.idToIndex[id] = currentPointerIndex; |
| 5862 | mCurrentRawPointerData.markIdBit(id, |
| 5863 | mCurrentRawPointerData.isHovering(currentPointerIndex)); |
| 5864 | |
| 5865 | #if DEBUG_POINTER_ASSIGNMENT |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 5866 | ALOGD("assignPointerIds - assigned: cur=%d, id=%d", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5867 | currentPointerIndex, id); |
| 5868 | #endif |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5869 | } |
| 5870 | } |
| 5871 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5872 | int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5873 | if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) { |
| 5874 | return AKEY_STATE_VIRTUAL; |
| 5875 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5876 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5877 | size_t numVirtualKeys = mVirtualKeys.size(); |
| 5878 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 5879 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
| 5880 | if (virtualKey.keyCode == keyCode) { |
| 5881 | return AKEY_STATE_UP; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5882 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5883 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5884 | |
| 5885 | return AKEY_STATE_UNKNOWN; |
| 5886 | } |
| 5887 | |
| 5888 | int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5889 | if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) { |
| 5890 | return AKEY_STATE_VIRTUAL; |
| 5891 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5892 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5893 | size_t numVirtualKeys = mVirtualKeys.size(); |
| 5894 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 5895 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
| 5896 | if (virtualKey.scanCode == scanCode) { |
| 5897 | return AKEY_STATE_UP; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5898 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5899 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5900 | |
| 5901 | return AKEY_STATE_UNKNOWN; |
| 5902 | } |
| 5903 | |
| 5904 | bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 5905 | const int32_t* keyCodes, uint8_t* outFlags) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5906 | size_t numVirtualKeys = mVirtualKeys.size(); |
| 5907 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 5908 | const VirtualKey& virtualKey = mVirtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5909 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5910 | for (size_t i = 0; i < numCodes; i++) { |
| 5911 | if (virtualKey.keyCode == keyCodes[i]) { |
| 5912 | outFlags[i] = 1; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5913 | } |
| 5914 | } |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5915 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5916 | |
| 5917 | return true; |
| 5918 | } |
| 5919 | |
| 5920 | |
| 5921 | // --- SingleTouchInputMapper --- |
| 5922 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 5923 | SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) : |
| 5924 | TouchInputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5925 | } |
| 5926 | |
| 5927 | SingleTouchInputMapper::~SingleTouchInputMapper() { |
| 5928 | } |
| 5929 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5930 | void SingleTouchInputMapper::reset(nsecs_t when) { |
| 5931 | mSingleTouchMotionAccumulator.reset(getDevice()); |
| 5932 | |
| 5933 | TouchInputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5934 | } |
| 5935 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5936 | void SingleTouchInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5937 | TouchInputMapper::process(rawEvent); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5938 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5939 | mSingleTouchMotionAccumulator.process(rawEvent); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5940 | } |
| 5941 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5942 | void SingleTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) { |
Jeff Brown | d87c6d5 | 2011-08-10 14:55:59 -0700 | [diff] [blame] | 5943 | if (mTouchButtonAccumulator.isToolActive()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5944 | mCurrentRawPointerData.pointerCount = 1; |
| 5945 | mCurrentRawPointerData.idToIndex[0] = 0; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5946 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5947 | bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE |
| 5948 | && (mTouchButtonAccumulator.isHovering() |
| 5949 | || (mRawPointerAxes.pressure.valid |
| 5950 | && mSingleTouchMotionAccumulator.getAbsolutePressure() <= 0)); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5951 | mCurrentRawPointerData.markIdBit(0, isHovering); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5952 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5953 | RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0]; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5954 | outPointer.id = 0; |
| 5955 | outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX(); |
| 5956 | outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY(); |
| 5957 | outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure(); |
| 5958 | outPointer.touchMajor = 0; |
| 5959 | outPointer.touchMinor = 0; |
| 5960 | outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth(); |
| 5961 | outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth(); |
| 5962 | outPointer.orientation = 0; |
| 5963 | outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5964 | outPointer.tiltX = mSingleTouchMotionAccumulator.getAbsoluteTiltX(); |
| 5965 | outPointer.tiltY = mSingleTouchMotionAccumulator.getAbsoluteTiltY(); |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5966 | outPointer.toolType = mTouchButtonAccumulator.getToolType(); |
| 5967 | if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 5968 | outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 5969 | } |
| 5970 | outPointer.isHovering = isHovering; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5971 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5972 | } |
| 5973 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5974 | void SingleTouchInputMapper::configureRawPointerAxes() { |
| 5975 | TouchInputMapper::configureRawPointerAxes(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5976 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 5977 | getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x); |
| 5978 | getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y); |
| 5979 | getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure); |
| 5980 | getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor); |
| 5981 | getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 5982 | getAbsoluteAxisInfo(ABS_TILT_X, &mRawPointerAxes.tiltX); |
| 5983 | getAbsoluteAxisInfo(ABS_TILT_Y, &mRawPointerAxes.tiltY); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5984 | } |
| 5985 | |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 5986 | bool SingleTouchInputMapper::hasStylus() const { |
| 5987 | return mTouchButtonAccumulator.hasStylus(); |
| 5988 | } |
| 5989 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5990 | |
| 5991 | // --- MultiTouchInputMapper --- |
| 5992 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 5993 | MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) : |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 5994 | TouchInputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5995 | } |
| 5996 | |
| 5997 | MultiTouchInputMapper::~MultiTouchInputMapper() { |
| 5998 | } |
| 5999 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 6000 | void MultiTouchInputMapper::reset(nsecs_t when) { |
| 6001 | mMultiTouchMotionAccumulator.reset(getDevice()); |
| 6002 | |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 6003 | mPointerIdBits.clear(); |
Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 6004 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 6005 | TouchInputMapper::reset(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 6006 | } |
| 6007 | |
| 6008 | void MultiTouchInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 6009 | TouchInputMapper::process(rawEvent); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 6010 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 6011 | mMultiTouchMotionAccumulator.process(rawEvent); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 6012 | } |
| 6013 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 6014 | void MultiTouchInputMapper::syncTouch(nsecs_t when, bool* outHavePointerIds) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 6015 | size_t inCount = mMultiTouchMotionAccumulator.getSlotCount(); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 6016 | size_t outCount = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 6017 | BitSet32 newPointerIdBits; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 6018 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 6019 | for (size_t inIndex = 0; inIndex < inCount; inIndex++) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 6020 | const MultiTouchMotionAccumulator::Slot* inSlot = |
| 6021 | mMultiTouchMotionAccumulator.getSlot(inIndex); |
| 6022 | if (!inSlot->isInUse()) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 6023 | continue; |
| 6024 | } |
| 6025 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 6026 | if (outCount >= MAX_POINTERS) { |
| 6027 | #if DEBUG_POINTERS |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 6028 | ALOGD("MultiTouch device %s emitted more than maximum of %d pointers; " |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 6029 | "ignoring the rest.", |
| 6030 | getDeviceName().string(), MAX_POINTERS); |
| 6031 | #endif |
| 6032 | break; // too many fingers! |
| 6033 | } |
| 6034 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 6035 | RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount]; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 6036 | outPointer.x = inSlot->getX(); |
| 6037 | outPointer.y = inSlot->getY(); |
| 6038 | outPointer.pressure = inSlot->getPressure(); |
| 6039 | outPointer.touchMajor = inSlot->getTouchMajor(); |
| 6040 | outPointer.touchMinor = inSlot->getTouchMinor(); |
| 6041 | outPointer.toolMajor = inSlot->getToolMajor(); |
| 6042 | outPointer.toolMinor = inSlot->getToolMinor(); |
| 6043 | outPointer.orientation = inSlot->getOrientation(); |
| 6044 | outPointer.distance = inSlot->getDistance(); |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 6045 | outPointer.tiltX = 0; |
| 6046 | outPointer.tiltY = 0; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 6047 | |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 6048 | outPointer.toolType = inSlot->getToolType(); |
| 6049 | if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 6050 | outPointer.toolType = mTouchButtonAccumulator.getToolType(); |
| 6051 | if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) { |
| 6052 | outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 6053 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 6054 | } |
| 6055 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 6056 | bool isHovering = mTouchButtonAccumulator.getToolType() != AMOTION_EVENT_TOOL_TYPE_MOUSE |
| 6057 | && (mTouchButtonAccumulator.isHovering() |
| 6058 | || (mRawPointerAxes.pressure.valid && inSlot->getPressure() <= 0)); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 6059 | outPointer.isHovering = isHovering; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 6060 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 6061 | // Assign pointer id using tracking id if available. |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 6062 | if (*outHavePointerIds) { |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 6063 | int32_t trackingId = inSlot->getTrackingId(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 6064 | int32_t id = -1; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 6065 | if (trackingId >= 0) { |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 6066 | for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 6067 | uint32_t n = idBits.clearFirstMarkedBit(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 6068 | if (mPointerTrackingIdMap[n] == trackingId) { |
| 6069 | id = n; |
| 6070 | } |
| 6071 | } |
| 6072 | |
| 6073 | if (id < 0 && !mPointerIdBits.isFull()) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 6074 | id = mPointerIdBits.markFirstUnmarkedBit(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 6075 | mPointerTrackingIdMap[id] = trackingId; |
| 6076 | } |
| 6077 | } |
| 6078 | if (id < 0) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 6079 | *outHavePointerIds = false; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 6080 | mCurrentRawPointerData.clearIdBits(); |
| 6081 | newPointerIdBits.clear(); |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 6082 | } else { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 6083 | outPointer.id = id; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 6084 | mCurrentRawPointerData.idToIndex[id] = outCount; |
| 6085 | mCurrentRawPointerData.markIdBit(id, isHovering); |
| 6086 | newPointerIdBits.markBit(id); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 6087 | } |
| 6088 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 6089 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 6090 | outCount += 1; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 6091 | } |
| 6092 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 6093 | mCurrentRawPointerData.pointerCount = outCount; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 6094 | mPointerIdBits = newPointerIdBits; |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 6095 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 6096 | mMultiTouchMotionAccumulator.finishSync(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 6097 | } |
| 6098 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 6099 | void MultiTouchInputMapper::configureRawPointerAxes() { |
| 6100 | TouchInputMapper::configureRawPointerAxes(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 6101 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 6102 | getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x); |
| 6103 | getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y); |
| 6104 | getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor); |
| 6105 | getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor); |
| 6106 | getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor); |
| 6107 | getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor); |
| 6108 | getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation); |
| 6109 | getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure); |
| 6110 | getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance); |
| 6111 | getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId); |
| 6112 | getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 6113 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 6114 | if (mRawPointerAxes.trackingId.valid |
| 6115 | && mRawPointerAxes.slot.valid |
| 6116 | && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) { |
| 6117 | size_t slotCount = mRawPointerAxes.slot.maxValue + 1; |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 6118 | if (slotCount > MAX_SLOTS) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 6119 | ALOGW("MultiTouch Device %s reported %d slots but the framework " |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 6120 | "only supports a maximum of %d slots at this time.", |
Jeff Brown | 49754db | 2011-07-01 17:37:58 -0700 | [diff] [blame] | 6121 | getDeviceName().string(), slotCount, MAX_SLOTS); |
| 6122 | slotCount = MAX_SLOTS; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 6123 | } |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 6124 | mMultiTouchMotionAccumulator.configure(getDevice(), |
| 6125 | slotCount, true /*usingSlotsProtocol*/); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 6126 | } else { |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 6127 | mMultiTouchMotionAccumulator.configure(getDevice(), |
| 6128 | MAX_POINTERS, false /*usingSlotsProtocol*/); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 6129 | } |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 6130 | } |
| 6131 | |
Jeff Brown | 00710e9 | 2012-04-19 15:18:26 -0700 | [diff] [blame] | 6132 | bool MultiTouchInputMapper::hasStylus() const { |
| 6133 | return mMultiTouchMotionAccumulator.hasStylus() |
| 6134 | || mTouchButtonAccumulator.hasStylus(); |
| 6135 | } |
| 6136 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 6137 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6138 | // --- JoystickInputMapper --- |
| 6139 | |
| 6140 | JoystickInputMapper::JoystickInputMapper(InputDevice* device) : |
| 6141 | InputMapper(device) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6142 | } |
| 6143 | |
| 6144 | JoystickInputMapper::~JoystickInputMapper() { |
| 6145 | } |
| 6146 | |
| 6147 | uint32_t JoystickInputMapper::getSources() { |
| 6148 | return AINPUT_SOURCE_JOYSTICK; |
| 6149 | } |
| 6150 | |
| 6151 | void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 6152 | InputMapper::populateDeviceInfo(info); |
| 6153 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6154 | for (size_t i = 0; i < mAxes.size(); i++) { |
| 6155 | const Axis& axis = mAxes.valueAt(i); |
Michael Wright | 2b08c61 | 2013-04-24 20:05:10 -0700 | [diff] [blame] | 6156 | addMotionRange(axis.axisInfo.axis, axis, info); |
| 6157 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6158 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
Michael Wright | 2b08c61 | 2013-04-24 20:05:10 -0700 | [diff] [blame] | 6159 | addMotionRange(axis.axisInfo.highAxis, axis, info); |
| 6160 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6161 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6162 | } |
| 6163 | } |
| 6164 | |
Michael Wright | 2b08c61 | 2013-04-24 20:05:10 -0700 | [diff] [blame] | 6165 | void JoystickInputMapper::addMotionRange(int32_t axisId, const Axis& axis, |
| 6166 | InputDeviceInfo* info) { |
| 6167 | info->addMotionRange(axisId, AINPUT_SOURCE_JOYSTICK, |
| 6168 | axis.min, axis.max, axis.flat, axis.fuzz, axis.resolution); |
| 6169 | /* In order to ease the transition for developers from using the old axes |
| 6170 | * to the newer, more semantically correct axes, we'll continue to register |
| 6171 | * the old axes as duplicates of their corresponding new ones. */ |
| 6172 | int32_t compatAxis = getCompatAxis(axisId); |
| 6173 | if (compatAxis >= 0) { |
| 6174 | info->addMotionRange(compatAxis, AINPUT_SOURCE_JOYSTICK, |
| 6175 | axis.min, axis.max, axis.flat, axis.fuzz, axis.resolution); |
| 6176 | } |
| 6177 | } |
| 6178 | |
| 6179 | /* A mapping from axes the joystick actually has to the axes that should be |
| 6180 | * artificially created for compatibility purposes. |
| 6181 | * Returns -1 if no compatibility axis is needed. */ |
| 6182 | int32_t JoystickInputMapper::getCompatAxis(int32_t axis) { |
| 6183 | switch(axis) { |
| 6184 | case AMOTION_EVENT_AXIS_LTRIGGER: |
| 6185 | return AMOTION_EVENT_AXIS_BRAKE; |
| 6186 | case AMOTION_EVENT_AXIS_RTRIGGER: |
| 6187 | return AMOTION_EVENT_AXIS_GAS; |
| 6188 | } |
| 6189 | return -1; |
| 6190 | } |
| 6191 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6192 | void JoystickInputMapper::dump(String8& dump) { |
| 6193 | dump.append(INDENT2 "Joystick Input Mapper:\n"); |
| 6194 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6195 | dump.append(INDENT3 "Axes:\n"); |
| 6196 | size_t numAxes = mAxes.size(); |
| 6197 | for (size_t i = 0; i < numAxes; i++) { |
| 6198 | const Axis& axis = mAxes.valueAt(i); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6199 | const char* label = getAxisLabel(axis.axisInfo.axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6200 | if (label) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6201 | dump.appendFormat(INDENT4 "%s", label); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6202 | } else { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6203 | dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6204 | } |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6205 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 6206 | label = getAxisLabel(axis.axisInfo.highAxis); |
| 6207 | if (label) { |
| 6208 | dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue); |
| 6209 | } else { |
| 6210 | dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis, |
| 6211 | axis.axisInfo.splitValue); |
| 6212 | } |
| 6213 | } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) { |
| 6214 | dump.append(" (invert)"); |
| 6215 | } |
| 6216 | |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 6217 | dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f, resolution=%0.5f\n", |
| 6218 | axis.min, axis.max, axis.flat, axis.fuzz, axis.resolution); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6219 | dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, " |
| 6220 | "highScale=%0.5f, highOffset=%0.5f\n", |
| 6221 | axis.scale, axis.offset, axis.highScale, axis.highOffset); |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 6222 | dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, " |
| 6223 | "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n", |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6224 | mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue, |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 6225 | axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6226 | } |
| 6227 | } |
| 6228 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 6229 | void JoystickInputMapper::configure(nsecs_t when, |
| 6230 | const InputReaderConfiguration* config, uint32_t changes) { |
| 6231 | InputMapper::configure(when, config, changes); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6232 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 6233 | if (!changes) { // first time only |
| 6234 | // Collect all axes. |
| 6235 | for (int32_t abs = 0; abs <= ABS_MAX; abs++) { |
Jeff Brown | 9ee285a | 2011-08-31 12:56:34 -0700 | [diff] [blame] | 6236 | if (!(getAbsAxisUsage(abs, getDevice()->getClasses()) |
| 6237 | & INPUT_DEVICE_CLASS_JOYSTICK)) { |
| 6238 | continue; // axis must be claimed by a different device |
| 6239 | } |
| 6240 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 6241 | RawAbsoluteAxisInfo rawAxisInfo; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 6242 | getAbsoluteAxisInfo(abs, &rawAxisInfo); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 6243 | if (rawAxisInfo.valid) { |
| 6244 | // Map axis. |
| 6245 | AxisInfo axisInfo; |
| 6246 | bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo); |
| 6247 | if (!explicitlyMapped) { |
| 6248 | // Axis is not explicitly mapped, will choose a generic axis later. |
| 6249 | axisInfo.mode = AxisInfo::MODE_NORMAL; |
| 6250 | axisInfo.axis = -1; |
| 6251 | } |
| 6252 | |
| 6253 | // Apply flat override. |
| 6254 | int32_t rawFlat = axisInfo.flatOverride < 0 |
| 6255 | ? rawAxisInfo.flat : axisInfo.flatOverride; |
| 6256 | |
| 6257 | // Calculate scaling factors and limits. |
| 6258 | Axis axis; |
| 6259 | if (axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 6260 | float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue); |
| 6261 | float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue); |
| 6262 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 6263 | scale, 0.0f, highScale, 0.0f, |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 6264 | 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale, |
| 6265 | rawAxisInfo.resolution * scale); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 6266 | } else if (isCenteredAxis(axisInfo.axis)) { |
| 6267 | float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); |
| 6268 | float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale; |
| 6269 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 6270 | scale, offset, scale, offset, |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 6271 | -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale, |
| 6272 | rawAxisInfo.resolution * scale); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 6273 | } else { |
| 6274 | float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); |
| 6275 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 6276 | scale, 0.0f, scale, 0.0f, |
Michael Wright | c6091c6 | 2013-04-01 20:56:04 -0700 | [diff] [blame] | 6277 | 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale, |
| 6278 | rawAxisInfo.resolution * scale); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 6279 | } |
| 6280 | |
| 6281 | // To eliminate noise while the joystick is at rest, filter out small variations |
| 6282 | // in axis values up front. |
| 6283 | axis.filter = axis.flat * 0.25f; |
| 6284 | |
| 6285 | mAxes.add(abs, axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6286 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6287 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6288 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 6289 | // If there are too many axes, start dropping them. |
| 6290 | // Prefer to keep explicitly mapped axes. |
| 6291 | if (mAxes.size() > PointerCoords::MAX_AXES) { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 6292 | 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] | 6293 | getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES); |
| 6294 | pruneAxes(true); |
| 6295 | pruneAxes(false); |
| 6296 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6297 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 6298 | // Assign generic axis ids to remaining axes. |
| 6299 | int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1; |
| 6300 | size_t numAxes = mAxes.size(); |
| 6301 | for (size_t i = 0; i < numAxes; i++) { |
| 6302 | Axis& axis = mAxes.editValueAt(i); |
| 6303 | if (axis.axisInfo.axis < 0) { |
| 6304 | while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16 |
| 6305 | && haveAxis(nextGenericAxisId)) { |
| 6306 | nextGenericAxisId += 1; |
| 6307 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6308 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 6309 | if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) { |
| 6310 | axis.axisInfo.axis = nextGenericAxisId; |
| 6311 | nextGenericAxisId += 1; |
| 6312 | } else { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 6313 | 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] | 6314 | "have already been assigned to other axes.", |
| 6315 | getDeviceName().string(), mAxes.keyAt(i)); |
| 6316 | mAxes.removeItemsAt(i--); |
| 6317 | numAxes -= 1; |
| 6318 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6319 | } |
| 6320 | } |
| 6321 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6322 | } |
| 6323 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6324 | bool JoystickInputMapper::haveAxis(int32_t axisId) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6325 | size_t numAxes = mAxes.size(); |
| 6326 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6327 | const Axis& axis = mAxes.valueAt(i); |
| 6328 | if (axis.axisInfo.axis == axisId |
| 6329 | || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT |
| 6330 | && axis.axisInfo.highAxis == axisId)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6331 | return true; |
| 6332 | } |
| 6333 | } |
| 6334 | return false; |
| 6335 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6336 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6337 | void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) { |
| 6338 | size_t i = mAxes.size(); |
| 6339 | while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) { |
| 6340 | if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) { |
| 6341 | continue; |
| 6342 | } |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 6343 | ALOGI("Discarding joystick '%s' axis %d because there are too many axes.", |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6344 | getDeviceName().string(), mAxes.keyAt(i)); |
| 6345 | mAxes.removeItemsAt(i); |
| 6346 | } |
| 6347 | } |
| 6348 | |
| 6349 | bool JoystickInputMapper::isCenteredAxis(int32_t axis) { |
| 6350 | switch (axis) { |
| 6351 | case AMOTION_EVENT_AXIS_X: |
| 6352 | case AMOTION_EVENT_AXIS_Y: |
| 6353 | case AMOTION_EVENT_AXIS_Z: |
| 6354 | case AMOTION_EVENT_AXIS_RX: |
| 6355 | case AMOTION_EVENT_AXIS_RY: |
| 6356 | case AMOTION_EVENT_AXIS_RZ: |
| 6357 | case AMOTION_EVENT_AXIS_HAT_X: |
| 6358 | case AMOTION_EVENT_AXIS_HAT_Y: |
| 6359 | case AMOTION_EVENT_AXIS_ORIENTATION: |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6360 | case AMOTION_EVENT_AXIS_RUDDER: |
| 6361 | case AMOTION_EVENT_AXIS_WHEEL: |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6362 | return true; |
| 6363 | default: |
| 6364 | return false; |
| 6365 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6366 | } |
| 6367 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 6368 | void JoystickInputMapper::reset(nsecs_t when) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6369 | // Recenter all axes. |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6370 | size_t numAxes = mAxes.size(); |
| 6371 | for (size_t i = 0; i < numAxes; i++) { |
| 6372 | Axis& axis = mAxes.editValueAt(i); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6373 | axis.resetValue(); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6374 | } |
| 6375 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 6376 | InputMapper::reset(when); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6377 | } |
| 6378 | |
| 6379 | void JoystickInputMapper::process(const RawEvent* rawEvent) { |
| 6380 | switch (rawEvent->type) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6381 | case EV_ABS: { |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 6382 | ssize_t index = mAxes.indexOfKey(rawEvent->code); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6383 | if (index >= 0) { |
| 6384 | Axis& axis = mAxes.editValueAt(index); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6385 | float newValue, highNewValue; |
| 6386 | switch (axis.axisInfo.mode) { |
| 6387 | case AxisInfo::MODE_INVERT: |
| 6388 | newValue = (axis.rawAxisInfo.maxValue - rawEvent->value) |
| 6389 | * axis.scale + axis.offset; |
| 6390 | highNewValue = 0.0f; |
| 6391 | break; |
| 6392 | case AxisInfo::MODE_SPLIT: |
| 6393 | if (rawEvent->value < axis.axisInfo.splitValue) { |
| 6394 | newValue = (axis.axisInfo.splitValue - rawEvent->value) |
| 6395 | * axis.scale + axis.offset; |
| 6396 | highNewValue = 0.0f; |
| 6397 | } else if (rawEvent->value > axis.axisInfo.splitValue) { |
| 6398 | newValue = 0.0f; |
| 6399 | highNewValue = (rawEvent->value - axis.axisInfo.splitValue) |
| 6400 | * axis.highScale + axis.highOffset; |
| 6401 | } else { |
| 6402 | newValue = 0.0f; |
| 6403 | highNewValue = 0.0f; |
| 6404 | } |
| 6405 | break; |
| 6406 | default: |
| 6407 | newValue = rawEvent->value * axis.scale + axis.offset; |
| 6408 | highNewValue = 0.0f; |
| 6409 | break; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6410 | } |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6411 | axis.newValue = newValue; |
| 6412 | axis.highNewValue = highNewValue; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6413 | } |
| 6414 | break; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6415 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6416 | |
| 6417 | case EV_SYN: |
Jeff Brown | 49ccac5 | 2012-04-11 18:27:33 -0700 | [diff] [blame] | 6418 | switch (rawEvent->code) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6419 | case SYN_REPORT: |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6420 | sync(rawEvent->when, false /*force*/); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6421 | break; |
| 6422 | } |
| 6423 | break; |
| 6424 | } |
| 6425 | } |
| 6426 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6427 | void JoystickInputMapper::sync(nsecs_t when, bool force) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6428 | if (!filterAxes(force)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6429 | return; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6430 | } |
| 6431 | |
| 6432 | int32_t metaState = mContext->getGlobalMetaState(); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 6433 | int32_t buttonState = 0; |
| 6434 | |
| 6435 | PointerProperties pointerProperties; |
| 6436 | pointerProperties.clear(); |
| 6437 | pointerProperties.id = 0; |
| 6438 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6439 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6440 | PointerCoords pointerCoords; |
| 6441 | pointerCoords.clear(); |
| 6442 | |
| 6443 | size_t numAxes = mAxes.size(); |
| 6444 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6445 | const Axis& axis = mAxes.valueAt(i); |
Michael Wright | 2b08c61 | 2013-04-24 20:05:10 -0700 | [diff] [blame] | 6446 | setPointerCoordsAxisValue(&pointerCoords, axis.axisInfo.axis, axis.currentValue); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6447 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
Michael Wright | 2b08c61 | 2013-04-24 20:05:10 -0700 | [diff] [blame] | 6448 | setPointerCoordsAxisValue(&pointerCoords, axis.axisInfo.highAxis, |
| 6449 | axis.highCurrentValue); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6450 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6451 | } |
| 6452 | |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 6453 | // Moving a joystick axis should not wake the device because joysticks can |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 6454 | // be fairly noisy even when not in use. On the other hand, pushing a gamepad |
| 6455 | // button will likely wake the device. |
| 6456 | // TODO: Use the input device configuration to control this behavior more finely. |
| 6457 | uint32_t policyFlags = 0; |
| 6458 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 6459 | NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 6460 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 6461 | ADISPLAY_ID_NONE, 1, &pointerProperties, &pointerCoords, 0, 0, 0); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 6462 | getListener()->notifyMotion(&args); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6463 | } |
| 6464 | |
Michael Wright | 2b08c61 | 2013-04-24 20:05:10 -0700 | [diff] [blame] | 6465 | void JoystickInputMapper::setPointerCoordsAxisValue(PointerCoords* pointerCoords, |
| 6466 | int32_t axis, float value) { |
| 6467 | pointerCoords->setAxisValue(axis, value); |
| 6468 | /* In order to ease the transition for developers from using the old axes |
| 6469 | * to the newer, more semantically correct axes, we'll continue to produce |
| 6470 | * values for the old axes as mirrors of the value of their corresponding |
| 6471 | * new axes. */ |
| 6472 | int32_t compatAxis = getCompatAxis(axis); |
| 6473 | if (compatAxis >= 0) { |
| 6474 | pointerCoords->setAxisValue(compatAxis, value); |
| 6475 | } |
| 6476 | } |
| 6477 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6478 | bool JoystickInputMapper::filterAxes(bool force) { |
| 6479 | bool atLeastOneSignificantChange = force; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6480 | size_t numAxes = mAxes.size(); |
| 6481 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 6482 | Axis& axis = mAxes.editValueAt(i); |
| 6483 | if (force || hasValueChangedSignificantly(axis.filter, |
| 6484 | axis.newValue, axis.currentValue, axis.min, axis.max)) { |
| 6485 | axis.currentValue = axis.newValue; |
| 6486 | atLeastOneSignificantChange = true; |
| 6487 | } |
| 6488 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 6489 | if (force || hasValueChangedSignificantly(axis.filter, |
| 6490 | axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) { |
| 6491 | axis.highCurrentValue = axis.highNewValue; |
| 6492 | atLeastOneSignificantChange = true; |
| 6493 | } |
| 6494 | } |
| 6495 | } |
| 6496 | return atLeastOneSignificantChange; |
| 6497 | } |
| 6498 | |
| 6499 | bool JoystickInputMapper::hasValueChangedSignificantly( |
| 6500 | float filter, float newValue, float currentValue, float min, float max) { |
| 6501 | if (newValue != currentValue) { |
| 6502 | // Filter out small changes in value unless the value is converging on the axis |
| 6503 | // bounds or center point. This is intended to reduce the amount of information |
| 6504 | // sent to applications by particularly noisy joysticks (such as PS3). |
| 6505 | if (fabs(newValue - currentValue) > filter |
| 6506 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min) |
| 6507 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max) |
| 6508 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) { |
| 6509 | return true; |
| 6510 | } |
| 6511 | } |
| 6512 | return false; |
| 6513 | } |
| 6514 | |
| 6515 | bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange( |
| 6516 | float filter, float newValue, float currentValue, float thresholdValue) { |
| 6517 | float newDistance = fabs(newValue - thresholdValue); |
| 6518 | if (newDistance < filter) { |
| 6519 | float oldDistance = fabs(currentValue - thresholdValue); |
| 6520 | if (newDistance < oldDistance) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6521 | return true; |
| 6522 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6523 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 6524 | return false; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 6525 | } |
| 6526 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 6527 | } // namespace android |