blob: a11a0101a42847a573163956702c812b47997be6 [file] [log] [blame]
Jeff Brown46b9ac02010-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 Brown349703e2010-06-22 01:27:15 -070014#define DEBUG_HACKS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070015
16// Log debug messages about virtual key processing.
Jeff Brown349703e2010-06-22 01:27:15 -070017#define DEBUG_VIRTUAL_KEYS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070018
19// Log debug messages about pointers.
Jeff Brown349703e2010-06-22 01:27:15 -070020#define DEBUG_POINTERS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070021
Jeff Brown5c225b12010-06-16 01:53:36 -070022// Log debug messages about pointer assignment calculations.
23#define DEBUG_POINTER_ASSIGNMENT 0
24
Jeff Brown46b9ac02010-04-22 18:58:52 -070025#include <cutils/log.h>
26#include <ui/InputReader.h>
Jeff Brown6b53e8d2010-11-10 16:03:06 -080027#include <ui/Keyboard.h>
Jeff Brown90655042010-12-02 13:50:46 -080028#include <ui/VirtualKeyMap.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070029
30#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070031#include <stdlib.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070032#include <unistd.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070033#include <errno.h>
34#include <limits.h>
Jeff Brownc5ed5912010-07-14 18:48:53 -070035#include <math.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070036
Jeff Brown8d608662010-08-30 03:02:23 -070037#define INDENT " "
Jeff Brownef3d7e82010-09-30 14:33:04 -070038#define INDENT2 " "
39#define INDENT3 " "
40#define INDENT4 " "
Jeff Brown8d608662010-08-30 03:02:23 -070041
Jeff Brown46b9ac02010-04-22 18:58:52 -070042namespace android {
43
44// --- Static Functions ---
45
46template<typename T>
47inline static T abs(const T& value) {
48 return value < 0 ? - value : value;
49}
50
51template<typename T>
52inline static T min(const T& a, const T& b) {
53 return a < b ? a : b;
54}
55
Jeff Brown5c225b12010-06-16 01:53:36 -070056template<typename T>
57inline static void swap(T& a, T& b) {
58 T temp = a;
59 a = b;
60 b = temp;
61}
62
Jeff Brown8d608662010-08-30 03:02:23 -070063inline static float avg(float x, float y) {
64 return (x + y) / 2;
65}
66
67inline static float pythag(float x, float y) {
68 return sqrtf(x * x + y * y);
69}
70
Jeff Brownef3d7e82010-09-30 14:33:04 -070071static inline const char* toString(bool value) {
72 return value ? "true" : "false";
73}
74
Jeff Brown46b9ac02010-04-22 18:58:52 -070075static const int32_t keyCodeRotationMap[][4] = {
76 // key codes enumerated counter-clockwise with the original (unrotated) key first
77 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd035822010-06-30 16:10:35 -070078 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
79 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
80 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
81 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac02010-04-22 18:58:52 -070082};
83static const int keyCodeRotationMapSize =
84 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
85
86int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brown9c3cda02010-06-15 01:31:58 -070087 if (orientation != InputReaderPolicyInterface::ROTATION_0) {
Jeff Brown46b9ac02010-04-22 18:58:52 -070088 for (int i = 0; i < keyCodeRotationMapSize; i++) {
89 if (keyCode == keyCodeRotationMap[i][0]) {
90 return keyCodeRotationMap[i][orientation];
91 }
92 }
93 }
94 return keyCode;
95}
96
Jeff Brown6d0fec22010-07-23 21:28:06 -070097static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
98 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
99}
100
Jeff Brown46b9ac02010-04-22 18:58:52 -0700101
Jeff Brown46b9ac02010-04-22 18:58:52 -0700102// --- InputReader ---
103
104InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700105 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700106 const sp<InputDispatcherInterface>& dispatcher) :
Jeff Brown6d0fec22010-07-23 21:28:06 -0700107 mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher),
108 mGlobalMetaState(0) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700109 configureExcludedDevices();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700110 updateGlobalMetaState();
111 updateInputConfiguration();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700112}
113
114InputReader::~InputReader() {
115 for (size_t i = 0; i < mDevices.size(); i++) {
116 delete mDevices.valueAt(i);
117 }
118}
119
120void InputReader::loopOnce() {
121 RawEvent rawEvent;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700122 mEventHub->getEvent(& rawEvent);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700123
124#if DEBUG_RAW_EVENTS
Jeff Brown90655042010-12-02 13:50:46 -0800125 LOGD("Input event: device=%d type=0x%x scancode=%d keycode=%d value=%d",
Jeff Brown46b9ac02010-04-22 18:58:52 -0700126 rawEvent.deviceId, rawEvent.type, rawEvent.scanCode, rawEvent.keyCode,
127 rawEvent.value);
128#endif
129
130 process(& rawEvent);
131}
132
133void InputReader::process(const RawEvent* rawEvent) {
134 switch (rawEvent->type) {
135 case EventHubInterface::DEVICE_ADDED:
Jeff Brown7342bb92010-10-01 18:55:43 -0700136 addDevice(rawEvent->deviceId);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700137 break;
138
139 case EventHubInterface::DEVICE_REMOVED:
Jeff Brown7342bb92010-10-01 18:55:43 -0700140 removeDevice(rawEvent->deviceId);
141 break;
142
143 case EventHubInterface::FINISHED_DEVICE_SCAN:
Jeff Brownc3db8582010-10-20 15:33:38 -0700144 handleConfigurationChanged(rawEvent->when);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700145 break;
146
Jeff Brown6d0fec22010-07-23 21:28:06 -0700147 default:
148 consumeEvent(rawEvent);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700149 break;
150 }
151}
152
Jeff Brown7342bb92010-10-01 18:55:43 -0700153void InputReader::addDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700154 String8 name = mEventHub->getDeviceName(deviceId);
155 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
156
157 InputDevice* device = createDevice(deviceId, name, classes);
158 device->configure();
159
Jeff Brown8d608662010-08-30 03:02:23 -0700160 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800161 LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700162 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800163 LOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700164 device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700165 }
166
Jeff Brown6d0fec22010-07-23 21:28:06 -0700167 bool added = false;
168 { // acquire device registry writer lock
169 RWLock::AutoWLock _wl(mDeviceRegistryLock);
170
171 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
172 if (deviceIndex < 0) {
173 mDevices.add(deviceId, device);
174 added = true;
175 }
176 } // release device registry writer lock
177
178 if (! added) {
179 LOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
180 delete device;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700181 return;
182 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700183}
184
Jeff Brown7342bb92010-10-01 18:55:43 -0700185void InputReader::removeDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700186 bool removed = false;
187 InputDevice* device = NULL;
188 { // acquire device registry writer lock
189 RWLock::AutoWLock _wl(mDeviceRegistryLock);
190
191 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
192 if (deviceIndex >= 0) {
193 device = mDevices.valueAt(deviceIndex);
194 mDevices.removeItemsAt(deviceIndex, 1);
195 removed = true;
196 }
197 } // release device registry writer lock
198
199 if (! removed) {
200 LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700201 return;
202 }
203
Jeff Brown6d0fec22010-07-23 21:28:06 -0700204 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800205 LOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700206 device->getId(), device->getName().string());
207 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800208 LOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700209 device->getId(), device->getName().string(), device->getSources());
210 }
211
Jeff Brown8d608662010-08-30 03:02:23 -0700212 device->reset();
213
Jeff Brown6d0fec22010-07-23 21:28:06 -0700214 delete device;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700215}
216
Jeff Brown6d0fec22010-07-23 21:28:06 -0700217InputDevice* InputReader::createDevice(int32_t deviceId, const String8& name, uint32_t classes) {
218 InputDevice* device = new InputDevice(this, deviceId, name);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700219
Jeff Brown6d0fec22010-07-23 21:28:06 -0700220 // Switch-like devices.
221 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
222 device->addMapper(new SwitchInputMapper(device));
223 }
224
225 // Keyboard-like devices.
226 uint32_t keyboardSources = 0;
227 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
228 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
229 keyboardSources |= AINPUT_SOURCE_KEYBOARD;
230 }
231 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
232 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
233 }
234 if (classes & INPUT_DEVICE_CLASS_DPAD) {
235 keyboardSources |= AINPUT_SOURCE_DPAD;
236 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700237
238 if (keyboardSources != 0) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800239 device->addMapper(new KeyboardInputMapper(device, keyboardSources, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700240 }
241
Jeff Brown83c09682010-12-23 17:50:18 -0800242 // Cursor-like devices.
243 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
244 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700245 }
246
247 // Touchscreen-like devices.
248 if (classes & INPUT_DEVICE_CLASS_TOUCHSCREEN_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800249 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700250 } else if (classes & INPUT_DEVICE_CLASS_TOUCHSCREEN) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800251 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700252 }
253
254 return device;
255}
256
257void InputReader::consumeEvent(const RawEvent* rawEvent) {
258 int32_t deviceId = rawEvent->deviceId;
259
260 { // acquire device registry reader lock
261 RWLock::AutoRLock _rl(mDeviceRegistryLock);
262
263 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
264 if (deviceIndex < 0) {
265 LOGW("Discarding event for unknown deviceId %d.", deviceId);
266 return;
267 }
268
269 InputDevice* device = mDevices.valueAt(deviceIndex);
270 if (device->isIgnored()) {
271 //LOGD("Discarding event for ignored deviceId %d.", deviceId);
272 return;
273 }
274
275 device->process(rawEvent);
276 } // release device registry reader lock
277}
278
Jeff Brownc3db8582010-10-20 15:33:38 -0700279void InputReader::handleConfigurationChanged(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700280 // Reset global meta state because it depends on the list of all configured devices.
281 updateGlobalMetaState();
282
283 // Update input configuration.
284 updateInputConfiguration();
285
286 // Enqueue configuration changed.
287 mDispatcher->notifyConfigurationChanged(when);
288}
289
290void InputReader::configureExcludedDevices() {
291 Vector<String8> excludedDeviceNames;
292 mPolicy->getExcludedDeviceNames(excludedDeviceNames);
293
294 for (size_t i = 0; i < excludedDeviceNames.size(); i++) {
295 mEventHub->addExcludedDevice(excludedDeviceNames[i]);
296 }
297}
298
299void InputReader::updateGlobalMetaState() {
300 { // acquire state lock
301 AutoMutex _l(mStateLock);
302
303 mGlobalMetaState = 0;
304
305 { // acquire device registry reader lock
306 RWLock::AutoRLock _rl(mDeviceRegistryLock);
307
308 for (size_t i = 0; i < mDevices.size(); i++) {
309 InputDevice* device = mDevices.valueAt(i);
310 mGlobalMetaState |= device->getMetaState();
311 }
312 } // release device registry reader lock
313 } // release state lock
314}
315
316int32_t InputReader::getGlobalMetaState() {
317 { // acquire state lock
318 AutoMutex _l(mStateLock);
319
320 return mGlobalMetaState;
321 } // release state lock
322}
323
324void InputReader::updateInputConfiguration() {
325 { // acquire state lock
326 AutoMutex _l(mStateLock);
327
328 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
329 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
330 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
331 { // acquire device registry reader lock
332 RWLock::AutoRLock _rl(mDeviceRegistryLock);
333
334 InputDeviceInfo deviceInfo;
335 for (size_t i = 0; i < mDevices.size(); i++) {
336 InputDevice* device = mDevices.valueAt(i);
337 device->getDeviceInfo(& deviceInfo);
338 uint32_t sources = deviceInfo.getSources();
339
340 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
341 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
342 }
343 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
344 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
345 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
346 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
347 }
348 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
349 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700350 }
351 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700352 } // release device registry reader lock
Jeff Brown46b9ac02010-04-22 18:58:52 -0700353
Jeff Brown6d0fec22010-07-23 21:28:06 -0700354 mInputConfiguration.touchScreen = touchScreenConfig;
355 mInputConfiguration.keyboard = keyboardConfig;
356 mInputConfiguration.navigation = navigationConfig;
357 } // release state lock
358}
359
360void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
361 { // acquire state lock
362 AutoMutex _l(mStateLock);
363
364 *outConfiguration = mInputConfiguration;
365 } // release state lock
366}
367
368status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
369 { // acquire device registry reader lock
370 RWLock::AutoRLock _rl(mDeviceRegistryLock);
371
372 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
373 if (deviceIndex < 0) {
374 return NAME_NOT_FOUND;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700375 }
376
Jeff Brown6d0fec22010-07-23 21:28:06 -0700377 InputDevice* device = mDevices.valueAt(deviceIndex);
378 if (device->isIgnored()) {
379 return NAME_NOT_FOUND;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700380 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700381
382 device->getDeviceInfo(outDeviceInfo);
383 return OK;
384 } // release device registy reader lock
385}
386
387void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
388 outDeviceIds.clear();
389
390 { // acquire device registry reader lock
391 RWLock::AutoRLock _rl(mDeviceRegistryLock);
392
393 size_t numDevices = mDevices.size();
394 for (size_t i = 0; i < numDevices; i++) {
395 InputDevice* device = mDevices.valueAt(i);
396 if (! device->isIgnored()) {
397 outDeviceIds.add(device->getId());
398 }
399 }
400 } // release device registy reader lock
401}
402
403int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
404 int32_t keyCode) {
405 return getState(deviceId, sourceMask, keyCode, & InputDevice::getKeyCodeState);
406}
407
408int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
409 int32_t scanCode) {
410 return getState(deviceId, sourceMask, scanCode, & InputDevice::getScanCodeState);
411}
412
413int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
414 return getState(deviceId, sourceMask, switchCode, & InputDevice::getSwitchState);
415}
416
417int32_t InputReader::getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
418 GetStateFunc getStateFunc) {
419 { // acquire device registry reader lock
420 RWLock::AutoRLock _rl(mDeviceRegistryLock);
421
422 int32_t result = AKEY_STATE_UNKNOWN;
423 if (deviceId >= 0) {
424 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
425 if (deviceIndex >= 0) {
426 InputDevice* device = mDevices.valueAt(deviceIndex);
427 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
428 result = (device->*getStateFunc)(sourceMask, code);
429 }
430 }
431 } else {
432 size_t numDevices = mDevices.size();
433 for (size_t i = 0; i < numDevices; i++) {
434 InputDevice* device = mDevices.valueAt(i);
435 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
436 result = (device->*getStateFunc)(sourceMask, code);
437 if (result >= AKEY_STATE_DOWN) {
438 return result;
439 }
440 }
441 }
442 }
443 return result;
444 } // release device registy reader lock
445}
446
447bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
448 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
449 memset(outFlags, 0, numCodes);
450 return markSupportedKeyCodes(deviceId, sourceMask, numCodes, keyCodes, outFlags);
451}
452
453bool InputReader::markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
454 const int32_t* keyCodes, uint8_t* outFlags) {
455 { // acquire device registry reader lock
456 RWLock::AutoRLock _rl(mDeviceRegistryLock);
457 bool result = false;
458 if (deviceId >= 0) {
459 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
460 if (deviceIndex >= 0) {
461 InputDevice* device = mDevices.valueAt(deviceIndex);
462 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
463 result = device->markSupportedKeyCodes(sourceMask,
464 numCodes, keyCodes, outFlags);
465 }
466 }
467 } else {
468 size_t numDevices = mDevices.size();
469 for (size_t i = 0; i < numDevices; i++) {
470 InputDevice* device = mDevices.valueAt(i);
471 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
472 result |= device->markSupportedKeyCodes(sourceMask,
473 numCodes, keyCodes, outFlags);
474 }
475 }
476 }
477 return result;
478 } // release device registy reader lock
479}
480
Jeff Brownb88102f2010-09-08 11:49:43 -0700481void InputReader::dump(String8& dump) {
Jeff Brownf2f48712010-10-01 17:46:21 -0700482 mEventHub->dump(dump);
483 dump.append("\n");
484
485 dump.append("Input Reader State:\n");
486
Jeff Brownef3d7e82010-09-30 14:33:04 -0700487 { // acquire device registry reader lock
488 RWLock::AutoRLock _rl(mDeviceRegistryLock);
Jeff Brownb88102f2010-09-08 11:49:43 -0700489
Jeff Brownef3d7e82010-09-30 14:33:04 -0700490 for (size_t i = 0; i < mDevices.size(); i++) {
491 mDevices.valueAt(i)->dump(dump);
Jeff Brownb88102f2010-09-08 11:49:43 -0700492 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700493 } // release device registy reader lock
Jeff Brownb88102f2010-09-08 11:49:43 -0700494}
495
Jeff Brown6d0fec22010-07-23 21:28:06 -0700496
497// --- InputReaderThread ---
498
499InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
500 Thread(/*canCallJava*/ true), mReader(reader) {
501}
502
503InputReaderThread::~InputReaderThread() {
504}
505
506bool InputReaderThread::threadLoop() {
507 mReader->loopOnce();
508 return true;
509}
510
511
512// --- InputDevice ---
513
514InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) :
515 mContext(context), mId(id), mName(name), mSources(0) {
516}
517
518InputDevice::~InputDevice() {
519 size_t numMappers = mMappers.size();
520 for (size_t i = 0; i < numMappers; i++) {
521 delete mMappers[i];
522 }
523 mMappers.clear();
524}
525
Jeff Brownef3d7e82010-09-30 14:33:04 -0700526static void dumpMotionRange(String8& dump, const InputDeviceInfo& deviceInfo,
527 int32_t rangeType, const char* name) {
528 const InputDeviceInfo::MotionRange* range = deviceInfo.getMotionRange(rangeType);
529 if (range) {
530 dump.appendFormat(INDENT3 "%s: min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
531 name, range->min, range->max, range->flat, range->fuzz);
532 }
533}
534
535void InputDevice::dump(String8& dump) {
536 InputDeviceInfo deviceInfo;
537 getDeviceInfo(& deviceInfo);
538
Jeff Brown90655042010-12-02 13:50:46 -0800539 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700540 deviceInfo.getName().string());
541 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
542 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
543 if (!deviceInfo.getMotionRanges().isEmpty()) {
544 dump.append(INDENT2 "Motion Ranges:\n");
545 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_X, "X");
546 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_Y, "Y");
547 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_PRESSURE, "Pressure");
548 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_SIZE, "Size");
549 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOUCH_MAJOR, "TouchMajor");
550 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOUCH_MINOR, "TouchMinor");
551 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOOL_MAJOR, "ToolMajor");
552 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOOL_MINOR, "ToolMinor");
553 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_ORIENTATION, "Orientation");
554 }
555
556 size_t numMappers = mMappers.size();
557 for (size_t i = 0; i < numMappers; i++) {
558 InputMapper* mapper = mMappers[i];
559 mapper->dump(dump);
560 }
561}
562
Jeff Brown6d0fec22010-07-23 21:28:06 -0700563void InputDevice::addMapper(InputMapper* mapper) {
564 mMappers.add(mapper);
565}
566
567void InputDevice::configure() {
Jeff Brown8d608662010-08-30 03:02:23 -0700568 if (! isIgnored()) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800569 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
Jeff Brown8d608662010-08-30 03:02:23 -0700570 }
571
Jeff Brown6d0fec22010-07-23 21:28:06 -0700572 mSources = 0;
573
574 size_t numMappers = mMappers.size();
575 for (size_t i = 0; i < numMappers; i++) {
576 InputMapper* mapper = mMappers[i];
577 mapper->configure();
578 mSources |= mapper->getSources();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700579 }
580}
581
Jeff Brown6d0fec22010-07-23 21:28:06 -0700582void InputDevice::reset() {
583 size_t numMappers = mMappers.size();
584 for (size_t i = 0; i < numMappers; i++) {
585 InputMapper* mapper = mMappers[i];
586 mapper->reset();
587 }
588}
Jeff Brown46b9ac02010-04-22 18:58:52 -0700589
Jeff Brown6d0fec22010-07-23 21:28:06 -0700590void InputDevice::process(const RawEvent* rawEvent) {
591 size_t numMappers = mMappers.size();
592 for (size_t i = 0; i < numMappers; i++) {
593 InputMapper* mapper = mMappers[i];
594 mapper->process(rawEvent);
595 }
596}
Jeff Brown46b9ac02010-04-22 18:58:52 -0700597
Jeff Brown6d0fec22010-07-23 21:28:06 -0700598void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
599 outDeviceInfo->initialize(mId, mName);
600
601 size_t numMappers = mMappers.size();
602 for (size_t i = 0; i < numMappers; i++) {
603 InputMapper* mapper = mMappers[i];
604 mapper->populateDeviceInfo(outDeviceInfo);
605 }
606}
607
608int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
609 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
610}
611
612int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
613 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
614}
615
616int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
617 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
618}
619
620int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
621 int32_t result = AKEY_STATE_UNKNOWN;
622 size_t numMappers = mMappers.size();
623 for (size_t i = 0; i < numMappers; i++) {
624 InputMapper* mapper = mMappers[i];
625 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
626 result = (mapper->*getStateFunc)(sourceMask, code);
627 if (result >= AKEY_STATE_DOWN) {
628 return result;
629 }
630 }
631 }
632 return result;
633}
634
635bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
636 const int32_t* keyCodes, uint8_t* outFlags) {
637 bool result = false;
638 size_t numMappers = mMappers.size();
639 for (size_t i = 0; i < numMappers; i++) {
640 InputMapper* mapper = mMappers[i];
641 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
642 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
643 }
644 }
645 return result;
646}
647
648int32_t InputDevice::getMetaState() {
649 int32_t result = 0;
650 size_t numMappers = mMappers.size();
651 for (size_t i = 0; i < numMappers; i++) {
652 InputMapper* mapper = mMappers[i];
653 result |= mapper->getMetaState();
654 }
655 return result;
656}
657
658
659// --- InputMapper ---
660
661InputMapper::InputMapper(InputDevice* device) :
662 mDevice(device), mContext(device->getContext()) {
663}
664
665InputMapper::~InputMapper() {
666}
667
668void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
669 info->addSource(getSources());
670}
671
Jeff Brownef3d7e82010-09-30 14:33:04 -0700672void InputMapper::dump(String8& dump) {
673}
674
Jeff Brown6d0fec22010-07-23 21:28:06 -0700675void InputMapper::configure() {
676}
677
678void InputMapper::reset() {
679}
680
681int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
682 return AKEY_STATE_UNKNOWN;
683}
684
685int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
686 return AKEY_STATE_UNKNOWN;
687}
688
689int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
690 return AKEY_STATE_UNKNOWN;
691}
692
693bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
694 const int32_t* keyCodes, uint8_t* outFlags) {
695 return false;
696}
697
698int32_t InputMapper::getMetaState() {
699 return 0;
700}
701
Jeff Brown6d0fec22010-07-23 21:28:06 -0700702
703// --- SwitchInputMapper ---
704
705SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
706 InputMapper(device) {
707}
708
709SwitchInputMapper::~SwitchInputMapper() {
710}
711
712uint32_t SwitchInputMapper::getSources() {
713 return 0;
714}
715
716void SwitchInputMapper::process(const RawEvent* rawEvent) {
717 switch (rawEvent->type) {
718 case EV_SW:
719 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
720 break;
721 }
722}
723
724void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownb6997262010-10-08 22:31:17 -0700725 getDispatcher()->notifySwitch(when, switchCode, switchValue, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700726}
727
728int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
729 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
730}
731
732
733// --- KeyboardInputMapper ---
734
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800735KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700736 uint32_t sources, int32_t keyboardType) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800737 InputMapper(device), mSources(sources),
Jeff Brown6d0fec22010-07-23 21:28:06 -0700738 mKeyboardType(keyboardType) {
Jeff Brown6328cdc2010-07-29 18:18:33 -0700739 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700740}
741
742KeyboardInputMapper::~KeyboardInputMapper() {
743}
744
Jeff Brown6328cdc2010-07-29 18:18:33 -0700745void KeyboardInputMapper::initializeLocked() {
746 mLocked.metaState = AMETA_NONE;
747 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700748}
749
750uint32_t KeyboardInputMapper::getSources() {
751 return mSources;
752}
753
754void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
755 InputMapper::populateDeviceInfo(info);
756
757 info->setKeyboardType(mKeyboardType);
758}
759
Jeff Brownef3d7e82010-09-30 14:33:04 -0700760void KeyboardInputMapper::dump(String8& dump) {
761 { // acquire lock
762 AutoMutex _l(mLock);
763 dump.append(INDENT2 "Keyboard Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800764 dumpParameters(dump);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700765 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
766 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mLocked.keyDowns.size());
767 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mLocked.metaState);
768 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
769 } // release lock
770}
771
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800772
773void KeyboardInputMapper::configure() {
774 InputMapper::configure();
775
776 // Configure basic parameters.
777 configureParameters();
Jeff Brown49ed71d2010-12-06 17:13:33 -0800778
779 // Reset LEDs.
780 {
781 AutoMutex _l(mLock);
782 resetLedStateLocked();
783 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800784}
785
786void KeyboardInputMapper::configureParameters() {
787 mParameters.orientationAware = false;
788 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
789 mParameters.orientationAware);
790
791 mParameters.associatedDisplayId = mParameters.orientationAware ? 0 : -1;
792}
793
794void KeyboardInputMapper::dumpParameters(String8& dump) {
795 dump.append(INDENT3 "Parameters:\n");
796 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
797 mParameters.associatedDisplayId);
798 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
799 toString(mParameters.orientationAware));
800}
801
Jeff Brown6d0fec22010-07-23 21:28:06 -0700802void KeyboardInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -0700803 for (;;) {
804 int32_t keyCode, scanCode;
805 { // acquire lock
806 AutoMutex _l(mLock);
807
808 // Synthesize key up event on reset if keys are currently down.
809 if (mLocked.keyDowns.isEmpty()) {
810 initializeLocked();
Jeff Brown49ed71d2010-12-06 17:13:33 -0800811 resetLedStateLocked();
Jeff Brown6328cdc2010-07-29 18:18:33 -0700812 break; // done
813 }
814
815 const KeyDown& keyDown = mLocked.keyDowns.top();
816 keyCode = keyDown.keyCode;
817 scanCode = keyDown.scanCode;
818 } // release lock
819
Jeff Brown6d0fec22010-07-23 21:28:06 -0700820 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700821 processKey(when, false, keyCode, scanCode, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700822 }
823
824 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700825 getContext()->updateGlobalMetaState();
826}
827
828void KeyboardInputMapper::process(const RawEvent* rawEvent) {
829 switch (rawEvent->type) {
830 case EV_KEY: {
831 int32_t scanCode = rawEvent->scanCode;
832 if (isKeyboardOrGamepadKey(scanCode)) {
833 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
834 rawEvent->flags);
835 }
836 break;
837 }
838 }
839}
840
841bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
842 return scanCode < BTN_MOUSE
843 || scanCode >= KEY_OK
844 || (scanCode >= BTN_GAMEPAD && scanCode < BTN_DIGI);
845}
846
Jeff Brown6328cdc2010-07-29 18:18:33 -0700847void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
848 int32_t scanCode, uint32_t policyFlags) {
849 int32_t newMetaState;
850 nsecs_t downTime;
851 bool metaStateChanged = false;
852
853 { // acquire lock
854 AutoMutex _l(mLock);
855
856 if (down) {
857 // Rotate key codes according to orientation if needed.
858 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800859 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -0700860 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800861 if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
862 NULL, NULL, & orientation)) {
Jeff Brown51e7fe72010-10-29 22:19:53 -0700863 orientation = InputReaderPolicyInterface::ROTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700864 }
865
866 keyCode = rotateKeyCode(keyCode, orientation);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700867 }
868
Jeff Brown6328cdc2010-07-29 18:18:33 -0700869 // Add key down.
870 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
871 if (keyDownIndex >= 0) {
872 // key repeat, be sure to use same keycode as before in case of rotation
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800873 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700874 } else {
875 // key down
876 mLocked.keyDowns.push();
877 KeyDown& keyDown = mLocked.keyDowns.editTop();
878 keyDown.keyCode = keyCode;
879 keyDown.scanCode = scanCode;
880 }
881
882 mLocked.downTime = when;
883 } else {
884 // Remove key down.
885 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
886 if (keyDownIndex >= 0) {
887 // key up, be sure to use same keycode as before in case of rotation
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800888 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700889 mLocked.keyDowns.removeAt(size_t(keyDownIndex));
890 } else {
891 // key was not actually down
892 LOGI("Dropping key up from device %s because the key was not down. "
893 "keyCode=%d, scanCode=%d",
894 getDeviceName().string(), keyCode, scanCode);
895 return;
896 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700897 }
898
Jeff Brown6328cdc2010-07-29 18:18:33 -0700899 int32_t oldMetaState = mLocked.metaState;
900 newMetaState = updateMetaState(keyCode, down, oldMetaState);
901 if (oldMetaState != newMetaState) {
902 mLocked.metaState = newMetaState;
903 metaStateChanged = true;
Jeff Brown497a92c2010-09-12 17:55:08 -0700904 updateLedStateLocked(false);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700905 }
Jeff Brownfd035822010-06-30 16:10:35 -0700906
Jeff Brown6328cdc2010-07-29 18:18:33 -0700907 downTime = mLocked.downTime;
908 } // release lock
909
910 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700911 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700912 }
913
Jeff Brown497a92c2010-09-12 17:55:08 -0700914 if (policyFlags & POLICY_FLAG_FUNCTION) {
915 newMetaState |= AMETA_FUNCTION_ON;
916 }
Jeff Brown83c09682010-12-23 17:50:18 -0800917 getDispatcher()->notifyKey(when, getDeviceId(), mSources, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -0700918 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
919 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700920}
921
Jeff Brown6328cdc2010-07-29 18:18:33 -0700922ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) {
923 size_t n = mLocked.keyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700924 for (size_t i = 0; i < n; i++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -0700925 if (mLocked.keyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700926 return i;
927 }
928 }
929 return -1;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700930}
931
Jeff Brown6d0fec22010-07-23 21:28:06 -0700932int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
933 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
934}
Jeff Brown46b9ac02010-04-22 18:58:52 -0700935
Jeff Brown6d0fec22010-07-23 21:28:06 -0700936int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
937 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
938}
Jeff Brown46b9ac02010-04-22 18:58:52 -0700939
Jeff Brown6d0fec22010-07-23 21:28:06 -0700940bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
941 const int32_t* keyCodes, uint8_t* outFlags) {
942 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
943}
944
945int32_t KeyboardInputMapper::getMetaState() {
Jeff Brown6328cdc2010-07-29 18:18:33 -0700946 { // acquire lock
947 AutoMutex _l(mLock);
948 return mLocked.metaState;
949 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -0700950}
951
Jeff Brown49ed71d2010-12-06 17:13:33 -0800952void KeyboardInputMapper::resetLedStateLocked() {
953 initializeLedStateLocked(mLocked.capsLockLedState, LED_CAPSL);
954 initializeLedStateLocked(mLocked.numLockLedState, LED_NUML);
955 initializeLedStateLocked(mLocked.scrollLockLedState, LED_SCROLLL);
956
957 updateLedStateLocked(true);
958}
959
960void KeyboardInputMapper::initializeLedStateLocked(LockedState::LedState& ledState, int32_t led) {
961 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
962 ledState.on = false;
963}
964
Jeff Brown497a92c2010-09-12 17:55:08 -0700965void KeyboardInputMapper::updateLedStateLocked(bool reset) {
966 updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL,
Jeff Brown51e7fe72010-10-29 22:19:53 -0700967 AMETA_CAPS_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -0700968 updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML,
Jeff Brown51e7fe72010-10-29 22:19:53 -0700969 AMETA_NUM_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -0700970 updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe72010-10-29 22:19:53 -0700971 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -0700972}
973
974void KeyboardInputMapper::updateLedStateForModifierLocked(LockedState::LedState& ledState,
975 int32_t led, int32_t modifier, bool reset) {
976 if (ledState.avail) {
977 bool desiredState = (mLocked.metaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800978 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -0700979 getEventHub()->setLedState(getDeviceId(), led, desiredState);
980 ledState.on = desiredState;
981 }
982 }
983}
984
Jeff Brown6d0fec22010-07-23 21:28:06 -0700985
Jeff Brown83c09682010-12-23 17:50:18 -0800986// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -0700987
Jeff Brown83c09682010-12-23 17:50:18 -0800988CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800989 InputMapper(device) {
Jeff Brown6328cdc2010-07-29 18:18:33 -0700990 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700991}
992
Jeff Brown83c09682010-12-23 17:50:18 -0800993CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700994}
995
Jeff Brown83c09682010-12-23 17:50:18 -0800996uint32_t CursorInputMapper::getSources() {
997 return mSources;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700998}
999
Jeff Brown83c09682010-12-23 17:50:18 -08001000void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001001 InputMapper::populateDeviceInfo(info);
1002
Jeff Brown83c09682010-12-23 17:50:18 -08001003 if (mParameters.mode == Parameters::MODE_POINTER) {
1004 float minX, minY, maxX, maxY;
1005 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
1006 info->addMotionRange(AINPUT_MOTION_RANGE_X, minX, maxX, 0.0f, 0.0f);
1007 info->addMotionRange(AINPUT_MOTION_RANGE_Y, minY, maxY, 0.0f, 0.0f);
1008 }
1009 } else {
1010 info->addMotionRange(AINPUT_MOTION_RANGE_X, -1.0f, 1.0f, 0.0f, mXScale);
1011 info->addMotionRange(AINPUT_MOTION_RANGE_Y, -1.0f, 1.0f, 0.0f, mYScale);
1012 }
1013 info->addMotionRange(AINPUT_MOTION_RANGE_PRESSURE, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001014}
1015
Jeff Brown83c09682010-12-23 17:50:18 -08001016void CursorInputMapper::dump(String8& dump) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07001017 { // acquire lock
1018 AutoMutex _l(mLock);
Jeff Brown83c09682010-12-23 17:50:18 -08001019 dump.append(INDENT2 "Cursor Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001020 dumpParameters(dump);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001021 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1022 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
1023 dump.appendFormat(INDENT3 "Down: %s\n", toString(mLocked.down));
1024 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1025 } // release lock
1026}
1027
Jeff Brown83c09682010-12-23 17:50:18 -08001028void CursorInputMapper::configure() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001029 InputMapper::configure();
1030
1031 // Configure basic parameters.
1032 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08001033
1034 // Configure device mode.
1035 switch (mParameters.mode) {
1036 case Parameters::MODE_POINTER:
1037 mSources = AINPUT_SOURCE_MOUSE;
1038 mXPrecision = 1.0f;
1039 mYPrecision = 1.0f;
1040 mXScale = 1.0f;
1041 mYScale = 1.0f;
1042 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
1043 break;
1044 case Parameters::MODE_NAVIGATION:
1045 mSources = AINPUT_SOURCE_TRACKBALL;
1046 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1047 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1048 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1049 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1050 break;
1051 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001052}
1053
Jeff Brown83c09682010-12-23 17:50:18 -08001054void CursorInputMapper::configureParameters() {
1055 mParameters.mode = Parameters::MODE_POINTER;
1056 String8 cursorModeString;
1057 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
1058 if (cursorModeString == "navigation") {
1059 mParameters.mode = Parameters::MODE_NAVIGATION;
1060 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
1061 LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
1062 }
1063 }
1064
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001065 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08001066 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001067 mParameters.orientationAware);
1068
Jeff Brown83c09682010-12-23 17:50:18 -08001069 mParameters.associatedDisplayId = mParameters.mode == Parameters::MODE_POINTER
1070 || mParameters.orientationAware ? 0 : -1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001071}
1072
Jeff Brown83c09682010-12-23 17:50:18 -08001073void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001074 dump.append(INDENT3 "Parameters:\n");
1075 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1076 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08001077
1078 switch (mParameters.mode) {
1079 case Parameters::MODE_POINTER:
1080 dump.append(INDENT4 "Mode: pointer\n");
1081 break;
1082 case Parameters::MODE_NAVIGATION:
1083 dump.append(INDENT4 "Mode: navigation\n");
1084 break;
1085 default:
1086 assert(false);
1087 }
1088
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001089 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1090 toString(mParameters.orientationAware));
1091}
1092
Jeff Brown83c09682010-12-23 17:50:18 -08001093void CursorInputMapper::initializeLocked() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001094 mAccumulator.clear();
1095
Jeff Brown6328cdc2010-07-29 18:18:33 -07001096 mLocked.down = false;
1097 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001098}
1099
Jeff Brown83c09682010-12-23 17:50:18 -08001100void CursorInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001101 for (;;) {
1102 { // acquire lock
1103 AutoMutex _l(mLock);
1104
1105 if (! mLocked.down) {
1106 initializeLocked();
1107 break; // done
1108 }
1109 } // release lock
1110
Jeff Brown83c09682010-12-23 17:50:18 -08001111 // Synthesize button up event on reset.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001112 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001113 mAccumulator.fields = Accumulator::FIELD_BTN_MOUSE;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001114 mAccumulator.btnMouse = false;
1115 sync(when);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001116 }
1117
Jeff Brown6d0fec22010-07-23 21:28:06 -07001118 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001119}
Jeff Brown46b9ac02010-04-22 18:58:52 -07001120
Jeff Brown83c09682010-12-23 17:50:18 -08001121void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001122 switch (rawEvent->type) {
1123 case EV_KEY:
1124 switch (rawEvent->scanCode) {
1125 case BTN_MOUSE:
1126 mAccumulator.fields |= Accumulator::FIELD_BTN_MOUSE;
1127 mAccumulator.btnMouse = rawEvent->value != 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001128 // Sync now since BTN_MOUSE is not necessarily followed by SYN_REPORT and
1129 // we need to ensure that we report the up/down promptly.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001130 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001131 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001132 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001133 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001134
Jeff Brown6d0fec22010-07-23 21:28:06 -07001135 case EV_REL:
1136 switch (rawEvent->scanCode) {
1137 case REL_X:
1138 mAccumulator.fields |= Accumulator::FIELD_REL_X;
1139 mAccumulator.relX = rawEvent->value;
1140 break;
1141 case REL_Y:
1142 mAccumulator.fields |= Accumulator::FIELD_REL_Y;
1143 mAccumulator.relY = rawEvent->value;
1144 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001145 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001146 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001147
Jeff Brown6d0fec22010-07-23 21:28:06 -07001148 case EV_SYN:
1149 switch (rawEvent->scanCode) {
1150 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001151 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001152 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001153 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001154 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001155 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001156}
1157
Jeff Brown83c09682010-12-23 17:50:18 -08001158void CursorInputMapper::sync(nsecs_t when) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001159 uint32_t fields = mAccumulator.fields;
1160 if (fields == 0) {
1161 return; // no new state changes, so nothing to do
1162 }
1163
Jeff Brown6328cdc2010-07-29 18:18:33 -07001164 int motionEventAction;
1165 PointerCoords pointerCoords;
1166 nsecs_t downTime;
1167 { // acquire lock
1168 AutoMutex _l(mLock);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001169
Jeff Brown6328cdc2010-07-29 18:18:33 -07001170 bool downChanged = fields & Accumulator::FIELD_BTN_MOUSE;
1171
1172 if (downChanged) {
1173 if (mAccumulator.btnMouse) {
1174 mLocked.down = true;
1175 mLocked.downTime = when;
1176 } else {
1177 mLocked.down = false;
1178 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001179 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001180
Jeff Brown6328cdc2010-07-29 18:18:33 -07001181 downTime = mLocked.downTime;
Jeff Brown83c09682010-12-23 17:50:18 -08001182 float deltaX = fields & Accumulator::FIELD_REL_X ? mAccumulator.relX * mXScale : 0.0f;
1183 float deltaY = fields & Accumulator::FIELD_REL_Y ? mAccumulator.relY * mYScale : 0.0f;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001184
Jeff Brown6328cdc2010-07-29 18:18:33 -07001185 if (downChanged) {
1186 motionEventAction = mLocked.down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001187 } else {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001188 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001189 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001190
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001191 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
Jeff Brown83c09682010-12-23 17:50:18 -08001192 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001193 // Rotate motion based on display orientation if needed.
1194 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1195 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001196 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1197 NULL, NULL, & orientation)) {
Jeff Brown51e7fe72010-10-29 22:19:53 -07001198 orientation = InputReaderPolicyInterface::ROTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001199 }
1200
1201 float temp;
1202 switch (orientation) {
1203 case InputReaderPolicyInterface::ROTATION_90:
Jeff Brown83c09682010-12-23 17:50:18 -08001204 temp = deltaX;
1205 deltaX = deltaY;
1206 deltaY = -temp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001207 break;
1208
1209 case InputReaderPolicyInterface::ROTATION_180:
Jeff Brown83c09682010-12-23 17:50:18 -08001210 deltaX = -deltaX;
1211 deltaY = -deltaY;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001212 break;
1213
1214 case InputReaderPolicyInterface::ROTATION_270:
Jeff Brown83c09682010-12-23 17:50:18 -08001215 temp = deltaX;
1216 deltaX = -deltaY;
1217 deltaY = temp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001218 break;
1219 }
1220 }
Jeff Brown83c09682010-12-23 17:50:18 -08001221
1222 if (mPointerController != NULL) {
1223 mPointerController->move(deltaX, deltaY);
1224 if (downChanged) {
1225 mPointerController->setButtonState(mLocked.down ? POINTER_BUTTON_1 : 0);
1226 }
1227 mPointerController->getPosition(&pointerCoords.x, &pointerCoords.y);
1228 } else {
1229 pointerCoords.x = deltaX;
1230 pointerCoords.y = deltaY;
1231 }
1232
1233 pointerCoords.pressure = mLocked.down ? 1.0f : 0.0f;
1234 pointerCoords.size = 0;
1235 pointerCoords.touchMajor = 0;
1236 pointerCoords.touchMinor = 0;
1237 pointerCoords.toolMajor = 0;
1238 pointerCoords.toolMinor = 0;
1239 pointerCoords.orientation = 0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001240 } // release lock
1241
Jeff Brown6d0fec22010-07-23 21:28:06 -07001242 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001243 int32_t pointerId = 0;
Jeff Brown83c09682010-12-23 17:50:18 -08001244 getDispatcher()->notifyMotion(when, getDeviceId(), mSources, 0,
Jeff Brown85a31762010-09-01 17:01:00 -07001245 motionEventAction, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brownb6997262010-10-08 22:31:17 -07001246 1, &pointerId, &pointerCoords, mXPrecision, mYPrecision, downTime);
1247
1248 mAccumulator.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001249}
1250
Jeff Brown83c09682010-12-23 17:50:18 -08001251int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07001252 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
1253 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1254 } else {
1255 return AKEY_STATE_UNKNOWN;
1256 }
1257}
1258
Jeff Brown6d0fec22010-07-23 21:28:06 -07001259
1260// --- TouchInputMapper ---
1261
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001262TouchInputMapper::TouchInputMapper(InputDevice* device) :
1263 InputMapper(device) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001264 mLocked.surfaceOrientation = -1;
1265 mLocked.surfaceWidth = -1;
1266 mLocked.surfaceHeight = -1;
1267
1268 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001269}
1270
1271TouchInputMapper::~TouchInputMapper() {
1272}
1273
1274uint32_t TouchInputMapper::getSources() {
Jeff Brown83c09682010-12-23 17:50:18 -08001275 return mSources;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001276}
1277
1278void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1279 InputMapper::populateDeviceInfo(info);
1280
Jeff Brown6328cdc2010-07-29 18:18:33 -07001281 { // acquire lock
1282 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001283
Jeff Brown6328cdc2010-07-29 18:18:33 -07001284 // Ensure surface information is up to date so that orientation changes are
1285 // noticed immediately.
1286 configureSurfaceLocked();
1287
1288 info->addMotionRange(AINPUT_MOTION_RANGE_X, mLocked.orientedRanges.x);
1289 info->addMotionRange(AINPUT_MOTION_RANGE_Y, mLocked.orientedRanges.y);
Jeff Brown8d608662010-08-30 03:02:23 -07001290
1291 if (mLocked.orientedRanges.havePressure) {
1292 info->addMotionRange(AINPUT_MOTION_RANGE_PRESSURE,
1293 mLocked.orientedRanges.pressure);
1294 }
1295
1296 if (mLocked.orientedRanges.haveSize) {
1297 info->addMotionRange(AINPUT_MOTION_RANGE_SIZE,
1298 mLocked.orientedRanges.size);
1299 }
1300
Jeff Brownc6d282b2010-10-14 21:42:15 -07001301 if (mLocked.orientedRanges.haveTouchSize) {
Jeff Brown8d608662010-08-30 03:02:23 -07001302 info->addMotionRange(AINPUT_MOTION_RANGE_TOUCH_MAJOR,
1303 mLocked.orientedRanges.touchMajor);
1304 info->addMotionRange(AINPUT_MOTION_RANGE_TOUCH_MINOR,
1305 mLocked.orientedRanges.touchMinor);
1306 }
1307
Jeff Brownc6d282b2010-10-14 21:42:15 -07001308 if (mLocked.orientedRanges.haveToolSize) {
Jeff Brown8d608662010-08-30 03:02:23 -07001309 info->addMotionRange(AINPUT_MOTION_RANGE_TOOL_MAJOR,
1310 mLocked.orientedRanges.toolMajor);
1311 info->addMotionRange(AINPUT_MOTION_RANGE_TOOL_MINOR,
1312 mLocked.orientedRanges.toolMinor);
1313 }
1314
1315 if (mLocked.orientedRanges.haveOrientation) {
1316 info->addMotionRange(AINPUT_MOTION_RANGE_ORIENTATION,
1317 mLocked.orientedRanges.orientation);
1318 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001319 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001320}
1321
Jeff Brownef3d7e82010-09-30 14:33:04 -07001322void TouchInputMapper::dump(String8& dump) {
1323 { // acquire lock
1324 AutoMutex _l(mLock);
1325 dump.append(INDENT2 "Touch Input Mapper:\n");
Jeff Brownef3d7e82010-09-30 14:33:04 -07001326 dumpParameters(dump);
1327 dumpVirtualKeysLocked(dump);
1328 dumpRawAxes(dump);
1329 dumpCalibration(dump);
1330 dumpSurfaceLocked(dump);
Jeff Brown511ee5f2010-10-18 13:32:20 -07001331 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
Jeff Brownc6d282b2010-10-14 21:42:15 -07001332 dump.appendFormat(INDENT4 "XOrigin: %d\n", mLocked.xOrigin);
1333 dump.appendFormat(INDENT4 "YOrigin: %d\n", mLocked.yOrigin);
1334 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mLocked.xScale);
1335 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mLocked.yScale);
1336 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mLocked.xPrecision);
1337 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mLocked.yPrecision);
1338 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mLocked.geometricScale);
1339 dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mLocked.toolSizeLinearScale);
1340 dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mLocked.toolSizeLinearBias);
1341 dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mLocked.toolSizeAreaScale);
1342 dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mLocked.toolSizeAreaBias);
1343 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mLocked.pressureScale);
1344 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mLocked.sizeScale);
1345 dump.appendFormat(INDENT4 "OrientationSCale: %0.3f\n", mLocked.orientationScale);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001346 } // release lock
1347}
1348
Jeff Brown6328cdc2010-07-29 18:18:33 -07001349void TouchInputMapper::initializeLocked() {
1350 mCurrentTouch.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001351 mLastTouch.clear();
1352 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001353
1354 for (uint32_t i = 0; i < MAX_POINTERS; i++) {
1355 mAveragingTouchFilter.historyStart[i] = 0;
1356 mAveragingTouchFilter.historyEnd[i] = 0;
1357 }
1358
1359 mJumpyTouchFilter.jumpyPointsDropped = 0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001360
1361 mLocked.currentVirtualKey.down = false;
Jeff Brown8d608662010-08-30 03:02:23 -07001362
1363 mLocked.orientedRanges.havePressure = false;
1364 mLocked.orientedRanges.haveSize = false;
Jeff Brownc6d282b2010-10-14 21:42:15 -07001365 mLocked.orientedRanges.haveTouchSize = false;
1366 mLocked.orientedRanges.haveToolSize = false;
Jeff Brown8d608662010-08-30 03:02:23 -07001367 mLocked.orientedRanges.haveOrientation = false;
1368}
1369
Jeff Brown6d0fec22010-07-23 21:28:06 -07001370void TouchInputMapper::configure() {
1371 InputMapper::configure();
1372
1373 // Configure basic parameters.
Jeff Brown8d608662010-08-30 03:02:23 -07001374 configureParameters();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001375
Jeff Brown83c09682010-12-23 17:50:18 -08001376 // Configure sources.
1377 switch (mParameters.deviceType) {
1378 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
1379 mSources = AINPUT_SOURCE_TOUCHSCREEN;
1380 break;
1381 case Parameters::DEVICE_TYPE_TOUCH_PAD:
1382 mSources = AINPUT_SOURCE_TOUCHPAD;
1383 break;
1384 default:
1385 assert(false);
1386 }
1387
Jeff Brown6d0fec22010-07-23 21:28:06 -07001388 // Configure absolute axis information.
Jeff Brown8d608662010-08-30 03:02:23 -07001389 configureRawAxes();
Jeff Brown8d608662010-08-30 03:02:23 -07001390
1391 // Prepare input device calibration.
1392 parseCalibration();
1393 resolveCalibration();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001394
Jeff Brown6328cdc2010-07-29 18:18:33 -07001395 { // acquire lock
1396 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001397
Jeff Brown8d608662010-08-30 03:02:23 -07001398 // Configure surface dimensions and orientation.
Jeff Brown6328cdc2010-07-29 18:18:33 -07001399 configureSurfaceLocked();
1400 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001401}
1402
Jeff Brown8d608662010-08-30 03:02:23 -07001403void TouchInputMapper::configureParameters() {
1404 mParameters.useBadTouchFilter = getPolicy()->filterTouchEvents();
1405 mParameters.useAveragingTouchFilter = getPolicy()->filterTouchEvents();
1406 mParameters.useJumpyTouchFilter = getPolicy()->filterJumpyTouchEvents();
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001407
1408 String8 deviceTypeString;
1409 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
1410 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
1411 deviceTypeString)) {
1412 if (deviceTypeString == "touchPad") {
1413 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
1414 } else if (deviceTypeString != "touchScreen") {
1415 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
1416 }
1417 }
1418 bool isTouchScreen = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
1419
1420 mParameters.orientationAware = isTouchScreen;
1421 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
1422 mParameters.orientationAware);
1423
1424 mParameters.associatedDisplayId = mParameters.orientationAware || isTouchScreen ? 0 : -1;
Jeff Brown8d608662010-08-30 03:02:23 -07001425}
1426
Jeff Brownef3d7e82010-09-30 14:33:04 -07001427void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001428 dump.append(INDENT3 "Parameters:\n");
1429
1430 switch (mParameters.deviceType) {
1431 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
1432 dump.append(INDENT4 "DeviceType: touchScreen\n");
1433 break;
1434 case Parameters::DEVICE_TYPE_TOUCH_PAD:
1435 dump.append(INDENT4 "DeviceType: touchPad\n");
1436 break;
1437 default:
1438 assert(false);
1439 }
1440
1441 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1442 mParameters.associatedDisplayId);
1443 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1444 toString(mParameters.orientationAware));
1445
1446 dump.appendFormat(INDENT4 "UseBadTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07001447 toString(mParameters.useBadTouchFilter));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001448 dump.appendFormat(INDENT4 "UseAveragingTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07001449 toString(mParameters.useAveragingTouchFilter));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001450 dump.appendFormat(INDENT4 "UseJumpyTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07001451 toString(mParameters.useJumpyTouchFilter));
Jeff Brownb88102f2010-09-08 11:49:43 -07001452}
1453
Jeff Brown8d608662010-08-30 03:02:23 -07001454void TouchInputMapper::configureRawAxes() {
1455 mRawAxes.x.clear();
1456 mRawAxes.y.clear();
1457 mRawAxes.pressure.clear();
1458 mRawAxes.touchMajor.clear();
1459 mRawAxes.touchMinor.clear();
1460 mRawAxes.toolMajor.clear();
1461 mRawAxes.toolMinor.clear();
1462 mRawAxes.orientation.clear();
1463}
1464
Jeff Brownef3d7e82010-09-30 14:33:04 -07001465static void dumpAxisInfo(String8& dump, RawAbsoluteAxisInfo axis, const char* name) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001466 if (axis.valid) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07001467 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d\n",
Jeff Brownb88102f2010-09-08 11:49:43 -07001468 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz);
1469 } else {
Jeff Brownef3d7e82010-09-30 14:33:04 -07001470 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
Jeff Brownb88102f2010-09-08 11:49:43 -07001471 }
1472}
1473
Jeff Brownef3d7e82010-09-30 14:33:04 -07001474void TouchInputMapper::dumpRawAxes(String8& dump) {
1475 dump.append(INDENT3 "Raw Axes:\n");
1476 dumpAxisInfo(dump, mRawAxes.x, "X");
1477 dumpAxisInfo(dump, mRawAxes.y, "Y");
1478 dumpAxisInfo(dump, mRawAxes.pressure, "Pressure");
1479 dumpAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor");
1480 dumpAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor");
1481 dumpAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor");
1482 dumpAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor");
1483 dumpAxisInfo(dump, mRawAxes.orientation, "Orientation");
Jeff Brown6d0fec22010-07-23 21:28:06 -07001484}
1485
Jeff Brown6328cdc2010-07-29 18:18:33 -07001486bool TouchInputMapper::configureSurfaceLocked() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001487 // Update orientation and dimensions if needed.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001488 int32_t orientation = InputReaderPolicyInterface::ROTATION_0;
1489 int32_t width = mRawAxes.x.getRange();
1490 int32_t height = mRawAxes.y.getRange();
1491
1492 if (mParameters.associatedDisplayId >= 0) {
1493 bool wantSize = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
1494 bool wantOrientation = mParameters.orientationAware;
1495
Jeff Brown6328cdc2010-07-29 18:18:33 -07001496 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001497 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1498 wantSize ? &width : NULL, wantSize ? &height : NULL,
1499 wantOrientation ? &orientation : NULL)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001500 return false;
1501 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001502 }
1503
Jeff Brown6328cdc2010-07-29 18:18:33 -07001504 bool orientationChanged = mLocked.surfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001505 if (orientationChanged) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001506 mLocked.surfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001507 }
1508
Jeff Brown6328cdc2010-07-29 18:18:33 -07001509 bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001510 if (sizeChanged) {
Jeff Brown90655042010-12-02 13:50:46 -08001511 LOGI("Device reconfigured: id=%d, name='%s', display size is now %dx%d",
Jeff Brownef3d7e82010-09-30 14:33:04 -07001512 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown8d608662010-08-30 03:02:23 -07001513
Jeff Brown6328cdc2010-07-29 18:18:33 -07001514 mLocked.surfaceWidth = width;
1515 mLocked.surfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001516
Jeff Brown8d608662010-08-30 03:02:23 -07001517 // Configure X and Y factors.
1518 if (mRawAxes.x.valid && mRawAxes.y.valid) {
Jeff Brown511ee5f2010-10-18 13:32:20 -07001519 mLocked.xOrigin = mCalibration.haveXOrigin
1520 ? mCalibration.xOrigin
1521 : mRawAxes.x.minValue;
1522 mLocked.yOrigin = mCalibration.haveYOrigin
1523 ? mCalibration.yOrigin
1524 : mRawAxes.y.minValue;
1525 mLocked.xScale = mCalibration.haveXScale
1526 ? mCalibration.xScale
1527 : float(width) / mRawAxes.x.getRange();
1528 mLocked.yScale = mCalibration.haveYScale
1529 ? mCalibration.yScale
1530 : float(height) / mRawAxes.y.getRange();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001531 mLocked.xPrecision = 1.0f / mLocked.xScale;
1532 mLocked.yPrecision = 1.0f / mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001533
Jeff Brown6328cdc2010-07-29 18:18:33 -07001534 configureVirtualKeysLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001535 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07001536 LOGW(INDENT "Touch device did not report support for X or Y axis!");
Jeff Brown6328cdc2010-07-29 18:18:33 -07001537 mLocked.xOrigin = 0;
1538 mLocked.yOrigin = 0;
1539 mLocked.xScale = 1.0f;
1540 mLocked.yScale = 1.0f;
1541 mLocked.xPrecision = 1.0f;
1542 mLocked.yPrecision = 1.0f;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001543 }
1544
Jeff Brown8d608662010-08-30 03:02:23 -07001545 // Scale factor for terms that are not oriented in a particular axis.
1546 // If the pixels are square then xScale == yScale otherwise we fake it
1547 // by choosing an average.
1548 mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001549
Jeff Brown8d608662010-08-30 03:02:23 -07001550 // Size of diagonal axis.
1551 float diagonalSize = pythag(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001552
Jeff Brown8d608662010-08-30 03:02:23 -07001553 // TouchMajor and TouchMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07001554 if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) {
1555 mLocked.orientedRanges.haveTouchSize = true;
Jeff Brown8d608662010-08-30 03:02:23 -07001556 mLocked.orientedRanges.touchMajor.min = 0;
1557 mLocked.orientedRanges.touchMajor.max = diagonalSize;
1558 mLocked.orientedRanges.touchMajor.flat = 0;
1559 mLocked.orientedRanges.touchMajor.fuzz = 0;
1560 mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor;
1561 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001562
Jeff Brown8d608662010-08-30 03:02:23 -07001563 // ToolMajor and ToolMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07001564 mLocked.toolSizeLinearScale = 0;
1565 mLocked.toolSizeLinearBias = 0;
1566 mLocked.toolSizeAreaScale = 0;
1567 mLocked.toolSizeAreaBias = 0;
1568 if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
1569 if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) {
1570 if (mCalibration.haveToolSizeLinearScale) {
1571 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
Jeff Brown8d608662010-08-30 03:02:23 -07001572 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07001573 mLocked.toolSizeLinearScale = float(min(width, height))
Jeff Brown8d608662010-08-30 03:02:23 -07001574 / mRawAxes.toolMajor.maxValue;
1575 }
1576
Jeff Brownc6d282b2010-10-14 21:42:15 -07001577 if (mCalibration.haveToolSizeLinearBias) {
1578 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
1579 }
1580 } else if (mCalibration.toolSizeCalibration ==
1581 Calibration::TOOL_SIZE_CALIBRATION_AREA) {
1582 if (mCalibration.haveToolSizeLinearScale) {
1583 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
1584 } else {
1585 mLocked.toolSizeLinearScale = min(width, height);
1586 }
1587
1588 if (mCalibration.haveToolSizeLinearBias) {
1589 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
1590 }
1591
1592 if (mCalibration.haveToolSizeAreaScale) {
1593 mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale;
1594 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1595 mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue;
1596 }
1597
1598 if (mCalibration.haveToolSizeAreaBias) {
1599 mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -07001600 }
1601 }
1602
Jeff Brownc6d282b2010-10-14 21:42:15 -07001603 mLocked.orientedRanges.haveToolSize = true;
Jeff Brown8d608662010-08-30 03:02:23 -07001604 mLocked.orientedRanges.toolMajor.min = 0;
1605 mLocked.orientedRanges.toolMajor.max = diagonalSize;
1606 mLocked.orientedRanges.toolMajor.flat = 0;
1607 mLocked.orientedRanges.toolMajor.fuzz = 0;
1608 mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor;
1609 }
1610
1611 // Pressure factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07001612 mLocked.pressureScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07001613 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
1614 RawAbsoluteAxisInfo rawPressureAxis;
1615 switch (mCalibration.pressureSource) {
1616 case Calibration::PRESSURE_SOURCE_PRESSURE:
1617 rawPressureAxis = mRawAxes.pressure;
1618 break;
1619 case Calibration::PRESSURE_SOURCE_TOUCH:
1620 rawPressureAxis = mRawAxes.touchMajor;
1621 break;
1622 default:
1623 rawPressureAxis.clear();
1624 }
1625
Jeff Brown8d608662010-08-30 03:02:23 -07001626 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
1627 || mCalibration.pressureCalibration
1628 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
1629 if (mCalibration.havePressureScale) {
1630 mLocked.pressureScale = mCalibration.pressureScale;
1631 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
1632 mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue;
1633 }
1634 }
1635
1636 mLocked.orientedRanges.havePressure = true;
1637 mLocked.orientedRanges.pressure.min = 0;
1638 mLocked.orientedRanges.pressure.max = 1.0;
1639 mLocked.orientedRanges.pressure.flat = 0;
1640 mLocked.orientedRanges.pressure.fuzz = 0;
1641 }
1642
1643 // Size factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07001644 mLocked.sizeScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07001645 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07001646 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
1647 if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1648 mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue;
1649 }
1650 }
1651
1652 mLocked.orientedRanges.haveSize = true;
1653 mLocked.orientedRanges.size.min = 0;
1654 mLocked.orientedRanges.size.max = 1.0;
1655 mLocked.orientedRanges.size.flat = 0;
1656 mLocked.orientedRanges.size.fuzz = 0;
1657 }
1658
1659 // Orientation
Jeff Brownc6d282b2010-10-14 21:42:15 -07001660 mLocked.orientationScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07001661 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07001662 if (mCalibration.orientationCalibration
1663 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
1664 if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) {
1665 mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue;
1666 }
1667 }
1668
1669 mLocked.orientedRanges.orientation.min = - M_PI_2;
1670 mLocked.orientedRanges.orientation.max = M_PI_2;
1671 mLocked.orientedRanges.orientation.flat = 0;
1672 mLocked.orientedRanges.orientation.fuzz = 0;
1673 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001674 }
1675
1676 if (orientationChanged || sizeChanged) {
1677 // Compute oriented surface dimensions, precision, and scales.
1678 float orientedXScale, orientedYScale;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001679 switch (mLocked.surfaceOrientation) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001680 case InputReaderPolicyInterface::ROTATION_90:
1681 case InputReaderPolicyInterface::ROTATION_270:
Jeff Brown6328cdc2010-07-29 18:18:33 -07001682 mLocked.orientedSurfaceWidth = mLocked.surfaceHeight;
1683 mLocked.orientedSurfaceHeight = mLocked.surfaceWidth;
1684 mLocked.orientedXPrecision = mLocked.yPrecision;
1685 mLocked.orientedYPrecision = mLocked.xPrecision;
1686 orientedXScale = mLocked.yScale;
1687 orientedYScale = mLocked.xScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001688 break;
1689 default:
Jeff Brown6328cdc2010-07-29 18:18:33 -07001690 mLocked.orientedSurfaceWidth = mLocked.surfaceWidth;
1691 mLocked.orientedSurfaceHeight = mLocked.surfaceHeight;
1692 mLocked.orientedXPrecision = mLocked.xPrecision;
1693 mLocked.orientedYPrecision = mLocked.yPrecision;
1694 orientedXScale = mLocked.xScale;
1695 orientedYScale = mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001696 break;
1697 }
1698
1699 // Configure position ranges.
Jeff Brown6328cdc2010-07-29 18:18:33 -07001700 mLocked.orientedRanges.x.min = 0;
1701 mLocked.orientedRanges.x.max = mLocked.orientedSurfaceWidth;
1702 mLocked.orientedRanges.x.flat = 0;
1703 mLocked.orientedRanges.x.fuzz = orientedXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001704
Jeff Brown6328cdc2010-07-29 18:18:33 -07001705 mLocked.orientedRanges.y.min = 0;
1706 mLocked.orientedRanges.y.max = mLocked.orientedSurfaceHeight;
1707 mLocked.orientedRanges.y.flat = 0;
1708 mLocked.orientedRanges.y.fuzz = orientedYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001709 }
1710
1711 return true;
1712}
1713
Jeff Brownef3d7e82010-09-30 14:33:04 -07001714void TouchInputMapper::dumpSurfaceLocked(String8& dump) {
1715 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth);
1716 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight);
1717 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07001718}
1719
Jeff Brown6328cdc2010-07-29 18:18:33 -07001720void TouchInputMapper::configureVirtualKeysLocked() {
Jeff Brown8d608662010-08-30 03:02:23 -07001721 assert(mRawAxes.x.valid && mRawAxes.y.valid);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001722
Jeff Brown8d608662010-08-30 03:02:23 -07001723 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08001724 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001725
Jeff Brown6328cdc2010-07-29 18:18:33 -07001726 mLocked.virtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001727
Jeff Brown6328cdc2010-07-29 18:18:33 -07001728 if (virtualKeyDefinitions.size() == 0) {
1729 return;
1730 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001731
Jeff Brown6328cdc2010-07-29 18:18:33 -07001732 mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size());
1733
Jeff Brown8d608662010-08-30 03:02:23 -07001734 int32_t touchScreenLeft = mRawAxes.x.minValue;
1735 int32_t touchScreenTop = mRawAxes.y.minValue;
1736 int32_t touchScreenWidth = mRawAxes.x.getRange();
1737 int32_t touchScreenHeight = mRawAxes.y.getRange();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001738
1739 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07001740 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07001741 virtualKeyDefinitions[i];
1742
1743 mLocked.virtualKeys.add();
1744 VirtualKey& virtualKey = mLocked.virtualKeys.editTop();
1745
1746 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1747 int32_t keyCode;
1748 uint32_t flags;
1749 if (getEventHub()->scancodeToKeycode(getDeviceId(), virtualKey.scanCode,
1750 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07001751 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
1752 virtualKey.scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001753 mLocked.virtualKeys.pop(); // drop the key
1754 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001755 }
1756
Jeff Brown6328cdc2010-07-29 18:18:33 -07001757 virtualKey.keyCode = keyCode;
1758 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001759
Jeff Brown6328cdc2010-07-29 18:18:33 -07001760 // convert the key definition's display coordinates into touch coordinates for a hit box
1761 int32_t halfWidth = virtualKeyDefinition.width / 2;
1762 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001763
Jeff Brown6328cdc2010-07-29 18:18:33 -07001764 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
1765 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1766 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
1767 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1768 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
1769 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
1770 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
1771 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001772
Jeff Brownef3d7e82010-09-30 14:33:04 -07001773 }
1774}
1775
1776void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) {
1777 if (!mLocked.virtualKeys.isEmpty()) {
1778 dump.append(INDENT3 "Virtual Keys:\n");
1779
1780 for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) {
1781 const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i);
1782 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
1783 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1784 i, virtualKey.scanCode, virtualKey.keyCode,
1785 virtualKey.hitLeft, virtualKey.hitRight,
1786 virtualKey.hitTop, virtualKey.hitBottom);
1787 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001788 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001789}
1790
Jeff Brown8d608662010-08-30 03:02:23 -07001791void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001792 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07001793 Calibration& out = mCalibration;
1794
Jeff Brown511ee5f2010-10-18 13:32:20 -07001795 // Position
1796 out.haveXOrigin = in.tryGetProperty(String8("touch.position.xOrigin"), out.xOrigin);
1797 out.haveYOrigin = in.tryGetProperty(String8("touch.position.yOrigin"), out.yOrigin);
1798 out.haveXScale = in.tryGetProperty(String8("touch.position.xScale"), out.xScale);
1799 out.haveYScale = in.tryGetProperty(String8("touch.position.yScale"), out.yScale);
1800
Jeff Brownc6d282b2010-10-14 21:42:15 -07001801 // Touch Size
1802 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT;
1803 String8 touchSizeCalibrationString;
1804 if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) {
1805 if (touchSizeCalibrationString == "none") {
1806 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
1807 } else if (touchSizeCalibrationString == "geometric") {
1808 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC;
1809 } else if (touchSizeCalibrationString == "pressure") {
1810 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
1811 } else if (touchSizeCalibrationString != "default") {
1812 LOGW("Invalid value for touch.touchSize.calibration: '%s'",
1813 touchSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07001814 }
1815 }
1816
Jeff Brownc6d282b2010-10-14 21:42:15 -07001817 // Tool Size
1818 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT;
1819 String8 toolSizeCalibrationString;
1820 if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) {
1821 if (toolSizeCalibrationString == "none") {
1822 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
1823 } else if (toolSizeCalibrationString == "geometric") {
1824 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC;
1825 } else if (toolSizeCalibrationString == "linear") {
1826 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
1827 } else if (toolSizeCalibrationString == "area") {
1828 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA;
1829 } else if (toolSizeCalibrationString != "default") {
1830 LOGW("Invalid value for touch.toolSize.calibration: '%s'",
1831 toolSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07001832 }
1833 }
1834
Jeff Brownc6d282b2010-10-14 21:42:15 -07001835 out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"),
1836 out.toolSizeLinearScale);
1837 out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"),
1838 out.toolSizeLinearBias);
1839 out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"),
1840 out.toolSizeAreaScale);
1841 out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"),
1842 out.toolSizeAreaBias);
1843 out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"),
1844 out.toolSizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07001845
1846 // Pressure
1847 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
1848 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07001849 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07001850 if (pressureCalibrationString == "none") {
1851 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
1852 } else if (pressureCalibrationString == "physical") {
1853 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
1854 } else if (pressureCalibrationString == "amplitude") {
1855 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
1856 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07001857 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07001858 pressureCalibrationString.string());
1859 }
1860 }
1861
1862 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
1863 String8 pressureSourceString;
1864 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
1865 if (pressureSourceString == "pressure") {
1866 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
1867 } else if (pressureSourceString == "touch") {
1868 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
1869 } else if (pressureSourceString != "default") {
1870 LOGW("Invalid value for touch.pressure.source: '%s'",
1871 pressureSourceString.string());
1872 }
1873 }
1874
1875 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
1876 out.pressureScale);
1877
1878 // Size
1879 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
1880 String8 sizeCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07001881 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07001882 if (sizeCalibrationString == "none") {
1883 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
1884 } else if (sizeCalibrationString == "normalized") {
1885 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
1886 } else if (sizeCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07001887 LOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07001888 sizeCalibrationString.string());
1889 }
1890 }
1891
1892 // Orientation
1893 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
1894 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07001895 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07001896 if (orientationCalibrationString == "none") {
1897 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
1898 } else if (orientationCalibrationString == "interpolated") {
1899 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
1900 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07001901 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07001902 orientationCalibrationString.string());
1903 }
1904 }
1905}
1906
1907void TouchInputMapper::resolveCalibration() {
1908 // Pressure
1909 switch (mCalibration.pressureSource) {
1910 case Calibration::PRESSURE_SOURCE_DEFAULT:
1911 if (mRawAxes.pressure.valid) {
1912 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
1913 } else if (mRawAxes.touchMajor.valid) {
1914 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
1915 }
1916 break;
1917
1918 case Calibration::PRESSURE_SOURCE_PRESSURE:
1919 if (! mRawAxes.pressure.valid) {
1920 LOGW("Calibration property touch.pressure.source is 'pressure' but "
1921 "the pressure axis is not available.");
1922 }
1923 break;
1924
1925 case Calibration::PRESSURE_SOURCE_TOUCH:
1926 if (! mRawAxes.touchMajor.valid) {
1927 LOGW("Calibration property touch.pressure.source is 'touch' but "
1928 "the touchMajor axis is not available.");
1929 }
1930 break;
1931
1932 default:
1933 break;
1934 }
1935
1936 switch (mCalibration.pressureCalibration) {
1937 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
1938 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
1939 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
1940 } else {
1941 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
1942 }
1943 break;
1944
1945 default:
1946 break;
1947 }
1948
Jeff Brownc6d282b2010-10-14 21:42:15 -07001949 // Tool Size
1950 switch (mCalibration.toolSizeCalibration) {
1951 case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07001952 if (mRawAxes.toolMajor.valid) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07001953 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
Jeff Brown8d608662010-08-30 03:02:23 -07001954 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07001955 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07001956 }
1957 break;
1958
1959 default:
1960 break;
1961 }
1962
Jeff Brownc6d282b2010-10-14 21:42:15 -07001963 // Touch Size
1964 switch (mCalibration.touchSizeCalibration) {
1965 case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07001966 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
Jeff Brownc6d282b2010-10-14 21:42:15 -07001967 && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
1968 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
Jeff Brown8d608662010-08-30 03:02:23 -07001969 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07001970 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07001971 }
1972 break;
1973
1974 default:
1975 break;
1976 }
1977
1978 // Size
1979 switch (mCalibration.sizeCalibration) {
1980 case Calibration::SIZE_CALIBRATION_DEFAULT:
1981 if (mRawAxes.toolMajor.valid) {
1982 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
1983 } else {
1984 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
1985 }
1986 break;
1987
1988 default:
1989 break;
1990 }
1991
1992 // Orientation
1993 switch (mCalibration.orientationCalibration) {
1994 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
1995 if (mRawAxes.orientation.valid) {
1996 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
1997 } else {
1998 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
1999 }
2000 break;
2001
2002 default:
2003 break;
2004 }
2005}
2006
Jeff Brownef3d7e82010-09-30 14:33:04 -07002007void TouchInputMapper::dumpCalibration(String8& dump) {
2008 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07002009
Jeff Brown511ee5f2010-10-18 13:32:20 -07002010 // Position
2011 if (mCalibration.haveXOrigin) {
2012 dump.appendFormat(INDENT4 "touch.position.xOrigin: %d\n", mCalibration.xOrigin);
2013 }
2014 if (mCalibration.haveYOrigin) {
2015 dump.appendFormat(INDENT4 "touch.position.yOrigin: %d\n", mCalibration.yOrigin);
2016 }
2017 if (mCalibration.haveXScale) {
2018 dump.appendFormat(INDENT4 "touch.position.xScale: %0.3f\n", mCalibration.xScale);
2019 }
2020 if (mCalibration.haveYScale) {
2021 dump.appendFormat(INDENT4 "touch.position.yScale: %0.3f\n", mCalibration.yScale);
2022 }
2023
Jeff Brownc6d282b2010-10-14 21:42:15 -07002024 // Touch Size
2025 switch (mCalibration.touchSizeCalibration) {
2026 case Calibration::TOUCH_SIZE_CALIBRATION_NONE:
2027 dump.append(INDENT4 "touch.touchSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002028 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002029 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
2030 dump.append(INDENT4 "touch.touchSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002031 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002032 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
2033 dump.append(INDENT4 "touch.touchSize.calibration: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002034 break;
2035 default:
2036 assert(false);
2037 }
2038
Jeff Brownc6d282b2010-10-14 21:42:15 -07002039 // Tool Size
2040 switch (mCalibration.toolSizeCalibration) {
2041 case Calibration::TOOL_SIZE_CALIBRATION_NONE:
2042 dump.append(INDENT4 "touch.toolSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002043 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002044 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
2045 dump.append(INDENT4 "touch.toolSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002046 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002047 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
2048 dump.append(INDENT4 "touch.toolSize.calibration: linear\n");
2049 break;
2050 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2051 dump.append(INDENT4 "touch.toolSize.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002052 break;
2053 default:
2054 assert(false);
2055 }
2056
Jeff Brownc6d282b2010-10-14 21:42:15 -07002057 if (mCalibration.haveToolSizeLinearScale) {
2058 dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n",
2059 mCalibration.toolSizeLinearScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002060 }
2061
Jeff Brownc6d282b2010-10-14 21:42:15 -07002062 if (mCalibration.haveToolSizeLinearBias) {
2063 dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n",
2064 mCalibration.toolSizeLinearBias);
Jeff Brown8d608662010-08-30 03:02:23 -07002065 }
2066
Jeff Brownc6d282b2010-10-14 21:42:15 -07002067 if (mCalibration.haveToolSizeAreaScale) {
2068 dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n",
2069 mCalibration.toolSizeAreaScale);
2070 }
2071
2072 if (mCalibration.haveToolSizeAreaBias) {
2073 dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n",
2074 mCalibration.toolSizeAreaBias);
2075 }
2076
2077 if (mCalibration.haveToolSizeIsSummed) {
Jeff Brown1f245102010-11-18 20:53:46 -08002078 dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %s\n",
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002079 toString(mCalibration.toolSizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07002080 }
2081
2082 // Pressure
2083 switch (mCalibration.pressureCalibration) {
2084 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002085 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002086 break;
2087 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002088 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002089 break;
2090 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002091 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002092 break;
2093 default:
2094 assert(false);
2095 }
2096
2097 switch (mCalibration.pressureSource) {
2098 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002099 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002100 break;
2101 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002102 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002103 break;
2104 case Calibration::PRESSURE_SOURCE_DEFAULT:
2105 break;
2106 default:
2107 assert(false);
2108 }
2109
2110 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07002111 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
2112 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002113 }
2114
2115 // Size
2116 switch (mCalibration.sizeCalibration) {
2117 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002118 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002119 break;
2120 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002121 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002122 break;
2123 default:
2124 assert(false);
2125 }
2126
2127 // Orientation
2128 switch (mCalibration.orientationCalibration) {
2129 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002130 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002131 break;
2132 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002133 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002134 break;
2135 default:
2136 assert(false);
2137 }
2138}
2139
Jeff Brown6d0fec22010-07-23 21:28:06 -07002140void TouchInputMapper::reset() {
2141 // Synthesize touch up event if touch is currently down.
2142 // This will also take care of finishing virtual key processing if needed.
2143 if (mLastTouch.pointerCount != 0) {
2144 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
2145 mCurrentTouch.clear();
2146 syncTouch(when, true);
2147 }
2148
Jeff Brown6328cdc2010-07-29 18:18:33 -07002149 { // acquire lock
2150 AutoMutex _l(mLock);
2151 initializeLocked();
2152 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07002153
Jeff Brown6328cdc2010-07-29 18:18:33 -07002154 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002155}
2156
2157void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002158 uint32_t policyFlags = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002159
Jeff Brown6328cdc2010-07-29 18:18:33 -07002160 // Preprocess pointer data.
Jeff Brown46b9ac02010-04-22 18:58:52 -07002161
Jeff Brown6d0fec22010-07-23 21:28:06 -07002162 if (mParameters.useBadTouchFilter) {
2163 if (applyBadTouchFilter()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002164 havePointerIds = false;
2165 }
2166 }
2167
Jeff Brown6d0fec22010-07-23 21:28:06 -07002168 if (mParameters.useJumpyTouchFilter) {
2169 if (applyJumpyTouchFilter()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002170 havePointerIds = false;
2171 }
2172 }
2173
2174 if (! havePointerIds) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002175 calculatePointerIds();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002176 }
2177
Jeff Brown6d0fec22010-07-23 21:28:06 -07002178 TouchData temp;
2179 TouchData* savedTouch;
2180 if (mParameters.useAveragingTouchFilter) {
2181 temp.copyFrom(mCurrentTouch);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002182 savedTouch = & temp;
2183
Jeff Brown6d0fec22010-07-23 21:28:06 -07002184 applyAveragingTouchFilter();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002185 } else {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002186 savedTouch = & mCurrentTouch;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002187 }
2188
Jeff Brown6328cdc2010-07-29 18:18:33 -07002189 // Process touches and virtual keys.
Jeff Brown46b9ac02010-04-22 18:58:52 -07002190
Jeff Brown6d0fec22010-07-23 21:28:06 -07002191 TouchResult touchResult = consumeOffScreenTouches(when, policyFlags);
2192 if (touchResult == DISPATCH_TOUCH) {
2193 dispatchTouches(when, policyFlags);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002194 }
2195
Jeff Brown6328cdc2010-07-29 18:18:33 -07002196 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brown46b9ac02010-04-22 18:58:52 -07002197
Jeff Brown6d0fec22010-07-23 21:28:06 -07002198 if (touchResult == DROP_STROKE) {
2199 mLastTouch.clear();
2200 } else {
2201 mLastTouch.copyFrom(*savedTouch);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002202 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002203}
2204
Jeff Brown6d0fec22010-07-23 21:28:06 -07002205TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches(
2206 nsecs_t when, uint32_t policyFlags) {
2207 int32_t keyEventAction, keyEventFlags;
2208 int32_t keyCode, scanCode, downTime;
2209 TouchResult touchResult;
Jeff Brown349703e2010-06-22 01:27:15 -07002210
Jeff Brown6328cdc2010-07-29 18:18:33 -07002211 { // acquire lock
2212 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002213
Jeff Brown6328cdc2010-07-29 18:18:33 -07002214 // Update surface size and orientation, including virtual key positions.
2215 if (! configureSurfaceLocked()) {
2216 return DROP_STROKE;
2217 }
2218
2219 // Check for virtual key press.
2220 if (mLocked.currentVirtualKey.down) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002221 if (mCurrentTouch.pointerCount == 0) {
2222 // Pointer went up while virtual key was down.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002223 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002224#if DEBUG_VIRTUAL_KEYS
2225 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07002226 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002227#endif
2228 keyEventAction = AKEY_EVENT_ACTION_UP;
2229 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2230 touchResult = SKIP_TOUCH;
2231 goto DispatchVirtualKey;
2232 }
2233
2234 if (mCurrentTouch.pointerCount == 1) {
2235 int32_t x = mCurrentTouch.pointers[0].x;
2236 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002237 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
2238 if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002239 // Pointer is still within the space of the virtual key.
2240 return SKIP_TOUCH;
2241 }
2242 }
2243
2244 // Pointer left virtual key area or another pointer also went down.
2245 // Send key cancellation and drop the stroke so subsequent motions will be
2246 // considered fresh downs. This is useful when the user swipes away from the
2247 // virtual key area into the main display surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002248 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002249#if DEBUG_VIRTUAL_KEYS
2250 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07002251 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002252#endif
2253 keyEventAction = AKEY_EVENT_ACTION_UP;
2254 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
2255 | AKEY_EVENT_FLAG_CANCELED;
Jeff Brownc3db8582010-10-20 15:33:38 -07002256
2257 // Check whether the pointer moved inside the display area where we should
2258 // start a new stroke.
2259 int32_t x = mCurrentTouch.pointers[0].x;
2260 int32_t y = mCurrentTouch.pointers[0].y;
2261 if (isPointInsideSurfaceLocked(x, y)) {
2262 mLastTouch.clear();
2263 touchResult = DISPATCH_TOUCH;
2264 } else {
2265 touchResult = DROP_STROKE;
2266 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002267 } else {
2268 if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) {
2269 // Pointer just went down. Handle off-screen touches, if needed.
2270 int32_t x = mCurrentTouch.pointers[0].x;
2271 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002272 if (! isPointInsideSurfaceLocked(x, y)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002273 // If exactly one pointer went down, check for virtual key hit.
2274 // Otherwise we will drop the entire stroke.
2275 if (mCurrentTouch.pointerCount == 1) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002276 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002277 if (virtualKey) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002278 mLocked.currentVirtualKey.down = true;
2279 mLocked.currentVirtualKey.downTime = when;
2280 mLocked.currentVirtualKey.keyCode = virtualKey->keyCode;
2281 mLocked.currentVirtualKey.scanCode = virtualKey->scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002282#if DEBUG_VIRTUAL_KEYS
2283 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07002284 mLocked.currentVirtualKey.keyCode,
2285 mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002286#endif
2287 keyEventAction = AKEY_EVENT_ACTION_DOWN;
2288 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM
2289 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2290 touchResult = SKIP_TOUCH;
2291 goto DispatchVirtualKey;
2292 }
2293 }
2294 return DROP_STROKE;
2295 }
2296 }
2297 return DISPATCH_TOUCH;
2298 }
2299
2300 DispatchVirtualKey:
2301 // Collect remaining state needed to dispatch virtual key.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002302 keyCode = mLocked.currentVirtualKey.keyCode;
2303 scanCode = mLocked.currentVirtualKey.scanCode;
2304 downTime = mLocked.currentVirtualKey.downTime;
2305 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07002306
2307 // Dispatch virtual key.
2308 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown0eaf3932010-10-01 14:55:30 -07002309 policyFlags |= POLICY_FLAG_VIRTUAL;
Jeff Brownb6997262010-10-08 22:31:17 -07002310 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
2311 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
2312 return touchResult;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002313}
2314
Jeff Brown6d0fec22010-07-23 21:28:06 -07002315void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
2316 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2317 uint32_t lastPointerCount = mLastTouch.pointerCount;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002318 if (currentPointerCount == 0 && lastPointerCount == 0) {
2319 return; // nothing to do!
2320 }
2321
Jeff Brown6d0fec22010-07-23 21:28:06 -07002322 BitSet32 currentIdBits = mCurrentTouch.idBits;
2323 BitSet32 lastIdBits = mLastTouch.idBits;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002324
2325 if (currentIdBits == lastIdBits) {
2326 // No pointer id changes so this is a move event.
2327 // The dispatcher takes care of batching moves so we don't have to deal with that here.
Jeff Brownc5ed5912010-07-14 18:48:53 -07002328 int32_t motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002329 dispatchTouch(when, policyFlags, & mCurrentTouch,
Jeff Brown8d608662010-08-30 03:02:23 -07002330 currentIdBits, -1, currentPointerCount, motionEventAction);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002331 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07002332 // There may be pointers going up and pointers going down and pointers moving
2333 // all at the same time.
Jeff Brown46b9ac02010-04-22 18:58:52 -07002334 BitSet32 upIdBits(lastIdBits.value & ~ currentIdBits.value);
2335 BitSet32 downIdBits(currentIdBits.value & ~ lastIdBits.value);
2336 BitSet32 activeIdBits(lastIdBits.value);
Jeff Brown8d608662010-08-30 03:02:23 -07002337 uint32_t pointerCount = lastPointerCount;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002338
Jeff Brownc3db8582010-10-20 15:33:38 -07002339 // Produce an intermediate representation of the touch data that consists of the
2340 // old location of pointers that have just gone up and the new location of pointers that
2341 // have just moved but omits the location of pointers that have just gone down.
2342 TouchData interimTouch;
2343 interimTouch.copyFrom(mLastTouch);
2344
2345 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
2346 bool moveNeeded = false;
2347 while (!moveIdBits.isEmpty()) {
2348 uint32_t moveId = moveIdBits.firstMarkedBit();
2349 moveIdBits.clearBit(moveId);
2350
2351 int32_t oldIndex = mLastTouch.idToIndex[moveId];
2352 int32_t newIndex = mCurrentTouch.idToIndex[moveId];
2353 if (mLastTouch.pointers[oldIndex] != mCurrentTouch.pointers[newIndex]) {
2354 interimTouch.pointers[oldIndex] = mCurrentTouch.pointers[newIndex];
2355 moveNeeded = true;
2356 }
2357 }
2358
2359 // Dispatch pointer up events using the interim pointer locations.
2360 while (!upIdBits.isEmpty()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002361 uint32_t upId = upIdBits.firstMarkedBit();
2362 upIdBits.clearBit(upId);
2363 BitSet32 oldActiveIdBits = activeIdBits;
2364 activeIdBits.clearBit(upId);
2365
2366 int32_t motionEventAction;
2367 if (activeIdBits.isEmpty()) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07002368 motionEventAction = AMOTION_EVENT_ACTION_UP;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002369 } else {
Jeff Brown00ba8842010-07-16 15:01:56 -07002370 motionEventAction = AMOTION_EVENT_ACTION_POINTER_UP;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002371 }
2372
Jeff Brownc3db8582010-10-20 15:33:38 -07002373 dispatchTouch(when, policyFlags, &interimTouch,
Jeff Brown8d608662010-08-30 03:02:23 -07002374 oldActiveIdBits, upId, pointerCount, motionEventAction);
2375 pointerCount -= 1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002376 }
2377
Jeff Brownc3db8582010-10-20 15:33:38 -07002378 // Dispatch move events if any of the remaining pointers moved from their old locations.
2379 // Although applications receive new locations as part of individual pointer up
2380 // events, they do not generally handle them except when presented in a move event.
2381 if (moveNeeded) {
2382 dispatchTouch(when, policyFlags, &mCurrentTouch,
2383 activeIdBits, -1, pointerCount, AMOTION_EVENT_ACTION_MOVE);
2384 }
2385
2386 // Dispatch pointer down events using the new pointer locations.
2387 while (!downIdBits.isEmpty()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002388 uint32_t downId = downIdBits.firstMarkedBit();
2389 downIdBits.clearBit(downId);
2390 BitSet32 oldActiveIdBits = activeIdBits;
2391 activeIdBits.markBit(downId);
2392
2393 int32_t motionEventAction;
2394 if (oldActiveIdBits.isEmpty()) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07002395 motionEventAction = AMOTION_EVENT_ACTION_DOWN;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002396 mDownTime = when;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002397 } else {
Jeff Brown00ba8842010-07-16 15:01:56 -07002398 motionEventAction = AMOTION_EVENT_ACTION_POINTER_DOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002399 }
2400
Jeff Brown8d608662010-08-30 03:02:23 -07002401 pointerCount += 1;
Jeff Brownc3db8582010-10-20 15:33:38 -07002402 dispatchTouch(when, policyFlags, &mCurrentTouch,
Jeff Brown8d608662010-08-30 03:02:23 -07002403 activeIdBits, downId, pointerCount, motionEventAction);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002404 }
2405 }
2406}
2407
Jeff Brown6d0fec22010-07-23 21:28:06 -07002408void TouchInputMapper::dispatchTouch(nsecs_t when, uint32_t policyFlags,
Jeff Brown8d608662010-08-30 03:02:23 -07002409 TouchData* touch, BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
Jeff Brown46b9ac02010-04-22 18:58:52 -07002410 int32_t motionEventAction) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002411 int32_t pointerIds[MAX_POINTERS];
2412 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brown46b9ac02010-04-22 18:58:52 -07002413 int32_t motionEventEdgeFlags = 0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002414 float xPrecision, yPrecision;
2415
2416 { // acquire lock
2417 AutoMutex _l(mLock);
2418
2419 // Walk through the the active pointers and map touch screen coordinates (TouchData) into
2420 // display coordinates (PointerCoords) and adjust for display orientation.
Jeff Brown8d608662010-08-30 03:02:23 -07002421 for (uint32_t outIndex = 0; ! idBits.isEmpty(); outIndex++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002422 uint32_t id = idBits.firstMarkedBit();
2423 idBits.clearBit(id);
Jeff Brown8d608662010-08-30 03:02:23 -07002424 uint32_t inIndex = touch->idToIndex[id];
Jeff Brown6328cdc2010-07-29 18:18:33 -07002425
Jeff Brown8d608662010-08-30 03:02:23 -07002426 const PointerData& in = touch->pointers[inIndex];
Jeff Brown6328cdc2010-07-29 18:18:33 -07002427
Jeff Brown8d608662010-08-30 03:02:23 -07002428 // X and Y
2429 float x = float(in.x - mLocked.xOrigin) * mLocked.xScale;
2430 float y = float(in.y - mLocked.yOrigin) * mLocked.yScale;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002431
Jeff Brown8d608662010-08-30 03:02:23 -07002432 // ToolMajor and ToolMinor
2433 float toolMajor, toolMinor;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002434 switch (mCalibration.toolSizeCalibration) {
2435 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brown8d608662010-08-30 03:02:23 -07002436 toolMajor = in.toolMajor * mLocked.geometricScale;
2437 if (mRawAxes.toolMinor.valid) {
2438 toolMinor = in.toolMinor * mLocked.geometricScale;
2439 } else {
2440 toolMinor = toolMajor;
2441 }
2442 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002443 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
Jeff Brown8d608662010-08-30 03:02:23 -07002444 toolMajor = in.toolMajor != 0
Jeff Brownc6d282b2010-10-14 21:42:15 -07002445 ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias
Jeff Brown8d608662010-08-30 03:02:23 -07002446 : 0;
2447 if (mRawAxes.toolMinor.valid) {
2448 toolMinor = in.toolMinor != 0
Jeff Brownc6d282b2010-10-14 21:42:15 -07002449 ? in.toolMinor * mLocked.toolSizeLinearScale
2450 + mLocked.toolSizeLinearBias
Jeff Brown8d608662010-08-30 03:02:23 -07002451 : 0;
2452 } else {
2453 toolMinor = toolMajor;
2454 }
2455 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002456 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2457 if (in.toolMajor != 0) {
2458 float diameter = sqrtf(in.toolMajor
2459 * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias);
2460 toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias;
2461 } else {
2462 toolMajor = 0;
2463 }
2464 toolMinor = toolMajor;
2465 break;
Jeff Brown8d608662010-08-30 03:02:23 -07002466 default:
2467 toolMajor = 0;
2468 toolMinor = 0;
2469 break;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002470 }
2471
Jeff Brownc6d282b2010-10-14 21:42:15 -07002472 if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) {
Jeff Brown8d608662010-08-30 03:02:23 -07002473 toolMajor /= pointerCount;
2474 toolMinor /= pointerCount;
2475 }
2476
2477 // Pressure
2478 float rawPressure;
2479 switch (mCalibration.pressureSource) {
2480 case Calibration::PRESSURE_SOURCE_PRESSURE:
2481 rawPressure = in.pressure;
2482 break;
2483 case Calibration::PRESSURE_SOURCE_TOUCH:
2484 rawPressure = in.touchMajor;
2485 break;
2486 default:
2487 rawPressure = 0;
2488 }
2489
2490 float pressure;
2491 switch (mCalibration.pressureCalibration) {
2492 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
2493 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
2494 pressure = rawPressure * mLocked.pressureScale;
2495 break;
2496 default:
2497 pressure = 1;
2498 break;
2499 }
2500
2501 // TouchMajor and TouchMinor
2502 float touchMajor, touchMinor;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002503 switch (mCalibration.touchSizeCalibration) {
2504 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brown8d608662010-08-30 03:02:23 -07002505 touchMajor = in.touchMajor * mLocked.geometricScale;
2506 if (mRawAxes.touchMinor.valid) {
2507 touchMinor = in.touchMinor * mLocked.geometricScale;
2508 } else {
2509 touchMinor = touchMajor;
2510 }
2511 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002512 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
Jeff Brown8d608662010-08-30 03:02:23 -07002513 touchMajor = toolMajor * pressure;
2514 touchMinor = toolMinor * pressure;
2515 break;
2516 default:
2517 touchMajor = 0;
2518 touchMinor = 0;
2519 break;
2520 }
2521
2522 if (touchMajor > toolMajor) {
2523 touchMajor = toolMajor;
2524 }
2525 if (touchMinor > toolMinor) {
2526 touchMinor = toolMinor;
2527 }
2528
2529 // Size
2530 float size;
2531 switch (mCalibration.sizeCalibration) {
2532 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
2533 float rawSize = mRawAxes.toolMinor.valid
2534 ? avg(in.toolMajor, in.toolMinor)
2535 : in.toolMajor;
2536 size = rawSize * mLocked.sizeScale;
2537 break;
2538 }
2539 default:
2540 size = 0;
2541 break;
2542 }
2543
2544 // Orientation
2545 float orientation;
2546 switch (mCalibration.orientationCalibration) {
2547 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
2548 orientation = in.orientation * mLocked.orientationScale;
2549 break;
2550 default:
2551 orientation = 0;
2552 }
2553
2554 // Adjust coords for orientation.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002555 switch (mLocked.surfaceOrientation) {
2556 case InputReaderPolicyInterface::ROTATION_90: {
2557 float xTemp = x;
2558 x = y;
2559 y = mLocked.surfaceWidth - xTemp;
2560 orientation -= M_PI_2;
2561 if (orientation < - M_PI_2) {
2562 orientation += M_PI;
2563 }
2564 break;
2565 }
2566 case InputReaderPolicyInterface::ROTATION_180: {
2567 x = mLocked.surfaceWidth - x;
2568 y = mLocked.surfaceHeight - y;
2569 orientation = - orientation;
2570 break;
2571 }
2572 case InputReaderPolicyInterface::ROTATION_270: {
2573 float xTemp = x;
2574 x = mLocked.surfaceHeight - y;
2575 y = xTemp;
2576 orientation += M_PI_2;
2577 if (orientation > M_PI_2) {
2578 orientation -= M_PI;
2579 }
2580 break;
2581 }
2582 }
2583
Jeff Brown8d608662010-08-30 03:02:23 -07002584 // Write output coords.
2585 PointerCoords& out = pointerCoords[outIndex];
2586 out.x = x;
2587 out.y = y;
2588 out.pressure = pressure;
2589 out.size = size;
2590 out.touchMajor = touchMajor;
2591 out.touchMinor = touchMinor;
2592 out.toolMajor = toolMajor;
2593 out.toolMinor = toolMinor;
2594 out.orientation = orientation;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002595
Jeff Brown8d608662010-08-30 03:02:23 -07002596 pointerIds[outIndex] = int32_t(id);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002597
2598 if (id == changedId) {
Jeff Brown8d608662010-08-30 03:02:23 -07002599 motionEventAction |= outIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002600 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002601 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002602
2603 // Check edge flags by looking only at the first pointer since the flags are
2604 // global to the event.
2605 if (motionEventAction == AMOTION_EVENT_ACTION_DOWN) {
2606 if (pointerCoords[0].x <= 0) {
2607 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_LEFT;
2608 } else if (pointerCoords[0].x >= mLocked.orientedSurfaceWidth) {
2609 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_RIGHT;
2610 }
2611 if (pointerCoords[0].y <= 0) {
2612 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_TOP;
2613 } else if (pointerCoords[0].y >= mLocked.orientedSurfaceHeight) {
2614 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_BOTTOM;
2615 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002616 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002617
2618 xPrecision = mLocked.orientedXPrecision;
2619 yPrecision = mLocked.orientedYPrecision;
2620 } // release lock
Jeff Brown46b9ac02010-04-22 18:58:52 -07002621
Jeff Brown83c09682010-12-23 17:50:18 -08002622 getDispatcher()->notifyMotion(when, getDeviceId(), mSources, policyFlags,
Jeff Brown85a31762010-09-01 17:01:00 -07002623 motionEventAction, 0, getContext()->getGlobalMetaState(), motionEventEdgeFlags,
Jeff Brown46b9ac02010-04-22 18:58:52 -07002624 pointerCount, pointerIds, pointerCoords,
Jeff Brown6328cdc2010-07-29 18:18:33 -07002625 xPrecision, yPrecision, mDownTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002626}
2627
Jeff Brown6328cdc2010-07-29 18:18:33 -07002628bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) {
Jeff Brown8d608662010-08-30 03:02:23 -07002629 if (mRawAxes.x.valid && mRawAxes.y.valid) {
2630 return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue
2631 && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002632 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002633 return true;
2634}
2635
Jeff Brown6328cdc2010-07-29 18:18:33 -07002636const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked(
2637 int32_t x, int32_t y) {
2638 size_t numVirtualKeys = mLocked.virtualKeys.size();
2639 for (size_t i = 0; i < numVirtualKeys; i++) {
2640 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07002641
2642#if DEBUG_VIRTUAL_KEYS
2643 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
2644 "left=%d, top=%d, right=%d, bottom=%d",
2645 x, y,
2646 virtualKey.keyCode, virtualKey.scanCode,
2647 virtualKey.hitLeft, virtualKey.hitTop,
2648 virtualKey.hitRight, virtualKey.hitBottom);
2649#endif
2650
2651 if (virtualKey.isHit(x, y)) {
2652 return & virtualKey;
2653 }
2654 }
2655
2656 return NULL;
2657}
2658
2659void TouchInputMapper::calculatePointerIds() {
2660 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2661 uint32_t lastPointerCount = mLastTouch.pointerCount;
2662
2663 if (currentPointerCount == 0) {
2664 // No pointers to assign.
2665 mCurrentTouch.idBits.clear();
2666 } else if (lastPointerCount == 0) {
2667 // All pointers are new.
2668 mCurrentTouch.idBits.clear();
2669 for (uint32_t i = 0; i < currentPointerCount; i++) {
2670 mCurrentTouch.pointers[i].id = i;
2671 mCurrentTouch.idToIndex[i] = i;
2672 mCurrentTouch.idBits.markBit(i);
2673 }
2674 } else if (currentPointerCount == 1 && lastPointerCount == 1) {
2675 // Only one pointer and no change in count so it must have the same id as before.
2676 uint32_t id = mLastTouch.pointers[0].id;
2677 mCurrentTouch.pointers[0].id = id;
2678 mCurrentTouch.idToIndex[id] = 0;
2679 mCurrentTouch.idBits.value = BitSet32::valueForBit(id);
2680 } else {
2681 // General case.
2682 // We build a heap of squared euclidean distances between current and last pointers
2683 // associated with the current and last pointer indices. Then, we find the best
2684 // match (by distance) for each current pointer.
2685 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
2686
2687 uint32_t heapSize = 0;
2688 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
2689 currentPointerIndex++) {
2690 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
2691 lastPointerIndex++) {
2692 int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x
2693 - mLastTouch.pointers[lastPointerIndex].x;
2694 int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y
2695 - mLastTouch.pointers[lastPointerIndex].y;
2696
2697 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
2698
2699 // Insert new element into the heap (sift up).
2700 heap[heapSize].currentPointerIndex = currentPointerIndex;
2701 heap[heapSize].lastPointerIndex = lastPointerIndex;
2702 heap[heapSize].distance = distance;
2703 heapSize += 1;
2704 }
2705 }
2706
2707 // Heapify
2708 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
2709 startIndex -= 1;
2710 for (uint32_t parentIndex = startIndex; ;) {
2711 uint32_t childIndex = parentIndex * 2 + 1;
2712 if (childIndex >= heapSize) {
2713 break;
2714 }
2715
2716 if (childIndex + 1 < heapSize
2717 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2718 childIndex += 1;
2719 }
2720
2721 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2722 break;
2723 }
2724
2725 swap(heap[parentIndex], heap[childIndex]);
2726 parentIndex = childIndex;
2727 }
2728 }
2729
2730#if DEBUG_POINTER_ASSIGNMENT
2731 LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize);
2732 for (size_t i = 0; i < heapSize; i++) {
2733 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2734 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2735 heap[i].distance);
2736 }
2737#endif
2738
2739 // Pull matches out by increasing order of distance.
2740 // To avoid reassigning pointers that have already been matched, the loop keeps track
2741 // of which last and current pointers have been matched using the matchedXXXBits variables.
2742 // It also tracks the used pointer id bits.
2743 BitSet32 matchedLastBits(0);
2744 BitSet32 matchedCurrentBits(0);
2745 BitSet32 usedIdBits(0);
2746 bool first = true;
2747 for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) {
2748 for (;;) {
2749 if (first) {
2750 // The first time through the loop, we just consume the root element of
2751 // the heap (the one with smallest distance).
2752 first = false;
2753 } else {
2754 // Previous iterations consumed the root element of the heap.
2755 // Pop root element off of the heap (sift down).
2756 heapSize -= 1;
2757 assert(heapSize > 0);
2758
2759 // Sift down.
2760 heap[0] = heap[heapSize];
2761 for (uint32_t parentIndex = 0; ;) {
2762 uint32_t childIndex = parentIndex * 2 + 1;
2763 if (childIndex >= heapSize) {
2764 break;
2765 }
2766
2767 if (childIndex + 1 < heapSize
2768 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2769 childIndex += 1;
2770 }
2771
2772 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2773 break;
2774 }
2775
2776 swap(heap[parentIndex], heap[childIndex]);
2777 parentIndex = childIndex;
2778 }
2779
2780#if DEBUG_POINTER_ASSIGNMENT
2781 LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize);
2782 for (size_t i = 0; i < heapSize; i++) {
2783 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2784 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2785 heap[i].distance);
2786 }
2787#endif
2788 }
2789
2790 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
2791 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
2792
2793 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
2794 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
2795
2796 matchedCurrentBits.markBit(currentPointerIndex);
2797 matchedLastBits.markBit(lastPointerIndex);
2798
2799 uint32_t id = mLastTouch.pointers[lastPointerIndex].id;
2800 mCurrentTouch.pointers[currentPointerIndex].id = id;
2801 mCurrentTouch.idToIndex[id] = currentPointerIndex;
2802 usedIdBits.markBit(id);
2803
2804#if DEBUG_POINTER_ASSIGNMENT
2805 LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
2806 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
2807#endif
2808 break;
2809 }
2810 }
2811
2812 // Assign fresh ids to new pointers.
2813 if (currentPointerCount > lastPointerCount) {
2814 for (uint32_t i = currentPointerCount - lastPointerCount; ;) {
2815 uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit();
2816 uint32_t id = usedIdBits.firstUnmarkedBit();
2817
2818 mCurrentTouch.pointers[currentPointerIndex].id = id;
2819 mCurrentTouch.idToIndex[id] = currentPointerIndex;
2820 usedIdBits.markBit(id);
2821
2822#if DEBUG_POINTER_ASSIGNMENT
2823 LOGD("calculatePointerIds - assigned: cur=%d, id=%d",
2824 currentPointerIndex, id);
2825#endif
2826
2827 if (--i == 0) break; // done
2828 matchedCurrentBits.markBit(currentPointerIndex);
2829 }
2830 }
2831
2832 // Fix id bits.
2833 mCurrentTouch.idBits = usedIdBits;
2834 }
2835}
2836
2837/* Special hack for devices that have bad screen data: if one of the
2838 * points has moved more than a screen height from the last position,
2839 * then drop it. */
2840bool TouchInputMapper::applyBadTouchFilter() {
2841 // This hack requires valid axis parameters.
Jeff Brown8d608662010-08-30 03:02:23 -07002842 if (! mRawAxes.y.valid) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002843 return false;
2844 }
2845
2846 uint32_t pointerCount = mCurrentTouch.pointerCount;
2847
2848 // Nothing to do if there are no points.
2849 if (pointerCount == 0) {
2850 return false;
2851 }
2852
2853 // Don't do anything if a finger is going down or up. We run
2854 // here before assigning pointer IDs, so there isn't a good
2855 // way to do per-finger matching.
2856 if (pointerCount != mLastTouch.pointerCount) {
2857 return false;
2858 }
2859
2860 // We consider a single movement across more than a 7/16 of
2861 // the long size of the screen to be bad. This was a magic value
2862 // determined by looking at the maximum distance it is feasible
2863 // to actually move in one sample.
Jeff Brown8d608662010-08-30 03:02:23 -07002864 int32_t maxDeltaY = mRawAxes.y.getRange() * 7 / 16;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002865
2866 // XXX The original code in InputDevice.java included commented out
2867 // code for testing the X axis. Note that when we drop a point
2868 // we don't actually restore the old X either. Strange.
2869 // The old code also tries to track when bad points were previously
2870 // detected but it turns out that due to the placement of a "break"
2871 // at the end of the loop, we never set mDroppedBadPoint to true
2872 // so it is effectively dead code.
2873 // Need to figure out if the old code is busted or just overcomplicated
2874 // but working as intended.
2875
2876 // Look through all new points and see if any are farther than
2877 // acceptable from all previous points.
2878 for (uint32_t i = pointerCount; i-- > 0; ) {
2879 int32_t y = mCurrentTouch.pointers[i].y;
2880 int32_t closestY = INT_MAX;
2881 int32_t closestDeltaY = 0;
2882
2883#if DEBUG_HACKS
2884 LOGD("BadTouchFilter: Looking at next point #%d: y=%d", i, y);
2885#endif
2886
2887 for (uint32_t j = pointerCount; j-- > 0; ) {
2888 int32_t lastY = mLastTouch.pointers[j].y;
2889 int32_t deltaY = abs(y - lastY);
2890
2891#if DEBUG_HACKS
2892 LOGD("BadTouchFilter: Comparing with last point #%d: y=%d deltaY=%d",
2893 j, lastY, deltaY);
2894#endif
2895
2896 if (deltaY < maxDeltaY) {
2897 goto SkipSufficientlyClosePoint;
2898 }
2899 if (deltaY < closestDeltaY) {
2900 closestDeltaY = deltaY;
2901 closestY = lastY;
2902 }
2903 }
2904
2905 // Must not have found a close enough match.
2906#if DEBUG_HACKS
2907 LOGD("BadTouchFilter: Dropping bad point #%d: newY=%d oldY=%d deltaY=%d maxDeltaY=%d",
2908 i, y, closestY, closestDeltaY, maxDeltaY);
2909#endif
2910
2911 mCurrentTouch.pointers[i].y = closestY;
2912 return true; // XXX original code only corrects one point
2913
2914 SkipSufficientlyClosePoint: ;
2915 }
2916
2917 // No change.
2918 return false;
2919}
2920
2921/* Special hack for devices that have bad screen data: drop points where
2922 * the coordinate value for one axis has jumped to the other pointer's location.
2923 */
2924bool TouchInputMapper::applyJumpyTouchFilter() {
2925 // This hack requires valid axis parameters.
Jeff Brown8d608662010-08-30 03:02:23 -07002926 if (! mRawAxes.y.valid) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002927 return false;
2928 }
2929
2930 uint32_t pointerCount = mCurrentTouch.pointerCount;
2931 if (mLastTouch.pointerCount != pointerCount) {
2932#if DEBUG_HACKS
2933 LOGD("JumpyTouchFilter: Different pointer count %d -> %d",
2934 mLastTouch.pointerCount, pointerCount);
2935 for (uint32_t i = 0; i < pointerCount; i++) {
2936 LOGD(" Pointer %d (%d, %d)", i,
2937 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
2938 }
2939#endif
2940
2941 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_TRANSITION_DROPS) {
2942 if (mLastTouch.pointerCount == 1 && pointerCount == 2) {
2943 // Just drop the first few events going from 1 to 2 pointers.
2944 // They're bad often enough that they're not worth considering.
2945 mCurrentTouch.pointerCount = 1;
2946 mJumpyTouchFilter.jumpyPointsDropped += 1;
2947
2948#if DEBUG_HACKS
2949 LOGD("JumpyTouchFilter: Pointer 2 dropped");
2950#endif
2951 return true;
2952 } else if (mLastTouch.pointerCount == 2 && pointerCount == 1) {
2953 // The event when we go from 2 -> 1 tends to be messed up too
2954 mCurrentTouch.pointerCount = 2;
2955 mCurrentTouch.pointers[0] = mLastTouch.pointers[0];
2956 mCurrentTouch.pointers[1] = mLastTouch.pointers[1];
2957 mJumpyTouchFilter.jumpyPointsDropped += 1;
2958
2959#if DEBUG_HACKS
2960 for (int32_t i = 0; i < 2; i++) {
2961 LOGD("JumpyTouchFilter: Pointer %d replaced (%d, %d)", i,
2962 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
2963 }
2964#endif
2965 return true;
2966 }
2967 }
2968 // Reset jumpy points dropped on other transitions or if limit exceeded.
2969 mJumpyTouchFilter.jumpyPointsDropped = 0;
2970
2971#if DEBUG_HACKS
2972 LOGD("JumpyTouchFilter: Transition - drop limit reset");
2973#endif
2974 return false;
2975 }
2976
2977 // We have the same number of pointers as last time.
2978 // A 'jumpy' point is one where the coordinate value for one axis
2979 // has jumped to the other pointer's location. No need to do anything
2980 // else if we only have one pointer.
2981 if (pointerCount < 2) {
2982 return false;
2983 }
2984
2985 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_DROP_LIMIT) {
Jeff Brown8d608662010-08-30 03:02:23 -07002986 int jumpyEpsilon = mRawAxes.y.getRange() / JUMPY_EPSILON_DIVISOR;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002987
2988 // We only replace the single worst jumpy point as characterized by pointer distance
2989 // in a single axis.
2990 int32_t badPointerIndex = -1;
2991 int32_t badPointerReplacementIndex = -1;
2992 int32_t badPointerDistance = INT_MIN; // distance to be corrected
2993
2994 for (uint32_t i = pointerCount; i-- > 0; ) {
2995 int32_t x = mCurrentTouch.pointers[i].x;
2996 int32_t y = mCurrentTouch.pointers[i].y;
2997
2998#if DEBUG_HACKS
2999 LOGD("JumpyTouchFilter: Point %d (%d, %d)", i, x, y);
3000#endif
3001
3002 // Check if a touch point is too close to another's coordinates
3003 bool dropX = false, dropY = false;
3004 for (uint32_t j = 0; j < pointerCount; j++) {
3005 if (i == j) {
3006 continue;
3007 }
3008
3009 if (abs(x - mCurrentTouch.pointers[j].x) <= jumpyEpsilon) {
3010 dropX = true;
3011 break;
3012 }
3013
3014 if (abs(y - mCurrentTouch.pointers[j].y) <= jumpyEpsilon) {
3015 dropY = true;
3016 break;
3017 }
3018 }
3019 if (! dropX && ! dropY) {
3020 continue; // not jumpy
3021 }
3022
3023 // Find a replacement candidate by comparing with older points on the
3024 // complementary (non-jumpy) axis.
3025 int32_t distance = INT_MIN; // distance to be corrected
3026 int32_t replacementIndex = -1;
3027
3028 if (dropX) {
3029 // X looks too close. Find an older replacement point with a close Y.
3030 int32_t smallestDeltaY = INT_MAX;
3031 for (uint32_t j = 0; j < pointerCount; j++) {
3032 int32_t deltaY = abs(y - mLastTouch.pointers[j].y);
3033 if (deltaY < smallestDeltaY) {
3034 smallestDeltaY = deltaY;
3035 replacementIndex = j;
3036 }
3037 }
3038 distance = abs(x - mLastTouch.pointers[replacementIndex].x);
3039 } else {
3040 // Y looks too close. Find an older replacement point with a close X.
3041 int32_t smallestDeltaX = INT_MAX;
3042 for (uint32_t j = 0; j < pointerCount; j++) {
3043 int32_t deltaX = abs(x - mLastTouch.pointers[j].x);
3044 if (deltaX < smallestDeltaX) {
3045 smallestDeltaX = deltaX;
3046 replacementIndex = j;
3047 }
3048 }
3049 distance = abs(y - mLastTouch.pointers[replacementIndex].y);
3050 }
3051
3052 // If replacing this pointer would correct a worse error than the previous ones
3053 // considered, then use this replacement instead.
3054 if (distance > badPointerDistance) {
3055 badPointerIndex = i;
3056 badPointerReplacementIndex = replacementIndex;
3057 badPointerDistance = distance;
3058 }
3059 }
3060
3061 // Correct the jumpy pointer if one was found.
3062 if (badPointerIndex >= 0) {
3063#if DEBUG_HACKS
3064 LOGD("JumpyTouchFilter: Replacing bad pointer %d with (%d, %d)",
3065 badPointerIndex,
3066 mLastTouch.pointers[badPointerReplacementIndex].x,
3067 mLastTouch.pointers[badPointerReplacementIndex].y);
3068#endif
3069
3070 mCurrentTouch.pointers[badPointerIndex].x =
3071 mLastTouch.pointers[badPointerReplacementIndex].x;
3072 mCurrentTouch.pointers[badPointerIndex].y =
3073 mLastTouch.pointers[badPointerReplacementIndex].y;
3074 mJumpyTouchFilter.jumpyPointsDropped += 1;
3075 return true;
3076 }
3077 }
3078
3079 mJumpyTouchFilter.jumpyPointsDropped = 0;
3080 return false;
3081}
3082
3083/* Special hack for devices that have bad screen data: aggregate and
3084 * compute averages of the coordinate data, to reduce the amount of
3085 * jitter seen by applications. */
3086void TouchInputMapper::applyAveragingTouchFilter() {
3087 for (uint32_t currentIndex = 0; currentIndex < mCurrentTouch.pointerCount; currentIndex++) {
3088 uint32_t id = mCurrentTouch.pointers[currentIndex].id;
3089 int32_t x = mCurrentTouch.pointers[currentIndex].x;
3090 int32_t y = mCurrentTouch.pointers[currentIndex].y;
Jeff Brown8d608662010-08-30 03:02:23 -07003091 int32_t pressure;
3092 switch (mCalibration.pressureSource) {
3093 case Calibration::PRESSURE_SOURCE_PRESSURE:
3094 pressure = mCurrentTouch.pointers[currentIndex].pressure;
3095 break;
3096 case Calibration::PRESSURE_SOURCE_TOUCH:
3097 pressure = mCurrentTouch.pointers[currentIndex].touchMajor;
3098 break;
3099 default:
3100 pressure = 1;
3101 break;
3102 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003103
3104 if (mLastTouch.idBits.hasBit(id)) {
3105 // Pointer was down before and is still down now.
3106 // Compute average over history trace.
3107 uint32_t start = mAveragingTouchFilter.historyStart[id];
3108 uint32_t end = mAveragingTouchFilter.historyEnd[id];
3109
3110 int64_t deltaX = x - mAveragingTouchFilter.historyData[end].pointers[id].x;
3111 int64_t deltaY = y - mAveragingTouchFilter.historyData[end].pointers[id].y;
3112 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3113
3114#if DEBUG_HACKS
3115 LOGD("AveragingTouchFilter: Pointer id %d - Distance from last sample: %lld",
3116 id, distance);
3117#endif
3118
3119 if (distance < AVERAGING_DISTANCE_LIMIT) {
3120 // Increment end index in preparation for recording new historical data.
3121 end += 1;
3122 if (end > AVERAGING_HISTORY_SIZE) {
3123 end = 0;
3124 }
3125
3126 // If the end index has looped back to the start index then we have filled
3127 // the historical trace up to the desired size so we drop the historical
3128 // data at the start of the trace.
3129 if (end == start) {
3130 start += 1;
3131 if (start > AVERAGING_HISTORY_SIZE) {
3132 start = 0;
3133 }
3134 }
3135
3136 // Add the raw data to the historical trace.
3137 mAveragingTouchFilter.historyStart[id] = start;
3138 mAveragingTouchFilter.historyEnd[id] = end;
3139 mAveragingTouchFilter.historyData[end].pointers[id].x = x;
3140 mAveragingTouchFilter.historyData[end].pointers[id].y = y;
3141 mAveragingTouchFilter.historyData[end].pointers[id].pressure = pressure;
3142
3143 // Average over all historical positions in the trace by total pressure.
3144 int32_t averagedX = 0;
3145 int32_t averagedY = 0;
3146 int32_t totalPressure = 0;
3147 for (;;) {
3148 int32_t historicalX = mAveragingTouchFilter.historyData[start].pointers[id].x;
3149 int32_t historicalY = mAveragingTouchFilter.historyData[start].pointers[id].y;
3150 int32_t historicalPressure = mAveragingTouchFilter.historyData[start]
3151 .pointers[id].pressure;
3152
3153 averagedX += historicalX * historicalPressure;
3154 averagedY += historicalY * historicalPressure;
3155 totalPressure += historicalPressure;
3156
3157 if (start == end) {
3158 break;
3159 }
3160
3161 start += 1;
3162 if (start > AVERAGING_HISTORY_SIZE) {
3163 start = 0;
3164 }
3165 }
3166
Jeff Brown8d608662010-08-30 03:02:23 -07003167 if (totalPressure != 0) {
3168 averagedX /= totalPressure;
3169 averagedY /= totalPressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003170
3171#if DEBUG_HACKS
Jeff Brown8d608662010-08-30 03:02:23 -07003172 LOGD("AveragingTouchFilter: Pointer id %d - "
3173 "totalPressure=%d, averagedX=%d, averagedY=%d", id, totalPressure,
3174 averagedX, averagedY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003175#endif
3176
Jeff Brown8d608662010-08-30 03:02:23 -07003177 mCurrentTouch.pointers[currentIndex].x = averagedX;
3178 mCurrentTouch.pointers[currentIndex].y = averagedY;
3179 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003180 } else {
3181#if DEBUG_HACKS
3182 LOGD("AveragingTouchFilter: Pointer id %d - Exceeded max distance", id);
3183#endif
3184 }
3185 } else {
3186#if DEBUG_HACKS
3187 LOGD("AveragingTouchFilter: Pointer id %d - Pointer went up", id);
3188#endif
3189 }
3190
3191 // Reset pointer history.
3192 mAveragingTouchFilter.historyStart[id] = 0;
3193 mAveragingTouchFilter.historyEnd[id] = 0;
3194 mAveragingTouchFilter.historyData[0].pointers[id].x = x;
3195 mAveragingTouchFilter.historyData[0].pointers[id].y = y;
3196 mAveragingTouchFilter.historyData[0].pointers[id].pressure = pressure;
3197 }
3198}
3199
3200int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003201 { // acquire lock
3202 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003203
Jeff Brown6328cdc2010-07-29 18:18:33 -07003204 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003205 return AKEY_STATE_VIRTUAL;
3206 }
3207
Jeff Brown6328cdc2010-07-29 18:18:33 -07003208 size_t numVirtualKeys = mLocked.virtualKeys.size();
3209 for (size_t i = 0; i < numVirtualKeys; i++) {
3210 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07003211 if (virtualKey.keyCode == keyCode) {
3212 return AKEY_STATE_UP;
3213 }
3214 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003215 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07003216
3217 return AKEY_STATE_UNKNOWN;
3218}
3219
3220int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003221 { // acquire lock
3222 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003223
Jeff Brown6328cdc2010-07-29 18:18:33 -07003224 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003225 return AKEY_STATE_VIRTUAL;
3226 }
3227
Jeff Brown6328cdc2010-07-29 18:18:33 -07003228 size_t numVirtualKeys = mLocked.virtualKeys.size();
3229 for (size_t i = 0; i < numVirtualKeys; i++) {
3230 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07003231 if (virtualKey.scanCode == scanCode) {
3232 return AKEY_STATE_UP;
3233 }
3234 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003235 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07003236
3237 return AKEY_STATE_UNKNOWN;
3238}
3239
3240bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
3241 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003242 { // acquire lock
3243 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003244
Jeff Brown6328cdc2010-07-29 18:18:33 -07003245 size_t numVirtualKeys = mLocked.virtualKeys.size();
3246 for (size_t i = 0; i < numVirtualKeys; i++) {
3247 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07003248
3249 for (size_t i = 0; i < numCodes; i++) {
3250 if (virtualKey.keyCode == keyCodes[i]) {
3251 outFlags[i] = 1;
3252 }
3253 }
3254 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003255 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07003256
3257 return true;
3258}
3259
3260
3261// --- SingleTouchInputMapper ---
3262
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003263SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
3264 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003265 initialize();
3266}
3267
3268SingleTouchInputMapper::~SingleTouchInputMapper() {
3269}
3270
3271void SingleTouchInputMapper::initialize() {
3272 mAccumulator.clear();
3273
3274 mDown = false;
3275 mX = 0;
3276 mY = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003277 mPressure = 0; // default to 0 for devices that don't report pressure
3278 mToolWidth = 0; // default to 0 for devices that don't report tool width
Jeff Brown6d0fec22010-07-23 21:28:06 -07003279}
3280
3281void SingleTouchInputMapper::reset() {
3282 TouchInputMapper::reset();
3283
Jeff Brown6d0fec22010-07-23 21:28:06 -07003284 initialize();
3285 }
3286
3287void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
3288 switch (rawEvent->type) {
3289 case EV_KEY:
3290 switch (rawEvent->scanCode) {
3291 case BTN_TOUCH:
3292 mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH;
3293 mAccumulator.btnTouch = rawEvent->value != 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003294 // Don't sync immediately. Wait until the next SYN_REPORT since we might
3295 // not have received valid position information yet. This logic assumes that
3296 // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003297 break;
3298 }
3299 break;
3300
3301 case EV_ABS:
3302 switch (rawEvent->scanCode) {
3303 case ABS_X:
3304 mAccumulator.fields |= Accumulator::FIELD_ABS_X;
3305 mAccumulator.absX = rawEvent->value;
3306 break;
3307 case ABS_Y:
3308 mAccumulator.fields |= Accumulator::FIELD_ABS_Y;
3309 mAccumulator.absY = rawEvent->value;
3310 break;
3311 case ABS_PRESSURE:
3312 mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE;
3313 mAccumulator.absPressure = rawEvent->value;
3314 break;
3315 case ABS_TOOL_WIDTH:
3316 mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH;
3317 mAccumulator.absToolWidth = rawEvent->value;
3318 break;
3319 }
3320 break;
3321
3322 case EV_SYN:
3323 switch (rawEvent->scanCode) {
3324 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003325 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003326 break;
3327 }
3328 break;
3329 }
3330}
3331
3332void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003333 uint32_t fields = mAccumulator.fields;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003334 if (fields == 0) {
3335 return; // no new state changes, so nothing to do
3336 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003337
3338 if (fields & Accumulator::FIELD_BTN_TOUCH) {
3339 mDown = mAccumulator.btnTouch;
3340 }
3341
3342 if (fields & Accumulator::FIELD_ABS_X) {
3343 mX = mAccumulator.absX;
3344 }
3345
3346 if (fields & Accumulator::FIELD_ABS_Y) {
3347 mY = mAccumulator.absY;
3348 }
3349
3350 if (fields & Accumulator::FIELD_ABS_PRESSURE) {
3351 mPressure = mAccumulator.absPressure;
3352 }
3353
3354 if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) {
Jeff Brown8d608662010-08-30 03:02:23 -07003355 mToolWidth = mAccumulator.absToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003356 }
3357
3358 mCurrentTouch.clear();
3359
3360 if (mDown) {
3361 mCurrentTouch.pointerCount = 1;
3362 mCurrentTouch.pointers[0].id = 0;
3363 mCurrentTouch.pointers[0].x = mX;
3364 mCurrentTouch.pointers[0].y = mY;
3365 mCurrentTouch.pointers[0].pressure = mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -07003366 mCurrentTouch.pointers[0].touchMajor = 0;
3367 mCurrentTouch.pointers[0].touchMinor = 0;
3368 mCurrentTouch.pointers[0].toolMajor = mToolWidth;
3369 mCurrentTouch.pointers[0].toolMinor = mToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003370 mCurrentTouch.pointers[0].orientation = 0;
3371 mCurrentTouch.idToIndex[0] = 0;
3372 mCurrentTouch.idBits.markBit(0);
3373 }
3374
3375 syncTouch(when, true);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003376
3377 mAccumulator.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003378}
3379
Jeff Brown8d608662010-08-30 03:02:23 -07003380void SingleTouchInputMapper::configureRawAxes() {
3381 TouchInputMapper::configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003382
Jeff Brown8d608662010-08-30 03:02:23 -07003383 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x);
3384 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y);
3385 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure);
3386 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003387}
3388
3389
3390// --- MultiTouchInputMapper ---
3391
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003392MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
3393 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003394 initialize();
3395}
3396
3397MultiTouchInputMapper::~MultiTouchInputMapper() {
3398}
3399
3400void MultiTouchInputMapper::initialize() {
3401 mAccumulator.clear();
3402}
3403
3404void MultiTouchInputMapper::reset() {
3405 TouchInputMapper::reset();
3406
Jeff Brown6d0fec22010-07-23 21:28:06 -07003407 initialize();
3408}
3409
3410void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
3411 switch (rawEvent->type) {
3412 case EV_ABS: {
3413 uint32_t pointerIndex = mAccumulator.pointerCount;
3414 Accumulator::Pointer* pointer = & mAccumulator.pointers[pointerIndex];
3415
3416 switch (rawEvent->scanCode) {
3417 case ABS_MT_POSITION_X:
3418 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_X;
3419 pointer->absMTPositionX = rawEvent->value;
3420 break;
3421 case ABS_MT_POSITION_Y:
3422 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y;
3423 pointer->absMTPositionY = rawEvent->value;
3424 break;
3425 case ABS_MT_TOUCH_MAJOR:
3426 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR;
3427 pointer->absMTTouchMajor = rawEvent->value;
3428 break;
3429 case ABS_MT_TOUCH_MINOR:
3430 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR;
3431 pointer->absMTTouchMinor = rawEvent->value;
3432 break;
3433 case ABS_MT_WIDTH_MAJOR:
3434 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR;
3435 pointer->absMTWidthMajor = rawEvent->value;
3436 break;
3437 case ABS_MT_WIDTH_MINOR:
3438 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR;
3439 pointer->absMTWidthMinor = rawEvent->value;
3440 break;
3441 case ABS_MT_ORIENTATION:
3442 pointer->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION;
3443 pointer->absMTOrientation = rawEvent->value;
3444 break;
3445 case ABS_MT_TRACKING_ID:
3446 pointer->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID;
3447 pointer->absMTTrackingId = rawEvent->value;
3448 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003449 case ABS_MT_PRESSURE:
3450 pointer->fields |= Accumulator::FIELD_ABS_MT_PRESSURE;
3451 pointer->absMTPressure = rawEvent->value;
3452 break;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003453 }
3454 break;
3455 }
3456
3457 case EV_SYN:
3458 switch (rawEvent->scanCode) {
3459 case SYN_MT_REPORT: {
3460 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
3461 uint32_t pointerIndex = mAccumulator.pointerCount;
3462
3463 if (mAccumulator.pointers[pointerIndex].fields) {
3464 if (pointerIndex == MAX_POINTERS) {
3465 LOGW("MultiTouch device driver returned more than maximum of %d pointers.",
3466 MAX_POINTERS);
3467 } else {
3468 pointerIndex += 1;
3469 mAccumulator.pointerCount = pointerIndex;
3470 }
3471 }
3472
3473 mAccumulator.pointers[pointerIndex].clear();
3474 break;
3475 }
3476
3477 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003478 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003479 break;
3480 }
3481 break;
3482 }
3483}
3484
3485void MultiTouchInputMapper::sync(nsecs_t when) {
3486 static const uint32_t REQUIRED_FIELDS =
Jeff Brown8d608662010-08-30 03:02:23 -07003487 Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003488
Jeff Brown6d0fec22010-07-23 21:28:06 -07003489 uint32_t inCount = mAccumulator.pointerCount;
3490 uint32_t outCount = 0;
3491 bool havePointerIds = true;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003492
Jeff Brown6d0fec22010-07-23 21:28:06 -07003493 mCurrentTouch.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003494
Jeff Brown6d0fec22010-07-23 21:28:06 -07003495 for (uint32_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003496 const Accumulator::Pointer& inPointer = mAccumulator.pointers[inIndex];
3497 uint32_t fields = inPointer.fields;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003498
Jeff Brown6d0fec22010-07-23 21:28:06 -07003499 if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003500 // Some drivers send empty MT sync packets without X / Y to indicate a pointer up.
3501 // Drop this finger.
Jeff Brown46b9ac02010-04-22 18:58:52 -07003502 continue;
3503 }
3504
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003505 PointerData& outPointer = mCurrentTouch.pointers[outCount];
3506 outPointer.x = inPointer.absMTPositionX;
3507 outPointer.y = inPointer.absMTPositionY;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003508
Jeff Brown8d608662010-08-30 03:02:23 -07003509 if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) {
3510 if (inPointer.absMTPressure <= 0) {
Jeff Brownc3db8582010-10-20 15:33:38 -07003511 // Some devices send sync packets with X / Y but with a 0 pressure to indicate
3512 // a pointer going up. Drop this finger.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003513 continue;
3514 }
Jeff Brown8d608662010-08-30 03:02:23 -07003515 outPointer.pressure = inPointer.absMTPressure;
3516 } else {
3517 // Default pressure to 0 if absent.
3518 outPointer.pressure = 0;
3519 }
3520
3521 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) {
3522 if (inPointer.absMTTouchMajor <= 0) {
3523 // Some devices send sync packets with X / Y but with a 0 touch major to indicate
3524 // a pointer going up. Drop this finger.
3525 continue;
3526 }
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003527 outPointer.touchMajor = inPointer.absMTTouchMajor;
3528 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003529 // Default touch area to 0 if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003530 outPointer.touchMajor = 0;
3531 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003532
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003533 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) {
3534 outPointer.touchMinor = inPointer.absMTTouchMinor;
3535 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003536 // Assume touch area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003537 outPointer.touchMinor = outPointer.touchMajor;
3538 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003539
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003540 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) {
3541 outPointer.toolMajor = inPointer.absMTWidthMajor;
3542 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003543 // Default tool area to 0 if absent.
3544 outPointer.toolMajor = 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003545 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003546
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003547 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) {
3548 outPointer.toolMinor = inPointer.absMTWidthMinor;
3549 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003550 // Assume tool area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003551 outPointer.toolMinor = outPointer.toolMajor;
3552 }
3553
3554 if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) {
3555 outPointer.orientation = inPointer.absMTOrientation;
3556 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003557 // Default orientation to vertical if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003558 outPointer.orientation = 0;
3559 }
3560
Jeff Brown8d608662010-08-30 03:02:23 -07003561 // Assign pointer id using tracking id if available.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003562 if (havePointerIds) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003563 if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) {
3564 uint32_t id = uint32_t(inPointer.absMTTrackingId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003565
Jeff Brown6d0fec22010-07-23 21:28:06 -07003566 if (id > MAX_POINTER_ID) {
3567#if DEBUG_POINTERS
3568 LOGD("Pointers: Ignoring driver provided pointer id %d because "
Jeff Brown01ce2e92010-09-26 22:20:12 -07003569 "it is larger than max supported id %d",
Jeff Brown6d0fec22010-07-23 21:28:06 -07003570 id, MAX_POINTER_ID);
3571#endif
3572 havePointerIds = false;
3573 }
3574 else {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003575 outPointer.id = id;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003576 mCurrentTouch.idToIndex[id] = outCount;
3577 mCurrentTouch.idBits.markBit(id);
3578 }
3579 } else {
3580 havePointerIds = false;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003581 }
3582 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003583
Jeff Brown6d0fec22010-07-23 21:28:06 -07003584 outCount += 1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003585 }
3586
Jeff Brown6d0fec22010-07-23 21:28:06 -07003587 mCurrentTouch.pointerCount = outCount;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003588
Jeff Brown6d0fec22010-07-23 21:28:06 -07003589 syncTouch(when, havePointerIds);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003590
3591 mAccumulator.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003592}
3593
Jeff Brown8d608662010-08-30 03:02:23 -07003594void MultiTouchInputMapper::configureRawAxes() {
3595 TouchInputMapper::configureRawAxes();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003596
Jeff Brown8d608662010-08-30 03:02:23 -07003597 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, & mRawAxes.x);
3598 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, & mRawAxes.y);
3599 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, & mRawAxes.touchMajor);
3600 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, & mRawAxes.touchMinor);
3601 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, & mRawAxes.toolMajor);
3602 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, & mRawAxes.toolMinor);
3603 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, & mRawAxes.orientation);
3604 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, & mRawAxes.pressure);
Jeff Brown9c3cda02010-06-15 01:31:58 -07003605}
3606
Jeff Brown46b9ac02010-04-22 18:58:52 -07003607
3608} // namespace android