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