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