blob: aa690e5fd1ec6fc9142fcac4ec31da64f8cdf1f6 [file] [log] [blame]
Jeff Browne839a582010-04-22 18:58:52 -07001//
2// Copyright 2010 The Android Open Source Project
3//
4// The input reader.
5//
6#define LOG_TAG "InputReader"
7
8//#define LOG_NDEBUG 0
9
10// Log debug messages for each raw event received from the EventHub.
11#define DEBUG_RAW_EVENTS 0
12
13// Log debug messages about touch screen filtering hacks.
Jeff Brown50de30a2010-06-22 01:27:15 -070014#define DEBUG_HACKS 0
Jeff Browne839a582010-04-22 18:58:52 -070015
16// Log debug messages about virtual key processing.
Jeff Brown50de30a2010-06-22 01:27:15 -070017#define DEBUG_VIRTUAL_KEYS 0
Jeff Browne839a582010-04-22 18:58:52 -070018
19// Log debug messages about pointers.
Jeff Brown50de30a2010-06-22 01:27:15 -070020#define DEBUG_POINTERS 0
Jeff Browne839a582010-04-22 18:58:52 -070021
Jeff Brownf4a4ec22010-06-16 01:53:36 -070022// Log debug messages about pointer assignment calculations.
23#define DEBUG_POINTER_ASSIGNMENT 0
24
Jeff Browne839a582010-04-22 18:58:52 -070025#include <cutils/log.h>
26#include <ui/InputReader.h>
Jeff Browna3477c82010-11-10 16:03:06 -080027#include <ui/Keyboard.h>
Jeff Browne839a582010-04-22 18:58:52 -070028
29#include <stddef.h>
Jeff Brown38a7fab2010-08-30 03:02:23 -070030#include <stdlib.h>
Jeff Browne839a582010-04-22 18:58:52 -070031#include <unistd.h>
Jeff Browne839a582010-04-22 18:58:52 -070032#include <errno.h>
33#include <limits.h>
Jeff Brown5c1ed842010-07-14 18:48:53 -070034#include <math.h>
Jeff Browne839a582010-04-22 18:58:52 -070035
Jeff Brown38a7fab2010-08-30 03:02:23 -070036#define INDENT " "
Jeff Brown26c94ff2010-09-30 14:33:04 -070037#define INDENT2 " "
38#define INDENT3 " "
39#define INDENT4 " "
Jeff Brown38a7fab2010-08-30 03:02:23 -070040
Jeff Browne839a582010-04-22 18:58:52 -070041namespace android {
42
43// --- Static Functions ---
44
45template<typename T>
46inline static T abs(const T& value) {
47 return value < 0 ? - value : value;
48}
49
50template<typename T>
51inline static T min(const T& a, const T& b) {
52 return a < b ? a : b;
53}
54
Jeff Brownf4a4ec22010-06-16 01:53:36 -070055template<typename T>
56inline static void swap(T& a, T& b) {
57 T temp = a;
58 a = b;
59 b = temp;
60}
61
Jeff Brown38a7fab2010-08-30 03:02:23 -070062inline static float avg(float x, float y) {
63 return (x + y) / 2;
64}
65
66inline static float pythag(float x, float y) {
67 return sqrtf(x * x + y * y);
68}
69
Jeff Brown26c94ff2010-09-30 14:33:04 -070070static inline const char* toString(bool value) {
71 return value ? "true" : "false";
72}
73
Jeff Browne839a582010-04-22 18:58:52 -070074static const int32_t keyCodeRotationMap[][4] = {
75 // key codes enumerated counter-clockwise with the original (unrotated) key first
76 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brown8575a872010-06-30 16:10:35 -070077 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
78 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
79 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
80 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Browne839a582010-04-22 18:58:52 -070081};
82static const int keyCodeRotationMapSize =
83 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
84
85int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brown54bc2812010-06-15 01:31:58 -070086 if (orientation != InputReaderPolicyInterface::ROTATION_0) {
Jeff Browne839a582010-04-22 18:58:52 -070087 for (int i = 0; i < keyCodeRotationMapSize; i++) {
88 if (keyCode == keyCodeRotationMap[i][0]) {
89 return keyCodeRotationMap[i][orientation];
90 }
91 }
92 }
93 return keyCode;
94}
95
Jeff Browne57e8952010-07-23 21:28:06 -070096static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
97 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
98}
99
Jeff Browne839a582010-04-22 18:58:52 -0700100
Jeff Browne839a582010-04-22 18:58:52 -0700101// --- InputReader ---
102
103InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown54bc2812010-06-15 01:31:58 -0700104 const sp<InputReaderPolicyInterface>& policy,
Jeff Browne839a582010-04-22 18:58:52 -0700105 const sp<InputDispatcherInterface>& dispatcher) :
Jeff Browne57e8952010-07-23 21:28:06 -0700106 mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher),
107 mGlobalMetaState(0) {
Jeff Brown54bc2812010-06-15 01:31:58 -0700108 configureExcludedDevices();
Jeff Browne57e8952010-07-23 21:28:06 -0700109 updateGlobalMetaState();
110 updateInputConfiguration();
Jeff Browne839a582010-04-22 18:58:52 -0700111}
112
113InputReader::~InputReader() {
114 for (size_t i = 0; i < mDevices.size(); i++) {
115 delete mDevices.valueAt(i);
116 }
117}
118
119void InputReader::loopOnce() {
120 RawEvent rawEvent;
Jeff Browne57e8952010-07-23 21:28:06 -0700121 mEventHub->getEvent(& rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700122
123#if DEBUG_RAW_EVENTS
124 LOGD("Input event: device=0x%x type=0x%x scancode=%d keycode=%d value=%d",
125 rawEvent.deviceId, rawEvent.type, rawEvent.scanCode, rawEvent.keyCode,
126 rawEvent.value);
127#endif
128
129 process(& rawEvent);
130}
131
132void InputReader::process(const RawEvent* rawEvent) {
133 switch (rawEvent->type) {
134 case EventHubInterface::DEVICE_ADDED:
Jeff Brown1ad00e92010-10-01 18:55:43 -0700135 addDevice(rawEvent->deviceId);
Jeff Browne839a582010-04-22 18:58:52 -0700136 break;
137
138 case EventHubInterface::DEVICE_REMOVED:
Jeff Brown1ad00e92010-10-01 18:55:43 -0700139 removeDevice(rawEvent->deviceId);
140 break;
141
142 case EventHubInterface::FINISHED_DEVICE_SCAN:
Jeff Brown3c3cc622010-10-20 15:33:38 -0700143 handleConfigurationChanged(rawEvent->when);
Jeff Browne839a582010-04-22 18:58:52 -0700144 break;
145
Jeff Browne57e8952010-07-23 21:28:06 -0700146 default:
147 consumeEvent(rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700148 break;
149 }
150}
151
Jeff Brown1ad00e92010-10-01 18:55:43 -0700152void InputReader::addDevice(int32_t deviceId) {
Jeff Browne57e8952010-07-23 21:28:06 -0700153 String8 name = mEventHub->getDeviceName(deviceId);
154 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
155
156 InputDevice* device = createDevice(deviceId, name, classes);
157 device->configure();
158
Jeff Brown38a7fab2010-08-30 03:02:23 -0700159 if (device->isIgnored()) {
Jeff Brown26c94ff2010-09-30 14:33:04 -0700160 LOGI("Device added: id=0x%x, name=%s (ignored non-input device)", deviceId, name.string());
Jeff Brown38a7fab2010-08-30 03:02:23 -0700161 } else {
Jeff Brown26c94ff2010-09-30 14:33:04 -0700162 LOGI("Device added: id=0x%x, name=%s, sources=%08x", deviceId, name.string(),
163 device->getSources());
Jeff Brown38a7fab2010-08-30 03:02:23 -0700164 }
165
Jeff Browne57e8952010-07-23 21:28:06 -0700166 bool added = false;
167 { // acquire device registry writer lock
168 RWLock::AutoWLock _wl(mDeviceRegistryLock);
169
170 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
171 if (deviceIndex < 0) {
172 mDevices.add(deviceId, device);
173 added = true;
174 }
175 } // release device registry writer lock
176
177 if (! added) {
178 LOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
179 delete device;
Jeff Browne839a582010-04-22 18:58:52 -0700180 return;
181 }
Jeff Browne839a582010-04-22 18:58:52 -0700182}
183
Jeff Brown1ad00e92010-10-01 18:55:43 -0700184void InputReader::removeDevice(int32_t deviceId) {
Jeff Browne57e8952010-07-23 21:28:06 -0700185 bool removed = false;
186 InputDevice* device = NULL;
187 { // acquire device registry writer lock
188 RWLock::AutoWLock _wl(mDeviceRegistryLock);
189
190 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
191 if (deviceIndex >= 0) {
192 device = mDevices.valueAt(deviceIndex);
193 mDevices.removeItemsAt(deviceIndex, 1);
194 removed = true;
195 }
196 } // release device registry writer lock
197
198 if (! removed) {
199 LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Browne839a582010-04-22 18:58:52 -0700200 return;
201 }
202
Jeff Browne57e8952010-07-23 21:28:06 -0700203 if (device->isIgnored()) {
204 LOGI("Device removed: id=0x%x, name=%s (ignored non-input device)",
205 device->getId(), device->getName().string());
206 } else {
207 LOGI("Device removed: id=0x%x, name=%s, sources=%08x",
208 device->getId(), device->getName().string(), device->getSources());
209 }
210
Jeff Brown38a7fab2010-08-30 03:02:23 -0700211 device->reset();
212
Jeff Browne57e8952010-07-23 21:28:06 -0700213 delete device;
Jeff Browne839a582010-04-22 18:58:52 -0700214}
215
Jeff Browne57e8952010-07-23 21:28:06 -0700216InputDevice* InputReader::createDevice(int32_t deviceId, const String8& name, uint32_t classes) {
217 InputDevice* device = new InputDevice(this, deviceId, name);
Jeff Browne839a582010-04-22 18:58:52 -0700218
Jeff Browne57e8952010-07-23 21:28:06 -0700219 // Switch-like devices.
220 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
221 device->addMapper(new SwitchInputMapper(device));
222 }
223
224 // Keyboard-like devices.
225 uint32_t keyboardSources = 0;
226 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
227 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
228 keyboardSources |= AINPUT_SOURCE_KEYBOARD;
229 }
230 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
231 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
232 }
233 if (classes & INPUT_DEVICE_CLASS_DPAD) {
234 keyboardSources |= AINPUT_SOURCE_DPAD;
235 }
Jeff Browne57e8952010-07-23 21:28:06 -0700236
237 if (keyboardSources != 0) {
Jeff Brown66888372010-11-29 17:37:49 -0800238 device->addMapper(new KeyboardInputMapper(device, keyboardSources, keyboardType));
Jeff Browne57e8952010-07-23 21:28:06 -0700239 }
240
241 // Trackball-like devices.
242 if (classes & INPUT_DEVICE_CLASS_TRACKBALL) {
Jeff Brown66888372010-11-29 17:37:49 -0800243 device->addMapper(new TrackballInputMapper(device));
Jeff Browne57e8952010-07-23 21:28:06 -0700244 }
245
246 // Touchscreen-like devices.
247 if (classes & INPUT_DEVICE_CLASS_TOUCHSCREEN_MT) {
Jeff Brown66888372010-11-29 17:37:49 -0800248 device->addMapper(new MultiTouchInputMapper(device));
Jeff Browne57e8952010-07-23 21:28:06 -0700249 } else if (classes & INPUT_DEVICE_CLASS_TOUCHSCREEN) {
Jeff Brown66888372010-11-29 17:37:49 -0800250 device->addMapper(new SingleTouchInputMapper(device));
Jeff Browne57e8952010-07-23 21:28:06 -0700251 }
252
253 return device;
254}
255
256void InputReader::consumeEvent(const RawEvent* rawEvent) {
257 int32_t deviceId = rawEvent->deviceId;
258
259 { // acquire device registry reader lock
260 RWLock::AutoRLock _rl(mDeviceRegistryLock);
261
262 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
263 if (deviceIndex < 0) {
264 LOGW("Discarding event for unknown deviceId %d.", deviceId);
265 return;
266 }
267
268 InputDevice* device = mDevices.valueAt(deviceIndex);
269 if (device->isIgnored()) {
270 //LOGD("Discarding event for ignored deviceId %d.", deviceId);
271 return;
272 }
273
274 device->process(rawEvent);
275 } // release device registry reader lock
276}
277
Jeff Brown3c3cc622010-10-20 15:33:38 -0700278void InputReader::handleConfigurationChanged(nsecs_t when) {
Jeff Browne57e8952010-07-23 21:28:06 -0700279 // Reset global meta state because it depends on the list of all configured devices.
280 updateGlobalMetaState();
281
282 // Update input configuration.
283 updateInputConfiguration();
284
285 // Enqueue configuration changed.
286 mDispatcher->notifyConfigurationChanged(when);
287}
288
289void InputReader::configureExcludedDevices() {
290 Vector<String8> excludedDeviceNames;
291 mPolicy->getExcludedDeviceNames(excludedDeviceNames);
292
293 for (size_t i = 0; i < excludedDeviceNames.size(); i++) {
294 mEventHub->addExcludedDevice(excludedDeviceNames[i]);
295 }
296}
297
298void InputReader::updateGlobalMetaState() {
299 { // acquire state lock
300 AutoMutex _l(mStateLock);
301
302 mGlobalMetaState = 0;
303
304 { // acquire device registry reader lock
305 RWLock::AutoRLock _rl(mDeviceRegistryLock);
306
307 for (size_t i = 0; i < mDevices.size(); i++) {
308 InputDevice* device = mDevices.valueAt(i);
309 mGlobalMetaState |= device->getMetaState();
310 }
311 } // release device registry reader lock
312 } // release state lock
313}
314
315int32_t InputReader::getGlobalMetaState() {
316 { // acquire state lock
317 AutoMutex _l(mStateLock);
318
319 return mGlobalMetaState;
320 } // release state lock
321}
322
323void InputReader::updateInputConfiguration() {
324 { // acquire state lock
325 AutoMutex _l(mStateLock);
326
327 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
328 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
329 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
330 { // acquire device registry reader lock
331 RWLock::AutoRLock _rl(mDeviceRegistryLock);
332
333 InputDeviceInfo deviceInfo;
334 for (size_t i = 0; i < mDevices.size(); i++) {
335 InputDevice* device = mDevices.valueAt(i);
336 device->getDeviceInfo(& deviceInfo);
337 uint32_t sources = deviceInfo.getSources();
338
339 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
340 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
341 }
342 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
343 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
344 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
345 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
346 }
347 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
348 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
Jeff Browne839a582010-04-22 18:58:52 -0700349 }
350 }
Jeff Browne57e8952010-07-23 21:28:06 -0700351 } // release device registry reader lock
Jeff Browne839a582010-04-22 18:58:52 -0700352
Jeff Browne57e8952010-07-23 21:28:06 -0700353 mInputConfiguration.touchScreen = touchScreenConfig;
354 mInputConfiguration.keyboard = keyboardConfig;
355 mInputConfiguration.navigation = navigationConfig;
356 } // release state lock
357}
358
359void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
360 { // acquire state lock
361 AutoMutex _l(mStateLock);
362
363 *outConfiguration = mInputConfiguration;
364 } // release state lock
365}
366
367status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
368 { // acquire device registry reader lock
369 RWLock::AutoRLock _rl(mDeviceRegistryLock);
370
371 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
372 if (deviceIndex < 0) {
373 return NAME_NOT_FOUND;
Jeff Browne839a582010-04-22 18:58:52 -0700374 }
375
Jeff Browne57e8952010-07-23 21:28:06 -0700376 InputDevice* device = mDevices.valueAt(deviceIndex);
377 if (device->isIgnored()) {
378 return NAME_NOT_FOUND;
Jeff Browne839a582010-04-22 18:58:52 -0700379 }
Jeff Browne57e8952010-07-23 21:28:06 -0700380
381 device->getDeviceInfo(outDeviceInfo);
382 return OK;
383 } // release device registy reader lock
384}
385
386void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
387 outDeviceIds.clear();
388
389 { // acquire device registry reader lock
390 RWLock::AutoRLock _rl(mDeviceRegistryLock);
391
392 size_t numDevices = mDevices.size();
393 for (size_t i = 0; i < numDevices; i++) {
394 InputDevice* device = mDevices.valueAt(i);
395 if (! device->isIgnored()) {
396 outDeviceIds.add(device->getId());
397 }
398 }
399 } // release device registy reader lock
400}
401
402int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
403 int32_t keyCode) {
404 return getState(deviceId, sourceMask, keyCode, & InputDevice::getKeyCodeState);
405}
406
407int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
408 int32_t scanCode) {
409 return getState(deviceId, sourceMask, scanCode, & InputDevice::getScanCodeState);
410}
411
412int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
413 return getState(deviceId, sourceMask, switchCode, & InputDevice::getSwitchState);
414}
415
416int32_t InputReader::getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
417 GetStateFunc getStateFunc) {
418 { // acquire device registry reader lock
419 RWLock::AutoRLock _rl(mDeviceRegistryLock);
420
421 int32_t result = AKEY_STATE_UNKNOWN;
422 if (deviceId >= 0) {
423 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
424 if (deviceIndex >= 0) {
425 InputDevice* device = mDevices.valueAt(deviceIndex);
426 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
427 result = (device->*getStateFunc)(sourceMask, code);
428 }
429 }
430 } else {
431 size_t numDevices = mDevices.size();
432 for (size_t i = 0; i < numDevices; i++) {
433 InputDevice* device = mDevices.valueAt(i);
434 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
435 result = (device->*getStateFunc)(sourceMask, code);
436 if (result >= AKEY_STATE_DOWN) {
437 return result;
438 }
439 }
440 }
441 }
442 return result;
443 } // release device registy reader lock
444}
445
446bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
447 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
448 memset(outFlags, 0, numCodes);
449 return markSupportedKeyCodes(deviceId, sourceMask, numCodes, keyCodes, outFlags);
450}
451
452bool InputReader::markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
453 const int32_t* keyCodes, uint8_t* outFlags) {
454 { // acquire device registry reader lock
455 RWLock::AutoRLock _rl(mDeviceRegistryLock);
456 bool result = false;
457 if (deviceId >= 0) {
458 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
459 if (deviceIndex >= 0) {
460 InputDevice* device = mDevices.valueAt(deviceIndex);
461 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
462 result = device->markSupportedKeyCodes(sourceMask,
463 numCodes, keyCodes, outFlags);
464 }
465 }
466 } else {
467 size_t numDevices = mDevices.size();
468 for (size_t i = 0; i < numDevices; i++) {
469 InputDevice* device = mDevices.valueAt(i);
470 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
471 result |= device->markSupportedKeyCodes(sourceMask,
472 numCodes, keyCodes, outFlags);
473 }
474 }
475 }
476 return result;
477 } // release device registy reader lock
478}
479
Jeff Browna665ca82010-09-08 11:49:43 -0700480void InputReader::dump(String8& dump) {
Jeff Brown2806e382010-10-01 17:46:21 -0700481 mEventHub->dump(dump);
482 dump.append("\n");
483
484 dump.append("Input Reader State:\n");
485
Jeff Brown26c94ff2010-09-30 14:33:04 -0700486 { // acquire device registry reader lock
487 RWLock::AutoRLock _rl(mDeviceRegistryLock);
Jeff Browna665ca82010-09-08 11:49:43 -0700488
Jeff Brown26c94ff2010-09-30 14:33:04 -0700489 for (size_t i = 0; i < mDevices.size(); i++) {
490 mDevices.valueAt(i)->dump(dump);
Jeff Browna665ca82010-09-08 11:49:43 -0700491 }
Jeff Brown26c94ff2010-09-30 14:33:04 -0700492 } // release device registy reader lock
Jeff Browna665ca82010-09-08 11:49:43 -0700493}
494
Jeff Browne57e8952010-07-23 21:28:06 -0700495
496// --- InputReaderThread ---
497
498InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
499 Thread(/*canCallJava*/ true), mReader(reader) {
500}
501
502InputReaderThread::~InputReaderThread() {
503}
504
505bool InputReaderThread::threadLoop() {
506 mReader->loopOnce();
507 return true;
508}
509
510
511// --- InputDevice ---
512
513InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) :
514 mContext(context), mId(id), mName(name), mSources(0) {
515}
516
517InputDevice::~InputDevice() {
518 size_t numMappers = mMappers.size();
519 for (size_t i = 0; i < numMappers; i++) {
520 delete mMappers[i];
521 }
522 mMappers.clear();
523}
524
Jeff Brown26c94ff2010-09-30 14:33:04 -0700525static void dumpMotionRange(String8& dump, const InputDeviceInfo& deviceInfo,
526 int32_t rangeType, const char* name) {
527 const InputDeviceInfo::MotionRange* range = deviceInfo.getMotionRange(rangeType);
528 if (range) {
529 dump.appendFormat(INDENT3 "%s: min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
530 name, range->min, range->max, range->flat, range->fuzz);
531 }
532}
533
534void InputDevice::dump(String8& dump) {
535 InputDeviceInfo deviceInfo;
536 getDeviceInfo(& deviceInfo);
537
538 dump.appendFormat(INDENT "Device 0x%x: %s\n", deviceInfo.getId(),
539 deviceInfo.getName().string());
540 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
541 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
542 if (!deviceInfo.getMotionRanges().isEmpty()) {
543 dump.append(INDENT2 "Motion Ranges:\n");
544 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_X, "X");
545 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_Y, "Y");
546 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_PRESSURE, "Pressure");
547 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_SIZE, "Size");
548 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOUCH_MAJOR, "TouchMajor");
549 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOUCH_MINOR, "TouchMinor");
550 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOOL_MAJOR, "ToolMajor");
551 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOOL_MINOR, "ToolMinor");
552 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_ORIENTATION, "Orientation");
553 }
554
555 size_t numMappers = mMappers.size();
556 for (size_t i = 0; i < numMappers; i++) {
557 InputMapper* mapper = mMappers[i];
558 mapper->dump(dump);
559 }
560}
561
Jeff Browne57e8952010-07-23 21:28:06 -0700562void InputDevice::addMapper(InputMapper* mapper) {
563 mMappers.add(mapper);
564}
565
566void InputDevice::configure() {
Jeff Brown38a7fab2010-08-30 03:02:23 -0700567 if (! isIgnored()) {
Jeff Brown66888372010-11-29 17:37:49 -0800568 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700569 }
570
Jeff Browne57e8952010-07-23 21:28:06 -0700571 mSources = 0;
572
573 size_t numMappers = mMappers.size();
574 for (size_t i = 0; i < numMappers; i++) {
575 InputMapper* mapper = mMappers[i];
576 mapper->configure();
577 mSources |= mapper->getSources();
Jeff Browne839a582010-04-22 18:58:52 -0700578 }
579}
580
Jeff Browne57e8952010-07-23 21:28:06 -0700581void InputDevice::reset() {
582 size_t numMappers = mMappers.size();
583 for (size_t i = 0; i < numMappers; i++) {
584 InputMapper* mapper = mMappers[i];
585 mapper->reset();
586 }
587}
Jeff Browne839a582010-04-22 18:58:52 -0700588
Jeff Browne57e8952010-07-23 21:28:06 -0700589void InputDevice::process(const RawEvent* rawEvent) {
590 size_t numMappers = mMappers.size();
591 for (size_t i = 0; i < numMappers; i++) {
592 InputMapper* mapper = mMappers[i];
593 mapper->process(rawEvent);
594 }
595}
Jeff Browne839a582010-04-22 18:58:52 -0700596
Jeff Browne57e8952010-07-23 21:28:06 -0700597void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
598 outDeviceInfo->initialize(mId, mName);
599
600 size_t numMappers = mMappers.size();
601 for (size_t i = 0; i < numMappers; i++) {
602 InputMapper* mapper = mMappers[i];
603 mapper->populateDeviceInfo(outDeviceInfo);
604 }
605}
606
607int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
608 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
609}
610
611int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
612 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
613}
614
615int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
616 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
617}
618
619int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
620 int32_t result = AKEY_STATE_UNKNOWN;
621 size_t numMappers = mMappers.size();
622 for (size_t i = 0; i < numMappers; i++) {
623 InputMapper* mapper = mMappers[i];
624 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
625 result = (mapper->*getStateFunc)(sourceMask, code);
626 if (result >= AKEY_STATE_DOWN) {
627 return result;
628 }
629 }
630 }
631 return result;
632}
633
634bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
635 const int32_t* keyCodes, uint8_t* outFlags) {
636 bool result = false;
637 size_t numMappers = mMappers.size();
638 for (size_t i = 0; i < numMappers; i++) {
639 InputMapper* mapper = mMappers[i];
640 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
641 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
642 }
643 }
644 return result;
645}
646
647int32_t InputDevice::getMetaState() {
648 int32_t result = 0;
649 size_t numMappers = mMappers.size();
650 for (size_t i = 0; i < numMappers; i++) {
651 InputMapper* mapper = mMappers[i];
652 result |= mapper->getMetaState();
653 }
654 return result;
655}
656
657
658// --- InputMapper ---
659
660InputMapper::InputMapper(InputDevice* device) :
661 mDevice(device), mContext(device->getContext()) {
662}
663
664InputMapper::~InputMapper() {
665}
666
667void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
668 info->addSource(getSources());
669}
670
Jeff Brown26c94ff2010-09-30 14:33:04 -0700671void InputMapper::dump(String8& dump) {
672}
673
Jeff Browne57e8952010-07-23 21:28:06 -0700674void InputMapper::configure() {
675}
676
677void InputMapper::reset() {
678}
679
680int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
681 return AKEY_STATE_UNKNOWN;
682}
683
684int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
685 return AKEY_STATE_UNKNOWN;
686}
687
688int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
689 return AKEY_STATE_UNKNOWN;
690}
691
692bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
693 const int32_t* keyCodes, uint8_t* outFlags) {
694 return false;
695}
696
697int32_t InputMapper::getMetaState() {
698 return 0;
699}
700
Jeff Browne57e8952010-07-23 21:28:06 -0700701
702// --- SwitchInputMapper ---
703
704SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
705 InputMapper(device) {
706}
707
708SwitchInputMapper::~SwitchInputMapper() {
709}
710
711uint32_t SwitchInputMapper::getSources() {
712 return 0;
713}
714
715void SwitchInputMapper::process(const RawEvent* rawEvent) {
716 switch (rawEvent->type) {
717 case EV_SW:
718 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
719 break;
720 }
721}
722
723void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brown90f0cee2010-10-08 22:31:17 -0700724 getDispatcher()->notifySwitch(when, switchCode, switchValue, 0);
Jeff Browne57e8952010-07-23 21:28:06 -0700725}
726
727int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
728 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
729}
730
731
732// --- KeyboardInputMapper ---
733
Jeff Brown66888372010-11-29 17:37:49 -0800734KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Browne57e8952010-07-23 21:28:06 -0700735 uint32_t sources, int32_t keyboardType) :
Jeff Brown66888372010-11-29 17:37:49 -0800736 InputMapper(device), mSources(sources),
Jeff Browne57e8952010-07-23 21:28:06 -0700737 mKeyboardType(keyboardType) {
Jeff Brownb51719b2010-07-29 18:18:33 -0700738 initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700739}
740
741KeyboardInputMapper::~KeyboardInputMapper() {
742}
743
Jeff Brownb51719b2010-07-29 18:18:33 -0700744void KeyboardInputMapper::initializeLocked() {
745 mLocked.metaState = AMETA_NONE;
746 mLocked.downTime = 0;
Jeff Brown6a817e22010-09-12 17:55:08 -0700747
748 initializeLedStateLocked(mLocked.capsLockLedState, LED_CAPSL);
749 initializeLedStateLocked(mLocked.numLockLedState, LED_NUML);
750 initializeLedStateLocked(mLocked.scrollLockLedState, LED_SCROLLL);
751
752 updateLedStateLocked(true);
753}
754
755void KeyboardInputMapper::initializeLedStateLocked(LockedState::LedState& ledState, int32_t led) {
756 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
757 ledState.on = false;
Jeff Browne57e8952010-07-23 21:28:06 -0700758}
759
760uint32_t KeyboardInputMapper::getSources() {
761 return mSources;
762}
763
764void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
765 InputMapper::populateDeviceInfo(info);
766
767 info->setKeyboardType(mKeyboardType);
768}
769
Jeff Brown26c94ff2010-09-30 14:33:04 -0700770void KeyboardInputMapper::dump(String8& dump) {
771 { // acquire lock
772 AutoMutex _l(mLock);
773 dump.append(INDENT2 "Keyboard Input Mapper:\n");
Jeff Brown66888372010-11-29 17:37:49 -0800774 dumpParameters(dump);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700775 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
776 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mLocked.keyDowns.size());
777 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mLocked.metaState);
778 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
779 } // release lock
780}
781
Jeff Brown66888372010-11-29 17:37:49 -0800782
783void KeyboardInputMapper::configure() {
784 InputMapper::configure();
785
786 // Configure basic parameters.
787 configureParameters();
788}
789
790void KeyboardInputMapper::configureParameters() {
791 mParameters.orientationAware = false;
792 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
793 mParameters.orientationAware);
794
795 mParameters.associatedDisplayId = mParameters.orientationAware ? 0 : -1;
796}
797
798void KeyboardInputMapper::dumpParameters(String8& dump) {
799 dump.append(INDENT3 "Parameters:\n");
800 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
801 mParameters.associatedDisplayId);
802 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
803 toString(mParameters.orientationAware));
804}
805
Jeff Browne57e8952010-07-23 21:28:06 -0700806void KeyboardInputMapper::reset() {
Jeff Brownb51719b2010-07-29 18:18:33 -0700807 for (;;) {
808 int32_t keyCode, scanCode;
809 { // acquire lock
810 AutoMutex _l(mLock);
811
812 // Synthesize key up event on reset if keys are currently down.
813 if (mLocked.keyDowns.isEmpty()) {
814 initializeLocked();
815 break; // done
816 }
817
818 const KeyDown& keyDown = mLocked.keyDowns.top();
819 keyCode = keyDown.keyCode;
820 scanCode = keyDown.scanCode;
821 } // release lock
822
Jeff Browne57e8952010-07-23 21:28:06 -0700823 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownb51719b2010-07-29 18:18:33 -0700824 processKey(when, false, keyCode, scanCode, 0);
Jeff Browne57e8952010-07-23 21:28:06 -0700825 }
826
827 InputMapper::reset();
Jeff Browne57e8952010-07-23 21:28:06 -0700828 getContext()->updateGlobalMetaState();
829}
830
831void KeyboardInputMapper::process(const RawEvent* rawEvent) {
832 switch (rawEvent->type) {
833 case EV_KEY: {
834 int32_t scanCode = rawEvent->scanCode;
835 if (isKeyboardOrGamepadKey(scanCode)) {
836 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
837 rawEvent->flags);
838 }
839 break;
840 }
841 }
842}
843
844bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
845 return scanCode < BTN_MOUSE
846 || scanCode >= KEY_OK
847 || (scanCode >= BTN_GAMEPAD && scanCode < BTN_DIGI);
848}
849
Jeff Brownb51719b2010-07-29 18:18:33 -0700850void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
851 int32_t scanCode, uint32_t policyFlags) {
852 int32_t newMetaState;
853 nsecs_t downTime;
854 bool metaStateChanged = false;
855
856 { // acquire lock
857 AutoMutex _l(mLock);
858
859 if (down) {
860 // Rotate key codes according to orientation if needed.
861 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown66888372010-11-29 17:37:49 -0800862 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brownb51719b2010-07-29 18:18:33 -0700863 int32_t orientation;
Jeff Brown66888372010-11-29 17:37:49 -0800864 if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
865 NULL, NULL, & orientation)) {
Jeff Brownd4ecee92010-10-29 22:19:53 -0700866 orientation = InputReaderPolicyInterface::ROTATION_0;
Jeff Brownb51719b2010-07-29 18:18:33 -0700867 }
868
869 keyCode = rotateKeyCode(keyCode, orientation);
Jeff Browne57e8952010-07-23 21:28:06 -0700870 }
871
Jeff Brownb51719b2010-07-29 18:18:33 -0700872 // Add key down.
873 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
874 if (keyDownIndex >= 0) {
875 // key repeat, be sure to use same keycode as before in case of rotation
Jeff Browna3477c82010-11-10 16:03:06 -0800876 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brownb51719b2010-07-29 18:18:33 -0700877 } else {
878 // key down
879 mLocked.keyDowns.push();
880 KeyDown& keyDown = mLocked.keyDowns.editTop();
881 keyDown.keyCode = keyCode;
882 keyDown.scanCode = scanCode;
883 }
884
885 mLocked.downTime = when;
886 } else {
887 // Remove key down.
888 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
889 if (keyDownIndex >= 0) {
890 // key up, be sure to use same keycode as before in case of rotation
Jeff Browna3477c82010-11-10 16:03:06 -0800891 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brownb51719b2010-07-29 18:18:33 -0700892 mLocked.keyDowns.removeAt(size_t(keyDownIndex));
893 } else {
894 // key was not actually down
895 LOGI("Dropping key up from device %s because the key was not down. "
896 "keyCode=%d, scanCode=%d",
897 getDeviceName().string(), keyCode, scanCode);
898 return;
899 }
Jeff Browne57e8952010-07-23 21:28:06 -0700900 }
901
Jeff Brownb51719b2010-07-29 18:18:33 -0700902 int32_t oldMetaState = mLocked.metaState;
903 newMetaState = updateMetaState(keyCode, down, oldMetaState);
904 if (oldMetaState != newMetaState) {
905 mLocked.metaState = newMetaState;
906 metaStateChanged = true;
Jeff Brown6a817e22010-09-12 17:55:08 -0700907 updateLedStateLocked(false);
Jeff Browne57e8952010-07-23 21:28:06 -0700908 }
Jeff Brown8575a872010-06-30 16:10:35 -0700909
Jeff Brownb51719b2010-07-29 18:18:33 -0700910 downTime = mLocked.downTime;
911 } // release lock
912
913 if (metaStateChanged) {
Jeff Browne57e8952010-07-23 21:28:06 -0700914 getContext()->updateGlobalMetaState();
Jeff Browne839a582010-04-22 18:58:52 -0700915 }
916
Jeff Brown6a817e22010-09-12 17:55:08 -0700917 if (policyFlags & POLICY_FLAG_FUNCTION) {
918 newMetaState |= AMETA_FUNCTION_ON;
919 }
Jeff Browne57e8952010-07-23 21:28:06 -0700920 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
Jeff Brown90f0cee2010-10-08 22:31:17 -0700921 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
922 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Browne839a582010-04-22 18:58:52 -0700923}
924
Jeff Brownb51719b2010-07-29 18:18:33 -0700925ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) {
926 size_t n = mLocked.keyDowns.size();
Jeff Browne57e8952010-07-23 21:28:06 -0700927 for (size_t i = 0; i < n; i++) {
Jeff Brownb51719b2010-07-29 18:18:33 -0700928 if (mLocked.keyDowns[i].scanCode == scanCode) {
Jeff Browne57e8952010-07-23 21:28:06 -0700929 return i;
930 }
931 }
932 return -1;
Jeff Browne839a582010-04-22 18:58:52 -0700933}
934
Jeff Browne57e8952010-07-23 21:28:06 -0700935int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
936 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
937}
Jeff Browne839a582010-04-22 18:58:52 -0700938
Jeff Browne57e8952010-07-23 21:28:06 -0700939int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
940 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
941}
Jeff Browne839a582010-04-22 18:58:52 -0700942
Jeff Browne57e8952010-07-23 21:28:06 -0700943bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
944 const int32_t* keyCodes, uint8_t* outFlags) {
945 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
946}
947
948int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownb51719b2010-07-29 18:18:33 -0700949 { // acquire lock
950 AutoMutex _l(mLock);
951 return mLocked.metaState;
952 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -0700953}
954
Jeff Brown6a817e22010-09-12 17:55:08 -0700955void KeyboardInputMapper::updateLedStateLocked(bool reset) {
956 updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL,
Jeff Brownd4ecee92010-10-29 22:19:53 -0700957 AMETA_CAPS_LOCK_ON, reset);
Jeff Brown6a817e22010-09-12 17:55:08 -0700958 updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML,
Jeff Brownd4ecee92010-10-29 22:19:53 -0700959 AMETA_NUM_LOCK_ON, reset);
Jeff Brown6a817e22010-09-12 17:55:08 -0700960 updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL,
Jeff Brownd4ecee92010-10-29 22:19:53 -0700961 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown6a817e22010-09-12 17:55:08 -0700962}
963
964void KeyboardInputMapper::updateLedStateForModifierLocked(LockedState::LedState& ledState,
965 int32_t led, int32_t modifier, bool reset) {
966 if (ledState.avail) {
967 bool desiredState = (mLocked.metaState & modifier) != 0;
968 if (ledState.on != desiredState) {
969 getEventHub()->setLedState(getDeviceId(), led, desiredState);
970 ledState.on = desiredState;
971 }
972 }
973}
974
Jeff Browne57e8952010-07-23 21:28:06 -0700975
976// --- TrackballInputMapper ---
977
Jeff Brown66888372010-11-29 17:37:49 -0800978TrackballInputMapper::TrackballInputMapper(InputDevice* device) :
979 InputMapper(device) {
Jeff Browne57e8952010-07-23 21:28:06 -0700980 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
981 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
982 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
983 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
984
Jeff Brownb51719b2010-07-29 18:18:33 -0700985 initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700986}
987
988TrackballInputMapper::~TrackballInputMapper() {
989}
990
991uint32_t TrackballInputMapper::getSources() {
992 return AINPUT_SOURCE_TRACKBALL;
993}
994
995void TrackballInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
996 InputMapper::populateDeviceInfo(info);
997
998 info->addMotionRange(AINPUT_MOTION_RANGE_X, -1.0f, 1.0f, 0.0f, mXScale);
999 info->addMotionRange(AINPUT_MOTION_RANGE_Y, -1.0f, 1.0f, 0.0f, mYScale);
1000}
1001
Jeff Brown26c94ff2010-09-30 14:33:04 -07001002void TrackballInputMapper::dump(String8& dump) {
1003 { // acquire lock
1004 AutoMutex _l(mLock);
1005 dump.append(INDENT2 "Trackball Input Mapper:\n");
Jeff Brown66888372010-11-29 17:37:49 -08001006 dumpParameters(dump);
Jeff Brown26c94ff2010-09-30 14:33:04 -07001007 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1008 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
1009 dump.appendFormat(INDENT3 "Down: %s\n", toString(mLocked.down));
1010 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1011 } // release lock
1012}
1013
Jeff Brown66888372010-11-29 17:37:49 -08001014void TrackballInputMapper::configure() {
1015 InputMapper::configure();
1016
1017 // Configure basic parameters.
1018 configureParameters();
1019}
1020
1021void TrackballInputMapper::configureParameters() {
1022 mParameters.orientationAware = false;
1023 getDevice()->getConfiguration().tryGetProperty(String8("trackball.orientationAware"),
1024 mParameters.orientationAware);
1025
1026 mParameters.associatedDisplayId = mParameters.orientationAware ? 0 : -1;
1027}
1028
1029void TrackballInputMapper::dumpParameters(String8& dump) {
1030 dump.append(INDENT3 "Parameters:\n");
1031 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1032 mParameters.associatedDisplayId);
1033 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1034 toString(mParameters.orientationAware));
1035}
1036
Jeff Brownb51719b2010-07-29 18:18:33 -07001037void TrackballInputMapper::initializeLocked() {
Jeff Browne57e8952010-07-23 21:28:06 -07001038 mAccumulator.clear();
1039
Jeff Brownb51719b2010-07-29 18:18:33 -07001040 mLocked.down = false;
1041 mLocked.downTime = 0;
Jeff Browne57e8952010-07-23 21:28:06 -07001042}
1043
1044void TrackballInputMapper::reset() {
Jeff Brownb51719b2010-07-29 18:18:33 -07001045 for (;;) {
1046 { // acquire lock
1047 AutoMutex _l(mLock);
1048
1049 if (! mLocked.down) {
1050 initializeLocked();
1051 break; // done
1052 }
1053 } // release lock
1054
1055 // Synthesize trackball button up event on reset.
Jeff Browne57e8952010-07-23 21:28:06 -07001056 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownb51719b2010-07-29 18:18:33 -07001057 mAccumulator.fields = Accumulator::FIELD_BTN_MOUSE;
Jeff Browne57e8952010-07-23 21:28:06 -07001058 mAccumulator.btnMouse = false;
1059 sync(when);
Jeff Browne839a582010-04-22 18:58:52 -07001060 }
1061
Jeff Browne57e8952010-07-23 21:28:06 -07001062 InputMapper::reset();
Jeff Browne57e8952010-07-23 21:28:06 -07001063}
Jeff Browne839a582010-04-22 18:58:52 -07001064
Jeff Browne57e8952010-07-23 21:28:06 -07001065void TrackballInputMapper::process(const RawEvent* rawEvent) {
1066 switch (rawEvent->type) {
1067 case EV_KEY:
1068 switch (rawEvent->scanCode) {
1069 case BTN_MOUSE:
1070 mAccumulator.fields |= Accumulator::FIELD_BTN_MOUSE;
1071 mAccumulator.btnMouse = rawEvent->value != 0;
Jeff Brownd64c8552010-08-17 20:38:35 -07001072 // Sync now since BTN_MOUSE is not necessarily followed by SYN_REPORT and
1073 // we need to ensure that we report the up/down promptly.
Jeff Browne57e8952010-07-23 21:28:06 -07001074 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07001075 break;
Jeff Browne839a582010-04-22 18:58:52 -07001076 }
Jeff Browne57e8952010-07-23 21:28:06 -07001077 break;
Jeff Browne839a582010-04-22 18:58:52 -07001078
Jeff Browne57e8952010-07-23 21:28:06 -07001079 case EV_REL:
1080 switch (rawEvent->scanCode) {
1081 case REL_X:
1082 mAccumulator.fields |= Accumulator::FIELD_REL_X;
1083 mAccumulator.relX = rawEvent->value;
1084 break;
1085 case REL_Y:
1086 mAccumulator.fields |= Accumulator::FIELD_REL_Y;
1087 mAccumulator.relY = rawEvent->value;
1088 break;
Jeff Browne839a582010-04-22 18:58:52 -07001089 }
Jeff Browne57e8952010-07-23 21:28:06 -07001090 break;
Jeff Browne839a582010-04-22 18:58:52 -07001091
Jeff Browne57e8952010-07-23 21:28:06 -07001092 case EV_SYN:
1093 switch (rawEvent->scanCode) {
1094 case SYN_REPORT:
Jeff Brownd64c8552010-08-17 20:38:35 -07001095 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07001096 break;
Jeff Browne839a582010-04-22 18:58:52 -07001097 }
Jeff Browne57e8952010-07-23 21:28:06 -07001098 break;
Jeff Browne839a582010-04-22 18:58:52 -07001099 }
Jeff Browne839a582010-04-22 18:58:52 -07001100}
1101
Jeff Browne57e8952010-07-23 21:28:06 -07001102void TrackballInputMapper::sync(nsecs_t when) {
Jeff Brownd64c8552010-08-17 20:38:35 -07001103 uint32_t fields = mAccumulator.fields;
1104 if (fields == 0) {
1105 return; // no new state changes, so nothing to do
1106 }
1107
Jeff Brownb51719b2010-07-29 18:18:33 -07001108 int motionEventAction;
1109 PointerCoords pointerCoords;
1110 nsecs_t downTime;
1111 { // acquire lock
1112 AutoMutex _l(mLock);
Jeff Browne839a582010-04-22 18:58:52 -07001113
Jeff Brownb51719b2010-07-29 18:18:33 -07001114 bool downChanged = fields & Accumulator::FIELD_BTN_MOUSE;
1115
1116 if (downChanged) {
1117 if (mAccumulator.btnMouse) {
1118 mLocked.down = true;
1119 mLocked.downTime = when;
1120 } else {
1121 mLocked.down = false;
1122 }
Jeff Browne57e8952010-07-23 21:28:06 -07001123 }
Jeff Browne839a582010-04-22 18:58:52 -07001124
Jeff Brownb51719b2010-07-29 18:18:33 -07001125 downTime = mLocked.downTime;
1126 float x = fields & Accumulator::FIELD_REL_X ? mAccumulator.relX * mXScale : 0.0f;
1127 float y = fields & Accumulator::FIELD_REL_Y ? mAccumulator.relY * mYScale : 0.0f;
Jeff Browne839a582010-04-22 18:58:52 -07001128
Jeff Brownb51719b2010-07-29 18:18:33 -07001129 if (downChanged) {
1130 motionEventAction = mLocked.down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
Jeff Browne57e8952010-07-23 21:28:06 -07001131 } else {
Jeff Brownb51719b2010-07-29 18:18:33 -07001132 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browne57e8952010-07-23 21:28:06 -07001133 }
Jeff Browne839a582010-04-22 18:58:52 -07001134
Jeff Brownb51719b2010-07-29 18:18:33 -07001135 pointerCoords.x = x;
1136 pointerCoords.y = y;
1137 pointerCoords.pressure = mLocked.down ? 1.0f : 0.0f;
1138 pointerCoords.size = 0;
1139 pointerCoords.touchMajor = 0;
1140 pointerCoords.touchMinor = 0;
1141 pointerCoords.toolMajor = 0;
1142 pointerCoords.toolMinor = 0;
1143 pointerCoords.orientation = 0;
Jeff Browne839a582010-04-22 18:58:52 -07001144
Jeff Brown66888372010-11-29 17:37:49 -08001145 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
1146 && (x != 0.0f || y != 0.0f)) {
Jeff Brownb51719b2010-07-29 18:18:33 -07001147 // Rotate motion based on display orientation if needed.
1148 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1149 int32_t orientation;
Jeff Brown66888372010-11-29 17:37:49 -08001150 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1151 NULL, NULL, & orientation)) {
Jeff Brownd4ecee92010-10-29 22:19:53 -07001152 orientation = InputReaderPolicyInterface::ROTATION_0;
Jeff Brownb51719b2010-07-29 18:18:33 -07001153 }
1154
1155 float temp;
1156 switch (orientation) {
1157 case InputReaderPolicyInterface::ROTATION_90:
1158 temp = pointerCoords.x;
1159 pointerCoords.x = pointerCoords.y;
1160 pointerCoords.y = - temp;
1161 break;
1162
1163 case InputReaderPolicyInterface::ROTATION_180:
1164 pointerCoords.x = - pointerCoords.x;
1165 pointerCoords.y = - pointerCoords.y;
1166 break;
1167
1168 case InputReaderPolicyInterface::ROTATION_270:
1169 temp = pointerCoords.x;
1170 pointerCoords.x = - pointerCoords.y;
1171 pointerCoords.y = temp;
1172 break;
1173 }
1174 }
1175 } // release lock
1176
Jeff Browne57e8952010-07-23 21:28:06 -07001177 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownb51719b2010-07-29 18:18:33 -07001178 int32_t pointerId = 0;
Jeff Brown90f0cee2010-10-08 22:31:17 -07001179 getDispatcher()->notifyMotion(when, getDeviceId(), AINPUT_SOURCE_TRACKBALL, 0,
Jeff Brownaf30ff62010-09-01 17:01:00 -07001180 motionEventAction, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown90f0cee2010-10-08 22:31:17 -07001181 1, &pointerId, &pointerCoords, mXPrecision, mYPrecision, downTime);
1182
1183 mAccumulator.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07001184}
1185
Jeff Brown8d4dfd22010-08-10 15:47:53 -07001186int32_t TrackballInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1187 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
1188 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1189 } else {
1190 return AKEY_STATE_UNKNOWN;
1191 }
1192}
1193
Jeff Browne57e8952010-07-23 21:28:06 -07001194
1195// --- TouchInputMapper ---
1196
Jeff Brown66888372010-11-29 17:37:49 -08001197TouchInputMapper::TouchInputMapper(InputDevice* device) :
1198 InputMapper(device) {
Jeff Brownb51719b2010-07-29 18:18:33 -07001199 mLocked.surfaceOrientation = -1;
1200 mLocked.surfaceWidth = -1;
1201 mLocked.surfaceHeight = -1;
1202
1203 initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -07001204}
1205
1206TouchInputMapper::~TouchInputMapper() {
1207}
1208
1209uint32_t TouchInputMapper::getSources() {
Jeff Brown66888372010-11-29 17:37:49 -08001210 switch (mParameters.deviceType) {
1211 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
1212 return AINPUT_SOURCE_TOUCHSCREEN;
1213 case Parameters::DEVICE_TYPE_TOUCH_PAD:
1214 return AINPUT_SOURCE_TOUCHPAD;
1215 default:
1216 assert(false);
1217 return AINPUT_SOURCE_UNKNOWN;
1218 }
Jeff Browne57e8952010-07-23 21:28:06 -07001219}
1220
1221void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1222 InputMapper::populateDeviceInfo(info);
1223
Jeff Brownb51719b2010-07-29 18:18:33 -07001224 { // acquire lock
1225 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07001226
Jeff Brownb51719b2010-07-29 18:18:33 -07001227 // Ensure surface information is up to date so that orientation changes are
1228 // noticed immediately.
1229 configureSurfaceLocked();
1230
1231 info->addMotionRange(AINPUT_MOTION_RANGE_X, mLocked.orientedRanges.x);
1232 info->addMotionRange(AINPUT_MOTION_RANGE_Y, mLocked.orientedRanges.y);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001233
1234 if (mLocked.orientedRanges.havePressure) {
1235 info->addMotionRange(AINPUT_MOTION_RANGE_PRESSURE,
1236 mLocked.orientedRanges.pressure);
1237 }
1238
1239 if (mLocked.orientedRanges.haveSize) {
1240 info->addMotionRange(AINPUT_MOTION_RANGE_SIZE,
1241 mLocked.orientedRanges.size);
1242 }
1243
Jeff Brown6b337e72010-10-14 21:42:15 -07001244 if (mLocked.orientedRanges.haveTouchSize) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001245 info->addMotionRange(AINPUT_MOTION_RANGE_TOUCH_MAJOR,
1246 mLocked.orientedRanges.touchMajor);
1247 info->addMotionRange(AINPUT_MOTION_RANGE_TOUCH_MINOR,
1248 mLocked.orientedRanges.touchMinor);
1249 }
1250
Jeff Brown6b337e72010-10-14 21:42:15 -07001251 if (mLocked.orientedRanges.haveToolSize) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001252 info->addMotionRange(AINPUT_MOTION_RANGE_TOOL_MAJOR,
1253 mLocked.orientedRanges.toolMajor);
1254 info->addMotionRange(AINPUT_MOTION_RANGE_TOOL_MINOR,
1255 mLocked.orientedRanges.toolMinor);
1256 }
1257
1258 if (mLocked.orientedRanges.haveOrientation) {
1259 info->addMotionRange(AINPUT_MOTION_RANGE_ORIENTATION,
1260 mLocked.orientedRanges.orientation);
1261 }
Jeff Brownb51719b2010-07-29 18:18:33 -07001262 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07001263}
1264
Jeff Brown26c94ff2010-09-30 14:33:04 -07001265void TouchInputMapper::dump(String8& dump) {
1266 { // acquire lock
1267 AutoMutex _l(mLock);
1268 dump.append(INDENT2 "Touch Input Mapper:\n");
Jeff Brown26c94ff2010-09-30 14:33:04 -07001269 dumpParameters(dump);
1270 dumpVirtualKeysLocked(dump);
1271 dumpRawAxes(dump);
1272 dumpCalibration(dump);
1273 dumpSurfaceLocked(dump);
Jeff Brown60b57762010-10-18 13:32:20 -07001274 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
Jeff Brown6b337e72010-10-14 21:42:15 -07001275 dump.appendFormat(INDENT4 "XOrigin: %d\n", mLocked.xOrigin);
1276 dump.appendFormat(INDENT4 "YOrigin: %d\n", mLocked.yOrigin);
1277 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mLocked.xScale);
1278 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mLocked.yScale);
1279 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mLocked.xPrecision);
1280 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mLocked.yPrecision);
1281 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mLocked.geometricScale);
1282 dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mLocked.toolSizeLinearScale);
1283 dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mLocked.toolSizeLinearBias);
1284 dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mLocked.toolSizeAreaScale);
1285 dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mLocked.toolSizeAreaBias);
1286 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mLocked.pressureScale);
1287 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mLocked.sizeScale);
1288 dump.appendFormat(INDENT4 "OrientationSCale: %0.3f\n", mLocked.orientationScale);
Jeff Brown26c94ff2010-09-30 14:33:04 -07001289 } // release lock
1290}
1291
Jeff Brownb51719b2010-07-29 18:18:33 -07001292void TouchInputMapper::initializeLocked() {
1293 mCurrentTouch.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07001294 mLastTouch.clear();
1295 mDownTime = 0;
Jeff Browne57e8952010-07-23 21:28:06 -07001296
1297 for (uint32_t i = 0; i < MAX_POINTERS; i++) {
1298 mAveragingTouchFilter.historyStart[i] = 0;
1299 mAveragingTouchFilter.historyEnd[i] = 0;
1300 }
1301
1302 mJumpyTouchFilter.jumpyPointsDropped = 0;
Jeff Brownb51719b2010-07-29 18:18:33 -07001303
1304 mLocked.currentVirtualKey.down = false;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001305
1306 mLocked.orientedRanges.havePressure = false;
1307 mLocked.orientedRanges.haveSize = false;
Jeff Brown6b337e72010-10-14 21:42:15 -07001308 mLocked.orientedRanges.haveTouchSize = false;
1309 mLocked.orientedRanges.haveToolSize = false;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001310 mLocked.orientedRanges.haveOrientation = false;
1311}
1312
Jeff Browne57e8952010-07-23 21:28:06 -07001313void TouchInputMapper::configure() {
1314 InputMapper::configure();
1315
1316 // Configure basic parameters.
Jeff Brown38a7fab2010-08-30 03:02:23 -07001317 configureParameters();
Jeff Browne57e8952010-07-23 21:28:06 -07001318
1319 // Configure absolute axis information.
Jeff Brown38a7fab2010-08-30 03:02:23 -07001320 configureRawAxes();
Jeff Brown38a7fab2010-08-30 03:02:23 -07001321
1322 // Prepare input device calibration.
1323 parseCalibration();
1324 resolveCalibration();
Jeff Browne57e8952010-07-23 21:28:06 -07001325
Jeff Brownb51719b2010-07-29 18:18:33 -07001326 { // acquire lock
1327 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07001328
Jeff Brown38a7fab2010-08-30 03:02:23 -07001329 // Configure surface dimensions and orientation.
Jeff Brownb51719b2010-07-29 18:18:33 -07001330 configureSurfaceLocked();
1331 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07001332}
1333
Jeff Brown38a7fab2010-08-30 03:02:23 -07001334void TouchInputMapper::configureParameters() {
1335 mParameters.useBadTouchFilter = getPolicy()->filterTouchEvents();
1336 mParameters.useAveragingTouchFilter = getPolicy()->filterTouchEvents();
1337 mParameters.useJumpyTouchFilter = getPolicy()->filterJumpyTouchEvents();
Jeff Brown66888372010-11-29 17:37:49 -08001338
1339 String8 deviceTypeString;
1340 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
1341 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
1342 deviceTypeString)) {
1343 if (deviceTypeString == "touchPad") {
1344 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
1345 } else if (deviceTypeString != "touchScreen") {
1346 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
1347 }
1348 }
1349 bool isTouchScreen = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
1350
1351 mParameters.orientationAware = isTouchScreen;
1352 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
1353 mParameters.orientationAware);
1354
1355 mParameters.associatedDisplayId = mParameters.orientationAware || isTouchScreen ? 0 : -1;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001356}
1357
Jeff Brown26c94ff2010-09-30 14:33:04 -07001358void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown66888372010-11-29 17:37:49 -08001359 dump.append(INDENT3 "Parameters:\n");
1360
1361 switch (mParameters.deviceType) {
1362 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
1363 dump.append(INDENT4 "DeviceType: touchScreen\n");
1364 break;
1365 case Parameters::DEVICE_TYPE_TOUCH_PAD:
1366 dump.append(INDENT4 "DeviceType: touchPad\n");
1367 break;
1368 default:
1369 assert(false);
1370 }
1371
1372 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1373 mParameters.associatedDisplayId);
1374 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1375 toString(mParameters.orientationAware));
1376
1377 dump.appendFormat(INDENT4 "UseBadTouchFilter: %s\n",
Jeff Brown26c94ff2010-09-30 14:33:04 -07001378 toString(mParameters.useBadTouchFilter));
Jeff Brown66888372010-11-29 17:37:49 -08001379 dump.appendFormat(INDENT4 "UseAveragingTouchFilter: %s\n",
Jeff Brown26c94ff2010-09-30 14:33:04 -07001380 toString(mParameters.useAveragingTouchFilter));
Jeff Brown66888372010-11-29 17:37:49 -08001381 dump.appendFormat(INDENT4 "UseJumpyTouchFilter: %s\n",
Jeff Brown26c94ff2010-09-30 14:33:04 -07001382 toString(mParameters.useJumpyTouchFilter));
Jeff Browna665ca82010-09-08 11:49:43 -07001383}
1384
Jeff Brown38a7fab2010-08-30 03:02:23 -07001385void TouchInputMapper::configureRawAxes() {
1386 mRawAxes.x.clear();
1387 mRawAxes.y.clear();
1388 mRawAxes.pressure.clear();
1389 mRawAxes.touchMajor.clear();
1390 mRawAxes.touchMinor.clear();
1391 mRawAxes.toolMajor.clear();
1392 mRawAxes.toolMinor.clear();
1393 mRawAxes.orientation.clear();
1394}
1395
Jeff Brown26c94ff2010-09-30 14:33:04 -07001396static void dumpAxisInfo(String8& dump, RawAbsoluteAxisInfo axis, const char* name) {
Jeff Browna665ca82010-09-08 11:49:43 -07001397 if (axis.valid) {
Jeff Brown26c94ff2010-09-30 14:33:04 -07001398 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d\n",
Jeff Browna665ca82010-09-08 11:49:43 -07001399 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz);
1400 } else {
Jeff Brown26c94ff2010-09-30 14:33:04 -07001401 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
Jeff Browna665ca82010-09-08 11:49:43 -07001402 }
1403}
1404
Jeff Brown26c94ff2010-09-30 14:33:04 -07001405void TouchInputMapper::dumpRawAxes(String8& dump) {
1406 dump.append(INDENT3 "Raw Axes:\n");
1407 dumpAxisInfo(dump, mRawAxes.x, "X");
1408 dumpAxisInfo(dump, mRawAxes.y, "Y");
1409 dumpAxisInfo(dump, mRawAxes.pressure, "Pressure");
1410 dumpAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor");
1411 dumpAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor");
1412 dumpAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor");
1413 dumpAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor");
1414 dumpAxisInfo(dump, mRawAxes.orientation, "Orientation");
Jeff Browne57e8952010-07-23 21:28:06 -07001415}
1416
Jeff Brownb51719b2010-07-29 18:18:33 -07001417bool TouchInputMapper::configureSurfaceLocked() {
Jeff Browne57e8952010-07-23 21:28:06 -07001418 // Update orientation and dimensions if needed.
Jeff Brown66888372010-11-29 17:37:49 -08001419 int32_t orientation = InputReaderPolicyInterface::ROTATION_0;
1420 int32_t width = mRawAxes.x.getRange();
1421 int32_t height = mRawAxes.y.getRange();
1422
1423 if (mParameters.associatedDisplayId >= 0) {
1424 bool wantSize = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
1425 bool wantOrientation = mParameters.orientationAware;
1426
Jeff Brownb51719b2010-07-29 18:18:33 -07001427 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown66888372010-11-29 17:37:49 -08001428 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1429 wantSize ? &width : NULL, wantSize ? &height : NULL,
1430 wantOrientation ? &orientation : NULL)) {
Jeff Browne57e8952010-07-23 21:28:06 -07001431 return false;
1432 }
Jeff Browne57e8952010-07-23 21:28:06 -07001433 }
1434
Jeff Brownb51719b2010-07-29 18:18:33 -07001435 bool orientationChanged = mLocked.surfaceOrientation != orientation;
Jeff Browne57e8952010-07-23 21:28:06 -07001436 if (orientationChanged) {
Jeff Brownb51719b2010-07-29 18:18:33 -07001437 mLocked.surfaceOrientation = orientation;
Jeff Browne57e8952010-07-23 21:28:06 -07001438 }
1439
Jeff Brownb51719b2010-07-29 18:18:33 -07001440 bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height;
Jeff Browne57e8952010-07-23 21:28:06 -07001441 if (sizeChanged) {
Jeff Brown26c94ff2010-09-30 14:33:04 -07001442 LOGI("Device reconfigured: id=0x%x, name=%s, display size is now %dx%d",
1443 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001444
Jeff Brownb51719b2010-07-29 18:18:33 -07001445 mLocked.surfaceWidth = width;
1446 mLocked.surfaceHeight = height;
Jeff Browne57e8952010-07-23 21:28:06 -07001447
Jeff Brown38a7fab2010-08-30 03:02:23 -07001448 // Configure X and Y factors.
1449 if (mRawAxes.x.valid && mRawAxes.y.valid) {
Jeff Brown60b57762010-10-18 13:32:20 -07001450 mLocked.xOrigin = mCalibration.haveXOrigin
1451 ? mCalibration.xOrigin
1452 : mRawAxes.x.minValue;
1453 mLocked.yOrigin = mCalibration.haveYOrigin
1454 ? mCalibration.yOrigin
1455 : mRawAxes.y.minValue;
1456 mLocked.xScale = mCalibration.haveXScale
1457 ? mCalibration.xScale
1458 : float(width) / mRawAxes.x.getRange();
1459 mLocked.yScale = mCalibration.haveYScale
1460 ? mCalibration.yScale
1461 : float(height) / mRawAxes.y.getRange();
Jeff Brownb51719b2010-07-29 18:18:33 -07001462 mLocked.xPrecision = 1.0f / mLocked.xScale;
1463 mLocked.yPrecision = 1.0f / mLocked.yScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001464
Jeff Brownb51719b2010-07-29 18:18:33 -07001465 configureVirtualKeysLocked();
Jeff Browne57e8952010-07-23 21:28:06 -07001466 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001467 LOGW(INDENT "Touch device did not report support for X or Y axis!");
Jeff Brownb51719b2010-07-29 18:18:33 -07001468 mLocked.xOrigin = 0;
1469 mLocked.yOrigin = 0;
1470 mLocked.xScale = 1.0f;
1471 mLocked.yScale = 1.0f;
1472 mLocked.xPrecision = 1.0f;
1473 mLocked.yPrecision = 1.0f;
Jeff Browne57e8952010-07-23 21:28:06 -07001474 }
1475
Jeff Brown38a7fab2010-08-30 03:02:23 -07001476 // Scale factor for terms that are not oriented in a particular axis.
1477 // If the pixels are square then xScale == yScale otherwise we fake it
1478 // by choosing an average.
1479 mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale);
Jeff Browne57e8952010-07-23 21:28:06 -07001480
Jeff Brown38a7fab2010-08-30 03:02:23 -07001481 // Size of diagonal axis.
1482 float diagonalSize = pythag(width, height);
Jeff Browne57e8952010-07-23 21:28:06 -07001483
Jeff Brown38a7fab2010-08-30 03:02:23 -07001484 // TouchMajor and TouchMinor factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001485 if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) {
1486 mLocked.orientedRanges.haveTouchSize = true;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001487 mLocked.orientedRanges.touchMajor.min = 0;
1488 mLocked.orientedRanges.touchMajor.max = diagonalSize;
1489 mLocked.orientedRanges.touchMajor.flat = 0;
1490 mLocked.orientedRanges.touchMajor.fuzz = 0;
1491 mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor;
1492 }
Jeff Brownb51719b2010-07-29 18:18:33 -07001493
Jeff Brown38a7fab2010-08-30 03:02:23 -07001494 // ToolMajor and ToolMinor factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001495 mLocked.toolSizeLinearScale = 0;
1496 mLocked.toolSizeLinearBias = 0;
1497 mLocked.toolSizeAreaScale = 0;
1498 mLocked.toolSizeAreaBias = 0;
1499 if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
1500 if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) {
1501 if (mCalibration.haveToolSizeLinearScale) {
1502 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001503 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
Jeff Brown6b337e72010-10-14 21:42:15 -07001504 mLocked.toolSizeLinearScale = float(min(width, height))
Jeff Brown38a7fab2010-08-30 03:02:23 -07001505 / mRawAxes.toolMajor.maxValue;
1506 }
1507
Jeff Brown6b337e72010-10-14 21:42:15 -07001508 if (mCalibration.haveToolSizeLinearBias) {
1509 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
1510 }
1511 } else if (mCalibration.toolSizeCalibration ==
1512 Calibration::TOOL_SIZE_CALIBRATION_AREA) {
1513 if (mCalibration.haveToolSizeLinearScale) {
1514 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
1515 } else {
1516 mLocked.toolSizeLinearScale = min(width, height);
1517 }
1518
1519 if (mCalibration.haveToolSizeLinearBias) {
1520 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
1521 }
1522
1523 if (mCalibration.haveToolSizeAreaScale) {
1524 mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale;
1525 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1526 mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue;
1527 }
1528
1529 if (mCalibration.haveToolSizeAreaBias) {
1530 mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001531 }
1532 }
1533
Jeff Brown6b337e72010-10-14 21:42:15 -07001534 mLocked.orientedRanges.haveToolSize = true;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001535 mLocked.orientedRanges.toolMajor.min = 0;
1536 mLocked.orientedRanges.toolMajor.max = diagonalSize;
1537 mLocked.orientedRanges.toolMajor.flat = 0;
1538 mLocked.orientedRanges.toolMajor.fuzz = 0;
1539 mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor;
1540 }
1541
1542 // Pressure factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001543 mLocked.pressureScale = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001544 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
1545 RawAbsoluteAxisInfo rawPressureAxis;
1546 switch (mCalibration.pressureSource) {
1547 case Calibration::PRESSURE_SOURCE_PRESSURE:
1548 rawPressureAxis = mRawAxes.pressure;
1549 break;
1550 case Calibration::PRESSURE_SOURCE_TOUCH:
1551 rawPressureAxis = mRawAxes.touchMajor;
1552 break;
1553 default:
1554 rawPressureAxis.clear();
1555 }
1556
Jeff Brown38a7fab2010-08-30 03:02:23 -07001557 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
1558 || mCalibration.pressureCalibration
1559 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
1560 if (mCalibration.havePressureScale) {
1561 mLocked.pressureScale = mCalibration.pressureScale;
1562 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
1563 mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue;
1564 }
1565 }
1566
1567 mLocked.orientedRanges.havePressure = true;
1568 mLocked.orientedRanges.pressure.min = 0;
1569 mLocked.orientedRanges.pressure.max = 1.0;
1570 mLocked.orientedRanges.pressure.flat = 0;
1571 mLocked.orientedRanges.pressure.fuzz = 0;
1572 }
1573
1574 // Size factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001575 mLocked.sizeScale = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001576 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001577 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
1578 if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1579 mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue;
1580 }
1581 }
1582
1583 mLocked.orientedRanges.haveSize = true;
1584 mLocked.orientedRanges.size.min = 0;
1585 mLocked.orientedRanges.size.max = 1.0;
1586 mLocked.orientedRanges.size.flat = 0;
1587 mLocked.orientedRanges.size.fuzz = 0;
1588 }
1589
1590 // Orientation
Jeff Brown6b337e72010-10-14 21:42:15 -07001591 mLocked.orientationScale = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001592 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001593 if (mCalibration.orientationCalibration
1594 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
1595 if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) {
1596 mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue;
1597 }
1598 }
1599
1600 mLocked.orientedRanges.orientation.min = - M_PI_2;
1601 mLocked.orientedRanges.orientation.max = M_PI_2;
1602 mLocked.orientedRanges.orientation.flat = 0;
1603 mLocked.orientedRanges.orientation.fuzz = 0;
1604 }
Jeff Browne57e8952010-07-23 21:28:06 -07001605 }
1606
1607 if (orientationChanged || sizeChanged) {
1608 // Compute oriented surface dimensions, precision, and scales.
1609 float orientedXScale, orientedYScale;
Jeff Brownb51719b2010-07-29 18:18:33 -07001610 switch (mLocked.surfaceOrientation) {
Jeff Browne57e8952010-07-23 21:28:06 -07001611 case InputReaderPolicyInterface::ROTATION_90:
1612 case InputReaderPolicyInterface::ROTATION_270:
Jeff Brownb51719b2010-07-29 18:18:33 -07001613 mLocked.orientedSurfaceWidth = mLocked.surfaceHeight;
1614 mLocked.orientedSurfaceHeight = mLocked.surfaceWidth;
1615 mLocked.orientedXPrecision = mLocked.yPrecision;
1616 mLocked.orientedYPrecision = mLocked.xPrecision;
1617 orientedXScale = mLocked.yScale;
1618 orientedYScale = mLocked.xScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001619 break;
1620 default:
Jeff Brownb51719b2010-07-29 18:18:33 -07001621 mLocked.orientedSurfaceWidth = mLocked.surfaceWidth;
1622 mLocked.orientedSurfaceHeight = mLocked.surfaceHeight;
1623 mLocked.orientedXPrecision = mLocked.xPrecision;
1624 mLocked.orientedYPrecision = mLocked.yPrecision;
1625 orientedXScale = mLocked.xScale;
1626 orientedYScale = mLocked.yScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001627 break;
1628 }
1629
1630 // Configure position ranges.
Jeff Brownb51719b2010-07-29 18:18:33 -07001631 mLocked.orientedRanges.x.min = 0;
1632 mLocked.orientedRanges.x.max = mLocked.orientedSurfaceWidth;
1633 mLocked.orientedRanges.x.flat = 0;
1634 mLocked.orientedRanges.x.fuzz = orientedXScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001635
Jeff Brownb51719b2010-07-29 18:18:33 -07001636 mLocked.orientedRanges.y.min = 0;
1637 mLocked.orientedRanges.y.max = mLocked.orientedSurfaceHeight;
1638 mLocked.orientedRanges.y.flat = 0;
1639 mLocked.orientedRanges.y.fuzz = orientedYScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001640 }
1641
1642 return true;
1643}
1644
Jeff Brown26c94ff2010-09-30 14:33:04 -07001645void TouchInputMapper::dumpSurfaceLocked(String8& dump) {
1646 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth);
1647 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight);
1648 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation);
Jeff Browna665ca82010-09-08 11:49:43 -07001649}
1650
Jeff Brownb51719b2010-07-29 18:18:33 -07001651void TouchInputMapper::configureVirtualKeysLocked() {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001652 assert(mRawAxes.x.valid && mRawAxes.y.valid);
Jeff Browne57e8952010-07-23 21:28:06 -07001653
Jeff Brownb51719b2010-07-29 18:18:33 -07001654 // Note: getVirtualKeyDefinitions is non-reentrant so we can continue holding the lock.
Jeff Brown38a7fab2010-08-30 03:02:23 -07001655 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Browne57e8952010-07-23 21:28:06 -07001656 getPolicy()->getVirtualKeyDefinitions(getDeviceName(), virtualKeyDefinitions);
1657
Jeff Brownb51719b2010-07-29 18:18:33 -07001658 mLocked.virtualKeys.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07001659
Jeff Brownb51719b2010-07-29 18:18:33 -07001660 if (virtualKeyDefinitions.size() == 0) {
1661 return;
1662 }
Jeff Browne57e8952010-07-23 21:28:06 -07001663
Jeff Brownb51719b2010-07-29 18:18:33 -07001664 mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size());
1665
Jeff Brown38a7fab2010-08-30 03:02:23 -07001666 int32_t touchScreenLeft = mRawAxes.x.minValue;
1667 int32_t touchScreenTop = mRawAxes.y.minValue;
1668 int32_t touchScreenWidth = mRawAxes.x.getRange();
1669 int32_t touchScreenHeight = mRawAxes.y.getRange();
Jeff Brownb51719b2010-07-29 18:18:33 -07001670
1671 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001672 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brownb51719b2010-07-29 18:18:33 -07001673 virtualKeyDefinitions[i];
1674
1675 mLocked.virtualKeys.add();
1676 VirtualKey& virtualKey = mLocked.virtualKeys.editTop();
1677
1678 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1679 int32_t keyCode;
1680 uint32_t flags;
1681 if (getEventHub()->scancodeToKeycode(getDeviceId(), virtualKey.scanCode,
1682 & keyCode, & flags)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001683 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
1684 virtualKey.scanCode);
Jeff Brownb51719b2010-07-29 18:18:33 -07001685 mLocked.virtualKeys.pop(); // drop the key
1686 continue;
Jeff Browne57e8952010-07-23 21:28:06 -07001687 }
1688
Jeff Brownb51719b2010-07-29 18:18:33 -07001689 virtualKey.keyCode = keyCode;
1690 virtualKey.flags = flags;
Jeff Browne57e8952010-07-23 21:28:06 -07001691
Jeff Brownb51719b2010-07-29 18:18:33 -07001692 // convert the key definition's display coordinates into touch coordinates for a hit box
1693 int32_t halfWidth = virtualKeyDefinition.width / 2;
1694 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Browne57e8952010-07-23 21:28:06 -07001695
Jeff Brownb51719b2010-07-29 18:18:33 -07001696 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
1697 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1698 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
1699 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1700 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
1701 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
1702 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
1703 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
Jeff Browne57e8952010-07-23 21:28:06 -07001704
Jeff Brown26c94ff2010-09-30 14:33:04 -07001705 }
1706}
1707
1708void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) {
1709 if (!mLocked.virtualKeys.isEmpty()) {
1710 dump.append(INDENT3 "Virtual Keys:\n");
1711
1712 for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) {
1713 const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i);
1714 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
1715 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1716 i, virtualKey.scanCode, virtualKey.keyCode,
1717 virtualKey.hitLeft, virtualKey.hitRight,
1718 virtualKey.hitTop, virtualKey.hitBottom);
1719 }
Jeff Brownb51719b2010-07-29 18:18:33 -07001720 }
Jeff Browne57e8952010-07-23 21:28:06 -07001721}
1722
Jeff Brown38a7fab2010-08-30 03:02:23 -07001723void TouchInputMapper::parseCalibration() {
Jeff Brown66888372010-11-29 17:37:49 -08001724 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown38a7fab2010-08-30 03:02:23 -07001725 Calibration& out = mCalibration;
1726
Jeff Brown60b57762010-10-18 13:32:20 -07001727 // Position
1728 out.haveXOrigin = in.tryGetProperty(String8("touch.position.xOrigin"), out.xOrigin);
1729 out.haveYOrigin = in.tryGetProperty(String8("touch.position.yOrigin"), out.yOrigin);
1730 out.haveXScale = in.tryGetProperty(String8("touch.position.xScale"), out.xScale);
1731 out.haveYScale = in.tryGetProperty(String8("touch.position.yScale"), out.yScale);
1732
Jeff Brown6b337e72010-10-14 21:42:15 -07001733 // Touch Size
1734 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT;
1735 String8 touchSizeCalibrationString;
1736 if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) {
1737 if (touchSizeCalibrationString == "none") {
1738 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
1739 } else if (touchSizeCalibrationString == "geometric") {
1740 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC;
1741 } else if (touchSizeCalibrationString == "pressure") {
1742 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
1743 } else if (touchSizeCalibrationString != "default") {
1744 LOGW("Invalid value for touch.touchSize.calibration: '%s'",
1745 touchSizeCalibrationString.string());
Jeff Brown38a7fab2010-08-30 03:02:23 -07001746 }
1747 }
1748
Jeff Brown6b337e72010-10-14 21:42:15 -07001749 // Tool Size
1750 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT;
1751 String8 toolSizeCalibrationString;
1752 if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) {
1753 if (toolSizeCalibrationString == "none") {
1754 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
1755 } else if (toolSizeCalibrationString == "geometric") {
1756 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC;
1757 } else if (toolSizeCalibrationString == "linear") {
1758 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
1759 } else if (toolSizeCalibrationString == "area") {
1760 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA;
1761 } else if (toolSizeCalibrationString != "default") {
1762 LOGW("Invalid value for touch.toolSize.calibration: '%s'",
1763 toolSizeCalibrationString.string());
Jeff Brown38a7fab2010-08-30 03:02:23 -07001764 }
1765 }
1766
Jeff Brown6b337e72010-10-14 21:42:15 -07001767 out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"),
1768 out.toolSizeLinearScale);
1769 out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"),
1770 out.toolSizeLinearBias);
1771 out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"),
1772 out.toolSizeAreaScale);
1773 out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"),
1774 out.toolSizeAreaBias);
1775 out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"),
1776 out.toolSizeIsSummed);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001777
1778 // Pressure
1779 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
1780 String8 pressureCalibrationString;
Jeff Brown6b337e72010-10-14 21:42:15 -07001781 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001782 if (pressureCalibrationString == "none") {
1783 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
1784 } else if (pressureCalibrationString == "physical") {
1785 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
1786 } else if (pressureCalibrationString == "amplitude") {
1787 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
1788 } else if (pressureCalibrationString != "default") {
Jeff Brown6b337e72010-10-14 21:42:15 -07001789 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown38a7fab2010-08-30 03:02:23 -07001790 pressureCalibrationString.string());
1791 }
1792 }
1793
1794 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
1795 String8 pressureSourceString;
1796 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
1797 if (pressureSourceString == "pressure") {
1798 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
1799 } else if (pressureSourceString == "touch") {
1800 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
1801 } else if (pressureSourceString != "default") {
1802 LOGW("Invalid value for touch.pressure.source: '%s'",
1803 pressureSourceString.string());
1804 }
1805 }
1806
1807 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
1808 out.pressureScale);
1809
1810 // Size
1811 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
1812 String8 sizeCalibrationString;
Jeff Brown6b337e72010-10-14 21:42:15 -07001813 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001814 if (sizeCalibrationString == "none") {
1815 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
1816 } else if (sizeCalibrationString == "normalized") {
1817 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
1818 } else if (sizeCalibrationString != "default") {
Jeff Brown6b337e72010-10-14 21:42:15 -07001819 LOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Brown38a7fab2010-08-30 03:02:23 -07001820 sizeCalibrationString.string());
1821 }
1822 }
1823
1824 // Orientation
1825 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
1826 String8 orientationCalibrationString;
Jeff Brown6b337e72010-10-14 21:42:15 -07001827 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001828 if (orientationCalibrationString == "none") {
1829 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
1830 } else if (orientationCalibrationString == "interpolated") {
1831 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
1832 } else if (orientationCalibrationString != "default") {
Jeff Brown6b337e72010-10-14 21:42:15 -07001833 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown38a7fab2010-08-30 03:02:23 -07001834 orientationCalibrationString.string());
1835 }
1836 }
1837}
1838
1839void TouchInputMapper::resolveCalibration() {
1840 // Pressure
1841 switch (mCalibration.pressureSource) {
1842 case Calibration::PRESSURE_SOURCE_DEFAULT:
1843 if (mRawAxes.pressure.valid) {
1844 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
1845 } else if (mRawAxes.touchMajor.valid) {
1846 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
1847 }
1848 break;
1849
1850 case Calibration::PRESSURE_SOURCE_PRESSURE:
1851 if (! mRawAxes.pressure.valid) {
1852 LOGW("Calibration property touch.pressure.source is 'pressure' but "
1853 "the pressure axis is not available.");
1854 }
1855 break;
1856
1857 case Calibration::PRESSURE_SOURCE_TOUCH:
1858 if (! mRawAxes.touchMajor.valid) {
1859 LOGW("Calibration property touch.pressure.source is 'touch' but "
1860 "the touchMajor axis is not available.");
1861 }
1862 break;
1863
1864 default:
1865 break;
1866 }
1867
1868 switch (mCalibration.pressureCalibration) {
1869 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
1870 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
1871 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
1872 } else {
1873 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
1874 }
1875 break;
1876
1877 default:
1878 break;
1879 }
1880
Jeff Brown6b337e72010-10-14 21:42:15 -07001881 // Tool Size
1882 switch (mCalibration.toolSizeCalibration) {
1883 case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT:
Jeff Brown38a7fab2010-08-30 03:02:23 -07001884 if (mRawAxes.toolMajor.valid) {
Jeff Brown6b337e72010-10-14 21:42:15 -07001885 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001886 } else {
Jeff Brown6b337e72010-10-14 21:42:15 -07001887 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001888 }
1889 break;
1890
1891 default:
1892 break;
1893 }
1894
Jeff Brown6b337e72010-10-14 21:42:15 -07001895 // Touch Size
1896 switch (mCalibration.touchSizeCalibration) {
1897 case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT:
Jeff Brown38a7fab2010-08-30 03:02:23 -07001898 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
Jeff Brown6b337e72010-10-14 21:42:15 -07001899 && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
1900 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001901 } else {
Jeff Brown6b337e72010-10-14 21:42:15 -07001902 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001903 }
1904 break;
1905
1906 default:
1907 break;
1908 }
1909
1910 // Size
1911 switch (mCalibration.sizeCalibration) {
1912 case Calibration::SIZE_CALIBRATION_DEFAULT:
1913 if (mRawAxes.toolMajor.valid) {
1914 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
1915 } else {
1916 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
1917 }
1918 break;
1919
1920 default:
1921 break;
1922 }
1923
1924 // Orientation
1925 switch (mCalibration.orientationCalibration) {
1926 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
1927 if (mRawAxes.orientation.valid) {
1928 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
1929 } else {
1930 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
1931 }
1932 break;
1933
1934 default:
1935 break;
1936 }
1937}
1938
Jeff Brown26c94ff2010-09-30 14:33:04 -07001939void TouchInputMapper::dumpCalibration(String8& dump) {
1940 dump.append(INDENT3 "Calibration:\n");
Jeff Browna665ca82010-09-08 11:49:43 -07001941
Jeff Brown60b57762010-10-18 13:32:20 -07001942 // Position
1943 if (mCalibration.haveXOrigin) {
1944 dump.appendFormat(INDENT4 "touch.position.xOrigin: %d\n", mCalibration.xOrigin);
1945 }
1946 if (mCalibration.haveYOrigin) {
1947 dump.appendFormat(INDENT4 "touch.position.yOrigin: %d\n", mCalibration.yOrigin);
1948 }
1949 if (mCalibration.haveXScale) {
1950 dump.appendFormat(INDENT4 "touch.position.xScale: %0.3f\n", mCalibration.xScale);
1951 }
1952 if (mCalibration.haveYScale) {
1953 dump.appendFormat(INDENT4 "touch.position.yScale: %0.3f\n", mCalibration.yScale);
1954 }
1955
Jeff Brown6b337e72010-10-14 21:42:15 -07001956 // Touch Size
1957 switch (mCalibration.touchSizeCalibration) {
1958 case Calibration::TOUCH_SIZE_CALIBRATION_NONE:
1959 dump.append(INDENT4 "touch.touchSize.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001960 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001961 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
1962 dump.append(INDENT4 "touch.touchSize.calibration: geometric\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001963 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001964 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
1965 dump.append(INDENT4 "touch.touchSize.calibration: pressure\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001966 break;
1967 default:
1968 assert(false);
1969 }
1970
Jeff Brown6b337e72010-10-14 21:42:15 -07001971 // Tool Size
1972 switch (mCalibration.toolSizeCalibration) {
1973 case Calibration::TOOL_SIZE_CALIBRATION_NONE:
1974 dump.append(INDENT4 "touch.toolSize.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001975 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001976 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
1977 dump.append(INDENT4 "touch.toolSize.calibration: geometric\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001978 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001979 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
1980 dump.append(INDENT4 "touch.toolSize.calibration: linear\n");
1981 break;
1982 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
1983 dump.append(INDENT4 "touch.toolSize.calibration: area\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001984 break;
1985 default:
1986 assert(false);
1987 }
1988
Jeff Brown6b337e72010-10-14 21:42:15 -07001989 if (mCalibration.haveToolSizeLinearScale) {
1990 dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n",
1991 mCalibration.toolSizeLinearScale);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001992 }
1993
Jeff Brown6b337e72010-10-14 21:42:15 -07001994 if (mCalibration.haveToolSizeLinearBias) {
1995 dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n",
1996 mCalibration.toolSizeLinearBias);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001997 }
1998
Jeff Brown6b337e72010-10-14 21:42:15 -07001999 if (mCalibration.haveToolSizeAreaScale) {
2000 dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n",
2001 mCalibration.toolSizeAreaScale);
2002 }
2003
2004 if (mCalibration.haveToolSizeAreaBias) {
2005 dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n",
2006 mCalibration.toolSizeAreaBias);
2007 }
2008
2009 if (mCalibration.haveToolSizeIsSummed) {
Jeff Brown53c16642010-11-18 20:53:46 -08002010 dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %s\n",
Jeff Brown66888372010-11-29 17:37:49 -08002011 toString(mCalibration.toolSizeIsSummed));
Jeff Brown38a7fab2010-08-30 03:02:23 -07002012 }
2013
2014 // Pressure
2015 switch (mCalibration.pressureCalibration) {
2016 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002017 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002018 break;
2019 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002020 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002021 break;
2022 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002023 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002024 break;
2025 default:
2026 assert(false);
2027 }
2028
2029 switch (mCalibration.pressureSource) {
2030 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002031 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002032 break;
2033 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002034 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002035 break;
2036 case Calibration::PRESSURE_SOURCE_DEFAULT:
2037 break;
2038 default:
2039 assert(false);
2040 }
2041
2042 if (mCalibration.havePressureScale) {
Jeff Brown26c94ff2010-09-30 14:33:04 -07002043 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
2044 mCalibration.pressureScale);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002045 }
2046
2047 // Size
2048 switch (mCalibration.sizeCalibration) {
2049 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002050 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002051 break;
2052 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002053 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002054 break;
2055 default:
2056 assert(false);
2057 }
2058
2059 // Orientation
2060 switch (mCalibration.orientationCalibration) {
2061 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002062 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002063 break;
2064 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002065 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002066 break;
2067 default:
2068 assert(false);
2069 }
2070}
2071
Jeff Browne57e8952010-07-23 21:28:06 -07002072void TouchInputMapper::reset() {
2073 // Synthesize touch up event if touch is currently down.
2074 // This will also take care of finishing virtual key processing if needed.
2075 if (mLastTouch.pointerCount != 0) {
2076 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
2077 mCurrentTouch.clear();
2078 syncTouch(when, true);
2079 }
2080
Jeff Brownb51719b2010-07-29 18:18:33 -07002081 { // acquire lock
2082 AutoMutex _l(mLock);
2083 initializeLocked();
2084 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07002085
Jeff Brownb51719b2010-07-29 18:18:33 -07002086 InputMapper::reset();
Jeff Browne57e8952010-07-23 21:28:06 -07002087}
2088
2089void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Browne839a582010-04-22 18:58:52 -07002090 uint32_t policyFlags = 0;
Jeff Browne839a582010-04-22 18:58:52 -07002091
Jeff Brownb51719b2010-07-29 18:18:33 -07002092 // Preprocess pointer data.
Jeff Browne839a582010-04-22 18:58:52 -07002093
Jeff Browne57e8952010-07-23 21:28:06 -07002094 if (mParameters.useBadTouchFilter) {
2095 if (applyBadTouchFilter()) {
Jeff Browne839a582010-04-22 18:58:52 -07002096 havePointerIds = false;
2097 }
2098 }
2099
Jeff Browne57e8952010-07-23 21:28:06 -07002100 if (mParameters.useJumpyTouchFilter) {
2101 if (applyJumpyTouchFilter()) {
Jeff Browne839a582010-04-22 18:58:52 -07002102 havePointerIds = false;
2103 }
2104 }
2105
2106 if (! havePointerIds) {
Jeff Browne57e8952010-07-23 21:28:06 -07002107 calculatePointerIds();
Jeff Browne839a582010-04-22 18:58:52 -07002108 }
2109
Jeff Browne57e8952010-07-23 21:28:06 -07002110 TouchData temp;
2111 TouchData* savedTouch;
2112 if (mParameters.useAveragingTouchFilter) {
2113 temp.copyFrom(mCurrentTouch);
Jeff Browne839a582010-04-22 18:58:52 -07002114 savedTouch = & temp;
2115
Jeff Browne57e8952010-07-23 21:28:06 -07002116 applyAveragingTouchFilter();
Jeff Browne839a582010-04-22 18:58:52 -07002117 } else {
Jeff Browne57e8952010-07-23 21:28:06 -07002118 savedTouch = & mCurrentTouch;
Jeff Browne839a582010-04-22 18:58:52 -07002119 }
2120
Jeff Brownb51719b2010-07-29 18:18:33 -07002121 // Process touches and virtual keys.
Jeff Browne839a582010-04-22 18:58:52 -07002122
Jeff Browne57e8952010-07-23 21:28:06 -07002123 TouchResult touchResult = consumeOffScreenTouches(when, policyFlags);
2124 if (touchResult == DISPATCH_TOUCH) {
2125 dispatchTouches(when, policyFlags);
Jeff Browne839a582010-04-22 18:58:52 -07002126 }
2127
Jeff Brownb51719b2010-07-29 18:18:33 -07002128 // Copy current touch to last touch in preparation for the next cycle.
Jeff Browne839a582010-04-22 18:58:52 -07002129
Jeff Browne57e8952010-07-23 21:28:06 -07002130 if (touchResult == DROP_STROKE) {
2131 mLastTouch.clear();
2132 } else {
2133 mLastTouch.copyFrom(*savedTouch);
Jeff Browne839a582010-04-22 18:58:52 -07002134 }
Jeff Browne839a582010-04-22 18:58:52 -07002135}
2136
Jeff Browne57e8952010-07-23 21:28:06 -07002137TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches(
2138 nsecs_t when, uint32_t policyFlags) {
2139 int32_t keyEventAction, keyEventFlags;
2140 int32_t keyCode, scanCode, downTime;
2141 TouchResult touchResult;
Jeff Brown50de30a2010-06-22 01:27:15 -07002142
Jeff Brownb51719b2010-07-29 18:18:33 -07002143 { // acquire lock
2144 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07002145
Jeff Brownb51719b2010-07-29 18:18:33 -07002146 // Update surface size and orientation, including virtual key positions.
2147 if (! configureSurfaceLocked()) {
2148 return DROP_STROKE;
2149 }
2150
2151 // Check for virtual key press.
2152 if (mLocked.currentVirtualKey.down) {
Jeff Browne57e8952010-07-23 21:28:06 -07002153 if (mCurrentTouch.pointerCount == 0) {
2154 // Pointer went up while virtual key was down.
Jeff Brownb51719b2010-07-29 18:18:33 -07002155 mLocked.currentVirtualKey.down = false;
Jeff Browne57e8952010-07-23 21:28:06 -07002156#if DEBUG_VIRTUAL_KEYS
2157 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brown3c3cc622010-10-20 15:33:38 -07002158 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Browne57e8952010-07-23 21:28:06 -07002159#endif
2160 keyEventAction = AKEY_EVENT_ACTION_UP;
2161 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2162 touchResult = SKIP_TOUCH;
2163 goto DispatchVirtualKey;
2164 }
2165
2166 if (mCurrentTouch.pointerCount == 1) {
2167 int32_t x = mCurrentTouch.pointers[0].x;
2168 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brownb51719b2010-07-29 18:18:33 -07002169 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
2170 if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07002171 // Pointer is still within the space of the virtual key.
2172 return SKIP_TOUCH;
2173 }
2174 }
2175
2176 // Pointer left virtual key area or another pointer also went down.
2177 // Send key cancellation and drop the stroke so subsequent motions will be
2178 // considered fresh downs. This is useful when the user swipes away from the
2179 // virtual key area into the main display surface.
Jeff Brownb51719b2010-07-29 18:18:33 -07002180 mLocked.currentVirtualKey.down = false;
Jeff Browne57e8952010-07-23 21:28:06 -07002181#if DEBUG_VIRTUAL_KEYS
2182 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brown3c3cc622010-10-20 15:33:38 -07002183 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Browne57e8952010-07-23 21:28:06 -07002184#endif
2185 keyEventAction = AKEY_EVENT_ACTION_UP;
2186 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
2187 | AKEY_EVENT_FLAG_CANCELED;
Jeff Brown3c3cc622010-10-20 15:33:38 -07002188
2189 // Check whether the pointer moved inside the display area where we should
2190 // start a new stroke.
2191 int32_t x = mCurrentTouch.pointers[0].x;
2192 int32_t y = mCurrentTouch.pointers[0].y;
2193 if (isPointInsideSurfaceLocked(x, y)) {
2194 mLastTouch.clear();
2195 touchResult = DISPATCH_TOUCH;
2196 } else {
2197 touchResult = DROP_STROKE;
2198 }
Jeff Browne57e8952010-07-23 21:28:06 -07002199 } else {
2200 if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) {
2201 // Pointer just went down. Handle off-screen touches, if needed.
2202 int32_t x = mCurrentTouch.pointers[0].x;
2203 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brownb51719b2010-07-29 18:18:33 -07002204 if (! isPointInsideSurfaceLocked(x, y)) {
Jeff Browne57e8952010-07-23 21:28:06 -07002205 // If exactly one pointer went down, check for virtual key hit.
2206 // Otherwise we will drop the entire stroke.
2207 if (mCurrentTouch.pointerCount == 1) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002208 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
Jeff Browne57e8952010-07-23 21:28:06 -07002209 if (virtualKey) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002210 mLocked.currentVirtualKey.down = true;
2211 mLocked.currentVirtualKey.downTime = when;
2212 mLocked.currentVirtualKey.keyCode = virtualKey->keyCode;
2213 mLocked.currentVirtualKey.scanCode = virtualKey->scanCode;
Jeff Browne57e8952010-07-23 21:28:06 -07002214#if DEBUG_VIRTUAL_KEYS
2215 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brown3c3cc622010-10-20 15:33:38 -07002216 mLocked.currentVirtualKey.keyCode,
2217 mLocked.currentVirtualKey.scanCode);
Jeff Browne57e8952010-07-23 21:28:06 -07002218#endif
2219 keyEventAction = AKEY_EVENT_ACTION_DOWN;
2220 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM
2221 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2222 touchResult = SKIP_TOUCH;
2223 goto DispatchVirtualKey;
2224 }
2225 }
2226 return DROP_STROKE;
2227 }
2228 }
2229 return DISPATCH_TOUCH;
2230 }
2231
2232 DispatchVirtualKey:
2233 // Collect remaining state needed to dispatch virtual key.
Jeff Brownb51719b2010-07-29 18:18:33 -07002234 keyCode = mLocked.currentVirtualKey.keyCode;
2235 scanCode = mLocked.currentVirtualKey.scanCode;
2236 downTime = mLocked.currentVirtualKey.downTime;
2237 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07002238
2239 // Dispatch virtual key.
2240 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown956c0fb2010-10-01 14:55:30 -07002241 policyFlags |= POLICY_FLAG_VIRTUAL;
Jeff Brown90f0cee2010-10-08 22:31:17 -07002242 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
2243 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
2244 return touchResult;
Jeff Browne839a582010-04-22 18:58:52 -07002245}
2246
Jeff Browne57e8952010-07-23 21:28:06 -07002247void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
2248 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2249 uint32_t lastPointerCount = mLastTouch.pointerCount;
Jeff Browne839a582010-04-22 18:58:52 -07002250 if (currentPointerCount == 0 && lastPointerCount == 0) {
2251 return; // nothing to do!
2252 }
2253
Jeff Browne57e8952010-07-23 21:28:06 -07002254 BitSet32 currentIdBits = mCurrentTouch.idBits;
2255 BitSet32 lastIdBits = mLastTouch.idBits;
Jeff Browne839a582010-04-22 18:58:52 -07002256
2257 if (currentIdBits == lastIdBits) {
2258 // No pointer id changes so this is a move event.
2259 // The dispatcher takes care of batching moves so we don't have to deal with that here.
Jeff Brown5c1ed842010-07-14 18:48:53 -07002260 int32_t motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browne57e8952010-07-23 21:28:06 -07002261 dispatchTouch(when, policyFlags, & mCurrentTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002262 currentIdBits, -1, currentPointerCount, motionEventAction);
Jeff Browne839a582010-04-22 18:58:52 -07002263 } else {
Jeff Brown3c3cc622010-10-20 15:33:38 -07002264 // There may be pointers going up and pointers going down and pointers moving
2265 // all at the same time.
Jeff Browne839a582010-04-22 18:58:52 -07002266 BitSet32 upIdBits(lastIdBits.value & ~ currentIdBits.value);
2267 BitSet32 downIdBits(currentIdBits.value & ~ lastIdBits.value);
2268 BitSet32 activeIdBits(lastIdBits.value);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002269 uint32_t pointerCount = lastPointerCount;
Jeff Browne839a582010-04-22 18:58:52 -07002270
Jeff Brown3c3cc622010-10-20 15:33:38 -07002271 // Produce an intermediate representation of the touch data that consists of the
2272 // old location of pointers that have just gone up and the new location of pointers that
2273 // have just moved but omits the location of pointers that have just gone down.
2274 TouchData interimTouch;
2275 interimTouch.copyFrom(mLastTouch);
2276
2277 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
2278 bool moveNeeded = false;
2279 while (!moveIdBits.isEmpty()) {
2280 uint32_t moveId = moveIdBits.firstMarkedBit();
2281 moveIdBits.clearBit(moveId);
2282
2283 int32_t oldIndex = mLastTouch.idToIndex[moveId];
2284 int32_t newIndex = mCurrentTouch.idToIndex[moveId];
2285 if (mLastTouch.pointers[oldIndex] != mCurrentTouch.pointers[newIndex]) {
2286 interimTouch.pointers[oldIndex] = mCurrentTouch.pointers[newIndex];
2287 moveNeeded = true;
2288 }
2289 }
2290
2291 // Dispatch pointer up events using the interim pointer locations.
2292 while (!upIdBits.isEmpty()) {
Jeff Browne839a582010-04-22 18:58:52 -07002293 uint32_t upId = upIdBits.firstMarkedBit();
2294 upIdBits.clearBit(upId);
2295 BitSet32 oldActiveIdBits = activeIdBits;
2296 activeIdBits.clearBit(upId);
2297
2298 int32_t motionEventAction;
2299 if (activeIdBits.isEmpty()) {
Jeff Brown5c1ed842010-07-14 18:48:53 -07002300 motionEventAction = AMOTION_EVENT_ACTION_UP;
Jeff Browne839a582010-04-22 18:58:52 -07002301 } else {
Jeff Brown3cf1c9b2010-07-16 15:01:56 -07002302 motionEventAction = AMOTION_EVENT_ACTION_POINTER_UP;
Jeff Browne839a582010-04-22 18:58:52 -07002303 }
2304
Jeff Brown3c3cc622010-10-20 15:33:38 -07002305 dispatchTouch(when, policyFlags, &interimTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002306 oldActiveIdBits, upId, pointerCount, motionEventAction);
2307 pointerCount -= 1;
Jeff Browne839a582010-04-22 18:58:52 -07002308 }
2309
Jeff Brown3c3cc622010-10-20 15:33:38 -07002310 // Dispatch move events if any of the remaining pointers moved from their old locations.
2311 // Although applications receive new locations as part of individual pointer up
2312 // events, they do not generally handle them except when presented in a move event.
2313 if (moveNeeded) {
2314 dispatchTouch(when, policyFlags, &mCurrentTouch,
2315 activeIdBits, -1, pointerCount, AMOTION_EVENT_ACTION_MOVE);
2316 }
2317
2318 // Dispatch pointer down events using the new pointer locations.
2319 while (!downIdBits.isEmpty()) {
Jeff Browne839a582010-04-22 18:58:52 -07002320 uint32_t downId = downIdBits.firstMarkedBit();
2321 downIdBits.clearBit(downId);
2322 BitSet32 oldActiveIdBits = activeIdBits;
2323 activeIdBits.markBit(downId);
2324
2325 int32_t motionEventAction;
2326 if (oldActiveIdBits.isEmpty()) {
Jeff Brown5c1ed842010-07-14 18:48:53 -07002327 motionEventAction = AMOTION_EVENT_ACTION_DOWN;
Jeff Browne57e8952010-07-23 21:28:06 -07002328 mDownTime = when;
Jeff Browne839a582010-04-22 18:58:52 -07002329 } else {
Jeff Brown3cf1c9b2010-07-16 15:01:56 -07002330 motionEventAction = AMOTION_EVENT_ACTION_POINTER_DOWN;
Jeff Browne839a582010-04-22 18:58:52 -07002331 }
2332
Jeff Brown38a7fab2010-08-30 03:02:23 -07002333 pointerCount += 1;
Jeff Brown3c3cc622010-10-20 15:33:38 -07002334 dispatchTouch(when, policyFlags, &mCurrentTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002335 activeIdBits, downId, pointerCount, motionEventAction);
Jeff Browne839a582010-04-22 18:58:52 -07002336 }
2337 }
2338}
2339
Jeff Browne57e8952010-07-23 21:28:06 -07002340void TouchInputMapper::dispatchTouch(nsecs_t when, uint32_t policyFlags,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002341 TouchData* touch, BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
Jeff Browne839a582010-04-22 18:58:52 -07002342 int32_t motionEventAction) {
Jeff Browne839a582010-04-22 18:58:52 -07002343 int32_t pointerIds[MAX_POINTERS];
2344 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Browne839a582010-04-22 18:58:52 -07002345 int32_t motionEventEdgeFlags = 0;
Jeff Brownb51719b2010-07-29 18:18:33 -07002346 float xPrecision, yPrecision;
2347
2348 { // acquire lock
2349 AutoMutex _l(mLock);
2350
2351 // Walk through the the active pointers and map touch screen coordinates (TouchData) into
2352 // display coordinates (PointerCoords) and adjust for display orientation.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002353 for (uint32_t outIndex = 0; ! idBits.isEmpty(); outIndex++) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002354 uint32_t id = idBits.firstMarkedBit();
2355 idBits.clearBit(id);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002356 uint32_t inIndex = touch->idToIndex[id];
Jeff Brownb51719b2010-07-29 18:18:33 -07002357
Jeff Brown38a7fab2010-08-30 03:02:23 -07002358 const PointerData& in = touch->pointers[inIndex];
Jeff Brownb51719b2010-07-29 18:18:33 -07002359
Jeff Brown38a7fab2010-08-30 03:02:23 -07002360 // X and Y
2361 float x = float(in.x - mLocked.xOrigin) * mLocked.xScale;
2362 float y = float(in.y - mLocked.yOrigin) * mLocked.yScale;
Jeff Brownb51719b2010-07-29 18:18:33 -07002363
Jeff Brown38a7fab2010-08-30 03:02:23 -07002364 // ToolMajor and ToolMinor
2365 float toolMajor, toolMinor;
Jeff Brown6b337e72010-10-14 21:42:15 -07002366 switch (mCalibration.toolSizeCalibration) {
2367 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002368 toolMajor = in.toolMajor * mLocked.geometricScale;
2369 if (mRawAxes.toolMinor.valid) {
2370 toolMinor = in.toolMinor * mLocked.geometricScale;
2371 } else {
2372 toolMinor = toolMajor;
2373 }
2374 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002375 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002376 toolMajor = in.toolMajor != 0
Jeff Brown6b337e72010-10-14 21:42:15 -07002377 ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias
Jeff Brown38a7fab2010-08-30 03:02:23 -07002378 : 0;
2379 if (mRawAxes.toolMinor.valid) {
2380 toolMinor = in.toolMinor != 0
Jeff Brown6b337e72010-10-14 21:42:15 -07002381 ? in.toolMinor * mLocked.toolSizeLinearScale
2382 + mLocked.toolSizeLinearBias
Jeff Brown38a7fab2010-08-30 03:02:23 -07002383 : 0;
2384 } else {
2385 toolMinor = toolMajor;
2386 }
2387 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002388 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2389 if (in.toolMajor != 0) {
2390 float diameter = sqrtf(in.toolMajor
2391 * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias);
2392 toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias;
2393 } else {
2394 toolMajor = 0;
2395 }
2396 toolMinor = toolMajor;
2397 break;
Jeff Brown38a7fab2010-08-30 03:02:23 -07002398 default:
2399 toolMajor = 0;
2400 toolMinor = 0;
2401 break;
Jeff Brownb51719b2010-07-29 18:18:33 -07002402 }
2403
Jeff Brown6b337e72010-10-14 21:42:15 -07002404 if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002405 toolMajor /= pointerCount;
2406 toolMinor /= pointerCount;
2407 }
2408
2409 // Pressure
2410 float rawPressure;
2411 switch (mCalibration.pressureSource) {
2412 case Calibration::PRESSURE_SOURCE_PRESSURE:
2413 rawPressure = in.pressure;
2414 break;
2415 case Calibration::PRESSURE_SOURCE_TOUCH:
2416 rawPressure = in.touchMajor;
2417 break;
2418 default:
2419 rawPressure = 0;
2420 }
2421
2422 float pressure;
2423 switch (mCalibration.pressureCalibration) {
2424 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
2425 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
2426 pressure = rawPressure * mLocked.pressureScale;
2427 break;
2428 default:
2429 pressure = 1;
2430 break;
2431 }
2432
2433 // TouchMajor and TouchMinor
2434 float touchMajor, touchMinor;
Jeff Brown6b337e72010-10-14 21:42:15 -07002435 switch (mCalibration.touchSizeCalibration) {
2436 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002437 touchMajor = in.touchMajor * mLocked.geometricScale;
2438 if (mRawAxes.touchMinor.valid) {
2439 touchMinor = in.touchMinor * mLocked.geometricScale;
2440 } else {
2441 touchMinor = touchMajor;
2442 }
2443 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002444 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002445 touchMajor = toolMajor * pressure;
2446 touchMinor = toolMinor * pressure;
2447 break;
2448 default:
2449 touchMajor = 0;
2450 touchMinor = 0;
2451 break;
2452 }
2453
2454 if (touchMajor > toolMajor) {
2455 touchMajor = toolMajor;
2456 }
2457 if (touchMinor > toolMinor) {
2458 touchMinor = toolMinor;
2459 }
2460
2461 // Size
2462 float size;
2463 switch (mCalibration.sizeCalibration) {
2464 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
2465 float rawSize = mRawAxes.toolMinor.valid
2466 ? avg(in.toolMajor, in.toolMinor)
2467 : in.toolMajor;
2468 size = rawSize * mLocked.sizeScale;
2469 break;
2470 }
2471 default:
2472 size = 0;
2473 break;
2474 }
2475
2476 // Orientation
2477 float orientation;
2478 switch (mCalibration.orientationCalibration) {
2479 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
2480 orientation = in.orientation * mLocked.orientationScale;
2481 break;
2482 default:
2483 orientation = 0;
2484 }
2485
2486 // Adjust coords for orientation.
Jeff Brownb51719b2010-07-29 18:18:33 -07002487 switch (mLocked.surfaceOrientation) {
2488 case InputReaderPolicyInterface::ROTATION_90: {
2489 float xTemp = x;
2490 x = y;
2491 y = mLocked.surfaceWidth - xTemp;
2492 orientation -= M_PI_2;
2493 if (orientation < - M_PI_2) {
2494 orientation += M_PI;
2495 }
2496 break;
2497 }
2498 case InputReaderPolicyInterface::ROTATION_180: {
2499 x = mLocked.surfaceWidth - x;
2500 y = mLocked.surfaceHeight - y;
2501 orientation = - orientation;
2502 break;
2503 }
2504 case InputReaderPolicyInterface::ROTATION_270: {
2505 float xTemp = x;
2506 x = mLocked.surfaceHeight - y;
2507 y = xTemp;
2508 orientation += M_PI_2;
2509 if (orientation > M_PI_2) {
2510 orientation -= M_PI;
2511 }
2512 break;
2513 }
2514 }
2515
Jeff Brown38a7fab2010-08-30 03:02:23 -07002516 // Write output coords.
2517 PointerCoords& out = pointerCoords[outIndex];
2518 out.x = x;
2519 out.y = y;
2520 out.pressure = pressure;
2521 out.size = size;
2522 out.touchMajor = touchMajor;
2523 out.touchMinor = touchMinor;
2524 out.toolMajor = toolMajor;
2525 out.toolMinor = toolMinor;
2526 out.orientation = orientation;
Jeff Brownb51719b2010-07-29 18:18:33 -07002527
Jeff Brown38a7fab2010-08-30 03:02:23 -07002528 pointerIds[outIndex] = int32_t(id);
Jeff Brownb51719b2010-07-29 18:18:33 -07002529
2530 if (id == changedId) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002531 motionEventAction |= outIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
Jeff Brownb51719b2010-07-29 18:18:33 -07002532 }
Jeff Browne839a582010-04-22 18:58:52 -07002533 }
Jeff Brownb51719b2010-07-29 18:18:33 -07002534
2535 // Check edge flags by looking only at the first pointer since the flags are
2536 // global to the event.
2537 if (motionEventAction == AMOTION_EVENT_ACTION_DOWN) {
2538 if (pointerCoords[0].x <= 0) {
2539 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_LEFT;
2540 } else if (pointerCoords[0].x >= mLocked.orientedSurfaceWidth) {
2541 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_RIGHT;
2542 }
2543 if (pointerCoords[0].y <= 0) {
2544 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_TOP;
2545 } else if (pointerCoords[0].y >= mLocked.orientedSurfaceHeight) {
2546 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_BOTTOM;
2547 }
Jeff Browne839a582010-04-22 18:58:52 -07002548 }
Jeff Brownb51719b2010-07-29 18:18:33 -07002549
2550 xPrecision = mLocked.orientedXPrecision;
2551 yPrecision = mLocked.orientedYPrecision;
2552 } // release lock
Jeff Browne839a582010-04-22 18:58:52 -07002553
Jeff Brown77e26fc2010-10-07 13:44:51 -07002554 getDispatcher()->notifyMotion(when, getDeviceId(), getSources(), policyFlags,
Jeff Brownaf30ff62010-09-01 17:01:00 -07002555 motionEventAction, 0, getContext()->getGlobalMetaState(), motionEventEdgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -07002556 pointerCount, pointerIds, pointerCoords,
Jeff Brownb51719b2010-07-29 18:18:33 -07002557 xPrecision, yPrecision, mDownTime);
Jeff Browne839a582010-04-22 18:58:52 -07002558}
2559
Jeff Brownb51719b2010-07-29 18:18:33 -07002560bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002561 if (mRawAxes.x.valid && mRawAxes.y.valid) {
2562 return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue
2563 && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue;
Jeff Browne839a582010-04-22 18:58:52 -07002564 }
Jeff Browne57e8952010-07-23 21:28:06 -07002565 return true;
2566}
2567
Jeff Brownb51719b2010-07-29 18:18:33 -07002568const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked(
2569 int32_t x, int32_t y) {
2570 size_t numVirtualKeys = mLocked.virtualKeys.size();
2571 for (size_t i = 0; i < numVirtualKeys; i++) {
2572 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07002573
2574#if DEBUG_VIRTUAL_KEYS
2575 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
2576 "left=%d, top=%d, right=%d, bottom=%d",
2577 x, y,
2578 virtualKey.keyCode, virtualKey.scanCode,
2579 virtualKey.hitLeft, virtualKey.hitTop,
2580 virtualKey.hitRight, virtualKey.hitBottom);
2581#endif
2582
2583 if (virtualKey.isHit(x, y)) {
2584 return & virtualKey;
2585 }
2586 }
2587
2588 return NULL;
2589}
2590
2591void TouchInputMapper::calculatePointerIds() {
2592 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2593 uint32_t lastPointerCount = mLastTouch.pointerCount;
2594
2595 if (currentPointerCount == 0) {
2596 // No pointers to assign.
2597 mCurrentTouch.idBits.clear();
2598 } else if (lastPointerCount == 0) {
2599 // All pointers are new.
2600 mCurrentTouch.idBits.clear();
2601 for (uint32_t i = 0; i < currentPointerCount; i++) {
2602 mCurrentTouch.pointers[i].id = i;
2603 mCurrentTouch.idToIndex[i] = i;
2604 mCurrentTouch.idBits.markBit(i);
2605 }
2606 } else if (currentPointerCount == 1 && lastPointerCount == 1) {
2607 // Only one pointer and no change in count so it must have the same id as before.
2608 uint32_t id = mLastTouch.pointers[0].id;
2609 mCurrentTouch.pointers[0].id = id;
2610 mCurrentTouch.idToIndex[id] = 0;
2611 mCurrentTouch.idBits.value = BitSet32::valueForBit(id);
2612 } else {
2613 // General case.
2614 // We build a heap of squared euclidean distances between current and last pointers
2615 // associated with the current and last pointer indices. Then, we find the best
2616 // match (by distance) for each current pointer.
2617 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
2618
2619 uint32_t heapSize = 0;
2620 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
2621 currentPointerIndex++) {
2622 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
2623 lastPointerIndex++) {
2624 int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x
2625 - mLastTouch.pointers[lastPointerIndex].x;
2626 int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y
2627 - mLastTouch.pointers[lastPointerIndex].y;
2628
2629 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
2630
2631 // Insert new element into the heap (sift up).
2632 heap[heapSize].currentPointerIndex = currentPointerIndex;
2633 heap[heapSize].lastPointerIndex = lastPointerIndex;
2634 heap[heapSize].distance = distance;
2635 heapSize += 1;
2636 }
2637 }
2638
2639 // Heapify
2640 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
2641 startIndex -= 1;
2642 for (uint32_t parentIndex = startIndex; ;) {
2643 uint32_t childIndex = parentIndex * 2 + 1;
2644 if (childIndex >= heapSize) {
2645 break;
2646 }
2647
2648 if (childIndex + 1 < heapSize
2649 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2650 childIndex += 1;
2651 }
2652
2653 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2654 break;
2655 }
2656
2657 swap(heap[parentIndex], heap[childIndex]);
2658 parentIndex = childIndex;
2659 }
2660 }
2661
2662#if DEBUG_POINTER_ASSIGNMENT
2663 LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize);
2664 for (size_t i = 0; i < heapSize; i++) {
2665 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2666 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2667 heap[i].distance);
2668 }
2669#endif
2670
2671 // Pull matches out by increasing order of distance.
2672 // To avoid reassigning pointers that have already been matched, the loop keeps track
2673 // of which last and current pointers have been matched using the matchedXXXBits variables.
2674 // It also tracks the used pointer id bits.
2675 BitSet32 matchedLastBits(0);
2676 BitSet32 matchedCurrentBits(0);
2677 BitSet32 usedIdBits(0);
2678 bool first = true;
2679 for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) {
2680 for (;;) {
2681 if (first) {
2682 // The first time through the loop, we just consume the root element of
2683 // the heap (the one with smallest distance).
2684 first = false;
2685 } else {
2686 // Previous iterations consumed the root element of the heap.
2687 // Pop root element off of the heap (sift down).
2688 heapSize -= 1;
2689 assert(heapSize > 0);
2690
2691 // Sift down.
2692 heap[0] = heap[heapSize];
2693 for (uint32_t parentIndex = 0; ;) {
2694 uint32_t childIndex = parentIndex * 2 + 1;
2695 if (childIndex >= heapSize) {
2696 break;
2697 }
2698
2699 if (childIndex + 1 < heapSize
2700 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2701 childIndex += 1;
2702 }
2703
2704 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2705 break;
2706 }
2707
2708 swap(heap[parentIndex], heap[childIndex]);
2709 parentIndex = childIndex;
2710 }
2711
2712#if DEBUG_POINTER_ASSIGNMENT
2713 LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize);
2714 for (size_t i = 0; i < heapSize; i++) {
2715 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2716 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2717 heap[i].distance);
2718 }
2719#endif
2720 }
2721
2722 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
2723 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
2724
2725 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
2726 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
2727
2728 matchedCurrentBits.markBit(currentPointerIndex);
2729 matchedLastBits.markBit(lastPointerIndex);
2730
2731 uint32_t id = mLastTouch.pointers[lastPointerIndex].id;
2732 mCurrentTouch.pointers[currentPointerIndex].id = id;
2733 mCurrentTouch.idToIndex[id] = currentPointerIndex;
2734 usedIdBits.markBit(id);
2735
2736#if DEBUG_POINTER_ASSIGNMENT
2737 LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
2738 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
2739#endif
2740 break;
2741 }
2742 }
2743
2744 // Assign fresh ids to new pointers.
2745 if (currentPointerCount > lastPointerCount) {
2746 for (uint32_t i = currentPointerCount - lastPointerCount; ;) {
2747 uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit();
2748 uint32_t id = usedIdBits.firstUnmarkedBit();
2749
2750 mCurrentTouch.pointers[currentPointerIndex].id = id;
2751 mCurrentTouch.idToIndex[id] = currentPointerIndex;
2752 usedIdBits.markBit(id);
2753
2754#if DEBUG_POINTER_ASSIGNMENT
2755 LOGD("calculatePointerIds - assigned: cur=%d, id=%d",
2756 currentPointerIndex, id);
2757#endif
2758
2759 if (--i == 0) break; // done
2760 matchedCurrentBits.markBit(currentPointerIndex);
2761 }
2762 }
2763
2764 // Fix id bits.
2765 mCurrentTouch.idBits = usedIdBits;
2766 }
2767}
2768
2769/* Special hack for devices that have bad screen data: if one of the
2770 * points has moved more than a screen height from the last position,
2771 * then drop it. */
2772bool TouchInputMapper::applyBadTouchFilter() {
2773 // This hack requires valid axis parameters.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002774 if (! mRawAxes.y.valid) {
Jeff Browne57e8952010-07-23 21:28:06 -07002775 return false;
2776 }
2777
2778 uint32_t pointerCount = mCurrentTouch.pointerCount;
2779
2780 // Nothing to do if there are no points.
2781 if (pointerCount == 0) {
2782 return false;
2783 }
2784
2785 // Don't do anything if a finger is going down or up. We run
2786 // here before assigning pointer IDs, so there isn't a good
2787 // way to do per-finger matching.
2788 if (pointerCount != mLastTouch.pointerCount) {
2789 return false;
2790 }
2791
2792 // We consider a single movement across more than a 7/16 of
2793 // the long size of the screen to be bad. This was a magic value
2794 // determined by looking at the maximum distance it is feasible
2795 // to actually move in one sample.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002796 int32_t maxDeltaY = mRawAxes.y.getRange() * 7 / 16;
Jeff Browne57e8952010-07-23 21:28:06 -07002797
2798 // XXX The original code in InputDevice.java included commented out
2799 // code for testing the X axis. Note that when we drop a point
2800 // we don't actually restore the old X either. Strange.
2801 // The old code also tries to track when bad points were previously
2802 // detected but it turns out that due to the placement of a "break"
2803 // at the end of the loop, we never set mDroppedBadPoint to true
2804 // so it is effectively dead code.
2805 // Need to figure out if the old code is busted or just overcomplicated
2806 // but working as intended.
2807
2808 // Look through all new points and see if any are farther than
2809 // acceptable from all previous points.
2810 for (uint32_t i = pointerCount; i-- > 0; ) {
2811 int32_t y = mCurrentTouch.pointers[i].y;
2812 int32_t closestY = INT_MAX;
2813 int32_t closestDeltaY = 0;
2814
2815#if DEBUG_HACKS
2816 LOGD("BadTouchFilter: Looking at next point #%d: y=%d", i, y);
2817#endif
2818
2819 for (uint32_t j = pointerCount; j-- > 0; ) {
2820 int32_t lastY = mLastTouch.pointers[j].y;
2821 int32_t deltaY = abs(y - lastY);
2822
2823#if DEBUG_HACKS
2824 LOGD("BadTouchFilter: Comparing with last point #%d: y=%d deltaY=%d",
2825 j, lastY, deltaY);
2826#endif
2827
2828 if (deltaY < maxDeltaY) {
2829 goto SkipSufficientlyClosePoint;
2830 }
2831 if (deltaY < closestDeltaY) {
2832 closestDeltaY = deltaY;
2833 closestY = lastY;
2834 }
2835 }
2836
2837 // Must not have found a close enough match.
2838#if DEBUG_HACKS
2839 LOGD("BadTouchFilter: Dropping bad point #%d: newY=%d oldY=%d deltaY=%d maxDeltaY=%d",
2840 i, y, closestY, closestDeltaY, maxDeltaY);
2841#endif
2842
2843 mCurrentTouch.pointers[i].y = closestY;
2844 return true; // XXX original code only corrects one point
2845
2846 SkipSufficientlyClosePoint: ;
2847 }
2848
2849 // No change.
2850 return false;
2851}
2852
2853/* Special hack for devices that have bad screen data: drop points where
2854 * the coordinate value for one axis has jumped to the other pointer's location.
2855 */
2856bool TouchInputMapper::applyJumpyTouchFilter() {
2857 // This hack requires valid axis parameters.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002858 if (! mRawAxes.y.valid) {
Jeff Browne57e8952010-07-23 21:28:06 -07002859 return false;
2860 }
2861
2862 uint32_t pointerCount = mCurrentTouch.pointerCount;
2863 if (mLastTouch.pointerCount != pointerCount) {
2864#if DEBUG_HACKS
2865 LOGD("JumpyTouchFilter: Different pointer count %d -> %d",
2866 mLastTouch.pointerCount, pointerCount);
2867 for (uint32_t i = 0; i < pointerCount; i++) {
2868 LOGD(" Pointer %d (%d, %d)", i,
2869 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
2870 }
2871#endif
2872
2873 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_TRANSITION_DROPS) {
2874 if (mLastTouch.pointerCount == 1 && pointerCount == 2) {
2875 // Just drop the first few events going from 1 to 2 pointers.
2876 // They're bad often enough that they're not worth considering.
2877 mCurrentTouch.pointerCount = 1;
2878 mJumpyTouchFilter.jumpyPointsDropped += 1;
2879
2880#if DEBUG_HACKS
2881 LOGD("JumpyTouchFilter: Pointer 2 dropped");
2882#endif
2883 return true;
2884 } else if (mLastTouch.pointerCount == 2 && pointerCount == 1) {
2885 // The event when we go from 2 -> 1 tends to be messed up too
2886 mCurrentTouch.pointerCount = 2;
2887 mCurrentTouch.pointers[0] = mLastTouch.pointers[0];
2888 mCurrentTouch.pointers[1] = mLastTouch.pointers[1];
2889 mJumpyTouchFilter.jumpyPointsDropped += 1;
2890
2891#if DEBUG_HACKS
2892 for (int32_t i = 0; i < 2; i++) {
2893 LOGD("JumpyTouchFilter: Pointer %d replaced (%d, %d)", i,
2894 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
2895 }
2896#endif
2897 return true;
2898 }
2899 }
2900 // Reset jumpy points dropped on other transitions or if limit exceeded.
2901 mJumpyTouchFilter.jumpyPointsDropped = 0;
2902
2903#if DEBUG_HACKS
2904 LOGD("JumpyTouchFilter: Transition - drop limit reset");
2905#endif
2906 return false;
2907 }
2908
2909 // We have the same number of pointers as last time.
2910 // A 'jumpy' point is one where the coordinate value for one axis
2911 // has jumped to the other pointer's location. No need to do anything
2912 // else if we only have one pointer.
2913 if (pointerCount < 2) {
2914 return false;
2915 }
2916
2917 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_DROP_LIMIT) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002918 int jumpyEpsilon = mRawAxes.y.getRange() / JUMPY_EPSILON_DIVISOR;
Jeff Browne57e8952010-07-23 21:28:06 -07002919
2920 // We only replace the single worst jumpy point as characterized by pointer distance
2921 // in a single axis.
2922 int32_t badPointerIndex = -1;
2923 int32_t badPointerReplacementIndex = -1;
2924 int32_t badPointerDistance = INT_MIN; // distance to be corrected
2925
2926 for (uint32_t i = pointerCount; i-- > 0; ) {
2927 int32_t x = mCurrentTouch.pointers[i].x;
2928 int32_t y = mCurrentTouch.pointers[i].y;
2929
2930#if DEBUG_HACKS
2931 LOGD("JumpyTouchFilter: Point %d (%d, %d)", i, x, y);
2932#endif
2933
2934 // Check if a touch point is too close to another's coordinates
2935 bool dropX = false, dropY = false;
2936 for (uint32_t j = 0; j < pointerCount; j++) {
2937 if (i == j) {
2938 continue;
2939 }
2940
2941 if (abs(x - mCurrentTouch.pointers[j].x) <= jumpyEpsilon) {
2942 dropX = true;
2943 break;
2944 }
2945
2946 if (abs(y - mCurrentTouch.pointers[j].y) <= jumpyEpsilon) {
2947 dropY = true;
2948 break;
2949 }
2950 }
2951 if (! dropX && ! dropY) {
2952 continue; // not jumpy
2953 }
2954
2955 // Find a replacement candidate by comparing with older points on the
2956 // complementary (non-jumpy) axis.
2957 int32_t distance = INT_MIN; // distance to be corrected
2958 int32_t replacementIndex = -1;
2959
2960 if (dropX) {
2961 // X looks too close. Find an older replacement point with a close Y.
2962 int32_t smallestDeltaY = INT_MAX;
2963 for (uint32_t j = 0; j < pointerCount; j++) {
2964 int32_t deltaY = abs(y - mLastTouch.pointers[j].y);
2965 if (deltaY < smallestDeltaY) {
2966 smallestDeltaY = deltaY;
2967 replacementIndex = j;
2968 }
2969 }
2970 distance = abs(x - mLastTouch.pointers[replacementIndex].x);
2971 } else {
2972 // Y looks too close. Find an older replacement point with a close X.
2973 int32_t smallestDeltaX = INT_MAX;
2974 for (uint32_t j = 0; j < pointerCount; j++) {
2975 int32_t deltaX = abs(x - mLastTouch.pointers[j].x);
2976 if (deltaX < smallestDeltaX) {
2977 smallestDeltaX = deltaX;
2978 replacementIndex = j;
2979 }
2980 }
2981 distance = abs(y - mLastTouch.pointers[replacementIndex].y);
2982 }
2983
2984 // If replacing this pointer would correct a worse error than the previous ones
2985 // considered, then use this replacement instead.
2986 if (distance > badPointerDistance) {
2987 badPointerIndex = i;
2988 badPointerReplacementIndex = replacementIndex;
2989 badPointerDistance = distance;
2990 }
2991 }
2992
2993 // Correct the jumpy pointer if one was found.
2994 if (badPointerIndex >= 0) {
2995#if DEBUG_HACKS
2996 LOGD("JumpyTouchFilter: Replacing bad pointer %d with (%d, %d)",
2997 badPointerIndex,
2998 mLastTouch.pointers[badPointerReplacementIndex].x,
2999 mLastTouch.pointers[badPointerReplacementIndex].y);
3000#endif
3001
3002 mCurrentTouch.pointers[badPointerIndex].x =
3003 mLastTouch.pointers[badPointerReplacementIndex].x;
3004 mCurrentTouch.pointers[badPointerIndex].y =
3005 mLastTouch.pointers[badPointerReplacementIndex].y;
3006 mJumpyTouchFilter.jumpyPointsDropped += 1;
3007 return true;
3008 }
3009 }
3010
3011 mJumpyTouchFilter.jumpyPointsDropped = 0;
3012 return false;
3013}
3014
3015/* Special hack for devices that have bad screen data: aggregate and
3016 * compute averages of the coordinate data, to reduce the amount of
3017 * jitter seen by applications. */
3018void TouchInputMapper::applyAveragingTouchFilter() {
3019 for (uint32_t currentIndex = 0; currentIndex < mCurrentTouch.pointerCount; currentIndex++) {
3020 uint32_t id = mCurrentTouch.pointers[currentIndex].id;
3021 int32_t x = mCurrentTouch.pointers[currentIndex].x;
3022 int32_t y = mCurrentTouch.pointers[currentIndex].y;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003023 int32_t pressure;
3024 switch (mCalibration.pressureSource) {
3025 case Calibration::PRESSURE_SOURCE_PRESSURE:
3026 pressure = mCurrentTouch.pointers[currentIndex].pressure;
3027 break;
3028 case Calibration::PRESSURE_SOURCE_TOUCH:
3029 pressure = mCurrentTouch.pointers[currentIndex].touchMajor;
3030 break;
3031 default:
3032 pressure = 1;
3033 break;
3034 }
Jeff Browne57e8952010-07-23 21:28:06 -07003035
3036 if (mLastTouch.idBits.hasBit(id)) {
3037 // Pointer was down before and is still down now.
3038 // Compute average over history trace.
3039 uint32_t start = mAveragingTouchFilter.historyStart[id];
3040 uint32_t end = mAveragingTouchFilter.historyEnd[id];
3041
3042 int64_t deltaX = x - mAveragingTouchFilter.historyData[end].pointers[id].x;
3043 int64_t deltaY = y - mAveragingTouchFilter.historyData[end].pointers[id].y;
3044 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3045
3046#if DEBUG_HACKS
3047 LOGD("AveragingTouchFilter: Pointer id %d - Distance from last sample: %lld",
3048 id, distance);
3049#endif
3050
3051 if (distance < AVERAGING_DISTANCE_LIMIT) {
3052 // Increment end index in preparation for recording new historical data.
3053 end += 1;
3054 if (end > AVERAGING_HISTORY_SIZE) {
3055 end = 0;
3056 }
3057
3058 // If the end index has looped back to the start index then we have filled
3059 // the historical trace up to the desired size so we drop the historical
3060 // data at the start of the trace.
3061 if (end == start) {
3062 start += 1;
3063 if (start > AVERAGING_HISTORY_SIZE) {
3064 start = 0;
3065 }
3066 }
3067
3068 // Add the raw data to the historical trace.
3069 mAveragingTouchFilter.historyStart[id] = start;
3070 mAveragingTouchFilter.historyEnd[id] = end;
3071 mAveragingTouchFilter.historyData[end].pointers[id].x = x;
3072 mAveragingTouchFilter.historyData[end].pointers[id].y = y;
3073 mAveragingTouchFilter.historyData[end].pointers[id].pressure = pressure;
3074
3075 // Average over all historical positions in the trace by total pressure.
3076 int32_t averagedX = 0;
3077 int32_t averagedY = 0;
3078 int32_t totalPressure = 0;
3079 for (;;) {
3080 int32_t historicalX = mAveragingTouchFilter.historyData[start].pointers[id].x;
3081 int32_t historicalY = mAveragingTouchFilter.historyData[start].pointers[id].y;
3082 int32_t historicalPressure = mAveragingTouchFilter.historyData[start]
3083 .pointers[id].pressure;
3084
3085 averagedX += historicalX * historicalPressure;
3086 averagedY += historicalY * historicalPressure;
3087 totalPressure += historicalPressure;
3088
3089 if (start == end) {
3090 break;
3091 }
3092
3093 start += 1;
3094 if (start > AVERAGING_HISTORY_SIZE) {
3095 start = 0;
3096 }
3097 }
3098
Jeff Brown38a7fab2010-08-30 03:02:23 -07003099 if (totalPressure != 0) {
3100 averagedX /= totalPressure;
3101 averagedY /= totalPressure;
Jeff Browne57e8952010-07-23 21:28:06 -07003102
3103#if DEBUG_HACKS
Jeff Brown38a7fab2010-08-30 03:02:23 -07003104 LOGD("AveragingTouchFilter: Pointer id %d - "
3105 "totalPressure=%d, averagedX=%d, averagedY=%d", id, totalPressure,
3106 averagedX, averagedY);
Jeff Browne57e8952010-07-23 21:28:06 -07003107#endif
3108
Jeff Brown38a7fab2010-08-30 03:02:23 -07003109 mCurrentTouch.pointers[currentIndex].x = averagedX;
3110 mCurrentTouch.pointers[currentIndex].y = averagedY;
3111 }
Jeff Browne57e8952010-07-23 21:28:06 -07003112 } else {
3113#if DEBUG_HACKS
3114 LOGD("AveragingTouchFilter: Pointer id %d - Exceeded max distance", id);
3115#endif
3116 }
3117 } else {
3118#if DEBUG_HACKS
3119 LOGD("AveragingTouchFilter: Pointer id %d - Pointer went up", id);
3120#endif
3121 }
3122
3123 // Reset pointer history.
3124 mAveragingTouchFilter.historyStart[id] = 0;
3125 mAveragingTouchFilter.historyEnd[id] = 0;
3126 mAveragingTouchFilter.historyData[0].pointers[id].x = x;
3127 mAveragingTouchFilter.historyData[0].pointers[id].y = y;
3128 mAveragingTouchFilter.historyData[0].pointers[id].pressure = pressure;
3129 }
3130}
3131
3132int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownb51719b2010-07-29 18:18:33 -07003133 { // acquire lock
3134 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07003135
Jeff Brownb51719b2010-07-29 18:18:33 -07003136 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07003137 return AKEY_STATE_VIRTUAL;
3138 }
3139
Jeff Brownb51719b2010-07-29 18:18:33 -07003140 size_t numVirtualKeys = mLocked.virtualKeys.size();
3141 for (size_t i = 0; i < numVirtualKeys; i++) {
3142 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07003143 if (virtualKey.keyCode == keyCode) {
3144 return AKEY_STATE_UP;
3145 }
3146 }
Jeff Brownb51719b2010-07-29 18:18:33 -07003147 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07003148
3149 return AKEY_STATE_UNKNOWN;
3150}
3151
3152int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownb51719b2010-07-29 18:18:33 -07003153 { // acquire lock
3154 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07003155
Jeff Brownb51719b2010-07-29 18:18:33 -07003156 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07003157 return AKEY_STATE_VIRTUAL;
3158 }
3159
Jeff Brownb51719b2010-07-29 18:18:33 -07003160 size_t numVirtualKeys = mLocked.virtualKeys.size();
3161 for (size_t i = 0; i < numVirtualKeys; i++) {
3162 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07003163 if (virtualKey.scanCode == scanCode) {
3164 return AKEY_STATE_UP;
3165 }
3166 }
Jeff Brownb51719b2010-07-29 18:18:33 -07003167 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07003168
3169 return AKEY_STATE_UNKNOWN;
3170}
3171
3172bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
3173 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownb51719b2010-07-29 18:18:33 -07003174 { // acquire lock
3175 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07003176
Jeff Brownb51719b2010-07-29 18:18:33 -07003177 size_t numVirtualKeys = mLocked.virtualKeys.size();
3178 for (size_t i = 0; i < numVirtualKeys; i++) {
3179 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07003180
3181 for (size_t i = 0; i < numCodes; i++) {
3182 if (virtualKey.keyCode == keyCodes[i]) {
3183 outFlags[i] = 1;
3184 }
3185 }
3186 }
Jeff Brownb51719b2010-07-29 18:18:33 -07003187 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07003188
3189 return true;
3190}
3191
3192
3193// --- SingleTouchInputMapper ---
3194
Jeff Brown66888372010-11-29 17:37:49 -08003195SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
3196 TouchInputMapper(device) {
Jeff Browne57e8952010-07-23 21:28:06 -07003197 initialize();
3198}
3199
3200SingleTouchInputMapper::~SingleTouchInputMapper() {
3201}
3202
3203void SingleTouchInputMapper::initialize() {
3204 mAccumulator.clear();
3205
3206 mDown = false;
3207 mX = 0;
3208 mY = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003209 mPressure = 0; // default to 0 for devices that don't report pressure
3210 mToolWidth = 0; // default to 0 for devices that don't report tool width
Jeff Browne57e8952010-07-23 21:28:06 -07003211}
3212
3213void SingleTouchInputMapper::reset() {
3214 TouchInputMapper::reset();
3215
Jeff Browne57e8952010-07-23 21:28:06 -07003216 initialize();
3217 }
3218
3219void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
3220 switch (rawEvent->type) {
3221 case EV_KEY:
3222 switch (rawEvent->scanCode) {
3223 case BTN_TOUCH:
3224 mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH;
3225 mAccumulator.btnTouch = rawEvent->value != 0;
Jeff Brownd64c8552010-08-17 20:38:35 -07003226 // Don't sync immediately. Wait until the next SYN_REPORT since we might
3227 // not have received valid position information yet. This logic assumes that
3228 // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet.
Jeff Browne57e8952010-07-23 21:28:06 -07003229 break;
3230 }
3231 break;
3232
3233 case EV_ABS:
3234 switch (rawEvent->scanCode) {
3235 case ABS_X:
3236 mAccumulator.fields |= Accumulator::FIELD_ABS_X;
3237 mAccumulator.absX = rawEvent->value;
3238 break;
3239 case ABS_Y:
3240 mAccumulator.fields |= Accumulator::FIELD_ABS_Y;
3241 mAccumulator.absY = rawEvent->value;
3242 break;
3243 case ABS_PRESSURE:
3244 mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE;
3245 mAccumulator.absPressure = rawEvent->value;
3246 break;
3247 case ABS_TOOL_WIDTH:
3248 mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH;
3249 mAccumulator.absToolWidth = rawEvent->value;
3250 break;
3251 }
3252 break;
3253
3254 case EV_SYN:
3255 switch (rawEvent->scanCode) {
3256 case SYN_REPORT:
Jeff Brownd64c8552010-08-17 20:38:35 -07003257 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07003258 break;
3259 }
3260 break;
3261 }
3262}
3263
3264void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Browne57e8952010-07-23 21:28:06 -07003265 uint32_t fields = mAccumulator.fields;
Jeff Brownd64c8552010-08-17 20:38:35 -07003266 if (fields == 0) {
3267 return; // no new state changes, so nothing to do
3268 }
Jeff Browne57e8952010-07-23 21:28:06 -07003269
3270 if (fields & Accumulator::FIELD_BTN_TOUCH) {
3271 mDown = mAccumulator.btnTouch;
3272 }
3273
3274 if (fields & Accumulator::FIELD_ABS_X) {
3275 mX = mAccumulator.absX;
3276 }
3277
3278 if (fields & Accumulator::FIELD_ABS_Y) {
3279 mY = mAccumulator.absY;
3280 }
3281
3282 if (fields & Accumulator::FIELD_ABS_PRESSURE) {
3283 mPressure = mAccumulator.absPressure;
3284 }
3285
3286 if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003287 mToolWidth = mAccumulator.absToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -07003288 }
3289
3290 mCurrentTouch.clear();
3291
3292 if (mDown) {
3293 mCurrentTouch.pointerCount = 1;
3294 mCurrentTouch.pointers[0].id = 0;
3295 mCurrentTouch.pointers[0].x = mX;
3296 mCurrentTouch.pointers[0].y = mY;
3297 mCurrentTouch.pointers[0].pressure = mPressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003298 mCurrentTouch.pointers[0].touchMajor = 0;
3299 mCurrentTouch.pointers[0].touchMinor = 0;
3300 mCurrentTouch.pointers[0].toolMajor = mToolWidth;
3301 mCurrentTouch.pointers[0].toolMinor = mToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -07003302 mCurrentTouch.pointers[0].orientation = 0;
3303 mCurrentTouch.idToIndex[0] = 0;
3304 mCurrentTouch.idBits.markBit(0);
3305 }
3306
3307 syncTouch(when, true);
Jeff Brownd64c8552010-08-17 20:38:35 -07003308
3309 mAccumulator.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07003310}
3311
Jeff Brown38a7fab2010-08-30 03:02:23 -07003312void SingleTouchInputMapper::configureRawAxes() {
3313 TouchInputMapper::configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -07003314
Jeff Brown38a7fab2010-08-30 03:02:23 -07003315 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x);
3316 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y);
3317 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure);
3318 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor);
Jeff Browne57e8952010-07-23 21:28:06 -07003319}
3320
3321
3322// --- MultiTouchInputMapper ---
3323
Jeff Brown66888372010-11-29 17:37:49 -08003324MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
3325 TouchInputMapper(device) {
Jeff Browne57e8952010-07-23 21:28:06 -07003326 initialize();
3327}
3328
3329MultiTouchInputMapper::~MultiTouchInputMapper() {
3330}
3331
3332void MultiTouchInputMapper::initialize() {
3333 mAccumulator.clear();
3334}
3335
3336void MultiTouchInputMapper::reset() {
3337 TouchInputMapper::reset();
3338
Jeff Browne57e8952010-07-23 21:28:06 -07003339 initialize();
3340}
3341
3342void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
3343 switch (rawEvent->type) {
3344 case EV_ABS: {
3345 uint32_t pointerIndex = mAccumulator.pointerCount;
3346 Accumulator::Pointer* pointer = & mAccumulator.pointers[pointerIndex];
3347
3348 switch (rawEvent->scanCode) {
3349 case ABS_MT_POSITION_X:
3350 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_X;
3351 pointer->absMTPositionX = rawEvent->value;
3352 break;
3353 case ABS_MT_POSITION_Y:
3354 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y;
3355 pointer->absMTPositionY = rawEvent->value;
3356 break;
3357 case ABS_MT_TOUCH_MAJOR:
3358 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR;
3359 pointer->absMTTouchMajor = rawEvent->value;
3360 break;
3361 case ABS_MT_TOUCH_MINOR:
3362 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR;
3363 pointer->absMTTouchMinor = rawEvent->value;
3364 break;
3365 case ABS_MT_WIDTH_MAJOR:
3366 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR;
3367 pointer->absMTWidthMajor = rawEvent->value;
3368 break;
3369 case ABS_MT_WIDTH_MINOR:
3370 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR;
3371 pointer->absMTWidthMinor = rawEvent->value;
3372 break;
3373 case ABS_MT_ORIENTATION:
3374 pointer->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION;
3375 pointer->absMTOrientation = rawEvent->value;
3376 break;
3377 case ABS_MT_TRACKING_ID:
3378 pointer->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID;
3379 pointer->absMTTrackingId = rawEvent->value;
3380 break;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003381 case ABS_MT_PRESSURE:
3382 pointer->fields |= Accumulator::FIELD_ABS_MT_PRESSURE;
3383 pointer->absMTPressure = rawEvent->value;
3384 break;
Jeff Browne57e8952010-07-23 21:28:06 -07003385 }
3386 break;
3387 }
3388
3389 case EV_SYN:
3390 switch (rawEvent->scanCode) {
3391 case SYN_MT_REPORT: {
3392 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
3393 uint32_t pointerIndex = mAccumulator.pointerCount;
3394
3395 if (mAccumulator.pointers[pointerIndex].fields) {
3396 if (pointerIndex == MAX_POINTERS) {
3397 LOGW("MultiTouch device driver returned more than maximum of %d pointers.",
3398 MAX_POINTERS);
3399 } else {
3400 pointerIndex += 1;
3401 mAccumulator.pointerCount = pointerIndex;
3402 }
3403 }
3404
3405 mAccumulator.pointers[pointerIndex].clear();
3406 break;
3407 }
3408
3409 case SYN_REPORT:
Jeff Brownd64c8552010-08-17 20:38:35 -07003410 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07003411 break;
3412 }
3413 break;
3414 }
3415}
3416
3417void MultiTouchInputMapper::sync(nsecs_t when) {
3418 static const uint32_t REQUIRED_FIELDS =
Jeff Brown38a7fab2010-08-30 03:02:23 -07003419 Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y;
Jeff Browne839a582010-04-22 18:58:52 -07003420
Jeff Browne57e8952010-07-23 21:28:06 -07003421 uint32_t inCount = mAccumulator.pointerCount;
3422 uint32_t outCount = 0;
3423 bool havePointerIds = true;
Jeff Browne839a582010-04-22 18:58:52 -07003424
Jeff Browne57e8952010-07-23 21:28:06 -07003425 mCurrentTouch.clear();
Jeff Browne839a582010-04-22 18:58:52 -07003426
Jeff Browne57e8952010-07-23 21:28:06 -07003427 for (uint32_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003428 const Accumulator::Pointer& inPointer = mAccumulator.pointers[inIndex];
3429 uint32_t fields = inPointer.fields;
Jeff Browne839a582010-04-22 18:58:52 -07003430
Jeff Browne57e8952010-07-23 21:28:06 -07003431 if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003432 // Some drivers send empty MT sync packets without X / Y to indicate a pointer up.
3433 // Drop this finger.
Jeff Browne839a582010-04-22 18:58:52 -07003434 continue;
3435 }
3436
Jeff Brownd64c8552010-08-17 20:38:35 -07003437 PointerData& outPointer = mCurrentTouch.pointers[outCount];
3438 outPointer.x = inPointer.absMTPositionX;
3439 outPointer.y = inPointer.absMTPositionY;
Jeff Browne839a582010-04-22 18:58:52 -07003440
Jeff Brown38a7fab2010-08-30 03:02:23 -07003441 if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) {
3442 if (inPointer.absMTPressure <= 0) {
Jeff Brown3c3cc622010-10-20 15:33:38 -07003443 // Some devices send sync packets with X / Y but with a 0 pressure to indicate
3444 // a pointer going up. Drop this finger.
Jeff Brownd64c8552010-08-17 20:38:35 -07003445 continue;
3446 }
Jeff Brown38a7fab2010-08-30 03:02:23 -07003447 outPointer.pressure = inPointer.absMTPressure;
3448 } else {
3449 // Default pressure to 0 if absent.
3450 outPointer.pressure = 0;
3451 }
3452
3453 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) {
3454 if (inPointer.absMTTouchMajor <= 0) {
3455 // Some devices send sync packets with X / Y but with a 0 touch major to indicate
3456 // a pointer going up. Drop this finger.
3457 continue;
3458 }
Jeff Brownd64c8552010-08-17 20:38:35 -07003459 outPointer.touchMajor = inPointer.absMTTouchMajor;
3460 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003461 // Default touch area to 0 if absent.
Jeff Brownd64c8552010-08-17 20:38:35 -07003462 outPointer.touchMajor = 0;
3463 }
Jeff Browne839a582010-04-22 18:58:52 -07003464
Jeff Brownd64c8552010-08-17 20:38:35 -07003465 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) {
3466 outPointer.touchMinor = inPointer.absMTTouchMinor;
3467 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003468 // Assume touch area is circular.
Jeff Brownd64c8552010-08-17 20:38:35 -07003469 outPointer.touchMinor = outPointer.touchMajor;
3470 }
Jeff Browne839a582010-04-22 18:58:52 -07003471
Jeff Brownd64c8552010-08-17 20:38:35 -07003472 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) {
3473 outPointer.toolMajor = inPointer.absMTWidthMajor;
3474 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003475 // Default tool area to 0 if absent.
3476 outPointer.toolMajor = 0;
Jeff Brownd64c8552010-08-17 20:38:35 -07003477 }
Jeff Browne839a582010-04-22 18:58:52 -07003478
Jeff Brownd64c8552010-08-17 20:38:35 -07003479 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) {
3480 outPointer.toolMinor = inPointer.absMTWidthMinor;
3481 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003482 // Assume tool area is circular.
Jeff Brownd64c8552010-08-17 20:38:35 -07003483 outPointer.toolMinor = outPointer.toolMajor;
3484 }
3485
3486 if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) {
3487 outPointer.orientation = inPointer.absMTOrientation;
3488 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003489 // Default orientation to vertical if absent.
Jeff Brownd64c8552010-08-17 20:38:35 -07003490 outPointer.orientation = 0;
3491 }
3492
Jeff Brown38a7fab2010-08-30 03:02:23 -07003493 // Assign pointer id using tracking id if available.
Jeff Browne57e8952010-07-23 21:28:06 -07003494 if (havePointerIds) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003495 if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) {
3496 uint32_t id = uint32_t(inPointer.absMTTrackingId);
Jeff Browne839a582010-04-22 18:58:52 -07003497
Jeff Browne57e8952010-07-23 21:28:06 -07003498 if (id > MAX_POINTER_ID) {
3499#if DEBUG_POINTERS
3500 LOGD("Pointers: Ignoring driver provided pointer id %d because "
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07003501 "it is larger than max supported id %d",
Jeff Browne57e8952010-07-23 21:28:06 -07003502 id, MAX_POINTER_ID);
3503#endif
3504 havePointerIds = false;
3505 }
3506 else {
Jeff Brownd64c8552010-08-17 20:38:35 -07003507 outPointer.id = id;
Jeff Browne57e8952010-07-23 21:28:06 -07003508 mCurrentTouch.idToIndex[id] = outCount;
3509 mCurrentTouch.idBits.markBit(id);
3510 }
3511 } else {
3512 havePointerIds = false;
Jeff Browne839a582010-04-22 18:58:52 -07003513 }
3514 }
Jeff Browne839a582010-04-22 18:58:52 -07003515
Jeff Browne57e8952010-07-23 21:28:06 -07003516 outCount += 1;
Jeff Browne839a582010-04-22 18:58:52 -07003517 }
3518
Jeff Browne57e8952010-07-23 21:28:06 -07003519 mCurrentTouch.pointerCount = outCount;
Jeff Browne839a582010-04-22 18:58:52 -07003520
Jeff Browne57e8952010-07-23 21:28:06 -07003521 syncTouch(when, havePointerIds);
Jeff Brownd64c8552010-08-17 20:38:35 -07003522
3523 mAccumulator.clear();
Jeff Browne839a582010-04-22 18:58:52 -07003524}
3525
Jeff Brown38a7fab2010-08-30 03:02:23 -07003526void MultiTouchInputMapper::configureRawAxes() {
3527 TouchInputMapper::configureRawAxes();
Jeff Browne839a582010-04-22 18:58:52 -07003528
Jeff Brown38a7fab2010-08-30 03:02:23 -07003529 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, & mRawAxes.x);
3530 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, & mRawAxes.y);
3531 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, & mRawAxes.touchMajor);
3532 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, & mRawAxes.touchMinor);
3533 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, & mRawAxes.toolMajor);
3534 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, & mRawAxes.toolMinor);
3535 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, & mRawAxes.orientation);
3536 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, & mRawAxes.pressure);
Jeff Brown54bc2812010-06-15 01:31:58 -07003537}
3538
Jeff Browne839a582010-04-22 18:58:52 -07003539
3540} // namespace android