blob: 62251217c3307c9a7defe1c7f3fc6ca6afe9ca14 [file] [log] [blame]
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001//
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 Brown349703e2010-06-22 01:27:15 -070014#define DEBUG_HACKS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070015
16// Log debug messages about virtual key processing.
Jeff Brown349703e2010-06-22 01:27:15 -070017#define DEBUG_VIRTUAL_KEYS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070018
19// Log debug messages about pointers.
Jeff Brown349703e2010-06-22 01:27:15 -070020#define DEBUG_POINTERS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070021
Jeff Brown5c225b12010-06-16 01:53:36 -070022// Log debug messages about pointer assignment calculations.
23#define DEBUG_POINTER_ASSIGNMENT 0
24
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070025#include <cutils/log.h>
26#include <ui/InputReader.h>
27
28#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070029#include <stdlib.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070030#include <unistd.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070031#include <errno.h>
32#include <limits.h>
Jeff Brownc5ed5912010-07-14 18:48:53 -070033#include <math.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070034
Jeff Brown8d608662010-08-30 03:02:23 -070035#define INDENT " "
Jeff Brownef3d7e82010-09-30 14:33:04 -070036#define INDENT2 " "
37#define INDENT3 " "
38#define INDENT4 " "
Jeff Brown8d608662010-08-30 03:02:23 -070039
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070040namespace android {
41
42// --- Static Functions ---
43
44template<typename T>
45inline static T abs(const T& value) {
46 return value < 0 ? - value : value;
47}
48
49template<typename T>
50inline static T min(const T& a, const T& b) {
51 return a < b ? a : b;
52}
53
Jeff Brown5c225b12010-06-16 01:53:36 -070054template<typename T>
55inline static void swap(T& a, T& b) {
56 T temp = a;
57 a = b;
58 b = temp;
59}
60
Jeff Brown8d608662010-08-30 03:02:23 -070061inline static float avg(float x, float y) {
62 return (x + y) / 2;
63}
64
65inline static float pythag(float x, float y) {
66 return sqrtf(x * x + y * y);
67}
68
Jeff Brownef3d7e82010-09-30 14:33:04 -070069static inline const char* toString(bool value) {
70 return value ? "true" : "false";
71}
72
Jeff Brown497a92c2010-09-12 17:55:08 -070073int32_t setEphemeralMetaState(int32_t mask, bool down, int32_t oldMetaState) {
74 int32_t newMetaState;
75 if (down) {
76 newMetaState = oldMetaState | mask;
77 } else {
78 newMetaState = oldMetaState &
79 ~(mask | AMETA_ALT_ON | AMETA_SHIFT_ON | AMETA_CTRL_ON | AMETA_META_ON);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070080 }
81
Jeff Brownc5ed5912010-07-14 18:48:53 -070082 if (newMetaState & (AMETA_ALT_LEFT_ON | AMETA_ALT_RIGHT_ON)) {
83 newMetaState |= AMETA_ALT_ON;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070084 }
85
Jeff Brownc5ed5912010-07-14 18:48:53 -070086 if (newMetaState & (AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_RIGHT_ON)) {
87 newMetaState |= AMETA_SHIFT_ON;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070088 }
89
Jeff Brown497a92c2010-09-12 17:55:08 -070090 if (newMetaState & (AMETA_CTRL_LEFT_ON | AMETA_CTRL_RIGHT_ON)) {
91 newMetaState |= AMETA_CTRL_ON;
92 }
93
94 if (newMetaState & (AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON)) {
95 newMetaState |= AMETA_META_ON;
96 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070097 return newMetaState;
98}
99
Jeff Brown497a92c2010-09-12 17:55:08 -0700100int32_t toggleLockedMetaState(int32_t mask, bool down, int32_t oldMetaState) {
101 if (down) {
102 return oldMetaState;
103 } else {
104 return oldMetaState ^ mask;
105 }
106}
107
108int32_t updateMetaState(int32_t keyCode, bool down, int32_t oldMetaState) {
109 int32_t mask;
110 switch (keyCode) {
111 case AKEYCODE_ALT_LEFT:
112 return setEphemeralMetaState(AMETA_ALT_LEFT_ON, down, oldMetaState);
113 case AKEYCODE_ALT_RIGHT:
114 return setEphemeralMetaState(AMETA_ALT_RIGHT_ON, down, oldMetaState);
115 case AKEYCODE_SHIFT_LEFT:
116 return setEphemeralMetaState(AMETA_SHIFT_LEFT_ON, down, oldMetaState);
117 case AKEYCODE_SHIFT_RIGHT:
118 return setEphemeralMetaState(AMETA_SHIFT_RIGHT_ON, down, oldMetaState);
119 case AKEYCODE_SYM:
120 return setEphemeralMetaState(AMETA_SYM_ON, down, oldMetaState);
121 case AKEYCODE_FUNCTION:
122 return setEphemeralMetaState(AMETA_FUNCTION_ON, down, oldMetaState);
123 case AKEYCODE_CTRL_LEFT:
124 return setEphemeralMetaState(AMETA_CTRL_LEFT_ON, down, oldMetaState);
125 case AKEYCODE_CTRL_RIGHT:
126 return setEphemeralMetaState(AMETA_CTRL_RIGHT_ON, down, oldMetaState);
127 case AKEYCODE_META_LEFT:
128 return setEphemeralMetaState(AMETA_META_LEFT_ON, down, oldMetaState);
129 case AKEYCODE_META_RIGHT:
130 return setEphemeralMetaState(AMETA_META_RIGHT_ON, down, oldMetaState);
131 case AKEYCODE_CAPS_LOCK:
132 return toggleLockedMetaState(AMETA_CAPS_LOCK_LATCHED, down, oldMetaState);
133 case AKEYCODE_NUM_LOCK:
134 return toggleLockedMetaState(AMETA_NUM_LOCK_LATCHED, down, oldMetaState);
135 case AKEYCODE_SCROLL_LOCK:
136 return toggleLockedMetaState(AMETA_SCROLL_LOCK_LATCHED, down, oldMetaState);
137 default:
138 return oldMetaState;
139 }
140}
141
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700142static const int32_t keyCodeRotationMap[][4] = {
143 // key codes enumerated counter-clockwise with the original (unrotated) key first
144 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd0358292010-06-30 16:10:35 -0700145 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
146 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
147 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
148 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700149};
150static const int keyCodeRotationMapSize =
151 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
152
153int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700154 if (orientation != InputReaderPolicyInterface::ROTATION_0) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700155 for (int i = 0; i < keyCodeRotationMapSize; i++) {
156 if (keyCode == keyCodeRotationMap[i][0]) {
157 return keyCodeRotationMap[i][orientation];
158 }
159 }
160 }
161 return keyCode;
162}
163
Jeff Brown6d0fec22010-07-23 21:28:06 -0700164static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
165 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
166}
167
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700168
Jeff Brown8d608662010-08-30 03:02:23 -0700169// --- InputDeviceCalibration ---
170
171InputDeviceCalibration::InputDeviceCalibration() {
172}
173
174void InputDeviceCalibration::clear() {
175 mProperties.clear();
176}
177
178void InputDeviceCalibration::addProperty(const String8& key, const String8& value) {
179 mProperties.add(key, value);
180}
181
182bool InputDeviceCalibration::tryGetProperty(const String8& key, String8& outValue) const {
183 ssize_t index = mProperties.indexOfKey(key);
184 if (index < 0) {
185 return false;
186 }
187
188 outValue = mProperties.valueAt(index);
189 return true;
190}
191
192bool InputDeviceCalibration::tryGetProperty(const String8& key, int32_t& outValue) const {
193 String8 stringValue;
194 if (! tryGetProperty(key, stringValue) || stringValue.length() == 0) {
195 return false;
196 }
197
198 char* end;
199 int value = strtol(stringValue.string(), & end, 10);
200 if (*end != '\0') {
201 LOGW("Input device calibration key '%s' has invalid value '%s'. Expected an integer.",
202 key.string(), stringValue.string());
203 return false;
204 }
205 outValue = value;
206 return true;
207}
208
209bool InputDeviceCalibration::tryGetProperty(const String8& key, float& outValue) const {
210 String8 stringValue;
211 if (! tryGetProperty(key, stringValue) || stringValue.length() == 0) {
212 return false;
213 }
214
215 char* end;
216 float value = strtof(stringValue.string(), & end);
217 if (*end != '\0') {
218 LOGW("Input device calibration key '%s' has invalid value '%s'. Expected a float.",
219 key.string(), stringValue.string());
220 return false;
221 }
222 outValue = value;
223 return true;
224}
225
226
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700227// --- InputReader ---
228
229InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700230 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700231 const sp<InputDispatcherInterface>& dispatcher) :
Jeff Brown6d0fec22010-07-23 21:28:06 -0700232 mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher),
233 mGlobalMetaState(0) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700234 configureExcludedDevices();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700235 updateGlobalMetaState();
236 updateInputConfiguration();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700237}
238
239InputReader::~InputReader() {
240 for (size_t i = 0; i < mDevices.size(); i++) {
241 delete mDevices.valueAt(i);
242 }
243}
244
245void InputReader::loopOnce() {
246 RawEvent rawEvent;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700247 mEventHub->getEvent(& rawEvent);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700248
249#if DEBUG_RAW_EVENTS
250 LOGD("Input event: device=0x%x type=0x%x scancode=%d keycode=%d value=%d",
251 rawEvent.deviceId, rawEvent.type, rawEvent.scanCode, rawEvent.keyCode,
252 rawEvent.value);
253#endif
254
255 process(& rawEvent);
256}
257
258void InputReader::process(const RawEvent* rawEvent) {
259 switch (rawEvent->type) {
260 case EventHubInterface::DEVICE_ADDED:
Jeff Brown7342bb92010-10-01 18:55:43 -0700261 addDevice(rawEvent->deviceId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700262 break;
263
264 case EventHubInterface::DEVICE_REMOVED:
Jeff Brown7342bb92010-10-01 18:55:43 -0700265 removeDevice(rawEvent->deviceId);
266 break;
267
268 case EventHubInterface::FINISHED_DEVICE_SCAN:
269 handleConfigurationChanged();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700270 break;
271
Jeff Brown6d0fec22010-07-23 21:28:06 -0700272 default:
273 consumeEvent(rawEvent);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700274 break;
275 }
276}
277
Jeff Brown7342bb92010-10-01 18:55:43 -0700278void InputReader::addDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700279 String8 name = mEventHub->getDeviceName(deviceId);
280 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
281
282 InputDevice* device = createDevice(deviceId, name, classes);
283 device->configure();
284
Jeff Brown8d608662010-08-30 03:02:23 -0700285 if (device->isIgnored()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700286 LOGI("Device added: id=0x%x, name=%s (ignored non-input device)", deviceId, name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700287 } else {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700288 LOGI("Device added: id=0x%x, name=%s, sources=%08x", deviceId, name.string(),
289 device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700290 }
291
Jeff Brown6d0fec22010-07-23 21:28:06 -0700292 bool added = false;
293 { // acquire device registry writer lock
294 RWLock::AutoWLock _wl(mDeviceRegistryLock);
295
296 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
297 if (deviceIndex < 0) {
298 mDevices.add(deviceId, device);
299 added = true;
300 }
301 } // release device registry writer lock
302
303 if (! added) {
304 LOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
305 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700306 return;
307 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700308}
309
Jeff Brown7342bb92010-10-01 18:55:43 -0700310void InputReader::removeDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700311 bool removed = false;
312 InputDevice* device = NULL;
313 { // acquire device registry writer lock
314 RWLock::AutoWLock _wl(mDeviceRegistryLock);
315
316 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
317 if (deviceIndex >= 0) {
318 device = mDevices.valueAt(deviceIndex);
319 mDevices.removeItemsAt(deviceIndex, 1);
320 removed = true;
321 }
322 } // release device registry writer lock
323
324 if (! removed) {
325 LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700326 return;
327 }
328
Jeff Brown6d0fec22010-07-23 21:28:06 -0700329 if (device->isIgnored()) {
330 LOGI("Device removed: id=0x%x, name=%s (ignored non-input device)",
331 device->getId(), device->getName().string());
332 } else {
333 LOGI("Device removed: id=0x%x, name=%s, sources=%08x",
334 device->getId(), device->getName().string(), device->getSources());
335 }
336
Jeff Brown8d608662010-08-30 03:02:23 -0700337 device->reset();
338
Jeff Brown6d0fec22010-07-23 21:28:06 -0700339 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700340}
341
Jeff Brown6d0fec22010-07-23 21:28:06 -0700342InputDevice* InputReader::createDevice(int32_t deviceId, const String8& name, uint32_t classes) {
343 InputDevice* device = new InputDevice(this, deviceId, name);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700344
Jeff Brown6d0fec22010-07-23 21:28:06 -0700345 const int32_t associatedDisplayId = 0; // FIXME: hardcoded for current single-display devices
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700346
Jeff Brown6d0fec22010-07-23 21:28:06 -0700347 // Switch-like devices.
348 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
349 device->addMapper(new SwitchInputMapper(device));
350 }
351
352 // Keyboard-like devices.
353 uint32_t keyboardSources = 0;
354 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
355 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
356 keyboardSources |= AINPUT_SOURCE_KEYBOARD;
357 }
358 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
359 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
360 }
361 if (classes & INPUT_DEVICE_CLASS_DPAD) {
362 keyboardSources |= AINPUT_SOURCE_DPAD;
363 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700364
365 if (keyboardSources != 0) {
366 device->addMapper(new KeyboardInputMapper(device,
367 associatedDisplayId, keyboardSources, keyboardType));
368 }
369
370 // Trackball-like devices.
371 if (classes & INPUT_DEVICE_CLASS_TRACKBALL) {
372 device->addMapper(new TrackballInputMapper(device, associatedDisplayId));
373 }
374
375 // Touchscreen-like devices.
376 if (classes & INPUT_DEVICE_CLASS_TOUCHSCREEN_MT) {
377 device->addMapper(new MultiTouchInputMapper(device, associatedDisplayId));
378 } else if (classes & INPUT_DEVICE_CLASS_TOUCHSCREEN) {
379 device->addMapper(new SingleTouchInputMapper(device, associatedDisplayId));
380 }
381
382 return device;
383}
384
385void InputReader::consumeEvent(const RawEvent* rawEvent) {
386 int32_t deviceId = rawEvent->deviceId;
387
388 { // acquire device registry reader lock
389 RWLock::AutoRLock _rl(mDeviceRegistryLock);
390
391 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
392 if (deviceIndex < 0) {
393 LOGW("Discarding event for unknown deviceId %d.", deviceId);
394 return;
395 }
396
397 InputDevice* device = mDevices.valueAt(deviceIndex);
398 if (device->isIgnored()) {
399 //LOGD("Discarding event for ignored deviceId %d.", deviceId);
400 return;
401 }
402
403 device->process(rawEvent);
404 } // release device registry reader lock
405}
406
Jeff Brown7342bb92010-10-01 18:55:43 -0700407void InputReader::handleConfigurationChanged() {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700408 // Reset global meta state because it depends on the list of all configured devices.
409 updateGlobalMetaState();
410
411 // Update input configuration.
412 updateInputConfiguration();
413
414 // Enqueue configuration changed.
Jeff Brown7342bb92010-10-01 18:55:43 -0700415 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700416 mDispatcher->notifyConfigurationChanged(when);
417}
418
419void InputReader::configureExcludedDevices() {
420 Vector<String8> excludedDeviceNames;
421 mPolicy->getExcludedDeviceNames(excludedDeviceNames);
422
423 for (size_t i = 0; i < excludedDeviceNames.size(); i++) {
424 mEventHub->addExcludedDevice(excludedDeviceNames[i]);
425 }
426}
427
428void InputReader::updateGlobalMetaState() {
429 { // acquire state lock
430 AutoMutex _l(mStateLock);
431
432 mGlobalMetaState = 0;
433
434 { // acquire device registry reader lock
435 RWLock::AutoRLock _rl(mDeviceRegistryLock);
436
437 for (size_t i = 0; i < mDevices.size(); i++) {
438 InputDevice* device = mDevices.valueAt(i);
439 mGlobalMetaState |= device->getMetaState();
440 }
441 } // release device registry reader lock
442 } // release state lock
443}
444
445int32_t InputReader::getGlobalMetaState() {
446 { // acquire state lock
447 AutoMutex _l(mStateLock);
448
449 return mGlobalMetaState;
450 } // release state lock
451}
452
453void InputReader::updateInputConfiguration() {
454 { // acquire state lock
455 AutoMutex _l(mStateLock);
456
457 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
458 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
459 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
460 { // acquire device registry reader lock
461 RWLock::AutoRLock _rl(mDeviceRegistryLock);
462
463 InputDeviceInfo deviceInfo;
464 for (size_t i = 0; i < mDevices.size(); i++) {
465 InputDevice* device = mDevices.valueAt(i);
466 device->getDeviceInfo(& deviceInfo);
467 uint32_t sources = deviceInfo.getSources();
468
469 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
470 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
471 }
472 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
473 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
474 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
475 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
476 }
477 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
478 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700479 }
480 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700481 } // release device registry reader lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700482
Jeff Brown6d0fec22010-07-23 21:28:06 -0700483 mInputConfiguration.touchScreen = touchScreenConfig;
484 mInputConfiguration.keyboard = keyboardConfig;
485 mInputConfiguration.navigation = navigationConfig;
486 } // release state lock
487}
488
489void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
490 { // acquire state lock
491 AutoMutex _l(mStateLock);
492
493 *outConfiguration = mInputConfiguration;
494 } // release state lock
495}
496
497status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
498 { // acquire device registry reader lock
499 RWLock::AutoRLock _rl(mDeviceRegistryLock);
500
501 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
502 if (deviceIndex < 0) {
503 return NAME_NOT_FOUND;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700504 }
505
Jeff Brown6d0fec22010-07-23 21:28:06 -0700506 InputDevice* device = mDevices.valueAt(deviceIndex);
507 if (device->isIgnored()) {
508 return NAME_NOT_FOUND;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700509 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700510
511 device->getDeviceInfo(outDeviceInfo);
512 return OK;
513 } // release device registy reader lock
514}
515
516void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
517 outDeviceIds.clear();
518
519 { // acquire device registry reader lock
520 RWLock::AutoRLock _rl(mDeviceRegistryLock);
521
522 size_t numDevices = mDevices.size();
523 for (size_t i = 0; i < numDevices; i++) {
524 InputDevice* device = mDevices.valueAt(i);
525 if (! device->isIgnored()) {
526 outDeviceIds.add(device->getId());
527 }
528 }
529 } // release device registy reader lock
530}
531
532int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
533 int32_t keyCode) {
534 return getState(deviceId, sourceMask, keyCode, & InputDevice::getKeyCodeState);
535}
536
537int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
538 int32_t scanCode) {
539 return getState(deviceId, sourceMask, scanCode, & InputDevice::getScanCodeState);
540}
541
542int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
543 return getState(deviceId, sourceMask, switchCode, & InputDevice::getSwitchState);
544}
545
546int32_t InputReader::getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
547 GetStateFunc getStateFunc) {
548 { // acquire device registry reader lock
549 RWLock::AutoRLock _rl(mDeviceRegistryLock);
550
551 int32_t result = AKEY_STATE_UNKNOWN;
552 if (deviceId >= 0) {
553 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
554 if (deviceIndex >= 0) {
555 InputDevice* device = mDevices.valueAt(deviceIndex);
556 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
557 result = (device->*getStateFunc)(sourceMask, code);
558 }
559 }
560 } else {
561 size_t numDevices = mDevices.size();
562 for (size_t i = 0; i < numDevices; i++) {
563 InputDevice* device = mDevices.valueAt(i);
564 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
565 result = (device->*getStateFunc)(sourceMask, code);
566 if (result >= AKEY_STATE_DOWN) {
567 return result;
568 }
569 }
570 }
571 }
572 return result;
573 } // release device registy reader lock
574}
575
576bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
577 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
578 memset(outFlags, 0, numCodes);
579 return markSupportedKeyCodes(deviceId, sourceMask, numCodes, keyCodes, outFlags);
580}
581
582bool InputReader::markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
583 const int32_t* keyCodes, uint8_t* outFlags) {
584 { // acquire device registry reader lock
585 RWLock::AutoRLock _rl(mDeviceRegistryLock);
586 bool result = false;
587 if (deviceId >= 0) {
588 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
589 if (deviceIndex >= 0) {
590 InputDevice* device = mDevices.valueAt(deviceIndex);
591 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
592 result = device->markSupportedKeyCodes(sourceMask,
593 numCodes, keyCodes, outFlags);
594 }
595 }
596 } else {
597 size_t numDevices = mDevices.size();
598 for (size_t i = 0; i < numDevices; i++) {
599 InputDevice* device = mDevices.valueAt(i);
600 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
601 result |= device->markSupportedKeyCodes(sourceMask,
602 numCodes, keyCodes, outFlags);
603 }
604 }
605 }
606 return result;
607 } // release device registy reader lock
608}
609
Jeff Brownb88102f2010-09-08 11:49:43 -0700610void InputReader::dump(String8& dump) {
Jeff Brownf2f487182010-10-01 17:46:21 -0700611 mEventHub->dump(dump);
612 dump.append("\n");
613
614 dump.append("Input Reader State:\n");
615
Jeff Brownef3d7e82010-09-30 14:33:04 -0700616 { // acquire device registry reader lock
617 RWLock::AutoRLock _rl(mDeviceRegistryLock);
Jeff Brownb88102f2010-09-08 11:49:43 -0700618
Jeff Brownef3d7e82010-09-30 14:33:04 -0700619 for (size_t i = 0; i < mDevices.size(); i++) {
620 mDevices.valueAt(i)->dump(dump);
Jeff Brownb88102f2010-09-08 11:49:43 -0700621 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700622 } // release device registy reader lock
Jeff Brownb88102f2010-09-08 11:49:43 -0700623}
624
Jeff Brown6d0fec22010-07-23 21:28:06 -0700625
626// --- InputReaderThread ---
627
628InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
629 Thread(/*canCallJava*/ true), mReader(reader) {
630}
631
632InputReaderThread::~InputReaderThread() {
633}
634
635bool InputReaderThread::threadLoop() {
636 mReader->loopOnce();
637 return true;
638}
639
640
641// --- InputDevice ---
642
643InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) :
644 mContext(context), mId(id), mName(name), mSources(0) {
645}
646
647InputDevice::~InputDevice() {
648 size_t numMappers = mMappers.size();
649 for (size_t i = 0; i < numMappers; i++) {
650 delete mMappers[i];
651 }
652 mMappers.clear();
653}
654
Jeff Brownef3d7e82010-09-30 14:33:04 -0700655static void dumpMotionRange(String8& dump, const InputDeviceInfo& deviceInfo,
656 int32_t rangeType, const char* name) {
657 const InputDeviceInfo::MotionRange* range = deviceInfo.getMotionRange(rangeType);
658 if (range) {
659 dump.appendFormat(INDENT3 "%s: min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
660 name, range->min, range->max, range->flat, range->fuzz);
661 }
662}
663
664void InputDevice::dump(String8& dump) {
665 InputDeviceInfo deviceInfo;
666 getDeviceInfo(& deviceInfo);
667
668 dump.appendFormat(INDENT "Device 0x%x: %s\n", deviceInfo.getId(),
669 deviceInfo.getName().string());
670 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
671 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
672 if (!deviceInfo.getMotionRanges().isEmpty()) {
673 dump.append(INDENT2 "Motion Ranges:\n");
674 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_X, "X");
675 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_Y, "Y");
676 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_PRESSURE, "Pressure");
677 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_SIZE, "Size");
678 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOUCH_MAJOR, "TouchMajor");
679 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOUCH_MINOR, "TouchMinor");
680 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOOL_MAJOR, "ToolMajor");
681 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOOL_MINOR, "ToolMinor");
682 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_ORIENTATION, "Orientation");
683 }
684
685 size_t numMappers = mMappers.size();
686 for (size_t i = 0; i < numMappers; i++) {
687 InputMapper* mapper = mMappers[i];
688 mapper->dump(dump);
689 }
690}
691
Jeff Brown6d0fec22010-07-23 21:28:06 -0700692void InputDevice::addMapper(InputMapper* mapper) {
693 mMappers.add(mapper);
694}
695
696void InputDevice::configure() {
Jeff Brown8d608662010-08-30 03:02:23 -0700697 if (! isIgnored()) {
698 mContext->getPolicy()->getInputDeviceCalibration(mName, mCalibration);
699 }
700
Jeff Brown6d0fec22010-07-23 21:28:06 -0700701 mSources = 0;
702
703 size_t numMappers = mMappers.size();
704 for (size_t i = 0; i < numMappers; i++) {
705 InputMapper* mapper = mMappers[i];
706 mapper->configure();
707 mSources |= mapper->getSources();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700708 }
709}
710
Jeff Brown6d0fec22010-07-23 21:28:06 -0700711void InputDevice::reset() {
712 size_t numMappers = mMappers.size();
713 for (size_t i = 0; i < numMappers; i++) {
714 InputMapper* mapper = mMappers[i];
715 mapper->reset();
716 }
717}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700718
Jeff Brown6d0fec22010-07-23 21:28:06 -0700719void InputDevice::process(const RawEvent* rawEvent) {
720 size_t numMappers = mMappers.size();
721 for (size_t i = 0; i < numMappers; i++) {
722 InputMapper* mapper = mMappers[i];
723 mapper->process(rawEvent);
724 }
725}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700726
Jeff Brown6d0fec22010-07-23 21:28:06 -0700727void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
728 outDeviceInfo->initialize(mId, mName);
729
730 size_t numMappers = mMappers.size();
731 for (size_t i = 0; i < numMappers; i++) {
732 InputMapper* mapper = mMappers[i];
733 mapper->populateDeviceInfo(outDeviceInfo);
734 }
735}
736
737int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
738 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
739}
740
741int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
742 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
743}
744
745int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
746 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
747}
748
749int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
750 int32_t result = AKEY_STATE_UNKNOWN;
751 size_t numMappers = mMappers.size();
752 for (size_t i = 0; i < numMappers; i++) {
753 InputMapper* mapper = mMappers[i];
754 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
755 result = (mapper->*getStateFunc)(sourceMask, code);
756 if (result >= AKEY_STATE_DOWN) {
757 return result;
758 }
759 }
760 }
761 return result;
762}
763
764bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
765 const int32_t* keyCodes, uint8_t* outFlags) {
766 bool result = false;
767 size_t numMappers = mMappers.size();
768 for (size_t i = 0; i < numMappers; i++) {
769 InputMapper* mapper = mMappers[i];
770 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
771 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
772 }
773 }
774 return result;
775}
776
777int32_t InputDevice::getMetaState() {
778 int32_t result = 0;
779 size_t numMappers = mMappers.size();
780 for (size_t i = 0; i < numMappers; i++) {
781 InputMapper* mapper = mMappers[i];
782 result |= mapper->getMetaState();
783 }
784 return result;
785}
786
787
788// --- InputMapper ---
789
790InputMapper::InputMapper(InputDevice* device) :
791 mDevice(device), mContext(device->getContext()) {
792}
793
794InputMapper::~InputMapper() {
795}
796
797void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
798 info->addSource(getSources());
799}
800
Jeff Brownef3d7e82010-09-30 14:33:04 -0700801void InputMapper::dump(String8& dump) {
802}
803
Jeff Brown6d0fec22010-07-23 21:28:06 -0700804void InputMapper::configure() {
805}
806
807void InputMapper::reset() {
808}
809
810int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
811 return AKEY_STATE_UNKNOWN;
812}
813
814int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
815 return AKEY_STATE_UNKNOWN;
816}
817
818int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
819 return AKEY_STATE_UNKNOWN;
820}
821
822bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
823 const int32_t* keyCodes, uint8_t* outFlags) {
824 return false;
825}
826
827int32_t InputMapper::getMetaState() {
828 return 0;
829}
830
Jeff Brown6d0fec22010-07-23 21:28:06 -0700831
832// --- SwitchInputMapper ---
833
834SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
835 InputMapper(device) {
836}
837
838SwitchInputMapper::~SwitchInputMapper() {
839}
840
841uint32_t SwitchInputMapper::getSources() {
842 return 0;
843}
844
845void SwitchInputMapper::process(const RawEvent* rawEvent) {
846 switch (rawEvent->type) {
847 case EV_SW:
848 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
849 break;
850 }
851}
852
853void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownb6997262010-10-08 22:31:17 -0700854 getDispatcher()->notifySwitch(when, switchCode, switchValue, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700855}
856
857int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
858 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
859}
860
861
862// --- KeyboardInputMapper ---
863
864KeyboardInputMapper::KeyboardInputMapper(InputDevice* device, int32_t associatedDisplayId,
865 uint32_t sources, int32_t keyboardType) :
866 InputMapper(device), mAssociatedDisplayId(associatedDisplayId), mSources(sources),
867 mKeyboardType(keyboardType) {
Jeff Brown6328cdc2010-07-29 18:18:33 -0700868 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700869}
870
871KeyboardInputMapper::~KeyboardInputMapper() {
872}
873
Jeff Brown6328cdc2010-07-29 18:18:33 -0700874void KeyboardInputMapper::initializeLocked() {
875 mLocked.metaState = AMETA_NONE;
876 mLocked.downTime = 0;
Jeff Brown497a92c2010-09-12 17:55:08 -0700877
878 initializeLedStateLocked(mLocked.capsLockLedState, LED_CAPSL);
879 initializeLedStateLocked(mLocked.numLockLedState, LED_NUML);
880 initializeLedStateLocked(mLocked.scrollLockLedState, LED_SCROLLL);
881
882 updateLedStateLocked(true);
883}
884
885void KeyboardInputMapper::initializeLedStateLocked(LockedState::LedState& ledState, int32_t led) {
886 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
887 ledState.on = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700888}
889
890uint32_t KeyboardInputMapper::getSources() {
891 return mSources;
892}
893
894void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
895 InputMapper::populateDeviceInfo(info);
896
897 info->setKeyboardType(mKeyboardType);
898}
899
Jeff Brownef3d7e82010-09-30 14:33:04 -0700900void KeyboardInputMapper::dump(String8& dump) {
901 { // acquire lock
902 AutoMutex _l(mLock);
903 dump.append(INDENT2 "Keyboard Input Mapper:\n");
904 dump.appendFormat(INDENT3 "AssociatedDisplayId: %d\n", mAssociatedDisplayId);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700905 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
906 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mLocked.keyDowns.size());
907 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mLocked.metaState);
908 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
909 } // release lock
910}
911
Jeff Brown6d0fec22010-07-23 21:28:06 -0700912void KeyboardInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -0700913 for (;;) {
914 int32_t keyCode, scanCode;
915 { // acquire lock
916 AutoMutex _l(mLock);
917
918 // Synthesize key up event on reset if keys are currently down.
919 if (mLocked.keyDowns.isEmpty()) {
920 initializeLocked();
921 break; // done
922 }
923
924 const KeyDown& keyDown = mLocked.keyDowns.top();
925 keyCode = keyDown.keyCode;
926 scanCode = keyDown.scanCode;
927 } // release lock
928
Jeff Brown6d0fec22010-07-23 21:28:06 -0700929 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700930 processKey(when, false, keyCode, scanCode, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700931 }
932
933 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700934 getContext()->updateGlobalMetaState();
935}
936
937void KeyboardInputMapper::process(const RawEvent* rawEvent) {
938 switch (rawEvent->type) {
939 case EV_KEY: {
940 int32_t scanCode = rawEvent->scanCode;
941 if (isKeyboardOrGamepadKey(scanCode)) {
942 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
943 rawEvent->flags);
944 }
945 break;
946 }
947 }
948}
949
950bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
951 return scanCode < BTN_MOUSE
952 || scanCode >= KEY_OK
953 || (scanCode >= BTN_GAMEPAD && scanCode < BTN_DIGI);
954}
955
Jeff Brown6328cdc2010-07-29 18:18:33 -0700956void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
957 int32_t scanCode, uint32_t policyFlags) {
958 int32_t newMetaState;
959 nsecs_t downTime;
960 bool metaStateChanged = false;
961
962 { // acquire lock
963 AutoMutex _l(mLock);
964
965 if (down) {
966 // Rotate key codes according to orientation if needed.
967 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
968 if (mAssociatedDisplayId >= 0) {
969 int32_t orientation;
970 if (! getPolicy()->getDisplayInfo(mAssociatedDisplayId, NULL, NULL, & orientation)) {
971 return;
972 }
973
974 keyCode = rotateKeyCode(keyCode, orientation);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700975 }
976
Jeff Brown6328cdc2010-07-29 18:18:33 -0700977 // Add key down.
978 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
979 if (keyDownIndex >= 0) {
980 // key repeat, be sure to use same keycode as before in case of rotation
981 keyCode = mLocked.keyDowns.top().keyCode;
982 } else {
983 // key down
984 mLocked.keyDowns.push();
985 KeyDown& keyDown = mLocked.keyDowns.editTop();
986 keyDown.keyCode = keyCode;
987 keyDown.scanCode = scanCode;
988 }
989
990 mLocked.downTime = when;
991 } else {
992 // Remove key down.
993 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
994 if (keyDownIndex >= 0) {
995 // key up, be sure to use same keycode as before in case of rotation
996 keyCode = mLocked.keyDowns.top().keyCode;
997 mLocked.keyDowns.removeAt(size_t(keyDownIndex));
998 } else {
999 // key was not actually down
1000 LOGI("Dropping key up from device %s because the key was not down. "
1001 "keyCode=%d, scanCode=%d",
1002 getDeviceName().string(), keyCode, scanCode);
1003 return;
1004 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001005 }
1006
Jeff Brown6328cdc2010-07-29 18:18:33 -07001007 int32_t oldMetaState = mLocked.metaState;
1008 newMetaState = updateMetaState(keyCode, down, oldMetaState);
1009 if (oldMetaState != newMetaState) {
1010 mLocked.metaState = newMetaState;
1011 metaStateChanged = true;
Jeff Brown497a92c2010-09-12 17:55:08 -07001012 updateLedStateLocked(false);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001013 }
Jeff Brownfd0358292010-06-30 16:10:35 -07001014
Jeff Brown6328cdc2010-07-29 18:18:33 -07001015 downTime = mLocked.downTime;
1016 } // release lock
1017
1018 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001019 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001020 }
1021
Jeff Brown497a92c2010-09-12 17:55:08 -07001022 if (policyFlags & POLICY_FLAG_FUNCTION) {
1023 newMetaState |= AMETA_FUNCTION_ON;
1024 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001025 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001026 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1027 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001028}
1029
Jeff Brown6328cdc2010-07-29 18:18:33 -07001030ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) {
1031 size_t n = mLocked.keyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001032 for (size_t i = 0; i < n; i++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001033 if (mLocked.keyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001034 return i;
1035 }
1036 }
1037 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001038}
1039
Jeff Brown6d0fec22010-07-23 21:28:06 -07001040int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1041 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1042}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001043
Jeff Brown6d0fec22010-07-23 21:28:06 -07001044int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1045 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1046}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001047
Jeff Brown6d0fec22010-07-23 21:28:06 -07001048bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1049 const int32_t* keyCodes, uint8_t* outFlags) {
1050 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1051}
1052
1053int32_t KeyboardInputMapper::getMetaState() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001054 { // acquire lock
1055 AutoMutex _l(mLock);
1056 return mLocked.metaState;
1057 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001058}
1059
Jeff Brown497a92c2010-09-12 17:55:08 -07001060void KeyboardInputMapper::updateLedStateLocked(bool reset) {
1061 updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL,
1062 AMETA_CAPS_LOCK_LATCHED, reset);
1063 updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML,
1064 AMETA_NUM_LOCK_LATCHED, reset);
1065 updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL,
1066 AMETA_SCROLL_LOCK_LATCHED, reset);
1067}
1068
1069void KeyboardInputMapper::updateLedStateForModifierLocked(LockedState::LedState& ledState,
1070 int32_t led, int32_t modifier, bool reset) {
1071 if (ledState.avail) {
1072 bool desiredState = (mLocked.metaState & modifier) != 0;
1073 if (ledState.on != desiredState) {
1074 getEventHub()->setLedState(getDeviceId(), led, desiredState);
1075 ledState.on = desiredState;
1076 }
1077 }
1078}
1079
Jeff Brown6d0fec22010-07-23 21:28:06 -07001080
1081// --- TrackballInputMapper ---
1082
1083TrackballInputMapper::TrackballInputMapper(InputDevice* device, int32_t associatedDisplayId) :
1084 InputMapper(device), mAssociatedDisplayId(associatedDisplayId) {
1085 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1086 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1087 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1088 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1089
Jeff Brown6328cdc2010-07-29 18:18:33 -07001090 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001091}
1092
1093TrackballInputMapper::~TrackballInputMapper() {
1094}
1095
1096uint32_t TrackballInputMapper::getSources() {
1097 return AINPUT_SOURCE_TRACKBALL;
1098}
1099
1100void TrackballInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1101 InputMapper::populateDeviceInfo(info);
1102
1103 info->addMotionRange(AINPUT_MOTION_RANGE_X, -1.0f, 1.0f, 0.0f, mXScale);
1104 info->addMotionRange(AINPUT_MOTION_RANGE_Y, -1.0f, 1.0f, 0.0f, mYScale);
1105}
1106
Jeff Brownef3d7e82010-09-30 14:33:04 -07001107void TrackballInputMapper::dump(String8& dump) {
1108 { // acquire lock
1109 AutoMutex _l(mLock);
1110 dump.append(INDENT2 "Trackball Input Mapper:\n");
1111 dump.appendFormat(INDENT3 "AssociatedDisplayId: %d\n", mAssociatedDisplayId);
1112 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1113 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
1114 dump.appendFormat(INDENT3 "Down: %s\n", toString(mLocked.down));
1115 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1116 } // release lock
1117}
1118
Jeff Brown6328cdc2010-07-29 18:18:33 -07001119void TrackballInputMapper::initializeLocked() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001120 mAccumulator.clear();
1121
Jeff Brown6328cdc2010-07-29 18:18:33 -07001122 mLocked.down = false;
1123 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001124}
1125
1126void TrackballInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001127 for (;;) {
1128 { // acquire lock
1129 AutoMutex _l(mLock);
1130
1131 if (! mLocked.down) {
1132 initializeLocked();
1133 break; // done
1134 }
1135 } // release lock
1136
1137 // Synthesize trackball button up event on reset.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001138 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001139 mAccumulator.fields = Accumulator::FIELD_BTN_MOUSE;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001140 mAccumulator.btnMouse = false;
1141 sync(when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001142 }
1143
Jeff Brown6d0fec22010-07-23 21:28:06 -07001144 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001145}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001146
Jeff Brown6d0fec22010-07-23 21:28:06 -07001147void TrackballInputMapper::process(const RawEvent* rawEvent) {
1148 switch (rawEvent->type) {
1149 case EV_KEY:
1150 switch (rawEvent->scanCode) {
1151 case BTN_MOUSE:
1152 mAccumulator.fields |= Accumulator::FIELD_BTN_MOUSE;
1153 mAccumulator.btnMouse = rawEvent->value != 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001154 // Sync now since BTN_MOUSE is not necessarily followed by SYN_REPORT and
1155 // we need to ensure that we report the up/down promptly.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001156 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001157 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001158 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001159 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001160
Jeff Brown6d0fec22010-07-23 21:28:06 -07001161 case EV_REL:
1162 switch (rawEvent->scanCode) {
1163 case REL_X:
1164 mAccumulator.fields |= Accumulator::FIELD_REL_X;
1165 mAccumulator.relX = rawEvent->value;
1166 break;
1167 case REL_Y:
1168 mAccumulator.fields |= Accumulator::FIELD_REL_Y;
1169 mAccumulator.relY = rawEvent->value;
1170 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001171 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001172 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001173
Jeff Brown6d0fec22010-07-23 21:28:06 -07001174 case EV_SYN:
1175 switch (rawEvent->scanCode) {
1176 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001177 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001178 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001179 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001180 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001181 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001182}
1183
Jeff Brown6d0fec22010-07-23 21:28:06 -07001184void TrackballInputMapper::sync(nsecs_t when) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001185 uint32_t fields = mAccumulator.fields;
1186 if (fields == 0) {
1187 return; // no new state changes, so nothing to do
1188 }
1189
Jeff Brown6328cdc2010-07-29 18:18:33 -07001190 int motionEventAction;
1191 PointerCoords pointerCoords;
1192 nsecs_t downTime;
1193 { // acquire lock
1194 AutoMutex _l(mLock);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001195
Jeff Brown6328cdc2010-07-29 18:18:33 -07001196 bool downChanged = fields & Accumulator::FIELD_BTN_MOUSE;
1197
1198 if (downChanged) {
1199 if (mAccumulator.btnMouse) {
1200 mLocked.down = true;
1201 mLocked.downTime = when;
1202 } else {
1203 mLocked.down = false;
1204 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001205 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001206
Jeff Brown6328cdc2010-07-29 18:18:33 -07001207 downTime = mLocked.downTime;
1208 float x = fields & Accumulator::FIELD_REL_X ? mAccumulator.relX * mXScale : 0.0f;
1209 float y = fields & Accumulator::FIELD_REL_Y ? mAccumulator.relY * mYScale : 0.0f;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001210
Jeff Brown6328cdc2010-07-29 18:18:33 -07001211 if (downChanged) {
1212 motionEventAction = mLocked.down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001213 } else {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001214 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001215 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001216
Jeff Brown6328cdc2010-07-29 18:18:33 -07001217 pointerCoords.x = x;
1218 pointerCoords.y = y;
1219 pointerCoords.pressure = mLocked.down ? 1.0f : 0.0f;
1220 pointerCoords.size = 0;
1221 pointerCoords.touchMajor = 0;
1222 pointerCoords.touchMinor = 0;
1223 pointerCoords.toolMajor = 0;
1224 pointerCoords.toolMinor = 0;
1225 pointerCoords.orientation = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001226
Jeff Brown6328cdc2010-07-29 18:18:33 -07001227 if (mAssociatedDisplayId >= 0 && (x != 0.0f || y != 0.0f)) {
1228 // Rotate motion based on display orientation if needed.
1229 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1230 int32_t orientation;
1231 if (! getPolicy()->getDisplayInfo(mAssociatedDisplayId, NULL, NULL, & orientation)) {
1232 return;
1233 }
1234
1235 float temp;
1236 switch (orientation) {
1237 case InputReaderPolicyInterface::ROTATION_90:
1238 temp = pointerCoords.x;
1239 pointerCoords.x = pointerCoords.y;
1240 pointerCoords.y = - temp;
1241 break;
1242
1243 case InputReaderPolicyInterface::ROTATION_180:
1244 pointerCoords.x = - pointerCoords.x;
1245 pointerCoords.y = - pointerCoords.y;
1246 break;
1247
1248 case InputReaderPolicyInterface::ROTATION_270:
1249 temp = pointerCoords.x;
1250 pointerCoords.x = - pointerCoords.y;
1251 pointerCoords.y = temp;
1252 break;
1253 }
1254 }
1255 } // release lock
1256
Jeff Brown6d0fec22010-07-23 21:28:06 -07001257 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001258 int32_t pointerId = 0;
Jeff Brownb6997262010-10-08 22:31:17 -07001259 getDispatcher()->notifyMotion(when, getDeviceId(), AINPUT_SOURCE_TRACKBALL, 0,
Jeff Brown85a31762010-09-01 17:01:00 -07001260 motionEventAction, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brownb6997262010-10-08 22:31:17 -07001261 1, &pointerId, &pointerCoords, mXPrecision, mYPrecision, downTime);
1262
1263 mAccumulator.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001264}
1265
Jeff Brownc3fc2d02010-08-10 15:47:53 -07001266int32_t TrackballInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1267 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
1268 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1269 } else {
1270 return AKEY_STATE_UNKNOWN;
1271 }
1272}
1273
Jeff Brown6d0fec22010-07-23 21:28:06 -07001274
1275// --- TouchInputMapper ---
1276
1277TouchInputMapper::TouchInputMapper(InputDevice* device, int32_t associatedDisplayId) :
Jeff Brown6328cdc2010-07-29 18:18:33 -07001278 InputMapper(device), mAssociatedDisplayId(associatedDisplayId) {
1279 mLocked.surfaceOrientation = -1;
1280 mLocked.surfaceWidth = -1;
1281 mLocked.surfaceHeight = -1;
1282
1283 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001284}
1285
1286TouchInputMapper::~TouchInputMapper() {
1287}
1288
1289uint32_t TouchInputMapper::getSources() {
1290 return mAssociatedDisplayId >= 0 ? AINPUT_SOURCE_TOUCHSCREEN : AINPUT_SOURCE_TOUCHPAD;
1291}
1292
1293void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1294 InputMapper::populateDeviceInfo(info);
1295
Jeff Brown6328cdc2010-07-29 18:18:33 -07001296 { // acquire lock
1297 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001298
Jeff Brown6328cdc2010-07-29 18:18:33 -07001299 // Ensure surface information is up to date so that orientation changes are
1300 // noticed immediately.
1301 configureSurfaceLocked();
1302
1303 info->addMotionRange(AINPUT_MOTION_RANGE_X, mLocked.orientedRanges.x);
1304 info->addMotionRange(AINPUT_MOTION_RANGE_Y, mLocked.orientedRanges.y);
Jeff Brown8d608662010-08-30 03:02:23 -07001305
1306 if (mLocked.orientedRanges.havePressure) {
1307 info->addMotionRange(AINPUT_MOTION_RANGE_PRESSURE,
1308 mLocked.orientedRanges.pressure);
1309 }
1310
1311 if (mLocked.orientedRanges.haveSize) {
1312 info->addMotionRange(AINPUT_MOTION_RANGE_SIZE,
1313 mLocked.orientedRanges.size);
1314 }
1315
1316 if (mLocked.orientedRanges.haveTouchArea) {
1317 info->addMotionRange(AINPUT_MOTION_RANGE_TOUCH_MAJOR,
1318 mLocked.orientedRanges.touchMajor);
1319 info->addMotionRange(AINPUT_MOTION_RANGE_TOUCH_MINOR,
1320 mLocked.orientedRanges.touchMinor);
1321 }
1322
1323 if (mLocked.orientedRanges.haveToolArea) {
1324 info->addMotionRange(AINPUT_MOTION_RANGE_TOOL_MAJOR,
1325 mLocked.orientedRanges.toolMajor);
1326 info->addMotionRange(AINPUT_MOTION_RANGE_TOOL_MINOR,
1327 mLocked.orientedRanges.toolMinor);
1328 }
1329
1330 if (mLocked.orientedRanges.haveOrientation) {
1331 info->addMotionRange(AINPUT_MOTION_RANGE_ORIENTATION,
1332 mLocked.orientedRanges.orientation);
1333 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001334 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001335}
1336
Jeff Brownef3d7e82010-09-30 14:33:04 -07001337void TouchInputMapper::dump(String8& dump) {
1338 { // acquire lock
1339 AutoMutex _l(mLock);
1340 dump.append(INDENT2 "Touch Input Mapper:\n");
1341 dump.appendFormat(INDENT3 "AssociatedDisplayId: %d\n", mAssociatedDisplayId);
1342 dumpParameters(dump);
1343 dumpVirtualKeysLocked(dump);
1344 dumpRawAxes(dump);
1345 dumpCalibration(dump);
1346 dumpSurfaceLocked(dump);
1347 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mLocked.xPrecision);
1348 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mLocked.yPrecision);
1349 } // release lock
1350}
1351
Jeff Brown6328cdc2010-07-29 18:18:33 -07001352void TouchInputMapper::initializeLocked() {
1353 mCurrentTouch.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001354 mLastTouch.clear();
1355 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001356
1357 for (uint32_t i = 0; i < MAX_POINTERS; i++) {
1358 mAveragingTouchFilter.historyStart[i] = 0;
1359 mAveragingTouchFilter.historyEnd[i] = 0;
1360 }
1361
1362 mJumpyTouchFilter.jumpyPointsDropped = 0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001363
1364 mLocked.currentVirtualKey.down = false;
Jeff Brown8d608662010-08-30 03:02:23 -07001365
1366 mLocked.orientedRanges.havePressure = false;
1367 mLocked.orientedRanges.haveSize = false;
1368 mLocked.orientedRanges.haveTouchArea = false;
1369 mLocked.orientedRanges.haveToolArea = false;
1370 mLocked.orientedRanges.haveOrientation = false;
1371}
1372
Jeff Brown6d0fec22010-07-23 21:28:06 -07001373void TouchInputMapper::configure() {
1374 InputMapper::configure();
1375
1376 // Configure basic parameters.
Jeff Brown8d608662010-08-30 03:02:23 -07001377 configureParameters();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001378
1379 // Configure absolute axis information.
Jeff Brown8d608662010-08-30 03:02:23 -07001380 configureRawAxes();
Jeff Brown8d608662010-08-30 03:02:23 -07001381
1382 // Prepare input device calibration.
1383 parseCalibration();
1384 resolveCalibration();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001385
Jeff Brown6328cdc2010-07-29 18:18:33 -07001386 { // acquire lock
1387 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001388
Jeff Brown8d608662010-08-30 03:02:23 -07001389 // Configure surface dimensions and orientation.
Jeff Brown6328cdc2010-07-29 18:18:33 -07001390 configureSurfaceLocked();
1391 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001392}
1393
Jeff Brown8d608662010-08-30 03:02:23 -07001394void TouchInputMapper::configureParameters() {
1395 mParameters.useBadTouchFilter = getPolicy()->filterTouchEvents();
1396 mParameters.useAveragingTouchFilter = getPolicy()->filterTouchEvents();
1397 mParameters.useJumpyTouchFilter = getPolicy()->filterJumpyTouchEvents();
1398}
1399
Jeff Brownef3d7e82010-09-30 14:33:04 -07001400void TouchInputMapper::dumpParameters(String8& dump) {
1401 dump.appendFormat(INDENT3 "UseBadTouchFilter: %s\n",
1402 toString(mParameters.useBadTouchFilter));
1403 dump.appendFormat(INDENT3 "UseAveragingTouchFilter: %s\n",
1404 toString(mParameters.useAveragingTouchFilter));
1405 dump.appendFormat(INDENT3 "UseJumpyTouchFilter: %s\n",
1406 toString(mParameters.useJumpyTouchFilter));
Jeff Brownb88102f2010-09-08 11:49:43 -07001407}
1408
Jeff Brown8d608662010-08-30 03:02:23 -07001409void TouchInputMapper::configureRawAxes() {
1410 mRawAxes.x.clear();
1411 mRawAxes.y.clear();
1412 mRawAxes.pressure.clear();
1413 mRawAxes.touchMajor.clear();
1414 mRawAxes.touchMinor.clear();
1415 mRawAxes.toolMajor.clear();
1416 mRawAxes.toolMinor.clear();
1417 mRawAxes.orientation.clear();
1418}
1419
Jeff Brownef3d7e82010-09-30 14:33:04 -07001420static void dumpAxisInfo(String8& dump, RawAbsoluteAxisInfo axis, const char* name) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001421 if (axis.valid) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07001422 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d\n",
Jeff Brownb88102f2010-09-08 11:49:43 -07001423 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz);
1424 } else {
Jeff Brownef3d7e82010-09-30 14:33:04 -07001425 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
Jeff Brownb88102f2010-09-08 11:49:43 -07001426 }
1427}
1428
Jeff Brownef3d7e82010-09-30 14:33:04 -07001429void TouchInputMapper::dumpRawAxes(String8& dump) {
1430 dump.append(INDENT3 "Raw Axes:\n");
1431 dumpAxisInfo(dump, mRawAxes.x, "X");
1432 dumpAxisInfo(dump, mRawAxes.y, "Y");
1433 dumpAxisInfo(dump, mRawAxes.pressure, "Pressure");
1434 dumpAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor");
1435 dumpAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor");
1436 dumpAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor");
1437 dumpAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor");
1438 dumpAxisInfo(dump, mRawAxes.orientation, "Orientation");
Jeff Brown6d0fec22010-07-23 21:28:06 -07001439}
1440
Jeff Brown6328cdc2010-07-29 18:18:33 -07001441bool TouchInputMapper::configureSurfaceLocked() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001442 // Update orientation and dimensions if needed.
1443 int32_t orientation;
1444 int32_t width, height;
1445 if (mAssociatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001446 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001447 if (! getPolicy()->getDisplayInfo(mAssociatedDisplayId, & width, & height, & orientation)) {
1448 return false;
1449 }
1450 } else {
1451 orientation = InputReaderPolicyInterface::ROTATION_0;
Jeff Brown8d608662010-08-30 03:02:23 -07001452 width = mRawAxes.x.getRange();
1453 height = mRawAxes.y.getRange();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001454 }
1455
Jeff Brown6328cdc2010-07-29 18:18:33 -07001456 bool orientationChanged = mLocked.surfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001457 if (orientationChanged) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001458 mLocked.surfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001459 }
1460
Jeff Brown6328cdc2010-07-29 18:18:33 -07001461 bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001462 if (sizeChanged) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07001463 LOGI("Device reconfigured: id=0x%x, name=%s, display size is now %dx%d",
1464 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown8d608662010-08-30 03:02:23 -07001465
Jeff Brown6328cdc2010-07-29 18:18:33 -07001466 mLocked.surfaceWidth = width;
1467 mLocked.surfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001468
Jeff Brown8d608662010-08-30 03:02:23 -07001469 // Configure X and Y factors.
1470 if (mRawAxes.x.valid && mRawAxes.y.valid) {
1471 mLocked.xOrigin = mRawAxes.x.minValue;
1472 mLocked.yOrigin = mRawAxes.y.minValue;
1473 mLocked.xScale = float(width) / mRawAxes.x.getRange();
1474 mLocked.yScale = float(height) / mRawAxes.y.getRange();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001475 mLocked.xPrecision = 1.0f / mLocked.xScale;
1476 mLocked.yPrecision = 1.0f / mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001477
Jeff Brown6328cdc2010-07-29 18:18:33 -07001478 configureVirtualKeysLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001479 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07001480 LOGW(INDENT "Touch device did not report support for X or Y axis!");
Jeff Brown6328cdc2010-07-29 18:18:33 -07001481 mLocked.xOrigin = 0;
1482 mLocked.yOrigin = 0;
1483 mLocked.xScale = 1.0f;
1484 mLocked.yScale = 1.0f;
1485 mLocked.xPrecision = 1.0f;
1486 mLocked.yPrecision = 1.0f;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001487 }
1488
Jeff Brown8d608662010-08-30 03:02:23 -07001489 // Scale factor for terms that are not oriented in a particular axis.
1490 // If the pixels are square then xScale == yScale otherwise we fake it
1491 // by choosing an average.
1492 mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001493
Jeff Brown8d608662010-08-30 03:02:23 -07001494 // Size of diagonal axis.
1495 float diagonalSize = pythag(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001496
Jeff Brown8d608662010-08-30 03:02:23 -07001497 // TouchMajor and TouchMinor factors.
1498 if (mCalibration.touchAreaCalibration != Calibration::TOUCH_AREA_CALIBRATION_NONE) {
1499 mLocked.orientedRanges.haveTouchArea = true;
1500 mLocked.orientedRanges.touchMajor.min = 0;
1501 mLocked.orientedRanges.touchMajor.max = diagonalSize;
1502 mLocked.orientedRanges.touchMajor.flat = 0;
1503 mLocked.orientedRanges.touchMajor.fuzz = 0;
1504 mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor;
1505 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001506
Jeff Brown8d608662010-08-30 03:02:23 -07001507 // ToolMajor and ToolMinor factors.
1508 if (mCalibration.toolAreaCalibration != Calibration::TOOL_AREA_CALIBRATION_NONE) {
1509 mLocked.toolAreaLinearScale = 0;
1510 mLocked.toolAreaLinearBias = 0;
1511 if (mCalibration.toolAreaCalibration == Calibration::TOOL_AREA_CALIBRATION_LINEAR) {
1512 if (mCalibration.haveToolAreaLinearScale) {
1513 mLocked.toolAreaLinearScale = mCalibration.toolAreaLinearScale;
1514 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1515 mLocked.toolAreaLinearScale = float(min(width, height))
1516 / mRawAxes.toolMajor.maxValue;
1517 }
1518
1519 if (mCalibration.haveToolAreaLinearBias) {
1520 mLocked.toolAreaLinearBias = mCalibration.toolAreaLinearBias;
1521 }
1522 }
1523
1524 mLocked.orientedRanges.haveToolArea = true;
1525 mLocked.orientedRanges.toolMajor.min = 0;
1526 mLocked.orientedRanges.toolMajor.max = diagonalSize;
1527 mLocked.orientedRanges.toolMajor.flat = 0;
1528 mLocked.orientedRanges.toolMajor.fuzz = 0;
1529 mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor;
1530 }
1531
1532 // Pressure factors.
1533 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
1534 RawAbsoluteAxisInfo rawPressureAxis;
1535 switch (mCalibration.pressureSource) {
1536 case Calibration::PRESSURE_SOURCE_PRESSURE:
1537 rawPressureAxis = mRawAxes.pressure;
1538 break;
1539 case Calibration::PRESSURE_SOURCE_TOUCH:
1540 rawPressureAxis = mRawAxes.touchMajor;
1541 break;
1542 default:
1543 rawPressureAxis.clear();
1544 }
1545
1546 mLocked.pressureScale = 0;
1547 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
1548 || mCalibration.pressureCalibration
1549 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
1550 if (mCalibration.havePressureScale) {
1551 mLocked.pressureScale = mCalibration.pressureScale;
1552 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
1553 mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue;
1554 }
1555 }
1556
1557 mLocked.orientedRanges.havePressure = true;
1558 mLocked.orientedRanges.pressure.min = 0;
1559 mLocked.orientedRanges.pressure.max = 1.0;
1560 mLocked.orientedRanges.pressure.flat = 0;
1561 mLocked.orientedRanges.pressure.fuzz = 0;
1562 }
1563
1564 // Size factors.
1565 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
1566 mLocked.sizeScale = 0;
1567 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
1568 if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1569 mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue;
1570 }
1571 }
1572
1573 mLocked.orientedRanges.haveSize = true;
1574 mLocked.orientedRanges.size.min = 0;
1575 mLocked.orientedRanges.size.max = 1.0;
1576 mLocked.orientedRanges.size.flat = 0;
1577 mLocked.orientedRanges.size.fuzz = 0;
1578 }
1579
1580 // Orientation
1581 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
1582 mLocked.orientationScale = 0;
1583 if (mCalibration.orientationCalibration
1584 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
1585 if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) {
1586 mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue;
1587 }
1588 }
1589
1590 mLocked.orientedRanges.orientation.min = - M_PI_2;
1591 mLocked.orientedRanges.orientation.max = M_PI_2;
1592 mLocked.orientedRanges.orientation.flat = 0;
1593 mLocked.orientedRanges.orientation.fuzz = 0;
1594 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001595 }
1596
1597 if (orientationChanged || sizeChanged) {
1598 // Compute oriented surface dimensions, precision, and scales.
1599 float orientedXScale, orientedYScale;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001600 switch (mLocked.surfaceOrientation) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001601 case InputReaderPolicyInterface::ROTATION_90:
1602 case InputReaderPolicyInterface::ROTATION_270:
Jeff Brown6328cdc2010-07-29 18:18:33 -07001603 mLocked.orientedSurfaceWidth = mLocked.surfaceHeight;
1604 mLocked.orientedSurfaceHeight = mLocked.surfaceWidth;
1605 mLocked.orientedXPrecision = mLocked.yPrecision;
1606 mLocked.orientedYPrecision = mLocked.xPrecision;
1607 orientedXScale = mLocked.yScale;
1608 orientedYScale = mLocked.xScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001609 break;
1610 default:
Jeff Brown6328cdc2010-07-29 18:18:33 -07001611 mLocked.orientedSurfaceWidth = mLocked.surfaceWidth;
1612 mLocked.orientedSurfaceHeight = mLocked.surfaceHeight;
1613 mLocked.orientedXPrecision = mLocked.xPrecision;
1614 mLocked.orientedYPrecision = mLocked.yPrecision;
1615 orientedXScale = mLocked.xScale;
1616 orientedYScale = mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001617 break;
1618 }
1619
1620 // Configure position ranges.
Jeff Brown6328cdc2010-07-29 18:18:33 -07001621 mLocked.orientedRanges.x.min = 0;
1622 mLocked.orientedRanges.x.max = mLocked.orientedSurfaceWidth;
1623 mLocked.orientedRanges.x.flat = 0;
1624 mLocked.orientedRanges.x.fuzz = orientedXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001625
Jeff Brown6328cdc2010-07-29 18:18:33 -07001626 mLocked.orientedRanges.y.min = 0;
1627 mLocked.orientedRanges.y.max = mLocked.orientedSurfaceHeight;
1628 mLocked.orientedRanges.y.flat = 0;
1629 mLocked.orientedRanges.y.fuzz = orientedYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001630 }
1631
1632 return true;
1633}
1634
Jeff Brownef3d7e82010-09-30 14:33:04 -07001635void TouchInputMapper::dumpSurfaceLocked(String8& dump) {
1636 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth);
1637 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight);
1638 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07001639}
1640
Jeff Brown6328cdc2010-07-29 18:18:33 -07001641void TouchInputMapper::configureVirtualKeysLocked() {
Jeff Brown8d608662010-08-30 03:02:23 -07001642 assert(mRawAxes.x.valid && mRawAxes.y.valid);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001643
Jeff Brown6328cdc2010-07-29 18:18:33 -07001644 // Note: getVirtualKeyDefinitions is non-reentrant so we can continue holding the lock.
Jeff Brown8d608662010-08-30 03:02:23 -07001645 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001646 getPolicy()->getVirtualKeyDefinitions(getDeviceName(), virtualKeyDefinitions);
1647
Jeff Brown6328cdc2010-07-29 18:18:33 -07001648 mLocked.virtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001649
Jeff Brown6328cdc2010-07-29 18:18:33 -07001650 if (virtualKeyDefinitions.size() == 0) {
1651 return;
1652 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001653
Jeff Brown6328cdc2010-07-29 18:18:33 -07001654 mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size());
1655
Jeff Brown8d608662010-08-30 03:02:23 -07001656 int32_t touchScreenLeft = mRawAxes.x.minValue;
1657 int32_t touchScreenTop = mRawAxes.y.minValue;
1658 int32_t touchScreenWidth = mRawAxes.x.getRange();
1659 int32_t touchScreenHeight = mRawAxes.y.getRange();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001660
1661 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07001662 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07001663 virtualKeyDefinitions[i];
1664
1665 mLocked.virtualKeys.add();
1666 VirtualKey& virtualKey = mLocked.virtualKeys.editTop();
1667
1668 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1669 int32_t keyCode;
1670 uint32_t flags;
1671 if (getEventHub()->scancodeToKeycode(getDeviceId(), virtualKey.scanCode,
1672 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07001673 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
1674 virtualKey.scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001675 mLocked.virtualKeys.pop(); // drop the key
1676 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001677 }
1678
Jeff Brown6328cdc2010-07-29 18:18:33 -07001679 virtualKey.keyCode = keyCode;
1680 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001681
Jeff Brown6328cdc2010-07-29 18:18:33 -07001682 // convert the key definition's display coordinates into touch coordinates for a hit box
1683 int32_t halfWidth = virtualKeyDefinition.width / 2;
1684 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001685
Jeff Brown6328cdc2010-07-29 18:18:33 -07001686 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
1687 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1688 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
1689 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1690 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
1691 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
1692 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
1693 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001694
Jeff Brownef3d7e82010-09-30 14:33:04 -07001695 }
1696}
1697
1698void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) {
1699 if (!mLocked.virtualKeys.isEmpty()) {
1700 dump.append(INDENT3 "Virtual Keys:\n");
1701
1702 for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) {
1703 const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i);
1704 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
1705 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1706 i, virtualKey.scanCode, virtualKey.keyCode,
1707 virtualKey.hitLeft, virtualKey.hitRight,
1708 virtualKey.hitTop, virtualKey.hitBottom);
1709 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001710 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001711}
1712
Jeff Brown8d608662010-08-30 03:02:23 -07001713void TouchInputMapper::parseCalibration() {
1714 const InputDeviceCalibration& in = getDevice()->getCalibration();
1715 Calibration& out = mCalibration;
1716
1717 // Touch Area
1718 out.touchAreaCalibration = Calibration::TOUCH_AREA_CALIBRATION_DEFAULT;
1719 String8 touchAreaCalibrationString;
1720 if (in.tryGetProperty(String8("touch.touchArea.calibration"), touchAreaCalibrationString)) {
1721 if (touchAreaCalibrationString == "none") {
1722 out.touchAreaCalibration = Calibration::TOUCH_AREA_CALIBRATION_NONE;
1723 } else if (touchAreaCalibrationString == "geometric") {
1724 out.touchAreaCalibration = Calibration::TOUCH_AREA_CALIBRATION_GEOMETRIC;
1725 } else if (touchAreaCalibrationString == "pressure") {
1726 out.touchAreaCalibration = Calibration::TOUCH_AREA_CALIBRATION_PRESSURE;
1727 } else if (touchAreaCalibrationString != "default") {
1728 LOGW("Invalid value for touch.touchArea.calibration: '%s'",
1729 touchAreaCalibrationString.string());
1730 }
1731 }
1732
1733 // Tool Area
1734 out.toolAreaCalibration = Calibration::TOOL_AREA_CALIBRATION_DEFAULT;
1735 String8 toolAreaCalibrationString;
1736 if (in.tryGetProperty(String8("tool.toolArea.calibration"), toolAreaCalibrationString)) {
1737 if (toolAreaCalibrationString == "none") {
1738 out.toolAreaCalibration = Calibration::TOOL_AREA_CALIBRATION_NONE;
1739 } else if (toolAreaCalibrationString == "geometric") {
1740 out.toolAreaCalibration = Calibration::TOOL_AREA_CALIBRATION_GEOMETRIC;
1741 } else if (toolAreaCalibrationString == "linear") {
1742 out.toolAreaCalibration = Calibration::TOOL_AREA_CALIBRATION_LINEAR;
1743 } else if (toolAreaCalibrationString != "default") {
1744 LOGW("Invalid value for tool.toolArea.calibration: '%s'",
1745 toolAreaCalibrationString.string());
1746 }
1747 }
1748
1749 out.haveToolAreaLinearScale = in.tryGetProperty(String8("touch.toolArea.linearScale"),
1750 out.toolAreaLinearScale);
1751 out.haveToolAreaLinearBias = in.tryGetProperty(String8("touch.toolArea.linearBias"),
1752 out.toolAreaLinearBias);
1753 out.haveToolAreaIsSummed = in.tryGetProperty(String8("touch.toolArea.isSummed"),
1754 out.toolAreaIsSummed);
1755
1756 // Pressure
1757 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
1758 String8 pressureCalibrationString;
1759 if (in.tryGetProperty(String8("tool.pressure.calibration"), pressureCalibrationString)) {
1760 if (pressureCalibrationString == "none") {
1761 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
1762 } else if (pressureCalibrationString == "physical") {
1763 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
1764 } else if (pressureCalibrationString == "amplitude") {
1765 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
1766 } else if (pressureCalibrationString != "default") {
1767 LOGW("Invalid value for tool.pressure.calibration: '%s'",
1768 pressureCalibrationString.string());
1769 }
1770 }
1771
1772 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
1773 String8 pressureSourceString;
1774 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
1775 if (pressureSourceString == "pressure") {
1776 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
1777 } else if (pressureSourceString == "touch") {
1778 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
1779 } else if (pressureSourceString != "default") {
1780 LOGW("Invalid value for touch.pressure.source: '%s'",
1781 pressureSourceString.string());
1782 }
1783 }
1784
1785 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
1786 out.pressureScale);
1787
1788 // Size
1789 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
1790 String8 sizeCalibrationString;
1791 if (in.tryGetProperty(String8("tool.size.calibration"), sizeCalibrationString)) {
1792 if (sizeCalibrationString == "none") {
1793 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
1794 } else if (sizeCalibrationString == "normalized") {
1795 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
1796 } else if (sizeCalibrationString != "default") {
1797 LOGW("Invalid value for tool.size.calibration: '%s'",
1798 sizeCalibrationString.string());
1799 }
1800 }
1801
1802 // Orientation
1803 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
1804 String8 orientationCalibrationString;
1805 if (in.tryGetProperty(String8("tool.orientation.calibration"), orientationCalibrationString)) {
1806 if (orientationCalibrationString == "none") {
1807 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
1808 } else if (orientationCalibrationString == "interpolated") {
1809 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
1810 } else if (orientationCalibrationString != "default") {
1811 LOGW("Invalid value for tool.orientation.calibration: '%s'",
1812 orientationCalibrationString.string());
1813 }
1814 }
1815}
1816
1817void TouchInputMapper::resolveCalibration() {
1818 // Pressure
1819 switch (mCalibration.pressureSource) {
1820 case Calibration::PRESSURE_SOURCE_DEFAULT:
1821 if (mRawAxes.pressure.valid) {
1822 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
1823 } else if (mRawAxes.touchMajor.valid) {
1824 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
1825 }
1826 break;
1827
1828 case Calibration::PRESSURE_SOURCE_PRESSURE:
1829 if (! mRawAxes.pressure.valid) {
1830 LOGW("Calibration property touch.pressure.source is 'pressure' but "
1831 "the pressure axis is not available.");
1832 }
1833 break;
1834
1835 case Calibration::PRESSURE_SOURCE_TOUCH:
1836 if (! mRawAxes.touchMajor.valid) {
1837 LOGW("Calibration property touch.pressure.source is 'touch' but "
1838 "the touchMajor axis is not available.");
1839 }
1840 break;
1841
1842 default:
1843 break;
1844 }
1845
1846 switch (mCalibration.pressureCalibration) {
1847 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
1848 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
1849 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
1850 } else {
1851 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
1852 }
1853 break;
1854
1855 default:
1856 break;
1857 }
1858
1859 // Tool Area
1860 switch (mCalibration.toolAreaCalibration) {
1861 case Calibration::TOOL_AREA_CALIBRATION_DEFAULT:
1862 if (mRawAxes.toolMajor.valid) {
1863 mCalibration.toolAreaCalibration = Calibration::TOOL_AREA_CALIBRATION_LINEAR;
1864 } else {
1865 mCalibration.toolAreaCalibration = Calibration::TOOL_AREA_CALIBRATION_NONE;
1866 }
1867 break;
1868
1869 default:
1870 break;
1871 }
1872
1873 // Touch Area
1874 switch (mCalibration.touchAreaCalibration) {
1875 case Calibration::TOUCH_AREA_CALIBRATION_DEFAULT:
1876 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
1877 && mCalibration.toolAreaCalibration != Calibration::TOOL_AREA_CALIBRATION_NONE) {
1878 mCalibration.touchAreaCalibration = Calibration::TOUCH_AREA_CALIBRATION_PRESSURE;
1879 } else {
1880 mCalibration.touchAreaCalibration = Calibration::TOUCH_AREA_CALIBRATION_NONE;
1881 }
1882 break;
1883
1884 default:
1885 break;
1886 }
1887
1888 // Size
1889 switch (mCalibration.sizeCalibration) {
1890 case Calibration::SIZE_CALIBRATION_DEFAULT:
1891 if (mRawAxes.toolMajor.valid) {
1892 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
1893 } else {
1894 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
1895 }
1896 break;
1897
1898 default:
1899 break;
1900 }
1901
1902 // Orientation
1903 switch (mCalibration.orientationCalibration) {
1904 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
1905 if (mRawAxes.orientation.valid) {
1906 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
1907 } else {
1908 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
1909 }
1910 break;
1911
1912 default:
1913 break;
1914 }
1915}
1916
Jeff Brownef3d7e82010-09-30 14:33:04 -07001917void TouchInputMapper::dumpCalibration(String8& dump) {
1918 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07001919
Jeff Brown8d608662010-08-30 03:02:23 -07001920 // Touch Area
1921 switch (mCalibration.touchAreaCalibration) {
1922 case Calibration::TOUCH_AREA_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07001923 dump.append(INDENT4 "touch.touchArea.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07001924 break;
1925 case Calibration::TOUCH_AREA_CALIBRATION_GEOMETRIC:
Jeff Brownef3d7e82010-09-30 14:33:04 -07001926 dump.append(INDENT4 "touch.touchArea.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07001927 break;
1928 case Calibration::TOUCH_AREA_CALIBRATION_PRESSURE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07001929 dump.append(INDENT4 "touch.touchArea.calibration: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07001930 break;
1931 default:
1932 assert(false);
1933 }
1934
1935 // Tool Area
1936 switch (mCalibration.toolAreaCalibration) {
1937 case Calibration::TOOL_AREA_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07001938 dump.append(INDENT4 "touch.toolArea.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07001939 break;
1940 case Calibration::TOOL_AREA_CALIBRATION_GEOMETRIC:
Jeff Brownef3d7e82010-09-30 14:33:04 -07001941 dump.append(INDENT4 "touch.toolArea.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07001942 break;
1943 case Calibration::TOOL_AREA_CALIBRATION_LINEAR:
Jeff Brownef3d7e82010-09-30 14:33:04 -07001944 dump.append(INDENT4 "touch.toolArea.calibration: linear\n");
Jeff Brown8d608662010-08-30 03:02:23 -07001945 break;
1946 default:
1947 assert(false);
1948 }
1949
1950 if (mCalibration.haveToolAreaLinearScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07001951 dump.appendFormat(INDENT4 "touch.toolArea.linearScale: %0.3f\n",
1952 mCalibration.toolAreaLinearScale);
Jeff Brown8d608662010-08-30 03:02:23 -07001953 }
1954
1955 if (mCalibration.haveToolAreaLinearBias) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07001956 dump.appendFormat(INDENT4 "touch.toolArea.linearBias: %0.3f\n",
1957 mCalibration.toolAreaLinearBias);
Jeff Brown8d608662010-08-30 03:02:23 -07001958 }
1959
1960 if (mCalibration.haveToolAreaIsSummed) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07001961 dump.appendFormat(INDENT4 "touch.toolArea.isSummed: %d\n",
1962 mCalibration.toolAreaIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07001963 }
1964
1965 // Pressure
1966 switch (mCalibration.pressureCalibration) {
1967 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07001968 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07001969 break;
1970 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07001971 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07001972 break;
1973 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07001974 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07001975 break;
1976 default:
1977 assert(false);
1978 }
1979
1980 switch (mCalibration.pressureSource) {
1981 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07001982 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07001983 break;
1984 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brownef3d7e82010-09-30 14:33:04 -07001985 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown8d608662010-08-30 03:02:23 -07001986 break;
1987 case Calibration::PRESSURE_SOURCE_DEFAULT:
1988 break;
1989 default:
1990 assert(false);
1991 }
1992
1993 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07001994 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
1995 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07001996 }
1997
1998 // Size
1999 switch (mCalibration.sizeCalibration) {
2000 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002001 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002002 break;
2003 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002004 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002005 break;
2006 default:
2007 assert(false);
2008 }
2009
2010 // Orientation
2011 switch (mCalibration.orientationCalibration) {
2012 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002013 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002014 break;
2015 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002016 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002017 break;
2018 default:
2019 assert(false);
2020 }
2021}
2022
Jeff Brown6d0fec22010-07-23 21:28:06 -07002023void TouchInputMapper::reset() {
2024 // Synthesize touch up event if touch is currently down.
2025 // This will also take care of finishing virtual key processing if needed.
2026 if (mLastTouch.pointerCount != 0) {
2027 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
2028 mCurrentTouch.clear();
2029 syncTouch(when, true);
2030 }
2031
Jeff Brown6328cdc2010-07-29 18:18:33 -07002032 { // acquire lock
2033 AutoMutex _l(mLock);
2034 initializeLocked();
2035 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07002036
Jeff Brown6328cdc2010-07-29 18:18:33 -07002037 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002038}
2039
2040void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002041 uint32_t policyFlags = 0;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002042
Jeff Brown6328cdc2010-07-29 18:18:33 -07002043 // Preprocess pointer data.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002044
Jeff Brown6d0fec22010-07-23 21:28:06 -07002045 if (mParameters.useBadTouchFilter) {
2046 if (applyBadTouchFilter()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002047 havePointerIds = false;
2048 }
2049 }
2050
Jeff Brown6d0fec22010-07-23 21:28:06 -07002051 if (mParameters.useJumpyTouchFilter) {
2052 if (applyJumpyTouchFilter()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002053 havePointerIds = false;
2054 }
2055 }
2056
2057 if (! havePointerIds) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002058 calculatePointerIds();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002059 }
2060
Jeff Brown6d0fec22010-07-23 21:28:06 -07002061 TouchData temp;
2062 TouchData* savedTouch;
2063 if (mParameters.useAveragingTouchFilter) {
2064 temp.copyFrom(mCurrentTouch);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002065 savedTouch = & temp;
2066
Jeff Brown6d0fec22010-07-23 21:28:06 -07002067 applyAveragingTouchFilter();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002068 } else {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002069 savedTouch = & mCurrentTouch;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002070 }
2071
Jeff Brown6328cdc2010-07-29 18:18:33 -07002072 // Process touches and virtual keys.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002073
Jeff Brown6d0fec22010-07-23 21:28:06 -07002074 TouchResult touchResult = consumeOffScreenTouches(when, policyFlags);
2075 if (touchResult == DISPATCH_TOUCH) {
2076 dispatchTouches(when, policyFlags);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002077 }
2078
Jeff Brown6328cdc2010-07-29 18:18:33 -07002079 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002080
Jeff Brown6d0fec22010-07-23 21:28:06 -07002081 if (touchResult == DROP_STROKE) {
2082 mLastTouch.clear();
2083 } else {
2084 mLastTouch.copyFrom(*savedTouch);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002085 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002086}
2087
Jeff Brown6d0fec22010-07-23 21:28:06 -07002088TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches(
2089 nsecs_t when, uint32_t policyFlags) {
2090 int32_t keyEventAction, keyEventFlags;
2091 int32_t keyCode, scanCode, downTime;
2092 TouchResult touchResult;
Jeff Brown349703e2010-06-22 01:27:15 -07002093
Jeff Brown6328cdc2010-07-29 18:18:33 -07002094 { // acquire lock
2095 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002096
Jeff Brown6328cdc2010-07-29 18:18:33 -07002097 // Update surface size and orientation, including virtual key positions.
2098 if (! configureSurfaceLocked()) {
2099 return DROP_STROKE;
2100 }
2101
2102 // Check for virtual key press.
2103 if (mLocked.currentVirtualKey.down) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002104 if (mCurrentTouch.pointerCount == 0) {
2105 // Pointer went up while virtual key was down.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002106 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002107#if DEBUG_VIRTUAL_KEYS
2108 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
2109 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
2110#endif
2111 keyEventAction = AKEY_EVENT_ACTION_UP;
2112 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2113 touchResult = SKIP_TOUCH;
2114 goto DispatchVirtualKey;
2115 }
2116
2117 if (mCurrentTouch.pointerCount == 1) {
2118 int32_t x = mCurrentTouch.pointers[0].x;
2119 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002120 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
2121 if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002122 // Pointer is still within the space of the virtual key.
2123 return SKIP_TOUCH;
2124 }
2125 }
2126
2127 // Pointer left virtual key area or another pointer also went down.
2128 // Send key cancellation and drop the stroke so subsequent motions will be
2129 // considered fresh downs. This is useful when the user swipes away from the
2130 // virtual key area into the main display surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002131 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002132#if DEBUG_VIRTUAL_KEYS
2133 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
2134 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
2135#endif
2136 keyEventAction = AKEY_EVENT_ACTION_UP;
2137 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
2138 | AKEY_EVENT_FLAG_CANCELED;
2139 touchResult = DROP_STROKE;
2140 goto DispatchVirtualKey;
2141 } else {
2142 if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) {
2143 // Pointer just went down. Handle off-screen touches, if needed.
2144 int32_t x = mCurrentTouch.pointers[0].x;
2145 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002146 if (! isPointInsideSurfaceLocked(x, y)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002147 // If exactly one pointer went down, check for virtual key hit.
2148 // Otherwise we will drop the entire stroke.
2149 if (mCurrentTouch.pointerCount == 1) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002150 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002151 if (virtualKey) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002152 mLocked.currentVirtualKey.down = true;
2153 mLocked.currentVirtualKey.downTime = when;
2154 mLocked.currentVirtualKey.keyCode = virtualKey->keyCode;
2155 mLocked.currentVirtualKey.scanCode = virtualKey->scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002156#if DEBUG_VIRTUAL_KEYS
2157 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
2158 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
2159#endif
2160 keyEventAction = AKEY_EVENT_ACTION_DOWN;
2161 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM
2162 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2163 touchResult = SKIP_TOUCH;
2164 goto DispatchVirtualKey;
2165 }
2166 }
2167 return DROP_STROKE;
2168 }
2169 }
2170 return DISPATCH_TOUCH;
2171 }
2172
2173 DispatchVirtualKey:
2174 // Collect remaining state needed to dispatch virtual key.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002175 keyCode = mLocked.currentVirtualKey.keyCode;
2176 scanCode = mLocked.currentVirtualKey.scanCode;
2177 downTime = mLocked.currentVirtualKey.downTime;
2178 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07002179
2180 // Dispatch virtual key.
2181 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown0eaf3932010-10-01 14:55:30 -07002182 policyFlags |= POLICY_FLAG_VIRTUAL;
Jeff Brownb6997262010-10-08 22:31:17 -07002183 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
2184 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
2185 return touchResult;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002186}
2187
Jeff Brown6d0fec22010-07-23 21:28:06 -07002188void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
2189 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2190 uint32_t lastPointerCount = mLastTouch.pointerCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002191 if (currentPointerCount == 0 && lastPointerCount == 0) {
2192 return; // nothing to do!
2193 }
2194
Jeff Brown6d0fec22010-07-23 21:28:06 -07002195 BitSet32 currentIdBits = mCurrentTouch.idBits;
2196 BitSet32 lastIdBits = mLastTouch.idBits;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002197
2198 if (currentIdBits == lastIdBits) {
2199 // No pointer id changes so this is a move event.
2200 // The dispatcher takes care of batching moves so we don't have to deal with that here.
Jeff Brownc5ed5912010-07-14 18:48:53 -07002201 int32_t motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002202 dispatchTouch(when, policyFlags, & mCurrentTouch,
Jeff Brown8d608662010-08-30 03:02:23 -07002203 currentIdBits, -1, currentPointerCount, motionEventAction);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002204 } else {
2205 // There may be pointers going up and pointers going down at the same time when pointer
2206 // ids are reported by the device driver.
2207 BitSet32 upIdBits(lastIdBits.value & ~ currentIdBits.value);
2208 BitSet32 downIdBits(currentIdBits.value & ~ lastIdBits.value);
2209 BitSet32 activeIdBits(lastIdBits.value);
Jeff Brown8d608662010-08-30 03:02:23 -07002210 uint32_t pointerCount = lastPointerCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002211
2212 while (! upIdBits.isEmpty()) {
2213 uint32_t upId = upIdBits.firstMarkedBit();
2214 upIdBits.clearBit(upId);
2215 BitSet32 oldActiveIdBits = activeIdBits;
2216 activeIdBits.clearBit(upId);
2217
2218 int32_t motionEventAction;
2219 if (activeIdBits.isEmpty()) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07002220 motionEventAction = AMOTION_EVENT_ACTION_UP;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002221 } else {
Jeff Brown00ba8842010-07-16 15:01:56 -07002222 motionEventAction = AMOTION_EVENT_ACTION_POINTER_UP;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002223 }
2224
Jeff Brown6d0fec22010-07-23 21:28:06 -07002225 dispatchTouch(when, policyFlags, & mLastTouch,
Jeff Brown8d608662010-08-30 03:02:23 -07002226 oldActiveIdBits, upId, pointerCount, motionEventAction);
2227 pointerCount -= 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002228 }
2229
2230 while (! downIdBits.isEmpty()) {
2231 uint32_t downId = downIdBits.firstMarkedBit();
2232 downIdBits.clearBit(downId);
2233 BitSet32 oldActiveIdBits = activeIdBits;
2234 activeIdBits.markBit(downId);
2235
2236 int32_t motionEventAction;
2237 if (oldActiveIdBits.isEmpty()) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07002238 motionEventAction = AMOTION_EVENT_ACTION_DOWN;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002239 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002240 } else {
Jeff Brown00ba8842010-07-16 15:01:56 -07002241 motionEventAction = AMOTION_EVENT_ACTION_POINTER_DOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002242 }
2243
Jeff Brown8d608662010-08-30 03:02:23 -07002244 pointerCount += 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002245 dispatchTouch(when, policyFlags, & mCurrentTouch,
Jeff Brown8d608662010-08-30 03:02:23 -07002246 activeIdBits, downId, pointerCount, motionEventAction);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002247 }
2248 }
2249}
2250
Jeff Brown6d0fec22010-07-23 21:28:06 -07002251void TouchInputMapper::dispatchTouch(nsecs_t when, uint32_t policyFlags,
Jeff Brown8d608662010-08-30 03:02:23 -07002252 TouchData* touch, BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002253 int32_t motionEventAction) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002254 int32_t pointerIds[MAX_POINTERS];
2255 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002256 int32_t motionEventEdgeFlags = 0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002257 float xPrecision, yPrecision;
2258
2259 { // acquire lock
2260 AutoMutex _l(mLock);
2261
2262 // Walk through the the active pointers and map touch screen coordinates (TouchData) into
2263 // display coordinates (PointerCoords) and adjust for display orientation.
Jeff Brown8d608662010-08-30 03:02:23 -07002264 for (uint32_t outIndex = 0; ! idBits.isEmpty(); outIndex++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002265 uint32_t id = idBits.firstMarkedBit();
2266 idBits.clearBit(id);
Jeff Brown8d608662010-08-30 03:02:23 -07002267 uint32_t inIndex = touch->idToIndex[id];
Jeff Brown6328cdc2010-07-29 18:18:33 -07002268
Jeff Brown8d608662010-08-30 03:02:23 -07002269 const PointerData& in = touch->pointers[inIndex];
Jeff Brown6328cdc2010-07-29 18:18:33 -07002270
Jeff Brown8d608662010-08-30 03:02:23 -07002271 // X and Y
2272 float x = float(in.x - mLocked.xOrigin) * mLocked.xScale;
2273 float y = float(in.y - mLocked.yOrigin) * mLocked.yScale;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002274
Jeff Brown8d608662010-08-30 03:02:23 -07002275 // ToolMajor and ToolMinor
2276 float toolMajor, toolMinor;
2277 switch (mCalibration.toolAreaCalibration) {
2278 case Calibration::TOOL_AREA_CALIBRATION_GEOMETRIC:
2279 toolMajor = in.toolMajor * mLocked.geometricScale;
2280 if (mRawAxes.toolMinor.valid) {
2281 toolMinor = in.toolMinor * mLocked.geometricScale;
2282 } else {
2283 toolMinor = toolMajor;
2284 }
2285 break;
2286 case Calibration::TOOL_AREA_CALIBRATION_LINEAR:
2287 toolMajor = in.toolMajor != 0
2288 ? in.toolMajor * mLocked.toolAreaLinearScale + mLocked.toolAreaLinearBias
2289 : 0;
2290 if (mRawAxes.toolMinor.valid) {
2291 toolMinor = in.toolMinor != 0
2292 ? in.toolMinor * mLocked.toolAreaLinearScale
2293 + mLocked.toolAreaLinearBias
2294 : 0;
2295 } else {
2296 toolMinor = toolMajor;
2297 }
2298 break;
2299 default:
2300 toolMajor = 0;
2301 toolMinor = 0;
2302 break;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002303 }
2304
Jeff Brown8d608662010-08-30 03:02:23 -07002305 if (mCalibration.haveToolAreaIsSummed && mCalibration.toolAreaIsSummed) {
2306 toolMajor /= pointerCount;
2307 toolMinor /= pointerCount;
2308 }
2309
2310 // Pressure
2311 float rawPressure;
2312 switch (mCalibration.pressureSource) {
2313 case Calibration::PRESSURE_SOURCE_PRESSURE:
2314 rawPressure = in.pressure;
2315 break;
2316 case Calibration::PRESSURE_SOURCE_TOUCH:
2317 rawPressure = in.touchMajor;
2318 break;
2319 default:
2320 rawPressure = 0;
2321 }
2322
2323 float pressure;
2324 switch (mCalibration.pressureCalibration) {
2325 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
2326 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
2327 pressure = rawPressure * mLocked.pressureScale;
2328 break;
2329 default:
2330 pressure = 1;
2331 break;
2332 }
2333
2334 // TouchMajor and TouchMinor
2335 float touchMajor, touchMinor;
2336 switch (mCalibration.touchAreaCalibration) {
2337 case Calibration::TOUCH_AREA_CALIBRATION_GEOMETRIC:
2338 touchMajor = in.touchMajor * mLocked.geometricScale;
2339 if (mRawAxes.touchMinor.valid) {
2340 touchMinor = in.touchMinor * mLocked.geometricScale;
2341 } else {
2342 touchMinor = touchMajor;
2343 }
2344 break;
2345 case Calibration::TOUCH_AREA_CALIBRATION_PRESSURE:
2346 touchMajor = toolMajor * pressure;
2347 touchMinor = toolMinor * pressure;
2348 break;
2349 default:
2350 touchMajor = 0;
2351 touchMinor = 0;
2352 break;
2353 }
2354
2355 if (touchMajor > toolMajor) {
2356 touchMajor = toolMajor;
2357 }
2358 if (touchMinor > toolMinor) {
2359 touchMinor = toolMinor;
2360 }
2361
2362 // Size
2363 float size;
2364 switch (mCalibration.sizeCalibration) {
2365 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
2366 float rawSize = mRawAxes.toolMinor.valid
2367 ? avg(in.toolMajor, in.toolMinor)
2368 : in.toolMajor;
2369 size = rawSize * mLocked.sizeScale;
2370 break;
2371 }
2372 default:
2373 size = 0;
2374 break;
2375 }
2376
2377 // Orientation
2378 float orientation;
2379 switch (mCalibration.orientationCalibration) {
2380 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
2381 orientation = in.orientation * mLocked.orientationScale;
2382 break;
2383 default:
2384 orientation = 0;
2385 }
2386
2387 // Adjust coords for orientation.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002388 switch (mLocked.surfaceOrientation) {
2389 case InputReaderPolicyInterface::ROTATION_90: {
2390 float xTemp = x;
2391 x = y;
2392 y = mLocked.surfaceWidth - xTemp;
2393 orientation -= M_PI_2;
2394 if (orientation < - M_PI_2) {
2395 orientation += M_PI;
2396 }
2397 break;
2398 }
2399 case InputReaderPolicyInterface::ROTATION_180: {
2400 x = mLocked.surfaceWidth - x;
2401 y = mLocked.surfaceHeight - y;
2402 orientation = - orientation;
2403 break;
2404 }
2405 case InputReaderPolicyInterface::ROTATION_270: {
2406 float xTemp = x;
2407 x = mLocked.surfaceHeight - y;
2408 y = xTemp;
2409 orientation += M_PI_2;
2410 if (orientation > M_PI_2) {
2411 orientation -= M_PI;
2412 }
2413 break;
2414 }
2415 }
2416
Jeff Brown8d608662010-08-30 03:02:23 -07002417 // Write output coords.
2418 PointerCoords& out = pointerCoords[outIndex];
2419 out.x = x;
2420 out.y = y;
2421 out.pressure = pressure;
2422 out.size = size;
2423 out.touchMajor = touchMajor;
2424 out.touchMinor = touchMinor;
2425 out.toolMajor = toolMajor;
2426 out.toolMinor = toolMinor;
2427 out.orientation = orientation;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002428
Jeff Brown8d608662010-08-30 03:02:23 -07002429 pointerIds[outIndex] = int32_t(id);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002430
2431 if (id == changedId) {
Jeff Brown8d608662010-08-30 03:02:23 -07002432 motionEventAction |= outIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002433 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002434 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002435
2436 // Check edge flags by looking only at the first pointer since the flags are
2437 // global to the event.
2438 if (motionEventAction == AMOTION_EVENT_ACTION_DOWN) {
2439 if (pointerCoords[0].x <= 0) {
2440 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_LEFT;
2441 } else if (pointerCoords[0].x >= mLocked.orientedSurfaceWidth) {
2442 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_RIGHT;
2443 }
2444 if (pointerCoords[0].y <= 0) {
2445 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_TOP;
2446 } else if (pointerCoords[0].y >= mLocked.orientedSurfaceHeight) {
2447 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_BOTTOM;
2448 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002449 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002450
2451 xPrecision = mLocked.orientedXPrecision;
2452 yPrecision = mLocked.orientedYPrecision;
2453 } // release lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002454
Jeff Brown8da727a2010-10-07 13:44:51 -07002455 getDispatcher()->notifyMotion(when, getDeviceId(), getSources(), policyFlags,
Jeff Brown85a31762010-09-01 17:01:00 -07002456 motionEventAction, 0, getContext()->getGlobalMetaState(), motionEventEdgeFlags,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002457 pointerCount, pointerIds, pointerCoords,
Jeff Brown6328cdc2010-07-29 18:18:33 -07002458 xPrecision, yPrecision, mDownTime);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002459}
2460
Jeff Brown6328cdc2010-07-29 18:18:33 -07002461bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) {
Jeff Brown8d608662010-08-30 03:02:23 -07002462 if (mRawAxes.x.valid && mRawAxes.y.valid) {
2463 return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue
2464 && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002465 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002466 return true;
2467}
2468
Jeff Brown6328cdc2010-07-29 18:18:33 -07002469const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked(
2470 int32_t x, int32_t y) {
2471 size_t numVirtualKeys = mLocked.virtualKeys.size();
2472 for (size_t i = 0; i < numVirtualKeys; i++) {
2473 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07002474
2475#if DEBUG_VIRTUAL_KEYS
2476 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
2477 "left=%d, top=%d, right=%d, bottom=%d",
2478 x, y,
2479 virtualKey.keyCode, virtualKey.scanCode,
2480 virtualKey.hitLeft, virtualKey.hitTop,
2481 virtualKey.hitRight, virtualKey.hitBottom);
2482#endif
2483
2484 if (virtualKey.isHit(x, y)) {
2485 return & virtualKey;
2486 }
2487 }
2488
2489 return NULL;
2490}
2491
2492void TouchInputMapper::calculatePointerIds() {
2493 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2494 uint32_t lastPointerCount = mLastTouch.pointerCount;
2495
2496 if (currentPointerCount == 0) {
2497 // No pointers to assign.
2498 mCurrentTouch.idBits.clear();
2499 } else if (lastPointerCount == 0) {
2500 // All pointers are new.
2501 mCurrentTouch.idBits.clear();
2502 for (uint32_t i = 0; i < currentPointerCount; i++) {
2503 mCurrentTouch.pointers[i].id = i;
2504 mCurrentTouch.idToIndex[i] = i;
2505 mCurrentTouch.idBits.markBit(i);
2506 }
2507 } else if (currentPointerCount == 1 && lastPointerCount == 1) {
2508 // Only one pointer and no change in count so it must have the same id as before.
2509 uint32_t id = mLastTouch.pointers[0].id;
2510 mCurrentTouch.pointers[0].id = id;
2511 mCurrentTouch.idToIndex[id] = 0;
2512 mCurrentTouch.idBits.value = BitSet32::valueForBit(id);
2513 } else {
2514 // General case.
2515 // We build a heap of squared euclidean distances between current and last pointers
2516 // associated with the current and last pointer indices. Then, we find the best
2517 // match (by distance) for each current pointer.
2518 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
2519
2520 uint32_t heapSize = 0;
2521 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
2522 currentPointerIndex++) {
2523 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
2524 lastPointerIndex++) {
2525 int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x
2526 - mLastTouch.pointers[lastPointerIndex].x;
2527 int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y
2528 - mLastTouch.pointers[lastPointerIndex].y;
2529
2530 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
2531
2532 // Insert new element into the heap (sift up).
2533 heap[heapSize].currentPointerIndex = currentPointerIndex;
2534 heap[heapSize].lastPointerIndex = lastPointerIndex;
2535 heap[heapSize].distance = distance;
2536 heapSize += 1;
2537 }
2538 }
2539
2540 // Heapify
2541 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
2542 startIndex -= 1;
2543 for (uint32_t parentIndex = startIndex; ;) {
2544 uint32_t childIndex = parentIndex * 2 + 1;
2545 if (childIndex >= heapSize) {
2546 break;
2547 }
2548
2549 if (childIndex + 1 < heapSize
2550 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2551 childIndex += 1;
2552 }
2553
2554 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2555 break;
2556 }
2557
2558 swap(heap[parentIndex], heap[childIndex]);
2559 parentIndex = childIndex;
2560 }
2561 }
2562
2563#if DEBUG_POINTER_ASSIGNMENT
2564 LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize);
2565 for (size_t i = 0; i < heapSize; i++) {
2566 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2567 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2568 heap[i].distance);
2569 }
2570#endif
2571
2572 // Pull matches out by increasing order of distance.
2573 // To avoid reassigning pointers that have already been matched, the loop keeps track
2574 // of which last and current pointers have been matched using the matchedXXXBits variables.
2575 // It also tracks the used pointer id bits.
2576 BitSet32 matchedLastBits(0);
2577 BitSet32 matchedCurrentBits(0);
2578 BitSet32 usedIdBits(0);
2579 bool first = true;
2580 for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) {
2581 for (;;) {
2582 if (first) {
2583 // The first time through the loop, we just consume the root element of
2584 // the heap (the one with smallest distance).
2585 first = false;
2586 } else {
2587 // Previous iterations consumed the root element of the heap.
2588 // Pop root element off of the heap (sift down).
2589 heapSize -= 1;
2590 assert(heapSize > 0);
2591
2592 // Sift down.
2593 heap[0] = heap[heapSize];
2594 for (uint32_t parentIndex = 0; ;) {
2595 uint32_t childIndex = parentIndex * 2 + 1;
2596 if (childIndex >= heapSize) {
2597 break;
2598 }
2599
2600 if (childIndex + 1 < heapSize
2601 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2602 childIndex += 1;
2603 }
2604
2605 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2606 break;
2607 }
2608
2609 swap(heap[parentIndex], heap[childIndex]);
2610 parentIndex = childIndex;
2611 }
2612
2613#if DEBUG_POINTER_ASSIGNMENT
2614 LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize);
2615 for (size_t i = 0; i < heapSize; i++) {
2616 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2617 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2618 heap[i].distance);
2619 }
2620#endif
2621 }
2622
2623 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
2624 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
2625
2626 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
2627 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
2628
2629 matchedCurrentBits.markBit(currentPointerIndex);
2630 matchedLastBits.markBit(lastPointerIndex);
2631
2632 uint32_t id = mLastTouch.pointers[lastPointerIndex].id;
2633 mCurrentTouch.pointers[currentPointerIndex].id = id;
2634 mCurrentTouch.idToIndex[id] = currentPointerIndex;
2635 usedIdBits.markBit(id);
2636
2637#if DEBUG_POINTER_ASSIGNMENT
2638 LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
2639 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
2640#endif
2641 break;
2642 }
2643 }
2644
2645 // Assign fresh ids to new pointers.
2646 if (currentPointerCount > lastPointerCount) {
2647 for (uint32_t i = currentPointerCount - lastPointerCount; ;) {
2648 uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit();
2649 uint32_t id = usedIdBits.firstUnmarkedBit();
2650
2651 mCurrentTouch.pointers[currentPointerIndex].id = id;
2652 mCurrentTouch.idToIndex[id] = currentPointerIndex;
2653 usedIdBits.markBit(id);
2654
2655#if DEBUG_POINTER_ASSIGNMENT
2656 LOGD("calculatePointerIds - assigned: cur=%d, id=%d",
2657 currentPointerIndex, id);
2658#endif
2659
2660 if (--i == 0) break; // done
2661 matchedCurrentBits.markBit(currentPointerIndex);
2662 }
2663 }
2664
2665 // Fix id bits.
2666 mCurrentTouch.idBits = usedIdBits;
2667 }
2668}
2669
2670/* Special hack for devices that have bad screen data: if one of the
2671 * points has moved more than a screen height from the last position,
2672 * then drop it. */
2673bool TouchInputMapper::applyBadTouchFilter() {
2674 // This hack requires valid axis parameters.
Jeff Brown8d608662010-08-30 03:02:23 -07002675 if (! mRawAxes.y.valid) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002676 return false;
2677 }
2678
2679 uint32_t pointerCount = mCurrentTouch.pointerCount;
2680
2681 // Nothing to do if there are no points.
2682 if (pointerCount == 0) {
2683 return false;
2684 }
2685
2686 // Don't do anything if a finger is going down or up. We run
2687 // here before assigning pointer IDs, so there isn't a good
2688 // way to do per-finger matching.
2689 if (pointerCount != mLastTouch.pointerCount) {
2690 return false;
2691 }
2692
2693 // We consider a single movement across more than a 7/16 of
2694 // the long size of the screen to be bad. This was a magic value
2695 // determined by looking at the maximum distance it is feasible
2696 // to actually move in one sample.
Jeff Brown8d608662010-08-30 03:02:23 -07002697 int32_t maxDeltaY = mRawAxes.y.getRange() * 7 / 16;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002698
2699 // XXX The original code in InputDevice.java included commented out
2700 // code for testing the X axis. Note that when we drop a point
2701 // we don't actually restore the old X either. Strange.
2702 // The old code also tries to track when bad points were previously
2703 // detected but it turns out that due to the placement of a "break"
2704 // at the end of the loop, we never set mDroppedBadPoint to true
2705 // so it is effectively dead code.
2706 // Need to figure out if the old code is busted or just overcomplicated
2707 // but working as intended.
2708
2709 // Look through all new points and see if any are farther than
2710 // acceptable from all previous points.
2711 for (uint32_t i = pointerCount; i-- > 0; ) {
2712 int32_t y = mCurrentTouch.pointers[i].y;
2713 int32_t closestY = INT_MAX;
2714 int32_t closestDeltaY = 0;
2715
2716#if DEBUG_HACKS
2717 LOGD("BadTouchFilter: Looking at next point #%d: y=%d", i, y);
2718#endif
2719
2720 for (uint32_t j = pointerCount; j-- > 0; ) {
2721 int32_t lastY = mLastTouch.pointers[j].y;
2722 int32_t deltaY = abs(y - lastY);
2723
2724#if DEBUG_HACKS
2725 LOGD("BadTouchFilter: Comparing with last point #%d: y=%d deltaY=%d",
2726 j, lastY, deltaY);
2727#endif
2728
2729 if (deltaY < maxDeltaY) {
2730 goto SkipSufficientlyClosePoint;
2731 }
2732 if (deltaY < closestDeltaY) {
2733 closestDeltaY = deltaY;
2734 closestY = lastY;
2735 }
2736 }
2737
2738 // Must not have found a close enough match.
2739#if DEBUG_HACKS
2740 LOGD("BadTouchFilter: Dropping bad point #%d: newY=%d oldY=%d deltaY=%d maxDeltaY=%d",
2741 i, y, closestY, closestDeltaY, maxDeltaY);
2742#endif
2743
2744 mCurrentTouch.pointers[i].y = closestY;
2745 return true; // XXX original code only corrects one point
2746
2747 SkipSufficientlyClosePoint: ;
2748 }
2749
2750 // No change.
2751 return false;
2752}
2753
2754/* Special hack for devices that have bad screen data: drop points where
2755 * the coordinate value for one axis has jumped to the other pointer's location.
2756 */
2757bool TouchInputMapper::applyJumpyTouchFilter() {
2758 // This hack requires valid axis parameters.
Jeff Brown8d608662010-08-30 03:02:23 -07002759 if (! mRawAxes.y.valid) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002760 return false;
2761 }
2762
2763 uint32_t pointerCount = mCurrentTouch.pointerCount;
2764 if (mLastTouch.pointerCount != pointerCount) {
2765#if DEBUG_HACKS
2766 LOGD("JumpyTouchFilter: Different pointer count %d -> %d",
2767 mLastTouch.pointerCount, pointerCount);
2768 for (uint32_t i = 0; i < pointerCount; i++) {
2769 LOGD(" Pointer %d (%d, %d)", i,
2770 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
2771 }
2772#endif
2773
2774 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_TRANSITION_DROPS) {
2775 if (mLastTouch.pointerCount == 1 && pointerCount == 2) {
2776 // Just drop the first few events going from 1 to 2 pointers.
2777 // They're bad often enough that they're not worth considering.
2778 mCurrentTouch.pointerCount = 1;
2779 mJumpyTouchFilter.jumpyPointsDropped += 1;
2780
2781#if DEBUG_HACKS
2782 LOGD("JumpyTouchFilter: Pointer 2 dropped");
2783#endif
2784 return true;
2785 } else if (mLastTouch.pointerCount == 2 && pointerCount == 1) {
2786 // The event when we go from 2 -> 1 tends to be messed up too
2787 mCurrentTouch.pointerCount = 2;
2788 mCurrentTouch.pointers[0] = mLastTouch.pointers[0];
2789 mCurrentTouch.pointers[1] = mLastTouch.pointers[1];
2790 mJumpyTouchFilter.jumpyPointsDropped += 1;
2791
2792#if DEBUG_HACKS
2793 for (int32_t i = 0; i < 2; i++) {
2794 LOGD("JumpyTouchFilter: Pointer %d replaced (%d, %d)", i,
2795 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
2796 }
2797#endif
2798 return true;
2799 }
2800 }
2801 // Reset jumpy points dropped on other transitions or if limit exceeded.
2802 mJumpyTouchFilter.jumpyPointsDropped = 0;
2803
2804#if DEBUG_HACKS
2805 LOGD("JumpyTouchFilter: Transition - drop limit reset");
2806#endif
2807 return false;
2808 }
2809
2810 // We have the same number of pointers as last time.
2811 // A 'jumpy' point is one where the coordinate value for one axis
2812 // has jumped to the other pointer's location. No need to do anything
2813 // else if we only have one pointer.
2814 if (pointerCount < 2) {
2815 return false;
2816 }
2817
2818 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_DROP_LIMIT) {
Jeff Brown8d608662010-08-30 03:02:23 -07002819 int jumpyEpsilon = mRawAxes.y.getRange() / JUMPY_EPSILON_DIVISOR;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002820
2821 // We only replace the single worst jumpy point as characterized by pointer distance
2822 // in a single axis.
2823 int32_t badPointerIndex = -1;
2824 int32_t badPointerReplacementIndex = -1;
2825 int32_t badPointerDistance = INT_MIN; // distance to be corrected
2826
2827 for (uint32_t i = pointerCount; i-- > 0; ) {
2828 int32_t x = mCurrentTouch.pointers[i].x;
2829 int32_t y = mCurrentTouch.pointers[i].y;
2830
2831#if DEBUG_HACKS
2832 LOGD("JumpyTouchFilter: Point %d (%d, %d)", i, x, y);
2833#endif
2834
2835 // Check if a touch point is too close to another's coordinates
2836 bool dropX = false, dropY = false;
2837 for (uint32_t j = 0; j < pointerCount; j++) {
2838 if (i == j) {
2839 continue;
2840 }
2841
2842 if (abs(x - mCurrentTouch.pointers[j].x) <= jumpyEpsilon) {
2843 dropX = true;
2844 break;
2845 }
2846
2847 if (abs(y - mCurrentTouch.pointers[j].y) <= jumpyEpsilon) {
2848 dropY = true;
2849 break;
2850 }
2851 }
2852 if (! dropX && ! dropY) {
2853 continue; // not jumpy
2854 }
2855
2856 // Find a replacement candidate by comparing with older points on the
2857 // complementary (non-jumpy) axis.
2858 int32_t distance = INT_MIN; // distance to be corrected
2859 int32_t replacementIndex = -1;
2860
2861 if (dropX) {
2862 // X looks too close. Find an older replacement point with a close Y.
2863 int32_t smallestDeltaY = INT_MAX;
2864 for (uint32_t j = 0; j < pointerCount; j++) {
2865 int32_t deltaY = abs(y - mLastTouch.pointers[j].y);
2866 if (deltaY < smallestDeltaY) {
2867 smallestDeltaY = deltaY;
2868 replacementIndex = j;
2869 }
2870 }
2871 distance = abs(x - mLastTouch.pointers[replacementIndex].x);
2872 } else {
2873 // Y looks too close. Find an older replacement point with a close X.
2874 int32_t smallestDeltaX = INT_MAX;
2875 for (uint32_t j = 0; j < pointerCount; j++) {
2876 int32_t deltaX = abs(x - mLastTouch.pointers[j].x);
2877 if (deltaX < smallestDeltaX) {
2878 smallestDeltaX = deltaX;
2879 replacementIndex = j;
2880 }
2881 }
2882 distance = abs(y - mLastTouch.pointers[replacementIndex].y);
2883 }
2884
2885 // If replacing this pointer would correct a worse error than the previous ones
2886 // considered, then use this replacement instead.
2887 if (distance > badPointerDistance) {
2888 badPointerIndex = i;
2889 badPointerReplacementIndex = replacementIndex;
2890 badPointerDistance = distance;
2891 }
2892 }
2893
2894 // Correct the jumpy pointer if one was found.
2895 if (badPointerIndex >= 0) {
2896#if DEBUG_HACKS
2897 LOGD("JumpyTouchFilter: Replacing bad pointer %d with (%d, %d)",
2898 badPointerIndex,
2899 mLastTouch.pointers[badPointerReplacementIndex].x,
2900 mLastTouch.pointers[badPointerReplacementIndex].y);
2901#endif
2902
2903 mCurrentTouch.pointers[badPointerIndex].x =
2904 mLastTouch.pointers[badPointerReplacementIndex].x;
2905 mCurrentTouch.pointers[badPointerIndex].y =
2906 mLastTouch.pointers[badPointerReplacementIndex].y;
2907 mJumpyTouchFilter.jumpyPointsDropped += 1;
2908 return true;
2909 }
2910 }
2911
2912 mJumpyTouchFilter.jumpyPointsDropped = 0;
2913 return false;
2914}
2915
2916/* Special hack for devices that have bad screen data: aggregate and
2917 * compute averages of the coordinate data, to reduce the amount of
2918 * jitter seen by applications. */
2919void TouchInputMapper::applyAveragingTouchFilter() {
2920 for (uint32_t currentIndex = 0; currentIndex < mCurrentTouch.pointerCount; currentIndex++) {
2921 uint32_t id = mCurrentTouch.pointers[currentIndex].id;
2922 int32_t x = mCurrentTouch.pointers[currentIndex].x;
2923 int32_t y = mCurrentTouch.pointers[currentIndex].y;
Jeff Brown8d608662010-08-30 03:02:23 -07002924 int32_t pressure;
2925 switch (mCalibration.pressureSource) {
2926 case Calibration::PRESSURE_SOURCE_PRESSURE:
2927 pressure = mCurrentTouch.pointers[currentIndex].pressure;
2928 break;
2929 case Calibration::PRESSURE_SOURCE_TOUCH:
2930 pressure = mCurrentTouch.pointers[currentIndex].touchMajor;
2931 break;
2932 default:
2933 pressure = 1;
2934 break;
2935 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002936
2937 if (mLastTouch.idBits.hasBit(id)) {
2938 // Pointer was down before and is still down now.
2939 // Compute average over history trace.
2940 uint32_t start = mAveragingTouchFilter.historyStart[id];
2941 uint32_t end = mAveragingTouchFilter.historyEnd[id];
2942
2943 int64_t deltaX = x - mAveragingTouchFilter.historyData[end].pointers[id].x;
2944 int64_t deltaY = y - mAveragingTouchFilter.historyData[end].pointers[id].y;
2945 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
2946
2947#if DEBUG_HACKS
2948 LOGD("AveragingTouchFilter: Pointer id %d - Distance from last sample: %lld",
2949 id, distance);
2950#endif
2951
2952 if (distance < AVERAGING_DISTANCE_LIMIT) {
2953 // Increment end index in preparation for recording new historical data.
2954 end += 1;
2955 if (end > AVERAGING_HISTORY_SIZE) {
2956 end = 0;
2957 }
2958
2959 // If the end index has looped back to the start index then we have filled
2960 // the historical trace up to the desired size so we drop the historical
2961 // data at the start of the trace.
2962 if (end == start) {
2963 start += 1;
2964 if (start > AVERAGING_HISTORY_SIZE) {
2965 start = 0;
2966 }
2967 }
2968
2969 // Add the raw data to the historical trace.
2970 mAveragingTouchFilter.historyStart[id] = start;
2971 mAveragingTouchFilter.historyEnd[id] = end;
2972 mAveragingTouchFilter.historyData[end].pointers[id].x = x;
2973 mAveragingTouchFilter.historyData[end].pointers[id].y = y;
2974 mAveragingTouchFilter.historyData[end].pointers[id].pressure = pressure;
2975
2976 // Average over all historical positions in the trace by total pressure.
2977 int32_t averagedX = 0;
2978 int32_t averagedY = 0;
2979 int32_t totalPressure = 0;
2980 for (;;) {
2981 int32_t historicalX = mAveragingTouchFilter.historyData[start].pointers[id].x;
2982 int32_t historicalY = mAveragingTouchFilter.historyData[start].pointers[id].y;
2983 int32_t historicalPressure = mAveragingTouchFilter.historyData[start]
2984 .pointers[id].pressure;
2985
2986 averagedX += historicalX * historicalPressure;
2987 averagedY += historicalY * historicalPressure;
2988 totalPressure += historicalPressure;
2989
2990 if (start == end) {
2991 break;
2992 }
2993
2994 start += 1;
2995 if (start > AVERAGING_HISTORY_SIZE) {
2996 start = 0;
2997 }
2998 }
2999
Jeff Brown8d608662010-08-30 03:02:23 -07003000 if (totalPressure != 0) {
3001 averagedX /= totalPressure;
3002 averagedY /= totalPressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003003
3004#if DEBUG_HACKS
Jeff Brown8d608662010-08-30 03:02:23 -07003005 LOGD("AveragingTouchFilter: Pointer id %d - "
3006 "totalPressure=%d, averagedX=%d, averagedY=%d", id, totalPressure,
3007 averagedX, averagedY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003008#endif
3009
Jeff Brown8d608662010-08-30 03:02:23 -07003010 mCurrentTouch.pointers[currentIndex].x = averagedX;
3011 mCurrentTouch.pointers[currentIndex].y = averagedY;
3012 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003013 } else {
3014#if DEBUG_HACKS
3015 LOGD("AveragingTouchFilter: Pointer id %d - Exceeded max distance", id);
3016#endif
3017 }
3018 } else {
3019#if DEBUG_HACKS
3020 LOGD("AveragingTouchFilter: Pointer id %d - Pointer went up", id);
3021#endif
3022 }
3023
3024 // Reset pointer history.
3025 mAveragingTouchFilter.historyStart[id] = 0;
3026 mAveragingTouchFilter.historyEnd[id] = 0;
3027 mAveragingTouchFilter.historyData[0].pointers[id].x = x;
3028 mAveragingTouchFilter.historyData[0].pointers[id].y = y;
3029 mAveragingTouchFilter.historyData[0].pointers[id].pressure = pressure;
3030 }
3031}
3032
3033int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003034 { // acquire lock
3035 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003036
Jeff Brown6328cdc2010-07-29 18:18:33 -07003037 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003038 return AKEY_STATE_VIRTUAL;
3039 }
3040
Jeff Brown6328cdc2010-07-29 18:18:33 -07003041 size_t numVirtualKeys = mLocked.virtualKeys.size();
3042 for (size_t i = 0; i < numVirtualKeys; i++) {
3043 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07003044 if (virtualKey.keyCode == keyCode) {
3045 return AKEY_STATE_UP;
3046 }
3047 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003048 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07003049
3050 return AKEY_STATE_UNKNOWN;
3051}
3052
3053int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003054 { // acquire lock
3055 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003056
Jeff Brown6328cdc2010-07-29 18:18:33 -07003057 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003058 return AKEY_STATE_VIRTUAL;
3059 }
3060
Jeff Brown6328cdc2010-07-29 18:18:33 -07003061 size_t numVirtualKeys = mLocked.virtualKeys.size();
3062 for (size_t i = 0; i < numVirtualKeys; i++) {
3063 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07003064 if (virtualKey.scanCode == scanCode) {
3065 return AKEY_STATE_UP;
3066 }
3067 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003068 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07003069
3070 return AKEY_STATE_UNKNOWN;
3071}
3072
3073bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
3074 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003075 { // acquire lock
3076 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003077
Jeff Brown6328cdc2010-07-29 18:18:33 -07003078 size_t numVirtualKeys = mLocked.virtualKeys.size();
3079 for (size_t i = 0; i < numVirtualKeys; i++) {
3080 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07003081
3082 for (size_t i = 0; i < numCodes; i++) {
3083 if (virtualKey.keyCode == keyCodes[i]) {
3084 outFlags[i] = 1;
3085 }
3086 }
3087 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003088 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07003089
3090 return true;
3091}
3092
3093
3094// --- SingleTouchInputMapper ---
3095
3096SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device, int32_t associatedDisplayId) :
3097 TouchInputMapper(device, associatedDisplayId) {
3098 initialize();
3099}
3100
3101SingleTouchInputMapper::~SingleTouchInputMapper() {
3102}
3103
3104void SingleTouchInputMapper::initialize() {
3105 mAccumulator.clear();
3106
3107 mDown = false;
3108 mX = 0;
3109 mY = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003110 mPressure = 0; // default to 0 for devices that don't report pressure
3111 mToolWidth = 0; // default to 0 for devices that don't report tool width
Jeff Brown6d0fec22010-07-23 21:28:06 -07003112}
3113
3114void SingleTouchInputMapper::reset() {
3115 TouchInputMapper::reset();
3116
Jeff Brown6d0fec22010-07-23 21:28:06 -07003117 initialize();
3118 }
3119
3120void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
3121 switch (rawEvent->type) {
3122 case EV_KEY:
3123 switch (rawEvent->scanCode) {
3124 case BTN_TOUCH:
3125 mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH;
3126 mAccumulator.btnTouch = rawEvent->value != 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003127 // Don't sync immediately. Wait until the next SYN_REPORT since we might
3128 // not have received valid position information yet. This logic assumes that
3129 // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003130 break;
3131 }
3132 break;
3133
3134 case EV_ABS:
3135 switch (rawEvent->scanCode) {
3136 case ABS_X:
3137 mAccumulator.fields |= Accumulator::FIELD_ABS_X;
3138 mAccumulator.absX = rawEvent->value;
3139 break;
3140 case ABS_Y:
3141 mAccumulator.fields |= Accumulator::FIELD_ABS_Y;
3142 mAccumulator.absY = rawEvent->value;
3143 break;
3144 case ABS_PRESSURE:
3145 mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE;
3146 mAccumulator.absPressure = rawEvent->value;
3147 break;
3148 case ABS_TOOL_WIDTH:
3149 mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH;
3150 mAccumulator.absToolWidth = rawEvent->value;
3151 break;
3152 }
3153 break;
3154
3155 case EV_SYN:
3156 switch (rawEvent->scanCode) {
3157 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003158 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003159 break;
3160 }
3161 break;
3162 }
3163}
3164
3165void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003166 uint32_t fields = mAccumulator.fields;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003167 if (fields == 0) {
3168 return; // no new state changes, so nothing to do
3169 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003170
3171 if (fields & Accumulator::FIELD_BTN_TOUCH) {
3172 mDown = mAccumulator.btnTouch;
3173 }
3174
3175 if (fields & Accumulator::FIELD_ABS_X) {
3176 mX = mAccumulator.absX;
3177 }
3178
3179 if (fields & Accumulator::FIELD_ABS_Y) {
3180 mY = mAccumulator.absY;
3181 }
3182
3183 if (fields & Accumulator::FIELD_ABS_PRESSURE) {
3184 mPressure = mAccumulator.absPressure;
3185 }
3186
3187 if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) {
Jeff Brown8d608662010-08-30 03:02:23 -07003188 mToolWidth = mAccumulator.absToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003189 }
3190
3191 mCurrentTouch.clear();
3192
3193 if (mDown) {
3194 mCurrentTouch.pointerCount = 1;
3195 mCurrentTouch.pointers[0].id = 0;
3196 mCurrentTouch.pointers[0].x = mX;
3197 mCurrentTouch.pointers[0].y = mY;
3198 mCurrentTouch.pointers[0].pressure = mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -07003199 mCurrentTouch.pointers[0].touchMajor = 0;
3200 mCurrentTouch.pointers[0].touchMinor = 0;
3201 mCurrentTouch.pointers[0].toolMajor = mToolWidth;
3202 mCurrentTouch.pointers[0].toolMinor = mToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003203 mCurrentTouch.pointers[0].orientation = 0;
3204 mCurrentTouch.idToIndex[0] = 0;
3205 mCurrentTouch.idBits.markBit(0);
3206 }
3207
3208 syncTouch(when, true);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003209
3210 mAccumulator.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003211}
3212
Jeff Brown8d608662010-08-30 03:02:23 -07003213void SingleTouchInputMapper::configureRawAxes() {
3214 TouchInputMapper::configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003215
Jeff Brown8d608662010-08-30 03:02:23 -07003216 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x);
3217 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y);
3218 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure);
3219 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003220}
3221
3222
3223// --- MultiTouchInputMapper ---
3224
3225MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device, int32_t associatedDisplayId) :
3226 TouchInputMapper(device, associatedDisplayId) {
3227 initialize();
3228}
3229
3230MultiTouchInputMapper::~MultiTouchInputMapper() {
3231}
3232
3233void MultiTouchInputMapper::initialize() {
3234 mAccumulator.clear();
3235}
3236
3237void MultiTouchInputMapper::reset() {
3238 TouchInputMapper::reset();
3239
Jeff Brown6d0fec22010-07-23 21:28:06 -07003240 initialize();
3241}
3242
3243void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
3244 switch (rawEvent->type) {
3245 case EV_ABS: {
3246 uint32_t pointerIndex = mAccumulator.pointerCount;
3247 Accumulator::Pointer* pointer = & mAccumulator.pointers[pointerIndex];
3248
3249 switch (rawEvent->scanCode) {
3250 case ABS_MT_POSITION_X:
3251 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_X;
3252 pointer->absMTPositionX = rawEvent->value;
3253 break;
3254 case ABS_MT_POSITION_Y:
3255 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y;
3256 pointer->absMTPositionY = rawEvent->value;
3257 break;
3258 case ABS_MT_TOUCH_MAJOR:
3259 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR;
3260 pointer->absMTTouchMajor = rawEvent->value;
3261 break;
3262 case ABS_MT_TOUCH_MINOR:
3263 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR;
3264 pointer->absMTTouchMinor = rawEvent->value;
3265 break;
3266 case ABS_MT_WIDTH_MAJOR:
3267 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR;
3268 pointer->absMTWidthMajor = rawEvent->value;
3269 break;
3270 case ABS_MT_WIDTH_MINOR:
3271 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR;
3272 pointer->absMTWidthMinor = rawEvent->value;
3273 break;
3274 case ABS_MT_ORIENTATION:
3275 pointer->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION;
3276 pointer->absMTOrientation = rawEvent->value;
3277 break;
3278 case ABS_MT_TRACKING_ID:
3279 pointer->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID;
3280 pointer->absMTTrackingId = rawEvent->value;
3281 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003282 case ABS_MT_PRESSURE:
3283 pointer->fields |= Accumulator::FIELD_ABS_MT_PRESSURE;
3284 pointer->absMTPressure = rawEvent->value;
3285 break;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003286 }
3287 break;
3288 }
3289
3290 case EV_SYN:
3291 switch (rawEvent->scanCode) {
3292 case SYN_MT_REPORT: {
3293 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
3294 uint32_t pointerIndex = mAccumulator.pointerCount;
3295
3296 if (mAccumulator.pointers[pointerIndex].fields) {
3297 if (pointerIndex == MAX_POINTERS) {
3298 LOGW("MultiTouch device driver returned more than maximum of %d pointers.",
3299 MAX_POINTERS);
3300 } else {
3301 pointerIndex += 1;
3302 mAccumulator.pointerCount = pointerIndex;
3303 }
3304 }
3305
3306 mAccumulator.pointers[pointerIndex].clear();
3307 break;
3308 }
3309
3310 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003311 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003312 break;
3313 }
3314 break;
3315 }
3316}
3317
3318void MultiTouchInputMapper::sync(nsecs_t when) {
3319 static const uint32_t REQUIRED_FIELDS =
Jeff Brown8d608662010-08-30 03:02:23 -07003320 Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003321
Jeff Brown6d0fec22010-07-23 21:28:06 -07003322 uint32_t inCount = mAccumulator.pointerCount;
3323 uint32_t outCount = 0;
3324 bool havePointerIds = true;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003325
Jeff Brown6d0fec22010-07-23 21:28:06 -07003326 mCurrentTouch.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003327
Jeff Brown6d0fec22010-07-23 21:28:06 -07003328 for (uint32_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003329 const Accumulator::Pointer& inPointer = mAccumulator.pointers[inIndex];
3330 uint32_t fields = inPointer.fields;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003331
Jeff Brown6d0fec22010-07-23 21:28:06 -07003332 if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003333 // Some drivers send empty MT sync packets without X / Y to indicate a pointer up.
3334 // Drop this finger.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003335 continue;
3336 }
3337
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003338 PointerData& outPointer = mCurrentTouch.pointers[outCount];
3339 outPointer.x = inPointer.absMTPositionX;
3340 outPointer.y = inPointer.absMTPositionY;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003341
Jeff Brown8d608662010-08-30 03:02:23 -07003342 if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) {
3343 if (inPointer.absMTPressure <= 0) {
3344 // Some devices send sync packets with X / Y but with a 0 presure to indicate
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003345 // a pointer up. Drop this finger.
3346 continue;
3347 }
Jeff Brown8d608662010-08-30 03:02:23 -07003348 outPointer.pressure = inPointer.absMTPressure;
3349 } else {
3350 // Default pressure to 0 if absent.
3351 outPointer.pressure = 0;
3352 }
3353
3354 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) {
3355 if (inPointer.absMTTouchMajor <= 0) {
3356 // Some devices send sync packets with X / Y but with a 0 touch major to indicate
3357 // a pointer going up. Drop this finger.
3358 continue;
3359 }
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003360 outPointer.touchMajor = inPointer.absMTTouchMajor;
3361 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003362 // Default touch area to 0 if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003363 outPointer.touchMajor = 0;
3364 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003365
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003366 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) {
3367 outPointer.touchMinor = inPointer.absMTTouchMinor;
3368 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003369 // Assume touch area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003370 outPointer.touchMinor = outPointer.touchMajor;
3371 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003372
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003373 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) {
3374 outPointer.toolMajor = inPointer.absMTWidthMajor;
3375 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003376 // Default tool area to 0 if absent.
3377 outPointer.toolMajor = 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003378 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003379
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003380 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) {
3381 outPointer.toolMinor = inPointer.absMTWidthMinor;
3382 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003383 // Assume tool area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003384 outPointer.toolMinor = outPointer.toolMajor;
3385 }
3386
3387 if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) {
3388 outPointer.orientation = inPointer.absMTOrientation;
3389 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003390 // Default orientation to vertical if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003391 outPointer.orientation = 0;
3392 }
3393
Jeff Brown8d608662010-08-30 03:02:23 -07003394 // Assign pointer id using tracking id if available.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003395 if (havePointerIds) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003396 if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) {
3397 uint32_t id = uint32_t(inPointer.absMTTrackingId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003398
Jeff Brown6d0fec22010-07-23 21:28:06 -07003399 if (id > MAX_POINTER_ID) {
3400#if DEBUG_POINTERS
3401 LOGD("Pointers: Ignoring driver provided pointer id %d because "
Jeff Brown01ce2e92010-09-26 22:20:12 -07003402 "it is larger than max supported id %d",
Jeff Brown6d0fec22010-07-23 21:28:06 -07003403 id, MAX_POINTER_ID);
3404#endif
3405 havePointerIds = false;
3406 }
3407 else {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003408 outPointer.id = id;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003409 mCurrentTouch.idToIndex[id] = outCount;
3410 mCurrentTouch.idBits.markBit(id);
3411 }
3412 } else {
3413 havePointerIds = false;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003414 }
3415 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003416
Jeff Brown6d0fec22010-07-23 21:28:06 -07003417 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003418 }
3419
Jeff Brown6d0fec22010-07-23 21:28:06 -07003420 mCurrentTouch.pointerCount = outCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003421
Jeff Brown6d0fec22010-07-23 21:28:06 -07003422 syncTouch(when, havePointerIds);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003423
3424 mAccumulator.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003425}
3426
Jeff Brown8d608662010-08-30 03:02:23 -07003427void MultiTouchInputMapper::configureRawAxes() {
3428 TouchInputMapper::configureRawAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003429
Jeff Brown8d608662010-08-30 03:02:23 -07003430 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, & mRawAxes.x);
3431 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, & mRawAxes.y);
3432 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, & mRawAxes.touchMajor);
3433 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, & mRawAxes.touchMinor);
3434 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, & mRawAxes.toolMajor);
3435 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, & mRawAxes.toolMinor);
3436 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, & mRawAxes.orientation);
3437 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, & mRawAxes.pressure);
Jeff Brown9c3cda02010-06-15 01:31:58 -07003438}
3439
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003440
3441} // namespace android