blob: 88b91e02912caf9e8885b7358774ad88da7e92f5 [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:
132 return toggleLockedMetaState(AMETA_CAPS_LOCK_LATCHED, down, oldMetaState);
133 case AKEYCODE_NUM_LOCK:
134 return toggleLockedMetaState(AMETA_NUM_LOCK_LATCHED, down, oldMetaState);
135 case AKEYCODE_SCROLL_LOCK:
136 return toggleLockedMetaState(AMETA_SCROLL_LOCK_LATCHED, down, oldMetaState);
137 default:
138 return oldMetaState;
139 }
140}
141
Jeff 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:
269 handleConfigurationChanged();
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 Brown1ad00e92010-10-01 18:55:43 -0700407void InputReader::handleConfigurationChanged() {
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.
Jeff Brown1ad00e92010-10-01 18:55:43 -0700415 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Browne57e8952010-07-23 21:28:06 -0700416 mDispatcher->notifyConfigurationChanged(when);
417}
418
419void InputReader::configureExcludedDevices() {
420 Vector<String8> excludedDeviceNames;
421 mPolicy->getExcludedDeviceNames(excludedDeviceNames);
422
423 for (size_t i = 0; i < excludedDeviceNames.size(); i++) {
424 mEventHub->addExcludedDevice(excludedDeviceNames[i]);
425 }
426}
427
428void InputReader::updateGlobalMetaState() {
429 { // acquire state lock
430 AutoMutex _l(mStateLock);
431
432 mGlobalMetaState = 0;
433
434 { // acquire device registry reader lock
435 RWLock::AutoRLock _rl(mDeviceRegistryLock);
436
437 for (size_t i = 0; i < mDevices.size(); i++) {
438 InputDevice* device = mDevices.valueAt(i);
439 mGlobalMetaState |= device->getMetaState();
440 }
441 } // release device registry reader lock
442 } // release state lock
443}
444
445int32_t InputReader::getGlobalMetaState() {
446 { // acquire state lock
447 AutoMutex _l(mStateLock);
448
449 return mGlobalMetaState;
450 } // release state lock
451}
452
453void InputReader::updateInputConfiguration() {
454 { // acquire state lock
455 AutoMutex _l(mStateLock);
456
457 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
458 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
459 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
460 { // acquire device registry reader lock
461 RWLock::AutoRLock _rl(mDeviceRegistryLock);
462
463 InputDeviceInfo deviceInfo;
464 for (size_t i = 0; i < mDevices.size(); i++) {
465 InputDevice* device = mDevices.valueAt(i);
466 device->getDeviceInfo(& deviceInfo);
467 uint32_t sources = deviceInfo.getSources();
468
469 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
470 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
471 }
472 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
473 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
474 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
475 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
476 }
477 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
478 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
Jeff Browne839a582010-04-22 18:58:52 -0700479 }
480 }
Jeff Browne57e8952010-07-23 21:28:06 -0700481 } // release device registry reader lock
Jeff Browne839a582010-04-22 18:58:52 -0700482
Jeff Browne57e8952010-07-23 21:28:06 -0700483 mInputConfiguration.touchScreen = touchScreenConfig;
484 mInputConfiguration.keyboard = keyboardConfig;
485 mInputConfiguration.navigation = navigationConfig;
486 } // release state lock
487}
488
489void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
490 { // acquire state lock
491 AutoMutex _l(mStateLock);
492
493 *outConfiguration = mInputConfiguration;
494 } // release state lock
495}
496
497status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
498 { // acquire device registry reader lock
499 RWLock::AutoRLock _rl(mDeviceRegistryLock);
500
501 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
502 if (deviceIndex < 0) {
503 return NAME_NOT_FOUND;
Jeff Browne839a582010-04-22 18:58:52 -0700504 }
505
Jeff Browne57e8952010-07-23 21:28:06 -0700506 InputDevice* device = mDevices.valueAt(deviceIndex);
507 if (device->isIgnored()) {
508 return NAME_NOT_FOUND;
Jeff Browne839a582010-04-22 18:58:52 -0700509 }
Jeff Browne57e8952010-07-23 21:28:06 -0700510
511 device->getDeviceInfo(outDeviceInfo);
512 return OK;
513 } // release device registy reader lock
514}
515
516void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
517 outDeviceIds.clear();
518
519 { // acquire device registry reader lock
520 RWLock::AutoRLock _rl(mDeviceRegistryLock);
521
522 size_t numDevices = mDevices.size();
523 for (size_t i = 0; i < numDevices; i++) {
524 InputDevice* device = mDevices.valueAt(i);
525 if (! device->isIgnored()) {
526 outDeviceIds.add(device->getId());
527 }
528 }
529 } // release device registy reader lock
530}
531
532int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
533 int32_t keyCode) {
534 return getState(deviceId, sourceMask, keyCode, & InputDevice::getKeyCodeState);
535}
536
537int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
538 int32_t scanCode) {
539 return getState(deviceId, sourceMask, scanCode, & InputDevice::getScanCodeState);
540}
541
542int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
543 return getState(deviceId, sourceMask, switchCode, & InputDevice::getSwitchState);
544}
545
546int32_t InputReader::getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
547 GetStateFunc getStateFunc) {
548 { // acquire device registry reader lock
549 RWLock::AutoRLock _rl(mDeviceRegistryLock);
550
551 int32_t result = AKEY_STATE_UNKNOWN;
552 if (deviceId >= 0) {
553 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
554 if (deviceIndex >= 0) {
555 InputDevice* device = mDevices.valueAt(deviceIndex);
556 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
557 result = (device->*getStateFunc)(sourceMask, code);
558 }
559 }
560 } else {
561 size_t numDevices = mDevices.size();
562 for (size_t i = 0; i < numDevices; i++) {
563 InputDevice* device = mDevices.valueAt(i);
564 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
565 result = (device->*getStateFunc)(sourceMask, code);
566 if (result >= AKEY_STATE_DOWN) {
567 return result;
568 }
569 }
570 }
571 }
572 return result;
573 } // release device registy reader lock
574}
575
576bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
577 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
578 memset(outFlags, 0, numCodes);
579 return markSupportedKeyCodes(deviceId, sourceMask, numCodes, keyCodes, outFlags);
580}
581
582bool InputReader::markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
583 const int32_t* keyCodes, uint8_t* outFlags) {
584 { // acquire device registry reader lock
585 RWLock::AutoRLock _rl(mDeviceRegistryLock);
586 bool result = false;
587 if (deviceId >= 0) {
588 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
589 if (deviceIndex >= 0) {
590 InputDevice* device = mDevices.valueAt(deviceIndex);
591 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
592 result = device->markSupportedKeyCodes(sourceMask,
593 numCodes, keyCodes, outFlags);
594 }
595 }
596 } else {
597 size_t numDevices = mDevices.size();
598 for (size_t i = 0; i < numDevices; i++) {
599 InputDevice* device = mDevices.valueAt(i);
600 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
601 result |= device->markSupportedKeyCodes(sourceMask,
602 numCodes, keyCodes, outFlags);
603 }
604 }
605 }
606 return result;
607 } // release device registy reader lock
608}
609
Jeff Browna665ca82010-09-08 11:49:43 -0700610void InputReader::dump(String8& dump) {
Jeff Brown2806e382010-10-01 17:46:21 -0700611 mEventHub->dump(dump);
612 dump.append("\n");
613
614 dump.append("Input Reader State:\n");
615
Jeff Brown26c94ff2010-09-30 14:33:04 -0700616 { // acquire device registry reader lock
617 RWLock::AutoRLock _rl(mDeviceRegistryLock);
Jeff Browna665ca82010-09-08 11:49:43 -0700618
Jeff Brown26c94ff2010-09-30 14:33:04 -0700619 for (size_t i = 0; i < mDevices.size(); i++) {
620 mDevices.valueAt(i)->dump(dump);
Jeff Browna665ca82010-09-08 11:49:43 -0700621 }
Jeff Brown26c94ff2010-09-30 14:33:04 -0700622 } // release device registy reader lock
Jeff Browna665ca82010-09-08 11:49:43 -0700623}
624
Jeff Browne57e8952010-07-23 21:28:06 -0700625
626// --- InputReaderThread ---
627
628InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
629 Thread(/*canCallJava*/ true), mReader(reader) {
630}
631
632InputReaderThread::~InputReaderThread() {
633}
634
635bool InputReaderThread::threadLoop() {
636 mReader->loopOnce();
637 return true;
638}
639
640
641// --- InputDevice ---
642
643InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) :
644 mContext(context), mId(id), mName(name), mSources(0) {
645}
646
647InputDevice::~InputDevice() {
648 size_t numMappers = mMappers.size();
649 for (size_t i = 0; i < numMappers; i++) {
650 delete mMappers[i];
651 }
652 mMappers.clear();
653}
654
Jeff Brown26c94ff2010-09-30 14:33:04 -0700655static void dumpMotionRange(String8& dump, const InputDeviceInfo& deviceInfo,
656 int32_t rangeType, const char* name) {
657 const InputDeviceInfo::MotionRange* range = deviceInfo.getMotionRange(rangeType);
658 if (range) {
659 dump.appendFormat(INDENT3 "%s: min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
660 name, range->min, range->max, range->flat, range->fuzz);
661 }
662}
663
664void InputDevice::dump(String8& dump) {
665 InputDeviceInfo deviceInfo;
666 getDeviceInfo(& deviceInfo);
667
668 dump.appendFormat(INDENT "Device 0x%x: %s\n", deviceInfo.getId(),
669 deviceInfo.getName().string());
670 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
671 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
672 if (!deviceInfo.getMotionRanges().isEmpty()) {
673 dump.append(INDENT2 "Motion Ranges:\n");
674 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_X, "X");
675 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_Y, "Y");
676 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_PRESSURE, "Pressure");
677 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_SIZE, "Size");
678 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOUCH_MAJOR, "TouchMajor");
679 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOUCH_MINOR, "TouchMinor");
680 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOOL_MAJOR, "ToolMajor");
681 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOOL_MINOR, "ToolMinor");
682 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_ORIENTATION, "Orientation");
683 }
684
685 size_t numMappers = mMappers.size();
686 for (size_t i = 0; i < numMappers; i++) {
687 InputMapper* mapper = mMappers[i];
688 mapper->dump(dump);
689 }
690}
691
Jeff Browne57e8952010-07-23 21:28:06 -0700692void InputDevice::addMapper(InputMapper* mapper) {
693 mMappers.add(mapper);
694}
695
696void InputDevice::configure() {
Jeff Brown38a7fab2010-08-30 03:02:23 -0700697 if (! isIgnored()) {
698 mContext->getPolicy()->getInputDeviceCalibration(mName, mCalibration);
699 }
700
Jeff Browne57e8952010-07-23 21:28:06 -0700701 mSources = 0;
702
703 size_t numMappers = mMappers.size();
704 for (size_t i = 0; i < numMappers; i++) {
705 InputMapper* mapper = mMappers[i];
706 mapper->configure();
707 mSources |= mapper->getSources();
Jeff Browne839a582010-04-22 18:58:52 -0700708 }
709}
710
Jeff Browne57e8952010-07-23 21:28:06 -0700711void InputDevice::reset() {
712 size_t numMappers = mMappers.size();
713 for (size_t i = 0; i < numMappers; i++) {
714 InputMapper* mapper = mMappers[i];
715 mapper->reset();
716 }
717}
Jeff Browne839a582010-04-22 18:58:52 -0700718
Jeff Browne57e8952010-07-23 21:28:06 -0700719void InputDevice::process(const RawEvent* rawEvent) {
720 size_t numMappers = mMappers.size();
721 for (size_t i = 0; i < numMappers; i++) {
722 InputMapper* mapper = mMappers[i];
723 mapper->process(rawEvent);
724 }
725}
Jeff Browne839a582010-04-22 18:58:52 -0700726
Jeff Browne57e8952010-07-23 21:28:06 -0700727void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
728 outDeviceInfo->initialize(mId, mName);
729
730 size_t numMappers = mMappers.size();
731 for (size_t i = 0; i < numMappers; i++) {
732 InputMapper* mapper = mMappers[i];
733 mapper->populateDeviceInfo(outDeviceInfo);
734 }
735}
736
737int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
738 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
739}
740
741int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
742 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
743}
744
745int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
746 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
747}
748
749int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
750 int32_t result = AKEY_STATE_UNKNOWN;
751 size_t numMappers = mMappers.size();
752 for (size_t i = 0; i < numMappers; i++) {
753 InputMapper* mapper = mMappers[i];
754 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
755 result = (mapper->*getStateFunc)(sourceMask, code);
756 if (result >= AKEY_STATE_DOWN) {
757 return result;
758 }
759 }
760 }
761 return result;
762}
763
764bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
765 const int32_t* keyCodes, uint8_t* outFlags) {
766 bool result = false;
767 size_t numMappers = mMappers.size();
768 for (size_t i = 0; i < numMappers; i++) {
769 InputMapper* mapper = mMappers[i];
770 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
771 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
772 }
773 }
774 return result;
775}
776
777int32_t InputDevice::getMetaState() {
778 int32_t result = 0;
779 size_t numMappers = mMappers.size();
780 for (size_t i = 0; i < numMappers; i++) {
781 InputMapper* mapper = mMappers[i];
782 result |= mapper->getMetaState();
783 }
784 return result;
785}
786
787
788// --- InputMapper ---
789
790InputMapper::InputMapper(InputDevice* device) :
791 mDevice(device), mContext(device->getContext()) {
792}
793
794InputMapper::~InputMapper() {
795}
796
797void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
798 info->addSource(getSources());
799}
800
Jeff Brown26c94ff2010-09-30 14:33:04 -0700801void InputMapper::dump(String8& dump) {
802}
803
Jeff Browne57e8952010-07-23 21:28:06 -0700804void InputMapper::configure() {
805}
806
807void InputMapper::reset() {
808}
809
810int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
811 return AKEY_STATE_UNKNOWN;
812}
813
814int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
815 return AKEY_STATE_UNKNOWN;
816}
817
818int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
819 return AKEY_STATE_UNKNOWN;
820}
821
822bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
823 const int32_t* keyCodes, uint8_t* outFlags) {
824 return false;
825}
826
827int32_t InputMapper::getMetaState() {
828 return 0;
829}
830
Jeff Browne57e8952010-07-23 21:28:06 -0700831
832// --- SwitchInputMapper ---
833
834SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
835 InputMapper(device) {
836}
837
838SwitchInputMapper::~SwitchInputMapper() {
839}
840
841uint32_t SwitchInputMapper::getSources() {
842 return 0;
843}
844
845void SwitchInputMapper::process(const RawEvent* rawEvent) {
846 switch (rawEvent->type) {
847 case EV_SW:
848 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
849 break;
850 }
851}
852
853void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brown90f0cee2010-10-08 22:31:17 -0700854 getDispatcher()->notifySwitch(when, switchCode, switchValue, 0);
Jeff Browne57e8952010-07-23 21:28:06 -0700855}
856
857int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
858 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
859}
860
861
862// --- KeyboardInputMapper ---
863
864KeyboardInputMapper::KeyboardInputMapper(InputDevice* device, int32_t associatedDisplayId,
865 uint32_t sources, int32_t keyboardType) :
866 InputMapper(device), mAssociatedDisplayId(associatedDisplayId), mSources(sources),
867 mKeyboardType(keyboardType) {
Jeff Brownb51719b2010-07-29 18:18:33 -0700868 initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700869}
870
871KeyboardInputMapper::~KeyboardInputMapper() {
872}
873
Jeff Brownb51719b2010-07-29 18:18:33 -0700874void KeyboardInputMapper::initializeLocked() {
875 mLocked.metaState = AMETA_NONE;
876 mLocked.downTime = 0;
Jeff Brown6a817e22010-09-12 17:55:08 -0700877
878 initializeLedStateLocked(mLocked.capsLockLedState, LED_CAPSL);
879 initializeLedStateLocked(mLocked.numLockLedState, LED_NUML);
880 initializeLedStateLocked(mLocked.scrollLockLedState, LED_SCROLLL);
881
882 updateLedStateLocked(true);
883}
884
885void KeyboardInputMapper::initializeLedStateLocked(LockedState::LedState& ledState, int32_t led) {
886 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
887 ledState.on = false;
Jeff Browne57e8952010-07-23 21:28:06 -0700888}
889
890uint32_t KeyboardInputMapper::getSources() {
891 return mSources;
892}
893
894void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
895 InputMapper::populateDeviceInfo(info);
896
897 info->setKeyboardType(mKeyboardType);
898}
899
Jeff Brown26c94ff2010-09-30 14:33:04 -0700900void KeyboardInputMapper::dump(String8& dump) {
901 { // acquire lock
902 AutoMutex _l(mLock);
903 dump.append(INDENT2 "Keyboard Input Mapper:\n");
904 dump.appendFormat(INDENT3 "AssociatedDisplayId: %d\n", mAssociatedDisplayId);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700905 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
906 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mLocked.keyDowns.size());
907 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mLocked.metaState);
908 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
909 } // release lock
910}
911
Jeff Browne57e8952010-07-23 21:28:06 -0700912void KeyboardInputMapper::reset() {
Jeff Brownb51719b2010-07-29 18:18:33 -0700913 for (;;) {
914 int32_t keyCode, scanCode;
915 { // acquire lock
916 AutoMutex _l(mLock);
917
918 // Synthesize key up event on reset if keys are currently down.
919 if (mLocked.keyDowns.isEmpty()) {
920 initializeLocked();
921 break; // done
922 }
923
924 const KeyDown& keyDown = mLocked.keyDowns.top();
925 keyCode = keyDown.keyCode;
926 scanCode = keyDown.scanCode;
927 } // release lock
928
Jeff Browne57e8952010-07-23 21:28:06 -0700929 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownb51719b2010-07-29 18:18:33 -0700930 processKey(when, false, keyCode, scanCode, 0);
Jeff Browne57e8952010-07-23 21:28:06 -0700931 }
932
933 InputMapper::reset();
Jeff Browne57e8952010-07-23 21:28:06 -0700934 getContext()->updateGlobalMetaState();
935}
936
937void KeyboardInputMapper::process(const RawEvent* rawEvent) {
938 switch (rawEvent->type) {
939 case EV_KEY: {
940 int32_t scanCode = rawEvent->scanCode;
941 if (isKeyboardOrGamepadKey(scanCode)) {
942 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
943 rawEvent->flags);
944 }
945 break;
946 }
947 }
948}
949
950bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
951 return scanCode < BTN_MOUSE
952 || scanCode >= KEY_OK
953 || (scanCode >= BTN_GAMEPAD && scanCode < BTN_DIGI);
954}
955
Jeff Brownb51719b2010-07-29 18:18:33 -0700956void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
957 int32_t scanCode, uint32_t policyFlags) {
958 int32_t newMetaState;
959 nsecs_t downTime;
960 bool metaStateChanged = false;
961
962 { // acquire lock
963 AutoMutex _l(mLock);
964
965 if (down) {
966 // Rotate key codes according to orientation if needed.
967 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
968 if (mAssociatedDisplayId >= 0) {
969 int32_t orientation;
970 if (! getPolicy()->getDisplayInfo(mAssociatedDisplayId, NULL, NULL, & orientation)) {
971 return;
972 }
973
974 keyCode = rotateKeyCode(keyCode, orientation);
Jeff Browne57e8952010-07-23 21:28:06 -0700975 }
976
Jeff Brownb51719b2010-07-29 18:18:33 -0700977 // Add key down.
978 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
979 if (keyDownIndex >= 0) {
980 // key repeat, be sure to use same keycode as before in case of rotation
981 keyCode = mLocked.keyDowns.top().keyCode;
982 } else {
983 // key down
984 mLocked.keyDowns.push();
985 KeyDown& keyDown = mLocked.keyDowns.editTop();
986 keyDown.keyCode = keyCode;
987 keyDown.scanCode = scanCode;
988 }
989
990 mLocked.downTime = when;
991 } else {
992 // Remove key down.
993 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
994 if (keyDownIndex >= 0) {
995 // key up, be sure to use same keycode as before in case of rotation
996 keyCode = mLocked.keyDowns.top().keyCode;
997 mLocked.keyDowns.removeAt(size_t(keyDownIndex));
998 } else {
999 // key was not actually down
1000 LOGI("Dropping key up from device %s because the key was not down. "
1001 "keyCode=%d, scanCode=%d",
1002 getDeviceName().string(), keyCode, scanCode);
1003 return;
1004 }
Jeff Browne57e8952010-07-23 21:28:06 -07001005 }
1006
Jeff Brownb51719b2010-07-29 18:18:33 -07001007 int32_t oldMetaState = mLocked.metaState;
1008 newMetaState = updateMetaState(keyCode, down, oldMetaState);
1009 if (oldMetaState != newMetaState) {
1010 mLocked.metaState = newMetaState;
1011 metaStateChanged = true;
Jeff Brown6a817e22010-09-12 17:55:08 -07001012 updateLedStateLocked(false);
Jeff Browne57e8952010-07-23 21:28:06 -07001013 }
Jeff Brown8575a872010-06-30 16:10:35 -07001014
Jeff Brownb51719b2010-07-29 18:18:33 -07001015 downTime = mLocked.downTime;
1016 } // release lock
1017
1018 if (metaStateChanged) {
Jeff Browne57e8952010-07-23 21:28:06 -07001019 getContext()->updateGlobalMetaState();
Jeff Browne839a582010-04-22 18:58:52 -07001020 }
1021
Jeff Brown6a817e22010-09-12 17:55:08 -07001022 if (policyFlags & POLICY_FLAG_FUNCTION) {
1023 newMetaState |= AMETA_FUNCTION_ON;
1024 }
Jeff Browne57e8952010-07-23 21:28:06 -07001025 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
Jeff Brown90f0cee2010-10-08 22:31:17 -07001026 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1027 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Browne839a582010-04-22 18:58:52 -07001028}
1029
Jeff Brownb51719b2010-07-29 18:18:33 -07001030ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) {
1031 size_t n = mLocked.keyDowns.size();
Jeff Browne57e8952010-07-23 21:28:06 -07001032 for (size_t i = 0; i < n; i++) {
Jeff Brownb51719b2010-07-29 18:18:33 -07001033 if (mLocked.keyDowns[i].scanCode == scanCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07001034 return i;
1035 }
1036 }
1037 return -1;
Jeff Browne839a582010-04-22 18:58:52 -07001038}
1039
Jeff Browne57e8952010-07-23 21:28:06 -07001040int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1041 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1042}
Jeff Browne839a582010-04-22 18:58:52 -07001043
Jeff Browne57e8952010-07-23 21:28:06 -07001044int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1045 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1046}
Jeff Browne839a582010-04-22 18:58:52 -07001047
Jeff Browne57e8952010-07-23 21:28:06 -07001048bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1049 const int32_t* keyCodes, uint8_t* outFlags) {
1050 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1051}
1052
1053int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownb51719b2010-07-29 18:18:33 -07001054 { // acquire lock
1055 AutoMutex _l(mLock);
1056 return mLocked.metaState;
1057 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07001058}
1059
Jeff Brown6a817e22010-09-12 17:55:08 -07001060void KeyboardInputMapper::updateLedStateLocked(bool reset) {
1061 updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL,
1062 AMETA_CAPS_LOCK_LATCHED, reset);
1063 updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML,
1064 AMETA_NUM_LOCK_LATCHED, reset);
1065 updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL,
1066 AMETA_SCROLL_LOCK_LATCHED, reset);
1067}
1068
1069void KeyboardInputMapper::updateLedStateForModifierLocked(LockedState::LedState& ledState,
1070 int32_t led, int32_t modifier, bool reset) {
1071 if (ledState.avail) {
1072 bool desiredState = (mLocked.metaState & modifier) != 0;
1073 if (ledState.on != desiredState) {
1074 getEventHub()->setLedState(getDeviceId(), led, desiredState);
1075 ledState.on = desiredState;
1076 }
1077 }
1078}
1079
Jeff Browne57e8952010-07-23 21:28:06 -07001080
1081// --- TrackballInputMapper ---
1082
1083TrackballInputMapper::TrackballInputMapper(InputDevice* device, int32_t associatedDisplayId) :
1084 InputMapper(device), mAssociatedDisplayId(associatedDisplayId) {
1085 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1086 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1087 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1088 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1089
Jeff Brownb51719b2010-07-29 18:18:33 -07001090 initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -07001091}
1092
1093TrackballInputMapper::~TrackballInputMapper() {
1094}
1095
1096uint32_t TrackballInputMapper::getSources() {
1097 return AINPUT_SOURCE_TRACKBALL;
1098}
1099
1100void TrackballInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1101 InputMapper::populateDeviceInfo(info);
1102
1103 info->addMotionRange(AINPUT_MOTION_RANGE_X, -1.0f, 1.0f, 0.0f, mXScale);
1104 info->addMotionRange(AINPUT_MOTION_RANGE_Y, -1.0f, 1.0f, 0.0f, mYScale);
1105}
1106
Jeff Brown26c94ff2010-09-30 14:33:04 -07001107void TrackballInputMapper::dump(String8& dump) {
1108 { // acquire lock
1109 AutoMutex _l(mLock);
1110 dump.append(INDENT2 "Trackball Input Mapper:\n");
1111 dump.appendFormat(INDENT3 "AssociatedDisplayId: %d\n", mAssociatedDisplayId);
1112 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1113 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
1114 dump.appendFormat(INDENT3 "Down: %s\n", toString(mLocked.down));
1115 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1116 } // release lock
1117}
1118
Jeff Brownb51719b2010-07-29 18:18:33 -07001119void TrackballInputMapper::initializeLocked() {
Jeff Browne57e8952010-07-23 21:28:06 -07001120 mAccumulator.clear();
1121
Jeff Brownb51719b2010-07-29 18:18:33 -07001122 mLocked.down = false;
1123 mLocked.downTime = 0;
Jeff Browne57e8952010-07-23 21:28:06 -07001124}
1125
1126void TrackballInputMapper::reset() {
Jeff Brownb51719b2010-07-29 18:18:33 -07001127 for (;;) {
1128 { // acquire lock
1129 AutoMutex _l(mLock);
1130
1131 if (! mLocked.down) {
1132 initializeLocked();
1133 break; // done
1134 }
1135 } // release lock
1136
1137 // Synthesize trackball button up event on reset.
Jeff Browne57e8952010-07-23 21:28:06 -07001138 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownb51719b2010-07-29 18:18:33 -07001139 mAccumulator.fields = Accumulator::FIELD_BTN_MOUSE;
Jeff Browne57e8952010-07-23 21:28:06 -07001140 mAccumulator.btnMouse = false;
1141 sync(when);
Jeff Browne839a582010-04-22 18:58:52 -07001142 }
1143
Jeff Browne57e8952010-07-23 21:28:06 -07001144 InputMapper::reset();
Jeff Browne57e8952010-07-23 21:28:06 -07001145}
Jeff Browne839a582010-04-22 18:58:52 -07001146
Jeff Browne57e8952010-07-23 21:28:06 -07001147void TrackballInputMapper::process(const RawEvent* rawEvent) {
1148 switch (rawEvent->type) {
1149 case EV_KEY:
1150 switch (rawEvent->scanCode) {
1151 case BTN_MOUSE:
1152 mAccumulator.fields |= Accumulator::FIELD_BTN_MOUSE;
1153 mAccumulator.btnMouse = rawEvent->value != 0;
Jeff Brownd64c8552010-08-17 20:38:35 -07001154 // Sync now since BTN_MOUSE is not necessarily followed by SYN_REPORT and
1155 // we need to ensure that we report the up/down promptly.
Jeff Browne57e8952010-07-23 21:28:06 -07001156 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07001157 break;
Jeff Browne839a582010-04-22 18:58:52 -07001158 }
Jeff Browne57e8952010-07-23 21:28:06 -07001159 break;
Jeff Browne839a582010-04-22 18:58:52 -07001160
Jeff Browne57e8952010-07-23 21:28:06 -07001161 case EV_REL:
1162 switch (rawEvent->scanCode) {
1163 case REL_X:
1164 mAccumulator.fields |= Accumulator::FIELD_REL_X;
1165 mAccumulator.relX = rawEvent->value;
1166 break;
1167 case REL_Y:
1168 mAccumulator.fields |= Accumulator::FIELD_REL_Y;
1169 mAccumulator.relY = rawEvent->value;
1170 break;
Jeff Browne839a582010-04-22 18:58:52 -07001171 }
Jeff Browne57e8952010-07-23 21:28:06 -07001172 break;
Jeff Browne839a582010-04-22 18:58:52 -07001173
Jeff Browne57e8952010-07-23 21:28:06 -07001174 case EV_SYN:
1175 switch (rawEvent->scanCode) {
1176 case SYN_REPORT:
Jeff Brownd64c8552010-08-17 20:38:35 -07001177 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07001178 break;
Jeff Browne839a582010-04-22 18:58:52 -07001179 }
Jeff Browne57e8952010-07-23 21:28:06 -07001180 break;
Jeff Browne839a582010-04-22 18:58:52 -07001181 }
Jeff Browne839a582010-04-22 18:58:52 -07001182}
1183
Jeff Browne57e8952010-07-23 21:28:06 -07001184void TrackballInputMapper::sync(nsecs_t when) {
Jeff Brownd64c8552010-08-17 20:38:35 -07001185 uint32_t fields = mAccumulator.fields;
1186 if (fields == 0) {
1187 return; // no new state changes, so nothing to do
1188 }
1189
Jeff Brownb51719b2010-07-29 18:18:33 -07001190 int motionEventAction;
1191 PointerCoords pointerCoords;
1192 nsecs_t downTime;
1193 { // acquire lock
1194 AutoMutex _l(mLock);
Jeff Browne839a582010-04-22 18:58:52 -07001195
Jeff Brownb51719b2010-07-29 18:18:33 -07001196 bool downChanged = fields & Accumulator::FIELD_BTN_MOUSE;
1197
1198 if (downChanged) {
1199 if (mAccumulator.btnMouse) {
1200 mLocked.down = true;
1201 mLocked.downTime = when;
1202 } else {
1203 mLocked.down = false;
1204 }
Jeff Browne57e8952010-07-23 21:28:06 -07001205 }
Jeff Browne839a582010-04-22 18:58:52 -07001206
Jeff Brownb51719b2010-07-29 18:18:33 -07001207 downTime = mLocked.downTime;
1208 float x = fields & Accumulator::FIELD_REL_X ? mAccumulator.relX * mXScale : 0.0f;
1209 float y = fields & Accumulator::FIELD_REL_Y ? mAccumulator.relY * mYScale : 0.0f;
Jeff Browne839a582010-04-22 18:58:52 -07001210
Jeff Brownb51719b2010-07-29 18:18:33 -07001211 if (downChanged) {
1212 motionEventAction = mLocked.down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
Jeff Browne57e8952010-07-23 21:28:06 -07001213 } else {
Jeff Brownb51719b2010-07-29 18:18:33 -07001214 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browne57e8952010-07-23 21:28:06 -07001215 }
Jeff Browne839a582010-04-22 18:58:52 -07001216
Jeff Brownb51719b2010-07-29 18:18:33 -07001217 pointerCoords.x = x;
1218 pointerCoords.y = y;
1219 pointerCoords.pressure = mLocked.down ? 1.0f : 0.0f;
1220 pointerCoords.size = 0;
1221 pointerCoords.touchMajor = 0;
1222 pointerCoords.touchMinor = 0;
1223 pointerCoords.toolMajor = 0;
1224 pointerCoords.toolMinor = 0;
1225 pointerCoords.orientation = 0;
Jeff Browne839a582010-04-22 18:58:52 -07001226
Jeff Brownb51719b2010-07-29 18:18:33 -07001227 if (mAssociatedDisplayId >= 0 && (x != 0.0f || y != 0.0f)) {
1228 // Rotate motion based on display orientation if needed.
1229 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1230 int32_t orientation;
1231 if (! getPolicy()->getDisplayInfo(mAssociatedDisplayId, NULL, NULL, & orientation)) {
1232 return;
1233 }
1234
1235 float temp;
1236 switch (orientation) {
1237 case InputReaderPolicyInterface::ROTATION_90:
1238 temp = pointerCoords.x;
1239 pointerCoords.x = pointerCoords.y;
1240 pointerCoords.y = - temp;
1241 break;
1242
1243 case InputReaderPolicyInterface::ROTATION_180:
1244 pointerCoords.x = - pointerCoords.x;
1245 pointerCoords.y = - pointerCoords.y;
1246 break;
1247
1248 case InputReaderPolicyInterface::ROTATION_270:
1249 temp = pointerCoords.x;
1250 pointerCoords.x = - pointerCoords.y;
1251 pointerCoords.y = temp;
1252 break;
1253 }
1254 }
1255 } // release lock
1256
Jeff Browne57e8952010-07-23 21:28:06 -07001257 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownb51719b2010-07-29 18:18:33 -07001258 int32_t pointerId = 0;
Jeff Brown90f0cee2010-10-08 22:31:17 -07001259 getDispatcher()->notifyMotion(when, getDeviceId(), AINPUT_SOURCE_TRACKBALL, 0,
Jeff Brownaf30ff62010-09-01 17:01:00 -07001260 motionEventAction, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown90f0cee2010-10-08 22:31:17 -07001261 1, &pointerId, &pointerCoords, mXPrecision, mYPrecision, downTime);
1262
1263 mAccumulator.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07001264}
1265
Jeff Brown8d4dfd22010-08-10 15:47:53 -07001266int32_t TrackballInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1267 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
1268 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1269 } else {
1270 return AKEY_STATE_UNKNOWN;
1271 }
1272}
1273
Jeff Browne57e8952010-07-23 21:28:06 -07001274
1275// --- TouchInputMapper ---
1276
1277TouchInputMapper::TouchInputMapper(InputDevice* device, int32_t associatedDisplayId) :
Jeff Brownb51719b2010-07-29 18:18:33 -07001278 InputMapper(device), mAssociatedDisplayId(associatedDisplayId) {
1279 mLocked.surfaceOrientation = -1;
1280 mLocked.surfaceWidth = -1;
1281 mLocked.surfaceHeight = -1;
1282
1283 initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -07001284}
1285
1286TouchInputMapper::~TouchInputMapper() {
1287}
1288
1289uint32_t TouchInputMapper::getSources() {
1290 return mAssociatedDisplayId >= 0 ? AINPUT_SOURCE_TOUCHSCREEN : AINPUT_SOURCE_TOUCHPAD;
1291}
1292
1293void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1294 InputMapper::populateDeviceInfo(info);
1295
Jeff Brownb51719b2010-07-29 18:18:33 -07001296 { // acquire lock
1297 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07001298
Jeff Brownb51719b2010-07-29 18:18:33 -07001299 // Ensure surface information is up to date so that orientation changes are
1300 // noticed immediately.
1301 configureSurfaceLocked();
1302
1303 info->addMotionRange(AINPUT_MOTION_RANGE_X, mLocked.orientedRanges.x);
1304 info->addMotionRange(AINPUT_MOTION_RANGE_Y, mLocked.orientedRanges.y);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001305
1306 if (mLocked.orientedRanges.havePressure) {
1307 info->addMotionRange(AINPUT_MOTION_RANGE_PRESSURE,
1308 mLocked.orientedRanges.pressure);
1309 }
1310
1311 if (mLocked.orientedRanges.haveSize) {
1312 info->addMotionRange(AINPUT_MOTION_RANGE_SIZE,
1313 mLocked.orientedRanges.size);
1314 }
1315
Jeff Brown6b337e72010-10-14 21:42:15 -07001316 if (mLocked.orientedRanges.haveTouchSize) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001317 info->addMotionRange(AINPUT_MOTION_RANGE_TOUCH_MAJOR,
1318 mLocked.orientedRanges.touchMajor);
1319 info->addMotionRange(AINPUT_MOTION_RANGE_TOUCH_MINOR,
1320 mLocked.orientedRanges.touchMinor);
1321 }
1322
Jeff Brown6b337e72010-10-14 21:42:15 -07001323 if (mLocked.orientedRanges.haveToolSize) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001324 info->addMotionRange(AINPUT_MOTION_RANGE_TOOL_MAJOR,
1325 mLocked.orientedRanges.toolMajor);
1326 info->addMotionRange(AINPUT_MOTION_RANGE_TOOL_MINOR,
1327 mLocked.orientedRanges.toolMinor);
1328 }
1329
1330 if (mLocked.orientedRanges.haveOrientation) {
1331 info->addMotionRange(AINPUT_MOTION_RANGE_ORIENTATION,
1332 mLocked.orientedRanges.orientation);
1333 }
Jeff Brownb51719b2010-07-29 18:18:33 -07001334 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07001335}
1336
Jeff Brown26c94ff2010-09-30 14:33:04 -07001337void TouchInputMapper::dump(String8& dump) {
1338 { // acquire lock
1339 AutoMutex _l(mLock);
1340 dump.append(INDENT2 "Touch Input Mapper:\n");
1341 dump.appendFormat(INDENT3 "AssociatedDisplayId: %d\n", mAssociatedDisplayId);
1342 dumpParameters(dump);
1343 dumpVirtualKeysLocked(dump);
1344 dumpRawAxes(dump);
1345 dumpCalibration(dump);
1346 dumpSurfaceLocked(dump);
Jeff Brown60b57762010-10-18 13:32:20 -07001347 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
Jeff Brown6b337e72010-10-14 21:42:15 -07001348 dump.appendFormat(INDENT4 "XOrigin: %d\n", mLocked.xOrigin);
1349 dump.appendFormat(INDENT4 "YOrigin: %d\n", mLocked.yOrigin);
1350 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mLocked.xScale);
1351 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mLocked.yScale);
1352 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mLocked.xPrecision);
1353 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mLocked.yPrecision);
1354 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mLocked.geometricScale);
1355 dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mLocked.toolSizeLinearScale);
1356 dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mLocked.toolSizeLinearBias);
1357 dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mLocked.toolSizeAreaScale);
1358 dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mLocked.toolSizeAreaBias);
1359 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mLocked.pressureScale);
1360 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mLocked.sizeScale);
1361 dump.appendFormat(INDENT4 "OrientationSCale: %0.3f\n", mLocked.orientationScale);
Jeff Brown26c94ff2010-09-30 14:33:04 -07001362 } // release lock
1363}
1364
Jeff Brownb51719b2010-07-29 18:18:33 -07001365void TouchInputMapper::initializeLocked() {
1366 mCurrentTouch.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07001367 mLastTouch.clear();
1368 mDownTime = 0;
Jeff Browne57e8952010-07-23 21:28:06 -07001369
1370 for (uint32_t i = 0; i < MAX_POINTERS; i++) {
1371 mAveragingTouchFilter.historyStart[i] = 0;
1372 mAveragingTouchFilter.historyEnd[i] = 0;
1373 }
1374
1375 mJumpyTouchFilter.jumpyPointsDropped = 0;
Jeff Brownb51719b2010-07-29 18:18:33 -07001376
1377 mLocked.currentVirtualKey.down = false;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001378
1379 mLocked.orientedRanges.havePressure = false;
1380 mLocked.orientedRanges.haveSize = false;
Jeff Brown6b337e72010-10-14 21:42:15 -07001381 mLocked.orientedRanges.haveTouchSize = false;
1382 mLocked.orientedRanges.haveToolSize = false;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001383 mLocked.orientedRanges.haveOrientation = false;
1384}
1385
Jeff Browne57e8952010-07-23 21:28:06 -07001386void TouchInputMapper::configure() {
1387 InputMapper::configure();
1388
1389 // Configure basic parameters.
Jeff Brown38a7fab2010-08-30 03:02:23 -07001390 configureParameters();
Jeff Browne57e8952010-07-23 21:28:06 -07001391
1392 // Configure absolute axis information.
Jeff Brown38a7fab2010-08-30 03:02:23 -07001393 configureRawAxes();
Jeff Brown38a7fab2010-08-30 03:02:23 -07001394
1395 // Prepare input device calibration.
1396 parseCalibration();
1397 resolveCalibration();
Jeff Browne57e8952010-07-23 21:28:06 -07001398
Jeff Brownb51719b2010-07-29 18:18:33 -07001399 { // acquire lock
1400 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07001401
Jeff Brown38a7fab2010-08-30 03:02:23 -07001402 // Configure surface dimensions and orientation.
Jeff Brownb51719b2010-07-29 18:18:33 -07001403 configureSurfaceLocked();
1404 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07001405}
1406
Jeff Brown38a7fab2010-08-30 03:02:23 -07001407void TouchInputMapper::configureParameters() {
1408 mParameters.useBadTouchFilter = getPolicy()->filterTouchEvents();
1409 mParameters.useAveragingTouchFilter = getPolicy()->filterTouchEvents();
1410 mParameters.useJumpyTouchFilter = getPolicy()->filterJumpyTouchEvents();
1411}
1412
Jeff Brown26c94ff2010-09-30 14:33:04 -07001413void TouchInputMapper::dumpParameters(String8& dump) {
1414 dump.appendFormat(INDENT3 "UseBadTouchFilter: %s\n",
1415 toString(mParameters.useBadTouchFilter));
1416 dump.appendFormat(INDENT3 "UseAveragingTouchFilter: %s\n",
1417 toString(mParameters.useAveragingTouchFilter));
1418 dump.appendFormat(INDENT3 "UseJumpyTouchFilter: %s\n",
1419 toString(mParameters.useJumpyTouchFilter));
Jeff Browna665ca82010-09-08 11:49:43 -07001420}
1421
Jeff Brown38a7fab2010-08-30 03:02:23 -07001422void TouchInputMapper::configureRawAxes() {
1423 mRawAxes.x.clear();
1424 mRawAxes.y.clear();
1425 mRawAxes.pressure.clear();
1426 mRawAxes.touchMajor.clear();
1427 mRawAxes.touchMinor.clear();
1428 mRawAxes.toolMajor.clear();
1429 mRawAxes.toolMinor.clear();
1430 mRawAxes.orientation.clear();
1431}
1432
Jeff Brown26c94ff2010-09-30 14:33:04 -07001433static void dumpAxisInfo(String8& dump, RawAbsoluteAxisInfo axis, const char* name) {
Jeff Browna665ca82010-09-08 11:49:43 -07001434 if (axis.valid) {
Jeff Brown26c94ff2010-09-30 14:33:04 -07001435 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d\n",
Jeff Browna665ca82010-09-08 11:49:43 -07001436 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz);
1437 } else {
Jeff Brown26c94ff2010-09-30 14:33:04 -07001438 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
Jeff Browna665ca82010-09-08 11:49:43 -07001439 }
1440}
1441
Jeff Brown26c94ff2010-09-30 14:33:04 -07001442void TouchInputMapper::dumpRawAxes(String8& dump) {
1443 dump.append(INDENT3 "Raw Axes:\n");
1444 dumpAxisInfo(dump, mRawAxes.x, "X");
1445 dumpAxisInfo(dump, mRawAxes.y, "Y");
1446 dumpAxisInfo(dump, mRawAxes.pressure, "Pressure");
1447 dumpAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor");
1448 dumpAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor");
1449 dumpAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor");
1450 dumpAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor");
1451 dumpAxisInfo(dump, mRawAxes.orientation, "Orientation");
Jeff Browne57e8952010-07-23 21:28:06 -07001452}
1453
Jeff Brownb51719b2010-07-29 18:18:33 -07001454bool TouchInputMapper::configureSurfaceLocked() {
Jeff Browne57e8952010-07-23 21:28:06 -07001455 // Update orientation and dimensions if needed.
1456 int32_t orientation;
1457 int32_t width, height;
1458 if (mAssociatedDisplayId >= 0) {
Jeff Brownb51719b2010-07-29 18:18:33 -07001459 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Browne57e8952010-07-23 21:28:06 -07001460 if (! getPolicy()->getDisplayInfo(mAssociatedDisplayId, & width, & height, & orientation)) {
1461 return false;
1462 }
1463 } else {
1464 orientation = InputReaderPolicyInterface::ROTATION_0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001465 width = mRawAxes.x.getRange();
1466 height = mRawAxes.y.getRange();
Jeff Browne57e8952010-07-23 21:28:06 -07001467 }
1468
Jeff Brownb51719b2010-07-29 18:18:33 -07001469 bool orientationChanged = mLocked.surfaceOrientation != orientation;
Jeff Browne57e8952010-07-23 21:28:06 -07001470 if (orientationChanged) {
Jeff Brownb51719b2010-07-29 18:18:33 -07001471 mLocked.surfaceOrientation = orientation;
Jeff Browne57e8952010-07-23 21:28:06 -07001472 }
1473
Jeff Brownb51719b2010-07-29 18:18:33 -07001474 bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height;
Jeff Browne57e8952010-07-23 21:28:06 -07001475 if (sizeChanged) {
Jeff Brown26c94ff2010-09-30 14:33:04 -07001476 LOGI("Device reconfigured: id=0x%x, name=%s, display size is now %dx%d",
1477 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001478
Jeff Brownb51719b2010-07-29 18:18:33 -07001479 mLocked.surfaceWidth = width;
1480 mLocked.surfaceHeight = height;
Jeff Browne57e8952010-07-23 21:28:06 -07001481
Jeff Brown38a7fab2010-08-30 03:02:23 -07001482 // Configure X and Y factors.
1483 if (mRawAxes.x.valid && mRawAxes.y.valid) {
Jeff Brown60b57762010-10-18 13:32:20 -07001484 mLocked.xOrigin = mCalibration.haveXOrigin
1485 ? mCalibration.xOrigin
1486 : mRawAxes.x.minValue;
1487 mLocked.yOrigin = mCalibration.haveYOrigin
1488 ? mCalibration.yOrigin
1489 : mRawAxes.y.minValue;
1490 mLocked.xScale = mCalibration.haveXScale
1491 ? mCalibration.xScale
1492 : float(width) / mRawAxes.x.getRange();
1493 mLocked.yScale = mCalibration.haveYScale
1494 ? mCalibration.yScale
1495 : float(height) / mRawAxes.y.getRange();
Jeff Brownb51719b2010-07-29 18:18:33 -07001496 mLocked.xPrecision = 1.0f / mLocked.xScale;
1497 mLocked.yPrecision = 1.0f / mLocked.yScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001498
Jeff Brownb51719b2010-07-29 18:18:33 -07001499 configureVirtualKeysLocked();
Jeff Browne57e8952010-07-23 21:28:06 -07001500 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001501 LOGW(INDENT "Touch device did not report support for X or Y axis!");
Jeff Brownb51719b2010-07-29 18:18:33 -07001502 mLocked.xOrigin = 0;
1503 mLocked.yOrigin = 0;
1504 mLocked.xScale = 1.0f;
1505 mLocked.yScale = 1.0f;
1506 mLocked.xPrecision = 1.0f;
1507 mLocked.yPrecision = 1.0f;
Jeff Browne57e8952010-07-23 21:28:06 -07001508 }
1509
Jeff Brown38a7fab2010-08-30 03:02:23 -07001510 // Scale factor for terms that are not oriented in a particular axis.
1511 // If the pixels are square then xScale == yScale otherwise we fake it
1512 // by choosing an average.
1513 mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale);
Jeff Browne57e8952010-07-23 21:28:06 -07001514
Jeff Brown38a7fab2010-08-30 03:02:23 -07001515 // Size of diagonal axis.
1516 float diagonalSize = pythag(width, height);
Jeff Browne57e8952010-07-23 21:28:06 -07001517
Jeff Brown38a7fab2010-08-30 03:02:23 -07001518 // TouchMajor and TouchMinor factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001519 if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) {
1520 mLocked.orientedRanges.haveTouchSize = true;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001521 mLocked.orientedRanges.touchMajor.min = 0;
1522 mLocked.orientedRanges.touchMajor.max = diagonalSize;
1523 mLocked.orientedRanges.touchMajor.flat = 0;
1524 mLocked.orientedRanges.touchMajor.fuzz = 0;
1525 mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor;
1526 }
Jeff Brownb51719b2010-07-29 18:18:33 -07001527
Jeff Brown38a7fab2010-08-30 03:02:23 -07001528 // ToolMajor and ToolMinor factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001529 mLocked.toolSizeLinearScale = 0;
1530 mLocked.toolSizeLinearBias = 0;
1531 mLocked.toolSizeAreaScale = 0;
1532 mLocked.toolSizeAreaBias = 0;
1533 if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
1534 if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) {
1535 if (mCalibration.haveToolSizeLinearScale) {
1536 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001537 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
Jeff Brown6b337e72010-10-14 21:42:15 -07001538 mLocked.toolSizeLinearScale = float(min(width, height))
Jeff Brown38a7fab2010-08-30 03:02:23 -07001539 / mRawAxes.toolMajor.maxValue;
1540 }
1541
Jeff Brown6b337e72010-10-14 21:42:15 -07001542 if (mCalibration.haveToolSizeLinearBias) {
1543 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
1544 }
1545 } else if (mCalibration.toolSizeCalibration ==
1546 Calibration::TOOL_SIZE_CALIBRATION_AREA) {
1547 if (mCalibration.haveToolSizeLinearScale) {
1548 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
1549 } else {
1550 mLocked.toolSizeLinearScale = min(width, height);
1551 }
1552
1553 if (mCalibration.haveToolSizeLinearBias) {
1554 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
1555 }
1556
1557 if (mCalibration.haveToolSizeAreaScale) {
1558 mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale;
1559 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1560 mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue;
1561 }
1562
1563 if (mCalibration.haveToolSizeAreaBias) {
1564 mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001565 }
1566 }
1567
Jeff Brown6b337e72010-10-14 21:42:15 -07001568 mLocked.orientedRanges.haveToolSize = true;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001569 mLocked.orientedRanges.toolMajor.min = 0;
1570 mLocked.orientedRanges.toolMajor.max = diagonalSize;
1571 mLocked.orientedRanges.toolMajor.flat = 0;
1572 mLocked.orientedRanges.toolMajor.fuzz = 0;
1573 mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor;
1574 }
1575
1576 // Pressure factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001577 mLocked.pressureScale = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001578 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
1579 RawAbsoluteAxisInfo rawPressureAxis;
1580 switch (mCalibration.pressureSource) {
1581 case Calibration::PRESSURE_SOURCE_PRESSURE:
1582 rawPressureAxis = mRawAxes.pressure;
1583 break;
1584 case Calibration::PRESSURE_SOURCE_TOUCH:
1585 rawPressureAxis = mRawAxes.touchMajor;
1586 break;
1587 default:
1588 rawPressureAxis.clear();
1589 }
1590
Jeff Brown38a7fab2010-08-30 03:02:23 -07001591 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
1592 || mCalibration.pressureCalibration
1593 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
1594 if (mCalibration.havePressureScale) {
1595 mLocked.pressureScale = mCalibration.pressureScale;
1596 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
1597 mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue;
1598 }
1599 }
1600
1601 mLocked.orientedRanges.havePressure = true;
1602 mLocked.orientedRanges.pressure.min = 0;
1603 mLocked.orientedRanges.pressure.max = 1.0;
1604 mLocked.orientedRanges.pressure.flat = 0;
1605 mLocked.orientedRanges.pressure.fuzz = 0;
1606 }
1607
1608 // Size factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001609 mLocked.sizeScale = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001610 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001611 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
1612 if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1613 mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue;
1614 }
1615 }
1616
1617 mLocked.orientedRanges.haveSize = true;
1618 mLocked.orientedRanges.size.min = 0;
1619 mLocked.orientedRanges.size.max = 1.0;
1620 mLocked.orientedRanges.size.flat = 0;
1621 mLocked.orientedRanges.size.fuzz = 0;
1622 }
1623
1624 // Orientation
Jeff Brown6b337e72010-10-14 21:42:15 -07001625 mLocked.orientationScale = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001626 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001627 if (mCalibration.orientationCalibration
1628 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
1629 if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) {
1630 mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue;
1631 }
1632 }
1633
1634 mLocked.orientedRanges.orientation.min = - M_PI_2;
1635 mLocked.orientedRanges.orientation.max = M_PI_2;
1636 mLocked.orientedRanges.orientation.flat = 0;
1637 mLocked.orientedRanges.orientation.fuzz = 0;
1638 }
Jeff Browne57e8952010-07-23 21:28:06 -07001639 }
1640
1641 if (orientationChanged || sizeChanged) {
1642 // Compute oriented surface dimensions, precision, and scales.
1643 float orientedXScale, orientedYScale;
Jeff Brownb51719b2010-07-29 18:18:33 -07001644 switch (mLocked.surfaceOrientation) {
Jeff Browne57e8952010-07-23 21:28:06 -07001645 case InputReaderPolicyInterface::ROTATION_90:
1646 case InputReaderPolicyInterface::ROTATION_270:
Jeff Brownb51719b2010-07-29 18:18:33 -07001647 mLocked.orientedSurfaceWidth = mLocked.surfaceHeight;
1648 mLocked.orientedSurfaceHeight = mLocked.surfaceWidth;
1649 mLocked.orientedXPrecision = mLocked.yPrecision;
1650 mLocked.orientedYPrecision = mLocked.xPrecision;
1651 orientedXScale = mLocked.yScale;
1652 orientedYScale = mLocked.xScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001653 break;
1654 default:
Jeff Brownb51719b2010-07-29 18:18:33 -07001655 mLocked.orientedSurfaceWidth = mLocked.surfaceWidth;
1656 mLocked.orientedSurfaceHeight = mLocked.surfaceHeight;
1657 mLocked.orientedXPrecision = mLocked.xPrecision;
1658 mLocked.orientedYPrecision = mLocked.yPrecision;
1659 orientedXScale = mLocked.xScale;
1660 orientedYScale = mLocked.yScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001661 break;
1662 }
1663
1664 // Configure position ranges.
Jeff Brownb51719b2010-07-29 18:18:33 -07001665 mLocked.orientedRanges.x.min = 0;
1666 mLocked.orientedRanges.x.max = mLocked.orientedSurfaceWidth;
1667 mLocked.orientedRanges.x.flat = 0;
1668 mLocked.orientedRanges.x.fuzz = orientedXScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001669
Jeff Brownb51719b2010-07-29 18:18:33 -07001670 mLocked.orientedRanges.y.min = 0;
1671 mLocked.orientedRanges.y.max = mLocked.orientedSurfaceHeight;
1672 mLocked.orientedRanges.y.flat = 0;
1673 mLocked.orientedRanges.y.fuzz = orientedYScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001674 }
1675
1676 return true;
1677}
1678
Jeff Brown26c94ff2010-09-30 14:33:04 -07001679void TouchInputMapper::dumpSurfaceLocked(String8& dump) {
1680 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth);
1681 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight);
1682 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation);
Jeff Browna665ca82010-09-08 11:49:43 -07001683}
1684
Jeff Brownb51719b2010-07-29 18:18:33 -07001685void TouchInputMapper::configureVirtualKeysLocked() {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001686 assert(mRawAxes.x.valid && mRawAxes.y.valid);
Jeff Browne57e8952010-07-23 21:28:06 -07001687
Jeff Brownb51719b2010-07-29 18:18:33 -07001688 // Note: getVirtualKeyDefinitions is non-reentrant so we can continue holding the lock.
Jeff Brown38a7fab2010-08-30 03:02:23 -07001689 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Browne57e8952010-07-23 21:28:06 -07001690 getPolicy()->getVirtualKeyDefinitions(getDeviceName(), virtualKeyDefinitions);
1691
Jeff Brownb51719b2010-07-29 18:18:33 -07001692 mLocked.virtualKeys.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07001693
Jeff Brownb51719b2010-07-29 18:18:33 -07001694 if (virtualKeyDefinitions.size() == 0) {
1695 return;
1696 }
Jeff Browne57e8952010-07-23 21:28:06 -07001697
Jeff Brownb51719b2010-07-29 18:18:33 -07001698 mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size());
1699
Jeff Brown38a7fab2010-08-30 03:02:23 -07001700 int32_t touchScreenLeft = mRawAxes.x.minValue;
1701 int32_t touchScreenTop = mRawAxes.y.minValue;
1702 int32_t touchScreenWidth = mRawAxes.x.getRange();
1703 int32_t touchScreenHeight = mRawAxes.y.getRange();
Jeff Brownb51719b2010-07-29 18:18:33 -07001704
1705 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001706 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brownb51719b2010-07-29 18:18:33 -07001707 virtualKeyDefinitions[i];
1708
1709 mLocked.virtualKeys.add();
1710 VirtualKey& virtualKey = mLocked.virtualKeys.editTop();
1711
1712 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1713 int32_t keyCode;
1714 uint32_t flags;
1715 if (getEventHub()->scancodeToKeycode(getDeviceId(), virtualKey.scanCode,
1716 & keyCode, & flags)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001717 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
1718 virtualKey.scanCode);
Jeff Brownb51719b2010-07-29 18:18:33 -07001719 mLocked.virtualKeys.pop(); // drop the key
1720 continue;
Jeff Browne57e8952010-07-23 21:28:06 -07001721 }
1722
Jeff Brownb51719b2010-07-29 18:18:33 -07001723 virtualKey.keyCode = keyCode;
1724 virtualKey.flags = flags;
Jeff Browne57e8952010-07-23 21:28:06 -07001725
Jeff Brownb51719b2010-07-29 18:18:33 -07001726 // convert the key definition's display coordinates into touch coordinates for a hit box
1727 int32_t halfWidth = virtualKeyDefinition.width / 2;
1728 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Browne57e8952010-07-23 21:28:06 -07001729
Jeff Brownb51719b2010-07-29 18:18:33 -07001730 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
1731 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1732 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
1733 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1734 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
1735 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
1736 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
1737 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
Jeff Browne57e8952010-07-23 21:28:06 -07001738
Jeff Brown26c94ff2010-09-30 14:33:04 -07001739 }
1740}
1741
1742void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) {
1743 if (!mLocked.virtualKeys.isEmpty()) {
1744 dump.append(INDENT3 "Virtual Keys:\n");
1745
1746 for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) {
1747 const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i);
1748 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
1749 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1750 i, virtualKey.scanCode, virtualKey.keyCode,
1751 virtualKey.hitLeft, virtualKey.hitRight,
1752 virtualKey.hitTop, virtualKey.hitBottom);
1753 }
Jeff Brownb51719b2010-07-29 18:18:33 -07001754 }
Jeff Browne57e8952010-07-23 21:28:06 -07001755}
1756
Jeff Brown38a7fab2010-08-30 03:02:23 -07001757void TouchInputMapper::parseCalibration() {
1758 const InputDeviceCalibration& in = getDevice()->getCalibration();
1759 Calibration& out = mCalibration;
1760
Jeff Brown60b57762010-10-18 13:32:20 -07001761 // Position
1762 out.haveXOrigin = in.tryGetProperty(String8("touch.position.xOrigin"), out.xOrigin);
1763 out.haveYOrigin = in.tryGetProperty(String8("touch.position.yOrigin"), out.yOrigin);
1764 out.haveXScale = in.tryGetProperty(String8("touch.position.xScale"), out.xScale);
1765 out.haveYScale = in.tryGetProperty(String8("touch.position.yScale"), out.yScale);
1766
Jeff Brown6b337e72010-10-14 21:42:15 -07001767 // Touch Size
1768 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT;
1769 String8 touchSizeCalibrationString;
1770 if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) {
1771 if (touchSizeCalibrationString == "none") {
1772 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
1773 } else if (touchSizeCalibrationString == "geometric") {
1774 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC;
1775 } else if (touchSizeCalibrationString == "pressure") {
1776 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
1777 } else if (touchSizeCalibrationString != "default") {
1778 LOGW("Invalid value for touch.touchSize.calibration: '%s'",
1779 touchSizeCalibrationString.string());
Jeff Brown38a7fab2010-08-30 03:02:23 -07001780 }
1781 }
1782
Jeff Brown6b337e72010-10-14 21:42:15 -07001783 // Tool Size
1784 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT;
1785 String8 toolSizeCalibrationString;
1786 if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) {
1787 if (toolSizeCalibrationString == "none") {
1788 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
1789 } else if (toolSizeCalibrationString == "geometric") {
1790 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC;
1791 } else if (toolSizeCalibrationString == "linear") {
1792 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
1793 } else if (toolSizeCalibrationString == "area") {
1794 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA;
1795 } else if (toolSizeCalibrationString != "default") {
1796 LOGW("Invalid value for touch.toolSize.calibration: '%s'",
1797 toolSizeCalibrationString.string());
Jeff Brown38a7fab2010-08-30 03:02:23 -07001798 }
1799 }
1800
Jeff Brown6b337e72010-10-14 21:42:15 -07001801 out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"),
1802 out.toolSizeLinearScale);
1803 out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"),
1804 out.toolSizeLinearBias);
1805 out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"),
1806 out.toolSizeAreaScale);
1807 out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"),
1808 out.toolSizeAreaBias);
1809 out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"),
1810 out.toolSizeIsSummed);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001811
1812 // Pressure
1813 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
1814 String8 pressureCalibrationString;
Jeff Brown6b337e72010-10-14 21:42:15 -07001815 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001816 if (pressureCalibrationString == "none") {
1817 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
1818 } else if (pressureCalibrationString == "physical") {
1819 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
1820 } else if (pressureCalibrationString == "amplitude") {
1821 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
1822 } else if (pressureCalibrationString != "default") {
Jeff Brown6b337e72010-10-14 21:42:15 -07001823 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown38a7fab2010-08-30 03:02:23 -07001824 pressureCalibrationString.string());
1825 }
1826 }
1827
1828 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
1829 String8 pressureSourceString;
1830 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
1831 if (pressureSourceString == "pressure") {
1832 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
1833 } else if (pressureSourceString == "touch") {
1834 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
1835 } else if (pressureSourceString != "default") {
1836 LOGW("Invalid value for touch.pressure.source: '%s'",
1837 pressureSourceString.string());
1838 }
1839 }
1840
1841 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
1842 out.pressureScale);
1843
1844 // Size
1845 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
1846 String8 sizeCalibrationString;
Jeff Brown6b337e72010-10-14 21:42:15 -07001847 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001848 if (sizeCalibrationString == "none") {
1849 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
1850 } else if (sizeCalibrationString == "normalized") {
1851 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
1852 } else if (sizeCalibrationString != "default") {
Jeff Brown6b337e72010-10-14 21:42:15 -07001853 LOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Brown38a7fab2010-08-30 03:02:23 -07001854 sizeCalibrationString.string());
1855 }
1856 }
1857
1858 // Orientation
1859 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
1860 String8 orientationCalibrationString;
Jeff Brown6b337e72010-10-14 21:42:15 -07001861 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001862 if (orientationCalibrationString == "none") {
1863 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
1864 } else if (orientationCalibrationString == "interpolated") {
1865 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
1866 } else if (orientationCalibrationString != "default") {
Jeff Brown6b337e72010-10-14 21:42:15 -07001867 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown38a7fab2010-08-30 03:02:23 -07001868 orientationCalibrationString.string());
1869 }
1870 }
1871}
1872
1873void TouchInputMapper::resolveCalibration() {
1874 // Pressure
1875 switch (mCalibration.pressureSource) {
1876 case Calibration::PRESSURE_SOURCE_DEFAULT:
1877 if (mRawAxes.pressure.valid) {
1878 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
1879 } else if (mRawAxes.touchMajor.valid) {
1880 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
1881 }
1882 break;
1883
1884 case Calibration::PRESSURE_SOURCE_PRESSURE:
1885 if (! mRawAxes.pressure.valid) {
1886 LOGW("Calibration property touch.pressure.source is 'pressure' but "
1887 "the pressure axis is not available.");
1888 }
1889 break;
1890
1891 case Calibration::PRESSURE_SOURCE_TOUCH:
1892 if (! mRawAxes.touchMajor.valid) {
1893 LOGW("Calibration property touch.pressure.source is 'touch' but "
1894 "the touchMajor axis is not available.");
1895 }
1896 break;
1897
1898 default:
1899 break;
1900 }
1901
1902 switch (mCalibration.pressureCalibration) {
1903 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
1904 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
1905 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
1906 } else {
1907 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
1908 }
1909 break;
1910
1911 default:
1912 break;
1913 }
1914
Jeff Brown6b337e72010-10-14 21:42:15 -07001915 // Tool Size
1916 switch (mCalibration.toolSizeCalibration) {
1917 case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT:
Jeff Brown38a7fab2010-08-30 03:02:23 -07001918 if (mRawAxes.toolMajor.valid) {
Jeff Brown6b337e72010-10-14 21:42:15 -07001919 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001920 } else {
Jeff Brown6b337e72010-10-14 21:42:15 -07001921 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001922 }
1923 break;
1924
1925 default:
1926 break;
1927 }
1928
Jeff Brown6b337e72010-10-14 21:42:15 -07001929 // Touch Size
1930 switch (mCalibration.touchSizeCalibration) {
1931 case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT:
Jeff Brown38a7fab2010-08-30 03:02:23 -07001932 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
Jeff Brown6b337e72010-10-14 21:42:15 -07001933 && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
1934 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001935 } else {
Jeff Brown6b337e72010-10-14 21:42:15 -07001936 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001937 }
1938 break;
1939
1940 default:
1941 break;
1942 }
1943
1944 // Size
1945 switch (mCalibration.sizeCalibration) {
1946 case Calibration::SIZE_CALIBRATION_DEFAULT:
1947 if (mRawAxes.toolMajor.valid) {
1948 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
1949 } else {
1950 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
1951 }
1952 break;
1953
1954 default:
1955 break;
1956 }
1957
1958 // Orientation
1959 switch (mCalibration.orientationCalibration) {
1960 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
1961 if (mRawAxes.orientation.valid) {
1962 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
1963 } else {
1964 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
1965 }
1966 break;
1967
1968 default:
1969 break;
1970 }
1971}
1972
Jeff Brown26c94ff2010-09-30 14:33:04 -07001973void TouchInputMapper::dumpCalibration(String8& dump) {
1974 dump.append(INDENT3 "Calibration:\n");
Jeff Browna665ca82010-09-08 11:49:43 -07001975
Jeff Brown60b57762010-10-18 13:32:20 -07001976 // Position
1977 if (mCalibration.haveXOrigin) {
1978 dump.appendFormat(INDENT4 "touch.position.xOrigin: %d\n", mCalibration.xOrigin);
1979 }
1980 if (mCalibration.haveYOrigin) {
1981 dump.appendFormat(INDENT4 "touch.position.yOrigin: %d\n", mCalibration.yOrigin);
1982 }
1983 if (mCalibration.haveXScale) {
1984 dump.appendFormat(INDENT4 "touch.position.xScale: %0.3f\n", mCalibration.xScale);
1985 }
1986 if (mCalibration.haveYScale) {
1987 dump.appendFormat(INDENT4 "touch.position.yScale: %0.3f\n", mCalibration.yScale);
1988 }
1989
Jeff Brown6b337e72010-10-14 21:42:15 -07001990 // Touch Size
1991 switch (mCalibration.touchSizeCalibration) {
1992 case Calibration::TOUCH_SIZE_CALIBRATION_NONE:
1993 dump.append(INDENT4 "touch.touchSize.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001994 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001995 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
1996 dump.append(INDENT4 "touch.touchSize.calibration: geometric\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001997 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001998 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
1999 dump.append(INDENT4 "touch.touchSize.calibration: pressure\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002000 break;
2001 default:
2002 assert(false);
2003 }
2004
Jeff Brown6b337e72010-10-14 21:42:15 -07002005 // Tool Size
2006 switch (mCalibration.toolSizeCalibration) {
2007 case Calibration::TOOL_SIZE_CALIBRATION_NONE:
2008 dump.append(INDENT4 "touch.toolSize.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002009 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002010 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
2011 dump.append(INDENT4 "touch.toolSize.calibration: geometric\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002012 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002013 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
2014 dump.append(INDENT4 "touch.toolSize.calibration: linear\n");
2015 break;
2016 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2017 dump.append(INDENT4 "touch.toolSize.calibration: area\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002018 break;
2019 default:
2020 assert(false);
2021 }
2022
Jeff Brown6b337e72010-10-14 21:42:15 -07002023 if (mCalibration.haveToolSizeLinearScale) {
2024 dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n",
2025 mCalibration.toolSizeLinearScale);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002026 }
2027
Jeff Brown6b337e72010-10-14 21:42:15 -07002028 if (mCalibration.haveToolSizeLinearBias) {
2029 dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n",
2030 mCalibration.toolSizeLinearBias);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002031 }
2032
Jeff Brown6b337e72010-10-14 21:42:15 -07002033 if (mCalibration.haveToolSizeAreaScale) {
2034 dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n",
2035 mCalibration.toolSizeAreaScale);
2036 }
2037
2038 if (mCalibration.haveToolSizeAreaBias) {
2039 dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n",
2040 mCalibration.toolSizeAreaBias);
2041 }
2042
2043 if (mCalibration.haveToolSizeIsSummed) {
2044 dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %d\n",
2045 mCalibration.toolSizeIsSummed);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002046 }
2047
2048 // Pressure
2049 switch (mCalibration.pressureCalibration) {
2050 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002051 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002052 break;
2053 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002054 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002055 break;
2056 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002057 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002058 break;
2059 default:
2060 assert(false);
2061 }
2062
2063 switch (mCalibration.pressureSource) {
2064 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002065 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002066 break;
2067 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002068 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002069 break;
2070 case Calibration::PRESSURE_SOURCE_DEFAULT:
2071 break;
2072 default:
2073 assert(false);
2074 }
2075
2076 if (mCalibration.havePressureScale) {
Jeff Brown26c94ff2010-09-30 14:33:04 -07002077 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
2078 mCalibration.pressureScale);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002079 }
2080
2081 // Size
2082 switch (mCalibration.sizeCalibration) {
2083 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002084 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002085 break;
2086 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002087 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002088 break;
2089 default:
2090 assert(false);
2091 }
2092
2093 // Orientation
2094 switch (mCalibration.orientationCalibration) {
2095 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002096 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002097 break;
2098 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002099 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002100 break;
2101 default:
2102 assert(false);
2103 }
2104}
2105
Jeff Browne57e8952010-07-23 21:28:06 -07002106void TouchInputMapper::reset() {
2107 // Synthesize touch up event if touch is currently down.
2108 // This will also take care of finishing virtual key processing if needed.
2109 if (mLastTouch.pointerCount != 0) {
2110 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
2111 mCurrentTouch.clear();
2112 syncTouch(when, true);
2113 }
2114
Jeff Brownb51719b2010-07-29 18:18:33 -07002115 { // acquire lock
2116 AutoMutex _l(mLock);
2117 initializeLocked();
2118 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07002119
Jeff Brownb51719b2010-07-29 18:18:33 -07002120 InputMapper::reset();
Jeff Browne57e8952010-07-23 21:28:06 -07002121}
2122
2123void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Browne839a582010-04-22 18:58:52 -07002124 uint32_t policyFlags = 0;
Jeff Browne839a582010-04-22 18:58:52 -07002125
Jeff Brownb51719b2010-07-29 18:18:33 -07002126 // Preprocess pointer data.
Jeff Browne839a582010-04-22 18:58:52 -07002127
Jeff Browne57e8952010-07-23 21:28:06 -07002128 if (mParameters.useBadTouchFilter) {
2129 if (applyBadTouchFilter()) {
Jeff Browne839a582010-04-22 18:58:52 -07002130 havePointerIds = false;
2131 }
2132 }
2133
Jeff Browne57e8952010-07-23 21:28:06 -07002134 if (mParameters.useJumpyTouchFilter) {
2135 if (applyJumpyTouchFilter()) {
Jeff Browne839a582010-04-22 18:58:52 -07002136 havePointerIds = false;
2137 }
2138 }
2139
2140 if (! havePointerIds) {
Jeff Browne57e8952010-07-23 21:28:06 -07002141 calculatePointerIds();
Jeff Browne839a582010-04-22 18:58:52 -07002142 }
2143
Jeff Browne57e8952010-07-23 21:28:06 -07002144 TouchData temp;
2145 TouchData* savedTouch;
2146 if (mParameters.useAveragingTouchFilter) {
2147 temp.copyFrom(mCurrentTouch);
Jeff Browne839a582010-04-22 18:58:52 -07002148 savedTouch = & temp;
2149
Jeff Browne57e8952010-07-23 21:28:06 -07002150 applyAveragingTouchFilter();
Jeff Browne839a582010-04-22 18:58:52 -07002151 } else {
Jeff Browne57e8952010-07-23 21:28:06 -07002152 savedTouch = & mCurrentTouch;
Jeff Browne839a582010-04-22 18:58:52 -07002153 }
2154
Jeff Brownb51719b2010-07-29 18:18:33 -07002155 // Process touches and virtual keys.
Jeff Browne839a582010-04-22 18:58:52 -07002156
Jeff Browne57e8952010-07-23 21:28:06 -07002157 TouchResult touchResult = consumeOffScreenTouches(when, policyFlags);
2158 if (touchResult == DISPATCH_TOUCH) {
2159 dispatchTouches(when, policyFlags);
Jeff Browne839a582010-04-22 18:58:52 -07002160 }
2161
Jeff Brownb51719b2010-07-29 18:18:33 -07002162 // Copy current touch to last touch in preparation for the next cycle.
Jeff Browne839a582010-04-22 18:58:52 -07002163
Jeff Browne57e8952010-07-23 21:28:06 -07002164 if (touchResult == DROP_STROKE) {
2165 mLastTouch.clear();
2166 } else {
2167 mLastTouch.copyFrom(*savedTouch);
Jeff Browne839a582010-04-22 18:58:52 -07002168 }
Jeff Browne839a582010-04-22 18:58:52 -07002169}
2170
Jeff Browne57e8952010-07-23 21:28:06 -07002171TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches(
2172 nsecs_t when, uint32_t policyFlags) {
2173 int32_t keyEventAction, keyEventFlags;
2174 int32_t keyCode, scanCode, downTime;
2175 TouchResult touchResult;
Jeff Brown50de30a2010-06-22 01:27:15 -07002176
Jeff Brownb51719b2010-07-29 18:18:33 -07002177 { // acquire lock
2178 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07002179
Jeff Brownb51719b2010-07-29 18:18:33 -07002180 // Update surface size and orientation, including virtual key positions.
2181 if (! configureSurfaceLocked()) {
2182 return DROP_STROKE;
2183 }
2184
2185 // Check for virtual key press.
2186 if (mLocked.currentVirtualKey.down) {
Jeff Browne57e8952010-07-23 21:28:06 -07002187 if (mCurrentTouch.pointerCount == 0) {
2188 // Pointer went up while virtual key was down.
Jeff Brownb51719b2010-07-29 18:18:33 -07002189 mLocked.currentVirtualKey.down = false;
Jeff Browne57e8952010-07-23 21:28:06 -07002190#if DEBUG_VIRTUAL_KEYS
2191 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
2192 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
2193#endif
2194 keyEventAction = AKEY_EVENT_ACTION_UP;
2195 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2196 touchResult = SKIP_TOUCH;
2197 goto DispatchVirtualKey;
2198 }
2199
2200 if (mCurrentTouch.pointerCount == 1) {
2201 int32_t x = mCurrentTouch.pointers[0].x;
2202 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brownb51719b2010-07-29 18:18:33 -07002203 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
2204 if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07002205 // Pointer is still within the space of the virtual key.
2206 return SKIP_TOUCH;
2207 }
2208 }
2209
2210 // Pointer left virtual key area or another pointer also went down.
2211 // Send key cancellation and drop the stroke so subsequent motions will be
2212 // considered fresh downs. This is useful when the user swipes away from the
2213 // virtual key area into the main display surface.
Jeff Brownb51719b2010-07-29 18:18:33 -07002214 mLocked.currentVirtualKey.down = false;
Jeff Browne57e8952010-07-23 21:28:06 -07002215#if DEBUG_VIRTUAL_KEYS
2216 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
2217 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
2218#endif
2219 keyEventAction = AKEY_EVENT_ACTION_UP;
2220 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
2221 | AKEY_EVENT_FLAG_CANCELED;
2222 touchResult = DROP_STROKE;
2223 goto DispatchVirtualKey;
2224 } else {
2225 if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) {
2226 // Pointer just went down. Handle off-screen touches, if needed.
2227 int32_t x = mCurrentTouch.pointers[0].x;
2228 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brownb51719b2010-07-29 18:18:33 -07002229 if (! isPointInsideSurfaceLocked(x, y)) {
Jeff Browne57e8952010-07-23 21:28:06 -07002230 // If exactly one pointer went down, check for virtual key hit.
2231 // Otherwise we will drop the entire stroke.
2232 if (mCurrentTouch.pointerCount == 1) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002233 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
Jeff Browne57e8952010-07-23 21:28:06 -07002234 if (virtualKey) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002235 mLocked.currentVirtualKey.down = true;
2236 mLocked.currentVirtualKey.downTime = when;
2237 mLocked.currentVirtualKey.keyCode = virtualKey->keyCode;
2238 mLocked.currentVirtualKey.scanCode = virtualKey->scanCode;
Jeff Browne57e8952010-07-23 21:28:06 -07002239#if DEBUG_VIRTUAL_KEYS
2240 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
2241 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
2242#endif
2243 keyEventAction = AKEY_EVENT_ACTION_DOWN;
2244 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM
2245 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2246 touchResult = SKIP_TOUCH;
2247 goto DispatchVirtualKey;
2248 }
2249 }
2250 return DROP_STROKE;
2251 }
2252 }
2253 return DISPATCH_TOUCH;
2254 }
2255
2256 DispatchVirtualKey:
2257 // Collect remaining state needed to dispatch virtual key.
Jeff Brownb51719b2010-07-29 18:18:33 -07002258 keyCode = mLocked.currentVirtualKey.keyCode;
2259 scanCode = mLocked.currentVirtualKey.scanCode;
2260 downTime = mLocked.currentVirtualKey.downTime;
2261 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07002262
2263 // Dispatch virtual key.
2264 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown956c0fb2010-10-01 14:55:30 -07002265 policyFlags |= POLICY_FLAG_VIRTUAL;
Jeff Brown90f0cee2010-10-08 22:31:17 -07002266 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
2267 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
2268 return touchResult;
Jeff Browne839a582010-04-22 18:58:52 -07002269}
2270
Jeff Browne57e8952010-07-23 21:28:06 -07002271void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
2272 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2273 uint32_t lastPointerCount = mLastTouch.pointerCount;
Jeff Browne839a582010-04-22 18:58:52 -07002274 if (currentPointerCount == 0 && lastPointerCount == 0) {
2275 return; // nothing to do!
2276 }
2277
Jeff Browne57e8952010-07-23 21:28:06 -07002278 BitSet32 currentIdBits = mCurrentTouch.idBits;
2279 BitSet32 lastIdBits = mLastTouch.idBits;
Jeff Browne839a582010-04-22 18:58:52 -07002280
2281 if (currentIdBits == lastIdBits) {
2282 // No pointer id changes so this is a move event.
2283 // The dispatcher takes care of batching moves so we don't have to deal with that here.
Jeff Brown5c1ed842010-07-14 18:48:53 -07002284 int32_t motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browne57e8952010-07-23 21:28:06 -07002285 dispatchTouch(when, policyFlags, & mCurrentTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002286 currentIdBits, -1, currentPointerCount, motionEventAction);
Jeff Browne839a582010-04-22 18:58:52 -07002287 } else {
2288 // There may be pointers going up and pointers going down at the same time when pointer
2289 // ids are reported by the device driver.
2290 BitSet32 upIdBits(lastIdBits.value & ~ currentIdBits.value);
2291 BitSet32 downIdBits(currentIdBits.value & ~ lastIdBits.value);
2292 BitSet32 activeIdBits(lastIdBits.value);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002293 uint32_t pointerCount = lastPointerCount;
Jeff Browne839a582010-04-22 18:58:52 -07002294
2295 while (! upIdBits.isEmpty()) {
2296 uint32_t upId = upIdBits.firstMarkedBit();
2297 upIdBits.clearBit(upId);
2298 BitSet32 oldActiveIdBits = activeIdBits;
2299 activeIdBits.clearBit(upId);
2300
2301 int32_t motionEventAction;
2302 if (activeIdBits.isEmpty()) {
Jeff Brown5c1ed842010-07-14 18:48:53 -07002303 motionEventAction = AMOTION_EVENT_ACTION_UP;
Jeff Browne839a582010-04-22 18:58:52 -07002304 } else {
Jeff Brown3cf1c9b2010-07-16 15:01:56 -07002305 motionEventAction = AMOTION_EVENT_ACTION_POINTER_UP;
Jeff Browne839a582010-04-22 18:58:52 -07002306 }
2307
Jeff Browne57e8952010-07-23 21:28:06 -07002308 dispatchTouch(when, policyFlags, & mLastTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002309 oldActiveIdBits, upId, pointerCount, motionEventAction);
2310 pointerCount -= 1;
Jeff Browne839a582010-04-22 18:58:52 -07002311 }
2312
2313 while (! downIdBits.isEmpty()) {
2314 uint32_t downId = downIdBits.firstMarkedBit();
2315 downIdBits.clearBit(downId);
2316 BitSet32 oldActiveIdBits = activeIdBits;
2317 activeIdBits.markBit(downId);
2318
2319 int32_t motionEventAction;
2320 if (oldActiveIdBits.isEmpty()) {
Jeff Brown5c1ed842010-07-14 18:48:53 -07002321 motionEventAction = AMOTION_EVENT_ACTION_DOWN;
Jeff Browne57e8952010-07-23 21:28:06 -07002322 mDownTime = when;
Jeff Browne839a582010-04-22 18:58:52 -07002323 } else {
Jeff Brown3cf1c9b2010-07-16 15:01:56 -07002324 motionEventAction = AMOTION_EVENT_ACTION_POINTER_DOWN;
Jeff Browne839a582010-04-22 18:58:52 -07002325 }
2326
Jeff Brown38a7fab2010-08-30 03:02:23 -07002327 pointerCount += 1;
Jeff Browne57e8952010-07-23 21:28:06 -07002328 dispatchTouch(when, policyFlags, & mCurrentTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002329 activeIdBits, downId, pointerCount, motionEventAction);
Jeff Browne839a582010-04-22 18:58:52 -07002330 }
2331 }
2332}
2333
Jeff Browne57e8952010-07-23 21:28:06 -07002334void TouchInputMapper::dispatchTouch(nsecs_t when, uint32_t policyFlags,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002335 TouchData* touch, BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
Jeff Browne839a582010-04-22 18:58:52 -07002336 int32_t motionEventAction) {
Jeff Browne839a582010-04-22 18:58:52 -07002337 int32_t pointerIds[MAX_POINTERS];
2338 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Browne839a582010-04-22 18:58:52 -07002339 int32_t motionEventEdgeFlags = 0;
Jeff Brownb51719b2010-07-29 18:18:33 -07002340 float xPrecision, yPrecision;
2341
2342 { // acquire lock
2343 AutoMutex _l(mLock);
2344
2345 // Walk through the the active pointers and map touch screen coordinates (TouchData) into
2346 // display coordinates (PointerCoords) and adjust for display orientation.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002347 for (uint32_t outIndex = 0; ! idBits.isEmpty(); outIndex++) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002348 uint32_t id = idBits.firstMarkedBit();
2349 idBits.clearBit(id);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002350 uint32_t inIndex = touch->idToIndex[id];
Jeff Brownb51719b2010-07-29 18:18:33 -07002351
Jeff Brown38a7fab2010-08-30 03:02:23 -07002352 const PointerData& in = touch->pointers[inIndex];
Jeff Brownb51719b2010-07-29 18:18:33 -07002353
Jeff Brown38a7fab2010-08-30 03:02:23 -07002354 // X and Y
2355 float x = float(in.x - mLocked.xOrigin) * mLocked.xScale;
2356 float y = float(in.y - mLocked.yOrigin) * mLocked.yScale;
Jeff Brownb51719b2010-07-29 18:18:33 -07002357
Jeff Brown38a7fab2010-08-30 03:02:23 -07002358 // ToolMajor and ToolMinor
2359 float toolMajor, toolMinor;
Jeff Brown6b337e72010-10-14 21:42:15 -07002360 switch (mCalibration.toolSizeCalibration) {
2361 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002362 toolMajor = in.toolMajor * mLocked.geometricScale;
2363 if (mRawAxes.toolMinor.valid) {
2364 toolMinor = in.toolMinor * mLocked.geometricScale;
2365 } else {
2366 toolMinor = toolMajor;
2367 }
2368 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002369 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002370 toolMajor = in.toolMajor != 0
Jeff Brown6b337e72010-10-14 21:42:15 -07002371 ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias
Jeff Brown38a7fab2010-08-30 03:02:23 -07002372 : 0;
2373 if (mRawAxes.toolMinor.valid) {
2374 toolMinor = in.toolMinor != 0
Jeff Brown6b337e72010-10-14 21:42:15 -07002375 ? in.toolMinor * mLocked.toolSizeLinearScale
2376 + mLocked.toolSizeLinearBias
Jeff Brown38a7fab2010-08-30 03:02:23 -07002377 : 0;
2378 } else {
2379 toolMinor = toolMajor;
2380 }
2381 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002382 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2383 if (in.toolMajor != 0) {
2384 float diameter = sqrtf(in.toolMajor
2385 * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias);
2386 toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias;
2387 } else {
2388 toolMajor = 0;
2389 }
2390 toolMinor = toolMajor;
2391 break;
Jeff Brown38a7fab2010-08-30 03:02:23 -07002392 default:
2393 toolMajor = 0;
2394 toolMinor = 0;
2395 break;
Jeff Brownb51719b2010-07-29 18:18:33 -07002396 }
2397
Jeff Brown6b337e72010-10-14 21:42:15 -07002398 if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002399 toolMajor /= pointerCount;
2400 toolMinor /= pointerCount;
2401 }
2402
2403 // Pressure
2404 float rawPressure;
2405 switch (mCalibration.pressureSource) {
2406 case Calibration::PRESSURE_SOURCE_PRESSURE:
2407 rawPressure = in.pressure;
2408 break;
2409 case Calibration::PRESSURE_SOURCE_TOUCH:
2410 rawPressure = in.touchMajor;
2411 break;
2412 default:
2413 rawPressure = 0;
2414 }
2415
2416 float pressure;
2417 switch (mCalibration.pressureCalibration) {
2418 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
2419 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
2420 pressure = rawPressure * mLocked.pressureScale;
2421 break;
2422 default:
2423 pressure = 1;
2424 break;
2425 }
2426
2427 // TouchMajor and TouchMinor
2428 float touchMajor, touchMinor;
Jeff Brown6b337e72010-10-14 21:42:15 -07002429 switch (mCalibration.touchSizeCalibration) {
2430 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002431 touchMajor = in.touchMajor * mLocked.geometricScale;
2432 if (mRawAxes.touchMinor.valid) {
2433 touchMinor = in.touchMinor * mLocked.geometricScale;
2434 } else {
2435 touchMinor = touchMajor;
2436 }
2437 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002438 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002439 touchMajor = toolMajor * pressure;
2440 touchMinor = toolMinor * pressure;
2441 break;
2442 default:
2443 touchMajor = 0;
2444 touchMinor = 0;
2445 break;
2446 }
2447
2448 if (touchMajor > toolMajor) {
2449 touchMajor = toolMajor;
2450 }
2451 if (touchMinor > toolMinor) {
2452 touchMinor = toolMinor;
2453 }
2454
2455 // Size
2456 float size;
2457 switch (mCalibration.sizeCalibration) {
2458 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
2459 float rawSize = mRawAxes.toolMinor.valid
2460 ? avg(in.toolMajor, in.toolMinor)
2461 : in.toolMajor;
2462 size = rawSize * mLocked.sizeScale;
2463 break;
2464 }
2465 default:
2466 size = 0;
2467 break;
2468 }
2469
2470 // Orientation
2471 float orientation;
2472 switch (mCalibration.orientationCalibration) {
2473 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
2474 orientation = in.orientation * mLocked.orientationScale;
2475 break;
2476 default:
2477 orientation = 0;
2478 }
2479
2480 // Adjust coords for orientation.
Jeff Brownb51719b2010-07-29 18:18:33 -07002481 switch (mLocked.surfaceOrientation) {
2482 case InputReaderPolicyInterface::ROTATION_90: {
2483 float xTemp = x;
2484 x = y;
2485 y = mLocked.surfaceWidth - xTemp;
2486 orientation -= M_PI_2;
2487 if (orientation < - M_PI_2) {
2488 orientation += M_PI;
2489 }
2490 break;
2491 }
2492 case InputReaderPolicyInterface::ROTATION_180: {
2493 x = mLocked.surfaceWidth - x;
2494 y = mLocked.surfaceHeight - y;
2495 orientation = - orientation;
2496 break;
2497 }
2498 case InputReaderPolicyInterface::ROTATION_270: {
2499 float xTemp = x;
2500 x = mLocked.surfaceHeight - y;
2501 y = xTemp;
2502 orientation += M_PI_2;
2503 if (orientation > M_PI_2) {
2504 orientation -= M_PI;
2505 }
2506 break;
2507 }
2508 }
2509
Jeff Brown38a7fab2010-08-30 03:02:23 -07002510 // Write output coords.
2511 PointerCoords& out = pointerCoords[outIndex];
2512 out.x = x;
2513 out.y = y;
2514 out.pressure = pressure;
2515 out.size = size;
2516 out.touchMajor = touchMajor;
2517 out.touchMinor = touchMinor;
2518 out.toolMajor = toolMajor;
2519 out.toolMinor = toolMinor;
2520 out.orientation = orientation;
Jeff Brownb51719b2010-07-29 18:18:33 -07002521
Jeff Brown38a7fab2010-08-30 03:02:23 -07002522 pointerIds[outIndex] = int32_t(id);
Jeff Brownb51719b2010-07-29 18:18:33 -07002523
2524 if (id == changedId) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002525 motionEventAction |= outIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
Jeff Brownb51719b2010-07-29 18:18:33 -07002526 }
Jeff Browne839a582010-04-22 18:58:52 -07002527 }
Jeff Brownb51719b2010-07-29 18:18:33 -07002528
2529 // Check edge flags by looking only at the first pointer since the flags are
2530 // global to the event.
2531 if (motionEventAction == AMOTION_EVENT_ACTION_DOWN) {
2532 if (pointerCoords[0].x <= 0) {
2533 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_LEFT;
2534 } else if (pointerCoords[0].x >= mLocked.orientedSurfaceWidth) {
2535 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_RIGHT;
2536 }
2537 if (pointerCoords[0].y <= 0) {
2538 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_TOP;
2539 } else if (pointerCoords[0].y >= mLocked.orientedSurfaceHeight) {
2540 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_BOTTOM;
2541 }
Jeff Browne839a582010-04-22 18:58:52 -07002542 }
Jeff Brownb51719b2010-07-29 18:18:33 -07002543
2544 xPrecision = mLocked.orientedXPrecision;
2545 yPrecision = mLocked.orientedYPrecision;
2546 } // release lock
Jeff Browne839a582010-04-22 18:58:52 -07002547
Jeff Brown77e26fc2010-10-07 13:44:51 -07002548 getDispatcher()->notifyMotion(when, getDeviceId(), getSources(), policyFlags,
Jeff Brownaf30ff62010-09-01 17:01:00 -07002549 motionEventAction, 0, getContext()->getGlobalMetaState(), motionEventEdgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -07002550 pointerCount, pointerIds, pointerCoords,
Jeff Brownb51719b2010-07-29 18:18:33 -07002551 xPrecision, yPrecision, mDownTime);
Jeff Browne839a582010-04-22 18:58:52 -07002552}
2553
Jeff Brownb51719b2010-07-29 18:18:33 -07002554bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002555 if (mRawAxes.x.valid && mRawAxes.y.valid) {
2556 return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue
2557 && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue;
Jeff Browne839a582010-04-22 18:58:52 -07002558 }
Jeff Browne57e8952010-07-23 21:28:06 -07002559 return true;
2560}
2561
Jeff Brownb51719b2010-07-29 18:18:33 -07002562const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked(
2563 int32_t x, int32_t y) {
2564 size_t numVirtualKeys = mLocked.virtualKeys.size();
2565 for (size_t i = 0; i < numVirtualKeys; i++) {
2566 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07002567
2568#if DEBUG_VIRTUAL_KEYS
2569 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
2570 "left=%d, top=%d, right=%d, bottom=%d",
2571 x, y,
2572 virtualKey.keyCode, virtualKey.scanCode,
2573 virtualKey.hitLeft, virtualKey.hitTop,
2574 virtualKey.hitRight, virtualKey.hitBottom);
2575#endif
2576
2577 if (virtualKey.isHit(x, y)) {
2578 return & virtualKey;
2579 }
2580 }
2581
2582 return NULL;
2583}
2584
2585void TouchInputMapper::calculatePointerIds() {
2586 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2587 uint32_t lastPointerCount = mLastTouch.pointerCount;
2588
2589 if (currentPointerCount == 0) {
2590 // No pointers to assign.
2591 mCurrentTouch.idBits.clear();
2592 } else if (lastPointerCount == 0) {
2593 // All pointers are new.
2594 mCurrentTouch.idBits.clear();
2595 for (uint32_t i = 0; i < currentPointerCount; i++) {
2596 mCurrentTouch.pointers[i].id = i;
2597 mCurrentTouch.idToIndex[i] = i;
2598 mCurrentTouch.idBits.markBit(i);
2599 }
2600 } else if (currentPointerCount == 1 && lastPointerCount == 1) {
2601 // Only one pointer and no change in count so it must have the same id as before.
2602 uint32_t id = mLastTouch.pointers[0].id;
2603 mCurrentTouch.pointers[0].id = id;
2604 mCurrentTouch.idToIndex[id] = 0;
2605 mCurrentTouch.idBits.value = BitSet32::valueForBit(id);
2606 } else {
2607 // General case.
2608 // We build a heap of squared euclidean distances between current and last pointers
2609 // associated with the current and last pointer indices. Then, we find the best
2610 // match (by distance) for each current pointer.
2611 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
2612
2613 uint32_t heapSize = 0;
2614 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
2615 currentPointerIndex++) {
2616 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
2617 lastPointerIndex++) {
2618 int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x
2619 - mLastTouch.pointers[lastPointerIndex].x;
2620 int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y
2621 - mLastTouch.pointers[lastPointerIndex].y;
2622
2623 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
2624
2625 // Insert new element into the heap (sift up).
2626 heap[heapSize].currentPointerIndex = currentPointerIndex;
2627 heap[heapSize].lastPointerIndex = lastPointerIndex;
2628 heap[heapSize].distance = distance;
2629 heapSize += 1;
2630 }
2631 }
2632
2633 // Heapify
2634 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
2635 startIndex -= 1;
2636 for (uint32_t parentIndex = startIndex; ;) {
2637 uint32_t childIndex = parentIndex * 2 + 1;
2638 if (childIndex >= heapSize) {
2639 break;
2640 }
2641
2642 if (childIndex + 1 < heapSize
2643 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2644 childIndex += 1;
2645 }
2646
2647 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2648 break;
2649 }
2650
2651 swap(heap[parentIndex], heap[childIndex]);
2652 parentIndex = childIndex;
2653 }
2654 }
2655
2656#if DEBUG_POINTER_ASSIGNMENT
2657 LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize);
2658 for (size_t i = 0; i < heapSize; i++) {
2659 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2660 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2661 heap[i].distance);
2662 }
2663#endif
2664
2665 // Pull matches out by increasing order of distance.
2666 // To avoid reassigning pointers that have already been matched, the loop keeps track
2667 // of which last and current pointers have been matched using the matchedXXXBits variables.
2668 // It also tracks the used pointer id bits.
2669 BitSet32 matchedLastBits(0);
2670 BitSet32 matchedCurrentBits(0);
2671 BitSet32 usedIdBits(0);
2672 bool first = true;
2673 for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) {
2674 for (;;) {
2675 if (first) {
2676 // The first time through the loop, we just consume the root element of
2677 // the heap (the one with smallest distance).
2678 first = false;
2679 } else {
2680 // Previous iterations consumed the root element of the heap.
2681 // Pop root element off of the heap (sift down).
2682 heapSize -= 1;
2683 assert(heapSize > 0);
2684
2685 // Sift down.
2686 heap[0] = heap[heapSize];
2687 for (uint32_t parentIndex = 0; ;) {
2688 uint32_t childIndex = parentIndex * 2 + 1;
2689 if (childIndex >= heapSize) {
2690 break;
2691 }
2692
2693 if (childIndex + 1 < heapSize
2694 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2695 childIndex += 1;
2696 }
2697
2698 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2699 break;
2700 }
2701
2702 swap(heap[parentIndex], heap[childIndex]);
2703 parentIndex = childIndex;
2704 }
2705
2706#if DEBUG_POINTER_ASSIGNMENT
2707 LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize);
2708 for (size_t i = 0; i < heapSize; i++) {
2709 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2710 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2711 heap[i].distance);
2712 }
2713#endif
2714 }
2715
2716 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
2717 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
2718
2719 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
2720 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
2721
2722 matchedCurrentBits.markBit(currentPointerIndex);
2723 matchedLastBits.markBit(lastPointerIndex);
2724
2725 uint32_t id = mLastTouch.pointers[lastPointerIndex].id;
2726 mCurrentTouch.pointers[currentPointerIndex].id = id;
2727 mCurrentTouch.idToIndex[id] = currentPointerIndex;
2728 usedIdBits.markBit(id);
2729
2730#if DEBUG_POINTER_ASSIGNMENT
2731 LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
2732 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
2733#endif
2734 break;
2735 }
2736 }
2737
2738 // Assign fresh ids to new pointers.
2739 if (currentPointerCount > lastPointerCount) {
2740 for (uint32_t i = currentPointerCount - lastPointerCount; ;) {
2741 uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit();
2742 uint32_t id = usedIdBits.firstUnmarkedBit();
2743
2744 mCurrentTouch.pointers[currentPointerIndex].id = id;
2745 mCurrentTouch.idToIndex[id] = currentPointerIndex;
2746 usedIdBits.markBit(id);
2747
2748#if DEBUG_POINTER_ASSIGNMENT
2749 LOGD("calculatePointerIds - assigned: cur=%d, id=%d",
2750 currentPointerIndex, id);
2751#endif
2752
2753 if (--i == 0) break; // done
2754 matchedCurrentBits.markBit(currentPointerIndex);
2755 }
2756 }
2757
2758 // Fix id bits.
2759 mCurrentTouch.idBits = usedIdBits;
2760 }
2761}
2762
2763/* Special hack for devices that have bad screen data: if one of the
2764 * points has moved more than a screen height from the last position,
2765 * then drop it. */
2766bool TouchInputMapper::applyBadTouchFilter() {
2767 // This hack requires valid axis parameters.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002768 if (! mRawAxes.y.valid) {
Jeff Browne57e8952010-07-23 21:28:06 -07002769 return false;
2770 }
2771
2772 uint32_t pointerCount = mCurrentTouch.pointerCount;
2773
2774 // Nothing to do if there are no points.
2775 if (pointerCount == 0) {
2776 return false;
2777 }
2778
2779 // Don't do anything if a finger is going down or up. We run
2780 // here before assigning pointer IDs, so there isn't a good
2781 // way to do per-finger matching.
2782 if (pointerCount != mLastTouch.pointerCount) {
2783 return false;
2784 }
2785
2786 // We consider a single movement across more than a 7/16 of
2787 // the long size of the screen to be bad. This was a magic value
2788 // determined by looking at the maximum distance it is feasible
2789 // to actually move in one sample.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002790 int32_t maxDeltaY = mRawAxes.y.getRange() * 7 / 16;
Jeff Browne57e8952010-07-23 21:28:06 -07002791
2792 // XXX The original code in InputDevice.java included commented out
2793 // code for testing the X axis. Note that when we drop a point
2794 // we don't actually restore the old X either. Strange.
2795 // The old code also tries to track when bad points were previously
2796 // detected but it turns out that due to the placement of a "break"
2797 // at the end of the loop, we never set mDroppedBadPoint to true
2798 // so it is effectively dead code.
2799 // Need to figure out if the old code is busted or just overcomplicated
2800 // but working as intended.
2801
2802 // Look through all new points and see if any are farther than
2803 // acceptable from all previous points.
2804 for (uint32_t i = pointerCount; i-- > 0; ) {
2805 int32_t y = mCurrentTouch.pointers[i].y;
2806 int32_t closestY = INT_MAX;
2807 int32_t closestDeltaY = 0;
2808
2809#if DEBUG_HACKS
2810 LOGD("BadTouchFilter: Looking at next point #%d: y=%d", i, y);
2811#endif
2812
2813 for (uint32_t j = pointerCount; j-- > 0; ) {
2814 int32_t lastY = mLastTouch.pointers[j].y;
2815 int32_t deltaY = abs(y - lastY);
2816
2817#if DEBUG_HACKS
2818 LOGD("BadTouchFilter: Comparing with last point #%d: y=%d deltaY=%d",
2819 j, lastY, deltaY);
2820#endif
2821
2822 if (deltaY < maxDeltaY) {
2823 goto SkipSufficientlyClosePoint;
2824 }
2825 if (deltaY < closestDeltaY) {
2826 closestDeltaY = deltaY;
2827 closestY = lastY;
2828 }
2829 }
2830
2831 // Must not have found a close enough match.
2832#if DEBUG_HACKS
2833 LOGD("BadTouchFilter: Dropping bad point #%d: newY=%d oldY=%d deltaY=%d maxDeltaY=%d",
2834 i, y, closestY, closestDeltaY, maxDeltaY);
2835#endif
2836
2837 mCurrentTouch.pointers[i].y = closestY;
2838 return true; // XXX original code only corrects one point
2839
2840 SkipSufficientlyClosePoint: ;
2841 }
2842
2843 // No change.
2844 return false;
2845}
2846
2847/* Special hack for devices that have bad screen data: drop points where
2848 * the coordinate value for one axis has jumped to the other pointer's location.
2849 */
2850bool TouchInputMapper::applyJumpyTouchFilter() {
2851 // This hack requires valid axis parameters.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002852 if (! mRawAxes.y.valid) {
Jeff Browne57e8952010-07-23 21:28:06 -07002853 return false;
2854 }
2855
2856 uint32_t pointerCount = mCurrentTouch.pointerCount;
2857 if (mLastTouch.pointerCount != pointerCount) {
2858#if DEBUG_HACKS
2859 LOGD("JumpyTouchFilter: Different pointer count %d -> %d",
2860 mLastTouch.pointerCount, pointerCount);
2861 for (uint32_t i = 0; i < pointerCount; i++) {
2862 LOGD(" Pointer %d (%d, %d)", i,
2863 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
2864 }
2865#endif
2866
2867 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_TRANSITION_DROPS) {
2868 if (mLastTouch.pointerCount == 1 && pointerCount == 2) {
2869 // Just drop the first few events going from 1 to 2 pointers.
2870 // They're bad often enough that they're not worth considering.
2871 mCurrentTouch.pointerCount = 1;
2872 mJumpyTouchFilter.jumpyPointsDropped += 1;
2873
2874#if DEBUG_HACKS
2875 LOGD("JumpyTouchFilter: Pointer 2 dropped");
2876#endif
2877 return true;
2878 } else if (mLastTouch.pointerCount == 2 && pointerCount == 1) {
2879 // The event when we go from 2 -> 1 tends to be messed up too
2880 mCurrentTouch.pointerCount = 2;
2881 mCurrentTouch.pointers[0] = mLastTouch.pointers[0];
2882 mCurrentTouch.pointers[1] = mLastTouch.pointers[1];
2883 mJumpyTouchFilter.jumpyPointsDropped += 1;
2884
2885#if DEBUG_HACKS
2886 for (int32_t i = 0; i < 2; i++) {
2887 LOGD("JumpyTouchFilter: Pointer %d replaced (%d, %d)", i,
2888 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
2889 }
2890#endif
2891 return true;
2892 }
2893 }
2894 // Reset jumpy points dropped on other transitions or if limit exceeded.
2895 mJumpyTouchFilter.jumpyPointsDropped = 0;
2896
2897#if DEBUG_HACKS
2898 LOGD("JumpyTouchFilter: Transition - drop limit reset");
2899#endif
2900 return false;
2901 }
2902
2903 // We have the same number of pointers as last time.
2904 // A 'jumpy' point is one where the coordinate value for one axis
2905 // has jumped to the other pointer's location. No need to do anything
2906 // else if we only have one pointer.
2907 if (pointerCount < 2) {
2908 return false;
2909 }
2910
2911 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_DROP_LIMIT) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002912 int jumpyEpsilon = mRawAxes.y.getRange() / JUMPY_EPSILON_DIVISOR;
Jeff Browne57e8952010-07-23 21:28:06 -07002913
2914 // We only replace the single worst jumpy point as characterized by pointer distance
2915 // in a single axis.
2916 int32_t badPointerIndex = -1;
2917 int32_t badPointerReplacementIndex = -1;
2918 int32_t badPointerDistance = INT_MIN; // distance to be corrected
2919
2920 for (uint32_t i = pointerCount; i-- > 0; ) {
2921 int32_t x = mCurrentTouch.pointers[i].x;
2922 int32_t y = mCurrentTouch.pointers[i].y;
2923
2924#if DEBUG_HACKS
2925 LOGD("JumpyTouchFilter: Point %d (%d, %d)", i, x, y);
2926#endif
2927
2928 // Check if a touch point is too close to another's coordinates
2929 bool dropX = false, dropY = false;
2930 for (uint32_t j = 0; j < pointerCount; j++) {
2931 if (i == j) {
2932 continue;
2933 }
2934
2935 if (abs(x - mCurrentTouch.pointers[j].x) <= jumpyEpsilon) {
2936 dropX = true;
2937 break;
2938 }
2939
2940 if (abs(y - mCurrentTouch.pointers[j].y) <= jumpyEpsilon) {
2941 dropY = true;
2942 break;
2943 }
2944 }
2945 if (! dropX && ! dropY) {
2946 continue; // not jumpy
2947 }
2948
2949 // Find a replacement candidate by comparing with older points on the
2950 // complementary (non-jumpy) axis.
2951 int32_t distance = INT_MIN; // distance to be corrected
2952 int32_t replacementIndex = -1;
2953
2954 if (dropX) {
2955 // X looks too close. Find an older replacement point with a close Y.
2956 int32_t smallestDeltaY = INT_MAX;
2957 for (uint32_t j = 0; j < pointerCount; j++) {
2958 int32_t deltaY = abs(y - mLastTouch.pointers[j].y);
2959 if (deltaY < smallestDeltaY) {
2960 smallestDeltaY = deltaY;
2961 replacementIndex = j;
2962 }
2963 }
2964 distance = abs(x - mLastTouch.pointers[replacementIndex].x);
2965 } else {
2966 // Y looks too close. Find an older replacement point with a close X.
2967 int32_t smallestDeltaX = INT_MAX;
2968 for (uint32_t j = 0; j < pointerCount; j++) {
2969 int32_t deltaX = abs(x - mLastTouch.pointers[j].x);
2970 if (deltaX < smallestDeltaX) {
2971 smallestDeltaX = deltaX;
2972 replacementIndex = j;
2973 }
2974 }
2975 distance = abs(y - mLastTouch.pointers[replacementIndex].y);
2976 }
2977
2978 // If replacing this pointer would correct a worse error than the previous ones
2979 // considered, then use this replacement instead.
2980 if (distance > badPointerDistance) {
2981 badPointerIndex = i;
2982 badPointerReplacementIndex = replacementIndex;
2983 badPointerDistance = distance;
2984 }
2985 }
2986
2987 // Correct the jumpy pointer if one was found.
2988 if (badPointerIndex >= 0) {
2989#if DEBUG_HACKS
2990 LOGD("JumpyTouchFilter: Replacing bad pointer %d with (%d, %d)",
2991 badPointerIndex,
2992 mLastTouch.pointers[badPointerReplacementIndex].x,
2993 mLastTouch.pointers[badPointerReplacementIndex].y);
2994#endif
2995
2996 mCurrentTouch.pointers[badPointerIndex].x =
2997 mLastTouch.pointers[badPointerReplacementIndex].x;
2998 mCurrentTouch.pointers[badPointerIndex].y =
2999 mLastTouch.pointers[badPointerReplacementIndex].y;
3000 mJumpyTouchFilter.jumpyPointsDropped += 1;
3001 return true;
3002 }
3003 }
3004
3005 mJumpyTouchFilter.jumpyPointsDropped = 0;
3006 return false;
3007}
3008
3009/* Special hack for devices that have bad screen data: aggregate and
3010 * compute averages of the coordinate data, to reduce the amount of
3011 * jitter seen by applications. */
3012void TouchInputMapper::applyAveragingTouchFilter() {
3013 for (uint32_t currentIndex = 0; currentIndex < mCurrentTouch.pointerCount; currentIndex++) {
3014 uint32_t id = mCurrentTouch.pointers[currentIndex].id;
3015 int32_t x = mCurrentTouch.pointers[currentIndex].x;
3016 int32_t y = mCurrentTouch.pointers[currentIndex].y;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003017 int32_t pressure;
3018 switch (mCalibration.pressureSource) {
3019 case Calibration::PRESSURE_SOURCE_PRESSURE:
3020 pressure = mCurrentTouch.pointers[currentIndex].pressure;
3021 break;
3022 case Calibration::PRESSURE_SOURCE_TOUCH:
3023 pressure = mCurrentTouch.pointers[currentIndex].touchMajor;
3024 break;
3025 default:
3026 pressure = 1;
3027 break;
3028 }
Jeff Browne57e8952010-07-23 21:28:06 -07003029
3030 if (mLastTouch.idBits.hasBit(id)) {
3031 // Pointer was down before and is still down now.
3032 // Compute average over history trace.
3033 uint32_t start = mAveragingTouchFilter.historyStart[id];
3034 uint32_t end = mAveragingTouchFilter.historyEnd[id];
3035
3036 int64_t deltaX = x - mAveragingTouchFilter.historyData[end].pointers[id].x;
3037 int64_t deltaY = y - mAveragingTouchFilter.historyData[end].pointers[id].y;
3038 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3039
3040#if DEBUG_HACKS
3041 LOGD("AveragingTouchFilter: Pointer id %d - Distance from last sample: %lld",
3042 id, distance);
3043#endif
3044
3045 if (distance < AVERAGING_DISTANCE_LIMIT) {
3046 // Increment end index in preparation for recording new historical data.
3047 end += 1;
3048 if (end > AVERAGING_HISTORY_SIZE) {
3049 end = 0;
3050 }
3051
3052 // If the end index has looped back to the start index then we have filled
3053 // the historical trace up to the desired size so we drop the historical
3054 // data at the start of the trace.
3055 if (end == start) {
3056 start += 1;
3057 if (start > AVERAGING_HISTORY_SIZE) {
3058 start = 0;
3059 }
3060 }
3061
3062 // Add the raw data to the historical trace.
3063 mAveragingTouchFilter.historyStart[id] = start;
3064 mAveragingTouchFilter.historyEnd[id] = end;
3065 mAveragingTouchFilter.historyData[end].pointers[id].x = x;
3066 mAveragingTouchFilter.historyData[end].pointers[id].y = y;
3067 mAveragingTouchFilter.historyData[end].pointers[id].pressure = pressure;
3068
3069 // Average over all historical positions in the trace by total pressure.
3070 int32_t averagedX = 0;
3071 int32_t averagedY = 0;
3072 int32_t totalPressure = 0;
3073 for (;;) {
3074 int32_t historicalX = mAveragingTouchFilter.historyData[start].pointers[id].x;
3075 int32_t historicalY = mAveragingTouchFilter.historyData[start].pointers[id].y;
3076 int32_t historicalPressure = mAveragingTouchFilter.historyData[start]
3077 .pointers[id].pressure;
3078
3079 averagedX += historicalX * historicalPressure;
3080 averagedY += historicalY * historicalPressure;
3081 totalPressure += historicalPressure;
3082
3083 if (start == end) {
3084 break;
3085 }
3086
3087 start += 1;
3088 if (start > AVERAGING_HISTORY_SIZE) {
3089 start = 0;
3090 }
3091 }
3092
Jeff Brown38a7fab2010-08-30 03:02:23 -07003093 if (totalPressure != 0) {
3094 averagedX /= totalPressure;
3095 averagedY /= totalPressure;
Jeff Browne57e8952010-07-23 21:28:06 -07003096
3097#if DEBUG_HACKS
Jeff Brown38a7fab2010-08-30 03:02:23 -07003098 LOGD("AveragingTouchFilter: Pointer id %d - "
3099 "totalPressure=%d, averagedX=%d, averagedY=%d", id, totalPressure,
3100 averagedX, averagedY);
Jeff Browne57e8952010-07-23 21:28:06 -07003101#endif
3102
Jeff Brown38a7fab2010-08-30 03:02:23 -07003103 mCurrentTouch.pointers[currentIndex].x = averagedX;
3104 mCurrentTouch.pointers[currentIndex].y = averagedY;
3105 }
Jeff Browne57e8952010-07-23 21:28:06 -07003106 } else {
3107#if DEBUG_HACKS
3108 LOGD("AveragingTouchFilter: Pointer id %d - Exceeded max distance", id);
3109#endif
3110 }
3111 } else {
3112#if DEBUG_HACKS
3113 LOGD("AveragingTouchFilter: Pointer id %d - Pointer went up", id);
3114#endif
3115 }
3116
3117 // Reset pointer history.
3118 mAveragingTouchFilter.historyStart[id] = 0;
3119 mAveragingTouchFilter.historyEnd[id] = 0;
3120 mAveragingTouchFilter.historyData[0].pointers[id].x = x;
3121 mAveragingTouchFilter.historyData[0].pointers[id].y = y;
3122 mAveragingTouchFilter.historyData[0].pointers[id].pressure = pressure;
3123 }
3124}
3125
3126int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownb51719b2010-07-29 18:18:33 -07003127 { // acquire lock
3128 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07003129
Jeff Brownb51719b2010-07-29 18:18:33 -07003130 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07003131 return AKEY_STATE_VIRTUAL;
3132 }
3133
Jeff Brownb51719b2010-07-29 18:18:33 -07003134 size_t numVirtualKeys = mLocked.virtualKeys.size();
3135 for (size_t i = 0; i < numVirtualKeys; i++) {
3136 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07003137 if (virtualKey.keyCode == keyCode) {
3138 return AKEY_STATE_UP;
3139 }
3140 }
Jeff Brownb51719b2010-07-29 18:18:33 -07003141 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07003142
3143 return AKEY_STATE_UNKNOWN;
3144}
3145
3146int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownb51719b2010-07-29 18:18:33 -07003147 { // acquire lock
3148 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07003149
Jeff Brownb51719b2010-07-29 18:18:33 -07003150 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07003151 return AKEY_STATE_VIRTUAL;
3152 }
3153
Jeff Brownb51719b2010-07-29 18:18:33 -07003154 size_t numVirtualKeys = mLocked.virtualKeys.size();
3155 for (size_t i = 0; i < numVirtualKeys; i++) {
3156 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07003157 if (virtualKey.scanCode == scanCode) {
3158 return AKEY_STATE_UP;
3159 }
3160 }
Jeff Brownb51719b2010-07-29 18:18:33 -07003161 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07003162
3163 return AKEY_STATE_UNKNOWN;
3164}
3165
3166bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
3167 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownb51719b2010-07-29 18:18:33 -07003168 { // acquire lock
3169 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07003170
Jeff Brownb51719b2010-07-29 18:18:33 -07003171 size_t numVirtualKeys = mLocked.virtualKeys.size();
3172 for (size_t i = 0; i < numVirtualKeys; i++) {
3173 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07003174
3175 for (size_t i = 0; i < numCodes; i++) {
3176 if (virtualKey.keyCode == keyCodes[i]) {
3177 outFlags[i] = 1;
3178 }
3179 }
3180 }
Jeff Brownb51719b2010-07-29 18:18:33 -07003181 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07003182
3183 return true;
3184}
3185
3186
3187// --- SingleTouchInputMapper ---
3188
3189SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device, int32_t associatedDisplayId) :
3190 TouchInputMapper(device, associatedDisplayId) {
3191 initialize();
3192}
3193
3194SingleTouchInputMapper::~SingleTouchInputMapper() {
3195}
3196
3197void SingleTouchInputMapper::initialize() {
3198 mAccumulator.clear();
3199
3200 mDown = false;
3201 mX = 0;
3202 mY = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003203 mPressure = 0; // default to 0 for devices that don't report pressure
3204 mToolWidth = 0; // default to 0 for devices that don't report tool width
Jeff Browne57e8952010-07-23 21:28:06 -07003205}
3206
3207void SingleTouchInputMapper::reset() {
3208 TouchInputMapper::reset();
3209
Jeff Browne57e8952010-07-23 21:28:06 -07003210 initialize();
3211 }
3212
3213void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
3214 switch (rawEvent->type) {
3215 case EV_KEY:
3216 switch (rawEvent->scanCode) {
3217 case BTN_TOUCH:
3218 mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH;
3219 mAccumulator.btnTouch = rawEvent->value != 0;
Jeff Brownd64c8552010-08-17 20:38:35 -07003220 // Don't sync immediately. Wait until the next SYN_REPORT since we might
3221 // not have received valid position information yet. This logic assumes that
3222 // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet.
Jeff Browne57e8952010-07-23 21:28:06 -07003223 break;
3224 }
3225 break;
3226
3227 case EV_ABS:
3228 switch (rawEvent->scanCode) {
3229 case ABS_X:
3230 mAccumulator.fields |= Accumulator::FIELD_ABS_X;
3231 mAccumulator.absX = rawEvent->value;
3232 break;
3233 case ABS_Y:
3234 mAccumulator.fields |= Accumulator::FIELD_ABS_Y;
3235 mAccumulator.absY = rawEvent->value;
3236 break;
3237 case ABS_PRESSURE:
3238 mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE;
3239 mAccumulator.absPressure = rawEvent->value;
3240 break;
3241 case ABS_TOOL_WIDTH:
3242 mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH;
3243 mAccumulator.absToolWidth = rawEvent->value;
3244 break;
3245 }
3246 break;
3247
3248 case EV_SYN:
3249 switch (rawEvent->scanCode) {
3250 case SYN_REPORT:
Jeff Brownd64c8552010-08-17 20:38:35 -07003251 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07003252 break;
3253 }
3254 break;
3255 }
3256}
3257
3258void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Browne57e8952010-07-23 21:28:06 -07003259 uint32_t fields = mAccumulator.fields;
Jeff Brownd64c8552010-08-17 20:38:35 -07003260 if (fields == 0) {
3261 return; // no new state changes, so nothing to do
3262 }
Jeff Browne57e8952010-07-23 21:28:06 -07003263
3264 if (fields & Accumulator::FIELD_BTN_TOUCH) {
3265 mDown = mAccumulator.btnTouch;
3266 }
3267
3268 if (fields & Accumulator::FIELD_ABS_X) {
3269 mX = mAccumulator.absX;
3270 }
3271
3272 if (fields & Accumulator::FIELD_ABS_Y) {
3273 mY = mAccumulator.absY;
3274 }
3275
3276 if (fields & Accumulator::FIELD_ABS_PRESSURE) {
3277 mPressure = mAccumulator.absPressure;
3278 }
3279
3280 if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003281 mToolWidth = mAccumulator.absToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -07003282 }
3283
3284 mCurrentTouch.clear();
3285
3286 if (mDown) {
3287 mCurrentTouch.pointerCount = 1;
3288 mCurrentTouch.pointers[0].id = 0;
3289 mCurrentTouch.pointers[0].x = mX;
3290 mCurrentTouch.pointers[0].y = mY;
3291 mCurrentTouch.pointers[0].pressure = mPressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003292 mCurrentTouch.pointers[0].touchMajor = 0;
3293 mCurrentTouch.pointers[0].touchMinor = 0;
3294 mCurrentTouch.pointers[0].toolMajor = mToolWidth;
3295 mCurrentTouch.pointers[0].toolMinor = mToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -07003296 mCurrentTouch.pointers[0].orientation = 0;
3297 mCurrentTouch.idToIndex[0] = 0;
3298 mCurrentTouch.idBits.markBit(0);
3299 }
3300
3301 syncTouch(when, true);
Jeff Brownd64c8552010-08-17 20:38:35 -07003302
3303 mAccumulator.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07003304}
3305
Jeff Brown38a7fab2010-08-30 03:02:23 -07003306void SingleTouchInputMapper::configureRawAxes() {
3307 TouchInputMapper::configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -07003308
Jeff Brown38a7fab2010-08-30 03:02:23 -07003309 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x);
3310 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y);
3311 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure);
3312 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor);
Jeff Browne57e8952010-07-23 21:28:06 -07003313}
3314
3315
3316// --- MultiTouchInputMapper ---
3317
3318MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device, int32_t associatedDisplayId) :
3319 TouchInputMapper(device, associatedDisplayId) {
3320 initialize();
3321}
3322
3323MultiTouchInputMapper::~MultiTouchInputMapper() {
3324}
3325
3326void MultiTouchInputMapper::initialize() {
3327 mAccumulator.clear();
3328}
3329
3330void MultiTouchInputMapper::reset() {
3331 TouchInputMapper::reset();
3332
Jeff Browne57e8952010-07-23 21:28:06 -07003333 initialize();
3334}
3335
3336void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
3337 switch (rawEvent->type) {
3338 case EV_ABS: {
3339 uint32_t pointerIndex = mAccumulator.pointerCount;
3340 Accumulator::Pointer* pointer = & mAccumulator.pointers[pointerIndex];
3341
3342 switch (rawEvent->scanCode) {
3343 case ABS_MT_POSITION_X:
3344 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_X;
3345 pointer->absMTPositionX = rawEvent->value;
3346 break;
3347 case ABS_MT_POSITION_Y:
3348 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y;
3349 pointer->absMTPositionY = rawEvent->value;
3350 break;
3351 case ABS_MT_TOUCH_MAJOR:
3352 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR;
3353 pointer->absMTTouchMajor = rawEvent->value;
3354 break;
3355 case ABS_MT_TOUCH_MINOR:
3356 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR;
3357 pointer->absMTTouchMinor = rawEvent->value;
3358 break;
3359 case ABS_MT_WIDTH_MAJOR:
3360 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR;
3361 pointer->absMTWidthMajor = rawEvent->value;
3362 break;
3363 case ABS_MT_WIDTH_MINOR:
3364 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR;
3365 pointer->absMTWidthMinor = rawEvent->value;
3366 break;
3367 case ABS_MT_ORIENTATION:
3368 pointer->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION;
3369 pointer->absMTOrientation = rawEvent->value;
3370 break;
3371 case ABS_MT_TRACKING_ID:
3372 pointer->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID;
3373 pointer->absMTTrackingId = rawEvent->value;
3374 break;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003375 case ABS_MT_PRESSURE:
3376 pointer->fields |= Accumulator::FIELD_ABS_MT_PRESSURE;
3377 pointer->absMTPressure = rawEvent->value;
3378 break;
Jeff Browne57e8952010-07-23 21:28:06 -07003379 }
3380 break;
3381 }
3382
3383 case EV_SYN:
3384 switch (rawEvent->scanCode) {
3385 case SYN_MT_REPORT: {
3386 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
3387 uint32_t pointerIndex = mAccumulator.pointerCount;
3388
3389 if (mAccumulator.pointers[pointerIndex].fields) {
3390 if (pointerIndex == MAX_POINTERS) {
3391 LOGW("MultiTouch device driver returned more than maximum of %d pointers.",
3392 MAX_POINTERS);
3393 } else {
3394 pointerIndex += 1;
3395 mAccumulator.pointerCount = pointerIndex;
3396 }
3397 }
3398
3399 mAccumulator.pointers[pointerIndex].clear();
3400 break;
3401 }
3402
3403 case SYN_REPORT:
Jeff Brownd64c8552010-08-17 20:38:35 -07003404 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07003405 break;
3406 }
3407 break;
3408 }
3409}
3410
3411void MultiTouchInputMapper::sync(nsecs_t when) {
3412 static const uint32_t REQUIRED_FIELDS =
Jeff Brown38a7fab2010-08-30 03:02:23 -07003413 Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y;
Jeff Browne839a582010-04-22 18:58:52 -07003414
Jeff Browne57e8952010-07-23 21:28:06 -07003415 uint32_t inCount = mAccumulator.pointerCount;
3416 uint32_t outCount = 0;
3417 bool havePointerIds = true;
Jeff Browne839a582010-04-22 18:58:52 -07003418
Jeff Browne57e8952010-07-23 21:28:06 -07003419 mCurrentTouch.clear();
Jeff Browne839a582010-04-22 18:58:52 -07003420
Jeff Browne57e8952010-07-23 21:28:06 -07003421 for (uint32_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003422 const Accumulator::Pointer& inPointer = mAccumulator.pointers[inIndex];
3423 uint32_t fields = inPointer.fields;
Jeff Browne839a582010-04-22 18:58:52 -07003424
Jeff Browne57e8952010-07-23 21:28:06 -07003425 if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003426 // Some drivers send empty MT sync packets without X / Y to indicate a pointer up.
3427 // Drop this finger.
Jeff Browne839a582010-04-22 18:58:52 -07003428 continue;
3429 }
3430
Jeff Brownd64c8552010-08-17 20:38:35 -07003431 PointerData& outPointer = mCurrentTouch.pointers[outCount];
3432 outPointer.x = inPointer.absMTPositionX;
3433 outPointer.y = inPointer.absMTPositionY;
Jeff Browne839a582010-04-22 18:58:52 -07003434
Jeff Brown38a7fab2010-08-30 03:02:23 -07003435 if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) {
3436 if (inPointer.absMTPressure <= 0) {
3437 // Some devices send sync packets with X / Y but with a 0 presure to indicate
Jeff Brownd64c8552010-08-17 20:38:35 -07003438 // a pointer up. Drop this finger.
3439 continue;
3440 }
Jeff Brown38a7fab2010-08-30 03:02:23 -07003441 outPointer.pressure = inPointer.absMTPressure;
3442 } else {
3443 // Default pressure to 0 if absent.
3444 outPointer.pressure = 0;
3445 }
3446
3447 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) {
3448 if (inPointer.absMTTouchMajor <= 0) {
3449 // Some devices send sync packets with X / Y but with a 0 touch major to indicate
3450 // a pointer going up. Drop this finger.
3451 continue;
3452 }
Jeff Brownd64c8552010-08-17 20:38:35 -07003453 outPointer.touchMajor = inPointer.absMTTouchMajor;
3454 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003455 // Default touch area to 0 if absent.
Jeff Brownd64c8552010-08-17 20:38:35 -07003456 outPointer.touchMajor = 0;
3457 }
Jeff Browne839a582010-04-22 18:58:52 -07003458
Jeff Brownd64c8552010-08-17 20:38:35 -07003459 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) {
3460 outPointer.touchMinor = inPointer.absMTTouchMinor;
3461 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003462 // Assume touch area is circular.
Jeff Brownd64c8552010-08-17 20:38:35 -07003463 outPointer.touchMinor = outPointer.touchMajor;
3464 }
Jeff Browne839a582010-04-22 18:58:52 -07003465
Jeff Brownd64c8552010-08-17 20:38:35 -07003466 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) {
3467 outPointer.toolMajor = inPointer.absMTWidthMajor;
3468 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003469 // Default tool area to 0 if absent.
3470 outPointer.toolMajor = 0;
Jeff Brownd64c8552010-08-17 20:38:35 -07003471 }
Jeff Browne839a582010-04-22 18:58:52 -07003472
Jeff Brownd64c8552010-08-17 20:38:35 -07003473 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) {
3474 outPointer.toolMinor = inPointer.absMTWidthMinor;
3475 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003476 // Assume tool area is circular.
Jeff Brownd64c8552010-08-17 20:38:35 -07003477 outPointer.toolMinor = outPointer.toolMajor;
3478 }
3479
3480 if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) {
3481 outPointer.orientation = inPointer.absMTOrientation;
3482 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003483 // Default orientation to vertical if absent.
Jeff Brownd64c8552010-08-17 20:38:35 -07003484 outPointer.orientation = 0;
3485 }
3486
Jeff Brown38a7fab2010-08-30 03:02:23 -07003487 // Assign pointer id using tracking id if available.
Jeff Browne57e8952010-07-23 21:28:06 -07003488 if (havePointerIds) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003489 if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) {
3490 uint32_t id = uint32_t(inPointer.absMTTrackingId);
Jeff Browne839a582010-04-22 18:58:52 -07003491
Jeff Browne57e8952010-07-23 21:28:06 -07003492 if (id > MAX_POINTER_ID) {
3493#if DEBUG_POINTERS
3494 LOGD("Pointers: Ignoring driver provided pointer id %d because "
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07003495 "it is larger than max supported id %d",
Jeff Browne57e8952010-07-23 21:28:06 -07003496 id, MAX_POINTER_ID);
3497#endif
3498 havePointerIds = false;
3499 }
3500 else {
Jeff Brownd64c8552010-08-17 20:38:35 -07003501 outPointer.id = id;
Jeff Browne57e8952010-07-23 21:28:06 -07003502 mCurrentTouch.idToIndex[id] = outCount;
3503 mCurrentTouch.idBits.markBit(id);
3504 }
3505 } else {
3506 havePointerIds = false;
Jeff Browne839a582010-04-22 18:58:52 -07003507 }
3508 }
Jeff Browne839a582010-04-22 18:58:52 -07003509
Jeff Browne57e8952010-07-23 21:28:06 -07003510 outCount += 1;
Jeff Browne839a582010-04-22 18:58:52 -07003511 }
3512
Jeff Browne57e8952010-07-23 21:28:06 -07003513 mCurrentTouch.pointerCount = outCount;
Jeff Browne839a582010-04-22 18:58:52 -07003514
Jeff Browne57e8952010-07-23 21:28:06 -07003515 syncTouch(when, havePointerIds);
Jeff Brownd64c8552010-08-17 20:38:35 -07003516
3517 mAccumulator.clear();
Jeff Browne839a582010-04-22 18:58:52 -07003518}
3519
Jeff Brown38a7fab2010-08-30 03:02:23 -07003520void MultiTouchInputMapper::configureRawAxes() {
3521 TouchInputMapper::configureRawAxes();
Jeff Browne839a582010-04-22 18:58:52 -07003522
Jeff Brown38a7fab2010-08-30 03:02:23 -07003523 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, & mRawAxes.x);
3524 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, & mRawAxes.y);
3525 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, & mRawAxes.touchMajor);
3526 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, & mRawAxes.touchMinor);
3527 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, & mRawAxes.toolMajor);
3528 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, & mRawAxes.toolMinor);
3529 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, & mRawAxes.orientation);
3530 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, & mRawAxes.pressure);
Jeff Brown54bc2812010-06-15 01:31:58 -07003531}
3532
Jeff Browne839a582010-04-22 18:58:52 -07003533
3534} // namespace android