blob: daff2d006219885d5243f93cf3d15560fa3edf32 [file] [log] [blame]
Jeff Browne839a582010-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 Brown50de30a2010-06-22 01:27:15 -070014#define DEBUG_HACKS 0
Jeff Browne839a582010-04-22 18:58:52 -070015
16// Log debug messages about virtual key processing.
Jeff Brown50de30a2010-06-22 01:27:15 -070017#define DEBUG_VIRTUAL_KEYS 0
Jeff Browne839a582010-04-22 18:58:52 -070018
19// Log debug messages about pointers.
Jeff Brown50de30a2010-06-22 01:27:15 -070020#define DEBUG_POINTERS 0
Jeff Browne839a582010-04-22 18:58:52 -070021
Jeff Brownf4a4ec22010-06-16 01:53:36 -070022// Log debug messages about pointer assignment calculations.
23#define DEBUG_POINTER_ASSIGNMENT 0
24
Jeff Browne839a582010-04-22 18:58:52 -070025#include <cutils/log.h>
26#include <ui/InputReader.h>
Jeff Browna3477c82010-11-10 16:03:06 -080027#include <ui/Keyboard.h>
Jeff Browne839a582010-04-22 18:58:52 -070028
29#include <stddef.h>
Jeff Brown38a7fab2010-08-30 03:02:23 -070030#include <stdlib.h>
Jeff Browne839a582010-04-22 18:58:52 -070031#include <unistd.h>
Jeff Browne839a582010-04-22 18:58:52 -070032#include <errno.h>
33#include <limits.h>
Jeff Brown5c1ed842010-07-14 18:48:53 -070034#include <math.h>
Jeff Browne839a582010-04-22 18:58:52 -070035
Jeff Brown38a7fab2010-08-30 03:02:23 -070036#define INDENT " "
Jeff Brown26c94ff2010-09-30 14:33:04 -070037#define INDENT2 " "
38#define INDENT3 " "
39#define INDENT4 " "
Jeff Brown38a7fab2010-08-30 03:02:23 -070040
Jeff Browne839a582010-04-22 18:58:52 -070041namespace android {
42
43// --- Static Functions ---
44
45template<typename T>
46inline static T abs(const T& value) {
47 return value < 0 ? - value : value;
48}
49
50template<typename T>
51inline static T min(const T& a, const T& b) {
52 return a < b ? a : b;
53}
54
Jeff Brownf4a4ec22010-06-16 01:53:36 -070055template<typename T>
56inline static void swap(T& a, T& b) {
57 T temp = a;
58 a = b;
59 b = temp;
60}
61
Jeff Brown38a7fab2010-08-30 03:02:23 -070062inline static float avg(float x, float y) {
63 return (x + y) / 2;
64}
65
66inline static float pythag(float x, float y) {
67 return sqrtf(x * x + y * y);
68}
69
Jeff Brown26c94ff2010-09-30 14:33:04 -070070static inline const char* toString(bool value) {
71 return value ? "true" : "false";
72}
73
Jeff Browne839a582010-04-22 18:58:52 -070074static const int32_t keyCodeRotationMap[][4] = {
75 // key codes enumerated counter-clockwise with the original (unrotated) key first
76 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brown8575a872010-06-30 16:10:35 -070077 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
78 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
79 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
80 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Browne839a582010-04-22 18:58:52 -070081};
82static const int keyCodeRotationMapSize =
83 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
84
85int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brown54bc2812010-06-15 01:31:58 -070086 if (orientation != InputReaderPolicyInterface::ROTATION_0) {
Jeff Browne839a582010-04-22 18:58:52 -070087 for (int i = 0; i < keyCodeRotationMapSize; i++) {
88 if (keyCode == keyCodeRotationMap[i][0]) {
89 return keyCodeRotationMap[i][orientation];
90 }
91 }
92 }
93 return keyCode;
94}
95
Jeff Browne57e8952010-07-23 21:28:06 -070096static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
97 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
98}
99
Jeff Browne839a582010-04-22 18:58:52 -0700100
Jeff Brown38a7fab2010-08-30 03:02:23 -0700101// --- InputDeviceCalibration ---
102
103InputDeviceCalibration::InputDeviceCalibration() {
104}
105
106void InputDeviceCalibration::clear() {
107 mProperties.clear();
108}
109
110void InputDeviceCalibration::addProperty(const String8& key, const String8& value) {
111 mProperties.add(key, value);
112}
113
114bool InputDeviceCalibration::tryGetProperty(const String8& key, String8& outValue) const {
115 ssize_t index = mProperties.indexOfKey(key);
116 if (index < 0) {
117 return false;
118 }
119
120 outValue = mProperties.valueAt(index);
121 return true;
122}
123
124bool InputDeviceCalibration::tryGetProperty(const String8& key, int32_t& outValue) const {
125 String8 stringValue;
126 if (! tryGetProperty(key, stringValue) || stringValue.length() == 0) {
127 return false;
128 }
129
130 char* end;
131 int value = strtol(stringValue.string(), & end, 10);
132 if (*end != '\0') {
133 LOGW("Input device calibration key '%s' has invalid value '%s'. Expected an integer.",
134 key.string(), stringValue.string());
135 return false;
136 }
137 outValue = value;
138 return true;
139}
140
141bool InputDeviceCalibration::tryGetProperty(const String8& key, float& outValue) const {
142 String8 stringValue;
143 if (! tryGetProperty(key, stringValue) || stringValue.length() == 0) {
144 return false;
145 }
146
147 char* end;
148 float value = strtof(stringValue.string(), & end);
149 if (*end != '\0') {
150 LOGW("Input device calibration key '%s' has invalid value '%s'. Expected a float.",
151 key.string(), stringValue.string());
152 return false;
153 }
154 outValue = value;
155 return true;
156}
157
158
Jeff Browne839a582010-04-22 18:58:52 -0700159// --- InputReader ---
160
161InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown54bc2812010-06-15 01:31:58 -0700162 const sp<InputReaderPolicyInterface>& policy,
Jeff Browne839a582010-04-22 18:58:52 -0700163 const sp<InputDispatcherInterface>& dispatcher) :
Jeff Browne57e8952010-07-23 21:28:06 -0700164 mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher),
165 mGlobalMetaState(0) {
Jeff Brown54bc2812010-06-15 01:31:58 -0700166 configureExcludedDevices();
Jeff Browne57e8952010-07-23 21:28:06 -0700167 updateGlobalMetaState();
168 updateInputConfiguration();
Jeff Browne839a582010-04-22 18:58:52 -0700169}
170
171InputReader::~InputReader() {
172 for (size_t i = 0; i < mDevices.size(); i++) {
173 delete mDevices.valueAt(i);
174 }
175}
176
177void InputReader::loopOnce() {
178 RawEvent rawEvent;
Jeff Browne57e8952010-07-23 21:28:06 -0700179 mEventHub->getEvent(& rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700180
181#if DEBUG_RAW_EVENTS
182 LOGD("Input event: device=0x%x type=0x%x scancode=%d keycode=%d value=%d",
183 rawEvent.deviceId, rawEvent.type, rawEvent.scanCode, rawEvent.keyCode,
184 rawEvent.value);
185#endif
186
187 process(& rawEvent);
188}
189
190void InputReader::process(const RawEvent* rawEvent) {
191 switch (rawEvent->type) {
192 case EventHubInterface::DEVICE_ADDED:
Jeff Brown1ad00e92010-10-01 18:55:43 -0700193 addDevice(rawEvent->deviceId);
Jeff Browne839a582010-04-22 18:58:52 -0700194 break;
195
196 case EventHubInterface::DEVICE_REMOVED:
Jeff Brown1ad00e92010-10-01 18:55:43 -0700197 removeDevice(rawEvent->deviceId);
198 break;
199
200 case EventHubInterface::FINISHED_DEVICE_SCAN:
Jeff Brown3c3cc622010-10-20 15:33:38 -0700201 handleConfigurationChanged(rawEvent->when);
Jeff Browne839a582010-04-22 18:58:52 -0700202 break;
203
Jeff Browne57e8952010-07-23 21:28:06 -0700204 default:
205 consumeEvent(rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700206 break;
207 }
208}
209
Jeff Brown1ad00e92010-10-01 18:55:43 -0700210void InputReader::addDevice(int32_t deviceId) {
Jeff Browne57e8952010-07-23 21:28:06 -0700211 String8 name = mEventHub->getDeviceName(deviceId);
212 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
213
214 InputDevice* device = createDevice(deviceId, name, classes);
215 device->configure();
216
Jeff Brown38a7fab2010-08-30 03:02:23 -0700217 if (device->isIgnored()) {
Jeff Brown26c94ff2010-09-30 14:33:04 -0700218 LOGI("Device added: id=0x%x, name=%s (ignored non-input device)", deviceId, name.string());
Jeff Brown38a7fab2010-08-30 03:02:23 -0700219 } else {
Jeff Brown26c94ff2010-09-30 14:33:04 -0700220 LOGI("Device added: id=0x%x, name=%s, sources=%08x", deviceId, name.string(),
221 device->getSources());
Jeff Brown38a7fab2010-08-30 03:02:23 -0700222 }
223
Jeff Browne57e8952010-07-23 21:28:06 -0700224 bool added = false;
225 { // acquire device registry writer lock
226 RWLock::AutoWLock _wl(mDeviceRegistryLock);
227
228 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
229 if (deviceIndex < 0) {
230 mDevices.add(deviceId, device);
231 added = true;
232 }
233 } // release device registry writer lock
234
235 if (! added) {
236 LOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
237 delete device;
Jeff Browne839a582010-04-22 18:58:52 -0700238 return;
239 }
Jeff Browne839a582010-04-22 18:58:52 -0700240}
241
Jeff Brown1ad00e92010-10-01 18:55:43 -0700242void InputReader::removeDevice(int32_t deviceId) {
Jeff Browne57e8952010-07-23 21:28:06 -0700243 bool removed = false;
244 InputDevice* device = NULL;
245 { // acquire device registry writer lock
246 RWLock::AutoWLock _wl(mDeviceRegistryLock);
247
248 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
249 if (deviceIndex >= 0) {
250 device = mDevices.valueAt(deviceIndex);
251 mDevices.removeItemsAt(deviceIndex, 1);
252 removed = true;
253 }
254 } // release device registry writer lock
255
256 if (! removed) {
257 LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Browne839a582010-04-22 18:58:52 -0700258 return;
259 }
260
Jeff Browne57e8952010-07-23 21:28:06 -0700261 if (device->isIgnored()) {
262 LOGI("Device removed: id=0x%x, name=%s (ignored non-input device)",
263 device->getId(), device->getName().string());
264 } else {
265 LOGI("Device removed: id=0x%x, name=%s, sources=%08x",
266 device->getId(), device->getName().string(), device->getSources());
267 }
268
Jeff Brown38a7fab2010-08-30 03:02:23 -0700269 device->reset();
270
Jeff Browne57e8952010-07-23 21:28:06 -0700271 delete device;
Jeff Browne839a582010-04-22 18:58:52 -0700272}
273
Jeff Browne57e8952010-07-23 21:28:06 -0700274InputDevice* InputReader::createDevice(int32_t deviceId, const String8& name, uint32_t classes) {
275 InputDevice* device = new InputDevice(this, deviceId, name);
Jeff Browne839a582010-04-22 18:58:52 -0700276
Jeff Browne57e8952010-07-23 21:28:06 -0700277 const int32_t associatedDisplayId = 0; // FIXME: hardcoded for current single-display devices
Jeff Browne839a582010-04-22 18:58:52 -0700278
Jeff Browne57e8952010-07-23 21:28:06 -0700279 // Switch-like devices.
280 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
281 device->addMapper(new SwitchInputMapper(device));
282 }
283
284 // Keyboard-like devices.
285 uint32_t keyboardSources = 0;
286 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
287 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
288 keyboardSources |= AINPUT_SOURCE_KEYBOARD;
289 }
290 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
291 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
292 }
293 if (classes & INPUT_DEVICE_CLASS_DPAD) {
294 keyboardSources |= AINPUT_SOURCE_DPAD;
295 }
Jeff Browne57e8952010-07-23 21:28:06 -0700296
297 if (keyboardSources != 0) {
298 device->addMapper(new KeyboardInputMapper(device,
299 associatedDisplayId, keyboardSources, keyboardType));
300 }
301
302 // Trackball-like devices.
303 if (classes & INPUT_DEVICE_CLASS_TRACKBALL) {
304 device->addMapper(new TrackballInputMapper(device, associatedDisplayId));
305 }
306
307 // Touchscreen-like devices.
308 if (classes & INPUT_DEVICE_CLASS_TOUCHSCREEN_MT) {
309 device->addMapper(new MultiTouchInputMapper(device, associatedDisplayId));
310 } else if (classes & INPUT_DEVICE_CLASS_TOUCHSCREEN) {
311 device->addMapper(new SingleTouchInputMapper(device, associatedDisplayId));
312 }
313
314 return device;
315}
316
317void InputReader::consumeEvent(const RawEvent* rawEvent) {
318 int32_t deviceId = rawEvent->deviceId;
319
320 { // acquire device registry reader lock
321 RWLock::AutoRLock _rl(mDeviceRegistryLock);
322
323 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
324 if (deviceIndex < 0) {
325 LOGW("Discarding event for unknown deviceId %d.", deviceId);
326 return;
327 }
328
329 InputDevice* device = mDevices.valueAt(deviceIndex);
330 if (device->isIgnored()) {
331 //LOGD("Discarding event for ignored deviceId %d.", deviceId);
332 return;
333 }
334
335 device->process(rawEvent);
336 } // release device registry reader lock
337}
338
Jeff Brown3c3cc622010-10-20 15:33:38 -0700339void InputReader::handleConfigurationChanged(nsecs_t when) {
Jeff Browne57e8952010-07-23 21:28:06 -0700340 // Reset global meta state because it depends on the list of all configured devices.
341 updateGlobalMetaState();
342
343 // Update input configuration.
344 updateInputConfiguration();
345
346 // Enqueue configuration changed.
347 mDispatcher->notifyConfigurationChanged(when);
348}
349
350void InputReader::configureExcludedDevices() {
351 Vector<String8> excludedDeviceNames;
352 mPolicy->getExcludedDeviceNames(excludedDeviceNames);
353
354 for (size_t i = 0; i < excludedDeviceNames.size(); i++) {
355 mEventHub->addExcludedDevice(excludedDeviceNames[i]);
356 }
357}
358
359void InputReader::updateGlobalMetaState() {
360 { // acquire state lock
361 AutoMutex _l(mStateLock);
362
363 mGlobalMetaState = 0;
364
365 { // acquire device registry reader lock
366 RWLock::AutoRLock _rl(mDeviceRegistryLock);
367
368 for (size_t i = 0; i < mDevices.size(); i++) {
369 InputDevice* device = mDevices.valueAt(i);
370 mGlobalMetaState |= device->getMetaState();
371 }
372 } // release device registry reader lock
373 } // release state lock
374}
375
376int32_t InputReader::getGlobalMetaState() {
377 { // acquire state lock
378 AutoMutex _l(mStateLock);
379
380 return mGlobalMetaState;
381 } // release state lock
382}
383
384void InputReader::updateInputConfiguration() {
385 { // acquire state lock
386 AutoMutex _l(mStateLock);
387
388 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
389 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
390 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
391 { // acquire device registry reader lock
392 RWLock::AutoRLock _rl(mDeviceRegistryLock);
393
394 InputDeviceInfo deviceInfo;
395 for (size_t i = 0; i < mDevices.size(); i++) {
396 InputDevice* device = mDevices.valueAt(i);
397 device->getDeviceInfo(& deviceInfo);
398 uint32_t sources = deviceInfo.getSources();
399
400 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
401 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
402 }
403 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
404 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
405 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
406 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
407 }
408 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
409 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
Jeff Browne839a582010-04-22 18:58:52 -0700410 }
411 }
Jeff Browne57e8952010-07-23 21:28:06 -0700412 } // release device registry reader lock
Jeff Browne839a582010-04-22 18:58:52 -0700413
Jeff Browne57e8952010-07-23 21:28:06 -0700414 mInputConfiguration.touchScreen = touchScreenConfig;
415 mInputConfiguration.keyboard = keyboardConfig;
416 mInputConfiguration.navigation = navigationConfig;
417 } // release state lock
418}
419
420void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
421 { // acquire state lock
422 AutoMutex _l(mStateLock);
423
424 *outConfiguration = mInputConfiguration;
425 } // release state lock
426}
427
428status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
429 { // acquire device registry reader lock
430 RWLock::AutoRLock _rl(mDeviceRegistryLock);
431
432 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
433 if (deviceIndex < 0) {
434 return NAME_NOT_FOUND;
Jeff Browne839a582010-04-22 18:58:52 -0700435 }
436
Jeff Browne57e8952010-07-23 21:28:06 -0700437 InputDevice* device = mDevices.valueAt(deviceIndex);
438 if (device->isIgnored()) {
439 return NAME_NOT_FOUND;
Jeff Browne839a582010-04-22 18:58:52 -0700440 }
Jeff Browne57e8952010-07-23 21:28:06 -0700441
442 device->getDeviceInfo(outDeviceInfo);
443 return OK;
444 } // release device registy reader lock
445}
446
447void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
448 outDeviceIds.clear();
449
450 { // acquire device registry reader lock
451 RWLock::AutoRLock _rl(mDeviceRegistryLock);
452
453 size_t numDevices = mDevices.size();
454 for (size_t i = 0; i < numDevices; i++) {
455 InputDevice* device = mDevices.valueAt(i);
456 if (! device->isIgnored()) {
457 outDeviceIds.add(device->getId());
458 }
459 }
460 } // release device registy reader lock
461}
462
463int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
464 int32_t keyCode) {
465 return getState(deviceId, sourceMask, keyCode, & InputDevice::getKeyCodeState);
466}
467
468int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
469 int32_t scanCode) {
470 return getState(deviceId, sourceMask, scanCode, & InputDevice::getScanCodeState);
471}
472
473int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
474 return getState(deviceId, sourceMask, switchCode, & InputDevice::getSwitchState);
475}
476
477int32_t InputReader::getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
478 GetStateFunc getStateFunc) {
479 { // acquire device registry reader lock
480 RWLock::AutoRLock _rl(mDeviceRegistryLock);
481
482 int32_t result = AKEY_STATE_UNKNOWN;
483 if (deviceId >= 0) {
484 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
485 if (deviceIndex >= 0) {
486 InputDevice* device = mDevices.valueAt(deviceIndex);
487 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
488 result = (device->*getStateFunc)(sourceMask, code);
489 }
490 }
491 } else {
492 size_t numDevices = mDevices.size();
493 for (size_t i = 0; i < numDevices; i++) {
494 InputDevice* device = mDevices.valueAt(i);
495 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
496 result = (device->*getStateFunc)(sourceMask, code);
497 if (result >= AKEY_STATE_DOWN) {
498 return result;
499 }
500 }
501 }
502 }
503 return result;
504 } // release device registy reader lock
505}
506
507bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
508 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
509 memset(outFlags, 0, numCodes);
510 return markSupportedKeyCodes(deviceId, sourceMask, numCodes, keyCodes, outFlags);
511}
512
513bool InputReader::markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
514 const int32_t* keyCodes, uint8_t* outFlags) {
515 { // acquire device registry reader lock
516 RWLock::AutoRLock _rl(mDeviceRegistryLock);
517 bool result = false;
518 if (deviceId >= 0) {
519 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
520 if (deviceIndex >= 0) {
521 InputDevice* device = mDevices.valueAt(deviceIndex);
522 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
523 result = device->markSupportedKeyCodes(sourceMask,
524 numCodes, keyCodes, outFlags);
525 }
526 }
527 } else {
528 size_t numDevices = mDevices.size();
529 for (size_t i = 0; i < numDevices; i++) {
530 InputDevice* device = mDevices.valueAt(i);
531 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
532 result |= device->markSupportedKeyCodes(sourceMask,
533 numCodes, keyCodes, outFlags);
534 }
535 }
536 }
537 return result;
538 } // release device registy reader lock
539}
540
Jeff Browna665ca82010-09-08 11:49:43 -0700541void InputReader::dump(String8& dump) {
Jeff Brown2806e382010-10-01 17:46:21 -0700542 mEventHub->dump(dump);
543 dump.append("\n");
544
545 dump.append("Input Reader State:\n");
546
Jeff Brown26c94ff2010-09-30 14:33:04 -0700547 { // acquire device registry reader lock
548 RWLock::AutoRLock _rl(mDeviceRegistryLock);
Jeff Browna665ca82010-09-08 11:49:43 -0700549
Jeff Brown26c94ff2010-09-30 14:33:04 -0700550 for (size_t i = 0; i < mDevices.size(); i++) {
551 mDevices.valueAt(i)->dump(dump);
Jeff Browna665ca82010-09-08 11:49:43 -0700552 }
Jeff Brown26c94ff2010-09-30 14:33:04 -0700553 } // release device registy reader lock
Jeff Browna665ca82010-09-08 11:49:43 -0700554}
555
Jeff Browne57e8952010-07-23 21:28:06 -0700556
557// --- InputReaderThread ---
558
559InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
560 Thread(/*canCallJava*/ true), mReader(reader) {
561}
562
563InputReaderThread::~InputReaderThread() {
564}
565
566bool InputReaderThread::threadLoop() {
567 mReader->loopOnce();
568 return true;
569}
570
571
572// --- InputDevice ---
573
574InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) :
575 mContext(context), mId(id), mName(name), mSources(0) {
576}
577
578InputDevice::~InputDevice() {
579 size_t numMappers = mMappers.size();
580 for (size_t i = 0; i < numMappers; i++) {
581 delete mMappers[i];
582 }
583 mMappers.clear();
584}
585
Jeff Brown26c94ff2010-09-30 14:33:04 -0700586static void dumpMotionRange(String8& dump, const InputDeviceInfo& deviceInfo,
587 int32_t rangeType, const char* name) {
588 const InputDeviceInfo::MotionRange* range = deviceInfo.getMotionRange(rangeType);
589 if (range) {
590 dump.appendFormat(INDENT3 "%s: min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
591 name, range->min, range->max, range->flat, range->fuzz);
592 }
593}
594
595void InputDevice::dump(String8& dump) {
596 InputDeviceInfo deviceInfo;
597 getDeviceInfo(& deviceInfo);
598
599 dump.appendFormat(INDENT "Device 0x%x: %s\n", deviceInfo.getId(),
600 deviceInfo.getName().string());
601 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
602 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
603 if (!deviceInfo.getMotionRanges().isEmpty()) {
604 dump.append(INDENT2 "Motion Ranges:\n");
605 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_X, "X");
606 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_Y, "Y");
607 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_PRESSURE, "Pressure");
608 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_SIZE, "Size");
609 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOUCH_MAJOR, "TouchMajor");
610 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOUCH_MINOR, "TouchMinor");
611 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOOL_MAJOR, "ToolMajor");
612 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOOL_MINOR, "ToolMinor");
613 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_ORIENTATION, "Orientation");
614 }
615
616 size_t numMappers = mMappers.size();
617 for (size_t i = 0; i < numMappers; i++) {
618 InputMapper* mapper = mMappers[i];
619 mapper->dump(dump);
620 }
621}
622
Jeff Browne57e8952010-07-23 21:28:06 -0700623void InputDevice::addMapper(InputMapper* mapper) {
624 mMappers.add(mapper);
625}
626
627void InputDevice::configure() {
Jeff Brown38a7fab2010-08-30 03:02:23 -0700628 if (! isIgnored()) {
629 mContext->getPolicy()->getInputDeviceCalibration(mName, mCalibration);
630 }
631
Jeff Browne57e8952010-07-23 21:28:06 -0700632 mSources = 0;
633
634 size_t numMappers = mMappers.size();
635 for (size_t i = 0; i < numMappers; i++) {
636 InputMapper* mapper = mMappers[i];
637 mapper->configure();
638 mSources |= mapper->getSources();
Jeff Browne839a582010-04-22 18:58:52 -0700639 }
640}
641
Jeff Browne57e8952010-07-23 21:28:06 -0700642void InputDevice::reset() {
643 size_t numMappers = mMappers.size();
644 for (size_t i = 0; i < numMappers; i++) {
645 InputMapper* mapper = mMappers[i];
646 mapper->reset();
647 }
648}
Jeff Browne839a582010-04-22 18:58:52 -0700649
Jeff Browne57e8952010-07-23 21:28:06 -0700650void InputDevice::process(const RawEvent* rawEvent) {
651 size_t numMappers = mMappers.size();
652 for (size_t i = 0; i < numMappers; i++) {
653 InputMapper* mapper = mMappers[i];
654 mapper->process(rawEvent);
655 }
656}
Jeff Browne839a582010-04-22 18:58:52 -0700657
Jeff Browne57e8952010-07-23 21:28:06 -0700658void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
659 outDeviceInfo->initialize(mId, mName);
660
661 size_t numMappers = mMappers.size();
662 for (size_t i = 0; i < numMappers; i++) {
663 InputMapper* mapper = mMappers[i];
664 mapper->populateDeviceInfo(outDeviceInfo);
665 }
666}
667
668int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
669 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
670}
671
672int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
673 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
674}
675
676int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
677 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
678}
679
680int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
681 int32_t result = AKEY_STATE_UNKNOWN;
682 size_t numMappers = mMappers.size();
683 for (size_t i = 0; i < numMappers; i++) {
684 InputMapper* mapper = mMappers[i];
685 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
686 result = (mapper->*getStateFunc)(sourceMask, code);
687 if (result >= AKEY_STATE_DOWN) {
688 return result;
689 }
690 }
691 }
692 return result;
693}
694
695bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
696 const int32_t* keyCodes, uint8_t* outFlags) {
697 bool result = false;
698 size_t numMappers = mMappers.size();
699 for (size_t i = 0; i < numMappers; i++) {
700 InputMapper* mapper = mMappers[i];
701 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
702 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
703 }
704 }
705 return result;
706}
707
708int32_t InputDevice::getMetaState() {
709 int32_t result = 0;
710 size_t numMappers = mMappers.size();
711 for (size_t i = 0; i < numMappers; i++) {
712 InputMapper* mapper = mMappers[i];
713 result |= mapper->getMetaState();
714 }
715 return result;
716}
717
718
719// --- InputMapper ---
720
721InputMapper::InputMapper(InputDevice* device) :
722 mDevice(device), mContext(device->getContext()) {
723}
724
725InputMapper::~InputMapper() {
726}
727
728void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
729 info->addSource(getSources());
730}
731
Jeff Brown26c94ff2010-09-30 14:33:04 -0700732void InputMapper::dump(String8& dump) {
733}
734
Jeff Browne57e8952010-07-23 21:28:06 -0700735void InputMapper::configure() {
736}
737
738void InputMapper::reset() {
739}
740
741int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
742 return AKEY_STATE_UNKNOWN;
743}
744
745int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
746 return AKEY_STATE_UNKNOWN;
747}
748
749int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
750 return AKEY_STATE_UNKNOWN;
751}
752
753bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
754 const int32_t* keyCodes, uint8_t* outFlags) {
755 return false;
756}
757
758int32_t InputMapper::getMetaState() {
759 return 0;
760}
761
Jeff Browne57e8952010-07-23 21:28:06 -0700762
763// --- SwitchInputMapper ---
764
765SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
766 InputMapper(device) {
767}
768
769SwitchInputMapper::~SwitchInputMapper() {
770}
771
772uint32_t SwitchInputMapper::getSources() {
773 return 0;
774}
775
776void SwitchInputMapper::process(const RawEvent* rawEvent) {
777 switch (rawEvent->type) {
778 case EV_SW:
779 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
780 break;
781 }
782}
783
784void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brown90f0cee2010-10-08 22:31:17 -0700785 getDispatcher()->notifySwitch(when, switchCode, switchValue, 0);
Jeff Browne57e8952010-07-23 21:28:06 -0700786}
787
788int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
789 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
790}
791
792
793// --- KeyboardInputMapper ---
794
795KeyboardInputMapper::KeyboardInputMapper(InputDevice* device, int32_t associatedDisplayId,
796 uint32_t sources, int32_t keyboardType) :
797 InputMapper(device), mAssociatedDisplayId(associatedDisplayId), mSources(sources),
798 mKeyboardType(keyboardType) {
Jeff Brownb51719b2010-07-29 18:18:33 -0700799 initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700800}
801
802KeyboardInputMapper::~KeyboardInputMapper() {
803}
804
Jeff Brownb51719b2010-07-29 18:18:33 -0700805void KeyboardInputMapper::initializeLocked() {
806 mLocked.metaState = AMETA_NONE;
807 mLocked.downTime = 0;
Jeff Brown6a817e22010-09-12 17:55:08 -0700808
809 initializeLedStateLocked(mLocked.capsLockLedState, LED_CAPSL);
810 initializeLedStateLocked(mLocked.numLockLedState, LED_NUML);
811 initializeLedStateLocked(mLocked.scrollLockLedState, LED_SCROLLL);
812
813 updateLedStateLocked(true);
814}
815
816void KeyboardInputMapper::initializeLedStateLocked(LockedState::LedState& ledState, int32_t led) {
817 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
818 ledState.on = false;
Jeff Browne57e8952010-07-23 21:28:06 -0700819}
820
821uint32_t KeyboardInputMapper::getSources() {
822 return mSources;
823}
824
825void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
826 InputMapper::populateDeviceInfo(info);
827
828 info->setKeyboardType(mKeyboardType);
829}
830
Jeff Brown26c94ff2010-09-30 14:33:04 -0700831void KeyboardInputMapper::dump(String8& dump) {
832 { // acquire lock
833 AutoMutex _l(mLock);
834 dump.append(INDENT2 "Keyboard Input Mapper:\n");
835 dump.appendFormat(INDENT3 "AssociatedDisplayId: %d\n", mAssociatedDisplayId);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700836 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
837 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mLocked.keyDowns.size());
838 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mLocked.metaState);
839 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
840 } // release lock
841}
842
Jeff Browne57e8952010-07-23 21:28:06 -0700843void KeyboardInputMapper::reset() {
Jeff Brownb51719b2010-07-29 18:18:33 -0700844 for (;;) {
845 int32_t keyCode, scanCode;
846 { // acquire lock
847 AutoMutex _l(mLock);
848
849 // Synthesize key up event on reset if keys are currently down.
850 if (mLocked.keyDowns.isEmpty()) {
851 initializeLocked();
852 break; // done
853 }
854
855 const KeyDown& keyDown = mLocked.keyDowns.top();
856 keyCode = keyDown.keyCode;
857 scanCode = keyDown.scanCode;
858 } // release lock
859
Jeff Browne57e8952010-07-23 21:28:06 -0700860 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownb51719b2010-07-29 18:18:33 -0700861 processKey(when, false, keyCode, scanCode, 0);
Jeff Browne57e8952010-07-23 21:28:06 -0700862 }
863
864 InputMapper::reset();
Jeff Browne57e8952010-07-23 21:28:06 -0700865 getContext()->updateGlobalMetaState();
866}
867
868void KeyboardInputMapper::process(const RawEvent* rawEvent) {
869 switch (rawEvent->type) {
870 case EV_KEY: {
871 int32_t scanCode = rawEvent->scanCode;
872 if (isKeyboardOrGamepadKey(scanCode)) {
873 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
874 rawEvent->flags);
875 }
876 break;
877 }
878 }
879}
880
881bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
882 return scanCode < BTN_MOUSE
883 || scanCode >= KEY_OK
884 || (scanCode >= BTN_GAMEPAD && scanCode < BTN_DIGI);
885}
886
Jeff Brownb51719b2010-07-29 18:18:33 -0700887void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
888 int32_t scanCode, uint32_t policyFlags) {
889 int32_t newMetaState;
890 nsecs_t downTime;
891 bool metaStateChanged = false;
892
893 { // acquire lock
894 AutoMutex _l(mLock);
895
896 if (down) {
897 // Rotate key codes according to orientation if needed.
898 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
899 if (mAssociatedDisplayId >= 0) {
900 int32_t orientation;
Jeff Brownd4ecee92010-10-29 22:19:53 -0700901 if (!getPolicy()->getDisplayInfo(mAssociatedDisplayId, NULL, NULL, & orientation)) {
902 orientation = InputReaderPolicyInterface::ROTATION_0;
Jeff Brownb51719b2010-07-29 18:18:33 -0700903 }
904
905 keyCode = rotateKeyCode(keyCode, orientation);
Jeff Browne57e8952010-07-23 21:28:06 -0700906 }
907
Jeff Brownb51719b2010-07-29 18:18:33 -0700908 // Add key down.
909 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
910 if (keyDownIndex >= 0) {
911 // key repeat, be sure to use same keycode as before in case of rotation
Jeff Browna3477c82010-11-10 16:03:06 -0800912 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brownb51719b2010-07-29 18:18:33 -0700913 } else {
914 // key down
915 mLocked.keyDowns.push();
916 KeyDown& keyDown = mLocked.keyDowns.editTop();
917 keyDown.keyCode = keyCode;
918 keyDown.scanCode = scanCode;
919 }
920
921 mLocked.downTime = when;
922 } else {
923 // Remove key down.
924 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
925 if (keyDownIndex >= 0) {
926 // key up, be sure to use same keycode as before in case of rotation
Jeff Browna3477c82010-11-10 16:03:06 -0800927 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brownb51719b2010-07-29 18:18:33 -0700928 mLocked.keyDowns.removeAt(size_t(keyDownIndex));
929 } else {
930 // key was not actually down
931 LOGI("Dropping key up from device %s because the key was not down. "
932 "keyCode=%d, scanCode=%d",
933 getDeviceName().string(), keyCode, scanCode);
934 return;
935 }
Jeff Browne57e8952010-07-23 21:28:06 -0700936 }
937
Jeff Brownb51719b2010-07-29 18:18:33 -0700938 int32_t oldMetaState = mLocked.metaState;
939 newMetaState = updateMetaState(keyCode, down, oldMetaState);
940 if (oldMetaState != newMetaState) {
941 mLocked.metaState = newMetaState;
942 metaStateChanged = true;
Jeff Brown6a817e22010-09-12 17:55:08 -0700943 updateLedStateLocked(false);
Jeff Browne57e8952010-07-23 21:28:06 -0700944 }
Jeff Brown8575a872010-06-30 16:10:35 -0700945
Jeff Brownb51719b2010-07-29 18:18:33 -0700946 downTime = mLocked.downTime;
947 } // release lock
948
949 if (metaStateChanged) {
Jeff Browne57e8952010-07-23 21:28:06 -0700950 getContext()->updateGlobalMetaState();
Jeff Browne839a582010-04-22 18:58:52 -0700951 }
952
Jeff Brown6a817e22010-09-12 17:55:08 -0700953 if (policyFlags & POLICY_FLAG_FUNCTION) {
954 newMetaState |= AMETA_FUNCTION_ON;
955 }
Jeff Browne57e8952010-07-23 21:28:06 -0700956 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
Jeff Brown90f0cee2010-10-08 22:31:17 -0700957 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
958 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Browne839a582010-04-22 18:58:52 -0700959}
960
Jeff Brownb51719b2010-07-29 18:18:33 -0700961ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) {
962 size_t n = mLocked.keyDowns.size();
Jeff Browne57e8952010-07-23 21:28:06 -0700963 for (size_t i = 0; i < n; i++) {
Jeff Brownb51719b2010-07-29 18:18:33 -0700964 if (mLocked.keyDowns[i].scanCode == scanCode) {
Jeff Browne57e8952010-07-23 21:28:06 -0700965 return i;
966 }
967 }
968 return -1;
Jeff Browne839a582010-04-22 18:58:52 -0700969}
970
Jeff Browne57e8952010-07-23 21:28:06 -0700971int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
972 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
973}
Jeff Browne839a582010-04-22 18:58:52 -0700974
Jeff Browne57e8952010-07-23 21:28:06 -0700975int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
976 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
977}
Jeff Browne839a582010-04-22 18:58:52 -0700978
Jeff Browne57e8952010-07-23 21:28:06 -0700979bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
980 const int32_t* keyCodes, uint8_t* outFlags) {
981 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
982}
983
984int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownb51719b2010-07-29 18:18:33 -0700985 { // acquire lock
986 AutoMutex _l(mLock);
987 return mLocked.metaState;
988 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -0700989}
990
Jeff Brown6a817e22010-09-12 17:55:08 -0700991void KeyboardInputMapper::updateLedStateLocked(bool reset) {
992 updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL,
Jeff Brownd4ecee92010-10-29 22:19:53 -0700993 AMETA_CAPS_LOCK_ON, reset);
Jeff Brown6a817e22010-09-12 17:55:08 -0700994 updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML,
Jeff Brownd4ecee92010-10-29 22:19:53 -0700995 AMETA_NUM_LOCK_ON, reset);
Jeff Brown6a817e22010-09-12 17:55:08 -0700996 updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL,
Jeff Brownd4ecee92010-10-29 22:19:53 -0700997 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown6a817e22010-09-12 17:55:08 -0700998}
999
1000void KeyboardInputMapper::updateLedStateForModifierLocked(LockedState::LedState& ledState,
1001 int32_t led, int32_t modifier, bool reset) {
1002 if (ledState.avail) {
1003 bool desiredState = (mLocked.metaState & modifier) != 0;
1004 if (ledState.on != desiredState) {
1005 getEventHub()->setLedState(getDeviceId(), led, desiredState);
1006 ledState.on = desiredState;
1007 }
1008 }
1009}
1010
Jeff Browne57e8952010-07-23 21:28:06 -07001011
1012// --- TrackballInputMapper ---
1013
1014TrackballInputMapper::TrackballInputMapper(InputDevice* device, int32_t associatedDisplayId) :
1015 InputMapper(device), mAssociatedDisplayId(associatedDisplayId) {
1016 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1017 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1018 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1019 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1020
Jeff Brownb51719b2010-07-29 18:18:33 -07001021 initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -07001022}
1023
1024TrackballInputMapper::~TrackballInputMapper() {
1025}
1026
1027uint32_t TrackballInputMapper::getSources() {
1028 return AINPUT_SOURCE_TRACKBALL;
1029}
1030
1031void TrackballInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1032 InputMapper::populateDeviceInfo(info);
1033
1034 info->addMotionRange(AINPUT_MOTION_RANGE_X, -1.0f, 1.0f, 0.0f, mXScale);
1035 info->addMotionRange(AINPUT_MOTION_RANGE_Y, -1.0f, 1.0f, 0.0f, mYScale);
1036}
1037
Jeff Brown26c94ff2010-09-30 14:33:04 -07001038void TrackballInputMapper::dump(String8& dump) {
1039 { // acquire lock
1040 AutoMutex _l(mLock);
1041 dump.append(INDENT2 "Trackball Input Mapper:\n");
1042 dump.appendFormat(INDENT3 "AssociatedDisplayId: %d\n", mAssociatedDisplayId);
1043 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1044 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
1045 dump.appendFormat(INDENT3 "Down: %s\n", toString(mLocked.down));
1046 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1047 } // release lock
1048}
1049
Jeff Brownb51719b2010-07-29 18:18:33 -07001050void TrackballInputMapper::initializeLocked() {
Jeff Browne57e8952010-07-23 21:28:06 -07001051 mAccumulator.clear();
1052
Jeff Brownb51719b2010-07-29 18:18:33 -07001053 mLocked.down = false;
1054 mLocked.downTime = 0;
Jeff Browne57e8952010-07-23 21:28:06 -07001055}
1056
1057void TrackballInputMapper::reset() {
Jeff Brownb51719b2010-07-29 18:18:33 -07001058 for (;;) {
1059 { // acquire lock
1060 AutoMutex _l(mLock);
1061
1062 if (! mLocked.down) {
1063 initializeLocked();
1064 break; // done
1065 }
1066 } // release lock
1067
1068 // Synthesize trackball button up event on reset.
Jeff Browne57e8952010-07-23 21:28:06 -07001069 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownb51719b2010-07-29 18:18:33 -07001070 mAccumulator.fields = Accumulator::FIELD_BTN_MOUSE;
Jeff Browne57e8952010-07-23 21:28:06 -07001071 mAccumulator.btnMouse = false;
1072 sync(when);
Jeff Browne839a582010-04-22 18:58:52 -07001073 }
1074
Jeff Browne57e8952010-07-23 21:28:06 -07001075 InputMapper::reset();
Jeff Browne57e8952010-07-23 21:28:06 -07001076}
Jeff Browne839a582010-04-22 18:58:52 -07001077
Jeff Browne57e8952010-07-23 21:28:06 -07001078void TrackballInputMapper::process(const RawEvent* rawEvent) {
1079 switch (rawEvent->type) {
1080 case EV_KEY:
1081 switch (rawEvent->scanCode) {
1082 case BTN_MOUSE:
1083 mAccumulator.fields |= Accumulator::FIELD_BTN_MOUSE;
1084 mAccumulator.btnMouse = rawEvent->value != 0;
Jeff Brownd64c8552010-08-17 20:38:35 -07001085 // Sync now since BTN_MOUSE is not necessarily followed by SYN_REPORT and
1086 // we need to ensure that we report the up/down promptly.
Jeff Browne57e8952010-07-23 21:28:06 -07001087 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07001088 break;
Jeff Browne839a582010-04-22 18:58:52 -07001089 }
Jeff Browne57e8952010-07-23 21:28:06 -07001090 break;
Jeff Browne839a582010-04-22 18:58:52 -07001091
Jeff Browne57e8952010-07-23 21:28:06 -07001092 case EV_REL:
1093 switch (rawEvent->scanCode) {
1094 case REL_X:
1095 mAccumulator.fields |= Accumulator::FIELD_REL_X;
1096 mAccumulator.relX = rawEvent->value;
1097 break;
1098 case REL_Y:
1099 mAccumulator.fields |= Accumulator::FIELD_REL_Y;
1100 mAccumulator.relY = rawEvent->value;
1101 break;
Jeff Browne839a582010-04-22 18:58:52 -07001102 }
Jeff Browne57e8952010-07-23 21:28:06 -07001103 break;
Jeff Browne839a582010-04-22 18:58:52 -07001104
Jeff Browne57e8952010-07-23 21:28:06 -07001105 case EV_SYN:
1106 switch (rawEvent->scanCode) {
1107 case SYN_REPORT:
Jeff Brownd64c8552010-08-17 20:38:35 -07001108 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07001109 break;
Jeff Browne839a582010-04-22 18:58:52 -07001110 }
Jeff Browne57e8952010-07-23 21:28:06 -07001111 break;
Jeff Browne839a582010-04-22 18:58:52 -07001112 }
Jeff Browne839a582010-04-22 18:58:52 -07001113}
1114
Jeff Browne57e8952010-07-23 21:28:06 -07001115void TrackballInputMapper::sync(nsecs_t when) {
Jeff Brownd64c8552010-08-17 20:38:35 -07001116 uint32_t fields = mAccumulator.fields;
1117 if (fields == 0) {
1118 return; // no new state changes, so nothing to do
1119 }
1120
Jeff Brownb51719b2010-07-29 18:18:33 -07001121 int motionEventAction;
1122 PointerCoords pointerCoords;
1123 nsecs_t downTime;
1124 { // acquire lock
1125 AutoMutex _l(mLock);
Jeff Browne839a582010-04-22 18:58:52 -07001126
Jeff Brownb51719b2010-07-29 18:18:33 -07001127 bool downChanged = fields & Accumulator::FIELD_BTN_MOUSE;
1128
1129 if (downChanged) {
1130 if (mAccumulator.btnMouse) {
1131 mLocked.down = true;
1132 mLocked.downTime = when;
1133 } else {
1134 mLocked.down = false;
1135 }
Jeff Browne57e8952010-07-23 21:28:06 -07001136 }
Jeff Browne839a582010-04-22 18:58:52 -07001137
Jeff Brownb51719b2010-07-29 18:18:33 -07001138 downTime = mLocked.downTime;
1139 float x = fields & Accumulator::FIELD_REL_X ? mAccumulator.relX * mXScale : 0.0f;
1140 float y = fields & Accumulator::FIELD_REL_Y ? mAccumulator.relY * mYScale : 0.0f;
Jeff Browne839a582010-04-22 18:58:52 -07001141
Jeff Brownb51719b2010-07-29 18:18:33 -07001142 if (downChanged) {
1143 motionEventAction = mLocked.down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
Jeff Browne57e8952010-07-23 21:28:06 -07001144 } else {
Jeff Brownb51719b2010-07-29 18:18:33 -07001145 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browne57e8952010-07-23 21:28:06 -07001146 }
Jeff Browne839a582010-04-22 18:58:52 -07001147
Jeff Brownb51719b2010-07-29 18:18:33 -07001148 pointerCoords.x = x;
1149 pointerCoords.y = y;
1150 pointerCoords.pressure = mLocked.down ? 1.0f : 0.0f;
1151 pointerCoords.size = 0;
1152 pointerCoords.touchMajor = 0;
1153 pointerCoords.touchMinor = 0;
1154 pointerCoords.toolMajor = 0;
1155 pointerCoords.toolMinor = 0;
1156 pointerCoords.orientation = 0;
Jeff Browne839a582010-04-22 18:58:52 -07001157
Jeff Brownb51719b2010-07-29 18:18:33 -07001158 if (mAssociatedDisplayId >= 0 && (x != 0.0f || y != 0.0f)) {
1159 // Rotate motion based on display orientation if needed.
1160 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1161 int32_t orientation;
1162 if (! getPolicy()->getDisplayInfo(mAssociatedDisplayId, NULL, NULL, & orientation)) {
Jeff Brownd4ecee92010-10-29 22:19:53 -07001163 orientation = InputReaderPolicyInterface::ROTATION_0;
Jeff Brownb51719b2010-07-29 18:18:33 -07001164 }
1165
1166 float temp;
1167 switch (orientation) {
1168 case InputReaderPolicyInterface::ROTATION_90:
1169 temp = pointerCoords.x;
1170 pointerCoords.x = pointerCoords.y;
1171 pointerCoords.y = - temp;
1172 break;
1173
1174 case InputReaderPolicyInterface::ROTATION_180:
1175 pointerCoords.x = - pointerCoords.x;
1176 pointerCoords.y = - pointerCoords.y;
1177 break;
1178
1179 case InputReaderPolicyInterface::ROTATION_270:
1180 temp = pointerCoords.x;
1181 pointerCoords.x = - pointerCoords.y;
1182 pointerCoords.y = temp;
1183 break;
1184 }
1185 }
1186 } // release lock
1187
Jeff Browne57e8952010-07-23 21:28:06 -07001188 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownb51719b2010-07-29 18:18:33 -07001189 int32_t pointerId = 0;
Jeff Brown90f0cee2010-10-08 22:31:17 -07001190 getDispatcher()->notifyMotion(when, getDeviceId(), AINPUT_SOURCE_TRACKBALL, 0,
Jeff Brownaf30ff62010-09-01 17:01:00 -07001191 motionEventAction, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown90f0cee2010-10-08 22:31:17 -07001192 1, &pointerId, &pointerCoords, mXPrecision, mYPrecision, downTime);
1193
1194 mAccumulator.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07001195}
1196
Jeff Brown8d4dfd22010-08-10 15:47:53 -07001197int32_t TrackballInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1198 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
1199 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1200 } else {
1201 return AKEY_STATE_UNKNOWN;
1202 }
1203}
1204
Jeff Browne57e8952010-07-23 21:28:06 -07001205
1206// --- TouchInputMapper ---
1207
1208TouchInputMapper::TouchInputMapper(InputDevice* device, int32_t associatedDisplayId) :
Jeff Brownb51719b2010-07-29 18:18:33 -07001209 InputMapper(device), mAssociatedDisplayId(associatedDisplayId) {
1210 mLocked.surfaceOrientation = -1;
1211 mLocked.surfaceWidth = -1;
1212 mLocked.surfaceHeight = -1;
1213
1214 initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -07001215}
1216
1217TouchInputMapper::~TouchInputMapper() {
1218}
1219
1220uint32_t TouchInputMapper::getSources() {
1221 return mAssociatedDisplayId >= 0 ? AINPUT_SOURCE_TOUCHSCREEN : AINPUT_SOURCE_TOUCHPAD;
1222}
1223
1224void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1225 InputMapper::populateDeviceInfo(info);
1226
Jeff Brownb51719b2010-07-29 18:18:33 -07001227 { // acquire lock
1228 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07001229
Jeff Brownb51719b2010-07-29 18:18:33 -07001230 // Ensure surface information is up to date so that orientation changes are
1231 // noticed immediately.
1232 configureSurfaceLocked();
1233
1234 info->addMotionRange(AINPUT_MOTION_RANGE_X, mLocked.orientedRanges.x);
1235 info->addMotionRange(AINPUT_MOTION_RANGE_Y, mLocked.orientedRanges.y);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001236
1237 if (mLocked.orientedRanges.havePressure) {
1238 info->addMotionRange(AINPUT_MOTION_RANGE_PRESSURE,
1239 mLocked.orientedRanges.pressure);
1240 }
1241
1242 if (mLocked.orientedRanges.haveSize) {
1243 info->addMotionRange(AINPUT_MOTION_RANGE_SIZE,
1244 mLocked.orientedRanges.size);
1245 }
1246
Jeff Brown6b337e72010-10-14 21:42:15 -07001247 if (mLocked.orientedRanges.haveTouchSize) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001248 info->addMotionRange(AINPUT_MOTION_RANGE_TOUCH_MAJOR,
1249 mLocked.orientedRanges.touchMajor);
1250 info->addMotionRange(AINPUT_MOTION_RANGE_TOUCH_MINOR,
1251 mLocked.orientedRanges.touchMinor);
1252 }
1253
Jeff Brown6b337e72010-10-14 21:42:15 -07001254 if (mLocked.orientedRanges.haveToolSize) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001255 info->addMotionRange(AINPUT_MOTION_RANGE_TOOL_MAJOR,
1256 mLocked.orientedRanges.toolMajor);
1257 info->addMotionRange(AINPUT_MOTION_RANGE_TOOL_MINOR,
1258 mLocked.orientedRanges.toolMinor);
1259 }
1260
1261 if (mLocked.orientedRanges.haveOrientation) {
1262 info->addMotionRange(AINPUT_MOTION_RANGE_ORIENTATION,
1263 mLocked.orientedRanges.orientation);
1264 }
Jeff Brownb51719b2010-07-29 18:18:33 -07001265 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07001266}
1267
Jeff Brown26c94ff2010-09-30 14:33:04 -07001268void TouchInputMapper::dump(String8& dump) {
1269 { // acquire lock
1270 AutoMutex _l(mLock);
1271 dump.append(INDENT2 "Touch Input Mapper:\n");
1272 dump.appendFormat(INDENT3 "AssociatedDisplayId: %d\n", mAssociatedDisplayId);
1273 dumpParameters(dump);
1274 dumpVirtualKeysLocked(dump);
1275 dumpRawAxes(dump);
1276 dumpCalibration(dump);
1277 dumpSurfaceLocked(dump);
Jeff Brown60b57762010-10-18 13:32:20 -07001278 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
Jeff Brown6b337e72010-10-14 21:42:15 -07001279 dump.appendFormat(INDENT4 "XOrigin: %d\n", mLocked.xOrigin);
1280 dump.appendFormat(INDENT4 "YOrigin: %d\n", mLocked.yOrigin);
1281 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mLocked.xScale);
1282 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mLocked.yScale);
1283 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mLocked.xPrecision);
1284 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mLocked.yPrecision);
1285 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mLocked.geometricScale);
1286 dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mLocked.toolSizeLinearScale);
1287 dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mLocked.toolSizeLinearBias);
1288 dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mLocked.toolSizeAreaScale);
1289 dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mLocked.toolSizeAreaBias);
1290 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mLocked.pressureScale);
1291 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mLocked.sizeScale);
1292 dump.appendFormat(INDENT4 "OrientationSCale: %0.3f\n", mLocked.orientationScale);
Jeff Brown26c94ff2010-09-30 14:33:04 -07001293 } // release lock
1294}
1295
Jeff Brownb51719b2010-07-29 18:18:33 -07001296void TouchInputMapper::initializeLocked() {
1297 mCurrentTouch.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07001298 mLastTouch.clear();
1299 mDownTime = 0;
Jeff Browne57e8952010-07-23 21:28:06 -07001300
1301 for (uint32_t i = 0; i < MAX_POINTERS; i++) {
1302 mAveragingTouchFilter.historyStart[i] = 0;
1303 mAveragingTouchFilter.historyEnd[i] = 0;
1304 }
1305
1306 mJumpyTouchFilter.jumpyPointsDropped = 0;
Jeff Brownb51719b2010-07-29 18:18:33 -07001307
1308 mLocked.currentVirtualKey.down = false;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001309
1310 mLocked.orientedRanges.havePressure = false;
1311 mLocked.orientedRanges.haveSize = false;
Jeff Brown6b337e72010-10-14 21:42:15 -07001312 mLocked.orientedRanges.haveTouchSize = false;
1313 mLocked.orientedRanges.haveToolSize = false;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001314 mLocked.orientedRanges.haveOrientation = false;
1315}
1316
Jeff Browne57e8952010-07-23 21:28:06 -07001317void TouchInputMapper::configure() {
1318 InputMapper::configure();
1319
1320 // Configure basic parameters.
Jeff Brown38a7fab2010-08-30 03:02:23 -07001321 configureParameters();
Jeff Browne57e8952010-07-23 21:28:06 -07001322
1323 // Configure absolute axis information.
Jeff Brown38a7fab2010-08-30 03:02:23 -07001324 configureRawAxes();
Jeff Brown38a7fab2010-08-30 03:02:23 -07001325
1326 // Prepare input device calibration.
1327 parseCalibration();
1328 resolveCalibration();
Jeff Browne57e8952010-07-23 21:28:06 -07001329
Jeff Brownb51719b2010-07-29 18:18:33 -07001330 { // acquire lock
1331 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07001332
Jeff Brown38a7fab2010-08-30 03:02:23 -07001333 // Configure surface dimensions and orientation.
Jeff Brownb51719b2010-07-29 18:18:33 -07001334 configureSurfaceLocked();
1335 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07001336}
1337
Jeff Brown38a7fab2010-08-30 03:02:23 -07001338void TouchInputMapper::configureParameters() {
1339 mParameters.useBadTouchFilter = getPolicy()->filterTouchEvents();
1340 mParameters.useAveragingTouchFilter = getPolicy()->filterTouchEvents();
1341 mParameters.useJumpyTouchFilter = getPolicy()->filterJumpyTouchEvents();
1342}
1343
Jeff Brown26c94ff2010-09-30 14:33:04 -07001344void TouchInputMapper::dumpParameters(String8& dump) {
1345 dump.appendFormat(INDENT3 "UseBadTouchFilter: %s\n",
1346 toString(mParameters.useBadTouchFilter));
1347 dump.appendFormat(INDENT3 "UseAveragingTouchFilter: %s\n",
1348 toString(mParameters.useAveragingTouchFilter));
1349 dump.appendFormat(INDENT3 "UseJumpyTouchFilter: %s\n",
1350 toString(mParameters.useJumpyTouchFilter));
Jeff Browna665ca82010-09-08 11:49:43 -07001351}
1352
Jeff Brown38a7fab2010-08-30 03:02:23 -07001353void TouchInputMapper::configureRawAxes() {
1354 mRawAxes.x.clear();
1355 mRawAxes.y.clear();
1356 mRawAxes.pressure.clear();
1357 mRawAxes.touchMajor.clear();
1358 mRawAxes.touchMinor.clear();
1359 mRawAxes.toolMajor.clear();
1360 mRawAxes.toolMinor.clear();
1361 mRawAxes.orientation.clear();
1362}
1363
Jeff Brown26c94ff2010-09-30 14:33:04 -07001364static void dumpAxisInfo(String8& dump, RawAbsoluteAxisInfo axis, const char* name) {
Jeff Browna665ca82010-09-08 11:49:43 -07001365 if (axis.valid) {
Jeff Brown26c94ff2010-09-30 14:33:04 -07001366 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d\n",
Jeff Browna665ca82010-09-08 11:49:43 -07001367 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz);
1368 } else {
Jeff Brown26c94ff2010-09-30 14:33:04 -07001369 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
Jeff Browna665ca82010-09-08 11:49:43 -07001370 }
1371}
1372
Jeff Brown26c94ff2010-09-30 14:33:04 -07001373void TouchInputMapper::dumpRawAxes(String8& dump) {
1374 dump.append(INDENT3 "Raw Axes:\n");
1375 dumpAxisInfo(dump, mRawAxes.x, "X");
1376 dumpAxisInfo(dump, mRawAxes.y, "Y");
1377 dumpAxisInfo(dump, mRawAxes.pressure, "Pressure");
1378 dumpAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor");
1379 dumpAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor");
1380 dumpAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor");
1381 dumpAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor");
1382 dumpAxisInfo(dump, mRawAxes.orientation, "Orientation");
Jeff Browne57e8952010-07-23 21:28:06 -07001383}
1384
Jeff Brownb51719b2010-07-29 18:18:33 -07001385bool TouchInputMapper::configureSurfaceLocked() {
Jeff Browne57e8952010-07-23 21:28:06 -07001386 // Update orientation and dimensions if needed.
1387 int32_t orientation;
1388 int32_t width, height;
1389 if (mAssociatedDisplayId >= 0) {
Jeff Brownb51719b2010-07-29 18:18:33 -07001390 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Browne57e8952010-07-23 21:28:06 -07001391 if (! getPolicy()->getDisplayInfo(mAssociatedDisplayId, & width, & height, & orientation)) {
1392 return false;
1393 }
1394 } else {
1395 orientation = InputReaderPolicyInterface::ROTATION_0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001396 width = mRawAxes.x.getRange();
1397 height = mRawAxes.y.getRange();
Jeff Browne57e8952010-07-23 21:28:06 -07001398 }
1399
Jeff Brownb51719b2010-07-29 18:18:33 -07001400 bool orientationChanged = mLocked.surfaceOrientation != orientation;
Jeff Browne57e8952010-07-23 21:28:06 -07001401 if (orientationChanged) {
Jeff Brownb51719b2010-07-29 18:18:33 -07001402 mLocked.surfaceOrientation = orientation;
Jeff Browne57e8952010-07-23 21:28:06 -07001403 }
1404
Jeff Brownb51719b2010-07-29 18:18:33 -07001405 bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height;
Jeff Browne57e8952010-07-23 21:28:06 -07001406 if (sizeChanged) {
Jeff Brown26c94ff2010-09-30 14:33:04 -07001407 LOGI("Device reconfigured: id=0x%x, name=%s, display size is now %dx%d",
1408 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001409
Jeff Brownb51719b2010-07-29 18:18:33 -07001410 mLocked.surfaceWidth = width;
1411 mLocked.surfaceHeight = height;
Jeff Browne57e8952010-07-23 21:28:06 -07001412
Jeff Brown38a7fab2010-08-30 03:02:23 -07001413 // Configure X and Y factors.
1414 if (mRawAxes.x.valid && mRawAxes.y.valid) {
Jeff Brown60b57762010-10-18 13:32:20 -07001415 mLocked.xOrigin = mCalibration.haveXOrigin
1416 ? mCalibration.xOrigin
1417 : mRawAxes.x.minValue;
1418 mLocked.yOrigin = mCalibration.haveYOrigin
1419 ? mCalibration.yOrigin
1420 : mRawAxes.y.minValue;
1421 mLocked.xScale = mCalibration.haveXScale
1422 ? mCalibration.xScale
1423 : float(width) / mRawAxes.x.getRange();
1424 mLocked.yScale = mCalibration.haveYScale
1425 ? mCalibration.yScale
1426 : float(height) / mRawAxes.y.getRange();
Jeff Brownb51719b2010-07-29 18:18:33 -07001427 mLocked.xPrecision = 1.0f / mLocked.xScale;
1428 mLocked.yPrecision = 1.0f / mLocked.yScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001429
Jeff Brownb51719b2010-07-29 18:18:33 -07001430 configureVirtualKeysLocked();
Jeff Browne57e8952010-07-23 21:28:06 -07001431 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001432 LOGW(INDENT "Touch device did not report support for X or Y axis!");
Jeff Brownb51719b2010-07-29 18:18:33 -07001433 mLocked.xOrigin = 0;
1434 mLocked.yOrigin = 0;
1435 mLocked.xScale = 1.0f;
1436 mLocked.yScale = 1.0f;
1437 mLocked.xPrecision = 1.0f;
1438 mLocked.yPrecision = 1.0f;
Jeff Browne57e8952010-07-23 21:28:06 -07001439 }
1440
Jeff Brown38a7fab2010-08-30 03:02:23 -07001441 // Scale factor for terms that are not oriented in a particular axis.
1442 // If the pixels are square then xScale == yScale otherwise we fake it
1443 // by choosing an average.
1444 mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale);
Jeff Browne57e8952010-07-23 21:28:06 -07001445
Jeff Brown38a7fab2010-08-30 03:02:23 -07001446 // Size of diagonal axis.
1447 float diagonalSize = pythag(width, height);
Jeff Browne57e8952010-07-23 21:28:06 -07001448
Jeff Brown38a7fab2010-08-30 03:02:23 -07001449 // TouchMajor and TouchMinor factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001450 if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) {
1451 mLocked.orientedRanges.haveTouchSize = true;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001452 mLocked.orientedRanges.touchMajor.min = 0;
1453 mLocked.orientedRanges.touchMajor.max = diagonalSize;
1454 mLocked.orientedRanges.touchMajor.flat = 0;
1455 mLocked.orientedRanges.touchMajor.fuzz = 0;
1456 mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor;
1457 }
Jeff Brownb51719b2010-07-29 18:18:33 -07001458
Jeff Brown38a7fab2010-08-30 03:02:23 -07001459 // ToolMajor and ToolMinor factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001460 mLocked.toolSizeLinearScale = 0;
1461 mLocked.toolSizeLinearBias = 0;
1462 mLocked.toolSizeAreaScale = 0;
1463 mLocked.toolSizeAreaBias = 0;
1464 if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
1465 if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) {
1466 if (mCalibration.haveToolSizeLinearScale) {
1467 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001468 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
Jeff Brown6b337e72010-10-14 21:42:15 -07001469 mLocked.toolSizeLinearScale = float(min(width, height))
Jeff Brown38a7fab2010-08-30 03:02:23 -07001470 / mRawAxes.toolMajor.maxValue;
1471 }
1472
Jeff Brown6b337e72010-10-14 21:42:15 -07001473 if (mCalibration.haveToolSizeLinearBias) {
1474 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
1475 }
1476 } else if (mCalibration.toolSizeCalibration ==
1477 Calibration::TOOL_SIZE_CALIBRATION_AREA) {
1478 if (mCalibration.haveToolSizeLinearScale) {
1479 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
1480 } else {
1481 mLocked.toolSizeLinearScale = min(width, height);
1482 }
1483
1484 if (mCalibration.haveToolSizeLinearBias) {
1485 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
1486 }
1487
1488 if (mCalibration.haveToolSizeAreaScale) {
1489 mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale;
1490 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1491 mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue;
1492 }
1493
1494 if (mCalibration.haveToolSizeAreaBias) {
1495 mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001496 }
1497 }
1498
Jeff Brown6b337e72010-10-14 21:42:15 -07001499 mLocked.orientedRanges.haveToolSize = true;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001500 mLocked.orientedRanges.toolMajor.min = 0;
1501 mLocked.orientedRanges.toolMajor.max = diagonalSize;
1502 mLocked.orientedRanges.toolMajor.flat = 0;
1503 mLocked.orientedRanges.toolMajor.fuzz = 0;
1504 mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor;
1505 }
1506
1507 // Pressure factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001508 mLocked.pressureScale = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001509 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
1510 RawAbsoluteAxisInfo rawPressureAxis;
1511 switch (mCalibration.pressureSource) {
1512 case Calibration::PRESSURE_SOURCE_PRESSURE:
1513 rawPressureAxis = mRawAxes.pressure;
1514 break;
1515 case Calibration::PRESSURE_SOURCE_TOUCH:
1516 rawPressureAxis = mRawAxes.touchMajor;
1517 break;
1518 default:
1519 rawPressureAxis.clear();
1520 }
1521
Jeff Brown38a7fab2010-08-30 03:02:23 -07001522 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
1523 || mCalibration.pressureCalibration
1524 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
1525 if (mCalibration.havePressureScale) {
1526 mLocked.pressureScale = mCalibration.pressureScale;
1527 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
1528 mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue;
1529 }
1530 }
1531
1532 mLocked.orientedRanges.havePressure = true;
1533 mLocked.orientedRanges.pressure.min = 0;
1534 mLocked.orientedRanges.pressure.max = 1.0;
1535 mLocked.orientedRanges.pressure.flat = 0;
1536 mLocked.orientedRanges.pressure.fuzz = 0;
1537 }
1538
1539 // Size factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001540 mLocked.sizeScale = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001541 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001542 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
1543 if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1544 mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue;
1545 }
1546 }
1547
1548 mLocked.orientedRanges.haveSize = true;
1549 mLocked.orientedRanges.size.min = 0;
1550 mLocked.orientedRanges.size.max = 1.0;
1551 mLocked.orientedRanges.size.flat = 0;
1552 mLocked.orientedRanges.size.fuzz = 0;
1553 }
1554
1555 // Orientation
Jeff Brown6b337e72010-10-14 21:42:15 -07001556 mLocked.orientationScale = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001557 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001558 if (mCalibration.orientationCalibration
1559 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
1560 if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) {
1561 mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue;
1562 }
1563 }
1564
1565 mLocked.orientedRanges.orientation.min = - M_PI_2;
1566 mLocked.orientedRanges.orientation.max = M_PI_2;
1567 mLocked.orientedRanges.orientation.flat = 0;
1568 mLocked.orientedRanges.orientation.fuzz = 0;
1569 }
Jeff Browne57e8952010-07-23 21:28:06 -07001570 }
1571
1572 if (orientationChanged || sizeChanged) {
1573 // Compute oriented surface dimensions, precision, and scales.
1574 float orientedXScale, orientedYScale;
Jeff Brownb51719b2010-07-29 18:18:33 -07001575 switch (mLocked.surfaceOrientation) {
Jeff Browne57e8952010-07-23 21:28:06 -07001576 case InputReaderPolicyInterface::ROTATION_90:
1577 case InputReaderPolicyInterface::ROTATION_270:
Jeff Brownb51719b2010-07-29 18:18:33 -07001578 mLocked.orientedSurfaceWidth = mLocked.surfaceHeight;
1579 mLocked.orientedSurfaceHeight = mLocked.surfaceWidth;
1580 mLocked.orientedXPrecision = mLocked.yPrecision;
1581 mLocked.orientedYPrecision = mLocked.xPrecision;
1582 orientedXScale = mLocked.yScale;
1583 orientedYScale = mLocked.xScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001584 break;
1585 default:
Jeff Brownb51719b2010-07-29 18:18:33 -07001586 mLocked.orientedSurfaceWidth = mLocked.surfaceWidth;
1587 mLocked.orientedSurfaceHeight = mLocked.surfaceHeight;
1588 mLocked.orientedXPrecision = mLocked.xPrecision;
1589 mLocked.orientedYPrecision = mLocked.yPrecision;
1590 orientedXScale = mLocked.xScale;
1591 orientedYScale = mLocked.yScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001592 break;
1593 }
1594
1595 // Configure position ranges.
Jeff Brownb51719b2010-07-29 18:18:33 -07001596 mLocked.orientedRanges.x.min = 0;
1597 mLocked.orientedRanges.x.max = mLocked.orientedSurfaceWidth;
1598 mLocked.orientedRanges.x.flat = 0;
1599 mLocked.orientedRanges.x.fuzz = orientedXScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001600
Jeff Brownb51719b2010-07-29 18:18:33 -07001601 mLocked.orientedRanges.y.min = 0;
1602 mLocked.orientedRanges.y.max = mLocked.orientedSurfaceHeight;
1603 mLocked.orientedRanges.y.flat = 0;
1604 mLocked.orientedRanges.y.fuzz = orientedYScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001605 }
1606
1607 return true;
1608}
1609
Jeff Brown26c94ff2010-09-30 14:33:04 -07001610void TouchInputMapper::dumpSurfaceLocked(String8& dump) {
1611 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth);
1612 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight);
1613 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation);
Jeff Browna665ca82010-09-08 11:49:43 -07001614}
1615
Jeff Brownb51719b2010-07-29 18:18:33 -07001616void TouchInputMapper::configureVirtualKeysLocked() {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001617 assert(mRawAxes.x.valid && mRawAxes.y.valid);
Jeff Browne57e8952010-07-23 21:28:06 -07001618
Jeff Brownb51719b2010-07-29 18:18:33 -07001619 // Note: getVirtualKeyDefinitions is non-reentrant so we can continue holding the lock.
Jeff Brown38a7fab2010-08-30 03:02:23 -07001620 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Browne57e8952010-07-23 21:28:06 -07001621 getPolicy()->getVirtualKeyDefinitions(getDeviceName(), virtualKeyDefinitions);
1622
Jeff Brownb51719b2010-07-29 18:18:33 -07001623 mLocked.virtualKeys.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07001624
Jeff Brownb51719b2010-07-29 18:18:33 -07001625 if (virtualKeyDefinitions.size() == 0) {
1626 return;
1627 }
Jeff Browne57e8952010-07-23 21:28:06 -07001628
Jeff Brownb51719b2010-07-29 18:18:33 -07001629 mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size());
1630
Jeff Brown38a7fab2010-08-30 03:02:23 -07001631 int32_t touchScreenLeft = mRawAxes.x.minValue;
1632 int32_t touchScreenTop = mRawAxes.y.minValue;
1633 int32_t touchScreenWidth = mRawAxes.x.getRange();
1634 int32_t touchScreenHeight = mRawAxes.y.getRange();
Jeff Brownb51719b2010-07-29 18:18:33 -07001635
1636 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001637 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brownb51719b2010-07-29 18:18:33 -07001638 virtualKeyDefinitions[i];
1639
1640 mLocked.virtualKeys.add();
1641 VirtualKey& virtualKey = mLocked.virtualKeys.editTop();
1642
1643 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1644 int32_t keyCode;
1645 uint32_t flags;
1646 if (getEventHub()->scancodeToKeycode(getDeviceId(), virtualKey.scanCode,
1647 & keyCode, & flags)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001648 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
1649 virtualKey.scanCode);
Jeff Brownb51719b2010-07-29 18:18:33 -07001650 mLocked.virtualKeys.pop(); // drop the key
1651 continue;
Jeff Browne57e8952010-07-23 21:28:06 -07001652 }
1653
Jeff Brownb51719b2010-07-29 18:18:33 -07001654 virtualKey.keyCode = keyCode;
1655 virtualKey.flags = flags;
Jeff Browne57e8952010-07-23 21:28:06 -07001656
Jeff Brownb51719b2010-07-29 18:18:33 -07001657 // convert the key definition's display coordinates into touch coordinates for a hit box
1658 int32_t halfWidth = virtualKeyDefinition.width / 2;
1659 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Browne57e8952010-07-23 21:28:06 -07001660
Jeff Brownb51719b2010-07-29 18:18:33 -07001661 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
1662 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1663 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
1664 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1665 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
1666 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
1667 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
1668 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
Jeff Browne57e8952010-07-23 21:28:06 -07001669
Jeff Brown26c94ff2010-09-30 14:33:04 -07001670 }
1671}
1672
1673void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) {
1674 if (!mLocked.virtualKeys.isEmpty()) {
1675 dump.append(INDENT3 "Virtual Keys:\n");
1676
1677 for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) {
1678 const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i);
1679 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
1680 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1681 i, virtualKey.scanCode, virtualKey.keyCode,
1682 virtualKey.hitLeft, virtualKey.hitRight,
1683 virtualKey.hitTop, virtualKey.hitBottom);
1684 }
Jeff Brownb51719b2010-07-29 18:18:33 -07001685 }
Jeff Browne57e8952010-07-23 21:28:06 -07001686}
1687
Jeff Brown38a7fab2010-08-30 03:02:23 -07001688void TouchInputMapper::parseCalibration() {
1689 const InputDeviceCalibration& in = getDevice()->getCalibration();
1690 Calibration& out = mCalibration;
1691
Jeff Brown60b57762010-10-18 13:32:20 -07001692 // Position
1693 out.haveXOrigin = in.tryGetProperty(String8("touch.position.xOrigin"), out.xOrigin);
1694 out.haveYOrigin = in.tryGetProperty(String8("touch.position.yOrigin"), out.yOrigin);
1695 out.haveXScale = in.tryGetProperty(String8("touch.position.xScale"), out.xScale);
1696 out.haveYScale = in.tryGetProperty(String8("touch.position.yScale"), out.yScale);
1697
Jeff Brown6b337e72010-10-14 21:42:15 -07001698 // Touch Size
1699 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT;
1700 String8 touchSizeCalibrationString;
1701 if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) {
1702 if (touchSizeCalibrationString == "none") {
1703 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
1704 } else if (touchSizeCalibrationString == "geometric") {
1705 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC;
1706 } else if (touchSizeCalibrationString == "pressure") {
1707 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
1708 } else if (touchSizeCalibrationString != "default") {
1709 LOGW("Invalid value for touch.touchSize.calibration: '%s'",
1710 touchSizeCalibrationString.string());
Jeff Brown38a7fab2010-08-30 03:02:23 -07001711 }
1712 }
1713
Jeff Brown6b337e72010-10-14 21:42:15 -07001714 // Tool Size
1715 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT;
1716 String8 toolSizeCalibrationString;
1717 if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) {
1718 if (toolSizeCalibrationString == "none") {
1719 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
1720 } else if (toolSizeCalibrationString == "geometric") {
1721 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC;
1722 } else if (toolSizeCalibrationString == "linear") {
1723 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
1724 } else if (toolSizeCalibrationString == "area") {
1725 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA;
1726 } else if (toolSizeCalibrationString != "default") {
1727 LOGW("Invalid value for touch.toolSize.calibration: '%s'",
1728 toolSizeCalibrationString.string());
Jeff Brown38a7fab2010-08-30 03:02:23 -07001729 }
1730 }
1731
Jeff Brown6b337e72010-10-14 21:42:15 -07001732 out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"),
1733 out.toolSizeLinearScale);
1734 out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"),
1735 out.toolSizeLinearBias);
1736 out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"),
1737 out.toolSizeAreaScale);
1738 out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"),
1739 out.toolSizeAreaBias);
1740 out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"),
1741 out.toolSizeIsSummed);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001742
1743 // Pressure
1744 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
1745 String8 pressureCalibrationString;
Jeff Brown6b337e72010-10-14 21:42:15 -07001746 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001747 if (pressureCalibrationString == "none") {
1748 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
1749 } else if (pressureCalibrationString == "physical") {
1750 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
1751 } else if (pressureCalibrationString == "amplitude") {
1752 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
1753 } else if (pressureCalibrationString != "default") {
Jeff Brown6b337e72010-10-14 21:42:15 -07001754 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown38a7fab2010-08-30 03:02:23 -07001755 pressureCalibrationString.string());
1756 }
1757 }
1758
1759 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
1760 String8 pressureSourceString;
1761 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
1762 if (pressureSourceString == "pressure") {
1763 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
1764 } else if (pressureSourceString == "touch") {
1765 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
1766 } else if (pressureSourceString != "default") {
1767 LOGW("Invalid value for touch.pressure.source: '%s'",
1768 pressureSourceString.string());
1769 }
1770 }
1771
1772 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
1773 out.pressureScale);
1774
1775 // Size
1776 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
1777 String8 sizeCalibrationString;
Jeff Brown6b337e72010-10-14 21:42:15 -07001778 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001779 if (sizeCalibrationString == "none") {
1780 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
1781 } else if (sizeCalibrationString == "normalized") {
1782 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
1783 } else if (sizeCalibrationString != "default") {
Jeff Brown6b337e72010-10-14 21:42:15 -07001784 LOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Brown38a7fab2010-08-30 03:02:23 -07001785 sizeCalibrationString.string());
1786 }
1787 }
1788
1789 // Orientation
1790 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
1791 String8 orientationCalibrationString;
Jeff Brown6b337e72010-10-14 21:42:15 -07001792 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001793 if (orientationCalibrationString == "none") {
1794 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
1795 } else if (orientationCalibrationString == "interpolated") {
1796 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
1797 } else if (orientationCalibrationString != "default") {
Jeff Brown6b337e72010-10-14 21:42:15 -07001798 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown38a7fab2010-08-30 03:02:23 -07001799 orientationCalibrationString.string());
1800 }
1801 }
1802}
1803
1804void TouchInputMapper::resolveCalibration() {
1805 // Pressure
1806 switch (mCalibration.pressureSource) {
1807 case Calibration::PRESSURE_SOURCE_DEFAULT:
1808 if (mRawAxes.pressure.valid) {
1809 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
1810 } else if (mRawAxes.touchMajor.valid) {
1811 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
1812 }
1813 break;
1814
1815 case Calibration::PRESSURE_SOURCE_PRESSURE:
1816 if (! mRawAxes.pressure.valid) {
1817 LOGW("Calibration property touch.pressure.source is 'pressure' but "
1818 "the pressure axis is not available.");
1819 }
1820 break;
1821
1822 case Calibration::PRESSURE_SOURCE_TOUCH:
1823 if (! mRawAxes.touchMajor.valid) {
1824 LOGW("Calibration property touch.pressure.source is 'touch' but "
1825 "the touchMajor axis is not available.");
1826 }
1827 break;
1828
1829 default:
1830 break;
1831 }
1832
1833 switch (mCalibration.pressureCalibration) {
1834 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
1835 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
1836 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
1837 } else {
1838 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
1839 }
1840 break;
1841
1842 default:
1843 break;
1844 }
1845
Jeff Brown6b337e72010-10-14 21:42:15 -07001846 // Tool Size
1847 switch (mCalibration.toolSizeCalibration) {
1848 case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT:
Jeff Brown38a7fab2010-08-30 03:02:23 -07001849 if (mRawAxes.toolMajor.valid) {
Jeff Brown6b337e72010-10-14 21:42:15 -07001850 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001851 } else {
Jeff Brown6b337e72010-10-14 21:42:15 -07001852 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001853 }
1854 break;
1855
1856 default:
1857 break;
1858 }
1859
Jeff Brown6b337e72010-10-14 21:42:15 -07001860 // Touch Size
1861 switch (mCalibration.touchSizeCalibration) {
1862 case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT:
Jeff Brown38a7fab2010-08-30 03:02:23 -07001863 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
Jeff Brown6b337e72010-10-14 21:42:15 -07001864 && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
1865 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001866 } else {
Jeff Brown6b337e72010-10-14 21:42:15 -07001867 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001868 }
1869 break;
1870
1871 default:
1872 break;
1873 }
1874
1875 // Size
1876 switch (mCalibration.sizeCalibration) {
1877 case Calibration::SIZE_CALIBRATION_DEFAULT:
1878 if (mRawAxes.toolMajor.valid) {
1879 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
1880 } else {
1881 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
1882 }
1883 break;
1884
1885 default:
1886 break;
1887 }
1888
1889 // Orientation
1890 switch (mCalibration.orientationCalibration) {
1891 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
1892 if (mRawAxes.orientation.valid) {
1893 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
1894 } else {
1895 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
1896 }
1897 break;
1898
1899 default:
1900 break;
1901 }
1902}
1903
Jeff Brown26c94ff2010-09-30 14:33:04 -07001904void TouchInputMapper::dumpCalibration(String8& dump) {
1905 dump.append(INDENT3 "Calibration:\n");
Jeff Browna665ca82010-09-08 11:49:43 -07001906
Jeff Brown60b57762010-10-18 13:32:20 -07001907 // Position
1908 if (mCalibration.haveXOrigin) {
1909 dump.appendFormat(INDENT4 "touch.position.xOrigin: %d\n", mCalibration.xOrigin);
1910 }
1911 if (mCalibration.haveYOrigin) {
1912 dump.appendFormat(INDENT4 "touch.position.yOrigin: %d\n", mCalibration.yOrigin);
1913 }
1914 if (mCalibration.haveXScale) {
1915 dump.appendFormat(INDENT4 "touch.position.xScale: %0.3f\n", mCalibration.xScale);
1916 }
1917 if (mCalibration.haveYScale) {
1918 dump.appendFormat(INDENT4 "touch.position.yScale: %0.3f\n", mCalibration.yScale);
1919 }
1920
Jeff Brown6b337e72010-10-14 21:42:15 -07001921 // Touch Size
1922 switch (mCalibration.touchSizeCalibration) {
1923 case Calibration::TOUCH_SIZE_CALIBRATION_NONE:
1924 dump.append(INDENT4 "touch.touchSize.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001925 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001926 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
1927 dump.append(INDENT4 "touch.touchSize.calibration: geometric\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001928 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001929 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
1930 dump.append(INDENT4 "touch.touchSize.calibration: pressure\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001931 break;
1932 default:
1933 assert(false);
1934 }
1935
Jeff Brown6b337e72010-10-14 21:42:15 -07001936 // Tool Size
1937 switch (mCalibration.toolSizeCalibration) {
1938 case Calibration::TOOL_SIZE_CALIBRATION_NONE:
1939 dump.append(INDENT4 "touch.toolSize.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001940 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001941 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
1942 dump.append(INDENT4 "touch.toolSize.calibration: geometric\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001943 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001944 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
1945 dump.append(INDENT4 "touch.toolSize.calibration: linear\n");
1946 break;
1947 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
1948 dump.append(INDENT4 "touch.toolSize.calibration: area\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001949 break;
1950 default:
1951 assert(false);
1952 }
1953
Jeff Brown6b337e72010-10-14 21:42:15 -07001954 if (mCalibration.haveToolSizeLinearScale) {
1955 dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n",
1956 mCalibration.toolSizeLinearScale);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001957 }
1958
Jeff Brown6b337e72010-10-14 21:42:15 -07001959 if (mCalibration.haveToolSizeLinearBias) {
1960 dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n",
1961 mCalibration.toolSizeLinearBias);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001962 }
1963
Jeff Brown6b337e72010-10-14 21:42:15 -07001964 if (mCalibration.haveToolSizeAreaScale) {
1965 dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n",
1966 mCalibration.toolSizeAreaScale);
1967 }
1968
1969 if (mCalibration.haveToolSizeAreaBias) {
1970 dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n",
1971 mCalibration.toolSizeAreaBias);
1972 }
1973
1974 if (mCalibration.haveToolSizeIsSummed) {
1975 dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %d\n",
1976 mCalibration.toolSizeIsSummed);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001977 }
1978
1979 // Pressure
1980 switch (mCalibration.pressureCalibration) {
1981 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07001982 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001983 break;
1984 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brown26c94ff2010-09-30 14:33:04 -07001985 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001986 break;
1987 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07001988 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001989 break;
1990 default:
1991 assert(false);
1992 }
1993
1994 switch (mCalibration.pressureSource) {
1995 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07001996 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001997 break;
1998 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brown26c94ff2010-09-30 14:33:04 -07001999 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002000 break;
2001 case Calibration::PRESSURE_SOURCE_DEFAULT:
2002 break;
2003 default:
2004 assert(false);
2005 }
2006
2007 if (mCalibration.havePressureScale) {
Jeff Brown26c94ff2010-09-30 14:33:04 -07002008 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
2009 mCalibration.pressureScale);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002010 }
2011
2012 // Size
2013 switch (mCalibration.sizeCalibration) {
2014 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002015 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002016 break;
2017 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002018 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002019 break;
2020 default:
2021 assert(false);
2022 }
2023
2024 // Orientation
2025 switch (mCalibration.orientationCalibration) {
2026 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002027 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002028 break;
2029 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002030 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002031 break;
2032 default:
2033 assert(false);
2034 }
2035}
2036
Jeff Browne57e8952010-07-23 21:28:06 -07002037void TouchInputMapper::reset() {
2038 // Synthesize touch up event if touch is currently down.
2039 // This will also take care of finishing virtual key processing if needed.
2040 if (mLastTouch.pointerCount != 0) {
2041 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
2042 mCurrentTouch.clear();
2043 syncTouch(when, true);
2044 }
2045
Jeff Brownb51719b2010-07-29 18:18:33 -07002046 { // acquire lock
2047 AutoMutex _l(mLock);
2048 initializeLocked();
2049 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07002050
Jeff Brownb51719b2010-07-29 18:18:33 -07002051 InputMapper::reset();
Jeff Browne57e8952010-07-23 21:28:06 -07002052}
2053
2054void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Browne839a582010-04-22 18:58:52 -07002055 uint32_t policyFlags = 0;
Jeff Browne839a582010-04-22 18:58:52 -07002056
Jeff Brownb51719b2010-07-29 18:18:33 -07002057 // Preprocess pointer data.
Jeff Browne839a582010-04-22 18:58:52 -07002058
Jeff Browne57e8952010-07-23 21:28:06 -07002059 if (mParameters.useBadTouchFilter) {
2060 if (applyBadTouchFilter()) {
Jeff Browne839a582010-04-22 18:58:52 -07002061 havePointerIds = false;
2062 }
2063 }
2064
Jeff Browne57e8952010-07-23 21:28:06 -07002065 if (mParameters.useJumpyTouchFilter) {
2066 if (applyJumpyTouchFilter()) {
Jeff Browne839a582010-04-22 18:58:52 -07002067 havePointerIds = false;
2068 }
2069 }
2070
2071 if (! havePointerIds) {
Jeff Browne57e8952010-07-23 21:28:06 -07002072 calculatePointerIds();
Jeff Browne839a582010-04-22 18:58:52 -07002073 }
2074
Jeff Browne57e8952010-07-23 21:28:06 -07002075 TouchData temp;
2076 TouchData* savedTouch;
2077 if (mParameters.useAveragingTouchFilter) {
2078 temp.copyFrom(mCurrentTouch);
Jeff Browne839a582010-04-22 18:58:52 -07002079 savedTouch = & temp;
2080
Jeff Browne57e8952010-07-23 21:28:06 -07002081 applyAveragingTouchFilter();
Jeff Browne839a582010-04-22 18:58:52 -07002082 } else {
Jeff Browne57e8952010-07-23 21:28:06 -07002083 savedTouch = & mCurrentTouch;
Jeff Browne839a582010-04-22 18:58:52 -07002084 }
2085
Jeff Brownb51719b2010-07-29 18:18:33 -07002086 // Process touches and virtual keys.
Jeff Browne839a582010-04-22 18:58:52 -07002087
Jeff Browne57e8952010-07-23 21:28:06 -07002088 TouchResult touchResult = consumeOffScreenTouches(when, policyFlags);
2089 if (touchResult == DISPATCH_TOUCH) {
2090 dispatchTouches(when, policyFlags);
Jeff Browne839a582010-04-22 18:58:52 -07002091 }
2092
Jeff Brownb51719b2010-07-29 18:18:33 -07002093 // Copy current touch to last touch in preparation for the next cycle.
Jeff Browne839a582010-04-22 18:58:52 -07002094
Jeff Browne57e8952010-07-23 21:28:06 -07002095 if (touchResult == DROP_STROKE) {
2096 mLastTouch.clear();
2097 } else {
2098 mLastTouch.copyFrom(*savedTouch);
Jeff Browne839a582010-04-22 18:58:52 -07002099 }
Jeff Browne839a582010-04-22 18:58:52 -07002100}
2101
Jeff Browne57e8952010-07-23 21:28:06 -07002102TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches(
2103 nsecs_t when, uint32_t policyFlags) {
2104 int32_t keyEventAction, keyEventFlags;
2105 int32_t keyCode, scanCode, downTime;
2106 TouchResult touchResult;
Jeff Brown50de30a2010-06-22 01:27:15 -07002107
Jeff Brownb51719b2010-07-29 18:18:33 -07002108 { // acquire lock
2109 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07002110
Jeff Brownb51719b2010-07-29 18:18:33 -07002111 // Update surface size and orientation, including virtual key positions.
2112 if (! configureSurfaceLocked()) {
2113 return DROP_STROKE;
2114 }
2115
2116 // Check for virtual key press.
2117 if (mLocked.currentVirtualKey.down) {
Jeff Browne57e8952010-07-23 21:28:06 -07002118 if (mCurrentTouch.pointerCount == 0) {
2119 // Pointer went up while virtual key was down.
Jeff Brownb51719b2010-07-29 18:18:33 -07002120 mLocked.currentVirtualKey.down = false;
Jeff Browne57e8952010-07-23 21:28:06 -07002121#if DEBUG_VIRTUAL_KEYS
2122 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brown3c3cc622010-10-20 15:33:38 -07002123 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Browne57e8952010-07-23 21:28:06 -07002124#endif
2125 keyEventAction = AKEY_EVENT_ACTION_UP;
2126 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2127 touchResult = SKIP_TOUCH;
2128 goto DispatchVirtualKey;
2129 }
2130
2131 if (mCurrentTouch.pointerCount == 1) {
2132 int32_t x = mCurrentTouch.pointers[0].x;
2133 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brownb51719b2010-07-29 18:18:33 -07002134 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
2135 if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07002136 // Pointer is still within the space of the virtual key.
2137 return SKIP_TOUCH;
2138 }
2139 }
2140
2141 // Pointer left virtual key area or another pointer also went down.
2142 // Send key cancellation and drop the stroke so subsequent motions will be
2143 // considered fresh downs. This is useful when the user swipes away from the
2144 // virtual key area into the main display surface.
Jeff Brownb51719b2010-07-29 18:18:33 -07002145 mLocked.currentVirtualKey.down = false;
Jeff Browne57e8952010-07-23 21:28:06 -07002146#if DEBUG_VIRTUAL_KEYS
2147 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brown3c3cc622010-10-20 15:33:38 -07002148 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Browne57e8952010-07-23 21:28:06 -07002149#endif
2150 keyEventAction = AKEY_EVENT_ACTION_UP;
2151 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
2152 | AKEY_EVENT_FLAG_CANCELED;
Jeff Brown3c3cc622010-10-20 15:33:38 -07002153
2154 // Check whether the pointer moved inside the display area where we should
2155 // start a new stroke.
2156 int32_t x = mCurrentTouch.pointers[0].x;
2157 int32_t y = mCurrentTouch.pointers[0].y;
2158 if (isPointInsideSurfaceLocked(x, y)) {
2159 mLastTouch.clear();
2160 touchResult = DISPATCH_TOUCH;
2161 } else {
2162 touchResult = DROP_STROKE;
2163 }
Jeff Browne57e8952010-07-23 21:28:06 -07002164 } else {
2165 if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) {
2166 // Pointer just went down. Handle off-screen touches, if needed.
2167 int32_t x = mCurrentTouch.pointers[0].x;
2168 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brownb51719b2010-07-29 18:18:33 -07002169 if (! isPointInsideSurfaceLocked(x, y)) {
Jeff Browne57e8952010-07-23 21:28:06 -07002170 // If exactly one pointer went down, check for virtual key hit.
2171 // Otherwise we will drop the entire stroke.
2172 if (mCurrentTouch.pointerCount == 1) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002173 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
Jeff Browne57e8952010-07-23 21:28:06 -07002174 if (virtualKey) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002175 mLocked.currentVirtualKey.down = true;
2176 mLocked.currentVirtualKey.downTime = when;
2177 mLocked.currentVirtualKey.keyCode = virtualKey->keyCode;
2178 mLocked.currentVirtualKey.scanCode = virtualKey->scanCode;
Jeff Browne57e8952010-07-23 21:28:06 -07002179#if DEBUG_VIRTUAL_KEYS
2180 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brown3c3cc622010-10-20 15:33:38 -07002181 mLocked.currentVirtualKey.keyCode,
2182 mLocked.currentVirtualKey.scanCode);
Jeff Browne57e8952010-07-23 21:28:06 -07002183#endif
2184 keyEventAction = AKEY_EVENT_ACTION_DOWN;
2185 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM
2186 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2187 touchResult = SKIP_TOUCH;
2188 goto DispatchVirtualKey;
2189 }
2190 }
2191 return DROP_STROKE;
2192 }
2193 }
2194 return DISPATCH_TOUCH;
2195 }
2196
2197 DispatchVirtualKey:
2198 // Collect remaining state needed to dispatch virtual key.
Jeff Brownb51719b2010-07-29 18:18:33 -07002199 keyCode = mLocked.currentVirtualKey.keyCode;
2200 scanCode = mLocked.currentVirtualKey.scanCode;
2201 downTime = mLocked.currentVirtualKey.downTime;
2202 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07002203
2204 // Dispatch virtual key.
2205 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown956c0fb2010-10-01 14:55:30 -07002206 policyFlags |= POLICY_FLAG_VIRTUAL;
Jeff Brown90f0cee2010-10-08 22:31:17 -07002207 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
2208 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
2209 return touchResult;
Jeff Browne839a582010-04-22 18:58:52 -07002210}
2211
Jeff Browne57e8952010-07-23 21:28:06 -07002212void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
2213 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2214 uint32_t lastPointerCount = mLastTouch.pointerCount;
Jeff Browne839a582010-04-22 18:58:52 -07002215 if (currentPointerCount == 0 && lastPointerCount == 0) {
2216 return; // nothing to do!
2217 }
2218
Jeff Browne57e8952010-07-23 21:28:06 -07002219 BitSet32 currentIdBits = mCurrentTouch.idBits;
2220 BitSet32 lastIdBits = mLastTouch.idBits;
Jeff Browne839a582010-04-22 18:58:52 -07002221
2222 if (currentIdBits == lastIdBits) {
2223 // No pointer id changes so this is a move event.
2224 // The dispatcher takes care of batching moves so we don't have to deal with that here.
Jeff Brown5c1ed842010-07-14 18:48:53 -07002225 int32_t motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browne57e8952010-07-23 21:28:06 -07002226 dispatchTouch(when, policyFlags, & mCurrentTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002227 currentIdBits, -1, currentPointerCount, motionEventAction);
Jeff Browne839a582010-04-22 18:58:52 -07002228 } else {
Jeff Brown3c3cc622010-10-20 15:33:38 -07002229 // There may be pointers going up and pointers going down and pointers moving
2230 // all at the same time.
Jeff Browne839a582010-04-22 18:58:52 -07002231 BitSet32 upIdBits(lastIdBits.value & ~ currentIdBits.value);
2232 BitSet32 downIdBits(currentIdBits.value & ~ lastIdBits.value);
2233 BitSet32 activeIdBits(lastIdBits.value);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002234 uint32_t pointerCount = lastPointerCount;
Jeff Browne839a582010-04-22 18:58:52 -07002235
Jeff Brown3c3cc622010-10-20 15:33:38 -07002236 // Produce an intermediate representation of the touch data that consists of the
2237 // old location of pointers that have just gone up and the new location of pointers that
2238 // have just moved but omits the location of pointers that have just gone down.
2239 TouchData interimTouch;
2240 interimTouch.copyFrom(mLastTouch);
2241
2242 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
2243 bool moveNeeded = false;
2244 while (!moveIdBits.isEmpty()) {
2245 uint32_t moveId = moveIdBits.firstMarkedBit();
2246 moveIdBits.clearBit(moveId);
2247
2248 int32_t oldIndex = mLastTouch.idToIndex[moveId];
2249 int32_t newIndex = mCurrentTouch.idToIndex[moveId];
2250 if (mLastTouch.pointers[oldIndex] != mCurrentTouch.pointers[newIndex]) {
2251 interimTouch.pointers[oldIndex] = mCurrentTouch.pointers[newIndex];
2252 moveNeeded = true;
2253 }
2254 }
2255
2256 // Dispatch pointer up events using the interim pointer locations.
2257 while (!upIdBits.isEmpty()) {
Jeff Browne839a582010-04-22 18:58:52 -07002258 uint32_t upId = upIdBits.firstMarkedBit();
2259 upIdBits.clearBit(upId);
2260 BitSet32 oldActiveIdBits = activeIdBits;
2261 activeIdBits.clearBit(upId);
2262
2263 int32_t motionEventAction;
2264 if (activeIdBits.isEmpty()) {
Jeff Brown5c1ed842010-07-14 18:48:53 -07002265 motionEventAction = AMOTION_EVENT_ACTION_UP;
Jeff Browne839a582010-04-22 18:58:52 -07002266 } else {
Jeff Brown3cf1c9b2010-07-16 15:01:56 -07002267 motionEventAction = AMOTION_EVENT_ACTION_POINTER_UP;
Jeff Browne839a582010-04-22 18:58:52 -07002268 }
2269
Jeff Brown3c3cc622010-10-20 15:33:38 -07002270 dispatchTouch(when, policyFlags, &interimTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002271 oldActiveIdBits, upId, pointerCount, motionEventAction);
2272 pointerCount -= 1;
Jeff Browne839a582010-04-22 18:58:52 -07002273 }
2274
Jeff Brown3c3cc622010-10-20 15:33:38 -07002275 // Dispatch move events if any of the remaining pointers moved from their old locations.
2276 // Although applications receive new locations as part of individual pointer up
2277 // events, they do not generally handle them except when presented in a move event.
2278 if (moveNeeded) {
2279 dispatchTouch(when, policyFlags, &mCurrentTouch,
2280 activeIdBits, -1, pointerCount, AMOTION_EVENT_ACTION_MOVE);
2281 }
2282
2283 // Dispatch pointer down events using the new pointer locations.
2284 while (!downIdBits.isEmpty()) {
Jeff Browne839a582010-04-22 18:58:52 -07002285 uint32_t downId = downIdBits.firstMarkedBit();
2286 downIdBits.clearBit(downId);
2287 BitSet32 oldActiveIdBits = activeIdBits;
2288 activeIdBits.markBit(downId);
2289
2290 int32_t motionEventAction;
2291 if (oldActiveIdBits.isEmpty()) {
Jeff Brown5c1ed842010-07-14 18:48:53 -07002292 motionEventAction = AMOTION_EVENT_ACTION_DOWN;
Jeff Browne57e8952010-07-23 21:28:06 -07002293 mDownTime = when;
Jeff Browne839a582010-04-22 18:58:52 -07002294 } else {
Jeff Brown3cf1c9b2010-07-16 15:01:56 -07002295 motionEventAction = AMOTION_EVENT_ACTION_POINTER_DOWN;
Jeff Browne839a582010-04-22 18:58:52 -07002296 }
2297
Jeff Brown38a7fab2010-08-30 03:02:23 -07002298 pointerCount += 1;
Jeff Brown3c3cc622010-10-20 15:33:38 -07002299 dispatchTouch(when, policyFlags, &mCurrentTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002300 activeIdBits, downId, pointerCount, motionEventAction);
Jeff Browne839a582010-04-22 18:58:52 -07002301 }
2302 }
2303}
2304
Jeff Browne57e8952010-07-23 21:28:06 -07002305void TouchInputMapper::dispatchTouch(nsecs_t when, uint32_t policyFlags,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002306 TouchData* touch, BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
Jeff Browne839a582010-04-22 18:58:52 -07002307 int32_t motionEventAction) {
Jeff Browne839a582010-04-22 18:58:52 -07002308 int32_t pointerIds[MAX_POINTERS];
2309 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Browne839a582010-04-22 18:58:52 -07002310 int32_t motionEventEdgeFlags = 0;
Jeff Brownb51719b2010-07-29 18:18:33 -07002311 float xPrecision, yPrecision;
2312
2313 { // acquire lock
2314 AutoMutex _l(mLock);
2315
2316 // Walk through the the active pointers and map touch screen coordinates (TouchData) into
2317 // display coordinates (PointerCoords) and adjust for display orientation.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002318 for (uint32_t outIndex = 0; ! idBits.isEmpty(); outIndex++) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002319 uint32_t id = idBits.firstMarkedBit();
2320 idBits.clearBit(id);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002321 uint32_t inIndex = touch->idToIndex[id];
Jeff Brownb51719b2010-07-29 18:18:33 -07002322
Jeff Brown38a7fab2010-08-30 03:02:23 -07002323 const PointerData& in = touch->pointers[inIndex];
Jeff Brownb51719b2010-07-29 18:18:33 -07002324
Jeff Brown38a7fab2010-08-30 03:02:23 -07002325 // X and Y
2326 float x = float(in.x - mLocked.xOrigin) * mLocked.xScale;
2327 float y = float(in.y - mLocked.yOrigin) * mLocked.yScale;
Jeff Brownb51719b2010-07-29 18:18:33 -07002328
Jeff Brown38a7fab2010-08-30 03:02:23 -07002329 // ToolMajor and ToolMinor
2330 float toolMajor, toolMinor;
Jeff Brown6b337e72010-10-14 21:42:15 -07002331 switch (mCalibration.toolSizeCalibration) {
2332 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002333 toolMajor = in.toolMajor * mLocked.geometricScale;
2334 if (mRawAxes.toolMinor.valid) {
2335 toolMinor = in.toolMinor * mLocked.geometricScale;
2336 } else {
2337 toolMinor = toolMajor;
2338 }
2339 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002340 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002341 toolMajor = in.toolMajor != 0
Jeff Brown6b337e72010-10-14 21:42:15 -07002342 ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias
Jeff Brown38a7fab2010-08-30 03:02:23 -07002343 : 0;
2344 if (mRawAxes.toolMinor.valid) {
2345 toolMinor = in.toolMinor != 0
Jeff Brown6b337e72010-10-14 21:42:15 -07002346 ? in.toolMinor * mLocked.toolSizeLinearScale
2347 + mLocked.toolSizeLinearBias
Jeff Brown38a7fab2010-08-30 03:02:23 -07002348 : 0;
2349 } else {
2350 toolMinor = toolMajor;
2351 }
2352 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002353 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2354 if (in.toolMajor != 0) {
2355 float diameter = sqrtf(in.toolMajor
2356 * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias);
2357 toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias;
2358 } else {
2359 toolMajor = 0;
2360 }
2361 toolMinor = toolMajor;
2362 break;
Jeff Brown38a7fab2010-08-30 03:02:23 -07002363 default:
2364 toolMajor = 0;
2365 toolMinor = 0;
2366 break;
Jeff Brownb51719b2010-07-29 18:18:33 -07002367 }
2368
Jeff Brown6b337e72010-10-14 21:42:15 -07002369 if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002370 toolMajor /= pointerCount;
2371 toolMinor /= pointerCount;
2372 }
2373
2374 // Pressure
2375 float rawPressure;
2376 switch (mCalibration.pressureSource) {
2377 case Calibration::PRESSURE_SOURCE_PRESSURE:
2378 rawPressure = in.pressure;
2379 break;
2380 case Calibration::PRESSURE_SOURCE_TOUCH:
2381 rawPressure = in.touchMajor;
2382 break;
2383 default:
2384 rawPressure = 0;
2385 }
2386
2387 float pressure;
2388 switch (mCalibration.pressureCalibration) {
2389 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
2390 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
2391 pressure = rawPressure * mLocked.pressureScale;
2392 break;
2393 default:
2394 pressure = 1;
2395 break;
2396 }
2397
2398 // TouchMajor and TouchMinor
2399 float touchMajor, touchMinor;
Jeff Brown6b337e72010-10-14 21:42:15 -07002400 switch (mCalibration.touchSizeCalibration) {
2401 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002402 touchMajor = in.touchMajor * mLocked.geometricScale;
2403 if (mRawAxes.touchMinor.valid) {
2404 touchMinor = in.touchMinor * mLocked.geometricScale;
2405 } else {
2406 touchMinor = touchMajor;
2407 }
2408 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002409 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002410 touchMajor = toolMajor * pressure;
2411 touchMinor = toolMinor * pressure;
2412 break;
2413 default:
2414 touchMajor = 0;
2415 touchMinor = 0;
2416 break;
2417 }
2418
2419 if (touchMajor > toolMajor) {
2420 touchMajor = toolMajor;
2421 }
2422 if (touchMinor > toolMinor) {
2423 touchMinor = toolMinor;
2424 }
2425
2426 // Size
2427 float size;
2428 switch (mCalibration.sizeCalibration) {
2429 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
2430 float rawSize = mRawAxes.toolMinor.valid
2431 ? avg(in.toolMajor, in.toolMinor)
2432 : in.toolMajor;
2433 size = rawSize * mLocked.sizeScale;
2434 break;
2435 }
2436 default:
2437 size = 0;
2438 break;
2439 }
2440
2441 // Orientation
2442 float orientation;
2443 switch (mCalibration.orientationCalibration) {
2444 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
2445 orientation = in.orientation * mLocked.orientationScale;
2446 break;
2447 default:
2448 orientation = 0;
2449 }
2450
2451 // Adjust coords for orientation.
Jeff Brownb51719b2010-07-29 18:18:33 -07002452 switch (mLocked.surfaceOrientation) {
2453 case InputReaderPolicyInterface::ROTATION_90: {
2454 float xTemp = x;
2455 x = y;
2456 y = mLocked.surfaceWidth - xTemp;
2457 orientation -= M_PI_2;
2458 if (orientation < - M_PI_2) {
2459 orientation += M_PI;
2460 }
2461 break;
2462 }
2463 case InputReaderPolicyInterface::ROTATION_180: {
2464 x = mLocked.surfaceWidth - x;
2465 y = mLocked.surfaceHeight - y;
2466 orientation = - orientation;
2467 break;
2468 }
2469 case InputReaderPolicyInterface::ROTATION_270: {
2470 float xTemp = x;
2471 x = mLocked.surfaceHeight - y;
2472 y = xTemp;
2473 orientation += M_PI_2;
2474 if (orientation > M_PI_2) {
2475 orientation -= M_PI;
2476 }
2477 break;
2478 }
2479 }
2480
Jeff Brown38a7fab2010-08-30 03:02:23 -07002481 // Write output coords.
2482 PointerCoords& out = pointerCoords[outIndex];
2483 out.x = x;
2484 out.y = y;
2485 out.pressure = pressure;
2486 out.size = size;
2487 out.touchMajor = touchMajor;
2488 out.touchMinor = touchMinor;
2489 out.toolMajor = toolMajor;
2490 out.toolMinor = toolMinor;
2491 out.orientation = orientation;
Jeff Brownb51719b2010-07-29 18:18:33 -07002492
Jeff Brown38a7fab2010-08-30 03:02:23 -07002493 pointerIds[outIndex] = int32_t(id);
Jeff Brownb51719b2010-07-29 18:18:33 -07002494
2495 if (id == changedId) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002496 motionEventAction |= outIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
Jeff Brownb51719b2010-07-29 18:18:33 -07002497 }
Jeff Browne839a582010-04-22 18:58:52 -07002498 }
Jeff Brownb51719b2010-07-29 18:18:33 -07002499
2500 // Check edge flags by looking only at the first pointer since the flags are
2501 // global to the event.
2502 if (motionEventAction == AMOTION_EVENT_ACTION_DOWN) {
2503 if (pointerCoords[0].x <= 0) {
2504 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_LEFT;
2505 } else if (pointerCoords[0].x >= mLocked.orientedSurfaceWidth) {
2506 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_RIGHT;
2507 }
2508 if (pointerCoords[0].y <= 0) {
2509 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_TOP;
2510 } else if (pointerCoords[0].y >= mLocked.orientedSurfaceHeight) {
2511 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_BOTTOM;
2512 }
Jeff Browne839a582010-04-22 18:58:52 -07002513 }
Jeff Brownb51719b2010-07-29 18:18:33 -07002514
2515 xPrecision = mLocked.orientedXPrecision;
2516 yPrecision = mLocked.orientedYPrecision;
2517 } // release lock
Jeff Browne839a582010-04-22 18:58:52 -07002518
Jeff Brown77e26fc2010-10-07 13:44:51 -07002519 getDispatcher()->notifyMotion(when, getDeviceId(), getSources(), policyFlags,
Jeff Brownaf30ff62010-09-01 17:01:00 -07002520 motionEventAction, 0, getContext()->getGlobalMetaState(), motionEventEdgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -07002521 pointerCount, pointerIds, pointerCoords,
Jeff Brownb51719b2010-07-29 18:18:33 -07002522 xPrecision, yPrecision, mDownTime);
Jeff Browne839a582010-04-22 18:58:52 -07002523}
2524
Jeff Brownb51719b2010-07-29 18:18:33 -07002525bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002526 if (mRawAxes.x.valid && mRawAxes.y.valid) {
2527 return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue
2528 && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue;
Jeff Browne839a582010-04-22 18:58:52 -07002529 }
Jeff Browne57e8952010-07-23 21:28:06 -07002530 return true;
2531}
2532
Jeff Brownb51719b2010-07-29 18:18:33 -07002533const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked(
2534 int32_t x, int32_t y) {
2535 size_t numVirtualKeys = mLocked.virtualKeys.size();
2536 for (size_t i = 0; i < numVirtualKeys; i++) {
2537 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07002538
2539#if DEBUG_VIRTUAL_KEYS
2540 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
2541 "left=%d, top=%d, right=%d, bottom=%d",
2542 x, y,
2543 virtualKey.keyCode, virtualKey.scanCode,
2544 virtualKey.hitLeft, virtualKey.hitTop,
2545 virtualKey.hitRight, virtualKey.hitBottom);
2546#endif
2547
2548 if (virtualKey.isHit(x, y)) {
2549 return & virtualKey;
2550 }
2551 }
2552
2553 return NULL;
2554}
2555
2556void TouchInputMapper::calculatePointerIds() {
2557 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2558 uint32_t lastPointerCount = mLastTouch.pointerCount;
2559
2560 if (currentPointerCount == 0) {
2561 // No pointers to assign.
2562 mCurrentTouch.idBits.clear();
2563 } else if (lastPointerCount == 0) {
2564 // All pointers are new.
2565 mCurrentTouch.idBits.clear();
2566 for (uint32_t i = 0; i < currentPointerCount; i++) {
2567 mCurrentTouch.pointers[i].id = i;
2568 mCurrentTouch.idToIndex[i] = i;
2569 mCurrentTouch.idBits.markBit(i);
2570 }
2571 } else if (currentPointerCount == 1 && lastPointerCount == 1) {
2572 // Only one pointer and no change in count so it must have the same id as before.
2573 uint32_t id = mLastTouch.pointers[0].id;
2574 mCurrentTouch.pointers[0].id = id;
2575 mCurrentTouch.idToIndex[id] = 0;
2576 mCurrentTouch.idBits.value = BitSet32::valueForBit(id);
2577 } else {
2578 // General case.
2579 // We build a heap of squared euclidean distances between current and last pointers
2580 // associated with the current and last pointer indices. Then, we find the best
2581 // match (by distance) for each current pointer.
2582 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
2583
2584 uint32_t heapSize = 0;
2585 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
2586 currentPointerIndex++) {
2587 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
2588 lastPointerIndex++) {
2589 int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x
2590 - mLastTouch.pointers[lastPointerIndex].x;
2591 int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y
2592 - mLastTouch.pointers[lastPointerIndex].y;
2593
2594 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
2595
2596 // Insert new element into the heap (sift up).
2597 heap[heapSize].currentPointerIndex = currentPointerIndex;
2598 heap[heapSize].lastPointerIndex = lastPointerIndex;
2599 heap[heapSize].distance = distance;
2600 heapSize += 1;
2601 }
2602 }
2603
2604 // Heapify
2605 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
2606 startIndex -= 1;
2607 for (uint32_t parentIndex = startIndex; ;) {
2608 uint32_t childIndex = parentIndex * 2 + 1;
2609 if (childIndex >= heapSize) {
2610 break;
2611 }
2612
2613 if (childIndex + 1 < heapSize
2614 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2615 childIndex += 1;
2616 }
2617
2618 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2619 break;
2620 }
2621
2622 swap(heap[parentIndex], heap[childIndex]);
2623 parentIndex = childIndex;
2624 }
2625 }
2626
2627#if DEBUG_POINTER_ASSIGNMENT
2628 LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize);
2629 for (size_t i = 0; i < heapSize; i++) {
2630 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2631 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2632 heap[i].distance);
2633 }
2634#endif
2635
2636 // Pull matches out by increasing order of distance.
2637 // To avoid reassigning pointers that have already been matched, the loop keeps track
2638 // of which last and current pointers have been matched using the matchedXXXBits variables.
2639 // It also tracks the used pointer id bits.
2640 BitSet32 matchedLastBits(0);
2641 BitSet32 matchedCurrentBits(0);
2642 BitSet32 usedIdBits(0);
2643 bool first = true;
2644 for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) {
2645 for (;;) {
2646 if (first) {
2647 // The first time through the loop, we just consume the root element of
2648 // the heap (the one with smallest distance).
2649 first = false;
2650 } else {
2651 // Previous iterations consumed the root element of the heap.
2652 // Pop root element off of the heap (sift down).
2653 heapSize -= 1;
2654 assert(heapSize > 0);
2655
2656 // Sift down.
2657 heap[0] = heap[heapSize];
2658 for (uint32_t parentIndex = 0; ;) {
2659 uint32_t childIndex = parentIndex * 2 + 1;
2660 if (childIndex >= heapSize) {
2661 break;
2662 }
2663
2664 if (childIndex + 1 < heapSize
2665 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2666 childIndex += 1;
2667 }
2668
2669 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2670 break;
2671 }
2672
2673 swap(heap[parentIndex], heap[childIndex]);
2674 parentIndex = childIndex;
2675 }
2676
2677#if DEBUG_POINTER_ASSIGNMENT
2678 LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize);
2679 for (size_t i = 0; i < heapSize; i++) {
2680 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2681 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2682 heap[i].distance);
2683 }
2684#endif
2685 }
2686
2687 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
2688 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
2689
2690 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
2691 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
2692
2693 matchedCurrentBits.markBit(currentPointerIndex);
2694 matchedLastBits.markBit(lastPointerIndex);
2695
2696 uint32_t id = mLastTouch.pointers[lastPointerIndex].id;
2697 mCurrentTouch.pointers[currentPointerIndex].id = id;
2698 mCurrentTouch.idToIndex[id] = currentPointerIndex;
2699 usedIdBits.markBit(id);
2700
2701#if DEBUG_POINTER_ASSIGNMENT
2702 LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
2703 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
2704#endif
2705 break;
2706 }
2707 }
2708
2709 // Assign fresh ids to new pointers.
2710 if (currentPointerCount > lastPointerCount) {
2711 for (uint32_t i = currentPointerCount - lastPointerCount; ;) {
2712 uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit();
2713 uint32_t id = usedIdBits.firstUnmarkedBit();
2714
2715 mCurrentTouch.pointers[currentPointerIndex].id = id;
2716 mCurrentTouch.idToIndex[id] = currentPointerIndex;
2717 usedIdBits.markBit(id);
2718
2719#if DEBUG_POINTER_ASSIGNMENT
2720 LOGD("calculatePointerIds - assigned: cur=%d, id=%d",
2721 currentPointerIndex, id);
2722#endif
2723
2724 if (--i == 0) break; // done
2725 matchedCurrentBits.markBit(currentPointerIndex);
2726 }
2727 }
2728
2729 // Fix id bits.
2730 mCurrentTouch.idBits = usedIdBits;
2731 }
2732}
2733
2734/* Special hack for devices that have bad screen data: if one of the
2735 * points has moved more than a screen height from the last position,
2736 * then drop it. */
2737bool TouchInputMapper::applyBadTouchFilter() {
2738 // This hack requires valid axis parameters.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002739 if (! mRawAxes.y.valid) {
Jeff Browne57e8952010-07-23 21:28:06 -07002740 return false;
2741 }
2742
2743 uint32_t pointerCount = mCurrentTouch.pointerCount;
2744
2745 // Nothing to do if there are no points.
2746 if (pointerCount == 0) {
2747 return false;
2748 }
2749
2750 // Don't do anything if a finger is going down or up. We run
2751 // here before assigning pointer IDs, so there isn't a good
2752 // way to do per-finger matching.
2753 if (pointerCount != mLastTouch.pointerCount) {
2754 return false;
2755 }
2756
2757 // We consider a single movement across more than a 7/16 of
2758 // the long size of the screen to be bad. This was a magic value
2759 // determined by looking at the maximum distance it is feasible
2760 // to actually move in one sample.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002761 int32_t maxDeltaY = mRawAxes.y.getRange() * 7 / 16;
Jeff Browne57e8952010-07-23 21:28:06 -07002762
2763 // XXX The original code in InputDevice.java included commented out
2764 // code for testing the X axis. Note that when we drop a point
2765 // we don't actually restore the old X either. Strange.
2766 // The old code also tries to track when bad points were previously
2767 // detected but it turns out that due to the placement of a "break"
2768 // at the end of the loop, we never set mDroppedBadPoint to true
2769 // so it is effectively dead code.
2770 // Need to figure out if the old code is busted or just overcomplicated
2771 // but working as intended.
2772
2773 // Look through all new points and see if any are farther than
2774 // acceptable from all previous points.
2775 for (uint32_t i = pointerCount; i-- > 0; ) {
2776 int32_t y = mCurrentTouch.pointers[i].y;
2777 int32_t closestY = INT_MAX;
2778 int32_t closestDeltaY = 0;
2779
2780#if DEBUG_HACKS
2781 LOGD("BadTouchFilter: Looking at next point #%d: y=%d", i, y);
2782#endif
2783
2784 for (uint32_t j = pointerCount; j-- > 0; ) {
2785 int32_t lastY = mLastTouch.pointers[j].y;
2786 int32_t deltaY = abs(y - lastY);
2787
2788#if DEBUG_HACKS
2789 LOGD("BadTouchFilter: Comparing with last point #%d: y=%d deltaY=%d",
2790 j, lastY, deltaY);
2791#endif
2792
2793 if (deltaY < maxDeltaY) {
2794 goto SkipSufficientlyClosePoint;
2795 }
2796 if (deltaY < closestDeltaY) {
2797 closestDeltaY = deltaY;
2798 closestY = lastY;
2799 }
2800 }
2801
2802 // Must not have found a close enough match.
2803#if DEBUG_HACKS
2804 LOGD("BadTouchFilter: Dropping bad point #%d: newY=%d oldY=%d deltaY=%d maxDeltaY=%d",
2805 i, y, closestY, closestDeltaY, maxDeltaY);
2806#endif
2807
2808 mCurrentTouch.pointers[i].y = closestY;
2809 return true; // XXX original code only corrects one point
2810
2811 SkipSufficientlyClosePoint: ;
2812 }
2813
2814 // No change.
2815 return false;
2816}
2817
2818/* Special hack for devices that have bad screen data: drop points where
2819 * the coordinate value for one axis has jumped to the other pointer's location.
2820 */
2821bool TouchInputMapper::applyJumpyTouchFilter() {
2822 // This hack requires valid axis parameters.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002823 if (! mRawAxes.y.valid) {
Jeff Browne57e8952010-07-23 21:28:06 -07002824 return false;
2825 }
2826
2827 uint32_t pointerCount = mCurrentTouch.pointerCount;
2828 if (mLastTouch.pointerCount != pointerCount) {
2829#if DEBUG_HACKS
2830 LOGD("JumpyTouchFilter: Different pointer count %d -> %d",
2831 mLastTouch.pointerCount, pointerCount);
2832 for (uint32_t i = 0; i < pointerCount; i++) {
2833 LOGD(" Pointer %d (%d, %d)", i,
2834 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
2835 }
2836#endif
2837
2838 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_TRANSITION_DROPS) {
2839 if (mLastTouch.pointerCount == 1 && pointerCount == 2) {
2840 // Just drop the first few events going from 1 to 2 pointers.
2841 // They're bad often enough that they're not worth considering.
2842 mCurrentTouch.pointerCount = 1;
2843 mJumpyTouchFilter.jumpyPointsDropped += 1;
2844
2845#if DEBUG_HACKS
2846 LOGD("JumpyTouchFilter: Pointer 2 dropped");
2847#endif
2848 return true;
2849 } else if (mLastTouch.pointerCount == 2 && pointerCount == 1) {
2850 // The event when we go from 2 -> 1 tends to be messed up too
2851 mCurrentTouch.pointerCount = 2;
2852 mCurrentTouch.pointers[0] = mLastTouch.pointers[0];
2853 mCurrentTouch.pointers[1] = mLastTouch.pointers[1];
2854 mJumpyTouchFilter.jumpyPointsDropped += 1;
2855
2856#if DEBUG_HACKS
2857 for (int32_t i = 0; i < 2; i++) {
2858 LOGD("JumpyTouchFilter: Pointer %d replaced (%d, %d)", i,
2859 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
2860 }
2861#endif
2862 return true;
2863 }
2864 }
2865 // Reset jumpy points dropped on other transitions or if limit exceeded.
2866 mJumpyTouchFilter.jumpyPointsDropped = 0;
2867
2868#if DEBUG_HACKS
2869 LOGD("JumpyTouchFilter: Transition - drop limit reset");
2870#endif
2871 return false;
2872 }
2873
2874 // We have the same number of pointers as last time.
2875 // A 'jumpy' point is one where the coordinate value for one axis
2876 // has jumped to the other pointer's location. No need to do anything
2877 // else if we only have one pointer.
2878 if (pointerCount < 2) {
2879 return false;
2880 }
2881
2882 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_DROP_LIMIT) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002883 int jumpyEpsilon = mRawAxes.y.getRange() / JUMPY_EPSILON_DIVISOR;
Jeff Browne57e8952010-07-23 21:28:06 -07002884
2885 // We only replace the single worst jumpy point as characterized by pointer distance
2886 // in a single axis.
2887 int32_t badPointerIndex = -1;
2888 int32_t badPointerReplacementIndex = -1;
2889 int32_t badPointerDistance = INT_MIN; // distance to be corrected
2890
2891 for (uint32_t i = pointerCount; i-- > 0; ) {
2892 int32_t x = mCurrentTouch.pointers[i].x;
2893 int32_t y = mCurrentTouch.pointers[i].y;
2894
2895#if DEBUG_HACKS
2896 LOGD("JumpyTouchFilter: Point %d (%d, %d)", i, x, y);
2897#endif
2898
2899 // Check if a touch point is too close to another's coordinates
2900 bool dropX = false, dropY = false;
2901 for (uint32_t j = 0; j < pointerCount; j++) {
2902 if (i == j) {
2903 continue;
2904 }
2905
2906 if (abs(x - mCurrentTouch.pointers[j].x) <= jumpyEpsilon) {
2907 dropX = true;
2908 break;
2909 }
2910
2911 if (abs(y - mCurrentTouch.pointers[j].y) <= jumpyEpsilon) {
2912 dropY = true;
2913 break;
2914 }
2915 }
2916 if (! dropX && ! dropY) {
2917 continue; // not jumpy
2918 }
2919
2920 // Find a replacement candidate by comparing with older points on the
2921 // complementary (non-jumpy) axis.
2922 int32_t distance = INT_MIN; // distance to be corrected
2923 int32_t replacementIndex = -1;
2924
2925 if (dropX) {
2926 // X looks too close. Find an older replacement point with a close Y.
2927 int32_t smallestDeltaY = INT_MAX;
2928 for (uint32_t j = 0; j < pointerCount; j++) {
2929 int32_t deltaY = abs(y - mLastTouch.pointers[j].y);
2930 if (deltaY < smallestDeltaY) {
2931 smallestDeltaY = deltaY;
2932 replacementIndex = j;
2933 }
2934 }
2935 distance = abs(x - mLastTouch.pointers[replacementIndex].x);
2936 } else {
2937 // Y looks too close. Find an older replacement point with a close X.
2938 int32_t smallestDeltaX = INT_MAX;
2939 for (uint32_t j = 0; j < pointerCount; j++) {
2940 int32_t deltaX = abs(x - mLastTouch.pointers[j].x);
2941 if (deltaX < smallestDeltaX) {
2942 smallestDeltaX = deltaX;
2943 replacementIndex = j;
2944 }
2945 }
2946 distance = abs(y - mLastTouch.pointers[replacementIndex].y);
2947 }
2948
2949 // If replacing this pointer would correct a worse error than the previous ones
2950 // considered, then use this replacement instead.
2951 if (distance > badPointerDistance) {
2952 badPointerIndex = i;
2953 badPointerReplacementIndex = replacementIndex;
2954 badPointerDistance = distance;
2955 }
2956 }
2957
2958 // Correct the jumpy pointer if one was found.
2959 if (badPointerIndex >= 0) {
2960#if DEBUG_HACKS
2961 LOGD("JumpyTouchFilter: Replacing bad pointer %d with (%d, %d)",
2962 badPointerIndex,
2963 mLastTouch.pointers[badPointerReplacementIndex].x,
2964 mLastTouch.pointers[badPointerReplacementIndex].y);
2965#endif
2966
2967 mCurrentTouch.pointers[badPointerIndex].x =
2968 mLastTouch.pointers[badPointerReplacementIndex].x;
2969 mCurrentTouch.pointers[badPointerIndex].y =
2970 mLastTouch.pointers[badPointerReplacementIndex].y;
2971 mJumpyTouchFilter.jumpyPointsDropped += 1;
2972 return true;
2973 }
2974 }
2975
2976 mJumpyTouchFilter.jumpyPointsDropped = 0;
2977 return false;
2978}
2979
2980/* Special hack for devices that have bad screen data: aggregate and
2981 * compute averages of the coordinate data, to reduce the amount of
2982 * jitter seen by applications. */
2983void TouchInputMapper::applyAveragingTouchFilter() {
2984 for (uint32_t currentIndex = 0; currentIndex < mCurrentTouch.pointerCount; currentIndex++) {
2985 uint32_t id = mCurrentTouch.pointers[currentIndex].id;
2986 int32_t x = mCurrentTouch.pointers[currentIndex].x;
2987 int32_t y = mCurrentTouch.pointers[currentIndex].y;
Jeff Brown38a7fab2010-08-30 03:02:23 -07002988 int32_t pressure;
2989 switch (mCalibration.pressureSource) {
2990 case Calibration::PRESSURE_SOURCE_PRESSURE:
2991 pressure = mCurrentTouch.pointers[currentIndex].pressure;
2992 break;
2993 case Calibration::PRESSURE_SOURCE_TOUCH:
2994 pressure = mCurrentTouch.pointers[currentIndex].touchMajor;
2995 break;
2996 default:
2997 pressure = 1;
2998 break;
2999 }
Jeff Browne57e8952010-07-23 21:28:06 -07003000
3001 if (mLastTouch.idBits.hasBit(id)) {
3002 // Pointer was down before and is still down now.
3003 // Compute average over history trace.
3004 uint32_t start = mAveragingTouchFilter.historyStart[id];
3005 uint32_t end = mAveragingTouchFilter.historyEnd[id];
3006
3007 int64_t deltaX = x - mAveragingTouchFilter.historyData[end].pointers[id].x;
3008 int64_t deltaY = y - mAveragingTouchFilter.historyData[end].pointers[id].y;
3009 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3010
3011#if DEBUG_HACKS
3012 LOGD("AveragingTouchFilter: Pointer id %d - Distance from last sample: %lld",
3013 id, distance);
3014#endif
3015
3016 if (distance < AVERAGING_DISTANCE_LIMIT) {
3017 // Increment end index in preparation for recording new historical data.
3018 end += 1;
3019 if (end > AVERAGING_HISTORY_SIZE) {
3020 end = 0;
3021 }
3022
3023 // If the end index has looped back to the start index then we have filled
3024 // the historical trace up to the desired size so we drop the historical
3025 // data at the start of the trace.
3026 if (end == start) {
3027 start += 1;
3028 if (start > AVERAGING_HISTORY_SIZE) {
3029 start = 0;
3030 }
3031 }
3032
3033 // Add the raw data to the historical trace.
3034 mAveragingTouchFilter.historyStart[id] = start;
3035 mAveragingTouchFilter.historyEnd[id] = end;
3036 mAveragingTouchFilter.historyData[end].pointers[id].x = x;
3037 mAveragingTouchFilter.historyData[end].pointers[id].y = y;
3038 mAveragingTouchFilter.historyData[end].pointers[id].pressure = pressure;
3039
3040 // Average over all historical positions in the trace by total pressure.
3041 int32_t averagedX = 0;
3042 int32_t averagedY = 0;
3043 int32_t totalPressure = 0;
3044 for (;;) {
3045 int32_t historicalX = mAveragingTouchFilter.historyData[start].pointers[id].x;
3046 int32_t historicalY = mAveragingTouchFilter.historyData[start].pointers[id].y;
3047 int32_t historicalPressure = mAveragingTouchFilter.historyData[start]
3048 .pointers[id].pressure;
3049
3050 averagedX += historicalX * historicalPressure;
3051 averagedY += historicalY * historicalPressure;
3052 totalPressure += historicalPressure;
3053
3054 if (start == end) {
3055 break;
3056 }
3057
3058 start += 1;
3059 if (start > AVERAGING_HISTORY_SIZE) {
3060 start = 0;
3061 }
3062 }
3063
Jeff Brown38a7fab2010-08-30 03:02:23 -07003064 if (totalPressure != 0) {
3065 averagedX /= totalPressure;
3066 averagedY /= totalPressure;
Jeff Browne57e8952010-07-23 21:28:06 -07003067
3068#if DEBUG_HACKS
Jeff Brown38a7fab2010-08-30 03:02:23 -07003069 LOGD("AveragingTouchFilter: Pointer id %d - "
3070 "totalPressure=%d, averagedX=%d, averagedY=%d", id, totalPressure,
3071 averagedX, averagedY);
Jeff Browne57e8952010-07-23 21:28:06 -07003072#endif
3073
Jeff Brown38a7fab2010-08-30 03:02:23 -07003074 mCurrentTouch.pointers[currentIndex].x = averagedX;
3075 mCurrentTouch.pointers[currentIndex].y = averagedY;
3076 }
Jeff Browne57e8952010-07-23 21:28:06 -07003077 } else {
3078#if DEBUG_HACKS
3079 LOGD("AveragingTouchFilter: Pointer id %d - Exceeded max distance", id);
3080#endif
3081 }
3082 } else {
3083#if DEBUG_HACKS
3084 LOGD("AveragingTouchFilter: Pointer id %d - Pointer went up", id);
3085#endif
3086 }
3087
3088 // Reset pointer history.
3089 mAveragingTouchFilter.historyStart[id] = 0;
3090 mAveragingTouchFilter.historyEnd[id] = 0;
3091 mAveragingTouchFilter.historyData[0].pointers[id].x = x;
3092 mAveragingTouchFilter.historyData[0].pointers[id].y = y;
3093 mAveragingTouchFilter.historyData[0].pointers[id].pressure = pressure;
3094 }
3095}
3096
3097int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownb51719b2010-07-29 18:18:33 -07003098 { // acquire lock
3099 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07003100
Jeff Brownb51719b2010-07-29 18:18:33 -07003101 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07003102 return AKEY_STATE_VIRTUAL;
3103 }
3104
Jeff Brownb51719b2010-07-29 18:18:33 -07003105 size_t numVirtualKeys = mLocked.virtualKeys.size();
3106 for (size_t i = 0; i < numVirtualKeys; i++) {
3107 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07003108 if (virtualKey.keyCode == keyCode) {
3109 return AKEY_STATE_UP;
3110 }
3111 }
Jeff Brownb51719b2010-07-29 18:18:33 -07003112 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07003113
3114 return AKEY_STATE_UNKNOWN;
3115}
3116
3117int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownb51719b2010-07-29 18:18:33 -07003118 { // acquire lock
3119 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07003120
Jeff Brownb51719b2010-07-29 18:18:33 -07003121 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07003122 return AKEY_STATE_VIRTUAL;
3123 }
3124
Jeff Brownb51719b2010-07-29 18:18:33 -07003125 size_t numVirtualKeys = mLocked.virtualKeys.size();
3126 for (size_t i = 0; i < numVirtualKeys; i++) {
3127 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07003128 if (virtualKey.scanCode == scanCode) {
3129 return AKEY_STATE_UP;
3130 }
3131 }
Jeff Brownb51719b2010-07-29 18:18:33 -07003132 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07003133
3134 return AKEY_STATE_UNKNOWN;
3135}
3136
3137bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
3138 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownb51719b2010-07-29 18:18:33 -07003139 { // acquire lock
3140 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07003141
Jeff Brownb51719b2010-07-29 18:18:33 -07003142 size_t numVirtualKeys = mLocked.virtualKeys.size();
3143 for (size_t i = 0; i < numVirtualKeys; i++) {
3144 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07003145
3146 for (size_t i = 0; i < numCodes; i++) {
3147 if (virtualKey.keyCode == keyCodes[i]) {
3148 outFlags[i] = 1;
3149 }
3150 }
3151 }
Jeff Brownb51719b2010-07-29 18:18:33 -07003152 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07003153
3154 return true;
3155}
3156
3157
3158// --- SingleTouchInputMapper ---
3159
3160SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device, int32_t associatedDisplayId) :
3161 TouchInputMapper(device, associatedDisplayId) {
3162 initialize();
3163}
3164
3165SingleTouchInputMapper::~SingleTouchInputMapper() {
3166}
3167
3168void SingleTouchInputMapper::initialize() {
3169 mAccumulator.clear();
3170
3171 mDown = false;
3172 mX = 0;
3173 mY = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003174 mPressure = 0; // default to 0 for devices that don't report pressure
3175 mToolWidth = 0; // default to 0 for devices that don't report tool width
Jeff Browne57e8952010-07-23 21:28:06 -07003176}
3177
3178void SingleTouchInputMapper::reset() {
3179 TouchInputMapper::reset();
3180
Jeff Browne57e8952010-07-23 21:28:06 -07003181 initialize();
3182 }
3183
3184void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
3185 switch (rawEvent->type) {
3186 case EV_KEY:
3187 switch (rawEvent->scanCode) {
3188 case BTN_TOUCH:
3189 mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH;
3190 mAccumulator.btnTouch = rawEvent->value != 0;
Jeff Brownd64c8552010-08-17 20:38:35 -07003191 // Don't sync immediately. Wait until the next SYN_REPORT since we might
3192 // not have received valid position information yet. This logic assumes that
3193 // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet.
Jeff Browne57e8952010-07-23 21:28:06 -07003194 break;
3195 }
3196 break;
3197
3198 case EV_ABS:
3199 switch (rawEvent->scanCode) {
3200 case ABS_X:
3201 mAccumulator.fields |= Accumulator::FIELD_ABS_X;
3202 mAccumulator.absX = rawEvent->value;
3203 break;
3204 case ABS_Y:
3205 mAccumulator.fields |= Accumulator::FIELD_ABS_Y;
3206 mAccumulator.absY = rawEvent->value;
3207 break;
3208 case ABS_PRESSURE:
3209 mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE;
3210 mAccumulator.absPressure = rawEvent->value;
3211 break;
3212 case ABS_TOOL_WIDTH:
3213 mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH;
3214 mAccumulator.absToolWidth = rawEvent->value;
3215 break;
3216 }
3217 break;
3218
3219 case EV_SYN:
3220 switch (rawEvent->scanCode) {
3221 case SYN_REPORT:
Jeff Brownd64c8552010-08-17 20:38:35 -07003222 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07003223 break;
3224 }
3225 break;
3226 }
3227}
3228
3229void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Browne57e8952010-07-23 21:28:06 -07003230 uint32_t fields = mAccumulator.fields;
Jeff Brownd64c8552010-08-17 20:38:35 -07003231 if (fields == 0) {
3232 return; // no new state changes, so nothing to do
3233 }
Jeff Browne57e8952010-07-23 21:28:06 -07003234
3235 if (fields & Accumulator::FIELD_BTN_TOUCH) {
3236 mDown = mAccumulator.btnTouch;
3237 }
3238
3239 if (fields & Accumulator::FIELD_ABS_X) {
3240 mX = mAccumulator.absX;
3241 }
3242
3243 if (fields & Accumulator::FIELD_ABS_Y) {
3244 mY = mAccumulator.absY;
3245 }
3246
3247 if (fields & Accumulator::FIELD_ABS_PRESSURE) {
3248 mPressure = mAccumulator.absPressure;
3249 }
3250
3251 if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003252 mToolWidth = mAccumulator.absToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -07003253 }
3254
3255 mCurrentTouch.clear();
3256
3257 if (mDown) {
3258 mCurrentTouch.pointerCount = 1;
3259 mCurrentTouch.pointers[0].id = 0;
3260 mCurrentTouch.pointers[0].x = mX;
3261 mCurrentTouch.pointers[0].y = mY;
3262 mCurrentTouch.pointers[0].pressure = mPressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003263 mCurrentTouch.pointers[0].touchMajor = 0;
3264 mCurrentTouch.pointers[0].touchMinor = 0;
3265 mCurrentTouch.pointers[0].toolMajor = mToolWidth;
3266 mCurrentTouch.pointers[0].toolMinor = mToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -07003267 mCurrentTouch.pointers[0].orientation = 0;
3268 mCurrentTouch.idToIndex[0] = 0;
3269 mCurrentTouch.idBits.markBit(0);
3270 }
3271
3272 syncTouch(when, true);
Jeff Brownd64c8552010-08-17 20:38:35 -07003273
3274 mAccumulator.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07003275}
3276
Jeff Brown38a7fab2010-08-30 03:02:23 -07003277void SingleTouchInputMapper::configureRawAxes() {
3278 TouchInputMapper::configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -07003279
Jeff Brown38a7fab2010-08-30 03:02:23 -07003280 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x);
3281 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y);
3282 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure);
3283 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor);
Jeff Browne57e8952010-07-23 21:28:06 -07003284}
3285
3286
3287// --- MultiTouchInputMapper ---
3288
3289MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device, int32_t associatedDisplayId) :
3290 TouchInputMapper(device, associatedDisplayId) {
3291 initialize();
3292}
3293
3294MultiTouchInputMapper::~MultiTouchInputMapper() {
3295}
3296
3297void MultiTouchInputMapper::initialize() {
3298 mAccumulator.clear();
3299}
3300
3301void MultiTouchInputMapper::reset() {
3302 TouchInputMapper::reset();
3303
Jeff Browne57e8952010-07-23 21:28:06 -07003304 initialize();
3305}
3306
3307void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
3308 switch (rawEvent->type) {
3309 case EV_ABS: {
3310 uint32_t pointerIndex = mAccumulator.pointerCount;
3311 Accumulator::Pointer* pointer = & mAccumulator.pointers[pointerIndex];
3312
3313 switch (rawEvent->scanCode) {
3314 case ABS_MT_POSITION_X:
3315 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_X;
3316 pointer->absMTPositionX = rawEvent->value;
3317 break;
3318 case ABS_MT_POSITION_Y:
3319 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y;
3320 pointer->absMTPositionY = rawEvent->value;
3321 break;
3322 case ABS_MT_TOUCH_MAJOR:
3323 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR;
3324 pointer->absMTTouchMajor = rawEvent->value;
3325 break;
3326 case ABS_MT_TOUCH_MINOR:
3327 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR;
3328 pointer->absMTTouchMinor = rawEvent->value;
3329 break;
3330 case ABS_MT_WIDTH_MAJOR:
3331 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR;
3332 pointer->absMTWidthMajor = rawEvent->value;
3333 break;
3334 case ABS_MT_WIDTH_MINOR:
3335 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR;
3336 pointer->absMTWidthMinor = rawEvent->value;
3337 break;
3338 case ABS_MT_ORIENTATION:
3339 pointer->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION;
3340 pointer->absMTOrientation = rawEvent->value;
3341 break;
3342 case ABS_MT_TRACKING_ID:
3343 pointer->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID;
3344 pointer->absMTTrackingId = rawEvent->value;
3345 break;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003346 case ABS_MT_PRESSURE:
3347 pointer->fields |= Accumulator::FIELD_ABS_MT_PRESSURE;
3348 pointer->absMTPressure = rawEvent->value;
3349 break;
Jeff Browne57e8952010-07-23 21:28:06 -07003350 }
3351 break;
3352 }
3353
3354 case EV_SYN:
3355 switch (rawEvent->scanCode) {
3356 case SYN_MT_REPORT: {
3357 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
3358 uint32_t pointerIndex = mAccumulator.pointerCount;
3359
3360 if (mAccumulator.pointers[pointerIndex].fields) {
3361 if (pointerIndex == MAX_POINTERS) {
3362 LOGW("MultiTouch device driver returned more than maximum of %d pointers.",
3363 MAX_POINTERS);
3364 } else {
3365 pointerIndex += 1;
3366 mAccumulator.pointerCount = pointerIndex;
3367 }
3368 }
3369
3370 mAccumulator.pointers[pointerIndex].clear();
3371 break;
3372 }
3373
3374 case SYN_REPORT:
Jeff Brownd64c8552010-08-17 20:38:35 -07003375 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07003376 break;
3377 }
3378 break;
3379 }
3380}
3381
3382void MultiTouchInputMapper::sync(nsecs_t when) {
3383 static const uint32_t REQUIRED_FIELDS =
Jeff Brown38a7fab2010-08-30 03:02:23 -07003384 Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y;
Jeff Browne839a582010-04-22 18:58:52 -07003385
Jeff Browne57e8952010-07-23 21:28:06 -07003386 uint32_t inCount = mAccumulator.pointerCount;
3387 uint32_t outCount = 0;
3388 bool havePointerIds = true;
Jeff Browne839a582010-04-22 18:58:52 -07003389
Jeff Browne57e8952010-07-23 21:28:06 -07003390 mCurrentTouch.clear();
Jeff Browne839a582010-04-22 18:58:52 -07003391
Jeff Browne57e8952010-07-23 21:28:06 -07003392 for (uint32_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003393 const Accumulator::Pointer& inPointer = mAccumulator.pointers[inIndex];
3394 uint32_t fields = inPointer.fields;
Jeff Browne839a582010-04-22 18:58:52 -07003395
Jeff Browne57e8952010-07-23 21:28:06 -07003396 if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003397 // Some drivers send empty MT sync packets without X / Y to indicate a pointer up.
3398 // Drop this finger.
Jeff Browne839a582010-04-22 18:58:52 -07003399 continue;
3400 }
3401
Jeff Brownd64c8552010-08-17 20:38:35 -07003402 PointerData& outPointer = mCurrentTouch.pointers[outCount];
3403 outPointer.x = inPointer.absMTPositionX;
3404 outPointer.y = inPointer.absMTPositionY;
Jeff Browne839a582010-04-22 18:58:52 -07003405
Jeff Brown38a7fab2010-08-30 03:02:23 -07003406 if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) {
3407 if (inPointer.absMTPressure <= 0) {
Jeff Brown3c3cc622010-10-20 15:33:38 -07003408 // Some devices send sync packets with X / Y but with a 0 pressure to indicate
3409 // a pointer going up. Drop this finger.
Jeff Brownd64c8552010-08-17 20:38:35 -07003410 continue;
3411 }
Jeff Brown38a7fab2010-08-30 03:02:23 -07003412 outPointer.pressure = inPointer.absMTPressure;
3413 } else {
3414 // Default pressure to 0 if absent.
3415 outPointer.pressure = 0;
3416 }
3417
3418 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) {
3419 if (inPointer.absMTTouchMajor <= 0) {
3420 // Some devices send sync packets with X / Y but with a 0 touch major to indicate
3421 // a pointer going up. Drop this finger.
3422 continue;
3423 }
Jeff Brownd64c8552010-08-17 20:38:35 -07003424 outPointer.touchMajor = inPointer.absMTTouchMajor;
3425 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003426 // Default touch area to 0 if absent.
Jeff Brownd64c8552010-08-17 20:38:35 -07003427 outPointer.touchMajor = 0;
3428 }
Jeff Browne839a582010-04-22 18:58:52 -07003429
Jeff Brownd64c8552010-08-17 20:38:35 -07003430 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) {
3431 outPointer.touchMinor = inPointer.absMTTouchMinor;
3432 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003433 // Assume touch area is circular.
Jeff Brownd64c8552010-08-17 20:38:35 -07003434 outPointer.touchMinor = outPointer.touchMajor;
3435 }
Jeff Browne839a582010-04-22 18:58:52 -07003436
Jeff Brownd64c8552010-08-17 20:38:35 -07003437 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) {
3438 outPointer.toolMajor = inPointer.absMTWidthMajor;
3439 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003440 // Default tool area to 0 if absent.
3441 outPointer.toolMajor = 0;
Jeff Brownd64c8552010-08-17 20:38:35 -07003442 }
Jeff Browne839a582010-04-22 18:58:52 -07003443
Jeff Brownd64c8552010-08-17 20:38:35 -07003444 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) {
3445 outPointer.toolMinor = inPointer.absMTWidthMinor;
3446 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003447 // Assume tool area is circular.
Jeff Brownd64c8552010-08-17 20:38:35 -07003448 outPointer.toolMinor = outPointer.toolMajor;
3449 }
3450
3451 if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) {
3452 outPointer.orientation = inPointer.absMTOrientation;
3453 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003454 // Default orientation to vertical if absent.
Jeff Brownd64c8552010-08-17 20:38:35 -07003455 outPointer.orientation = 0;
3456 }
3457
Jeff Brown38a7fab2010-08-30 03:02:23 -07003458 // Assign pointer id using tracking id if available.
Jeff Browne57e8952010-07-23 21:28:06 -07003459 if (havePointerIds) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003460 if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) {
3461 uint32_t id = uint32_t(inPointer.absMTTrackingId);
Jeff Browne839a582010-04-22 18:58:52 -07003462
Jeff Browne57e8952010-07-23 21:28:06 -07003463 if (id > MAX_POINTER_ID) {
3464#if DEBUG_POINTERS
3465 LOGD("Pointers: Ignoring driver provided pointer id %d because "
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07003466 "it is larger than max supported id %d",
Jeff Browne57e8952010-07-23 21:28:06 -07003467 id, MAX_POINTER_ID);
3468#endif
3469 havePointerIds = false;
3470 }
3471 else {
Jeff Brownd64c8552010-08-17 20:38:35 -07003472 outPointer.id = id;
Jeff Browne57e8952010-07-23 21:28:06 -07003473 mCurrentTouch.idToIndex[id] = outCount;
3474 mCurrentTouch.idBits.markBit(id);
3475 }
3476 } else {
3477 havePointerIds = false;
Jeff Browne839a582010-04-22 18:58:52 -07003478 }
3479 }
Jeff Browne839a582010-04-22 18:58:52 -07003480
Jeff Browne57e8952010-07-23 21:28:06 -07003481 outCount += 1;
Jeff Browne839a582010-04-22 18:58:52 -07003482 }
3483
Jeff Browne57e8952010-07-23 21:28:06 -07003484 mCurrentTouch.pointerCount = outCount;
Jeff Browne839a582010-04-22 18:58:52 -07003485
Jeff Browne57e8952010-07-23 21:28:06 -07003486 syncTouch(when, havePointerIds);
Jeff Brownd64c8552010-08-17 20:38:35 -07003487
3488 mAccumulator.clear();
Jeff Browne839a582010-04-22 18:58:52 -07003489}
3490
Jeff Brown38a7fab2010-08-30 03:02:23 -07003491void MultiTouchInputMapper::configureRawAxes() {
3492 TouchInputMapper::configureRawAxes();
Jeff Browne839a582010-04-22 18:58:52 -07003493
Jeff Brown38a7fab2010-08-30 03:02:23 -07003494 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, & mRawAxes.x);
3495 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, & mRawAxes.y);
3496 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, & mRawAxes.touchMajor);
3497 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, & mRawAxes.touchMinor);
3498 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, & mRawAxes.toolMajor);
3499 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, & mRawAxes.toolMinor);
3500 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, & mRawAxes.orientation);
3501 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, & mRawAxes.pressure);
Jeff Brown54bc2812010-06-15 01:31:58 -07003502}
3503
Jeff Browne839a582010-04-22 18:58:52 -07003504
3505} // namespace android