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