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 | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 39 | #include "InputReader.h" |
| 40 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 41 | #include <cutils/log.h> |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 42 | #include <ui/Keyboard.h> |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 43 | #include <ui/VirtualKeyMap.h> |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 44 | |
| 45 | #include <stddef.h> |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 46 | #include <stdlib.h> |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 47 | #include <unistd.h> |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 48 | #include <errno.h> |
| 49 | #include <limits.h> |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 50 | #include <math.h> |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 51 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 52 | #define INDENT " " |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 53 | #define INDENT2 " " |
| 54 | #define INDENT3 " " |
| 55 | #define INDENT4 " " |
Jeff Brown | aba321a | 2011-06-28 20:34:40 -0700 | [diff] [blame] | 56 | #define INDENT5 " " |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 57 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 58 | namespace android { |
| 59 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 60 | // --- Constants --- |
| 61 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 62 | // Maximum number of slots supported when using the slot-based Multitouch Protocol B. |
| 63 | static const size_t MAX_SLOTS = 32; |
| 64 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 65 | // --- Static Functions --- |
| 66 | |
| 67 | template<typename T> |
| 68 | inline static T abs(const T& value) { |
| 69 | return value < 0 ? - value : value; |
| 70 | } |
| 71 | |
| 72 | template<typename T> |
| 73 | inline static T min(const T& a, const T& b) { |
| 74 | return a < b ? a : b; |
| 75 | } |
| 76 | |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 77 | template<typename T> |
| 78 | inline static void swap(T& a, T& b) { |
| 79 | T temp = a; |
| 80 | a = b; |
| 81 | b = temp; |
| 82 | } |
| 83 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 84 | inline static float avg(float x, float y) { |
| 85 | return (x + y) / 2; |
| 86 | } |
| 87 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 88 | inline static float distance(float x1, float y1, float x2, float y2) { |
| 89 | return hypotf(x1 - x2, y1 - y2); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 92 | inline static int32_t signExtendNybble(int32_t value) { |
| 93 | return value >= 8 ? value - 16 : value; |
| 94 | } |
| 95 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 96 | static inline const char* toString(bool value) { |
| 97 | return value ? "true" : "false"; |
| 98 | } |
| 99 | |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 100 | static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation, |
| 101 | const int32_t map[][4], size_t mapSize) { |
| 102 | if (orientation != DISPLAY_ORIENTATION_0) { |
| 103 | for (size_t i = 0; i < mapSize; i++) { |
| 104 | if (value == map[i][0]) { |
| 105 | return map[i][orientation]; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | return value; |
| 110 | } |
| 111 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 112 | static const int32_t keyCodeRotationMap[][4] = { |
| 113 | // key codes enumerated counter-clockwise with the original (unrotated) key first |
| 114 | // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 115 | { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT }, |
| 116 | { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN }, |
| 117 | { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT }, |
| 118 | { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP }, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 119 | }; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 120 | static const size_t keyCodeRotationMapSize = |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 121 | sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]); |
| 122 | |
Jeff Brown | 6069139 | 2011-07-15 19:08:26 -0700 | [diff] [blame^] | 123 | static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) { |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 124 | return rotateValueUsingRotationMap(keyCode, orientation, |
| 125 | keyCodeRotationMap, keyCodeRotationMapSize); |
| 126 | } |
| 127 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 128 | static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) { |
| 129 | return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0; |
| 130 | } |
| 131 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 132 | static uint32_t getButtonStateForScanCode(int32_t scanCode) { |
| 133 | // Currently all buttons are mapped to the primary button. |
| 134 | switch (scanCode) { |
| 135 | case BTN_LEFT: |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 136 | return AMOTION_EVENT_BUTTON_PRIMARY; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 137 | case BTN_RIGHT: |
Jeff Brown | 53ca3f1 | 2011-06-27 18:36:00 -0700 | [diff] [blame] | 138 | case BTN_STYLUS: |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 139 | return AMOTION_EVENT_BUTTON_SECONDARY; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 140 | case BTN_MIDDLE: |
Jeff Brown | 53ca3f1 | 2011-06-27 18:36:00 -0700 | [diff] [blame] | 141 | case BTN_STYLUS2: |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 142 | return AMOTION_EVENT_BUTTON_TERTIARY; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 143 | case BTN_SIDE: |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 144 | return AMOTION_EVENT_BUTTON_BACK; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 145 | case BTN_FORWARD: |
Jeff Brown | 53ca3f1 | 2011-06-27 18:36:00 -0700 | [diff] [blame] | 146 | case BTN_EXTRA: |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 147 | return AMOTION_EVENT_BUTTON_FORWARD; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 148 | case BTN_BACK: |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 149 | return AMOTION_EVENT_BUTTON_BACK; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 150 | case BTN_TASK: |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 151 | default: |
| 152 | return 0; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // 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] | 157 | // button states. This determines whether the event is reported as a touch event. |
| 158 | static bool isPointerDown(int32_t buttonState) { |
| 159 | return buttonState & |
| 160 | (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY |
Jeff Brown | 53ca3f1 | 2011-06-27 18:36:00 -0700 | [diff] [blame] | 161 | | AMOTION_EVENT_BUTTON_TERTIARY); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 164 | static float calculateCommonVector(float a, float b) { |
| 165 | if (a > 0 && b > 0) { |
| 166 | return a < b ? a : b; |
| 167 | } else if (a < 0 && b < 0) { |
| 168 | return a > b ? a : b; |
| 169 | } else { |
| 170 | return 0; |
| 171 | } |
| 172 | } |
| 173 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 174 | static void synthesizeButtonKey(InputReaderContext* context, int32_t action, |
| 175 | nsecs_t when, int32_t deviceId, uint32_t source, |
| 176 | uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState, |
| 177 | int32_t buttonState, int32_t keyCode) { |
| 178 | if ( |
| 179 | (action == AKEY_EVENT_ACTION_DOWN |
| 180 | && !(lastButtonState & buttonState) |
| 181 | && (currentButtonState & buttonState)) |
| 182 | || (action == AKEY_EVENT_ACTION_UP |
| 183 | && (lastButtonState & buttonState) |
| 184 | && !(currentButtonState & buttonState))) { |
| 185 | context->getDispatcher()->notifyKey(when, deviceId, source, policyFlags, |
| 186 | action, 0, keyCode, 0, context->getGlobalMetaState(), when); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | static void synthesizeButtonKeys(InputReaderContext* context, int32_t action, |
| 191 | nsecs_t when, int32_t deviceId, uint32_t source, |
| 192 | uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState) { |
| 193 | synthesizeButtonKey(context, action, when, deviceId, source, policyFlags, |
| 194 | lastButtonState, currentButtonState, |
| 195 | AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK); |
| 196 | synthesizeButtonKey(context, action, when, deviceId, source, policyFlags, |
| 197 | lastButtonState, currentButtonState, |
| 198 | AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD); |
| 199 | } |
| 200 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 201 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 202 | // --- InputReader --- |
| 203 | |
| 204 | InputReader::InputReader(const sp<EventHubInterface>& eventHub, |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 205 | const sp<InputReaderPolicyInterface>& policy, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 206 | const sp<InputDispatcherInterface>& dispatcher) : |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 207 | mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher), |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 208 | mGlobalMetaState(0), mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX), |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 209 | mConfigurationChangesToRefresh(0) { |
| 210 | refreshConfiguration(0); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 211 | updateGlobalMetaState(); |
| 212 | updateInputConfiguration(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | InputReader::~InputReader() { |
| 216 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 217 | delete mDevices.valueAt(i); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void InputReader::loopOnce() { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 222 | uint32_t changes; |
| 223 | { // acquire lock |
| 224 | AutoMutex _l(mStateLock); |
| 225 | |
| 226 | changes = mConfigurationChangesToRefresh; |
| 227 | mConfigurationChangesToRefresh = 0; |
| 228 | } // release lock |
| 229 | |
| 230 | if (changes) { |
| 231 | refreshConfiguration(changes); |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 234 | int32_t timeoutMillis = -1; |
| 235 | if (mNextTimeout != LLONG_MAX) { |
| 236 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 237 | timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout); |
| 238 | } |
| 239 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 240 | size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE); |
| 241 | if (count) { |
| 242 | processEvents(mEventBuffer, count); |
| 243 | } |
| 244 | if (!count || timeoutMillis == 0) { |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 245 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 246 | #if DEBUG_RAW_EVENTS |
| 247 | LOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f); |
| 248 | #endif |
| 249 | mNextTimeout = LLONG_MAX; |
| 250 | timeoutExpired(now); |
| 251 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 254 | void InputReader::processEvents(const RawEvent* rawEvents, size_t count) { |
| 255 | for (const RawEvent* rawEvent = rawEvents; count;) { |
| 256 | int32_t type = rawEvent->type; |
| 257 | size_t batchSize = 1; |
| 258 | if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) { |
| 259 | int32_t deviceId = rawEvent->deviceId; |
| 260 | while (batchSize < count) { |
| 261 | if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT |
| 262 | || rawEvent[batchSize].deviceId != deviceId) { |
| 263 | break; |
| 264 | } |
| 265 | batchSize += 1; |
| 266 | } |
| 267 | #if DEBUG_RAW_EVENTS |
| 268 | LOGD("BatchSize: %d Count: %d", batchSize, count); |
| 269 | #endif |
| 270 | processEventsForDevice(deviceId, rawEvent, batchSize); |
| 271 | } else { |
| 272 | switch (rawEvent->type) { |
| 273 | case EventHubInterface::DEVICE_ADDED: |
| 274 | addDevice(rawEvent->deviceId); |
| 275 | break; |
| 276 | case EventHubInterface::DEVICE_REMOVED: |
| 277 | removeDevice(rawEvent->deviceId); |
| 278 | break; |
| 279 | case EventHubInterface::FINISHED_DEVICE_SCAN: |
| 280 | handleConfigurationChanged(rawEvent->when); |
| 281 | break; |
| 282 | default: |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 283 | LOG_ASSERT(false); // can't happen |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 284 | break; |
| 285 | } |
| 286 | } |
| 287 | count -= batchSize; |
| 288 | rawEvent += batchSize; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 292 | void InputReader::addDevice(int32_t deviceId) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 293 | String8 name = mEventHub->getDeviceName(deviceId); |
| 294 | uint32_t classes = mEventHub->getDeviceClasses(deviceId); |
| 295 | |
| 296 | InputDevice* device = createDevice(deviceId, name, classes); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 297 | device->configure(&mConfig, 0); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 298 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 299 | if (device->isIgnored()) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 300 | LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 301 | } else { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 302 | LOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(), |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 303 | device->getSources()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 306 | bool added = false; |
| 307 | { // acquire device registry writer lock |
| 308 | RWLock::AutoWLock _wl(mDeviceRegistryLock); |
| 309 | |
| 310 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 311 | if (deviceIndex < 0) { |
| 312 | mDevices.add(deviceId, device); |
| 313 | added = true; |
| 314 | } |
| 315 | } // release device registry writer lock |
| 316 | |
| 317 | if (! added) { |
| 318 | LOGW("Ignoring spurious device added event for deviceId %d.", deviceId); |
| 319 | delete device; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 320 | return; |
| 321 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 324 | void InputReader::removeDevice(int32_t deviceId) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 325 | bool removed = false; |
| 326 | InputDevice* device = NULL; |
| 327 | { // acquire device registry writer lock |
| 328 | RWLock::AutoWLock _wl(mDeviceRegistryLock); |
| 329 | |
| 330 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 331 | if (deviceIndex >= 0) { |
| 332 | device = mDevices.valueAt(deviceIndex); |
| 333 | mDevices.removeItemsAt(deviceIndex, 1); |
| 334 | removed = true; |
| 335 | } |
| 336 | } // release device registry writer lock |
| 337 | |
| 338 | if (! removed) { |
| 339 | LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 340 | return; |
| 341 | } |
| 342 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 343 | if (device->isIgnored()) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 344 | LOGI("Device removed: id=%d, name='%s' (ignored non-input device)", |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 345 | device->getId(), device->getName().string()); |
| 346 | } else { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 347 | LOGI("Device removed: id=%d, name='%s', sources=0x%08x", |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 348 | device->getId(), device->getName().string(), device->getSources()); |
| 349 | } |
| 350 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 351 | device->reset(); |
| 352 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 353 | delete device; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 356 | InputDevice* InputReader::createDevice(int32_t deviceId, const String8& name, uint32_t classes) { |
| 357 | InputDevice* device = new InputDevice(this, deviceId, name); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 358 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 359 | // External devices. |
| 360 | if (classes & INPUT_DEVICE_CLASS_EXTERNAL) { |
| 361 | device->setExternal(true); |
| 362 | } |
| 363 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 364 | // Switch-like devices. |
| 365 | if (classes & INPUT_DEVICE_CLASS_SWITCH) { |
| 366 | device->addMapper(new SwitchInputMapper(device)); |
| 367 | } |
| 368 | |
| 369 | // Keyboard-like devices. |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 370 | uint32_t keyboardSource = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 371 | int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC; |
| 372 | if (classes & INPUT_DEVICE_CLASS_KEYBOARD) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 373 | keyboardSource |= AINPUT_SOURCE_KEYBOARD; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 374 | } |
| 375 | if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) { |
| 376 | keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC; |
| 377 | } |
| 378 | if (classes & INPUT_DEVICE_CLASS_DPAD) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 379 | keyboardSource |= AINPUT_SOURCE_DPAD; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 380 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 381 | if (classes & INPUT_DEVICE_CLASS_GAMEPAD) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 382 | keyboardSource |= AINPUT_SOURCE_GAMEPAD; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 383 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 384 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 385 | if (keyboardSource != 0) { |
| 386 | device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 389 | // Cursor-like devices. |
| 390 | if (classes & INPUT_DEVICE_CLASS_CURSOR) { |
| 391 | device->addMapper(new CursorInputMapper(device)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 394 | // Touchscreens and touchpad devices. |
| 395 | if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 396 | device->addMapper(new MultiTouchInputMapper(device)); |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 397 | } else if (classes & INPUT_DEVICE_CLASS_TOUCH) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 398 | device->addMapper(new SingleTouchInputMapper(device)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 401 | // Joystick-like devices. |
| 402 | if (classes & INPUT_DEVICE_CLASS_JOYSTICK) { |
| 403 | device->addMapper(new JoystickInputMapper(device)); |
| 404 | } |
| 405 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 406 | return device; |
| 407 | } |
| 408 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 409 | void InputReader::processEventsForDevice(int32_t deviceId, |
| 410 | const RawEvent* rawEvents, size_t count) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 411 | { // acquire device registry reader lock |
| 412 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 413 | |
| 414 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 415 | if (deviceIndex < 0) { |
| 416 | LOGW("Discarding event for unknown deviceId %d.", deviceId); |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 421 | if (device->isIgnored()) { |
| 422 | //LOGD("Discarding event for ignored deviceId %d.", deviceId); |
| 423 | return; |
| 424 | } |
| 425 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 426 | device->process(rawEvents, count); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 427 | } // release device registry reader lock |
| 428 | } |
| 429 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 430 | void InputReader::timeoutExpired(nsecs_t when) { |
| 431 | { // acquire device registry reader lock |
| 432 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 433 | |
| 434 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 435 | InputDevice* device = mDevices.valueAt(i); |
| 436 | if (!device->isIgnored()) { |
| 437 | device->timeoutExpired(when); |
| 438 | } |
| 439 | } |
| 440 | } // release device registry reader lock |
| 441 | } |
| 442 | |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 443 | void InputReader::handleConfigurationChanged(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 444 | // Reset global meta state because it depends on the list of all configured devices. |
| 445 | updateGlobalMetaState(); |
| 446 | |
| 447 | // Update input configuration. |
| 448 | updateInputConfiguration(); |
| 449 | |
| 450 | // Enqueue configuration changed. |
| 451 | mDispatcher->notifyConfigurationChanged(when); |
| 452 | } |
| 453 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 454 | void InputReader::refreshConfiguration(uint32_t changes) { |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 455 | mPolicy->getReaderConfiguration(&mConfig); |
| 456 | mEventHub->setExcludedDevices(mConfig.excludedDeviceNames); |
| 457 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 458 | if (changes) { |
| 459 | LOGI("Reconfiguring input devices. changes=0x%08x", changes); |
| 460 | |
| 461 | if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) { |
| 462 | mEventHub->requestReopenDevices(); |
| 463 | } else { |
| 464 | { // acquire device registry reader lock |
| 465 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 466 | |
| 467 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 468 | InputDevice* device = mDevices.valueAt(i); |
| 469 | device->configure(&mConfig, changes); |
| 470 | } |
| 471 | } // release device registry reader lock |
| 472 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
| 476 | void InputReader::updateGlobalMetaState() { |
| 477 | { // acquire state lock |
| 478 | AutoMutex _l(mStateLock); |
| 479 | |
| 480 | mGlobalMetaState = 0; |
| 481 | |
| 482 | { // acquire device registry reader lock |
| 483 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 484 | |
| 485 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 486 | InputDevice* device = mDevices.valueAt(i); |
| 487 | mGlobalMetaState |= device->getMetaState(); |
| 488 | } |
| 489 | } // release device registry reader lock |
| 490 | } // release state lock |
| 491 | } |
| 492 | |
| 493 | int32_t InputReader::getGlobalMetaState() { |
| 494 | { // acquire state lock |
| 495 | AutoMutex _l(mStateLock); |
| 496 | |
| 497 | return mGlobalMetaState; |
| 498 | } // release state lock |
| 499 | } |
| 500 | |
| 501 | void InputReader::updateInputConfiguration() { |
| 502 | { // acquire state lock |
| 503 | AutoMutex _l(mStateLock); |
| 504 | |
| 505 | int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH; |
| 506 | int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS; |
| 507 | int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV; |
| 508 | { // acquire device registry reader lock |
| 509 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 510 | |
| 511 | InputDeviceInfo deviceInfo; |
| 512 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 513 | InputDevice* device = mDevices.valueAt(i); |
| 514 | device->getDeviceInfo(& deviceInfo); |
| 515 | uint32_t sources = deviceInfo.getSources(); |
| 516 | |
| 517 | if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) { |
| 518 | touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER; |
| 519 | } |
| 520 | if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) { |
| 521 | navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL; |
| 522 | } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) { |
| 523 | navigationConfig = InputConfiguration::NAVIGATION_DPAD; |
| 524 | } |
| 525 | if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) { |
| 526 | keyboardConfig = InputConfiguration::KEYBOARD_QWERTY; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 527 | } |
| 528 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 529 | } // release device registry reader lock |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 530 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 531 | mInputConfiguration.touchScreen = touchScreenConfig; |
| 532 | mInputConfiguration.keyboard = keyboardConfig; |
| 533 | mInputConfiguration.navigation = navigationConfig; |
| 534 | } // release state lock |
| 535 | } |
| 536 | |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 537 | void InputReader::disableVirtualKeysUntil(nsecs_t time) { |
| 538 | mDisableVirtualKeysTimeout = time; |
| 539 | } |
| 540 | |
| 541 | bool InputReader::shouldDropVirtualKey(nsecs_t now, |
| 542 | InputDevice* device, int32_t keyCode, int32_t scanCode) { |
| 543 | if (now < mDisableVirtualKeysTimeout) { |
| 544 | LOGI("Dropping virtual key from device %s because virtual keys are " |
| 545 | "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d", |
| 546 | device->getName().string(), |
| 547 | (mDisableVirtualKeysTimeout - now) * 0.000001, |
| 548 | keyCode, scanCode); |
| 549 | return true; |
| 550 | } else { |
| 551 | return false; |
| 552 | } |
| 553 | } |
| 554 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 555 | void InputReader::fadePointer() { |
| 556 | { // acquire device registry reader lock |
| 557 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 558 | |
| 559 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 560 | InputDevice* device = mDevices.valueAt(i); |
| 561 | device->fadePointer(); |
| 562 | } |
| 563 | } // release device registry reader lock |
| 564 | } |
| 565 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 566 | void InputReader::requestTimeoutAtTime(nsecs_t when) { |
| 567 | if (when < mNextTimeout) { |
| 568 | mNextTimeout = when; |
| 569 | } |
| 570 | } |
| 571 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 572 | void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) { |
| 573 | { // acquire state lock |
| 574 | AutoMutex _l(mStateLock); |
| 575 | |
| 576 | *outConfiguration = mInputConfiguration; |
| 577 | } // release state lock |
| 578 | } |
| 579 | |
| 580 | status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) { |
| 581 | { // acquire device registry reader lock |
| 582 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 583 | |
| 584 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 585 | if (deviceIndex < 0) { |
| 586 | return NAME_NOT_FOUND; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 589 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 590 | if (device->isIgnored()) { |
| 591 | return NAME_NOT_FOUND; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 592 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 593 | |
| 594 | device->getDeviceInfo(outDeviceInfo); |
| 595 | return OK; |
| 596 | } // release device registy reader lock |
| 597 | } |
| 598 | |
| 599 | void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) { |
| 600 | outDeviceIds.clear(); |
| 601 | |
| 602 | { // acquire device registry reader lock |
| 603 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 604 | |
| 605 | size_t numDevices = mDevices.size(); |
| 606 | for (size_t i = 0; i < numDevices; i++) { |
| 607 | InputDevice* device = mDevices.valueAt(i); |
| 608 | if (! device->isIgnored()) { |
| 609 | outDeviceIds.add(device->getId()); |
| 610 | } |
| 611 | } |
| 612 | } // release device registy reader lock |
| 613 | } |
| 614 | |
| 615 | int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, |
| 616 | int32_t keyCode) { |
| 617 | return getState(deviceId, sourceMask, keyCode, & InputDevice::getKeyCodeState); |
| 618 | } |
| 619 | |
| 620 | int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, |
| 621 | int32_t scanCode) { |
| 622 | return getState(deviceId, sourceMask, scanCode, & InputDevice::getScanCodeState); |
| 623 | } |
| 624 | |
| 625 | int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) { |
| 626 | return getState(deviceId, sourceMask, switchCode, & InputDevice::getSwitchState); |
| 627 | } |
| 628 | |
| 629 | int32_t InputReader::getState(int32_t deviceId, uint32_t sourceMask, int32_t code, |
| 630 | GetStateFunc getStateFunc) { |
| 631 | { // acquire device registry reader lock |
| 632 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 633 | |
| 634 | int32_t result = AKEY_STATE_UNKNOWN; |
| 635 | if (deviceId >= 0) { |
| 636 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 637 | if (deviceIndex >= 0) { |
| 638 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 639 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 640 | result = (device->*getStateFunc)(sourceMask, code); |
| 641 | } |
| 642 | } |
| 643 | } else { |
| 644 | size_t numDevices = mDevices.size(); |
| 645 | for (size_t i = 0; i < numDevices; i++) { |
| 646 | InputDevice* device = mDevices.valueAt(i); |
| 647 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 648 | result = (device->*getStateFunc)(sourceMask, code); |
| 649 | if (result >= AKEY_STATE_DOWN) { |
| 650 | return result; |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | return result; |
| 656 | } // release device registy reader lock |
| 657 | } |
| 658 | |
| 659 | bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, |
| 660 | size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) { |
| 661 | memset(outFlags, 0, numCodes); |
| 662 | return markSupportedKeyCodes(deviceId, sourceMask, numCodes, keyCodes, outFlags); |
| 663 | } |
| 664 | |
| 665 | bool InputReader::markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes, |
| 666 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 667 | { // acquire device registry reader lock |
| 668 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 669 | bool result = false; |
| 670 | if (deviceId >= 0) { |
| 671 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 672 | if (deviceIndex >= 0) { |
| 673 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 674 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 675 | result = device->markSupportedKeyCodes(sourceMask, |
| 676 | numCodes, keyCodes, outFlags); |
| 677 | } |
| 678 | } |
| 679 | } else { |
| 680 | size_t numDevices = mDevices.size(); |
| 681 | for (size_t i = 0; i < numDevices; i++) { |
| 682 | InputDevice* device = mDevices.valueAt(i); |
| 683 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 684 | result |= device->markSupportedKeyCodes(sourceMask, |
| 685 | numCodes, keyCodes, outFlags); |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | return result; |
| 690 | } // release device registy reader lock |
| 691 | } |
| 692 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 693 | void InputReader::requestRefreshConfiguration(uint32_t changes) { |
| 694 | if (changes) { |
| 695 | bool needWake; |
| 696 | { // acquire lock |
| 697 | AutoMutex _l(mStateLock); |
Jeff Brown | 93fa9b3 | 2011-06-14 17:09:25 -0700 | [diff] [blame] | 698 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 699 | needWake = !mConfigurationChangesToRefresh; |
| 700 | mConfigurationChangesToRefresh |= changes; |
| 701 | } // release lock |
| 702 | |
| 703 | if (needWake) { |
| 704 | mEventHub->wake(); |
| 705 | } |
| 706 | } |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 709 | void InputReader::dump(String8& dump) { |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 710 | mEventHub->dump(dump); |
| 711 | dump.append("\n"); |
| 712 | |
| 713 | dump.append("Input Reader State:\n"); |
| 714 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 715 | { // acquire device registry reader lock |
| 716 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 717 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 718 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 719 | mDevices.valueAt(i)->dump(dump); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 720 | } |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 721 | } // release device registy reader lock |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 722 | |
| 723 | dump.append(INDENT "Configuration:\n"); |
| 724 | dump.append(INDENT2 "ExcludedDeviceNames: ["); |
| 725 | for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) { |
| 726 | if (i != 0) { |
| 727 | dump.append(", "); |
| 728 | } |
| 729 | dump.append(mConfig.excludedDeviceNames.itemAt(i).string()); |
| 730 | } |
| 731 | dump.append("]\n"); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 732 | dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n", |
| 733 | mConfig.virtualKeyQuietTime * 0.000001f); |
| 734 | |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 735 | dump.appendFormat(INDENT2 "PointerVelocityControlParameters: " |
| 736 | "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n", |
| 737 | mConfig.pointerVelocityControlParameters.scale, |
| 738 | mConfig.pointerVelocityControlParameters.lowThreshold, |
| 739 | mConfig.pointerVelocityControlParameters.highThreshold, |
| 740 | mConfig.pointerVelocityControlParameters.acceleration); |
| 741 | |
| 742 | dump.appendFormat(INDENT2 "WheelVelocityControlParameters: " |
| 743 | "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n", |
| 744 | mConfig.wheelVelocityControlParameters.scale, |
| 745 | mConfig.wheelVelocityControlParameters.lowThreshold, |
| 746 | mConfig.wheelVelocityControlParameters.highThreshold, |
| 747 | mConfig.wheelVelocityControlParameters.acceleration); |
| 748 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 749 | dump.appendFormat(INDENT2 "PointerGesture:\n"); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 750 | dump.appendFormat(INDENT3 "Enabled: %s\n", |
| 751 | toString(mConfig.pointerGesturesEnabled)); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 752 | dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n", |
| 753 | mConfig.pointerGestureQuietInterval * 0.000001f); |
| 754 | dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n", |
| 755 | mConfig.pointerGestureDragMinSwitchSpeed); |
| 756 | dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n", |
| 757 | mConfig.pointerGestureTapInterval * 0.000001f); |
| 758 | dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n", |
| 759 | mConfig.pointerGestureTapDragInterval * 0.000001f); |
| 760 | dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n", |
| 761 | mConfig.pointerGestureTapSlop); |
| 762 | dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n", |
| 763 | mConfig.pointerGestureMultitouchSettleInterval * 0.000001f); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 764 | dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n", |
| 765 | mConfig.pointerGestureMultitouchMinDistance); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 766 | dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n", |
| 767 | mConfig.pointerGestureSwipeTransitionAngleCosine); |
| 768 | dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n", |
| 769 | mConfig.pointerGestureSwipeMaxWidthRatio); |
| 770 | dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n", |
| 771 | mConfig.pointerGestureMovementSpeedRatio); |
| 772 | dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n", |
| 773 | mConfig.pointerGestureZoomSpeedRatio); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 776 | |
| 777 | // --- InputReaderThread --- |
| 778 | |
| 779 | InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) : |
| 780 | Thread(/*canCallJava*/ true), mReader(reader) { |
| 781 | } |
| 782 | |
| 783 | InputReaderThread::~InputReaderThread() { |
| 784 | } |
| 785 | |
| 786 | bool InputReaderThread::threadLoop() { |
| 787 | mReader->loopOnce(); |
| 788 | return true; |
| 789 | } |
| 790 | |
| 791 | |
| 792 | // --- InputDevice --- |
| 793 | |
| 794 | InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) : |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 795 | mContext(context), mId(id), mName(name), mSources(0), |
| 796 | mIsExternal(false), mDropUntilNextSync(false) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | InputDevice::~InputDevice() { |
| 800 | size_t numMappers = mMappers.size(); |
| 801 | for (size_t i = 0; i < numMappers; i++) { |
| 802 | delete mMappers[i]; |
| 803 | } |
| 804 | mMappers.clear(); |
| 805 | } |
| 806 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 807 | void InputDevice::dump(String8& dump) { |
| 808 | InputDeviceInfo deviceInfo; |
| 809 | getDeviceInfo(& deviceInfo); |
| 810 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 811 | dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(), |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 812 | deviceInfo.getName().string()); |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 813 | dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal)); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 814 | dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources()); |
| 815 | dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType()); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 816 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 817 | const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges(); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 818 | if (!ranges.isEmpty()) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 819 | dump.append(INDENT2 "Motion Ranges:\n"); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 820 | for (size_t i = 0; i < ranges.size(); i++) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 821 | const InputDeviceInfo::MotionRange& range = ranges.itemAt(i); |
| 822 | const char* label = getAxisLabel(range.axis); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 823 | char name[32]; |
| 824 | if (label) { |
| 825 | strncpy(name, label, sizeof(name)); |
| 826 | name[sizeof(name) - 1] = '\0'; |
| 827 | } else { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 828 | snprintf(name, sizeof(name), "%d", range.axis); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 829 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 830 | dump.appendFormat(INDENT3 "%s: source=0x%08x, " |
| 831 | "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n", |
| 832 | name, range.source, range.min, range.max, range.flat, range.fuzz); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 833 | } |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | size_t numMappers = mMappers.size(); |
| 837 | for (size_t i = 0; i < numMappers; i++) { |
| 838 | InputMapper* mapper = mMappers[i]; |
| 839 | mapper->dump(dump); |
| 840 | } |
| 841 | } |
| 842 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 843 | void InputDevice::addMapper(InputMapper* mapper) { |
| 844 | mMappers.add(mapper); |
| 845 | } |
| 846 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 847 | void InputDevice::configure(const InputReaderConfiguration* config, uint32_t changes) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 848 | mSources = 0; |
| 849 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 850 | if (!isIgnored()) { |
| 851 | if (!changes) { // first time only |
| 852 | mContext->getEventHub()->getConfiguration(mId, &mConfiguration); |
| 853 | } |
| 854 | |
| 855 | size_t numMappers = mMappers.size(); |
| 856 | for (size_t i = 0; i < numMappers; i++) { |
| 857 | InputMapper* mapper = mMappers[i]; |
| 858 | mapper->configure(config, changes); |
| 859 | mSources |= mapper->getSources(); |
| 860 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 864 | void InputDevice::reset() { |
| 865 | size_t numMappers = mMappers.size(); |
| 866 | for (size_t i = 0; i < numMappers; i++) { |
| 867 | InputMapper* mapper = mMappers[i]; |
| 868 | mapper->reset(); |
| 869 | } |
| 870 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 871 | |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 872 | void InputDevice::process(const RawEvent* rawEvents, size_t count) { |
| 873 | // Process all of the events in order for each mapper. |
| 874 | // We cannot simply ask each mapper to process them in bulk because mappers may |
| 875 | // have side-effects that must be interleaved. For example, joystick movement events and |
| 876 | // gamepad button presses are handled by different mappers but they should be dispatched |
| 877 | // in the order received. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 878 | size_t numMappers = mMappers.size(); |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 879 | for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) { |
| 880 | #if DEBUG_RAW_EVENTS |
| 881 | LOGD("Input event: device=%d type=0x%04x scancode=0x%04x " |
Jeff Brown | 2e45fb6 | 2011-06-29 21:19:05 -0700 | [diff] [blame] | 882 | "keycode=0x%04x value=0x%08x flags=0x%08x", |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 883 | rawEvent->deviceId, rawEvent->type, rawEvent->scanCode, rawEvent->keyCode, |
| 884 | rawEvent->value, rawEvent->flags); |
| 885 | #endif |
| 886 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 887 | if (mDropUntilNextSync) { |
| 888 | if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) { |
| 889 | mDropUntilNextSync = false; |
| 890 | #if DEBUG_RAW_EVENTS |
| 891 | LOGD("Recovered from input event buffer overrun."); |
| 892 | #endif |
| 893 | } else { |
| 894 | #if DEBUG_RAW_EVENTS |
| 895 | LOGD("Dropped input event while waiting for next input sync."); |
| 896 | #endif |
| 897 | } |
| 898 | } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_DROPPED) { |
| 899 | LOGI("Detected input event buffer overrun for device %s.", mName.string()); |
| 900 | mDropUntilNextSync = true; |
| 901 | reset(); |
| 902 | } else { |
| 903 | for (size_t i = 0; i < numMappers; i++) { |
| 904 | InputMapper* mapper = mMappers[i]; |
| 905 | mapper->process(rawEvent); |
| 906 | } |
Jeff Brown | b719874 | 2011-03-18 18:14:26 -0700 | [diff] [blame] | 907 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 908 | } |
| 909 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 910 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 911 | void InputDevice::timeoutExpired(nsecs_t when) { |
| 912 | size_t numMappers = mMappers.size(); |
| 913 | for (size_t i = 0; i < numMappers; i++) { |
| 914 | InputMapper* mapper = mMappers[i]; |
| 915 | mapper->timeoutExpired(when); |
| 916 | } |
| 917 | } |
| 918 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 919 | void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) { |
| 920 | outDeviceInfo->initialize(mId, mName); |
| 921 | |
| 922 | size_t numMappers = mMappers.size(); |
| 923 | for (size_t i = 0; i < numMappers; i++) { |
| 924 | InputMapper* mapper = mMappers[i]; |
| 925 | mapper->populateDeviceInfo(outDeviceInfo); |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 930 | return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState); |
| 931 | } |
| 932 | |
| 933 | int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 934 | return getState(sourceMask, scanCode, & InputMapper::getScanCodeState); |
| 935 | } |
| 936 | |
| 937 | int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 938 | return getState(sourceMask, switchCode, & InputMapper::getSwitchState); |
| 939 | } |
| 940 | |
| 941 | int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) { |
| 942 | int32_t result = AKEY_STATE_UNKNOWN; |
| 943 | size_t numMappers = mMappers.size(); |
| 944 | for (size_t i = 0; i < numMappers; i++) { |
| 945 | InputMapper* mapper = mMappers[i]; |
| 946 | if (sourcesMatchMask(mapper->getSources(), sourceMask)) { |
| 947 | result = (mapper->*getStateFunc)(sourceMask, code); |
| 948 | if (result >= AKEY_STATE_DOWN) { |
| 949 | return result; |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | return result; |
| 954 | } |
| 955 | |
| 956 | bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 957 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 958 | bool result = false; |
| 959 | size_t numMappers = mMappers.size(); |
| 960 | for (size_t i = 0; i < numMappers; i++) { |
| 961 | InputMapper* mapper = mMappers[i]; |
| 962 | if (sourcesMatchMask(mapper->getSources(), sourceMask)) { |
| 963 | result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags); |
| 964 | } |
| 965 | } |
| 966 | return result; |
| 967 | } |
| 968 | |
| 969 | int32_t InputDevice::getMetaState() { |
| 970 | int32_t result = 0; |
| 971 | size_t numMappers = mMappers.size(); |
| 972 | for (size_t i = 0; i < numMappers; i++) { |
| 973 | InputMapper* mapper = mMappers[i]; |
| 974 | result |= mapper->getMetaState(); |
| 975 | } |
| 976 | return result; |
| 977 | } |
| 978 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 979 | void InputDevice::fadePointer() { |
| 980 | size_t numMappers = mMappers.size(); |
| 981 | for (size_t i = 0; i < numMappers; i++) { |
| 982 | InputMapper* mapper = mMappers[i]; |
| 983 | mapper->fadePointer(); |
| 984 | } |
| 985 | } |
| 986 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 987 | |
| 988 | // --- InputMapper --- |
| 989 | |
| 990 | InputMapper::InputMapper(InputDevice* device) : |
| 991 | mDevice(device), mContext(device->getContext()) { |
| 992 | } |
| 993 | |
| 994 | InputMapper::~InputMapper() { |
| 995 | } |
| 996 | |
| 997 | void InputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 998 | info->addSource(getSources()); |
| 999 | } |
| 1000 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1001 | void InputMapper::dump(String8& dump) { |
| 1002 | } |
| 1003 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 1004 | void InputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | void InputMapper::reset() { |
| 1008 | } |
| 1009 | |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 1010 | void InputMapper::timeoutExpired(nsecs_t when) { |
| 1011 | } |
| 1012 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1013 | int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 1014 | return AKEY_STATE_UNKNOWN; |
| 1015 | } |
| 1016 | |
| 1017 | int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 1018 | return AKEY_STATE_UNKNOWN; |
| 1019 | } |
| 1020 | |
| 1021 | int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 1022 | return AKEY_STATE_UNKNOWN; |
| 1023 | } |
| 1024 | |
| 1025 | bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 1026 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 1027 | return false; |
| 1028 | } |
| 1029 | |
| 1030 | int32_t InputMapper::getMetaState() { |
| 1031 | return 0; |
| 1032 | } |
| 1033 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1034 | void InputMapper::fadePointer() { |
| 1035 | } |
| 1036 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1037 | void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump, |
| 1038 | const RawAbsoluteAxisInfo& axis, const char* name) { |
| 1039 | if (axis.valid) { |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 1040 | dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n", |
| 1041 | name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1042 | } else { |
| 1043 | dump.appendFormat(INDENT4 "%s: unknown range\n", name); |
| 1044 | } |
| 1045 | } |
| 1046 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1047 | |
| 1048 | // --- SwitchInputMapper --- |
| 1049 | |
| 1050 | SwitchInputMapper::SwitchInputMapper(InputDevice* device) : |
| 1051 | InputMapper(device) { |
| 1052 | } |
| 1053 | |
| 1054 | SwitchInputMapper::~SwitchInputMapper() { |
| 1055 | } |
| 1056 | |
| 1057 | uint32_t SwitchInputMapper::getSources() { |
Jeff Brown | 89de57a | 2011-01-19 18:41:38 -0800 | [diff] [blame] | 1058 | return AINPUT_SOURCE_SWITCH; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | void SwitchInputMapper::process(const RawEvent* rawEvent) { |
| 1062 | switch (rawEvent->type) { |
| 1063 | case EV_SW: |
| 1064 | processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value); |
| 1065 | break; |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) { |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1070 | getDispatcher()->notifySwitch(when, switchCode, switchValue, 0); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 1074 | return getEventHub()->getSwitchState(getDeviceId(), switchCode); |
| 1075 | } |
| 1076 | |
| 1077 | |
| 1078 | // --- KeyboardInputMapper --- |
| 1079 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1080 | KeyboardInputMapper::KeyboardInputMapper(InputDevice* device, |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1081 | uint32_t source, int32_t keyboardType) : |
| 1082 | InputMapper(device), mSource(source), |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1083 | mKeyboardType(keyboardType) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1084 | initializeLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | KeyboardInputMapper::~KeyboardInputMapper() { |
| 1088 | } |
| 1089 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1090 | void KeyboardInputMapper::initializeLocked() { |
| 1091 | mLocked.metaState = AMETA_NONE; |
| 1092 | mLocked.downTime = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | uint32_t KeyboardInputMapper::getSources() { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1096 | return mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 1100 | InputMapper::populateDeviceInfo(info); |
| 1101 | |
| 1102 | info->setKeyboardType(mKeyboardType); |
| 1103 | } |
| 1104 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1105 | void KeyboardInputMapper::dump(String8& dump) { |
| 1106 | { // acquire lock |
| 1107 | AutoMutex _l(mLock); |
| 1108 | dump.append(INDENT2 "Keyboard Input Mapper:\n"); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1109 | dumpParameters(dump); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1110 | dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType); |
| 1111 | dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mLocked.keyDowns.size()); |
| 1112 | dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mLocked.metaState); |
| 1113 | dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime); |
| 1114 | } // release lock |
| 1115 | } |
| 1116 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1117 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 1118 | void KeyboardInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) { |
| 1119 | InputMapper::configure(config, changes); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1120 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 1121 | if (!changes) { // first time only |
| 1122 | // Configure basic parameters. |
| 1123 | configureParameters(); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1124 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 1125 | // Reset LEDs. |
| 1126 | { |
| 1127 | AutoMutex _l(mLock); |
| 1128 | resetLedStateLocked(); |
| 1129 | } |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1130 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | void KeyboardInputMapper::configureParameters() { |
| 1134 | mParameters.orientationAware = false; |
| 1135 | getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"), |
| 1136 | mParameters.orientationAware); |
| 1137 | |
| 1138 | mParameters.associatedDisplayId = mParameters.orientationAware ? 0 : -1; |
| 1139 | } |
| 1140 | |
| 1141 | void KeyboardInputMapper::dumpParameters(String8& dump) { |
| 1142 | dump.append(INDENT3 "Parameters:\n"); |
| 1143 | dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n", |
| 1144 | mParameters.associatedDisplayId); |
| 1145 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 1146 | toString(mParameters.orientationAware)); |
| 1147 | } |
| 1148 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1149 | void KeyboardInputMapper::reset() { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1150 | for (;;) { |
| 1151 | int32_t keyCode, scanCode; |
| 1152 | { // acquire lock |
| 1153 | AutoMutex _l(mLock); |
| 1154 | |
| 1155 | // Synthesize key up event on reset if keys are currently down. |
| 1156 | if (mLocked.keyDowns.isEmpty()) { |
| 1157 | initializeLocked(); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1158 | resetLedStateLocked(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1159 | break; // done |
| 1160 | } |
| 1161 | |
| 1162 | const KeyDown& keyDown = mLocked.keyDowns.top(); |
| 1163 | keyCode = keyDown.keyCode; |
| 1164 | scanCode = keyDown.scanCode; |
| 1165 | } // release lock |
| 1166 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1167 | nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1168 | processKey(when, false, keyCode, scanCode, 0); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | InputMapper::reset(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1172 | getContext()->updateGlobalMetaState(); |
| 1173 | } |
| 1174 | |
| 1175 | void KeyboardInputMapper::process(const RawEvent* rawEvent) { |
| 1176 | switch (rawEvent->type) { |
| 1177 | case EV_KEY: { |
| 1178 | int32_t scanCode = rawEvent->scanCode; |
| 1179 | if (isKeyboardOrGamepadKey(scanCode)) { |
| 1180 | processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode, |
| 1181 | rawEvent->flags); |
| 1182 | } |
| 1183 | break; |
| 1184 | } |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) { |
| 1189 | return scanCode < BTN_MOUSE |
| 1190 | || scanCode >= KEY_OK |
Jeff Brown | 9e8e40c | 2011-03-03 03:39:29 -0800 | [diff] [blame] | 1191 | || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE) |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1192 | || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1195 | void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode, |
| 1196 | int32_t scanCode, uint32_t policyFlags) { |
| 1197 | int32_t newMetaState; |
| 1198 | nsecs_t downTime; |
| 1199 | bool metaStateChanged = false; |
| 1200 | |
| 1201 | { // acquire lock |
| 1202 | AutoMutex _l(mLock); |
| 1203 | |
| 1204 | if (down) { |
| 1205 | // Rotate key codes according to orientation if needed. |
| 1206 | // Note: getDisplayInfo is non-reentrant so we can continue holding the lock. |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1207 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1208 | int32_t orientation; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1209 | if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId, |
| 1210 | NULL, NULL, & orientation)) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1211 | orientation = DISPLAY_ORIENTATION_0; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | keyCode = rotateKeyCode(keyCode, orientation); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1217 | // Add key down. |
| 1218 | ssize_t keyDownIndex = findKeyDownLocked(scanCode); |
| 1219 | if (keyDownIndex >= 0) { |
| 1220 | // key repeat, be sure to use same keycode as before in case of rotation |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 1221 | keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1222 | } else { |
| 1223 | // key down |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 1224 | if ((policyFlags & POLICY_FLAG_VIRTUAL) |
| 1225 | && mContext->shouldDropVirtualKey(when, |
| 1226 | getDevice(), keyCode, scanCode)) { |
| 1227 | return; |
| 1228 | } |
| 1229 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1230 | mLocked.keyDowns.push(); |
| 1231 | KeyDown& keyDown = mLocked.keyDowns.editTop(); |
| 1232 | keyDown.keyCode = keyCode; |
| 1233 | keyDown.scanCode = scanCode; |
| 1234 | } |
| 1235 | |
| 1236 | mLocked.downTime = when; |
| 1237 | } else { |
| 1238 | // Remove key down. |
| 1239 | ssize_t keyDownIndex = findKeyDownLocked(scanCode); |
| 1240 | if (keyDownIndex >= 0) { |
| 1241 | // key up, be sure to use same keycode as before in case of rotation |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 1242 | keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1243 | mLocked.keyDowns.removeAt(size_t(keyDownIndex)); |
| 1244 | } else { |
| 1245 | // key was not actually down |
| 1246 | LOGI("Dropping key up from device %s because the key was not down. " |
| 1247 | "keyCode=%d, scanCode=%d", |
| 1248 | getDeviceName().string(), keyCode, scanCode); |
| 1249 | return; |
| 1250 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1251 | } |
| 1252 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1253 | int32_t oldMetaState = mLocked.metaState; |
| 1254 | newMetaState = updateMetaState(keyCode, down, oldMetaState); |
| 1255 | if (oldMetaState != newMetaState) { |
| 1256 | mLocked.metaState = newMetaState; |
| 1257 | metaStateChanged = true; |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1258 | updateLedStateLocked(false); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1259 | } |
Jeff Brown | fd03582 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 1260 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1261 | downTime = mLocked.downTime; |
| 1262 | } // release lock |
| 1263 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1264 | // Key down on external an keyboard should wake the device. |
| 1265 | // We don't do this for internal keyboards to prevent them from waking up in your pocket. |
| 1266 | // For internal keyboards, the key layout file should specify the policy flags for |
| 1267 | // each wake key individually. |
| 1268 | // TODO: Use the input device configuration to control this behavior more finely. |
| 1269 | if (down && getDevice()->isExternal() |
| 1270 | && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) { |
| 1271 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 1272 | } |
| 1273 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1274 | if (metaStateChanged) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1275 | getContext()->updateGlobalMetaState(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1276 | } |
| 1277 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1278 | if (down && !isMetaKey(keyCode)) { |
| 1279 | getContext()->fadePointer(); |
| 1280 | } |
| 1281 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1282 | getDispatcher()->notifyKey(when, getDeviceId(), mSource, policyFlags, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1283 | down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, |
| 1284 | AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1285 | } |
| 1286 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1287 | ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) { |
| 1288 | size_t n = mLocked.keyDowns.size(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1289 | for (size_t i = 0; i < n; i++) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1290 | if (mLocked.keyDowns[i].scanCode == scanCode) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1291 | return i; |
| 1292 | } |
| 1293 | } |
| 1294 | return -1; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1297 | int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 1298 | return getEventHub()->getKeyCodeState(getDeviceId(), keyCode); |
| 1299 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1300 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1301 | int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 1302 | return getEventHub()->getScanCodeState(getDeviceId(), scanCode); |
| 1303 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1304 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1305 | bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 1306 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 1307 | return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags); |
| 1308 | } |
| 1309 | |
| 1310 | int32_t KeyboardInputMapper::getMetaState() { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1311 | { // acquire lock |
| 1312 | AutoMutex _l(mLock); |
| 1313 | return mLocked.metaState; |
| 1314 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1315 | } |
| 1316 | |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1317 | void KeyboardInputMapper::resetLedStateLocked() { |
| 1318 | initializeLedStateLocked(mLocked.capsLockLedState, LED_CAPSL); |
| 1319 | initializeLedStateLocked(mLocked.numLockLedState, LED_NUML); |
| 1320 | initializeLedStateLocked(mLocked.scrollLockLedState, LED_SCROLLL); |
| 1321 | |
| 1322 | updateLedStateLocked(true); |
| 1323 | } |
| 1324 | |
| 1325 | void KeyboardInputMapper::initializeLedStateLocked(LockedState::LedState& ledState, int32_t led) { |
| 1326 | ledState.avail = getEventHub()->hasLed(getDeviceId(), led); |
| 1327 | ledState.on = false; |
| 1328 | } |
| 1329 | |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1330 | void KeyboardInputMapper::updateLedStateLocked(bool reset) { |
| 1331 | updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL, |
Jeff Brown | 51e7fe7 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 1332 | AMETA_CAPS_LOCK_ON, reset); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1333 | updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML, |
Jeff Brown | 51e7fe7 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 1334 | AMETA_NUM_LOCK_ON, reset); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1335 | updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL, |
Jeff Brown | 51e7fe7 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 1336 | AMETA_SCROLL_LOCK_ON, reset); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | void KeyboardInputMapper::updateLedStateForModifierLocked(LockedState::LedState& ledState, |
| 1340 | int32_t led, int32_t modifier, bool reset) { |
| 1341 | if (ledState.avail) { |
| 1342 | bool desiredState = (mLocked.metaState & modifier) != 0; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1343 | if (reset || ledState.on != desiredState) { |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1344 | getEventHub()->setLedState(getDeviceId(), led, desiredState); |
| 1345 | ledState.on = desiredState; |
| 1346 | } |
| 1347 | } |
| 1348 | } |
| 1349 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1350 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1351 | // --- CursorInputMapper --- |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1352 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1353 | CursorInputMapper::CursorInputMapper(InputDevice* device) : |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1354 | InputMapper(device) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1355 | initializeLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1356 | } |
| 1357 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1358 | CursorInputMapper::~CursorInputMapper() { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1359 | } |
| 1360 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1361 | uint32_t CursorInputMapper::getSources() { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1362 | return mSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1365 | void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1366 | InputMapper::populateDeviceInfo(info); |
| 1367 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1368 | if (mParameters.mode == Parameters::MODE_POINTER) { |
| 1369 | float minX, minY, maxX, maxY; |
| 1370 | if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1371 | info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f); |
| 1372 | info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1373 | } |
| 1374 | } else { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1375 | info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale); |
| 1376 | info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1377 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1378 | info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1379 | |
| 1380 | if (mHaveVWheel) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1381 | info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1382 | } |
| 1383 | if (mHaveHWheel) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1384 | info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1385 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1386 | } |
| 1387 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1388 | void CursorInputMapper::dump(String8& dump) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1389 | { // acquire lock |
| 1390 | AutoMutex _l(mLock); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1391 | dump.append(INDENT2 "Cursor Input Mapper:\n"); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1392 | dumpParameters(dump); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1393 | dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale); |
| 1394 | dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1395 | dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision); |
| 1396 | dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1397 | dump.appendFormat(INDENT3 "HaveVWheel: %s\n", toString(mHaveVWheel)); |
| 1398 | dump.appendFormat(INDENT3 "HaveHWheel: %s\n", toString(mHaveHWheel)); |
| 1399 | dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale); |
| 1400 | dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1401 | dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mLocked.buttonState); |
| 1402 | dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mLocked.buttonState))); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1403 | dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime); |
| 1404 | } // release lock |
| 1405 | } |
| 1406 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 1407 | void CursorInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) { |
| 1408 | InputMapper::configure(config, changes); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1409 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 1410 | if (!changes) { // first time only |
| 1411 | // Configure basic parameters. |
| 1412 | configureParameters(); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1413 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 1414 | // Configure device mode. |
| 1415 | switch (mParameters.mode) { |
| 1416 | case Parameters::MODE_POINTER: |
| 1417 | mSource = AINPUT_SOURCE_MOUSE; |
| 1418 | mXPrecision = 1.0f; |
| 1419 | mYPrecision = 1.0f; |
| 1420 | mXScale = 1.0f; |
| 1421 | mYScale = 1.0f; |
| 1422 | mPointerController = getPolicy()->obtainPointerController(getDeviceId()); |
| 1423 | break; |
| 1424 | case Parameters::MODE_NAVIGATION: |
| 1425 | mSource = AINPUT_SOURCE_TRACKBALL; |
| 1426 | mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 1427 | mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 1428 | mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 1429 | mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 1430 | break; |
| 1431 | } |
| 1432 | |
| 1433 | mVWheelScale = 1.0f; |
| 1434 | mHWheelScale = 1.0f; |
| 1435 | |
| 1436 | mHaveVWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_WHEEL); |
| 1437 | mHaveHWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_HWHEEL); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1438 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1439 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 1440 | if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) { |
| 1441 | mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters); |
| 1442 | mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters); |
| 1443 | mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters); |
| 1444 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1445 | } |
| 1446 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1447 | void CursorInputMapper::configureParameters() { |
| 1448 | mParameters.mode = Parameters::MODE_POINTER; |
| 1449 | String8 cursorModeString; |
| 1450 | if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) { |
| 1451 | if (cursorModeString == "navigation") { |
| 1452 | mParameters.mode = Parameters::MODE_NAVIGATION; |
| 1453 | } else if (cursorModeString != "pointer" && cursorModeString != "default") { |
| 1454 | LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string()); |
| 1455 | } |
| 1456 | } |
| 1457 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1458 | mParameters.orientationAware = false; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1459 | getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"), |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1460 | mParameters.orientationAware); |
| 1461 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1462 | mParameters.associatedDisplayId = mParameters.mode == Parameters::MODE_POINTER |
| 1463 | || mParameters.orientationAware ? 0 : -1; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1464 | } |
| 1465 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1466 | void CursorInputMapper::dumpParameters(String8& dump) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1467 | dump.append(INDENT3 "Parameters:\n"); |
| 1468 | dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n", |
| 1469 | mParameters.associatedDisplayId); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1470 | |
| 1471 | switch (mParameters.mode) { |
| 1472 | case Parameters::MODE_POINTER: |
| 1473 | dump.append(INDENT4 "Mode: pointer\n"); |
| 1474 | break; |
| 1475 | case Parameters::MODE_NAVIGATION: |
| 1476 | dump.append(INDENT4 "Mode: navigation\n"); |
| 1477 | break; |
| 1478 | default: |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 1479 | LOG_ASSERT(false); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1480 | } |
| 1481 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1482 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 1483 | toString(mParameters.orientationAware)); |
| 1484 | } |
| 1485 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1486 | void CursorInputMapper::initializeLocked() { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1487 | mAccumulator.clear(); |
| 1488 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1489 | mLocked.buttonState = 0; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1490 | mLocked.downTime = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1491 | } |
| 1492 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1493 | void CursorInputMapper::reset() { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1494 | for (;;) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 1495 | int32_t buttonState; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1496 | { // acquire lock |
| 1497 | AutoMutex _l(mLock); |
| 1498 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1499 | buttonState = mLocked.buttonState; |
| 1500 | if (!buttonState) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1501 | initializeLocked(); |
| 1502 | break; // done |
| 1503 | } |
| 1504 | } // release lock |
| 1505 | |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 1506 | // Reset velocity. |
| 1507 | mPointerVelocityControl.reset(); |
| 1508 | mWheelXVelocityControl.reset(); |
| 1509 | mWheelYVelocityControl.reset(); |
| 1510 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1511 | // Synthesize button up event on reset. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1512 | nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1513 | mAccumulator.clear(); |
| 1514 | mAccumulator.buttonDown = 0; |
| 1515 | mAccumulator.buttonUp = buttonState; |
| 1516 | mAccumulator.fields = Accumulator::FIELD_BUTTONS; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1517 | sync(when); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1518 | } |
| 1519 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1520 | InputMapper::reset(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1521 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1522 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1523 | void CursorInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1524 | switch (rawEvent->type) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1525 | case EV_KEY: { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 1526 | int32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1527 | if (buttonState) { |
| 1528 | if (rawEvent->value) { |
| 1529 | mAccumulator.buttonDown = buttonState; |
| 1530 | mAccumulator.buttonUp = 0; |
| 1531 | } else { |
| 1532 | mAccumulator.buttonDown = 0; |
| 1533 | mAccumulator.buttonUp = buttonState; |
| 1534 | } |
| 1535 | mAccumulator.fields |= Accumulator::FIELD_BUTTONS; |
| 1536 | |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 1537 | // Sync now since BTN_MOUSE is not necessarily followed by SYN_REPORT and |
| 1538 | // we need to ensure that we report the up/down promptly. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1539 | sync(rawEvent->when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1540 | break; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1541 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1542 | break; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1543 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1544 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1545 | case EV_REL: |
| 1546 | switch (rawEvent->scanCode) { |
| 1547 | case REL_X: |
| 1548 | mAccumulator.fields |= Accumulator::FIELD_REL_X; |
| 1549 | mAccumulator.relX = rawEvent->value; |
| 1550 | break; |
| 1551 | case REL_Y: |
| 1552 | mAccumulator.fields |= Accumulator::FIELD_REL_Y; |
| 1553 | mAccumulator.relY = rawEvent->value; |
| 1554 | break; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 1555 | case REL_WHEEL: |
| 1556 | mAccumulator.fields |= Accumulator::FIELD_REL_WHEEL; |
| 1557 | mAccumulator.relWheel = rawEvent->value; |
| 1558 | break; |
| 1559 | case REL_HWHEEL: |
| 1560 | mAccumulator.fields |= Accumulator::FIELD_REL_HWHEEL; |
| 1561 | mAccumulator.relHWheel = rawEvent->value; |
| 1562 | break; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1563 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1564 | break; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1565 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1566 | case EV_SYN: |
| 1567 | switch (rawEvent->scanCode) { |
| 1568 | case SYN_REPORT: |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 1569 | sync(rawEvent->when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1570 | break; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1571 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1572 | break; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1573 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1574 | } |
| 1575 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1576 | void CursorInputMapper::sync(nsecs_t when) { |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 1577 | uint32_t fields = mAccumulator.fields; |
| 1578 | if (fields == 0) { |
| 1579 | return; // no new state changes, so nothing to do |
| 1580 | } |
| 1581 | |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 1582 | int32_t motionEventAction; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 1583 | int32_t lastButtonState, currentButtonState; |
| 1584 | PointerProperties pointerProperties; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1585 | PointerCoords pointerCoords; |
| 1586 | nsecs_t downTime; |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1587 | float vscroll, hscroll; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1588 | { // acquire lock |
| 1589 | AutoMutex _l(mLock); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1590 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 1591 | lastButtonState = mLocked.buttonState; |
| 1592 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1593 | bool down, downChanged; |
| 1594 | bool wasDown = isPointerDown(mLocked.buttonState); |
| 1595 | bool buttonsChanged = fields & Accumulator::FIELD_BUTTONS; |
| 1596 | if (buttonsChanged) { |
| 1597 | mLocked.buttonState = (mLocked.buttonState | mAccumulator.buttonDown) |
| 1598 | & ~mAccumulator.buttonUp; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1599 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1600 | down = isPointerDown(mLocked.buttonState); |
| 1601 | |
| 1602 | if (!wasDown && down) { |
| 1603 | mLocked.downTime = when; |
| 1604 | downChanged = true; |
| 1605 | } else if (wasDown && !down) { |
| 1606 | downChanged = true; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1607 | } else { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1608 | downChanged = false; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1609 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1610 | } else { |
| 1611 | down = wasDown; |
| 1612 | downChanged = false; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1613 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1614 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 1615 | currentButtonState = mLocked.buttonState; |
| 1616 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1617 | downTime = mLocked.downTime; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1618 | float deltaX = fields & Accumulator::FIELD_REL_X ? mAccumulator.relX * mXScale : 0.0f; |
| 1619 | float deltaY = fields & Accumulator::FIELD_REL_Y ? mAccumulator.relY * mYScale : 0.0f; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1620 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1621 | if (downChanged) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1622 | motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP; |
| 1623 | } else if (down || mPointerController == NULL) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1624 | motionEventAction = AMOTION_EVENT_ACTION_MOVE; |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 1625 | } else { |
| 1626 | motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1627 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1628 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1629 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0 |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1630 | && (deltaX != 0.0f || deltaY != 0.0f)) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1631 | // Rotate motion based on display orientation if needed. |
| 1632 | // Note: getDisplayInfo is non-reentrant so we can continue holding the lock. |
| 1633 | int32_t orientation; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1634 | if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId, |
| 1635 | NULL, NULL, & orientation)) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1636 | orientation = DISPLAY_ORIENTATION_0; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | float temp; |
| 1640 | switch (orientation) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1641 | case DISPLAY_ORIENTATION_90: |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1642 | temp = deltaX; |
| 1643 | deltaX = deltaY; |
| 1644 | deltaY = -temp; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1645 | break; |
| 1646 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1647 | case DISPLAY_ORIENTATION_180: |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1648 | deltaX = -deltaX; |
| 1649 | deltaY = -deltaY; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1650 | break; |
| 1651 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1652 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1653 | temp = deltaX; |
| 1654 | deltaX = -deltaY; |
| 1655 | deltaY = temp; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1656 | break; |
| 1657 | } |
| 1658 | } |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1659 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 1660 | pointerProperties.clear(); |
| 1661 | pointerProperties.id = 0; |
| 1662 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE; |
| 1663 | |
| 1664 | pointerCoords.clear(); |
| 1665 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 1666 | if (mHaveVWheel && (fields & Accumulator::FIELD_REL_WHEEL)) { |
| 1667 | vscroll = mAccumulator.relWheel; |
| 1668 | } else { |
| 1669 | vscroll = 0; |
| 1670 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 1671 | mWheelYVelocityControl.move(when, NULL, &vscroll); |
| 1672 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 1673 | if (mHaveHWheel && (fields & Accumulator::FIELD_REL_HWHEEL)) { |
| 1674 | hscroll = mAccumulator.relHWheel; |
| 1675 | } else { |
| 1676 | hscroll = 0; |
| 1677 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 1678 | mWheelXVelocityControl.move(when, &hscroll, NULL); |
| 1679 | |
| 1680 | mPointerVelocityControl.move(when, &deltaX, &deltaY); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 1681 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1682 | if (mPointerController != NULL) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 1683 | if (deltaX != 0 || deltaY != 0 || vscroll != 0 || hscroll != 0 |
| 1684 | || buttonsChanged) { |
| 1685 | mPointerController->setPresentation( |
| 1686 | PointerControllerInterface::PRESENTATION_POINTER); |
| 1687 | |
| 1688 | if (deltaX != 0 || deltaY != 0) { |
| 1689 | mPointerController->move(deltaX, deltaY); |
| 1690 | } |
| 1691 | |
| 1692 | if (buttonsChanged) { |
| 1693 | mPointerController->setButtonState(mLocked.buttonState); |
| 1694 | } |
| 1695 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 1696 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1697 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1698 | |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 1699 | float x, y; |
| 1700 | mPointerController->getPosition(&x, &y); |
Jeff Brown | ebbd5d1 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 1701 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 1702 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1703 | } else { |
Jeff Brown | ebbd5d1 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 1704 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX); |
| 1705 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1706 | } |
| 1707 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1708 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1709 | } // release lock |
| 1710 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1711 | // Moving an external trackball or mouse should wake the device. |
| 1712 | // We don't do this for internal cursor devices to prevent them from waking up |
| 1713 | // the device in your pocket. |
| 1714 | // TODO: Use the input device configuration to control this behavior more finely. |
| 1715 | uint32_t policyFlags = 0; |
| 1716 | if (getDevice()->isExternal()) { |
| 1717 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 1718 | } |
| 1719 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 1720 | // Synthesize key down from buttons if needed. |
| 1721 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource, |
| 1722 | policyFlags, lastButtonState, currentButtonState); |
| 1723 | |
| 1724 | // Send motion event. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1725 | int32_t metaState = mContext->getGlobalMetaState(); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1726 | getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags, |
Jeff Brown | a611137 | 2011-07-14 21:48:23 -0700 | [diff] [blame] | 1727 | motionEventAction, 0, metaState, currentButtonState, 0, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 1728 | 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1729 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1730 | // Send hover move after UP to tell the application that the mouse is hovering now. |
| 1731 | if (motionEventAction == AMOTION_EVENT_ACTION_UP |
| 1732 | && mPointerController != NULL) { |
| 1733 | getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 1734 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 1735 | metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 1736 | 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1737 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1738 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1739 | // Send scroll events. |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1740 | if (vscroll != 0 || hscroll != 0) { |
| 1741 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll); |
| 1742 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll); |
| 1743 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1744 | getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 1745 | AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState, |
| 1746 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 1747 | 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime); |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1748 | } |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1749 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 1750 | // Synthesize key up from buttons if needed. |
| 1751 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource, |
| 1752 | policyFlags, lastButtonState, currentButtonState); |
| 1753 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1754 | mAccumulator.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1755 | } |
| 1756 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1757 | int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Jeff Brown | c3fc2d0 | 2010-08-10 15:47:53 -0700 | [diff] [blame] | 1758 | if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) { |
| 1759 | return getEventHub()->getScanCodeState(getDeviceId(), scanCode); |
| 1760 | } else { |
| 1761 | return AKEY_STATE_UNKNOWN; |
| 1762 | } |
| 1763 | } |
| 1764 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1765 | void CursorInputMapper::fadePointer() { |
| 1766 | { // acquire lock |
| 1767 | AutoMutex _l(mLock); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1768 | if (mPointerController != NULL) { |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 1769 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1770 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1771 | } // release lock |
| 1772 | } |
| 1773 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1774 | |
| 1775 | // --- TouchInputMapper --- |
| 1776 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1777 | TouchInputMapper::TouchInputMapper(InputDevice* device) : |
| 1778 | InputMapper(device) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1779 | mLocked.surfaceOrientation = -1; |
| 1780 | mLocked.surfaceWidth = -1; |
| 1781 | mLocked.surfaceHeight = -1; |
| 1782 | |
| 1783 | initializeLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | TouchInputMapper::~TouchInputMapper() { |
| 1787 | } |
| 1788 | |
| 1789 | uint32_t TouchInputMapper::getSources() { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1790 | return mTouchSource | mPointerSource; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 1794 | InputMapper::populateDeviceInfo(info); |
| 1795 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1796 | { // acquire lock |
| 1797 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1798 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1799 | // Ensure surface information is up to date so that orientation changes are |
| 1800 | // noticed immediately. |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1801 | if (!configureSurfaceLocked()) { |
| 1802 | return; |
| 1803 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1804 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1805 | info->addMotionRange(mLocked.orientedRanges.x); |
| 1806 | info->addMotionRange(mLocked.orientedRanges.y); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1807 | |
| 1808 | if (mLocked.orientedRanges.havePressure) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1809 | info->addMotionRange(mLocked.orientedRanges.pressure); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | if (mLocked.orientedRanges.haveSize) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1813 | info->addMotionRange(mLocked.orientedRanges.size); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1814 | } |
| 1815 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1816 | if (mLocked.orientedRanges.haveTouchSize) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1817 | info->addMotionRange(mLocked.orientedRanges.touchMajor); |
| 1818 | info->addMotionRange(mLocked.orientedRanges.touchMinor); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1821 | if (mLocked.orientedRanges.haveToolSize) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1822 | info->addMotionRange(mLocked.orientedRanges.toolMajor); |
| 1823 | info->addMotionRange(mLocked.orientedRanges.toolMinor); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1824 | } |
| 1825 | |
| 1826 | if (mLocked.orientedRanges.haveOrientation) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1827 | info->addMotionRange(mLocked.orientedRanges.orientation); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1828 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1829 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 1830 | if (mLocked.orientedRanges.haveDistance) { |
| 1831 | info->addMotionRange(mLocked.orientedRanges.distance); |
| 1832 | } |
| 1833 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1834 | if (mPointerController != NULL) { |
| 1835 | float minX, minY, maxX, maxY; |
| 1836 | if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) { |
| 1837 | info->addMotionRange(AMOTION_EVENT_AXIS_X, mPointerSource, |
| 1838 | minX, maxX, 0.0f, 0.0f); |
| 1839 | info->addMotionRange(AMOTION_EVENT_AXIS_Y, mPointerSource, |
| 1840 | minY, maxY, 0.0f, 0.0f); |
| 1841 | } |
| 1842 | info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mPointerSource, |
| 1843 | 0.0f, 1.0f, 0.0f, 0.0f); |
| 1844 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1845 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1846 | } |
| 1847 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1848 | void TouchInputMapper::dump(String8& dump) { |
| 1849 | { // acquire lock |
| 1850 | AutoMutex _l(mLock); |
| 1851 | dump.append(INDENT2 "Touch Input Mapper:\n"); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1852 | dumpParameters(dump); |
| 1853 | dumpVirtualKeysLocked(dump); |
| 1854 | dumpRawAxes(dump); |
| 1855 | dumpCalibration(dump); |
| 1856 | dumpSurfaceLocked(dump); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1857 | |
Jeff Brown | 511ee5f | 2010-10-18 13:32:20 -0700 | [diff] [blame] | 1858 | dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n"); |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1859 | dump.appendFormat(INDENT4 "XScale: %0.3f\n", mLocked.xScale); |
| 1860 | dump.appendFormat(INDENT4 "YScale: %0.3f\n", mLocked.yScale); |
| 1861 | dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mLocked.xPrecision); |
| 1862 | dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mLocked.yPrecision); |
| 1863 | dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mLocked.geometricScale); |
| 1864 | dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mLocked.toolSizeLinearScale); |
| 1865 | dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mLocked.toolSizeLinearBias); |
| 1866 | dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mLocked.toolSizeAreaScale); |
| 1867 | dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mLocked.toolSizeAreaBias); |
| 1868 | dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mLocked.pressureScale); |
| 1869 | dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mLocked.sizeScale); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1870 | dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mLocked.orientationScale); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 1871 | dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mLocked.distanceScale); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 1872 | |
| 1873 | dump.appendFormat(INDENT3 "Last Touch:\n"); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1874 | dump.appendFormat(INDENT4 "Button State: 0x%08x\n", mLastTouch.buttonState); |
Jeff Brown | aba321a | 2011-06-28 20:34:40 -0700 | [diff] [blame] | 1875 | dump.appendFormat(INDENT4 "Pointer Count: %d\n", mLastTouch.pointerCount); |
| 1876 | for (uint32_t i = 0; i < mLastTouch.pointerCount; i++) { |
| 1877 | const PointerData& pointer = mLastTouch.pointers[i]; |
| 1878 | dump.appendFormat(INDENT5 "[%d]: id=%d, x=%d, y=%d, pressure=%d, " |
| 1879 | "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, " |
| 1880 | "orientation=%d, distance=%d, isStylus=%s\n", i, |
| 1881 | pointer.id, pointer.x, pointer.y, pointer.pressure, |
| 1882 | pointer.touchMajor, pointer.touchMinor, pointer.toolMajor, pointer.toolMinor, |
| 1883 | pointer.orientation, pointer.distance, toString(pointer.isStylus)); |
| 1884 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1885 | |
| 1886 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) { |
| 1887 | dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n"); |
| 1888 | dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n", |
| 1889 | mLocked.pointerGestureXMovementScale); |
| 1890 | dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n", |
| 1891 | mLocked.pointerGestureYMovementScale); |
| 1892 | dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n", |
| 1893 | mLocked.pointerGestureXZoomScale); |
| 1894 | dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n", |
| 1895 | mLocked.pointerGestureYZoomScale); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 1896 | dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n", |
| 1897 | mLocked.pointerGestureMaxSwipeWidth); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1898 | } |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1899 | } // release lock |
| 1900 | } |
| 1901 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1902 | void TouchInputMapper::initializeLocked() { |
| 1903 | mCurrentTouch.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1904 | mLastTouch.clear(); |
| 1905 | mDownTime = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1906 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1907 | mLocked.currentVirtualKey.down = false; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1908 | |
| 1909 | mLocked.orientedRanges.havePressure = false; |
| 1910 | mLocked.orientedRanges.haveSize = false; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1911 | mLocked.orientedRanges.haveTouchSize = false; |
| 1912 | mLocked.orientedRanges.haveToolSize = false; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1913 | mLocked.orientedRanges.haveOrientation = false; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 1914 | mLocked.orientedRanges.haveDistance = false; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1915 | |
| 1916 | mPointerGesture.reset(); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1917 | } |
| 1918 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 1919 | void TouchInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) { |
| 1920 | InputMapper::configure(config, changes); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1921 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 1922 | mConfig = *config; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1923 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 1924 | if (!changes) { // first time only |
| 1925 | // Configure basic parameters. |
| 1926 | configureParameters(); |
| 1927 | |
| 1928 | // Configure sources. |
| 1929 | switch (mParameters.deviceType) { |
| 1930 | case Parameters::DEVICE_TYPE_TOUCH_SCREEN: |
| 1931 | mTouchSource = AINPUT_SOURCE_TOUCHSCREEN; |
| 1932 | mPointerSource = 0; |
| 1933 | break; |
| 1934 | case Parameters::DEVICE_TYPE_TOUCH_PAD: |
| 1935 | mTouchSource = AINPUT_SOURCE_TOUCHPAD; |
| 1936 | mPointerSource = 0; |
| 1937 | break; |
| 1938 | case Parameters::DEVICE_TYPE_POINTER: |
| 1939 | mTouchSource = AINPUT_SOURCE_TOUCHPAD; |
| 1940 | mPointerSource = AINPUT_SOURCE_MOUSE; |
| 1941 | break; |
| 1942 | default: |
| 1943 | LOG_ASSERT(false); |
| 1944 | } |
| 1945 | |
| 1946 | // Configure absolute axis information. |
| 1947 | configureRawAxes(); |
| 1948 | |
| 1949 | // Prepare input device calibration. |
| 1950 | parseCalibration(); |
| 1951 | resolveCalibration(); |
| 1952 | |
| 1953 | { // acquire lock |
| 1954 | AutoMutex _l(mLock); |
| 1955 | |
| 1956 | // Configure surface dimensions and orientation. |
| 1957 | configureSurfaceLocked(); |
| 1958 | } // release lock |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1959 | } |
| 1960 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 1961 | if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) { |
| 1962 | mPointerGesture.pointerVelocityControl.setParameters( |
| 1963 | mConfig.pointerVelocityControlParameters); |
| 1964 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1965 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 1966 | if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT)) { |
| 1967 | // Reset the touch screen when pointer gesture enablement changes. |
| 1968 | reset(); |
| 1969 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1970 | } |
| 1971 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1972 | void TouchInputMapper::configureParameters() { |
Jeff Brown | b126822 | 2011-06-03 17:06:16 -0700 | [diff] [blame] | 1973 | // Use the pointer presentation mode for devices that do not support distinct |
| 1974 | // multitouch. The spot-based presentation relies on being able to accurately |
| 1975 | // locate two or more fingers on the touch pad. |
| 1976 | mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT) |
| 1977 | ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 1978 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 1979 | String8 gestureModeString; |
| 1980 | if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"), |
| 1981 | gestureModeString)) { |
| 1982 | if (gestureModeString == "pointer") { |
| 1983 | mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER; |
| 1984 | } else if (gestureModeString == "spots") { |
| 1985 | mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS; |
| 1986 | } else if (gestureModeString != "default") { |
| 1987 | LOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string()); |
| 1988 | } |
| 1989 | } |
| 1990 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 1991 | if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X) |
| 1992 | || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) { |
| 1993 | // The device is a cursor device with a touch pad attached. |
| 1994 | // By default don't use the touch pad to move the pointer. |
| 1995 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 1996 | } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) { |
| 1997 | // The device is a pointing device like a track pad. |
| 1998 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
| 1999 | } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) { |
| 2000 | // The device is a touch screen. |
| 2001 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2002 | } else { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2003 | // The device is a touch pad of unknown purpose. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2004 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
| 2005 | } |
| 2006 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2007 | String8 deviceTypeString; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2008 | if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"), |
| 2009 | deviceTypeString)) { |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 2010 | if (deviceTypeString == "touchScreen") { |
| 2011 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2012 | } else if (deviceTypeString == "touchPad") { |
| 2013 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2014 | } else if (deviceTypeString == "pointer") { |
| 2015 | mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2016 | } else if (deviceTypeString != "default") { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2017 | LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string()); |
| 2018 | } |
| 2019 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2020 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2021 | mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2022 | getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"), |
| 2023 | mParameters.orientationAware); |
| 2024 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2025 | mParameters.associatedDisplayId = mParameters.orientationAware |
| 2026 | || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2027 | || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2028 | ? 0 : -1; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2029 | } |
| 2030 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2031 | void TouchInputMapper::dumpParameters(String8& dump) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2032 | dump.append(INDENT3 "Parameters:\n"); |
| 2033 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 2034 | switch (mParameters.gestureMode) { |
| 2035 | case Parameters::GESTURE_MODE_POINTER: |
| 2036 | dump.append(INDENT4 "GestureMode: pointer\n"); |
| 2037 | break; |
| 2038 | case Parameters::GESTURE_MODE_SPOTS: |
| 2039 | dump.append(INDENT4 "GestureMode: spots\n"); |
| 2040 | break; |
| 2041 | default: |
| 2042 | assert(false); |
| 2043 | } |
| 2044 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2045 | switch (mParameters.deviceType) { |
| 2046 | case Parameters::DEVICE_TYPE_TOUCH_SCREEN: |
| 2047 | dump.append(INDENT4 "DeviceType: touchScreen\n"); |
| 2048 | break; |
| 2049 | case Parameters::DEVICE_TYPE_TOUCH_PAD: |
| 2050 | dump.append(INDENT4 "DeviceType: touchPad\n"); |
| 2051 | break; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2052 | case Parameters::DEVICE_TYPE_POINTER: |
| 2053 | dump.append(INDENT4 "DeviceType: pointer\n"); |
| 2054 | break; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2055 | default: |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 2056 | LOG_ASSERT(false); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n", |
| 2060 | mParameters.associatedDisplayId); |
| 2061 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 2062 | toString(mParameters.orientationAware)); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2063 | } |
| 2064 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2065 | void TouchInputMapper::configureRawAxes() { |
| 2066 | mRawAxes.x.clear(); |
| 2067 | mRawAxes.y.clear(); |
| 2068 | mRawAxes.pressure.clear(); |
| 2069 | mRawAxes.touchMajor.clear(); |
| 2070 | mRawAxes.touchMinor.clear(); |
| 2071 | mRawAxes.toolMajor.clear(); |
| 2072 | mRawAxes.toolMinor.clear(); |
| 2073 | mRawAxes.orientation.clear(); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2074 | mRawAxes.distance.clear(); |
| 2075 | mRawAxes.trackingId.clear(); |
| 2076 | mRawAxes.slot.clear(); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2077 | } |
| 2078 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2079 | void TouchInputMapper::dumpRawAxes(String8& dump) { |
| 2080 | dump.append(INDENT3 "Raw Axes:\n"); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2081 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.x, "X"); |
| 2082 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.y, "Y"); |
| 2083 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.pressure, "Pressure"); |
| 2084 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor"); |
| 2085 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor"); |
| 2086 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor"); |
| 2087 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor"); |
| 2088 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.orientation, "Orientation"); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2089 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.distance, "Distance"); |
| 2090 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.trackingId, "TrackingId"); |
| 2091 | dumpRawAbsoluteAxisInfo(dump, mRawAxes.slot, "Slot"); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2092 | } |
| 2093 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2094 | bool TouchInputMapper::configureSurfaceLocked() { |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2095 | // Ensure we have valid X and Y axes. |
| 2096 | if (!mRawAxes.x.valid || !mRawAxes.y.valid) { |
| 2097 | LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! " |
| 2098 | "The device will be inoperable.", getDeviceName().string()); |
| 2099 | return false; |
| 2100 | } |
| 2101 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2102 | // Update orientation and dimensions if needed. |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 2103 | int32_t orientation = DISPLAY_ORIENTATION_0; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2104 | int32_t width = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1; |
| 2105 | int32_t height = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2106 | |
| 2107 | if (mParameters.associatedDisplayId >= 0) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2108 | // Note: getDisplayInfo is non-reentrant so we can continue holding the lock. |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2109 | if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId, |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2110 | &mLocked.associatedDisplayWidth, &mLocked.associatedDisplayHeight, |
| 2111 | &mLocked.associatedDisplayOrientation)) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2112 | return false; |
| 2113 | } |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2114 | |
| 2115 | // A touch screen inherits the dimensions of the display. |
| 2116 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) { |
| 2117 | width = mLocked.associatedDisplayWidth; |
| 2118 | height = mLocked.associatedDisplayHeight; |
| 2119 | } |
| 2120 | |
| 2121 | // The device inherits the orientation of the display if it is orientation aware. |
| 2122 | if (mParameters.orientationAware) { |
| 2123 | orientation = mLocked.associatedDisplayOrientation; |
| 2124 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2125 | } |
| 2126 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2127 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER |
| 2128 | && mPointerController == NULL) { |
| 2129 | mPointerController = getPolicy()->obtainPointerController(getDeviceId()); |
| 2130 | } |
| 2131 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2132 | bool orientationChanged = mLocked.surfaceOrientation != orientation; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2133 | if (orientationChanged) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2134 | mLocked.surfaceOrientation = orientation; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2135 | } |
| 2136 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2137 | bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2138 | if (sizeChanged) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2139 | LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d", |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2140 | getDeviceId(), getDeviceName().string(), width, height); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2141 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2142 | mLocked.surfaceWidth = width; |
| 2143 | mLocked.surfaceHeight = height; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2144 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2145 | // Configure X and Y factors. |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2146 | mLocked.xScale = float(width) / (mRawAxes.x.maxValue - mRawAxes.x.minValue + 1); |
| 2147 | mLocked.yScale = float(height) / (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1); |
| 2148 | mLocked.xPrecision = 1.0f / mLocked.xScale; |
| 2149 | mLocked.yPrecision = 1.0f / mLocked.yScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2150 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2151 | mLocked.orientedRanges.x.axis = AMOTION_EVENT_AXIS_X; |
| 2152 | mLocked.orientedRanges.x.source = mTouchSource; |
| 2153 | mLocked.orientedRanges.y.axis = AMOTION_EVENT_AXIS_Y; |
| 2154 | mLocked.orientedRanges.y.source = mTouchSource; |
| 2155 | |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2156 | configureVirtualKeysLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2157 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2158 | // Scale factor for terms that are not oriented in a particular axis. |
| 2159 | // If the pixels are square then xScale == yScale otherwise we fake it |
| 2160 | // by choosing an average. |
| 2161 | mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2162 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2163 | // Size of diagonal axis. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2164 | float diagonalSize = hypotf(width, height); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2165 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2166 | // TouchMajor and TouchMinor factors. |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2167 | if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) { |
| 2168 | mLocked.orientedRanges.haveTouchSize = true; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2169 | |
| 2170 | mLocked.orientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR; |
| 2171 | mLocked.orientedRanges.touchMajor.source = mTouchSource; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2172 | mLocked.orientedRanges.touchMajor.min = 0; |
| 2173 | mLocked.orientedRanges.touchMajor.max = diagonalSize; |
| 2174 | mLocked.orientedRanges.touchMajor.flat = 0; |
| 2175 | mLocked.orientedRanges.touchMajor.fuzz = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2176 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2177 | mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2178 | mLocked.orientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2179 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2180 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2181 | // ToolMajor and ToolMinor factors. |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2182 | mLocked.toolSizeLinearScale = 0; |
| 2183 | mLocked.toolSizeLinearBias = 0; |
| 2184 | mLocked.toolSizeAreaScale = 0; |
| 2185 | mLocked.toolSizeAreaBias = 0; |
| 2186 | if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) { |
| 2187 | if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) { |
| 2188 | if (mCalibration.haveToolSizeLinearScale) { |
| 2189 | mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2190 | } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2191 | mLocked.toolSizeLinearScale = float(min(width, height)) |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2192 | / mRawAxes.toolMajor.maxValue; |
| 2193 | } |
| 2194 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2195 | if (mCalibration.haveToolSizeLinearBias) { |
| 2196 | mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias; |
| 2197 | } |
| 2198 | } else if (mCalibration.toolSizeCalibration == |
| 2199 | Calibration::TOOL_SIZE_CALIBRATION_AREA) { |
| 2200 | if (mCalibration.haveToolSizeLinearScale) { |
| 2201 | mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale; |
| 2202 | } else { |
| 2203 | mLocked.toolSizeLinearScale = min(width, height); |
| 2204 | } |
| 2205 | |
| 2206 | if (mCalibration.haveToolSizeLinearBias) { |
| 2207 | mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias; |
| 2208 | } |
| 2209 | |
| 2210 | if (mCalibration.haveToolSizeAreaScale) { |
| 2211 | mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale; |
| 2212 | } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) { |
| 2213 | mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue; |
| 2214 | } |
| 2215 | |
| 2216 | if (mCalibration.haveToolSizeAreaBias) { |
| 2217 | mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2218 | } |
| 2219 | } |
| 2220 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2221 | mLocked.orientedRanges.haveToolSize = true; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2222 | |
| 2223 | mLocked.orientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR; |
| 2224 | mLocked.orientedRanges.toolMajor.source = mTouchSource; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2225 | mLocked.orientedRanges.toolMajor.min = 0; |
| 2226 | mLocked.orientedRanges.toolMajor.max = diagonalSize; |
| 2227 | mLocked.orientedRanges.toolMajor.flat = 0; |
| 2228 | mLocked.orientedRanges.toolMajor.fuzz = 0; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2229 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2230 | mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2231 | mLocked.orientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2232 | } |
| 2233 | |
| 2234 | // Pressure factors. |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2235 | mLocked.pressureScale = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2236 | if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) { |
| 2237 | RawAbsoluteAxisInfo rawPressureAxis; |
| 2238 | switch (mCalibration.pressureSource) { |
| 2239 | case Calibration::PRESSURE_SOURCE_PRESSURE: |
| 2240 | rawPressureAxis = mRawAxes.pressure; |
| 2241 | break; |
| 2242 | case Calibration::PRESSURE_SOURCE_TOUCH: |
| 2243 | rawPressureAxis = mRawAxes.touchMajor; |
| 2244 | break; |
| 2245 | default: |
| 2246 | rawPressureAxis.clear(); |
| 2247 | } |
| 2248 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2249 | if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL |
| 2250 | || mCalibration.pressureCalibration |
| 2251 | == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) { |
| 2252 | if (mCalibration.havePressureScale) { |
| 2253 | mLocked.pressureScale = mCalibration.pressureScale; |
| 2254 | } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) { |
| 2255 | mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue; |
| 2256 | } |
| 2257 | } |
| 2258 | |
| 2259 | mLocked.orientedRanges.havePressure = true; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2260 | |
| 2261 | mLocked.orientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE; |
| 2262 | mLocked.orientedRanges.pressure.source = mTouchSource; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2263 | mLocked.orientedRanges.pressure.min = 0; |
| 2264 | mLocked.orientedRanges.pressure.max = 1.0; |
| 2265 | mLocked.orientedRanges.pressure.flat = 0; |
| 2266 | mLocked.orientedRanges.pressure.fuzz = 0; |
| 2267 | } |
| 2268 | |
| 2269 | // Size factors. |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2270 | mLocked.sizeScale = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2271 | if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2272 | if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) { |
| 2273 | if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) { |
| 2274 | mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue; |
| 2275 | } |
| 2276 | } |
| 2277 | |
| 2278 | mLocked.orientedRanges.haveSize = true; |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2279 | |
| 2280 | mLocked.orientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE; |
| 2281 | mLocked.orientedRanges.size.source = mTouchSource; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2282 | mLocked.orientedRanges.size.min = 0; |
| 2283 | mLocked.orientedRanges.size.max = 1.0; |
| 2284 | mLocked.orientedRanges.size.flat = 0; |
| 2285 | mLocked.orientedRanges.size.fuzz = 0; |
| 2286 | } |
| 2287 | |
| 2288 | // Orientation |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2289 | mLocked.orientationScale = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2290 | if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2291 | if (mCalibration.orientationCalibration |
| 2292 | == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) { |
| 2293 | if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) { |
| 2294 | mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue; |
| 2295 | } |
| 2296 | } |
| 2297 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2298 | mLocked.orientedRanges.haveOrientation = true; |
| 2299 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2300 | mLocked.orientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION; |
| 2301 | mLocked.orientedRanges.orientation.source = mTouchSource; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2302 | mLocked.orientedRanges.orientation.min = - M_PI_2; |
| 2303 | mLocked.orientedRanges.orientation.max = M_PI_2; |
| 2304 | mLocked.orientedRanges.orientation.flat = 0; |
| 2305 | mLocked.orientedRanges.orientation.fuzz = 0; |
| 2306 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2307 | |
| 2308 | // Distance |
| 2309 | mLocked.distanceScale = 0; |
| 2310 | if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) { |
| 2311 | if (mCalibration.distanceCalibration |
| 2312 | == Calibration::DISTANCE_CALIBRATION_SCALED) { |
| 2313 | if (mCalibration.haveDistanceScale) { |
| 2314 | mLocked.distanceScale = mCalibration.distanceScale; |
| 2315 | } else { |
| 2316 | mLocked.distanceScale = 1.0f; |
| 2317 | } |
| 2318 | } |
| 2319 | |
| 2320 | mLocked.orientedRanges.haveDistance = true; |
| 2321 | |
| 2322 | mLocked.orientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE; |
| 2323 | mLocked.orientedRanges.distance.source = mTouchSource; |
| 2324 | mLocked.orientedRanges.distance.min = |
| 2325 | mRawAxes.distance.minValue * mLocked.distanceScale; |
| 2326 | mLocked.orientedRanges.distance.max = |
| 2327 | mRawAxes.distance.minValue * mLocked.distanceScale; |
| 2328 | mLocked.orientedRanges.distance.flat = 0; |
| 2329 | mLocked.orientedRanges.distance.fuzz = |
| 2330 | mRawAxes.distance.fuzz * mLocked.distanceScale; |
| 2331 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | if (orientationChanged || sizeChanged) { |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2335 | // Compute oriented surface dimensions, precision, scales and ranges. |
| 2336 | // Note that the maximum value reported is an inclusive maximum value so it is one |
| 2337 | // unit less than the total width or height of surface. |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2338 | switch (mLocked.surfaceOrientation) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 2339 | case DISPLAY_ORIENTATION_90: |
| 2340 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2341 | mLocked.orientedSurfaceWidth = mLocked.surfaceHeight; |
| 2342 | mLocked.orientedSurfaceHeight = mLocked.surfaceWidth; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2343 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2344 | mLocked.orientedXPrecision = mLocked.yPrecision; |
| 2345 | mLocked.orientedYPrecision = mLocked.xPrecision; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2346 | |
| 2347 | mLocked.orientedRanges.x.min = 0; |
| 2348 | mLocked.orientedRanges.x.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue) |
| 2349 | * mLocked.yScale; |
| 2350 | mLocked.orientedRanges.x.flat = 0; |
| 2351 | mLocked.orientedRanges.x.fuzz = mLocked.yScale; |
| 2352 | |
| 2353 | mLocked.orientedRanges.y.min = 0; |
| 2354 | mLocked.orientedRanges.y.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue) |
| 2355 | * mLocked.xScale; |
| 2356 | mLocked.orientedRanges.y.flat = 0; |
| 2357 | mLocked.orientedRanges.y.fuzz = mLocked.xScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2358 | break; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2359 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2360 | default: |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2361 | mLocked.orientedSurfaceWidth = mLocked.surfaceWidth; |
| 2362 | mLocked.orientedSurfaceHeight = mLocked.surfaceHeight; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2363 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2364 | mLocked.orientedXPrecision = mLocked.xPrecision; |
| 2365 | mLocked.orientedYPrecision = mLocked.yPrecision; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2366 | |
| 2367 | mLocked.orientedRanges.x.min = 0; |
| 2368 | mLocked.orientedRanges.x.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue) |
| 2369 | * mLocked.xScale; |
| 2370 | mLocked.orientedRanges.x.flat = 0; |
| 2371 | mLocked.orientedRanges.x.fuzz = mLocked.xScale; |
| 2372 | |
| 2373 | mLocked.orientedRanges.y.min = 0; |
| 2374 | mLocked.orientedRanges.y.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue) |
| 2375 | * mLocked.yScale; |
| 2376 | mLocked.orientedRanges.y.flat = 0; |
| 2377 | mLocked.orientedRanges.y.fuzz = mLocked.yScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2378 | break; |
| 2379 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2380 | |
| 2381 | // Compute pointer gesture detection parameters. |
| 2382 | // TODO: These factors should not be hardcoded. |
| 2383 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) { |
| 2384 | int32_t rawWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1; |
| 2385 | int32_t rawHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2386 | float rawDiagonal = hypotf(rawWidth, rawHeight); |
| 2387 | float displayDiagonal = hypotf(mLocked.associatedDisplayWidth, |
| 2388 | mLocked.associatedDisplayHeight); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2389 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2390 | // 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] | 2391 | // given area relative to the diagonal size of the display when no acceleration |
| 2392 | // is applied. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2393 | // Assume that the touch pad has a square aspect ratio such that movements in |
| 2394 | // X and Y of the same number of raw units cover the same physical distance. |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2395 | mLocked.pointerGestureXMovementScale = mConfig.pointerGestureMovementSpeedRatio |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2396 | * displayDiagonal / rawDiagonal; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2397 | mLocked.pointerGestureYMovementScale = mLocked.pointerGestureXMovementScale; |
| 2398 | |
| 2399 | // Scale zooms to cover a smaller range of the display than movements do. |
| 2400 | // This value determines the area around the pointer that is affected by freeform |
| 2401 | // pointer gestures. |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2402 | mLocked.pointerGestureXZoomScale = mConfig.pointerGestureZoomSpeedRatio |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2403 | * displayDiagonal / rawDiagonal; |
| 2404 | mLocked.pointerGestureYZoomScale = mLocked.pointerGestureXZoomScale; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2405 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2406 | // Max width between pointers to detect a swipe gesture is more than some fraction |
| 2407 | // of the diagonal axis of the touch pad. Touches that are wider than this are |
| 2408 | // translated into freeform gestures. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 2409 | mLocked.pointerGestureMaxSwipeWidth = |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2410 | mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2411 | |
| 2412 | // Reset the current pointer gesture. |
| 2413 | mPointerGesture.reset(); |
| 2414 | |
| 2415 | // Remove any current spots. |
| 2416 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 2417 | mPointerController->clearSpots(); |
| 2418 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2419 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | return true; |
| 2423 | } |
| 2424 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2425 | void TouchInputMapper::dumpSurfaceLocked(String8& dump) { |
| 2426 | dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth); |
| 2427 | dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight); |
| 2428 | dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2429 | } |
| 2430 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2431 | void TouchInputMapper::configureVirtualKeysLocked() { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2432 | Vector<VirtualKeyDefinition> virtualKeyDefinitions; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 2433 | getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2434 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2435 | mLocked.virtualKeys.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2436 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2437 | if (virtualKeyDefinitions.size() == 0) { |
| 2438 | return; |
| 2439 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2440 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2441 | mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size()); |
| 2442 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2443 | int32_t touchScreenLeft = mRawAxes.x.minValue; |
| 2444 | int32_t touchScreenTop = mRawAxes.y.minValue; |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 2445 | int32_t touchScreenWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1; |
| 2446 | int32_t touchScreenHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2447 | |
| 2448 | for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2449 | const VirtualKeyDefinition& virtualKeyDefinition = |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2450 | virtualKeyDefinitions[i]; |
| 2451 | |
| 2452 | mLocked.virtualKeys.add(); |
| 2453 | VirtualKey& virtualKey = mLocked.virtualKeys.editTop(); |
| 2454 | |
| 2455 | virtualKey.scanCode = virtualKeyDefinition.scanCode; |
| 2456 | int32_t keyCode; |
| 2457 | uint32_t flags; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 2458 | if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode, |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2459 | & keyCode, & flags)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2460 | LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", |
| 2461 | virtualKey.scanCode); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2462 | mLocked.virtualKeys.pop(); // drop the key |
| 2463 | continue; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2464 | } |
| 2465 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2466 | virtualKey.keyCode = keyCode; |
| 2467 | virtualKey.flags = flags; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2468 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2469 | // convert the key definition's display coordinates into touch coordinates for a hit box |
| 2470 | int32_t halfWidth = virtualKeyDefinition.width / 2; |
| 2471 | int32_t halfHeight = virtualKeyDefinition.height / 2; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2472 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2473 | virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth) |
| 2474 | * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft; |
| 2475 | virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth) |
| 2476 | * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft; |
| 2477 | virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight) |
| 2478 | * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop; |
| 2479 | virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight) |
| 2480 | * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop; |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2481 | } |
| 2482 | } |
| 2483 | |
| 2484 | void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) { |
| 2485 | if (!mLocked.virtualKeys.isEmpty()) { |
| 2486 | dump.append(INDENT3 "Virtual Keys:\n"); |
| 2487 | |
| 2488 | for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) { |
| 2489 | const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i); |
| 2490 | dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, " |
| 2491 | "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n", |
| 2492 | i, virtualKey.scanCode, virtualKey.keyCode, |
| 2493 | virtualKey.hitLeft, virtualKey.hitRight, |
| 2494 | virtualKey.hitTop, virtualKey.hitBottom); |
| 2495 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2496 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2497 | } |
| 2498 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2499 | void TouchInputMapper::parseCalibration() { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2500 | const PropertyMap& in = getDevice()->getConfiguration(); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2501 | Calibration& out = mCalibration; |
| 2502 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2503 | // Touch Size |
| 2504 | out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT; |
| 2505 | String8 touchSizeCalibrationString; |
| 2506 | if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) { |
| 2507 | if (touchSizeCalibrationString == "none") { |
| 2508 | out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE; |
| 2509 | } else if (touchSizeCalibrationString == "geometric") { |
| 2510 | out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC; |
| 2511 | } else if (touchSizeCalibrationString == "pressure") { |
| 2512 | out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE; |
| 2513 | } else if (touchSizeCalibrationString != "default") { |
| 2514 | LOGW("Invalid value for touch.touchSize.calibration: '%s'", |
| 2515 | touchSizeCalibrationString.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2516 | } |
| 2517 | } |
| 2518 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2519 | // Tool Size |
| 2520 | out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT; |
| 2521 | String8 toolSizeCalibrationString; |
| 2522 | if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) { |
| 2523 | if (toolSizeCalibrationString == "none") { |
| 2524 | out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE; |
| 2525 | } else if (toolSizeCalibrationString == "geometric") { |
| 2526 | out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC; |
| 2527 | } else if (toolSizeCalibrationString == "linear") { |
| 2528 | out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR; |
| 2529 | } else if (toolSizeCalibrationString == "area") { |
| 2530 | out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA; |
| 2531 | } else if (toolSizeCalibrationString != "default") { |
| 2532 | LOGW("Invalid value for touch.toolSize.calibration: '%s'", |
| 2533 | toolSizeCalibrationString.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2534 | } |
| 2535 | } |
| 2536 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2537 | out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"), |
| 2538 | out.toolSizeLinearScale); |
| 2539 | out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"), |
| 2540 | out.toolSizeLinearBias); |
| 2541 | out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"), |
| 2542 | out.toolSizeAreaScale); |
| 2543 | out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"), |
| 2544 | out.toolSizeAreaBias); |
| 2545 | out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"), |
| 2546 | out.toolSizeIsSummed); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2547 | |
| 2548 | // Pressure |
| 2549 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT; |
| 2550 | String8 pressureCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2551 | if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2552 | if (pressureCalibrationString == "none") { |
| 2553 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; |
| 2554 | } else if (pressureCalibrationString == "physical") { |
| 2555 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL; |
| 2556 | } else if (pressureCalibrationString == "amplitude") { |
| 2557 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE; |
| 2558 | } else if (pressureCalibrationString != "default") { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2559 | LOGW("Invalid value for touch.pressure.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2560 | pressureCalibrationString.string()); |
| 2561 | } |
| 2562 | } |
| 2563 | |
| 2564 | out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT; |
| 2565 | String8 pressureSourceString; |
| 2566 | if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) { |
| 2567 | if (pressureSourceString == "pressure") { |
| 2568 | out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE; |
| 2569 | } else if (pressureSourceString == "touch") { |
| 2570 | out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH; |
| 2571 | } else if (pressureSourceString != "default") { |
| 2572 | LOGW("Invalid value for touch.pressure.source: '%s'", |
| 2573 | pressureSourceString.string()); |
| 2574 | } |
| 2575 | } |
| 2576 | |
| 2577 | out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"), |
| 2578 | out.pressureScale); |
| 2579 | |
| 2580 | // Size |
| 2581 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT; |
| 2582 | String8 sizeCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2583 | if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2584 | if (sizeCalibrationString == "none") { |
| 2585 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; |
| 2586 | } else if (sizeCalibrationString == "normalized") { |
| 2587 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED; |
| 2588 | } else if (sizeCalibrationString != "default") { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2589 | LOGW("Invalid value for touch.size.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2590 | sizeCalibrationString.string()); |
| 2591 | } |
| 2592 | } |
| 2593 | |
| 2594 | // Orientation |
| 2595 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT; |
| 2596 | String8 orientationCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2597 | if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2598 | if (orientationCalibrationString == "none") { |
| 2599 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; |
| 2600 | } else if (orientationCalibrationString == "interpolated") { |
| 2601 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 2602 | } else if (orientationCalibrationString == "vector") { |
| 2603 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2604 | } else if (orientationCalibrationString != "default") { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2605 | LOGW("Invalid value for touch.orientation.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2606 | orientationCalibrationString.string()); |
| 2607 | } |
| 2608 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2609 | |
| 2610 | // Distance |
| 2611 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT; |
| 2612 | String8 distanceCalibrationString; |
| 2613 | if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) { |
| 2614 | if (distanceCalibrationString == "none") { |
| 2615 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE; |
| 2616 | } else if (distanceCalibrationString == "scaled") { |
| 2617 | out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED; |
| 2618 | } else if (distanceCalibrationString != "default") { |
| 2619 | LOGW("Invalid value for touch.distance.calibration: '%s'", |
| 2620 | distanceCalibrationString.string()); |
| 2621 | } |
| 2622 | } |
| 2623 | |
| 2624 | out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"), |
| 2625 | out.distanceScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2626 | } |
| 2627 | |
| 2628 | void TouchInputMapper::resolveCalibration() { |
| 2629 | // Pressure |
| 2630 | switch (mCalibration.pressureSource) { |
| 2631 | case Calibration::PRESSURE_SOURCE_DEFAULT: |
| 2632 | if (mRawAxes.pressure.valid) { |
| 2633 | mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE; |
| 2634 | } else if (mRawAxes.touchMajor.valid) { |
| 2635 | mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH; |
| 2636 | } |
| 2637 | break; |
| 2638 | |
| 2639 | case Calibration::PRESSURE_SOURCE_PRESSURE: |
| 2640 | if (! mRawAxes.pressure.valid) { |
| 2641 | LOGW("Calibration property touch.pressure.source is 'pressure' but " |
| 2642 | "the pressure axis is not available."); |
| 2643 | } |
| 2644 | break; |
| 2645 | |
| 2646 | case Calibration::PRESSURE_SOURCE_TOUCH: |
| 2647 | if (! mRawAxes.touchMajor.valid) { |
| 2648 | LOGW("Calibration property touch.pressure.source is 'touch' but " |
| 2649 | "the touchMajor axis is not available."); |
| 2650 | } |
| 2651 | break; |
| 2652 | |
| 2653 | default: |
| 2654 | break; |
| 2655 | } |
| 2656 | |
| 2657 | switch (mCalibration.pressureCalibration) { |
| 2658 | case Calibration::PRESSURE_CALIBRATION_DEFAULT: |
| 2659 | if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) { |
| 2660 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE; |
| 2661 | } else { |
| 2662 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; |
| 2663 | } |
| 2664 | break; |
| 2665 | |
| 2666 | default: |
| 2667 | break; |
| 2668 | } |
| 2669 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2670 | // Tool Size |
| 2671 | switch (mCalibration.toolSizeCalibration) { |
| 2672 | case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT: |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2673 | if (mRawAxes.toolMajor.valid) { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2674 | mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2675 | } else { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2676 | mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2677 | } |
| 2678 | break; |
| 2679 | |
| 2680 | default: |
| 2681 | break; |
| 2682 | } |
| 2683 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2684 | // Touch Size |
| 2685 | switch (mCalibration.touchSizeCalibration) { |
| 2686 | case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT: |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2687 | if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2688 | && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) { |
| 2689 | mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2690 | } else { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2691 | mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2692 | } |
| 2693 | break; |
| 2694 | |
| 2695 | default: |
| 2696 | break; |
| 2697 | } |
| 2698 | |
| 2699 | // Size |
| 2700 | switch (mCalibration.sizeCalibration) { |
| 2701 | case Calibration::SIZE_CALIBRATION_DEFAULT: |
| 2702 | if (mRawAxes.toolMajor.valid) { |
| 2703 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED; |
| 2704 | } else { |
| 2705 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; |
| 2706 | } |
| 2707 | break; |
| 2708 | |
| 2709 | default: |
| 2710 | break; |
| 2711 | } |
| 2712 | |
| 2713 | // Orientation |
| 2714 | switch (mCalibration.orientationCalibration) { |
| 2715 | case Calibration::ORIENTATION_CALIBRATION_DEFAULT: |
| 2716 | if (mRawAxes.orientation.valid) { |
| 2717 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; |
| 2718 | } else { |
| 2719 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; |
| 2720 | } |
| 2721 | break; |
| 2722 | |
| 2723 | default: |
| 2724 | break; |
| 2725 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2726 | |
| 2727 | // Distance |
| 2728 | switch (mCalibration.distanceCalibration) { |
| 2729 | case Calibration::DISTANCE_CALIBRATION_DEFAULT: |
| 2730 | if (mRawAxes.distance.valid) { |
| 2731 | mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED; |
| 2732 | } else { |
| 2733 | mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE; |
| 2734 | } |
| 2735 | break; |
| 2736 | |
| 2737 | default: |
| 2738 | break; |
| 2739 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2740 | } |
| 2741 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2742 | void TouchInputMapper::dumpCalibration(String8& dump) { |
| 2743 | dump.append(INDENT3 "Calibration:\n"); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2744 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2745 | // Touch Size |
| 2746 | switch (mCalibration.touchSizeCalibration) { |
| 2747 | case Calibration::TOUCH_SIZE_CALIBRATION_NONE: |
| 2748 | dump.append(INDENT4 "touch.touchSize.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2749 | break; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2750 | case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC: |
| 2751 | dump.append(INDENT4 "touch.touchSize.calibration: geometric\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2752 | break; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2753 | case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE: |
| 2754 | dump.append(INDENT4 "touch.touchSize.calibration: pressure\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2755 | break; |
| 2756 | default: |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 2757 | LOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2758 | } |
| 2759 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2760 | // Tool Size |
| 2761 | switch (mCalibration.toolSizeCalibration) { |
| 2762 | case Calibration::TOOL_SIZE_CALIBRATION_NONE: |
| 2763 | dump.append(INDENT4 "touch.toolSize.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2764 | break; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2765 | case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC: |
| 2766 | dump.append(INDENT4 "touch.toolSize.calibration: geometric\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2767 | break; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2768 | case Calibration::TOOL_SIZE_CALIBRATION_LINEAR: |
| 2769 | dump.append(INDENT4 "touch.toolSize.calibration: linear\n"); |
| 2770 | break; |
| 2771 | case Calibration::TOOL_SIZE_CALIBRATION_AREA: |
| 2772 | dump.append(INDENT4 "touch.toolSize.calibration: area\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2773 | break; |
| 2774 | default: |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 2775 | LOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2776 | } |
| 2777 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2778 | if (mCalibration.haveToolSizeLinearScale) { |
| 2779 | dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n", |
| 2780 | mCalibration.toolSizeLinearScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2781 | } |
| 2782 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2783 | if (mCalibration.haveToolSizeLinearBias) { |
| 2784 | dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n", |
| 2785 | mCalibration.toolSizeLinearBias); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2786 | } |
| 2787 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2788 | if (mCalibration.haveToolSizeAreaScale) { |
| 2789 | dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n", |
| 2790 | mCalibration.toolSizeAreaScale); |
| 2791 | } |
| 2792 | |
| 2793 | if (mCalibration.haveToolSizeAreaBias) { |
| 2794 | dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n", |
| 2795 | mCalibration.toolSizeAreaBias); |
| 2796 | } |
| 2797 | |
| 2798 | if (mCalibration.haveToolSizeIsSummed) { |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 2799 | dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %s\n", |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2800 | toString(mCalibration.toolSizeIsSummed)); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2801 | } |
| 2802 | |
| 2803 | // Pressure |
| 2804 | switch (mCalibration.pressureCalibration) { |
| 2805 | case Calibration::PRESSURE_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2806 | dump.append(INDENT4 "touch.pressure.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2807 | break; |
| 2808 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2809 | dump.append(INDENT4 "touch.pressure.calibration: physical\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2810 | break; |
| 2811 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2812 | dump.append(INDENT4 "touch.pressure.calibration: amplitude\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2813 | break; |
| 2814 | default: |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 2815 | LOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2816 | } |
| 2817 | |
| 2818 | switch (mCalibration.pressureSource) { |
| 2819 | case Calibration::PRESSURE_SOURCE_PRESSURE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2820 | dump.append(INDENT4 "touch.pressure.source: pressure\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2821 | break; |
| 2822 | case Calibration::PRESSURE_SOURCE_TOUCH: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2823 | dump.append(INDENT4 "touch.pressure.source: touch\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2824 | break; |
| 2825 | case Calibration::PRESSURE_SOURCE_DEFAULT: |
| 2826 | break; |
| 2827 | default: |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 2828 | LOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2829 | } |
| 2830 | |
| 2831 | if (mCalibration.havePressureScale) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2832 | dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n", |
| 2833 | mCalibration.pressureScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2834 | } |
| 2835 | |
| 2836 | // Size |
| 2837 | switch (mCalibration.sizeCalibration) { |
| 2838 | case Calibration::SIZE_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2839 | dump.append(INDENT4 "touch.size.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2840 | break; |
| 2841 | case Calibration::SIZE_CALIBRATION_NORMALIZED: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2842 | dump.append(INDENT4 "touch.size.calibration: normalized\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2843 | break; |
| 2844 | default: |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 2845 | LOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2846 | } |
| 2847 | |
| 2848 | // Orientation |
| 2849 | switch (mCalibration.orientationCalibration) { |
| 2850 | case Calibration::ORIENTATION_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2851 | dump.append(INDENT4 "touch.orientation.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2852 | break; |
| 2853 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2854 | dump.append(INDENT4 "touch.orientation.calibration: interpolated\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2855 | break; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 2856 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: |
| 2857 | dump.append(INDENT4 "touch.orientation.calibration: vector\n"); |
| 2858 | break; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2859 | default: |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 2860 | LOG_ASSERT(false); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2861 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 2862 | |
| 2863 | // Distance |
| 2864 | switch (mCalibration.distanceCalibration) { |
| 2865 | case Calibration::DISTANCE_CALIBRATION_NONE: |
| 2866 | dump.append(INDENT4 "touch.distance.calibration: none\n"); |
| 2867 | break; |
| 2868 | case Calibration::DISTANCE_CALIBRATION_SCALED: |
| 2869 | dump.append(INDENT4 "touch.distance.calibration: scaled\n"); |
| 2870 | break; |
| 2871 | default: |
| 2872 | LOG_ASSERT(false); |
| 2873 | } |
| 2874 | |
| 2875 | if (mCalibration.haveDistanceScale) { |
| 2876 | dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n", |
| 2877 | mCalibration.distanceScale); |
| 2878 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2879 | } |
| 2880 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2881 | void TouchInputMapper::reset() { |
| 2882 | // Synthesize touch up event if touch is currently down. |
| 2883 | // This will also take care of finishing virtual key processing if needed. |
| 2884 | if (mLastTouch.pointerCount != 0) { |
| 2885 | nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2886 | mCurrentTouch.clear(); |
| 2887 | syncTouch(when, true); |
| 2888 | } |
| 2889 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2890 | { // acquire lock |
| 2891 | AutoMutex _l(mLock); |
| 2892 | initializeLocked(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2893 | |
| 2894 | if (mPointerController != NULL |
| 2895 | && mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2896 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 2897 | mPointerController->clearSpots(); |
| 2898 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2899 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2900 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2901 | InputMapper::reset(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2902 | } |
| 2903 | |
| 2904 | void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) { |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 2905 | #if DEBUG_RAW_EVENTS |
| 2906 | if (!havePointerIds) { |
| 2907 | LOGD("syncTouch: pointerCount=%d, no pointer ids", mCurrentTouch.pointerCount); |
| 2908 | } else { |
| 2909 | LOGD("syncTouch: pointerCount=%d, up=0x%08x, down=0x%08x, move=0x%08x, " |
| 2910 | "last=0x%08x, current=0x%08x", mCurrentTouch.pointerCount, |
| 2911 | mLastTouch.idBits.value & ~mCurrentTouch.idBits.value, |
| 2912 | mCurrentTouch.idBits.value & ~mLastTouch.idBits.value, |
| 2913 | mLastTouch.idBits.value & mCurrentTouch.idBits.value, |
| 2914 | mLastTouch.idBits.value, mCurrentTouch.idBits.value); |
| 2915 | } |
| 2916 | #endif |
| 2917 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2918 | // Preprocess pointer data. |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 2919 | if (!havePointerIds) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2920 | calculatePointerIds(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2921 | } |
| 2922 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2923 | uint32_t policyFlags = 0; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2924 | if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2925 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) { |
| 2926 | // If this is a touch screen, hide the pointer on an initial down. |
| 2927 | getContext()->fadePointer(); |
| 2928 | } |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 2929 | |
| 2930 | // Initial downs on external touch devices should wake the device. |
| 2931 | // We don't do this for internal touch screens to prevent them from waking |
| 2932 | // up in your pocket. |
| 2933 | // TODO: Use the input device configuration to control this behavior more finely. |
| 2934 | if (getDevice()->isExternal()) { |
| 2935 | policyFlags |= POLICY_FLAG_WAKE_DROPPED; |
| 2936 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 2937 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2938 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2939 | // Synthesize key down from buttons if needed. |
| 2940 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mTouchSource, |
| 2941 | policyFlags, mLastTouch.buttonState, mCurrentTouch.buttonState); |
| 2942 | |
| 2943 | // Send motion events. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 2944 | TouchResult touchResult; |
| 2945 | if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount == 0 |
| 2946 | && mLastTouch.buttonState == mCurrentTouch.buttonState) { |
| 2947 | // Drop spurious syncs. |
| 2948 | touchResult = DROP_STROKE; |
| 2949 | } else { |
| 2950 | // Process touches and virtual keys. |
| 2951 | touchResult = consumeOffScreenTouches(when, policyFlags); |
| 2952 | if (touchResult == DISPATCH_TOUCH) { |
| 2953 | suppressSwipeOntoVirtualKeys(when); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 2954 | if (mPointerController != NULL && mConfig.pointerGesturesEnabled) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 2955 | dispatchPointerGestures(when, policyFlags, false /*isTimeout*/); |
| 2956 | } |
| 2957 | dispatchTouches(when, policyFlags); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2958 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2959 | } |
| 2960 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2961 | // Synthesize key up from buttons if needed. |
| 2962 | synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mTouchSource, |
| 2963 | policyFlags, mLastTouch.buttonState, mCurrentTouch.buttonState); |
| 2964 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2965 | // Copy current touch to last touch in preparation for the next cycle. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2966 | // Keep the button state so we can track edge-triggered button state changes. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2967 | if (touchResult == DROP_STROKE) { |
| 2968 | mLastTouch.clear(); |
Jeff Brown | a4d1bc5 | 2011-07-01 19:23:40 -0700 | [diff] [blame] | 2969 | mLastTouch.buttonState = mCurrentTouch.buttonState; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2970 | } else { |
Jeff Brown | a4d1bc5 | 2011-07-01 19:23:40 -0700 | [diff] [blame] | 2971 | mLastTouch.copyFrom(mCurrentTouch); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2972 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2973 | } |
| 2974 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 2975 | void TouchInputMapper::timeoutExpired(nsecs_t when) { |
| 2976 | if (mPointerController != NULL) { |
| 2977 | dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/); |
| 2978 | } |
| 2979 | } |
| 2980 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2981 | TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches( |
| 2982 | nsecs_t when, uint32_t policyFlags) { |
| 2983 | int32_t keyEventAction, keyEventFlags; |
| 2984 | int32_t keyCode, scanCode, downTime; |
| 2985 | TouchResult touchResult; |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 2986 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2987 | { // acquire lock |
| 2988 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2989 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2990 | // Update surface size and orientation, including virtual key positions. |
| 2991 | if (! configureSurfaceLocked()) { |
| 2992 | return DROP_STROKE; |
| 2993 | } |
| 2994 | |
| 2995 | // Check for virtual key press. |
| 2996 | if (mLocked.currentVirtualKey.down) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2997 | if (mCurrentTouch.pointerCount == 0) { |
| 2998 | // Pointer went up while virtual key was down. |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2999 | mLocked.currentVirtualKey.down = false; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3000 | #if DEBUG_VIRTUAL_KEYS |
| 3001 | LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d", |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3002 | mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3003 | #endif |
| 3004 | keyEventAction = AKEY_EVENT_ACTION_UP; |
| 3005 | keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3006 | touchResult = SKIP_TOUCH; |
| 3007 | goto DispatchVirtualKey; |
| 3008 | } |
| 3009 | |
| 3010 | if (mCurrentTouch.pointerCount == 1) { |
| 3011 | int32_t x = mCurrentTouch.pointers[0].x; |
| 3012 | int32_t y = mCurrentTouch.pointers[0].y; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3013 | const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y); |
| 3014 | if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3015 | // Pointer is still within the space of the virtual key. |
| 3016 | return SKIP_TOUCH; |
| 3017 | } |
| 3018 | } |
| 3019 | |
| 3020 | // Pointer left virtual key area or another pointer also went down. |
| 3021 | // Send key cancellation and drop the stroke so subsequent motions will be |
| 3022 | // considered fresh downs. This is useful when the user swipes away from the |
| 3023 | // virtual key area into the main display surface. |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3024 | mLocked.currentVirtualKey.down = false; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3025 | #if DEBUG_VIRTUAL_KEYS |
| 3026 | LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d", |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3027 | mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3028 | #endif |
| 3029 | keyEventAction = AKEY_EVENT_ACTION_UP; |
| 3030 | keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
| 3031 | | AKEY_EVENT_FLAG_CANCELED; |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3032 | |
| 3033 | // Check whether the pointer moved inside the display area where we should |
| 3034 | // start a new stroke. |
| 3035 | int32_t x = mCurrentTouch.pointers[0].x; |
| 3036 | int32_t y = mCurrentTouch.pointers[0].y; |
| 3037 | if (isPointInsideSurfaceLocked(x, y)) { |
| 3038 | mLastTouch.clear(); |
| 3039 | touchResult = DISPATCH_TOUCH; |
| 3040 | } else { |
| 3041 | touchResult = DROP_STROKE; |
| 3042 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3043 | } else { |
| 3044 | if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) { |
| 3045 | // Pointer just went down. Handle off-screen touches, if needed. |
| 3046 | int32_t x = mCurrentTouch.pointers[0].x; |
| 3047 | int32_t y = mCurrentTouch.pointers[0].y; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3048 | if (! isPointInsideSurfaceLocked(x, y)) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3049 | // If exactly one pointer went down, check for virtual key hit. |
| 3050 | // Otherwise we will drop the entire stroke. |
| 3051 | if (mCurrentTouch.pointerCount == 1) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3052 | const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3053 | if (virtualKey) { |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3054 | if (mContext->shouldDropVirtualKey(when, getDevice(), |
| 3055 | virtualKey->keyCode, virtualKey->scanCode)) { |
| 3056 | return DROP_STROKE; |
| 3057 | } |
| 3058 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3059 | mLocked.currentVirtualKey.down = true; |
| 3060 | mLocked.currentVirtualKey.downTime = when; |
| 3061 | mLocked.currentVirtualKey.keyCode = virtualKey->keyCode; |
| 3062 | mLocked.currentVirtualKey.scanCode = virtualKey->scanCode; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3063 | #if DEBUG_VIRTUAL_KEYS |
| 3064 | LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d", |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3065 | mLocked.currentVirtualKey.keyCode, |
| 3066 | mLocked.currentVirtualKey.scanCode); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3067 | #endif |
| 3068 | keyEventAction = AKEY_EVENT_ACTION_DOWN; |
| 3069 | keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM |
| 3070 | | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 3071 | touchResult = SKIP_TOUCH; |
| 3072 | goto DispatchVirtualKey; |
| 3073 | } |
| 3074 | } |
| 3075 | return DROP_STROKE; |
| 3076 | } |
| 3077 | } |
| 3078 | return DISPATCH_TOUCH; |
| 3079 | } |
| 3080 | |
| 3081 | DispatchVirtualKey: |
| 3082 | // Collect remaining state needed to dispatch virtual key. |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3083 | keyCode = mLocked.currentVirtualKey.keyCode; |
| 3084 | scanCode = mLocked.currentVirtualKey.scanCode; |
| 3085 | downTime = mLocked.currentVirtualKey.downTime; |
| 3086 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3087 | |
| 3088 | // Dispatch virtual key. |
| 3089 | int32_t metaState = mContext->getGlobalMetaState(); |
Jeff Brown | 0eaf393 | 2010-10-01 14:55:30 -0700 | [diff] [blame] | 3090 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3091 | getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags, |
| 3092 | keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime); |
| 3093 | return touchResult; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3094 | } |
| 3095 | |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 3096 | void TouchInputMapper::suppressSwipeOntoVirtualKeys(nsecs_t when) { |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3097 | // Disable all virtual key touches that happen within a short time interval of the |
| 3098 | // most recent touch. The idea is to filter out stray virtual key presses when |
| 3099 | // interacting with the touch screen. |
| 3100 | // |
| 3101 | // Problems we're trying to solve: |
| 3102 | // |
| 3103 | // 1. While scrolling a list or dragging the window shade, the user swipes down into a |
| 3104 | // virtual key area that is implemented by a separate touch panel and accidentally |
| 3105 | // triggers a virtual key. |
| 3106 | // |
| 3107 | // 2. While typing in the on screen keyboard, the user taps slightly outside the screen |
| 3108 | // area and accidentally triggers a virtual key. This often happens when virtual keys |
| 3109 | // are layed out below the screen near to where the on screen keyboard's space bar |
| 3110 | // is displayed. |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 3111 | if (mConfig.virtualKeyQuietTime > 0 && mCurrentTouch.pointerCount != 0) { |
| 3112 | mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime); |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 3113 | } |
| 3114 | } |
| 3115 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3116 | void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) { |
| 3117 | uint32_t currentPointerCount = mCurrentTouch.pointerCount; |
| 3118 | uint32_t lastPointerCount = mLastTouch.pointerCount; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3119 | if (currentPointerCount == 0 && lastPointerCount == 0) { |
| 3120 | return; // nothing to do! |
| 3121 | } |
| 3122 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3123 | // Update current touch coordinates. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3124 | float xPrecision, yPrecision; |
Jeff Brown | a611137 | 2011-07-14 21:48:23 -0700 | [diff] [blame] | 3125 | prepareTouches(&xPrecision, &yPrecision); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3126 | |
| 3127 | // Dispatch motions. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3128 | BitSet32 currentIdBits = mCurrentTouch.idBits; |
| 3129 | BitSet32 lastIdBits = mLastTouch.idBits; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3130 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 3131 | int32_t buttonState = mCurrentTouch.buttonState; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3132 | |
| 3133 | if (currentIdBits == lastIdBits) { |
| 3134 | // No pointer id changes so this is a move event. |
| 3135 | // The dispatcher takes care of batching moves so we don't have to deal with that here. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3136 | dispatchMotion(when, policyFlags, mTouchSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3137 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, |
| 3138 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 3139 | mCurrentTouchProperties, mCurrentTouchCoords, |
| 3140 | mCurrentTouch.idToIndex, currentIdBits, -1, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3141 | xPrecision, yPrecision, mDownTime); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3142 | } else { |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3143 | // There may be pointers going up and pointers going down and pointers moving |
| 3144 | // all at the same time. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3145 | BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value); |
| 3146 | BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3147 | BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3148 | BitSet32 dispatchedIdBits(lastIdBits.value); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3149 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3150 | // Update last coordinates of pointers that have moved so that we observe the new |
| 3151 | // 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] | 3152 | bool moveNeeded = updateMovedPointers( |
| 3153 | mCurrentTouchProperties, mCurrentTouchCoords, mCurrentTouch.idToIndex, |
| 3154 | mLastTouchProperties, mLastTouchCoords, mLastTouch.idToIndex, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3155 | moveIdBits); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3156 | if (buttonState != mLastTouch.buttonState) { |
| 3157 | moveNeeded = true; |
| 3158 | } |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3159 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3160 | // Dispatch pointer up events. |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3161 | while (!upIdBits.isEmpty()) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3162 | uint32_t upId = upIdBits.firstMarkedBit(); |
| 3163 | upIdBits.clearBit(upId); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3164 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3165 | dispatchMotion(when, policyFlags, mTouchSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3166 | AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0, |
| 3167 | mLastTouchProperties, mLastTouchCoords, |
| 3168 | mLastTouch.idToIndex, dispatchedIdBits, upId, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3169 | xPrecision, yPrecision, mDownTime); |
| 3170 | dispatchedIdBits.clearBit(upId); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3171 | } |
| 3172 | |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3173 | // Dispatch move events if any of the remaining pointers moved from their old locations. |
| 3174 | // Although applications receive new locations as part of individual pointer up |
| 3175 | // events, they do not generally handle them except when presented in a move event. |
| 3176 | if (moveNeeded) { |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 3177 | LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3178 | dispatchMotion(when, policyFlags, mTouchSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3179 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0, |
| 3180 | mCurrentTouchProperties, mCurrentTouchCoords, |
| 3181 | mCurrentTouch.idToIndex, dispatchedIdBits, -1, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3182 | xPrecision, yPrecision, mDownTime); |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3183 | } |
| 3184 | |
| 3185 | // Dispatch pointer down events using the new pointer locations. |
| 3186 | while (!downIdBits.isEmpty()) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3187 | uint32_t downId = downIdBits.firstMarkedBit(); |
| 3188 | downIdBits.clearBit(downId); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3189 | dispatchedIdBits.markBit(downId); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3190 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3191 | if (dispatchedIdBits.count() == 1) { |
| 3192 | // First pointer is going down. Set down time. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3193 | mDownTime = when; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3194 | } |
| 3195 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3196 | dispatchMotion(when, policyFlags, mTouchSource, |
Jeff Brown | a611137 | 2011-07-14 21:48:23 -0700 | [diff] [blame] | 3197 | AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3198 | mCurrentTouchProperties, mCurrentTouchCoords, |
| 3199 | mCurrentTouch.idToIndex, dispatchedIdBits, downId, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3200 | xPrecision, yPrecision, mDownTime); |
| 3201 | } |
| 3202 | } |
| 3203 | |
| 3204 | // Update state for next time. |
| 3205 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3206 | mLastTouchProperties[i].copyFrom(mCurrentTouchProperties[i]); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3207 | mLastTouchCoords[i].copyFrom(mCurrentTouchCoords[i]); |
| 3208 | } |
| 3209 | } |
| 3210 | |
Jeff Brown | a611137 | 2011-07-14 21:48:23 -0700 | [diff] [blame] | 3211 | void TouchInputMapper::prepareTouches(float* outXPrecision, float* outYPrecision) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3212 | uint32_t currentPointerCount = mCurrentTouch.pointerCount; |
| 3213 | uint32_t lastPointerCount = mLastTouch.pointerCount; |
| 3214 | |
| 3215 | AutoMutex _l(mLock); |
| 3216 | |
| 3217 | // Walk through the the active pointers and map touch screen coordinates (TouchData) into |
| 3218 | // display or surface coordinates (PointerCoords) and adjust for display orientation. |
| 3219 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
| 3220 | const PointerData& in = mCurrentTouch.pointers[i]; |
| 3221 | |
| 3222 | // ToolMajor and ToolMinor |
| 3223 | float toolMajor, toolMinor; |
| 3224 | switch (mCalibration.toolSizeCalibration) { |
| 3225 | case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC: |
| 3226 | toolMajor = in.toolMajor * mLocked.geometricScale; |
| 3227 | if (mRawAxes.toolMinor.valid) { |
| 3228 | toolMinor = in.toolMinor * mLocked.geometricScale; |
| 3229 | } else { |
| 3230 | toolMinor = toolMajor; |
| 3231 | } |
| 3232 | break; |
| 3233 | case Calibration::TOOL_SIZE_CALIBRATION_LINEAR: |
| 3234 | toolMajor = in.toolMajor != 0 |
| 3235 | ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias |
| 3236 | : 0; |
| 3237 | if (mRawAxes.toolMinor.valid) { |
| 3238 | toolMinor = in.toolMinor != 0 |
| 3239 | ? in.toolMinor * mLocked.toolSizeLinearScale |
| 3240 | + mLocked.toolSizeLinearBias |
| 3241 | : 0; |
| 3242 | } else { |
| 3243 | toolMinor = toolMajor; |
| 3244 | } |
| 3245 | break; |
| 3246 | case Calibration::TOOL_SIZE_CALIBRATION_AREA: |
| 3247 | if (in.toolMajor != 0) { |
| 3248 | float diameter = sqrtf(in.toolMajor |
| 3249 | * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias); |
| 3250 | toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias; |
| 3251 | } else { |
| 3252 | toolMajor = 0; |
| 3253 | } |
| 3254 | toolMinor = toolMajor; |
| 3255 | break; |
| 3256 | default: |
| 3257 | toolMajor = 0; |
| 3258 | toolMinor = 0; |
| 3259 | break; |
| 3260 | } |
| 3261 | |
| 3262 | if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) { |
| 3263 | toolMajor /= currentPointerCount; |
| 3264 | toolMinor /= currentPointerCount; |
| 3265 | } |
| 3266 | |
| 3267 | // Pressure |
| 3268 | float rawPressure; |
| 3269 | switch (mCalibration.pressureSource) { |
| 3270 | case Calibration::PRESSURE_SOURCE_PRESSURE: |
| 3271 | rawPressure = in.pressure; |
| 3272 | break; |
| 3273 | case Calibration::PRESSURE_SOURCE_TOUCH: |
| 3274 | rawPressure = in.touchMajor; |
| 3275 | break; |
| 3276 | default: |
| 3277 | rawPressure = 0; |
| 3278 | } |
| 3279 | |
| 3280 | float pressure; |
| 3281 | switch (mCalibration.pressureCalibration) { |
| 3282 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: |
| 3283 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: |
| 3284 | pressure = rawPressure * mLocked.pressureScale; |
| 3285 | break; |
| 3286 | default: |
| 3287 | pressure = 1; |
| 3288 | break; |
| 3289 | } |
| 3290 | |
| 3291 | // TouchMajor and TouchMinor |
| 3292 | float touchMajor, touchMinor; |
| 3293 | switch (mCalibration.touchSizeCalibration) { |
| 3294 | case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC: |
| 3295 | touchMajor = in.touchMajor * mLocked.geometricScale; |
| 3296 | if (mRawAxes.touchMinor.valid) { |
| 3297 | touchMinor = in.touchMinor * mLocked.geometricScale; |
| 3298 | } else { |
| 3299 | touchMinor = touchMajor; |
| 3300 | } |
| 3301 | break; |
| 3302 | case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE: |
| 3303 | touchMajor = toolMajor * pressure; |
| 3304 | touchMinor = toolMinor * pressure; |
| 3305 | break; |
| 3306 | default: |
| 3307 | touchMajor = 0; |
| 3308 | touchMinor = 0; |
| 3309 | break; |
| 3310 | } |
| 3311 | |
| 3312 | if (touchMajor > toolMajor) { |
| 3313 | touchMajor = toolMajor; |
| 3314 | } |
| 3315 | if (touchMinor > toolMinor) { |
| 3316 | touchMinor = toolMinor; |
| 3317 | } |
| 3318 | |
| 3319 | // Size |
| 3320 | float size; |
| 3321 | switch (mCalibration.sizeCalibration) { |
| 3322 | case Calibration::SIZE_CALIBRATION_NORMALIZED: { |
| 3323 | float rawSize = mRawAxes.toolMinor.valid |
| 3324 | ? avg(in.toolMajor, in.toolMinor) |
| 3325 | : in.toolMajor; |
| 3326 | size = rawSize * mLocked.sizeScale; |
| 3327 | break; |
| 3328 | } |
| 3329 | default: |
| 3330 | size = 0; |
| 3331 | break; |
| 3332 | } |
| 3333 | |
| 3334 | // Orientation |
| 3335 | float orientation; |
| 3336 | switch (mCalibration.orientationCalibration) { |
| 3337 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: |
| 3338 | orientation = in.orientation * mLocked.orientationScale; |
| 3339 | break; |
| 3340 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: { |
| 3341 | int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4); |
| 3342 | int32_t c2 = signExtendNybble(in.orientation & 0x0f); |
| 3343 | if (c1 != 0 || c2 != 0) { |
| 3344 | orientation = atan2f(c1, c2) * 0.5f; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3345 | float scale = 1.0f + hypotf(c1, c2) / 16.0f; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3346 | touchMajor *= scale; |
| 3347 | touchMinor /= scale; |
| 3348 | toolMajor *= scale; |
| 3349 | toolMinor /= scale; |
| 3350 | } else { |
| 3351 | orientation = 0; |
| 3352 | } |
| 3353 | break; |
| 3354 | } |
| 3355 | default: |
| 3356 | orientation = 0; |
| 3357 | } |
| 3358 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3359 | // Distance |
| 3360 | float distance; |
| 3361 | switch (mCalibration.distanceCalibration) { |
| 3362 | case Calibration::DISTANCE_CALIBRATION_SCALED: |
| 3363 | distance = in.distance * mLocked.distanceScale; |
| 3364 | break; |
| 3365 | default: |
| 3366 | distance = 0; |
| 3367 | } |
| 3368 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3369 | // X and Y |
| 3370 | // Adjust coords for surface orientation. |
| 3371 | float x, y; |
| 3372 | switch (mLocked.surfaceOrientation) { |
| 3373 | case DISPLAY_ORIENTATION_90: |
| 3374 | x = float(in.y - mRawAxes.y.minValue) * mLocked.yScale; |
| 3375 | y = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale; |
| 3376 | orientation -= M_PI_2; |
| 3377 | if (orientation < - M_PI_2) { |
| 3378 | orientation += M_PI; |
| 3379 | } |
| 3380 | break; |
| 3381 | case DISPLAY_ORIENTATION_180: |
| 3382 | x = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale; |
| 3383 | y = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale; |
| 3384 | break; |
| 3385 | case DISPLAY_ORIENTATION_270: |
| 3386 | x = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale; |
| 3387 | y = float(in.x - mRawAxes.x.minValue) * mLocked.xScale; |
| 3388 | orientation += M_PI_2; |
| 3389 | if (orientation > M_PI_2) { |
| 3390 | orientation -= M_PI; |
| 3391 | } |
| 3392 | break; |
| 3393 | default: |
| 3394 | x = float(in.x - mRawAxes.x.minValue) * mLocked.xScale; |
| 3395 | y = float(in.y - mRawAxes.y.minValue) * mLocked.yScale; |
| 3396 | break; |
| 3397 | } |
| 3398 | |
| 3399 | // Write output coords. |
| 3400 | PointerCoords& out = mCurrentTouchCoords[i]; |
| 3401 | out.clear(); |
| 3402 | out.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 3403 | out.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 3404 | out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure); |
| 3405 | out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size); |
| 3406 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor); |
| 3407 | out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor); |
| 3408 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor); |
| 3409 | out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor); |
| 3410 | out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 3411 | if (distance != 0) { |
| 3412 | out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance); |
| 3413 | } |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3414 | |
| 3415 | // Write output properties. |
| 3416 | PointerProperties& properties = mCurrentTouchProperties[i]; |
| 3417 | properties.clear(); |
| 3418 | properties.id = mCurrentTouch.pointers[i].id; |
| 3419 | properties.toolType = getTouchToolType(mCurrentTouch.pointers[i].isStylus); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3420 | } |
| 3421 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3422 | *outXPrecision = mLocked.orientedXPrecision; |
| 3423 | *outYPrecision = mLocked.orientedYPrecision; |
| 3424 | } |
| 3425 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3426 | void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags, |
| 3427 | bool isTimeout) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3428 | // Update current gesture coordinates. |
| 3429 | bool cancelPreviousGesture, finishPreviousGesture; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3430 | bool sendEvents = preparePointerGestures(when, |
| 3431 | &cancelPreviousGesture, &finishPreviousGesture, isTimeout); |
| 3432 | if (!sendEvents) { |
| 3433 | return; |
| 3434 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 3435 | if (finishPreviousGesture) { |
| 3436 | cancelPreviousGesture = false; |
| 3437 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3438 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 3439 | // Update the pointer presentation and spots. |
| 3440 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 3441 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT); |
| 3442 | if (finishPreviousGesture || cancelPreviousGesture) { |
| 3443 | mPointerController->clearSpots(); |
| 3444 | } |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 3445 | mPointerController->setSpots(mPointerGesture.currentGestureCoords, |
| 3446 | mPointerGesture.currentGestureIdToIndex, |
| 3447 | mPointerGesture.currentGestureIdBits); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 3448 | } else { |
| 3449 | mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER); |
| 3450 | } |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 3451 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 3452 | // Show or hide the pointer if needed. |
| 3453 | switch (mPointerGesture.currentGestureMode) { |
| 3454 | case PointerGesture::NEUTRAL: |
| 3455 | case PointerGesture::QUIET: |
| 3456 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS |
| 3457 | && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE |
| 3458 | || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) { |
| 3459 | // Remind the user of where the pointer is after finishing a gesture with spots. |
| 3460 | mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 3461 | } |
| 3462 | break; |
| 3463 | case PointerGesture::TAP: |
| 3464 | case PointerGesture::TAP_DRAG: |
| 3465 | case PointerGesture::BUTTON_CLICK_OR_DRAG: |
| 3466 | case PointerGesture::HOVER: |
| 3467 | case PointerGesture::PRESS: |
| 3468 | // Unfade the pointer when the current gesture manipulates the |
| 3469 | // area directly under the pointer. |
| 3470 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 3471 | break; |
| 3472 | case PointerGesture::SWIPE: |
| 3473 | case PointerGesture::FREEFORM: |
| 3474 | // Fade the pointer when the current gesture manipulates a different |
| 3475 | // area and there are spots to guide the user experience. |
| 3476 | if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) { |
| 3477 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
| 3478 | } else { |
| 3479 | mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE); |
| 3480 | } |
| 3481 | break; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3482 | } |
| 3483 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3484 | // Send events! |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3485 | int32_t metaState = getContext()->getGlobalMetaState(); |
| 3486 | int32_t buttonState = mCurrentTouch.buttonState; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3487 | |
| 3488 | // Update last coordinates of pointers that have moved so that we observe the new |
| 3489 | // 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] | 3490 | bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP |
| 3491 | || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG |
| 3492 | || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3493 | || mPointerGesture.currentGestureMode == PointerGesture::PRESS |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3494 | || mPointerGesture.currentGestureMode == PointerGesture::SWIPE |
| 3495 | || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM; |
| 3496 | bool moveNeeded = false; |
| 3497 | if (down && !cancelPreviousGesture && !finishPreviousGesture |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3498 | && !mPointerGesture.lastGestureIdBits.isEmpty() |
| 3499 | && !mPointerGesture.currentGestureIdBits.isEmpty()) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3500 | BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value |
| 3501 | & mPointerGesture.lastGestureIdBits.value); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3502 | moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3503 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3504 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3505 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 3506 | movedGestureIdBits); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3507 | if (buttonState != mLastTouch.buttonState) { |
| 3508 | moveNeeded = true; |
| 3509 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3510 | } |
| 3511 | |
| 3512 | // Send motion events for all pointers that went up or were canceled. |
| 3513 | BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits); |
| 3514 | if (!dispatchedGestureIdBits.isEmpty()) { |
| 3515 | if (cancelPreviousGesture) { |
| 3516 | dispatchMotion(when, policyFlags, mPointerSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3517 | AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState, |
| 3518 | AMOTION_EVENT_EDGE_FLAG_NONE, |
| 3519 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3520 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 3521 | dispatchedGestureIdBits, -1, |
| 3522 | 0, 0, mPointerGesture.downTime); |
| 3523 | |
| 3524 | dispatchedGestureIdBits.clear(); |
| 3525 | } else { |
| 3526 | BitSet32 upGestureIdBits; |
| 3527 | if (finishPreviousGesture) { |
| 3528 | upGestureIdBits = dispatchedGestureIdBits; |
| 3529 | } else { |
| 3530 | upGestureIdBits.value = dispatchedGestureIdBits.value |
| 3531 | & ~mPointerGesture.currentGestureIdBits.value; |
| 3532 | } |
| 3533 | while (!upGestureIdBits.isEmpty()) { |
| 3534 | uint32_t id = upGestureIdBits.firstMarkedBit(); |
| 3535 | upGestureIdBits.clearBit(id); |
| 3536 | |
| 3537 | dispatchMotion(when, policyFlags, mPointerSource, |
| 3538 | AMOTION_EVENT_ACTION_POINTER_UP, 0, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3539 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 3540 | mPointerGesture.lastGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3541 | mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex, |
| 3542 | dispatchedGestureIdBits, id, |
| 3543 | 0, 0, mPointerGesture.downTime); |
| 3544 | |
| 3545 | dispatchedGestureIdBits.clearBit(id); |
| 3546 | } |
| 3547 | } |
| 3548 | } |
| 3549 | |
| 3550 | // Send motion events for all pointers that moved. |
| 3551 | if (moveNeeded) { |
| 3552 | dispatchMotion(when, policyFlags, mPointerSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3553 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 3554 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3555 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 3556 | dispatchedGestureIdBits, -1, |
| 3557 | 0, 0, mPointerGesture.downTime); |
| 3558 | } |
| 3559 | |
| 3560 | // Send motion events for all pointers that went down. |
| 3561 | if (down) { |
| 3562 | BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value |
| 3563 | & ~dispatchedGestureIdBits.value); |
| 3564 | while (!downGestureIdBits.isEmpty()) { |
| 3565 | uint32_t id = downGestureIdBits.firstMarkedBit(); |
| 3566 | downGestureIdBits.clearBit(id); |
| 3567 | dispatchedGestureIdBits.markBit(id); |
| 3568 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3569 | if (dispatchedGestureIdBits.count() == 1) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3570 | mPointerGesture.downTime = when; |
| 3571 | } |
| 3572 | |
| 3573 | dispatchMotion(when, policyFlags, mPointerSource, |
Jeff Brown | a611137 | 2011-07-14 21:48:23 -0700 | [diff] [blame] | 3574 | AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3575 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3576 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 3577 | dispatchedGestureIdBits, id, |
| 3578 | 0, 0, mPointerGesture.downTime); |
| 3579 | } |
| 3580 | } |
| 3581 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3582 | // Send motion events for hover. |
| 3583 | if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) { |
| 3584 | dispatchMotion(when, policyFlags, mPointerSource, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3585 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 3586 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 3587 | mPointerGesture.currentGestureProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3588 | mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex, |
| 3589 | mPointerGesture.currentGestureIdBits, -1, |
| 3590 | 0, 0, mPointerGesture.downTime); |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 3591 | } else if (dispatchedGestureIdBits.isEmpty() |
| 3592 | && !mPointerGesture.lastGestureIdBits.isEmpty()) { |
| 3593 | // Synthesize a hover move event after all pointers go up to indicate that |
| 3594 | // the pointer is hovering again even if the user is not currently touching |
| 3595 | // the touch pad. This ensures that a view will receive a fresh hover enter |
| 3596 | // event after a tap. |
| 3597 | float x, y; |
| 3598 | mPointerController->getPosition(&x, &y); |
| 3599 | |
| 3600 | PointerProperties pointerProperties; |
| 3601 | pointerProperties.clear(); |
| 3602 | pointerProperties.id = 0; |
| 3603 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER; |
| 3604 | |
| 3605 | PointerCoords pointerCoords; |
| 3606 | pointerCoords.clear(); |
| 3607 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 3608 | pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 3609 | |
| 3610 | getDispatcher()->notifyMotion(when, getDeviceId(), mPointerSource, policyFlags, |
| 3611 | AMOTION_EVENT_ACTION_HOVER_MOVE, 0, |
| 3612 | metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 3613 | 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3614 | } |
| 3615 | |
| 3616 | // Update state. |
| 3617 | mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode; |
| 3618 | if (!down) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3619 | mPointerGesture.lastGestureIdBits.clear(); |
| 3620 | } else { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3621 | mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits; |
| 3622 | for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) { |
| 3623 | uint32_t id = idBits.firstMarkedBit(); |
| 3624 | idBits.clearBit(id); |
| 3625 | uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3626 | mPointerGesture.lastGestureProperties[index].copyFrom( |
| 3627 | mPointerGesture.currentGestureProperties[index]); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3628 | mPointerGesture.lastGestureCoords[index].copyFrom( |
| 3629 | mPointerGesture.currentGestureCoords[index]); |
| 3630 | mPointerGesture.lastGestureIdToIndex[id] = index; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3631 | } |
| 3632 | } |
| 3633 | } |
| 3634 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3635 | bool TouchInputMapper::preparePointerGestures(nsecs_t when, |
| 3636 | bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3637 | *outCancelPreviousGesture = false; |
| 3638 | *outFinishPreviousGesture = false; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3639 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3640 | AutoMutex _l(mLock); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3641 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3642 | // Handle TAP timeout. |
| 3643 | if (isTimeout) { |
| 3644 | #if DEBUG_GESTURES |
| 3645 | LOGD("Gestures: Processing timeout"); |
| 3646 | #endif |
| 3647 | |
| 3648 | if (mPointerGesture.lastGestureMode == PointerGesture::TAP) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 3649 | if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3650 | // The tap/drag timeout has not yet expired. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 3651 | getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 3652 | + mConfig.pointerGestureTapDragInterval); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3653 | } else { |
| 3654 | // The tap is finished. |
| 3655 | #if DEBUG_GESTURES |
| 3656 | LOGD("Gestures: TAP finished"); |
| 3657 | #endif |
| 3658 | *outFinishPreviousGesture = true; |
| 3659 | |
| 3660 | mPointerGesture.activeGestureId = -1; |
| 3661 | mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL; |
| 3662 | mPointerGesture.currentGestureIdBits.clear(); |
| 3663 | |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 3664 | mPointerGesture.pointerVelocityControl.reset(); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3665 | return true; |
| 3666 | } |
| 3667 | } |
| 3668 | |
| 3669 | // We did not handle this timeout. |
| 3670 | return false; |
| 3671 | } |
| 3672 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3673 | // Update the velocity tracker. |
| 3674 | { |
| 3675 | VelocityTracker::Position positions[MAX_POINTERS]; |
| 3676 | uint32_t count = 0; |
| 3677 | for (BitSet32 idBits(mCurrentTouch.idBits); !idBits.isEmpty(); count++) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3678 | uint32_t id = idBits.firstMarkedBit(); |
| 3679 | idBits.clearBit(id); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3680 | uint32_t index = mCurrentTouch.idToIndex[id]; |
| 3681 | positions[count].x = mCurrentTouch.pointers[index].x |
| 3682 | * mLocked.pointerGestureXMovementScale; |
| 3683 | positions[count].y = mCurrentTouch.pointers[index].y |
| 3684 | * mLocked.pointerGestureYMovementScale; |
| 3685 | } |
| 3686 | mPointerGesture.velocityTracker.addMovement(when, mCurrentTouch.idBits, positions); |
| 3687 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3688 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3689 | // Pick a new active touch id if needed. |
| 3690 | // Choose an arbitrary pointer that just went down, if there is one. |
| 3691 | // Otherwise choose an arbitrary remaining pointer. |
| 3692 | // 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] | 3693 | // We keep the same active touch id for as long as possible. |
| 3694 | bool activeTouchChanged = false; |
| 3695 | int32_t lastActiveTouchId = mPointerGesture.activeTouchId; |
| 3696 | int32_t activeTouchId = lastActiveTouchId; |
| 3697 | if (activeTouchId < 0) { |
| 3698 | if (!mCurrentTouch.idBits.isEmpty()) { |
| 3699 | activeTouchChanged = true; |
| 3700 | activeTouchId = mPointerGesture.activeTouchId = mCurrentTouch.idBits.firstMarkedBit(); |
| 3701 | mPointerGesture.firstTouchTime = when; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3702 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3703 | } else if (!mCurrentTouch.idBits.hasBit(activeTouchId)) { |
| 3704 | activeTouchChanged = true; |
| 3705 | if (!mCurrentTouch.idBits.isEmpty()) { |
| 3706 | activeTouchId = mPointerGesture.activeTouchId = mCurrentTouch.idBits.firstMarkedBit(); |
| 3707 | } else { |
| 3708 | activeTouchId = mPointerGesture.activeTouchId = -1; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3709 | } |
| 3710 | } |
| 3711 | |
| 3712 | // Determine whether we are in quiet time. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3713 | bool isQuietTime = false; |
| 3714 | if (activeTouchId < 0) { |
| 3715 | mPointerGesture.resetQuietTime(); |
| 3716 | } else { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 3717 | isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3718 | if (!isQuietTime) { |
| 3719 | if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS |
| 3720 | || mPointerGesture.lastGestureMode == PointerGesture::SWIPE |
| 3721 | || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM) |
| 3722 | && mCurrentTouch.pointerCount < 2) { |
| 3723 | // Enter quiet time when exiting swipe or freeform state. |
| 3724 | // This is to prevent accidentally entering the hover state and flinging the |
| 3725 | // pointer when finishing a swipe and there is still one pointer left onscreen. |
| 3726 | isQuietTime = true; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3727 | } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3728 | && mCurrentTouch.pointerCount >= 2 |
| 3729 | && !isPointerDown(mCurrentTouch.buttonState)) { |
| 3730 | // Enter quiet time when releasing the button and there are still two or more |
| 3731 | // fingers down. This may indicate that one finger was used to press the button |
| 3732 | // but it has not gone up yet. |
| 3733 | isQuietTime = true; |
| 3734 | } |
| 3735 | if (isQuietTime) { |
| 3736 | mPointerGesture.quietTime = when; |
| 3737 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3738 | } |
| 3739 | } |
| 3740 | |
| 3741 | // Switch states based on button and pointer state. |
| 3742 | if (isQuietTime) { |
| 3743 | // Case 1: Quiet time. (QUIET) |
| 3744 | #if DEBUG_GESTURES |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 3745 | LOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 3746 | + mConfig.pointerGestureQuietInterval - when) * 0.000001f); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3747 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 3748 | if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) { |
| 3749 | *outFinishPreviousGesture = true; |
| 3750 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3751 | |
| 3752 | mPointerGesture.activeGestureId = -1; |
| 3753 | mPointerGesture.currentGestureMode = PointerGesture::QUIET; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3754 | mPointerGesture.currentGestureIdBits.clear(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3755 | |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 3756 | mPointerGesture.pointerVelocityControl.reset(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3757 | } else if (isPointerDown(mCurrentTouch.buttonState)) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3758 | // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG) |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3759 | // The pointer follows the active touch point. |
| 3760 | // Emit DOWN, MOVE, UP events at the pointer location. |
| 3761 | // |
| 3762 | // Only the active touch matters; other fingers are ignored. This policy helps |
| 3763 | // to handle the case where the user places a second finger on the touch pad |
| 3764 | // to apply the necessary force to depress an integrated button below the surface. |
| 3765 | // We don't want the second finger to be delivered to applications. |
| 3766 | // |
| 3767 | // For this to work well, we need to make sure to track the pointer that is really |
| 3768 | // active. If the user first puts one finger down to click then adds another |
| 3769 | // finger to drag then the active pointer should switch to the finger that is |
| 3770 | // being dragged. |
| 3771 | #if DEBUG_GESTURES |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3772 | LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, " |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3773 | "currentTouchPointerCount=%d", activeTouchId, mCurrentTouch.pointerCount); |
| 3774 | #endif |
| 3775 | // Reset state when just starting. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3776 | if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3777 | *outFinishPreviousGesture = true; |
| 3778 | mPointerGesture.activeGestureId = 0; |
| 3779 | } |
| 3780 | |
| 3781 | // Switch pointers if needed. |
| 3782 | // Find the fastest pointer and follow it. |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 3783 | if (activeTouchId >= 0 && mCurrentTouch.pointerCount > 1) { |
| 3784 | int32_t bestId = -1; |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 3785 | float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed; |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 3786 | for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) { |
| 3787 | uint32_t id = mCurrentTouch.pointers[i].id; |
| 3788 | float vx, vy; |
| 3789 | if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) { |
| 3790 | float speed = hypotf(vx, vy); |
| 3791 | if (speed > bestSpeed) { |
| 3792 | bestId = id; |
| 3793 | bestSpeed = speed; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3794 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3795 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 3796 | } |
| 3797 | if (bestId >= 0 && bestId != activeTouchId) { |
| 3798 | mPointerGesture.activeTouchId = activeTouchId = bestId; |
| 3799 | activeTouchChanged = true; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3800 | #if DEBUG_GESTURES |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 3801 | LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, " |
| 3802 | "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3803 | #endif |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3804 | } |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 3805 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3806 | |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 3807 | if (activeTouchId >= 0 && mLastTouch.idBits.hasBit(activeTouchId)) { |
| 3808 | const PointerData& currentPointer = |
| 3809 | mCurrentTouch.pointers[mCurrentTouch.idToIndex[activeTouchId]]; |
| 3810 | const PointerData& lastPointer = |
| 3811 | mLastTouch.pointers[mLastTouch.idToIndex[activeTouchId]]; |
| 3812 | float deltaX = (currentPointer.x - lastPointer.x) |
| 3813 | * mLocked.pointerGestureXMovementScale; |
| 3814 | float deltaY = (currentPointer.y - lastPointer.y) |
| 3815 | * mLocked.pointerGestureYMovementScale; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3816 | |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 3817 | mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY); |
| 3818 | |
| 3819 | // Move the pointer using a relative motion. |
| 3820 | // When using spots, the click will occur at the position of the anchor |
| 3821 | // spot and all other spots will move there. |
| 3822 | mPointerController->move(deltaX, deltaY); |
| 3823 | } else { |
| 3824 | mPointerGesture.pointerVelocityControl.reset(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3825 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3826 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3827 | float x, y; |
| 3828 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 3829 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3830 | mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3831 | mPointerGesture.currentGestureIdBits.clear(); |
| 3832 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 3833 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3834 | mPointerGesture.currentGestureProperties[0].clear(); |
| 3835 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
| 3836 | mPointerGesture.currentGestureProperties[0].toolType = |
| 3837 | AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3838 | mPointerGesture.currentGestureCoords[0].clear(); |
| 3839 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 3840 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
| 3841 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
| 3842 | } else if (mCurrentTouch.pointerCount == 0) { |
| 3843 | // Case 3. No fingers down and button is not pressed. (NEUTRAL) |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 3844 | if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) { |
| 3845 | *outFinishPreviousGesture = true; |
| 3846 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3847 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3848 | // Watch for taps coming out of HOVER or TAP_DRAG mode. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 3849 | // Checking for taps after TAP_DRAG allows us to detect double-taps. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3850 | bool tapped = false; |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3851 | if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER |
| 3852 | || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3853 | && mLastTouch.pointerCount == 1) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 3854 | if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3855 | float x, y; |
| 3856 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 3857 | if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop |
| 3858 | && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3859 | #if DEBUG_GESTURES |
| 3860 | LOGD("Gestures: TAP"); |
| 3861 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3862 | |
| 3863 | mPointerGesture.tapUpTime = when; |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 3864 | getContext()->requestTimeoutAtTime(when |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 3865 | + mConfig.pointerGestureTapDragInterval); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3866 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3867 | mPointerGesture.activeGestureId = 0; |
| 3868 | mPointerGesture.currentGestureMode = PointerGesture::TAP; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3869 | mPointerGesture.currentGestureIdBits.clear(); |
| 3870 | mPointerGesture.currentGestureIdBits.markBit( |
| 3871 | mPointerGesture.activeGestureId); |
| 3872 | mPointerGesture.currentGestureIdToIndex[ |
| 3873 | mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3874 | mPointerGesture.currentGestureProperties[0].clear(); |
| 3875 | mPointerGesture.currentGestureProperties[0].id = |
| 3876 | mPointerGesture.activeGestureId; |
| 3877 | mPointerGesture.currentGestureProperties[0].toolType = |
| 3878 | AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3879 | mPointerGesture.currentGestureCoords[0].clear(); |
| 3880 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3881 | AMOTION_EVENT_AXIS_X, mPointerGesture.tapX); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3882 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3883 | AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3884 | mPointerGesture.currentGestureCoords[0].setAxisValue( |
| 3885 | AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3886 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3887 | tapped = true; |
| 3888 | } else { |
| 3889 | #if DEBUG_GESTURES |
| 3890 | LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f", |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3891 | x - mPointerGesture.tapX, |
| 3892 | y - mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3893 | #endif |
| 3894 | } |
| 3895 | } else { |
| 3896 | #if DEBUG_GESTURES |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3897 | LOGD("Gestures: Not a TAP, %0.3fms since down", |
| 3898 | (when - mPointerGesture.tapDownTime) * 0.000001f); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3899 | #endif |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3900 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3901 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3902 | |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 3903 | mPointerGesture.pointerVelocityControl.reset(); |
| 3904 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3905 | if (!tapped) { |
| 3906 | #if DEBUG_GESTURES |
| 3907 | LOGD("Gestures: NEUTRAL"); |
| 3908 | #endif |
| 3909 | mPointerGesture.activeGestureId = -1; |
| 3910 | mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3911 | mPointerGesture.currentGestureIdBits.clear(); |
| 3912 | } |
| 3913 | } else if (mCurrentTouch.pointerCount == 1) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3914 | // 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] | 3915 | // The pointer follows the active touch point. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3916 | // When in HOVER, emit HOVER_MOVE events at the pointer location. |
| 3917 | // When in TAP_DRAG, emit MOVE events at the pointer location. |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 3918 | LOG_ASSERT(activeTouchId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3919 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3920 | mPointerGesture.currentGestureMode = PointerGesture::HOVER; |
| 3921 | if (mPointerGesture.lastGestureMode == PointerGesture::TAP) { |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 3922 | if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3923 | float x, y; |
| 3924 | mPointerController->getPosition(&x, &y); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 3925 | if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop |
| 3926 | && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3927 | mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG; |
| 3928 | } else { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3929 | #if DEBUG_GESTURES |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3930 | LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f", |
| 3931 | x - mPointerGesture.tapX, |
| 3932 | y - mPointerGesture.tapY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3933 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3934 | } |
| 3935 | } else { |
| 3936 | #if DEBUG_GESTURES |
| 3937 | LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up", |
| 3938 | (when - mPointerGesture.tapUpTime) * 0.000001f); |
| 3939 | #endif |
| 3940 | } |
| 3941 | } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) { |
| 3942 | mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG; |
| 3943 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3944 | |
| 3945 | if (mLastTouch.idBits.hasBit(activeTouchId)) { |
| 3946 | const PointerData& currentPointer = |
| 3947 | mCurrentTouch.pointers[mCurrentTouch.idToIndex[activeTouchId]]; |
| 3948 | const PointerData& lastPointer = |
| 3949 | mLastTouch.pointers[mLastTouch.idToIndex[activeTouchId]]; |
| 3950 | float deltaX = (currentPointer.x - lastPointer.x) |
| 3951 | * mLocked.pointerGestureXMovementScale; |
| 3952 | float deltaY = (currentPointer.y - lastPointer.y) |
| 3953 | * mLocked.pointerGestureYMovementScale; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3954 | |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 3955 | mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY); |
| 3956 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 3957 | // Move the pointer using a relative motion. |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3958 | // 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] | 3959 | mPointerController->move(deltaX, deltaY); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 3960 | } else { |
| 3961 | mPointerGesture.pointerVelocityControl.reset(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3962 | } |
| 3963 | |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3964 | bool down; |
| 3965 | if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) { |
| 3966 | #if DEBUG_GESTURES |
| 3967 | LOGD("Gestures: TAP_DRAG"); |
| 3968 | #endif |
| 3969 | down = true; |
| 3970 | } else { |
| 3971 | #if DEBUG_GESTURES |
| 3972 | LOGD("Gestures: HOVER"); |
| 3973 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 3974 | if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) { |
| 3975 | *outFinishPreviousGesture = true; |
| 3976 | } |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3977 | mPointerGesture.activeGestureId = 0; |
| 3978 | down = false; |
| 3979 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3980 | |
| 3981 | float x, y; |
| 3982 | mPointerController->getPosition(&x, &y); |
| 3983 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3984 | mPointerGesture.currentGestureIdBits.clear(); |
| 3985 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 3986 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3987 | mPointerGesture.currentGestureProperties[0].clear(); |
| 3988 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
| 3989 | mPointerGesture.currentGestureProperties[0].toolType = |
| 3990 | AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3991 | mPointerGesture.currentGestureCoords[0].clear(); |
| 3992 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x); |
| 3993 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y); |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3994 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, |
| 3995 | down ? 1.0f : 0.0f); |
| 3996 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 3997 | if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) { |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 3998 | mPointerGesture.resetTap(); |
| 3999 | mPointerGesture.tapDownTime = when; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4000 | mPointerGesture.tapX = x; |
| 4001 | mPointerGesture.tapY = y; |
| 4002 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4003 | } else { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4004 | // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM) |
| 4005 | // We need to provide feedback for each finger that goes down so we cannot wait |
| 4006 | // for the fingers to move before deciding what to do. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4007 | // |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4008 | // The ambiguous case is deciding what to do when there are two fingers down but they |
| 4009 | // have not moved enough to determine whether they are part of a drag or part of a |
| 4010 | // freeform gesture, or just a press or long-press at the pointer location. |
| 4011 | // |
| 4012 | // When there are two fingers we start with the PRESS hypothesis and we generate a |
| 4013 | // down at the pointer location. |
| 4014 | // |
| 4015 | // When the two fingers move enough or when additional fingers are added, we make |
| 4016 | // a decision to transition into SWIPE or FREEFORM mode accordingly. |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 4017 | LOG_ASSERT(activeTouchId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4018 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4019 | bool settled = when >= mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4020 | + mConfig.pointerGestureMultitouchSettleInterval; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4021 | if (mPointerGesture.lastGestureMode != PointerGesture::PRESS |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4022 | && mPointerGesture.lastGestureMode != PointerGesture::SWIPE |
| 4023 | && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4024 | *outFinishPreviousGesture = true; |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4025 | } else if (!settled && mCurrentTouch.pointerCount > mLastTouch.pointerCount) { |
| 4026 | // Additional pointers have gone down but not yet settled. |
| 4027 | // Reset the gesture. |
| 4028 | #if DEBUG_GESTURES |
| 4029 | LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, " |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4030 | "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4031 | + mConfig.pointerGestureMultitouchSettleInterval - when) |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4032 | * 0.000001f); |
| 4033 | #endif |
| 4034 | *outCancelPreviousGesture = true; |
| 4035 | } else { |
| 4036 | // Continue previous gesture. |
| 4037 | mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode; |
| 4038 | } |
| 4039 | |
| 4040 | if (*outFinishPreviousGesture || *outCancelPreviousGesture) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4041 | mPointerGesture.currentGestureMode = PointerGesture::PRESS; |
| 4042 | mPointerGesture.activeGestureId = 0; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4043 | mPointerGesture.referenceIdBits.clear(); |
Jeff Brown | 19c97d46 | 2011-06-01 12:33:19 -0700 | [diff] [blame] | 4044 | mPointerGesture.pointerVelocityControl.reset(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4045 | |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4046 | // 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] | 4047 | #if DEBUG_GESTURES |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4048 | LOGD("Gestures: Using centroid as reference for MULTITOUCH, " |
| 4049 | "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4050 | + mConfig.pointerGestureMultitouchSettleInterval - when) |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4051 | * 0.000001f); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4052 | #endif |
Jeff Brown | cb5ffcf | 2011-06-06 20:03:18 -0700 | [diff] [blame] | 4053 | mCurrentTouch.getCentroid(&mPointerGesture.referenceTouchX, |
| 4054 | &mPointerGesture.referenceTouchY); |
| 4055 | mPointerController->getPosition(&mPointerGesture.referenceGestureX, |
| 4056 | &mPointerGesture.referenceGestureY); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4057 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4058 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4059 | // Clear the reference deltas for fingers not yet included in the reference calculation. |
| 4060 | for (BitSet32 idBits(mCurrentTouch.idBits.value & ~mPointerGesture.referenceIdBits.value); |
| 4061 | !idBits.isEmpty(); ) { |
| 4062 | uint32_t id = idBits.firstMarkedBit(); |
| 4063 | idBits.clearBit(id); |
| 4064 | |
| 4065 | mPointerGesture.referenceDeltas[id].dx = 0; |
| 4066 | mPointerGesture.referenceDeltas[id].dy = 0; |
| 4067 | } |
| 4068 | mPointerGesture.referenceIdBits = mCurrentTouch.idBits; |
| 4069 | |
| 4070 | // Add delta for all fingers and calculate a common movement delta. |
| 4071 | float commonDeltaX = 0, commonDeltaY = 0; |
| 4072 | BitSet32 commonIdBits(mLastTouch.idBits.value & mCurrentTouch.idBits.value); |
| 4073 | for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) { |
| 4074 | bool first = (idBits == commonIdBits); |
| 4075 | uint32_t id = idBits.firstMarkedBit(); |
| 4076 | idBits.clearBit(id); |
| 4077 | |
| 4078 | const PointerData& cpd = mCurrentTouch.pointers[mCurrentTouch.idToIndex[id]]; |
| 4079 | const PointerData& lpd = mLastTouch.pointers[mLastTouch.idToIndex[id]]; |
| 4080 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
| 4081 | delta.dx += cpd.x - lpd.x; |
| 4082 | delta.dy += cpd.y - lpd.y; |
| 4083 | |
| 4084 | if (first) { |
| 4085 | commonDeltaX = delta.dx; |
| 4086 | commonDeltaY = delta.dy; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4087 | } else { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4088 | commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx); |
| 4089 | commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy); |
| 4090 | } |
| 4091 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4092 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4093 | // Consider transitions from PRESS to SWIPE or MULTITOUCH. |
| 4094 | if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) { |
| 4095 | float dist[MAX_POINTER_ID + 1]; |
| 4096 | int32_t distOverThreshold = 0; |
| 4097 | for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) { |
| 4098 | uint32_t id = idBits.firstMarkedBit(); |
| 4099 | idBits.clearBit(id); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4100 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4101 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
| 4102 | dist[id] = hypotf(delta.dx * mLocked.pointerGestureXZoomScale, |
| 4103 | delta.dy * mLocked.pointerGestureYZoomScale); |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4104 | if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4105 | distOverThreshold += 1; |
| 4106 | } |
| 4107 | } |
| 4108 | |
| 4109 | // Only transition when at least two pointers have moved further than |
| 4110 | // the minimum distance threshold. |
| 4111 | if (distOverThreshold >= 2) { |
| 4112 | float d; |
| 4113 | if (mCurrentTouch.pointerCount > 2) { |
| 4114 | // There are more than two pointers, switch to FREEFORM. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4115 | #if DEBUG_GESTURES |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4116 | LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2", |
| 4117 | mCurrentTouch.pointerCount); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4118 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4119 | *outCancelPreviousGesture = true; |
| 4120 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 4121 | } else if (((d = distance( |
| 4122 | mCurrentTouch.pointers[0].x, mCurrentTouch.pointers[0].y, |
| 4123 | mCurrentTouch.pointers[1].x, mCurrentTouch.pointers[1].y)) |
| 4124 | > mLocked.pointerGestureMaxSwipeWidth)) { |
| 4125 | // There are two pointers but they are too far apart for a SWIPE, |
| 4126 | // switch to FREEFORM. |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4127 | #if DEBUG_GESTURES |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4128 | LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f", |
| 4129 | d, mLocked.pointerGestureMaxSwipeWidth); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4130 | #endif |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4131 | *outCancelPreviousGesture = true; |
| 4132 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 4133 | } else { |
| 4134 | // There are two pointers. Wait for both pointers to start moving |
| 4135 | // before deciding whether this is a SWIPE or FREEFORM gesture. |
| 4136 | uint32_t id1 = mCurrentTouch.pointers[0].id; |
| 4137 | uint32_t id2 = mCurrentTouch.pointers[1].id; |
| 4138 | float dist1 = dist[id1]; |
| 4139 | float dist2 = dist[id2]; |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4140 | if (dist1 >= mConfig.pointerGestureMultitouchMinDistance |
| 4141 | && dist2 >= mConfig.pointerGestureMultitouchMinDistance) { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4142 | // Calculate the dot product of the displacement vectors. |
| 4143 | // When the vectors are oriented in approximately the same direction, |
| 4144 | // the angle betweeen them is near zero and the cosine of the angle |
| 4145 | // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2). |
| 4146 | PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1]; |
| 4147 | PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2]; |
Jeff Brown | 6674d9b | 2011-06-07 16:50:14 -0700 | [diff] [blame] | 4148 | float dx1 = delta1.dx * mLocked.pointerGestureXZoomScale; |
| 4149 | float dy1 = delta1.dy * mLocked.pointerGestureYZoomScale; |
| 4150 | float dx2 = delta2.dx * mLocked.pointerGestureXZoomScale; |
| 4151 | float dy2 = delta2.dy * mLocked.pointerGestureYZoomScale; |
| 4152 | float dot = dx1 * dx2 + dy1 * dy2; |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4153 | float cosine = dot / (dist1 * dist2); // denominator always > 0 |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4154 | if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) { |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4155 | // Pointers are moving in the same direction. Switch to SWIPE. |
| 4156 | #if DEBUG_GESTURES |
| 4157 | LOGD("Gestures: PRESS transitioned to SWIPE, " |
| 4158 | "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, " |
| 4159 | "cosine %0.3f >= %0.3f", |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4160 | dist1, mConfig.pointerGestureMultitouchMinDistance, |
| 4161 | dist2, mConfig.pointerGestureMultitouchMinDistance, |
| 4162 | cosine, mConfig.pointerGestureSwipeTransitionAngleCosine); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4163 | #endif |
| 4164 | mPointerGesture.currentGestureMode = PointerGesture::SWIPE; |
| 4165 | } else { |
| 4166 | // Pointers are moving in different directions. Switch to FREEFORM. |
| 4167 | #if DEBUG_GESTURES |
| 4168 | LOGD("Gestures: PRESS transitioned to FREEFORM, " |
| 4169 | "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, " |
| 4170 | "cosine %0.3f < %0.3f", |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 4171 | dist1, mConfig.pointerGestureMultitouchMinDistance, |
| 4172 | dist2, mConfig.pointerGestureMultitouchMinDistance, |
| 4173 | cosine, mConfig.pointerGestureSwipeTransitionAngleCosine); |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4174 | #endif |
| 4175 | *outCancelPreviousGesture = true; |
| 4176 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
| 4177 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4178 | } |
| 4179 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4180 | } |
| 4181 | } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4182 | // Switch from SWIPE to FREEFORM if additional pointers go down. |
| 4183 | // Cancel previous gesture. |
| 4184 | if (mCurrentTouch.pointerCount > 2) { |
| 4185 | #if DEBUG_GESTURES |
| 4186 | LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2", |
| 4187 | mCurrentTouch.pointerCount); |
| 4188 | #endif |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4189 | *outCancelPreviousGesture = true; |
| 4190 | mPointerGesture.currentGestureMode = PointerGesture::FREEFORM; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4191 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4192 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4193 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4194 | // Move the reference points based on the overall group motion of the fingers |
| 4195 | // except in PRESS mode while waiting for a transition to occur. |
| 4196 | if (mPointerGesture.currentGestureMode != PointerGesture::PRESS |
| 4197 | && (commonDeltaX || commonDeltaY)) { |
| 4198 | for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4199 | uint32_t id = idBits.firstMarkedBit(); |
| 4200 | idBits.clearBit(id); |
| 4201 | |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4202 | PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id]; |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4203 | delta.dx = 0; |
| 4204 | delta.dy = 0; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4205 | } |
| 4206 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4207 | mPointerGesture.referenceTouchX += commonDeltaX; |
| 4208 | mPointerGesture.referenceTouchY += commonDeltaY; |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4209 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4210 | commonDeltaX *= mLocked.pointerGestureXMovementScale; |
| 4211 | commonDeltaY *= mLocked.pointerGestureYMovementScale; |
| 4212 | mPointerGesture.pointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY); |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4213 | |
Jeff Brown | bb3fcba0c | 2011-06-06 19:23:05 -0700 | [diff] [blame] | 4214 | mPointerGesture.referenceGestureX += commonDeltaX; |
| 4215 | mPointerGesture.referenceGestureY += commonDeltaY; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4216 | } |
| 4217 | |
| 4218 | // Report gestures. |
| 4219 | if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) { |
| 4220 | // PRESS mode. |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4221 | #if DEBUG_GESTURES |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4222 | LOGD("Gestures: PRESS activeTouchId=%d," |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4223 | "activeGestureId=%d, currentTouchPointerCount=%d", |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4224 | activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4225 | #endif |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 4226 | LOG_ASSERT(mPointerGesture.activeGestureId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4227 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4228 | mPointerGesture.currentGestureIdBits.clear(); |
| 4229 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4230 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4231 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4232 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
| 4233 | mPointerGesture.currentGestureProperties[0].toolType = |
| 4234 | AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4235 | mPointerGesture.currentGestureCoords[0].clear(); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4236 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 4237 | mPointerGesture.referenceGestureX); |
| 4238 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, |
| 4239 | mPointerGesture.referenceGestureY); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4240 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4241 | } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) { |
| 4242 | // SWIPE mode. |
| 4243 | #if DEBUG_GESTURES |
| 4244 | LOGD("Gestures: SWIPE activeTouchId=%d," |
| 4245 | "activeGestureId=%d, currentTouchPointerCount=%d", |
| 4246 | activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount); |
| 4247 | #endif |
| 4248 | LOG_ASSERT(mPointerGesture.activeGestureId >= 0); |
| 4249 | |
| 4250 | mPointerGesture.currentGestureIdBits.clear(); |
| 4251 | mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4252 | mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4253 | mPointerGesture.currentGestureProperties[0].clear(); |
| 4254 | mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId; |
| 4255 | mPointerGesture.currentGestureProperties[0].toolType = |
| 4256 | AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4257 | mPointerGesture.currentGestureCoords[0].clear(); |
| 4258 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, |
| 4259 | mPointerGesture.referenceGestureX); |
| 4260 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, |
| 4261 | mPointerGesture.referenceGestureY); |
| 4262 | mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4263 | } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) { |
| 4264 | // FREEFORM mode. |
| 4265 | #if DEBUG_GESTURES |
| 4266 | LOGD("Gestures: FREEFORM activeTouchId=%d," |
| 4267 | "activeGestureId=%d, currentTouchPointerCount=%d", |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4268 | activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4269 | #endif |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 4270 | LOG_ASSERT(mPointerGesture.activeGestureId >= 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4271 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4272 | mPointerGesture.currentGestureIdBits.clear(); |
| 4273 | |
| 4274 | BitSet32 mappedTouchIdBits; |
| 4275 | BitSet32 usedGestureIdBits; |
| 4276 | if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) { |
| 4277 | // Initially, assign the active gesture id to the active touch point |
| 4278 | // if there is one. No other touch id bits are mapped yet. |
| 4279 | if (!*outCancelPreviousGesture) { |
| 4280 | mappedTouchIdBits.markBit(activeTouchId); |
| 4281 | usedGestureIdBits.markBit(mPointerGesture.activeGestureId); |
| 4282 | mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] = |
| 4283 | mPointerGesture.activeGestureId; |
| 4284 | } else { |
| 4285 | mPointerGesture.activeGestureId = -1; |
| 4286 | } |
| 4287 | } else { |
| 4288 | // Otherwise, assume we mapped all touches from the previous frame. |
| 4289 | // Reuse all mappings that are still applicable. |
| 4290 | mappedTouchIdBits.value = mLastTouch.idBits.value & mCurrentTouch.idBits.value; |
| 4291 | usedGestureIdBits = mPointerGesture.lastGestureIdBits; |
| 4292 | |
| 4293 | // Check whether we need to choose a new active gesture id because the |
| 4294 | // current went went up. |
| 4295 | for (BitSet32 upTouchIdBits(mLastTouch.idBits.value & ~mCurrentTouch.idBits.value); |
| 4296 | !upTouchIdBits.isEmpty(); ) { |
| 4297 | uint32_t upTouchId = upTouchIdBits.firstMarkedBit(); |
| 4298 | upTouchIdBits.clearBit(upTouchId); |
| 4299 | uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId]; |
| 4300 | if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) { |
| 4301 | mPointerGesture.activeGestureId = -1; |
| 4302 | break; |
| 4303 | } |
| 4304 | } |
| 4305 | } |
| 4306 | |
| 4307 | #if DEBUG_GESTURES |
| 4308 | LOGD("Gestures: FREEFORM follow up " |
| 4309 | "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, " |
| 4310 | "activeGestureId=%d", |
| 4311 | mappedTouchIdBits.value, usedGestureIdBits.value, |
| 4312 | mPointerGesture.activeGestureId); |
| 4313 | #endif |
| 4314 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4315 | for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4316 | uint32_t touchId = mCurrentTouch.pointers[i].id; |
| 4317 | uint32_t gestureId; |
| 4318 | if (!mappedTouchIdBits.hasBit(touchId)) { |
| 4319 | gestureId = usedGestureIdBits.firstUnmarkedBit(); |
| 4320 | usedGestureIdBits.markBit(gestureId); |
| 4321 | mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId; |
| 4322 | #if DEBUG_GESTURES |
| 4323 | LOGD("Gestures: FREEFORM " |
| 4324 | "new mapping for touch id %d -> gesture id %d", |
| 4325 | touchId, gestureId); |
| 4326 | #endif |
| 4327 | } else { |
| 4328 | gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId]; |
| 4329 | #if DEBUG_GESTURES |
| 4330 | LOGD("Gestures: FREEFORM " |
| 4331 | "existing mapping for touch id %d -> gesture id %d", |
| 4332 | touchId, gestureId); |
| 4333 | #endif |
| 4334 | } |
| 4335 | mPointerGesture.currentGestureIdBits.markBit(gestureId); |
| 4336 | mPointerGesture.currentGestureIdToIndex[gestureId] = i; |
| 4337 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4338 | float x = (mCurrentTouch.pointers[i].x - mPointerGesture.referenceTouchX) |
| 4339 | * mLocked.pointerGestureXZoomScale + mPointerGesture.referenceGestureX; |
| 4340 | float y = (mCurrentTouch.pointers[i].y - mPointerGesture.referenceTouchY) |
| 4341 | * mLocked.pointerGestureYZoomScale + mPointerGesture.referenceGestureY; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4342 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4343 | mPointerGesture.currentGestureProperties[i].clear(); |
| 4344 | mPointerGesture.currentGestureProperties[i].id = gestureId; |
| 4345 | mPointerGesture.currentGestureProperties[i].toolType = |
| 4346 | AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4347 | mPointerGesture.currentGestureCoords[i].clear(); |
| 4348 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
| 4349 | AMOTION_EVENT_AXIS_X, x); |
| 4350 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
| 4351 | AMOTION_EVENT_AXIS_Y, y); |
| 4352 | mPointerGesture.currentGestureCoords[i].setAxisValue( |
| 4353 | AMOTION_EVENT_AXIS_PRESSURE, 1.0f); |
| 4354 | } |
| 4355 | |
| 4356 | if (mPointerGesture.activeGestureId < 0) { |
| 4357 | mPointerGesture.activeGestureId = |
| 4358 | mPointerGesture.currentGestureIdBits.firstMarkedBit(); |
| 4359 | #if DEBUG_GESTURES |
| 4360 | LOGD("Gestures: FREEFORM new " |
| 4361 | "activeGestureId=%d", mPointerGesture.activeGestureId); |
| 4362 | #endif |
| 4363 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4364 | } |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4365 | } |
| 4366 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4367 | mPointerController->setButtonState(mCurrentTouch.buttonState); |
| 4368 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4369 | #if DEBUG_GESTURES |
| 4370 | LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, " |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4371 | "currentGestureMode=%d, currentGestureIdBits=0x%08x, " |
| 4372 | "lastGestureMode=%d, lastGestureIdBits=0x%08x", |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4373 | toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture), |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 4374 | mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value, |
| 4375 | mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4376 | for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) { |
| 4377 | uint32_t id = idBits.firstMarkedBit(); |
| 4378 | idBits.clearBit(id); |
| 4379 | uint32_t index = mPointerGesture.currentGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4380 | const PointerProperties& properties = mPointerGesture.currentGestureProperties[index]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4381 | const PointerCoords& coords = mPointerGesture.currentGestureCoords[index]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4382 | LOGD(" currentGesture[%d]: index=%d, toolType=%d, " |
| 4383 | "x=%0.3f, y=%0.3f, pressure=%0.3f", |
| 4384 | id, index, properties.toolType, |
| 4385 | coords.getAxisValue(AMOTION_EVENT_AXIS_X), |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4386 | coords.getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4387 | coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); |
| 4388 | } |
| 4389 | for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) { |
| 4390 | uint32_t id = idBits.firstMarkedBit(); |
| 4391 | idBits.clearBit(id); |
| 4392 | uint32_t index = mPointerGesture.lastGestureIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4393 | const PointerProperties& properties = mPointerGesture.lastGestureProperties[index]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4394 | const PointerCoords& coords = mPointerGesture.lastGestureCoords[index]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4395 | LOGD(" lastGesture[%d]: index=%d, toolType=%d, " |
| 4396 | "x=%0.3f, y=%0.3f, pressure=%0.3f", |
| 4397 | id, index, properties.toolType, |
| 4398 | coords.getAxisValue(AMOTION_EVENT_AXIS_X), |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4399 | coords.getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 4400 | coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE)); |
| 4401 | } |
| 4402 | #endif |
Jeff Brown | 79ac969 | 2011-04-19 21:20:10 -0700 | [diff] [blame] | 4403 | return true; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4407 | int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags, |
| 4408 | const PointerProperties* properties, const PointerCoords* coords, |
| 4409 | const uint32_t* idToIndex, BitSet32 idBits, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4410 | int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) { |
| 4411 | PointerCoords pointerCoords[MAX_POINTERS]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4412 | PointerProperties pointerProperties[MAX_POINTERS]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4413 | uint32_t pointerCount = 0; |
| 4414 | while (!idBits.isEmpty()) { |
| 4415 | uint32_t id = idBits.firstMarkedBit(); |
| 4416 | idBits.clearBit(id); |
| 4417 | uint32_t index = idToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4418 | pointerProperties[pointerCount].copyFrom(properties[index]); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4419 | pointerCoords[pointerCount].copyFrom(coords[index]); |
| 4420 | |
| 4421 | if (changedId >= 0 && id == uint32_t(changedId)) { |
| 4422 | action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
| 4423 | } |
| 4424 | |
| 4425 | pointerCount += 1; |
| 4426 | } |
| 4427 | |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 4428 | LOG_ASSERT(pointerCount != 0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4429 | |
| 4430 | if (changedId >= 0 && pointerCount == 1) { |
| 4431 | // Replace initial down and final up action. |
| 4432 | // We can compare the action without masking off the changed pointer index |
| 4433 | // because we know the index is 0. |
| 4434 | if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 4435 | action = AMOTION_EVENT_ACTION_DOWN; |
| 4436 | } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 4437 | action = AMOTION_EVENT_ACTION_UP; |
| 4438 | } else { |
| 4439 | // Can't happen. |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 4440 | LOG_ASSERT(false); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4441 | } |
| 4442 | } |
| 4443 | |
| 4444 | getDispatcher()->notifyMotion(when, getDeviceId(), source, policyFlags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4445 | action, flags, metaState, buttonState, edgeFlags, |
| 4446 | pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4447 | } |
| 4448 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4449 | bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties, |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4450 | const PointerCoords* inCoords, const uint32_t* inIdToIndex, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4451 | PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex, |
| 4452 | BitSet32 idBits) const { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4453 | bool changed = false; |
| 4454 | while (!idBits.isEmpty()) { |
| 4455 | uint32_t id = idBits.firstMarkedBit(); |
| 4456 | idBits.clearBit(id); |
| 4457 | |
| 4458 | uint32_t inIndex = inIdToIndex[id]; |
| 4459 | uint32_t outIndex = outIdToIndex[id]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4460 | |
| 4461 | const PointerProperties& curInProperties = inProperties[inIndex]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4462 | const PointerCoords& curInCoords = inCoords[inIndex]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4463 | PointerProperties& curOutProperties = outProperties[outIndex]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4464 | PointerCoords& curOutCoords = outCoords[outIndex]; |
| 4465 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4466 | if (curInProperties != curOutProperties) { |
| 4467 | curOutProperties.copyFrom(curInProperties); |
| 4468 | changed = true; |
| 4469 | } |
| 4470 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4471 | if (curInCoords != curOutCoords) { |
| 4472 | curOutCoords.copyFrom(curInCoords); |
| 4473 | changed = true; |
| 4474 | } |
| 4475 | } |
| 4476 | return changed; |
| 4477 | } |
| 4478 | |
| 4479 | void TouchInputMapper::fadePointer() { |
| 4480 | { // acquire lock |
| 4481 | AutoMutex _l(mLock); |
| 4482 | if (mPointerController != NULL) { |
Jeff Brown | 538881e | 2011-05-25 18:23:38 -0700 | [diff] [blame] | 4483 | mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4484 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4485 | } // release lock |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4486 | } |
| 4487 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4488 | int32_t TouchInputMapper::getTouchToolType(bool isStylus) const { |
| 4489 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) { |
| 4490 | return isStylus ? AMOTION_EVENT_TOOL_TYPE_STYLUS : AMOTION_EVENT_TOOL_TYPE_FINGER; |
| 4491 | } else { |
| 4492 | return isStylus ? AMOTION_EVENT_TOOL_TYPE_INDIRECT_STYLUS |
| 4493 | : AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER; |
| 4494 | } |
| 4495 | } |
| 4496 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4497 | bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) { |
Jeff Brown | 9626b14 | 2011-03-03 02:09:54 -0800 | [diff] [blame] | 4498 | return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue |
| 4499 | && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4500 | } |
| 4501 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4502 | const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked( |
| 4503 | int32_t x, int32_t y) { |
| 4504 | size_t numVirtualKeys = mLocked.virtualKeys.size(); |
| 4505 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 4506 | const VirtualKey& virtualKey = mLocked.virtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4507 | |
| 4508 | #if DEBUG_VIRTUAL_KEYS |
| 4509 | LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, " |
| 4510 | "left=%d, top=%d, right=%d, bottom=%d", |
| 4511 | x, y, |
| 4512 | virtualKey.keyCode, virtualKey.scanCode, |
| 4513 | virtualKey.hitLeft, virtualKey.hitTop, |
| 4514 | virtualKey.hitRight, virtualKey.hitBottom); |
| 4515 | #endif |
| 4516 | |
| 4517 | if (virtualKey.isHit(x, y)) { |
| 4518 | return & virtualKey; |
| 4519 | } |
| 4520 | } |
| 4521 | |
| 4522 | return NULL; |
| 4523 | } |
| 4524 | |
| 4525 | void TouchInputMapper::calculatePointerIds() { |
| 4526 | uint32_t currentPointerCount = mCurrentTouch.pointerCount; |
| 4527 | uint32_t lastPointerCount = mLastTouch.pointerCount; |
| 4528 | |
| 4529 | if (currentPointerCount == 0) { |
| 4530 | // No pointers to assign. |
| 4531 | mCurrentTouch.idBits.clear(); |
| 4532 | } else if (lastPointerCount == 0) { |
| 4533 | // All pointers are new. |
| 4534 | mCurrentTouch.idBits.clear(); |
| 4535 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
| 4536 | mCurrentTouch.pointers[i].id = i; |
| 4537 | mCurrentTouch.idToIndex[i] = i; |
| 4538 | mCurrentTouch.idBits.markBit(i); |
| 4539 | } |
| 4540 | } else if (currentPointerCount == 1 && lastPointerCount == 1) { |
| 4541 | // Only one pointer and no change in count so it must have the same id as before. |
| 4542 | uint32_t id = mLastTouch.pointers[0].id; |
| 4543 | mCurrentTouch.pointers[0].id = id; |
| 4544 | mCurrentTouch.idToIndex[id] = 0; |
| 4545 | mCurrentTouch.idBits.value = BitSet32::valueForBit(id); |
| 4546 | } else { |
| 4547 | // General case. |
| 4548 | // We build a heap of squared euclidean distances between current and last pointers |
| 4549 | // associated with the current and last pointer indices. Then, we find the best |
| 4550 | // match (by distance) for each current pointer. |
| 4551 | PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS]; |
| 4552 | |
| 4553 | uint32_t heapSize = 0; |
| 4554 | for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount; |
| 4555 | currentPointerIndex++) { |
| 4556 | for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount; |
| 4557 | lastPointerIndex++) { |
| 4558 | int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x |
| 4559 | - mLastTouch.pointers[lastPointerIndex].x; |
| 4560 | int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y |
| 4561 | - mLastTouch.pointers[lastPointerIndex].y; |
| 4562 | |
| 4563 | uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY); |
| 4564 | |
| 4565 | // Insert new element into the heap (sift up). |
| 4566 | heap[heapSize].currentPointerIndex = currentPointerIndex; |
| 4567 | heap[heapSize].lastPointerIndex = lastPointerIndex; |
| 4568 | heap[heapSize].distance = distance; |
| 4569 | heapSize += 1; |
| 4570 | } |
| 4571 | } |
| 4572 | |
| 4573 | // Heapify |
| 4574 | for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) { |
| 4575 | startIndex -= 1; |
| 4576 | for (uint32_t parentIndex = startIndex; ;) { |
| 4577 | uint32_t childIndex = parentIndex * 2 + 1; |
| 4578 | if (childIndex >= heapSize) { |
| 4579 | break; |
| 4580 | } |
| 4581 | |
| 4582 | if (childIndex + 1 < heapSize |
| 4583 | && heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 4584 | childIndex += 1; |
| 4585 | } |
| 4586 | |
| 4587 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 4588 | break; |
| 4589 | } |
| 4590 | |
| 4591 | swap(heap[parentIndex], heap[childIndex]); |
| 4592 | parentIndex = childIndex; |
| 4593 | } |
| 4594 | } |
| 4595 | |
| 4596 | #if DEBUG_POINTER_ASSIGNMENT |
| 4597 | LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize); |
| 4598 | for (size_t i = 0; i < heapSize; i++) { |
| 4599 | LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", |
| 4600 | i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, |
| 4601 | heap[i].distance); |
| 4602 | } |
| 4603 | #endif |
| 4604 | |
| 4605 | // Pull matches out by increasing order of distance. |
| 4606 | // To avoid reassigning pointers that have already been matched, the loop keeps track |
| 4607 | // of which last and current pointers have been matched using the matchedXXXBits variables. |
| 4608 | // It also tracks the used pointer id bits. |
| 4609 | BitSet32 matchedLastBits(0); |
| 4610 | BitSet32 matchedCurrentBits(0); |
| 4611 | BitSet32 usedIdBits(0); |
| 4612 | bool first = true; |
| 4613 | for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) { |
| 4614 | for (;;) { |
| 4615 | if (first) { |
| 4616 | // The first time through the loop, we just consume the root element of |
| 4617 | // the heap (the one with smallest distance). |
| 4618 | first = false; |
| 4619 | } else { |
| 4620 | // Previous iterations consumed the root element of the heap. |
| 4621 | // Pop root element off of the heap (sift down). |
| 4622 | heapSize -= 1; |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 4623 | LOG_ASSERT(heapSize > 0); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4624 | |
| 4625 | // Sift down. |
| 4626 | heap[0] = heap[heapSize]; |
| 4627 | for (uint32_t parentIndex = 0; ;) { |
| 4628 | uint32_t childIndex = parentIndex * 2 + 1; |
| 4629 | if (childIndex >= heapSize) { |
| 4630 | break; |
| 4631 | } |
| 4632 | |
| 4633 | if (childIndex + 1 < heapSize |
| 4634 | && heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 4635 | childIndex += 1; |
| 4636 | } |
| 4637 | |
| 4638 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 4639 | break; |
| 4640 | } |
| 4641 | |
| 4642 | swap(heap[parentIndex], heap[childIndex]); |
| 4643 | parentIndex = childIndex; |
| 4644 | } |
| 4645 | |
| 4646 | #if DEBUG_POINTER_ASSIGNMENT |
| 4647 | LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize); |
| 4648 | for (size_t i = 0; i < heapSize; i++) { |
| 4649 | LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", |
| 4650 | i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, |
| 4651 | heap[i].distance); |
| 4652 | } |
| 4653 | #endif |
| 4654 | } |
| 4655 | |
| 4656 | uint32_t currentPointerIndex = heap[0].currentPointerIndex; |
| 4657 | if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched |
| 4658 | |
| 4659 | uint32_t lastPointerIndex = heap[0].lastPointerIndex; |
| 4660 | if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched |
| 4661 | |
| 4662 | matchedCurrentBits.markBit(currentPointerIndex); |
| 4663 | matchedLastBits.markBit(lastPointerIndex); |
| 4664 | |
| 4665 | uint32_t id = mLastTouch.pointers[lastPointerIndex].id; |
| 4666 | mCurrentTouch.pointers[currentPointerIndex].id = id; |
| 4667 | mCurrentTouch.idToIndex[id] = currentPointerIndex; |
| 4668 | usedIdBits.markBit(id); |
| 4669 | |
| 4670 | #if DEBUG_POINTER_ASSIGNMENT |
| 4671 | LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld", |
| 4672 | lastPointerIndex, currentPointerIndex, id, heap[0].distance); |
| 4673 | #endif |
| 4674 | break; |
| 4675 | } |
| 4676 | } |
| 4677 | |
| 4678 | // Assign fresh ids to new pointers. |
| 4679 | if (currentPointerCount > lastPointerCount) { |
| 4680 | for (uint32_t i = currentPointerCount - lastPointerCount; ;) { |
| 4681 | uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit(); |
| 4682 | uint32_t id = usedIdBits.firstUnmarkedBit(); |
| 4683 | |
| 4684 | mCurrentTouch.pointers[currentPointerIndex].id = id; |
| 4685 | mCurrentTouch.idToIndex[id] = currentPointerIndex; |
| 4686 | usedIdBits.markBit(id); |
| 4687 | |
| 4688 | #if DEBUG_POINTER_ASSIGNMENT |
| 4689 | LOGD("calculatePointerIds - assigned: cur=%d, id=%d", |
| 4690 | currentPointerIndex, id); |
| 4691 | #endif |
| 4692 | |
| 4693 | if (--i == 0) break; // done |
| 4694 | matchedCurrentBits.markBit(currentPointerIndex); |
| 4695 | } |
| 4696 | } |
| 4697 | |
| 4698 | // Fix id bits. |
| 4699 | mCurrentTouch.idBits = usedIdBits; |
| 4700 | } |
| 4701 | } |
| 4702 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4703 | int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4704 | { // acquire lock |
| 4705 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4706 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4707 | if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4708 | return AKEY_STATE_VIRTUAL; |
| 4709 | } |
| 4710 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4711 | size_t numVirtualKeys = mLocked.virtualKeys.size(); |
| 4712 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 4713 | const VirtualKey& virtualKey = mLocked.virtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4714 | if (virtualKey.keyCode == keyCode) { |
| 4715 | return AKEY_STATE_UP; |
| 4716 | } |
| 4717 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4718 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4719 | |
| 4720 | return AKEY_STATE_UNKNOWN; |
| 4721 | } |
| 4722 | |
| 4723 | int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4724 | { // acquire lock |
| 4725 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4726 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4727 | if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4728 | return AKEY_STATE_VIRTUAL; |
| 4729 | } |
| 4730 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4731 | size_t numVirtualKeys = mLocked.virtualKeys.size(); |
| 4732 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 4733 | const VirtualKey& virtualKey = mLocked.virtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4734 | if (virtualKey.scanCode == scanCode) { |
| 4735 | return AKEY_STATE_UP; |
| 4736 | } |
| 4737 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4738 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4739 | |
| 4740 | return AKEY_STATE_UNKNOWN; |
| 4741 | } |
| 4742 | |
| 4743 | bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 4744 | const int32_t* keyCodes, uint8_t* outFlags) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4745 | { // acquire lock |
| 4746 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4747 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4748 | size_t numVirtualKeys = mLocked.virtualKeys.size(); |
| 4749 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 4750 | const VirtualKey& virtualKey = mLocked.virtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4751 | |
| 4752 | for (size_t i = 0; i < numCodes; i++) { |
| 4753 | if (virtualKey.keyCode == keyCodes[i]) { |
| 4754 | outFlags[i] = 1; |
| 4755 | } |
| 4756 | } |
| 4757 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 4758 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4759 | |
| 4760 | return true; |
| 4761 | } |
| 4762 | |
| 4763 | |
| 4764 | // --- SingleTouchInputMapper --- |
| 4765 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 4766 | SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) : |
| 4767 | TouchInputMapper(device) { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 4768 | clearState(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4769 | } |
| 4770 | |
| 4771 | SingleTouchInputMapper::~SingleTouchInputMapper() { |
| 4772 | } |
| 4773 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 4774 | void SingleTouchInputMapper::clearState() { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4775 | mAccumulator.clear(); |
| 4776 | |
| 4777 | mDown = false; |
| 4778 | mX = 0; |
| 4779 | mY = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 4780 | mPressure = 0; // default to 0 for devices that don't report pressure |
| 4781 | mToolWidth = 0; // default to 0 for devices that don't report tool width |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4782 | mButtonState = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4783 | } |
| 4784 | |
| 4785 | void SingleTouchInputMapper::reset() { |
| 4786 | TouchInputMapper::reset(); |
| 4787 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 4788 | clearState(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4789 | } |
| 4790 | |
| 4791 | void SingleTouchInputMapper::process(const RawEvent* rawEvent) { |
| 4792 | switch (rawEvent->type) { |
| 4793 | case EV_KEY: |
| 4794 | switch (rawEvent->scanCode) { |
| 4795 | case BTN_TOUCH: |
| 4796 | mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH; |
| 4797 | mAccumulator.btnTouch = rawEvent->value != 0; |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 4798 | // Don't sync immediately. Wait until the next SYN_REPORT since we might |
| 4799 | // not have received valid position information yet. This logic assumes that |
| 4800 | // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4801 | break; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4802 | default: |
| 4803 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4804 | int32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4805 | if (buttonState) { |
| 4806 | if (rawEvent->value) { |
| 4807 | mAccumulator.buttonDown |= buttonState; |
| 4808 | } else { |
| 4809 | mAccumulator.buttonUp |= buttonState; |
| 4810 | } |
| 4811 | mAccumulator.fields |= Accumulator::FIELD_BUTTONS; |
| 4812 | } |
| 4813 | } |
| 4814 | break; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4815 | } |
| 4816 | break; |
| 4817 | |
| 4818 | case EV_ABS: |
| 4819 | switch (rawEvent->scanCode) { |
| 4820 | case ABS_X: |
| 4821 | mAccumulator.fields |= Accumulator::FIELD_ABS_X; |
| 4822 | mAccumulator.absX = rawEvent->value; |
| 4823 | break; |
| 4824 | case ABS_Y: |
| 4825 | mAccumulator.fields |= Accumulator::FIELD_ABS_Y; |
| 4826 | mAccumulator.absY = rawEvent->value; |
| 4827 | break; |
| 4828 | case ABS_PRESSURE: |
| 4829 | mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE; |
| 4830 | mAccumulator.absPressure = rawEvent->value; |
| 4831 | break; |
| 4832 | case ABS_TOOL_WIDTH: |
| 4833 | mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH; |
| 4834 | mAccumulator.absToolWidth = rawEvent->value; |
| 4835 | break; |
| 4836 | } |
| 4837 | break; |
| 4838 | |
| 4839 | case EV_SYN: |
| 4840 | switch (rawEvent->scanCode) { |
| 4841 | case SYN_REPORT: |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 4842 | sync(rawEvent->when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4843 | break; |
| 4844 | } |
| 4845 | break; |
| 4846 | } |
| 4847 | } |
| 4848 | |
| 4849 | void SingleTouchInputMapper::sync(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4850 | uint32_t fields = mAccumulator.fields; |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 4851 | if (fields == 0) { |
| 4852 | return; // no new state changes, so nothing to do |
| 4853 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4854 | |
| 4855 | if (fields & Accumulator::FIELD_BTN_TOUCH) { |
| 4856 | mDown = mAccumulator.btnTouch; |
| 4857 | } |
| 4858 | |
| 4859 | if (fields & Accumulator::FIELD_ABS_X) { |
| 4860 | mX = mAccumulator.absX; |
| 4861 | } |
| 4862 | |
| 4863 | if (fields & Accumulator::FIELD_ABS_Y) { |
| 4864 | mY = mAccumulator.absY; |
| 4865 | } |
| 4866 | |
| 4867 | if (fields & Accumulator::FIELD_ABS_PRESSURE) { |
| 4868 | mPressure = mAccumulator.absPressure; |
| 4869 | } |
| 4870 | |
| 4871 | if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 4872 | mToolWidth = mAccumulator.absToolWidth; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4873 | } |
| 4874 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4875 | if (fields & Accumulator::FIELD_BUTTONS) { |
| 4876 | mButtonState = (mButtonState | mAccumulator.buttonDown) & ~mAccumulator.buttonUp; |
| 4877 | } |
| 4878 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4879 | mCurrentTouch.clear(); |
| 4880 | |
| 4881 | if (mDown) { |
| 4882 | mCurrentTouch.pointerCount = 1; |
| 4883 | mCurrentTouch.pointers[0].id = 0; |
| 4884 | mCurrentTouch.pointers[0].x = mX; |
| 4885 | mCurrentTouch.pointers[0].y = mY; |
| 4886 | mCurrentTouch.pointers[0].pressure = mPressure; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 4887 | mCurrentTouch.pointers[0].touchMajor = 0; |
| 4888 | mCurrentTouch.pointers[0].touchMinor = 0; |
| 4889 | mCurrentTouch.pointers[0].toolMajor = mToolWidth; |
| 4890 | mCurrentTouch.pointers[0].toolMinor = mToolWidth; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4891 | mCurrentTouch.pointers[0].orientation = 0; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 4892 | mCurrentTouch.pointers[0].distance = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4893 | mCurrentTouch.pointers[0].isStylus = false; // TODO: Set stylus |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4894 | mCurrentTouch.idToIndex[0] = 0; |
| 4895 | mCurrentTouch.idBits.markBit(0); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4896 | mCurrentTouch.buttonState = mButtonState; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4897 | } |
| 4898 | |
| 4899 | syncTouch(when, true); |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 4900 | |
| 4901 | mAccumulator.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4902 | } |
| 4903 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 4904 | void SingleTouchInputMapper::configureRawAxes() { |
| 4905 | TouchInputMapper::configureRawAxes(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4906 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 4907 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x); |
| 4908 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y); |
| 4909 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure); |
| 4910 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4911 | } |
| 4912 | |
| 4913 | |
| 4914 | // --- MultiTouchInputMapper --- |
| 4915 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 4916 | MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) : |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 4917 | TouchInputMapper(device), mSlotCount(0), mUsingSlotsProtocol(false) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4918 | } |
| 4919 | |
| 4920 | MultiTouchInputMapper::~MultiTouchInputMapper() { |
| 4921 | } |
| 4922 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 4923 | void MultiTouchInputMapper::clearState() { |
Jeff Brown | 441a9c2 | 2011-06-02 18:22:25 -0700 | [diff] [blame] | 4924 | mAccumulator.clearSlots(mSlotCount); |
| 4925 | mAccumulator.clearButtons(); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4926 | mButtonState = 0; |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 4927 | mPointerIdBits.clear(); |
Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 4928 | |
| 4929 | if (mUsingSlotsProtocol) { |
| 4930 | // Query the driver for the current slot index and use it as the initial slot |
| 4931 | // before we start reading events from the device. It is possible that the |
| 4932 | // current slot index will not be the same as it was when the first event was |
| 4933 | // written into the evdev buffer, which means the input mapper could start |
| 4934 | // out of sync with the initial state of the events in the evdev buffer. |
| 4935 | // In the extremely unlikely case that this happens, the data from |
| 4936 | // two slots will be confused until the next ABS_MT_SLOT event is received. |
| 4937 | // This can cause the touch point to "jump", but at least there will be |
| 4938 | // no stuck touches. |
| 4939 | status_t status = getEventHub()->getAbsoluteAxisValue(getDeviceId(), ABS_MT_SLOT, |
| 4940 | &mAccumulator.currentSlot); |
| 4941 | if (status) { |
| 4942 | LOGW("Could not retrieve current multitouch slot index. status=%d", status); |
| 4943 | mAccumulator.currentSlot = -1; |
| 4944 | } |
| 4945 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4946 | } |
| 4947 | |
| 4948 | void MultiTouchInputMapper::reset() { |
| 4949 | TouchInputMapper::reset(); |
| 4950 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 4951 | clearState(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4952 | } |
| 4953 | |
| 4954 | void MultiTouchInputMapper::process(const RawEvent* rawEvent) { |
| 4955 | switch (rawEvent->type) { |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4956 | case EV_KEY: { |
| 4957 | if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4958 | int32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4959 | if (buttonState) { |
| 4960 | if (rawEvent->value) { |
| 4961 | mAccumulator.buttonDown |= buttonState; |
| 4962 | } else { |
| 4963 | mAccumulator.buttonUp |= buttonState; |
| 4964 | } |
| 4965 | } |
| 4966 | } |
| 4967 | break; |
| 4968 | } |
| 4969 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4970 | case EV_ABS: { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 4971 | bool newSlot = false; |
| 4972 | if (mUsingSlotsProtocol && rawEvent->scanCode == ABS_MT_SLOT) { |
| 4973 | mAccumulator.currentSlot = rawEvent->value; |
| 4974 | newSlot = true; |
| 4975 | } |
| 4976 | |
| 4977 | if (mAccumulator.currentSlot < 0 || size_t(mAccumulator.currentSlot) >= mSlotCount) { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 4978 | #if DEBUG_POINTERS |
Jeff Brown | 441a9c2 | 2011-06-02 18:22:25 -0700 | [diff] [blame] | 4979 | if (newSlot) { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 4980 | LOGW("MultiTouch device %s emitted invalid slot index %d but it " |
| 4981 | "should be between 0 and %d; ignoring this slot.", |
| 4982 | getDeviceName().string(), mAccumulator.currentSlot, mSlotCount); |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 4983 | } |
Jeff Brown | 441a9c2 | 2011-06-02 18:22:25 -0700 | [diff] [blame] | 4984 | #endif |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 4985 | break; |
| 4986 | } |
| 4987 | |
| 4988 | Accumulator::Slot* slot = &mAccumulator.slots[mAccumulator.currentSlot]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4989 | |
| 4990 | switch (rawEvent->scanCode) { |
| 4991 | case ABS_MT_POSITION_X: |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 4992 | slot->fields |= Accumulator::FIELD_ABS_MT_POSITION_X; |
| 4993 | slot->absMTPositionX = rawEvent->value; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4994 | break; |
| 4995 | case ABS_MT_POSITION_Y: |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 4996 | slot->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y; |
| 4997 | slot->absMTPositionY = rawEvent->value; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 4998 | break; |
| 4999 | case ABS_MT_TOUCH_MAJOR: |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5000 | slot->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR; |
| 5001 | slot->absMTTouchMajor = rawEvent->value; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5002 | break; |
| 5003 | case ABS_MT_TOUCH_MINOR: |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5004 | slot->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR; |
| 5005 | slot->absMTTouchMinor = rawEvent->value; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5006 | break; |
| 5007 | case ABS_MT_WIDTH_MAJOR: |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5008 | slot->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR; |
| 5009 | slot->absMTWidthMajor = rawEvent->value; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5010 | break; |
| 5011 | case ABS_MT_WIDTH_MINOR: |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5012 | slot->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR; |
| 5013 | slot->absMTWidthMinor = rawEvent->value; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5014 | break; |
| 5015 | case ABS_MT_ORIENTATION: |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5016 | slot->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION; |
| 5017 | slot->absMTOrientation = rawEvent->value; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5018 | break; |
| 5019 | case ABS_MT_TRACKING_ID: |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5020 | if (mUsingSlotsProtocol && rawEvent->value < 0) { |
| 5021 | slot->clear(); |
| 5022 | } else { |
| 5023 | slot->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID; |
| 5024 | slot->absMTTrackingId = rawEvent->value; |
| 5025 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5026 | break; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5027 | case ABS_MT_PRESSURE: |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5028 | slot->fields |= Accumulator::FIELD_ABS_MT_PRESSURE; |
| 5029 | slot->absMTPressure = rawEvent->value; |
| 5030 | break; |
| 5031 | case ABS_MT_TOOL_TYPE: |
| 5032 | slot->fields |= Accumulator::FIELD_ABS_MT_TOOL_TYPE; |
| 5033 | slot->absMTToolType = rawEvent->value; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5034 | break; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5035 | } |
| 5036 | break; |
| 5037 | } |
| 5038 | |
| 5039 | case EV_SYN: |
| 5040 | switch (rawEvent->scanCode) { |
| 5041 | case SYN_MT_REPORT: { |
| 5042 | // MultiTouch Sync: The driver has returned all data for *one* of the pointers. |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5043 | mAccumulator.currentSlot += 1; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5044 | break; |
| 5045 | } |
| 5046 | |
| 5047 | case SYN_REPORT: |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5048 | sync(rawEvent->when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5049 | break; |
| 5050 | } |
| 5051 | break; |
| 5052 | } |
| 5053 | } |
| 5054 | |
| 5055 | void MultiTouchInputMapper::sync(nsecs_t when) { |
| 5056 | static const uint32_t REQUIRED_FIELDS = |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5057 | Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5058 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5059 | size_t inCount = mSlotCount; |
| 5060 | size_t outCount = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5061 | bool havePointerIds = true; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5062 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5063 | mCurrentTouch.clear(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5064 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5065 | for (size_t inIndex = 0; inIndex < inCount; inIndex++) { |
| 5066 | const Accumulator::Slot& inSlot = mAccumulator.slots[inIndex]; |
| 5067 | uint32_t fields = inSlot.fields; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5068 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5069 | if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) { |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5070 | // Some drivers send empty MT sync packets without X / Y to indicate a pointer up. |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5071 | // This may also indicate an unused slot. |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5072 | // Drop this finger. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5073 | continue; |
| 5074 | } |
| 5075 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5076 | if (outCount >= MAX_POINTERS) { |
| 5077 | #if DEBUG_POINTERS |
| 5078 | LOGD("MultiTouch device %s emitted more than maximum of %d pointers; " |
| 5079 | "ignoring the rest.", |
| 5080 | getDeviceName().string(), MAX_POINTERS); |
| 5081 | #endif |
| 5082 | break; // too many fingers! |
| 5083 | } |
| 5084 | |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5085 | PointerData& outPointer = mCurrentTouch.pointers[outCount]; |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5086 | outPointer.x = inSlot.absMTPositionX; |
| 5087 | outPointer.y = inSlot.absMTPositionY; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5088 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5089 | if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5090 | outPointer.pressure = inSlot.absMTPressure; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5091 | } else { |
| 5092 | // Default pressure to 0 if absent. |
| 5093 | outPointer.pressure = 0; |
| 5094 | } |
| 5095 | |
| 5096 | if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5097 | if (inSlot.absMTTouchMajor <= 0) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5098 | // Some devices send sync packets with X / Y but with a 0 touch major to indicate |
| 5099 | // a pointer going up. Drop this finger. |
| 5100 | continue; |
| 5101 | } |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5102 | outPointer.touchMajor = inSlot.absMTTouchMajor; |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5103 | } else { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5104 | // Default touch area to 0 if absent. |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5105 | outPointer.touchMajor = 0; |
| 5106 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5107 | |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5108 | if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5109 | outPointer.touchMinor = inSlot.absMTTouchMinor; |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5110 | } else { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5111 | // Assume touch area is circular. |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5112 | outPointer.touchMinor = outPointer.touchMajor; |
| 5113 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5114 | |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5115 | if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5116 | outPointer.toolMajor = inSlot.absMTWidthMajor; |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5117 | } else { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5118 | // Default tool area to 0 if absent. |
| 5119 | outPointer.toolMajor = 0; |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5120 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5121 | |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5122 | if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5123 | outPointer.toolMinor = inSlot.absMTWidthMinor; |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5124 | } else { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5125 | // Assume tool area is circular. |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5126 | outPointer.toolMinor = outPointer.toolMajor; |
| 5127 | } |
| 5128 | |
| 5129 | if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5130 | outPointer.orientation = inSlot.absMTOrientation; |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5131 | } else { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5132 | // Default orientation to vertical if absent. |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5133 | outPointer.orientation = 0; |
| 5134 | } |
| 5135 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5136 | if (fields & Accumulator::FIELD_ABS_MT_DISTANCE) { |
| 5137 | outPointer.distance = inSlot.absMTDistance; |
| 5138 | } else { |
| 5139 | // Default distance is 0 (direct contact). |
| 5140 | outPointer.distance = 0; |
| 5141 | } |
| 5142 | |
| 5143 | if (fields & Accumulator::FIELD_ABS_MT_TOOL_TYPE) { |
| 5144 | outPointer.isStylus = (inSlot.absMTToolType == MT_TOOL_PEN); |
| 5145 | } else { |
| 5146 | // Assume this is not a stylus. |
| 5147 | outPointer.isStylus = false; |
| 5148 | } |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5149 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5150 | // Assign pointer id using tracking id if available. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5151 | if (havePointerIds) { |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5152 | int32_t id = -1; |
| 5153 | if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) { |
| 5154 | int32_t trackingId = inSlot.absMTTrackingId; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5155 | |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5156 | for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) { |
| 5157 | uint32_t n = idBits.firstMarkedBit(); |
| 5158 | idBits.clearBit(n); |
| 5159 | |
| 5160 | if (mPointerTrackingIdMap[n] == trackingId) { |
| 5161 | id = n; |
| 5162 | } |
| 5163 | } |
| 5164 | |
| 5165 | if (id < 0 && !mPointerIdBits.isFull()) { |
| 5166 | id = mPointerIdBits.firstUnmarkedBit(); |
| 5167 | mPointerIdBits.markBit(id); |
| 5168 | mPointerTrackingIdMap[id] = trackingId; |
| 5169 | } |
| 5170 | } |
| 5171 | if (id < 0) { |
| 5172 | havePointerIds = false; |
| 5173 | mCurrentTouch.idBits.clear(); |
| 5174 | } else { |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5175 | outPointer.id = id; |
| 5176 | mCurrentTouch.idToIndex[id] = outCount; |
| 5177 | mCurrentTouch.idBits.markBit(id); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5178 | } |
| 5179 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5180 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5181 | outCount += 1; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5182 | } |
| 5183 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5184 | mCurrentTouch.pointerCount = outCount; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5185 | |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 5186 | mButtonState = (mButtonState | mAccumulator.buttonDown) & ~mAccumulator.buttonUp; |
| 5187 | mCurrentTouch.buttonState = mButtonState; |
| 5188 | |
Jeff Brown | 6894a29 | 2011-07-01 17:59:27 -0700 | [diff] [blame] | 5189 | mPointerIdBits = mCurrentTouch.idBits; |
| 5190 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 5191 | syncTouch(when, havePointerIds); |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 5192 | |
Jeff Brown | 441a9c2 | 2011-06-02 18:22:25 -0700 | [diff] [blame] | 5193 | if (!mUsingSlotsProtocol) { |
| 5194 | mAccumulator.clearSlots(mSlotCount); |
| 5195 | } |
| 5196 | mAccumulator.clearButtons(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5197 | } |
| 5198 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 5199 | void MultiTouchInputMapper::configureRawAxes() { |
| 5200 | TouchInputMapper::configureRawAxes(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5201 | |
Jeff Brown | 80fd47c | 2011-05-24 01:07:44 -0700 | [diff] [blame] | 5202 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, &mRawAxes.x); |
| 5203 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, &mRawAxes.y); |
| 5204 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, &mRawAxes.touchMajor); |
| 5205 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, &mRawAxes.touchMinor); |
| 5206 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, &mRawAxes.toolMajor); |
| 5207 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, &mRawAxes.toolMinor); |
| 5208 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, &mRawAxes.orientation); |
| 5209 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, &mRawAxes.pressure); |
| 5210 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_DISTANCE, &mRawAxes.distance); |
| 5211 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TRACKING_ID, &mRawAxes.trackingId); |
| 5212 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_SLOT, &mRawAxes.slot); |
| 5213 | |
| 5214 | if (mRawAxes.trackingId.valid |
| 5215 | && mRawAxes.slot.valid && mRawAxes.slot.minValue == 0 && mRawAxes.slot.maxValue > 0) { |
| 5216 | mSlotCount = mRawAxes.slot.maxValue + 1; |
| 5217 | if (mSlotCount > MAX_SLOTS) { |
| 5218 | LOGW("MultiTouch Device %s reported %d slots but the framework " |
| 5219 | "only supports a maximum of %d slots at this time.", |
| 5220 | getDeviceName().string(), mSlotCount, MAX_SLOTS); |
| 5221 | mSlotCount = MAX_SLOTS; |
| 5222 | } |
| 5223 | mUsingSlotsProtocol = true; |
| 5224 | } else { |
| 5225 | mSlotCount = MAX_POINTERS; |
| 5226 | mUsingSlotsProtocol = false; |
| 5227 | } |
| 5228 | |
| 5229 | mAccumulator.allocateSlots(mSlotCount); |
Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 5230 | |
| 5231 | clearState(); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 5232 | } |
| 5233 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5234 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5235 | // --- JoystickInputMapper --- |
| 5236 | |
| 5237 | JoystickInputMapper::JoystickInputMapper(InputDevice* device) : |
| 5238 | InputMapper(device) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5239 | } |
| 5240 | |
| 5241 | JoystickInputMapper::~JoystickInputMapper() { |
| 5242 | } |
| 5243 | |
| 5244 | uint32_t JoystickInputMapper::getSources() { |
| 5245 | return AINPUT_SOURCE_JOYSTICK; |
| 5246 | } |
| 5247 | |
| 5248 | void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 5249 | InputMapper::populateDeviceInfo(info); |
| 5250 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5251 | for (size_t i = 0; i < mAxes.size(); i++) { |
| 5252 | const Axis& axis = mAxes.valueAt(i); |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 5253 | info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK, |
| 5254 | axis.min, axis.max, axis.flat, axis.fuzz); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5255 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 5256 | info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK, |
| 5257 | axis.min, axis.max, axis.flat, axis.fuzz); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5258 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5259 | } |
| 5260 | } |
| 5261 | |
| 5262 | void JoystickInputMapper::dump(String8& dump) { |
| 5263 | dump.append(INDENT2 "Joystick Input Mapper:\n"); |
| 5264 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5265 | dump.append(INDENT3 "Axes:\n"); |
| 5266 | size_t numAxes = mAxes.size(); |
| 5267 | for (size_t i = 0; i < numAxes; i++) { |
| 5268 | const Axis& axis = mAxes.valueAt(i); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5269 | const char* label = getAxisLabel(axis.axisInfo.axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5270 | if (label) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5271 | dump.appendFormat(INDENT4 "%s", label); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5272 | } else { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5273 | dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5274 | } |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5275 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 5276 | label = getAxisLabel(axis.axisInfo.highAxis); |
| 5277 | if (label) { |
| 5278 | dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue); |
| 5279 | } else { |
| 5280 | dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis, |
| 5281 | axis.axisInfo.splitValue); |
| 5282 | } |
| 5283 | } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) { |
| 5284 | dump.append(" (invert)"); |
| 5285 | } |
| 5286 | |
| 5287 | dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n", |
| 5288 | axis.min, axis.max, axis.flat, axis.fuzz); |
| 5289 | dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, " |
| 5290 | "highScale=%0.5f, highOffset=%0.5f\n", |
| 5291 | axis.scale, axis.offset, axis.highScale, axis.highOffset); |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 5292 | dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, " |
| 5293 | "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n", |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5294 | mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue, |
Jeff Brown | b3a2d13 | 2011-06-12 18:14:50 -0700 | [diff] [blame] | 5295 | axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5296 | } |
| 5297 | } |
| 5298 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5299 | void JoystickInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) { |
| 5300 | InputMapper::configure(config, changes); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5301 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5302 | if (!changes) { // first time only |
| 5303 | // Collect all axes. |
| 5304 | for (int32_t abs = 0; abs <= ABS_MAX; abs++) { |
| 5305 | RawAbsoluteAxisInfo rawAxisInfo; |
| 5306 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), abs, &rawAxisInfo); |
| 5307 | if (rawAxisInfo.valid) { |
| 5308 | // Map axis. |
| 5309 | AxisInfo axisInfo; |
| 5310 | bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo); |
| 5311 | if (!explicitlyMapped) { |
| 5312 | // Axis is not explicitly mapped, will choose a generic axis later. |
| 5313 | axisInfo.mode = AxisInfo::MODE_NORMAL; |
| 5314 | axisInfo.axis = -1; |
| 5315 | } |
| 5316 | |
| 5317 | // Apply flat override. |
| 5318 | int32_t rawFlat = axisInfo.flatOverride < 0 |
| 5319 | ? rawAxisInfo.flat : axisInfo.flatOverride; |
| 5320 | |
| 5321 | // Calculate scaling factors and limits. |
| 5322 | Axis axis; |
| 5323 | if (axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 5324 | float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue); |
| 5325 | float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue); |
| 5326 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 5327 | scale, 0.0f, highScale, 0.0f, |
| 5328 | 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
| 5329 | } else if (isCenteredAxis(axisInfo.axis)) { |
| 5330 | float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); |
| 5331 | float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale; |
| 5332 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 5333 | scale, offset, scale, offset, |
| 5334 | -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
| 5335 | } else { |
| 5336 | float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); |
| 5337 | axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, |
| 5338 | scale, 0.0f, scale, 0.0f, |
| 5339 | 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale); |
| 5340 | } |
| 5341 | |
| 5342 | // To eliminate noise while the joystick is at rest, filter out small variations |
| 5343 | // in axis values up front. |
| 5344 | axis.filter = axis.flat * 0.25f; |
| 5345 | |
| 5346 | mAxes.add(abs, axis); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5347 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5348 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5349 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5350 | // If there are too many axes, start dropping them. |
| 5351 | // Prefer to keep explicitly mapped axes. |
| 5352 | if (mAxes.size() > PointerCoords::MAX_AXES) { |
| 5353 | LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.", |
| 5354 | getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES); |
| 5355 | pruneAxes(true); |
| 5356 | pruneAxes(false); |
| 5357 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5358 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5359 | // Assign generic axis ids to remaining axes. |
| 5360 | int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1; |
| 5361 | size_t numAxes = mAxes.size(); |
| 5362 | for (size_t i = 0; i < numAxes; i++) { |
| 5363 | Axis& axis = mAxes.editValueAt(i); |
| 5364 | if (axis.axisInfo.axis < 0) { |
| 5365 | while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16 |
| 5366 | && haveAxis(nextGenericAxisId)) { |
| 5367 | nextGenericAxisId += 1; |
| 5368 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5369 | |
Jeff Brown | 474dcb5 | 2011-06-14 20:22:50 -0700 | [diff] [blame] | 5370 | if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) { |
| 5371 | axis.axisInfo.axis = nextGenericAxisId; |
| 5372 | nextGenericAxisId += 1; |
| 5373 | } else { |
| 5374 | LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids " |
| 5375 | "have already been assigned to other axes.", |
| 5376 | getDeviceName().string(), mAxes.keyAt(i)); |
| 5377 | mAxes.removeItemsAt(i--); |
| 5378 | numAxes -= 1; |
| 5379 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5380 | } |
| 5381 | } |
| 5382 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5383 | } |
| 5384 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5385 | bool JoystickInputMapper::haveAxis(int32_t axisId) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5386 | size_t numAxes = mAxes.size(); |
| 5387 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5388 | const Axis& axis = mAxes.valueAt(i); |
| 5389 | if (axis.axisInfo.axis == axisId |
| 5390 | || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT |
| 5391 | && axis.axisInfo.highAxis == axisId)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5392 | return true; |
| 5393 | } |
| 5394 | } |
| 5395 | return false; |
| 5396 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5397 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5398 | void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) { |
| 5399 | size_t i = mAxes.size(); |
| 5400 | while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) { |
| 5401 | if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) { |
| 5402 | continue; |
| 5403 | } |
| 5404 | LOGI("Discarding joystick '%s' axis %d because there are too many axes.", |
| 5405 | getDeviceName().string(), mAxes.keyAt(i)); |
| 5406 | mAxes.removeItemsAt(i); |
| 5407 | } |
| 5408 | } |
| 5409 | |
| 5410 | bool JoystickInputMapper::isCenteredAxis(int32_t axis) { |
| 5411 | switch (axis) { |
| 5412 | case AMOTION_EVENT_AXIS_X: |
| 5413 | case AMOTION_EVENT_AXIS_Y: |
| 5414 | case AMOTION_EVENT_AXIS_Z: |
| 5415 | case AMOTION_EVENT_AXIS_RX: |
| 5416 | case AMOTION_EVENT_AXIS_RY: |
| 5417 | case AMOTION_EVENT_AXIS_RZ: |
| 5418 | case AMOTION_EVENT_AXIS_HAT_X: |
| 5419 | case AMOTION_EVENT_AXIS_HAT_Y: |
| 5420 | case AMOTION_EVENT_AXIS_ORIENTATION: |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5421 | case AMOTION_EVENT_AXIS_RUDDER: |
| 5422 | case AMOTION_EVENT_AXIS_WHEEL: |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5423 | return true; |
| 5424 | default: |
| 5425 | return false; |
| 5426 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5427 | } |
| 5428 | |
| 5429 | void JoystickInputMapper::reset() { |
| 5430 | // Recenter all axes. |
| 5431 | nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5432 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5433 | size_t numAxes = mAxes.size(); |
| 5434 | for (size_t i = 0; i < numAxes; i++) { |
| 5435 | Axis& axis = mAxes.editValueAt(i); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5436 | axis.resetValue(); |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5437 | } |
| 5438 | |
| 5439 | sync(when, true /*force*/); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5440 | |
| 5441 | InputMapper::reset(); |
| 5442 | } |
| 5443 | |
| 5444 | void JoystickInputMapper::process(const RawEvent* rawEvent) { |
| 5445 | switch (rawEvent->type) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5446 | case EV_ABS: { |
| 5447 | ssize_t index = mAxes.indexOfKey(rawEvent->scanCode); |
| 5448 | if (index >= 0) { |
| 5449 | Axis& axis = mAxes.editValueAt(index); |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5450 | float newValue, highNewValue; |
| 5451 | switch (axis.axisInfo.mode) { |
| 5452 | case AxisInfo::MODE_INVERT: |
| 5453 | newValue = (axis.rawAxisInfo.maxValue - rawEvent->value) |
| 5454 | * axis.scale + axis.offset; |
| 5455 | highNewValue = 0.0f; |
| 5456 | break; |
| 5457 | case AxisInfo::MODE_SPLIT: |
| 5458 | if (rawEvent->value < axis.axisInfo.splitValue) { |
| 5459 | newValue = (axis.axisInfo.splitValue - rawEvent->value) |
| 5460 | * axis.scale + axis.offset; |
| 5461 | highNewValue = 0.0f; |
| 5462 | } else if (rawEvent->value > axis.axisInfo.splitValue) { |
| 5463 | newValue = 0.0f; |
| 5464 | highNewValue = (rawEvent->value - axis.axisInfo.splitValue) |
| 5465 | * axis.highScale + axis.highOffset; |
| 5466 | } else { |
| 5467 | newValue = 0.0f; |
| 5468 | highNewValue = 0.0f; |
| 5469 | } |
| 5470 | break; |
| 5471 | default: |
| 5472 | newValue = rawEvent->value * axis.scale + axis.offset; |
| 5473 | highNewValue = 0.0f; |
| 5474 | break; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5475 | } |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5476 | axis.newValue = newValue; |
| 5477 | axis.highNewValue = highNewValue; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5478 | } |
| 5479 | break; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5480 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5481 | |
| 5482 | case EV_SYN: |
| 5483 | switch (rawEvent->scanCode) { |
| 5484 | case SYN_REPORT: |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5485 | sync(rawEvent->when, false /*force*/); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5486 | break; |
| 5487 | } |
| 5488 | break; |
| 5489 | } |
| 5490 | } |
| 5491 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5492 | void JoystickInputMapper::sync(nsecs_t when, bool force) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5493 | if (!filterAxes(force)) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5494 | return; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5495 | } |
| 5496 | |
| 5497 | int32_t metaState = mContext->getGlobalMetaState(); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5498 | int32_t buttonState = 0; |
| 5499 | |
| 5500 | PointerProperties pointerProperties; |
| 5501 | pointerProperties.clear(); |
| 5502 | pointerProperties.id = 0; |
| 5503 | pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5504 | |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5505 | PointerCoords pointerCoords; |
| 5506 | pointerCoords.clear(); |
| 5507 | |
| 5508 | size_t numAxes = mAxes.size(); |
| 5509 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5510 | const Axis& axis = mAxes.valueAt(i); |
| 5511 | pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue); |
| 5512 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 5513 | pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue); |
| 5514 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5515 | } |
| 5516 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 5517 | // Moving a joystick axis should not wake the devide because joysticks can |
| 5518 | // be fairly noisy even when not in use. On the other hand, pushing a gamepad |
| 5519 | // button will likely wake the device. |
| 5520 | // TODO: Use the input device configuration to control this behavior more finely. |
| 5521 | uint32_t policyFlags = 0; |
| 5522 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 5523 | getDispatcher()->notifyMotion(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 5524 | AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE, |
| 5525 | 1, &pointerProperties, &pointerCoords, 0, 0, 0); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5526 | } |
| 5527 | |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5528 | bool JoystickInputMapper::filterAxes(bool force) { |
| 5529 | bool atLeastOneSignificantChange = force; |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5530 | size_t numAxes = mAxes.size(); |
| 5531 | for (size_t i = 0; i < numAxes; i++) { |
Jeff Brown | 8529745 | 2011-03-04 13:07:49 -0800 | [diff] [blame] | 5532 | Axis& axis = mAxes.editValueAt(i); |
| 5533 | if (force || hasValueChangedSignificantly(axis.filter, |
| 5534 | axis.newValue, axis.currentValue, axis.min, axis.max)) { |
| 5535 | axis.currentValue = axis.newValue; |
| 5536 | atLeastOneSignificantChange = true; |
| 5537 | } |
| 5538 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 5539 | if (force || hasValueChangedSignificantly(axis.filter, |
| 5540 | axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) { |
| 5541 | axis.highCurrentValue = axis.highNewValue; |
| 5542 | atLeastOneSignificantChange = true; |
| 5543 | } |
| 5544 | } |
| 5545 | } |
| 5546 | return atLeastOneSignificantChange; |
| 5547 | } |
| 5548 | |
| 5549 | bool JoystickInputMapper::hasValueChangedSignificantly( |
| 5550 | float filter, float newValue, float currentValue, float min, float max) { |
| 5551 | if (newValue != currentValue) { |
| 5552 | // Filter out small changes in value unless the value is converging on the axis |
| 5553 | // bounds or center point. This is intended to reduce the amount of information |
| 5554 | // sent to applications by particularly noisy joysticks (such as PS3). |
| 5555 | if (fabs(newValue - currentValue) > filter |
| 5556 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min) |
| 5557 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max) |
| 5558 | || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) { |
| 5559 | return true; |
| 5560 | } |
| 5561 | } |
| 5562 | return false; |
| 5563 | } |
| 5564 | |
| 5565 | bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange( |
| 5566 | float filter, float newValue, float currentValue, float thresholdValue) { |
| 5567 | float newDistance = fabs(newValue - thresholdValue); |
| 5568 | if (newDistance < filter) { |
| 5569 | float oldDistance = fabs(currentValue - thresholdValue); |
| 5570 | if (newDistance < oldDistance) { |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5571 | return true; |
| 5572 | } |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5573 | } |
Jeff Brown | 6f2fba4 | 2011-02-19 01:08:02 -0800 | [diff] [blame] | 5574 | return false; |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 5575 | } |
| 5576 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 5577 | } // namespace android |