blob: b91e93a47c4192be04d8aa6d8dbb82cf7f9a39f5 [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 Brown6a817e22010-09-12 17:55:08 -070073int32_t setEphemeralMetaState(int32_t mask, bool down, int32_t oldMetaState) {
74 int32_t newMetaState;
75 if (down) {
76 newMetaState = oldMetaState | mask;
77 } else {
78 newMetaState = oldMetaState &
79 ~(mask | AMETA_ALT_ON | AMETA_SHIFT_ON | AMETA_CTRL_ON | AMETA_META_ON);
Jeff Browne839a582010-04-22 18:58:52 -070080 }
81
Jeff Brown5c1ed842010-07-14 18:48:53 -070082 if (newMetaState & (AMETA_ALT_LEFT_ON | AMETA_ALT_RIGHT_ON)) {
83 newMetaState |= AMETA_ALT_ON;
Jeff Browne839a582010-04-22 18:58:52 -070084 }
85
Jeff Brown5c1ed842010-07-14 18:48:53 -070086 if (newMetaState & (AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_RIGHT_ON)) {
87 newMetaState |= AMETA_SHIFT_ON;
Jeff Browne839a582010-04-22 18:58:52 -070088 }
89
Jeff Brown6a817e22010-09-12 17:55:08 -070090 if (newMetaState & (AMETA_CTRL_LEFT_ON | AMETA_CTRL_RIGHT_ON)) {
91 newMetaState |= AMETA_CTRL_ON;
92 }
93
94 if (newMetaState & (AMETA_META_LEFT_ON | AMETA_META_RIGHT_ON)) {
95 newMetaState |= AMETA_META_ON;
96 }
Jeff Browne839a582010-04-22 18:58:52 -070097 return newMetaState;
98}
99
Jeff Brown6a817e22010-09-12 17:55:08 -0700100int32_t toggleLockedMetaState(int32_t mask, bool down, int32_t oldMetaState) {
101 if (down) {
102 return oldMetaState;
103 } else {
104 return oldMetaState ^ mask;
105 }
106}
107
108int32_t updateMetaState(int32_t keyCode, bool down, int32_t oldMetaState) {
109 int32_t mask;
110 switch (keyCode) {
111 case AKEYCODE_ALT_LEFT:
112 return setEphemeralMetaState(AMETA_ALT_LEFT_ON, down, oldMetaState);
113 case AKEYCODE_ALT_RIGHT:
114 return setEphemeralMetaState(AMETA_ALT_RIGHT_ON, down, oldMetaState);
115 case AKEYCODE_SHIFT_LEFT:
116 return setEphemeralMetaState(AMETA_SHIFT_LEFT_ON, down, oldMetaState);
117 case AKEYCODE_SHIFT_RIGHT:
118 return setEphemeralMetaState(AMETA_SHIFT_RIGHT_ON, down, oldMetaState);
119 case AKEYCODE_SYM:
120 return setEphemeralMetaState(AMETA_SYM_ON, down, oldMetaState);
121 case AKEYCODE_FUNCTION:
122 return setEphemeralMetaState(AMETA_FUNCTION_ON, down, oldMetaState);
123 case AKEYCODE_CTRL_LEFT:
124 return setEphemeralMetaState(AMETA_CTRL_LEFT_ON, down, oldMetaState);
125 case AKEYCODE_CTRL_RIGHT:
126 return setEphemeralMetaState(AMETA_CTRL_RIGHT_ON, down, oldMetaState);
127 case AKEYCODE_META_LEFT:
128 return setEphemeralMetaState(AMETA_META_LEFT_ON, down, oldMetaState);
129 case AKEYCODE_META_RIGHT:
130 return setEphemeralMetaState(AMETA_META_RIGHT_ON, down, oldMetaState);
131 case AKEYCODE_CAPS_LOCK:
Jeff Brownd4ecee92010-10-29 22:19:53 -0700132 return toggleLockedMetaState(AMETA_CAPS_LOCK_ON, down, oldMetaState);
Jeff Brown6a817e22010-09-12 17:55:08 -0700133 case AKEYCODE_NUM_LOCK:
Jeff Brownd4ecee92010-10-29 22:19:53 -0700134 return toggleLockedMetaState(AMETA_NUM_LOCK_ON, down, oldMetaState);
Jeff Brown6a817e22010-09-12 17:55:08 -0700135 case AKEYCODE_SCROLL_LOCK:
Jeff Brownd4ecee92010-10-29 22:19:53 -0700136 return toggleLockedMetaState(AMETA_SCROLL_LOCK_ON, down, oldMetaState);
Jeff Brown6a817e22010-09-12 17:55:08 -0700137 default:
138 return oldMetaState;
139 }
140}
141
Jeff Browne839a582010-04-22 18:58:52 -0700142static const int32_t keyCodeRotationMap[][4] = {
143 // key codes enumerated counter-clockwise with the original (unrotated) key first
144 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brown8575a872010-06-30 16:10:35 -0700145 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
146 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
147 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
148 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Browne839a582010-04-22 18:58:52 -0700149};
150static const int keyCodeRotationMapSize =
151 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
152
153int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brown54bc2812010-06-15 01:31:58 -0700154 if (orientation != InputReaderPolicyInterface::ROTATION_0) {
Jeff Browne839a582010-04-22 18:58:52 -0700155 for (int i = 0; i < keyCodeRotationMapSize; i++) {
156 if (keyCode == keyCodeRotationMap[i][0]) {
157 return keyCodeRotationMap[i][orientation];
158 }
159 }
160 }
161 return keyCode;
162}
163
Jeff Browne57e8952010-07-23 21:28:06 -0700164static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
165 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
166}
167
Jeff Browne839a582010-04-22 18:58:52 -0700168
Jeff Brown38a7fab2010-08-30 03:02:23 -0700169// --- InputDeviceCalibration ---
170
171InputDeviceCalibration::InputDeviceCalibration() {
172}
173
174void InputDeviceCalibration::clear() {
175 mProperties.clear();
176}
177
178void InputDeviceCalibration::addProperty(const String8& key, const String8& value) {
179 mProperties.add(key, value);
180}
181
182bool InputDeviceCalibration::tryGetProperty(const String8& key, String8& outValue) const {
183 ssize_t index = mProperties.indexOfKey(key);
184 if (index < 0) {
185 return false;
186 }
187
188 outValue = mProperties.valueAt(index);
189 return true;
190}
191
192bool InputDeviceCalibration::tryGetProperty(const String8& key, int32_t& outValue) const {
193 String8 stringValue;
194 if (! tryGetProperty(key, stringValue) || stringValue.length() == 0) {
195 return false;
196 }
197
198 char* end;
199 int value = strtol(stringValue.string(), & end, 10);
200 if (*end != '\0') {
201 LOGW("Input device calibration key '%s' has invalid value '%s'. Expected an integer.",
202 key.string(), stringValue.string());
203 return false;
204 }
205 outValue = value;
206 return true;
207}
208
209bool InputDeviceCalibration::tryGetProperty(const String8& key, float& outValue) const {
210 String8 stringValue;
211 if (! tryGetProperty(key, stringValue) || stringValue.length() == 0) {
212 return false;
213 }
214
215 char* end;
216 float value = strtof(stringValue.string(), & end);
217 if (*end != '\0') {
218 LOGW("Input device calibration key '%s' has invalid value '%s'. Expected a float.",
219 key.string(), stringValue.string());
220 return false;
221 }
222 outValue = value;
223 return true;
224}
225
226
Jeff Browne839a582010-04-22 18:58:52 -0700227// --- InputReader ---
228
229InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown54bc2812010-06-15 01:31:58 -0700230 const sp<InputReaderPolicyInterface>& policy,
Jeff Browne839a582010-04-22 18:58:52 -0700231 const sp<InputDispatcherInterface>& dispatcher) :
Jeff Browne57e8952010-07-23 21:28:06 -0700232 mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher),
233 mGlobalMetaState(0) {
Jeff Brown54bc2812010-06-15 01:31:58 -0700234 configureExcludedDevices();
Jeff Browne57e8952010-07-23 21:28:06 -0700235 updateGlobalMetaState();
236 updateInputConfiguration();
Jeff Browne839a582010-04-22 18:58:52 -0700237}
238
239InputReader::~InputReader() {
240 for (size_t i = 0; i < mDevices.size(); i++) {
241 delete mDevices.valueAt(i);
242 }
243}
244
245void InputReader::loopOnce() {
246 RawEvent rawEvent;
Jeff Browne57e8952010-07-23 21:28:06 -0700247 mEventHub->getEvent(& rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700248
249#if DEBUG_RAW_EVENTS
250 LOGD("Input event: device=0x%x type=0x%x scancode=%d keycode=%d value=%d",
251 rawEvent.deviceId, rawEvent.type, rawEvent.scanCode, rawEvent.keyCode,
252 rawEvent.value);
253#endif
254
255 process(& rawEvent);
256}
257
258void InputReader::process(const RawEvent* rawEvent) {
259 switch (rawEvent->type) {
260 case EventHubInterface::DEVICE_ADDED:
Jeff Brown1ad00e92010-10-01 18:55:43 -0700261 addDevice(rawEvent->deviceId);
Jeff Browne839a582010-04-22 18:58:52 -0700262 break;
263
264 case EventHubInterface::DEVICE_REMOVED:
Jeff Brown1ad00e92010-10-01 18:55:43 -0700265 removeDevice(rawEvent->deviceId);
266 break;
267
268 case EventHubInterface::FINISHED_DEVICE_SCAN:
Jeff Brown3c3cc622010-10-20 15:33:38 -0700269 handleConfigurationChanged(rawEvent->when);
Jeff Browne839a582010-04-22 18:58:52 -0700270 break;
271
Jeff Browne57e8952010-07-23 21:28:06 -0700272 default:
273 consumeEvent(rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700274 break;
275 }
276}
277
Jeff Brown1ad00e92010-10-01 18:55:43 -0700278void InputReader::addDevice(int32_t deviceId) {
Jeff Browne57e8952010-07-23 21:28:06 -0700279 String8 name = mEventHub->getDeviceName(deviceId);
280 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
281
282 InputDevice* device = createDevice(deviceId, name, classes);
283 device->configure();
284
Jeff Brown38a7fab2010-08-30 03:02:23 -0700285 if (device->isIgnored()) {
Jeff Brown26c94ff2010-09-30 14:33:04 -0700286 LOGI("Device added: id=0x%x, name=%s (ignored non-input device)", deviceId, name.string());
Jeff Brown38a7fab2010-08-30 03:02:23 -0700287 } else {
Jeff Brown26c94ff2010-09-30 14:33:04 -0700288 LOGI("Device added: id=0x%x, name=%s, sources=%08x", deviceId, name.string(),
289 device->getSources());
Jeff Brown38a7fab2010-08-30 03:02:23 -0700290 }
291
Jeff Browne57e8952010-07-23 21:28:06 -0700292 bool added = false;
293 { // acquire device registry writer lock
294 RWLock::AutoWLock _wl(mDeviceRegistryLock);
295
296 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
297 if (deviceIndex < 0) {
298 mDevices.add(deviceId, device);
299 added = true;
300 }
301 } // release device registry writer lock
302
303 if (! added) {
304 LOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
305 delete device;
Jeff Browne839a582010-04-22 18:58:52 -0700306 return;
307 }
Jeff Browne839a582010-04-22 18:58:52 -0700308}
309
Jeff Brown1ad00e92010-10-01 18:55:43 -0700310void InputReader::removeDevice(int32_t deviceId) {
Jeff Browne57e8952010-07-23 21:28:06 -0700311 bool removed = false;
312 InputDevice* device = NULL;
313 { // acquire device registry writer lock
314 RWLock::AutoWLock _wl(mDeviceRegistryLock);
315
316 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
317 if (deviceIndex >= 0) {
318 device = mDevices.valueAt(deviceIndex);
319 mDevices.removeItemsAt(deviceIndex, 1);
320 removed = true;
321 }
322 } // release device registry writer lock
323
324 if (! removed) {
325 LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Browne839a582010-04-22 18:58:52 -0700326 return;
327 }
328
Jeff Browne57e8952010-07-23 21:28:06 -0700329 if (device->isIgnored()) {
330 LOGI("Device removed: id=0x%x, name=%s (ignored non-input device)",
331 device->getId(), device->getName().string());
332 } else {
333 LOGI("Device removed: id=0x%x, name=%s, sources=%08x",
334 device->getId(), device->getName().string(), device->getSources());
335 }
336
Jeff Brown38a7fab2010-08-30 03:02:23 -0700337 device->reset();
338
Jeff Browne57e8952010-07-23 21:28:06 -0700339 delete device;
Jeff Browne839a582010-04-22 18:58:52 -0700340}
341
Jeff Browne57e8952010-07-23 21:28:06 -0700342InputDevice* InputReader::createDevice(int32_t deviceId, const String8& name, uint32_t classes) {
343 InputDevice* device = new InputDevice(this, deviceId, name);
Jeff Browne839a582010-04-22 18:58:52 -0700344
Jeff Browne57e8952010-07-23 21:28:06 -0700345 const int32_t associatedDisplayId = 0; // FIXME: hardcoded for current single-display devices
Jeff Browne839a582010-04-22 18:58:52 -0700346
Jeff Browne57e8952010-07-23 21:28:06 -0700347 // Switch-like devices.
348 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
349 device->addMapper(new SwitchInputMapper(device));
350 }
351
352 // Keyboard-like devices.
353 uint32_t keyboardSources = 0;
354 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
355 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
356 keyboardSources |= AINPUT_SOURCE_KEYBOARD;
357 }
358 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
359 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
360 }
361 if (classes & INPUT_DEVICE_CLASS_DPAD) {
362 keyboardSources |= AINPUT_SOURCE_DPAD;
363 }
Jeff Browne57e8952010-07-23 21:28:06 -0700364
365 if (keyboardSources != 0) {
366 device->addMapper(new KeyboardInputMapper(device,
367 associatedDisplayId, keyboardSources, keyboardType));
368 }
369
370 // Trackball-like devices.
371 if (classes & INPUT_DEVICE_CLASS_TRACKBALL) {
372 device->addMapper(new TrackballInputMapper(device, associatedDisplayId));
373 }
374
375 // Touchscreen-like devices.
376 if (classes & INPUT_DEVICE_CLASS_TOUCHSCREEN_MT) {
377 device->addMapper(new MultiTouchInputMapper(device, associatedDisplayId));
378 } else if (classes & INPUT_DEVICE_CLASS_TOUCHSCREEN) {
379 device->addMapper(new SingleTouchInputMapper(device, associatedDisplayId));
380 }
381
382 return device;
383}
384
385void InputReader::consumeEvent(const RawEvent* rawEvent) {
386 int32_t deviceId = rawEvent->deviceId;
387
388 { // acquire device registry reader lock
389 RWLock::AutoRLock _rl(mDeviceRegistryLock);
390
391 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
392 if (deviceIndex < 0) {
393 LOGW("Discarding event for unknown deviceId %d.", deviceId);
394 return;
395 }
396
397 InputDevice* device = mDevices.valueAt(deviceIndex);
398 if (device->isIgnored()) {
399 //LOGD("Discarding event for ignored deviceId %d.", deviceId);
400 return;
401 }
402
403 device->process(rawEvent);
404 } // release device registry reader lock
405}
406
Jeff Brown3c3cc622010-10-20 15:33:38 -0700407void InputReader::handleConfigurationChanged(nsecs_t when) {
Jeff Browne57e8952010-07-23 21:28:06 -0700408 // Reset global meta state because it depends on the list of all configured devices.
409 updateGlobalMetaState();
410
411 // Update input configuration.
412 updateInputConfiguration();
413
414 // Enqueue configuration changed.
415 mDispatcher->notifyConfigurationChanged(when);
416}
417
418void InputReader::configureExcludedDevices() {
419 Vector<String8> excludedDeviceNames;
420 mPolicy->getExcludedDeviceNames(excludedDeviceNames);
421
422 for (size_t i = 0; i < excludedDeviceNames.size(); i++) {
423 mEventHub->addExcludedDevice(excludedDeviceNames[i]);
424 }
425}
426
427void InputReader::updateGlobalMetaState() {
428 { // acquire state lock
429 AutoMutex _l(mStateLock);
430
431 mGlobalMetaState = 0;
432
433 { // acquire device registry reader lock
434 RWLock::AutoRLock _rl(mDeviceRegistryLock);
435
436 for (size_t i = 0; i < mDevices.size(); i++) {
437 InputDevice* device = mDevices.valueAt(i);
438 mGlobalMetaState |= device->getMetaState();
439 }
440 } // release device registry reader lock
441 } // release state lock
442}
443
444int32_t InputReader::getGlobalMetaState() {
445 { // acquire state lock
446 AutoMutex _l(mStateLock);
447
448 return mGlobalMetaState;
449 } // release state lock
450}
451
452void InputReader::updateInputConfiguration() {
453 { // acquire state lock
454 AutoMutex _l(mStateLock);
455
456 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
457 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
458 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
459 { // acquire device registry reader lock
460 RWLock::AutoRLock _rl(mDeviceRegistryLock);
461
462 InputDeviceInfo deviceInfo;
463 for (size_t i = 0; i < mDevices.size(); i++) {
464 InputDevice* device = mDevices.valueAt(i);
465 device->getDeviceInfo(& deviceInfo);
466 uint32_t sources = deviceInfo.getSources();
467
468 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
469 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
470 }
471 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
472 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
473 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
474 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
475 }
476 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
477 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
Jeff Browne839a582010-04-22 18:58:52 -0700478 }
479 }
Jeff Browne57e8952010-07-23 21:28:06 -0700480 } // release device registry reader lock
Jeff Browne839a582010-04-22 18:58:52 -0700481
Jeff Browne57e8952010-07-23 21:28:06 -0700482 mInputConfiguration.touchScreen = touchScreenConfig;
483 mInputConfiguration.keyboard = keyboardConfig;
484 mInputConfiguration.navigation = navigationConfig;
485 } // release state lock
486}
487
488void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
489 { // acquire state lock
490 AutoMutex _l(mStateLock);
491
492 *outConfiguration = mInputConfiguration;
493 } // release state lock
494}
495
496status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
497 { // acquire device registry reader lock
498 RWLock::AutoRLock _rl(mDeviceRegistryLock);
499
500 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
501 if (deviceIndex < 0) {
502 return NAME_NOT_FOUND;
Jeff Browne839a582010-04-22 18:58:52 -0700503 }
504
Jeff Browne57e8952010-07-23 21:28:06 -0700505 InputDevice* device = mDevices.valueAt(deviceIndex);
506 if (device->isIgnored()) {
507 return NAME_NOT_FOUND;
Jeff Browne839a582010-04-22 18:58:52 -0700508 }
Jeff Browne57e8952010-07-23 21:28:06 -0700509
510 device->getDeviceInfo(outDeviceInfo);
511 return OK;
512 } // release device registy reader lock
513}
514
515void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
516 outDeviceIds.clear();
517
518 { // acquire device registry reader lock
519 RWLock::AutoRLock _rl(mDeviceRegistryLock);
520
521 size_t numDevices = mDevices.size();
522 for (size_t i = 0; i < numDevices; i++) {
523 InputDevice* device = mDevices.valueAt(i);
524 if (! device->isIgnored()) {
525 outDeviceIds.add(device->getId());
526 }
527 }
528 } // release device registy reader lock
529}
530
531int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
532 int32_t keyCode) {
533 return getState(deviceId, sourceMask, keyCode, & InputDevice::getKeyCodeState);
534}
535
536int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
537 int32_t scanCode) {
538 return getState(deviceId, sourceMask, scanCode, & InputDevice::getScanCodeState);
539}
540
541int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
542 return getState(deviceId, sourceMask, switchCode, & InputDevice::getSwitchState);
543}
544
545int32_t InputReader::getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
546 GetStateFunc getStateFunc) {
547 { // acquire device registry reader lock
548 RWLock::AutoRLock _rl(mDeviceRegistryLock);
549
550 int32_t result = AKEY_STATE_UNKNOWN;
551 if (deviceId >= 0) {
552 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
553 if (deviceIndex >= 0) {
554 InputDevice* device = mDevices.valueAt(deviceIndex);
555 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
556 result = (device->*getStateFunc)(sourceMask, code);
557 }
558 }
559 } else {
560 size_t numDevices = mDevices.size();
561 for (size_t i = 0; i < numDevices; i++) {
562 InputDevice* device = mDevices.valueAt(i);
563 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
564 result = (device->*getStateFunc)(sourceMask, code);
565 if (result >= AKEY_STATE_DOWN) {
566 return result;
567 }
568 }
569 }
570 }
571 return result;
572 } // release device registy reader lock
573}
574
575bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
576 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
577 memset(outFlags, 0, numCodes);
578 return markSupportedKeyCodes(deviceId, sourceMask, numCodes, keyCodes, outFlags);
579}
580
581bool InputReader::markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
582 const int32_t* keyCodes, uint8_t* outFlags) {
583 { // acquire device registry reader lock
584 RWLock::AutoRLock _rl(mDeviceRegistryLock);
585 bool result = false;
586 if (deviceId >= 0) {
587 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
588 if (deviceIndex >= 0) {
589 InputDevice* device = mDevices.valueAt(deviceIndex);
590 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
591 result = device->markSupportedKeyCodes(sourceMask,
592 numCodes, keyCodes, outFlags);
593 }
594 }
595 } else {
596 size_t numDevices = mDevices.size();
597 for (size_t i = 0; i < numDevices; i++) {
598 InputDevice* device = mDevices.valueAt(i);
599 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
600 result |= device->markSupportedKeyCodes(sourceMask,
601 numCodes, keyCodes, outFlags);
602 }
603 }
604 }
605 return result;
606 } // release device registy reader lock
607}
608
Jeff Browna665ca82010-09-08 11:49:43 -0700609void InputReader::dump(String8& dump) {
Jeff Brown2806e382010-10-01 17:46:21 -0700610 mEventHub->dump(dump);
611 dump.append("\n");
612
613 dump.append("Input Reader State:\n");
614
Jeff Brown26c94ff2010-09-30 14:33:04 -0700615 { // acquire device registry reader lock
616 RWLock::AutoRLock _rl(mDeviceRegistryLock);
Jeff Browna665ca82010-09-08 11:49:43 -0700617
Jeff Brown26c94ff2010-09-30 14:33:04 -0700618 for (size_t i = 0; i < mDevices.size(); i++) {
619 mDevices.valueAt(i)->dump(dump);
Jeff Browna665ca82010-09-08 11:49:43 -0700620 }
Jeff Brown26c94ff2010-09-30 14:33:04 -0700621 } // release device registy reader lock
Jeff Browna665ca82010-09-08 11:49:43 -0700622}
623
Jeff Browne57e8952010-07-23 21:28:06 -0700624
625// --- InputReaderThread ---
626
627InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
628 Thread(/*canCallJava*/ true), mReader(reader) {
629}
630
631InputReaderThread::~InputReaderThread() {
632}
633
634bool InputReaderThread::threadLoop() {
635 mReader->loopOnce();
636 return true;
637}
638
639
640// --- InputDevice ---
641
642InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) :
643 mContext(context), mId(id), mName(name), mSources(0) {
644}
645
646InputDevice::~InputDevice() {
647 size_t numMappers = mMappers.size();
648 for (size_t i = 0; i < numMappers; i++) {
649 delete mMappers[i];
650 }
651 mMappers.clear();
652}
653
Jeff Brown26c94ff2010-09-30 14:33:04 -0700654static void dumpMotionRange(String8& dump, const InputDeviceInfo& deviceInfo,
655 int32_t rangeType, const char* name) {
656 const InputDeviceInfo::MotionRange* range = deviceInfo.getMotionRange(rangeType);
657 if (range) {
658 dump.appendFormat(INDENT3 "%s: min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
659 name, range->min, range->max, range->flat, range->fuzz);
660 }
661}
662
663void InputDevice::dump(String8& dump) {
664 InputDeviceInfo deviceInfo;
665 getDeviceInfo(& deviceInfo);
666
667 dump.appendFormat(INDENT "Device 0x%x: %s\n", deviceInfo.getId(),
668 deviceInfo.getName().string());
669 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
670 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
671 if (!deviceInfo.getMotionRanges().isEmpty()) {
672 dump.append(INDENT2 "Motion Ranges:\n");
673 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_X, "X");
674 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_Y, "Y");
675 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_PRESSURE, "Pressure");
676 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_SIZE, "Size");
677 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOUCH_MAJOR, "TouchMajor");
678 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOUCH_MINOR, "TouchMinor");
679 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOOL_MAJOR, "ToolMajor");
680 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOOL_MINOR, "ToolMinor");
681 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_ORIENTATION, "Orientation");
682 }
683
684 size_t numMappers = mMappers.size();
685 for (size_t i = 0; i < numMappers; i++) {
686 InputMapper* mapper = mMappers[i];
687 mapper->dump(dump);
688 }
689}
690
Jeff Browne57e8952010-07-23 21:28:06 -0700691void InputDevice::addMapper(InputMapper* mapper) {
692 mMappers.add(mapper);
693}
694
695void InputDevice::configure() {
Jeff Brown38a7fab2010-08-30 03:02:23 -0700696 if (! isIgnored()) {
697 mContext->getPolicy()->getInputDeviceCalibration(mName, mCalibration);
698 }
699
Jeff Browne57e8952010-07-23 21:28:06 -0700700 mSources = 0;
701
702 size_t numMappers = mMappers.size();
703 for (size_t i = 0; i < numMappers; i++) {
704 InputMapper* mapper = mMappers[i];
705 mapper->configure();
706 mSources |= mapper->getSources();
Jeff Browne839a582010-04-22 18:58:52 -0700707 }
708}
709
Jeff Browne57e8952010-07-23 21:28:06 -0700710void InputDevice::reset() {
711 size_t numMappers = mMappers.size();
712 for (size_t i = 0; i < numMappers; i++) {
713 InputMapper* mapper = mMappers[i];
714 mapper->reset();
715 }
716}
Jeff Browne839a582010-04-22 18:58:52 -0700717
Jeff Browne57e8952010-07-23 21:28:06 -0700718void InputDevice::process(const RawEvent* rawEvent) {
719 size_t numMappers = mMappers.size();
720 for (size_t i = 0; i < numMappers; i++) {
721 InputMapper* mapper = mMappers[i];
722 mapper->process(rawEvent);
723 }
724}
Jeff Browne839a582010-04-22 18:58:52 -0700725
Jeff Browne57e8952010-07-23 21:28:06 -0700726void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
727 outDeviceInfo->initialize(mId, mName);
728
729 size_t numMappers = mMappers.size();
730 for (size_t i = 0; i < numMappers; i++) {
731 InputMapper* mapper = mMappers[i];
732 mapper->populateDeviceInfo(outDeviceInfo);
733 }
734}
735
736int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
737 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
738}
739
740int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
741 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
742}
743
744int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
745 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
746}
747
748int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
749 int32_t result = AKEY_STATE_UNKNOWN;
750 size_t numMappers = mMappers.size();
751 for (size_t i = 0; i < numMappers; i++) {
752 InputMapper* mapper = mMappers[i];
753 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
754 result = (mapper->*getStateFunc)(sourceMask, code);
755 if (result >= AKEY_STATE_DOWN) {
756 return result;
757 }
758 }
759 }
760 return result;
761}
762
763bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
764 const int32_t* keyCodes, uint8_t* outFlags) {
765 bool result = false;
766 size_t numMappers = mMappers.size();
767 for (size_t i = 0; i < numMappers; i++) {
768 InputMapper* mapper = mMappers[i];
769 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
770 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
771 }
772 }
773 return result;
774}
775
776int32_t InputDevice::getMetaState() {
777 int32_t result = 0;
778 size_t numMappers = mMappers.size();
779 for (size_t i = 0; i < numMappers; i++) {
780 InputMapper* mapper = mMappers[i];
781 result |= mapper->getMetaState();
782 }
783 return result;
784}
785
786
787// --- InputMapper ---
788
789InputMapper::InputMapper(InputDevice* device) :
790 mDevice(device), mContext(device->getContext()) {
791}
792
793InputMapper::~InputMapper() {
794}
795
796void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
797 info->addSource(getSources());
798}
799
Jeff Brown26c94ff2010-09-30 14:33:04 -0700800void InputMapper::dump(String8& dump) {
801}
802
Jeff Browne57e8952010-07-23 21:28:06 -0700803void InputMapper::configure() {
804}
805
806void InputMapper::reset() {
807}
808
809int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
810 return AKEY_STATE_UNKNOWN;
811}
812
813int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
814 return AKEY_STATE_UNKNOWN;
815}
816
817int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
818 return AKEY_STATE_UNKNOWN;
819}
820
821bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
822 const int32_t* keyCodes, uint8_t* outFlags) {
823 return false;
824}
825
826int32_t InputMapper::getMetaState() {
827 return 0;
828}
829
Jeff Browne57e8952010-07-23 21:28:06 -0700830
831// --- SwitchInputMapper ---
832
833SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
834 InputMapper(device) {
835}
836
837SwitchInputMapper::~SwitchInputMapper() {
838}
839
840uint32_t SwitchInputMapper::getSources() {
841 return 0;
842}
843
844void SwitchInputMapper::process(const RawEvent* rawEvent) {
845 switch (rawEvent->type) {
846 case EV_SW:
847 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
848 break;
849 }
850}
851
852void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brown90f0cee2010-10-08 22:31:17 -0700853 getDispatcher()->notifySwitch(when, switchCode, switchValue, 0);
Jeff Browne57e8952010-07-23 21:28:06 -0700854}
855
856int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
857 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
858}
859
860
861// --- KeyboardInputMapper ---
862
863KeyboardInputMapper::KeyboardInputMapper(InputDevice* device, int32_t associatedDisplayId,
864 uint32_t sources, int32_t keyboardType) :
865 InputMapper(device), mAssociatedDisplayId(associatedDisplayId), mSources(sources),
866 mKeyboardType(keyboardType) {
Jeff Brownb51719b2010-07-29 18:18:33 -0700867 initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700868}
869
870KeyboardInputMapper::~KeyboardInputMapper() {
871}
872
Jeff Brownb51719b2010-07-29 18:18:33 -0700873void KeyboardInputMapper::initializeLocked() {
874 mLocked.metaState = AMETA_NONE;
875 mLocked.downTime = 0;
Jeff Brown6a817e22010-09-12 17:55:08 -0700876
877 initializeLedStateLocked(mLocked.capsLockLedState, LED_CAPSL);
878 initializeLedStateLocked(mLocked.numLockLedState, LED_NUML);
879 initializeLedStateLocked(mLocked.scrollLockLedState, LED_SCROLLL);
880
881 updateLedStateLocked(true);
882}
883
884void KeyboardInputMapper::initializeLedStateLocked(LockedState::LedState& ledState, int32_t led) {
885 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
886 ledState.on = false;
Jeff Browne57e8952010-07-23 21:28:06 -0700887}
888
889uint32_t KeyboardInputMapper::getSources() {
890 return mSources;
891}
892
893void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
894 InputMapper::populateDeviceInfo(info);
895
896 info->setKeyboardType(mKeyboardType);
897}
898
Jeff Brown26c94ff2010-09-30 14:33:04 -0700899void KeyboardInputMapper::dump(String8& dump) {
900 { // acquire lock
901 AutoMutex _l(mLock);
902 dump.append(INDENT2 "Keyboard Input Mapper:\n");
903 dump.appendFormat(INDENT3 "AssociatedDisplayId: %d\n", mAssociatedDisplayId);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700904 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
905 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mLocked.keyDowns.size());
906 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mLocked.metaState);
907 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
908 } // release lock
909}
910
Jeff Browne57e8952010-07-23 21:28:06 -0700911void KeyboardInputMapper::reset() {
Jeff Brownb51719b2010-07-29 18:18:33 -0700912 for (;;) {
913 int32_t keyCode, scanCode;
914 { // acquire lock
915 AutoMutex _l(mLock);
916
917 // Synthesize key up event on reset if keys are currently down.
918 if (mLocked.keyDowns.isEmpty()) {
919 initializeLocked();
920 break; // done
921 }
922
923 const KeyDown& keyDown = mLocked.keyDowns.top();
924 keyCode = keyDown.keyCode;
925 scanCode = keyDown.scanCode;
926 } // release lock
927
Jeff Browne57e8952010-07-23 21:28:06 -0700928 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownb51719b2010-07-29 18:18:33 -0700929 processKey(when, false, keyCode, scanCode, 0);
Jeff Browne57e8952010-07-23 21:28:06 -0700930 }
931
932 InputMapper::reset();
Jeff Browne57e8952010-07-23 21:28:06 -0700933 getContext()->updateGlobalMetaState();
934}
935
936void KeyboardInputMapper::process(const RawEvent* rawEvent) {
937 switch (rawEvent->type) {
938 case EV_KEY: {
939 int32_t scanCode = rawEvent->scanCode;
940 if (isKeyboardOrGamepadKey(scanCode)) {
941 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
942 rawEvent->flags);
943 }
944 break;
945 }
946 }
947}
948
949bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
950 return scanCode < BTN_MOUSE
951 || scanCode >= KEY_OK
952 || (scanCode >= BTN_GAMEPAD && scanCode < BTN_DIGI);
953}
954
Jeff Brownb51719b2010-07-29 18:18:33 -0700955void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
956 int32_t scanCode, uint32_t policyFlags) {
957 int32_t newMetaState;
958 nsecs_t downTime;
959 bool metaStateChanged = false;
960
961 { // acquire lock
962 AutoMutex _l(mLock);
963
964 if (down) {
965 // Rotate key codes according to orientation if needed.
966 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
967 if (mAssociatedDisplayId >= 0) {
968 int32_t orientation;
Jeff Brownd4ecee92010-10-29 22:19:53 -0700969 if (!getPolicy()->getDisplayInfo(mAssociatedDisplayId, NULL, NULL, & orientation)) {
970 orientation = InputReaderPolicyInterface::ROTATION_0;
Jeff Brownb51719b2010-07-29 18:18:33 -0700971 }
972
973 keyCode = rotateKeyCode(keyCode, orientation);
Jeff Browne57e8952010-07-23 21:28:06 -0700974 }
975
Jeff Brownb51719b2010-07-29 18:18:33 -0700976 // Add key down.
977 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
978 if (keyDownIndex >= 0) {
979 // key repeat, be sure to use same keycode as before in case of rotation
980 keyCode = mLocked.keyDowns.top().keyCode;
981 } else {
982 // key down
983 mLocked.keyDowns.push();
984 KeyDown& keyDown = mLocked.keyDowns.editTop();
985 keyDown.keyCode = keyCode;
986 keyDown.scanCode = scanCode;
987 }
988
989 mLocked.downTime = when;
990 } else {
991 // Remove key down.
992 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
993 if (keyDownIndex >= 0) {
994 // key up, be sure to use same keycode as before in case of rotation
995 keyCode = mLocked.keyDowns.top().keyCode;
996 mLocked.keyDowns.removeAt(size_t(keyDownIndex));
997 } else {
998 // key was not actually down
999 LOGI("Dropping key up from device %s because the key was not down. "
1000 "keyCode=%d, scanCode=%d",
1001 getDeviceName().string(), keyCode, scanCode);
1002 return;
1003 }
Jeff Browne57e8952010-07-23 21:28:06 -07001004 }
1005
Jeff Brownb51719b2010-07-29 18:18:33 -07001006 int32_t oldMetaState = mLocked.metaState;
1007 newMetaState = updateMetaState(keyCode, down, oldMetaState);
1008 if (oldMetaState != newMetaState) {
1009 mLocked.metaState = newMetaState;
1010 metaStateChanged = true;
Jeff Brown6a817e22010-09-12 17:55:08 -07001011 updateLedStateLocked(false);
Jeff Browne57e8952010-07-23 21:28:06 -07001012 }
Jeff Brown8575a872010-06-30 16:10:35 -07001013
Jeff Brownb51719b2010-07-29 18:18:33 -07001014 downTime = mLocked.downTime;
1015 } // release lock
1016
1017 if (metaStateChanged) {
Jeff Browne57e8952010-07-23 21:28:06 -07001018 getContext()->updateGlobalMetaState();
Jeff Browne839a582010-04-22 18:58:52 -07001019 }
1020
Jeff Brown6a817e22010-09-12 17:55:08 -07001021 if (policyFlags & POLICY_FLAG_FUNCTION) {
1022 newMetaState |= AMETA_FUNCTION_ON;
1023 }
Jeff Browne57e8952010-07-23 21:28:06 -07001024 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
Jeff Brown90f0cee2010-10-08 22:31:17 -07001025 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1026 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Browne839a582010-04-22 18:58:52 -07001027}
1028
Jeff Brownb51719b2010-07-29 18:18:33 -07001029ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) {
1030 size_t n = mLocked.keyDowns.size();
Jeff Browne57e8952010-07-23 21:28:06 -07001031 for (size_t i = 0; i < n; i++) {
Jeff Brownb51719b2010-07-29 18:18:33 -07001032 if (mLocked.keyDowns[i].scanCode == scanCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07001033 return i;
1034 }
1035 }
1036 return -1;
Jeff Browne839a582010-04-22 18:58:52 -07001037}
1038
Jeff Browne57e8952010-07-23 21:28:06 -07001039int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1040 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1041}
Jeff Browne839a582010-04-22 18:58:52 -07001042
Jeff Browne57e8952010-07-23 21:28:06 -07001043int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1044 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1045}
Jeff Browne839a582010-04-22 18:58:52 -07001046
Jeff Browne57e8952010-07-23 21:28:06 -07001047bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1048 const int32_t* keyCodes, uint8_t* outFlags) {
1049 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1050}
1051
1052int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownb51719b2010-07-29 18:18:33 -07001053 { // acquire lock
1054 AutoMutex _l(mLock);
1055 return mLocked.metaState;
1056 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07001057}
1058
Jeff Brown6a817e22010-09-12 17:55:08 -07001059void KeyboardInputMapper::updateLedStateLocked(bool reset) {
1060 updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL,
Jeff Brownd4ecee92010-10-29 22:19:53 -07001061 AMETA_CAPS_LOCK_ON, reset);
Jeff Brown6a817e22010-09-12 17:55:08 -07001062 updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML,
Jeff Brownd4ecee92010-10-29 22:19:53 -07001063 AMETA_NUM_LOCK_ON, reset);
Jeff Brown6a817e22010-09-12 17:55:08 -07001064 updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL,
Jeff Brownd4ecee92010-10-29 22:19:53 -07001065 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown6a817e22010-09-12 17:55:08 -07001066}
1067
1068void KeyboardInputMapper::updateLedStateForModifierLocked(LockedState::LedState& ledState,
1069 int32_t led, int32_t modifier, bool reset) {
1070 if (ledState.avail) {
1071 bool desiredState = (mLocked.metaState & modifier) != 0;
1072 if (ledState.on != desiredState) {
1073 getEventHub()->setLedState(getDeviceId(), led, desiredState);
1074 ledState.on = desiredState;
1075 }
1076 }
1077}
1078
Jeff Browne57e8952010-07-23 21:28:06 -07001079
1080// --- TrackballInputMapper ---
1081
1082TrackballInputMapper::TrackballInputMapper(InputDevice* device, int32_t associatedDisplayId) :
1083 InputMapper(device), mAssociatedDisplayId(associatedDisplayId) {
1084 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1085 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1086 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1087 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1088
Jeff Brownb51719b2010-07-29 18:18:33 -07001089 initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -07001090}
1091
1092TrackballInputMapper::~TrackballInputMapper() {
1093}
1094
1095uint32_t TrackballInputMapper::getSources() {
1096 return AINPUT_SOURCE_TRACKBALL;
1097}
1098
1099void TrackballInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1100 InputMapper::populateDeviceInfo(info);
1101
1102 info->addMotionRange(AINPUT_MOTION_RANGE_X, -1.0f, 1.0f, 0.0f, mXScale);
1103 info->addMotionRange(AINPUT_MOTION_RANGE_Y, -1.0f, 1.0f, 0.0f, mYScale);
1104}
1105
Jeff Brown26c94ff2010-09-30 14:33:04 -07001106void TrackballInputMapper::dump(String8& dump) {
1107 { // acquire lock
1108 AutoMutex _l(mLock);
1109 dump.append(INDENT2 "Trackball Input Mapper:\n");
1110 dump.appendFormat(INDENT3 "AssociatedDisplayId: %d\n", mAssociatedDisplayId);
1111 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1112 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
1113 dump.appendFormat(INDENT3 "Down: %s\n", toString(mLocked.down));
1114 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1115 } // release lock
1116}
1117
Jeff Brownb51719b2010-07-29 18:18:33 -07001118void TrackballInputMapper::initializeLocked() {
Jeff Browne57e8952010-07-23 21:28:06 -07001119 mAccumulator.clear();
1120
Jeff Brownb51719b2010-07-29 18:18:33 -07001121 mLocked.down = false;
1122 mLocked.downTime = 0;
Jeff Browne57e8952010-07-23 21:28:06 -07001123}
1124
1125void TrackballInputMapper::reset() {
Jeff Brownb51719b2010-07-29 18:18:33 -07001126 for (;;) {
1127 { // acquire lock
1128 AutoMutex _l(mLock);
1129
1130 if (! mLocked.down) {
1131 initializeLocked();
1132 break; // done
1133 }
1134 } // release lock
1135
1136 // Synthesize trackball button up event on reset.
Jeff Browne57e8952010-07-23 21:28:06 -07001137 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownb51719b2010-07-29 18:18:33 -07001138 mAccumulator.fields = Accumulator::FIELD_BTN_MOUSE;
Jeff Browne57e8952010-07-23 21:28:06 -07001139 mAccumulator.btnMouse = false;
1140 sync(when);
Jeff Browne839a582010-04-22 18:58:52 -07001141 }
1142
Jeff Browne57e8952010-07-23 21:28:06 -07001143 InputMapper::reset();
Jeff Browne57e8952010-07-23 21:28:06 -07001144}
Jeff Browne839a582010-04-22 18:58:52 -07001145
Jeff Browne57e8952010-07-23 21:28:06 -07001146void TrackballInputMapper::process(const RawEvent* rawEvent) {
1147 switch (rawEvent->type) {
1148 case EV_KEY:
1149 switch (rawEvent->scanCode) {
1150 case BTN_MOUSE:
1151 mAccumulator.fields |= Accumulator::FIELD_BTN_MOUSE;
1152 mAccumulator.btnMouse = rawEvent->value != 0;
Jeff Brownd64c8552010-08-17 20:38:35 -07001153 // Sync now since BTN_MOUSE is not necessarily followed by SYN_REPORT and
1154 // we need to ensure that we report the up/down promptly.
Jeff Browne57e8952010-07-23 21:28:06 -07001155 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07001156 break;
Jeff Browne839a582010-04-22 18:58:52 -07001157 }
Jeff Browne57e8952010-07-23 21:28:06 -07001158 break;
Jeff Browne839a582010-04-22 18:58:52 -07001159
Jeff Browne57e8952010-07-23 21:28:06 -07001160 case EV_REL:
1161 switch (rawEvent->scanCode) {
1162 case REL_X:
1163 mAccumulator.fields |= Accumulator::FIELD_REL_X;
1164 mAccumulator.relX = rawEvent->value;
1165 break;
1166 case REL_Y:
1167 mAccumulator.fields |= Accumulator::FIELD_REL_Y;
1168 mAccumulator.relY = rawEvent->value;
1169 break;
Jeff Browne839a582010-04-22 18:58:52 -07001170 }
Jeff Browne57e8952010-07-23 21:28:06 -07001171 break;
Jeff Browne839a582010-04-22 18:58:52 -07001172
Jeff Browne57e8952010-07-23 21:28:06 -07001173 case EV_SYN:
1174 switch (rawEvent->scanCode) {
1175 case SYN_REPORT:
Jeff Brownd64c8552010-08-17 20:38:35 -07001176 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07001177 break;
Jeff Browne839a582010-04-22 18:58:52 -07001178 }
Jeff Browne57e8952010-07-23 21:28:06 -07001179 break;
Jeff Browne839a582010-04-22 18:58:52 -07001180 }
Jeff Browne839a582010-04-22 18:58:52 -07001181}
1182
Jeff Browne57e8952010-07-23 21:28:06 -07001183void TrackballInputMapper::sync(nsecs_t when) {
Jeff Brownd64c8552010-08-17 20:38:35 -07001184 uint32_t fields = mAccumulator.fields;
1185 if (fields == 0) {
1186 return; // no new state changes, so nothing to do
1187 }
1188
Jeff Brownb51719b2010-07-29 18:18:33 -07001189 int motionEventAction;
1190 PointerCoords pointerCoords;
1191 nsecs_t downTime;
1192 { // acquire lock
1193 AutoMutex _l(mLock);
Jeff Browne839a582010-04-22 18:58:52 -07001194
Jeff Brownb51719b2010-07-29 18:18:33 -07001195 bool downChanged = fields & Accumulator::FIELD_BTN_MOUSE;
1196
1197 if (downChanged) {
1198 if (mAccumulator.btnMouse) {
1199 mLocked.down = true;
1200 mLocked.downTime = when;
1201 } else {
1202 mLocked.down = false;
1203 }
Jeff Browne57e8952010-07-23 21:28:06 -07001204 }
Jeff Browne839a582010-04-22 18:58:52 -07001205
Jeff Brownb51719b2010-07-29 18:18:33 -07001206 downTime = mLocked.downTime;
1207 float x = fields & Accumulator::FIELD_REL_X ? mAccumulator.relX * mXScale : 0.0f;
1208 float y = fields & Accumulator::FIELD_REL_Y ? mAccumulator.relY * mYScale : 0.0f;
Jeff Browne839a582010-04-22 18:58:52 -07001209
Jeff Brownb51719b2010-07-29 18:18:33 -07001210 if (downChanged) {
1211 motionEventAction = mLocked.down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
Jeff Browne57e8952010-07-23 21:28:06 -07001212 } else {
Jeff Brownb51719b2010-07-29 18:18:33 -07001213 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browne57e8952010-07-23 21:28:06 -07001214 }
Jeff Browne839a582010-04-22 18:58:52 -07001215
Jeff Brownb51719b2010-07-29 18:18:33 -07001216 pointerCoords.x = x;
1217 pointerCoords.y = y;
1218 pointerCoords.pressure = mLocked.down ? 1.0f : 0.0f;
1219 pointerCoords.size = 0;
1220 pointerCoords.touchMajor = 0;
1221 pointerCoords.touchMinor = 0;
1222 pointerCoords.toolMajor = 0;
1223 pointerCoords.toolMinor = 0;
1224 pointerCoords.orientation = 0;
Jeff Browne839a582010-04-22 18:58:52 -07001225
Jeff Brownb51719b2010-07-29 18:18:33 -07001226 if (mAssociatedDisplayId >= 0 && (x != 0.0f || y != 0.0f)) {
1227 // Rotate motion based on display orientation if needed.
1228 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1229 int32_t orientation;
1230 if (! getPolicy()->getDisplayInfo(mAssociatedDisplayId, NULL, NULL, & orientation)) {
Jeff Brownd4ecee92010-10-29 22:19:53 -07001231 orientation = InputReaderPolicyInterface::ROTATION_0;
Jeff Brownb51719b2010-07-29 18:18:33 -07001232 }
1233
1234 float temp;
1235 switch (orientation) {
1236 case InputReaderPolicyInterface::ROTATION_90:
1237 temp = pointerCoords.x;
1238 pointerCoords.x = pointerCoords.y;
1239 pointerCoords.y = - temp;
1240 break;
1241
1242 case InputReaderPolicyInterface::ROTATION_180:
1243 pointerCoords.x = - pointerCoords.x;
1244 pointerCoords.y = - pointerCoords.y;
1245 break;
1246
1247 case InputReaderPolicyInterface::ROTATION_270:
1248 temp = pointerCoords.x;
1249 pointerCoords.x = - pointerCoords.y;
1250 pointerCoords.y = temp;
1251 break;
1252 }
1253 }
1254 } // release lock
1255
Jeff Browne57e8952010-07-23 21:28:06 -07001256 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownb51719b2010-07-29 18:18:33 -07001257 int32_t pointerId = 0;
Jeff Brown90f0cee2010-10-08 22:31:17 -07001258 getDispatcher()->notifyMotion(when, getDeviceId(), AINPUT_SOURCE_TRACKBALL, 0,
Jeff Brownaf30ff62010-09-01 17:01:00 -07001259 motionEventAction, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown90f0cee2010-10-08 22:31:17 -07001260 1, &pointerId, &pointerCoords, mXPrecision, mYPrecision, downTime);
1261
1262 mAccumulator.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07001263}
1264
Jeff Brown8d4dfd22010-08-10 15:47:53 -07001265int32_t TrackballInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1266 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
1267 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1268 } else {
1269 return AKEY_STATE_UNKNOWN;
1270 }
1271}
1272
Jeff Browne57e8952010-07-23 21:28:06 -07001273
1274// --- TouchInputMapper ---
1275
1276TouchInputMapper::TouchInputMapper(InputDevice* device, int32_t associatedDisplayId) :
Jeff Brownb51719b2010-07-29 18:18:33 -07001277 InputMapper(device), mAssociatedDisplayId(associatedDisplayId) {
1278 mLocked.surfaceOrientation = -1;
1279 mLocked.surfaceWidth = -1;
1280 mLocked.surfaceHeight = -1;
1281
1282 initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -07001283}
1284
1285TouchInputMapper::~TouchInputMapper() {
1286}
1287
1288uint32_t TouchInputMapper::getSources() {
1289 return mAssociatedDisplayId >= 0 ? AINPUT_SOURCE_TOUCHSCREEN : AINPUT_SOURCE_TOUCHPAD;
1290}
1291
1292void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1293 InputMapper::populateDeviceInfo(info);
1294
Jeff Brownb51719b2010-07-29 18:18:33 -07001295 { // acquire lock
1296 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07001297
Jeff Brownb51719b2010-07-29 18:18:33 -07001298 // Ensure surface information is up to date so that orientation changes are
1299 // noticed immediately.
1300 configureSurfaceLocked();
1301
1302 info->addMotionRange(AINPUT_MOTION_RANGE_X, mLocked.orientedRanges.x);
1303 info->addMotionRange(AINPUT_MOTION_RANGE_Y, mLocked.orientedRanges.y);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001304
1305 if (mLocked.orientedRanges.havePressure) {
1306 info->addMotionRange(AINPUT_MOTION_RANGE_PRESSURE,
1307 mLocked.orientedRanges.pressure);
1308 }
1309
1310 if (mLocked.orientedRanges.haveSize) {
1311 info->addMotionRange(AINPUT_MOTION_RANGE_SIZE,
1312 mLocked.orientedRanges.size);
1313 }
1314
Jeff Brown6b337e72010-10-14 21:42:15 -07001315 if (mLocked.orientedRanges.haveTouchSize) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001316 info->addMotionRange(AINPUT_MOTION_RANGE_TOUCH_MAJOR,
1317 mLocked.orientedRanges.touchMajor);
1318 info->addMotionRange(AINPUT_MOTION_RANGE_TOUCH_MINOR,
1319 mLocked.orientedRanges.touchMinor);
1320 }
1321
Jeff Brown6b337e72010-10-14 21:42:15 -07001322 if (mLocked.orientedRanges.haveToolSize) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001323 info->addMotionRange(AINPUT_MOTION_RANGE_TOOL_MAJOR,
1324 mLocked.orientedRanges.toolMajor);
1325 info->addMotionRange(AINPUT_MOTION_RANGE_TOOL_MINOR,
1326 mLocked.orientedRanges.toolMinor);
1327 }
1328
1329 if (mLocked.orientedRanges.haveOrientation) {
1330 info->addMotionRange(AINPUT_MOTION_RANGE_ORIENTATION,
1331 mLocked.orientedRanges.orientation);
1332 }
Jeff Brownb51719b2010-07-29 18:18:33 -07001333 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07001334}
1335
Jeff Brown26c94ff2010-09-30 14:33:04 -07001336void TouchInputMapper::dump(String8& dump) {
1337 { // acquire lock
1338 AutoMutex _l(mLock);
1339 dump.append(INDENT2 "Touch Input Mapper:\n");
1340 dump.appendFormat(INDENT3 "AssociatedDisplayId: %d\n", mAssociatedDisplayId);
1341 dumpParameters(dump);
1342 dumpVirtualKeysLocked(dump);
1343 dumpRawAxes(dump);
1344 dumpCalibration(dump);
1345 dumpSurfaceLocked(dump);
Jeff Brown60b57762010-10-18 13:32:20 -07001346 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
Jeff Brown6b337e72010-10-14 21:42:15 -07001347 dump.appendFormat(INDENT4 "XOrigin: %d\n", mLocked.xOrigin);
1348 dump.appendFormat(INDENT4 "YOrigin: %d\n", mLocked.yOrigin);
1349 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mLocked.xScale);
1350 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mLocked.yScale);
1351 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mLocked.xPrecision);
1352 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mLocked.yPrecision);
1353 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mLocked.geometricScale);
1354 dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mLocked.toolSizeLinearScale);
1355 dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mLocked.toolSizeLinearBias);
1356 dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mLocked.toolSizeAreaScale);
1357 dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mLocked.toolSizeAreaBias);
1358 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mLocked.pressureScale);
1359 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mLocked.sizeScale);
1360 dump.appendFormat(INDENT4 "OrientationSCale: %0.3f\n", mLocked.orientationScale);
Jeff Brown26c94ff2010-09-30 14:33:04 -07001361 } // release lock
1362}
1363
Jeff Brownb51719b2010-07-29 18:18:33 -07001364void TouchInputMapper::initializeLocked() {
1365 mCurrentTouch.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07001366 mLastTouch.clear();
1367 mDownTime = 0;
Jeff Browne57e8952010-07-23 21:28:06 -07001368
1369 for (uint32_t i = 0; i < MAX_POINTERS; i++) {
1370 mAveragingTouchFilter.historyStart[i] = 0;
1371 mAveragingTouchFilter.historyEnd[i] = 0;
1372 }
1373
1374 mJumpyTouchFilter.jumpyPointsDropped = 0;
Jeff Brownb51719b2010-07-29 18:18:33 -07001375
1376 mLocked.currentVirtualKey.down = false;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001377
1378 mLocked.orientedRanges.havePressure = false;
1379 mLocked.orientedRanges.haveSize = false;
Jeff Brown6b337e72010-10-14 21:42:15 -07001380 mLocked.orientedRanges.haveTouchSize = false;
1381 mLocked.orientedRanges.haveToolSize = false;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001382 mLocked.orientedRanges.haveOrientation = false;
1383}
1384
Jeff Browne57e8952010-07-23 21:28:06 -07001385void TouchInputMapper::configure() {
1386 InputMapper::configure();
1387
1388 // Configure basic parameters.
Jeff Brown38a7fab2010-08-30 03:02:23 -07001389 configureParameters();
Jeff Browne57e8952010-07-23 21:28:06 -07001390
1391 // Configure absolute axis information.
Jeff Brown38a7fab2010-08-30 03:02:23 -07001392 configureRawAxes();
Jeff Brown38a7fab2010-08-30 03:02:23 -07001393
1394 // Prepare input device calibration.
1395 parseCalibration();
1396 resolveCalibration();
Jeff Browne57e8952010-07-23 21:28:06 -07001397
Jeff Brownb51719b2010-07-29 18:18:33 -07001398 { // acquire lock
1399 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07001400
Jeff Brown38a7fab2010-08-30 03:02:23 -07001401 // Configure surface dimensions and orientation.
Jeff Brownb51719b2010-07-29 18:18:33 -07001402 configureSurfaceLocked();
1403 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07001404}
1405
Jeff Brown38a7fab2010-08-30 03:02:23 -07001406void TouchInputMapper::configureParameters() {
1407 mParameters.useBadTouchFilter = getPolicy()->filterTouchEvents();
1408 mParameters.useAveragingTouchFilter = getPolicy()->filterTouchEvents();
1409 mParameters.useJumpyTouchFilter = getPolicy()->filterJumpyTouchEvents();
1410}
1411
Jeff Brown26c94ff2010-09-30 14:33:04 -07001412void TouchInputMapper::dumpParameters(String8& dump) {
1413 dump.appendFormat(INDENT3 "UseBadTouchFilter: %s\n",
1414 toString(mParameters.useBadTouchFilter));
1415 dump.appendFormat(INDENT3 "UseAveragingTouchFilter: %s\n",
1416 toString(mParameters.useAveragingTouchFilter));
1417 dump.appendFormat(INDENT3 "UseJumpyTouchFilter: %s\n",
1418 toString(mParameters.useJumpyTouchFilter));
Jeff Browna665ca82010-09-08 11:49:43 -07001419}
1420
Jeff Brown38a7fab2010-08-30 03:02:23 -07001421void TouchInputMapper::configureRawAxes() {
1422 mRawAxes.x.clear();
1423 mRawAxes.y.clear();
1424 mRawAxes.pressure.clear();
1425 mRawAxes.touchMajor.clear();
1426 mRawAxes.touchMinor.clear();
1427 mRawAxes.toolMajor.clear();
1428 mRawAxes.toolMinor.clear();
1429 mRawAxes.orientation.clear();
1430}
1431
Jeff Brown26c94ff2010-09-30 14:33:04 -07001432static void dumpAxisInfo(String8& dump, RawAbsoluteAxisInfo axis, const char* name) {
Jeff Browna665ca82010-09-08 11:49:43 -07001433 if (axis.valid) {
Jeff Brown26c94ff2010-09-30 14:33:04 -07001434 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d\n",
Jeff Browna665ca82010-09-08 11:49:43 -07001435 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz);
1436 } else {
Jeff Brown26c94ff2010-09-30 14:33:04 -07001437 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
Jeff Browna665ca82010-09-08 11:49:43 -07001438 }
1439}
1440
Jeff Brown26c94ff2010-09-30 14:33:04 -07001441void TouchInputMapper::dumpRawAxes(String8& dump) {
1442 dump.append(INDENT3 "Raw Axes:\n");
1443 dumpAxisInfo(dump, mRawAxes.x, "X");
1444 dumpAxisInfo(dump, mRawAxes.y, "Y");
1445 dumpAxisInfo(dump, mRawAxes.pressure, "Pressure");
1446 dumpAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor");
1447 dumpAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor");
1448 dumpAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor");
1449 dumpAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor");
1450 dumpAxisInfo(dump, mRawAxes.orientation, "Orientation");
Jeff Browne57e8952010-07-23 21:28:06 -07001451}
1452
Jeff Brownb51719b2010-07-29 18:18:33 -07001453bool TouchInputMapper::configureSurfaceLocked() {
Jeff Browne57e8952010-07-23 21:28:06 -07001454 // Update orientation and dimensions if needed.
1455 int32_t orientation;
1456 int32_t width, height;
1457 if (mAssociatedDisplayId >= 0) {
Jeff Brownb51719b2010-07-29 18:18:33 -07001458 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Browne57e8952010-07-23 21:28:06 -07001459 if (! getPolicy()->getDisplayInfo(mAssociatedDisplayId, & width, & height, & orientation)) {
1460 return false;
1461 }
1462 } else {
1463 orientation = InputReaderPolicyInterface::ROTATION_0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001464 width = mRawAxes.x.getRange();
1465 height = mRawAxes.y.getRange();
Jeff Browne57e8952010-07-23 21:28:06 -07001466 }
1467
Jeff Brownb51719b2010-07-29 18:18:33 -07001468 bool orientationChanged = mLocked.surfaceOrientation != orientation;
Jeff Browne57e8952010-07-23 21:28:06 -07001469 if (orientationChanged) {
Jeff Brownb51719b2010-07-29 18:18:33 -07001470 mLocked.surfaceOrientation = orientation;
Jeff Browne57e8952010-07-23 21:28:06 -07001471 }
1472
Jeff Brownb51719b2010-07-29 18:18:33 -07001473 bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height;
Jeff Browne57e8952010-07-23 21:28:06 -07001474 if (sizeChanged) {
Jeff Brown26c94ff2010-09-30 14:33:04 -07001475 LOGI("Device reconfigured: id=0x%x, name=%s, display size is now %dx%d",
1476 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001477
Jeff Brownb51719b2010-07-29 18:18:33 -07001478 mLocked.surfaceWidth = width;
1479 mLocked.surfaceHeight = height;
Jeff Browne57e8952010-07-23 21:28:06 -07001480
Jeff Brown38a7fab2010-08-30 03:02:23 -07001481 // Configure X and Y factors.
1482 if (mRawAxes.x.valid && mRawAxes.y.valid) {
Jeff Brown60b57762010-10-18 13:32:20 -07001483 mLocked.xOrigin = mCalibration.haveXOrigin
1484 ? mCalibration.xOrigin
1485 : mRawAxes.x.minValue;
1486 mLocked.yOrigin = mCalibration.haveYOrigin
1487 ? mCalibration.yOrigin
1488 : mRawAxes.y.minValue;
1489 mLocked.xScale = mCalibration.haveXScale
1490 ? mCalibration.xScale
1491 : float(width) / mRawAxes.x.getRange();
1492 mLocked.yScale = mCalibration.haveYScale
1493 ? mCalibration.yScale
1494 : float(height) / mRawAxes.y.getRange();
Jeff Brownb51719b2010-07-29 18:18:33 -07001495 mLocked.xPrecision = 1.0f / mLocked.xScale;
1496 mLocked.yPrecision = 1.0f / mLocked.yScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001497
Jeff Brownb51719b2010-07-29 18:18:33 -07001498 configureVirtualKeysLocked();
Jeff Browne57e8952010-07-23 21:28:06 -07001499 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001500 LOGW(INDENT "Touch device did not report support for X or Y axis!");
Jeff Brownb51719b2010-07-29 18:18:33 -07001501 mLocked.xOrigin = 0;
1502 mLocked.yOrigin = 0;
1503 mLocked.xScale = 1.0f;
1504 mLocked.yScale = 1.0f;
1505 mLocked.xPrecision = 1.0f;
1506 mLocked.yPrecision = 1.0f;
Jeff Browne57e8952010-07-23 21:28:06 -07001507 }
1508
Jeff Brown38a7fab2010-08-30 03:02:23 -07001509 // Scale factor for terms that are not oriented in a particular axis.
1510 // If the pixels are square then xScale == yScale otherwise we fake it
1511 // by choosing an average.
1512 mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale);
Jeff Browne57e8952010-07-23 21:28:06 -07001513
Jeff Brown38a7fab2010-08-30 03:02:23 -07001514 // Size of diagonal axis.
1515 float diagonalSize = pythag(width, height);
Jeff Browne57e8952010-07-23 21:28:06 -07001516
Jeff Brown38a7fab2010-08-30 03:02:23 -07001517 // TouchMajor and TouchMinor factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001518 if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) {
1519 mLocked.orientedRanges.haveTouchSize = true;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001520 mLocked.orientedRanges.touchMajor.min = 0;
1521 mLocked.orientedRanges.touchMajor.max = diagonalSize;
1522 mLocked.orientedRanges.touchMajor.flat = 0;
1523 mLocked.orientedRanges.touchMajor.fuzz = 0;
1524 mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor;
1525 }
Jeff Brownb51719b2010-07-29 18:18:33 -07001526
Jeff Brown38a7fab2010-08-30 03:02:23 -07001527 // ToolMajor and ToolMinor factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001528 mLocked.toolSizeLinearScale = 0;
1529 mLocked.toolSizeLinearBias = 0;
1530 mLocked.toolSizeAreaScale = 0;
1531 mLocked.toolSizeAreaBias = 0;
1532 if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
1533 if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) {
1534 if (mCalibration.haveToolSizeLinearScale) {
1535 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001536 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
Jeff Brown6b337e72010-10-14 21:42:15 -07001537 mLocked.toolSizeLinearScale = float(min(width, height))
Jeff Brown38a7fab2010-08-30 03:02:23 -07001538 / mRawAxes.toolMajor.maxValue;
1539 }
1540
Jeff Brown6b337e72010-10-14 21:42:15 -07001541 if (mCalibration.haveToolSizeLinearBias) {
1542 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
1543 }
1544 } else if (mCalibration.toolSizeCalibration ==
1545 Calibration::TOOL_SIZE_CALIBRATION_AREA) {
1546 if (mCalibration.haveToolSizeLinearScale) {
1547 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
1548 } else {
1549 mLocked.toolSizeLinearScale = min(width, height);
1550 }
1551
1552 if (mCalibration.haveToolSizeLinearBias) {
1553 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
1554 }
1555
1556 if (mCalibration.haveToolSizeAreaScale) {
1557 mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale;
1558 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1559 mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue;
1560 }
1561
1562 if (mCalibration.haveToolSizeAreaBias) {
1563 mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001564 }
1565 }
1566
Jeff Brown6b337e72010-10-14 21:42:15 -07001567 mLocked.orientedRanges.haveToolSize = true;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001568 mLocked.orientedRanges.toolMajor.min = 0;
1569 mLocked.orientedRanges.toolMajor.max = diagonalSize;
1570 mLocked.orientedRanges.toolMajor.flat = 0;
1571 mLocked.orientedRanges.toolMajor.fuzz = 0;
1572 mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor;
1573 }
1574
1575 // Pressure factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001576 mLocked.pressureScale = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001577 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
1578 RawAbsoluteAxisInfo rawPressureAxis;
1579 switch (mCalibration.pressureSource) {
1580 case Calibration::PRESSURE_SOURCE_PRESSURE:
1581 rawPressureAxis = mRawAxes.pressure;
1582 break;
1583 case Calibration::PRESSURE_SOURCE_TOUCH:
1584 rawPressureAxis = mRawAxes.touchMajor;
1585 break;
1586 default:
1587 rawPressureAxis.clear();
1588 }
1589
Jeff Brown38a7fab2010-08-30 03:02:23 -07001590 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
1591 || mCalibration.pressureCalibration
1592 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
1593 if (mCalibration.havePressureScale) {
1594 mLocked.pressureScale = mCalibration.pressureScale;
1595 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
1596 mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue;
1597 }
1598 }
1599
1600 mLocked.orientedRanges.havePressure = true;
1601 mLocked.orientedRanges.pressure.min = 0;
1602 mLocked.orientedRanges.pressure.max = 1.0;
1603 mLocked.orientedRanges.pressure.flat = 0;
1604 mLocked.orientedRanges.pressure.fuzz = 0;
1605 }
1606
1607 // Size factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001608 mLocked.sizeScale = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001609 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001610 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
1611 if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1612 mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue;
1613 }
1614 }
1615
1616 mLocked.orientedRanges.haveSize = true;
1617 mLocked.orientedRanges.size.min = 0;
1618 mLocked.orientedRanges.size.max = 1.0;
1619 mLocked.orientedRanges.size.flat = 0;
1620 mLocked.orientedRanges.size.fuzz = 0;
1621 }
1622
1623 // Orientation
Jeff Brown6b337e72010-10-14 21:42:15 -07001624 mLocked.orientationScale = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001625 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001626 if (mCalibration.orientationCalibration
1627 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
1628 if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) {
1629 mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue;
1630 }
1631 }
1632
1633 mLocked.orientedRanges.orientation.min = - M_PI_2;
1634 mLocked.orientedRanges.orientation.max = M_PI_2;
1635 mLocked.orientedRanges.orientation.flat = 0;
1636 mLocked.orientedRanges.orientation.fuzz = 0;
1637 }
Jeff Browne57e8952010-07-23 21:28:06 -07001638 }
1639
1640 if (orientationChanged || sizeChanged) {
1641 // Compute oriented surface dimensions, precision, and scales.
1642 float orientedXScale, orientedYScale;
Jeff Brownb51719b2010-07-29 18:18:33 -07001643 switch (mLocked.surfaceOrientation) {
Jeff Browne57e8952010-07-23 21:28:06 -07001644 case InputReaderPolicyInterface::ROTATION_90:
1645 case InputReaderPolicyInterface::ROTATION_270:
Jeff Brownb51719b2010-07-29 18:18:33 -07001646 mLocked.orientedSurfaceWidth = mLocked.surfaceHeight;
1647 mLocked.orientedSurfaceHeight = mLocked.surfaceWidth;
1648 mLocked.orientedXPrecision = mLocked.yPrecision;
1649 mLocked.orientedYPrecision = mLocked.xPrecision;
1650 orientedXScale = mLocked.yScale;
1651 orientedYScale = mLocked.xScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001652 break;
1653 default:
Jeff Brownb51719b2010-07-29 18:18:33 -07001654 mLocked.orientedSurfaceWidth = mLocked.surfaceWidth;
1655 mLocked.orientedSurfaceHeight = mLocked.surfaceHeight;
1656 mLocked.orientedXPrecision = mLocked.xPrecision;
1657 mLocked.orientedYPrecision = mLocked.yPrecision;
1658 orientedXScale = mLocked.xScale;
1659 orientedYScale = mLocked.yScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001660 break;
1661 }
1662
1663 // Configure position ranges.
Jeff Brownb51719b2010-07-29 18:18:33 -07001664 mLocked.orientedRanges.x.min = 0;
1665 mLocked.orientedRanges.x.max = mLocked.orientedSurfaceWidth;
1666 mLocked.orientedRanges.x.flat = 0;
1667 mLocked.orientedRanges.x.fuzz = orientedXScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001668
Jeff Brownb51719b2010-07-29 18:18:33 -07001669 mLocked.orientedRanges.y.min = 0;
1670 mLocked.orientedRanges.y.max = mLocked.orientedSurfaceHeight;
1671 mLocked.orientedRanges.y.flat = 0;
1672 mLocked.orientedRanges.y.fuzz = orientedYScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001673 }
1674
1675 return true;
1676}
1677
Jeff Brown26c94ff2010-09-30 14:33:04 -07001678void TouchInputMapper::dumpSurfaceLocked(String8& dump) {
1679 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth);
1680 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight);
1681 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation);
Jeff Browna665ca82010-09-08 11:49:43 -07001682}
1683
Jeff Brownb51719b2010-07-29 18:18:33 -07001684void TouchInputMapper::configureVirtualKeysLocked() {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001685 assert(mRawAxes.x.valid && mRawAxes.y.valid);
Jeff Browne57e8952010-07-23 21:28:06 -07001686
Jeff Brownb51719b2010-07-29 18:18:33 -07001687 // Note: getVirtualKeyDefinitions is non-reentrant so we can continue holding the lock.
Jeff Brown38a7fab2010-08-30 03:02:23 -07001688 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Browne57e8952010-07-23 21:28:06 -07001689 getPolicy()->getVirtualKeyDefinitions(getDeviceName(), virtualKeyDefinitions);
1690
Jeff Brownb51719b2010-07-29 18:18:33 -07001691 mLocked.virtualKeys.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07001692
Jeff Brownb51719b2010-07-29 18:18:33 -07001693 if (virtualKeyDefinitions.size() == 0) {
1694 return;
1695 }
Jeff Browne57e8952010-07-23 21:28:06 -07001696
Jeff Brownb51719b2010-07-29 18:18:33 -07001697 mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size());
1698
Jeff Brown38a7fab2010-08-30 03:02:23 -07001699 int32_t touchScreenLeft = mRawAxes.x.minValue;
1700 int32_t touchScreenTop = mRawAxes.y.minValue;
1701 int32_t touchScreenWidth = mRawAxes.x.getRange();
1702 int32_t touchScreenHeight = mRawAxes.y.getRange();
Jeff Brownb51719b2010-07-29 18:18:33 -07001703
1704 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001705 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brownb51719b2010-07-29 18:18:33 -07001706 virtualKeyDefinitions[i];
1707
1708 mLocked.virtualKeys.add();
1709 VirtualKey& virtualKey = mLocked.virtualKeys.editTop();
1710
1711 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1712 int32_t keyCode;
1713 uint32_t flags;
1714 if (getEventHub()->scancodeToKeycode(getDeviceId(), virtualKey.scanCode,
1715 & keyCode, & flags)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001716 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
1717 virtualKey.scanCode);
Jeff Brownb51719b2010-07-29 18:18:33 -07001718 mLocked.virtualKeys.pop(); // drop the key
1719 continue;
Jeff Browne57e8952010-07-23 21:28:06 -07001720 }
1721
Jeff Brownb51719b2010-07-29 18:18:33 -07001722 virtualKey.keyCode = keyCode;
1723 virtualKey.flags = flags;
Jeff Browne57e8952010-07-23 21:28:06 -07001724
Jeff Brownb51719b2010-07-29 18:18:33 -07001725 // convert the key definition's display coordinates into touch coordinates for a hit box
1726 int32_t halfWidth = virtualKeyDefinition.width / 2;
1727 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Browne57e8952010-07-23 21:28:06 -07001728
Jeff Brownb51719b2010-07-29 18:18:33 -07001729 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
1730 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1731 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
1732 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1733 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
1734 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
1735 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
1736 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
Jeff Browne57e8952010-07-23 21:28:06 -07001737
Jeff Brown26c94ff2010-09-30 14:33:04 -07001738 }
1739}
1740
1741void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) {
1742 if (!mLocked.virtualKeys.isEmpty()) {
1743 dump.append(INDENT3 "Virtual Keys:\n");
1744
1745 for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) {
1746 const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i);
1747 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
1748 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1749 i, virtualKey.scanCode, virtualKey.keyCode,
1750 virtualKey.hitLeft, virtualKey.hitRight,
1751 virtualKey.hitTop, virtualKey.hitBottom);
1752 }
Jeff Brownb51719b2010-07-29 18:18:33 -07001753 }
Jeff Browne57e8952010-07-23 21:28:06 -07001754}
1755
Jeff Brown38a7fab2010-08-30 03:02:23 -07001756void TouchInputMapper::parseCalibration() {
1757 const InputDeviceCalibration& in = getDevice()->getCalibration();
1758 Calibration& out = mCalibration;
1759
Jeff Brown60b57762010-10-18 13:32:20 -07001760 // Position
1761 out.haveXOrigin = in.tryGetProperty(String8("touch.position.xOrigin"), out.xOrigin);
1762 out.haveYOrigin = in.tryGetProperty(String8("touch.position.yOrigin"), out.yOrigin);
1763 out.haveXScale = in.tryGetProperty(String8("touch.position.xScale"), out.xScale);
1764 out.haveYScale = in.tryGetProperty(String8("touch.position.yScale"), out.yScale);
1765
Jeff Brown6b337e72010-10-14 21:42:15 -07001766 // Touch Size
1767 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT;
1768 String8 touchSizeCalibrationString;
1769 if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) {
1770 if (touchSizeCalibrationString == "none") {
1771 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
1772 } else if (touchSizeCalibrationString == "geometric") {
1773 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC;
1774 } else if (touchSizeCalibrationString == "pressure") {
1775 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
1776 } else if (touchSizeCalibrationString != "default") {
1777 LOGW("Invalid value for touch.touchSize.calibration: '%s'",
1778 touchSizeCalibrationString.string());
Jeff Brown38a7fab2010-08-30 03:02:23 -07001779 }
1780 }
1781
Jeff Brown6b337e72010-10-14 21:42:15 -07001782 // Tool Size
1783 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT;
1784 String8 toolSizeCalibrationString;
1785 if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) {
1786 if (toolSizeCalibrationString == "none") {
1787 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
1788 } else if (toolSizeCalibrationString == "geometric") {
1789 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC;
1790 } else if (toolSizeCalibrationString == "linear") {
1791 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
1792 } else if (toolSizeCalibrationString == "area") {
1793 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA;
1794 } else if (toolSizeCalibrationString != "default") {
1795 LOGW("Invalid value for touch.toolSize.calibration: '%s'",
1796 toolSizeCalibrationString.string());
Jeff Brown38a7fab2010-08-30 03:02:23 -07001797 }
1798 }
1799
Jeff Brown6b337e72010-10-14 21:42:15 -07001800 out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"),
1801 out.toolSizeLinearScale);
1802 out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"),
1803 out.toolSizeLinearBias);
1804 out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"),
1805 out.toolSizeAreaScale);
1806 out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"),
1807 out.toolSizeAreaBias);
1808 out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"),
1809 out.toolSizeIsSummed);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001810
1811 // Pressure
1812 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
1813 String8 pressureCalibrationString;
Jeff Brown6b337e72010-10-14 21:42:15 -07001814 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001815 if (pressureCalibrationString == "none") {
1816 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
1817 } else if (pressureCalibrationString == "physical") {
1818 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
1819 } else if (pressureCalibrationString == "amplitude") {
1820 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
1821 } else if (pressureCalibrationString != "default") {
Jeff Brown6b337e72010-10-14 21:42:15 -07001822 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown38a7fab2010-08-30 03:02:23 -07001823 pressureCalibrationString.string());
1824 }
1825 }
1826
1827 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
1828 String8 pressureSourceString;
1829 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
1830 if (pressureSourceString == "pressure") {
1831 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
1832 } else if (pressureSourceString == "touch") {
1833 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
1834 } else if (pressureSourceString != "default") {
1835 LOGW("Invalid value for touch.pressure.source: '%s'",
1836 pressureSourceString.string());
1837 }
1838 }
1839
1840 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
1841 out.pressureScale);
1842
1843 // Size
1844 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
1845 String8 sizeCalibrationString;
Jeff Brown6b337e72010-10-14 21:42:15 -07001846 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001847 if (sizeCalibrationString == "none") {
1848 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
1849 } else if (sizeCalibrationString == "normalized") {
1850 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
1851 } else if (sizeCalibrationString != "default") {
Jeff Brown6b337e72010-10-14 21:42:15 -07001852 LOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Brown38a7fab2010-08-30 03:02:23 -07001853 sizeCalibrationString.string());
1854 }
1855 }
1856
1857 // Orientation
1858 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
1859 String8 orientationCalibrationString;
Jeff Brown6b337e72010-10-14 21:42:15 -07001860 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001861 if (orientationCalibrationString == "none") {
1862 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
1863 } else if (orientationCalibrationString == "interpolated") {
1864 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
1865 } else if (orientationCalibrationString != "default") {
Jeff Brown6b337e72010-10-14 21:42:15 -07001866 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown38a7fab2010-08-30 03:02:23 -07001867 orientationCalibrationString.string());
1868 }
1869 }
1870}
1871
1872void TouchInputMapper::resolveCalibration() {
1873 // Pressure
1874 switch (mCalibration.pressureSource) {
1875 case Calibration::PRESSURE_SOURCE_DEFAULT:
1876 if (mRawAxes.pressure.valid) {
1877 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
1878 } else if (mRawAxes.touchMajor.valid) {
1879 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
1880 }
1881 break;
1882
1883 case Calibration::PRESSURE_SOURCE_PRESSURE:
1884 if (! mRawAxes.pressure.valid) {
1885 LOGW("Calibration property touch.pressure.source is 'pressure' but "
1886 "the pressure axis is not available.");
1887 }
1888 break;
1889
1890 case Calibration::PRESSURE_SOURCE_TOUCH:
1891 if (! mRawAxes.touchMajor.valid) {
1892 LOGW("Calibration property touch.pressure.source is 'touch' but "
1893 "the touchMajor axis is not available.");
1894 }
1895 break;
1896
1897 default:
1898 break;
1899 }
1900
1901 switch (mCalibration.pressureCalibration) {
1902 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
1903 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
1904 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
1905 } else {
1906 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
1907 }
1908 break;
1909
1910 default:
1911 break;
1912 }
1913
Jeff Brown6b337e72010-10-14 21:42:15 -07001914 // Tool Size
1915 switch (mCalibration.toolSizeCalibration) {
1916 case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT:
Jeff Brown38a7fab2010-08-30 03:02:23 -07001917 if (mRawAxes.toolMajor.valid) {
Jeff Brown6b337e72010-10-14 21:42:15 -07001918 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001919 } else {
Jeff Brown6b337e72010-10-14 21:42:15 -07001920 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001921 }
1922 break;
1923
1924 default:
1925 break;
1926 }
1927
Jeff Brown6b337e72010-10-14 21:42:15 -07001928 // Touch Size
1929 switch (mCalibration.touchSizeCalibration) {
1930 case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT:
Jeff Brown38a7fab2010-08-30 03:02:23 -07001931 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
Jeff Brown6b337e72010-10-14 21:42:15 -07001932 && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
1933 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001934 } else {
Jeff Brown6b337e72010-10-14 21:42:15 -07001935 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001936 }
1937 break;
1938
1939 default:
1940 break;
1941 }
1942
1943 // Size
1944 switch (mCalibration.sizeCalibration) {
1945 case Calibration::SIZE_CALIBRATION_DEFAULT:
1946 if (mRawAxes.toolMajor.valid) {
1947 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
1948 } else {
1949 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
1950 }
1951 break;
1952
1953 default:
1954 break;
1955 }
1956
1957 // Orientation
1958 switch (mCalibration.orientationCalibration) {
1959 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
1960 if (mRawAxes.orientation.valid) {
1961 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
1962 } else {
1963 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
1964 }
1965 break;
1966
1967 default:
1968 break;
1969 }
1970}
1971
Jeff Brown26c94ff2010-09-30 14:33:04 -07001972void TouchInputMapper::dumpCalibration(String8& dump) {
1973 dump.append(INDENT3 "Calibration:\n");
Jeff Browna665ca82010-09-08 11:49:43 -07001974
Jeff Brown60b57762010-10-18 13:32:20 -07001975 // Position
1976 if (mCalibration.haveXOrigin) {
1977 dump.appendFormat(INDENT4 "touch.position.xOrigin: %d\n", mCalibration.xOrigin);
1978 }
1979 if (mCalibration.haveYOrigin) {
1980 dump.appendFormat(INDENT4 "touch.position.yOrigin: %d\n", mCalibration.yOrigin);
1981 }
1982 if (mCalibration.haveXScale) {
1983 dump.appendFormat(INDENT4 "touch.position.xScale: %0.3f\n", mCalibration.xScale);
1984 }
1985 if (mCalibration.haveYScale) {
1986 dump.appendFormat(INDENT4 "touch.position.yScale: %0.3f\n", mCalibration.yScale);
1987 }
1988
Jeff Brown6b337e72010-10-14 21:42:15 -07001989 // Touch Size
1990 switch (mCalibration.touchSizeCalibration) {
1991 case Calibration::TOUCH_SIZE_CALIBRATION_NONE:
1992 dump.append(INDENT4 "touch.touchSize.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001993 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001994 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
1995 dump.append(INDENT4 "touch.touchSize.calibration: geometric\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001996 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001997 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
1998 dump.append(INDENT4 "touch.touchSize.calibration: pressure\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001999 break;
2000 default:
2001 assert(false);
2002 }
2003
Jeff Brown6b337e72010-10-14 21:42:15 -07002004 // Tool Size
2005 switch (mCalibration.toolSizeCalibration) {
2006 case Calibration::TOOL_SIZE_CALIBRATION_NONE:
2007 dump.append(INDENT4 "touch.toolSize.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002008 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002009 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
2010 dump.append(INDENT4 "touch.toolSize.calibration: geometric\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002011 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002012 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
2013 dump.append(INDENT4 "touch.toolSize.calibration: linear\n");
2014 break;
2015 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2016 dump.append(INDENT4 "touch.toolSize.calibration: area\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002017 break;
2018 default:
2019 assert(false);
2020 }
2021
Jeff Brown6b337e72010-10-14 21:42:15 -07002022 if (mCalibration.haveToolSizeLinearScale) {
2023 dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n",
2024 mCalibration.toolSizeLinearScale);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002025 }
2026
Jeff Brown6b337e72010-10-14 21:42:15 -07002027 if (mCalibration.haveToolSizeLinearBias) {
2028 dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n",
2029 mCalibration.toolSizeLinearBias);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002030 }
2031
Jeff Brown6b337e72010-10-14 21:42:15 -07002032 if (mCalibration.haveToolSizeAreaScale) {
2033 dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n",
2034 mCalibration.toolSizeAreaScale);
2035 }
2036
2037 if (mCalibration.haveToolSizeAreaBias) {
2038 dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n",
2039 mCalibration.toolSizeAreaBias);
2040 }
2041
2042 if (mCalibration.haveToolSizeIsSummed) {
2043 dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %d\n",
2044 mCalibration.toolSizeIsSummed);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002045 }
2046
2047 // Pressure
2048 switch (mCalibration.pressureCalibration) {
2049 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002050 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002051 break;
2052 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002053 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002054 break;
2055 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002056 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002057 break;
2058 default:
2059 assert(false);
2060 }
2061
2062 switch (mCalibration.pressureSource) {
2063 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002064 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002065 break;
2066 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002067 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002068 break;
2069 case Calibration::PRESSURE_SOURCE_DEFAULT:
2070 break;
2071 default:
2072 assert(false);
2073 }
2074
2075 if (mCalibration.havePressureScale) {
Jeff Brown26c94ff2010-09-30 14:33:04 -07002076 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
2077 mCalibration.pressureScale);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002078 }
2079
2080 // Size
2081 switch (mCalibration.sizeCalibration) {
2082 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002083 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002084 break;
2085 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002086 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002087 break;
2088 default:
2089 assert(false);
2090 }
2091
2092 // Orientation
2093 switch (mCalibration.orientationCalibration) {
2094 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002095 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002096 break;
2097 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002098 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002099 break;
2100 default:
2101 assert(false);
2102 }
2103}
2104
Jeff Browne57e8952010-07-23 21:28:06 -07002105void TouchInputMapper::reset() {
2106 // Synthesize touch up event if touch is currently down.
2107 // This will also take care of finishing virtual key processing if needed.
2108 if (mLastTouch.pointerCount != 0) {
2109 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
2110 mCurrentTouch.clear();
2111 syncTouch(when, true);
2112 }
2113
Jeff Brownb51719b2010-07-29 18:18:33 -07002114 { // acquire lock
2115 AutoMutex _l(mLock);
2116 initializeLocked();
2117 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07002118
Jeff Brownb51719b2010-07-29 18:18:33 -07002119 InputMapper::reset();
Jeff Browne57e8952010-07-23 21:28:06 -07002120}
2121
2122void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Browne839a582010-04-22 18:58:52 -07002123 uint32_t policyFlags = 0;
Jeff Browne839a582010-04-22 18:58:52 -07002124
Jeff Brownb51719b2010-07-29 18:18:33 -07002125 // Preprocess pointer data.
Jeff Browne839a582010-04-22 18:58:52 -07002126
Jeff Browne57e8952010-07-23 21:28:06 -07002127 if (mParameters.useBadTouchFilter) {
2128 if (applyBadTouchFilter()) {
Jeff Browne839a582010-04-22 18:58:52 -07002129 havePointerIds = false;
2130 }
2131 }
2132
Jeff Browne57e8952010-07-23 21:28:06 -07002133 if (mParameters.useJumpyTouchFilter) {
2134 if (applyJumpyTouchFilter()) {
Jeff Browne839a582010-04-22 18:58:52 -07002135 havePointerIds = false;
2136 }
2137 }
2138
2139 if (! havePointerIds) {
Jeff Browne57e8952010-07-23 21:28:06 -07002140 calculatePointerIds();
Jeff Browne839a582010-04-22 18:58:52 -07002141 }
2142
Jeff Browne57e8952010-07-23 21:28:06 -07002143 TouchData temp;
2144 TouchData* savedTouch;
2145 if (mParameters.useAveragingTouchFilter) {
2146 temp.copyFrom(mCurrentTouch);
Jeff Browne839a582010-04-22 18:58:52 -07002147 savedTouch = & temp;
2148
Jeff Browne57e8952010-07-23 21:28:06 -07002149 applyAveragingTouchFilter();
Jeff Browne839a582010-04-22 18:58:52 -07002150 } else {
Jeff Browne57e8952010-07-23 21:28:06 -07002151 savedTouch = & mCurrentTouch;
Jeff Browne839a582010-04-22 18:58:52 -07002152 }
2153
Jeff Brownb51719b2010-07-29 18:18:33 -07002154 // Process touches and virtual keys.
Jeff Browne839a582010-04-22 18:58:52 -07002155
Jeff Browne57e8952010-07-23 21:28:06 -07002156 TouchResult touchResult = consumeOffScreenTouches(when, policyFlags);
2157 if (touchResult == DISPATCH_TOUCH) {
2158 dispatchTouches(when, policyFlags);
Jeff Browne839a582010-04-22 18:58:52 -07002159 }
2160
Jeff Brownb51719b2010-07-29 18:18:33 -07002161 // Copy current touch to last touch in preparation for the next cycle.
Jeff Browne839a582010-04-22 18:58:52 -07002162
Jeff Browne57e8952010-07-23 21:28:06 -07002163 if (touchResult == DROP_STROKE) {
2164 mLastTouch.clear();
2165 } else {
2166 mLastTouch.copyFrom(*savedTouch);
Jeff Browne839a582010-04-22 18:58:52 -07002167 }
Jeff Browne839a582010-04-22 18:58:52 -07002168}
2169
Jeff Browne57e8952010-07-23 21:28:06 -07002170TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches(
2171 nsecs_t when, uint32_t policyFlags) {
2172 int32_t keyEventAction, keyEventFlags;
2173 int32_t keyCode, scanCode, downTime;
2174 TouchResult touchResult;
Jeff Brown50de30a2010-06-22 01:27:15 -07002175
Jeff Brownb51719b2010-07-29 18:18:33 -07002176 { // acquire lock
2177 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07002178
Jeff Brownb51719b2010-07-29 18:18:33 -07002179 // Update surface size and orientation, including virtual key positions.
2180 if (! configureSurfaceLocked()) {
2181 return DROP_STROKE;
2182 }
2183
2184 // Check for virtual key press.
2185 if (mLocked.currentVirtualKey.down) {
Jeff Browne57e8952010-07-23 21:28:06 -07002186 if (mCurrentTouch.pointerCount == 0) {
2187 // Pointer went up while virtual key was down.
Jeff Brownb51719b2010-07-29 18:18:33 -07002188 mLocked.currentVirtualKey.down = false;
Jeff Browne57e8952010-07-23 21:28:06 -07002189#if DEBUG_VIRTUAL_KEYS
2190 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brown3c3cc622010-10-20 15:33:38 -07002191 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Browne57e8952010-07-23 21:28:06 -07002192#endif
2193 keyEventAction = AKEY_EVENT_ACTION_UP;
2194 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2195 touchResult = SKIP_TOUCH;
2196 goto DispatchVirtualKey;
2197 }
2198
2199 if (mCurrentTouch.pointerCount == 1) {
2200 int32_t x = mCurrentTouch.pointers[0].x;
2201 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brownb51719b2010-07-29 18:18:33 -07002202 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
2203 if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07002204 // Pointer is still within the space of the virtual key.
2205 return SKIP_TOUCH;
2206 }
2207 }
2208
2209 // Pointer left virtual key area or another pointer also went down.
2210 // Send key cancellation and drop the stroke so subsequent motions will be
2211 // considered fresh downs. This is useful when the user swipes away from the
2212 // virtual key area into the main display surface.
Jeff Brownb51719b2010-07-29 18:18:33 -07002213 mLocked.currentVirtualKey.down = false;
Jeff Browne57e8952010-07-23 21:28:06 -07002214#if DEBUG_VIRTUAL_KEYS
2215 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brown3c3cc622010-10-20 15:33:38 -07002216 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Browne57e8952010-07-23 21:28:06 -07002217#endif
2218 keyEventAction = AKEY_EVENT_ACTION_UP;
2219 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
2220 | AKEY_EVENT_FLAG_CANCELED;
Jeff Brown3c3cc622010-10-20 15:33:38 -07002221
2222 // Check whether the pointer moved inside the display area where we should
2223 // start a new stroke.
2224 int32_t x = mCurrentTouch.pointers[0].x;
2225 int32_t y = mCurrentTouch.pointers[0].y;
2226 if (isPointInsideSurfaceLocked(x, y)) {
2227 mLastTouch.clear();
2228 touchResult = DISPATCH_TOUCH;
2229 } else {
2230 touchResult = DROP_STROKE;
2231 }
Jeff Browne57e8952010-07-23 21:28:06 -07002232 } else {
2233 if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) {
2234 // Pointer just went down. Handle off-screen touches, if needed.
2235 int32_t x = mCurrentTouch.pointers[0].x;
2236 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brownb51719b2010-07-29 18:18:33 -07002237 if (! isPointInsideSurfaceLocked(x, y)) {
Jeff Browne57e8952010-07-23 21:28:06 -07002238 // If exactly one pointer went down, check for virtual key hit.
2239 // Otherwise we will drop the entire stroke.
2240 if (mCurrentTouch.pointerCount == 1) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002241 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
Jeff Browne57e8952010-07-23 21:28:06 -07002242 if (virtualKey) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002243 mLocked.currentVirtualKey.down = true;
2244 mLocked.currentVirtualKey.downTime = when;
2245 mLocked.currentVirtualKey.keyCode = virtualKey->keyCode;
2246 mLocked.currentVirtualKey.scanCode = virtualKey->scanCode;
Jeff Browne57e8952010-07-23 21:28:06 -07002247#if DEBUG_VIRTUAL_KEYS
2248 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brown3c3cc622010-10-20 15:33:38 -07002249 mLocked.currentVirtualKey.keyCode,
2250 mLocked.currentVirtualKey.scanCode);
Jeff Browne57e8952010-07-23 21:28:06 -07002251#endif
2252 keyEventAction = AKEY_EVENT_ACTION_DOWN;
2253 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM
2254 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2255 touchResult = SKIP_TOUCH;
2256 goto DispatchVirtualKey;
2257 }
2258 }
2259 return DROP_STROKE;
2260 }
2261 }
2262 return DISPATCH_TOUCH;
2263 }
2264
2265 DispatchVirtualKey:
2266 // Collect remaining state needed to dispatch virtual key.
Jeff Brownb51719b2010-07-29 18:18:33 -07002267 keyCode = mLocked.currentVirtualKey.keyCode;
2268 scanCode = mLocked.currentVirtualKey.scanCode;
2269 downTime = mLocked.currentVirtualKey.downTime;
2270 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07002271
2272 // Dispatch virtual key.
2273 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown956c0fb2010-10-01 14:55:30 -07002274 policyFlags |= POLICY_FLAG_VIRTUAL;
Jeff Brown90f0cee2010-10-08 22:31:17 -07002275 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
2276 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
2277 return touchResult;
Jeff Browne839a582010-04-22 18:58:52 -07002278}
2279
Jeff Browne57e8952010-07-23 21:28:06 -07002280void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
2281 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2282 uint32_t lastPointerCount = mLastTouch.pointerCount;
Jeff Browne839a582010-04-22 18:58:52 -07002283 if (currentPointerCount == 0 && lastPointerCount == 0) {
2284 return; // nothing to do!
2285 }
2286
Jeff Browne57e8952010-07-23 21:28:06 -07002287 BitSet32 currentIdBits = mCurrentTouch.idBits;
2288 BitSet32 lastIdBits = mLastTouch.idBits;
Jeff Browne839a582010-04-22 18:58:52 -07002289
2290 if (currentIdBits == lastIdBits) {
2291 // No pointer id changes so this is a move event.
2292 // The dispatcher takes care of batching moves so we don't have to deal with that here.
Jeff Brown5c1ed842010-07-14 18:48:53 -07002293 int32_t motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browne57e8952010-07-23 21:28:06 -07002294 dispatchTouch(when, policyFlags, & mCurrentTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002295 currentIdBits, -1, currentPointerCount, motionEventAction);
Jeff Browne839a582010-04-22 18:58:52 -07002296 } else {
Jeff Brown3c3cc622010-10-20 15:33:38 -07002297 // There may be pointers going up and pointers going down and pointers moving
2298 // all at the same time.
Jeff Browne839a582010-04-22 18:58:52 -07002299 BitSet32 upIdBits(lastIdBits.value & ~ currentIdBits.value);
2300 BitSet32 downIdBits(currentIdBits.value & ~ lastIdBits.value);
2301 BitSet32 activeIdBits(lastIdBits.value);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002302 uint32_t pointerCount = lastPointerCount;
Jeff Browne839a582010-04-22 18:58:52 -07002303
Jeff Brown3c3cc622010-10-20 15:33:38 -07002304 // Produce an intermediate representation of the touch data that consists of the
2305 // old location of pointers that have just gone up and the new location of pointers that
2306 // have just moved but omits the location of pointers that have just gone down.
2307 TouchData interimTouch;
2308 interimTouch.copyFrom(mLastTouch);
2309
2310 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
2311 bool moveNeeded = false;
2312 while (!moveIdBits.isEmpty()) {
2313 uint32_t moveId = moveIdBits.firstMarkedBit();
2314 moveIdBits.clearBit(moveId);
2315
2316 int32_t oldIndex = mLastTouch.idToIndex[moveId];
2317 int32_t newIndex = mCurrentTouch.idToIndex[moveId];
2318 if (mLastTouch.pointers[oldIndex] != mCurrentTouch.pointers[newIndex]) {
2319 interimTouch.pointers[oldIndex] = mCurrentTouch.pointers[newIndex];
2320 moveNeeded = true;
2321 }
2322 }
2323
2324 // Dispatch pointer up events using the interim pointer locations.
2325 while (!upIdBits.isEmpty()) {
Jeff Browne839a582010-04-22 18:58:52 -07002326 uint32_t upId = upIdBits.firstMarkedBit();
2327 upIdBits.clearBit(upId);
2328 BitSet32 oldActiveIdBits = activeIdBits;
2329 activeIdBits.clearBit(upId);
2330
2331 int32_t motionEventAction;
2332 if (activeIdBits.isEmpty()) {
Jeff Brown5c1ed842010-07-14 18:48:53 -07002333 motionEventAction = AMOTION_EVENT_ACTION_UP;
Jeff Browne839a582010-04-22 18:58:52 -07002334 } else {
Jeff Brown3cf1c9b2010-07-16 15:01:56 -07002335 motionEventAction = AMOTION_EVENT_ACTION_POINTER_UP;
Jeff Browne839a582010-04-22 18:58:52 -07002336 }
2337
Jeff Brown3c3cc622010-10-20 15:33:38 -07002338 dispatchTouch(when, policyFlags, &interimTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002339 oldActiveIdBits, upId, pointerCount, motionEventAction);
2340 pointerCount -= 1;
Jeff Browne839a582010-04-22 18:58:52 -07002341 }
2342
Jeff Brown3c3cc622010-10-20 15:33:38 -07002343 // Dispatch move events if any of the remaining pointers moved from their old locations.
2344 // Although applications receive new locations as part of individual pointer up
2345 // events, they do not generally handle them except when presented in a move event.
2346 if (moveNeeded) {
2347 dispatchTouch(when, policyFlags, &mCurrentTouch,
2348 activeIdBits, -1, pointerCount, AMOTION_EVENT_ACTION_MOVE);
2349 }
2350
2351 // Dispatch pointer down events using the new pointer locations.
2352 while (!downIdBits.isEmpty()) {
Jeff Browne839a582010-04-22 18:58:52 -07002353 uint32_t downId = downIdBits.firstMarkedBit();
2354 downIdBits.clearBit(downId);
2355 BitSet32 oldActiveIdBits = activeIdBits;
2356 activeIdBits.markBit(downId);
2357
2358 int32_t motionEventAction;
2359 if (oldActiveIdBits.isEmpty()) {
Jeff Brown5c1ed842010-07-14 18:48:53 -07002360 motionEventAction = AMOTION_EVENT_ACTION_DOWN;
Jeff Browne57e8952010-07-23 21:28:06 -07002361 mDownTime = when;
Jeff Browne839a582010-04-22 18:58:52 -07002362 } else {
Jeff Brown3cf1c9b2010-07-16 15:01:56 -07002363 motionEventAction = AMOTION_EVENT_ACTION_POINTER_DOWN;
Jeff Browne839a582010-04-22 18:58:52 -07002364 }
2365
Jeff Brown38a7fab2010-08-30 03:02:23 -07002366 pointerCount += 1;
Jeff Brown3c3cc622010-10-20 15:33:38 -07002367 dispatchTouch(when, policyFlags, &mCurrentTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002368 activeIdBits, downId, pointerCount, motionEventAction);
Jeff Browne839a582010-04-22 18:58:52 -07002369 }
2370 }
2371}
2372
Jeff Browne57e8952010-07-23 21:28:06 -07002373void TouchInputMapper::dispatchTouch(nsecs_t when, uint32_t policyFlags,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002374 TouchData* touch, BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
Jeff Browne839a582010-04-22 18:58:52 -07002375 int32_t motionEventAction) {
Jeff Browne839a582010-04-22 18:58:52 -07002376 int32_t pointerIds[MAX_POINTERS];
2377 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Browne839a582010-04-22 18:58:52 -07002378 int32_t motionEventEdgeFlags = 0;
Jeff Brownb51719b2010-07-29 18:18:33 -07002379 float xPrecision, yPrecision;
2380
2381 { // acquire lock
2382 AutoMutex _l(mLock);
2383
2384 // Walk through the the active pointers and map touch screen coordinates (TouchData) into
2385 // display coordinates (PointerCoords) and adjust for display orientation.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002386 for (uint32_t outIndex = 0; ! idBits.isEmpty(); outIndex++) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002387 uint32_t id = idBits.firstMarkedBit();
2388 idBits.clearBit(id);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002389 uint32_t inIndex = touch->idToIndex[id];
Jeff Brownb51719b2010-07-29 18:18:33 -07002390
Jeff Brown38a7fab2010-08-30 03:02:23 -07002391 const PointerData& in = touch->pointers[inIndex];
Jeff Brownb51719b2010-07-29 18:18:33 -07002392
Jeff Brown38a7fab2010-08-30 03:02:23 -07002393 // X and Y
2394 float x = float(in.x - mLocked.xOrigin) * mLocked.xScale;
2395 float y = float(in.y - mLocked.yOrigin) * mLocked.yScale;
Jeff Brownb51719b2010-07-29 18:18:33 -07002396
Jeff Brown38a7fab2010-08-30 03:02:23 -07002397 // ToolMajor and ToolMinor
2398 float toolMajor, toolMinor;
Jeff Brown6b337e72010-10-14 21:42:15 -07002399 switch (mCalibration.toolSizeCalibration) {
2400 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002401 toolMajor = in.toolMajor * mLocked.geometricScale;
2402 if (mRawAxes.toolMinor.valid) {
2403 toolMinor = in.toolMinor * mLocked.geometricScale;
2404 } else {
2405 toolMinor = toolMajor;
2406 }
2407 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002408 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002409 toolMajor = in.toolMajor != 0
Jeff Brown6b337e72010-10-14 21:42:15 -07002410 ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias
Jeff Brown38a7fab2010-08-30 03:02:23 -07002411 : 0;
2412 if (mRawAxes.toolMinor.valid) {
2413 toolMinor = in.toolMinor != 0
Jeff Brown6b337e72010-10-14 21:42:15 -07002414 ? in.toolMinor * mLocked.toolSizeLinearScale
2415 + mLocked.toolSizeLinearBias
Jeff Brown38a7fab2010-08-30 03:02:23 -07002416 : 0;
2417 } else {
2418 toolMinor = toolMajor;
2419 }
2420 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002421 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2422 if (in.toolMajor != 0) {
2423 float diameter = sqrtf(in.toolMajor
2424 * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias);
2425 toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias;
2426 } else {
2427 toolMajor = 0;
2428 }
2429 toolMinor = toolMajor;
2430 break;
Jeff Brown38a7fab2010-08-30 03:02:23 -07002431 default:
2432 toolMajor = 0;
2433 toolMinor = 0;
2434 break;
Jeff Brownb51719b2010-07-29 18:18:33 -07002435 }
2436
Jeff Brown6b337e72010-10-14 21:42:15 -07002437 if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002438 toolMajor /= pointerCount;
2439 toolMinor /= pointerCount;
2440 }
2441
2442 // Pressure
2443 float rawPressure;
2444 switch (mCalibration.pressureSource) {
2445 case Calibration::PRESSURE_SOURCE_PRESSURE:
2446 rawPressure = in.pressure;
2447 break;
2448 case Calibration::PRESSURE_SOURCE_TOUCH:
2449 rawPressure = in.touchMajor;
2450 break;
2451 default:
2452 rawPressure = 0;
2453 }
2454
2455 float pressure;
2456 switch (mCalibration.pressureCalibration) {
2457 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
2458 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
2459 pressure = rawPressure * mLocked.pressureScale;
2460 break;
2461 default:
2462 pressure = 1;
2463 break;
2464 }
2465
2466 // TouchMajor and TouchMinor
2467 float touchMajor, touchMinor;
Jeff Brown6b337e72010-10-14 21:42:15 -07002468 switch (mCalibration.touchSizeCalibration) {
2469 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002470 touchMajor = in.touchMajor * mLocked.geometricScale;
2471 if (mRawAxes.touchMinor.valid) {
2472 touchMinor = in.touchMinor * mLocked.geometricScale;
2473 } else {
2474 touchMinor = touchMajor;
2475 }
2476 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002477 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002478 touchMajor = toolMajor * pressure;
2479 touchMinor = toolMinor * pressure;
2480 break;
2481 default:
2482 touchMajor = 0;
2483 touchMinor = 0;
2484 break;
2485 }
2486
2487 if (touchMajor > toolMajor) {
2488 touchMajor = toolMajor;
2489 }
2490 if (touchMinor > toolMinor) {
2491 touchMinor = toolMinor;
2492 }
2493
2494 // Size
2495 float size;
2496 switch (mCalibration.sizeCalibration) {
2497 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
2498 float rawSize = mRawAxes.toolMinor.valid
2499 ? avg(in.toolMajor, in.toolMinor)
2500 : in.toolMajor;
2501 size = rawSize * mLocked.sizeScale;
2502 break;
2503 }
2504 default:
2505 size = 0;
2506 break;
2507 }
2508
2509 // Orientation
2510 float orientation;
2511 switch (mCalibration.orientationCalibration) {
2512 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
2513 orientation = in.orientation * mLocked.orientationScale;
2514 break;
2515 default:
2516 orientation = 0;
2517 }
2518
2519 // Adjust coords for orientation.
Jeff Brownb51719b2010-07-29 18:18:33 -07002520 switch (mLocked.surfaceOrientation) {
2521 case InputReaderPolicyInterface::ROTATION_90: {
2522 float xTemp = x;
2523 x = y;
2524 y = mLocked.surfaceWidth - xTemp;
2525 orientation -= M_PI_2;
2526 if (orientation < - M_PI_2) {
2527 orientation += M_PI;
2528 }
2529 break;
2530 }
2531 case InputReaderPolicyInterface::ROTATION_180: {
2532 x = mLocked.surfaceWidth - x;
2533 y = mLocked.surfaceHeight - y;
2534 orientation = - orientation;
2535 break;
2536 }
2537 case InputReaderPolicyInterface::ROTATION_270: {
2538 float xTemp = x;
2539 x = mLocked.surfaceHeight - y;
2540 y = xTemp;
2541 orientation += M_PI_2;
2542 if (orientation > M_PI_2) {
2543 orientation -= M_PI;
2544 }
2545 break;
2546 }
2547 }
2548
Jeff Brown38a7fab2010-08-30 03:02:23 -07002549 // Write output coords.
2550 PointerCoords& out = pointerCoords[outIndex];
2551 out.x = x;
2552 out.y = y;
2553 out.pressure = pressure;
2554 out.size = size;
2555 out.touchMajor = touchMajor;
2556 out.touchMinor = touchMinor;
2557 out.toolMajor = toolMajor;
2558 out.toolMinor = toolMinor;
2559 out.orientation = orientation;
Jeff Brownb51719b2010-07-29 18:18:33 -07002560
Jeff Brown38a7fab2010-08-30 03:02:23 -07002561 pointerIds[outIndex] = int32_t(id);
Jeff Brownb51719b2010-07-29 18:18:33 -07002562
2563 if (id == changedId) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002564 motionEventAction |= outIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
Jeff Brownb51719b2010-07-29 18:18:33 -07002565 }
Jeff Browne839a582010-04-22 18:58:52 -07002566 }
Jeff Brownb51719b2010-07-29 18:18:33 -07002567
2568 // Check edge flags by looking only at the first pointer since the flags are
2569 // global to the event.
2570 if (motionEventAction == AMOTION_EVENT_ACTION_DOWN) {
2571 if (pointerCoords[0].x <= 0) {
2572 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_LEFT;
2573 } else if (pointerCoords[0].x >= mLocked.orientedSurfaceWidth) {
2574 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_RIGHT;
2575 }
2576 if (pointerCoords[0].y <= 0) {
2577 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_TOP;
2578 } else if (pointerCoords[0].y >= mLocked.orientedSurfaceHeight) {
2579 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_BOTTOM;
2580 }
Jeff Browne839a582010-04-22 18:58:52 -07002581 }
Jeff Brownb51719b2010-07-29 18:18:33 -07002582
2583 xPrecision = mLocked.orientedXPrecision;
2584 yPrecision = mLocked.orientedYPrecision;
2585 } // release lock
Jeff Browne839a582010-04-22 18:58:52 -07002586
Jeff Brown77e26fc2010-10-07 13:44:51 -07002587 getDispatcher()->notifyMotion(when, getDeviceId(), getSources(), policyFlags,
Jeff Brownaf30ff62010-09-01 17:01:00 -07002588 motionEventAction, 0, getContext()->getGlobalMetaState(), motionEventEdgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -07002589 pointerCount, pointerIds, pointerCoords,
Jeff Brownb51719b2010-07-29 18:18:33 -07002590 xPrecision, yPrecision, mDownTime);
Jeff Browne839a582010-04-22 18:58:52 -07002591}
2592
Jeff Brownb51719b2010-07-29 18:18:33 -07002593bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002594 if (mRawAxes.x.valid && mRawAxes.y.valid) {
2595 return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue
2596 && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue;
Jeff Browne839a582010-04-22 18:58:52 -07002597 }
Jeff Browne57e8952010-07-23 21:28:06 -07002598 return true;
2599}
2600
Jeff Brownb51719b2010-07-29 18:18:33 -07002601const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked(
2602 int32_t x, int32_t y) {
2603 size_t numVirtualKeys = mLocked.virtualKeys.size();
2604 for (size_t i = 0; i < numVirtualKeys; i++) {
2605 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07002606
2607#if DEBUG_VIRTUAL_KEYS
2608 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
2609 "left=%d, top=%d, right=%d, bottom=%d",
2610 x, y,
2611 virtualKey.keyCode, virtualKey.scanCode,
2612 virtualKey.hitLeft, virtualKey.hitTop,
2613 virtualKey.hitRight, virtualKey.hitBottom);
2614#endif
2615
2616 if (virtualKey.isHit(x, y)) {
2617 return & virtualKey;
2618 }
2619 }
2620
2621 return NULL;
2622}
2623
2624void TouchInputMapper::calculatePointerIds() {
2625 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2626 uint32_t lastPointerCount = mLastTouch.pointerCount;
2627
2628 if (currentPointerCount == 0) {
2629 // No pointers to assign.
2630 mCurrentTouch.idBits.clear();
2631 } else if (lastPointerCount == 0) {
2632 // All pointers are new.
2633 mCurrentTouch.idBits.clear();
2634 for (uint32_t i = 0; i < currentPointerCount; i++) {
2635 mCurrentTouch.pointers[i].id = i;
2636 mCurrentTouch.idToIndex[i] = i;
2637 mCurrentTouch.idBits.markBit(i);
2638 }
2639 } else if (currentPointerCount == 1 && lastPointerCount == 1) {
2640 // Only one pointer and no change in count so it must have the same id as before.
2641 uint32_t id = mLastTouch.pointers[0].id;
2642 mCurrentTouch.pointers[0].id = id;
2643 mCurrentTouch.idToIndex[id] = 0;
2644 mCurrentTouch.idBits.value = BitSet32::valueForBit(id);
2645 } else {
2646 // General case.
2647 // We build a heap of squared euclidean distances between current and last pointers
2648 // associated with the current and last pointer indices. Then, we find the best
2649 // match (by distance) for each current pointer.
2650 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
2651
2652 uint32_t heapSize = 0;
2653 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
2654 currentPointerIndex++) {
2655 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
2656 lastPointerIndex++) {
2657 int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x
2658 - mLastTouch.pointers[lastPointerIndex].x;
2659 int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y
2660 - mLastTouch.pointers[lastPointerIndex].y;
2661
2662 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
2663
2664 // Insert new element into the heap (sift up).
2665 heap[heapSize].currentPointerIndex = currentPointerIndex;
2666 heap[heapSize].lastPointerIndex = lastPointerIndex;
2667 heap[heapSize].distance = distance;
2668 heapSize += 1;
2669 }
2670 }
2671
2672 // Heapify
2673 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
2674 startIndex -= 1;
2675 for (uint32_t parentIndex = startIndex; ;) {
2676 uint32_t childIndex = parentIndex * 2 + 1;
2677 if (childIndex >= heapSize) {
2678 break;
2679 }
2680
2681 if (childIndex + 1 < heapSize
2682 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2683 childIndex += 1;
2684 }
2685
2686 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2687 break;
2688 }
2689
2690 swap(heap[parentIndex], heap[childIndex]);
2691 parentIndex = childIndex;
2692 }
2693 }
2694
2695#if DEBUG_POINTER_ASSIGNMENT
2696 LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize);
2697 for (size_t i = 0; i < heapSize; i++) {
2698 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2699 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2700 heap[i].distance);
2701 }
2702#endif
2703
2704 // Pull matches out by increasing order of distance.
2705 // To avoid reassigning pointers that have already been matched, the loop keeps track
2706 // of which last and current pointers have been matched using the matchedXXXBits variables.
2707 // It also tracks the used pointer id bits.
2708 BitSet32 matchedLastBits(0);
2709 BitSet32 matchedCurrentBits(0);
2710 BitSet32 usedIdBits(0);
2711 bool first = true;
2712 for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) {
2713 for (;;) {
2714 if (first) {
2715 // The first time through the loop, we just consume the root element of
2716 // the heap (the one with smallest distance).
2717 first = false;
2718 } else {
2719 // Previous iterations consumed the root element of the heap.
2720 // Pop root element off of the heap (sift down).
2721 heapSize -= 1;
2722 assert(heapSize > 0);
2723
2724 // Sift down.
2725 heap[0] = heap[heapSize];
2726 for (uint32_t parentIndex = 0; ;) {
2727 uint32_t childIndex = parentIndex * 2 + 1;
2728 if (childIndex >= heapSize) {
2729 break;
2730 }
2731
2732 if (childIndex + 1 < heapSize
2733 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2734 childIndex += 1;
2735 }
2736
2737 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2738 break;
2739 }
2740
2741 swap(heap[parentIndex], heap[childIndex]);
2742 parentIndex = childIndex;
2743 }
2744
2745#if DEBUG_POINTER_ASSIGNMENT
2746 LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize);
2747 for (size_t i = 0; i < heapSize; i++) {
2748 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2749 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2750 heap[i].distance);
2751 }
2752#endif
2753 }
2754
2755 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
2756 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
2757
2758 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
2759 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
2760
2761 matchedCurrentBits.markBit(currentPointerIndex);
2762 matchedLastBits.markBit(lastPointerIndex);
2763
2764 uint32_t id = mLastTouch.pointers[lastPointerIndex].id;
2765 mCurrentTouch.pointers[currentPointerIndex].id = id;
2766 mCurrentTouch.idToIndex[id] = currentPointerIndex;
2767 usedIdBits.markBit(id);
2768
2769#if DEBUG_POINTER_ASSIGNMENT
2770 LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
2771 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
2772#endif
2773 break;
2774 }
2775 }
2776
2777 // Assign fresh ids to new pointers.
2778 if (currentPointerCount > lastPointerCount) {
2779 for (uint32_t i = currentPointerCount - lastPointerCount; ;) {
2780 uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit();
2781 uint32_t id = usedIdBits.firstUnmarkedBit();
2782
2783 mCurrentTouch.pointers[currentPointerIndex].id = id;
2784 mCurrentTouch.idToIndex[id] = currentPointerIndex;
2785 usedIdBits.markBit(id);
2786
2787#if DEBUG_POINTER_ASSIGNMENT
2788 LOGD("calculatePointerIds - assigned: cur=%d, id=%d",
2789 currentPointerIndex, id);
2790#endif
2791
2792 if (--i == 0) break; // done
2793 matchedCurrentBits.markBit(currentPointerIndex);
2794 }
2795 }
2796
2797 // Fix id bits.
2798 mCurrentTouch.idBits = usedIdBits;
2799 }
2800}
2801
2802/* Special hack for devices that have bad screen data: if one of the
2803 * points has moved more than a screen height from the last position,
2804 * then drop it. */
2805bool TouchInputMapper::applyBadTouchFilter() {
2806 // This hack requires valid axis parameters.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002807 if (! mRawAxes.y.valid) {
Jeff Browne57e8952010-07-23 21:28:06 -07002808 return false;
2809 }
2810
2811 uint32_t pointerCount = mCurrentTouch.pointerCount;
2812
2813 // Nothing to do if there are no points.
2814 if (pointerCount == 0) {
2815 return false;
2816 }
2817
2818 // Don't do anything if a finger is going down or up. We run
2819 // here before assigning pointer IDs, so there isn't a good
2820 // way to do per-finger matching.
2821 if (pointerCount != mLastTouch.pointerCount) {
2822 return false;
2823 }
2824
2825 // We consider a single movement across more than a 7/16 of
2826 // the long size of the screen to be bad. This was a magic value
2827 // determined by looking at the maximum distance it is feasible
2828 // to actually move in one sample.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002829 int32_t maxDeltaY = mRawAxes.y.getRange() * 7 / 16;
Jeff Browne57e8952010-07-23 21:28:06 -07002830
2831 // XXX The original code in InputDevice.java included commented out
2832 // code for testing the X axis. Note that when we drop a point
2833 // we don't actually restore the old X either. Strange.
2834 // The old code also tries to track when bad points were previously
2835 // detected but it turns out that due to the placement of a "break"
2836 // at the end of the loop, we never set mDroppedBadPoint to true
2837 // so it is effectively dead code.
2838 // Need to figure out if the old code is busted or just overcomplicated
2839 // but working as intended.
2840
2841 // Look through all new points and see if any are farther than
2842 // acceptable from all previous points.
2843 for (uint32_t i = pointerCount; i-- > 0; ) {
2844 int32_t y = mCurrentTouch.pointers[i].y;
2845 int32_t closestY = INT_MAX;
2846 int32_t closestDeltaY = 0;
2847
2848#if DEBUG_HACKS
2849 LOGD("BadTouchFilter: Looking at next point #%d: y=%d", i, y);
2850#endif
2851
2852 for (uint32_t j = pointerCount; j-- > 0; ) {
2853 int32_t lastY = mLastTouch.pointers[j].y;
2854 int32_t deltaY = abs(y - lastY);
2855
2856#if DEBUG_HACKS
2857 LOGD("BadTouchFilter: Comparing with last point #%d: y=%d deltaY=%d",
2858 j, lastY, deltaY);
2859#endif
2860
2861 if (deltaY < maxDeltaY) {
2862 goto SkipSufficientlyClosePoint;
2863 }
2864 if (deltaY < closestDeltaY) {
2865 closestDeltaY = deltaY;
2866 closestY = lastY;
2867 }
2868 }
2869
2870 // Must not have found a close enough match.
2871#if DEBUG_HACKS
2872 LOGD("BadTouchFilter: Dropping bad point #%d: newY=%d oldY=%d deltaY=%d maxDeltaY=%d",
2873 i, y, closestY, closestDeltaY, maxDeltaY);
2874#endif
2875
2876 mCurrentTouch.pointers[i].y = closestY;
2877 return true; // XXX original code only corrects one point
2878
2879 SkipSufficientlyClosePoint: ;
2880 }
2881
2882 // No change.
2883 return false;
2884}
2885
2886/* Special hack for devices that have bad screen data: drop points where
2887 * the coordinate value for one axis has jumped to the other pointer's location.
2888 */
2889bool TouchInputMapper::applyJumpyTouchFilter() {
2890 // This hack requires valid axis parameters.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002891 if (! mRawAxes.y.valid) {
Jeff Browne57e8952010-07-23 21:28:06 -07002892 return false;
2893 }
2894
2895 uint32_t pointerCount = mCurrentTouch.pointerCount;
2896 if (mLastTouch.pointerCount != pointerCount) {
2897#if DEBUG_HACKS
2898 LOGD("JumpyTouchFilter: Different pointer count %d -> %d",
2899 mLastTouch.pointerCount, pointerCount);
2900 for (uint32_t i = 0; i < pointerCount; i++) {
2901 LOGD(" Pointer %d (%d, %d)", i,
2902 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
2903 }
2904#endif
2905
2906 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_TRANSITION_DROPS) {
2907 if (mLastTouch.pointerCount == 1 && pointerCount == 2) {
2908 // Just drop the first few events going from 1 to 2 pointers.
2909 // They're bad often enough that they're not worth considering.
2910 mCurrentTouch.pointerCount = 1;
2911 mJumpyTouchFilter.jumpyPointsDropped += 1;
2912
2913#if DEBUG_HACKS
2914 LOGD("JumpyTouchFilter: Pointer 2 dropped");
2915#endif
2916 return true;
2917 } else if (mLastTouch.pointerCount == 2 && pointerCount == 1) {
2918 // The event when we go from 2 -> 1 tends to be messed up too
2919 mCurrentTouch.pointerCount = 2;
2920 mCurrentTouch.pointers[0] = mLastTouch.pointers[0];
2921 mCurrentTouch.pointers[1] = mLastTouch.pointers[1];
2922 mJumpyTouchFilter.jumpyPointsDropped += 1;
2923
2924#if DEBUG_HACKS
2925 for (int32_t i = 0; i < 2; i++) {
2926 LOGD("JumpyTouchFilter: Pointer %d replaced (%d, %d)", i,
2927 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
2928 }
2929#endif
2930 return true;
2931 }
2932 }
2933 // Reset jumpy points dropped on other transitions or if limit exceeded.
2934 mJumpyTouchFilter.jumpyPointsDropped = 0;
2935
2936#if DEBUG_HACKS
2937 LOGD("JumpyTouchFilter: Transition - drop limit reset");
2938#endif
2939 return false;
2940 }
2941
2942 // We have the same number of pointers as last time.
2943 // A 'jumpy' point is one where the coordinate value for one axis
2944 // has jumped to the other pointer's location. No need to do anything
2945 // else if we only have one pointer.
2946 if (pointerCount < 2) {
2947 return false;
2948 }
2949
2950 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_DROP_LIMIT) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002951 int jumpyEpsilon = mRawAxes.y.getRange() / JUMPY_EPSILON_DIVISOR;
Jeff Browne57e8952010-07-23 21:28:06 -07002952
2953 // We only replace the single worst jumpy point as characterized by pointer distance
2954 // in a single axis.
2955 int32_t badPointerIndex = -1;
2956 int32_t badPointerReplacementIndex = -1;
2957 int32_t badPointerDistance = INT_MIN; // distance to be corrected
2958
2959 for (uint32_t i = pointerCount; i-- > 0; ) {
2960 int32_t x = mCurrentTouch.pointers[i].x;
2961 int32_t y = mCurrentTouch.pointers[i].y;
2962
2963#if DEBUG_HACKS
2964 LOGD("JumpyTouchFilter: Point %d (%d, %d)", i, x, y);
2965#endif
2966
2967 // Check if a touch point is too close to another's coordinates
2968 bool dropX = false, dropY = false;
2969 for (uint32_t j = 0; j < pointerCount; j++) {
2970 if (i == j) {
2971 continue;
2972 }
2973
2974 if (abs(x - mCurrentTouch.pointers[j].x) <= jumpyEpsilon) {
2975 dropX = true;
2976 break;
2977 }
2978
2979 if (abs(y - mCurrentTouch.pointers[j].y) <= jumpyEpsilon) {
2980 dropY = true;
2981 break;
2982 }
2983 }
2984 if (! dropX && ! dropY) {
2985 continue; // not jumpy
2986 }
2987
2988 // Find a replacement candidate by comparing with older points on the
2989 // complementary (non-jumpy) axis.
2990 int32_t distance = INT_MIN; // distance to be corrected
2991 int32_t replacementIndex = -1;
2992
2993 if (dropX) {
2994 // X looks too close. Find an older replacement point with a close Y.
2995 int32_t smallestDeltaY = INT_MAX;
2996 for (uint32_t j = 0; j < pointerCount; j++) {
2997 int32_t deltaY = abs(y - mLastTouch.pointers[j].y);
2998 if (deltaY < smallestDeltaY) {
2999 smallestDeltaY = deltaY;
3000 replacementIndex = j;
3001 }
3002 }
3003 distance = abs(x - mLastTouch.pointers[replacementIndex].x);
3004 } else {
3005 // Y looks too close. Find an older replacement point with a close X.
3006 int32_t smallestDeltaX = INT_MAX;
3007 for (uint32_t j = 0; j < pointerCount; j++) {
3008 int32_t deltaX = abs(x - mLastTouch.pointers[j].x);
3009 if (deltaX < smallestDeltaX) {
3010 smallestDeltaX = deltaX;
3011 replacementIndex = j;
3012 }
3013 }
3014 distance = abs(y - mLastTouch.pointers[replacementIndex].y);
3015 }
3016
3017 // If replacing this pointer would correct a worse error than the previous ones
3018 // considered, then use this replacement instead.
3019 if (distance > badPointerDistance) {
3020 badPointerIndex = i;
3021 badPointerReplacementIndex = replacementIndex;
3022 badPointerDistance = distance;
3023 }
3024 }
3025
3026 // Correct the jumpy pointer if one was found.
3027 if (badPointerIndex >= 0) {
3028#if DEBUG_HACKS
3029 LOGD("JumpyTouchFilter: Replacing bad pointer %d with (%d, %d)",
3030 badPointerIndex,
3031 mLastTouch.pointers[badPointerReplacementIndex].x,
3032 mLastTouch.pointers[badPointerReplacementIndex].y);
3033#endif
3034
3035 mCurrentTouch.pointers[badPointerIndex].x =
3036 mLastTouch.pointers[badPointerReplacementIndex].x;
3037 mCurrentTouch.pointers[badPointerIndex].y =
3038 mLastTouch.pointers[badPointerReplacementIndex].y;
3039 mJumpyTouchFilter.jumpyPointsDropped += 1;
3040 return true;
3041 }
3042 }
3043
3044 mJumpyTouchFilter.jumpyPointsDropped = 0;
3045 return false;
3046}
3047
3048/* Special hack for devices that have bad screen data: aggregate and
3049 * compute averages of the coordinate data, to reduce the amount of
3050 * jitter seen by applications. */
3051void TouchInputMapper::applyAveragingTouchFilter() {
3052 for (uint32_t currentIndex = 0; currentIndex < mCurrentTouch.pointerCount; currentIndex++) {
3053 uint32_t id = mCurrentTouch.pointers[currentIndex].id;
3054 int32_t x = mCurrentTouch.pointers[currentIndex].x;
3055 int32_t y = mCurrentTouch.pointers[currentIndex].y;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003056 int32_t pressure;
3057 switch (mCalibration.pressureSource) {
3058 case Calibration::PRESSURE_SOURCE_PRESSURE:
3059 pressure = mCurrentTouch.pointers[currentIndex].pressure;
3060 break;
3061 case Calibration::PRESSURE_SOURCE_TOUCH:
3062 pressure = mCurrentTouch.pointers[currentIndex].touchMajor;
3063 break;
3064 default:
3065 pressure = 1;
3066 break;
3067 }
Jeff Browne57e8952010-07-23 21:28:06 -07003068
3069 if (mLastTouch.idBits.hasBit(id)) {
3070 // Pointer was down before and is still down now.
3071 // Compute average over history trace.
3072 uint32_t start = mAveragingTouchFilter.historyStart[id];
3073 uint32_t end = mAveragingTouchFilter.historyEnd[id];
3074
3075 int64_t deltaX = x - mAveragingTouchFilter.historyData[end].pointers[id].x;
3076 int64_t deltaY = y - mAveragingTouchFilter.historyData[end].pointers[id].y;
3077 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3078
3079#if DEBUG_HACKS
3080 LOGD("AveragingTouchFilter: Pointer id %d - Distance from last sample: %lld",
3081 id, distance);
3082#endif
3083
3084 if (distance < AVERAGING_DISTANCE_LIMIT) {
3085 // Increment end index in preparation for recording new historical data.
3086 end += 1;
3087 if (end > AVERAGING_HISTORY_SIZE) {
3088 end = 0;
3089 }
3090
3091 // If the end index has looped back to the start index then we have filled
3092 // the historical trace up to the desired size so we drop the historical
3093 // data at the start of the trace.
3094 if (end == start) {
3095 start += 1;
3096 if (start > AVERAGING_HISTORY_SIZE) {
3097 start = 0;
3098 }
3099 }
3100
3101 // Add the raw data to the historical trace.
3102 mAveragingTouchFilter.historyStart[id] = start;
3103 mAveragingTouchFilter.historyEnd[id] = end;
3104 mAveragingTouchFilter.historyData[end].pointers[id].x = x;
3105 mAveragingTouchFilter.historyData[end].pointers[id].y = y;
3106 mAveragingTouchFilter.historyData[end].pointers[id].pressure = pressure;
3107
3108 // Average over all historical positions in the trace by total pressure.
3109 int32_t averagedX = 0;
3110 int32_t averagedY = 0;
3111 int32_t totalPressure = 0;
3112 for (;;) {
3113 int32_t historicalX = mAveragingTouchFilter.historyData[start].pointers[id].x;
3114 int32_t historicalY = mAveragingTouchFilter.historyData[start].pointers[id].y;
3115 int32_t historicalPressure = mAveragingTouchFilter.historyData[start]
3116 .pointers[id].pressure;
3117
3118 averagedX += historicalX * historicalPressure;
3119 averagedY += historicalY * historicalPressure;
3120 totalPressure += historicalPressure;
3121
3122 if (start == end) {
3123 break;
3124 }
3125
3126 start += 1;
3127 if (start > AVERAGING_HISTORY_SIZE) {
3128 start = 0;
3129 }
3130 }
3131
Jeff Brown38a7fab2010-08-30 03:02:23 -07003132 if (totalPressure != 0) {
3133 averagedX /= totalPressure;
3134 averagedY /= totalPressure;
Jeff Browne57e8952010-07-23 21:28:06 -07003135
3136#if DEBUG_HACKS
Jeff Brown38a7fab2010-08-30 03:02:23 -07003137 LOGD("AveragingTouchFilter: Pointer id %d - "
3138 "totalPressure=%d, averagedX=%d, averagedY=%d", id, totalPressure,
3139 averagedX, averagedY);
Jeff Browne57e8952010-07-23 21:28:06 -07003140#endif
3141
Jeff Brown38a7fab2010-08-30 03:02:23 -07003142 mCurrentTouch.pointers[currentIndex].x = averagedX;
3143 mCurrentTouch.pointers[currentIndex].y = averagedY;
3144 }
Jeff Browne57e8952010-07-23 21:28:06 -07003145 } else {
3146#if DEBUG_HACKS
3147 LOGD("AveragingTouchFilter: Pointer id %d - Exceeded max distance", id);
3148#endif
3149 }
3150 } else {
3151#if DEBUG_HACKS
3152 LOGD("AveragingTouchFilter: Pointer id %d - Pointer went up", id);
3153#endif
3154 }
3155
3156 // Reset pointer history.
3157 mAveragingTouchFilter.historyStart[id] = 0;
3158 mAveragingTouchFilter.historyEnd[id] = 0;
3159 mAveragingTouchFilter.historyData[0].pointers[id].x = x;
3160 mAveragingTouchFilter.historyData[0].pointers[id].y = y;
3161 mAveragingTouchFilter.historyData[0].pointers[id].pressure = pressure;
3162 }
3163}
3164
3165int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownb51719b2010-07-29 18:18:33 -07003166 { // acquire lock
3167 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07003168
Jeff Brownb51719b2010-07-29 18:18:33 -07003169 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07003170 return AKEY_STATE_VIRTUAL;
3171 }
3172
Jeff Brownb51719b2010-07-29 18:18:33 -07003173 size_t numVirtualKeys = mLocked.virtualKeys.size();
3174 for (size_t i = 0; i < numVirtualKeys; i++) {
3175 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07003176 if (virtualKey.keyCode == keyCode) {
3177 return AKEY_STATE_UP;
3178 }
3179 }
Jeff Brownb51719b2010-07-29 18:18:33 -07003180 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07003181
3182 return AKEY_STATE_UNKNOWN;
3183}
3184
3185int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownb51719b2010-07-29 18:18:33 -07003186 { // acquire lock
3187 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07003188
Jeff Brownb51719b2010-07-29 18:18:33 -07003189 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07003190 return AKEY_STATE_VIRTUAL;
3191 }
3192
Jeff Brownb51719b2010-07-29 18:18:33 -07003193 size_t numVirtualKeys = mLocked.virtualKeys.size();
3194 for (size_t i = 0; i < numVirtualKeys; i++) {
3195 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07003196 if (virtualKey.scanCode == scanCode) {
3197 return AKEY_STATE_UP;
3198 }
3199 }
Jeff Brownb51719b2010-07-29 18:18:33 -07003200 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07003201
3202 return AKEY_STATE_UNKNOWN;
3203}
3204
3205bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
3206 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownb51719b2010-07-29 18:18:33 -07003207 { // acquire lock
3208 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07003209
Jeff Brownb51719b2010-07-29 18:18:33 -07003210 size_t numVirtualKeys = mLocked.virtualKeys.size();
3211 for (size_t i = 0; i < numVirtualKeys; i++) {
3212 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07003213
3214 for (size_t i = 0; i < numCodes; i++) {
3215 if (virtualKey.keyCode == keyCodes[i]) {
3216 outFlags[i] = 1;
3217 }
3218 }
3219 }
Jeff Brownb51719b2010-07-29 18:18:33 -07003220 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07003221
3222 return true;
3223}
3224
3225
3226// --- SingleTouchInputMapper ---
3227
3228SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device, int32_t associatedDisplayId) :
3229 TouchInputMapper(device, associatedDisplayId) {
3230 initialize();
3231}
3232
3233SingleTouchInputMapper::~SingleTouchInputMapper() {
3234}
3235
3236void SingleTouchInputMapper::initialize() {
3237 mAccumulator.clear();
3238
3239 mDown = false;
3240 mX = 0;
3241 mY = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003242 mPressure = 0; // default to 0 for devices that don't report pressure
3243 mToolWidth = 0; // default to 0 for devices that don't report tool width
Jeff Browne57e8952010-07-23 21:28:06 -07003244}
3245
3246void SingleTouchInputMapper::reset() {
3247 TouchInputMapper::reset();
3248
Jeff Browne57e8952010-07-23 21:28:06 -07003249 initialize();
3250 }
3251
3252void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
3253 switch (rawEvent->type) {
3254 case EV_KEY:
3255 switch (rawEvent->scanCode) {
3256 case BTN_TOUCH:
3257 mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH;
3258 mAccumulator.btnTouch = rawEvent->value != 0;
Jeff Brownd64c8552010-08-17 20:38:35 -07003259 // Don't sync immediately. Wait until the next SYN_REPORT since we might
3260 // not have received valid position information yet. This logic assumes that
3261 // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet.
Jeff Browne57e8952010-07-23 21:28:06 -07003262 break;
3263 }
3264 break;
3265
3266 case EV_ABS:
3267 switch (rawEvent->scanCode) {
3268 case ABS_X:
3269 mAccumulator.fields |= Accumulator::FIELD_ABS_X;
3270 mAccumulator.absX = rawEvent->value;
3271 break;
3272 case ABS_Y:
3273 mAccumulator.fields |= Accumulator::FIELD_ABS_Y;
3274 mAccumulator.absY = rawEvent->value;
3275 break;
3276 case ABS_PRESSURE:
3277 mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE;
3278 mAccumulator.absPressure = rawEvent->value;
3279 break;
3280 case ABS_TOOL_WIDTH:
3281 mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH;
3282 mAccumulator.absToolWidth = rawEvent->value;
3283 break;
3284 }
3285 break;
3286
3287 case EV_SYN:
3288 switch (rawEvent->scanCode) {
3289 case SYN_REPORT:
Jeff Brownd64c8552010-08-17 20:38:35 -07003290 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07003291 break;
3292 }
3293 break;
3294 }
3295}
3296
3297void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Browne57e8952010-07-23 21:28:06 -07003298 uint32_t fields = mAccumulator.fields;
Jeff Brownd64c8552010-08-17 20:38:35 -07003299 if (fields == 0) {
3300 return; // no new state changes, so nothing to do
3301 }
Jeff Browne57e8952010-07-23 21:28:06 -07003302
3303 if (fields & Accumulator::FIELD_BTN_TOUCH) {
3304 mDown = mAccumulator.btnTouch;
3305 }
3306
3307 if (fields & Accumulator::FIELD_ABS_X) {
3308 mX = mAccumulator.absX;
3309 }
3310
3311 if (fields & Accumulator::FIELD_ABS_Y) {
3312 mY = mAccumulator.absY;
3313 }
3314
3315 if (fields & Accumulator::FIELD_ABS_PRESSURE) {
3316 mPressure = mAccumulator.absPressure;
3317 }
3318
3319 if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003320 mToolWidth = mAccumulator.absToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -07003321 }
3322
3323 mCurrentTouch.clear();
3324
3325 if (mDown) {
3326 mCurrentTouch.pointerCount = 1;
3327 mCurrentTouch.pointers[0].id = 0;
3328 mCurrentTouch.pointers[0].x = mX;
3329 mCurrentTouch.pointers[0].y = mY;
3330 mCurrentTouch.pointers[0].pressure = mPressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003331 mCurrentTouch.pointers[0].touchMajor = 0;
3332 mCurrentTouch.pointers[0].touchMinor = 0;
3333 mCurrentTouch.pointers[0].toolMajor = mToolWidth;
3334 mCurrentTouch.pointers[0].toolMinor = mToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -07003335 mCurrentTouch.pointers[0].orientation = 0;
3336 mCurrentTouch.idToIndex[0] = 0;
3337 mCurrentTouch.idBits.markBit(0);
3338 }
3339
3340 syncTouch(when, true);
Jeff Brownd64c8552010-08-17 20:38:35 -07003341
3342 mAccumulator.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07003343}
3344
Jeff Brown38a7fab2010-08-30 03:02:23 -07003345void SingleTouchInputMapper::configureRawAxes() {
3346 TouchInputMapper::configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -07003347
Jeff Brown38a7fab2010-08-30 03:02:23 -07003348 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x);
3349 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y);
3350 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure);
3351 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor);
Jeff Browne57e8952010-07-23 21:28:06 -07003352}
3353
3354
3355// --- MultiTouchInputMapper ---
3356
3357MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device, int32_t associatedDisplayId) :
3358 TouchInputMapper(device, associatedDisplayId) {
3359 initialize();
3360}
3361
3362MultiTouchInputMapper::~MultiTouchInputMapper() {
3363}
3364
3365void MultiTouchInputMapper::initialize() {
3366 mAccumulator.clear();
3367}
3368
3369void MultiTouchInputMapper::reset() {
3370 TouchInputMapper::reset();
3371
Jeff Browne57e8952010-07-23 21:28:06 -07003372 initialize();
3373}
3374
3375void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
3376 switch (rawEvent->type) {
3377 case EV_ABS: {
3378 uint32_t pointerIndex = mAccumulator.pointerCount;
3379 Accumulator::Pointer* pointer = & mAccumulator.pointers[pointerIndex];
3380
3381 switch (rawEvent->scanCode) {
3382 case ABS_MT_POSITION_X:
3383 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_X;
3384 pointer->absMTPositionX = rawEvent->value;
3385 break;
3386 case ABS_MT_POSITION_Y:
3387 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y;
3388 pointer->absMTPositionY = rawEvent->value;
3389 break;
3390 case ABS_MT_TOUCH_MAJOR:
3391 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR;
3392 pointer->absMTTouchMajor = rawEvent->value;
3393 break;
3394 case ABS_MT_TOUCH_MINOR:
3395 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR;
3396 pointer->absMTTouchMinor = rawEvent->value;
3397 break;
3398 case ABS_MT_WIDTH_MAJOR:
3399 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR;
3400 pointer->absMTWidthMajor = rawEvent->value;
3401 break;
3402 case ABS_MT_WIDTH_MINOR:
3403 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR;
3404 pointer->absMTWidthMinor = rawEvent->value;
3405 break;
3406 case ABS_MT_ORIENTATION:
3407 pointer->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION;
3408 pointer->absMTOrientation = rawEvent->value;
3409 break;
3410 case ABS_MT_TRACKING_ID:
3411 pointer->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID;
3412 pointer->absMTTrackingId = rawEvent->value;
3413 break;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003414 case ABS_MT_PRESSURE:
3415 pointer->fields |= Accumulator::FIELD_ABS_MT_PRESSURE;
3416 pointer->absMTPressure = rawEvent->value;
3417 break;
Jeff Browne57e8952010-07-23 21:28:06 -07003418 }
3419 break;
3420 }
3421
3422 case EV_SYN:
3423 switch (rawEvent->scanCode) {
3424 case SYN_MT_REPORT: {
3425 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
3426 uint32_t pointerIndex = mAccumulator.pointerCount;
3427
3428 if (mAccumulator.pointers[pointerIndex].fields) {
3429 if (pointerIndex == MAX_POINTERS) {
3430 LOGW("MultiTouch device driver returned more than maximum of %d pointers.",
3431 MAX_POINTERS);
3432 } else {
3433 pointerIndex += 1;
3434 mAccumulator.pointerCount = pointerIndex;
3435 }
3436 }
3437
3438 mAccumulator.pointers[pointerIndex].clear();
3439 break;
3440 }
3441
3442 case SYN_REPORT:
Jeff Brownd64c8552010-08-17 20:38:35 -07003443 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07003444 break;
3445 }
3446 break;
3447 }
3448}
3449
3450void MultiTouchInputMapper::sync(nsecs_t when) {
3451 static const uint32_t REQUIRED_FIELDS =
Jeff Brown38a7fab2010-08-30 03:02:23 -07003452 Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y;
Jeff Browne839a582010-04-22 18:58:52 -07003453
Jeff Browne57e8952010-07-23 21:28:06 -07003454 uint32_t inCount = mAccumulator.pointerCount;
3455 uint32_t outCount = 0;
3456 bool havePointerIds = true;
Jeff Browne839a582010-04-22 18:58:52 -07003457
Jeff Browne57e8952010-07-23 21:28:06 -07003458 mCurrentTouch.clear();
Jeff Browne839a582010-04-22 18:58:52 -07003459
Jeff Browne57e8952010-07-23 21:28:06 -07003460 for (uint32_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003461 const Accumulator::Pointer& inPointer = mAccumulator.pointers[inIndex];
3462 uint32_t fields = inPointer.fields;
Jeff Browne839a582010-04-22 18:58:52 -07003463
Jeff Browne57e8952010-07-23 21:28:06 -07003464 if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003465 // Some drivers send empty MT sync packets without X / Y to indicate a pointer up.
3466 // Drop this finger.
Jeff Browne839a582010-04-22 18:58:52 -07003467 continue;
3468 }
3469
Jeff Brownd64c8552010-08-17 20:38:35 -07003470 PointerData& outPointer = mCurrentTouch.pointers[outCount];
3471 outPointer.x = inPointer.absMTPositionX;
3472 outPointer.y = inPointer.absMTPositionY;
Jeff Browne839a582010-04-22 18:58:52 -07003473
Jeff Brown38a7fab2010-08-30 03:02:23 -07003474 if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) {
3475 if (inPointer.absMTPressure <= 0) {
Jeff Brown3c3cc622010-10-20 15:33:38 -07003476 // Some devices send sync packets with X / Y but with a 0 pressure to indicate
3477 // a pointer going up. Drop this finger.
Jeff Brownd64c8552010-08-17 20:38:35 -07003478 continue;
3479 }
Jeff Brown38a7fab2010-08-30 03:02:23 -07003480 outPointer.pressure = inPointer.absMTPressure;
3481 } else {
3482 // Default pressure to 0 if absent.
3483 outPointer.pressure = 0;
3484 }
3485
3486 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) {
3487 if (inPointer.absMTTouchMajor <= 0) {
3488 // Some devices send sync packets with X / Y but with a 0 touch major to indicate
3489 // a pointer going up. Drop this finger.
3490 continue;
3491 }
Jeff Brownd64c8552010-08-17 20:38:35 -07003492 outPointer.touchMajor = inPointer.absMTTouchMajor;
3493 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003494 // Default touch area to 0 if absent.
Jeff Brownd64c8552010-08-17 20:38:35 -07003495 outPointer.touchMajor = 0;
3496 }
Jeff Browne839a582010-04-22 18:58:52 -07003497
Jeff Brownd64c8552010-08-17 20:38:35 -07003498 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) {
3499 outPointer.touchMinor = inPointer.absMTTouchMinor;
3500 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003501 // Assume touch area is circular.
Jeff Brownd64c8552010-08-17 20:38:35 -07003502 outPointer.touchMinor = outPointer.touchMajor;
3503 }
Jeff Browne839a582010-04-22 18:58:52 -07003504
Jeff Brownd64c8552010-08-17 20:38:35 -07003505 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) {
3506 outPointer.toolMajor = inPointer.absMTWidthMajor;
3507 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003508 // Default tool area to 0 if absent.
3509 outPointer.toolMajor = 0;
Jeff Brownd64c8552010-08-17 20:38:35 -07003510 }
Jeff Browne839a582010-04-22 18:58:52 -07003511
Jeff Brownd64c8552010-08-17 20:38:35 -07003512 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) {
3513 outPointer.toolMinor = inPointer.absMTWidthMinor;
3514 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003515 // Assume tool area is circular.
Jeff Brownd64c8552010-08-17 20:38:35 -07003516 outPointer.toolMinor = outPointer.toolMajor;
3517 }
3518
3519 if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) {
3520 outPointer.orientation = inPointer.absMTOrientation;
3521 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003522 // Default orientation to vertical if absent.
Jeff Brownd64c8552010-08-17 20:38:35 -07003523 outPointer.orientation = 0;
3524 }
3525
Jeff Brown38a7fab2010-08-30 03:02:23 -07003526 // Assign pointer id using tracking id if available.
Jeff Browne57e8952010-07-23 21:28:06 -07003527 if (havePointerIds) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003528 if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) {
3529 uint32_t id = uint32_t(inPointer.absMTTrackingId);
Jeff Browne839a582010-04-22 18:58:52 -07003530
Jeff Browne57e8952010-07-23 21:28:06 -07003531 if (id > MAX_POINTER_ID) {
3532#if DEBUG_POINTERS
3533 LOGD("Pointers: Ignoring driver provided pointer id %d because "
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07003534 "it is larger than max supported id %d",
Jeff Browne57e8952010-07-23 21:28:06 -07003535 id, MAX_POINTER_ID);
3536#endif
3537 havePointerIds = false;
3538 }
3539 else {
Jeff Brownd64c8552010-08-17 20:38:35 -07003540 outPointer.id = id;
Jeff Browne57e8952010-07-23 21:28:06 -07003541 mCurrentTouch.idToIndex[id] = outCount;
3542 mCurrentTouch.idBits.markBit(id);
3543 }
3544 } else {
3545 havePointerIds = false;
Jeff Browne839a582010-04-22 18:58:52 -07003546 }
3547 }
Jeff Browne839a582010-04-22 18:58:52 -07003548
Jeff Browne57e8952010-07-23 21:28:06 -07003549 outCount += 1;
Jeff Browne839a582010-04-22 18:58:52 -07003550 }
3551
Jeff Browne57e8952010-07-23 21:28:06 -07003552 mCurrentTouch.pointerCount = outCount;
Jeff Browne839a582010-04-22 18:58:52 -07003553
Jeff Browne57e8952010-07-23 21:28:06 -07003554 syncTouch(when, havePointerIds);
Jeff Brownd64c8552010-08-17 20:38:35 -07003555
3556 mAccumulator.clear();
Jeff Browne839a582010-04-22 18:58:52 -07003557}
3558
Jeff Brown38a7fab2010-08-30 03:02:23 -07003559void MultiTouchInputMapper::configureRawAxes() {
3560 TouchInputMapper::configureRawAxes();
Jeff Browne839a582010-04-22 18:58:52 -07003561
Jeff Brown38a7fab2010-08-30 03:02:23 -07003562 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, & mRawAxes.x);
3563 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, & mRawAxes.y);
3564 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, & mRawAxes.touchMajor);
3565 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, & mRawAxes.touchMinor);
3566 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, & mRawAxes.toolMajor);
3567 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, & mRawAxes.toolMinor);
3568 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, & mRawAxes.orientation);
3569 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, & mRawAxes.pressure);
Jeff Brown54bc2812010-06-15 01:31:58 -07003570}
3571
Jeff Browne839a582010-04-22 18:58:52 -07003572
3573} // namespace android