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