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