blob: 01ebda919271f725dab382d70704ca106ba4c8d4 [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 Brown6b337e72010-10-14 21:42:15 -07001347 dump.appendFormat(INDENT3 "Translation and Scaling Factors:");
1348 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) {
1484 mLocked.xOrigin = mRawAxes.x.minValue;
1485 mLocked.yOrigin = mRawAxes.y.minValue;
1486 mLocked.xScale = float(width) / mRawAxes.x.getRange();
1487 mLocked.yScale = float(height) / mRawAxes.y.getRange();
Jeff Brownb51719b2010-07-29 18:18:33 -07001488 mLocked.xPrecision = 1.0f / mLocked.xScale;
1489 mLocked.yPrecision = 1.0f / mLocked.yScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001490
Jeff Brownb51719b2010-07-29 18:18:33 -07001491 configureVirtualKeysLocked();
Jeff Browne57e8952010-07-23 21:28:06 -07001492 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001493 LOGW(INDENT "Touch device did not report support for X or Y axis!");
Jeff Brownb51719b2010-07-29 18:18:33 -07001494 mLocked.xOrigin = 0;
1495 mLocked.yOrigin = 0;
1496 mLocked.xScale = 1.0f;
1497 mLocked.yScale = 1.0f;
1498 mLocked.xPrecision = 1.0f;
1499 mLocked.yPrecision = 1.0f;
Jeff Browne57e8952010-07-23 21:28:06 -07001500 }
1501
Jeff Brown38a7fab2010-08-30 03:02:23 -07001502 // Scale factor for terms that are not oriented in a particular axis.
1503 // If the pixels are square then xScale == yScale otherwise we fake it
1504 // by choosing an average.
1505 mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale);
Jeff Browne57e8952010-07-23 21:28:06 -07001506
Jeff Brown38a7fab2010-08-30 03:02:23 -07001507 // Size of diagonal axis.
1508 float diagonalSize = pythag(width, height);
Jeff Browne57e8952010-07-23 21:28:06 -07001509
Jeff Brown38a7fab2010-08-30 03:02:23 -07001510 // TouchMajor and TouchMinor factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001511 if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) {
1512 mLocked.orientedRanges.haveTouchSize = true;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001513 mLocked.orientedRanges.touchMajor.min = 0;
1514 mLocked.orientedRanges.touchMajor.max = diagonalSize;
1515 mLocked.orientedRanges.touchMajor.flat = 0;
1516 mLocked.orientedRanges.touchMajor.fuzz = 0;
1517 mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor;
1518 }
Jeff Brownb51719b2010-07-29 18:18:33 -07001519
Jeff Brown38a7fab2010-08-30 03:02:23 -07001520 // ToolMajor and ToolMinor factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001521 mLocked.toolSizeLinearScale = 0;
1522 mLocked.toolSizeLinearBias = 0;
1523 mLocked.toolSizeAreaScale = 0;
1524 mLocked.toolSizeAreaBias = 0;
1525 if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
1526 if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) {
1527 if (mCalibration.haveToolSizeLinearScale) {
1528 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001529 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
Jeff Brown6b337e72010-10-14 21:42:15 -07001530 mLocked.toolSizeLinearScale = float(min(width, height))
Jeff Brown38a7fab2010-08-30 03:02:23 -07001531 / mRawAxes.toolMajor.maxValue;
1532 }
1533
Jeff Brown6b337e72010-10-14 21:42:15 -07001534 if (mCalibration.haveToolSizeLinearBias) {
1535 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
1536 }
1537 } else if (mCalibration.toolSizeCalibration ==
1538 Calibration::TOOL_SIZE_CALIBRATION_AREA) {
1539 if (mCalibration.haveToolSizeLinearScale) {
1540 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
1541 } else {
1542 mLocked.toolSizeLinearScale = min(width, height);
1543 }
1544
1545 if (mCalibration.haveToolSizeLinearBias) {
1546 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
1547 }
1548
1549 if (mCalibration.haveToolSizeAreaScale) {
1550 mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale;
1551 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1552 mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue;
1553 }
1554
1555 if (mCalibration.haveToolSizeAreaBias) {
1556 mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001557 }
1558 }
1559
Jeff Brown6b337e72010-10-14 21:42:15 -07001560 mLocked.orientedRanges.haveToolSize = true;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001561 mLocked.orientedRanges.toolMajor.min = 0;
1562 mLocked.orientedRanges.toolMajor.max = diagonalSize;
1563 mLocked.orientedRanges.toolMajor.flat = 0;
1564 mLocked.orientedRanges.toolMajor.fuzz = 0;
1565 mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor;
1566 }
1567
1568 // Pressure factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001569 mLocked.pressureScale = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001570 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
1571 RawAbsoluteAxisInfo rawPressureAxis;
1572 switch (mCalibration.pressureSource) {
1573 case Calibration::PRESSURE_SOURCE_PRESSURE:
1574 rawPressureAxis = mRawAxes.pressure;
1575 break;
1576 case Calibration::PRESSURE_SOURCE_TOUCH:
1577 rawPressureAxis = mRawAxes.touchMajor;
1578 break;
1579 default:
1580 rawPressureAxis.clear();
1581 }
1582
Jeff Brown38a7fab2010-08-30 03:02:23 -07001583 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
1584 || mCalibration.pressureCalibration
1585 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
1586 if (mCalibration.havePressureScale) {
1587 mLocked.pressureScale = mCalibration.pressureScale;
1588 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
1589 mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue;
1590 }
1591 }
1592
1593 mLocked.orientedRanges.havePressure = true;
1594 mLocked.orientedRanges.pressure.min = 0;
1595 mLocked.orientedRanges.pressure.max = 1.0;
1596 mLocked.orientedRanges.pressure.flat = 0;
1597 mLocked.orientedRanges.pressure.fuzz = 0;
1598 }
1599
1600 // Size factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001601 mLocked.sizeScale = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001602 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001603 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
1604 if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1605 mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue;
1606 }
1607 }
1608
1609 mLocked.orientedRanges.haveSize = true;
1610 mLocked.orientedRanges.size.min = 0;
1611 mLocked.orientedRanges.size.max = 1.0;
1612 mLocked.orientedRanges.size.flat = 0;
1613 mLocked.orientedRanges.size.fuzz = 0;
1614 }
1615
1616 // Orientation
Jeff Brown6b337e72010-10-14 21:42:15 -07001617 mLocked.orientationScale = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001618 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001619 if (mCalibration.orientationCalibration
1620 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
1621 if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) {
1622 mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue;
1623 }
1624 }
1625
1626 mLocked.orientedRanges.orientation.min = - M_PI_2;
1627 mLocked.orientedRanges.orientation.max = M_PI_2;
1628 mLocked.orientedRanges.orientation.flat = 0;
1629 mLocked.orientedRanges.orientation.fuzz = 0;
1630 }
Jeff Browne57e8952010-07-23 21:28:06 -07001631 }
1632
1633 if (orientationChanged || sizeChanged) {
1634 // Compute oriented surface dimensions, precision, and scales.
1635 float orientedXScale, orientedYScale;
Jeff Brownb51719b2010-07-29 18:18:33 -07001636 switch (mLocked.surfaceOrientation) {
Jeff Browne57e8952010-07-23 21:28:06 -07001637 case InputReaderPolicyInterface::ROTATION_90:
1638 case InputReaderPolicyInterface::ROTATION_270:
Jeff Brownb51719b2010-07-29 18:18:33 -07001639 mLocked.orientedSurfaceWidth = mLocked.surfaceHeight;
1640 mLocked.orientedSurfaceHeight = mLocked.surfaceWidth;
1641 mLocked.orientedXPrecision = mLocked.yPrecision;
1642 mLocked.orientedYPrecision = mLocked.xPrecision;
1643 orientedXScale = mLocked.yScale;
1644 orientedYScale = mLocked.xScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001645 break;
1646 default:
Jeff Brownb51719b2010-07-29 18:18:33 -07001647 mLocked.orientedSurfaceWidth = mLocked.surfaceWidth;
1648 mLocked.orientedSurfaceHeight = mLocked.surfaceHeight;
1649 mLocked.orientedXPrecision = mLocked.xPrecision;
1650 mLocked.orientedYPrecision = mLocked.yPrecision;
1651 orientedXScale = mLocked.xScale;
1652 orientedYScale = mLocked.yScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001653 break;
1654 }
1655
1656 // Configure position ranges.
Jeff Brownb51719b2010-07-29 18:18:33 -07001657 mLocked.orientedRanges.x.min = 0;
1658 mLocked.orientedRanges.x.max = mLocked.orientedSurfaceWidth;
1659 mLocked.orientedRanges.x.flat = 0;
1660 mLocked.orientedRanges.x.fuzz = orientedXScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001661
Jeff Brownb51719b2010-07-29 18:18:33 -07001662 mLocked.orientedRanges.y.min = 0;
1663 mLocked.orientedRanges.y.max = mLocked.orientedSurfaceHeight;
1664 mLocked.orientedRanges.y.flat = 0;
1665 mLocked.orientedRanges.y.fuzz = orientedYScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001666 }
1667
1668 return true;
1669}
1670
Jeff Brown26c94ff2010-09-30 14:33:04 -07001671void TouchInputMapper::dumpSurfaceLocked(String8& dump) {
1672 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth);
1673 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight);
1674 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation);
Jeff Browna665ca82010-09-08 11:49:43 -07001675}
1676
Jeff Brownb51719b2010-07-29 18:18:33 -07001677void TouchInputMapper::configureVirtualKeysLocked() {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001678 assert(mRawAxes.x.valid && mRawAxes.y.valid);
Jeff Browne57e8952010-07-23 21:28:06 -07001679
Jeff Brownb51719b2010-07-29 18:18:33 -07001680 // Note: getVirtualKeyDefinitions is non-reentrant so we can continue holding the lock.
Jeff Brown38a7fab2010-08-30 03:02:23 -07001681 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Browne57e8952010-07-23 21:28:06 -07001682 getPolicy()->getVirtualKeyDefinitions(getDeviceName(), virtualKeyDefinitions);
1683
Jeff Brownb51719b2010-07-29 18:18:33 -07001684 mLocked.virtualKeys.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07001685
Jeff Brownb51719b2010-07-29 18:18:33 -07001686 if (virtualKeyDefinitions.size() == 0) {
1687 return;
1688 }
Jeff Browne57e8952010-07-23 21:28:06 -07001689
Jeff Brownb51719b2010-07-29 18:18:33 -07001690 mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size());
1691
Jeff Brown38a7fab2010-08-30 03:02:23 -07001692 int32_t touchScreenLeft = mRawAxes.x.minValue;
1693 int32_t touchScreenTop = mRawAxes.y.minValue;
1694 int32_t touchScreenWidth = mRawAxes.x.getRange();
1695 int32_t touchScreenHeight = mRawAxes.y.getRange();
Jeff Brownb51719b2010-07-29 18:18:33 -07001696
1697 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001698 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brownb51719b2010-07-29 18:18:33 -07001699 virtualKeyDefinitions[i];
1700
1701 mLocked.virtualKeys.add();
1702 VirtualKey& virtualKey = mLocked.virtualKeys.editTop();
1703
1704 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1705 int32_t keyCode;
1706 uint32_t flags;
1707 if (getEventHub()->scancodeToKeycode(getDeviceId(), virtualKey.scanCode,
1708 & keyCode, & flags)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001709 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
1710 virtualKey.scanCode);
Jeff Brownb51719b2010-07-29 18:18:33 -07001711 mLocked.virtualKeys.pop(); // drop the key
1712 continue;
Jeff Browne57e8952010-07-23 21:28:06 -07001713 }
1714
Jeff Brownb51719b2010-07-29 18:18:33 -07001715 virtualKey.keyCode = keyCode;
1716 virtualKey.flags = flags;
Jeff Browne57e8952010-07-23 21:28:06 -07001717
Jeff Brownb51719b2010-07-29 18:18:33 -07001718 // convert the key definition's display coordinates into touch coordinates for a hit box
1719 int32_t halfWidth = virtualKeyDefinition.width / 2;
1720 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Browne57e8952010-07-23 21:28:06 -07001721
Jeff Brownb51719b2010-07-29 18:18:33 -07001722 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
1723 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1724 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
1725 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1726 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
1727 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
1728 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
1729 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
Jeff Browne57e8952010-07-23 21:28:06 -07001730
Jeff Brown26c94ff2010-09-30 14:33:04 -07001731 }
1732}
1733
1734void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) {
1735 if (!mLocked.virtualKeys.isEmpty()) {
1736 dump.append(INDENT3 "Virtual Keys:\n");
1737
1738 for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) {
1739 const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i);
1740 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
1741 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1742 i, virtualKey.scanCode, virtualKey.keyCode,
1743 virtualKey.hitLeft, virtualKey.hitRight,
1744 virtualKey.hitTop, virtualKey.hitBottom);
1745 }
Jeff Brownb51719b2010-07-29 18:18:33 -07001746 }
Jeff Browne57e8952010-07-23 21:28:06 -07001747}
1748
Jeff Brown38a7fab2010-08-30 03:02:23 -07001749void TouchInputMapper::parseCalibration() {
1750 const InputDeviceCalibration& in = getDevice()->getCalibration();
1751 Calibration& out = mCalibration;
1752
Jeff Brown6b337e72010-10-14 21:42:15 -07001753 // Touch Size
1754 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT;
1755 String8 touchSizeCalibrationString;
1756 if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) {
1757 if (touchSizeCalibrationString == "none") {
1758 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
1759 } else if (touchSizeCalibrationString == "geometric") {
1760 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC;
1761 } else if (touchSizeCalibrationString == "pressure") {
1762 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
1763 } else if (touchSizeCalibrationString != "default") {
1764 LOGW("Invalid value for touch.touchSize.calibration: '%s'",
1765 touchSizeCalibrationString.string());
Jeff Brown38a7fab2010-08-30 03:02:23 -07001766 }
1767 }
1768
Jeff Brown6b337e72010-10-14 21:42:15 -07001769 // Tool Size
1770 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT;
1771 String8 toolSizeCalibrationString;
1772 if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) {
1773 if (toolSizeCalibrationString == "none") {
1774 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
1775 } else if (toolSizeCalibrationString == "geometric") {
1776 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC;
1777 } else if (toolSizeCalibrationString == "linear") {
1778 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
1779 } else if (toolSizeCalibrationString == "area") {
1780 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA;
1781 } else if (toolSizeCalibrationString != "default") {
1782 LOGW("Invalid value for touch.toolSize.calibration: '%s'",
1783 toolSizeCalibrationString.string());
Jeff Brown38a7fab2010-08-30 03:02:23 -07001784 }
1785 }
1786
Jeff Brown6b337e72010-10-14 21:42:15 -07001787 out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"),
1788 out.toolSizeLinearScale);
1789 out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"),
1790 out.toolSizeLinearBias);
1791 out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"),
1792 out.toolSizeAreaScale);
1793 out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"),
1794 out.toolSizeAreaBias);
1795 out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"),
1796 out.toolSizeIsSummed);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001797
1798 // Pressure
1799 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
1800 String8 pressureCalibrationString;
Jeff Brown6b337e72010-10-14 21:42:15 -07001801 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001802 if (pressureCalibrationString == "none") {
1803 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
1804 } else if (pressureCalibrationString == "physical") {
1805 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
1806 } else if (pressureCalibrationString == "amplitude") {
1807 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
1808 } else if (pressureCalibrationString != "default") {
Jeff Brown6b337e72010-10-14 21:42:15 -07001809 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown38a7fab2010-08-30 03:02:23 -07001810 pressureCalibrationString.string());
1811 }
1812 }
1813
1814 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
1815 String8 pressureSourceString;
1816 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
1817 if (pressureSourceString == "pressure") {
1818 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
1819 } else if (pressureSourceString == "touch") {
1820 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
1821 } else if (pressureSourceString != "default") {
1822 LOGW("Invalid value for touch.pressure.source: '%s'",
1823 pressureSourceString.string());
1824 }
1825 }
1826
1827 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
1828 out.pressureScale);
1829
1830 // Size
1831 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
1832 String8 sizeCalibrationString;
Jeff Brown6b337e72010-10-14 21:42:15 -07001833 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001834 if (sizeCalibrationString == "none") {
1835 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
1836 } else if (sizeCalibrationString == "normalized") {
1837 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
1838 } else if (sizeCalibrationString != "default") {
Jeff Brown6b337e72010-10-14 21:42:15 -07001839 LOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Brown38a7fab2010-08-30 03:02:23 -07001840 sizeCalibrationString.string());
1841 }
1842 }
1843
1844 // Orientation
1845 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
1846 String8 orientationCalibrationString;
Jeff Brown6b337e72010-10-14 21:42:15 -07001847 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001848 if (orientationCalibrationString == "none") {
1849 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
1850 } else if (orientationCalibrationString == "interpolated") {
1851 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
1852 } else if (orientationCalibrationString != "default") {
Jeff Brown6b337e72010-10-14 21:42:15 -07001853 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown38a7fab2010-08-30 03:02:23 -07001854 orientationCalibrationString.string());
1855 }
1856 }
1857}
1858
1859void TouchInputMapper::resolveCalibration() {
1860 // Pressure
1861 switch (mCalibration.pressureSource) {
1862 case Calibration::PRESSURE_SOURCE_DEFAULT:
1863 if (mRawAxes.pressure.valid) {
1864 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
1865 } else if (mRawAxes.touchMajor.valid) {
1866 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
1867 }
1868 break;
1869
1870 case Calibration::PRESSURE_SOURCE_PRESSURE:
1871 if (! mRawAxes.pressure.valid) {
1872 LOGW("Calibration property touch.pressure.source is 'pressure' but "
1873 "the pressure axis is not available.");
1874 }
1875 break;
1876
1877 case Calibration::PRESSURE_SOURCE_TOUCH:
1878 if (! mRawAxes.touchMajor.valid) {
1879 LOGW("Calibration property touch.pressure.source is 'touch' but "
1880 "the touchMajor axis is not available.");
1881 }
1882 break;
1883
1884 default:
1885 break;
1886 }
1887
1888 switch (mCalibration.pressureCalibration) {
1889 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
1890 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
1891 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
1892 } else {
1893 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
1894 }
1895 break;
1896
1897 default:
1898 break;
1899 }
1900
Jeff Brown6b337e72010-10-14 21:42:15 -07001901 // Tool Size
1902 switch (mCalibration.toolSizeCalibration) {
1903 case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT:
Jeff Brown38a7fab2010-08-30 03:02:23 -07001904 if (mRawAxes.toolMajor.valid) {
Jeff Brown6b337e72010-10-14 21:42:15 -07001905 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001906 } else {
Jeff Brown6b337e72010-10-14 21:42:15 -07001907 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001908 }
1909 break;
1910
1911 default:
1912 break;
1913 }
1914
Jeff Brown6b337e72010-10-14 21:42:15 -07001915 // Touch Size
1916 switch (mCalibration.touchSizeCalibration) {
1917 case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT:
Jeff Brown38a7fab2010-08-30 03:02:23 -07001918 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
Jeff Brown6b337e72010-10-14 21:42:15 -07001919 && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
1920 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001921 } else {
Jeff Brown6b337e72010-10-14 21:42:15 -07001922 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001923 }
1924 break;
1925
1926 default:
1927 break;
1928 }
1929
1930 // Size
1931 switch (mCalibration.sizeCalibration) {
1932 case Calibration::SIZE_CALIBRATION_DEFAULT:
1933 if (mRawAxes.toolMajor.valid) {
1934 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
1935 } else {
1936 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
1937 }
1938 break;
1939
1940 default:
1941 break;
1942 }
1943
1944 // Orientation
1945 switch (mCalibration.orientationCalibration) {
1946 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
1947 if (mRawAxes.orientation.valid) {
1948 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
1949 } else {
1950 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
1951 }
1952 break;
1953
1954 default:
1955 break;
1956 }
1957}
1958
Jeff Brown26c94ff2010-09-30 14:33:04 -07001959void TouchInputMapper::dumpCalibration(String8& dump) {
1960 dump.append(INDENT3 "Calibration:\n");
Jeff Browna665ca82010-09-08 11:49:43 -07001961
Jeff Brown6b337e72010-10-14 21:42:15 -07001962 // Touch Size
1963 switch (mCalibration.touchSizeCalibration) {
1964 case Calibration::TOUCH_SIZE_CALIBRATION_NONE:
1965 dump.append(INDENT4 "touch.touchSize.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001966 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001967 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
1968 dump.append(INDENT4 "touch.touchSize.calibration: geometric\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001969 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001970 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
1971 dump.append(INDENT4 "touch.touchSize.calibration: pressure\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001972 break;
1973 default:
1974 assert(false);
1975 }
1976
Jeff Brown6b337e72010-10-14 21:42:15 -07001977 // Tool Size
1978 switch (mCalibration.toolSizeCalibration) {
1979 case Calibration::TOOL_SIZE_CALIBRATION_NONE:
1980 dump.append(INDENT4 "touch.toolSize.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001981 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001982 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
1983 dump.append(INDENT4 "touch.toolSize.calibration: geometric\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001984 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001985 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
1986 dump.append(INDENT4 "touch.toolSize.calibration: linear\n");
1987 break;
1988 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
1989 dump.append(INDENT4 "touch.toolSize.calibration: area\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001990 break;
1991 default:
1992 assert(false);
1993 }
1994
Jeff Brown6b337e72010-10-14 21:42:15 -07001995 if (mCalibration.haveToolSizeLinearScale) {
1996 dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n",
1997 mCalibration.toolSizeLinearScale);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001998 }
1999
Jeff Brown6b337e72010-10-14 21:42:15 -07002000 if (mCalibration.haveToolSizeLinearBias) {
2001 dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n",
2002 mCalibration.toolSizeLinearBias);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002003 }
2004
Jeff Brown6b337e72010-10-14 21:42:15 -07002005 if (mCalibration.haveToolSizeAreaScale) {
2006 dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n",
2007 mCalibration.toolSizeAreaScale);
2008 }
2009
2010 if (mCalibration.haveToolSizeAreaBias) {
2011 dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n",
2012 mCalibration.toolSizeAreaBias);
2013 }
2014
2015 if (mCalibration.haveToolSizeIsSummed) {
2016 dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %d\n",
2017 mCalibration.toolSizeIsSummed);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002018 }
2019
2020 // Pressure
2021 switch (mCalibration.pressureCalibration) {
2022 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002023 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002024 break;
2025 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002026 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002027 break;
2028 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002029 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002030 break;
2031 default:
2032 assert(false);
2033 }
2034
2035 switch (mCalibration.pressureSource) {
2036 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002037 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002038 break;
2039 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002040 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002041 break;
2042 case Calibration::PRESSURE_SOURCE_DEFAULT:
2043 break;
2044 default:
2045 assert(false);
2046 }
2047
2048 if (mCalibration.havePressureScale) {
Jeff Brown26c94ff2010-09-30 14:33:04 -07002049 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
2050 mCalibration.pressureScale);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002051 }
2052
2053 // Size
2054 switch (mCalibration.sizeCalibration) {
2055 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002056 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002057 break;
2058 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002059 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002060 break;
2061 default:
2062 assert(false);
2063 }
2064
2065 // Orientation
2066 switch (mCalibration.orientationCalibration) {
2067 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002068 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002069 break;
2070 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002071 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002072 break;
2073 default:
2074 assert(false);
2075 }
2076}
2077
Jeff Browne57e8952010-07-23 21:28:06 -07002078void TouchInputMapper::reset() {
2079 // Synthesize touch up event if touch is currently down.
2080 // This will also take care of finishing virtual key processing if needed.
2081 if (mLastTouch.pointerCount != 0) {
2082 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
2083 mCurrentTouch.clear();
2084 syncTouch(when, true);
2085 }
2086
Jeff Brownb51719b2010-07-29 18:18:33 -07002087 { // acquire lock
2088 AutoMutex _l(mLock);
2089 initializeLocked();
2090 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07002091
Jeff Brownb51719b2010-07-29 18:18:33 -07002092 InputMapper::reset();
Jeff Browne57e8952010-07-23 21:28:06 -07002093}
2094
2095void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Browne839a582010-04-22 18:58:52 -07002096 uint32_t policyFlags = 0;
Jeff Browne839a582010-04-22 18:58:52 -07002097
Jeff Brownb51719b2010-07-29 18:18:33 -07002098 // Preprocess pointer data.
Jeff Browne839a582010-04-22 18:58:52 -07002099
Jeff Browne57e8952010-07-23 21:28:06 -07002100 if (mParameters.useBadTouchFilter) {
2101 if (applyBadTouchFilter()) {
Jeff Browne839a582010-04-22 18:58:52 -07002102 havePointerIds = false;
2103 }
2104 }
2105
Jeff Browne57e8952010-07-23 21:28:06 -07002106 if (mParameters.useJumpyTouchFilter) {
2107 if (applyJumpyTouchFilter()) {
Jeff Browne839a582010-04-22 18:58:52 -07002108 havePointerIds = false;
2109 }
2110 }
2111
2112 if (! havePointerIds) {
Jeff Browne57e8952010-07-23 21:28:06 -07002113 calculatePointerIds();
Jeff Browne839a582010-04-22 18:58:52 -07002114 }
2115
Jeff Browne57e8952010-07-23 21:28:06 -07002116 TouchData temp;
2117 TouchData* savedTouch;
2118 if (mParameters.useAveragingTouchFilter) {
2119 temp.copyFrom(mCurrentTouch);
Jeff Browne839a582010-04-22 18:58:52 -07002120 savedTouch = & temp;
2121
Jeff Browne57e8952010-07-23 21:28:06 -07002122 applyAveragingTouchFilter();
Jeff Browne839a582010-04-22 18:58:52 -07002123 } else {
Jeff Browne57e8952010-07-23 21:28:06 -07002124 savedTouch = & mCurrentTouch;
Jeff Browne839a582010-04-22 18:58:52 -07002125 }
2126
Jeff Brownb51719b2010-07-29 18:18:33 -07002127 // Process touches and virtual keys.
Jeff Browne839a582010-04-22 18:58:52 -07002128
Jeff Browne57e8952010-07-23 21:28:06 -07002129 TouchResult touchResult = consumeOffScreenTouches(when, policyFlags);
2130 if (touchResult == DISPATCH_TOUCH) {
2131 dispatchTouches(when, policyFlags);
Jeff Browne839a582010-04-22 18:58:52 -07002132 }
2133
Jeff Brownb51719b2010-07-29 18:18:33 -07002134 // Copy current touch to last touch in preparation for the next cycle.
Jeff Browne839a582010-04-22 18:58:52 -07002135
Jeff Browne57e8952010-07-23 21:28:06 -07002136 if (touchResult == DROP_STROKE) {
2137 mLastTouch.clear();
2138 } else {
2139 mLastTouch.copyFrom(*savedTouch);
Jeff Browne839a582010-04-22 18:58:52 -07002140 }
Jeff Browne839a582010-04-22 18:58:52 -07002141}
2142
Jeff Browne57e8952010-07-23 21:28:06 -07002143TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches(
2144 nsecs_t when, uint32_t policyFlags) {
2145 int32_t keyEventAction, keyEventFlags;
2146 int32_t keyCode, scanCode, downTime;
2147 TouchResult touchResult;
Jeff Brown50de30a2010-06-22 01:27:15 -07002148
Jeff Brownb51719b2010-07-29 18:18:33 -07002149 { // acquire lock
2150 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07002151
Jeff Brownb51719b2010-07-29 18:18:33 -07002152 // Update surface size and orientation, including virtual key positions.
2153 if (! configureSurfaceLocked()) {
2154 return DROP_STROKE;
2155 }
2156
2157 // Check for virtual key press.
2158 if (mLocked.currentVirtualKey.down) {
Jeff Browne57e8952010-07-23 21:28:06 -07002159 if (mCurrentTouch.pointerCount == 0) {
2160 // Pointer went up while virtual key was down.
Jeff Brownb51719b2010-07-29 18:18:33 -07002161 mLocked.currentVirtualKey.down = false;
Jeff Browne57e8952010-07-23 21:28:06 -07002162#if DEBUG_VIRTUAL_KEYS
2163 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
2164 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
2165#endif
2166 keyEventAction = AKEY_EVENT_ACTION_UP;
2167 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2168 touchResult = SKIP_TOUCH;
2169 goto DispatchVirtualKey;
2170 }
2171
2172 if (mCurrentTouch.pointerCount == 1) {
2173 int32_t x = mCurrentTouch.pointers[0].x;
2174 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brownb51719b2010-07-29 18:18:33 -07002175 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
2176 if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07002177 // Pointer is still within the space of the virtual key.
2178 return SKIP_TOUCH;
2179 }
2180 }
2181
2182 // Pointer left virtual key area or another pointer also went down.
2183 // Send key cancellation and drop the stroke so subsequent motions will be
2184 // considered fresh downs. This is useful when the user swipes away from the
2185 // virtual key area into the main display surface.
Jeff Brownb51719b2010-07-29 18:18:33 -07002186 mLocked.currentVirtualKey.down = false;
Jeff Browne57e8952010-07-23 21:28:06 -07002187#if DEBUG_VIRTUAL_KEYS
2188 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
2189 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
2190#endif
2191 keyEventAction = AKEY_EVENT_ACTION_UP;
2192 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
2193 | AKEY_EVENT_FLAG_CANCELED;
2194 touchResult = DROP_STROKE;
2195 goto DispatchVirtualKey;
2196 } else {
2197 if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) {
2198 // Pointer just went down. Handle off-screen touches, if needed.
2199 int32_t x = mCurrentTouch.pointers[0].x;
2200 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brownb51719b2010-07-29 18:18:33 -07002201 if (! isPointInsideSurfaceLocked(x, y)) {
Jeff Browne57e8952010-07-23 21:28:06 -07002202 // If exactly one pointer went down, check for virtual key hit.
2203 // Otherwise we will drop the entire stroke.
2204 if (mCurrentTouch.pointerCount == 1) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002205 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
Jeff Browne57e8952010-07-23 21:28:06 -07002206 if (virtualKey) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002207 mLocked.currentVirtualKey.down = true;
2208 mLocked.currentVirtualKey.downTime = when;
2209 mLocked.currentVirtualKey.keyCode = virtualKey->keyCode;
2210 mLocked.currentVirtualKey.scanCode = virtualKey->scanCode;
Jeff Browne57e8952010-07-23 21:28:06 -07002211#if DEBUG_VIRTUAL_KEYS
2212 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
2213 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
2214#endif
2215 keyEventAction = AKEY_EVENT_ACTION_DOWN;
2216 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM
2217 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2218 touchResult = SKIP_TOUCH;
2219 goto DispatchVirtualKey;
2220 }
2221 }
2222 return DROP_STROKE;
2223 }
2224 }
2225 return DISPATCH_TOUCH;
2226 }
2227
2228 DispatchVirtualKey:
2229 // Collect remaining state needed to dispatch virtual key.
Jeff Brownb51719b2010-07-29 18:18:33 -07002230 keyCode = mLocked.currentVirtualKey.keyCode;
2231 scanCode = mLocked.currentVirtualKey.scanCode;
2232 downTime = mLocked.currentVirtualKey.downTime;
2233 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07002234
2235 // Dispatch virtual key.
2236 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown956c0fb2010-10-01 14:55:30 -07002237 policyFlags |= POLICY_FLAG_VIRTUAL;
Jeff Brown90f0cee2010-10-08 22:31:17 -07002238 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
2239 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
2240 return touchResult;
Jeff Browne839a582010-04-22 18:58:52 -07002241}
2242
Jeff Browne57e8952010-07-23 21:28:06 -07002243void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
2244 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2245 uint32_t lastPointerCount = mLastTouch.pointerCount;
Jeff Browne839a582010-04-22 18:58:52 -07002246 if (currentPointerCount == 0 && lastPointerCount == 0) {
2247 return; // nothing to do!
2248 }
2249
Jeff Browne57e8952010-07-23 21:28:06 -07002250 BitSet32 currentIdBits = mCurrentTouch.idBits;
2251 BitSet32 lastIdBits = mLastTouch.idBits;
Jeff Browne839a582010-04-22 18:58:52 -07002252
2253 if (currentIdBits == lastIdBits) {
2254 // No pointer id changes so this is a move event.
2255 // The dispatcher takes care of batching moves so we don't have to deal with that here.
Jeff Brown5c1ed842010-07-14 18:48:53 -07002256 int32_t motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browne57e8952010-07-23 21:28:06 -07002257 dispatchTouch(when, policyFlags, & mCurrentTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002258 currentIdBits, -1, currentPointerCount, motionEventAction);
Jeff Browne839a582010-04-22 18:58:52 -07002259 } else {
2260 // There may be pointers going up and pointers going down at the same time when pointer
2261 // ids are reported by the device driver.
2262 BitSet32 upIdBits(lastIdBits.value & ~ currentIdBits.value);
2263 BitSet32 downIdBits(currentIdBits.value & ~ lastIdBits.value);
2264 BitSet32 activeIdBits(lastIdBits.value);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002265 uint32_t pointerCount = lastPointerCount;
Jeff Browne839a582010-04-22 18:58:52 -07002266
2267 while (! upIdBits.isEmpty()) {
2268 uint32_t upId = upIdBits.firstMarkedBit();
2269 upIdBits.clearBit(upId);
2270 BitSet32 oldActiveIdBits = activeIdBits;
2271 activeIdBits.clearBit(upId);
2272
2273 int32_t motionEventAction;
2274 if (activeIdBits.isEmpty()) {
Jeff Brown5c1ed842010-07-14 18:48:53 -07002275 motionEventAction = AMOTION_EVENT_ACTION_UP;
Jeff Browne839a582010-04-22 18:58:52 -07002276 } else {
Jeff Brown3cf1c9b2010-07-16 15:01:56 -07002277 motionEventAction = AMOTION_EVENT_ACTION_POINTER_UP;
Jeff Browne839a582010-04-22 18:58:52 -07002278 }
2279
Jeff Browne57e8952010-07-23 21:28:06 -07002280 dispatchTouch(when, policyFlags, & mLastTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002281 oldActiveIdBits, upId, pointerCount, motionEventAction);
2282 pointerCount -= 1;
Jeff Browne839a582010-04-22 18:58:52 -07002283 }
2284
2285 while (! downIdBits.isEmpty()) {
2286 uint32_t downId = downIdBits.firstMarkedBit();
2287 downIdBits.clearBit(downId);
2288 BitSet32 oldActiveIdBits = activeIdBits;
2289 activeIdBits.markBit(downId);
2290
2291 int32_t motionEventAction;
2292 if (oldActiveIdBits.isEmpty()) {
Jeff Brown5c1ed842010-07-14 18:48:53 -07002293 motionEventAction = AMOTION_EVENT_ACTION_DOWN;
Jeff Browne57e8952010-07-23 21:28:06 -07002294 mDownTime = when;
Jeff Browne839a582010-04-22 18:58:52 -07002295 } else {
Jeff Brown3cf1c9b2010-07-16 15:01:56 -07002296 motionEventAction = AMOTION_EVENT_ACTION_POINTER_DOWN;
Jeff Browne839a582010-04-22 18:58:52 -07002297 }
2298
Jeff Brown38a7fab2010-08-30 03:02:23 -07002299 pointerCount += 1;
Jeff Browne57e8952010-07-23 21:28:06 -07002300 dispatchTouch(when, policyFlags, & mCurrentTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002301 activeIdBits, downId, pointerCount, motionEventAction);
Jeff Browne839a582010-04-22 18:58:52 -07002302 }
2303 }
2304}
2305
Jeff Browne57e8952010-07-23 21:28:06 -07002306void TouchInputMapper::dispatchTouch(nsecs_t when, uint32_t policyFlags,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002307 TouchData* touch, BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
Jeff Browne839a582010-04-22 18:58:52 -07002308 int32_t motionEventAction) {
Jeff Browne839a582010-04-22 18:58:52 -07002309 int32_t pointerIds[MAX_POINTERS];
2310 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Browne839a582010-04-22 18:58:52 -07002311 int32_t motionEventEdgeFlags = 0;
Jeff Brownb51719b2010-07-29 18:18:33 -07002312 float xPrecision, yPrecision;
2313
2314 { // acquire lock
2315 AutoMutex _l(mLock);
2316
2317 // Walk through the the active pointers and map touch screen coordinates (TouchData) into
2318 // display coordinates (PointerCoords) and adjust for display orientation.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002319 for (uint32_t outIndex = 0; ! idBits.isEmpty(); outIndex++) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002320 uint32_t id = idBits.firstMarkedBit();
2321 idBits.clearBit(id);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002322 uint32_t inIndex = touch->idToIndex[id];
Jeff Brownb51719b2010-07-29 18:18:33 -07002323
Jeff Brown38a7fab2010-08-30 03:02:23 -07002324 const PointerData& in = touch->pointers[inIndex];
Jeff Brownb51719b2010-07-29 18:18:33 -07002325
Jeff Brown38a7fab2010-08-30 03:02:23 -07002326 // X and Y
2327 float x = float(in.x - mLocked.xOrigin) * mLocked.xScale;
2328 float y = float(in.y - mLocked.yOrigin) * mLocked.yScale;
Jeff Brownb51719b2010-07-29 18:18:33 -07002329
Jeff Brown38a7fab2010-08-30 03:02:23 -07002330 // ToolMajor and ToolMinor
2331 float toolMajor, toolMinor;
Jeff Brown6b337e72010-10-14 21:42:15 -07002332 switch (mCalibration.toolSizeCalibration) {
2333 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002334 toolMajor = in.toolMajor * mLocked.geometricScale;
2335 if (mRawAxes.toolMinor.valid) {
2336 toolMinor = in.toolMinor * mLocked.geometricScale;
2337 } else {
2338 toolMinor = toolMajor;
2339 }
2340 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002341 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002342 toolMajor = in.toolMajor != 0
Jeff Brown6b337e72010-10-14 21:42:15 -07002343 ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias
Jeff Brown38a7fab2010-08-30 03:02:23 -07002344 : 0;
2345 if (mRawAxes.toolMinor.valid) {
2346 toolMinor = in.toolMinor != 0
Jeff Brown6b337e72010-10-14 21:42:15 -07002347 ? in.toolMinor * mLocked.toolSizeLinearScale
2348 + mLocked.toolSizeLinearBias
Jeff Brown38a7fab2010-08-30 03:02:23 -07002349 : 0;
2350 } else {
2351 toolMinor = toolMajor;
2352 }
2353 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002354 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2355 if (in.toolMajor != 0) {
2356 float diameter = sqrtf(in.toolMajor
2357 * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias);
2358 toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias;
2359 } else {
2360 toolMajor = 0;
2361 }
2362 toolMinor = toolMajor;
2363 break;
Jeff Brown38a7fab2010-08-30 03:02:23 -07002364 default:
2365 toolMajor = 0;
2366 toolMinor = 0;
2367 break;
Jeff Brownb51719b2010-07-29 18:18:33 -07002368 }
2369
Jeff Brown6b337e72010-10-14 21:42:15 -07002370 if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002371 toolMajor /= pointerCount;
2372 toolMinor /= pointerCount;
2373 }
2374
2375 // Pressure
2376 float rawPressure;
2377 switch (mCalibration.pressureSource) {
2378 case Calibration::PRESSURE_SOURCE_PRESSURE:
2379 rawPressure = in.pressure;
2380 break;
2381 case Calibration::PRESSURE_SOURCE_TOUCH:
2382 rawPressure = in.touchMajor;
2383 break;
2384 default:
2385 rawPressure = 0;
2386 }
2387
2388 float pressure;
2389 switch (mCalibration.pressureCalibration) {
2390 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
2391 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
2392 pressure = rawPressure * mLocked.pressureScale;
2393 break;
2394 default:
2395 pressure = 1;
2396 break;
2397 }
2398
2399 // TouchMajor and TouchMinor
2400 float touchMajor, touchMinor;
Jeff Brown6b337e72010-10-14 21:42:15 -07002401 switch (mCalibration.touchSizeCalibration) {
2402 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002403 touchMajor = in.touchMajor * mLocked.geometricScale;
2404 if (mRawAxes.touchMinor.valid) {
2405 touchMinor = in.touchMinor * mLocked.geometricScale;
2406 } else {
2407 touchMinor = touchMajor;
2408 }
2409 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002410 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002411 touchMajor = toolMajor * pressure;
2412 touchMinor = toolMinor * pressure;
2413 break;
2414 default:
2415 touchMajor = 0;
2416 touchMinor = 0;
2417 break;
2418 }
2419
2420 if (touchMajor > toolMajor) {
2421 touchMajor = toolMajor;
2422 }
2423 if (touchMinor > toolMinor) {
2424 touchMinor = toolMinor;
2425 }
2426
2427 // Size
2428 float size;
2429 switch (mCalibration.sizeCalibration) {
2430 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
2431 float rawSize = mRawAxes.toolMinor.valid
2432 ? avg(in.toolMajor, in.toolMinor)
2433 : in.toolMajor;
2434 size = rawSize * mLocked.sizeScale;
2435 break;
2436 }
2437 default:
2438 size = 0;
2439 break;
2440 }
2441
2442 // Orientation
2443 float orientation;
2444 switch (mCalibration.orientationCalibration) {
2445 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
2446 orientation = in.orientation * mLocked.orientationScale;
2447 break;
2448 default:
2449 orientation = 0;
2450 }
2451
2452 // Adjust coords for orientation.
Jeff Brownb51719b2010-07-29 18:18:33 -07002453 switch (mLocked.surfaceOrientation) {
2454 case InputReaderPolicyInterface::ROTATION_90: {
2455 float xTemp = x;
2456 x = y;
2457 y = mLocked.surfaceWidth - xTemp;
2458 orientation -= M_PI_2;
2459 if (orientation < - M_PI_2) {
2460 orientation += M_PI;
2461 }
2462 break;
2463 }
2464 case InputReaderPolicyInterface::ROTATION_180: {
2465 x = mLocked.surfaceWidth - x;
2466 y = mLocked.surfaceHeight - y;
2467 orientation = - orientation;
2468 break;
2469 }
2470 case InputReaderPolicyInterface::ROTATION_270: {
2471 float xTemp = x;
2472 x = mLocked.surfaceHeight - y;
2473 y = xTemp;
2474 orientation += M_PI_2;
2475 if (orientation > M_PI_2) {
2476 orientation -= M_PI;
2477 }
2478 break;
2479 }
2480 }
2481
Jeff Brown38a7fab2010-08-30 03:02:23 -07002482 // Write output coords.
2483 PointerCoords& out = pointerCoords[outIndex];
2484 out.x = x;
2485 out.y = y;
2486 out.pressure = pressure;
2487 out.size = size;
2488 out.touchMajor = touchMajor;
2489 out.touchMinor = touchMinor;
2490 out.toolMajor = toolMajor;
2491 out.toolMinor = toolMinor;
2492 out.orientation = orientation;
Jeff Brownb51719b2010-07-29 18:18:33 -07002493
Jeff Brown38a7fab2010-08-30 03:02:23 -07002494 pointerIds[outIndex] = int32_t(id);
Jeff Brownb51719b2010-07-29 18:18:33 -07002495
2496 if (id == changedId) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002497 motionEventAction |= outIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
Jeff Brownb51719b2010-07-29 18:18:33 -07002498 }
Jeff Browne839a582010-04-22 18:58:52 -07002499 }
Jeff Brownb51719b2010-07-29 18:18:33 -07002500
2501 // Check edge flags by looking only at the first pointer since the flags are
2502 // global to the event.
2503 if (motionEventAction == AMOTION_EVENT_ACTION_DOWN) {
2504 if (pointerCoords[0].x <= 0) {
2505 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_LEFT;
2506 } else if (pointerCoords[0].x >= mLocked.orientedSurfaceWidth) {
2507 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_RIGHT;
2508 }
2509 if (pointerCoords[0].y <= 0) {
2510 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_TOP;
2511 } else if (pointerCoords[0].y >= mLocked.orientedSurfaceHeight) {
2512 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_BOTTOM;
2513 }
Jeff Browne839a582010-04-22 18:58:52 -07002514 }
Jeff Brownb51719b2010-07-29 18:18:33 -07002515
2516 xPrecision = mLocked.orientedXPrecision;
2517 yPrecision = mLocked.orientedYPrecision;
2518 } // release lock
Jeff Browne839a582010-04-22 18:58:52 -07002519
Jeff Brown77e26fc2010-10-07 13:44:51 -07002520 getDispatcher()->notifyMotion(when, getDeviceId(), getSources(), policyFlags,
Jeff Brownaf30ff62010-09-01 17:01:00 -07002521 motionEventAction, 0, getContext()->getGlobalMetaState(), motionEventEdgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -07002522 pointerCount, pointerIds, pointerCoords,
Jeff Brownb51719b2010-07-29 18:18:33 -07002523 xPrecision, yPrecision, mDownTime);
Jeff Browne839a582010-04-22 18:58:52 -07002524}
2525
Jeff Brownb51719b2010-07-29 18:18:33 -07002526bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002527 if (mRawAxes.x.valid && mRawAxes.y.valid) {
2528 return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue
2529 && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue;
Jeff Browne839a582010-04-22 18:58:52 -07002530 }
Jeff Browne57e8952010-07-23 21:28:06 -07002531 return true;
2532}
2533
Jeff Brownb51719b2010-07-29 18:18:33 -07002534const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked(
2535 int32_t x, int32_t y) {
2536 size_t numVirtualKeys = mLocked.virtualKeys.size();
2537 for (size_t i = 0; i < numVirtualKeys; i++) {
2538 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07002539
2540#if DEBUG_VIRTUAL_KEYS
2541 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
2542 "left=%d, top=%d, right=%d, bottom=%d",
2543 x, y,
2544 virtualKey.keyCode, virtualKey.scanCode,
2545 virtualKey.hitLeft, virtualKey.hitTop,
2546 virtualKey.hitRight, virtualKey.hitBottom);
2547#endif
2548
2549 if (virtualKey.isHit(x, y)) {
2550 return & virtualKey;
2551 }
2552 }
2553
2554 return NULL;
2555}
2556
2557void TouchInputMapper::calculatePointerIds() {
2558 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2559 uint32_t lastPointerCount = mLastTouch.pointerCount;
2560
2561 if (currentPointerCount == 0) {
2562 // No pointers to assign.
2563 mCurrentTouch.idBits.clear();
2564 } else if (lastPointerCount == 0) {
2565 // All pointers are new.
2566 mCurrentTouch.idBits.clear();
2567 for (uint32_t i = 0; i < currentPointerCount; i++) {
2568 mCurrentTouch.pointers[i].id = i;
2569 mCurrentTouch.idToIndex[i] = i;
2570 mCurrentTouch.idBits.markBit(i);
2571 }
2572 } else if (currentPointerCount == 1 && lastPointerCount == 1) {
2573 // Only one pointer and no change in count so it must have the same id as before.
2574 uint32_t id = mLastTouch.pointers[0].id;
2575 mCurrentTouch.pointers[0].id = id;
2576 mCurrentTouch.idToIndex[id] = 0;
2577 mCurrentTouch.idBits.value = BitSet32::valueForBit(id);
2578 } else {
2579 // General case.
2580 // We build a heap of squared euclidean distances between current and last pointers
2581 // associated with the current and last pointer indices. Then, we find the best
2582 // match (by distance) for each current pointer.
2583 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
2584
2585 uint32_t heapSize = 0;
2586 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
2587 currentPointerIndex++) {
2588 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
2589 lastPointerIndex++) {
2590 int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x
2591 - mLastTouch.pointers[lastPointerIndex].x;
2592 int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y
2593 - mLastTouch.pointers[lastPointerIndex].y;
2594
2595 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
2596
2597 // Insert new element into the heap (sift up).
2598 heap[heapSize].currentPointerIndex = currentPointerIndex;
2599 heap[heapSize].lastPointerIndex = lastPointerIndex;
2600 heap[heapSize].distance = distance;
2601 heapSize += 1;
2602 }
2603 }
2604
2605 // Heapify
2606 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
2607 startIndex -= 1;
2608 for (uint32_t parentIndex = startIndex; ;) {
2609 uint32_t childIndex = parentIndex * 2 + 1;
2610 if (childIndex >= heapSize) {
2611 break;
2612 }
2613
2614 if (childIndex + 1 < heapSize
2615 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2616 childIndex += 1;
2617 }
2618
2619 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2620 break;
2621 }
2622
2623 swap(heap[parentIndex], heap[childIndex]);
2624 parentIndex = childIndex;
2625 }
2626 }
2627
2628#if DEBUG_POINTER_ASSIGNMENT
2629 LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize);
2630 for (size_t i = 0; i < heapSize; i++) {
2631 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2632 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2633 heap[i].distance);
2634 }
2635#endif
2636
2637 // Pull matches out by increasing order of distance.
2638 // To avoid reassigning pointers that have already been matched, the loop keeps track
2639 // of which last and current pointers have been matched using the matchedXXXBits variables.
2640 // It also tracks the used pointer id bits.
2641 BitSet32 matchedLastBits(0);
2642 BitSet32 matchedCurrentBits(0);
2643 BitSet32 usedIdBits(0);
2644 bool first = true;
2645 for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) {
2646 for (;;) {
2647 if (first) {
2648 // The first time through the loop, we just consume the root element of
2649 // the heap (the one with smallest distance).
2650 first = false;
2651 } else {
2652 // Previous iterations consumed the root element of the heap.
2653 // Pop root element off of the heap (sift down).
2654 heapSize -= 1;
2655 assert(heapSize > 0);
2656
2657 // Sift down.
2658 heap[0] = heap[heapSize];
2659 for (uint32_t parentIndex = 0; ;) {
2660 uint32_t childIndex = parentIndex * 2 + 1;
2661 if (childIndex >= heapSize) {
2662 break;
2663 }
2664
2665 if (childIndex + 1 < heapSize
2666 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2667 childIndex += 1;
2668 }
2669
2670 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2671 break;
2672 }
2673
2674 swap(heap[parentIndex], heap[childIndex]);
2675 parentIndex = childIndex;
2676 }
2677
2678#if DEBUG_POINTER_ASSIGNMENT
2679 LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize);
2680 for (size_t i = 0; i < heapSize; i++) {
2681 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2682 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2683 heap[i].distance);
2684 }
2685#endif
2686 }
2687
2688 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
2689 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
2690
2691 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
2692 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
2693
2694 matchedCurrentBits.markBit(currentPointerIndex);
2695 matchedLastBits.markBit(lastPointerIndex);
2696
2697 uint32_t id = mLastTouch.pointers[lastPointerIndex].id;
2698 mCurrentTouch.pointers[currentPointerIndex].id = id;
2699 mCurrentTouch.idToIndex[id] = currentPointerIndex;
2700 usedIdBits.markBit(id);
2701
2702#if DEBUG_POINTER_ASSIGNMENT
2703 LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
2704 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
2705#endif
2706 break;
2707 }
2708 }
2709
2710 // Assign fresh ids to new pointers.
2711 if (currentPointerCount > lastPointerCount) {
2712 for (uint32_t i = currentPointerCount - lastPointerCount; ;) {
2713 uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit();
2714 uint32_t id = usedIdBits.firstUnmarkedBit();
2715
2716 mCurrentTouch.pointers[currentPointerIndex].id = id;
2717 mCurrentTouch.idToIndex[id] = currentPointerIndex;
2718 usedIdBits.markBit(id);
2719
2720#if DEBUG_POINTER_ASSIGNMENT
2721 LOGD("calculatePointerIds - assigned: cur=%d, id=%d",
2722 currentPointerIndex, id);
2723#endif
2724
2725 if (--i == 0) break; // done
2726 matchedCurrentBits.markBit(currentPointerIndex);
2727 }
2728 }
2729
2730 // Fix id bits.
2731 mCurrentTouch.idBits = usedIdBits;
2732 }
2733}
2734
2735/* Special hack for devices that have bad screen data: if one of the
2736 * points has moved more than a screen height from the last position,
2737 * then drop it. */
2738bool TouchInputMapper::applyBadTouchFilter() {
2739 // This hack requires valid axis parameters.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002740 if (! mRawAxes.y.valid) {
Jeff Browne57e8952010-07-23 21:28:06 -07002741 return false;
2742 }
2743
2744 uint32_t pointerCount = mCurrentTouch.pointerCount;
2745
2746 // Nothing to do if there are no points.
2747 if (pointerCount == 0) {
2748 return false;
2749 }
2750
2751 // Don't do anything if a finger is going down or up. We run
2752 // here before assigning pointer IDs, so there isn't a good
2753 // way to do per-finger matching.
2754 if (pointerCount != mLastTouch.pointerCount) {
2755 return false;
2756 }
2757
2758 // We consider a single movement across more than a 7/16 of
2759 // the long size of the screen to be bad. This was a magic value
2760 // determined by looking at the maximum distance it is feasible
2761 // to actually move in one sample.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002762 int32_t maxDeltaY = mRawAxes.y.getRange() * 7 / 16;
Jeff Browne57e8952010-07-23 21:28:06 -07002763
2764 // XXX The original code in InputDevice.java included commented out
2765 // code for testing the X axis. Note that when we drop a point
2766 // we don't actually restore the old X either. Strange.
2767 // The old code also tries to track when bad points were previously
2768 // detected but it turns out that due to the placement of a "break"
2769 // at the end of the loop, we never set mDroppedBadPoint to true
2770 // so it is effectively dead code.
2771 // Need to figure out if the old code is busted or just overcomplicated
2772 // but working as intended.
2773
2774 // Look through all new points and see if any are farther than
2775 // acceptable from all previous points.
2776 for (uint32_t i = pointerCount; i-- > 0; ) {
2777 int32_t y = mCurrentTouch.pointers[i].y;
2778 int32_t closestY = INT_MAX;
2779 int32_t closestDeltaY = 0;
2780
2781#if DEBUG_HACKS
2782 LOGD("BadTouchFilter: Looking at next point #%d: y=%d", i, y);
2783#endif
2784
2785 for (uint32_t j = pointerCount; j-- > 0; ) {
2786 int32_t lastY = mLastTouch.pointers[j].y;
2787 int32_t deltaY = abs(y - lastY);
2788
2789#if DEBUG_HACKS
2790 LOGD("BadTouchFilter: Comparing with last point #%d: y=%d deltaY=%d",
2791 j, lastY, deltaY);
2792#endif
2793
2794 if (deltaY < maxDeltaY) {
2795 goto SkipSufficientlyClosePoint;
2796 }
2797 if (deltaY < closestDeltaY) {
2798 closestDeltaY = deltaY;
2799 closestY = lastY;
2800 }
2801 }
2802
2803 // Must not have found a close enough match.
2804#if DEBUG_HACKS
2805 LOGD("BadTouchFilter: Dropping bad point #%d: newY=%d oldY=%d deltaY=%d maxDeltaY=%d",
2806 i, y, closestY, closestDeltaY, maxDeltaY);
2807#endif
2808
2809 mCurrentTouch.pointers[i].y = closestY;
2810 return true; // XXX original code only corrects one point
2811
2812 SkipSufficientlyClosePoint: ;
2813 }
2814
2815 // No change.
2816 return false;
2817}
2818
2819/* Special hack for devices that have bad screen data: drop points where
2820 * the coordinate value for one axis has jumped to the other pointer's location.
2821 */
2822bool TouchInputMapper::applyJumpyTouchFilter() {
2823 // This hack requires valid axis parameters.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002824 if (! mRawAxes.y.valid) {
Jeff Browne57e8952010-07-23 21:28:06 -07002825 return false;
2826 }
2827
2828 uint32_t pointerCount = mCurrentTouch.pointerCount;
2829 if (mLastTouch.pointerCount != pointerCount) {
2830#if DEBUG_HACKS
2831 LOGD("JumpyTouchFilter: Different pointer count %d -> %d",
2832 mLastTouch.pointerCount, pointerCount);
2833 for (uint32_t i = 0; i < pointerCount; i++) {
2834 LOGD(" Pointer %d (%d, %d)", i,
2835 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
2836 }
2837#endif
2838
2839 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_TRANSITION_DROPS) {
2840 if (mLastTouch.pointerCount == 1 && pointerCount == 2) {
2841 // Just drop the first few events going from 1 to 2 pointers.
2842 // They're bad often enough that they're not worth considering.
2843 mCurrentTouch.pointerCount = 1;
2844 mJumpyTouchFilter.jumpyPointsDropped += 1;
2845
2846#if DEBUG_HACKS
2847 LOGD("JumpyTouchFilter: Pointer 2 dropped");
2848#endif
2849 return true;
2850 } else if (mLastTouch.pointerCount == 2 && pointerCount == 1) {
2851 // The event when we go from 2 -> 1 tends to be messed up too
2852 mCurrentTouch.pointerCount = 2;
2853 mCurrentTouch.pointers[0] = mLastTouch.pointers[0];
2854 mCurrentTouch.pointers[1] = mLastTouch.pointers[1];
2855 mJumpyTouchFilter.jumpyPointsDropped += 1;
2856
2857#if DEBUG_HACKS
2858 for (int32_t i = 0; i < 2; i++) {
2859 LOGD("JumpyTouchFilter: Pointer %d replaced (%d, %d)", i,
2860 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
2861 }
2862#endif
2863 return true;
2864 }
2865 }
2866 // Reset jumpy points dropped on other transitions or if limit exceeded.
2867 mJumpyTouchFilter.jumpyPointsDropped = 0;
2868
2869#if DEBUG_HACKS
2870 LOGD("JumpyTouchFilter: Transition - drop limit reset");
2871#endif
2872 return false;
2873 }
2874
2875 // We have the same number of pointers as last time.
2876 // A 'jumpy' point is one where the coordinate value for one axis
2877 // has jumped to the other pointer's location. No need to do anything
2878 // else if we only have one pointer.
2879 if (pointerCount < 2) {
2880 return false;
2881 }
2882
2883 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_DROP_LIMIT) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002884 int jumpyEpsilon = mRawAxes.y.getRange() / JUMPY_EPSILON_DIVISOR;
Jeff Browne57e8952010-07-23 21:28:06 -07002885
2886 // We only replace the single worst jumpy point as characterized by pointer distance
2887 // in a single axis.
2888 int32_t badPointerIndex = -1;
2889 int32_t badPointerReplacementIndex = -1;
2890 int32_t badPointerDistance = INT_MIN; // distance to be corrected
2891
2892 for (uint32_t i = pointerCount; i-- > 0; ) {
2893 int32_t x = mCurrentTouch.pointers[i].x;
2894 int32_t y = mCurrentTouch.pointers[i].y;
2895
2896#if DEBUG_HACKS
2897 LOGD("JumpyTouchFilter: Point %d (%d, %d)", i, x, y);
2898#endif
2899
2900 // Check if a touch point is too close to another's coordinates
2901 bool dropX = false, dropY = false;
2902 for (uint32_t j = 0; j < pointerCount; j++) {
2903 if (i == j) {
2904 continue;
2905 }
2906
2907 if (abs(x - mCurrentTouch.pointers[j].x) <= jumpyEpsilon) {
2908 dropX = true;
2909 break;
2910 }
2911
2912 if (abs(y - mCurrentTouch.pointers[j].y) <= jumpyEpsilon) {
2913 dropY = true;
2914 break;
2915 }
2916 }
2917 if (! dropX && ! dropY) {
2918 continue; // not jumpy
2919 }
2920
2921 // Find a replacement candidate by comparing with older points on the
2922 // complementary (non-jumpy) axis.
2923 int32_t distance = INT_MIN; // distance to be corrected
2924 int32_t replacementIndex = -1;
2925
2926 if (dropX) {
2927 // X looks too close. Find an older replacement point with a close Y.
2928 int32_t smallestDeltaY = INT_MAX;
2929 for (uint32_t j = 0; j < pointerCount; j++) {
2930 int32_t deltaY = abs(y - mLastTouch.pointers[j].y);
2931 if (deltaY < smallestDeltaY) {
2932 smallestDeltaY = deltaY;
2933 replacementIndex = j;
2934 }
2935 }
2936 distance = abs(x - mLastTouch.pointers[replacementIndex].x);
2937 } else {
2938 // Y looks too close. Find an older replacement point with a close X.
2939 int32_t smallestDeltaX = INT_MAX;
2940 for (uint32_t j = 0; j < pointerCount; j++) {
2941 int32_t deltaX = abs(x - mLastTouch.pointers[j].x);
2942 if (deltaX < smallestDeltaX) {
2943 smallestDeltaX = deltaX;
2944 replacementIndex = j;
2945 }
2946 }
2947 distance = abs(y - mLastTouch.pointers[replacementIndex].y);
2948 }
2949
2950 // If replacing this pointer would correct a worse error than the previous ones
2951 // considered, then use this replacement instead.
2952 if (distance > badPointerDistance) {
2953 badPointerIndex = i;
2954 badPointerReplacementIndex = replacementIndex;
2955 badPointerDistance = distance;
2956 }
2957 }
2958
2959 // Correct the jumpy pointer if one was found.
2960 if (badPointerIndex >= 0) {
2961#if DEBUG_HACKS
2962 LOGD("JumpyTouchFilter: Replacing bad pointer %d with (%d, %d)",
2963 badPointerIndex,
2964 mLastTouch.pointers[badPointerReplacementIndex].x,
2965 mLastTouch.pointers[badPointerReplacementIndex].y);
2966#endif
2967
2968 mCurrentTouch.pointers[badPointerIndex].x =
2969 mLastTouch.pointers[badPointerReplacementIndex].x;
2970 mCurrentTouch.pointers[badPointerIndex].y =
2971 mLastTouch.pointers[badPointerReplacementIndex].y;
2972 mJumpyTouchFilter.jumpyPointsDropped += 1;
2973 return true;
2974 }
2975 }
2976
2977 mJumpyTouchFilter.jumpyPointsDropped = 0;
2978 return false;
2979}
2980
2981/* Special hack for devices that have bad screen data: aggregate and
2982 * compute averages of the coordinate data, to reduce the amount of
2983 * jitter seen by applications. */
2984void TouchInputMapper::applyAveragingTouchFilter() {
2985 for (uint32_t currentIndex = 0; currentIndex < mCurrentTouch.pointerCount; currentIndex++) {
2986 uint32_t id = mCurrentTouch.pointers[currentIndex].id;
2987 int32_t x = mCurrentTouch.pointers[currentIndex].x;
2988 int32_t y = mCurrentTouch.pointers[currentIndex].y;
Jeff Brown38a7fab2010-08-30 03:02:23 -07002989 int32_t pressure;
2990 switch (mCalibration.pressureSource) {
2991 case Calibration::PRESSURE_SOURCE_PRESSURE:
2992 pressure = mCurrentTouch.pointers[currentIndex].pressure;
2993 break;
2994 case Calibration::PRESSURE_SOURCE_TOUCH:
2995 pressure = mCurrentTouch.pointers[currentIndex].touchMajor;
2996 break;
2997 default:
2998 pressure = 1;
2999 break;
3000 }
Jeff Browne57e8952010-07-23 21:28:06 -07003001
3002 if (mLastTouch.idBits.hasBit(id)) {
3003 // Pointer was down before and is still down now.
3004 // Compute average over history trace.
3005 uint32_t start = mAveragingTouchFilter.historyStart[id];
3006 uint32_t end = mAveragingTouchFilter.historyEnd[id];
3007
3008 int64_t deltaX = x - mAveragingTouchFilter.historyData[end].pointers[id].x;
3009 int64_t deltaY = y - mAveragingTouchFilter.historyData[end].pointers[id].y;
3010 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3011
3012#if DEBUG_HACKS
3013 LOGD("AveragingTouchFilter: Pointer id %d - Distance from last sample: %lld",
3014 id, distance);
3015#endif
3016
3017 if (distance < AVERAGING_DISTANCE_LIMIT) {
3018 // Increment end index in preparation for recording new historical data.
3019 end += 1;
3020 if (end > AVERAGING_HISTORY_SIZE) {
3021 end = 0;
3022 }
3023
3024 // If the end index has looped back to the start index then we have filled
3025 // the historical trace up to the desired size so we drop the historical
3026 // data at the start of the trace.
3027 if (end == start) {
3028 start += 1;
3029 if (start > AVERAGING_HISTORY_SIZE) {
3030 start = 0;
3031 }
3032 }
3033
3034 // Add the raw data to the historical trace.
3035 mAveragingTouchFilter.historyStart[id] = start;
3036 mAveragingTouchFilter.historyEnd[id] = end;
3037 mAveragingTouchFilter.historyData[end].pointers[id].x = x;
3038 mAveragingTouchFilter.historyData[end].pointers[id].y = y;
3039 mAveragingTouchFilter.historyData[end].pointers[id].pressure = pressure;
3040
3041 // Average over all historical positions in the trace by total pressure.
3042 int32_t averagedX = 0;
3043 int32_t averagedY = 0;
3044 int32_t totalPressure = 0;
3045 for (;;) {
3046 int32_t historicalX = mAveragingTouchFilter.historyData[start].pointers[id].x;
3047 int32_t historicalY = mAveragingTouchFilter.historyData[start].pointers[id].y;
3048 int32_t historicalPressure = mAveragingTouchFilter.historyData[start]
3049 .pointers[id].pressure;
3050
3051 averagedX += historicalX * historicalPressure;
3052 averagedY += historicalY * historicalPressure;
3053 totalPressure += historicalPressure;
3054
3055 if (start == end) {
3056 break;
3057 }
3058
3059 start += 1;
3060 if (start > AVERAGING_HISTORY_SIZE) {
3061 start = 0;
3062 }
3063 }
3064
Jeff Brown38a7fab2010-08-30 03:02:23 -07003065 if (totalPressure != 0) {
3066 averagedX /= totalPressure;
3067 averagedY /= totalPressure;
Jeff Browne57e8952010-07-23 21:28:06 -07003068
3069#if DEBUG_HACKS
Jeff Brown38a7fab2010-08-30 03:02:23 -07003070 LOGD("AveragingTouchFilter: Pointer id %d - "
3071 "totalPressure=%d, averagedX=%d, averagedY=%d", id, totalPressure,
3072 averagedX, averagedY);
Jeff Browne57e8952010-07-23 21:28:06 -07003073#endif
3074
Jeff Brown38a7fab2010-08-30 03:02:23 -07003075 mCurrentTouch.pointers[currentIndex].x = averagedX;
3076 mCurrentTouch.pointers[currentIndex].y = averagedY;
3077 }
Jeff Browne57e8952010-07-23 21:28:06 -07003078 } else {
3079#if DEBUG_HACKS
3080 LOGD("AveragingTouchFilter: Pointer id %d - Exceeded max distance", id);
3081#endif
3082 }
3083 } else {
3084#if DEBUG_HACKS
3085 LOGD("AveragingTouchFilter: Pointer id %d - Pointer went up", id);
3086#endif
3087 }
3088
3089 // Reset pointer history.
3090 mAveragingTouchFilter.historyStart[id] = 0;
3091 mAveragingTouchFilter.historyEnd[id] = 0;
3092 mAveragingTouchFilter.historyData[0].pointers[id].x = x;
3093 mAveragingTouchFilter.historyData[0].pointers[id].y = y;
3094 mAveragingTouchFilter.historyData[0].pointers[id].pressure = pressure;
3095 }
3096}
3097
3098int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownb51719b2010-07-29 18:18:33 -07003099 { // acquire lock
3100 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07003101
Jeff Brownb51719b2010-07-29 18:18:33 -07003102 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07003103 return AKEY_STATE_VIRTUAL;
3104 }
3105
Jeff Brownb51719b2010-07-29 18:18:33 -07003106 size_t numVirtualKeys = mLocked.virtualKeys.size();
3107 for (size_t i = 0; i < numVirtualKeys; i++) {
3108 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07003109 if (virtualKey.keyCode == keyCode) {
3110 return AKEY_STATE_UP;
3111 }
3112 }
Jeff Brownb51719b2010-07-29 18:18:33 -07003113 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07003114
3115 return AKEY_STATE_UNKNOWN;
3116}
3117
3118int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownb51719b2010-07-29 18:18:33 -07003119 { // acquire lock
3120 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07003121
Jeff Brownb51719b2010-07-29 18:18:33 -07003122 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07003123 return AKEY_STATE_VIRTUAL;
3124 }
3125
Jeff Brownb51719b2010-07-29 18:18:33 -07003126 size_t numVirtualKeys = mLocked.virtualKeys.size();
3127 for (size_t i = 0; i < numVirtualKeys; i++) {
3128 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07003129 if (virtualKey.scanCode == scanCode) {
3130 return AKEY_STATE_UP;
3131 }
3132 }
Jeff Brownb51719b2010-07-29 18:18:33 -07003133 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07003134
3135 return AKEY_STATE_UNKNOWN;
3136}
3137
3138bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
3139 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownb51719b2010-07-29 18:18:33 -07003140 { // acquire lock
3141 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07003142
Jeff Brownb51719b2010-07-29 18:18:33 -07003143 size_t numVirtualKeys = mLocked.virtualKeys.size();
3144 for (size_t i = 0; i < numVirtualKeys; i++) {
3145 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07003146
3147 for (size_t i = 0; i < numCodes; i++) {
3148 if (virtualKey.keyCode == keyCodes[i]) {
3149 outFlags[i] = 1;
3150 }
3151 }
3152 }
Jeff Brownb51719b2010-07-29 18:18:33 -07003153 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07003154
3155 return true;
3156}
3157
3158
3159// --- SingleTouchInputMapper ---
3160
3161SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device, int32_t associatedDisplayId) :
3162 TouchInputMapper(device, associatedDisplayId) {
3163 initialize();
3164}
3165
3166SingleTouchInputMapper::~SingleTouchInputMapper() {
3167}
3168
3169void SingleTouchInputMapper::initialize() {
3170 mAccumulator.clear();
3171
3172 mDown = false;
3173 mX = 0;
3174 mY = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003175 mPressure = 0; // default to 0 for devices that don't report pressure
3176 mToolWidth = 0; // default to 0 for devices that don't report tool width
Jeff Browne57e8952010-07-23 21:28:06 -07003177}
3178
3179void SingleTouchInputMapper::reset() {
3180 TouchInputMapper::reset();
3181
Jeff Browne57e8952010-07-23 21:28:06 -07003182 initialize();
3183 }
3184
3185void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
3186 switch (rawEvent->type) {
3187 case EV_KEY:
3188 switch (rawEvent->scanCode) {
3189 case BTN_TOUCH:
3190 mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH;
3191 mAccumulator.btnTouch = rawEvent->value != 0;
Jeff Brownd64c8552010-08-17 20:38:35 -07003192 // Don't sync immediately. Wait until the next SYN_REPORT since we might
3193 // not have received valid position information yet. This logic assumes that
3194 // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet.
Jeff Browne57e8952010-07-23 21:28:06 -07003195 break;
3196 }
3197 break;
3198
3199 case EV_ABS:
3200 switch (rawEvent->scanCode) {
3201 case ABS_X:
3202 mAccumulator.fields |= Accumulator::FIELD_ABS_X;
3203 mAccumulator.absX = rawEvent->value;
3204 break;
3205 case ABS_Y:
3206 mAccumulator.fields |= Accumulator::FIELD_ABS_Y;
3207 mAccumulator.absY = rawEvent->value;
3208 break;
3209 case ABS_PRESSURE:
3210 mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE;
3211 mAccumulator.absPressure = rawEvent->value;
3212 break;
3213 case ABS_TOOL_WIDTH:
3214 mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH;
3215 mAccumulator.absToolWidth = rawEvent->value;
3216 break;
3217 }
3218 break;
3219
3220 case EV_SYN:
3221 switch (rawEvent->scanCode) {
3222 case SYN_REPORT:
Jeff Brownd64c8552010-08-17 20:38:35 -07003223 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07003224 break;
3225 }
3226 break;
3227 }
3228}
3229
3230void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Browne57e8952010-07-23 21:28:06 -07003231 uint32_t fields = mAccumulator.fields;
Jeff Brownd64c8552010-08-17 20:38:35 -07003232 if (fields == 0) {
3233 return; // no new state changes, so nothing to do
3234 }
Jeff Browne57e8952010-07-23 21:28:06 -07003235
3236 if (fields & Accumulator::FIELD_BTN_TOUCH) {
3237 mDown = mAccumulator.btnTouch;
3238 }
3239
3240 if (fields & Accumulator::FIELD_ABS_X) {
3241 mX = mAccumulator.absX;
3242 }
3243
3244 if (fields & Accumulator::FIELD_ABS_Y) {
3245 mY = mAccumulator.absY;
3246 }
3247
3248 if (fields & Accumulator::FIELD_ABS_PRESSURE) {
3249 mPressure = mAccumulator.absPressure;
3250 }
3251
3252 if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003253 mToolWidth = mAccumulator.absToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -07003254 }
3255
3256 mCurrentTouch.clear();
3257
3258 if (mDown) {
3259 mCurrentTouch.pointerCount = 1;
3260 mCurrentTouch.pointers[0].id = 0;
3261 mCurrentTouch.pointers[0].x = mX;
3262 mCurrentTouch.pointers[0].y = mY;
3263 mCurrentTouch.pointers[0].pressure = mPressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003264 mCurrentTouch.pointers[0].touchMajor = 0;
3265 mCurrentTouch.pointers[0].touchMinor = 0;
3266 mCurrentTouch.pointers[0].toolMajor = mToolWidth;
3267 mCurrentTouch.pointers[0].toolMinor = mToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -07003268 mCurrentTouch.pointers[0].orientation = 0;
3269 mCurrentTouch.idToIndex[0] = 0;
3270 mCurrentTouch.idBits.markBit(0);
3271 }
3272
3273 syncTouch(when, true);
Jeff Brownd64c8552010-08-17 20:38:35 -07003274
3275 mAccumulator.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07003276}
3277
Jeff Brown38a7fab2010-08-30 03:02:23 -07003278void SingleTouchInputMapper::configureRawAxes() {
3279 TouchInputMapper::configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -07003280
Jeff Brown38a7fab2010-08-30 03:02:23 -07003281 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x);
3282 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y);
3283 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure);
3284 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor);
Jeff Browne57e8952010-07-23 21:28:06 -07003285}
3286
3287
3288// --- MultiTouchInputMapper ---
3289
3290MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device, int32_t associatedDisplayId) :
3291 TouchInputMapper(device, associatedDisplayId) {
3292 initialize();
3293}
3294
3295MultiTouchInputMapper::~MultiTouchInputMapper() {
3296}
3297
3298void MultiTouchInputMapper::initialize() {
3299 mAccumulator.clear();
3300}
3301
3302void MultiTouchInputMapper::reset() {
3303 TouchInputMapper::reset();
3304
Jeff Browne57e8952010-07-23 21:28:06 -07003305 initialize();
3306}
3307
3308void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
3309 switch (rawEvent->type) {
3310 case EV_ABS: {
3311 uint32_t pointerIndex = mAccumulator.pointerCount;
3312 Accumulator::Pointer* pointer = & mAccumulator.pointers[pointerIndex];
3313
3314 switch (rawEvent->scanCode) {
3315 case ABS_MT_POSITION_X:
3316 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_X;
3317 pointer->absMTPositionX = rawEvent->value;
3318 break;
3319 case ABS_MT_POSITION_Y:
3320 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y;
3321 pointer->absMTPositionY = rawEvent->value;
3322 break;
3323 case ABS_MT_TOUCH_MAJOR:
3324 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR;
3325 pointer->absMTTouchMajor = rawEvent->value;
3326 break;
3327 case ABS_MT_TOUCH_MINOR:
3328 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR;
3329 pointer->absMTTouchMinor = rawEvent->value;
3330 break;
3331 case ABS_MT_WIDTH_MAJOR:
3332 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR;
3333 pointer->absMTWidthMajor = rawEvent->value;
3334 break;
3335 case ABS_MT_WIDTH_MINOR:
3336 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR;
3337 pointer->absMTWidthMinor = rawEvent->value;
3338 break;
3339 case ABS_MT_ORIENTATION:
3340 pointer->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION;
3341 pointer->absMTOrientation = rawEvent->value;
3342 break;
3343 case ABS_MT_TRACKING_ID:
3344 pointer->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID;
3345 pointer->absMTTrackingId = rawEvent->value;
3346 break;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003347 case ABS_MT_PRESSURE:
3348 pointer->fields |= Accumulator::FIELD_ABS_MT_PRESSURE;
3349 pointer->absMTPressure = rawEvent->value;
3350 break;
Jeff Browne57e8952010-07-23 21:28:06 -07003351 }
3352 break;
3353 }
3354
3355 case EV_SYN:
3356 switch (rawEvent->scanCode) {
3357 case SYN_MT_REPORT: {
3358 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
3359 uint32_t pointerIndex = mAccumulator.pointerCount;
3360
3361 if (mAccumulator.pointers[pointerIndex].fields) {
3362 if (pointerIndex == MAX_POINTERS) {
3363 LOGW("MultiTouch device driver returned more than maximum of %d pointers.",
3364 MAX_POINTERS);
3365 } else {
3366 pointerIndex += 1;
3367 mAccumulator.pointerCount = pointerIndex;
3368 }
3369 }
3370
3371 mAccumulator.pointers[pointerIndex].clear();
3372 break;
3373 }
3374
3375 case SYN_REPORT:
Jeff Brownd64c8552010-08-17 20:38:35 -07003376 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07003377 break;
3378 }
3379 break;
3380 }
3381}
3382
3383void MultiTouchInputMapper::sync(nsecs_t when) {
3384 static const uint32_t REQUIRED_FIELDS =
Jeff Brown38a7fab2010-08-30 03:02:23 -07003385 Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y;
Jeff Browne839a582010-04-22 18:58:52 -07003386
Jeff Browne57e8952010-07-23 21:28:06 -07003387 uint32_t inCount = mAccumulator.pointerCount;
3388 uint32_t outCount = 0;
3389 bool havePointerIds = true;
Jeff Browne839a582010-04-22 18:58:52 -07003390
Jeff Browne57e8952010-07-23 21:28:06 -07003391 mCurrentTouch.clear();
Jeff Browne839a582010-04-22 18:58:52 -07003392
Jeff Browne57e8952010-07-23 21:28:06 -07003393 for (uint32_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003394 const Accumulator::Pointer& inPointer = mAccumulator.pointers[inIndex];
3395 uint32_t fields = inPointer.fields;
Jeff Browne839a582010-04-22 18:58:52 -07003396
Jeff Browne57e8952010-07-23 21:28:06 -07003397 if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003398 // Some drivers send empty MT sync packets without X / Y to indicate a pointer up.
3399 // Drop this finger.
Jeff Browne839a582010-04-22 18:58:52 -07003400 continue;
3401 }
3402
Jeff Brownd64c8552010-08-17 20:38:35 -07003403 PointerData& outPointer = mCurrentTouch.pointers[outCount];
3404 outPointer.x = inPointer.absMTPositionX;
3405 outPointer.y = inPointer.absMTPositionY;
Jeff Browne839a582010-04-22 18:58:52 -07003406
Jeff Brown38a7fab2010-08-30 03:02:23 -07003407 if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) {
3408 if (inPointer.absMTPressure <= 0) {
3409 // Some devices send sync packets with X / Y but with a 0 presure to indicate
Jeff Brownd64c8552010-08-17 20:38:35 -07003410 // a pointer up. Drop this finger.
3411 continue;
3412 }
Jeff Brown38a7fab2010-08-30 03:02:23 -07003413 outPointer.pressure = inPointer.absMTPressure;
3414 } else {
3415 // Default pressure to 0 if absent.
3416 outPointer.pressure = 0;
3417 }
3418
3419 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) {
3420 if (inPointer.absMTTouchMajor <= 0) {
3421 // Some devices send sync packets with X / Y but with a 0 touch major to indicate
3422 // a pointer going up. Drop this finger.
3423 continue;
3424 }
Jeff Brownd64c8552010-08-17 20:38:35 -07003425 outPointer.touchMajor = inPointer.absMTTouchMajor;
3426 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003427 // Default touch area to 0 if absent.
Jeff Brownd64c8552010-08-17 20:38:35 -07003428 outPointer.touchMajor = 0;
3429 }
Jeff Browne839a582010-04-22 18:58:52 -07003430
Jeff Brownd64c8552010-08-17 20:38:35 -07003431 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) {
3432 outPointer.touchMinor = inPointer.absMTTouchMinor;
3433 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003434 // Assume touch area is circular.
Jeff Brownd64c8552010-08-17 20:38:35 -07003435 outPointer.touchMinor = outPointer.touchMajor;
3436 }
Jeff Browne839a582010-04-22 18:58:52 -07003437
Jeff Brownd64c8552010-08-17 20:38:35 -07003438 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) {
3439 outPointer.toolMajor = inPointer.absMTWidthMajor;
3440 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003441 // Default tool area to 0 if absent.
3442 outPointer.toolMajor = 0;
Jeff Brownd64c8552010-08-17 20:38:35 -07003443 }
Jeff Browne839a582010-04-22 18:58:52 -07003444
Jeff Brownd64c8552010-08-17 20:38:35 -07003445 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) {
3446 outPointer.toolMinor = inPointer.absMTWidthMinor;
3447 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003448 // Assume tool area is circular.
Jeff Brownd64c8552010-08-17 20:38:35 -07003449 outPointer.toolMinor = outPointer.toolMajor;
3450 }
3451
3452 if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) {
3453 outPointer.orientation = inPointer.absMTOrientation;
3454 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003455 // Default orientation to vertical if absent.
Jeff Brownd64c8552010-08-17 20:38:35 -07003456 outPointer.orientation = 0;
3457 }
3458
Jeff Brown38a7fab2010-08-30 03:02:23 -07003459 // Assign pointer id using tracking id if available.
Jeff Browne57e8952010-07-23 21:28:06 -07003460 if (havePointerIds) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003461 if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) {
3462 uint32_t id = uint32_t(inPointer.absMTTrackingId);
Jeff Browne839a582010-04-22 18:58:52 -07003463
Jeff Browne57e8952010-07-23 21:28:06 -07003464 if (id > MAX_POINTER_ID) {
3465#if DEBUG_POINTERS
3466 LOGD("Pointers: Ignoring driver provided pointer id %d because "
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07003467 "it is larger than max supported id %d",
Jeff Browne57e8952010-07-23 21:28:06 -07003468 id, MAX_POINTER_ID);
3469#endif
3470 havePointerIds = false;
3471 }
3472 else {
Jeff Brownd64c8552010-08-17 20:38:35 -07003473 outPointer.id = id;
Jeff Browne57e8952010-07-23 21:28:06 -07003474 mCurrentTouch.idToIndex[id] = outCount;
3475 mCurrentTouch.idBits.markBit(id);
3476 }
3477 } else {
3478 havePointerIds = false;
Jeff Browne839a582010-04-22 18:58:52 -07003479 }
3480 }
Jeff Browne839a582010-04-22 18:58:52 -07003481
Jeff Browne57e8952010-07-23 21:28:06 -07003482 outCount += 1;
Jeff Browne839a582010-04-22 18:58:52 -07003483 }
3484
Jeff Browne57e8952010-07-23 21:28:06 -07003485 mCurrentTouch.pointerCount = outCount;
Jeff Browne839a582010-04-22 18:58:52 -07003486
Jeff Browne57e8952010-07-23 21:28:06 -07003487 syncTouch(when, havePointerIds);
Jeff Brownd64c8552010-08-17 20:38:35 -07003488
3489 mAccumulator.clear();
Jeff Browne839a582010-04-22 18:58:52 -07003490}
3491
Jeff Brown38a7fab2010-08-30 03:02:23 -07003492void MultiTouchInputMapper::configureRawAxes() {
3493 TouchInputMapper::configureRawAxes();
Jeff Browne839a582010-04-22 18:58:52 -07003494
Jeff Brown38a7fab2010-08-30 03:02:23 -07003495 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, & mRawAxes.x);
3496 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, & mRawAxes.y);
3497 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, & mRawAxes.touchMajor);
3498 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, & mRawAxes.touchMinor);
3499 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, & mRawAxes.toolMajor);
3500 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, & mRawAxes.toolMinor);
3501 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, & mRawAxes.orientation);
3502 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, & mRawAxes.pressure);
Jeff Brown54bc2812010-06-15 01:31:58 -07003503}
3504
Jeff Browne839a582010-04-22 18:58:52 -07003505
3506} // namespace android