Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 17 | #define LOG_TAG "InputReader" |
| 18 | |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
| 21 | // Log debug messages for each raw event received from the EventHub. |
| 22 | #define DEBUG_RAW_EVENTS 0 |
| 23 | |
| 24 | // Log debug messages about touch screen filtering hacks. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 25 | #define DEBUG_HACKS 0 |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 26 | |
| 27 | // Log debug messages about virtual key processing. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 28 | #define DEBUG_VIRTUAL_KEYS 0 |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 29 | |
| 30 | // Log debug messages about pointers. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 31 | #define DEBUG_POINTERS 0 |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 32 | |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 33 | // Log debug messages about pointer assignment calculations. |
| 34 | #define DEBUG_POINTER_ASSIGNMENT 0 |
| 35 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 36 | #include "InputReader.h" |
| 37 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 38 | #include <cutils/log.h> |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 39 | #include <ui/Keyboard.h> |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 40 | #include <ui/VirtualKeyMap.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 41 | |
| 42 | #include <stddef.h> |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 43 | #include <stdlib.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 44 | #include <unistd.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 45 | #include <errno.h> |
| 46 | #include <limits.h> |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 47 | #include <math.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 48 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 49 | #define INDENT " " |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 50 | #define INDENT2 " " |
| 51 | #define INDENT3 " " |
| 52 | #define INDENT4 " " |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 53 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 54 | namespace android { |
| 55 | |
| 56 | // --- Static Functions --- |
| 57 | |
| 58 | template<typename T> |
| 59 | inline static T abs(const T& value) { |
| 60 | return value < 0 ? - value : value; |
| 61 | } |
| 62 | |
| 63 | template<typename T> |
| 64 | inline static T min(const T& a, const T& b) { |
| 65 | return a < b ? a : b; |
| 66 | } |
| 67 | |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 68 | template<typename T> |
| 69 | inline static void swap(T& a, T& b) { |
| 70 | T temp = a; |
| 71 | a = b; |
| 72 | b = temp; |
| 73 | } |
| 74 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 75 | inline static float avg(float x, float y) { |
| 76 | return (x + y) / 2; |
| 77 | } |
| 78 | |
| 79 | inline static float pythag(float x, float y) { |
| 80 | return sqrtf(x * x + y * y); |
| 81 | } |
| 82 | |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 83 | inline static int32_t signExtendNybble(int32_t value) { |
| 84 | return value >= 8 ? value - 16 : value; |
| 85 | } |
| 86 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 87 | static inline const char* toString(bool value) { |
| 88 | return value ? "true" : "false"; |
| 89 | } |
| 90 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 91 | static const int32_t keyCodeRotationMap[][4] = { |
| 92 | // key codes enumerated counter-clockwise with the original (unrotated) key first |
| 93 | // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 94 | { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT }, |
| 95 | { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN }, |
| 96 | { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT }, |
| 97 | { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP }, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 98 | }; |
| 99 | static const int keyCodeRotationMapSize = |
| 100 | sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]); |
| 101 | |
| 102 | int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 103 | if (orientation != DISPLAY_ORIENTATION_0) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 104 | for (int i = 0; i < keyCodeRotationMapSize; i++) { |
| 105 | if (keyCode == keyCodeRotationMap[i][0]) { |
| 106 | return keyCodeRotationMap[i][orientation]; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | return keyCode; |
| 111 | } |
| 112 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 113 | static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) { |
| 114 | return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0; |
| 115 | } |
| 116 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 117 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 118 | // --- InputReader --- |
| 119 | |
| 120 | InputReader::InputReader(const sp<EventHubInterface>& eventHub, |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 121 | const sp<InputReaderPolicyInterface>& policy, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 122 | const sp<InputDispatcherInterface>& dispatcher) : |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 123 | mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher), |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 124 | mGlobalMetaState(0), mDisableVirtualKeysTimeout(-1) { |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 125 | configureExcludedDevices(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 126 | updateGlobalMetaState(); |
| 127 | updateInputConfiguration(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | InputReader::~InputReader() { |
| 131 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 132 | delete mDevices.valueAt(i); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void InputReader::loopOnce() { |
| 137 | RawEvent rawEvent; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 138 | mEventHub->getEvent(& rawEvent); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 139 | |
| 140 | #if DEBUG_RAW_EVENTS |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 141 | LOGD("Input event: device=%d type=0x%x scancode=%d keycode=%d value=%d", |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 142 | rawEvent.deviceId, rawEvent.type, rawEvent.scanCode, rawEvent.keyCode, |
| 143 | rawEvent.value); |
| 144 | #endif |
| 145 | |
| 146 | process(& rawEvent); |
| 147 | } |
| 148 | |
| 149 | void InputReader::process(const RawEvent* rawEvent) { |
| 150 | switch (rawEvent->type) { |
| 151 | case EventHubInterface::DEVICE_ADDED: |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 152 | addDevice(rawEvent->deviceId); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 153 | break; |
| 154 | |
| 155 | case EventHubInterface::DEVICE_REMOVED: |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 156 | removeDevice(rawEvent->deviceId); |
| 157 | break; |
| 158 | |
| 159 | case EventHubInterface::FINISHED_DEVICE_SCAN: |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 160 | handleConfigurationChanged(rawEvent->when); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 161 | break; |
| 162 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 163 | default: |
| 164 | consumeEvent(rawEvent); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 169 | void InputReader::addDevice(int32_t deviceId) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 170 | String8 name = mEventHub->getDeviceName(deviceId); |
| 171 | uint32_t classes = mEventHub->getDeviceClasses(deviceId); |
| 172 | |
| 173 | InputDevice* device = createDevice(deviceId, name, classes); |
| 174 | device->configure(); |
| 175 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 176 | if (device->isIgnored()) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 177 | 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] | 178 | } else { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 179 | 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] | 180 | device->getSources()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 183 | bool added = false; |
| 184 | { // acquire device registry writer lock |
| 185 | RWLock::AutoWLock _wl(mDeviceRegistryLock); |
| 186 | |
| 187 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 188 | if (deviceIndex < 0) { |
| 189 | mDevices.add(deviceId, device); |
| 190 | added = true; |
| 191 | } |
| 192 | } // release device registry writer lock |
| 193 | |
| 194 | if (! added) { |
| 195 | LOGW("Ignoring spurious device added event for deviceId %d.", deviceId); |
| 196 | delete device; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 197 | return; |
| 198 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Jeff Brown | 7342bb9 | 2010-10-01 18:55:43 -0700 | [diff] [blame] | 201 | void InputReader::removeDevice(int32_t deviceId) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 202 | bool removed = false; |
| 203 | InputDevice* device = NULL; |
| 204 | { // acquire device registry writer lock |
| 205 | RWLock::AutoWLock _wl(mDeviceRegistryLock); |
| 206 | |
| 207 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 208 | if (deviceIndex >= 0) { |
| 209 | device = mDevices.valueAt(deviceIndex); |
| 210 | mDevices.removeItemsAt(deviceIndex, 1); |
| 211 | removed = true; |
| 212 | } |
| 213 | } // release device registry writer lock |
| 214 | |
| 215 | if (! removed) { |
| 216 | LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 217 | return; |
| 218 | } |
| 219 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 220 | if (device->isIgnored()) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 221 | LOGI("Device removed: id=%d, name='%s' (ignored non-input device)", |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 222 | device->getId(), device->getName().string()); |
| 223 | } else { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 224 | LOGI("Device removed: id=%d, name='%s', sources=0x%08x", |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 225 | device->getId(), device->getName().string(), device->getSources()); |
| 226 | } |
| 227 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 228 | device->reset(); |
| 229 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 230 | delete device; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 233 | InputDevice* InputReader::createDevice(int32_t deviceId, const String8& name, uint32_t classes) { |
| 234 | InputDevice* device = new InputDevice(this, deviceId, name); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 235 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 236 | // Switch-like devices. |
| 237 | if (classes & INPUT_DEVICE_CLASS_SWITCH) { |
| 238 | device->addMapper(new SwitchInputMapper(device)); |
| 239 | } |
| 240 | |
| 241 | // Keyboard-like devices. |
| 242 | uint32_t keyboardSources = 0; |
| 243 | int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC; |
| 244 | if (classes & INPUT_DEVICE_CLASS_KEYBOARD) { |
| 245 | keyboardSources |= AINPUT_SOURCE_KEYBOARD; |
| 246 | } |
| 247 | if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) { |
| 248 | keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC; |
| 249 | } |
| 250 | if (classes & INPUT_DEVICE_CLASS_DPAD) { |
| 251 | keyboardSources |= AINPUT_SOURCE_DPAD; |
| 252 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 253 | |
| 254 | if (keyboardSources != 0) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 255 | device->addMapper(new KeyboardInputMapper(device, keyboardSources, keyboardType)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 258 | // Cursor-like devices. |
| 259 | if (classes & INPUT_DEVICE_CLASS_CURSOR) { |
| 260 | device->addMapper(new CursorInputMapper(device)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | // Touchscreen-like devices. |
| 264 | if (classes & INPUT_DEVICE_CLASS_TOUCHSCREEN_MT) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 265 | device->addMapper(new MultiTouchInputMapper(device)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 266 | } else if (classes & INPUT_DEVICE_CLASS_TOUCHSCREEN) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 267 | device->addMapper(new SingleTouchInputMapper(device)); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | return device; |
| 271 | } |
| 272 | |
| 273 | void InputReader::consumeEvent(const RawEvent* rawEvent) { |
| 274 | int32_t deviceId = rawEvent->deviceId; |
| 275 | |
| 276 | { // acquire device registry reader lock |
| 277 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 278 | |
| 279 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 280 | if (deviceIndex < 0) { |
| 281 | LOGW("Discarding event for unknown deviceId %d.", deviceId); |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 286 | if (device->isIgnored()) { |
| 287 | //LOGD("Discarding event for ignored deviceId %d.", deviceId); |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | device->process(rawEvent); |
| 292 | } // release device registry reader lock |
| 293 | } |
| 294 | |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 295 | void InputReader::handleConfigurationChanged(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 296 | // Reset global meta state because it depends on the list of all configured devices. |
| 297 | updateGlobalMetaState(); |
| 298 | |
| 299 | // Update input configuration. |
| 300 | updateInputConfiguration(); |
| 301 | |
| 302 | // Enqueue configuration changed. |
| 303 | mDispatcher->notifyConfigurationChanged(when); |
| 304 | } |
| 305 | |
| 306 | void InputReader::configureExcludedDevices() { |
| 307 | Vector<String8> excludedDeviceNames; |
| 308 | mPolicy->getExcludedDeviceNames(excludedDeviceNames); |
| 309 | |
| 310 | for (size_t i = 0; i < excludedDeviceNames.size(); i++) { |
| 311 | mEventHub->addExcludedDevice(excludedDeviceNames[i]); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | void InputReader::updateGlobalMetaState() { |
| 316 | { // acquire state lock |
| 317 | AutoMutex _l(mStateLock); |
| 318 | |
| 319 | mGlobalMetaState = 0; |
| 320 | |
| 321 | { // acquire device registry reader lock |
| 322 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 323 | |
| 324 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 325 | InputDevice* device = mDevices.valueAt(i); |
| 326 | mGlobalMetaState |= device->getMetaState(); |
| 327 | } |
| 328 | } // release device registry reader lock |
| 329 | } // release state lock |
| 330 | } |
| 331 | |
| 332 | int32_t InputReader::getGlobalMetaState() { |
| 333 | { // acquire state lock |
| 334 | AutoMutex _l(mStateLock); |
| 335 | |
| 336 | return mGlobalMetaState; |
| 337 | } // release state lock |
| 338 | } |
| 339 | |
| 340 | void InputReader::updateInputConfiguration() { |
| 341 | { // acquire state lock |
| 342 | AutoMutex _l(mStateLock); |
| 343 | |
| 344 | int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH; |
| 345 | int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS; |
| 346 | int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV; |
| 347 | { // acquire device registry reader lock |
| 348 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 349 | |
| 350 | InputDeviceInfo deviceInfo; |
| 351 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 352 | InputDevice* device = mDevices.valueAt(i); |
| 353 | device->getDeviceInfo(& deviceInfo); |
| 354 | uint32_t sources = deviceInfo.getSources(); |
| 355 | |
| 356 | if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) { |
| 357 | touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER; |
| 358 | } |
| 359 | if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) { |
| 360 | navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL; |
| 361 | } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) { |
| 362 | navigationConfig = InputConfiguration::NAVIGATION_DPAD; |
| 363 | } |
| 364 | if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) { |
| 365 | keyboardConfig = InputConfiguration::KEYBOARD_QWERTY; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 366 | } |
| 367 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 368 | } // release device registry reader lock |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 369 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 370 | mInputConfiguration.touchScreen = touchScreenConfig; |
| 371 | mInputConfiguration.keyboard = keyboardConfig; |
| 372 | mInputConfiguration.navigation = navigationConfig; |
| 373 | } // release state lock |
| 374 | } |
| 375 | |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 376 | void InputReader::disableVirtualKeysUntil(nsecs_t time) { |
| 377 | mDisableVirtualKeysTimeout = time; |
| 378 | } |
| 379 | |
| 380 | bool InputReader::shouldDropVirtualKey(nsecs_t now, |
| 381 | InputDevice* device, int32_t keyCode, int32_t scanCode) { |
| 382 | if (now < mDisableVirtualKeysTimeout) { |
| 383 | LOGI("Dropping virtual key from device %s because virtual keys are " |
| 384 | "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d", |
| 385 | device->getName().string(), |
| 386 | (mDisableVirtualKeysTimeout - now) * 0.000001, |
| 387 | keyCode, scanCode); |
| 388 | return true; |
| 389 | } else { |
| 390 | return false; |
| 391 | } |
| 392 | } |
| 393 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 394 | void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) { |
| 395 | { // acquire state lock |
| 396 | AutoMutex _l(mStateLock); |
| 397 | |
| 398 | *outConfiguration = mInputConfiguration; |
| 399 | } // release state lock |
| 400 | } |
| 401 | |
| 402 | status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) { |
| 403 | { // acquire device registry reader lock |
| 404 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 405 | |
| 406 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 407 | if (deviceIndex < 0) { |
| 408 | return NAME_NOT_FOUND; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 411 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 412 | if (device->isIgnored()) { |
| 413 | return NAME_NOT_FOUND; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 414 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 415 | |
| 416 | device->getDeviceInfo(outDeviceInfo); |
| 417 | return OK; |
| 418 | } // release device registy reader lock |
| 419 | } |
| 420 | |
| 421 | void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) { |
| 422 | outDeviceIds.clear(); |
| 423 | |
| 424 | { // acquire device registry reader lock |
| 425 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 426 | |
| 427 | size_t numDevices = mDevices.size(); |
| 428 | for (size_t i = 0; i < numDevices; i++) { |
| 429 | InputDevice* device = mDevices.valueAt(i); |
| 430 | if (! device->isIgnored()) { |
| 431 | outDeviceIds.add(device->getId()); |
| 432 | } |
| 433 | } |
| 434 | } // release device registy reader lock |
| 435 | } |
| 436 | |
| 437 | int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask, |
| 438 | int32_t keyCode) { |
| 439 | return getState(deviceId, sourceMask, keyCode, & InputDevice::getKeyCodeState); |
| 440 | } |
| 441 | |
| 442 | int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask, |
| 443 | int32_t scanCode) { |
| 444 | return getState(deviceId, sourceMask, scanCode, & InputDevice::getScanCodeState); |
| 445 | } |
| 446 | |
| 447 | int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) { |
| 448 | return getState(deviceId, sourceMask, switchCode, & InputDevice::getSwitchState); |
| 449 | } |
| 450 | |
| 451 | int32_t InputReader::getState(int32_t deviceId, uint32_t sourceMask, int32_t code, |
| 452 | GetStateFunc getStateFunc) { |
| 453 | { // acquire device registry reader lock |
| 454 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 455 | |
| 456 | int32_t result = AKEY_STATE_UNKNOWN; |
| 457 | if (deviceId >= 0) { |
| 458 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 459 | if (deviceIndex >= 0) { |
| 460 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 461 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 462 | result = (device->*getStateFunc)(sourceMask, code); |
| 463 | } |
| 464 | } |
| 465 | } else { |
| 466 | size_t numDevices = mDevices.size(); |
| 467 | for (size_t i = 0; i < numDevices; i++) { |
| 468 | InputDevice* device = mDevices.valueAt(i); |
| 469 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 470 | result = (device->*getStateFunc)(sourceMask, code); |
| 471 | if (result >= AKEY_STATE_DOWN) { |
| 472 | return result; |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | return result; |
| 478 | } // release device registy reader lock |
| 479 | } |
| 480 | |
| 481 | bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask, |
| 482 | size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) { |
| 483 | memset(outFlags, 0, numCodes); |
| 484 | return markSupportedKeyCodes(deviceId, sourceMask, numCodes, keyCodes, outFlags); |
| 485 | } |
| 486 | |
| 487 | bool InputReader::markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes, |
| 488 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 489 | { // acquire device registry reader lock |
| 490 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
| 491 | bool result = false; |
| 492 | if (deviceId >= 0) { |
| 493 | ssize_t deviceIndex = mDevices.indexOfKey(deviceId); |
| 494 | if (deviceIndex >= 0) { |
| 495 | InputDevice* device = mDevices.valueAt(deviceIndex); |
| 496 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 497 | result = device->markSupportedKeyCodes(sourceMask, |
| 498 | numCodes, keyCodes, outFlags); |
| 499 | } |
| 500 | } |
| 501 | } else { |
| 502 | size_t numDevices = mDevices.size(); |
| 503 | for (size_t i = 0; i < numDevices; i++) { |
| 504 | InputDevice* device = mDevices.valueAt(i); |
| 505 | if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) { |
| 506 | result |= device->markSupportedKeyCodes(sourceMask, |
| 507 | numCodes, keyCodes, outFlags); |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | return result; |
| 512 | } // release device registy reader lock |
| 513 | } |
| 514 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 515 | void InputReader::dump(String8& dump) { |
Jeff Brown | f2f48718 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 516 | mEventHub->dump(dump); |
| 517 | dump.append("\n"); |
| 518 | |
| 519 | dump.append("Input Reader State:\n"); |
| 520 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 521 | { // acquire device registry reader lock |
| 522 | RWLock::AutoRLock _rl(mDeviceRegistryLock); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 523 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 524 | for (size_t i = 0; i < mDevices.size(); i++) { |
| 525 | mDevices.valueAt(i)->dump(dump); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 526 | } |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 527 | } // release device registy reader lock |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 530 | |
| 531 | // --- InputReaderThread --- |
| 532 | |
| 533 | InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) : |
| 534 | Thread(/*canCallJava*/ true), mReader(reader) { |
| 535 | } |
| 536 | |
| 537 | InputReaderThread::~InputReaderThread() { |
| 538 | } |
| 539 | |
| 540 | bool InputReaderThread::threadLoop() { |
| 541 | mReader->loopOnce(); |
| 542 | return true; |
| 543 | } |
| 544 | |
| 545 | |
| 546 | // --- InputDevice --- |
| 547 | |
| 548 | InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) : |
| 549 | mContext(context), mId(id), mName(name), mSources(0) { |
| 550 | } |
| 551 | |
| 552 | InputDevice::~InputDevice() { |
| 553 | size_t numMappers = mMappers.size(); |
| 554 | for (size_t i = 0; i < numMappers; i++) { |
| 555 | delete mMappers[i]; |
| 556 | } |
| 557 | mMappers.clear(); |
| 558 | } |
| 559 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 560 | static void dumpMotionRange(String8& dump, const InputDeviceInfo& deviceInfo, |
| 561 | int32_t rangeType, const char* name) { |
| 562 | const InputDeviceInfo::MotionRange* range = deviceInfo.getMotionRange(rangeType); |
| 563 | if (range) { |
| 564 | dump.appendFormat(INDENT3 "%s: min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n", |
| 565 | name, range->min, range->max, range->flat, range->fuzz); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | void InputDevice::dump(String8& dump) { |
| 570 | InputDeviceInfo deviceInfo; |
| 571 | getDeviceInfo(& deviceInfo); |
| 572 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 573 | dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(), |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 574 | deviceInfo.getName().string()); |
| 575 | dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources()); |
| 576 | dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType()); |
| 577 | if (!deviceInfo.getMotionRanges().isEmpty()) { |
| 578 | dump.append(INDENT2 "Motion Ranges:\n"); |
| 579 | dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_X, "X"); |
| 580 | dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_Y, "Y"); |
| 581 | dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_PRESSURE, "Pressure"); |
| 582 | dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_SIZE, "Size"); |
| 583 | dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOUCH_MAJOR, "TouchMajor"); |
| 584 | dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOUCH_MINOR, "TouchMinor"); |
| 585 | dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOOL_MAJOR, "ToolMajor"); |
| 586 | dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOOL_MINOR, "ToolMinor"); |
| 587 | dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_ORIENTATION, "Orientation"); |
| 588 | } |
| 589 | |
| 590 | size_t numMappers = mMappers.size(); |
| 591 | for (size_t i = 0; i < numMappers; i++) { |
| 592 | InputMapper* mapper = mMappers[i]; |
| 593 | mapper->dump(dump); |
| 594 | } |
| 595 | } |
| 596 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 597 | void InputDevice::addMapper(InputMapper* mapper) { |
| 598 | mMappers.add(mapper); |
| 599 | } |
| 600 | |
| 601 | void InputDevice::configure() { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 602 | if (! isIgnored()) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 603 | mContext->getEventHub()->getConfiguration(mId, &mConfiguration); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 606 | mSources = 0; |
| 607 | |
| 608 | size_t numMappers = mMappers.size(); |
| 609 | for (size_t i = 0; i < numMappers; i++) { |
| 610 | InputMapper* mapper = mMappers[i]; |
| 611 | mapper->configure(); |
| 612 | mSources |= mapper->getSources(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 616 | void InputDevice::reset() { |
| 617 | size_t numMappers = mMappers.size(); |
| 618 | for (size_t i = 0; i < numMappers; i++) { |
| 619 | InputMapper* mapper = mMappers[i]; |
| 620 | mapper->reset(); |
| 621 | } |
| 622 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 623 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 624 | void InputDevice::process(const RawEvent* rawEvent) { |
| 625 | size_t numMappers = mMappers.size(); |
| 626 | for (size_t i = 0; i < numMappers; i++) { |
| 627 | InputMapper* mapper = mMappers[i]; |
| 628 | mapper->process(rawEvent); |
| 629 | } |
| 630 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 631 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 632 | void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) { |
| 633 | outDeviceInfo->initialize(mId, mName); |
| 634 | |
| 635 | size_t numMappers = mMappers.size(); |
| 636 | for (size_t i = 0; i < numMappers; i++) { |
| 637 | InputMapper* mapper = mMappers[i]; |
| 638 | mapper->populateDeviceInfo(outDeviceInfo); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 643 | return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState); |
| 644 | } |
| 645 | |
| 646 | int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 647 | return getState(sourceMask, scanCode, & InputMapper::getScanCodeState); |
| 648 | } |
| 649 | |
| 650 | int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 651 | return getState(sourceMask, switchCode, & InputMapper::getSwitchState); |
| 652 | } |
| 653 | |
| 654 | int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) { |
| 655 | int32_t result = AKEY_STATE_UNKNOWN; |
| 656 | size_t numMappers = mMappers.size(); |
| 657 | for (size_t i = 0; i < numMappers; i++) { |
| 658 | InputMapper* mapper = mMappers[i]; |
| 659 | if (sourcesMatchMask(mapper->getSources(), sourceMask)) { |
| 660 | result = (mapper->*getStateFunc)(sourceMask, code); |
| 661 | if (result >= AKEY_STATE_DOWN) { |
| 662 | return result; |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | return result; |
| 667 | } |
| 668 | |
| 669 | bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 670 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 671 | bool result = false; |
| 672 | size_t numMappers = mMappers.size(); |
| 673 | for (size_t i = 0; i < numMappers; i++) { |
| 674 | InputMapper* mapper = mMappers[i]; |
| 675 | if (sourcesMatchMask(mapper->getSources(), sourceMask)) { |
| 676 | result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags); |
| 677 | } |
| 678 | } |
| 679 | return result; |
| 680 | } |
| 681 | |
| 682 | int32_t InputDevice::getMetaState() { |
| 683 | int32_t result = 0; |
| 684 | size_t numMappers = mMappers.size(); |
| 685 | for (size_t i = 0; i < numMappers; i++) { |
| 686 | InputMapper* mapper = mMappers[i]; |
| 687 | result |= mapper->getMetaState(); |
| 688 | } |
| 689 | return result; |
| 690 | } |
| 691 | |
| 692 | |
| 693 | // --- InputMapper --- |
| 694 | |
| 695 | InputMapper::InputMapper(InputDevice* device) : |
| 696 | mDevice(device), mContext(device->getContext()) { |
| 697 | } |
| 698 | |
| 699 | InputMapper::~InputMapper() { |
| 700 | } |
| 701 | |
| 702 | void InputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 703 | info->addSource(getSources()); |
| 704 | } |
| 705 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 706 | void InputMapper::dump(String8& dump) { |
| 707 | } |
| 708 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 709 | void InputMapper::configure() { |
| 710 | } |
| 711 | |
| 712 | void InputMapper::reset() { |
| 713 | } |
| 714 | |
| 715 | int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 716 | return AKEY_STATE_UNKNOWN; |
| 717 | } |
| 718 | |
| 719 | int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 720 | return AKEY_STATE_UNKNOWN; |
| 721 | } |
| 722 | |
| 723 | int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 724 | return AKEY_STATE_UNKNOWN; |
| 725 | } |
| 726 | |
| 727 | bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 728 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 729 | return false; |
| 730 | } |
| 731 | |
| 732 | int32_t InputMapper::getMetaState() { |
| 733 | return 0; |
| 734 | } |
| 735 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 736 | |
| 737 | // --- SwitchInputMapper --- |
| 738 | |
| 739 | SwitchInputMapper::SwitchInputMapper(InputDevice* device) : |
| 740 | InputMapper(device) { |
| 741 | } |
| 742 | |
| 743 | SwitchInputMapper::~SwitchInputMapper() { |
| 744 | } |
| 745 | |
| 746 | uint32_t SwitchInputMapper::getSources() { |
Jeff Brown | 89de57a | 2011-01-19 18:41:38 -0800 | [diff] [blame^] | 747 | return AINPUT_SOURCE_SWITCH; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | void SwitchInputMapper::process(const RawEvent* rawEvent) { |
| 751 | switch (rawEvent->type) { |
| 752 | case EV_SW: |
| 753 | processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value); |
| 754 | break; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) { |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 759 | getDispatcher()->notifySwitch(when, switchCode, switchValue, 0); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) { |
| 763 | return getEventHub()->getSwitchState(getDeviceId(), switchCode); |
| 764 | } |
| 765 | |
| 766 | |
| 767 | // --- KeyboardInputMapper --- |
| 768 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 769 | KeyboardInputMapper::KeyboardInputMapper(InputDevice* device, |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 770 | uint32_t sources, int32_t keyboardType) : |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 771 | InputMapper(device), mSources(sources), |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 772 | mKeyboardType(keyboardType) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 773 | initializeLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | KeyboardInputMapper::~KeyboardInputMapper() { |
| 777 | } |
| 778 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 779 | void KeyboardInputMapper::initializeLocked() { |
| 780 | mLocked.metaState = AMETA_NONE; |
| 781 | mLocked.downTime = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | uint32_t KeyboardInputMapper::getSources() { |
| 785 | return mSources; |
| 786 | } |
| 787 | |
| 788 | void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 789 | InputMapper::populateDeviceInfo(info); |
| 790 | |
| 791 | info->setKeyboardType(mKeyboardType); |
| 792 | } |
| 793 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 794 | void KeyboardInputMapper::dump(String8& dump) { |
| 795 | { // acquire lock |
| 796 | AutoMutex _l(mLock); |
| 797 | dump.append(INDENT2 "Keyboard Input Mapper:\n"); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 798 | dumpParameters(dump); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 799 | dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType); |
| 800 | dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mLocked.keyDowns.size()); |
| 801 | dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mLocked.metaState); |
| 802 | dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime); |
| 803 | } // release lock |
| 804 | } |
| 805 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 806 | |
| 807 | void KeyboardInputMapper::configure() { |
| 808 | InputMapper::configure(); |
| 809 | |
| 810 | // Configure basic parameters. |
| 811 | configureParameters(); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 812 | |
| 813 | // Reset LEDs. |
| 814 | { |
| 815 | AutoMutex _l(mLock); |
| 816 | resetLedStateLocked(); |
| 817 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | void KeyboardInputMapper::configureParameters() { |
| 821 | mParameters.orientationAware = false; |
| 822 | getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"), |
| 823 | mParameters.orientationAware); |
| 824 | |
| 825 | mParameters.associatedDisplayId = mParameters.orientationAware ? 0 : -1; |
| 826 | } |
| 827 | |
| 828 | void KeyboardInputMapper::dumpParameters(String8& dump) { |
| 829 | dump.append(INDENT3 "Parameters:\n"); |
| 830 | dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n", |
| 831 | mParameters.associatedDisplayId); |
| 832 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 833 | toString(mParameters.orientationAware)); |
| 834 | } |
| 835 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 836 | void KeyboardInputMapper::reset() { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 837 | for (;;) { |
| 838 | int32_t keyCode, scanCode; |
| 839 | { // acquire lock |
| 840 | AutoMutex _l(mLock); |
| 841 | |
| 842 | // Synthesize key up event on reset if keys are currently down. |
| 843 | if (mLocked.keyDowns.isEmpty()) { |
| 844 | initializeLocked(); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 845 | resetLedStateLocked(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 846 | break; // done |
| 847 | } |
| 848 | |
| 849 | const KeyDown& keyDown = mLocked.keyDowns.top(); |
| 850 | keyCode = keyDown.keyCode; |
| 851 | scanCode = keyDown.scanCode; |
| 852 | } // release lock |
| 853 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 854 | nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 855 | processKey(when, false, keyCode, scanCode, 0); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | InputMapper::reset(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 859 | getContext()->updateGlobalMetaState(); |
| 860 | } |
| 861 | |
| 862 | void KeyboardInputMapper::process(const RawEvent* rawEvent) { |
| 863 | switch (rawEvent->type) { |
| 864 | case EV_KEY: { |
| 865 | int32_t scanCode = rawEvent->scanCode; |
| 866 | if (isKeyboardOrGamepadKey(scanCode)) { |
| 867 | processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode, |
| 868 | rawEvent->flags); |
| 869 | } |
| 870 | break; |
| 871 | } |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) { |
| 876 | return scanCode < BTN_MOUSE |
| 877 | || scanCode >= KEY_OK |
| 878 | || (scanCode >= BTN_GAMEPAD && scanCode < BTN_DIGI); |
| 879 | } |
| 880 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 881 | void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode, |
| 882 | int32_t scanCode, uint32_t policyFlags) { |
| 883 | int32_t newMetaState; |
| 884 | nsecs_t downTime; |
| 885 | bool metaStateChanged = false; |
| 886 | |
| 887 | { // acquire lock |
| 888 | AutoMutex _l(mLock); |
| 889 | |
| 890 | if (down) { |
| 891 | // Rotate key codes according to orientation if needed. |
| 892 | // Note: getDisplayInfo is non-reentrant so we can continue holding the lock. |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 893 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 894 | int32_t orientation; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 895 | if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId, |
| 896 | NULL, NULL, & orientation)) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 897 | orientation = DISPLAY_ORIENTATION_0; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | keyCode = rotateKeyCode(keyCode, orientation); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 901 | } |
| 902 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 903 | // Add key down. |
| 904 | ssize_t keyDownIndex = findKeyDownLocked(scanCode); |
| 905 | if (keyDownIndex >= 0) { |
| 906 | // 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] | 907 | keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 908 | } else { |
| 909 | // key down |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 910 | if ((policyFlags & POLICY_FLAG_VIRTUAL) |
| 911 | && mContext->shouldDropVirtualKey(when, |
| 912 | getDevice(), keyCode, scanCode)) { |
| 913 | return; |
| 914 | } |
| 915 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 916 | mLocked.keyDowns.push(); |
| 917 | KeyDown& keyDown = mLocked.keyDowns.editTop(); |
| 918 | keyDown.keyCode = keyCode; |
| 919 | keyDown.scanCode = scanCode; |
| 920 | } |
| 921 | |
| 922 | mLocked.downTime = when; |
| 923 | } else { |
| 924 | // Remove key down. |
| 925 | ssize_t keyDownIndex = findKeyDownLocked(scanCode); |
| 926 | if (keyDownIndex >= 0) { |
| 927 | // 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] | 928 | keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 929 | mLocked.keyDowns.removeAt(size_t(keyDownIndex)); |
| 930 | } else { |
| 931 | // key was not actually down |
| 932 | LOGI("Dropping key up from device %s because the key was not down. " |
| 933 | "keyCode=%d, scanCode=%d", |
| 934 | getDeviceName().string(), keyCode, scanCode); |
| 935 | return; |
| 936 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 937 | } |
| 938 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 939 | int32_t oldMetaState = mLocked.metaState; |
| 940 | newMetaState = updateMetaState(keyCode, down, oldMetaState); |
| 941 | if (oldMetaState != newMetaState) { |
| 942 | mLocked.metaState = newMetaState; |
| 943 | metaStateChanged = true; |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 944 | updateLedStateLocked(false); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 945 | } |
Jeff Brown | fd035829 | 2010-06-30 16:10:35 -0700 | [diff] [blame] | 946 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 947 | downTime = mLocked.downTime; |
| 948 | } // release lock |
| 949 | |
| 950 | if (metaStateChanged) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 951 | getContext()->updateGlobalMetaState(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 952 | } |
| 953 | |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 954 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 955 | newMetaState |= AMETA_FUNCTION_ON; |
| 956 | } |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 957 | getDispatcher()->notifyKey(when, getDeviceId(), mSources, policyFlags, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 958 | down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP, |
| 959 | AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 960 | } |
| 961 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 962 | ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) { |
| 963 | size_t n = mLocked.keyDowns.size(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 964 | for (size_t i = 0; i < n; i++) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 965 | if (mLocked.keyDowns[i].scanCode == scanCode) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 966 | return i; |
| 967 | } |
| 968 | } |
| 969 | return -1; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 970 | } |
| 971 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 972 | int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
| 973 | return getEventHub()->getKeyCodeState(getDeviceId(), keyCode); |
| 974 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 975 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 976 | int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
| 977 | return getEventHub()->getScanCodeState(getDeviceId(), scanCode); |
| 978 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 979 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 980 | bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 981 | const int32_t* keyCodes, uint8_t* outFlags) { |
| 982 | return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags); |
| 983 | } |
| 984 | |
| 985 | int32_t KeyboardInputMapper::getMetaState() { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 986 | { // acquire lock |
| 987 | AutoMutex _l(mLock); |
| 988 | return mLocked.metaState; |
| 989 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 990 | } |
| 991 | |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 992 | void KeyboardInputMapper::resetLedStateLocked() { |
| 993 | initializeLedStateLocked(mLocked.capsLockLedState, LED_CAPSL); |
| 994 | initializeLedStateLocked(mLocked.numLockLedState, LED_NUML); |
| 995 | initializeLedStateLocked(mLocked.scrollLockLedState, LED_SCROLLL); |
| 996 | |
| 997 | updateLedStateLocked(true); |
| 998 | } |
| 999 | |
| 1000 | void KeyboardInputMapper::initializeLedStateLocked(LockedState::LedState& ledState, int32_t led) { |
| 1001 | ledState.avail = getEventHub()->hasLed(getDeviceId(), led); |
| 1002 | ledState.on = false; |
| 1003 | } |
| 1004 | |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1005 | void KeyboardInputMapper::updateLedStateLocked(bool reset) { |
| 1006 | updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL, |
Jeff Brown | 51e7fe75 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 1007 | AMETA_CAPS_LOCK_ON, reset); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1008 | updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML, |
Jeff Brown | 51e7fe75 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 1009 | AMETA_NUM_LOCK_ON, reset); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1010 | updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL, |
Jeff Brown | 51e7fe75 | 2010-10-29 22:19:53 -0700 | [diff] [blame] | 1011 | AMETA_SCROLL_LOCK_ON, reset); |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | void KeyboardInputMapper::updateLedStateForModifierLocked(LockedState::LedState& ledState, |
| 1015 | int32_t led, int32_t modifier, bool reset) { |
| 1016 | if (ledState.avail) { |
| 1017 | bool desiredState = (mLocked.metaState & modifier) != 0; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 1018 | if (reset || ledState.on != desiredState) { |
Jeff Brown | 497a92c | 2010-09-12 17:55:08 -0700 | [diff] [blame] | 1019 | getEventHub()->setLedState(getDeviceId(), led, desiredState); |
| 1020 | ledState.on = desiredState; |
| 1021 | } |
| 1022 | } |
| 1023 | } |
| 1024 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1025 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1026 | // --- CursorInputMapper --- |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1027 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1028 | CursorInputMapper::CursorInputMapper(InputDevice* device) : |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1029 | InputMapper(device) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1030 | initializeLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1033 | CursorInputMapper::~CursorInputMapper() { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1034 | } |
| 1035 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1036 | uint32_t CursorInputMapper::getSources() { |
| 1037 | return mSources; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1038 | } |
| 1039 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1040 | void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1041 | InputMapper::populateDeviceInfo(info); |
| 1042 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1043 | if (mParameters.mode == Parameters::MODE_POINTER) { |
| 1044 | float minX, minY, maxX, maxY; |
| 1045 | if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) { |
| 1046 | info->addMotionRange(AINPUT_MOTION_RANGE_X, minX, maxX, 0.0f, 0.0f); |
| 1047 | info->addMotionRange(AINPUT_MOTION_RANGE_Y, minY, maxY, 0.0f, 0.0f); |
| 1048 | } |
| 1049 | } else { |
| 1050 | info->addMotionRange(AINPUT_MOTION_RANGE_X, -1.0f, 1.0f, 0.0f, mXScale); |
| 1051 | info->addMotionRange(AINPUT_MOTION_RANGE_Y, -1.0f, 1.0f, 0.0f, mYScale); |
| 1052 | } |
| 1053 | info->addMotionRange(AINPUT_MOTION_RANGE_PRESSURE, 0.0f, 1.0f, 0.0f, 0.0f); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1056 | void CursorInputMapper::dump(String8& dump) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1057 | { // acquire lock |
| 1058 | AutoMutex _l(mLock); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1059 | dump.append(INDENT2 "Cursor Input Mapper:\n"); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1060 | dumpParameters(dump); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1061 | dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision); |
| 1062 | dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision); |
| 1063 | dump.appendFormat(INDENT3 "Down: %s\n", toString(mLocked.down)); |
| 1064 | dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime); |
| 1065 | } // release lock |
| 1066 | } |
| 1067 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1068 | void CursorInputMapper::configure() { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1069 | InputMapper::configure(); |
| 1070 | |
| 1071 | // Configure basic parameters. |
| 1072 | configureParameters(); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1073 | |
| 1074 | // Configure device mode. |
| 1075 | switch (mParameters.mode) { |
| 1076 | case Parameters::MODE_POINTER: |
| 1077 | mSources = AINPUT_SOURCE_MOUSE; |
| 1078 | mXPrecision = 1.0f; |
| 1079 | mYPrecision = 1.0f; |
| 1080 | mXScale = 1.0f; |
| 1081 | mYScale = 1.0f; |
| 1082 | mPointerController = getPolicy()->obtainPointerController(getDeviceId()); |
| 1083 | break; |
| 1084 | case Parameters::MODE_NAVIGATION: |
| 1085 | mSources = AINPUT_SOURCE_TRACKBALL; |
| 1086 | mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 1087 | mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD; |
| 1088 | mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 1089 | mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD; |
| 1090 | break; |
| 1091 | } |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1092 | } |
| 1093 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1094 | void CursorInputMapper::configureParameters() { |
| 1095 | mParameters.mode = Parameters::MODE_POINTER; |
| 1096 | String8 cursorModeString; |
| 1097 | if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) { |
| 1098 | if (cursorModeString == "navigation") { |
| 1099 | mParameters.mode = Parameters::MODE_NAVIGATION; |
| 1100 | } else if (cursorModeString != "pointer" && cursorModeString != "default") { |
| 1101 | LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string()); |
| 1102 | } |
| 1103 | } |
| 1104 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1105 | mParameters.orientationAware = false; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1106 | getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"), |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1107 | mParameters.orientationAware); |
| 1108 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1109 | mParameters.associatedDisplayId = mParameters.mode == Parameters::MODE_POINTER |
| 1110 | || mParameters.orientationAware ? 0 : -1; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1111 | } |
| 1112 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1113 | void CursorInputMapper::dumpParameters(String8& dump) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1114 | dump.append(INDENT3 "Parameters:\n"); |
| 1115 | dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n", |
| 1116 | mParameters.associatedDisplayId); |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1117 | |
| 1118 | switch (mParameters.mode) { |
| 1119 | case Parameters::MODE_POINTER: |
| 1120 | dump.append(INDENT4 "Mode: pointer\n"); |
| 1121 | break; |
| 1122 | case Parameters::MODE_NAVIGATION: |
| 1123 | dump.append(INDENT4 "Mode: navigation\n"); |
| 1124 | break; |
| 1125 | default: |
| 1126 | assert(false); |
| 1127 | } |
| 1128 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1129 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 1130 | toString(mParameters.orientationAware)); |
| 1131 | } |
| 1132 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1133 | void CursorInputMapper::initializeLocked() { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1134 | mAccumulator.clear(); |
| 1135 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1136 | mLocked.down = false; |
| 1137 | mLocked.downTime = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1140 | void CursorInputMapper::reset() { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1141 | for (;;) { |
| 1142 | { // acquire lock |
| 1143 | AutoMutex _l(mLock); |
| 1144 | |
| 1145 | if (! mLocked.down) { |
| 1146 | initializeLocked(); |
| 1147 | break; // done |
| 1148 | } |
| 1149 | } // release lock |
| 1150 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1151 | // Synthesize button up event on reset. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1152 | nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1153 | mAccumulator.fields = Accumulator::FIELD_BTN_MOUSE; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1154 | mAccumulator.btnMouse = false; |
| 1155 | sync(when); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1158 | InputMapper::reset(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1159 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1160 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1161 | void CursorInputMapper::process(const RawEvent* rawEvent) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1162 | switch (rawEvent->type) { |
| 1163 | case EV_KEY: |
| 1164 | switch (rawEvent->scanCode) { |
| 1165 | case BTN_MOUSE: |
| 1166 | mAccumulator.fields |= Accumulator::FIELD_BTN_MOUSE; |
| 1167 | mAccumulator.btnMouse = rawEvent->value != 0; |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 1168 | // Sync now since BTN_MOUSE is not necessarily followed by SYN_REPORT and |
| 1169 | // we need to ensure that we report the up/down promptly. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1170 | sync(rawEvent->when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1171 | break; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1172 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1173 | break; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1174 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1175 | case EV_REL: |
| 1176 | switch (rawEvent->scanCode) { |
| 1177 | case REL_X: |
| 1178 | mAccumulator.fields |= Accumulator::FIELD_REL_X; |
| 1179 | mAccumulator.relX = rawEvent->value; |
| 1180 | break; |
| 1181 | case REL_Y: |
| 1182 | mAccumulator.fields |= Accumulator::FIELD_REL_Y; |
| 1183 | mAccumulator.relY = rawEvent->value; |
| 1184 | break; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1185 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1186 | break; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1187 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1188 | case EV_SYN: |
| 1189 | switch (rawEvent->scanCode) { |
| 1190 | case SYN_REPORT: |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 1191 | sync(rawEvent->when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1192 | break; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1193 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1194 | break; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1195 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1196 | } |
| 1197 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1198 | void CursorInputMapper::sync(nsecs_t when) { |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 1199 | uint32_t fields = mAccumulator.fields; |
| 1200 | if (fields == 0) { |
| 1201 | return; // no new state changes, so nothing to do |
| 1202 | } |
| 1203 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1204 | int motionEventAction; |
| 1205 | PointerCoords pointerCoords; |
| 1206 | nsecs_t downTime; |
| 1207 | { // acquire lock |
| 1208 | AutoMutex _l(mLock); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1209 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1210 | bool downChanged = fields & Accumulator::FIELD_BTN_MOUSE; |
| 1211 | |
| 1212 | if (downChanged) { |
| 1213 | if (mAccumulator.btnMouse) { |
Jeff Brown | 1c9d06e | 2011-01-14 17:24:16 -0800 | [diff] [blame] | 1214 | if (!mLocked.down) { |
| 1215 | mLocked.down = true; |
| 1216 | mLocked.downTime = when; |
| 1217 | } else { |
| 1218 | downChanged = false; |
| 1219 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1220 | } else { |
Jeff Brown | 1c9d06e | 2011-01-14 17:24:16 -0800 | [diff] [blame] | 1221 | if (mLocked.down) { |
| 1222 | mLocked.down = false; |
| 1223 | } else { |
| 1224 | downChanged = false; |
| 1225 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1226 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1227 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1228 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1229 | downTime = mLocked.downTime; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1230 | float deltaX = fields & Accumulator::FIELD_REL_X ? mAccumulator.relX * mXScale : 0.0f; |
| 1231 | float deltaY = fields & Accumulator::FIELD_REL_Y ? mAccumulator.relY * mYScale : 0.0f; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1232 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1233 | if (downChanged) { |
| 1234 | motionEventAction = mLocked.down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1235 | } else { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1236 | motionEventAction = AMOTION_EVENT_ACTION_MOVE; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1237 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1238 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1239 | if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0 |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1240 | && (deltaX != 0.0f || deltaY != 0.0f)) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1241 | // Rotate motion based on display orientation if needed. |
| 1242 | // Note: getDisplayInfo is non-reentrant so we can continue holding the lock. |
| 1243 | int32_t orientation; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1244 | if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId, |
| 1245 | NULL, NULL, & orientation)) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1246 | orientation = DISPLAY_ORIENTATION_0; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | float temp; |
| 1250 | switch (orientation) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1251 | case DISPLAY_ORIENTATION_90: |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1252 | temp = deltaX; |
| 1253 | deltaX = deltaY; |
| 1254 | deltaY = -temp; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1255 | break; |
| 1256 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1257 | case DISPLAY_ORIENTATION_180: |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1258 | deltaX = -deltaX; |
| 1259 | deltaY = -deltaY; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1260 | break; |
| 1261 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1262 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1263 | temp = deltaX; |
| 1264 | deltaX = -deltaY; |
| 1265 | deltaY = temp; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1266 | break; |
| 1267 | } |
| 1268 | } |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1269 | |
| 1270 | if (mPointerController != NULL) { |
| 1271 | mPointerController->move(deltaX, deltaY); |
| 1272 | if (downChanged) { |
| 1273 | mPointerController->setButtonState(mLocked.down ? POINTER_BUTTON_1 : 0); |
| 1274 | } |
| 1275 | mPointerController->getPosition(&pointerCoords.x, &pointerCoords.y); |
| 1276 | } else { |
| 1277 | pointerCoords.x = deltaX; |
| 1278 | pointerCoords.y = deltaY; |
| 1279 | } |
| 1280 | |
| 1281 | pointerCoords.pressure = mLocked.down ? 1.0f : 0.0f; |
| 1282 | pointerCoords.size = 0; |
| 1283 | pointerCoords.touchMajor = 0; |
| 1284 | pointerCoords.touchMinor = 0; |
| 1285 | pointerCoords.toolMajor = 0; |
| 1286 | pointerCoords.toolMinor = 0; |
| 1287 | pointerCoords.orientation = 0; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1288 | } // release lock |
| 1289 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1290 | int32_t metaState = mContext->getGlobalMetaState(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1291 | int32_t pointerId = 0; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1292 | getDispatcher()->notifyMotion(when, getDeviceId(), mSources, 0, |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 1293 | motionEventAction, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1294 | 1, &pointerId, &pointerCoords, mXPrecision, mYPrecision, downTime); |
| 1295 | |
| 1296 | mAccumulator.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1299 | int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Jeff Brown | c3fc2d0 | 2010-08-10 15:47:53 -0700 | [diff] [blame] | 1300 | if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) { |
| 1301 | return getEventHub()->getScanCodeState(getDeviceId(), scanCode); |
| 1302 | } else { |
| 1303 | return AKEY_STATE_UNKNOWN; |
| 1304 | } |
| 1305 | } |
| 1306 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1307 | |
| 1308 | // --- TouchInputMapper --- |
| 1309 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1310 | TouchInputMapper::TouchInputMapper(InputDevice* device) : |
| 1311 | InputMapper(device) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1312 | mLocked.surfaceOrientation = -1; |
| 1313 | mLocked.surfaceWidth = -1; |
| 1314 | mLocked.surfaceHeight = -1; |
| 1315 | |
| 1316 | initializeLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | TouchInputMapper::~TouchInputMapper() { |
| 1320 | } |
| 1321 | |
| 1322 | uint32_t TouchInputMapper::getSources() { |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1323 | return mSources; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) { |
| 1327 | InputMapper::populateDeviceInfo(info); |
| 1328 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1329 | { // acquire lock |
| 1330 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1331 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1332 | // Ensure surface information is up to date so that orientation changes are |
| 1333 | // noticed immediately. |
| 1334 | configureSurfaceLocked(); |
| 1335 | |
| 1336 | info->addMotionRange(AINPUT_MOTION_RANGE_X, mLocked.orientedRanges.x); |
| 1337 | info->addMotionRange(AINPUT_MOTION_RANGE_Y, mLocked.orientedRanges.y); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1338 | |
| 1339 | if (mLocked.orientedRanges.havePressure) { |
| 1340 | info->addMotionRange(AINPUT_MOTION_RANGE_PRESSURE, |
| 1341 | mLocked.orientedRanges.pressure); |
| 1342 | } |
| 1343 | |
| 1344 | if (mLocked.orientedRanges.haveSize) { |
| 1345 | info->addMotionRange(AINPUT_MOTION_RANGE_SIZE, |
| 1346 | mLocked.orientedRanges.size); |
| 1347 | } |
| 1348 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1349 | if (mLocked.orientedRanges.haveTouchSize) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1350 | info->addMotionRange(AINPUT_MOTION_RANGE_TOUCH_MAJOR, |
| 1351 | mLocked.orientedRanges.touchMajor); |
| 1352 | info->addMotionRange(AINPUT_MOTION_RANGE_TOUCH_MINOR, |
| 1353 | mLocked.orientedRanges.touchMinor); |
| 1354 | } |
| 1355 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1356 | if (mLocked.orientedRanges.haveToolSize) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1357 | info->addMotionRange(AINPUT_MOTION_RANGE_TOOL_MAJOR, |
| 1358 | mLocked.orientedRanges.toolMajor); |
| 1359 | info->addMotionRange(AINPUT_MOTION_RANGE_TOOL_MINOR, |
| 1360 | mLocked.orientedRanges.toolMinor); |
| 1361 | } |
| 1362 | |
| 1363 | if (mLocked.orientedRanges.haveOrientation) { |
| 1364 | info->addMotionRange(AINPUT_MOTION_RANGE_ORIENTATION, |
| 1365 | mLocked.orientedRanges.orientation); |
| 1366 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1367 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1368 | } |
| 1369 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1370 | void TouchInputMapper::dump(String8& dump) { |
| 1371 | { // acquire lock |
| 1372 | AutoMutex _l(mLock); |
| 1373 | dump.append(INDENT2 "Touch Input Mapper:\n"); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1374 | dumpParameters(dump); |
| 1375 | dumpVirtualKeysLocked(dump); |
| 1376 | dumpRawAxes(dump); |
| 1377 | dumpCalibration(dump); |
| 1378 | dumpSurfaceLocked(dump); |
Jeff Brown | 511ee5f | 2010-10-18 13:32:20 -0700 | [diff] [blame] | 1379 | dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n"); |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1380 | dump.appendFormat(INDENT4 "XOrigin: %d\n", mLocked.xOrigin); |
| 1381 | dump.appendFormat(INDENT4 "YOrigin: %d\n", mLocked.yOrigin); |
| 1382 | dump.appendFormat(INDENT4 "XScale: %0.3f\n", mLocked.xScale); |
| 1383 | dump.appendFormat(INDENT4 "YScale: %0.3f\n", mLocked.yScale); |
| 1384 | dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mLocked.xPrecision); |
| 1385 | dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mLocked.yPrecision); |
| 1386 | dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mLocked.geometricScale); |
| 1387 | dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mLocked.toolSizeLinearScale); |
| 1388 | dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mLocked.toolSizeLinearBias); |
| 1389 | dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mLocked.toolSizeAreaScale); |
| 1390 | dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mLocked.toolSizeAreaBias); |
| 1391 | dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mLocked.pressureScale); |
| 1392 | dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mLocked.sizeScale); |
| 1393 | dump.appendFormat(INDENT4 "OrientationSCale: %0.3f\n", mLocked.orientationScale); |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1394 | } // release lock |
| 1395 | } |
| 1396 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1397 | void TouchInputMapper::initializeLocked() { |
| 1398 | mCurrentTouch.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1399 | mLastTouch.clear(); |
| 1400 | mDownTime = 0; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1401 | |
| 1402 | for (uint32_t i = 0; i < MAX_POINTERS; i++) { |
| 1403 | mAveragingTouchFilter.historyStart[i] = 0; |
| 1404 | mAveragingTouchFilter.historyEnd[i] = 0; |
| 1405 | } |
| 1406 | |
| 1407 | mJumpyTouchFilter.jumpyPointsDropped = 0; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1408 | |
| 1409 | mLocked.currentVirtualKey.down = false; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1410 | |
| 1411 | mLocked.orientedRanges.havePressure = false; |
| 1412 | mLocked.orientedRanges.haveSize = false; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1413 | mLocked.orientedRanges.haveTouchSize = false; |
| 1414 | mLocked.orientedRanges.haveToolSize = false; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1415 | mLocked.orientedRanges.haveOrientation = false; |
| 1416 | } |
| 1417 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1418 | void TouchInputMapper::configure() { |
| 1419 | InputMapper::configure(); |
| 1420 | |
| 1421 | // Configure basic parameters. |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1422 | configureParameters(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1423 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1424 | // Configure sources. |
| 1425 | switch (mParameters.deviceType) { |
| 1426 | case Parameters::DEVICE_TYPE_TOUCH_SCREEN: |
| 1427 | mSources = AINPUT_SOURCE_TOUCHSCREEN; |
| 1428 | break; |
| 1429 | case Parameters::DEVICE_TYPE_TOUCH_PAD: |
| 1430 | mSources = AINPUT_SOURCE_TOUCHPAD; |
| 1431 | break; |
| 1432 | default: |
| 1433 | assert(false); |
| 1434 | } |
| 1435 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1436 | // Configure absolute axis information. |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1437 | configureRawAxes(); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1438 | |
| 1439 | // Prepare input device calibration. |
| 1440 | parseCalibration(); |
| 1441 | resolveCalibration(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1442 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1443 | { // acquire lock |
| 1444 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1445 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1446 | // Configure surface dimensions and orientation. |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1447 | configureSurfaceLocked(); |
| 1448 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1449 | } |
| 1450 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1451 | void TouchInputMapper::configureParameters() { |
| 1452 | mParameters.useBadTouchFilter = getPolicy()->filterTouchEvents(); |
| 1453 | mParameters.useAveragingTouchFilter = getPolicy()->filterTouchEvents(); |
| 1454 | mParameters.useJumpyTouchFilter = getPolicy()->filterJumpyTouchEvents(); |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 1455 | mParameters.virtualKeyQuietTime = getPolicy()->getVirtualKeyQuietTime(); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1456 | |
| 1457 | String8 deviceTypeString; |
| 1458 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
| 1459 | if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"), |
| 1460 | deviceTypeString)) { |
| 1461 | if (deviceTypeString == "touchPad") { |
| 1462 | mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD; |
| 1463 | } else if (deviceTypeString != "touchScreen") { |
| 1464 | LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string()); |
| 1465 | } |
| 1466 | } |
| 1467 | bool isTouchScreen = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
| 1468 | |
| 1469 | mParameters.orientationAware = isTouchScreen; |
| 1470 | getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"), |
| 1471 | mParameters.orientationAware); |
| 1472 | |
| 1473 | mParameters.associatedDisplayId = mParameters.orientationAware || isTouchScreen ? 0 : -1; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1474 | } |
| 1475 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1476 | void TouchInputMapper::dumpParameters(String8& dump) { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1477 | dump.append(INDENT3 "Parameters:\n"); |
| 1478 | |
| 1479 | switch (mParameters.deviceType) { |
| 1480 | case Parameters::DEVICE_TYPE_TOUCH_SCREEN: |
| 1481 | dump.append(INDENT4 "DeviceType: touchScreen\n"); |
| 1482 | break; |
| 1483 | case Parameters::DEVICE_TYPE_TOUCH_PAD: |
| 1484 | dump.append(INDENT4 "DeviceType: touchPad\n"); |
| 1485 | break; |
| 1486 | default: |
| 1487 | assert(false); |
| 1488 | } |
| 1489 | |
| 1490 | dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n", |
| 1491 | mParameters.associatedDisplayId); |
| 1492 | dump.appendFormat(INDENT4 "OrientationAware: %s\n", |
| 1493 | toString(mParameters.orientationAware)); |
| 1494 | |
| 1495 | dump.appendFormat(INDENT4 "UseBadTouchFilter: %s\n", |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1496 | toString(mParameters.useBadTouchFilter)); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1497 | dump.appendFormat(INDENT4 "UseAveragingTouchFilter: %s\n", |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1498 | toString(mParameters.useAveragingTouchFilter)); |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1499 | dump.appendFormat(INDENT4 "UseJumpyTouchFilter: %s\n", |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1500 | toString(mParameters.useJumpyTouchFilter)); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1501 | } |
| 1502 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1503 | void TouchInputMapper::configureRawAxes() { |
| 1504 | mRawAxes.x.clear(); |
| 1505 | mRawAxes.y.clear(); |
| 1506 | mRawAxes.pressure.clear(); |
| 1507 | mRawAxes.touchMajor.clear(); |
| 1508 | mRawAxes.touchMinor.clear(); |
| 1509 | mRawAxes.toolMajor.clear(); |
| 1510 | mRawAxes.toolMinor.clear(); |
| 1511 | mRawAxes.orientation.clear(); |
| 1512 | } |
| 1513 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1514 | static void dumpAxisInfo(String8& dump, RawAbsoluteAxisInfo axis, const char* name) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1515 | if (axis.valid) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1516 | dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d\n", |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1517 | name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz); |
| 1518 | } else { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1519 | dump.appendFormat(INDENT4 "%s: unknown range\n", name); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1520 | } |
| 1521 | } |
| 1522 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1523 | void TouchInputMapper::dumpRawAxes(String8& dump) { |
| 1524 | dump.append(INDENT3 "Raw Axes:\n"); |
| 1525 | dumpAxisInfo(dump, mRawAxes.x, "X"); |
| 1526 | dumpAxisInfo(dump, mRawAxes.y, "Y"); |
| 1527 | dumpAxisInfo(dump, mRawAxes.pressure, "Pressure"); |
| 1528 | dumpAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor"); |
| 1529 | dumpAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor"); |
| 1530 | dumpAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor"); |
| 1531 | dumpAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor"); |
| 1532 | dumpAxisInfo(dump, mRawAxes.orientation, "Orientation"); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1533 | } |
| 1534 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1535 | bool TouchInputMapper::configureSurfaceLocked() { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1536 | // Update orientation and dimensions if needed. |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1537 | int32_t orientation = DISPLAY_ORIENTATION_0; |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1538 | int32_t width = mRawAxes.x.getRange(); |
| 1539 | int32_t height = mRawAxes.y.getRange(); |
| 1540 | |
| 1541 | if (mParameters.associatedDisplayId >= 0) { |
| 1542 | bool wantSize = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN; |
| 1543 | bool wantOrientation = mParameters.orientationAware; |
| 1544 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1545 | // Note: getDisplayInfo is non-reentrant so we can continue holding the lock. |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1546 | if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId, |
| 1547 | wantSize ? &width : NULL, wantSize ? &height : NULL, |
| 1548 | wantOrientation ? &orientation : NULL)) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1549 | return false; |
| 1550 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1551 | } |
| 1552 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1553 | bool orientationChanged = mLocked.surfaceOrientation != orientation; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1554 | if (orientationChanged) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1555 | mLocked.surfaceOrientation = orientation; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1556 | } |
| 1557 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1558 | bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1559 | if (sizeChanged) { |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1560 | LOGI("Device reconfigured: id=%d, name='%s', display size is now %dx%d", |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1561 | getDeviceId(), getDeviceName().string(), width, height); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1562 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1563 | mLocked.surfaceWidth = width; |
| 1564 | mLocked.surfaceHeight = height; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1565 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1566 | // Configure X and Y factors. |
| 1567 | if (mRawAxes.x.valid && mRawAxes.y.valid) { |
Jeff Brown | 511ee5f | 2010-10-18 13:32:20 -0700 | [diff] [blame] | 1568 | mLocked.xOrigin = mCalibration.haveXOrigin |
| 1569 | ? mCalibration.xOrigin |
| 1570 | : mRawAxes.x.minValue; |
| 1571 | mLocked.yOrigin = mCalibration.haveYOrigin |
| 1572 | ? mCalibration.yOrigin |
| 1573 | : mRawAxes.y.minValue; |
| 1574 | mLocked.xScale = mCalibration.haveXScale |
| 1575 | ? mCalibration.xScale |
| 1576 | : float(width) / mRawAxes.x.getRange(); |
| 1577 | mLocked.yScale = mCalibration.haveYScale |
| 1578 | ? mCalibration.yScale |
| 1579 | : float(height) / mRawAxes.y.getRange(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1580 | mLocked.xPrecision = 1.0f / mLocked.xScale; |
| 1581 | mLocked.yPrecision = 1.0f / mLocked.yScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1582 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1583 | configureVirtualKeysLocked(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1584 | } else { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1585 | LOGW(INDENT "Touch device did not report support for X or Y axis!"); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1586 | mLocked.xOrigin = 0; |
| 1587 | mLocked.yOrigin = 0; |
| 1588 | mLocked.xScale = 1.0f; |
| 1589 | mLocked.yScale = 1.0f; |
| 1590 | mLocked.xPrecision = 1.0f; |
| 1591 | mLocked.yPrecision = 1.0f; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1592 | } |
| 1593 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1594 | // Scale factor for terms that are not oriented in a particular axis. |
| 1595 | // If the pixels are square then xScale == yScale otherwise we fake it |
| 1596 | // by choosing an average. |
| 1597 | mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1598 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1599 | // Size of diagonal axis. |
| 1600 | float diagonalSize = pythag(width, height); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1601 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1602 | // TouchMajor and TouchMinor factors. |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1603 | if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) { |
| 1604 | mLocked.orientedRanges.haveTouchSize = true; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1605 | mLocked.orientedRanges.touchMajor.min = 0; |
| 1606 | mLocked.orientedRanges.touchMajor.max = diagonalSize; |
| 1607 | mLocked.orientedRanges.touchMajor.flat = 0; |
| 1608 | mLocked.orientedRanges.touchMajor.fuzz = 0; |
| 1609 | mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor; |
| 1610 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1611 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1612 | // ToolMajor and ToolMinor factors. |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1613 | mLocked.toolSizeLinearScale = 0; |
| 1614 | mLocked.toolSizeLinearBias = 0; |
| 1615 | mLocked.toolSizeAreaScale = 0; |
| 1616 | mLocked.toolSizeAreaBias = 0; |
| 1617 | if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) { |
| 1618 | if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) { |
| 1619 | if (mCalibration.haveToolSizeLinearScale) { |
| 1620 | mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1621 | } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1622 | mLocked.toolSizeLinearScale = float(min(width, height)) |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1623 | / mRawAxes.toolMajor.maxValue; |
| 1624 | } |
| 1625 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1626 | if (mCalibration.haveToolSizeLinearBias) { |
| 1627 | mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias; |
| 1628 | } |
| 1629 | } else if (mCalibration.toolSizeCalibration == |
| 1630 | Calibration::TOOL_SIZE_CALIBRATION_AREA) { |
| 1631 | if (mCalibration.haveToolSizeLinearScale) { |
| 1632 | mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale; |
| 1633 | } else { |
| 1634 | mLocked.toolSizeLinearScale = min(width, height); |
| 1635 | } |
| 1636 | |
| 1637 | if (mCalibration.haveToolSizeLinearBias) { |
| 1638 | mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias; |
| 1639 | } |
| 1640 | |
| 1641 | if (mCalibration.haveToolSizeAreaScale) { |
| 1642 | mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale; |
| 1643 | } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) { |
| 1644 | mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue; |
| 1645 | } |
| 1646 | |
| 1647 | if (mCalibration.haveToolSizeAreaBias) { |
| 1648 | mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1649 | } |
| 1650 | } |
| 1651 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1652 | mLocked.orientedRanges.haveToolSize = true; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1653 | mLocked.orientedRanges.toolMajor.min = 0; |
| 1654 | mLocked.orientedRanges.toolMajor.max = diagonalSize; |
| 1655 | mLocked.orientedRanges.toolMajor.flat = 0; |
| 1656 | mLocked.orientedRanges.toolMajor.fuzz = 0; |
| 1657 | mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor; |
| 1658 | } |
| 1659 | |
| 1660 | // Pressure factors. |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1661 | mLocked.pressureScale = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1662 | if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) { |
| 1663 | RawAbsoluteAxisInfo rawPressureAxis; |
| 1664 | switch (mCalibration.pressureSource) { |
| 1665 | case Calibration::PRESSURE_SOURCE_PRESSURE: |
| 1666 | rawPressureAxis = mRawAxes.pressure; |
| 1667 | break; |
| 1668 | case Calibration::PRESSURE_SOURCE_TOUCH: |
| 1669 | rawPressureAxis = mRawAxes.touchMajor; |
| 1670 | break; |
| 1671 | default: |
| 1672 | rawPressureAxis.clear(); |
| 1673 | } |
| 1674 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1675 | if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL |
| 1676 | || mCalibration.pressureCalibration |
| 1677 | == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) { |
| 1678 | if (mCalibration.havePressureScale) { |
| 1679 | mLocked.pressureScale = mCalibration.pressureScale; |
| 1680 | } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) { |
| 1681 | mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue; |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | mLocked.orientedRanges.havePressure = true; |
| 1686 | mLocked.orientedRanges.pressure.min = 0; |
| 1687 | mLocked.orientedRanges.pressure.max = 1.0; |
| 1688 | mLocked.orientedRanges.pressure.flat = 0; |
| 1689 | mLocked.orientedRanges.pressure.fuzz = 0; |
| 1690 | } |
| 1691 | |
| 1692 | // Size factors. |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1693 | mLocked.sizeScale = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1694 | if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1695 | if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) { |
| 1696 | if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) { |
| 1697 | mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue; |
| 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | mLocked.orientedRanges.haveSize = true; |
| 1702 | mLocked.orientedRanges.size.min = 0; |
| 1703 | mLocked.orientedRanges.size.max = 1.0; |
| 1704 | mLocked.orientedRanges.size.flat = 0; |
| 1705 | mLocked.orientedRanges.size.fuzz = 0; |
| 1706 | } |
| 1707 | |
| 1708 | // Orientation |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1709 | mLocked.orientationScale = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1710 | if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1711 | if (mCalibration.orientationCalibration |
| 1712 | == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) { |
| 1713 | if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) { |
| 1714 | mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue; |
| 1715 | } |
| 1716 | } |
| 1717 | |
| 1718 | mLocked.orientedRanges.orientation.min = - M_PI_2; |
| 1719 | mLocked.orientedRanges.orientation.max = M_PI_2; |
| 1720 | mLocked.orientedRanges.orientation.flat = 0; |
| 1721 | mLocked.orientedRanges.orientation.fuzz = 0; |
| 1722 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | if (orientationChanged || sizeChanged) { |
| 1726 | // Compute oriented surface dimensions, precision, and scales. |
| 1727 | float orientedXScale, orientedYScale; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1728 | switch (mLocked.surfaceOrientation) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1729 | case DISPLAY_ORIENTATION_90: |
| 1730 | case DISPLAY_ORIENTATION_270: |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1731 | mLocked.orientedSurfaceWidth = mLocked.surfaceHeight; |
| 1732 | mLocked.orientedSurfaceHeight = mLocked.surfaceWidth; |
| 1733 | mLocked.orientedXPrecision = mLocked.yPrecision; |
| 1734 | mLocked.orientedYPrecision = mLocked.xPrecision; |
| 1735 | orientedXScale = mLocked.yScale; |
| 1736 | orientedYScale = mLocked.xScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1737 | break; |
| 1738 | default: |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1739 | mLocked.orientedSurfaceWidth = mLocked.surfaceWidth; |
| 1740 | mLocked.orientedSurfaceHeight = mLocked.surfaceHeight; |
| 1741 | mLocked.orientedXPrecision = mLocked.xPrecision; |
| 1742 | mLocked.orientedYPrecision = mLocked.yPrecision; |
| 1743 | orientedXScale = mLocked.xScale; |
| 1744 | orientedYScale = mLocked.yScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1745 | break; |
| 1746 | } |
| 1747 | |
| 1748 | // Configure position ranges. |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1749 | mLocked.orientedRanges.x.min = 0; |
| 1750 | mLocked.orientedRanges.x.max = mLocked.orientedSurfaceWidth; |
| 1751 | mLocked.orientedRanges.x.flat = 0; |
| 1752 | mLocked.orientedRanges.x.fuzz = orientedXScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1753 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1754 | mLocked.orientedRanges.y.min = 0; |
| 1755 | mLocked.orientedRanges.y.max = mLocked.orientedSurfaceHeight; |
| 1756 | mLocked.orientedRanges.y.flat = 0; |
| 1757 | mLocked.orientedRanges.y.fuzz = orientedYScale; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | return true; |
| 1761 | } |
| 1762 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1763 | void TouchInputMapper::dumpSurfaceLocked(String8& dump) { |
| 1764 | dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth); |
| 1765 | dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight); |
| 1766 | dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1767 | } |
| 1768 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1769 | void TouchInputMapper::configureVirtualKeysLocked() { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1770 | assert(mRawAxes.x.valid && mRawAxes.y.valid); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1771 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1772 | Vector<VirtualKeyDefinition> virtualKeyDefinitions; |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 1773 | getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1774 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1775 | mLocked.virtualKeys.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1776 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1777 | if (virtualKeyDefinitions.size() == 0) { |
| 1778 | return; |
| 1779 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1780 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1781 | mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size()); |
| 1782 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1783 | int32_t touchScreenLeft = mRawAxes.x.minValue; |
| 1784 | int32_t touchScreenTop = mRawAxes.y.minValue; |
| 1785 | int32_t touchScreenWidth = mRawAxes.x.getRange(); |
| 1786 | int32_t touchScreenHeight = mRawAxes.y.getRange(); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1787 | |
| 1788 | for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1789 | const VirtualKeyDefinition& virtualKeyDefinition = |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1790 | virtualKeyDefinitions[i]; |
| 1791 | |
| 1792 | mLocked.virtualKeys.add(); |
| 1793 | VirtualKey& virtualKey = mLocked.virtualKeys.editTop(); |
| 1794 | |
| 1795 | virtualKey.scanCode = virtualKeyDefinition.scanCode; |
| 1796 | int32_t keyCode; |
| 1797 | uint32_t flags; |
| 1798 | if (getEventHub()->scancodeToKeycode(getDeviceId(), virtualKey.scanCode, |
| 1799 | & keyCode, & flags)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1800 | LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring", |
| 1801 | virtualKey.scanCode); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1802 | mLocked.virtualKeys.pop(); // drop the key |
| 1803 | continue; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1804 | } |
| 1805 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1806 | virtualKey.keyCode = keyCode; |
| 1807 | virtualKey.flags = flags; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1808 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1809 | // convert the key definition's display coordinates into touch coordinates for a hit box |
| 1810 | int32_t halfWidth = virtualKeyDefinition.width / 2; |
| 1811 | int32_t halfHeight = virtualKeyDefinition.height / 2; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1812 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1813 | virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth) |
| 1814 | * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft; |
| 1815 | virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth) |
| 1816 | * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft; |
| 1817 | virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight) |
| 1818 | * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop; |
| 1819 | virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight) |
| 1820 | * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1821 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 1822 | } |
| 1823 | } |
| 1824 | |
| 1825 | void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) { |
| 1826 | if (!mLocked.virtualKeys.isEmpty()) { |
| 1827 | dump.append(INDENT3 "Virtual Keys:\n"); |
| 1828 | |
| 1829 | for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) { |
| 1830 | const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i); |
| 1831 | dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, " |
| 1832 | "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n", |
| 1833 | i, virtualKey.scanCode, virtualKey.keyCode, |
| 1834 | virtualKey.hitLeft, virtualKey.hitRight, |
| 1835 | virtualKey.hitTop, virtualKey.hitBottom); |
| 1836 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 1837 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 1838 | } |
| 1839 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1840 | void TouchInputMapper::parseCalibration() { |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 1841 | const PropertyMap& in = getDevice()->getConfiguration(); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1842 | Calibration& out = mCalibration; |
| 1843 | |
Jeff Brown | 511ee5f | 2010-10-18 13:32:20 -0700 | [diff] [blame] | 1844 | // Position |
| 1845 | out.haveXOrigin = in.tryGetProperty(String8("touch.position.xOrigin"), out.xOrigin); |
| 1846 | out.haveYOrigin = in.tryGetProperty(String8("touch.position.yOrigin"), out.yOrigin); |
| 1847 | out.haveXScale = in.tryGetProperty(String8("touch.position.xScale"), out.xScale); |
| 1848 | out.haveYScale = in.tryGetProperty(String8("touch.position.yScale"), out.yScale); |
| 1849 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1850 | // Touch Size |
| 1851 | out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT; |
| 1852 | String8 touchSizeCalibrationString; |
| 1853 | if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) { |
| 1854 | if (touchSizeCalibrationString == "none") { |
| 1855 | out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE; |
| 1856 | } else if (touchSizeCalibrationString == "geometric") { |
| 1857 | out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC; |
| 1858 | } else if (touchSizeCalibrationString == "pressure") { |
| 1859 | out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE; |
| 1860 | } else if (touchSizeCalibrationString != "default") { |
| 1861 | LOGW("Invalid value for touch.touchSize.calibration: '%s'", |
| 1862 | touchSizeCalibrationString.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1863 | } |
| 1864 | } |
| 1865 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1866 | // Tool Size |
| 1867 | out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT; |
| 1868 | String8 toolSizeCalibrationString; |
| 1869 | if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) { |
| 1870 | if (toolSizeCalibrationString == "none") { |
| 1871 | out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE; |
| 1872 | } else if (toolSizeCalibrationString == "geometric") { |
| 1873 | out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC; |
| 1874 | } else if (toolSizeCalibrationString == "linear") { |
| 1875 | out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR; |
| 1876 | } else if (toolSizeCalibrationString == "area") { |
| 1877 | out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA; |
| 1878 | } else if (toolSizeCalibrationString != "default") { |
| 1879 | LOGW("Invalid value for touch.toolSize.calibration: '%s'", |
| 1880 | toolSizeCalibrationString.string()); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1881 | } |
| 1882 | } |
| 1883 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1884 | out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"), |
| 1885 | out.toolSizeLinearScale); |
| 1886 | out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"), |
| 1887 | out.toolSizeLinearBias); |
| 1888 | out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"), |
| 1889 | out.toolSizeAreaScale); |
| 1890 | out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"), |
| 1891 | out.toolSizeAreaBias); |
| 1892 | out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"), |
| 1893 | out.toolSizeIsSummed); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1894 | |
| 1895 | // Pressure |
| 1896 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT; |
| 1897 | String8 pressureCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1898 | if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1899 | if (pressureCalibrationString == "none") { |
| 1900 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; |
| 1901 | } else if (pressureCalibrationString == "physical") { |
| 1902 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL; |
| 1903 | } else if (pressureCalibrationString == "amplitude") { |
| 1904 | out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE; |
| 1905 | } else if (pressureCalibrationString != "default") { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1906 | LOGW("Invalid value for touch.pressure.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1907 | pressureCalibrationString.string()); |
| 1908 | } |
| 1909 | } |
| 1910 | |
| 1911 | out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT; |
| 1912 | String8 pressureSourceString; |
| 1913 | if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) { |
| 1914 | if (pressureSourceString == "pressure") { |
| 1915 | out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE; |
| 1916 | } else if (pressureSourceString == "touch") { |
| 1917 | out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH; |
| 1918 | } else if (pressureSourceString != "default") { |
| 1919 | LOGW("Invalid value for touch.pressure.source: '%s'", |
| 1920 | pressureSourceString.string()); |
| 1921 | } |
| 1922 | } |
| 1923 | |
| 1924 | out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"), |
| 1925 | out.pressureScale); |
| 1926 | |
| 1927 | // Size |
| 1928 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT; |
| 1929 | String8 sizeCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1930 | if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1931 | if (sizeCalibrationString == "none") { |
| 1932 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; |
| 1933 | } else if (sizeCalibrationString == "normalized") { |
| 1934 | out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED; |
| 1935 | } else if (sizeCalibrationString != "default") { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1936 | LOGW("Invalid value for touch.size.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1937 | sizeCalibrationString.string()); |
| 1938 | } |
| 1939 | } |
| 1940 | |
| 1941 | // Orientation |
| 1942 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT; |
| 1943 | String8 orientationCalibrationString; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1944 | if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1945 | if (orientationCalibrationString == "none") { |
| 1946 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; |
| 1947 | } else if (orientationCalibrationString == "interpolated") { |
| 1948 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 1949 | } else if (orientationCalibrationString == "vector") { |
| 1950 | out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1951 | } else if (orientationCalibrationString != "default") { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 1952 | LOGW("Invalid value for touch.orientation.calibration: '%s'", |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 1953 | orientationCalibrationString.string()); |
| 1954 | } |
| 1955 | } |
| 1956 | } |
| 1957 | |
| 1958 | void TouchInputMapper::resolveCalibration() { |
| 1959 | // Pressure |
| 1960 | switch (mCalibration.pressureSource) { |
| 1961 | case Calibration::PRESSURE_SOURCE_DEFAULT: |
| 1962 | if (mRawAxes.pressure.valid) { |
| 1963 | mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE; |
| 1964 | } else if (mRawAxes.touchMajor.valid) { |
| 1965 | mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH; |
| 1966 | } |
| 1967 | break; |
| 1968 | |
| 1969 | case Calibration::PRESSURE_SOURCE_PRESSURE: |
| 1970 | if (! mRawAxes.pressure.valid) { |
| 1971 | LOGW("Calibration property touch.pressure.source is 'pressure' but " |
| 1972 | "the pressure axis is not available."); |
| 1973 | } |
| 1974 | break; |
| 1975 | |
| 1976 | case Calibration::PRESSURE_SOURCE_TOUCH: |
| 1977 | if (! mRawAxes.touchMajor.valid) { |
| 1978 | LOGW("Calibration property touch.pressure.source is 'touch' but " |
| 1979 | "the touchMajor axis is not available."); |
| 1980 | } |
| 1981 | break; |
| 1982 | |
| 1983 | default: |
| 1984 | break; |
| 1985 | } |
| 1986 | |
| 1987 | switch (mCalibration.pressureCalibration) { |
| 1988 | case Calibration::PRESSURE_CALIBRATION_DEFAULT: |
| 1989 | if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) { |
| 1990 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE; |
| 1991 | } else { |
| 1992 | mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE; |
| 1993 | } |
| 1994 | break; |
| 1995 | |
| 1996 | default: |
| 1997 | break; |
| 1998 | } |
| 1999 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2000 | // Tool Size |
| 2001 | switch (mCalibration.toolSizeCalibration) { |
| 2002 | case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT: |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2003 | if (mRawAxes.toolMajor.valid) { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2004 | mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2005 | } else { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2006 | mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2007 | } |
| 2008 | break; |
| 2009 | |
| 2010 | default: |
| 2011 | break; |
| 2012 | } |
| 2013 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2014 | // Touch Size |
| 2015 | switch (mCalibration.touchSizeCalibration) { |
| 2016 | case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT: |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2017 | if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2018 | && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) { |
| 2019 | mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2020 | } else { |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2021 | mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2022 | } |
| 2023 | break; |
| 2024 | |
| 2025 | default: |
| 2026 | break; |
| 2027 | } |
| 2028 | |
| 2029 | // Size |
| 2030 | switch (mCalibration.sizeCalibration) { |
| 2031 | case Calibration::SIZE_CALIBRATION_DEFAULT: |
| 2032 | if (mRawAxes.toolMajor.valid) { |
| 2033 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED; |
| 2034 | } else { |
| 2035 | mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE; |
| 2036 | } |
| 2037 | break; |
| 2038 | |
| 2039 | default: |
| 2040 | break; |
| 2041 | } |
| 2042 | |
| 2043 | // Orientation |
| 2044 | switch (mCalibration.orientationCalibration) { |
| 2045 | case Calibration::ORIENTATION_CALIBRATION_DEFAULT: |
| 2046 | if (mRawAxes.orientation.valid) { |
| 2047 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED; |
| 2048 | } else { |
| 2049 | mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE; |
| 2050 | } |
| 2051 | break; |
| 2052 | |
| 2053 | default: |
| 2054 | break; |
| 2055 | } |
| 2056 | } |
| 2057 | |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2058 | void TouchInputMapper::dumpCalibration(String8& dump) { |
| 2059 | dump.append(INDENT3 "Calibration:\n"); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2060 | |
Jeff Brown | 511ee5f | 2010-10-18 13:32:20 -0700 | [diff] [blame] | 2061 | // Position |
| 2062 | if (mCalibration.haveXOrigin) { |
| 2063 | dump.appendFormat(INDENT4 "touch.position.xOrigin: %d\n", mCalibration.xOrigin); |
| 2064 | } |
| 2065 | if (mCalibration.haveYOrigin) { |
| 2066 | dump.appendFormat(INDENT4 "touch.position.yOrigin: %d\n", mCalibration.yOrigin); |
| 2067 | } |
| 2068 | if (mCalibration.haveXScale) { |
| 2069 | dump.appendFormat(INDENT4 "touch.position.xScale: %0.3f\n", mCalibration.xScale); |
| 2070 | } |
| 2071 | if (mCalibration.haveYScale) { |
| 2072 | dump.appendFormat(INDENT4 "touch.position.yScale: %0.3f\n", mCalibration.yScale); |
| 2073 | } |
| 2074 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2075 | // Touch Size |
| 2076 | switch (mCalibration.touchSizeCalibration) { |
| 2077 | case Calibration::TOUCH_SIZE_CALIBRATION_NONE: |
| 2078 | dump.append(INDENT4 "touch.touchSize.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2079 | break; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2080 | case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC: |
| 2081 | dump.append(INDENT4 "touch.touchSize.calibration: geometric\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2082 | break; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2083 | case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE: |
| 2084 | dump.append(INDENT4 "touch.touchSize.calibration: pressure\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2085 | break; |
| 2086 | default: |
| 2087 | assert(false); |
| 2088 | } |
| 2089 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2090 | // Tool Size |
| 2091 | switch (mCalibration.toolSizeCalibration) { |
| 2092 | case Calibration::TOOL_SIZE_CALIBRATION_NONE: |
| 2093 | dump.append(INDENT4 "touch.toolSize.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2094 | break; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2095 | case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC: |
| 2096 | dump.append(INDENT4 "touch.toolSize.calibration: geometric\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2097 | break; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2098 | case Calibration::TOOL_SIZE_CALIBRATION_LINEAR: |
| 2099 | dump.append(INDENT4 "touch.toolSize.calibration: linear\n"); |
| 2100 | break; |
| 2101 | case Calibration::TOOL_SIZE_CALIBRATION_AREA: |
| 2102 | dump.append(INDENT4 "touch.toolSize.calibration: area\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2103 | break; |
| 2104 | default: |
| 2105 | assert(false); |
| 2106 | } |
| 2107 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2108 | if (mCalibration.haveToolSizeLinearScale) { |
| 2109 | dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n", |
| 2110 | mCalibration.toolSizeLinearScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2111 | } |
| 2112 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2113 | if (mCalibration.haveToolSizeLinearBias) { |
| 2114 | dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n", |
| 2115 | mCalibration.toolSizeLinearBias); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2116 | } |
| 2117 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2118 | if (mCalibration.haveToolSizeAreaScale) { |
| 2119 | dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n", |
| 2120 | mCalibration.toolSizeAreaScale); |
| 2121 | } |
| 2122 | |
| 2123 | if (mCalibration.haveToolSizeAreaBias) { |
| 2124 | dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n", |
| 2125 | mCalibration.toolSizeAreaBias); |
| 2126 | } |
| 2127 | |
| 2128 | if (mCalibration.haveToolSizeIsSummed) { |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 2129 | dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %s\n", |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 2130 | toString(mCalibration.toolSizeIsSummed)); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2131 | } |
| 2132 | |
| 2133 | // Pressure |
| 2134 | switch (mCalibration.pressureCalibration) { |
| 2135 | case Calibration::PRESSURE_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2136 | dump.append(INDENT4 "touch.pressure.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2137 | break; |
| 2138 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2139 | dump.append(INDENT4 "touch.pressure.calibration: physical\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2140 | break; |
| 2141 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2142 | dump.append(INDENT4 "touch.pressure.calibration: amplitude\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2143 | break; |
| 2144 | default: |
| 2145 | assert(false); |
| 2146 | } |
| 2147 | |
| 2148 | switch (mCalibration.pressureSource) { |
| 2149 | case Calibration::PRESSURE_SOURCE_PRESSURE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2150 | dump.append(INDENT4 "touch.pressure.source: pressure\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2151 | break; |
| 2152 | case Calibration::PRESSURE_SOURCE_TOUCH: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2153 | dump.append(INDENT4 "touch.pressure.source: touch\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2154 | break; |
| 2155 | case Calibration::PRESSURE_SOURCE_DEFAULT: |
| 2156 | break; |
| 2157 | default: |
| 2158 | assert(false); |
| 2159 | } |
| 2160 | |
| 2161 | if (mCalibration.havePressureScale) { |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2162 | dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n", |
| 2163 | mCalibration.pressureScale); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2164 | } |
| 2165 | |
| 2166 | // Size |
| 2167 | switch (mCalibration.sizeCalibration) { |
| 2168 | case Calibration::SIZE_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2169 | dump.append(INDENT4 "touch.size.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2170 | break; |
| 2171 | case Calibration::SIZE_CALIBRATION_NORMALIZED: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2172 | dump.append(INDENT4 "touch.size.calibration: normalized\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2173 | break; |
| 2174 | default: |
| 2175 | assert(false); |
| 2176 | } |
| 2177 | |
| 2178 | // Orientation |
| 2179 | switch (mCalibration.orientationCalibration) { |
| 2180 | case Calibration::ORIENTATION_CALIBRATION_NONE: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2181 | dump.append(INDENT4 "touch.orientation.calibration: none\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2182 | break; |
| 2183 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: |
Jeff Brown | ef3d7e8 | 2010-09-30 14:33:04 -0700 | [diff] [blame] | 2184 | dump.append(INDENT4 "touch.orientation.calibration: interpolated\n"); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2185 | break; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 2186 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: |
| 2187 | dump.append(INDENT4 "touch.orientation.calibration: vector\n"); |
| 2188 | break; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2189 | default: |
| 2190 | assert(false); |
| 2191 | } |
| 2192 | } |
| 2193 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2194 | void TouchInputMapper::reset() { |
| 2195 | // Synthesize touch up event if touch is currently down. |
| 2196 | // This will also take care of finishing virtual key processing if needed. |
| 2197 | if (mLastTouch.pointerCount != 0) { |
| 2198 | nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC); |
| 2199 | mCurrentTouch.clear(); |
| 2200 | syncTouch(when, true); |
| 2201 | } |
| 2202 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2203 | { // acquire lock |
| 2204 | AutoMutex _l(mLock); |
| 2205 | initializeLocked(); |
| 2206 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2207 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2208 | InputMapper::reset(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2209 | } |
| 2210 | |
| 2211 | void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2212 | uint32_t policyFlags = 0; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2213 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2214 | // Preprocess pointer data. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2215 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2216 | if (mParameters.useBadTouchFilter) { |
| 2217 | if (applyBadTouchFilter()) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2218 | havePointerIds = false; |
| 2219 | } |
| 2220 | } |
| 2221 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2222 | if (mParameters.useJumpyTouchFilter) { |
| 2223 | if (applyJumpyTouchFilter()) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2224 | havePointerIds = false; |
| 2225 | } |
| 2226 | } |
| 2227 | |
| 2228 | if (! havePointerIds) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2229 | calculatePointerIds(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2230 | } |
| 2231 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2232 | TouchData temp; |
| 2233 | TouchData* savedTouch; |
| 2234 | if (mParameters.useAveragingTouchFilter) { |
| 2235 | temp.copyFrom(mCurrentTouch); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2236 | savedTouch = & temp; |
| 2237 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2238 | applyAveragingTouchFilter(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2239 | } else { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2240 | savedTouch = & mCurrentTouch; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2241 | } |
| 2242 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2243 | // Process touches and virtual keys. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2244 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2245 | TouchResult touchResult = consumeOffScreenTouches(when, policyFlags); |
| 2246 | if (touchResult == DISPATCH_TOUCH) { |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 2247 | detectGestures(when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2248 | dispatchTouches(when, policyFlags); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2249 | } |
| 2250 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2251 | // Copy current touch to last touch in preparation for the next cycle. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2252 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2253 | if (touchResult == DROP_STROKE) { |
| 2254 | mLastTouch.clear(); |
| 2255 | } else { |
| 2256 | mLastTouch.copyFrom(*savedTouch); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2257 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2258 | } |
| 2259 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2260 | TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches( |
| 2261 | nsecs_t when, uint32_t policyFlags) { |
| 2262 | int32_t keyEventAction, keyEventFlags; |
| 2263 | int32_t keyCode, scanCode, downTime; |
| 2264 | TouchResult touchResult; |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 2265 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2266 | { // acquire lock |
| 2267 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2268 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2269 | // Update surface size and orientation, including virtual key positions. |
| 2270 | if (! configureSurfaceLocked()) { |
| 2271 | return DROP_STROKE; |
| 2272 | } |
| 2273 | |
| 2274 | // Check for virtual key press. |
| 2275 | if (mLocked.currentVirtualKey.down) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2276 | if (mCurrentTouch.pointerCount == 0) { |
| 2277 | // Pointer went up while virtual key was down. |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2278 | mLocked.currentVirtualKey.down = false; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2279 | #if DEBUG_VIRTUAL_KEYS |
| 2280 | LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d", |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2281 | mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2282 | #endif |
| 2283 | keyEventAction = AKEY_EVENT_ACTION_UP; |
| 2284 | keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 2285 | touchResult = SKIP_TOUCH; |
| 2286 | goto DispatchVirtualKey; |
| 2287 | } |
| 2288 | |
| 2289 | if (mCurrentTouch.pointerCount == 1) { |
| 2290 | int32_t x = mCurrentTouch.pointers[0].x; |
| 2291 | int32_t y = mCurrentTouch.pointers[0].y; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2292 | const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y); |
| 2293 | if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2294 | // Pointer is still within the space of the virtual key. |
| 2295 | return SKIP_TOUCH; |
| 2296 | } |
| 2297 | } |
| 2298 | |
| 2299 | // Pointer left virtual key area or another pointer also went down. |
| 2300 | // Send key cancellation and drop the stroke so subsequent motions will be |
| 2301 | // considered fresh downs. This is useful when the user swipes away from the |
| 2302 | // virtual key area into the main display surface. |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2303 | mLocked.currentVirtualKey.down = false; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2304 | #if DEBUG_VIRTUAL_KEYS |
| 2305 | LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d", |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2306 | mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2307 | #endif |
| 2308 | keyEventAction = AKEY_EVENT_ACTION_UP; |
| 2309 | keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY |
| 2310 | | AKEY_EVENT_FLAG_CANCELED; |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2311 | |
| 2312 | // Check whether the pointer moved inside the display area where we should |
| 2313 | // start a new stroke. |
| 2314 | int32_t x = mCurrentTouch.pointers[0].x; |
| 2315 | int32_t y = mCurrentTouch.pointers[0].y; |
| 2316 | if (isPointInsideSurfaceLocked(x, y)) { |
| 2317 | mLastTouch.clear(); |
| 2318 | touchResult = DISPATCH_TOUCH; |
| 2319 | } else { |
| 2320 | touchResult = DROP_STROKE; |
| 2321 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2322 | } else { |
| 2323 | if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) { |
| 2324 | // Pointer just went down. Handle off-screen touches, if needed. |
| 2325 | int32_t x = mCurrentTouch.pointers[0].x; |
| 2326 | int32_t y = mCurrentTouch.pointers[0].y; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2327 | if (! isPointInsideSurfaceLocked(x, y)) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2328 | // If exactly one pointer went down, check for virtual key hit. |
| 2329 | // Otherwise we will drop the entire stroke. |
| 2330 | if (mCurrentTouch.pointerCount == 1) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2331 | const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2332 | if (virtualKey) { |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 2333 | if (mContext->shouldDropVirtualKey(when, getDevice(), |
| 2334 | virtualKey->keyCode, virtualKey->scanCode)) { |
| 2335 | return DROP_STROKE; |
| 2336 | } |
| 2337 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2338 | mLocked.currentVirtualKey.down = true; |
| 2339 | mLocked.currentVirtualKey.downTime = when; |
| 2340 | mLocked.currentVirtualKey.keyCode = virtualKey->keyCode; |
| 2341 | mLocked.currentVirtualKey.scanCode = virtualKey->scanCode; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2342 | #if DEBUG_VIRTUAL_KEYS |
| 2343 | LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d", |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2344 | mLocked.currentVirtualKey.keyCode, |
| 2345 | mLocked.currentVirtualKey.scanCode); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2346 | #endif |
| 2347 | keyEventAction = AKEY_EVENT_ACTION_DOWN; |
| 2348 | keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM |
| 2349 | | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 2350 | touchResult = SKIP_TOUCH; |
| 2351 | goto DispatchVirtualKey; |
| 2352 | } |
| 2353 | } |
| 2354 | return DROP_STROKE; |
| 2355 | } |
| 2356 | } |
| 2357 | return DISPATCH_TOUCH; |
| 2358 | } |
| 2359 | |
| 2360 | DispatchVirtualKey: |
| 2361 | // Collect remaining state needed to dispatch virtual key. |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2362 | keyCode = mLocked.currentVirtualKey.keyCode; |
| 2363 | scanCode = mLocked.currentVirtualKey.scanCode; |
| 2364 | downTime = mLocked.currentVirtualKey.downTime; |
| 2365 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2366 | |
| 2367 | // Dispatch virtual key. |
| 2368 | int32_t metaState = mContext->getGlobalMetaState(); |
Jeff Brown | 0eaf393 | 2010-10-01 14:55:30 -0700 | [diff] [blame] | 2369 | policyFlags |= POLICY_FLAG_VIRTUAL; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2370 | getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags, |
| 2371 | keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime); |
| 2372 | return touchResult; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2373 | } |
| 2374 | |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 2375 | void TouchInputMapper::detectGestures(nsecs_t when) { |
| 2376 | // Disable all virtual key touches that happen within a short time interval of the |
| 2377 | // most recent touch. The idea is to filter out stray virtual key presses when |
| 2378 | // interacting with the touch screen. |
| 2379 | // |
| 2380 | // Problems we're trying to solve: |
| 2381 | // |
| 2382 | // 1. While scrolling a list or dragging the window shade, the user swipes down into a |
| 2383 | // virtual key area that is implemented by a separate touch panel and accidentally |
| 2384 | // triggers a virtual key. |
| 2385 | // |
| 2386 | // 2. While typing in the on screen keyboard, the user taps slightly outside the screen |
| 2387 | // area and accidentally triggers a virtual key. This often happens when virtual keys |
| 2388 | // are layed out below the screen near to where the on screen keyboard's space bar |
| 2389 | // is displayed. |
| 2390 | if (mParameters.virtualKeyQuietTime > 0 && mCurrentTouch.pointerCount != 0) { |
| 2391 | mContext->disableVirtualKeysUntil(when + mParameters.virtualKeyQuietTime); |
| 2392 | } |
| 2393 | } |
| 2394 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2395 | void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) { |
| 2396 | uint32_t currentPointerCount = mCurrentTouch.pointerCount; |
| 2397 | uint32_t lastPointerCount = mLastTouch.pointerCount; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2398 | if (currentPointerCount == 0 && lastPointerCount == 0) { |
| 2399 | return; // nothing to do! |
| 2400 | } |
| 2401 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2402 | BitSet32 currentIdBits = mCurrentTouch.idBits; |
| 2403 | BitSet32 lastIdBits = mLastTouch.idBits; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2404 | |
| 2405 | if (currentIdBits == lastIdBits) { |
| 2406 | // No pointer id changes so this is a move event. |
| 2407 | // The dispatcher takes care of batching moves so we don't have to deal with that here. |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 2408 | int32_t motionEventAction = AMOTION_EVENT_ACTION_MOVE; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2409 | dispatchTouch(when, policyFlags, & mCurrentTouch, |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2410 | currentIdBits, -1, currentPointerCount, motionEventAction); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2411 | } else { |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2412 | // There may be pointers going up and pointers going down and pointers moving |
| 2413 | // all at the same time. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2414 | BitSet32 upIdBits(lastIdBits.value & ~ currentIdBits.value); |
| 2415 | BitSet32 downIdBits(currentIdBits.value & ~ lastIdBits.value); |
| 2416 | BitSet32 activeIdBits(lastIdBits.value); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2417 | uint32_t pointerCount = lastPointerCount; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2418 | |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2419 | // Produce an intermediate representation of the touch data that consists of the |
| 2420 | // old location of pointers that have just gone up and the new location of pointers that |
| 2421 | // have just moved but omits the location of pointers that have just gone down. |
| 2422 | TouchData interimTouch; |
| 2423 | interimTouch.copyFrom(mLastTouch); |
| 2424 | |
| 2425 | BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value); |
| 2426 | bool moveNeeded = false; |
| 2427 | while (!moveIdBits.isEmpty()) { |
| 2428 | uint32_t moveId = moveIdBits.firstMarkedBit(); |
| 2429 | moveIdBits.clearBit(moveId); |
| 2430 | |
| 2431 | int32_t oldIndex = mLastTouch.idToIndex[moveId]; |
| 2432 | int32_t newIndex = mCurrentTouch.idToIndex[moveId]; |
| 2433 | if (mLastTouch.pointers[oldIndex] != mCurrentTouch.pointers[newIndex]) { |
| 2434 | interimTouch.pointers[oldIndex] = mCurrentTouch.pointers[newIndex]; |
| 2435 | moveNeeded = true; |
| 2436 | } |
| 2437 | } |
| 2438 | |
| 2439 | // Dispatch pointer up events using the interim pointer locations. |
| 2440 | while (!upIdBits.isEmpty()) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2441 | uint32_t upId = upIdBits.firstMarkedBit(); |
| 2442 | upIdBits.clearBit(upId); |
| 2443 | BitSet32 oldActiveIdBits = activeIdBits; |
| 2444 | activeIdBits.clearBit(upId); |
| 2445 | |
| 2446 | int32_t motionEventAction; |
| 2447 | if (activeIdBits.isEmpty()) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 2448 | motionEventAction = AMOTION_EVENT_ACTION_UP; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2449 | } else { |
Jeff Brown | 00ba884 | 2010-07-16 15:01:56 -0700 | [diff] [blame] | 2450 | motionEventAction = AMOTION_EVENT_ACTION_POINTER_UP; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2451 | } |
| 2452 | |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2453 | dispatchTouch(when, policyFlags, &interimTouch, |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2454 | oldActiveIdBits, upId, pointerCount, motionEventAction); |
| 2455 | pointerCount -= 1; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2456 | } |
| 2457 | |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2458 | // Dispatch move events if any of the remaining pointers moved from their old locations. |
| 2459 | // Although applications receive new locations as part of individual pointer up |
| 2460 | // events, they do not generally handle them except when presented in a move event. |
| 2461 | if (moveNeeded) { |
| 2462 | dispatchTouch(when, policyFlags, &mCurrentTouch, |
| 2463 | activeIdBits, -1, pointerCount, AMOTION_EVENT_ACTION_MOVE); |
| 2464 | } |
| 2465 | |
| 2466 | // Dispatch pointer down events using the new pointer locations. |
| 2467 | while (!downIdBits.isEmpty()) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2468 | uint32_t downId = downIdBits.firstMarkedBit(); |
| 2469 | downIdBits.clearBit(downId); |
| 2470 | BitSet32 oldActiveIdBits = activeIdBits; |
| 2471 | activeIdBits.markBit(downId); |
| 2472 | |
| 2473 | int32_t motionEventAction; |
| 2474 | if (oldActiveIdBits.isEmpty()) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 2475 | motionEventAction = AMOTION_EVENT_ACTION_DOWN; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2476 | mDownTime = when; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2477 | } else { |
Jeff Brown | 00ba884 | 2010-07-16 15:01:56 -0700 | [diff] [blame] | 2478 | motionEventAction = AMOTION_EVENT_ACTION_POINTER_DOWN; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2479 | } |
| 2480 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2481 | pointerCount += 1; |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 2482 | dispatchTouch(when, policyFlags, &mCurrentTouch, |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2483 | activeIdBits, downId, pointerCount, motionEventAction); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2484 | } |
| 2485 | } |
| 2486 | } |
| 2487 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2488 | void TouchInputMapper::dispatchTouch(nsecs_t when, uint32_t policyFlags, |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2489 | TouchData* touch, BitSet32 idBits, uint32_t changedId, uint32_t pointerCount, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2490 | int32_t motionEventAction) { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2491 | int32_t pointerIds[MAX_POINTERS]; |
| 2492 | PointerCoords pointerCoords[MAX_POINTERS]; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2493 | int32_t motionEventEdgeFlags = 0; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2494 | float xPrecision, yPrecision; |
| 2495 | |
| 2496 | { // acquire lock |
| 2497 | AutoMutex _l(mLock); |
| 2498 | |
| 2499 | // Walk through the the active pointers and map touch screen coordinates (TouchData) into |
| 2500 | // display coordinates (PointerCoords) and adjust for display orientation. |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2501 | for (uint32_t outIndex = 0; ! idBits.isEmpty(); outIndex++) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2502 | uint32_t id = idBits.firstMarkedBit(); |
| 2503 | idBits.clearBit(id); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2504 | uint32_t inIndex = touch->idToIndex[id]; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2505 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2506 | const PointerData& in = touch->pointers[inIndex]; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2507 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2508 | // X and Y |
| 2509 | float x = float(in.x - mLocked.xOrigin) * mLocked.xScale; |
| 2510 | float y = float(in.y - mLocked.yOrigin) * mLocked.yScale; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2511 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2512 | // ToolMajor and ToolMinor |
| 2513 | float toolMajor, toolMinor; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2514 | switch (mCalibration.toolSizeCalibration) { |
| 2515 | case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC: |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2516 | toolMajor = in.toolMajor * mLocked.geometricScale; |
| 2517 | if (mRawAxes.toolMinor.valid) { |
| 2518 | toolMinor = in.toolMinor * mLocked.geometricScale; |
| 2519 | } else { |
| 2520 | toolMinor = toolMajor; |
| 2521 | } |
| 2522 | break; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2523 | case Calibration::TOOL_SIZE_CALIBRATION_LINEAR: |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2524 | toolMajor = in.toolMajor != 0 |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2525 | ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2526 | : 0; |
| 2527 | if (mRawAxes.toolMinor.valid) { |
| 2528 | toolMinor = in.toolMinor != 0 |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2529 | ? in.toolMinor * mLocked.toolSizeLinearScale |
| 2530 | + mLocked.toolSizeLinearBias |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2531 | : 0; |
| 2532 | } else { |
| 2533 | toolMinor = toolMajor; |
| 2534 | } |
| 2535 | break; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2536 | case Calibration::TOOL_SIZE_CALIBRATION_AREA: |
| 2537 | if (in.toolMajor != 0) { |
| 2538 | float diameter = sqrtf(in.toolMajor |
| 2539 | * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias); |
| 2540 | toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias; |
| 2541 | } else { |
| 2542 | toolMajor = 0; |
| 2543 | } |
| 2544 | toolMinor = toolMajor; |
| 2545 | break; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2546 | default: |
| 2547 | toolMajor = 0; |
| 2548 | toolMinor = 0; |
| 2549 | break; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2550 | } |
| 2551 | |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2552 | if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2553 | toolMajor /= pointerCount; |
| 2554 | toolMinor /= pointerCount; |
| 2555 | } |
| 2556 | |
| 2557 | // Pressure |
| 2558 | float rawPressure; |
| 2559 | switch (mCalibration.pressureSource) { |
| 2560 | case Calibration::PRESSURE_SOURCE_PRESSURE: |
| 2561 | rawPressure = in.pressure; |
| 2562 | break; |
| 2563 | case Calibration::PRESSURE_SOURCE_TOUCH: |
| 2564 | rawPressure = in.touchMajor; |
| 2565 | break; |
| 2566 | default: |
| 2567 | rawPressure = 0; |
| 2568 | } |
| 2569 | |
| 2570 | float pressure; |
| 2571 | switch (mCalibration.pressureCalibration) { |
| 2572 | case Calibration::PRESSURE_CALIBRATION_PHYSICAL: |
| 2573 | case Calibration::PRESSURE_CALIBRATION_AMPLITUDE: |
| 2574 | pressure = rawPressure * mLocked.pressureScale; |
| 2575 | break; |
| 2576 | default: |
| 2577 | pressure = 1; |
| 2578 | break; |
| 2579 | } |
| 2580 | |
| 2581 | // TouchMajor and TouchMinor |
| 2582 | float touchMajor, touchMinor; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2583 | switch (mCalibration.touchSizeCalibration) { |
| 2584 | case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC: |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2585 | touchMajor = in.touchMajor * mLocked.geometricScale; |
| 2586 | if (mRawAxes.touchMinor.valid) { |
| 2587 | touchMinor = in.touchMinor * mLocked.geometricScale; |
| 2588 | } else { |
| 2589 | touchMinor = touchMajor; |
| 2590 | } |
| 2591 | break; |
Jeff Brown | c6d282b | 2010-10-14 21:42:15 -0700 | [diff] [blame] | 2592 | case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE: |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2593 | touchMajor = toolMajor * pressure; |
| 2594 | touchMinor = toolMinor * pressure; |
| 2595 | break; |
| 2596 | default: |
| 2597 | touchMajor = 0; |
| 2598 | touchMinor = 0; |
| 2599 | break; |
| 2600 | } |
| 2601 | |
| 2602 | if (touchMajor > toolMajor) { |
| 2603 | touchMajor = toolMajor; |
| 2604 | } |
| 2605 | if (touchMinor > toolMinor) { |
| 2606 | touchMinor = toolMinor; |
| 2607 | } |
| 2608 | |
| 2609 | // Size |
| 2610 | float size; |
| 2611 | switch (mCalibration.sizeCalibration) { |
| 2612 | case Calibration::SIZE_CALIBRATION_NORMALIZED: { |
| 2613 | float rawSize = mRawAxes.toolMinor.valid |
| 2614 | ? avg(in.toolMajor, in.toolMinor) |
| 2615 | : in.toolMajor; |
| 2616 | size = rawSize * mLocked.sizeScale; |
| 2617 | break; |
| 2618 | } |
| 2619 | default: |
| 2620 | size = 0; |
| 2621 | break; |
| 2622 | } |
| 2623 | |
| 2624 | // Orientation |
| 2625 | float orientation; |
| 2626 | switch (mCalibration.orientationCalibration) { |
| 2627 | case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED: |
| 2628 | orientation = in.orientation * mLocked.orientationScale; |
| 2629 | break; |
Jeff Brown | 517bb4c | 2011-01-14 19:09:23 -0800 | [diff] [blame] | 2630 | case Calibration::ORIENTATION_CALIBRATION_VECTOR: { |
| 2631 | int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4); |
| 2632 | int32_t c2 = signExtendNybble(in.orientation & 0x0f); |
| 2633 | if (c1 != 0 || c2 != 0) { |
| 2634 | orientation = atan2f(c1, c2) * 0.5f; |
| 2635 | float minorAxisScale = (16.0f - pythag(c1, c2)) / 16.0f; |
| 2636 | toolMinor *= minorAxisScale; |
| 2637 | touchMinor *= minorAxisScale; |
| 2638 | } else { |
| 2639 | orientation = 0; |
| 2640 | } |
| 2641 | break; |
| 2642 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2643 | default: |
| 2644 | orientation = 0; |
| 2645 | } |
| 2646 | |
| 2647 | // Adjust coords for orientation. |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2648 | switch (mLocked.surfaceOrientation) { |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 2649 | case DISPLAY_ORIENTATION_90: { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2650 | float xTemp = x; |
| 2651 | x = y; |
| 2652 | y = mLocked.surfaceWidth - xTemp; |
| 2653 | orientation -= M_PI_2; |
| 2654 | if (orientation < - M_PI_2) { |
| 2655 | orientation += M_PI; |
| 2656 | } |
| 2657 | break; |
| 2658 | } |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 2659 | case DISPLAY_ORIENTATION_180: { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2660 | x = mLocked.surfaceWidth - x; |
| 2661 | y = mLocked.surfaceHeight - y; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2662 | break; |
| 2663 | } |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 2664 | case DISPLAY_ORIENTATION_270: { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2665 | float xTemp = x; |
| 2666 | x = mLocked.surfaceHeight - y; |
| 2667 | y = xTemp; |
| 2668 | orientation += M_PI_2; |
| 2669 | if (orientation > M_PI_2) { |
| 2670 | orientation -= M_PI; |
| 2671 | } |
| 2672 | break; |
| 2673 | } |
| 2674 | } |
| 2675 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2676 | // Write output coords. |
| 2677 | PointerCoords& out = pointerCoords[outIndex]; |
| 2678 | out.x = x; |
| 2679 | out.y = y; |
| 2680 | out.pressure = pressure; |
| 2681 | out.size = size; |
| 2682 | out.touchMajor = touchMajor; |
| 2683 | out.touchMinor = touchMinor; |
| 2684 | out.toolMajor = toolMajor; |
| 2685 | out.toolMinor = toolMinor; |
| 2686 | out.orientation = orientation; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2687 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2688 | pointerIds[outIndex] = int32_t(id); |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2689 | |
| 2690 | if (id == changedId) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2691 | motionEventAction |= outIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2692 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2693 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2694 | |
| 2695 | // Check edge flags by looking only at the first pointer since the flags are |
| 2696 | // global to the event. |
| 2697 | if (motionEventAction == AMOTION_EVENT_ACTION_DOWN) { |
| 2698 | if (pointerCoords[0].x <= 0) { |
| 2699 | motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_LEFT; |
| 2700 | } else if (pointerCoords[0].x >= mLocked.orientedSurfaceWidth) { |
| 2701 | motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_RIGHT; |
| 2702 | } |
| 2703 | if (pointerCoords[0].y <= 0) { |
| 2704 | motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_TOP; |
| 2705 | } else if (pointerCoords[0].y >= mLocked.orientedSurfaceHeight) { |
| 2706 | motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_BOTTOM; |
| 2707 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2708 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2709 | |
| 2710 | xPrecision = mLocked.orientedXPrecision; |
| 2711 | yPrecision = mLocked.orientedYPrecision; |
| 2712 | } // release lock |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2713 | |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 2714 | getDispatcher()->notifyMotion(when, getDeviceId(), mSources, policyFlags, |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 2715 | motionEventAction, 0, getContext()->getGlobalMetaState(), motionEventEdgeFlags, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2716 | pointerCount, pointerIds, pointerCoords, |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2717 | xPrecision, yPrecision, mDownTime); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2718 | } |
| 2719 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2720 | bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2721 | if (mRawAxes.x.valid && mRawAxes.y.valid) { |
| 2722 | return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue |
| 2723 | && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2724 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2725 | return true; |
| 2726 | } |
| 2727 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 2728 | const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked( |
| 2729 | int32_t x, int32_t y) { |
| 2730 | size_t numVirtualKeys = mLocked.virtualKeys.size(); |
| 2731 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 2732 | const VirtualKey& virtualKey = mLocked.virtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2733 | |
| 2734 | #if DEBUG_VIRTUAL_KEYS |
| 2735 | LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, " |
| 2736 | "left=%d, top=%d, right=%d, bottom=%d", |
| 2737 | x, y, |
| 2738 | virtualKey.keyCode, virtualKey.scanCode, |
| 2739 | virtualKey.hitLeft, virtualKey.hitTop, |
| 2740 | virtualKey.hitRight, virtualKey.hitBottom); |
| 2741 | #endif |
| 2742 | |
| 2743 | if (virtualKey.isHit(x, y)) { |
| 2744 | return & virtualKey; |
| 2745 | } |
| 2746 | } |
| 2747 | |
| 2748 | return NULL; |
| 2749 | } |
| 2750 | |
| 2751 | void TouchInputMapper::calculatePointerIds() { |
| 2752 | uint32_t currentPointerCount = mCurrentTouch.pointerCount; |
| 2753 | uint32_t lastPointerCount = mLastTouch.pointerCount; |
| 2754 | |
| 2755 | if (currentPointerCount == 0) { |
| 2756 | // No pointers to assign. |
| 2757 | mCurrentTouch.idBits.clear(); |
| 2758 | } else if (lastPointerCount == 0) { |
| 2759 | // All pointers are new. |
| 2760 | mCurrentTouch.idBits.clear(); |
| 2761 | for (uint32_t i = 0; i < currentPointerCount; i++) { |
| 2762 | mCurrentTouch.pointers[i].id = i; |
| 2763 | mCurrentTouch.idToIndex[i] = i; |
| 2764 | mCurrentTouch.idBits.markBit(i); |
| 2765 | } |
| 2766 | } else if (currentPointerCount == 1 && lastPointerCount == 1) { |
| 2767 | // Only one pointer and no change in count so it must have the same id as before. |
| 2768 | uint32_t id = mLastTouch.pointers[0].id; |
| 2769 | mCurrentTouch.pointers[0].id = id; |
| 2770 | mCurrentTouch.idToIndex[id] = 0; |
| 2771 | mCurrentTouch.idBits.value = BitSet32::valueForBit(id); |
| 2772 | } else { |
| 2773 | // General case. |
| 2774 | // We build a heap of squared euclidean distances between current and last pointers |
| 2775 | // associated with the current and last pointer indices. Then, we find the best |
| 2776 | // match (by distance) for each current pointer. |
| 2777 | PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS]; |
| 2778 | |
| 2779 | uint32_t heapSize = 0; |
| 2780 | for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount; |
| 2781 | currentPointerIndex++) { |
| 2782 | for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount; |
| 2783 | lastPointerIndex++) { |
| 2784 | int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x |
| 2785 | - mLastTouch.pointers[lastPointerIndex].x; |
| 2786 | int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y |
| 2787 | - mLastTouch.pointers[lastPointerIndex].y; |
| 2788 | |
| 2789 | uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY); |
| 2790 | |
| 2791 | // Insert new element into the heap (sift up). |
| 2792 | heap[heapSize].currentPointerIndex = currentPointerIndex; |
| 2793 | heap[heapSize].lastPointerIndex = lastPointerIndex; |
| 2794 | heap[heapSize].distance = distance; |
| 2795 | heapSize += 1; |
| 2796 | } |
| 2797 | } |
| 2798 | |
| 2799 | // Heapify |
| 2800 | for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) { |
| 2801 | startIndex -= 1; |
| 2802 | for (uint32_t parentIndex = startIndex; ;) { |
| 2803 | uint32_t childIndex = parentIndex * 2 + 1; |
| 2804 | if (childIndex >= heapSize) { |
| 2805 | break; |
| 2806 | } |
| 2807 | |
| 2808 | if (childIndex + 1 < heapSize |
| 2809 | && heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 2810 | childIndex += 1; |
| 2811 | } |
| 2812 | |
| 2813 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 2814 | break; |
| 2815 | } |
| 2816 | |
| 2817 | swap(heap[parentIndex], heap[childIndex]); |
| 2818 | parentIndex = childIndex; |
| 2819 | } |
| 2820 | } |
| 2821 | |
| 2822 | #if DEBUG_POINTER_ASSIGNMENT |
| 2823 | LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize); |
| 2824 | for (size_t i = 0; i < heapSize; i++) { |
| 2825 | LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", |
| 2826 | i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, |
| 2827 | heap[i].distance); |
| 2828 | } |
| 2829 | #endif |
| 2830 | |
| 2831 | // Pull matches out by increasing order of distance. |
| 2832 | // To avoid reassigning pointers that have already been matched, the loop keeps track |
| 2833 | // of which last and current pointers have been matched using the matchedXXXBits variables. |
| 2834 | // It also tracks the used pointer id bits. |
| 2835 | BitSet32 matchedLastBits(0); |
| 2836 | BitSet32 matchedCurrentBits(0); |
| 2837 | BitSet32 usedIdBits(0); |
| 2838 | bool first = true; |
| 2839 | for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) { |
| 2840 | for (;;) { |
| 2841 | if (first) { |
| 2842 | // The first time through the loop, we just consume the root element of |
| 2843 | // the heap (the one with smallest distance). |
| 2844 | first = false; |
| 2845 | } else { |
| 2846 | // Previous iterations consumed the root element of the heap. |
| 2847 | // Pop root element off of the heap (sift down). |
| 2848 | heapSize -= 1; |
| 2849 | assert(heapSize > 0); |
| 2850 | |
| 2851 | // Sift down. |
| 2852 | heap[0] = heap[heapSize]; |
| 2853 | for (uint32_t parentIndex = 0; ;) { |
| 2854 | uint32_t childIndex = parentIndex * 2 + 1; |
| 2855 | if (childIndex >= heapSize) { |
| 2856 | break; |
| 2857 | } |
| 2858 | |
| 2859 | if (childIndex + 1 < heapSize |
| 2860 | && heap[childIndex + 1].distance < heap[childIndex].distance) { |
| 2861 | childIndex += 1; |
| 2862 | } |
| 2863 | |
| 2864 | if (heap[parentIndex].distance <= heap[childIndex].distance) { |
| 2865 | break; |
| 2866 | } |
| 2867 | |
| 2868 | swap(heap[parentIndex], heap[childIndex]); |
| 2869 | parentIndex = childIndex; |
| 2870 | } |
| 2871 | |
| 2872 | #if DEBUG_POINTER_ASSIGNMENT |
| 2873 | LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize); |
| 2874 | for (size_t i = 0; i < heapSize; i++) { |
| 2875 | LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld", |
| 2876 | i, heap[i].currentPointerIndex, heap[i].lastPointerIndex, |
| 2877 | heap[i].distance); |
| 2878 | } |
| 2879 | #endif |
| 2880 | } |
| 2881 | |
| 2882 | uint32_t currentPointerIndex = heap[0].currentPointerIndex; |
| 2883 | if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched |
| 2884 | |
| 2885 | uint32_t lastPointerIndex = heap[0].lastPointerIndex; |
| 2886 | if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched |
| 2887 | |
| 2888 | matchedCurrentBits.markBit(currentPointerIndex); |
| 2889 | matchedLastBits.markBit(lastPointerIndex); |
| 2890 | |
| 2891 | uint32_t id = mLastTouch.pointers[lastPointerIndex].id; |
| 2892 | mCurrentTouch.pointers[currentPointerIndex].id = id; |
| 2893 | mCurrentTouch.idToIndex[id] = currentPointerIndex; |
| 2894 | usedIdBits.markBit(id); |
| 2895 | |
| 2896 | #if DEBUG_POINTER_ASSIGNMENT |
| 2897 | LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld", |
| 2898 | lastPointerIndex, currentPointerIndex, id, heap[0].distance); |
| 2899 | #endif |
| 2900 | break; |
| 2901 | } |
| 2902 | } |
| 2903 | |
| 2904 | // Assign fresh ids to new pointers. |
| 2905 | if (currentPointerCount > lastPointerCount) { |
| 2906 | for (uint32_t i = currentPointerCount - lastPointerCount; ;) { |
| 2907 | uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit(); |
| 2908 | uint32_t id = usedIdBits.firstUnmarkedBit(); |
| 2909 | |
| 2910 | mCurrentTouch.pointers[currentPointerIndex].id = id; |
| 2911 | mCurrentTouch.idToIndex[id] = currentPointerIndex; |
| 2912 | usedIdBits.markBit(id); |
| 2913 | |
| 2914 | #if DEBUG_POINTER_ASSIGNMENT |
| 2915 | LOGD("calculatePointerIds - assigned: cur=%d, id=%d", |
| 2916 | currentPointerIndex, id); |
| 2917 | #endif |
| 2918 | |
| 2919 | if (--i == 0) break; // done |
| 2920 | matchedCurrentBits.markBit(currentPointerIndex); |
| 2921 | } |
| 2922 | } |
| 2923 | |
| 2924 | // Fix id bits. |
| 2925 | mCurrentTouch.idBits = usedIdBits; |
| 2926 | } |
| 2927 | } |
| 2928 | |
| 2929 | /* Special hack for devices that have bad screen data: if one of the |
| 2930 | * points has moved more than a screen height from the last position, |
| 2931 | * then drop it. */ |
| 2932 | bool TouchInputMapper::applyBadTouchFilter() { |
| 2933 | // This hack requires valid axis parameters. |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2934 | if (! mRawAxes.y.valid) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2935 | return false; |
| 2936 | } |
| 2937 | |
| 2938 | uint32_t pointerCount = mCurrentTouch.pointerCount; |
| 2939 | |
| 2940 | // Nothing to do if there are no points. |
| 2941 | if (pointerCount == 0) { |
| 2942 | return false; |
| 2943 | } |
| 2944 | |
| 2945 | // Don't do anything if a finger is going down or up. We run |
| 2946 | // here before assigning pointer IDs, so there isn't a good |
| 2947 | // way to do per-finger matching. |
| 2948 | if (pointerCount != mLastTouch.pointerCount) { |
| 2949 | return false; |
| 2950 | } |
| 2951 | |
| 2952 | // We consider a single movement across more than a 7/16 of |
| 2953 | // the long size of the screen to be bad. This was a magic value |
| 2954 | // determined by looking at the maximum distance it is feasible |
| 2955 | // to actually move in one sample. |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2956 | int32_t maxDeltaY = mRawAxes.y.getRange() * 7 / 16; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 2957 | |
| 2958 | // XXX The original code in InputDevice.java included commented out |
| 2959 | // code for testing the X axis. Note that when we drop a point |
| 2960 | // we don't actually restore the old X either. Strange. |
| 2961 | // The old code also tries to track when bad points were previously |
| 2962 | // detected but it turns out that due to the placement of a "break" |
| 2963 | // at the end of the loop, we never set mDroppedBadPoint to true |
| 2964 | // so it is effectively dead code. |
| 2965 | // Need to figure out if the old code is busted or just overcomplicated |
| 2966 | // but working as intended. |
| 2967 | |
| 2968 | // Look through all new points and see if any are farther than |
| 2969 | // acceptable from all previous points. |
| 2970 | for (uint32_t i = pointerCount; i-- > 0; ) { |
| 2971 | int32_t y = mCurrentTouch.pointers[i].y; |
| 2972 | int32_t closestY = INT_MAX; |
| 2973 | int32_t closestDeltaY = 0; |
| 2974 | |
| 2975 | #if DEBUG_HACKS |
| 2976 | LOGD("BadTouchFilter: Looking at next point #%d: y=%d", i, y); |
| 2977 | #endif |
| 2978 | |
| 2979 | for (uint32_t j = pointerCount; j-- > 0; ) { |
| 2980 | int32_t lastY = mLastTouch.pointers[j].y; |
| 2981 | int32_t deltaY = abs(y - lastY); |
| 2982 | |
| 2983 | #if DEBUG_HACKS |
| 2984 | LOGD("BadTouchFilter: Comparing with last point #%d: y=%d deltaY=%d", |
| 2985 | j, lastY, deltaY); |
| 2986 | #endif |
| 2987 | |
| 2988 | if (deltaY < maxDeltaY) { |
| 2989 | goto SkipSufficientlyClosePoint; |
| 2990 | } |
| 2991 | if (deltaY < closestDeltaY) { |
| 2992 | closestDeltaY = deltaY; |
| 2993 | closestY = lastY; |
| 2994 | } |
| 2995 | } |
| 2996 | |
| 2997 | // Must not have found a close enough match. |
| 2998 | #if DEBUG_HACKS |
| 2999 | LOGD("BadTouchFilter: Dropping bad point #%d: newY=%d oldY=%d deltaY=%d maxDeltaY=%d", |
| 3000 | i, y, closestY, closestDeltaY, maxDeltaY); |
| 3001 | #endif |
| 3002 | |
| 3003 | mCurrentTouch.pointers[i].y = closestY; |
| 3004 | return true; // XXX original code only corrects one point |
| 3005 | |
| 3006 | SkipSufficientlyClosePoint: ; |
| 3007 | } |
| 3008 | |
| 3009 | // No change. |
| 3010 | return false; |
| 3011 | } |
| 3012 | |
| 3013 | /* Special hack for devices that have bad screen data: drop points where |
| 3014 | * the coordinate value for one axis has jumped to the other pointer's location. |
| 3015 | */ |
| 3016 | bool TouchInputMapper::applyJumpyTouchFilter() { |
| 3017 | // This hack requires valid axis parameters. |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3018 | if (! mRawAxes.y.valid) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3019 | return false; |
| 3020 | } |
| 3021 | |
| 3022 | uint32_t pointerCount = mCurrentTouch.pointerCount; |
| 3023 | if (mLastTouch.pointerCount != pointerCount) { |
| 3024 | #if DEBUG_HACKS |
| 3025 | LOGD("JumpyTouchFilter: Different pointer count %d -> %d", |
| 3026 | mLastTouch.pointerCount, pointerCount); |
| 3027 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 3028 | LOGD(" Pointer %d (%d, %d)", i, |
| 3029 | mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y); |
| 3030 | } |
| 3031 | #endif |
| 3032 | |
| 3033 | if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_TRANSITION_DROPS) { |
| 3034 | if (mLastTouch.pointerCount == 1 && pointerCount == 2) { |
| 3035 | // Just drop the first few events going from 1 to 2 pointers. |
| 3036 | // They're bad often enough that they're not worth considering. |
| 3037 | mCurrentTouch.pointerCount = 1; |
| 3038 | mJumpyTouchFilter.jumpyPointsDropped += 1; |
| 3039 | |
| 3040 | #if DEBUG_HACKS |
| 3041 | LOGD("JumpyTouchFilter: Pointer 2 dropped"); |
| 3042 | #endif |
| 3043 | return true; |
| 3044 | } else if (mLastTouch.pointerCount == 2 && pointerCount == 1) { |
| 3045 | // The event when we go from 2 -> 1 tends to be messed up too |
| 3046 | mCurrentTouch.pointerCount = 2; |
| 3047 | mCurrentTouch.pointers[0] = mLastTouch.pointers[0]; |
| 3048 | mCurrentTouch.pointers[1] = mLastTouch.pointers[1]; |
| 3049 | mJumpyTouchFilter.jumpyPointsDropped += 1; |
| 3050 | |
| 3051 | #if DEBUG_HACKS |
| 3052 | for (int32_t i = 0; i < 2; i++) { |
| 3053 | LOGD("JumpyTouchFilter: Pointer %d replaced (%d, %d)", i, |
| 3054 | mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y); |
| 3055 | } |
| 3056 | #endif |
| 3057 | return true; |
| 3058 | } |
| 3059 | } |
| 3060 | // Reset jumpy points dropped on other transitions or if limit exceeded. |
| 3061 | mJumpyTouchFilter.jumpyPointsDropped = 0; |
| 3062 | |
| 3063 | #if DEBUG_HACKS |
| 3064 | LOGD("JumpyTouchFilter: Transition - drop limit reset"); |
| 3065 | #endif |
| 3066 | return false; |
| 3067 | } |
| 3068 | |
| 3069 | // We have the same number of pointers as last time. |
| 3070 | // A 'jumpy' point is one where the coordinate value for one axis |
| 3071 | // has jumped to the other pointer's location. No need to do anything |
| 3072 | // else if we only have one pointer. |
| 3073 | if (pointerCount < 2) { |
| 3074 | return false; |
| 3075 | } |
| 3076 | |
| 3077 | if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_DROP_LIMIT) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3078 | int jumpyEpsilon = mRawAxes.y.getRange() / JUMPY_EPSILON_DIVISOR; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3079 | |
| 3080 | // We only replace the single worst jumpy point as characterized by pointer distance |
| 3081 | // in a single axis. |
| 3082 | int32_t badPointerIndex = -1; |
| 3083 | int32_t badPointerReplacementIndex = -1; |
| 3084 | int32_t badPointerDistance = INT_MIN; // distance to be corrected |
| 3085 | |
| 3086 | for (uint32_t i = pointerCount; i-- > 0; ) { |
| 3087 | int32_t x = mCurrentTouch.pointers[i].x; |
| 3088 | int32_t y = mCurrentTouch.pointers[i].y; |
| 3089 | |
| 3090 | #if DEBUG_HACKS |
| 3091 | LOGD("JumpyTouchFilter: Point %d (%d, %d)", i, x, y); |
| 3092 | #endif |
| 3093 | |
| 3094 | // Check if a touch point is too close to another's coordinates |
| 3095 | bool dropX = false, dropY = false; |
| 3096 | for (uint32_t j = 0; j < pointerCount; j++) { |
| 3097 | if (i == j) { |
| 3098 | continue; |
| 3099 | } |
| 3100 | |
| 3101 | if (abs(x - mCurrentTouch.pointers[j].x) <= jumpyEpsilon) { |
| 3102 | dropX = true; |
| 3103 | break; |
| 3104 | } |
| 3105 | |
| 3106 | if (abs(y - mCurrentTouch.pointers[j].y) <= jumpyEpsilon) { |
| 3107 | dropY = true; |
| 3108 | break; |
| 3109 | } |
| 3110 | } |
| 3111 | if (! dropX && ! dropY) { |
| 3112 | continue; // not jumpy |
| 3113 | } |
| 3114 | |
| 3115 | // Find a replacement candidate by comparing with older points on the |
| 3116 | // complementary (non-jumpy) axis. |
| 3117 | int32_t distance = INT_MIN; // distance to be corrected |
| 3118 | int32_t replacementIndex = -1; |
| 3119 | |
| 3120 | if (dropX) { |
| 3121 | // X looks too close. Find an older replacement point with a close Y. |
| 3122 | int32_t smallestDeltaY = INT_MAX; |
| 3123 | for (uint32_t j = 0; j < pointerCount; j++) { |
| 3124 | int32_t deltaY = abs(y - mLastTouch.pointers[j].y); |
| 3125 | if (deltaY < smallestDeltaY) { |
| 3126 | smallestDeltaY = deltaY; |
| 3127 | replacementIndex = j; |
| 3128 | } |
| 3129 | } |
| 3130 | distance = abs(x - mLastTouch.pointers[replacementIndex].x); |
| 3131 | } else { |
| 3132 | // Y looks too close. Find an older replacement point with a close X. |
| 3133 | int32_t smallestDeltaX = INT_MAX; |
| 3134 | for (uint32_t j = 0; j < pointerCount; j++) { |
| 3135 | int32_t deltaX = abs(x - mLastTouch.pointers[j].x); |
| 3136 | if (deltaX < smallestDeltaX) { |
| 3137 | smallestDeltaX = deltaX; |
| 3138 | replacementIndex = j; |
| 3139 | } |
| 3140 | } |
| 3141 | distance = abs(y - mLastTouch.pointers[replacementIndex].y); |
| 3142 | } |
| 3143 | |
| 3144 | // If replacing this pointer would correct a worse error than the previous ones |
| 3145 | // considered, then use this replacement instead. |
| 3146 | if (distance > badPointerDistance) { |
| 3147 | badPointerIndex = i; |
| 3148 | badPointerReplacementIndex = replacementIndex; |
| 3149 | badPointerDistance = distance; |
| 3150 | } |
| 3151 | } |
| 3152 | |
| 3153 | // Correct the jumpy pointer if one was found. |
| 3154 | if (badPointerIndex >= 0) { |
| 3155 | #if DEBUG_HACKS |
| 3156 | LOGD("JumpyTouchFilter: Replacing bad pointer %d with (%d, %d)", |
| 3157 | badPointerIndex, |
| 3158 | mLastTouch.pointers[badPointerReplacementIndex].x, |
| 3159 | mLastTouch.pointers[badPointerReplacementIndex].y); |
| 3160 | #endif |
| 3161 | |
| 3162 | mCurrentTouch.pointers[badPointerIndex].x = |
| 3163 | mLastTouch.pointers[badPointerReplacementIndex].x; |
| 3164 | mCurrentTouch.pointers[badPointerIndex].y = |
| 3165 | mLastTouch.pointers[badPointerReplacementIndex].y; |
| 3166 | mJumpyTouchFilter.jumpyPointsDropped += 1; |
| 3167 | return true; |
| 3168 | } |
| 3169 | } |
| 3170 | |
| 3171 | mJumpyTouchFilter.jumpyPointsDropped = 0; |
| 3172 | return false; |
| 3173 | } |
| 3174 | |
| 3175 | /* Special hack for devices that have bad screen data: aggregate and |
| 3176 | * compute averages of the coordinate data, to reduce the amount of |
| 3177 | * jitter seen by applications. */ |
| 3178 | void TouchInputMapper::applyAveragingTouchFilter() { |
| 3179 | for (uint32_t currentIndex = 0; currentIndex < mCurrentTouch.pointerCount; currentIndex++) { |
| 3180 | uint32_t id = mCurrentTouch.pointers[currentIndex].id; |
| 3181 | int32_t x = mCurrentTouch.pointers[currentIndex].x; |
| 3182 | int32_t y = mCurrentTouch.pointers[currentIndex].y; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3183 | int32_t pressure; |
| 3184 | switch (mCalibration.pressureSource) { |
| 3185 | case Calibration::PRESSURE_SOURCE_PRESSURE: |
| 3186 | pressure = mCurrentTouch.pointers[currentIndex].pressure; |
| 3187 | break; |
| 3188 | case Calibration::PRESSURE_SOURCE_TOUCH: |
| 3189 | pressure = mCurrentTouch.pointers[currentIndex].touchMajor; |
| 3190 | break; |
| 3191 | default: |
| 3192 | pressure = 1; |
| 3193 | break; |
| 3194 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3195 | |
| 3196 | if (mLastTouch.idBits.hasBit(id)) { |
| 3197 | // Pointer was down before and is still down now. |
| 3198 | // Compute average over history trace. |
| 3199 | uint32_t start = mAveragingTouchFilter.historyStart[id]; |
| 3200 | uint32_t end = mAveragingTouchFilter.historyEnd[id]; |
| 3201 | |
| 3202 | int64_t deltaX = x - mAveragingTouchFilter.historyData[end].pointers[id].x; |
| 3203 | int64_t deltaY = y - mAveragingTouchFilter.historyData[end].pointers[id].y; |
| 3204 | uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY); |
| 3205 | |
| 3206 | #if DEBUG_HACKS |
| 3207 | LOGD("AveragingTouchFilter: Pointer id %d - Distance from last sample: %lld", |
| 3208 | id, distance); |
| 3209 | #endif |
| 3210 | |
| 3211 | if (distance < AVERAGING_DISTANCE_LIMIT) { |
| 3212 | // Increment end index in preparation for recording new historical data. |
| 3213 | end += 1; |
| 3214 | if (end > AVERAGING_HISTORY_SIZE) { |
| 3215 | end = 0; |
| 3216 | } |
| 3217 | |
| 3218 | // If the end index has looped back to the start index then we have filled |
| 3219 | // the historical trace up to the desired size so we drop the historical |
| 3220 | // data at the start of the trace. |
| 3221 | if (end == start) { |
| 3222 | start += 1; |
| 3223 | if (start > AVERAGING_HISTORY_SIZE) { |
| 3224 | start = 0; |
| 3225 | } |
| 3226 | } |
| 3227 | |
| 3228 | // Add the raw data to the historical trace. |
| 3229 | mAveragingTouchFilter.historyStart[id] = start; |
| 3230 | mAveragingTouchFilter.historyEnd[id] = end; |
| 3231 | mAveragingTouchFilter.historyData[end].pointers[id].x = x; |
| 3232 | mAveragingTouchFilter.historyData[end].pointers[id].y = y; |
| 3233 | mAveragingTouchFilter.historyData[end].pointers[id].pressure = pressure; |
| 3234 | |
| 3235 | // Average over all historical positions in the trace by total pressure. |
| 3236 | int32_t averagedX = 0; |
| 3237 | int32_t averagedY = 0; |
| 3238 | int32_t totalPressure = 0; |
| 3239 | for (;;) { |
| 3240 | int32_t historicalX = mAveragingTouchFilter.historyData[start].pointers[id].x; |
| 3241 | int32_t historicalY = mAveragingTouchFilter.historyData[start].pointers[id].y; |
| 3242 | int32_t historicalPressure = mAveragingTouchFilter.historyData[start] |
| 3243 | .pointers[id].pressure; |
| 3244 | |
| 3245 | averagedX += historicalX * historicalPressure; |
| 3246 | averagedY += historicalY * historicalPressure; |
| 3247 | totalPressure += historicalPressure; |
| 3248 | |
| 3249 | if (start == end) { |
| 3250 | break; |
| 3251 | } |
| 3252 | |
| 3253 | start += 1; |
| 3254 | if (start > AVERAGING_HISTORY_SIZE) { |
| 3255 | start = 0; |
| 3256 | } |
| 3257 | } |
| 3258 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3259 | if (totalPressure != 0) { |
| 3260 | averagedX /= totalPressure; |
| 3261 | averagedY /= totalPressure; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3262 | |
| 3263 | #if DEBUG_HACKS |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3264 | LOGD("AveragingTouchFilter: Pointer id %d - " |
| 3265 | "totalPressure=%d, averagedX=%d, averagedY=%d", id, totalPressure, |
| 3266 | averagedX, averagedY); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3267 | #endif |
| 3268 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3269 | mCurrentTouch.pointers[currentIndex].x = averagedX; |
| 3270 | mCurrentTouch.pointers[currentIndex].y = averagedY; |
| 3271 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3272 | } else { |
| 3273 | #if DEBUG_HACKS |
| 3274 | LOGD("AveragingTouchFilter: Pointer id %d - Exceeded max distance", id); |
| 3275 | #endif |
| 3276 | } |
| 3277 | } else { |
| 3278 | #if DEBUG_HACKS |
| 3279 | LOGD("AveragingTouchFilter: Pointer id %d - Pointer went up", id); |
| 3280 | #endif |
| 3281 | } |
| 3282 | |
| 3283 | // Reset pointer history. |
| 3284 | mAveragingTouchFilter.historyStart[id] = 0; |
| 3285 | mAveragingTouchFilter.historyEnd[id] = 0; |
| 3286 | mAveragingTouchFilter.historyData[0].pointers[id].x = x; |
| 3287 | mAveragingTouchFilter.historyData[0].pointers[id].y = y; |
| 3288 | mAveragingTouchFilter.historyData[0].pointers[id].pressure = pressure; |
| 3289 | } |
| 3290 | } |
| 3291 | |
| 3292 | int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3293 | { // acquire lock |
| 3294 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3295 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3296 | if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3297 | return AKEY_STATE_VIRTUAL; |
| 3298 | } |
| 3299 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3300 | size_t numVirtualKeys = mLocked.virtualKeys.size(); |
| 3301 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 3302 | const VirtualKey& virtualKey = mLocked.virtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3303 | if (virtualKey.keyCode == keyCode) { |
| 3304 | return AKEY_STATE_UP; |
| 3305 | } |
| 3306 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3307 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3308 | |
| 3309 | return AKEY_STATE_UNKNOWN; |
| 3310 | } |
| 3311 | |
| 3312 | int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3313 | { // acquire lock |
| 3314 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3315 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3316 | if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3317 | return AKEY_STATE_VIRTUAL; |
| 3318 | } |
| 3319 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3320 | size_t numVirtualKeys = mLocked.virtualKeys.size(); |
| 3321 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 3322 | const VirtualKey& virtualKey = mLocked.virtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3323 | if (virtualKey.scanCode == scanCode) { |
| 3324 | return AKEY_STATE_UP; |
| 3325 | } |
| 3326 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3327 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3328 | |
| 3329 | return AKEY_STATE_UNKNOWN; |
| 3330 | } |
| 3331 | |
| 3332 | bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes, |
| 3333 | const int32_t* keyCodes, uint8_t* outFlags) { |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3334 | { // acquire lock |
| 3335 | AutoMutex _l(mLock); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3336 | |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3337 | size_t numVirtualKeys = mLocked.virtualKeys.size(); |
| 3338 | for (size_t i = 0; i < numVirtualKeys; i++) { |
| 3339 | const VirtualKey& virtualKey = mLocked.virtualKeys[i]; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3340 | |
| 3341 | for (size_t i = 0; i < numCodes; i++) { |
| 3342 | if (virtualKey.keyCode == keyCodes[i]) { |
| 3343 | outFlags[i] = 1; |
| 3344 | } |
| 3345 | } |
| 3346 | } |
Jeff Brown | 6328cdc | 2010-07-29 18:18:33 -0700 | [diff] [blame] | 3347 | } // release lock |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3348 | |
| 3349 | return true; |
| 3350 | } |
| 3351 | |
| 3352 | |
| 3353 | // --- SingleTouchInputMapper --- |
| 3354 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 3355 | SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) : |
| 3356 | TouchInputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3357 | initialize(); |
| 3358 | } |
| 3359 | |
| 3360 | SingleTouchInputMapper::~SingleTouchInputMapper() { |
| 3361 | } |
| 3362 | |
| 3363 | void SingleTouchInputMapper::initialize() { |
| 3364 | mAccumulator.clear(); |
| 3365 | |
| 3366 | mDown = false; |
| 3367 | mX = 0; |
| 3368 | mY = 0; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3369 | mPressure = 0; // default to 0 for devices that don't report pressure |
| 3370 | mToolWidth = 0; // default to 0 for devices that don't report tool width |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3371 | } |
| 3372 | |
| 3373 | void SingleTouchInputMapper::reset() { |
| 3374 | TouchInputMapper::reset(); |
| 3375 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3376 | initialize(); |
| 3377 | } |
| 3378 | |
| 3379 | void SingleTouchInputMapper::process(const RawEvent* rawEvent) { |
| 3380 | switch (rawEvent->type) { |
| 3381 | case EV_KEY: |
| 3382 | switch (rawEvent->scanCode) { |
| 3383 | case BTN_TOUCH: |
| 3384 | mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH; |
| 3385 | mAccumulator.btnTouch = rawEvent->value != 0; |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3386 | // Don't sync immediately. Wait until the next SYN_REPORT since we might |
| 3387 | // not have received valid position information yet. This logic assumes that |
| 3388 | // 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] | 3389 | break; |
| 3390 | } |
| 3391 | break; |
| 3392 | |
| 3393 | case EV_ABS: |
| 3394 | switch (rawEvent->scanCode) { |
| 3395 | case ABS_X: |
| 3396 | mAccumulator.fields |= Accumulator::FIELD_ABS_X; |
| 3397 | mAccumulator.absX = rawEvent->value; |
| 3398 | break; |
| 3399 | case ABS_Y: |
| 3400 | mAccumulator.fields |= Accumulator::FIELD_ABS_Y; |
| 3401 | mAccumulator.absY = rawEvent->value; |
| 3402 | break; |
| 3403 | case ABS_PRESSURE: |
| 3404 | mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE; |
| 3405 | mAccumulator.absPressure = rawEvent->value; |
| 3406 | break; |
| 3407 | case ABS_TOOL_WIDTH: |
| 3408 | mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH; |
| 3409 | mAccumulator.absToolWidth = rawEvent->value; |
| 3410 | break; |
| 3411 | } |
| 3412 | break; |
| 3413 | |
| 3414 | case EV_SYN: |
| 3415 | switch (rawEvent->scanCode) { |
| 3416 | case SYN_REPORT: |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3417 | sync(rawEvent->when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3418 | break; |
| 3419 | } |
| 3420 | break; |
| 3421 | } |
| 3422 | } |
| 3423 | |
| 3424 | void SingleTouchInputMapper::sync(nsecs_t when) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3425 | uint32_t fields = mAccumulator.fields; |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3426 | if (fields == 0) { |
| 3427 | return; // no new state changes, so nothing to do |
| 3428 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3429 | |
| 3430 | if (fields & Accumulator::FIELD_BTN_TOUCH) { |
| 3431 | mDown = mAccumulator.btnTouch; |
| 3432 | } |
| 3433 | |
| 3434 | if (fields & Accumulator::FIELD_ABS_X) { |
| 3435 | mX = mAccumulator.absX; |
| 3436 | } |
| 3437 | |
| 3438 | if (fields & Accumulator::FIELD_ABS_Y) { |
| 3439 | mY = mAccumulator.absY; |
| 3440 | } |
| 3441 | |
| 3442 | if (fields & Accumulator::FIELD_ABS_PRESSURE) { |
| 3443 | mPressure = mAccumulator.absPressure; |
| 3444 | } |
| 3445 | |
| 3446 | if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3447 | mToolWidth = mAccumulator.absToolWidth; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3448 | } |
| 3449 | |
| 3450 | mCurrentTouch.clear(); |
| 3451 | |
| 3452 | if (mDown) { |
| 3453 | mCurrentTouch.pointerCount = 1; |
| 3454 | mCurrentTouch.pointers[0].id = 0; |
| 3455 | mCurrentTouch.pointers[0].x = mX; |
| 3456 | mCurrentTouch.pointers[0].y = mY; |
| 3457 | mCurrentTouch.pointers[0].pressure = mPressure; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3458 | mCurrentTouch.pointers[0].touchMajor = 0; |
| 3459 | mCurrentTouch.pointers[0].touchMinor = 0; |
| 3460 | mCurrentTouch.pointers[0].toolMajor = mToolWidth; |
| 3461 | mCurrentTouch.pointers[0].toolMinor = mToolWidth; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3462 | mCurrentTouch.pointers[0].orientation = 0; |
| 3463 | mCurrentTouch.idToIndex[0] = 0; |
| 3464 | mCurrentTouch.idBits.markBit(0); |
| 3465 | } |
| 3466 | |
| 3467 | syncTouch(when, true); |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3468 | |
| 3469 | mAccumulator.clear(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3470 | } |
| 3471 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3472 | void SingleTouchInputMapper::configureRawAxes() { |
| 3473 | TouchInputMapper::configureRawAxes(); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3474 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3475 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x); |
| 3476 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y); |
| 3477 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure); |
| 3478 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3479 | } |
| 3480 | |
| 3481 | |
| 3482 | // --- MultiTouchInputMapper --- |
| 3483 | |
Jeff Brown | 47e6b1b | 2010-11-29 17:37:49 -0800 | [diff] [blame] | 3484 | MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) : |
| 3485 | TouchInputMapper(device) { |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3486 | initialize(); |
| 3487 | } |
| 3488 | |
| 3489 | MultiTouchInputMapper::~MultiTouchInputMapper() { |
| 3490 | } |
| 3491 | |
| 3492 | void MultiTouchInputMapper::initialize() { |
| 3493 | mAccumulator.clear(); |
| 3494 | } |
| 3495 | |
| 3496 | void MultiTouchInputMapper::reset() { |
| 3497 | TouchInputMapper::reset(); |
| 3498 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3499 | initialize(); |
| 3500 | } |
| 3501 | |
| 3502 | void MultiTouchInputMapper::process(const RawEvent* rawEvent) { |
| 3503 | switch (rawEvent->type) { |
| 3504 | case EV_ABS: { |
| 3505 | uint32_t pointerIndex = mAccumulator.pointerCount; |
| 3506 | Accumulator::Pointer* pointer = & mAccumulator.pointers[pointerIndex]; |
| 3507 | |
| 3508 | switch (rawEvent->scanCode) { |
| 3509 | case ABS_MT_POSITION_X: |
| 3510 | pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_X; |
| 3511 | pointer->absMTPositionX = rawEvent->value; |
| 3512 | break; |
| 3513 | case ABS_MT_POSITION_Y: |
| 3514 | pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y; |
| 3515 | pointer->absMTPositionY = rawEvent->value; |
| 3516 | break; |
| 3517 | case ABS_MT_TOUCH_MAJOR: |
| 3518 | pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR; |
| 3519 | pointer->absMTTouchMajor = rawEvent->value; |
| 3520 | break; |
| 3521 | case ABS_MT_TOUCH_MINOR: |
| 3522 | pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR; |
| 3523 | pointer->absMTTouchMinor = rawEvent->value; |
| 3524 | break; |
| 3525 | case ABS_MT_WIDTH_MAJOR: |
| 3526 | pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR; |
| 3527 | pointer->absMTWidthMajor = rawEvent->value; |
| 3528 | break; |
| 3529 | case ABS_MT_WIDTH_MINOR: |
| 3530 | pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR; |
| 3531 | pointer->absMTWidthMinor = rawEvent->value; |
| 3532 | break; |
| 3533 | case ABS_MT_ORIENTATION: |
| 3534 | pointer->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION; |
| 3535 | pointer->absMTOrientation = rawEvent->value; |
| 3536 | break; |
| 3537 | case ABS_MT_TRACKING_ID: |
| 3538 | pointer->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID; |
| 3539 | pointer->absMTTrackingId = rawEvent->value; |
| 3540 | break; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3541 | case ABS_MT_PRESSURE: |
| 3542 | pointer->fields |= Accumulator::FIELD_ABS_MT_PRESSURE; |
| 3543 | pointer->absMTPressure = rawEvent->value; |
| 3544 | break; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3545 | } |
| 3546 | break; |
| 3547 | } |
| 3548 | |
| 3549 | case EV_SYN: |
| 3550 | switch (rawEvent->scanCode) { |
| 3551 | case SYN_MT_REPORT: { |
| 3552 | // MultiTouch Sync: The driver has returned all data for *one* of the pointers. |
| 3553 | uint32_t pointerIndex = mAccumulator.pointerCount; |
| 3554 | |
| 3555 | if (mAccumulator.pointers[pointerIndex].fields) { |
| 3556 | if (pointerIndex == MAX_POINTERS) { |
| 3557 | LOGW("MultiTouch device driver returned more than maximum of %d pointers.", |
| 3558 | MAX_POINTERS); |
| 3559 | } else { |
| 3560 | pointerIndex += 1; |
| 3561 | mAccumulator.pointerCount = pointerIndex; |
| 3562 | } |
| 3563 | } |
| 3564 | |
| 3565 | mAccumulator.pointers[pointerIndex].clear(); |
| 3566 | break; |
| 3567 | } |
| 3568 | |
| 3569 | case SYN_REPORT: |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3570 | sync(rawEvent->when); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3571 | break; |
| 3572 | } |
| 3573 | break; |
| 3574 | } |
| 3575 | } |
| 3576 | |
| 3577 | void MultiTouchInputMapper::sync(nsecs_t when) { |
| 3578 | static const uint32_t REQUIRED_FIELDS = |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3579 | Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3580 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3581 | uint32_t inCount = mAccumulator.pointerCount; |
| 3582 | uint32_t outCount = 0; |
| 3583 | bool havePointerIds = true; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3584 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3585 | mCurrentTouch.clear(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3586 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3587 | for (uint32_t inIndex = 0; inIndex < inCount; inIndex++) { |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3588 | const Accumulator::Pointer& inPointer = mAccumulator.pointers[inIndex]; |
| 3589 | uint32_t fields = inPointer.fields; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3590 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3591 | if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) { |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3592 | // Some drivers send empty MT sync packets without X / Y to indicate a pointer up. |
| 3593 | // Drop this finger. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3594 | continue; |
| 3595 | } |
| 3596 | |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3597 | PointerData& outPointer = mCurrentTouch.pointers[outCount]; |
| 3598 | outPointer.x = inPointer.absMTPositionX; |
| 3599 | outPointer.y = inPointer.absMTPositionY; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3600 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3601 | if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) { |
| 3602 | if (inPointer.absMTPressure <= 0) { |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 3603 | // Some devices send sync packets with X / Y but with a 0 pressure to indicate |
| 3604 | // a pointer going up. Drop this finger. |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3605 | continue; |
| 3606 | } |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3607 | outPointer.pressure = inPointer.absMTPressure; |
| 3608 | } else { |
| 3609 | // Default pressure to 0 if absent. |
| 3610 | outPointer.pressure = 0; |
| 3611 | } |
| 3612 | |
| 3613 | if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) { |
| 3614 | if (inPointer.absMTTouchMajor <= 0) { |
| 3615 | // Some devices send sync packets with X / Y but with a 0 touch major to indicate |
| 3616 | // a pointer going up. Drop this finger. |
| 3617 | continue; |
| 3618 | } |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3619 | outPointer.touchMajor = inPointer.absMTTouchMajor; |
| 3620 | } else { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3621 | // Default touch area to 0 if absent. |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3622 | outPointer.touchMajor = 0; |
| 3623 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3624 | |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3625 | if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) { |
| 3626 | outPointer.touchMinor = inPointer.absMTTouchMinor; |
| 3627 | } else { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3628 | // Assume touch area is circular. |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3629 | outPointer.touchMinor = outPointer.touchMajor; |
| 3630 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3631 | |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3632 | if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) { |
| 3633 | outPointer.toolMajor = inPointer.absMTWidthMajor; |
| 3634 | } else { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3635 | // Default tool area to 0 if absent. |
| 3636 | outPointer.toolMajor = 0; |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3637 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3638 | |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3639 | if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) { |
| 3640 | outPointer.toolMinor = inPointer.absMTWidthMinor; |
| 3641 | } else { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3642 | // Assume tool area is circular. |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3643 | outPointer.toolMinor = outPointer.toolMajor; |
| 3644 | } |
| 3645 | |
| 3646 | if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) { |
| 3647 | outPointer.orientation = inPointer.absMTOrientation; |
| 3648 | } else { |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3649 | // Default orientation to vertical if absent. |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3650 | outPointer.orientation = 0; |
| 3651 | } |
| 3652 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3653 | // Assign pointer id using tracking id if available. |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3654 | if (havePointerIds) { |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3655 | if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) { |
| 3656 | uint32_t id = uint32_t(inPointer.absMTTrackingId); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3657 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3658 | if (id > MAX_POINTER_ID) { |
| 3659 | #if DEBUG_POINTERS |
| 3660 | LOGD("Pointers: Ignoring driver provided pointer id %d because " |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 3661 | "it is larger than max supported id %d", |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3662 | id, MAX_POINTER_ID); |
| 3663 | #endif |
| 3664 | havePointerIds = false; |
| 3665 | } |
| 3666 | else { |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3667 | outPointer.id = id; |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3668 | mCurrentTouch.idToIndex[id] = outCount; |
| 3669 | mCurrentTouch.idBits.markBit(id); |
| 3670 | } |
| 3671 | } else { |
| 3672 | havePointerIds = false; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3673 | } |
| 3674 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3675 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3676 | outCount += 1; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3677 | } |
| 3678 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3679 | mCurrentTouch.pointerCount = outCount; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3680 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 3681 | syncTouch(when, havePointerIds); |
Jeff Brown | 2dfd7a7 | 2010-08-17 20:38:35 -0700 | [diff] [blame] | 3682 | |
| 3683 | mAccumulator.clear(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3684 | } |
| 3685 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3686 | void MultiTouchInputMapper::configureRawAxes() { |
| 3687 | TouchInputMapper::configureRawAxes(); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3688 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 3689 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, & mRawAxes.x); |
| 3690 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, & mRawAxes.y); |
| 3691 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, & mRawAxes.touchMajor); |
| 3692 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, & mRawAxes.touchMinor); |
| 3693 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, & mRawAxes.toolMajor); |
| 3694 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, & mRawAxes.toolMinor); |
| 3695 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, & mRawAxes.orientation); |
| 3696 | getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, & mRawAxes.pressure); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 3697 | } |
| 3698 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3699 | |
| 3700 | } // namespace android |