blob: 0560bb89fb2383ea2c01f547a60e2fe339b42396 [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>
27
28#include <stddef.h>
Jeff Brown38a7fab2010-08-30 03:02:23 -070029#include <stdlib.h>
Jeff Browne839a582010-04-22 18:58:52 -070030#include <unistd.h>
Jeff Browne839a582010-04-22 18:58:52 -070031#include <errno.h>
32#include <limits.h>
Jeff Brown5c1ed842010-07-14 18:48:53 -070033#include <math.h>
Jeff Browne839a582010-04-22 18:58:52 -070034
Jeff Brown38a7fab2010-08-30 03:02:23 -070035#define INDENT " "
Jeff Brown26c94ff2010-09-30 14:33:04 -070036#define INDENT2 " "
37#define INDENT3 " "
38#define INDENT4 " "
Jeff Brown38a7fab2010-08-30 03:02:23 -070039
Jeff Browne839a582010-04-22 18:58:52 -070040namespace android {
41
42// --- Static Functions ---
43
44template<typename T>
45inline static T abs(const T& value) {
46 return value < 0 ? - value : value;
47}
48
49template<typename T>
50inline static T min(const T& a, const T& b) {
51 return a < b ? a : b;
52}
53
Jeff Brownf4a4ec22010-06-16 01:53:36 -070054template<typename T>
55inline static void swap(T& a, T& b) {
56 T temp = a;
57 a = b;
58 b = temp;
59}
60
Jeff Brown38a7fab2010-08-30 03:02:23 -070061inline static float avg(float x, float y) {
62 return (x + y) / 2;
63}
64
65inline static float pythag(float x, float y) {
66 return sqrtf(x * x + y * y);
67}
68
Jeff Brown26c94ff2010-09-30 14:33:04 -070069static inline const char* toString(bool value) {
70 return value ? "true" : "false";
71}
72
Jeff Brownf4a4ec22010-06-16 01:53:36 -070073
Jeff Browne839a582010-04-22 18:58:52 -070074int32_t updateMetaState(int32_t keyCode, bool down, int32_t oldMetaState) {
75 int32_t mask;
76 switch (keyCode) {
Jeff Brown8575a872010-06-30 16:10:35 -070077 case AKEYCODE_ALT_LEFT:
Jeff Brown5c1ed842010-07-14 18:48:53 -070078 mask = AMETA_ALT_LEFT_ON;
Jeff Browne839a582010-04-22 18:58:52 -070079 break;
Jeff Brown8575a872010-06-30 16:10:35 -070080 case AKEYCODE_ALT_RIGHT:
Jeff Brown5c1ed842010-07-14 18:48:53 -070081 mask = AMETA_ALT_RIGHT_ON;
Jeff Browne839a582010-04-22 18:58:52 -070082 break;
Jeff Brown8575a872010-06-30 16:10:35 -070083 case AKEYCODE_SHIFT_LEFT:
Jeff Brown5c1ed842010-07-14 18:48:53 -070084 mask = AMETA_SHIFT_LEFT_ON;
Jeff Browne839a582010-04-22 18:58:52 -070085 break;
Jeff Brown8575a872010-06-30 16:10:35 -070086 case AKEYCODE_SHIFT_RIGHT:
Jeff Brown5c1ed842010-07-14 18:48:53 -070087 mask = AMETA_SHIFT_RIGHT_ON;
Jeff Browne839a582010-04-22 18:58:52 -070088 break;
Jeff Brown8575a872010-06-30 16:10:35 -070089 case AKEYCODE_SYM:
Jeff Brown5c1ed842010-07-14 18:48:53 -070090 mask = AMETA_SYM_ON;
Jeff Browne839a582010-04-22 18:58:52 -070091 break;
92 default:
93 return oldMetaState;
94 }
95
96 int32_t newMetaState = down ? oldMetaState | mask : oldMetaState & ~ mask
Jeff Brown5c1ed842010-07-14 18:48:53 -070097 & ~ (AMETA_ALT_ON | AMETA_SHIFT_ON);
Jeff Browne839a582010-04-22 18:58:52 -070098
Jeff Brown5c1ed842010-07-14 18:48:53 -070099 if (newMetaState & (AMETA_ALT_LEFT_ON | AMETA_ALT_RIGHT_ON)) {
100 newMetaState |= AMETA_ALT_ON;
Jeff Browne839a582010-04-22 18:58:52 -0700101 }
102
Jeff Brown5c1ed842010-07-14 18:48:53 -0700103 if (newMetaState & (AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_RIGHT_ON)) {
104 newMetaState |= AMETA_SHIFT_ON;
Jeff Browne839a582010-04-22 18:58:52 -0700105 }
106
107 return newMetaState;
108}
109
110static const int32_t keyCodeRotationMap[][4] = {
111 // key codes enumerated counter-clockwise with the original (unrotated) key first
112 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brown8575a872010-06-30 16:10:35 -0700113 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
114 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
115 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
116 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Browne839a582010-04-22 18:58:52 -0700117};
118static const int keyCodeRotationMapSize =
119 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
120
121int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brown54bc2812010-06-15 01:31:58 -0700122 if (orientation != InputReaderPolicyInterface::ROTATION_0) {
Jeff Browne839a582010-04-22 18:58:52 -0700123 for (int i = 0; i < keyCodeRotationMapSize; i++) {
124 if (keyCode == keyCodeRotationMap[i][0]) {
125 return keyCodeRotationMap[i][orientation];
126 }
127 }
128 }
129 return keyCode;
130}
131
Jeff Browne57e8952010-07-23 21:28:06 -0700132static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
133 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
134}
135
Jeff Browne839a582010-04-22 18:58:52 -0700136
Jeff Brown38a7fab2010-08-30 03:02:23 -0700137// --- InputDeviceCalibration ---
138
139InputDeviceCalibration::InputDeviceCalibration() {
140}
141
142void InputDeviceCalibration::clear() {
143 mProperties.clear();
144}
145
146void InputDeviceCalibration::addProperty(const String8& key, const String8& value) {
147 mProperties.add(key, value);
148}
149
150bool InputDeviceCalibration::tryGetProperty(const String8& key, String8& outValue) const {
151 ssize_t index = mProperties.indexOfKey(key);
152 if (index < 0) {
153 return false;
154 }
155
156 outValue = mProperties.valueAt(index);
157 return true;
158}
159
160bool InputDeviceCalibration::tryGetProperty(const String8& key, int32_t& outValue) const {
161 String8 stringValue;
162 if (! tryGetProperty(key, stringValue) || stringValue.length() == 0) {
163 return false;
164 }
165
166 char* end;
167 int value = strtol(stringValue.string(), & end, 10);
168 if (*end != '\0') {
169 LOGW("Input device calibration key '%s' has invalid value '%s'. Expected an integer.",
170 key.string(), stringValue.string());
171 return false;
172 }
173 outValue = value;
174 return true;
175}
176
177bool InputDeviceCalibration::tryGetProperty(const String8& key, float& outValue) const {
178 String8 stringValue;
179 if (! tryGetProperty(key, stringValue) || stringValue.length() == 0) {
180 return false;
181 }
182
183 char* end;
184 float value = strtof(stringValue.string(), & end);
185 if (*end != '\0') {
186 LOGW("Input device calibration key '%s' has invalid value '%s'. Expected a float.",
187 key.string(), stringValue.string());
188 return false;
189 }
190 outValue = value;
191 return true;
192}
193
194
Jeff Browne839a582010-04-22 18:58:52 -0700195// --- InputReader ---
196
197InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown54bc2812010-06-15 01:31:58 -0700198 const sp<InputReaderPolicyInterface>& policy,
Jeff Browne839a582010-04-22 18:58:52 -0700199 const sp<InputDispatcherInterface>& dispatcher) :
Jeff Browne57e8952010-07-23 21:28:06 -0700200 mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher),
201 mGlobalMetaState(0) {
Jeff Brown54bc2812010-06-15 01:31:58 -0700202 configureExcludedDevices();
Jeff Browne57e8952010-07-23 21:28:06 -0700203 updateGlobalMetaState();
204 updateInputConfiguration();
Jeff Browne839a582010-04-22 18:58:52 -0700205}
206
207InputReader::~InputReader() {
208 for (size_t i = 0; i < mDevices.size(); i++) {
209 delete mDevices.valueAt(i);
210 }
211}
212
213void InputReader::loopOnce() {
214 RawEvent rawEvent;
Jeff Browne57e8952010-07-23 21:28:06 -0700215 mEventHub->getEvent(& rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700216
217#if DEBUG_RAW_EVENTS
218 LOGD("Input event: device=0x%x type=0x%x scancode=%d keycode=%d value=%d",
219 rawEvent.deviceId, rawEvent.type, rawEvent.scanCode, rawEvent.keyCode,
220 rawEvent.value);
221#endif
222
223 process(& rawEvent);
224}
225
226void InputReader::process(const RawEvent* rawEvent) {
227 switch (rawEvent->type) {
228 case EventHubInterface::DEVICE_ADDED:
Jeff Brown1ad00e92010-10-01 18:55:43 -0700229 addDevice(rawEvent->deviceId);
Jeff Browne839a582010-04-22 18:58:52 -0700230 break;
231
232 case EventHubInterface::DEVICE_REMOVED:
Jeff Brown1ad00e92010-10-01 18:55:43 -0700233 removeDevice(rawEvent->deviceId);
234 break;
235
236 case EventHubInterface::FINISHED_DEVICE_SCAN:
237 handleConfigurationChanged();
Jeff Browne839a582010-04-22 18:58:52 -0700238 break;
239
Jeff Browne57e8952010-07-23 21:28:06 -0700240 default:
241 consumeEvent(rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700242 break;
243 }
244}
245
Jeff Brown1ad00e92010-10-01 18:55:43 -0700246void InputReader::addDevice(int32_t deviceId) {
Jeff Browne57e8952010-07-23 21:28:06 -0700247 String8 name = mEventHub->getDeviceName(deviceId);
248 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
249
250 InputDevice* device = createDevice(deviceId, name, classes);
251 device->configure();
252
Jeff Brown38a7fab2010-08-30 03:02:23 -0700253 if (device->isIgnored()) {
Jeff Brown26c94ff2010-09-30 14:33:04 -0700254 LOGI("Device added: id=0x%x, name=%s (ignored non-input device)", deviceId, name.string());
Jeff Brown38a7fab2010-08-30 03:02:23 -0700255 } else {
Jeff Brown26c94ff2010-09-30 14:33:04 -0700256 LOGI("Device added: id=0x%x, name=%s, sources=%08x", deviceId, name.string(),
257 device->getSources());
Jeff Brown38a7fab2010-08-30 03:02:23 -0700258 }
259
Jeff Browne57e8952010-07-23 21:28:06 -0700260 bool added = false;
261 { // acquire device registry writer lock
262 RWLock::AutoWLock _wl(mDeviceRegistryLock);
263
264 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
265 if (deviceIndex < 0) {
266 mDevices.add(deviceId, device);
267 added = true;
268 }
269 } // release device registry writer lock
270
271 if (! added) {
272 LOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
273 delete device;
Jeff Browne839a582010-04-22 18:58:52 -0700274 return;
275 }
Jeff Browne839a582010-04-22 18:58:52 -0700276}
277
Jeff Brown1ad00e92010-10-01 18:55:43 -0700278void InputReader::removeDevice(int32_t deviceId) {
Jeff Browne57e8952010-07-23 21:28:06 -0700279 bool removed = false;
280 InputDevice* device = NULL;
281 { // acquire device registry writer lock
282 RWLock::AutoWLock _wl(mDeviceRegistryLock);
283
284 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
285 if (deviceIndex >= 0) {
286 device = mDevices.valueAt(deviceIndex);
287 mDevices.removeItemsAt(deviceIndex, 1);
288 removed = true;
289 }
290 } // release device registry writer lock
291
292 if (! removed) {
293 LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Browne839a582010-04-22 18:58:52 -0700294 return;
295 }
296
Jeff Browne57e8952010-07-23 21:28:06 -0700297 if (device->isIgnored()) {
298 LOGI("Device removed: id=0x%x, name=%s (ignored non-input device)",
299 device->getId(), device->getName().string());
300 } else {
301 LOGI("Device removed: id=0x%x, name=%s, sources=%08x",
302 device->getId(), device->getName().string(), device->getSources());
303 }
304
Jeff Brown38a7fab2010-08-30 03:02:23 -0700305 device->reset();
306
Jeff Browne57e8952010-07-23 21:28:06 -0700307 delete device;
Jeff Browne839a582010-04-22 18:58:52 -0700308}
309
Jeff Browne57e8952010-07-23 21:28:06 -0700310InputDevice* InputReader::createDevice(int32_t deviceId, const String8& name, uint32_t classes) {
311 InputDevice* device = new InputDevice(this, deviceId, name);
Jeff Browne839a582010-04-22 18:58:52 -0700312
Jeff Browne57e8952010-07-23 21:28:06 -0700313 const int32_t associatedDisplayId = 0; // FIXME: hardcoded for current single-display devices
Jeff Browne839a582010-04-22 18:58:52 -0700314
Jeff Browne57e8952010-07-23 21:28:06 -0700315 // Switch-like devices.
316 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
317 device->addMapper(new SwitchInputMapper(device));
318 }
319
320 // Keyboard-like devices.
321 uint32_t keyboardSources = 0;
322 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
323 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
324 keyboardSources |= AINPUT_SOURCE_KEYBOARD;
325 }
326 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
327 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
328 }
329 if (classes & INPUT_DEVICE_CLASS_DPAD) {
330 keyboardSources |= AINPUT_SOURCE_DPAD;
331 }
Jeff Browne57e8952010-07-23 21:28:06 -0700332
333 if (keyboardSources != 0) {
334 device->addMapper(new KeyboardInputMapper(device,
335 associatedDisplayId, keyboardSources, keyboardType));
336 }
337
338 // Trackball-like devices.
339 if (classes & INPUT_DEVICE_CLASS_TRACKBALL) {
340 device->addMapper(new TrackballInputMapper(device, associatedDisplayId));
341 }
342
343 // Touchscreen-like devices.
344 if (classes & INPUT_DEVICE_CLASS_TOUCHSCREEN_MT) {
345 device->addMapper(new MultiTouchInputMapper(device, associatedDisplayId));
346 } else if (classes & INPUT_DEVICE_CLASS_TOUCHSCREEN) {
347 device->addMapper(new SingleTouchInputMapper(device, associatedDisplayId));
348 }
349
350 return device;
351}
352
353void InputReader::consumeEvent(const RawEvent* rawEvent) {
354 int32_t deviceId = rawEvent->deviceId;
355
356 { // acquire device registry reader lock
357 RWLock::AutoRLock _rl(mDeviceRegistryLock);
358
359 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
360 if (deviceIndex < 0) {
361 LOGW("Discarding event for unknown deviceId %d.", deviceId);
362 return;
363 }
364
365 InputDevice* device = mDevices.valueAt(deviceIndex);
366 if (device->isIgnored()) {
367 //LOGD("Discarding event for ignored deviceId %d.", deviceId);
368 return;
369 }
370
371 device->process(rawEvent);
372 } // release device registry reader lock
373}
374
Jeff Brown1ad00e92010-10-01 18:55:43 -0700375void InputReader::handleConfigurationChanged() {
Jeff Browne57e8952010-07-23 21:28:06 -0700376 // Reset global meta state because it depends on the list of all configured devices.
377 updateGlobalMetaState();
378
379 // Update input configuration.
380 updateInputConfiguration();
381
382 // Enqueue configuration changed.
Jeff Brown1ad00e92010-10-01 18:55:43 -0700383 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Browne57e8952010-07-23 21:28:06 -0700384 mDispatcher->notifyConfigurationChanged(when);
385}
386
387void InputReader::configureExcludedDevices() {
388 Vector<String8> excludedDeviceNames;
389 mPolicy->getExcludedDeviceNames(excludedDeviceNames);
390
391 for (size_t i = 0; i < excludedDeviceNames.size(); i++) {
392 mEventHub->addExcludedDevice(excludedDeviceNames[i]);
393 }
394}
395
396void InputReader::updateGlobalMetaState() {
397 { // acquire state lock
398 AutoMutex _l(mStateLock);
399
400 mGlobalMetaState = 0;
401
402 { // acquire device registry reader lock
403 RWLock::AutoRLock _rl(mDeviceRegistryLock);
404
405 for (size_t i = 0; i < mDevices.size(); i++) {
406 InputDevice* device = mDevices.valueAt(i);
407 mGlobalMetaState |= device->getMetaState();
408 }
409 } // release device registry reader lock
410 } // release state lock
411}
412
413int32_t InputReader::getGlobalMetaState() {
414 { // acquire state lock
415 AutoMutex _l(mStateLock);
416
417 return mGlobalMetaState;
418 } // release state lock
419}
420
421void InputReader::updateInputConfiguration() {
422 { // acquire state lock
423 AutoMutex _l(mStateLock);
424
425 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
426 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
427 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
428 { // acquire device registry reader lock
429 RWLock::AutoRLock _rl(mDeviceRegistryLock);
430
431 InputDeviceInfo deviceInfo;
432 for (size_t i = 0; i < mDevices.size(); i++) {
433 InputDevice* device = mDevices.valueAt(i);
434 device->getDeviceInfo(& deviceInfo);
435 uint32_t sources = deviceInfo.getSources();
436
437 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
438 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
439 }
440 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
441 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
442 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
443 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
444 }
445 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
446 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
Jeff Browne839a582010-04-22 18:58:52 -0700447 }
448 }
Jeff Browne57e8952010-07-23 21:28:06 -0700449 } // release device registry reader lock
Jeff Browne839a582010-04-22 18:58:52 -0700450
Jeff Browne57e8952010-07-23 21:28:06 -0700451 mInputConfiguration.touchScreen = touchScreenConfig;
452 mInputConfiguration.keyboard = keyboardConfig;
453 mInputConfiguration.navigation = navigationConfig;
454 } // release state lock
455}
456
457void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
458 { // acquire state lock
459 AutoMutex _l(mStateLock);
460
461 *outConfiguration = mInputConfiguration;
462 } // release state lock
463}
464
465status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
466 { // acquire device registry reader lock
467 RWLock::AutoRLock _rl(mDeviceRegistryLock);
468
469 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
470 if (deviceIndex < 0) {
471 return NAME_NOT_FOUND;
Jeff Browne839a582010-04-22 18:58:52 -0700472 }
473
Jeff Browne57e8952010-07-23 21:28:06 -0700474 InputDevice* device = mDevices.valueAt(deviceIndex);
475 if (device->isIgnored()) {
476 return NAME_NOT_FOUND;
Jeff Browne839a582010-04-22 18:58:52 -0700477 }
Jeff Browne57e8952010-07-23 21:28:06 -0700478
479 device->getDeviceInfo(outDeviceInfo);
480 return OK;
481 } // release device registy reader lock
482}
483
484void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
485 outDeviceIds.clear();
486
487 { // acquire device registry reader lock
488 RWLock::AutoRLock _rl(mDeviceRegistryLock);
489
490 size_t numDevices = mDevices.size();
491 for (size_t i = 0; i < numDevices; i++) {
492 InputDevice* device = mDevices.valueAt(i);
493 if (! device->isIgnored()) {
494 outDeviceIds.add(device->getId());
495 }
496 }
497 } // release device registy reader lock
498}
499
500int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
501 int32_t keyCode) {
502 return getState(deviceId, sourceMask, keyCode, & InputDevice::getKeyCodeState);
503}
504
505int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
506 int32_t scanCode) {
507 return getState(deviceId, sourceMask, scanCode, & InputDevice::getScanCodeState);
508}
509
510int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
511 return getState(deviceId, sourceMask, switchCode, & InputDevice::getSwitchState);
512}
513
514int32_t InputReader::getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
515 GetStateFunc getStateFunc) {
516 { // acquire device registry reader lock
517 RWLock::AutoRLock _rl(mDeviceRegistryLock);
518
519 int32_t result = AKEY_STATE_UNKNOWN;
520 if (deviceId >= 0) {
521 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
522 if (deviceIndex >= 0) {
523 InputDevice* device = mDevices.valueAt(deviceIndex);
524 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
525 result = (device->*getStateFunc)(sourceMask, code);
526 }
527 }
528 } else {
529 size_t numDevices = mDevices.size();
530 for (size_t i = 0; i < numDevices; i++) {
531 InputDevice* device = mDevices.valueAt(i);
532 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
533 result = (device->*getStateFunc)(sourceMask, code);
534 if (result >= AKEY_STATE_DOWN) {
535 return result;
536 }
537 }
538 }
539 }
540 return result;
541 } // release device registy reader lock
542}
543
544bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
545 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
546 memset(outFlags, 0, numCodes);
547 return markSupportedKeyCodes(deviceId, sourceMask, numCodes, keyCodes, outFlags);
548}
549
550bool InputReader::markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
551 const int32_t* keyCodes, uint8_t* outFlags) {
552 { // acquire device registry reader lock
553 RWLock::AutoRLock _rl(mDeviceRegistryLock);
554 bool result = false;
555 if (deviceId >= 0) {
556 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
557 if (deviceIndex >= 0) {
558 InputDevice* device = mDevices.valueAt(deviceIndex);
559 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
560 result = device->markSupportedKeyCodes(sourceMask,
561 numCodes, keyCodes, outFlags);
562 }
563 }
564 } else {
565 size_t numDevices = mDevices.size();
566 for (size_t i = 0; i < numDevices; i++) {
567 InputDevice* device = mDevices.valueAt(i);
568 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
569 result |= device->markSupportedKeyCodes(sourceMask,
570 numCodes, keyCodes, outFlags);
571 }
572 }
573 }
574 return result;
575 } // release device registy reader lock
576}
577
Jeff Browna665ca82010-09-08 11:49:43 -0700578void InputReader::dump(String8& dump) {
Jeff Brown2806e382010-10-01 17:46:21 -0700579 mEventHub->dump(dump);
580 dump.append("\n");
581
582 dump.append("Input Reader State:\n");
583
Jeff Brown26c94ff2010-09-30 14:33:04 -0700584 { // acquire device registry reader lock
585 RWLock::AutoRLock _rl(mDeviceRegistryLock);
Jeff Browna665ca82010-09-08 11:49:43 -0700586
Jeff Brown26c94ff2010-09-30 14:33:04 -0700587 for (size_t i = 0; i < mDevices.size(); i++) {
588 mDevices.valueAt(i)->dump(dump);
Jeff Browna665ca82010-09-08 11:49:43 -0700589 }
Jeff Brown26c94ff2010-09-30 14:33:04 -0700590 } // release device registy reader lock
Jeff Browna665ca82010-09-08 11:49:43 -0700591}
592
Jeff Browne57e8952010-07-23 21:28:06 -0700593
594// --- InputReaderThread ---
595
596InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
597 Thread(/*canCallJava*/ true), mReader(reader) {
598}
599
600InputReaderThread::~InputReaderThread() {
601}
602
603bool InputReaderThread::threadLoop() {
604 mReader->loopOnce();
605 return true;
606}
607
608
609// --- InputDevice ---
610
611InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) :
612 mContext(context), mId(id), mName(name), mSources(0) {
613}
614
615InputDevice::~InputDevice() {
616 size_t numMappers = mMappers.size();
617 for (size_t i = 0; i < numMappers; i++) {
618 delete mMappers[i];
619 }
620 mMappers.clear();
621}
622
Jeff Brown26c94ff2010-09-30 14:33:04 -0700623static void dumpMotionRange(String8& dump, const InputDeviceInfo& deviceInfo,
624 int32_t rangeType, const char* name) {
625 const InputDeviceInfo::MotionRange* range = deviceInfo.getMotionRange(rangeType);
626 if (range) {
627 dump.appendFormat(INDENT3 "%s: min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
628 name, range->min, range->max, range->flat, range->fuzz);
629 }
630}
631
632void InputDevice::dump(String8& dump) {
633 InputDeviceInfo deviceInfo;
634 getDeviceInfo(& deviceInfo);
635
636 dump.appendFormat(INDENT "Device 0x%x: %s\n", deviceInfo.getId(),
637 deviceInfo.getName().string());
638 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
639 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
640 if (!deviceInfo.getMotionRanges().isEmpty()) {
641 dump.append(INDENT2 "Motion Ranges:\n");
642 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_X, "X");
643 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_Y, "Y");
644 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_PRESSURE, "Pressure");
645 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_SIZE, "Size");
646 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOUCH_MAJOR, "TouchMajor");
647 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOUCH_MINOR, "TouchMinor");
648 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOOL_MAJOR, "ToolMajor");
649 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOOL_MINOR, "ToolMinor");
650 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_ORIENTATION, "Orientation");
651 }
652
653 size_t numMappers = mMappers.size();
654 for (size_t i = 0; i < numMappers; i++) {
655 InputMapper* mapper = mMappers[i];
656 mapper->dump(dump);
657 }
658}
659
Jeff Browne57e8952010-07-23 21:28:06 -0700660void InputDevice::addMapper(InputMapper* mapper) {
661 mMappers.add(mapper);
662}
663
664void InputDevice::configure() {
Jeff Brown38a7fab2010-08-30 03:02:23 -0700665 if (! isIgnored()) {
666 mContext->getPolicy()->getInputDeviceCalibration(mName, mCalibration);
667 }
668
Jeff Browne57e8952010-07-23 21:28:06 -0700669 mSources = 0;
670
671 size_t numMappers = mMappers.size();
672 for (size_t i = 0; i < numMappers; i++) {
673 InputMapper* mapper = mMappers[i];
674 mapper->configure();
675 mSources |= mapper->getSources();
Jeff Browne839a582010-04-22 18:58:52 -0700676 }
677}
678
Jeff Browne57e8952010-07-23 21:28:06 -0700679void InputDevice::reset() {
680 size_t numMappers = mMappers.size();
681 for (size_t i = 0; i < numMappers; i++) {
682 InputMapper* mapper = mMappers[i];
683 mapper->reset();
684 }
685}
Jeff Browne839a582010-04-22 18:58:52 -0700686
Jeff Browne57e8952010-07-23 21:28:06 -0700687void InputDevice::process(const RawEvent* rawEvent) {
688 size_t numMappers = mMappers.size();
689 for (size_t i = 0; i < numMappers; i++) {
690 InputMapper* mapper = mMappers[i];
691 mapper->process(rawEvent);
692 }
693}
Jeff Browne839a582010-04-22 18:58:52 -0700694
Jeff Browne57e8952010-07-23 21:28:06 -0700695void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
696 outDeviceInfo->initialize(mId, mName);
697
698 size_t numMappers = mMappers.size();
699 for (size_t i = 0; i < numMappers; i++) {
700 InputMapper* mapper = mMappers[i];
701 mapper->populateDeviceInfo(outDeviceInfo);
702 }
703}
704
705int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
706 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
707}
708
709int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
710 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
711}
712
713int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
714 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
715}
716
717int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
718 int32_t result = AKEY_STATE_UNKNOWN;
719 size_t numMappers = mMappers.size();
720 for (size_t i = 0; i < numMappers; i++) {
721 InputMapper* mapper = mMappers[i];
722 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
723 result = (mapper->*getStateFunc)(sourceMask, code);
724 if (result >= AKEY_STATE_DOWN) {
725 return result;
726 }
727 }
728 }
729 return result;
730}
731
732bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
733 const int32_t* keyCodes, uint8_t* outFlags) {
734 bool result = false;
735 size_t numMappers = mMappers.size();
736 for (size_t i = 0; i < numMappers; i++) {
737 InputMapper* mapper = mMappers[i];
738 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
739 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
740 }
741 }
742 return result;
743}
744
745int32_t InputDevice::getMetaState() {
746 int32_t result = 0;
747 size_t numMappers = mMappers.size();
748 for (size_t i = 0; i < numMappers; i++) {
749 InputMapper* mapper = mMappers[i];
750 result |= mapper->getMetaState();
751 }
752 return result;
753}
754
755
756// --- InputMapper ---
757
758InputMapper::InputMapper(InputDevice* device) :
759 mDevice(device), mContext(device->getContext()) {
760}
761
762InputMapper::~InputMapper() {
763}
764
765void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
766 info->addSource(getSources());
767}
768
Jeff Brown26c94ff2010-09-30 14:33:04 -0700769void InputMapper::dump(String8& dump) {
770}
771
Jeff Browne57e8952010-07-23 21:28:06 -0700772void InputMapper::configure() {
773}
774
775void InputMapper::reset() {
776}
777
778int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
779 return AKEY_STATE_UNKNOWN;
780}
781
782int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
783 return AKEY_STATE_UNKNOWN;
784}
785
786int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
787 return AKEY_STATE_UNKNOWN;
788}
789
790bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
791 const int32_t* keyCodes, uint8_t* outFlags) {
792 return false;
793}
794
795int32_t InputMapper::getMetaState() {
796 return 0;
797}
798
Jeff Browne57e8952010-07-23 21:28:06 -0700799
800// --- SwitchInputMapper ---
801
802SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
803 InputMapper(device) {
804}
805
806SwitchInputMapper::~SwitchInputMapper() {
807}
808
809uint32_t SwitchInputMapper::getSources() {
810 return 0;
811}
812
813void SwitchInputMapper::process(const RawEvent* rawEvent) {
814 switch (rawEvent->type) {
815 case EV_SW:
816 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
817 break;
818 }
819}
820
821void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brown90f0cee2010-10-08 22:31:17 -0700822 getDispatcher()->notifySwitch(when, switchCode, switchValue, 0);
Jeff Browne57e8952010-07-23 21:28:06 -0700823}
824
825int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
826 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
827}
828
829
830// --- KeyboardInputMapper ---
831
832KeyboardInputMapper::KeyboardInputMapper(InputDevice* device, int32_t associatedDisplayId,
833 uint32_t sources, int32_t keyboardType) :
834 InputMapper(device), mAssociatedDisplayId(associatedDisplayId), mSources(sources),
835 mKeyboardType(keyboardType) {
Jeff Brownb51719b2010-07-29 18:18:33 -0700836 initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700837}
838
839KeyboardInputMapper::~KeyboardInputMapper() {
840}
841
Jeff Brownb51719b2010-07-29 18:18:33 -0700842void KeyboardInputMapper::initializeLocked() {
843 mLocked.metaState = AMETA_NONE;
844 mLocked.downTime = 0;
Jeff Browne57e8952010-07-23 21:28:06 -0700845}
846
847uint32_t KeyboardInputMapper::getSources() {
848 return mSources;
849}
850
851void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
852 InputMapper::populateDeviceInfo(info);
853
854 info->setKeyboardType(mKeyboardType);
855}
856
Jeff Brown26c94ff2010-09-30 14:33:04 -0700857void KeyboardInputMapper::dump(String8& dump) {
858 { // acquire lock
859 AutoMutex _l(mLock);
860 dump.append(INDENT2 "Keyboard Input Mapper:\n");
861 dump.appendFormat(INDENT3 "AssociatedDisplayId: %d\n", mAssociatedDisplayId);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700862 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
863 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mLocked.keyDowns.size());
864 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mLocked.metaState);
865 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
866 } // release lock
867}
868
Jeff Browne57e8952010-07-23 21:28:06 -0700869void KeyboardInputMapper::reset() {
Jeff Brownb51719b2010-07-29 18:18:33 -0700870 for (;;) {
871 int32_t keyCode, scanCode;
872 { // acquire lock
873 AutoMutex _l(mLock);
874
875 // Synthesize key up event on reset if keys are currently down.
876 if (mLocked.keyDowns.isEmpty()) {
877 initializeLocked();
878 break; // done
879 }
880
881 const KeyDown& keyDown = mLocked.keyDowns.top();
882 keyCode = keyDown.keyCode;
883 scanCode = keyDown.scanCode;
884 } // release lock
885
Jeff Browne57e8952010-07-23 21:28:06 -0700886 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownb51719b2010-07-29 18:18:33 -0700887 processKey(when, false, keyCode, scanCode, 0);
Jeff Browne57e8952010-07-23 21:28:06 -0700888 }
889
890 InputMapper::reset();
Jeff Browne57e8952010-07-23 21:28:06 -0700891 getContext()->updateGlobalMetaState();
892}
893
894void KeyboardInputMapper::process(const RawEvent* rawEvent) {
895 switch (rawEvent->type) {
896 case EV_KEY: {
897 int32_t scanCode = rawEvent->scanCode;
898 if (isKeyboardOrGamepadKey(scanCode)) {
899 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
900 rawEvent->flags);
901 }
902 break;
903 }
904 }
905}
906
907bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
908 return scanCode < BTN_MOUSE
909 || scanCode >= KEY_OK
910 || (scanCode >= BTN_GAMEPAD && scanCode < BTN_DIGI);
911}
912
Jeff Brownb51719b2010-07-29 18:18:33 -0700913void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
914 int32_t scanCode, uint32_t policyFlags) {
915 int32_t newMetaState;
916 nsecs_t downTime;
917 bool metaStateChanged = false;
918
919 { // acquire lock
920 AutoMutex _l(mLock);
921
922 if (down) {
923 // Rotate key codes according to orientation if needed.
924 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
925 if (mAssociatedDisplayId >= 0) {
926 int32_t orientation;
927 if (! getPolicy()->getDisplayInfo(mAssociatedDisplayId, NULL, NULL, & orientation)) {
928 return;
929 }
930
931 keyCode = rotateKeyCode(keyCode, orientation);
Jeff Browne57e8952010-07-23 21:28:06 -0700932 }
933
Jeff Brownb51719b2010-07-29 18:18:33 -0700934 // Add key down.
935 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
936 if (keyDownIndex >= 0) {
937 // key repeat, be sure to use same keycode as before in case of rotation
938 keyCode = mLocked.keyDowns.top().keyCode;
939 } else {
940 // key down
941 mLocked.keyDowns.push();
942 KeyDown& keyDown = mLocked.keyDowns.editTop();
943 keyDown.keyCode = keyCode;
944 keyDown.scanCode = scanCode;
945 }
946
947 mLocked.downTime = when;
948 } else {
949 // Remove key down.
950 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
951 if (keyDownIndex >= 0) {
952 // key up, be sure to use same keycode as before in case of rotation
953 keyCode = mLocked.keyDowns.top().keyCode;
954 mLocked.keyDowns.removeAt(size_t(keyDownIndex));
955 } else {
956 // key was not actually down
957 LOGI("Dropping key up from device %s because the key was not down. "
958 "keyCode=%d, scanCode=%d",
959 getDeviceName().string(), keyCode, scanCode);
960 return;
961 }
Jeff Browne57e8952010-07-23 21:28:06 -0700962 }
963
Jeff Brownb51719b2010-07-29 18:18:33 -0700964 int32_t oldMetaState = mLocked.metaState;
965 newMetaState = updateMetaState(keyCode, down, oldMetaState);
966 if (oldMetaState != newMetaState) {
967 mLocked.metaState = newMetaState;
968 metaStateChanged = true;
Jeff Browne57e8952010-07-23 21:28:06 -0700969 }
Jeff Brown8575a872010-06-30 16:10:35 -0700970
Jeff Brownb51719b2010-07-29 18:18:33 -0700971 downTime = mLocked.downTime;
972 } // release lock
973
974 if (metaStateChanged) {
Jeff Browne57e8952010-07-23 21:28:06 -0700975 getContext()->updateGlobalMetaState();
Jeff Browne839a582010-04-22 18:58:52 -0700976 }
977
Jeff Browne57e8952010-07-23 21:28:06 -0700978 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
Jeff Brown90f0cee2010-10-08 22:31:17 -0700979 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
980 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Browne839a582010-04-22 18:58:52 -0700981}
982
Jeff Brownb51719b2010-07-29 18:18:33 -0700983ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) {
984 size_t n = mLocked.keyDowns.size();
Jeff Browne57e8952010-07-23 21:28:06 -0700985 for (size_t i = 0; i < n; i++) {
Jeff Brownb51719b2010-07-29 18:18:33 -0700986 if (mLocked.keyDowns[i].scanCode == scanCode) {
Jeff Browne57e8952010-07-23 21:28:06 -0700987 return i;
988 }
989 }
990 return -1;
Jeff Browne839a582010-04-22 18:58:52 -0700991}
992
Jeff Browne57e8952010-07-23 21:28:06 -0700993int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
994 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
995}
Jeff Browne839a582010-04-22 18:58:52 -0700996
Jeff Browne57e8952010-07-23 21:28:06 -0700997int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
998 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
999}
Jeff Browne839a582010-04-22 18:58:52 -07001000
Jeff Browne57e8952010-07-23 21:28:06 -07001001bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1002 const int32_t* keyCodes, uint8_t* outFlags) {
1003 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1004}
1005
1006int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownb51719b2010-07-29 18:18:33 -07001007 { // acquire lock
1008 AutoMutex _l(mLock);
1009 return mLocked.metaState;
1010 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07001011}
1012
1013
1014// --- TrackballInputMapper ---
1015
1016TrackballInputMapper::TrackballInputMapper(InputDevice* device, int32_t associatedDisplayId) :
1017 InputMapper(device), mAssociatedDisplayId(associatedDisplayId) {
1018 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1019 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1020 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1021 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1022
Jeff Brownb51719b2010-07-29 18:18:33 -07001023 initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -07001024}
1025
1026TrackballInputMapper::~TrackballInputMapper() {
1027}
1028
1029uint32_t TrackballInputMapper::getSources() {
1030 return AINPUT_SOURCE_TRACKBALL;
1031}
1032
1033void TrackballInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1034 InputMapper::populateDeviceInfo(info);
1035
1036 info->addMotionRange(AINPUT_MOTION_RANGE_X, -1.0f, 1.0f, 0.0f, mXScale);
1037 info->addMotionRange(AINPUT_MOTION_RANGE_Y, -1.0f, 1.0f, 0.0f, mYScale);
1038}
1039
Jeff Brown26c94ff2010-09-30 14:33:04 -07001040void TrackballInputMapper::dump(String8& dump) {
1041 { // acquire lock
1042 AutoMutex _l(mLock);
1043 dump.append(INDENT2 "Trackball Input Mapper:\n");
1044 dump.appendFormat(INDENT3 "AssociatedDisplayId: %d\n", mAssociatedDisplayId);
1045 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1046 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
1047 dump.appendFormat(INDENT3 "Down: %s\n", toString(mLocked.down));
1048 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1049 } // release lock
1050}
1051
Jeff Brownb51719b2010-07-29 18:18:33 -07001052void TrackballInputMapper::initializeLocked() {
Jeff Browne57e8952010-07-23 21:28:06 -07001053 mAccumulator.clear();
1054
Jeff Brownb51719b2010-07-29 18:18:33 -07001055 mLocked.down = false;
1056 mLocked.downTime = 0;
Jeff Browne57e8952010-07-23 21:28:06 -07001057}
1058
1059void TrackballInputMapper::reset() {
Jeff Brownb51719b2010-07-29 18:18:33 -07001060 for (;;) {
1061 { // acquire lock
1062 AutoMutex _l(mLock);
1063
1064 if (! mLocked.down) {
1065 initializeLocked();
1066 break; // done
1067 }
1068 } // release lock
1069
1070 // Synthesize trackball button up event on reset.
Jeff Browne57e8952010-07-23 21:28:06 -07001071 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownb51719b2010-07-29 18:18:33 -07001072 mAccumulator.fields = Accumulator::FIELD_BTN_MOUSE;
Jeff Browne57e8952010-07-23 21:28:06 -07001073 mAccumulator.btnMouse = false;
1074 sync(when);
Jeff Browne839a582010-04-22 18:58:52 -07001075 }
1076
Jeff Browne57e8952010-07-23 21:28:06 -07001077 InputMapper::reset();
Jeff Browne57e8952010-07-23 21:28:06 -07001078}
Jeff Browne839a582010-04-22 18:58:52 -07001079
Jeff Browne57e8952010-07-23 21:28:06 -07001080void TrackballInputMapper::process(const RawEvent* rawEvent) {
1081 switch (rawEvent->type) {
1082 case EV_KEY:
1083 switch (rawEvent->scanCode) {
1084 case BTN_MOUSE:
1085 mAccumulator.fields |= Accumulator::FIELD_BTN_MOUSE;
1086 mAccumulator.btnMouse = rawEvent->value != 0;
Jeff Brownd64c8552010-08-17 20:38:35 -07001087 // Sync now since BTN_MOUSE is not necessarily followed by SYN_REPORT and
1088 // we need to ensure that we report the up/down promptly.
Jeff Browne57e8952010-07-23 21:28:06 -07001089 sync(rawEvent->when);
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 break;
Jeff Browne839a582010-04-22 18:58:52 -07001093
Jeff Browne57e8952010-07-23 21:28:06 -07001094 case EV_REL:
1095 switch (rawEvent->scanCode) {
1096 case REL_X:
1097 mAccumulator.fields |= Accumulator::FIELD_REL_X;
1098 mAccumulator.relX = rawEvent->value;
1099 break;
1100 case REL_Y:
1101 mAccumulator.fields |= Accumulator::FIELD_REL_Y;
1102 mAccumulator.relY = rawEvent->value;
1103 break;
Jeff Browne839a582010-04-22 18:58:52 -07001104 }
Jeff Browne57e8952010-07-23 21:28:06 -07001105 break;
Jeff Browne839a582010-04-22 18:58:52 -07001106
Jeff Browne57e8952010-07-23 21:28:06 -07001107 case EV_SYN:
1108 switch (rawEvent->scanCode) {
1109 case SYN_REPORT:
Jeff Brownd64c8552010-08-17 20:38:35 -07001110 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07001111 break;
Jeff Browne839a582010-04-22 18:58:52 -07001112 }
Jeff Browne57e8952010-07-23 21:28:06 -07001113 break;
Jeff Browne839a582010-04-22 18:58:52 -07001114 }
Jeff Browne839a582010-04-22 18:58:52 -07001115}
1116
Jeff Browne57e8952010-07-23 21:28:06 -07001117void TrackballInputMapper::sync(nsecs_t when) {
Jeff Brownd64c8552010-08-17 20:38:35 -07001118 uint32_t fields = mAccumulator.fields;
1119 if (fields == 0) {
1120 return; // no new state changes, so nothing to do
1121 }
1122
Jeff Brownb51719b2010-07-29 18:18:33 -07001123 int motionEventAction;
1124 PointerCoords pointerCoords;
1125 nsecs_t downTime;
1126 { // acquire lock
1127 AutoMutex _l(mLock);
Jeff Browne839a582010-04-22 18:58:52 -07001128
Jeff Brownb51719b2010-07-29 18:18:33 -07001129 bool downChanged = fields & Accumulator::FIELD_BTN_MOUSE;
1130
1131 if (downChanged) {
1132 if (mAccumulator.btnMouse) {
1133 mLocked.down = true;
1134 mLocked.downTime = when;
1135 } else {
1136 mLocked.down = false;
1137 }
Jeff Browne57e8952010-07-23 21:28:06 -07001138 }
Jeff Browne839a582010-04-22 18:58:52 -07001139
Jeff Brownb51719b2010-07-29 18:18:33 -07001140 downTime = mLocked.downTime;
1141 float x = fields & Accumulator::FIELD_REL_X ? mAccumulator.relX * mXScale : 0.0f;
1142 float y = fields & Accumulator::FIELD_REL_Y ? mAccumulator.relY * mYScale : 0.0f;
Jeff Browne839a582010-04-22 18:58:52 -07001143
Jeff Brownb51719b2010-07-29 18:18:33 -07001144 if (downChanged) {
1145 motionEventAction = mLocked.down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
Jeff Browne57e8952010-07-23 21:28:06 -07001146 } else {
Jeff Brownb51719b2010-07-29 18:18:33 -07001147 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browne57e8952010-07-23 21:28:06 -07001148 }
Jeff Browne839a582010-04-22 18:58:52 -07001149
Jeff Brownb51719b2010-07-29 18:18:33 -07001150 pointerCoords.x = x;
1151 pointerCoords.y = y;
1152 pointerCoords.pressure = mLocked.down ? 1.0f : 0.0f;
1153 pointerCoords.size = 0;
1154 pointerCoords.touchMajor = 0;
1155 pointerCoords.touchMinor = 0;
1156 pointerCoords.toolMajor = 0;
1157 pointerCoords.toolMinor = 0;
1158 pointerCoords.orientation = 0;
Jeff Browne839a582010-04-22 18:58:52 -07001159
Jeff Brownb51719b2010-07-29 18:18:33 -07001160 if (mAssociatedDisplayId >= 0 && (x != 0.0f || y != 0.0f)) {
1161 // Rotate motion based on display orientation if needed.
1162 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1163 int32_t orientation;
1164 if (! getPolicy()->getDisplayInfo(mAssociatedDisplayId, NULL, NULL, & orientation)) {
1165 return;
1166 }
1167
1168 float temp;
1169 switch (orientation) {
1170 case InputReaderPolicyInterface::ROTATION_90:
1171 temp = pointerCoords.x;
1172 pointerCoords.x = pointerCoords.y;
1173 pointerCoords.y = - temp;
1174 break;
1175
1176 case InputReaderPolicyInterface::ROTATION_180:
1177 pointerCoords.x = - pointerCoords.x;
1178 pointerCoords.y = - pointerCoords.y;
1179 break;
1180
1181 case InputReaderPolicyInterface::ROTATION_270:
1182 temp = pointerCoords.x;
1183 pointerCoords.x = - pointerCoords.y;
1184 pointerCoords.y = temp;
1185 break;
1186 }
1187 }
1188 } // release lock
1189
Jeff Browne57e8952010-07-23 21:28:06 -07001190 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownb51719b2010-07-29 18:18:33 -07001191 int32_t pointerId = 0;
Jeff Brown90f0cee2010-10-08 22:31:17 -07001192 getDispatcher()->notifyMotion(when, getDeviceId(), AINPUT_SOURCE_TRACKBALL, 0,
Jeff Brownaf30ff62010-09-01 17:01:00 -07001193 motionEventAction, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown90f0cee2010-10-08 22:31:17 -07001194 1, &pointerId, &pointerCoords, mXPrecision, mYPrecision, downTime);
1195
1196 mAccumulator.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07001197}
1198
Jeff Brown8d4dfd22010-08-10 15:47:53 -07001199int32_t TrackballInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1200 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
1201 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1202 } else {
1203 return AKEY_STATE_UNKNOWN;
1204 }
1205}
1206
Jeff Browne57e8952010-07-23 21:28:06 -07001207
1208// --- TouchInputMapper ---
1209
1210TouchInputMapper::TouchInputMapper(InputDevice* device, int32_t associatedDisplayId) :
Jeff Brownb51719b2010-07-29 18:18:33 -07001211 InputMapper(device), mAssociatedDisplayId(associatedDisplayId) {
1212 mLocked.surfaceOrientation = -1;
1213 mLocked.surfaceWidth = -1;
1214 mLocked.surfaceHeight = -1;
1215
1216 initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -07001217}
1218
1219TouchInputMapper::~TouchInputMapper() {
1220}
1221
1222uint32_t TouchInputMapper::getSources() {
1223 return mAssociatedDisplayId >= 0 ? AINPUT_SOURCE_TOUCHSCREEN : AINPUT_SOURCE_TOUCHPAD;
1224}
1225
1226void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1227 InputMapper::populateDeviceInfo(info);
1228
Jeff Brownb51719b2010-07-29 18:18:33 -07001229 { // acquire lock
1230 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07001231
Jeff Brownb51719b2010-07-29 18:18:33 -07001232 // Ensure surface information is up to date so that orientation changes are
1233 // noticed immediately.
1234 configureSurfaceLocked();
1235
1236 info->addMotionRange(AINPUT_MOTION_RANGE_X, mLocked.orientedRanges.x);
1237 info->addMotionRange(AINPUT_MOTION_RANGE_Y, mLocked.orientedRanges.y);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001238
1239 if (mLocked.orientedRanges.havePressure) {
1240 info->addMotionRange(AINPUT_MOTION_RANGE_PRESSURE,
1241 mLocked.orientedRanges.pressure);
1242 }
1243
1244 if (mLocked.orientedRanges.haveSize) {
1245 info->addMotionRange(AINPUT_MOTION_RANGE_SIZE,
1246 mLocked.orientedRanges.size);
1247 }
1248
1249 if (mLocked.orientedRanges.haveTouchArea) {
1250 info->addMotionRange(AINPUT_MOTION_RANGE_TOUCH_MAJOR,
1251 mLocked.orientedRanges.touchMajor);
1252 info->addMotionRange(AINPUT_MOTION_RANGE_TOUCH_MINOR,
1253 mLocked.orientedRanges.touchMinor);
1254 }
1255
1256 if (mLocked.orientedRanges.haveToolArea) {
1257 info->addMotionRange(AINPUT_MOTION_RANGE_TOOL_MAJOR,
1258 mLocked.orientedRanges.toolMajor);
1259 info->addMotionRange(AINPUT_MOTION_RANGE_TOOL_MINOR,
1260 mLocked.orientedRanges.toolMinor);
1261 }
1262
1263 if (mLocked.orientedRanges.haveOrientation) {
1264 info->addMotionRange(AINPUT_MOTION_RANGE_ORIENTATION,
1265 mLocked.orientedRanges.orientation);
1266 }
Jeff Brownb51719b2010-07-29 18:18:33 -07001267 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07001268}
1269
Jeff Brown26c94ff2010-09-30 14:33:04 -07001270void TouchInputMapper::dump(String8& dump) {
1271 { // acquire lock
1272 AutoMutex _l(mLock);
1273 dump.append(INDENT2 "Touch Input Mapper:\n");
1274 dump.appendFormat(INDENT3 "AssociatedDisplayId: %d\n", mAssociatedDisplayId);
1275 dumpParameters(dump);
1276 dumpVirtualKeysLocked(dump);
1277 dumpRawAxes(dump);
1278 dumpCalibration(dump);
1279 dumpSurfaceLocked(dump);
1280 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mLocked.xPrecision);
1281 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mLocked.yPrecision);
1282 } // release lock
1283}
1284
Jeff Brownb51719b2010-07-29 18:18:33 -07001285void TouchInputMapper::initializeLocked() {
1286 mCurrentTouch.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07001287 mLastTouch.clear();
1288 mDownTime = 0;
Jeff Browne57e8952010-07-23 21:28:06 -07001289
1290 for (uint32_t i = 0; i < MAX_POINTERS; i++) {
1291 mAveragingTouchFilter.historyStart[i] = 0;
1292 mAveragingTouchFilter.historyEnd[i] = 0;
1293 }
1294
1295 mJumpyTouchFilter.jumpyPointsDropped = 0;
Jeff Brownb51719b2010-07-29 18:18:33 -07001296
1297 mLocked.currentVirtualKey.down = false;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001298
1299 mLocked.orientedRanges.havePressure = false;
1300 mLocked.orientedRanges.haveSize = false;
1301 mLocked.orientedRanges.haveTouchArea = false;
1302 mLocked.orientedRanges.haveToolArea = false;
1303 mLocked.orientedRanges.haveOrientation = false;
1304}
1305
Jeff Browne57e8952010-07-23 21:28:06 -07001306void TouchInputMapper::configure() {
1307 InputMapper::configure();
1308
1309 // Configure basic parameters.
Jeff Brown38a7fab2010-08-30 03:02:23 -07001310 configureParameters();
Jeff Browne57e8952010-07-23 21:28:06 -07001311
1312 // Configure absolute axis information.
Jeff Brown38a7fab2010-08-30 03:02:23 -07001313 configureRawAxes();
Jeff Brown38a7fab2010-08-30 03:02:23 -07001314
1315 // Prepare input device calibration.
1316 parseCalibration();
1317 resolveCalibration();
Jeff Browne57e8952010-07-23 21:28:06 -07001318
Jeff Brownb51719b2010-07-29 18:18:33 -07001319 { // acquire lock
1320 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07001321
Jeff Brown38a7fab2010-08-30 03:02:23 -07001322 // Configure surface dimensions and orientation.
Jeff Brownb51719b2010-07-29 18:18:33 -07001323 configureSurfaceLocked();
1324 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07001325}
1326
Jeff Brown38a7fab2010-08-30 03:02:23 -07001327void TouchInputMapper::configureParameters() {
1328 mParameters.useBadTouchFilter = getPolicy()->filterTouchEvents();
1329 mParameters.useAveragingTouchFilter = getPolicy()->filterTouchEvents();
1330 mParameters.useJumpyTouchFilter = getPolicy()->filterJumpyTouchEvents();
1331}
1332
Jeff Brown26c94ff2010-09-30 14:33:04 -07001333void TouchInputMapper::dumpParameters(String8& dump) {
1334 dump.appendFormat(INDENT3 "UseBadTouchFilter: %s\n",
1335 toString(mParameters.useBadTouchFilter));
1336 dump.appendFormat(INDENT3 "UseAveragingTouchFilter: %s\n",
1337 toString(mParameters.useAveragingTouchFilter));
1338 dump.appendFormat(INDENT3 "UseJumpyTouchFilter: %s\n",
1339 toString(mParameters.useJumpyTouchFilter));
Jeff Browna665ca82010-09-08 11:49:43 -07001340}
1341
Jeff Brown38a7fab2010-08-30 03:02:23 -07001342void TouchInputMapper::configureRawAxes() {
1343 mRawAxes.x.clear();
1344 mRawAxes.y.clear();
1345 mRawAxes.pressure.clear();
1346 mRawAxes.touchMajor.clear();
1347 mRawAxes.touchMinor.clear();
1348 mRawAxes.toolMajor.clear();
1349 mRawAxes.toolMinor.clear();
1350 mRawAxes.orientation.clear();
1351}
1352
Jeff Brown26c94ff2010-09-30 14:33:04 -07001353static void dumpAxisInfo(String8& dump, RawAbsoluteAxisInfo axis, const char* name) {
Jeff Browna665ca82010-09-08 11:49:43 -07001354 if (axis.valid) {
Jeff Brown26c94ff2010-09-30 14:33:04 -07001355 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d\n",
Jeff Browna665ca82010-09-08 11:49:43 -07001356 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz);
1357 } else {
Jeff Brown26c94ff2010-09-30 14:33:04 -07001358 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
Jeff Browna665ca82010-09-08 11:49:43 -07001359 }
1360}
1361
Jeff Brown26c94ff2010-09-30 14:33:04 -07001362void TouchInputMapper::dumpRawAxes(String8& dump) {
1363 dump.append(INDENT3 "Raw Axes:\n");
1364 dumpAxisInfo(dump, mRawAxes.x, "X");
1365 dumpAxisInfo(dump, mRawAxes.y, "Y");
1366 dumpAxisInfo(dump, mRawAxes.pressure, "Pressure");
1367 dumpAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor");
1368 dumpAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor");
1369 dumpAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor");
1370 dumpAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor");
1371 dumpAxisInfo(dump, mRawAxes.orientation, "Orientation");
Jeff Browne57e8952010-07-23 21:28:06 -07001372}
1373
Jeff Brownb51719b2010-07-29 18:18:33 -07001374bool TouchInputMapper::configureSurfaceLocked() {
Jeff Browne57e8952010-07-23 21:28:06 -07001375 // Update orientation and dimensions if needed.
1376 int32_t orientation;
1377 int32_t width, height;
1378 if (mAssociatedDisplayId >= 0) {
Jeff Brownb51719b2010-07-29 18:18:33 -07001379 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Browne57e8952010-07-23 21:28:06 -07001380 if (! getPolicy()->getDisplayInfo(mAssociatedDisplayId, & width, & height, & orientation)) {
1381 return false;
1382 }
1383 } else {
1384 orientation = InputReaderPolicyInterface::ROTATION_0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001385 width = mRawAxes.x.getRange();
1386 height = mRawAxes.y.getRange();
Jeff Browne57e8952010-07-23 21:28:06 -07001387 }
1388
Jeff Brownb51719b2010-07-29 18:18:33 -07001389 bool orientationChanged = mLocked.surfaceOrientation != orientation;
Jeff Browne57e8952010-07-23 21:28:06 -07001390 if (orientationChanged) {
Jeff Brownb51719b2010-07-29 18:18:33 -07001391 mLocked.surfaceOrientation = orientation;
Jeff Browne57e8952010-07-23 21:28:06 -07001392 }
1393
Jeff Brownb51719b2010-07-29 18:18:33 -07001394 bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height;
Jeff Browne57e8952010-07-23 21:28:06 -07001395 if (sizeChanged) {
Jeff Brown26c94ff2010-09-30 14:33:04 -07001396 LOGI("Device reconfigured: id=0x%x, name=%s, display size is now %dx%d",
1397 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001398
Jeff Brownb51719b2010-07-29 18:18:33 -07001399 mLocked.surfaceWidth = width;
1400 mLocked.surfaceHeight = height;
Jeff Browne57e8952010-07-23 21:28:06 -07001401
Jeff Brown38a7fab2010-08-30 03:02:23 -07001402 // Configure X and Y factors.
1403 if (mRawAxes.x.valid && mRawAxes.y.valid) {
1404 mLocked.xOrigin = mRawAxes.x.minValue;
1405 mLocked.yOrigin = mRawAxes.y.minValue;
1406 mLocked.xScale = float(width) / mRawAxes.x.getRange();
1407 mLocked.yScale = float(height) / mRawAxes.y.getRange();
Jeff Brownb51719b2010-07-29 18:18:33 -07001408 mLocked.xPrecision = 1.0f / mLocked.xScale;
1409 mLocked.yPrecision = 1.0f / mLocked.yScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001410
Jeff Brownb51719b2010-07-29 18:18:33 -07001411 configureVirtualKeysLocked();
Jeff Browne57e8952010-07-23 21:28:06 -07001412 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001413 LOGW(INDENT "Touch device did not report support for X or Y axis!");
Jeff Brownb51719b2010-07-29 18:18:33 -07001414 mLocked.xOrigin = 0;
1415 mLocked.yOrigin = 0;
1416 mLocked.xScale = 1.0f;
1417 mLocked.yScale = 1.0f;
1418 mLocked.xPrecision = 1.0f;
1419 mLocked.yPrecision = 1.0f;
Jeff Browne57e8952010-07-23 21:28:06 -07001420 }
1421
Jeff Brown38a7fab2010-08-30 03:02:23 -07001422 // Scale factor for terms that are not oriented in a particular axis.
1423 // If the pixels are square then xScale == yScale otherwise we fake it
1424 // by choosing an average.
1425 mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale);
Jeff Browne57e8952010-07-23 21:28:06 -07001426
Jeff Brown38a7fab2010-08-30 03:02:23 -07001427 // Size of diagonal axis.
1428 float diagonalSize = pythag(width, height);
Jeff Browne57e8952010-07-23 21:28:06 -07001429
Jeff Brown38a7fab2010-08-30 03:02:23 -07001430 // TouchMajor and TouchMinor factors.
1431 if (mCalibration.touchAreaCalibration != Calibration::TOUCH_AREA_CALIBRATION_NONE) {
1432 mLocked.orientedRanges.haveTouchArea = true;
1433 mLocked.orientedRanges.touchMajor.min = 0;
1434 mLocked.orientedRanges.touchMajor.max = diagonalSize;
1435 mLocked.orientedRanges.touchMajor.flat = 0;
1436 mLocked.orientedRanges.touchMajor.fuzz = 0;
1437 mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor;
1438 }
Jeff Brownb51719b2010-07-29 18:18:33 -07001439
Jeff Brown38a7fab2010-08-30 03:02:23 -07001440 // ToolMajor and ToolMinor factors.
1441 if (mCalibration.toolAreaCalibration != Calibration::TOOL_AREA_CALIBRATION_NONE) {
1442 mLocked.toolAreaLinearScale = 0;
1443 mLocked.toolAreaLinearBias = 0;
1444 if (mCalibration.toolAreaCalibration == Calibration::TOOL_AREA_CALIBRATION_LINEAR) {
1445 if (mCalibration.haveToolAreaLinearScale) {
1446 mLocked.toolAreaLinearScale = mCalibration.toolAreaLinearScale;
1447 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1448 mLocked.toolAreaLinearScale = float(min(width, height))
1449 / mRawAxes.toolMajor.maxValue;
1450 }
1451
1452 if (mCalibration.haveToolAreaLinearBias) {
1453 mLocked.toolAreaLinearBias = mCalibration.toolAreaLinearBias;
1454 }
1455 }
1456
1457 mLocked.orientedRanges.haveToolArea = true;
1458 mLocked.orientedRanges.toolMajor.min = 0;
1459 mLocked.orientedRanges.toolMajor.max = diagonalSize;
1460 mLocked.orientedRanges.toolMajor.flat = 0;
1461 mLocked.orientedRanges.toolMajor.fuzz = 0;
1462 mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor;
1463 }
1464
1465 // Pressure factors.
1466 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
1467 RawAbsoluteAxisInfo rawPressureAxis;
1468 switch (mCalibration.pressureSource) {
1469 case Calibration::PRESSURE_SOURCE_PRESSURE:
1470 rawPressureAxis = mRawAxes.pressure;
1471 break;
1472 case Calibration::PRESSURE_SOURCE_TOUCH:
1473 rawPressureAxis = mRawAxes.touchMajor;
1474 break;
1475 default:
1476 rawPressureAxis.clear();
1477 }
1478
1479 mLocked.pressureScale = 0;
1480 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
1481 || mCalibration.pressureCalibration
1482 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
1483 if (mCalibration.havePressureScale) {
1484 mLocked.pressureScale = mCalibration.pressureScale;
1485 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
1486 mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue;
1487 }
1488 }
1489
1490 mLocked.orientedRanges.havePressure = true;
1491 mLocked.orientedRanges.pressure.min = 0;
1492 mLocked.orientedRanges.pressure.max = 1.0;
1493 mLocked.orientedRanges.pressure.flat = 0;
1494 mLocked.orientedRanges.pressure.fuzz = 0;
1495 }
1496
1497 // Size factors.
1498 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
1499 mLocked.sizeScale = 0;
1500 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
1501 if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1502 mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue;
1503 }
1504 }
1505
1506 mLocked.orientedRanges.haveSize = true;
1507 mLocked.orientedRanges.size.min = 0;
1508 mLocked.orientedRanges.size.max = 1.0;
1509 mLocked.orientedRanges.size.flat = 0;
1510 mLocked.orientedRanges.size.fuzz = 0;
1511 }
1512
1513 // Orientation
1514 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
1515 mLocked.orientationScale = 0;
1516 if (mCalibration.orientationCalibration
1517 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
1518 if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) {
1519 mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue;
1520 }
1521 }
1522
1523 mLocked.orientedRanges.orientation.min = - M_PI_2;
1524 mLocked.orientedRanges.orientation.max = M_PI_2;
1525 mLocked.orientedRanges.orientation.flat = 0;
1526 mLocked.orientedRanges.orientation.fuzz = 0;
1527 }
Jeff Browne57e8952010-07-23 21:28:06 -07001528 }
1529
1530 if (orientationChanged || sizeChanged) {
1531 // Compute oriented surface dimensions, precision, and scales.
1532 float orientedXScale, orientedYScale;
Jeff Brownb51719b2010-07-29 18:18:33 -07001533 switch (mLocked.surfaceOrientation) {
Jeff Browne57e8952010-07-23 21:28:06 -07001534 case InputReaderPolicyInterface::ROTATION_90:
1535 case InputReaderPolicyInterface::ROTATION_270:
Jeff Brownb51719b2010-07-29 18:18:33 -07001536 mLocked.orientedSurfaceWidth = mLocked.surfaceHeight;
1537 mLocked.orientedSurfaceHeight = mLocked.surfaceWidth;
1538 mLocked.orientedXPrecision = mLocked.yPrecision;
1539 mLocked.orientedYPrecision = mLocked.xPrecision;
1540 orientedXScale = mLocked.yScale;
1541 orientedYScale = mLocked.xScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001542 break;
1543 default:
Jeff Brownb51719b2010-07-29 18:18:33 -07001544 mLocked.orientedSurfaceWidth = mLocked.surfaceWidth;
1545 mLocked.orientedSurfaceHeight = mLocked.surfaceHeight;
1546 mLocked.orientedXPrecision = mLocked.xPrecision;
1547 mLocked.orientedYPrecision = mLocked.yPrecision;
1548 orientedXScale = mLocked.xScale;
1549 orientedYScale = mLocked.yScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001550 break;
1551 }
1552
1553 // Configure position ranges.
Jeff Brownb51719b2010-07-29 18:18:33 -07001554 mLocked.orientedRanges.x.min = 0;
1555 mLocked.orientedRanges.x.max = mLocked.orientedSurfaceWidth;
1556 mLocked.orientedRanges.x.flat = 0;
1557 mLocked.orientedRanges.x.fuzz = orientedXScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001558
Jeff Brownb51719b2010-07-29 18:18:33 -07001559 mLocked.orientedRanges.y.min = 0;
1560 mLocked.orientedRanges.y.max = mLocked.orientedSurfaceHeight;
1561 mLocked.orientedRanges.y.flat = 0;
1562 mLocked.orientedRanges.y.fuzz = orientedYScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001563 }
1564
1565 return true;
1566}
1567
Jeff Brown26c94ff2010-09-30 14:33:04 -07001568void TouchInputMapper::dumpSurfaceLocked(String8& dump) {
1569 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth);
1570 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight);
1571 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation);
Jeff Browna665ca82010-09-08 11:49:43 -07001572}
1573
Jeff Brownb51719b2010-07-29 18:18:33 -07001574void TouchInputMapper::configureVirtualKeysLocked() {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001575 assert(mRawAxes.x.valid && mRawAxes.y.valid);
Jeff Browne57e8952010-07-23 21:28:06 -07001576
Jeff Brownb51719b2010-07-29 18:18:33 -07001577 // Note: getVirtualKeyDefinitions is non-reentrant so we can continue holding the lock.
Jeff Brown38a7fab2010-08-30 03:02:23 -07001578 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Browne57e8952010-07-23 21:28:06 -07001579 getPolicy()->getVirtualKeyDefinitions(getDeviceName(), virtualKeyDefinitions);
1580
Jeff Brownb51719b2010-07-29 18:18:33 -07001581 mLocked.virtualKeys.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07001582
Jeff Brownb51719b2010-07-29 18:18:33 -07001583 if (virtualKeyDefinitions.size() == 0) {
1584 return;
1585 }
Jeff Browne57e8952010-07-23 21:28:06 -07001586
Jeff Brownb51719b2010-07-29 18:18:33 -07001587 mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size());
1588
Jeff Brown38a7fab2010-08-30 03:02:23 -07001589 int32_t touchScreenLeft = mRawAxes.x.minValue;
1590 int32_t touchScreenTop = mRawAxes.y.minValue;
1591 int32_t touchScreenWidth = mRawAxes.x.getRange();
1592 int32_t touchScreenHeight = mRawAxes.y.getRange();
Jeff Brownb51719b2010-07-29 18:18:33 -07001593
1594 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001595 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brownb51719b2010-07-29 18:18:33 -07001596 virtualKeyDefinitions[i];
1597
1598 mLocked.virtualKeys.add();
1599 VirtualKey& virtualKey = mLocked.virtualKeys.editTop();
1600
1601 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1602 int32_t keyCode;
1603 uint32_t flags;
1604 if (getEventHub()->scancodeToKeycode(getDeviceId(), virtualKey.scanCode,
1605 & keyCode, & flags)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001606 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
1607 virtualKey.scanCode);
Jeff Brownb51719b2010-07-29 18:18:33 -07001608 mLocked.virtualKeys.pop(); // drop the key
1609 continue;
Jeff Browne57e8952010-07-23 21:28:06 -07001610 }
1611
Jeff Brownb51719b2010-07-29 18:18:33 -07001612 virtualKey.keyCode = keyCode;
1613 virtualKey.flags = flags;
Jeff Browne57e8952010-07-23 21:28:06 -07001614
Jeff Brownb51719b2010-07-29 18:18:33 -07001615 // convert the key definition's display coordinates into touch coordinates for a hit box
1616 int32_t halfWidth = virtualKeyDefinition.width / 2;
1617 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Browne57e8952010-07-23 21:28:06 -07001618
Jeff Brownb51719b2010-07-29 18:18:33 -07001619 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
1620 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1621 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
1622 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1623 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
1624 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
1625 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
1626 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
Jeff Browne57e8952010-07-23 21:28:06 -07001627
Jeff Brown26c94ff2010-09-30 14:33:04 -07001628 }
1629}
1630
1631void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) {
1632 if (!mLocked.virtualKeys.isEmpty()) {
1633 dump.append(INDENT3 "Virtual Keys:\n");
1634
1635 for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) {
1636 const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i);
1637 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
1638 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1639 i, virtualKey.scanCode, virtualKey.keyCode,
1640 virtualKey.hitLeft, virtualKey.hitRight,
1641 virtualKey.hitTop, virtualKey.hitBottom);
1642 }
Jeff Brownb51719b2010-07-29 18:18:33 -07001643 }
Jeff Browne57e8952010-07-23 21:28:06 -07001644}
1645
Jeff Brown38a7fab2010-08-30 03:02:23 -07001646void TouchInputMapper::parseCalibration() {
1647 const InputDeviceCalibration& in = getDevice()->getCalibration();
1648 Calibration& out = mCalibration;
1649
1650 // Touch Area
1651 out.touchAreaCalibration = Calibration::TOUCH_AREA_CALIBRATION_DEFAULT;
1652 String8 touchAreaCalibrationString;
1653 if (in.tryGetProperty(String8("touch.touchArea.calibration"), touchAreaCalibrationString)) {
1654 if (touchAreaCalibrationString == "none") {
1655 out.touchAreaCalibration = Calibration::TOUCH_AREA_CALIBRATION_NONE;
1656 } else if (touchAreaCalibrationString == "geometric") {
1657 out.touchAreaCalibration = Calibration::TOUCH_AREA_CALIBRATION_GEOMETRIC;
1658 } else if (touchAreaCalibrationString == "pressure") {
1659 out.touchAreaCalibration = Calibration::TOUCH_AREA_CALIBRATION_PRESSURE;
1660 } else if (touchAreaCalibrationString != "default") {
1661 LOGW("Invalid value for touch.touchArea.calibration: '%s'",
1662 touchAreaCalibrationString.string());
1663 }
1664 }
1665
1666 // Tool Area
1667 out.toolAreaCalibration = Calibration::TOOL_AREA_CALIBRATION_DEFAULT;
1668 String8 toolAreaCalibrationString;
1669 if (in.tryGetProperty(String8("tool.toolArea.calibration"), toolAreaCalibrationString)) {
1670 if (toolAreaCalibrationString == "none") {
1671 out.toolAreaCalibration = Calibration::TOOL_AREA_CALIBRATION_NONE;
1672 } else if (toolAreaCalibrationString == "geometric") {
1673 out.toolAreaCalibration = Calibration::TOOL_AREA_CALIBRATION_GEOMETRIC;
1674 } else if (toolAreaCalibrationString == "linear") {
1675 out.toolAreaCalibration = Calibration::TOOL_AREA_CALIBRATION_LINEAR;
1676 } else if (toolAreaCalibrationString != "default") {
1677 LOGW("Invalid value for tool.toolArea.calibration: '%s'",
1678 toolAreaCalibrationString.string());
1679 }
1680 }
1681
1682 out.haveToolAreaLinearScale = in.tryGetProperty(String8("touch.toolArea.linearScale"),
1683 out.toolAreaLinearScale);
1684 out.haveToolAreaLinearBias = in.tryGetProperty(String8("touch.toolArea.linearBias"),
1685 out.toolAreaLinearBias);
1686 out.haveToolAreaIsSummed = in.tryGetProperty(String8("touch.toolArea.isSummed"),
1687 out.toolAreaIsSummed);
1688
1689 // Pressure
1690 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
1691 String8 pressureCalibrationString;
1692 if (in.tryGetProperty(String8("tool.pressure.calibration"), pressureCalibrationString)) {
1693 if (pressureCalibrationString == "none") {
1694 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
1695 } else if (pressureCalibrationString == "physical") {
1696 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
1697 } else if (pressureCalibrationString == "amplitude") {
1698 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
1699 } else if (pressureCalibrationString != "default") {
1700 LOGW("Invalid value for tool.pressure.calibration: '%s'",
1701 pressureCalibrationString.string());
1702 }
1703 }
1704
1705 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
1706 String8 pressureSourceString;
1707 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
1708 if (pressureSourceString == "pressure") {
1709 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
1710 } else if (pressureSourceString == "touch") {
1711 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
1712 } else if (pressureSourceString != "default") {
1713 LOGW("Invalid value for touch.pressure.source: '%s'",
1714 pressureSourceString.string());
1715 }
1716 }
1717
1718 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
1719 out.pressureScale);
1720
1721 // Size
1722 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
1723 String8 sizeCalibrationString;
1724 if (in.tryGetProperty(String8("tool.size.calibration"), sizeCalibrationString)) {
1725 if (sizeCalibrationString == "none") {
1726 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
1727 } else if (sizeCalibrationString == "normalized") {
1728 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
1729 } else if (sizeCalibrationString != "default") {
1730 LOGW("Invalid value for tool.size.calibration: '%s'",
1731 sizeCalibrationString.string());
1732 }
1733 }
1734
1735 // Orientation
1736 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
1737 String8 orientationCalibrationString;
1738 if (in.tryGetProperty(String8("tool.orientation.calibration"), orientationCalibrationString)) {
1739 if (orientationCalibrationString == "none") {
1740 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
1741 } else if (orientationCalibrationString == "interpolated") {
1742 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
1743 } else if (orientationCalibrationString != "default") {
1744 LOGW("Invalid value for tool.orientation.calibration: '%s'",
1745 orientationCalibrationString.string());
1746 }
1747 }
1748}
1749
1750void TouchInputMapper::resolveCalibration() {
1751 // Pressure
1752 switch (mCalibration.pressureSource) {
1753 case Calibration::PRESSURE_SOURCE_DEFAULT:
1754 if (mRawAxes.pressure.valid) {
1755 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
1756 } else if (mRawAxes.touchMajor.valid) {
1757 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
1758 }
1759 break;
1760
1761 case Calibration::PRESSURE_SOURCE_PRESSURE:
1762 if (! mRawAxes.pressure.valid) {
1763 LOGW("Calibration property touch.pressure.source is 'pressure' but "
1764 "the pressure axis is not available.");
1765 }
1766 break;
1767
1768 case Calibration::PRESSURE_SOURCE_TOUCH:
1769 if (! mRawAxes.touchMajor.valid) {
1770 LOGW("Calibration property touch.pressure.source is 'touch' but "
1771 "the touchMajor axis is not available.");
1772 }
1773 break;
1774
1775 default:
1776 break;
1777 }
1778
1779 switch (mCalibration.pressureCalibration) {
1780 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
1781 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
1782 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
1783 } else {
1784 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
1785 }
1786 break;
1787
1788 default:
1789 break;
1790 }
1791
1792 // Tool Area
1793 switch (mCalibration.toolAreaCalibration) {
1794 case Calibration::TOOL_AREA_CALIBRATION_DEFAULT:
1795 if (mRawAxes.toolMajor.valid) {
1796 mCalibration.toolAreaCalibration = Calibration::TOOL_AREA_CALIBRATION_LINEAR;
1797 } else {
1798 mCalibration.toolAreaCalibration = Calibration::TOOL_AREA_CALIBRATION_NONE;
1799 }
1800 break;
1801
1802 default:
1803 break;
1804 }
1805
1806 // Touch Area
1807 switch (mCalibration.touchAreaCalibration) {
1808 case Calibration::TOUCH_AREA_CALIBRATION_DEFAULT:
1809 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
1810 && mCalibration.toolAreaCalibration != Calibration::TOOL_AREA_CALIBRATION_NONE) {
1811 mCalibration.touchAreaCalibration = Calibration::TOUCH_AREA_CALIBRATION_PRESSURE;
1812 } else {
1813 mCalibration.touchAreaCalibration = Calibration::TOUCH_AREA_CALIBRATION_NONE;
1814 }
1815 break;
1816
1817 default:
1818 break;
1819 }
1820
1821 // Size
1822 switch (mCalibration.sizeCalibration) {
1823 case Calibration::SIZE_CALIBRATION_DEFAULT:
1824 if (mRawAxes.toolMajor.valid) {
1825 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
1826 } else {
1827 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
1828 }
1829 break;
1830
1831 default:
1832 break;
1833 }
1834
1835 // Orientation
1836 switch (mCalibration.orientationCalibration) {
1837 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
1838 if (mRawAxes.orientation.valid) {
1839 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
1840 } else {
1841 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
1842 }
1843 break;
1844
1845 default:
1846 break;
1847 }
1848}
1849
Jeff Brown26c94ff2010-09-30 14:33:04 -07001850void TouchInputMapper::dumpCalibration(String8& dump) {
1851 dump.append(INDENT3 "Calibration:\n");
Jeff Browna665ca82010-09-08 11:49:43 -07001852
Jeff Brown38a7fab2010-08-30 03:02:23 -07001853 // Touch Area
1854 switch (mCalibration.touchAreaCalibration) {
1855 case Calibration::TOUCH_AREA_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07001856 dump.append(INDENT4 "touch.touchArea.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001857 break;
1858 case Calibration::TOUCH_AREA_CALIBRATION_GEOMETRIC:
Jeff Brown26c94ff2010-09-30 14:33:04 -07001859 dump.append(INDENT4 "touch.touchArea.calibration: geometric\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001860 break;
1861 case Calibration::TOUCH_AREA_CALIBRATION_PRESSURE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07001862 dump.append(INDENT4 "touch.touchArea.calibration: pressure\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001863 break;
1864 default:
1865 assert(false);
1866 }
1867
1868 // Tool Area
1869 switch (mCalibration.toolAreaCalibration) {
1870 case Calibration::TOOL_AREA_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07001871 dump.append(INDENT4 "touch.toolArea.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001872 break;
1873 case Calibration::TOOL_AREA_CALIBRATION_GEOMETRIC:
Jeff Brown26c94ff2010-09-30 14:33:04 -07001874 dump.append(INDENT4 "touch.toolArea.calibration: geometric\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001875 break;
1876 case Calibration::TOOL_AREA_CALIBRATION_LINEAR:
Jeff Brown26c94ff2010-09-30 14:33:04 -07001877 dump.append(INDENT4 "touch.toolArea.calibration: linear\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001878 break;
1879 default:
1880 assert(false);
1881 }
1882
1883 if (mCalibration.haveToolAreaLinearScale) {
Jeff Brown26c94ff2010-09-30 14:33:04 -07001884 dump.appendFormat(INDENT4 "touch.toolArea.linearScale: %0.3f\n",
1885 mCalibration.toolAreaLinearScale);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001886 }
1887
1888 if (mCalibration.haveToolAreaLinearBias) {
Jeff Brown26c94ff2010-09-30 14:33:04 -07001889 dump.appendFormat(INDENT4 "touch.toolArea.linearBias: %0.3f\n",
1890 mCalibration.toolAreaLinearBias);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001891 }
1892
1893 if (mCalibration.haveToolAreaIsSummed) {
Jeff Brown26c94ff2010-09-30 14:33:04 -07001894 dump.appendFormat(INDENT4 "touch.toolArea.isSummed: %d\n",
1895 mCalibration.toolAreaIsSummed);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001896 }
1897
1898 // Pressure
1899 switch (mCalibration.pressureCalibration) {
1900 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07001901 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001902 break;
1903 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brown26c94ff2010-09-30 14:33:04 -07001904 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001905 break;
1906 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07001907 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001908 break;
1909 default:
1910 assert(false);
1911 }
1912
1913 switch (mCalibration.pressureSource) {
1914 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07001915 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001916 break;
1917 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brown26c94ff2010-09-30 14:33:04 -07001918 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001919 break;
1920 case Calibration::PRESSURE_SOURCE_DEFAULT:
1921 break;
1922 default:
1923 assert(false);
1924 }
1925
1926 if (mCalibration.havePressureScale) {
Jeff Brown26c94ff2010-09-30 14:33:04 -07001927 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
1928 mCalibration.pressureScale);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001929 }
1930
1931 // Size
1932 switch (mCalibration.sizeCalibration) {
1933 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07001934 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001935 break;
1936 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brown26c94ff2010-09-30 14:33:04 -07001937 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001938 break;
1939 default:
1940 assert(false);
1941 }
1942
1943 // Orientation
1944 switch (mCalibration.orientationCalibration) {
1945 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07001946 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001947 break;
1948 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brown26c94ff2010-09-30 14:33:04 -07001949 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001950 break;
1951 default:
1952 assert(false);
1953 }
1954}
1955
Jeff Browne57e8952010-07-23 21:28:06 -07001956void TouchInputMapper::reset() {
1957 // Synthesize touch up event if touch is currently down.
1958 // This will also take care of finishing virtual key processing if needed.
1959 if (mLastTouch.pointerCount != 0) {
1960 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
1961 mCurrentTouch.clear();
1962 syncTouch(when, true);
1963 }
1964
Jeff Brownb51719b2010-07-29 18:18:33 -07001965 { // acquire lock
1966 AutoMutex _l(mLock);
1967 initializeLocked();
1968 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07001969
Jeff Brownb51719b2010-07-29 18:18:33 -07001970 InputMapper::reset();
Jeff Browne57e8952010-07-23 21:28:06 -07001971}
1972
1973void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Browne839a582010-04-22 18:58:52 -07001974 uint32_t policyFlags = 0;
Jeff Browne839a582010-04-22 18:58:52 -07001975
Jeff Brownb51719b2010-07-29 18:18:33 -07001976 // Preprocess pointer data.
Jeff Browne839a582010-04-22 18:58:52 -07001977
Jeff Browne57e8952010-07-23 21:28:06 -07001978 if (mParameters.useBadTouchFilter) {
1979 if (applyBadTouchFilter()) {
Jeff Browne839a582010-04-22 18:58:52 -07001980 havePointerIds = false;
1981 }
1982 }
1983
Jeff Browne57e8952010-07-23 21:28:06 -07001984 if (mParameters.useJumpyTouchFilter) {
1985 if (applyJumpyTouchFilter()) {
Jeff Browne839a582010-04-22 18:58:52 -07001986 havePointerIds = false;
1987 }
1988 }
1989
1990 if (! havePointerIds) {
Jeff Browne57e8952010-07-23 21:28:06 -07001991 calculatePointerIds();
Jeff Browne839a582010-04-22 18:58:52 -07001992 }
1993
Jeff Browne57e8952010-07-23 21:28:06 -07001994 TouchData temp;
1995 TouchData* savedTouch;
1996 if (mParameters.useAveragingTouchFilter) {
1997 temp.copyFrom(mCurrentTouch);
Jeff Browne839a582010-04-22 18:58:52 -07001998 savedTouch = & temp;
1999
Jeff Browne57e8952010-07-23 21:28:06 -07002000 applyAveragingTouchFilter();
Jeff Browne839a582010-04-22 18:58:52 -07002001 } else {
Jeff Browne57e8952010-07-23 21:28:06 -07002002 savedTouch = & mCurrentTouch;
Jeff Browne839a582010-04-22 18:58:52 -07002003 }
2004
Jeff Brownb51719b2010-07-29 18:18:33 -07002005 // Process touches and virtual keys.
Jeff Browne839a582010-04-22 18:58:52 -07002006
Jeff Browne57e8952010-07-23 21:28:06 -07002007 TouchResult touchResult = consumeOffScreenTouches(when, policyFlags);
2008 if (touchResult == DISPATCH_TOUCH) {
2009 dispatchTouches(when, policyFlags);
Jeff Browne839a582010-04-22 18:58:52 -07002010 }
2011
Jeff Brownb51719b2010-07-29 18:18:33 -07002012 // Copy current touch to last touch in preparation for the next cycle.
Jeff Browne839a582010-04-22 18:58:52 -07002013
Jeff Browne57e8952010-07-23 21:28:06 -07002014 if (touchResult == DROP_STROKE) {
2015 mLastTouch.clear();
2016 } else {
2017 mLastTouch.copyFrom(*savedTouch);
Jeff Browne839a582010-04-22 18:58:52 -07002018 }
Jeff Browne839a582010-04-22 18:58:52 -07002019}
2020
Jeff Browne57e8952010-07-23 21:28:06 -07002021TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches(
2022 nsecs_t when, uint32_t policyFlags) {
2023 int32_t keyEventAction, keyEventFlags;
2024 int32_t keyCode, scanCode, downTime;
2025 TouchResult touchResult;
Jeff Brown50de30a2010-06-22 01:27:15 -07002026
Jeff Brownb51719b2010-07-29 18:18:33 -07002027 { // acquire lock
2028 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07002029
Jeff Brownb51719b2010-07-29 18:18:33 -07002030 // Update surface size and orientation, including virtual key positions.
2031 if (! configureSurfaceLocked()) {
2032 return DROP_STROKE;
2033 }
2034
2035 // Check for virtual key press.
2036 if (mLocked.currentVirtualKey.down) {
Jeff Browne57e8952010-07-23 21:28:06 -07002037 if (mCurrentTouch.pointerCount == 0) {
2038 // Pointer went up while virtual key was down.
Jeff Brownb51719b2010-07-29 18:18:33 -07002039 mLocked.currentVirtualKey.down = false;
Jeff Browne57e8952010-07-23 21:28:06 -07002040#if DEBUG_VIRTUAL_KEYS
2041 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
2042 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
2043#endif
2044 keyEventAction = AKEY_EVENT_ACTION_UP;
2045 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2046 touchResult = SKIP_TOUCH;
2047 goto DispatchVirtualKey;
2048 }
2049
2050 if (mCurrentTouch.pointerCount == 1) {
2051 int32_t x = mCurrentTouch.pointers[0].x;
2052 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brownb51719b2010-07-29 18:18:33 -07002053 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
2054 if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07002055 // Pointer is still within the space of the virtual key.
2056 return SKIP_TOUCH;
2057 }
2058 }
2059
2060 // Pointer left virtual key area or another pointer also went down.
2061 // Send key cancellation and drop the stroke so subsequent motions will be
2062 // considered fresh downs. This is useful when the user swipes away from the
2063 // virtual key area into the main display surface.
Jeff Brownb51719b2010-07-29 18:18:33 -07002064 mLocked.currentVirtualKey.down = false;
Jeff Browne57e8952010-07-23 21:28:06 -07002065#if DEBUG_VIRTUAL_KEYS
2066 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
2067 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
2068#endif
2069 keyEventAction = AKEY_EVENT_ACTION_UP;
2070 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
2071 | AKEY_EVENT_FLAG_CANCELED;
2072 touchResult = DROP_STROKE;
2073 goto DispatchVirtualKey;
2074 } else {
2075 if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) {
2076 // Pointer just went down. Handle off-screen touches, if needed.
2077 int32_t x = mCurrentTouch.pointers[0].x;
2078 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brownb51719b2010-07-29 18:18:33 -07002079 if (! isPointInsideSurfaceLocked(x, y)) {
Jeff Browne57e8952010-07-23 21:28:06 -07002080 // If exactly one pointer went down, check for virtual key hit.
2081 // Otherwise we will drop the entire stroke.
2082 if (mCurrentTouch.pointerCount == 1) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002083 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
Jeff Browne57e8952010-07-23 21:28:06 -07002084 if (virtualKey) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002085 mLocked.currentVirtualKey.down = true;
2086 mLocked.currentVirtualKey.downTime = when;
2087 mLocked.currentVirtualKey.keyCode = virtualKey->keyCode;
2088 mLocked.currentVirtualKey.scanCode = virtualKey->scanCode;
Jeff Browne57e8952010-07-23 21:28:06 -07002089#if DEBUG_VIRTUAL_KEYS
2090 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
2091 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
2092#endif
2093 keyEventAction = AKEY_EVENT_ACTION_DOWN;
2094 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM
2095 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2096 touchResult = SKIP_TOUCH;
2097 goto DispatchVirtualKey;
2098 }
2099 }
2100 return DROP_STROKE;
2101 }
2102 }
2103 return DISPATCH_TOUCH;
2104 }
2105
2106 DispatchVirtualKey:
2107 // Collect remaining state needed to dispatch virtual key.
Jeff Brownb51719b2010-07-29 18:18:33 -07002108 keyCode = mLocked.currentVirtualKey.keyCode;
2109 scanCode = mLocked.currentVirtualKey.scanCode;
2110 downTime = mLocked.currentVirtualKey.downTime;
2111 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07002112
2113 // Dispatch virtual key.
2114 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown956c0fb2010-10-01 14:55:30 -07002115 policyFlags |= POLICY_FLAG_VIRTUAL;
Jeff Brown90f0cee2010-10-08 22:31:17 -07002116 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
2117 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
2118 return touchResult;
Jeff Browne839a582010-04-22 18:58:52 -07002119}
2120
Jeff Browne57e8952010-07-23 21:28:06 -07002121void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
2122 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2123 uint32_t lastPointerCount = mLastTouch.pointerCount;
Jeff Browne839a582010-04-22 18:58:52 -07002124 if (currentPointerCount == 0 && lastPointerCount == 0) {
2125 return; // nothing to do!
2126 }
2127
Jeff Browne57e8952010-07-23 21:28:06 -07002128 BitSet32 currentIdBits = mCurrentTouch.idBits;
2129 BitSet32 lastIdBits = mLastTouch.idBits;
Jeff Browne839a582010-04-22 18:58:52 -07002130
2131 if (currentIdBits == lastIdBits) {
2132 // No pointer id changes so this is a move event.
2133 // The dispatcher takes care of batching moves so we don't have to deal with that here.
Jeff Brown5c1ed842010-07-14 18:48:53 -07002134 int32_t motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browne57e8952010-07-23 21:28:06 -07002135 dispatchTouch(when, policyFlags, & mCurrentTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002136 currentIdBits, -1, currentPointerCount, motionEventAction);
Jeff Browne839a582010-04-22 18:58:52 -07002137 } else {
2138 // There may be pointers going up and pointers going down at the same time when pointer
2139 // ids are reported by the device driver.
2140 BitSet32 upIdBits(lastIdBits.value & ~ currentIdBits.value);
2141 BitSet32 downIdBits(currentIdBits.value & ~ lastIdBits.value);
2142 BitSet32 activeIdBits(lastIdBits.value);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002143 uint32_t pointerCount = lastPointerCount;
Jeff Browne839a582010-04-22 18:58:52 -07002144
2145 while (! upIdBits.isEmpty()) {
2146 uint32_t upId = upIdBits.firstMarkedBit();
2147 upIdBits.clearBit(upId);
2148 BitSet32 oldActiveIdBits = activeIdBits;
2149 activeIdBits.clearBit(upId);
2150
2151 int32_t motionEventAction;
2152 if (activeIdBits.isEmpty()) {
Jeff Brown5c1ed842010-07-14 18:48:53 -07002153 motionEventAction = AMOTION_EVENT_ACTION_UP;
Jeff Browne839a582010-04-22 18:58:52 -07002154 } else {
Jeff Brown3cf1c9b2010-07-16 15:01:56 -07002155 motionEventAction = AMOTION_EVENT_ACTION_POINTER_UP;
Jeff Browne839a582010-04-22 18:58:52 -07002156 }
2157
Jeff Browne57e8952010-07-23 21:28:06 -07002158 dispatchTouch(when, policyFlags, & mLastTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002159 oldActiveIdBits, upId, pointerCount, motionEventAction);
2160 pointerCount -= 1;
Jeff Browne839a582010-04-22 18:58:52 -07002161 }
2162
2163 while (! downIdBits.isEmpty()) {
2164 uint32_t downId = downIdBits.firstMarkedBit();
2165 downIdBits.clearBit(downId);
2166 BitSet32 oldActiveIdBits = activeIdBits;
2167 activeIdBits.markBit(downId);
2168
2169 int32_t motionEventAction;
2170 if (oldActiveIdBits.isEmpty()) {
Jeff Brown5c1ed842010-07-14 18:48:53 -07002171 motionEventAction = AMOTION_EVENT_ACTION_DOWN;
Jeff Browne57e8952010-07-23 21:28:06 -07002172 mDownTime = when;
Jeff Browne839a582010-04-22 18:58:52 -07002173 } else {
Jeff Brown3cf1c9b2010-07-16 15:01:56 -07002174 motionEventAction = AMOTION_EVENT_ACTION_POINTER_DOWN;
Jeff Browne839a582010-04-22 18:58:52 -07002175 }
2176
Jeff Brown38a7fab2010-08-30 03:02:23 -07002177 pointerCount += 1;
Jeff Browne57e8952010-07-23 21:28:06 -07002178 dispatchTouch(when, policyFlags, & mCurrentTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002179 activeIdBits, downId, pointerCount, motionEventAction);
Jeff Browne839a582010-04-22 18:58:52 -07002180 }
2181 }
2182}
2183
Jeff Browne57e8952010-07-23 21:28:06 -07002184void TouchInputMapper::dispatchTouch(nsecs_t when, uint32_t policyFlags,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002185 TouchData* touch, BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
Jeff Browne839a582010-04-22 18:58:52 -07002186 int32_t motionEventAction) {
Jeff Browne839a582010-04-22 18:58:52 -07002187 int32_t pointerIds[MAX_POINTERS];
2188 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Browne839a582010-04-22 18:58:52 -07002189 int32_t motionEventEdgeFlags = 0;
Jeff Brownb51719b2010-07-29 18:18:33 -07002190 float xPrecision, yPrecision;
2191
2192 { // acquire lock
2193 AutoMutex _l(mLock);
2194
2195 // Walk through the the active pointers and map touch screen coordinates (TouchData) into
2196 // display coordinates (PointerCoords) and adjust for display orientation.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002197 for (uint32_t outIndex = 0; ! idBits.isEmpty(); outIndex++) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002198 uint32_t id = idBits.firstMarkedBit();
2199 idBits.clearBit(id);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002200 uint32_t inIndex = touch->idToIndex[id];
Jeff Brownb51719b2010-07-29 18:18:33 -07002201
Jeff Brown38a7fab2010-08-30 03:02:23 -07002202 const PointerData& in = touch->pointers[inIndex];
Jeff Brownb51719b2010-07-29 18:18:33 -07002203
Jeff Brown38a7fab2010-08-30 03:02:23 -07002204 // X and Y
2205 float x = float(in.x - mLocked.xOrigin) * mLocked.xScale;
2206 float y = float(in.y - mLocked.yOrigin) * mLocked.yScale;
Jeff Brownb51719b2010-07-29 18:18:33 -07002207
Jeff Brown38a7fab2010-08-30 03:02:23 -07002208 // ToolMajor and ToolMinor
2209 float toolMajor, toolMinor;
2210 switch (mCalibration.toolAreaCalibration) {
2211 case Calibration::TOOL_AREA_CALIBRATION_GEOMETRIC:
2212 toolMajor = in.toolMajor * mLocked.geometricScale;
2213 if (mRawAxes.toolMinor.valid) {
2214 toolMinor = in.toolMinor * mLocked.geometricScale;
2215 } else {
2216 toolMinor = toolMajor;
2217 }
2218 break;
2219 case Calibration::TOOL_AREA_CALIBRATION_LINEAR:
2220 toolMajor = in.toolMajor != 0
2221 ? in.toolMajor * mLocked.toolAreaLinearScale + mLocked.toolAreaLinearBias
2222 : 0;
2223 if (mRawAxes.toolMinor.valid) {
2224 toolMinor = in.toolMinor != 0
2225 ? in.toolMinor * mLocked.toolAreaLinearScale
2226 + mLocked.toolAreaLinearBias
2227 : 0;
2228 } else {
2229 toolMinor = toolMajor;
2230 }
2231 break;
2232 default:
2233 toolMajor = 0;
2234 toolMinor = 0;
2235 break;
Jeff Brownb51719b2010-07-29 18:18:33 -07002236 }
2237
Jeff Brown38a7fab2010-08-30 03:02:23 -07002238 if (mCalibration.haveToolAreaIsSummed && mCalibration.toolAreaIsSummed) {
2239 toolMajor /= pointerCount;
2240 toolMinor /= pointerCount;
2241 }
2242
2243 // Pressure
2244 float rawPressure;
2245 switch (mCalibration.pressureSource) {
2246 case Calibration::PRESSURE_SOURCE_PRESSURE:
2247 rawPressure = in.pressure;
2248 break;
2249 case Calibration::PRESSURE_SOURCE_TOUCH:
2250 rawPressure = in.touchMajor;
2251 break;
2252 default:
2253 rawPressure = 0;
2254 }
2255
2256 float pressure;
2257 switch (mCalibration.pressureCalibration) {
2258 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
2259 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
2260 pressure = rawPressure * mLocked.pressureScale;
2261 break;
2262 default:
2263 pressure = 1;
2264 break;
2265 }
2266
2267 // TouchMajor and TouchMinor
2268 float touchMajor, touchMinor;
2269 switch (mCalibration.touchAreaCalibration) {
2270 case Calibration::TOUCH_AREA_CALIBRATION_GEOMETRIC:
2271 touchMajor = in.touchMajor * mLocked.geometricScale;
2272 if (mRawAxes.touchMinor.valid) {
2273 touchMinor = in.touchMinor * mLocked.geometricScale;
2274 } else {
2275 touchMinor = touchMajor;
2276 }
2277 break;
2278 case Calibration::TOUCH_AREA_CALIBRATION_PRESSURE:
2279 touchMajor = toolMajor * pressure;
2280 touchMinor = toolMinor * pressure;
2281 break;
2282 default:
2283 touchMajor = 0;
2284 touchMinor = 0;
2285 break;
2286 }
2287
2288 if (touchMajor > toolMajor) {
2289 touchMajor = toolMajor;
2290 }
2291 if (touchMinor > toolMinor) {
2292 touchMinor = toolMinor;
2293 }
2294
2295 // Size
2296 float size;
2297 switch (mCalibration.sizeCalibration) {
2298 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
2299 float rawSize = mRawAxes.toolMinor.valid
2300 ? avg(in.toolMajor, in.toolMinor)
2301 : in.toolMajor;
2302 size = rawSize * mLocked.sizeScale;
2303 break;
2304 }
2305 default:
2306 size = 0;
2307 break;
2308 }
2309
2310 // Orientation
2311 float orientation;
2312 switch (mCalibration.orientationCalibration) {
2313 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
2314 orientation = in.orientation * mLocked.orientationScale;
2315 break;
2316 default:
2317 orientation = 0;
2318 }
2319
2320 // Adjust coords for orientation.
Jeff Brownb51719b2010-07-29 18:18:33 -07002321 switch (mLocked.surfaceOrientation) {
2322 case InputReaderPolicyInterface::ROTATION_90: {
2323 float xTemp = x;
2324 x = y;
2325 y = mLocked.surfaceWidth - xTemp;
2326 orientation -= M_PI_2;
2327 if (orientation < - M_PI_2) {
2328 orientation += M_PI;
2329 }
2330 break;
2331 }
2332 case InputReaderPolicyInterface::ROTATION_180: {
2333 x = mLocked.surfaceWidth - x;
2334 y = mLocked.surfaceHeight - y;
2335 orientation = - orientation;
2336 break;
2337 }
2338 case InputReaderPolicyInterface::ROTATION_270: {
2339 float xTemp = x;
2340 x = mLocked.surfaceHeight - y;
2341 y = xTemp;
2342 orientation += M_PI_2;
2343 if (orientation > M_PI_2) {
2344 orientation -= M_PI;
2345 }
2346 break;
2347 }
2348 }
2349
Jeff Brown38a7fab2010-08-30 03:02:23 -07002350 // Write output coords.
2351 PointerCoords& out = pointerCoords[outIndex];
2352 out.x = x;
2353 out.y = y;
2354 out.pressure = pressure;
2355 out.size = size;
2356 out.touchMajor = touchMajor;
2357 out.touchMinor = touchMinor;
2358 out.toolMajor = toolMajor;
2359 out.toolMinor = toolMinor;
2360 out.orientation = orientation;
Jeff Brownb51719b2010-07-29 18:18:33 -07002361
Jeff Brown38a7fab2010-08-30 03:02:23 -07002362 pointerIds[outIndex] = int32_t(id);
Jeff Brownb51719b2010-07-29 18:18:33 -07002363
2364 if (id == changedId) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002365 motionEventAction |= outIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
Jeff Brownb51719b2010-07-29 18:18:33 -07002366 }
Jeff Browne839a582010-04-22 18:58:52 -07002367 }
Jeff Brownb51719b2010-07-29 18:18:33 -07002368
2369 // Check edge flags by looking only at the first pointer since the flags are
2370 // global to the event.
2371 if (motionEventAction == AMOTION_EVENT_ACTION_DOWN) {
2372 if (pointerCoords[0].x <= 0) {
2373 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_LEFT;
2374 } else if (pointerCoords[0].x >= mLocked.orientedSurfaceWidth) {
2375 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_RIGHT;
2376 }
2377 if (pointerCoords[0].y <= 0) {
2378 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_TOP;
2379 } else if (pointerCoords[0].y >= mLocked.orientedSurfaceHeight) {
2380 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_BOTTOM;
2381 }
Jeff Browne839a582010-04-22 18:58:52 -07002382 }
Jeff Brownb51719b2010-07-29 18:18:33 -07002383
2384 xPrecision = mLocked.orientedXPrecision;
2385 yPrecision = mLocked.orientedYPrecision;
2386 } // release lock
Jeff Browne839a582010-04-22 18:58:52 -07002387
Jeff Brown77e26fc2010-10-07 13:44:51 -07002388 getDispatcher()->notifyMotion(when, getDeviceId(), getSources(), policyFlags,
Jeff Brownaf30ff62010-09-01 17:01:00 -07002389 motionEventAction, 0, getContext()->getGlobalMetaState(), motionEventEdgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -07002390 pointerCount, pointerIds, pointerCoords,
Jeff Brownb51719b2010-07-29 18:18:33 -07002391 xPrecision, yPrecision, mDownTime);
Jeff Browne839a582010-04-22 18:58:52 -07002392}
2393
Jeff Brownb51719b2010-07-29 18:18:33 -07002394bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002395 if (mRawAxes.x.valid && mRawAxes.y.valid) {
2396 return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue
2397 && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue;
Jeff Browne839a582010-04-22 18:58:52 -07002398 }
Jeff Browne57e8952010-07-23 21:28:06 -07002399 return true;
2400}
2401
Jeff Brownb51719b2010-07-29 18:18:33 -07002402const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked(
2403 int32_t x, int32_t y) {
2404 size_t numVirtualKeys = mLocked.virtualKeys.size();
2405 for (size_t i = 0; i < numVirtualKeys; i++) {
2406 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07002407
2408#if DEBUG_VIRTUAL_KEYS
2409 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
2410 "left=%d, top=%d, right=%d, bottom=%d",
2411 x, y,
2412 virtualKey.keyCode, virtualKey.scanCode,
2413 virtualKey.hitLeft, virtualKey.hitTop,
2414 virtualKey.hitRight, virtualKey.hitBottom);
2415#endif
2416
2417 if (virtualKey.isHit(x, y)) {
2418 return & virtualKey;
2419 }
2420 }
2421
2422 return NULL;
2423}
2424
2425void TouchInputMapper::calculatePointerIds() {
2426 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2427 uint32_t lastPointerCount = mLastTouch.pointerCount;
2428
2429 if (currentPointerCount == 0) {
2430 // No pointers to assign.
2431 mCurrentTouch.idBits.clear();
2432 } else if (lastPointerCount == 0) {
2433 // All pointers are new.
2434 mCurrentTouch.idBits.clear();
2435 for (uint32_t i = 0; i < currentPointerCount; i++) {
2436 mCurrentTouch.pointers[i].id = i;
2437 mCurrentTouch.idToIndex[i] = i;
2438 mCurrentTouch.idBits.markBit(i);
2439 }
2440 } else if (currentPointerCount == 1 && lastPointerCount == 1) {
2441 // Only one pointer and no change in count so it must have the same id as before.
2442 uint32_t id = mLastTouch.pointers[0].id;
2443 mCurrentTouch.pointers[0].id = id;
2444 mCurrentTouch.idToIndex[id] = 0;
2445 mCurrentTouch.idBits.value = BitSet32::valueForBit(id);
2446 } else {
2447 // General case.
2448 // We build a heap of squared euclidean distances between current and last pointers
2449 // associated with the current and last pointer indices. Then, we find the best
2450 // match (by distance) for each current pointer.
2451 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
2452
2453 uint32_t heapSize = 0;
2454 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
2455 currentPointerIndex++) {
2456 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
2457 lastPointerIndex++) {
2458 int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x
2459 - mLastTouch.pointers[lastPointerIndex].x;
2460 int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y
2461 - mLastTouch.pointers[lastPointerIndex].y;
2462
2463 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
2464
2465 // Insert new element into the heap (sift up).
2466 heap[heapSize].currentPointerIndex = currentPointerIndex;
2467 heap[heapSize].lastPointerIndex = lastPointerIndex;
2468 heap[heapSize].distance = distance;
2469 heapSize += 1;
2470 }
2471 }
2472
2473 // Heapify
2474 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
2475 startIndex -= 1;
2476 for (uint32_t parentIndex = startIndex; ;) {
2477 uint32_t childIndex = parentIndex * 2 + 1;
2478 if (childIndex >= heapSize) {
2479 break;
2480 }
2481
2482 if (childIndex + 1 < heapSize
2483 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2484 childIndex += 1;
2485 }
2486
2487 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2488 break;
2489 }
2490
2491 swap(heap[parentIndex], heap[childIndex]);
2492 parentIndex = childIndex;
2493 }
2494 }
2495
2496#if DEBUG_POINTER_ASSIGNMENT
2497 LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize);
2498 for (size_t i = 0; i < heapSize; i++) {
2499 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2500 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2501 heap[i].distance);
2502 }
2503#endif
2504
2505 // Pull matches out by increasing order of distance.
2506 // To avoid reassigning pointers that have already been matched, the loop keeps track
2507 // of which last and current pointers have been matched using the matchedXXXBits variables.
2508 // It also tracks the used pointer id bits.
2509 BitSet32 matchedLastBits(0);
2510 BitSet32 matchedCurrentBits(0);
2511 BitSet32 usedIdBits(0);
2512 bool first = true;
2513 for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) {
2514 for (;;) {
2515 if (first) {
2516 // The first time through the loop, we just consume the root element of
2517 // the heap (the one with smallest distance).
2518 first = false;
2519 } else {
2520 // Previous iterations consumed the root element of the heap.
2521 // Pop root element off of the heap (sift down).
2522 heapSize -= 1;
2523 assert(heapSize > 0);
2524
2525 // Sift down.
2526 heap[0] = heap[heapSize];
2527 for (uint32_t parentIndex = 0; ;) {
2528 uint32_t childIndex = parentIndex * 2 + 1;
2529 if (childIndex >= heapSize) {
2530 break;
2531 }
2532
2533 if (childIndex + 1 < heapSize
2534 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2535 childIndex += 1;
2536 }
2537
2538 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2539 break;
2540 }
2541
2542 swap(heap[parentIndex], heap[childIndex]);
2543 parentIndex = childIndex;
2544 }
2545
2546#if DEBUG_POINTER_ASSIGNMENT
2547 LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize);
2548 for (size_t i = 0; i < heapSize; i++) {
2549 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2550 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2551 heap[i].distance);
2552 }
2553#endif
2554 }
2555
2556 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
2557 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
2558
2559 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
2560 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
2561
2562 matchedCurrentBits.markBit(currentPointerIndex);
2563 matchedLastBits.markBit(lastPointerIndex);
2564
2565 uint32_t id = mLastTouch.pointers[lastPointerIndex].id;
2566 mCurrentTouch.pointers[currentPointerIndex].id = id;
2567 mCurrentTouch.idToIndex[id] = currentPointerIndex;
2568 usedIdBits.markBit(id);
2569
2570#if DEBUG_POINTER_ASSIGNMENT
2571 LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
2572 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
2573#endif
2574 break;
2575 }
2576 }
2577
2578 // Assign fresh ids to new pointers.
2579 if (currentPointerCount > lastPointerCount) {
2580 for (uint32_t i = currentPointerCount - lastPointerCount; ;) {
2581 uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit();
2582 uint32_t id = usedIdBits.firstUnmarkedBit();
2583
2584 mCurrentTouch.pointers[currentPointerIndex].id = id;
2585 mCurrentTouch.idToIndex[id] = currentPointerIndex;
2586 usedIdBits.markBit(id);
2587
2588#if DEBUG_POINTER_ASSIGNMENT
2589 LOGD("calculatePointerIds - assigned: cur=%d, id=%d",
2590 currentPointerIndex, id);
2591#endif
2592
2593 if (--i == 0) break; // done
2594 matchedCurrentBits.markBit(currentPointerIndex);
2595 }
2596 }
2597
2598 // Fix id bits.
2599 mCurrentTouch.idBits = usedIdBits;
2600 }
2601}
2602
2603/* Special hack for devices that have bad screen data: if one of the
2604 * points has moved more than a screen height from the last position,
2605 * then drop it. */
2606bool TouchInputMapper::applyBadTouchFilter() {
2607 // This hack requires valid axis parameters.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002608 if (! mRawAxes.y.valid) {
Jeff Browne57e8952010-07-23 21:28:06 -07002609 return false;
2610 }
2611
2612 uint32_t pointerCount = mCurrentTouch.pointerCount;
2613
2614 // Nothing to do if there are no points.
2615 if (pointerCount == 0) {
2616 return false;
2617 }
2618
2619 // Don't do anything if a finger is going down or up. We run
2620 // here before assigning pointer IDs, so there isn't a good
2621 // way to do per-finger matching.
2622 if (pointerCount != mLastTouch.pointerCount) {
2623 return false;
2624 }
2625
2626 // We consider a single movement across more than a 7/16 of
2627 // the long size of the screen to be bad. This was a magic value
2628 // determined by looking at the maximum distance it is feasible
2629 // to actually move in one sample.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002630 int32_t maxDeltaY = mRawAxes.y.getRange() * 7 / 16;
Jeff Browne57e8952010-07-23 21:28:06 -07002631
2632 // XXX The original code in InputDevice.java included commented out
2633 // code for testing the X axis. Note that when we drop a point
2634 // we don't actually restore the old X either. Strange.
2635 // The old code also tries to track when bad points were previously
2636 // detected but it turns out that due to the placement of a "break"
2637 // at the end of the loop, we never set mDroppedBadPoint to true
2638 // so it is effectively dead code.
2639 // Need to figure out if the old code is busted or just overcomplicated
2640 // but working as intended.
2641
2642 // Look through all new points and see if any are farther than
2643 // acceptable from all previous points.
2644 for (uint32_t i = pointerCount; i-- > 0; ) {
2645 int32_t y = mCurrentTouch.pointers[i].y;
2646 int32_t closestY = INT_MAX;
2647 int32_t closestDeltaY = 0;
2648
2649#if DEBUG_HACKS
2650 LOGD("BadTouchFilter: Looking at next point #%d: y=%d", i, y);
2651#endif
2652
2653 for (uint32_t j = pointerCount; j-- > 0; ) {
2654 int32_t lastY = mLastTouch.pointers[j].y;
2655 int32_t deltaY = abs(y - lastY);
2656
2657#if DEBUG_HACKS
2658 LOGD("BadTouchFilter: Comparing with last point #%d: y=%d deltaY=%d",
2659 j, lastY, deltaY);
2660#endif
2661
2662 if (deltaY < maxDeltaY) {
2663 goto SkipSufficientlyClosePoint;
2664 }
2665 if (deltaY < closestDeltaY) {
2666 closestDeltaY = deltaY;
2667 closestY = lastY;
2668 }
2669 }
2670
2671 // Must not have found a close enough match.
2672#if DEBUG_HACKS
2673 LOGD("BadTouchFilter: Dropping bad point #%d: newY=%d oldY=%d deltaY=%d maxDeltaY=%d",
2674 i, y, closestY, closestDeltaY, maxDeltaY);
2675#endif
2676
2677 mCurrentTouch.pointers[i].y = closestY;
2678 return true; // XXX original code only corrects one point
2679
2680 SkipSufficientlyClosePoint: ;
2681 }
2682
2683 // No change.
2684 return false;
2685}
2686
2687/* Special hack for devices that have bad screen data: drop points where
2688 * the coordinate value for one axis has jumped to the other pointer's location.
2689 */
2690bool TouchInputMapper::applyJumpyTouchFilter() {
2691 // This hack requires valid axis parameters.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002692 if (! mRawAxes.y.valid) {
Jeff Browne57e8952010-07-23 21:28:06 -07002693 return false;
2694 }
2695
2696 uint32_t pointerCount = mCurrentTouch.pointerCount;
2697 if (mLastTouch.pointerCount != pointerCount) {
2698#if DEBUG_HACKS
2699 LOGD("JumpyTouchFilter: Different pointer count %d -> %d",
2700 mLastTouch.pointerCount, pointerCount);
2701 for (uint32_t i = 0; i < pointerCount; i++) {
2702 LOGD(" Pointer %d (%d, %d)", i,
2703 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
2704 }
2705#endif
2706
2707 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_TRANSITION_DROPS) {
2708 if (mLastTouch.pointerCount == 1 && pointerCount == 2) {
2709 // Just drop the first few events going from 1 to 2 pointers.
2710 // They're bad often enough that they're not worth considering.
2711 mCurrentTouch.pointerCount = 1;
2712 mJumpyTouchFilter.jumpyPointsDropped += 1;
2713
2714#if DEBUG_HACKS
2715 LOGD("JumpyTouchFilter: Pointer 2 dropped");
2716#endif
2717 return true;
2718 } else if (mLastTouch.pointerCount == 2 && pointerCount == 1) {
2719 // The event when we go from 2 -> 1 tends to be messed up too
2720 mCurrentTouch.pointerCount = 2;
2721 mCurrentTouch.pointers[0] = mLastTouch.pointers[0];
2722 mCurrentTouch.pointers[1] = mLastTouch.pointers[1];
2723 mJumpyTouchFilter.jumpyPointsDropped += 1;
2724
2725#if DEBUG_HACKS
2726 for (int32_t i = 0; i < 2; i++) {
2727 LOGD("JumpyTouchFilter: Pointer %d replaced (%d, %d)", i,
2728 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
2729 }
2730#endif
2731 return true;
2732 }
2733 }
2734 // Reset jumpy points dropped on other transitions or if limit exceeded.
2735 mJumpyTouchFilter.jumpyPointsDropped = 0;
2736
2737#if DEBUG_HACKS
2738 LOGD("JumpyTouchFilter: Transition - drop limit reset");
2739#endif
2740 return false;
2741 }
2742
2743 // We have the same number of pointers as last time.
2744 // A 'jumpy' point is one where the coordinate value for one axis
2745 // has jumped to the other pointer's location. No need to do anything
2746 // else if we only have one pointer.
2747 if (pointerCount < 2) {
2748 return false;
2749 }
2750
2751 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_DROP_LIMIT) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002752 int jumpyEpsilon = mRawAxes.y.getRange() / JUMPY_EPSILON_DIVISOR;
Jeff Browne57e8952010-07-23 21:28:06 -07002753
2754 // We only replace the single worst jumpy point as characterized by pointer distance
2755 // in a single axis.
2756 int32_t badPointerIndex = -1;
2757 int32_t badPointerReplacementIndex = -1;
2758 int32_t badPointerDistance = INT_MIN; // distance to be corrected
2759
2760 for (uint32_t i = pointerCount; i-- > 0; ) {
2761 int32_t x = mCurrentTouch.pointers[i].x;
2762 int32_t y = mCurrentTouch.pointers[i].y;
2763
2764#if DEBUG_HACKS
2765 LOGD("JumpyTouchFilter: Point %d (%d, %d)", i, x, y);
2766#endif
2767
2768 // Check if a touch point is too close to another's coordinates
2769 bool dropX = false, dropY = false;
2770 for (uint32_t j = 0; j < pointerCount; j++) {
2771 if (i == j) {
2772 continue;
2773 }
2774
2775 if (abs(x - mCurrentTouch.pointers[j].x) <= jumpyEpsilon) {
2776 dropX = true;
2777 break;
2778 }
2779
2780 if (abs(y - mCurrentTouch.pointers[j].y) <= jumpyEpsilon) {
2781 dropY = true;
2782 break;
2783 }
2784 }
2785 if (! dropX && ! dropY) {
2786 continue; // not jumpy
2787 }
2788
2789 // Find a replacement candidate by comparing with older points on the
2790 // complementary (non-jumpy) axis.
2791 int32_t distance = INT_MIN; // distance to be corrected
2792 int32_t replacementIndex = -1;
2793
2794 if (dropX) {
2795 // X looks too close. Find an older replacement point with a close Y.
2796 int32_t smallestDeltaY = INT_MAX;
2797 for (uint32_t j = 0; j < pointerCount; j++) {
2798 int32_t deltaY = abs(y - mLastTouch.pointers[j].y);
2799 if (deltaY < smallestDeltaY) {
2800 smallestDeltaY = deltaY;
2801 replacementIndex = j;
2802 }
2803 }
2804 distance = abs(x - mLastTouch.pointers[replacementIndex].x);
2805 } else {
2806 // Y looks too close. Find an older replacement point with a close X.
2807 int32_t smallestDeltaX = INT_MAX;
2808 for (uint32_t j = 0; j < pointerCount; j++) {
2809 int32_t deltaX = abs(x - mLastTouch.pointers[j].x);
2810 if (deltaX < smallestDeltaX) {
2811 smallestDeltaX = deltaX;
2812 replacementIndex = j;
2813 }
2814 }
2815 distance = abs(y - mLastTouch.pointers[replacementIndex].y);
2816 }
2817
2818 // If replacing this pointer would correct a worse error than the previous ones
2819 // considered, then use this replacement instead.
2820 if (distance > badPointerDistance) {
2821 badPointerIndex = i;
2822 badPointerReplacementIndex = replacementIndex;
2823 badPointerDistance = distance;
2824 }
2825 }
2826
2827 // Correct the jumpy pointer if one was found.
2828 if (badPointerIndex >= 0) {
2829#if DEBUG_HACKS
2830 LOGD("JumpyTouchFilter: Replacing bad pointer %d with (%d, %d)",
2831 badPointerIndex,
2832 mLastTouch.pointers[badPointerReplacementIndex].x,
2833 mLastTouch.pointers[badPointerReplacementIndex].y);
2834#endif
2835
2836 mCurrentTouch.pointers[badPointerIndex].x =
2837 mLastTouch.pointers[badPointerReplacementIndex].x;
2838 mCurrentTouch.pointers[badPointerIndex].y =
2839 mLastTouch.pointers[badPointerReplacementIndex].y;
2840 mJumpyTouchFilter.jumpyPointsDropped += 1;
2841 return true;
2842 }
2843 }
2844
2845 mJumpyTouchFilter.jumpyPointsDropped = 0;
2846 return false;
2847}
2848
2849/* Special hack for devices that have bad screen data: aggregate and
2850 * compute averages of the coordinate data, to reduce the amount of
2851 * jitter seen by applications. */
2852void TouchInputMapper::applyAveragingTouchFilter() {
2853 for (uint32_t currentIndex = 0; currentIndex < mCurrentTouch.pointerCount; currentIndex++) {
2854 uint32_t id = mCurrentTouch.pointers[currentIndex].id;
2855 int32_t x = mCurrentTouch.pointers[currentIndex].x;
2856 int32_t y = mCurrentTouch.pointers[currentIndex].y;
Jeff Brown38a7fab2010-08-30 03:02:23 -07002857 int32_t pressure;
2858 switch (mCalibration.pressureSource) {
2859 case Calibration::PRESSURE_SOURCE_PRESSURE:
2860 pressure = mCurrentTouch.pointers[currentIndex].pressure;
2861 break;
2862 case Calibration::PRESSURE_SOURCE_TOUCH:
2863 pressure = mCurrentTouch.pointers[currentIndex].touchMajor;
2864 break;
2865 default:
2866 pressure = 1;
2867 break;
2868 }
Jeff Browne57e8952010-07-23 21:28:06 -07002869
2870 if (mLastTouch.idBits.hasBit(id)) {
2871 // Pointer was down before and is still down now.
2872 // Compute average over history trace.
2873 uint32_t start = mAveragingTouchFilter.historyStart[id];
2874 uint32_t end = mAveragingTouchFilter.historyEnd[id];
2875
2876 int64_t deltaX = x - mAveragingTouchFilter.historyData[end].pointers[id].x;
2877 int64_t deltaY = y - mAveragingTouchFilter.historyData[end].pointers[id].y;
2878 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
2879
2880#if DEBUG_HACKS
2881 LOGD("AveragingTouchFilter: Pointer id %d - Distance from last sample: %lld",
2882 id, distance);
2883#endif
2884
2885 if (distance < AVERAGING_DISTANCE_LIMIT) {
2886 // Increment end index in preparation for recording new historical data.
2887 end += 1;
2888 if (end > AVERAGING_HISTORY_SIZE) {
2889 end = 0;
2890 }
2891
2892 // If the end index has looped back to the start index then we have filled
2893 // the historical trace up to the desired size so we drop the historical
2894 // data at the start of the trace.
2895 if (end == start) {
2896 start += 1;
2897 if (start > AVERAGING_HISTORY_SIZE) {
2898 start = 0;
2899 }
2900 }
2901
2902 // Add the raw data to the historical trace.
2903 mAveragingTouchFilter.historyStart[id] = start;
2904 mAveragingTouchFilter.historyEnd[id] = end;
2905 mAveragingTouchFilter.historyData[end].pointers[id].x = x;
2906 mAveragingTouchFilter.historyData[end].pointers[id].y = y;
2907 mAveragingTouchFilter.historyData[end].pointers[id].pressure = pressure;
2908
2909 // Average over all historical positions in the trace by total pressure.
2910 int32_t averagedX = 0;
2911 int32_t averagedY = 0;
2912 int32_t totalPressure = 0;
2913 for (;;) {
2914 int32_t historicalX = mAveragingTouchFilter.historyData[start].pointers[id].x;
2915 int32_t historicalY = mAveragingTouchFilter.historyData[start].pointers[id].y;
2916 int32_t historicalPressure = mAveragingTouchFilter.historyData[start]
2917 .pointers[id].pressure;
2918
2919 averagedX += historicalX * historicalPressure;
2920 averagedY += historicalY * historicalPressure;
2921 totalPressure += historicalPressure;
2922
2923 if (start == end) {
2924 break;
2925 }
2926
2927 start += 1;
2928 if (start > AVERAGING_HISTORY_SIZE) {
2929 start = 0;
2930 }
2931 }
2932
Jeff Brown38a7fab2010-08-30 03:02:23 -07002933 if (totalPressure != 0) {
2934 averagedX /= totalPressure;
2935 averagedY /= totalPressure;
Jeff Browne57e8952010-07-23 21:28:06 -07002936
2937#if DEBUG_HACKS
Jeff Brown38a7fab2010-08-30 03:02:23 -07002938 LOGD("AveragingTouchFilter: Pointer id %d - "
2939 "totalPressure=%d, averagedX=%d, averagedY=%d", id, totalPressure,
2940 averagedX, averagedY);
Jeff Browne57e8952010-07-23 21:28:06 -07002941#endif
2942
Jeff Brown38a7fab2010-08-30 03:02:23 -07002943 mCurrentTouch.pointers[currentIndex].x = averagedX;
2944 mCurrentTouch.pointers[currentIndex].y = averagedY;
2945 }
Jeff Browne57e8952010-07-23 21:28:06 -07002946 } else {
2947#if DEBUG_HACKS
2948 LOGD("AveragingTouchFilter: Pointer id %d - Exceeded max distance", id);
2949#endif
2950 }
2951 } else {
2952#if DEBUG_HACKS
2953 LOGD("AveragingTouchFilter: Pointer id %d - Pointer went up", id);
2954#endif
2955 }
2956
2957 // Reset pointer history.
2958 mAveragingTouchFilter.historyStart[id] = 0;
2959 mAveragingTouchFilter.historyEnd[id] = 0;
2960 mAveragingTouchFilter.historyData[0].pointers[id].x = x;
2961 mAveragingTouchFilter.historyData[0].pointers[id].y = y;
2962 mAveragingTouchFilter.historyData[0].pointers[id].pressure = pressure;
2963 }
2964}
2965
2966int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002967 { // acquire lock
2968 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07002969
Jeff Brownb51719b2010-07-29 18:18:33 -07002970 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07002971 return AKEY_STATE_VIRTUAL;
2972 }
2973
Jeff Brownb51719b2010-07-29 18:18:33 -07002974 size_t numVirtualKeys = mLocked.virtualKeys.size();
2975 for (size_t i = 0; i < numVirtualKeys; i++) {
2976 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07002977 if (virtualKey.keyCode == keyCode) {
2978 return AKEY_STATE_UP;
2979 }
2980 }
Jeff Brownb51719b2010-07-29 18:18:33 -07002981 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07002982
2983 return AKEY_STATE_UNKNOWN;
2984}
2985
2986int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002987 { // acquire lock
2988 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07002989
Jeff Brownb51719b2010-07-29 18:18:33 -07002990 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07002991 return AKEY_STATE_VIRTUAL;
2992 }
2993
Jeff Brownb51719b2010-07-29 18:18:33 -07002994 size_t numVirtualKeys = mLocked.virtualKeys.size();
2995 for (size_t i = 0; i < numVirtualKeys; i++) {
2996 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07002997 if (virtualKey.scanCode == scanCode) {
2998 return AKEY_STATE_UP;
2999 }
3000 }
Jeff Brownb51719b2010-07-29 18:18:33 -07003001 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07003002
3003 return AKEY_STATE_UNKNOWN;
3004}
3005
3006bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
3007 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownb51719b2010-07-29 18:18:33 -07003008 { // acquire lock
3009 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07003010
Jeff Brownb51719b2010-07-29 18:18:33 -07003011 size_t numVirtualKeys = mLocked.virtualKeys.size();
3012 for (size_t i = 0; i < numVirtualKeys; i++) {
3013 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07003014
3015 for (size_t i = 0; i < numCodes; i++) {
3016 if (virtualKey.keyCode == keyCodes[i]) {
3017 outFlags[i] = 1;
3018 }
3019 }
3020 }
Jeff Brownb51719b2010-07-29 18:18:33 -07003021 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07003022
3023 return true;
3024}
3025
3026
3027// --- SingleTouchInputMapper ---
3028
3029SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device, int32_t associatedDisplayId) :
3030 TouchInputMapper(device, associatedDisplayId) {
3031 initialize();
3032}
3033
3034SingleTouchInputMapper::~SingleTouchInputMapper() {
3035}
3036
3037void SingleTouchInputMapper::initialize() {
3038 mAccumulator.clear();
3039
3040 mDown = false;
3041 mX = 0;
3042 mY = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003043 mPressure = 0; // default to 0 for devices that don't report pressure
3044 mToolWidth = 0; // default to 0 for devices that don't report tool width
Jeff Browne57e8952010-07-23 21:28:06 -07003045}
3046
3047void SingleTouchInputMapper::reset() {
3048 TouchInputMapper::reset();
3049
Jeff Browne57e8952010-07-23 21:28:06 -07003050 initialize();
3051 }
3052
3053void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
3054 switch (rawEvent->type) {
3055 case EV_KEY:
3056 switch (rawEvent->scanCode) {
3057 case BTN_TOUCH:
3058 mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH;
3059 mAccumulator.btnTouch = rawEvent->value != 0;
Jeff Brownd64c8552010-08-17 20:38:35 -07003060 // Don't sync immediately. Wait until the next SYN_REPORT since we might
3061 // not have received valid position information yet. This logic assumes that
3062 // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet.
Jeff Browne57e8952010-07-23 21:28:06 -07003063 break;
3064 }
3065 break;
3066
3067 case EV_ABS:
3068 switch (rawEvent->scanCode) {
3069 case ABS_X:
3070 mAccumulator.fields |= Accumulator::FIELD_ABS_X;
3071 mAccumulator.absX = rawEvent->value;
3072 break;
3073 case ABS_Y:
3074 mAccumulator.fields |= Accumulator::FIELD_ABS_Y;
3075 mAccumulator.absY = rawEvent->value;
3076 break;
3077 case ABS_PRESSURE:
3078 mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE;
3079 mAccumulator.absPressure = rawEvent->value;
3080 break;
3081 case ABS_TOOL_WIDTH:
3082 mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH;
3083 mAccumulator.absToolWidth = rawEvent->value;
3084 break;
3085 }
3086 break;
3087
3088 case EV_SYN:
3089 switch (rawEvent->scanCode) {
3090 case SYN_REPORT:
Jeff Brownd64c8552010-08-17 20:38:35 -07003091 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07003092 break;
3093 }
3094 break;
3095 }
3096}
3097
3098void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Browne57e8952010-07-23 21:28:06 -07003099 uint32_t fields = mAccumulator.fields;
Jeff Brownd64c8552010-08-17 20:38:35 -07003100 if (fields == 0) {
3101 return; // no new state changes, so nothing to do
3102 }
Jeff Browne57e8952010-07-23 21:28:06 -07003103
3104 if (fields & Accumulator::FIELD_BTN_TOUCH) {
3105 mDown = mAccumulator.btnTouch;
3106 }
3107
3108 if (fields & Accumulator::FIELD_ABS_X) {
3109 mX = mAccumulator.absX;
3110 }
3111
3112 if (fields & Accumulator::FIELD_ABS_Y) {
3113 mY = mAccumulator.absY;
3114 }
3115
3116 if (fields & Accumulator::FIELD_ABS_PRESSURE) {
3117 mPressure = mAccumulator.absPressure;
3118 }
3119
3120 if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003121 mToolWidth = mAccumulator.absToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -07003122 }
3123
3124 mCurrentTouch.clear();
3125
3126 if (mDown) {
3127 mCurrentTouch.pointerCount = 1;
3128 mCurrentTouch.pointers[0].id = 0;
3129 mCurrentTouch.pointers[0].x = mX;
3130 mCurrentTouch.pointers[0].y = mY;
3131 mCurrentTouch.pointers[0].pressure = mPressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003132 mCurrentTouch.pointers[0].touchMajor = 0;
3133 mCurrentTouch.pointers[0].touchMinor = 0;
3134 mCurrentTouch.pointers[0].toolMajor = mToolWidth;
3135 mCurrentTouch.pointers[0].toolMinor = mToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -07003136 mCurrentTouch.pointers[0].orientation = 0;
3137 mCurrentTouch.idToIndex[0] = 0;
3138 mCurrentTouch.idBits.markBit(0);
3139 }
3140
3141 syncTouch(when, true);
Jeff Brownd64c8552010-08-17 20:38:35 -07003142
3143 mAccumulator.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07003144}
3145
Jeff Brown38a7fab2010-08-30 03:02:23 -07003146void SingleTouchInputMapper::configureRawAxes() {
3147 TouchInputMapper::configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -07003148
Jeff Brown38a7fab2010-08-30 03:02:23 -07003149 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x);
3150 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y);
3151 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure);
3152 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor);
Jeff Browne57e8952010-07-23 21:28:06 -07003153}
3154
3155
3156// --- MultiTouchInputMapper ---
3157
3158MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device, int32_t associatedDisplayId) :
3159 TouchInputMapper(device, associatedDisplayId) {
3160 initialize();
3161}
3162
3163MultiTouchInputMapper::~MultiTouchInputMapper() {
3164}
3165
3166void MultiTouchInputMapper::initialize() {
3167 mAccumulator.clear();
3168}
3169
3170void MultiTouchInputMapper::reset() {
3171 TouchInputMapper::reset();
3172
Jeff Browne57e8952010-07-23 21:28:06 -07003173 initialize();
3174}
3175
3176void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
3177 switch (rawEvent->type) {
3178 case EV_ABS: {
3179 uint32_t pointerIndex = mAccumulator.pointerCount;
3180 Accumulator::Pointer* pointer = & mAccumulator.pointers[pointerIndex];
3181
3182 switch (rawEvent->scanCode) {
3183 case ABS_MT_POSITION_X:
3184 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_X;
3185 pointer->absMTPositionX = rawEvent->value;
3186 break;
3187 case ABS_MT_POSITION_Y:
3188 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y;
3189 pointer->absMTPositionY = rawEvent->value;
3190 break;
3191 case ABS_MT_TOUCH_MAJOR:
3192 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR;
3193 pointer->absMTTouchMajor = rawEvent->value;
3194 break;
3195 case ABS_MT_TOUCH_MINOR:
3196 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR;
3197 pointer->absMTTouchMinor = rawEvent->value;
3198 break;
3199 case ABS_MT_WIDTH_MAJOR:
3200 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR;
3201 pointer->absMTWidthMajor = rawEvent->value;
3202 break;
3203 case ABS_MT_WIDTH_MINOR:
3204 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR;
3205 pointer->absMTWidthMinor = rawEvent->value;
3206 break;
3207 case ABS_MT_ORIENTATION:
3208 pointer->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION;
3209 pointer->absMTOrientation = rawEvent->value;
3210 break;
3211 case ABS_MT_TRACKING_ID:
3212 pointer->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID;
3213 pointer->absMTTrackingId = rawEvent->value;
3214 break;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003215 case ABS_MT_PRESSURE:
3216 pointer->fields |= Accumulator::FIELD_ABS_MT_PRESSURE;
3217 pointer->absMTPressure = rawEvent->value;
3218 break;
Jeff Browne57e8952010-07-23 21:28:06 -07003219 }
3220 break;
3221 }
3222
3223 case EV_SYN:
3224 switch (rawEvent->scanCode) {
3225 case SYN_MT_REPORT: {
3226 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
3227 uint32_t pointerIndex = mAccumulator.pointerCount;
3228
3229 if (mAccumulator.pointers[pointerIndex].fields) {
3230 if (pointerIndex == MAX_POINTERS) {
3231 LOGW("MultiTouch device driver returned more than maximum of %d pointers.",
3232 MAX_POINTERS);
3233 } else {
3234 pointerIndex += 1;
3235 mAccumulator.pointerCount = pointerIndex;
3236 }
3237 }
3238
3239 mAccumulator.pointers[pointerIndex].clear();
3240 break;
3241 }
3242
3243 case SYN_REPORT:
Jeff Brownd64c8552010-08-17 20:38:35 -07003244 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07003245 break;
3246 }
3247 break;
3248 }
3249}
3250
3251void MultiTouchInputMapper::sync(nsecs_t when) {
3252 static const uint32_t REQUIRED_FIELDS =
Jeff Brown38a7fab2010-08-30 03:02:23 -07003253 Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y;
Jeff Browne839a582010-04-22 18:58:52 -07003254
Jeff Browne57e8952010-07-23 21:28:06 -07003255 uint32_t inCount = mAccumulator.pointerCount;
3256 uint32_t outCount = 0;
3257 bool havePointerIds = true;
Jeff Browne839a582010-04-22 18:58:52 -07003258
Jeff Browne57e8952010-07-23 21:28:06 -07003259 mCurrentTouch.clear();
Jeff Browne839a582010-04-22 18:58:52 -07003260
Jeff Browne57e8952010-07-23 21:28:06 -07003261 for (uint32_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003262 const Accumulator::Pointer& inPointer = mAccumulator.pointers[inIndex];
3263 uint32_t fields = inPointer.fields;
Jeff Browne839a582010-04-22 18:58:52 -07003264
Jeff Browne57e8952010-07-23 21:28:06 -07003265 if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003266 // Some drivers send empty MT sync packets without X / Y to indicate a pointer up.
3267 // Drop this finger.
Jeff Browne839a582010-04-22 18:58:52 -07003268 continue;
3269 }
3270
Jeff Brownd64c8552010-08-17 20:38:35 -07003271 PointerData& outPointer = mCurrentTouch.pointers[outCount];
3272 outPointer.x = inPointer.absMTPositionX;
3273 outPointer.y = inPointer.absMTPositionY;
Jeff Browne839a582010-04-22 18:58:52 -07003274
Jeff Brown38a7fab2010-08-30 03:02:23 -07003275 if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) {
3276 if (inPointer.absMTPressure <= 0) {
3277 // Some devices send sync packets with X / Y but with a 0 presure to indicate
Jeff Brownd64c8552010-08-17 20:38:35 -07003278 // a pointer up. Drop this finger.
3279 continue;
3280 }
Jeff Brown38a7fab2010-08-30 03:02:23 -07003281 outPointer.pressure = inPointer.absMTPressure;
3282 } else {
3283 // Default pressure to 0 if absent.
3284 outPointer.pressure = 0;
3285 }
3286
3287 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) {
3288 if (inPointer.absMTTouchMajor <= 0) {
3289 // Some devices send sync packets with X / Y but with a 0 touch major to indicate
3290 // a pointer going up. Drop this finger.
3291 continue;
3292 }
Jeff Brownd64c8552010-08-17 20:38:35 -07003293 outPointer.touchMajor = inPointer.absMTTouchMajor;
3294 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003295 // Default touch area to 0 if absent.
Jeff Brownd64c8552010-08-17 20:38:35 -07003296 outPointer.touchMajor = 0;
3297 }
Jeff Browne839a582010-04-22 18:58:52 -07003298
Jeff Brownd64c8552010-08-17 20:38:35 -07003299 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) {
3300 outPointer.touchMinor = inPointer.absMTTouchMinor;
3301 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003302 // Assume touch area is circular.
Jeff Brownd64c8552010-08-17 20:38:35 -07003303 outPointer.touchMinor = outPointer.touchMajor;
3304 }
Jeff Browne839a582010-04-22 18:58:52 -07003305
Jeff Brownd64c8552010-08-17 20:38:35 -07003306 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) {
3307 outPointer.toolMajor = inPointer.absMTWidthMajor;
3308 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003309 // Default tool area to 0 if absent.
3310 outPointer.toolMajor = 0;
Jeff Brownd64c8552010-08-17 20:38:35 -07003311 }
Jeff Browne839a582010-04-22 18:58:52 -07003312
Jeff Brownd64c8552010-08-17 20:38:35 -07003313 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) {
3314 outPointer.toolMinor = inPointer.absMTWidthMinor;
3315 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003316 // Assume tool area is circular.
Jeff Brownd64c8552010-08-17 20:38:35 -07003317 outPointer.toolMinor = outPointer.toolMajor;
3318 }
3319
3320 if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) {
3321 outPointer.orientation = inPointer.absMTOrientation;
3322 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003323 // Default orientation to vertical if absent.
Jeff Brownd64c8552010-08-17 20:38:35 -07003324 outPointer.orientation = 0;
3325 }
3326
Jeff Brown38a7fab2010-08-30 03:02:23 -07003327 // Assign pointer id using tracking id if available.
Jeff Browne57e8952010-07-23 21:28:06 -07003328 if (havePointerIds) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003329 if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) {
3330 uint32_t id = uint32_t(inPointer.absMTTrackingId);
Jeff Browne839a582010-04-22 18:58:52 -07003331
Jeff Browne57e8952010-07-23 21:28:06 -07003332 if (id > MAX_POINTER_ID) {
3333#if DEBUG_POINTERS
3334 LOGD("Pointers: Ignoring driver provided pointer id %d because "
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07003335 "it is larger than max supported id %d",
Jeff Browne57e8952010-07-23 21:28:06 -07003336 id, MAX_POINTER_ID);
3337#endif
3338 havePointerIds = false;
3339 }
3340 else {
Jeff Brownd64c8552010-08-17 20:38:35 -07003341 outPointer.id = id;
Jeff Browne57e8952010-07-23 21:28:06 -07003342 mCurrentTouch.idToIndex[id] = outCount;
3343 mCurrentTouch.idBits.markBit(id);
3344 }
3345 } else {
3346 havePointerIds = false;
Jeff Browne839a582010-04-22 18:58:52 -07003347 }
3348 }
Jeff Browne839a582010-04-22 18:58:52 -07003349
Jeff Browne57e8952010-07-23 21:28:06 -07003350 outCount += 1;
Jeff Browne839a582010-04-22 18:58:52 -07003351 }
3352
Jeff Browne57e8952010-07-23 21:28:06 -07003353 mCurrentTouch.pointerCount = outCount;
Jeff Browne839a582010-04-22 18:58:52 -07003354
Jeff Browne57e8952010-07-23 21:28:06 -07003355 syncTouch(when, havePointerIds);
Jeff Brownd64c8552010-08-17 20:38:35 -07003356
3357 mAccumulator.clear();
Jeff Browne839a582010-04-22 18:58:52 -07003358}
3359
Jeff Brown38a7fab2010-08-30 03:02:23 -07003360void MultiTouchInputMapper::configureRawAxes() {
3361 TouchInputMapper::configureRawAxes();
Jeff Browne839a582010-04-22 18:58:52 -07003362
Jeff Brown38a7fab2010-08-30 03:02:23 -07003363 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, & mRawAxes.x);
3364 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, & mRawAxes.y);
3365 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, & mRawAxes.touchMajor);
3366 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, & mRawAxes.touchMinor);
3367 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, & mRawAxes.toolMajor);
3368 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, & mRawAxes.toolMinor);
3369 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, & mRawAxes.orientation);
3370 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, & mRawAxes.pressure);
Jeff Brown54bc2812010-06-15 01:31:58 -07003371}
3372
Jeff Browne839a582010-04-22 18:58:52 -07003373
3374} // namespace android