blob: 51ed09f87ed3cdab0015bdf2dfe2c5aa6170645b [file] [log] [blame]
Jeff Browne839a582010-04-22 18:58:52 -07001//
2// Copyright 2010 The Android Open Source Project
3//
4// The input reader.
5//
6#define LOG_TAG "InputReader"
7
8//#define LOG_NDEBUG 0
9
10// Log debug messages for each raw event received from the EventHub.
11#define DEBUG_RAW_EVENTS 0
12
13// Log debug messages about touch screen filtering hacks.
Jeff Brown50de30a2010-06-22 01:27:15 -070014#define DEBUG_HACKS 0
Jeff Browne839a582010-04-22 18:58:52 -070015
16// Log debug messages about virtual key processing.
Jeff Brown50de30a2010-06-22 01:27:15 -070017#define DEBUG_VIRTUAL_KEYS 0
Jeff Browne839a582010-04-22 18:58:52 -070018
19// Log debug messages about pointers.
Jeff Brown50de30a2010-06-22 01:27:15 -070020#define DEBUG_POINTERS 0
Jeff Browne839a582010-04-22 18:58:52 -070021
Jeff Brownf4a4ec22010-06-16 01:53:36 -070022// Log debug messages about pointer assignment calculations.
23#define DEBUG_POINTER_ASSIGNMENT 0
24
Jeff Browne839a582010-04-22 18:58:52 -070025#include <cutils/log.h>
26#include <ui/InputReader.h>
Jeff Browna3477c82010-11-10 16:03:06 -080027#include <ui/Keyboard.h>
Jeff Browndb360642010-12-02 13:50:46 -080028#include <ui/VirtualKeyMap.h>
Jeff Browne839a582010-04-22 18:58:52 -070029
30#include <stddef.h>
Jeff Brown38a7fab2010-08-30 03:02:23 -070031#include <stdlib.h>
Jeff Browne839a582010-04-22 18:58:52 -070032#include <unistd.h>
Jeff Browne839a582010-04-22 18:58:52 -070033#include <errno.h>
34#include <limits.h>
Jeff Brown5c1ed842010-07-14 18:48:53 -070035#include <math.h>
Jeff Browne839a582010-04-22 18:58:52 -070036
Jeff Brown38a7fab2010-08-30 03:02:23 -070037#define INDENT " "
Jeff Brown26c94ff2010-09-30 14:33:04 -070038#define INDENT2 " "
39#define INDENT3 " "
40#define INDENT4 " "
Jeff Brown38a7fab2010-08-30 03:02:23 -070041
Jeff Browne839a582010-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 Brownf4a4ec22010-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 Brown38a7fab2010-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 Brown26c94ff2010-09-30 14:33:04 -070071static inline const char* toString(bool value) {
72 return value ? "true" : "false";
73}
74
Jeff Browne839a582010-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 Brown8575a872010-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 Browne839a582010-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 Brown54bc2812010-06-15 01:31:58 -070087 if (orientation != InputReaderPolicyInterface::ROTATION_0) {
Jeff Browne839a582010-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 Browne57e8952010-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 Browne839a582010-04-22 18:58:52 -0700101
Jeff Browne839a582010-04-22 18:58:52 -0700102// --- InputReader ---
103
104InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown54bc2812010-06-15 01:31:58 -0700105 const sp<InputReaderPolicyInterface>& policy,
Jeff Browne839a582010-04-22 18:58:52 -0700106 const sp<InputDispatcherInterface>& dispatcher) :
Jeff Browne57e8952010-07-23 21:28:06 -0700107 mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher),
108 mGlobalMetaState(0) {
Jeff Brown54bc2812010-06-15 01:31:58 -0700109 configureExcludedDevices();
Jeff Browne57e8952010-07-23 21:28:06 -0700110 updateGlobalMetaState();
111 updateInputConfiguration();
Jeff Browne839a582010-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 Browne57e8952010-07-23 21:28:06 -0700122 mEventHub->getEvent(& rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700123
124#if DEBUG_RAW_EVENTS
Jeff Browndb360642010-12-02 13:50:46 -0800125 LOGD("Input event: device=%d type=0x%x scancode=%d keycode=%d value=%d",
Jeff Browne839a582010-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 Brown1ad00e92010-10-01 18:55:43 -0700136 addDevice(rawEvent->deviceId);
Jeff Browne839a582010-04-22 18:58:52 -0700137 break;
138
139 case EventHubInterface::DEVICE_REMOVED:
Jeff Brown1ad00e92010-10-01 18:55:43 -0700140 removeDevice(rawEvent->deviceId);
141 break;
142
143 case EventHubInterface::FINISHED_DEVICE_SCAN:
Jeff Brown3c3cc622010-10-20 15:33:38 -0700144 handleConfigurationChanged(rawEvent->when);
Jeff Browne839a582010-04-22 18:58:52 -0700145 break;
146
Jeff Browne57e8952010-07-23 21:28:06 -0700147 default:
148 consumeEvent(rawEvent);
Jeff Browne839a582010-04-22 18:58:52 -0700149 break;
150 }
151}
152
Jeff Brown1ad00e92010-10-01 18:55:43 -0700153void InputReader::addDevice(int32_t deviceId) {
Jeff Browne57e8952010-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 Brown38a7fab2010-08-30 03:02:23 -0700160 if (device->isIgnored()) {
Jeff Browndb360642010-12-02 13:50:46 -0800161 LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string());
Jeff Brown38a7fab2010-08-30 03:02:23 -0700162 } else {
Jeff Browndb360642010-12-02 13:50:46 -0800163 LOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(),
Jeff Brown26c94ff2010-09-30 14:33:04 -0700164 device->getSources());
Jeff Brown38a7fab2010-08-30 03:02:23 -0700165 }
166
Jeff Browne57e8952010-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 Browne839a582010-04-22 18:58:52 -0700181 return;
182 }
Jeff Browne839a582010-04-22 18:58:52 -0700183}
184
Jeff Brown1ad00e92010-10-01 18:55:43 -0700185void InputReader::removeDevice(int32_t deviceId) {
Jeff Browne57e8952010-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 Browne839a582010-04-22 18:58:52 -0700201 return;
202 }
203
Jeff Browne57e8952010-07-23 21:28:06 -0700204 if (device->isIgnored()) {
Jeff Browndb360642010-12-02 13:50:46 -0800205 LOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Browne57e8952010-07-23 21:28:06 -0700206 device->getId(), device->getName().string());
207 } else {
Jeff Browndb360642010-12-02 13:50:46 -0800208 LOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Browne57e8952010-07-23 21:28:06 -0700209 device->getId(), device->getName().string(), device->getSources());
210 }
211
Jeff Brown38a7fab2010-08-30 03:02:23 -0700212 device->reset();
213
Jeff Browne57e8952010-07-23 21:28:06 -0700214 delete device;
Jeff Browne839a582010-04-22 18:58:52 -0700215}
216
Jeff Browne57e8952010-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 Browne839a582010-04-22 18:58:52 -0700219
Jeff Browne57e8952010-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 Browne57e8952010-07-23 21:28:06 -0700237
238 if (keyboardSources != 0) {
Jeff Brown66888372010-11-29 17:37:49 -0800239 device->addMapper(new KeyboardInputMapper(device, keyboardSources, keyboardType));
Jeff Browne57e8952010-07-23 21:28:06 -0700240 }
241
242 // Trackball-like devices.
243 if (classes & INPUT_DEVICE_CLASS_TRACKBALL) {
Jeff Brown66888372010-11-29 17:37:49 -0800244 device->addMapper(new TrackballInputMapper(device));
Jeff Browne57e8952010-07-23 21:28:06 -0700245 }
246
247 // Touchscreen-like devices.
248 if (classes & INPUT_DEVICE_CLASS_TOUCHSCREEN_MT) {
Jeff Brown66888372010-11-29 17:37:49 -0800249 device->addMapper(new MultiTouchInputMapper(device));
Jeff Browne57e8952010-07-23 21:28:06 -0700250 } else if (classes & INPUT_DEVICE_CLASS_TOUCHSCREEN) {
Jeff Brown66888372010-11-29 17:37:49 -0800251 device->addMapper(new SingleTouchInputMapper(device));
Jeff Browne57e8952010-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 Brown3c3cc622010-10-20 15:33:38 -0700279void InputReader::handleConfigurationChanged(nsecs_t when) {
Jeff Browne57e8952010-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 Browne839a582010-04-22 18:58:52 -0700350 }
351 }
Jeff Browne57e8952010-07-23 21:28:06 -0700352 } // release device registry reader lock
Jeff Browne839a582010-04-22 18:58:52 -0700353
Jeff Browne57e8952010-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 Browne839a582010-04-22 18:58:52 -0700375 }
376
Jeff Browne57e8952010-07-23 21:28:06 -0700377 InputDevice* device = mDevices.valueAt(deviceIndex);
378 if (device->isIgnored()) {
379 return NAME_NOT_FOUND;
Jeff Browne839a582010-04-22 18:58:52 -0700380 }
Jeff Browne57e8952010-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 Browna665ca82010-09-08 11:49:43 -0700481void InputReader::dump(String8& dump) {
Jeff Brown2806e382010-10-01 17:46:21 -0700482 mEventHub->dump(dump);
483 dump.append("\n");
484
485 dump.append("Input Reader State:\n");
486
Jeff Brown26c94ff2010-09-30 14:33:04 -0700487 { // acquire device registry reader lock
488 RWLock::AutoRLock _rl(mDeviceRegistryLock);
Jeff Browna665ca82010-09-08 11:49:43 -0700489
Jeff Brown26c94ff2010-09-30 14:33:04 -0700490 for (size_t i = 0; i < mDevices.size(); i++) {
491 mDevices.valueAt(i)->dump(dump);
Jeff Browna665ca82010-09-08 11:49:43 -0700492 }
Jeff Brown26c94ff2010-09-30 14:33:04 -0700493 } // release device registy reader lock
Jeff Browna665ca82010-09-08 11:49:43 -0700494}
495
Jeff Browne57e8952010-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 Brown26c94ff2010-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 Browndb360642010-12-02 13:50:46 -0800539 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brown26c94ff2010-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 Browne57e8952010-07-23 21:28:06 -0700563void InputDevice::addMapper(InputMapper* mapper) {
564 mMappers.add(mapper);
565}
566
567void InputDevice::configure() {
Jeff Brown38a7fab2010-08-30 03:02:23 -0700568 if (! isIgnored()) {
Jeff Brown66888372010-11-29 17:37:49 -0800569 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
Jeff Brown38a7fab2010-08-30 03:02:23 -0700570 }
571
Jeff Browne57e8952010-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 Browne839a582010-04-22 18:58:52 -0700579 }
580}
581
Jeff Browne57e8952010-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 Browne839a582010-04-22 18:58:52 -0700589
Jeff Browne57e8952010-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 Browne839a582010-04-22 18:58:52 -0700597
Jeff Browne57e8952010-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 Brown26c94ff2010-09-30 14:33:04 -0700672void InputMapper::dump(String8& dump) {
673}
674
Jeff Browne57e8952010-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 Browne57e8952010-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 Brown90f0cee2010-10-08 22:31:17 -0700725 getDispatcher()->notifySwitch(when, switchCode, switchValue, 0);
Jeff Browne57e8952010-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 Brown66888372010-11-29 17:37:49 -0800735KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Browne57e8952010-07-23 21:28:06 -0700736 uint32_t sources, int32_t keyboardType) :
Jeff Brown66888372010-11-29 17:37:49 -0800737 InputMapper(device), mSources(sources),
Jeff Browne57e8952010-07-23 21:28:06 -0700738 mKeyboardType(keyboardType) {
Jeff Brownb51719b2010-07-29 18:18:33 -0700739 initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700740}
741
742KeyboardInputMapper::~KeyboardInputMapper() {
743}
744
Jeff Brownb51719b2010-07-29 18:18:33 -0700745void KeyboardInputMapper::initializeLocked() {
746 mLocked.metaState = AMETA_NONE;
747 mLocked.downTime = 0;
Jeff Browne57e8952010-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 Brown26c94ff2010-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 Brown66888372010-11-29 17:37:49 -0800764 dumpParameters(dump);
Jeff Brown26c94ff2010-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 Brown66888372010-11-29 17:37:49 -0800772
773void KeyboardInputMapper::configure() {
774 InputMapper::configure();
775
776 // Configure basic parameters.
777 configureParameters();
Jeff Brown02d85b52010-12-06 17:13:33 -0800778
779 // Reset LEDs.
780 {
781 AutoMutex _l(mLock);
782 resetLedStateLocked();
783 }
Jeff Brown66888372010-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 Browne57e8952010-07-23 21:28:06 -0700802void KeyboardInputMapper::reset() {
Jeff Brownb51719b2010-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 Brown02d85b52010-12-06 17:13:33 -0800811 resetLedStateLocked();
Jeff Brownb51719b2010-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 Browne57e8952010-07-23 21:28:06 -0700820 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownb51719b2010-07-29 18:18:33 -0700821 processKey(when, false, keyCode, scanCode, 0);
Jeff Browne57e8952010-07-23 21:28:06 -0700822 }
823
824 InputMapper::reset();
Jeff Browne57e8952010-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 Brownb51719b2010-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 Brown66888372010-11-29 17:37:49 -0800859 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brownb51719b2010-07-29 18:18:33 -0700860 int32_t orientation;
Jeff Brown66888372010-11-29 17:37:49 -0800861 if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
862 NULL, NULL, & orientation)) {
Jeff Brownd4ecee92010-10-29 22:19:53 -0700863 orientation = InputReaderPolicyInterface::ROTATION_0;
Jeff Brownb51719b2010-07-29 18:18:33 -0700864 }
865
866 keyCode = rotateKeyCode(keyCode, orientation);
Jeff Browne57e8952010-07-23 21:28:06 -0700867 }
868
Jeff Brownb51719b2010-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 Browna3477c82010-11-10 16:03:06 -0800873 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brownb51719b2010-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 Browna3477c82010-11-10 16:03:06 -0800888 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brownb51719b2010-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 Browne57e8952010-07-23 21:28:06 -0700897 }
898
Jeff Brownb51719b2010-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 Brown6a817e22010-09-12 17:55:08 -0700904 updateLedStateLocked(false);
Jeff Browne57e8952010-07-23 21:28:06 -0700905 }
Jeff Brown8575a872010-06-30 16:10:35 -0700906
Jeff Brownb51719b2010-07-29 18:18:33 -0700907 downTime = mLocked.downTime;
908 } // release lock
909
910 if (metaStateChanged) {
Jeff Browne57e8952010-07-23 21:28:06 -0700911 getContext()->updateGlobalMetaState();
Jeff Browne839a582010-04-22 18:58:52 -0700912 }
913
Jeff Brown6a817e22010-09-12 17:55:08 -0700914 if (policyFlags & POLICY_FLAG_FUNCTION) {
915 newMetaState |= AMETA_FUNCTION_ON;
916 }
Jeff Browne57e8952010-07-23 21:28:06 -0700917 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
Jeff Brown90f0cee2010-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 Browne839a582010-04-22 18:58:52 -0700920}
921
Jeff Brownb51719b2010-07-29 18:18:33 -0700922ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) {
923 size_t n = mLocked.keyDowns.size();
Jeff Browne57e8952010-07-23 21:28:06 -0700924 for (size_t i = 0; i < n; i++) {
Jeff Brownb51719b2010-07-29 18:18:33 -0700925 if (mLocked.keyDowns[i].scanCode == scanCode) {
Jeff Browne57e8952010-07-23 21:28:06 -0700926 return i;
927 }
928 }
929 return -1;
Jeff Browne839a582010-04-22 18:58:52 -0700930}
931
Jeff Browne57e8952010-07-23 21:28:06 -0700932int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
933 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
934}
Jeff Browne839a582010-04-22 18:58:52 -0700935
Jeff Browne57e8952010-07-23 21:28:06 -0700936int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
937 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
938}
Jeff Browne839a582010-04-22 18:58:52 -0700939
Jeff Browne57e8952010-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 Brownb51719b2010-07-29 18:18:33 -0700946 { // acquire lock
947 AutoMutex _l(mLock);
948 return mLocked.metaState;
949 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -0700950}
951
Jeff Brown02d85b52010-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 Brown6a817e22010-09-12 17:55:08 -0700965void KeyboardInputMapper::updateLedStateLocked(bool reset) {
966 updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL,
Jeff Brownd4ecee92010-10-29 22:19:53 -0700967 AMETA_CAPS_LOCK_ON, reset);
Jeff Brown6a817e22010-09-12 17:55:08 -0700968 updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML,
Jeff Brownd4ecee92010-10-29 22:19:53 -0700969 AMETA_NUM_LOCK_ON, reset);
Jeff Brown6a817e22010-09-12 17:55:08 -0700970 updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL,
Jeff Brownd4ecee92010-10-29 22:19:53 -0700971 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown6a817e22010-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 Brown02d85b52010-12-06 17:13:33 -0800978 if (reset || ledState.on != desiredState) {
Jeff Brown6a817e22010-09-12 17:55:08 -0700979 getEventHub()->setLedState(getDeviceId(), led, desiredState);
980 ledState.on = desiredState;
981 }
982 }
983}
984
Jeff Browne57e8952010-07-23 21:28:06 -0700985
986// --- TrackballInputMapper ---
987
Jeff Brown66888372010-11-29 17:37:49 -0800988TrackballInputMapper::TrackballInputMapper(InputDevice* device) :
989 InputMapper(device) {
Jeff Browne57e8952010-07-23 21:28:06 -0700990 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
991 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
992 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
993 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
994
Jeff Brownb51719b2010-07-29 18:18:33 -0700995 initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700996}
997
998TrackballInputMapper::~TrackballInputMapper() {
999}
1000
1001uint32_t TrackballInputMapper::getSources() {
1002 return AINPUT_SOURCE_TRACKBALL;
1003}
1004
1005void TrackballInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1006 InputMapper::populateDeviceInfo(info);
1007
1008 info->addMotionRange(AINPUT_MOTION_RANGE_X, -1.0f, 1.0f, 0.0f, mXScale);
1009 info->addMotionRange(AINPUT_MOTION_RANGE_Y, -1.0f, 1.0f, 0.0f, mYScale);
1010}
1011
Jeff Brown26c94ff2010-09-30 14:33:04 -07001012void TrackballInputMapper::dump(String8& dump) {
1013 { // acquire lock
1014 AutoMutex _l(mLock);
1015 dump.append(INDENT2 "Trackball Input Mapper:\n");
Jeff Brown66888372010-11-29 17:37:49 -08001016 dumpParameters(dump);
Jeff Brown26c94ff2010-09-30 14:33:04 -07001017 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1018 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
1019 dump.appendFormat(INDENT3 "Down: %s\n", toString(mLocked.down));
1020 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1021 } // release lock
1022}
1023
Jeff Brown66888372010-11-29 17:37:49 -08001024void TrackballInputMapper::configure() {
1025 InputMapper::configure();
1026
1027 // Configure basic parameters.
1028 configureParameters();
1029}
1030
1031void TrackballInputMapper::configureParameters() {
1032 mParameters.orientationAware = false;
1033 getDevice()->getConfiguration().tryGetProperty(String8("trackball.orientationAware"),
1034 mParameters.orientationAware);
1035
1036 mParameters.associatedDisplayId = mParameters.orientationAware ? 0 : -1;
1037}
1038
1039void TrackballInputMapper::dumpParameters(String8& dump) {
1040 dump.append(INDENT3 "Parameters:\n");
1041 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1042 mParameters.associatedDisplayId);
1043 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1044 toString(mParameters.orientationAware));
1045}
1046
Jeff Brownb51719b2010-07-29 18:18:33 -07001047void TrackballInputMapper::initializeLocked() {
Jeff Browne57e8952010-07-23 21:28:06 -07001048 mAccumulator.clear();
1049
Jeff Brownb51719b2010-07-29 18:18:33 -07001050 mLocked.down = false;
1051 mLocked.downTime = 0;
Jeff Browne57e8952010-07-23 21:28:06 -07001052}
1053
1054void TrackballInputMapper::reset() {
Jeff Brownb51719b2010-07-29 18:18:33 -07001055 for (;;) {
1056 { // acquire lock
1057 AutoMutex _l(mLock);
1058
1059 if (! mLocked.down) {
1060 initializeLocked();
1061 break; // done
1062 }
1063 } // release lock
1064
1065 // Synthesize trackball button up event on reset.
Jeff Browne57e8952010-07-23 21:28:06 -07001066 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownb51719b2010-07-29 18:18:33 -07001067 mAccumulator.fields = Accumulator::FIELD_BTN_MOUSE;
Jeff Browne57e8952010-07-23 21:28:06 -07001068 mAccumulator.btnMouse = false;
1069 sync(when);
Jeff Browne839a582010-04-22 18:58:52 -07001070 }
1071
Jeff Browne57e8952010-07-23 21:28:06 -07001072 InputMapper::reset();
Jeff Browne57e8952010-07-23 21:28:06 -07001073}
Jeff Browne839a582010-04-22 18:58:52 -07001074
Jeff Browne57e8952010-07-23 21:28:06 -07001075void TrackballInputMapper::process(const RawEvent* rawEvent) {
1076 switch (rawEvent->type) {
1077 case EV_KEY:
1078 switch (rawEvent->scanCode) {
1079 case BTN_MOUSE:
1080 mAccumulator.fields |= Accumulator::FIELD_BTN_MOUSE;
1081 mAccumulator.btnMouse = rawEvent->value != 0;
Jeff Brownd64c8552010-08-17 20:38:35 -07001082 // Sync now since BTN_MOUSE is not necessarily followed by SYN_REPORT and
1083 // we need to ensure that we report the up/down promptly.
Jeff Browne57e8952010-07-23 21:28:06 -07001084 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07001085 break;
Jeff Browne839a582010-04-22 18:58:52 -07001086 }
Jeff Browne57e8952010-07-23 21:28:06 -07001087 break;
Jeff Browne839a582010-04-22 18:58:52 -07001088
Jeff Browne57e8952010-07-23 21:28:06 -07001089 case EV_REL:
1090 switch (rawEvent->scanCode) {
1091 case REL_X:
1092 mAccumulator.fields |= Accumulator::FIELD_REL_X;
1093 mAccumulator.relX = rawEvent->value;
1094 break;
1095 case REL_Y:
1096 mAccumulator.fields |= Accumulator::FIELD_REL_Y;
1097 mAccumulator.relY = rawEvent->value;
1098 break;
Jeff Browne839a582010-04-22 18:58:52 -07001099 }
Jeff Browne57e8952010-07-23 21:28:06 -07001100 break;
Jeff Browne839a582010-04-22 18:58:52 -07001101
Jeff Browne57e8952010-07-23 21:28:06 -07001102 case EV_SYN:
1103 switch (rawEvent->scanCode) {
1104 case SYN_REPORT:
Jeff Brownd64c8552010-08-17 20:38:35 -07001105 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07001106 break;
Jeff Browne839a582010-04-22 18:58:52 -07001107 }
Jeff Browne57e8952010-07-23 21:28:06 -07001108 break;
Jeff Browne839a582010-04-22 18:58:52 -07001109 }
Jeff Browne839a582010-04-22 18:58:52 -07001110}
1111
Jeff Browne57e8952010-07-23 21:28:06 -07001112void TrackballInputMapper::sync(nsecs_t when) {
Jeff Brownd64c8552010-08-17 20:38:35 -07001113 uint32_t fields = mAccumulator.fields;
1114 if (fields == 0) {
1115 return; // no new state changes, so nothing to do
1116 }
1117
Jeff Brownb51719b2010-07-29 18:18:33 -07001118 int motionEventAction;
1119 PointerCoords pointerCoords;
1120 nsecs_t downTime;
1121 { // acquire lock
1122 AutoMutex _l(mLock);
Jeff Browne839a582010-04-22 18:58:52 -07001123
Jeff Brownb51719b2010-07-29 18:18:33 -07001124 bool downChanged = fields & Accumulator::FIELD_BTN_MOUSE;
1125
1126 if (downChanged) {
1127 if (mAccumulator.btnMouse) {
1128 mLocked.down = true;
1129 mLocked.downTime = when;
1130 } else {
1131 mLocked.down = false;
1132 }
Jeff Browne57e8952010-07-23 21:28:06 -07001133 }
Jeff Browne839a582010-04-22 18:58:52 -07001134
Jeff Brownb51719b2010-07-29 18:18:33 -07001135 downTime = mLocked.downTime;
1136 float x = fields & Accumulator::FIELD_REL_X ? mAccumulator.relX * mXScale : 0.0f;
1137 float y = fields & Accumulator::FIELD_REL_Y ? mAccumulator.relY * mYScale : 0.0f;
Jeff Browne839a582010-04-22 18:58:52 -07001138
Jeff Brownb51719b2010-07-29 18:18:33 -07001139 if (downChanged) {
1140 motionEventAction = mLocked.down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
Jeff Browne57e8952010-07-23 21:28:06 -07001141 } else {
Jeff Brownb51719b2010-07-29 18:18:33 -07001142 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browne57e8952010-07-23 21:28:06 -07001143 }
Jeff Browne839a582010-04-22 18:58:52 -07001144
Jeff Brownb51719b2010-07-29 18:18:33 -07001145 pointerCoords.x = x;
1146 pointerCoords.y = y;
1147 pointerCoords.pressure = mLocked.down ? 1.0f : 0.0f;
1148 pointerCoords.size = 0;
1149 pointerCoords.touchMajor = 0;
1150 pointerCoords.touchMinor = 0;
1151 pointerCoords.toolMajor = 0;
1152 pointerCoords.toolMinor = 0;
1153 pointerCoords.orientation = 0;
Jeff Browne839a582010-04-22 18:58:52 -07001154
Jeff Brown66888372010-11-29 17:37:49 -08001155 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
1156 && (x != 0.0f || y != 0.0f)) {
Jeff Brownb51719b2010-07-29 18:18:33 -07001157 // Rotate motion based on display orientation if needed.
1158 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1159 int32_t orientation;
Jeff Brown66888372010-11-29 17:37:49 -08001160 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1161 NULL, NULL, & orientation)) {
Jeff Brownd4ecee92010-10-29 22:19:53 -07001162 orientation = InputReaderPolicyInterface::ROTATION_0;
Jeff Brownb51719b2010-07-29 18:18:33 -07001163 }
1164
1165 float temp;
1166 switch (orientation) {
1167 case InputReaderPolicyInterface::ROTATION_90:
1168 temp = pointerCoords.x;
1169 pointerCoords.x = pointerCoords.y;
1170 pointerCoords.y = - temp;
1171 break;
1172
1173 case InputReaderPolicyInterface::ROTATION_180:
1174 pointerCoords.x = - pointerCoords.x;
1175 pointerCoords.y = - pointerCoords.y;
1176 break;
1177
1178 case InputReaderPolicyInterface::ROTATION_270:
1179 temp = pointerCoords.x;
1180 pointerCoords.x = - pointerCoords.y;
1181 pointerCoords.y = temp;
1182 break;
1183 }
1184 }
1185 } // release lock
1186
Jeff Browne57e8952010-07-23 21:28:06 -07001187 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownb51719b2010-07-29 18:18:33 -07001188 int32_t pointerId = 0;
Jeff Brown90f0cee2010-10-08 22:31:17 -07001189 getDispatcher()->notifyMotion(when, getDeviceId(), AINPUT_SOURCE_TRACKBALL, 0,
Jeff Brownaf30ff62010-09-01 17:01:00 -07001190 motionEventAction, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown90f0cee2010-10-08 22:31:17 -07001191 1, &pointerId, &pointerCoords, mXPrecision, mYPrecision, downTime);
1192
1193 mAccumulator.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07001194}
1195
Jeff Brown8d4dfd22010-08-10 15:47:53 -07001196int32_t TrackballInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1197 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
1198 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1199 } else {
1200 return AKEY_STATE_UNKNOWN;
1201 }
1202}
1203
Jeff Browne57e8952010-07-23 21:28:06 -07001204
1205// --- TouchInputMapper ---
1206
Jeff Brown66888372010-11-29 17:37:49 -08001207TouchInputMapper::TouchInputMapper(InputDevice* device) :
1208 InputMapper(device) {
Jeff Brownb51719b2010-07-29 18:18:33 -07001209 mLocked.surfaceOrientation = -1;
1210 mLocked.surfaceWidth = -1;
1211 mLocked.surfaceHeight = -1;
1212
1213 initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -07001214}
1215
1216TouchInputMapper::~TouchInputMapper() {
1217}
1218
1219uint32_t TouchInputMapper::getSources() {
Jeff Brown66888372010-11-29 17:37:49 -08001220 switch (mParameters.deviceType) {
1221 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
1222 return AINPUT_SOURCE_TOUCHSCREEN;
1223 case Parameters::DEVICE_TYPE_TOUCH_PAD:
1224 return AINPUT_SOURCE_TOUCHPAD;
1225 default:
1226 assert(false);
1227 return AINPUT_SOURCE_UNKNOWN;
1228 }
Jeff Browne57e8952010-07-23 21:28:06 -07001229}
1230
1231void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1232 InputMapper::populateDeviceInfo(info);
1233
Jeff Brownb51719b2010-07-29 18:18:33 -07001234 { // acquire lock
1235 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07001236
Jeff Brownb51719b2010-07-29 18:18:33 -07001237 // Ensure surface information is up to date so that orientation changes are
1238 // noticed immediately.
1239 configureSurfaceLocked();
1240
1241 info->addMotionRange(AINPUT_MOTION_RANGE_X, mLocked.orientedRanges.x);
1242 info->addMotionRange(AINPUT_MOTION_RANGE_Y, mLocked.orientedRanges.y);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001243
1244 if (mLocked.orientedRanges.havePressure) {
1245 info->addMotionRange(AINPUT_MOTION_RANGE_PRESSURE,
1246 mLocked.orientedRanges.pressure);
1247 }
1248
1249 if (mLocked.orientedRanges.haveSize) {
1250 info->addMotionRange(AINPUT_MOTION_RANGE_SIZE,
1251 mLocked.orientedRanges.size);
1252 }
1253
Jeff Brown6b337e72010-10-14 21:42:15 -07001254 if (mLocked.orientedRanges.haveTouchSize) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001255 info->addMotionRange(AINPUT_MOTION_RANGE_TOUCH_MAJOR,
1256 mLocked.orientedRanges.touchMajor);
1257 info->addMotionRange(AINPUT_MOTION_RANGE_TOUCH_MINOR,
1258 mLocked.orientedRanges.touchMinor);
1259 }
1260
Jeff Brown6b337e72010-10-14 21:42:15 -07001261 if (mLocked.orientedRanges.haveToolSize) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001262 info->addMotionRange(AINPUT_MOTION_RANGE_TOOL_MAJOR,
1263 mLocked.orientedRanges.toolMajor);
1264 info->addMotionRange(AINPUT_MOTION_RANGE_TOOL_MINOR,
1265 mLocked.orientedRanges.toolMinor);
1266 }
1267
1268 if (mLocked.orientedRanges.haveOrientation) {
1269 info->addMotionRange(AINPUT_MOTION_RANGE_ORIENTATION,
1270 mLocked.orientedRanges.orientation);
1271 }
Jeff Brownb51719b2010-07-29 18:18:33 -07001272 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07001273}
1274
Jeff Brown26c94ff2010-09-30 14:33:04 -07001275void TouchInputMapper::dump(String8& dump) {
1276 { // acquire lock
1277 AutoMutex _l(mLock);
1278 dump.append(INDENT2 "Touch Input Mapper:\n");
Jeff Brown26c94ff2010-09-30 14:33:04 -07001279 dumpParameters(dump);
1280 dumpVirtualKeysLocked(dump);
1281 dumpRawAxes(dump);
1282 dumpCalibration(dump);
1283 dumpSurfaceLocked(dump);
Jeff Brown60b57762010-10-18 13:32:20 -07001284 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
Jeff Brown6b337e72010-10-14 21:42:15 -07001285 dump.appendFormat(INDENT4 "XOrigin: %d\n", mLocked.xOrigin);
1286 dump.appendFormat(INDENT4 "YOrigin: %d\n", mLocked.yOrigin);
1287 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mLocked.xScale);
1288 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mLocked.yScale);
1289 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mLocked.xPrecision);
1290 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mLocked.yPrecision);
1291 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mLocked.geometricScale);
1292 dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mLocked.toolSizeLinearScale);
1293 dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mLocked.toolSizeLinearBias);
1294 dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mLocked.toolSizeAreaScale);
1295 dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mLocked.toolSizeAreaBias);
1296 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mLocked.pressureScale);
1297 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mLocked.sizeScale);
1298 dump.appendFormat(INDENT4 "OrientationSCale: %0.3f\n", mLocked.orientationScale);
Jeff Brown26c94ff2010-09-30 14:33:04 -07001299 } // release lock
1300}
1301
Jeff Brownb51719b2010-07-29 18:18:33 -07001302void TouchInputMapper::initializeLocked() {
1303 mCurrentTouch.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07001304 mLastTouch.clear();
1305 mDownTime = 0;
Jeff Browne57e8952010-07-23 21:28:06 -07001306
1307 for (uint32_t i = 0; i < MAX_POINTERS; i++) {
1308 mAveragingTouchFilter.historyStart[i] = 0;
1309 mAveragingTouchFilter.historyEnd[i] = 0;
1310 }
1311
1312 mJumpyTouchFilter.jumpyPointsDropped = 0;
Jeff Brownb51719b2010-07-29 18:18:33 -07001313
1314 mLocked.currentVirtualKey.down = false;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001315
1316 mLocked.orientedRanges.havePressure = false;
1317 mLocked.orientedRanges.haveSize = false;
Jeff Brown6b337e72010-10-14 21:42:15 -07001318 mLocked.orientedRanges.haveTouchSize = false;
1319 mLocked.orientedRanges.haveToolSize = false;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001320 mLocked.orientedRanges.haveOrientation = false;
1321}
1322
Jeff Browne57e8952010-07-23 21:28:06 -07001323void TouchInputMapper::configure() {
1324 InputMapper::configure();
1325
1326 // Configure basic parameters.
Jeff Brown38a7fab2010-08-30 03:02:23 -07001327 configureParameters();
Jeff Browne57e8952010-07-23 21:28:06 -07001328
1329 // Configure absolute axis information.
Jeff Brown38a7fab2010-08-30 03:02:23 -07001330 configureRawAxes();
Jeff Brown38a7fab2010-08-30 03:02:23 -07001331
1332 // Prepare input device calibration.
1333 parseCalibration();
1334 resolveCalibration();
Jeff Browne57e8952010-07-23 21:28:06 -07001335
Jeff Brownb51719b2010-07-29 18:18:33 -07001336 { // acquire lock
1337 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07001338
Jeff Brown38a7fab2010-08-30 03:02:23 -07001339 // Configure surface dimensions and orientation.
Jeff Brownb51719b2010-07-29 18:18:33 -07001340 configureSurfaceLocked();
1341 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07001342}
1343
Jeff Brown38a7fab2010-08-30 03:02:23 -07001344void TouchInputMapper::configureParameters() {
1345 mParameters.useBadTouchFilter = getPolicy()->filterTouchEvents();
1346 mParameters.useAveragingTouchFilter = getPolicy()->filterTouchEvents();
1347 mParameters.useJumpyTouchFilter = getPolicy()->filterJumpyTouchEvents();
Jeff Brown66888372010-11-29 17:37:49 -08001348
1349 String8 deviceTypeString;
1350 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
1351 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
1352 deviceTypeString)) {
1353 if (deviceTypeString == "touchPad") {
1354 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
1355 } else if (deviceTypeString != "touchScreen") {
1356 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
1357 }
1358 }
1359 bool isTouchScreen = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
1360
1361 mParameters.orientationAware = isTouchScreen;
1362 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
1363 mParameters.orientationAware);
1364
1365 mParameters.associatedDisplayId = mParameters.orientationAware || isTouchScreen ? 0 : -1;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001366}
1367
Jeff Brown26c94ff2010-09-30 14:33:04 -07001368void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown66888372010-11-29 17:37:49 -08001369 dump.append(INDENT3 "Parameters:\n");
1370
1371 switch (mParameters.deviceType) {
1372 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
1373 dump.append(INDENT4 "DeviceType: touchScreen\n");
1374 break;
1375 case Parameters::DEVICE_TYPE_TOUCH_PAD:
1376 dump.append(INDENT4 "DeviceType: touchPad\n");
1377 break;
1378 default:
1379 assert(false);
1380 }
1381
1382 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1383 mParameters.associatedDisplayId);
1384 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1385 toString(mParameters.orientationAware));
1386
1387 dump.appendFormat(INDENT4 "UseBadTouchFilter: %s\n",
Jeff Brown26c94ff2010-09-30 14:33:04 -07001388 toString(mParameters.useBadTouchFilter));
Jeff Brown66888372010-11-29 17:37:49 -08001389 dump.appendFormat(INDENT4 "UseAveragingTouchFilter: %s\n",
Jeff Brown26c94ff2010-09-30 14:33:04 -07001390 toString(mParameters.useAveragingTouchFilter));
Jeff Brown66888372010-11-29 17:37:49 -08001391 dump.appendFormat(INDENT4 "UseJumpyTouchFilter: %s\n",
Jeff Brown26c94ff2010-09-30 14:33:04 -07001392 toString(mParameters.useJumpyTouchFilter));
Jeff Browna665ca82010-09-08 11:49:43 -07001393}
1394
Jeff Brown38a7fab2010-08-30 03:02:23 -07001395void TouchInputMapper::configureRawAxes() {
1396 mRawAxes.x.clear();
1397 mRawAxes.y.clear();
1398 mRawAxes.pressure.clear();
1399 mRawAxes.touchMajor.clear();
1400 mRawAxes.touchMinor.clear();
1401 mRawAxes.toolMajor.clear();
1402 mRawAxes.toolMinor.clear();
1403 mRawAxes.orientation.clear();
1404}
1405
Jeff Brown26c94ff2010-09-30 14:33:04 -07001406static void dumpAxisInfo(String8& dump, RawAbsoluteAxisInfo axis, const char* name) {
Jeff Browna665ca82010-09-08 11:49:43 -07001407 if (axis.valid) {
Jeff Brown26c94ff2010-09-30 14:33:04 -07001408 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d\n",
Jeff Browna665ca82010-09-08 11:49:43 -07001409 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz);
1410 } else {
Jeff Brown26c94ff2010-09-30 14:33:04 -07001411 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
Jeff Browna665ca82010-09-08 11:49:43 -07001412 }
1413}
1414
Jeff Brown26c94ff2010-09-30 14:33:04 -07001415void TouchInputMapper::dumpRawAxes(String8& dump) {
1416 dump.append(INDENT3 "Raw Axes:\n");
1417 dumpAxisInfo(dump, mRawAxes.x, "X");
1418 dumpAxisInfo(dump, mRawAxes.y, "Y");
1419 dumpAxisInfo(dump, mRawAxes.pressure, "Pressure");
1420 dumpAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor");
1421 dumpAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor");
1422 dumpAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor");
1423 dumpAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor");
1424 dumpAxisInfo(dump, mRawAxes.orientation, "Orientation");
Jeff Browne57e8952010-07-23 21:28:06 -07001425}
1426
Jeff Brownb51719b2010-07-29 18:18:33 -07001427bool TouchInputMapper::configureSurfaceLocked() {
Jeff Browne57e8952010-07-23 21:28:06 -07001428 // Update orientation and dimensions if needed.
Jeff Brown66888372010-11-29 17:37:49 -08001429 int32_t orientation = InputReaderPolicyInterface::ROTATION_0;
1430 int32_t width = mRawAxes.x.getRange();
1431 int32_t height = mRawAxes.y.getRange();
1432
1433 if (mParameters.associatedDisplayId >= 0) {
1434 bool wantSize = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
1435 bool wantOrientation = mParameters.orientationAware;
1436
Jeff Brownb51719b2010-07-29 18:18:33 -07001437 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown66888372010-11-29 17:37:49 -08001438 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1439 wantSize ? &width : NULL, wantSize ? &height : NULL,
1440 wantOrientation ? &orientation : NULL)) {
Jeff Browne57e8952010-07-23 21:28:06 -07001441 return false;
1442 }
Jeff Browne57e8952010-07-23 21:28:06 -07001443 }
1444
Jeff Brownb51719b2010-07-29 18:18:33 -07001445 bool orientationChanged = mLocked.surfaceOrientation != orientation;
Jeff Browne57e8952010-07-23 21:28:06 -07001446 if (orientationChanged) {
Jeff Brownb51719b2010-07-29 18:18:33 -07001447 mLocked.surfaceOrientation = orientation;
Jeff Browne57e8952010-07-23 21:28:06 -07001448 }
1449
Jeff Brownb51719b2010-07-29 18:18:33 -07001450 bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height;
Jeff Browne57e8952010-07-23 21:28:06 -07001451 if (sizeChanged) {
Jeff Browndb360642010-12-02 13:50:46 -08001452 LOGI("Device reconfigured: id=%d, name='%s', display size is now %dx%d",
Jeff Brown26c94ff2010-09-30 14:33:04 -07001453 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001454
Jeff Brownb51719b2010-07-29 18:18:33 -07001455 mLocked.surfaceWidth = width;
1456 mLocked.surfaceHeight = height;
Jeff Browne57e8952010-07-23 21:28:06 -07001457
Jeff Brown38a7fab2010-08-30 03:02:23 -07001458 // Configure X and Y factors.
1459 if (mRawAxes.x.valid && mRawAxes.y.valid) {
Jeff Brown60b57762010-10-18 13:32:20 -07001460 mLocked.xOrigin = mCalibration.haveXOrigin
1461 ? mCalibration.xOrigin
1462 : mRawAxes.x.minValue;
1463 mLocked.yOrigin = mCalibration.haveYOrigin
1464 ? mCalibration.yOrigin
1465 : mRawAxes.y.minValue;
1466 mLocked.xScale = mCalibration.haveXScale
1467 ? mCalibration.xScale
1468 : float(width) / mRawAxes.x.getRange();
1469 mLocked.yScale = mCalibration.haveYScale
1470 ? mCalibration.yScale
1471 : float(height) / mRawAxes.y.getRange();
Jeff Brownb51719b2010-07-29 18:18:33 -07001472 mLocked.xPrecision = 1.0f / mLocked.xScale;
1473 mLocked.yPrecision = 1.0f / mLocked.yScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001474
Jeff Brownb51719b2010-07-29 18:18:33 -07001475 configureVirtualKeysLocked();
Jeff Browne57e8952010-07-23 21:28:06 -07001476 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001477 LOGW(INDENT "Touch device did not report support for X or Y axis!");
Jeff Brownb51719b2010-07-29 18:18:33 -07001478 mLocked.xOrigin = 0;
1479 mLocked.yOrigin = 0;
1480 mLocked.xScale = 1.0f;
1481 mLocked.yScale = 1.0f;
1482 mLocked.xPrecision = 1.0f;
1483 mLocked.yPrecision = 1.0f;
Jeff Browne57e8952010-07-23 21:28:06 -07001484 }
1485
Jeff Brown38a7fab2010-08-30 03:02:23 -07001486 // Scale factor for terms that are not oriented in a particular axis.
1487 // If the pixels are square then xScale == yScale otherwise we fake it
1488 // by choosing an average.
1489 mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale);
Jeff Browne57e8952010-07-23 21:28:06 -07001490
Jeff Brown38a7fab2010-08-30 03:02:23 -07001491 // Size of diagonal axis.
1492 float diagonalSize = pythag(width, height);
Jeff Browne57e8952010-07-23 21:28:06 -07001493
Jeff Brown38a7fab2010-08-30 03:02:23 -07001494 // TouchMajor and TouchMinor factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001495 if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) {
1496 mLocked.orientedRanges.haveTouchSize = true;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001497 mLocked.orientedRanges.touchMajor.min = 0;
1498 mLocked.orientedRanges.touchMajor.max = diagonalSize;
1499 mLocked.orientedRanges.touchMajor.flat = 0;
1500 mLocked.orientedRanges.touchMajor.fuzz = 0;
1501 mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor;
1502 }
Jeff Brownb51719b2010-07-29 18:18:33 -07001503
Jeff Brown38a7fab2010-08-30 03:02:23 -07001504 // ToolMajor and ToolMinor factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001505 mLocked.toolSizeLinearScale = 0;
1506 mLocked.toolSizeLinearBias = 0;
1507 mLocked.toolSizeAreaScale = 0;
1508 mLocked.toolSizeAreaBias = 0;
1509 if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
1510 if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) {
1511 if (mCalibration.haveToolSizeLinearScale) {
1512 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001513 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
Jeff Brown6b337e72010-10-14 21:42:15 -07001514 mLocked.toolSizeLinearScale = float(min(width, height))
Jeff Brown38a7fab2010-08-30 03:02:23 -07001515 / mRawAxes.toolMajor.maxValue;
1516 }
1517
Jeff Brown6b337e72010-10-14 21:42:15 -07001518 if (mCalibration.haveToolSizeLinearBias) {
1519 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
1520 }
1521 } else if (mCalibration.toolSizeCalibration ==
1522 Calibration::TOOL_SIZE_CALIBRATION_AREA) {
1523 if (mCalibration.haveToolSizeLinearScale) {
1524 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
1525 } else {
1526 mLocked.toolSizeLinearScale = min(width, height);
1527 }
1528
1529 if (mCalibration.haveToolSizeLinearBias) {
1530 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
1531 }
1532
1533 if (mCalibration.haveToolSizeAreaScale) {
1534 mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale;
1535 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1536 mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue;
1537 }
1538
1539 if (mCalibration.haveToolSizeAreaBias) {
1540 mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001541 }
1542 }
1543
Jeff Brown6b337e72010-10-14 21:42:15 -07001544 mLocked.orientedRanges.haveToolSize = true;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001545 mLocked.orientedRanges.toolMajor.min = 0;
1546 mLocked.orientedRanges.toolMajor.max = diagonalSize;
1547 mLocked.orientedRanges.toolMajor.flat = 0;
1548 mLocked.orientedRanges.toolMajor.fuzz = 0;
1549 mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor;
1550 }
1551
1552 // Pressure factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001553 mLocked.pressureScale = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001554 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
1555 RawAbsoluteAxisInfo rawPressureAxis;
1556 switch (mCalibration.pressureSource) {
1557 case Calibration::PRESSURE_SOURCE_PRESSURE:
1558 rawPressureAxis = mRawAxes.pressure;
1559 break;
1560 case Calibration::PRESSURE_SOURCE_TOUCH:
1561 rawPressureAxis = mRawAxes.touchMajor;
1562 break;
1563 default:
1564 rawPressureAxis.clear();
1565 }
1566
Jeff Brown38a7fab2010-08-30 03:02:23 -07001567 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
1568 || mCalibration.pressureCalibration
1569 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
1570 if (mCalibration.havePressureScale) {
1571 mLocked.pressureScale = mCalibration.pressureScale;
1572 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
1573 mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue;
1574 }
1575 }
1576
1577 mLocked.orientedRanges.havePressure = true;
1578 mLocked.orientedRanges.pressure.min = 0;
1579 mLocked.orientedRanges.pressure.max = 1.0;
1580 mLocked.orientedRanges.pressure.flat = 0;
1581 mLocked.orientedRanges.pressure.fuzz = 0;
1582 }
1583
1584 // Size factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001585 mLocked.sizeScale = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001586 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001587 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
1588 if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1589 mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue;
1590 }
1591 }
1592
1593 mLocked.orientedRanges.haveSize = true;
1594 mLocked.orientedRanges.size.min = 0;
1595 mLocked.orientedRanges.size.max = 1.0;
1596 mLocked.orientedRanges.size.flat = 0;
1597 mLocked.orientedRanges.size.fuzz = 0;
1598 }
1599
1600 // Orientation
Jeff Brown6b337e72010-10-14 21:42:15 -07001601 mLocked.orientationScale = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001602 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001603 if (mCalibration.orientationCalibration
1604 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
1605 if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) {
1606 mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue;
1607 }
1608 }
1609
1610 mLocked.orientedRanges.orientation.min = - M_PI_2;
1611 mLocked.orientedRanges.orientation.max = M_PI_2;
1612 mLocked.orientedRanges.orientation.flat = 0;
1613 mLocked.orientedRanges.orientation.fuzz = 0;
1614 }
Jeff Browne57e8952010-07-23 21:28:06 -07001615 }
1616
1617 if (orientationChanged || sizeChanged) {
1618 // Compute oriented surface dimensions, precision, and scales.
1619 float orientedXScale, orientedYScale;
Jeff Brownb51719b2010-07-29 18:18:33 -07001620 switch (mLocked.surfaceOrientation) {
Jeff Browne57e8952010-07-23 21:28:06 -07001621 case InputReaderPolicyInterface::ROTATION_90:
1622 case InputReaderPolicyInterface::ROTATION_270:
Jeff Brownb51719b2010-07-29 18:18:33 -07001623 mLocked.orientedSurfaceWidth = mLocked.surfaceHeight;
1624 mLocked.orientedSurfaceHeight = mLocked.surfaceWidth;
1625 mLocked.orientedXPrecision = mLocked.yPrecision;
1626 mLocked.orientedYPrecision = mLocked.xPrecision;
1627 orientedXScale = mLocked.yScale;
1628 orientedYScale = mLocked.xScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001629 break;
1630 default:
Jeff Brownb51719b2010-07-29 18:18:33 -07001631 mLocked.orientedSurfaceWidth = mLocked.surfaceWidth;
1632 mLocked.orientedSurfaceHeight = mLocked.surfaceHeight;
1633 mLocked.orientedXPrecision = mLocked.xPrecision;
1634 mLocked.orientedYPrecision = mLocked.yPrecision;
1635 orientedXScale = mLocked.xScale;
1636 orientedYScale = mLocked.yScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001637 break;
1638 }
1639
1640 // Configure position ranges.
Jeff Brownb51719b2010-07-29 18:18:33 -07001641 mLocked.orientedRanges.x.min = 0;
1642 mLocked.orientedRanges.x.max = mLocked.orientedSurfaceWidth;
1643 mLocked.orientedRanges.x.flat = 0;
1644 mLocked.orientedRanges.x.fuzz = orientedXScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001645
Jeff Brownb51719b2010-07-29 18:18:33 -07001646 mLocked.orientedRanges.y.min = 0;
1647 mLocked.orientedRanges.y.max = mLocked.orientedSurfaceHeight;
1648 mLocked.orientedRanges.y.flat = 0;
1649 mLocked.orientedRanges.y.fuzz = orientedYScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001650 }
1651
1652 return true;
1653}
1654
Jeff Brown26c94ff2010-09-30 14:33:04 -07001655void TouchInputMapper::dumpSurfaceLocked(String8& dump) {
1656 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth);
1657 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight);
1658 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation);
Jeff Browna665ca82010-09-08 11:49:43 -07001659}
1660
Jeff Brownb51719b2010-07-29 18:18:33 -07001661void TouchInputMapper::configureVirtualKeysLocked() {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001662 assert(mRawAxes.x.valid && mRawAxes.y.valid);
Jeff Browne57e8952010-07-23 21:28:06 -07001663
Jeff Brown38a7fab2010-08-30 03:02:23 -07001664 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Browndb360642010-12-02 13:50:46 -08001665 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Browne57e8952010-07-23 21:28:06 -07001666
Jeff Brownb51719b2010-07-29 18:18:33 -07001667 mLocked.virtualKeys.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07001668
Jeff Brownb51719b2010-07-29 18:18:33 -07001669 if (virtualKeyDefinitions.size() == 0) {
1670 return;
1671 }
Jeff Browne57e8952010-07-23 21:28:06 -07001672
Jeff Brownb51719b2010-07-29 18:18:33 -07001673 mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size());
1674
Jeff Brown38a7fab2010-08-30 03:02:23 -07001675 int32_t touchScreenLeft = mRawAxes.x.minValue;
1676 int32_t touchScreenTop = mRawAxes.y.minValue;
1677 int32_t touchScreenWidth = mRawAxes.x.getRange();
1678 int32_t touchScreenHeight = mRawAxes.y.getRange();
Jeff Brownb51719b2010-07-29 18:18:33 -07001679
1680 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001681 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brownb51719b2010-07-29 18:18:33 -07001682 virtualKeyDefinitions[i];
1683
1684 mLocked.virtualKeys.add();
1685 VirtualKey& virtualKey = mLocked.virtualKeys.editTop();
1686
1687 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1688 int32_t keyCode;
1689 uint32_t flags;
1690 if (getEventHub()->scancodeToKeycode(getDeviceId(), virtualKey.scanCode,
1691 & keyCode, & flags)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001692 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
1693 virtualKey.scanCode);
Jeff Brownb51719b2010-07-29 18:18:33 -07001694 mLocked.virtualKeys.pop(); // drop the key
1695 continue;
Jeff Browne57e8952010-07-23 21:28:06 -07001696 }
1697
Jeff Brownb51719b2010-07-29 18:18:33 -07001698 virtualKey.keyCode = keyCode;
1699 virtualKey.flags = flags;
Jeff Browne57e8952010-07-23 21:28:06 -07001700
Jeff Brownb51719b2010-07-29 18:18:33 -07001701 // convert the key definition's display coordinates into touch coordinates for a hit box
1702 int32_t halfWidth = virtualKeyDefinition.width / 2;
1703 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Browne57e8952010-07-23 21:28:06 -07001704
Jeff Brownb51719b2010-07-29 18:18:33 -07001705 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
1706 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1707 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
1708 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1709 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
1710 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
1711 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
1712 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
Jeff Browne57e8952010-07-23 21:28:06 -07001713
Jeff Brown26c94ff2010-09-30 14:33:04 -07001714 }
1715}
1716
1717void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) {
1718 if (!mLocked.virtualKeys.isEmpty()) {
1719 dump.append(INDENT3 "Virtual Keys:\n");
1720
1721 for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) {
1722 const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i);
1723 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
1724 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1725 i, virtualKey.scanCode, virtualKey.keyCode,
1726 virtualKey.hitLeft, virtualKey.hitRight,
1727 virtualKey.hitTop, virtualKey.hitBottom);
1728 }
Jeff Brownb51719b2010-07-29 18:18:33 -07001729 }
Jeff Browne57e8952010-07-23 21:28:06 -07001730}
1731
Jeff Brown38a7fab2010-08-30 03:02:23 -07001732void TouchInputMapper::parseCalibration() {
Jeff Brown66888372010-11-29 17:37:49 -08001733 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown38a7fab2010-08-30 03:02:23 -07001734 Calibration& out = mCalibration;
1735
Jeff Brown60b57762010-10-18 13:32:20 -07001736 // Position
1737 out.haveXOrigin = in.tryGetProperty(String8("touch.position.xOrigin"), out.xOrigin);
1738 out.haveYOrigin = in.tryGetProperty(String8("touch.position.yOrigin"), out.yOrigin);
1739 out.haveXScale = in.tryGetProperty(String8("touch.position.xScale"), out.xScale);
1740 out.haveYScale = in.tryGetProperty(String8("touch.position.yScale"), out.yScale);
1741
Jeff Brown6b337e72010-10-14 21:42:15 -07001742 // Touch Size
1743 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT;
1744 String8 touchSizeCalibrationString;
1745 if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) {
1746 if (touchSizeCalibrationString == "none") {
1747 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
1748 } else if (touchSizeCalibrationString == "geometric") {
1749 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC;
1750 } else if (touchSizeCalibrationString == "pressure") {
1751 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
1752 } else if (touchSizeCalibrationString != "default") {
1753 LOGW("Invalid value for touch.touchSize.calibration: '%s'",
1754 touchSizeCalibrationString.string());
Jeff Brown38a7fab2010-08-30 03:02:23 -07001755 }
1756 }
1757
Jeff Brown6b337e72010-10-14 21:42:15 -07001758 // Tool Size
1759 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT;
1760 String8 toolSizeCalibrationString;
1761 if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) {
1762 if (toolSizeCalibrationString == "none") {
1763 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
1764 } else if (toolSizeCalibrationString == "geometric") {
1765 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC;
1766 } else if (toolSizeCalibrationString == "linear") {
1767 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
1768 } else if (toolSizeCalibrationString == "area") {
1769 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA;
1770 } else if (toolSizeCalibrationString != "default") {
1771 LOGW("Invalid value for touch.toolSize.calibration: '%s'",
1772 toolSizeCalibrationString.string());
Jeff Brown38a7fab2010-08-30 03:02:23 -07001773 }
1774 }
1775
Jeff Brown6b337e72010-10-14 21:42:15 -07001776 out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"),
1777 out.toolSizeLinearScale);
1778 out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"),
1779 out.toolSizeLinearBias);
1780 out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"),
1781 out.toolSizeAreaScale);
1782 out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"),
1783 out.toolSizeAreaBias);
1784 out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"),
1785 out.toolSizeIsSummed);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001786
1787 // Pressure
1788 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
1789 String8 pressureCalibrationString;
Jeff Brown6b337e72010-10-14 21:42:15 -07001790 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001791 if (pressureCalibrationString == "none") {
1792 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
1793 } else if (pressureCalibrationString == "physical") {
1794 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
1795 } else if (pressureCalibrationString == "amplitude") {
1796 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
1797 } else if (pressureCalibrationString != "default") {
Jeff Brown6b337e72010-10-14 21:42:15 -07001798 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown38a7fab2010-08-30 03:02:23 -07001799 pressureCalibrationString.string());
1800 }
1801 }
1802
1803 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
1804 String8 pressureSourceString;
1805 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
1806 if (pressureSourceString == "pressure") {
1807 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
1808 } else if (pressureSourceString == "touch") {
1809 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
1810 } else if (pressureSourceString != "default") {
1811 LOGW("Invalid value for touch.pressure.source: '%s'",
1812 pressureSourceString.string());
1813 }
1814 }
1815
1816 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
1817 out.pressureScale);
1818
1819 // Size
1820 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
1821 String8 sizeCalibrationString;
Jeff Brown6b337e72010-10-14 21:42:15 -07001822 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001823 if (sizeCalibrationString == "none") {
1824 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
1825 } else if (sizeCalibrationString == "normalized") {
1826 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
1827 } else if (sizeCalibrationString != "default") {
Jeff Brown6b337e72010-10-14 21:42:15 -07001828 LOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Brown38a7fab2010-08-30 03:02:23 -07001829 sizeCalibrationString.string());
1830 }
1831 }
1832
1833 // Orientation
1834 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
1835 String8 orientationCalibrationString;
Jeff Brown6b337e72010-10-14 21:42:15 -07001836 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001837 if (orientationCalibrationString == "none") {
1838 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
1839 } else if (orientationCalibrationString == "interpolated") {
1840 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
1841 } else if (orientationCalibrationString != "default") {
Jeff Brown6b337e72010-10-14 21:42:15 -07001842 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown38a7fab2010-08-30 03:02:23 -07001843 orientationCalibrationString.string());
1844 }
1845 }
1846}
1847
1848void TouchInputMapper::resolveCalibration() {
1849 // Pressure
1850 switch (mCalibration.pressureSource) {
1851 case Calibration::PRESSURE_SOURCE_DEFAULT:
1852 if (mRawAxes.pressure.valid) {
1853 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
1854 } else if (mRawAxes.touchMajor.valid) {
1855 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
1856 }
1857 break;
1858
1859 case Calibration::PRESSURE_SOURCE_PRESSURE:
1860 if (! mRawAxes.pressure.valid) {
1861 LOGW("Calibration property touch.pressure.source is 'pressure' but "
1862 "the pressure axis is not available.");
1863 }
1864 break;
1865
1866 case Calibration::PRESSURE_SOURCE_TOUCH:
1867 if (! mRawAxes.touchMajor.valid) {
1868 LOGW("Calibration property touch.pressure.source is 'touch' but "
1869 "the touchMajor axis is not available.");
1870 }
1871 break;
1872
1873 default:
1874 break;
1875 }
1876
1877 switch (mCalibration.pressureCalibration) {
1878 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
1879 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
1880 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
1881 } else {
1882 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
1883 }
1884 break;
1885
1886 default:
1887 break;
1888 }
1889
Jeff Brown6b337e72010-10-14 21:42:15 -07001890 // Tool Size
1891 switch (mCalibration.toolSizeCalibration) {
1892 case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT:
Jeff Brown38a7fab2010-08-30 03:02:23 -07001893 if (mRawAxes.toolMajor.valid) {
Jeff Brown6b337e72010-10-14 21:42:15 -07001894 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001895 } else {
Jeff Brown6b337e72010-10-14 21:42:15 -07001896 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001897 }
1898 break;
1899
1900 default:
1901 break;
1902 }
1903
Jeff Brown6b337e72010-10-14 21:42:15 -07001904 // Touch Size
1905 switch (mCalibration.touchSizeCalibration) {
1906 case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT:
Jeff Brown38a7fab2010-08-30 03:02:23 -07001907 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
Jeff Brown6b337e72010-10-14 21:42:15 -07001908 && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
1909 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001910 } else {
Jeff Brown6b337e72010-10-14 21:42:15 -07001911 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001912 }
1913 break;
1914
1915 default:
1916 break;
1917 }
1918
1919 // Size
1920 switch (mCalibration.sizeCalibration) {
1921 case Calibration::SIZE_CALIBRATION_DEFAULT:
1922 if (mRawAxes.toolMajor.valid) {
1923 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
1924 } else {
1925 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
1926 }
1927 break;
1928
1929 default:
1930 break;
1931 }
1932
1933 // Orientation
1934 switch (mCalibration.orientationCalibration) {
1935 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
1936 if (mRawAxes.orientation.valid) {
1937 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
1938 } else {
1939 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
1940 }
1941 break;
1942
1943 default:
1944 break;
1945 }
1946}
1947
Jeff Brown26c94ff2010-09-30 14:33:04 -07001948void TouchInputMapper::dumpCalibration(String8& dump) {
1949 dump.append(INDENT3 "Calibration:\n");
Jeff Browna665ca82010-09-08 11:49:43 -07001950
Jeff Brown60b57762010-10-18 13:32:20 -07001951 // Position
1952 if (mCalibration.haveXOrigin) {
1953 dump.appendFormat(INDENT4 "touch.position.xOrigin: %d\n", mCalibration.xOrigin);
1954 }
1955 if (mCalibration.haveYOrigin) {
1956 dump.appendFormat(INDENT4 "touch.position.yOrigin: %d\n", mCalibration.yOrigin);
1957 }
1958 if (mCalibration.haveXScale) {
1959 dump.appendFormat(INDENT4 "touch.position.xScale: %0.3f\n", mCalibration.xScale);
1960 }
1961 if (mCalibration.haveYScale) {
1962 dump.appendFormat(INDENT4 "touch.position.yScale: %0.3f\n", mCalibration.yScale);
1963 }
1964
Jeff Brown6b337e72010-10-14 21:42:15 -07001965 // Touch Size
1966 switch (mCalibration.touchSizeCalibration) {
1967 case Calibration::TOUCH_SIZE_CALIBRATION_NONE:
1968 dump.append(INDENT4 "touch.touchSize.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001969 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001970 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
1971 dump.append(INDENT4 "touch.touchSize.calibration: geometric\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001972 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001973 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
1974 dump.append(INDENT4 "touch.touchSize.calibration: pressure\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001975 break;
1976 default:
1977 assert(false);
1978 }
1979
Jeff Brown6b337e72010-10-14 21:42:15 -07001980 // Tool Size
1981 switch (mCalibration.toolSizeCalibration) {
1982 case Calibration::TOOL_SIZE_CALIBRATION_NONE:
1983 dump.append(INDENT4 "touch.toolSize.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001984 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001985 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
1986 dump.append(INDENT4 "touch.toolSize.calibration: geometric\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001987 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001988 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
1989 dump.append(INDENT4 "touch.toolSize.calibration: linear\n");
1990 break;
1991 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
1992 dump.append(INDENT4 "touch.toolSize.calibration: area\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001993 break;
1994 default:
1995 assert(false);
1996 }
1997
Jeff Brown6b337e72010-10-14 21:42:15 -07001998 if (mCalibration.haveToolSizeLinearScale) {
1999 dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n",
2000 mCalibration.toolSizeLinearScale);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002001 }
2002
Jeff Brown6b337e72010-10-14 21:42:15 -07002003 if (mCalibration.haveToolSizeLinearBias) {
2004 dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n",
2005 mCalibration.toolSizeLinearBias);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002006 }
2007
Jeff Brown6b337e72010-10-14 21:42:15 -07002008 if (mCalibration.haveToolSizeAreaScale) {
2009 dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n",
2010 mCalibration.toolSizeAreaScale);
2011 }
2012
2013 if (mCalibration.haveToolSizeAreaBias) {
2014 dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n",
2015 mCalibration.toolSizeAreaBias);
2016 }
2017
2018 if (mCalibration.haveToolSizeIsSummed) {
Jeff Brown53c16642010-11-18 20:53:46 -08002019 dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %s\n",
Jeff Brown66888372010-11-29 17:37:49 -08002020 toString(mCalibration.toolSizeIsSummed));
Jeff Brown38a7fab2010-08-30 03:02:23 -07002021 }
2022
2023 // Pressure
2024 switch (mCalibration.pressureCalibration) {
2025 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002026 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002027 break;
2028 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002029 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002030 break;
2031 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002032 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002033 break;
2034 default:
2035 assert(false);
2036 }
2037
2038 switch (mCalibration.pressureSource) {
2039 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002040 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002041 break;
2042 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002043 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002044 break;
2045 case Calibration::PRESSURE_SOURCE_DEFAULT:
2046 break;
2047 default:
2048 assert(false);
2049 }
2050
2051 if (mCalibration.havePressureScale) {
Jeff Brown26c94ff2010-09-30 14:33:04 -07002052 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
2053 mCalibration.pressureScale);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002054 }
2055
2056 // Size
2057 switch (mCalibration.sizeCalibration) {
2058 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002059 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002060 break;
2061 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002062 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002063 break;
2064 default:
2065 assert(false);
2066 }
2067
2068 // Orientation
2069 switch (mCalibration.orientationCalibration) {
2070 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002071 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002072 break;
2073 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002074 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002075 break;
2076 default:
2077 assert(false);
2078 }
2079}
2080
Jeff Browne57e8952010-07-23 21:28:06 -07002081void TouchInputMapper::reset() {
2082 // Synthesize touch up event if touch is currently down.
2083 // This will also take care of finishing virtual key processing if needed.
2084 if (mLastTouch.pointerCount != 0) {
2085 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
2086 mCurrentTouch.clear();
2087 syncTouch(when, true);
2088 }
2089
Jeff Brownb51719b2010-07-29 18:18:33 -07002090 { // acquire lock
2091 AutoMutex _l(mLock);
2092 initializeLocked();
2093 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07002094
Jeff Brownb51719b2010-07-29 18:18:33 -07002095 InputMapper::reset();
Jeff Browne57e8952010-07-23 21:28:06 -07002096}
2097
2098void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Browne839a582010-04-22 18:58:52 -07002099 uint32_t policyFlags = 0;
Jeff Browne839a582010-04-22 18:58:52 -07002100
Jeff Brownb51719b2010-07-29 18:18:33 -07002101 // Preprocess pointer data.
Jeff Browne839a582010-04-22 18:58:52 -07002102
Jeff Browne57e8952010-07-23 21:28:06 -07002103 if (mParameters.useBadTouchFilter) {
2104 if (applyBadTouchFilter()) {
Jeff Browne839a582010-04-22 18:58:52 -07002105 havePointerIds = false;
2106 }
2107 }
2108
Jeff Browne57e8952010-07-23 21:28:06 -07002109 if (mParameters.useJumpyTouchFilter) {
2110 if (applyJumpyTouchFilter()) {
Jeff Browne839a582010-04-22 18:58:52 -07002111 havePointerIds = false;
2112 }
2113 }
2114
2115 if (! havePointerIds) {
Jeff Browne57e8952010-07-23 21:28:06 -07002116 calculatePointerIds();
Jeff Browne839a582010-04-22 18:58:52 -07002117 }
2118
Jeff Browne57e8952010-07-23 21:28:06 -07002119 TouchData temp;
2120 TouchData* savedTouch;
2121 if (mParameters.useAveragingTouchFilter) {
2122 temp.copyFrom(mCurrentTouch);
Jeff Browne839a582010-04-22 18:58:52 -07002123 savedTouch = & temp;
2124
Jeff Browne57e8952010-07-23 21:28:06 -07002125 applyAveragingTouchFilter();
Jeff Browne839a582010-04-22 18:58:52 -07002126 } else {
Jeff Browne57e8952010-07-23 21:28:06 -07002127 savedTouch = & mCurrentTouch;
Jeff Browne839a582010-04-22 18:58:52 -07002128 }
2129
Jeff Brownb51719b2010-07-29 18:18:33 -07002130 // Process touches and virtual keys.
Jeff Browne839a582010-04-22 18:58:52 -07002131
Jeff Browne57e8952010-07-23 21:28:06 -07002132 TouchResult touchResult = consumeOffScreenTouches(when, policyFlags);
2133 if (touchResult == DISPATCH_TOUCH) {
2134 dispatchTouches(when, policyFlags);
Jeff Browne839a582010-04-22 18:58:52 -07002135 }
2136
Jeff Brownb51719b2010-07-29 18:18:33 -07002137 // Copy current touch to last touch in preparation for the next cycle.
Jeff Browne839a582010-04-22 18:58:52 -07002138
Jeff Browne57e8952010-07-23 21:28:06 -07002139 if (touchResult == DROP_STROKE) {
2140 mLastTouch.clear();
2141 } else {
2142 mLastTouch.copyFrom(*savedTouch);
Jeff Browne839a582010-04-22 18:58:52 -07002143 }
Jeff Browne839a582010-04-22 18:58:52 -07002144}
2145
Jeff Browne57e8952010-07-23 21:28:06 -07002146TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches(
2147 nsecs_t when, uint32_t policyFlags) {
2148 int32_t keyEventAction, keyEventFlags;
2149 int32_t keyCode, scanCode, downTime;
2150 TouchResult touchResult;
Jeff Brown50de30a2010-06-22 01:27:15 -07002151
Jeff Brownb51719b2010-07-29 18:18:33 -07002152 { // acquire lock
2153 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07002154
Jeff Brownb51719b2010-07-29 18:18:33 -07002155 // Update surface size and orientation, including virtual key positions.
2156 if (! configureSurfaceLocked()) {
2157 return DROP_STROKE;
2158 }
2159
2160 // Check for virtual key press.
2161 if (mLocked.currentVirtualKey.down) {
Jeff Browne57e8952010-07-23 21:28:06 -07002162 if (mCurrentTouch.pointerCount == 0) {
2163 // Pointer went up while virtual key was down.
Jeff Brownb51719b2010-07-29 18:18:33 -07002164 mLocked.currentVirtualKey.down = false;
Jeff Browne57e8952010-07-23 21:28:06 -07002165#if DEBUG_VIRTUAL_KEYS
2166 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brown3c3cc622010-10-20 15:33:38 -07002167 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Browne57e8952010-07-23 21:28:06 -07002168#endif
2169 keyEventAction = AKEY_EVENT_ACTION_UP;
2170 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2171 touchResult = SKIP_TOUCH;
2172 goto DispatchVirtualKey;
2173 }
2174
2175 if (mCurrentTouch.pointerCount == 1) {
2176 int32_t x = mCurrentTouch.pointers[0].x;
2177 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brownb51719b2010-07-29 18:18:33 -07002178 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
2179 if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07002180 // Pointer is still within the space of the virtual key.
2181 return SKIP_TOUCH;
2182 }
2183 }
2184
2185 // Pointer left virtual key area or another pointer also went down.
2186 // Send key cancellation and drop the stroke so subsequent motions will be
2187 // considered fresh downs. This is useful when the user swipes away from the
2188 // virtual key area into the main display surface.
Jeff Brownb51719b2010-07-29 18:18:33 -07002189 mLocked.currentVirtualKey.down = false;
Jeff Browne57e8952010-07-23 21:28:06 -07002190#if DEBUG_VIRTUAL_KEYS
2191 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brown3c3cc622010-10-20 15:33:38 -07002192 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Browne57e8952010-07-23 21:28:06 -07002193#endif
2194 keyEventAction = AKEY_EVENT_ACTION_UP;
2195 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
2196 | AKEY_EVENT_FLAG_CANCELED;
Jeff Brown3c3cc622010-10-20 15:33:38 -07002197
2198 // Check whether the pointer moved inside the display area where we should
2199 // start a new stroke.
2200 int32_t x = mCurrentTouch.pointers[0].x;
2201 int32_t y = mCurrentTouch.pointers[0].y;
2202 if (isPointInsideSurfaceLocked(x, y)) {
2203 mLastTouch.clear();
2204 touchResult = DISPATCH_TOUCH;
2205 } else {
2206 touchResult = DROP_STROKE;
2207 }
Jeff Browne57e8952010-07-23 21:28:06 -07002208 } else {
2209 if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) {
2210 // Pointer just went down. Handle off-screen touches, if needed.
2211 int32_t x = mCurrentTouch.pointers[0].x;
2212 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brownb51719b2010-07-29 18:18:33 -07002213 if (! isPointInsideSurfaceLocked(x, y)) {
Jeff Browne57e8952010-07-23 21:28:06 -07002214 // If exactly one pointer went down, check for virtual key hit.
2215 // Otherwise we will drop the entire stroke.
2216 if (mCurrentTouch.pointerCount == 1) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002217 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
Jeff Browne57e8952010-07-23 21:28:06 -07002218 if (virtualKey) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002219 mLocked.currentVirtualKey.down = true;
2220 mLocked.currentVirtualKey.downTime = when;
2221 mLocked.currentVirtualKey.keyCode = virtualKey->keyCode;
2222 mLocked.currentVirtualKey.scanCode = virtualKey->scanCode;
Jeff Browne57e8952010-07-23 21:28:06 -07002223#if DEBUG_VIRTUAL_KEYS
2224 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brown3c3cc622010-10-20 15:33:38 -07002225 mLocked.currentVirtualKey.keyCode,
2226 mLocked.currentVirtualKey.scanCode);
Jeff Browne57e8952010-07-23 21:28:06 -07002227#endif
2228 keyEventAction = AKEY_EVENT_ACTION_DOWN;
2229 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM
2230 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2231 touchResult = SKIP_TOUCH;
2232 goto DispatchVirtualKey;
2233 }
2234 }
2235 return DROP_STROKE;
2236 }
2237 }
2238 return DISPATCH_TOUCH;
2239 }
2240
2241 DispatchVirtualKey:
2242 // Collect remaining state needed to dispatch virtual key.
Jeff Brownb51719b2010-07-29 18:18:33 -07002243 keyCode = mLocked.currentVirtualKey.keyCode;
2244 scanCode = mLocked.currentVirtualKey.scanCode;
2245 downTime = mLocked.currentVirtualKey.downTime;
2246 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07002247
2248 // Dispatch virtual key.
2249 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown956c0fb2010-10-01 14:55:30 -07002250 policyFlags |= POLICY_FLAG_VIRTUAL;
Jeff Brown90f0cee2010-10-08 22:31:17 -07002251 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
2252 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
2253 return touchResult;
Jeff Browne839a582010-04-22 18:58:52 -07002254}
2255
Jeff Browne57e8952010-07-23 21:28:06 -07002256void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
2257 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2258 uint32_t lastPointerCount = mLastTouch.pointerCount;
Jeff Browne839a582010-04-22 18:58:52 -07002259 if (currentPointerCount == 0 && lastPointerCount == 0) {
2260 return; // nothing to do!
2261 }
2262
Jeff Browne57e8952010-07-23 21:28:06 -07002263 BitSet32 currentIdBits = mCurrentTouch.idBits;
2264 BitSet32 lastIdBits = mLastTouch.idBits;
Jeff Browne839a582010-04-22 18:58:52 -07002265
2266 if (currentIdBits == lastIdBits) {
2267 // No pointer id changes so this is a move event.
2268 // The dispatcher takes care of batching moves so we don't have to deal with that here.
Jeff Brown5c1ed842010-07-14 18:48:53 -07002269 int32_t motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browne57e8952010-07-23 21:28:06 -07002270 dispatchTouch(when, policyFlags, & mCurrentTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002271 currentIdBits, -1, currentPointerCount, motionEventAction);
Jeff Browne839a582010-04-22 18:58:52 -07002272 } else {
Jeff Brown3c3cc622010-10-20 15:33:38 -07002273 // There may be pointers going up and pointers going down and pointers moving
2274 // all at the same time.
Jeff Browne839a582010-04-22 18:58:52 -07002275 BitSet32 upIdBits(lastIdBits.value & ~ currentIdBits.value);
2276 BitSet32 downIdBits(currentIdBits.value & ~ lastIdBits.value);
2277 BitSet32 activeIdBits(lastIdBits.value);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002278 uint32_t pointerCount = lastPointerCount;
Jeff Browne839a582010-04-22 18:58:52 -07002279
Jeff Brown3c3cc622010-10-20 15:33:38 -07002280 // Produce an intermediate representation of the touch data that consists of the
2281 // old location of pointers that have just gone up and the new location of pointers that
2282 // have just moved but omits the location of pointers that have just gone down.
2283 TouchData interimTouch;
2284 interimTouch.copyFrom(mLastTouch);
2285
2286 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
2287 bool moveNeeded = false;
2288 while (!moveIdBits.isEmpty()) {
2289 uint32_t moveId = moveIdBits.firstMarkedBit();
2290 moveIdBits.clearBit(moveId);
2291
2292 int32_t oldIndex = mLastTouch.idToIndex[moveId];
2293 int32_t newIndex = mCurrentTouch.idToIndex[moveId];
2294 if (mLastTouch.pointers[oldIndex] != mCurrentTouch.pointers[newIndex]) {
2295 interimTouch.pointers[oldIndex] = mCurrentTouch.pointers[newIndex];
2296 moveNeeded = true;
2297 }
2298 }
2299
2300 // Dispatch pointer up events using the interim pointer locations.
2301 while (!upIdBits.isEmpty()) {
Jeff Browne839a582010-04-22 18:58:52 -07002302 uint32_t upId = upIdBits.firstMarkedBit();
2303 upIdBits.clearBit(upId);
2304 BitSet32 oldActiveIdBits = activeIdBits;
2305 activeIdBits.clearBit(upId);
2306
2307 int32_t motionEventAction;
2308 if (activeIdBits.isEmpty()) {
Jeff Brown5c1ed842010-07-14 18:48:53 -07002309 motionEventAction = AMOTION_EVENT_ACTION_UP;
Jeff Browne839a582010-04-22 18:58:52 -07002310 } else {
Jeff Brown3cf1c9b2010-07-16 15:01:56 -07002311 motionEventAction = AMOTION_EVENT_ACTION_POINTER_UP;
Jeff Browne839a582010-04-22 18:58:52 -07002312 }
2313
Jeff Brown3c3cc622010-10-20 15:33:38 -07002314 dispatchTouch(when, policyFlags, &interimTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002315 oldActiveIdBits, upId, pointerCount, motionEventAction);
2316 pointerCount -= 1;
Jeff Browne839a582010-04-22 18:58:52 -07002317 }
2318
Jeff Brown3c3cc622010-10-20 15:33:38 -07002319 // Dispatch move events if any of the remaining pointers moved from their old locations.
2320 // Although applications receive new locations as part of individual pointer up
2321 // events, they do not generally handle them except when presented in a move event.
2322 if (moveNeeded) {
2323 dispatchTouch(when, policyFlags, &mCurrentTouch,
2324 activeIdBits, -1, pointerCount, AMOTION_EVENT_ACTION_MOVE);
2325 }
2326
2327 // Dispatch pointer down events using the new pointer locations.
2328 while (!downIdBits.isEmpty()) {
Jeff Browne839a582010-04-22 18:58:52 -07002329 uint32_t downId = downIdBits.firstMarkedBit();
2330 downIdBits.clearBit(downId);
2331 BitSet32 oldActiveIdBits = activeIdBits;
2332 activeIdBits.markBit(downId);
2333
2334 int32_t motionEventAction;
2335 if (oldActiveIdBits.isEmpty()) {
Jeff Brown5c1ed842010-07-14 18:48:53 -07002336 motionEventAction = AMOTION_EVENT_ACTION_DOWN;
Jeff Browne57e8952010-07-23 21:28:06 -07002337 mDownTime = when;
Jeff Browne839a582010-04-22 18:58:52 -07002338 } else {
Jeff Brown3cf1c9b2010-07-16 15:01:56 -07002339 motionEventAction = AMOTION_EVENT_ACTION_POINTER_DOWN;
Jeff Browne839a582010-04-22 18:58:52 -07002340 }
2341
Jeff Brown38a7fab2010-08-30 03:02:23 -07002342 pointerCount += 1;
Jeff Brown3c3cc622010-10-20 15:33:38 -07002343 dispatchTouch(when, policyFlags, &mCurrentTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002344 activeIdBits, downId, pointerCount, motionEventAction);
Jeff Browne839a582010-04-22 18:58:52 -07002345 }
2346 }
2347}
2348
Jeff Browne57e8952010-07-23 21:28:06 -07002349void TouchInputMapper::dispatchTouch(nsecs_t when, uint32_t policyFlags,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002350 TouchData* touch, BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
Jeff Browne839a582010-04-22 18:58:52 -07002351 int32_t motionEventAction) {
Jeff Browne839a582010-04-22 18:58:52 -07002352 int32_t pointerIds[MAX_POINTERS];
2353 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Browne839a582010-04-22 18:58:52 -07002354 int32_t motionEventEdgeFlags = 0;
Jeff Brownb51719b2010-07-29 18:18:33 -07002355 float xPrecision, yPrecision;
2356
2357 { // acquire lock
2358 AutoMutex _l(mLock);
2359
2360 // Walk through the the active pointers and map touch screen coordinates (TouchData) into
2361 // display coordinates (PointerCoords) and adjust for display orientation.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002362 for (uint32_t outIndex = 0; ! idBits.isEmpty(); outIndex++) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002363 uint32_t id = idBits.firstMarkedBit();
2364 idBits.clearBit(id);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002365 uint32_t inIndex = touch->idToIndex[id];
Jeff Brownb51719b2010-07-29 18:18:33 -07002366
Jeff Brown38a7fab2010-08-30 03:02:23 -07002367 const PointerData& in = touch->pointers[inIndex];
Jeff Brownb51719b2010-07-29 18:18:33 -07002368
Jeff Brown38a7fab2010-08-30 03:02:23 -07002369 // X and Y
2370 float x = float(in.x - mLocked.xOrigin) * mLocked.xScale;
2371 float y = float(in.y - mLocked.yOrigin) * mLocked.yScale;
Jeff Brownb51719b2010-07-29 18:18:33 -07002372
Jeff Brown38a7fab2010-08-30 03:02:23 -07002373 // ToolMajor and ToolMinor
2374 float toolMajor, toolMinor;
Jeff Brown6b337e72010-10-14 21:42:15 -07002375 switch (mCalibration.toolSizeCalibration) {
2376 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002377 toolMajor = in.toolMajor * mLocked.geometricScale;
2378 if (mRawAxes.toolMinor.valid) {
2379 toolMinor = in.toolMinor * mLocked.geometricScale;
2380 } else {
2381 toolMinor = toolMajor;
2382 }
2383 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002384 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002385 toolMajor = in.toolMajor != 0
Jeff Brown6b337e72010-10-14 21:42:15 -07002386 ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias
Jeff Brown38a7fab2010-08-30 03:02:23 -07002387 : 0;
2388 if (mRawAxes.toolMinor.valid) {
2389 toolMinor = in.toolMinor != 0
Jeff Brown6b337e72010-10-14 21:42:15 -07002390 ? in.toolMinor * mLocked.toolSizeLinearScale
2391 + mLocked.toolSizeLinearBias
Jeff Brown38a7fab2010-08-30 03:02:23 -07002392 : 0;
2393 } else {
2394 toolMinor = toolMajor;
2395 }
2396 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002397 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2398 if (in.toolMajor != 0) {
2399 float diameter = sqrtf(in.toolMajor
2400 * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias);
2401 toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias;
2402 } else {
2403 toolMajor = 0;
2404 }
2405 toolMinor = toolMajor;
2406 break;
Jeff Brown38a7fab2010-08-30 03:02:23 -07002407 default:
2408 toolMajor = 0;
2409 toolMinor = 0;
2410 break;
Jeff Brownb51719b2010-07-29 18:18:33 -07002411 }
2412
Jeff Brown6b337e72010-10-14 21:42:15 -07002413 if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002414 toolMajor /= pointerCount;
2415 toolMinor /= pointerCount;
2416 }
2417
2418 // Pressure
2419 float rawPressure;
2420 switch (mCalibration.pressureSource) {
2421 case Calibration::PRESSURE_SOURCE_PRESSURE:
2422 rawPressure = in.pressure;
2423 break;
2424 case Calibration::PRESSURE_SOURCE_TOUCH:
2425 rawPressure = in.touchMajor;
2426 break;
2427 default:
2428 rawPressure = 0;
2429 }
2430
2431 float pressure;
2432 switch (mCalibration.pressureCalibration) {
2433 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
2434 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
2435 pressure = rawPressure * mLocked.pressureScale;
2436 break;
2437 default:
2438 pressure = 1;
2439 break;
2440 }
2441
2442 // TouchMajor and TouchMinor
2443 float touchMajor, touchMinor;
Jeff Brown6b337e72010-10-14 21:42:15 -07002444 switch (mCalibration.touchSizeCalibration) {
2445 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002446 touchMajor = in.touchMajor * mLocked.geometricScale;
2447 if (mRawAxes.touchMinor.valid) {
2448 touchMinor = in.touchMinor * mLocked.geometricScale;
2449 } else {
2450 touchMinor = touchMajor;
2451 }
2452 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002453 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002454 touchMajor = toolMajor * pressure;
2455 touchMinor = toolMinor * pressure;
2456 break;
2457 default:
2458 touchMajor = 0;
2459 touchMinor = 0;
2460 break;
2461 }
2462
2463 if (touchMajor > toolMajor) {
2464 touchMajor = toolMajor;
2465 }
2466 if (touchMinor > toolMinor) {
2467 touchMinor = toolMinor;
2468 }
2469
2470 // Size
2471 float size;
2472 switch (mCalibration.sizeCalibration) {
2473 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
2474 float rawSize = mRawAxes.toolMinor.valid
2475 ? avg(in.toolMajor, in.toolMinor)
2476 : in.toolMajor;
2477 size = rawSize * mLocked.sizeScale;
2478 break;
2479 }
2480 default:
2481 size = 0;
2482 break;
2483 }
2484
2485 // Orientation
2486 float orientation;
2487 switch (mCalibration.orientationCalibration) {
2488 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
2489 orientation = in.orientation * mLocked.orientationScale;
2490 break;
2491 default:
2492 orientation = 0;
2493 }
2494
2495 // Adjust coords for orientation.
Jeff Brownb51719b2010-07-29 18:18:33 -07002496 switch (mLocked.surfaceOrientation) {
2497 case InputReaderPolicyInterface::ROTATION_90: {
2498 float xTemp = x;
2499 x = y;
2500 y = mLocked.surfaceWidth - xTemp;
2501 orientation -= M_PI_2;
2502 if (orientation < - M_PI_2) {
2503 orientation += M_PI;
2504 }
2505 break;
2506 }
2507 case InputReaderPolicyInterface::ROTATION_180: {
2508 x = mLocked.surfaceWidth - x;
2509 y = mLocked.surfaceHeight - y;
2510 orientation = - orientation;
2511 break;
2512 }
2513 case InputReaderPolicyInterface::ROTATION_270: {
2514 float xTemp = x;
2515 x = mLocked.surfaceHeight - y;
2516 y = xTemp;
2517 orientation += M_PI_2;
2518 if (orientation > M_PI_2) {
2519 orientation -= M_PI;
2520 }
2521 break;
2522 }
2523 }
2524
Jeff Brown38a7fab2010-08-30 03:02:23 -07002525 // Write output coords.
2526 PointerCoords& out = pointerCoords[outIndex];
2527 out.x = x;
2528 out.y = y;
2529 out.pressure = pressure;
2530 out.size = size;
2531 out.touchMajor = touchMajor;
2532 out.touchMinor = touchMinor;
2533 out.toolMajor = toolMajor;
2534 out.toolMinor = toolMinor;
2535 out.orientation = orientation;
Jeff Brownb51719b2010-07-29 18:18:33 -07002536
Jeff Brown38a7fab2010-08-30 03:02:23 -07002537 pointerIds[outIndex] = int32_t(id);
Jeff Brownb51719b2010-07-29 18:18:33 -07002538
2539 if (id == changedId) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002540 motionEventAction |= outIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
Jeff Brownb51719b2010-07-29 18:18:33 -07002541 }
Jeff Browne839a582010-04-22 18:58:52 -07002542 }
Jeff Brownb51719b2010-07-29 18:18:33 -07002543
2544 // Check edge flags by looking only at the first pointer since the flags are
2545 // global to the event.
2546 if (motionEventAction == AMOTION_EVENT_ACTION_DOWN) {
2547 if (pointerCoords[0].x <= 0) {
2548 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_LEFT;
2549 } else if (pointerCoords[0].x >= mLocked.orientedSurfaceWidth) {
2550 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_RIGHT;
2551 }
2552 if (pointerCoords[0].y <= 0) {
2553 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_TOP;
2554 } else if (pointerCoords[0].y >= mLocked.orientedSurfaceHeight) {
2555 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_BOTTOM;
2556 }
Jeff Browne839a582010-04-22 18:58:52 -07002557 }
Jeff Brownb51719b2010-07-29 18:18:33 -07002558
2559 xPrecision = mLocked.orientedXPrecision;
2560 yPrecision = mLocked.orientedYPrecision;
2561 } // release lock
Jeff Browne839a582010-04-22 18:58:52 -07002562
Jeff Brown77e26fc2010-10-07 13:44:51 -07002563 getDispatcher()->notifyMotion(when, getDeviceId(), getSources(), policyFlags,
Jeff Brownaf30ff62010-09-01 17:01:00 -07002564 motionEventAction, 0, getContext()->getGlobalMetaState(), motionEventEdgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -07002565 pointerCount, pointerIds, pointerCoords,
Jeff Brownb51719b2010-07-29 18:18:33 -07002566 xPrecision, yPrecision, mDownTime);
Jeff Browne839a582010-04-22 18:58:52 -07002567}
2568
Jeff Brownb51719b2010-07-29 18:18:33 -07002569bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002570 if (mRawAxes.x.valid && mRawAxes.y.valid) {
2571 return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue
2572 && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue;
Jeff Browne839a582010-04-22 18:58:52 -07002573 }
Jeff Browne57e8952010-07-23 21:28:06 -07002574 return true;
2575}
2576
Jeff Brownb51719b2010-07-29 18:18:33 -07002577const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked(
2578 int32_t x, int32_t y) {
2579 size_t numVirtualKeys = mLocked.virtualKeys.size();
2580 for (size_t i = 0; i < numVirtualKeys; i++) {
2581 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07002582
2583#if DEBUG_VIRTUAL_KEYS
2584 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
2585 "left=%d, top=%d, right=%d, bottom=%d",
2586 x, y,
2587 virtualKey.keyCode, virtualKey.scanCode,
2588 virtualKey.hitLeft, virtualKey.hitTop,
2589 virtualKey.hitRight, virtualKey.hitBottom);
2590#endif
2591
2592 if (virtualKey.isHit(x, y)) {
2593 return & virtualKey;
2594 }
2595 }
2596
2597 return NULL;
2598}
2599
2600void TouchInputMapper::calculatePointerIds() {
2601 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2602 uint32_t lastPointerCount = mLastTouch.pointerCount;
2603
2604 if (currentPointerCount == 0) {
2605 // No pointers to assign.
2606 mCurrentTouch.idBits.clear();
2607 } else if (lastPointerCount == 0) {
2608 // All pointers are new.
2609 mCurrentTouch.idBits.clear();
2610 for (uint32_t i = 0; i < currentPointerCount; i++) {
2611 mCurrentTouch.pointers[i].id = i;
2612 mCurrentTouch.idToIndex[i] = i;
2613 mCurrentTouch.idBits.markBit(i);
2614 }
2615 } else if (currentPointerCount == 1 && lastPointerCount == 1) {
2616 // Only one pointer and no change in count so it must have the same id as before.
2617 uint32_t id = mLastTouch.pointers[0].id;
2618 mCurrentTouch.pointers[0].id = id;
2619 mCurrentTouch.idToIndex[id] = 0;
2620 mCurrentTouch.idBits.value = BitSet32::valueForBit(id);
2621 } else {
2622 // General case.
2623 // We build a heap of squared euclidean distances between current and last pointers
2624 // associated with the current and last pointer indices. Then, we find the best
2625 // match (by distance) for each current pointer.
2626 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
2627
2628 uint32_t heapSize = 0;
2629 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
2630 currentPointerIndex++) {
2631 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
2632 lastPointerIndex++) {
2633 int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x
2634 - mLastTouch.pointers[lastPointerIndex].x;
2635 int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y
2636 - mLastTouch.pointers[lastPointerIndex].y;
2637
2638 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
2639
2640 // Insert new element into the heap (sift up).
2641 heap[heapSize].currentPointerIndex = currentPointerIndex;
2642 heap[heapSize].lastPointerIndex = lastPointerIndex;
2643 heap[heapSize].distance = distance;
2644 heapSize += 1;
2645 }
2646 }
2647
2648 // Heapify
2649 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
2650 startIndex -= 1;
2651 for (uint32_t parentIndex = startIndex; ;) {
2652 uint32_t childIndex = parentIndex * 2 + 1;
2653 if (childIndex >= heapSize) {
2654 break;
2655 }
2656
2657 if (childIndex + 1 < heapSize
2658 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2659 childIndex += 1;
2660 }
2661
2662 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2663 break;
2664 }
2665
2666 swap(heap[parentIndex], heap[childIndex]);
2667 parentIndex = childIndex;
2668 }
2669 }
2670
2671#if DEBUG_POINTER_ASSIGNMENT
2672 LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize);
2673 for (size_t i = 0; i < heapSize; i++) {
2674 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2675 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2676 heap[i].distance);
2677 }
2678#endif
2679
2680 // Pull matches out by increasing order of distance.
2681 // To avoid reassigning pointers that have already been matched, the loop keeps track
2682 // of which last and current pointers have been matched using the matchedXXXBits variables.
2683 // It also tracks the used pointer id bits.
2684 BitSet32 matchedLastBits(0);
2685 BitSet32 matchedCurrentBits(0);
2686 BitSet32 usedIdBits(0);
2687 bool first = true;
2688 for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) {
2689 for (;;) {
2690 if (first) {
2691 // The first time through the loop, we just consume the root element of
2692 // the heap (the one with smallest distance).
2693 first = false;
2694 } else {
2695 // Previous iterations consumed the root element of the heap.
2696 // Pop root element off of the heap (sift down).
2697 heapSize -= 1;
2698 assert(heapSize > 0);
2699
2700 // Sift down.
2701 heap[0] = heap[heapSize];
2702 for (uint32_t parentIndex = 0; ;) {
2703 uint32_t childIndex = parentIndex * 2 + 1;
2704 if (childIndex >= heapSize) {
2705 break;
2706 }
2707
2708 if (childIndex + 1 < heapSize
2709 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2710 childIndex += 1;
2711 }
2712
2713 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2714 break;
2715 }
2716
2717 swap(heap[parentIndex], heap[childIndex]);
2718 parentIndex = childIndex;
2719 }
2720
2721#if DEBUG_POINTER_ASSIGNMENT
2722 LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize);
2723 for (size_t i = 0; i < heapSize; i++) {
2724 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2725 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2726 heap[i].distance);
2727 }
2728#endif
2729 }
2730
2731 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
2732 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
2733
2734 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
2735 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
2736
2737 matchedCurrentBits.markBit(currentPointerIndex);
2738 matchedLastBits.markBit(lastPointerIndex);
2739
2740 uint32_t id = mLastTouch.pointers[lastPointerIndex].id;
2741 mCurrentTouch.pointers[currentPointerIndex].id = id;
2742 mCurrentTouch.idToIndex[id] = currentPointerIndex;
2743 usedIdBits.markBit(id);
2744
2745#if DEBUG_POINTER_ASSIGNMENT
2746 LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
2747 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
2748#endif
2749 break;
2750 }
2751 }
2752
2753 // Assign fresh ids to new pointers.
2754 if (currentPointerCount > lastPointerCount) {
2755 for (uint32_t i = currentPointerCount - lastPointerCount; ;) {
2756 uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit();
2757 uint32_t id = usedIdBits.firstUnmarkedBit();
2758
2759 mCurrentTouch.pointers[currentPointerIndex].id = id;
2760 mCurrentTouch.idToIndex[id] = currentPointerIndex;
2761 usedIdBits.markBit(id);
2762
2763#if DEBUG_POINTER_ASSIGNMENT
2764 LOGD("calculatePointerIds - assigned: cur=%d, id=%d",
2765 currentPointerIndex, id);
2766#endif
2767
2768 if (--i == 0) break; // done
2769 matchedCurrentBits.markBit(currentPointerIndex);
2770 }
2771 }
2772
2773 // Fix id bits.
2774 mCurrentTouch.idBits = usedIdBits;
2775 }
2776}
2777
2778/* Special hack for devices that have bad screen data: if one of the
2779 * points has moved more than a screen height from the last position,
2780 * then drop it. */
2781bool TouchInputMapper::applyBadTouchFilter() {
2782 // This hack requires valid axis parameters.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002783 if (! mRawAxes.y.valid) {
Jeff Browne57e8952010-07-23 21:28:06 -07002784 return false;
2785 }
2786
2787 uint32_t pointerCount = mCurrentTouch.pointerCount;
2788
2789 // Nothing to do if there are no points.
2790 if (pointerCount == 0) {
2791 return false;
2792 }
2793
2794 // Don't do anything if a finger is going down or up. We run
2795 // here before assigning pointer IDs, so there isn't a good
2796 // way to do per-finger matching.
2797 if (pointerCount != mLastTouch.pointerCount) {
2798 return false;
2799 }
2800
2801 // We consider a single movement across more than a 7/16 of
2802 // the long size of the screen to be bad. This was a magic value
2803 // determined by looking at the maximum distance it is feasible
2804 // to actually move in one sample.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002805 int32_t maxDeltaY = mRawAxes.y.getRange() * 7 / 16;
Jeff Browne57e8952010-07-23 21:28:06 -07002806
2807 // XXX The original code in InputDevice.java included commented out
2808 // code for testing the X axis. Note that when we drop a point
2809 // we don't actually restore the old X either. Strange.
2810 // The old code also tries to track when bad points were previously
2811 // detected but it turns out that due to the placement of a "break"
2812 // at the end of the loop, we never set mDroppedBadPoint to true
2813 // so it is effectively dead code.
2814 // Need to figure out if the old code is busted or just overcomplicated
2815 // but working as intended.
2816
2817 // Look through all new points and see if any are farther than
2818 // acceptable from all previous points.
2819 for (uint32_t i = pointerCount; i-- > 0; ) {
2820 int32_t y = mCurrentTouch.pointers[i].y;
2821 int32_t closestY = INT_MAX;
2822 int32_t closestDeltaY = 0;
2823
2824#if DEBUG_HACKS
2825 LOGD("BadTouchFilter: Looking at next point #%d: y=%d", i, y);
2826#endif
2827
2828 for (uint32_t j = pointerCount; j-- > 0; ) {
2829 int32_t lastY = mLastTouch.pointers[j].y;
2830 int32_t deltaY = abs(y - lastY);
2831
2832#if DEBUG_HACKS
2833 LOGD("BadTouchFilter: Comparing with last point #%d: y=%d deltaY=%d",
2834 j, lastY, deltaY);
2835#endif
2836
2837 if (deltaY < maxDeltaY) {
2838 goto SkipSufficientlyClosePoint;
2839 }
2840 if (deltaY < closestDeltaY) {
2841 closestDeltaY = deltaY;
2842 closestY = lastY;
2843 }
2844 }
2845
2846 // Must not have found a close enough match.
2847#if DEBUG_HACKS
2848 LOGD("BadTouchFilter: Dropping bad point #%d: newY=%d oldY=%d deltaY=%d maxDeltaY=%d",
2849 i, y, closestY, closestDeltaY, maxDeltaY);
2850#endif
2851
2852 mCurrentTouch.pointers[i].y = closestY;
2853 return true; // XXX original code only corrects one point
2854
2855 SkipSufficientlyClosePoint: ;
2856 }
2857
2858 // No change.
2859 return false;
2860}
2861
2862/* Special hack for devices that have bad screen data: drop points where
2863 * the coordinate value for one axis has jumped to the other pointer's location.
2864 */
2865bool TouchInputMapper::applyJumpyTouchFilter() {
2866 // This hack requires valid axis parameters.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002867 if (! mRawAxes.y.valid) {
Jeff Browne57e8952010-07-23 21:28:06 -07002868 return false;
2869 }
2870
2871 uint32_t pointerCount = mCurrentTouch.pointerCount;
2872 if (mLastTouch.pointerCount != pointerCount) {
2873#if DEBUG_HACKS
2874 LOGD("JumpyTouchFilter: Different pointer count %d -> %d",
2875 mLastTouch.pointerCount, pointerCount);
2876 for (uint32_t i = 0; i < pointerCount; i++) {
2877 LOGD(" Pointer %d (%d, %d)", i,
2878 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
2879 }
2880#endif
2881
2882 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_TRANSITION_DROPS) {
2883 if (mLastTouch.pointerCount == 1 && pointerCount == 2) {
2884 // Just drop the first few events going from 1 to 2 pointers.
2885 // They're bad often enough that they're not worth considering.
2886 mCurrentTouch.pointerCount = 1;
2887 mJumpyTouchFilter.jumpyPointsDropped += 1;
2888
2889#if DEBUG_HACKS
2890 LOGD("JumpyTouchFilter: Pointer 2 dropped");
2891#endif
2892 return true;
2893 } else if (mLastTouch.pointerCount == 2 && pointerCount == 1) {
2894 // The event when we go from 2 -> 1 tends to be messed up too
2895 mCurrentTouch.pointerCount = 2;
2896 mCurrentTouch.pointers[0] = mLastTouch.pointers[0];
2897 mCurrentTouch.pointers[1] = mLastTouch.pointers[1];
2898 mJumpyTouchFilter.jumpyPointsDropped += 1;
2899
2900#if DEBUG_HACKS
2901 for (int32_t i = 0; i < 2; i++) {
2902 LOGD("JumpyTouchFilter: Pointer %d replaced (%d, %d)", i,
2903 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
2904 }
2905#endif
2906 return true;
2907 }
2908 }
2909 // Reset jumpy points dropped on other transitions or if limit exceeded.
2910 mJumpyTouchFilter.jumpyPointsDropped = 0;
2911
2912#if DEBUG_HACKS
2913 LOGD("JumpyTouchFilter: Transition - drop limit reset");
2914#endif
2915 return false;
2916 }
2917
2918 // We have the same number of pointers as last time.
2919 // A 'jumpy' point is one where the coordinate value for one axis
2920 // has jumped to the other pointer's location. No need to do anything
2921 // else if we only have one pointer.
2922 if (pointerCount < 2) {
2923 return false;
2924 }
2925
2926 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_DROP_LIMIT) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002927 int jumpyEpsilon = mRawAxes.y.getRange() / JUMPY_EPSILON_DIVISOR;
Jeff Browne57e8952010-07-23 21:28:06 -07002928
2929 // We only replace the single worst jumpy point as characterized by pointer distance
2930 // in a single axis.
2931 int32_t badPointerIndex = -1;
2932 int32_t badPointerReplacementIndex = -1;
2933 int32_t badPointerDistance = INT_MIN; // distance to be corrected
2934
2935 for (uint32_t i = pointerCount; i-- > 0; ) {
2936 int32_t x = mCurrentTouch.pointers[i].x;
2937 int32_t y = mCurrentTouch.pointers[i].y;
2938
2939#if DEBUG_HACKS
2940 LOGD("JumpyTouchFilter: Point %d (%d, %d)", i, x, y);
2941#endif
2942
2943 // Check if a touch point is too close to another's coordinates
2944 bool dropX = false, dropY = false;
2945 for (uint32_t j = 0; j < pointerCount; j++) {
2946 if (i == j) {
2947 continue;
2948 }
2949
2950 if (abs(x - mCurrentTouch.pointers[j].x) <= jumpyEpsilon) {
2951 dropX = true;
2952 break;
2953 }
2954
2955 if (abs(y - mCurrentTouch.pointers[j].y) <= jumpyEpsilon) {
2956 dropY = true;
2957 break;
2958 }
2959 }
2960 if (! dropX && ! dropY) {
2961 continue; // not jumpy
2962 }
2963
2964 // Find a replacement candidate by comparing with older points on the
2965 // complementary (non-jumpy) axis.
2966 int32_t distance = INT_MIN; // distance to be corrected
2967 int32_t replacementIndex = -1;
2968
2969 if (dropX) {
2970 // X looks too close. Find an older replacement point with a close Y.
2971 int32_t smallestDeltaY = INT_MAX;
2972 for (uint32_t j = 0; j < pointerCount; j++) {
2973 int32_t deltaY = abs(y - mLastTouch.pointers[j].y);
2974 if (deltaY < smallestDeltaY) {
2975 smallestDeltaY = deltaY;
2976 replacementIndex = j;
2977 }
2978 }
2979 distance = abs(x - mLastTouch.pointers[replacementIndex].x);
2980 } else {
2981 // Y looks too close. Find an older replacement point with a close X.
2982 int32_t smallestDeltaX = INT_MAX;
2983 for (uint32_t j = 0; j < pointerCount; j++) {
2984 int32_t deltaX = abs(x - mLastTouch.pointers[j].x);
2985 if (deltaX < smallestDeltaX) {
2986 smallestDeltaX = deltaX;
2987 replacementIndex = j;
2988 }
2989 }
2990 distance = abs(y - mLastTouch.pointers[replacementIndex].y);
2991 }
2992
2993 // If replacing this pointer would correct a worse error than the previous ones
2994 // considered, then use this replacement instead.
2995 if (distance > badPointerDistance) {
2996 badPointerIndex = i;
2997 badPointerReplacementIndex = replacementIndex;
2998 badPointerDistance = distance;
2999 }
3000 }
3001
3002 // Correct the jumpy pointer if one was found.
3003 if (badPointerIndex >= 0) {
3004#if DEBUG_HACKS
3005 LOGD("JumpyTouchFilter: Replacing bad pointer %d with (%d, %d)",
3006 badPointerIndex,
3007 mLastTouch.pointers[badPointerReplacementIndex].x,
3008 mLastTouch.pointers[badPointerReplacementIndex].y);
3009#endif
3010
3011 mCurrentTouch.pointers[badPointerIndex].x =
3012 mLastTouch.pointers[badPointerReplacementIndex].x;
3013 mCurrentTouch.pointers[badPointerIndex].y =
3014 mLastTouch.pointers[badPointerReplacementIndex].y;
3015 mJumpyTouchFilter.jumpyPointsDropped += 1;
3016 return true;
3017 }
3018 }
3019
3020 mJumpyTouchFilter.jumpyPointsDropped = 0;
3021 return false;
3022}
3023
3024/* Special hack for devices that have bad screen data: aggregate and
3025 * compute averages of the coordinate data, to reduce the amount of
3026 * jitter seen by applications. */
3027void TouchInputMapper::applyAveragingTouchFilter() {
3028 for (uint32_t currentIndex = 0; currentIndex < mCurrentTouch.pointerCount; currentIndex++) {
3029 uint32_t id = mCurrentTouch.pointers[currentIndex].id;
3030 int32_t x = mCurrentTouch.pointers[currentIndex].x;
3031 int32_t y = mCurrentTouch.pointers[currentIndex].y;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003032 int32_t pressure;
3033 switch (mCalibration.pressureSource) {
3034 case Calibration::PRESSURE_SOURCE_PRESSURE:
3035 pressure = mCurrentTouch.pointers[currentIndex].pressure;
3036 break;
3037 case Calibration::PRESSURE_SOURCE_TOUCH:
3038 pressure = mCurrentTouch.pointers[currentIndex].touchMajor;
3039 break;
3040 default:
3041 pressure = 1;
3042 break;
3043 }
Jeff Browne57e8952010-07-23 21:28:06 -07003044
3045 if (mLastTouch.idBits.hasBit(id)) {
3046 // Pointer was down before and is still down now.
3047 // Compute average over history trace.
3048 uint32_t start = mAveragingTouchFilter.historyStart[id];
3049 uint32_t end = mAveragingTouchFilter.historyEnd[id];
3050
3051 int64_t deltaX = x - mAveragingTouchFilter.historyData[end].pointers[id].x;
3052 int64_t deltaY = y - mAveragingTouchFilter.historyData[end].pointers[id].y;
3053 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3054
3055#if DEBUG_HACKS
3056 LOGD("AveragingTouchFilter: Pointer id %d - Distance from last sample: %lld",
3057 id, distance);
3058#endif
3059
3060 if (distance < AVERAGING_DISTANCE_LIMIT) {
3061 // Increment end index in preparation for recording new historical data.
3062 end += 1;
3063 if (end > AVERAGING_HISTORY_SIZE) {
3064 end = 0;
3065 }
3066
3067 // If the end index has looped back to the start index then we have filled
3068 // the historical trace up to the desired size so we drop the historical
3069 // data at the start of the trace.
3070 if (end == start) {
3071 start += 1;
3072 if (start > AVERAGING_HISTORY_SIZE) {
3073 start = 0;
3074 }
3075 }
3076
3077 // Add the raw data to the historical trace.
3078 mAveragingTouchFilter.historyStart[id] = start;
3079 mAveragingTouchFilter.historyEnd[id] = end;
3080 mAveragingTouchFilter.historyData[end].pointers[id].x = x;
3081 mAveragingTouchFilter.historyData[end].pointers[id].y = y;
3082 mAveragingTouchFilter.historyData[end].pointers[id].pressure = pressure;
3083
3084 // Average over all historical positions in the trace by total pressure.
3085 int32_t averagedX = 0;
3086 int32_t averagedY = 0;
3087 int32_t totalPressure = 0;
3088 for (;;) {
3089 int32_t historicalX = mAveragingTouchFilter.historyData[start].pointers[id].x;
3090 int32_t historicalY = mAveragingTouchFilter.historyData[start].pointers[id].y;
3091 int32_t historicalPressure = mAveragingTouchFilter.historyData[start]
3092 .pointers[id].pressure;
3093
3094 averagedX += historicalX * historicalPressure;
3095 averagedY += historicalY * historicalPressure;
3096 totalPressure += historicalPressure;
3097
3098 if (start == end) {
3099 break;
3100 }
3101
3102 start += 1;
3103 if (start > AVERAGING_HISTORY_SIZE) {
3104 start = 0;
3105 }
3106 }
3107
Jeff Brown38a7fab2010-08-30 03:02:23 -07003108 if (totalPressure != 0) {
3109 averagedX /= totalPressure;
3110 averagedY /= totalPressure;
Jeff Browne57e8952010-07-23 21:28:06 -07003111
3112#if DEBUG_HACKS
Jeff Brown38a7fab2010-08-30 03:02:23 -07003113 LOGD("AveragingTouchFilter: Pointer id %d - "
3114 "totalPressure=%d, averagedX=%d, averagedY=%d", id, totalPressure,
3115 averagedX, averagedY);
Jeff Browne57e8952010-07-23 21:28:06 -07003116#endif
3117
Jeff Brown38a7fab2010-08-30 03:02:23 -07003118 mCurrentTouch.pointers[currentIndex].x = averagedX;
3119 mCurrentTouch.pointers[currentIndex].y = averagedY;
3120 }
Jeff Browne57e8952010-07-23 21:28:06 -07003121 } else {
3122#if DEBUG_HACKS
3123 LOGD("AveragingTouchFilter: Pointer id %d - Exceeded max distance", id);
3124#endif
3125 }
3126 } else {
3127#if DEBUG_HACKS
3128 LOGD("AveragingTouchFilter: Pointer id %d - Pointer went up", id);
3129#endif
3130 }
3131
3132 // Reset pointer history.
3133 mAveragingTouchFilter.historyStart[id] = 0;
3134 mAveragingTouchFilter.historyEnd[id] = 0;
3135 mAveragingTouchFilter.historyData[0].pointers[id].x = x;
3136 mAveragingTouchFilter.historyData[0].pointers[id].y = y;
3137 mAveragingTouchFilter.historyData[0].pointers[id].pressure = pressure;
3138 }
3139}
3140
3141int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownb51719b2010-07-29 18:18:33 -07003142 { // acquire lock
3143 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07003144
Jeff Brownb51719b2010-07-29 18:18:33 -07003145 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07003146 return AKEY_STATE_VIRTUAL;
3147 }
3148
Jeff Brownb51719b2010-07-29 18:18:33 -07003149 size_t numVirtualKeys = mLocked.virtualKeys.size();
3150 for (size_t i = 0; i < numVirtualKeys; i++) {
3151 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07003152 if (virtualKey.keyCode == keyCode) {
3153 return AKEY_STATE_UP;
3154 }
3155 }
Jeff Brownb51719b2010-07-29 18:18:33 -07003156 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07003157
3158 return AKEY_STATE_UNKNOWN;
3159}
3160
3161int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownb51719b2010-07-29 18:18:33 -07003162 { // acquire lock
3163 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07003164
Jeff Brownb51719b2010-07-29 18:18:33 -07003165 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07003166 return AKEY_STATE_VIRTUAL;
3167 }
3168
Jeff Brownb51719b2010-07-29 18:18:33 -07003169 size_t numVirtualKeys = mLocked.virtualKeys.size();
3170 for (size_t i = 0; i < numVirtualKeys; i++) {
3171 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07003172 if (virtualKey.scanCode == scanCode) {
3173 return AKEY_STATE_UP;
3174 }
3175 }
Jeff Brownb51719b2010-07-29 18:18:33 -07003176 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07003177
3178 return AKEY_STATE_UNKNOWN;
3179}
3180
3181bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
3182 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownb51719b2010-07-29 18:18:33 -07003183 { // acquire lock
3184 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07003185
Jeff Brownb51719b2010-07-29 18:18:33 -07003186 size_t numVirtualKeys = mLocked.virtualKeys.size();
3187 for (size_t i = 0; i < numVirtualKeys; i++) {
3188 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07003189
3190 for (size_t i = 0; i < numCodes; i++) {
3191 if (virtualKey.keyCode == keyCodes[i]) {
3192 outFlags[i] = 1;
3193 }
3194 }
3195 }
Jeff Brownb51719b2010-07-29 18:18:33 -07003196 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07003197
3198 return true;
3199}
3200
3201
3202// --- SingleTouchInputMapper ---
3203
Jeff Brown66888372010-11-29 17:37:49 -08003204SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
3205 TouchInputMapper(device) {
Jeff Browne57e8952010-07-23 21:28:06 -07003206 initialize();
3207}
3208
3209SingleTouchInputMapper::~SingleTouchInputMapper() {
3210}
3211
3212void SingleTouchInputMapper::initialize() {
3213 mAccumulator.clear();
3214
3215 mDown = false;
3216 mX = 0;
3217 mY = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003218 mPressure = 0; // default to 0 for devices that don't report pressure
3219 mToolWidth = 0; // default to 0 for devices that don't report tool width
Jeff Browne57e8952010-07-23 21:28:06 -07003220}
3221
3222void SingleTouchInputMapper::reset() {
3223 TouchInputMapper::reset();
3224
Jeff Browne57e8952010-07-23 21:28:06 -07003225 initialize();
3226 }
3227
3228void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
3229 switch (rawEvent->type) {
3230 case EV_KEY:
3231 switch (rawEvent->scanCode) {
3232 case BTN_TOUCH:
3233 mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH;
3234 mAccumulator.btnTouch = rawEvent->value != 0;
Jeff Brownd64c8552010-08-17 20:38:35 -07003235 // Don't sync immediately. Wait until the next SYN_REPORT since we might
3236 // not have received valid position information yet. This logic assumes that
3237 // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet.
Jeff Browne57e8952010-07-23 21:28:06 -07003238 break;
3239 }
3240 break;
3241
3242 case EV_ABS:
3243 switch (rawEvent->scanCode) {
3244 case ABS_X:
3245 mAccumulator.fields |= Accumulator::FIELD_ABS_X;
3246 mAccumulator.absX = rawEvent->value;
3247 break;
3248 case ABS_Y:
3249 mAccumulator.fields |= Accumulator::FIELD_ABS_Y;
3250 mAccumulator.absY = rawEvent->value;
3251 break;
3252 case ABS_PRESSURE:
3253 mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE;
3254 mAccumulator.absPressure = rawEvent->value;
3255 break;
3256 case ABS_TOOL_WIDTH:
3257 mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH;
3258 mAccumulator.absToolWidth = rawEvent->value;
3259 break;
3260 }
3261 break;
3262
3263 case EV_SYN:
3264 switch (rawEvent->scanCode) {
3265 case SYN_REPORT:
Jeff Brownd64c8552010-08-17 20:38:35 -07003266 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07003267 break;
3268 }
3269 break;
3270 }
3271}
3272
3273void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Browne57e8952010-07-23 21:28:06 -07003274 uint32_t fields = mAccumulator.fields;
Jeff Brownd64c8552010-08-17 20:38:35 -07003275 if (fields == 0) {
3276 return; // no new state changes, so nothing to do
3277 }
Jeff Browne57e8952010-07-23 21:28:06 -07003278
3279 if (fields & Accumulator::FIELD_BTN_TOUCH) {
3280 mDown = mAccumulator.btnTouch;
3281 }
3282
3283 if (fields & Accumulator::FIELD_ABS_X) {
3284 mX = mAccumulator.absX;
3285 }
3286
3287 if (fields & Accumulator::FIELD_ABS_Y) {
3288 mY = mAccumulator.absY;
3289 }
3290
3291 if (fields & Accumulator::FIELD_ABS_PRESSURE) {
3292 mPressure = mAccumulator.absPressure;
3293 }
3294
3295 if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003296 mToolWidth = mAccumulator.absToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -07003297 }
3298
3299 mCurrentTouch.clear();
3300
3301 if (mDown) {
3302 mCurrentTouch.pointerCount = 1;
3303 mCurrentTouch.pointers[0].id = 0;
3304 mCurrentTouch.pointers[0].x = mX;
3305 mCurrentTouch.pointers[0].y = mY;
3306 mCurrentTouch.pointers[0].pressure = mPressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003307 mCurrentTouch.pointers[0].touchMajor = 0;
3308 mCurrentTouch.pointers[0].touchMinor = 0;
3309 mCurrentTouch.pointers[0].toolMajor = mToolWidth;
3310 mCurrentTouch.pointers[0].toolMinor = mToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -07003311 mCurrentTouch.pointers[0].orientation = 0;
3312 mCurrentTouch.idToIndex[0] = 0;
3313 mCurrentTouch.idBits.markBit(0);
3314 }
3315
3316 syncTouch(when, true);
Jeff Brownd64c8552010-08-17 20:38:35 -07003317
3318 mAccumulator.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07003319}
3320
Jeff Brown38a7fab2010-08-30 03:02:23 -07003321void SingleTouchInputMapper::configureRawAxes() {
3322 TouchInputMapper::configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -07003323
Jeff Brown38a7fab2010-08-30 03:02:23 -07003324 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x);
3325 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y);
3326 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure);
3327 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor);
Jeff Browne57e8952010-07-23 21:28:06 -07003328}
3329
3330
3331// --- MultiTouchInputMapper ---
3332
Jeff Brown66888372010-11-29 17:37:49 -08003333MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
3334 TouchInputMapper(device) {
Jeff Browne57e8952010-07-23 21:28:06 -07003335 initialize();
3336}
3337
3338MultiTouchInputMapper::~MultiTouchInputMapper() {
3339}
3340
3341void MultiTouchInputMapper::initialize() {
3342 mAccumulator.clear();
3343}
3344
3345void MultiTouchInputMapper::reset() {
3346 TouchInputMapper::reset();
3347
Jeff Browne57e8952010-07-23 21:28:06 -07003348 initialize();
3349}
3350
3351void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
3352 switch (rawEvent->type) {
3353 case EV_ABS: {
3354 uint32_t pointerIndex = mAccumulator.pointerCount;
3355 Accumulator::Pointer* pointer = & mAccumulator.pointers[pointerIndex];
3356
3357 switch (rawEvent->scanCode) {
3358 case ABS_MT_POSITION_X:
3359 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_X;
3360 pointer->absMTPositionX = rawEvent->value;
3361 break;
3362 case ABS_MT_POSITION_Y:
3363 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y;
3364 pointer->absMTPositionY = rawEvent->value;
3365 break;
3366 case ABS_MT_TOUCH_MAJOR:
3367 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR;
3368 pointer->absMTTouchMajor = rawEvent->value;
3369 break;
3370 case ABS_MT_TOUCH_MINOR:
3371 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR;
3372 pointer->absMTTouchMinor = rawEvent->value;
3373 break;
3374 case ABS_MT_WIDTH_MAJOR:
3375 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR;
3376 pointer->absMTWidthMajor = rawEvent->value;
3377 break;
3378 case ABS_MT_WIDTH_MINOR:
3379 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR;
3380 pointer->absMTWidthMinor = rawEvent->value;
3381 break;
3382 case ABS_MT_ORIENTATION:
3383 pointer->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION;
3384 pointer->absMTOrientation = rawEvent->value;
3385 break;
3386 case ABS_MT_TRACKING_ID:
3387 pointer->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID;
3388 pointer->absMTTrackingId = rawEvent->value;
3389 break;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003390 case ABS_MT_PRESSURE:
3391 pointer->fields |= Accumulator::FIELD_ABS_MT_PRESSURE;
3392 pointer->absMTPressure = rawEvent->value;
3393 break;
Jeff Browne57e8952010-07-23 21:28:06 -07003394 }
3395 break;
3396 }
3397
3398 case EV_SYN:
3399 switch (rawEvent->scanCode) {
3400 case SYN_MT_REPORT: {
3401 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
3402 uint32_t pointerIndex = mAccumulator.pointerCount;
3403
3404 if (mAccumulator.pointers[pointerIndex].fields) {
3405 if (pointerIndex == MAX_POINTERS) {
3406 LOGW("MultiTouch device driver returned more than maximum of %d pointers.",
3407 MAX_POINTERS);
3408 } else {
3409 pointerIndex += 1;
3410 mAccumulator.pointerCount = pointerIndex;
3411 }
3412 }
3413
3414 mAccumulator.pointers[pointerIndex].clear();
3415 break;
3416 }
3417
3418 case SYN_REPORT:
Jeff Brownd64c8552010-08-17 20:38:35 -07003419 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07003420 break;
3421 }
3422 break;
3423 }
3424}
3425
3426void MultiTouchInputMapper::sync(nsecs_t when) {
3427 static const uint32_t REQUIRED_FIELDS =
Jeff Brown38a7fab2010-08-30 03:02:23 -07003428 Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y;
Jeff Browne839a582010-04-22 18:58:52 -07003429
Jeff Browne57e8952010-07-23 21:28:06 -07003430 uint32_t inCount = mAccumulator.pointerCount;
3431 uint32_t outCount = 0;
3432 bool havePointerIds = true;
Jeff Browne839a582010-04-22 18:58:52 -07003433
Jeff Browne57e8952010-07-23 21:28:06 -07003434 mCurrentTouch.clear();
Jeff Browne839a582010-04-22 18:58:52 -07003435
Jeff Browne57e8952010-07-23 21:28:06 -07003436 for (uint32_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003437 const Accumulator::Pointer& inPointer = mAccumulator.pointers[inIndex];
3438 uint32_t fields = inPointer.fields;
Jeff Browne839a582010-04-22 18:58:52 -07003439
Jeff Browne57e8952010-07-23 21:28:06 -07003440 if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003441 // Some drivers send empty MT sync packets without X / Y to indicate a pointer up.
3442 // Drop this finger.
Jeff Browne839a582010-04-22 18:58:52 -07003443 continue;
3444 }
3445
Jeff Brownd64c8552010-08-17 20:38:35 -07003446 PointerData& outPointer = mCurrentTouch.pointers[outCount];
3447 outPointer.x = inPointer.absMTPositionX;
3448 outPointer.y = inPointer.absMTPositionY;
Jeff Browne839a582010-04-22 18:58:52 -07003449
Jeff Brown38a7fab2010-08-30 03:02:23 -07003450 if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) {
3451 if (inPointer.absMTPressure <= 0) {
Jeff Brown3c3cc622010-10-20 15:33:38 -07003452 // Some devices send sync packets with X / Y but with a 0 pressure to indicate
3453 // a pointer going up. Drop this finger.
Jeff Brownd64c8552010-08-17 20:38:35 -07003454 continue;
3455 }
Jeff Brown38a7fab2010-08-30 03:02:23 -07003456 outPointer.pressure = inPointer.absMTPressure;
3457 } else {
3458 // Default pressure to 0 if absent.
3459 outPointer.pressure = 0;
3460 }
3461
3462 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) {
3463 if (inPointer.absMTTouchMajor <= 0) {
3464 // Some devices send sync packets with X / Y but with a 0 touch major to indicate
3465 // a pointer going up. Drop this finger.
3466 continue;
3467 }
Jeff Brownd64c8552010-08-17 20:38:35 -07003468 outPointer.touchMajor = inPointer.absMTTouchMajor;
3469 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003470 // Default touch area to 0 if absent.
Jeff Brownd64c8552010-08-17 20:38:35 -07003471 outPointer.touchMajor = 0;
3472 }
Jeff Browne839a582010-04-22 18:58:52 -07003473
Jeff Brownd64c8552010-08-17 20:38:35 -07003474 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) {
3475 outPointer.touchMinor = inPointer.absMTTouchMinor;
3476 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003477 // Assume touch area is circular.
Jeff Brownd64c8552010-08-17 20:38:35 -07003478 outPointer.touchMinor = outPointer.touchMajor;
3479 }
Jeff Browne839a582010-04-22 18:58:52 -07003480
Jeff Brownd64c8552010-08-17 20:38:35 -07003481 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) {
3482 outPointer.toolMajor = inPointer.absMTWidthMajor;
3483 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003484 // Default tool area to 0 if absent.
3485 outPointer.toolMajor = 0;
Jeff Brownd64c8552010-08-17 20:38:35 -07003486 }
Jeff Browne839a582010-04-22 18:58:52 -07003487
Jeff Brownd64c8552010-08-17 20:38:35 -07003488 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) {
3489 outPointer.toolMinor = inPointer.absMTWidthMinor;
3490 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003491 // Assume tool area is circular.
Jeff Brownd64c8552010-08-17 20:38:35 -07003492 outPointer.toolMinor = outPointer.toolMajor;
3493 }
3494
3495 if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) {
3496 outPointer.orientation = inPointer.absMTOrientation;
3497 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003498 // Default orientation to vertical if absent.
Jeff Brownd64c8552010-08-17 20:38:35 -07003499 outPointer.orientation = 0;
3500 }
3501
Jeff Brown38a7fab2010-08-30 03:02:23 -07003502 // Assign pointer id using tracking id if available.
Jeff Browne57e8952010-07-23 21:28:06 -07003503 if (havePointerIds) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003504 if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) {
3505 uint32_t id = uint32_t(inPointer.absMTTrackingId);
Jeff Browne839a582010-04-22 18:58:52 -07003506
Jeff Browne57e8952010-07-23 21:28:06 -07003507 if (id > MAX_POINTER_ID) {
3508#if DEBUG_POINTERS
3509 LOGD("Pointers: Ignoring driver provided pointer id %d because "
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07003510 "it is larger than max supported id %d",
Jeff Browne57e8952010-07-23 21:28:06 -07003511 id, MAX_POINTER_ID);
3512#endif
3513 havePointerIds = false;
3514 }
3515 else {
Jeff Brownd64c8552010-08-17 20:38:35 -07003516 outPointer.id = id;
Jeff Browne57e8952010-07-23 21:28:06 -07003517 mCurrentTouch.idToIndex[id] = outCount;
3518 mCurrentTouch.idBits.markBit(id);
3519 }
3520 } else {
3521 havePointerIds = false;
Jeff Browne839a582010-04-22 18:58:52 -07003522 }
3523 }
Jeff Browne839a582010-04-22 18:58:52 -07003524
Jeff Browne57e8952010-07-23 21:28:06 -07003525 outCount += 1;
Jeff Browne839a582010-04-22 18:58:52 -07003526 }
3527
Jeff Browne57e8952010-07-23 21:28:06 -07003528 mCurrentTouch.pointerCount = outCount;
Jeff Browne839a582010-04-22 18:58:52 -07003529
Jeff Browne57e8952010-07-23 21:28:06 -07003530 syncTouch(when, havePointerIds);
Jeff Brownd64c8552010-08-17 20:38:35 -07003531
3532 mAccumulator.clear();
Jeff Browne839a582010-04-22 18:58:52 -07003533}
3534
Jeff Brown38a7fab2010-08-30 03:02:23 -07003535void MultiTouchInputMapper::configureRawAxes() {
3536 TouchInputMapper::configureRawAxes();
Jeff Browne839a582010-04-22 18:58:52 -07003537
Jeff Brown38a7fab2010-08-30 03:02:23 -07003538 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, & mRawAxes.x);
3539 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, & mRawAxes.y);
3540 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, & mRawAxes.touchMajor);
3541 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, & mRawAxes.touchMinor);
3542 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, & mRawAxes.toolMajor);
3543 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, & mRawAxes.toolMinor);
3544 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, & mRawAxes.orientation);
3545 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, & mRawAxes.pressure);
Jeff Brown54bc2812010-06-15 01:31:58 -07003546}
3547
Jeff Browne839a582010-04-22 18:58:52 -07003548
3549} // namespace android