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