blob: 9cc96adf396b876b4fd9c6e4482e4817532026f4 [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 Brown6a817e22010-09-12 17:55:08 -0700748
749 initializeLedStateLocked(mLocked.capsLockLedState, LED_CAPSL);
750 initializeLedStateLocked(mLocked.numLockLedState, LED_NUML);
751 initializeLedStateLocked(mLocked.scrollLockLedState, LED_SCROLLL);
752
753 updateLedStateLocked(true);
754}
755
756void KeyboardInputMapper::initializeLedStateLocked(LockedState::LedState& ledState, int32_t led) {
757 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
758 ledState.on = false;
Jeff Browne57e8952010-07-23 21:28:06 -0700759}
760
761uint32_t KeyboardInputMapper::getSources() {
762 return mSources;
763}
764
765void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
766 InputMapper::populateDeviceInfo(info);
767
768 info->setKeyboardType(mKeyboardType);
769}
770
Jeff Brown26c94ff2010-09-30 14:33:04 -0700771void KeyboardInputMapper::dump(String8& dump) {
772 { // acquire lock
773 AutoMutex _l(mLock);
774 dump.append(INDENT2 "Keyboard Input Mapper:\n");
Jeff Brown66888372010-11-29 17:37:49 -0800775 dumpParameters(dump);
Jeff Brown26c94ff2010-09-30 14:33:04 -0700776 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
777 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mLocked.keyDowns.size());
778 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mLocked.metaState);
779 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
780 } // release lock
781}
782
Jeff Brown66888372010-11-29 17:37:49 -0800783
784void KeyboardInputMapper::configure() {
785 InputMapper::configure();
786
787 // Configure basic parameters.
788 configureParameters();
789}
790
791void KeyboardInputMapper::configureParameters() {
792 mParameters.orientationAware = false;
793 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
794 mParameters.orientationAware);
795
796 mParameters.associatedDisplayId = mParameters.orientationAware ? 0 : -1;
797}
798
799void KeyboardInputMapper::dumpParameters(String8& dump) {
800 dump.append(INDENT3 "Parameters:\n");
801 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
802 mParameters.associatedDisplayId);
803 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
804 toString(mParameters.orientationAware));
805}
806
Jeff Browne57e8952010-07-23 21:28:06 -0700807void KeyboardInputMapper::reset() {
Jeff Brownb51719b2010-07-29 18:18:33 -0700808 for (;;) {
809 int32_t keyCode, scanCode;
810 { // acquire lock
811 AutoMutex _l(mLock);
812
813 // Synthesize key up event on reset if keys are currently down.
814 if (mLocked.keyDowns.isEmpty()) {
815 initializeLocked();
816 break; // done
817 }
818
819 const KeyDown& keyDown = mLocked.keyDowns.top();
820 keyCode = keyDown.keyCode;
821 scanCode = keyDown.scanCode;
822 } // release lock
823
Jeff Browne57e8952010-07-23 21:28:06 -0700824 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownb51719b2010-07-29 18:18:33 -0700825 processKey(when, false, keyCode, scanCode, 0);
Jeff Browne57e8952010-07-23 21:28:06 -0700826 }
827
828 InputMapper::reset();
Jeff Browne57e8952010-07-23 21:28:06 -0700829 getContext()->updateGlobalMetaState();
830}
831
832void KeyboardInputMapper::process(const RawEvent* rawEvent) {
833 switch (rawEvent->type) {
834 case EV_KEY: {
835 int32_t scanCode = rawEvent->scanCode;
836 if (isKeyboardOrGamepadKey(scanCode)) {
837 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
838 rawEvent->flags);
839 }
840 break;
841 }
842 }
843}
844
845bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
846 return scanCode < BTN_MOUSE
847 || scanCode >= KEY_OK
848 || (scanCode >= BTN_GAMEPAD && scanCode < BTN_DIGI);
849}
850
Jeff Brownb51719b2010-07-29 18:18:33 -0700851void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
852 int32_t scanCode, uint32_t policyFlags) {
853 int32_t newMetaState;
854 nsecs_t downTime;
855 bool metaStateChanged = false;
856
857 { // acquire lock
858 AutoMutex _l(mLock);
859
860 if (down) {
861 // Rotate key codes according to orientation if needed.
862 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown66888372010-11-29 17:37:49 -0800863 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brownb51719b2010-07-29 18:18:33 -0700864 int32_t orientation;
Jeff Brown66888372010-11-29 17:37:49 -0800865 if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
866 NULL, NULL, & orientation)) {
Jeff Brownd4ecee92010-10-29 22:19:53 -0700867 orientation = InputReaderPolicyInterface::ROTATION_0;
Jeff Brownb51719b2010-07-29 18:18:33 -0700868 }
869
870 keyCode = rotateKeyCode(keyCode, orientation);
Jeff Browne57e8952010-07-23 21:28:06 -0700871 }
872
Jeff Brownb51719b2010-07-29 18:18:33 -0700873 // Add key down.
874 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
875 if (keyDownIndex >= 0) {
876 // key repeat, be sure to use same keycode as before in case of rotation
Jeff Browna3477c82010-11-10 16:03:06 -0800877 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brownb51719b2010-07-29 18:18:33 -0700878 } else {
879 // key down
880 mLocked.keyDowns.push();
881 KeyDown& keyDown = mLocked.keyDowns.editTop();
882 keyDown.keyCode = keyCode;
883 keyDown.scanCode = scanCode;
884 }
885
886 mLocked.downTime = when;
887 } else {
888 // Remove key down.
889 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
890 if (keyDownIndex >= 0) {
891 // key up, be sure to use same keycode as before in case of rotation
Jeff Browna3477c82010-11-10 16:03:06 -0800892 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brownb51719b2010-07-29 18:18:33 -0700893 mLocked.keyDowns.removeAt(size_t(keyDownIndex));
894 } else {
895 // key was not actually down
896 LOGI("Dropping key up from device %s because the key was not down. "
897 "keyCode=%d, scanCode=%d",
898 getDeviceName().string(), keyCode, scanCode);
899 return;
900 }
Jeff Browne57e8952010-07-23 21:28:06 -0700901 }
902
Jeff Brownb51719b2010-07-29 18:18:33 -0700903 int32_t oldMetaState = mLocked.metaState;
904 newMetaState = updateMetaState(keyCode, down, oldMetaState);
905 if (oldMetaState != newMetaState) {
906 mLocked.metaState = newMetaState;
907 metaStateChanged = true;
Jeff Brown6a817e22010-09-12 17:55:08 -0700908 updateLedStateLocked(false);
Jeff Browne57e8952010-07-23 21:28:06 -0700909 }
Jeff Brown8575a872010-06-30 16:10:35 -0700910
Jeff Brownb51719b2010-07-29 18:18:33 -0700911 downTime = mLocked.downTime;
912 } // release lock
913
914 if (metaStateChanged) {
Jeff Browne57e8952010-07-23 21:28:06 -0700915 getContext()->updateGlobalMetaState();
Jeff Browne839a582010-04-22 18:58:52 -0700916 }
917
Jeff Brown6a817e22010-09-12 17:55:08 -0700918 if (policyFlags & POLICY_FLAG_FUNCTION) {
919 newMetaState |= AMETA_FUNCTION_ON;
920 }
Jeff Browne57e8952010-07-23 21:28:06 -0700921 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
Jeff Brown90f0cee2010-10-08 22:31:17 -0700922 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
923 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Browne839a582010-04-22 18:58:52 -0700924}
925
Jeff Brownb51719b2010-07-29 18:18:33 -0700926ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) {
927 size_t n = mLocked.keyDowns.size();
Jeff Browne57e8952010-07-23 21:28:06 -0700928 for (size_t i = 0; i < n; i++) {
Jeff Brownb51719b2010-07-29 18:18:33 -0700929 if (mLocked.keyDowns[i].scanCode == scanCode) {
Jeff Browne57e8952010-07-23 21:28:06 -0700930 return i;
931 }
932 }
933 return -1;
Jeff Browne839a582010-04-22 18:58:52 -0700934}
935
Jeff Browne57e8952010-07-23 21:28:06 -0700936int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
937 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
938}
Jeff Browne839a582010-04-22 18:58:52 -0700939
Jeff Browne57e8952010-07-23 21:28:06 -0700940int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
941 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
942}
Jeff Browne839a582010-04-22 18:58:52 -0700943
Jeff Browne57e8952010-07-23 21:28:06 -0700944bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
945 const int32_t* keyCodes, uint8_t* outFlags) {
946 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
947}
948
949int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownb51719b2010-07-29 18:18:33 -0700950 { // acquire lock
951 AutoMutex _l(mLock);
952 return mLocked.metaState;
953 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -0700954}
955
Jeff Brown6a817e22010-09-12 17:55:08 -0700956void KeyboardInputMapper::updateLedStateLocked(bool reset) {
957 updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL,
Jeff Brownd4ecee92010-10-29 22:19:53 -0700958 AMETA_CAPS_LOCK_ON, reset);
Jeff Brown6a817e22010-09-12 17:55:08 -0700959 updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML,
Jeff Brownd4ecee92010-10-29 22:19:53 -0700960 AMETA_NUM_LOCK_ON, reset);
Jeff Brown6a817e22010-09-12 17:55:08 -0700961 updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL,
Jeff Brownd4ecee92010-10-29 22:19:53 -0700962 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown6a817e22010-09-12 17:55:08 -0700963}
964
965void KeyboardInputMapper::updateLedStateForModifierLocked(LockedState::LedState& ledState,
966 int32_t led, int32_t modifier, bool reset) {
967 if (ledState.avail) {
968 bool desiredState = (mLocked.metaState & modifier) != 0;
969 if (ledState.on != desiredState) {
970 getEventHub()->setLedState(getDeviceId(), led, desiredState);
971 ledState.on = desiredState;
972 }
973 }
974}
975
Jeff Browne57e8952010-07-23 21:28:06 -0700976
977// --- TrackballInputMapper ---
978
Jeff Brown66888372010-11-29 17:37:49 -0800979TrackballInputMapper::TrackballInputMapper(InputDevice* device) :
980 InputMapper(device) {
Jeff Browne57e8952010-07-23 21:28:06 -0700981 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
982 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
983 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
984 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
985
Jeff Brownb51719b2010-07-29 18:18:33 -0700986 initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -0700987}
988
989TrackballInputMapper::~TrackballInputMapper() {
990}
991
992uint32_t TrackballInputMapper::getSources() {
993 return AINPUT_SOURCE_TRACKBALL;
994}
995
996void TrackballInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
997 InputMapper::populateDeviceInfo(info);
998
999 info->addMotionRange(AINPUT_MOTION_RANGE_X, -1.0f, 1.0f, 0.0f, mXScale);
1000 info->addMotionRange(AINPUT_MOTION_RANGE_Y, -1.0f, 1.0f, 0.0f, mYScale);
1001}
1002
Jeff Brown26c94ff2010-09-30 14:33:04 -07001003void TrackballInputMapper::dump(String8& dump) {
1004 { // acquire lock
1005 AutoMutex _l(mLock);
1006 dump.append(INDENT2 "Trackball Input Mapper:\n");
Jeff Brown66888372010-11-29 17:37:49 -08001007 dumpParameters(dump);
Jeff Brown26c94ff2010-09-30 14:33:04 -07001008 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1009 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
1010 dump.appendFormat(INDENT3 "Down: %s\n", toString(mLocked.down));
1011 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1012 } // release lock
1013}
1014
Jeff Brown66888372010-11-29 17:37:49 -08001015void TrackballInputMapper::configure() {
1016 InputMapper::configure();
1017
1018 // Configure basic parameters.
1019 configureParameters();
1020}
1021
1022void TrackballInputMapper::configureParameters() {
1023 mParameters.orientationAware = false;
1024 getDevice()->getConfiguration().tryGetProperty(String8("trackball.orientationAware"),
1025 mParameters.orientationAware);
1026
1027 mParameters.associatedDisplayId = mParameters.orientationAware ? 0 : -1;
1028}
1029
1030void TrackballInputMapper::dumpParameters(String8& dump) {
1031 dump.append(INDENT3 "Parameters:\n");
1032 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1033 mParameters.associatedDisplayId);
1034 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1035 toString(mParameters.orientationAware));
1036}
1037
Jeff Brownb51719b2010-07-29 18:18:33 -07001038void TrackballInputMapper::initializeLocked() {
Jeff Browne57e8952010-07-23 21:28:06 -07001039 mAccumulator.clear();
1040
Jeff Brownb51719b2010-07-29 18:18:33 -07001041 mLocked.down = false;
1042 mLocked.downTime = 0;
Jeff Browne57e8952010-07-23 21:28:06 -07001043}
1044
1045void TrackballInputMapper::reset() {
Jeff Brownb51719b2010-07-29 18:18:33 -07001046 for (;;) {
1047 { // acquire lock
1048 AutoMutex _l(mLock);
1049
1050 if (! mLocked.down) {
1051 initializeLocked();
1052 break; // done
1053 }
1054 } // release lock
1055
1056 // Synthesize trackball button up event on reset.
Jeff Browne57e8952010-07-23 21:28:06 -07001057 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownb51719b2010-07-29 18:18:33 -07001058 mAccumulator.fields = Accumulator::FIELD_BTN_MOUSE;
Jeff Browne57e8952010-07-23 21:28:06 -07001059 mAccumulator.btnMouse = false;
1060 sync(when);
Jeff Browne839a582010-04-22 18:58:52 -07001061 }
1062
Jeff Browne57e8952010-07-23 21:28:06 -07001063 InputMapper::reset();
Jeff Browne57e8952010-07-23 21:28:06 -07001064}
Jeff Browne839a582010-04-22 18:58:52 -07001065
Jeff Browne57e8952010-07-23 21:28:06 -07001066void TrackballInputMapper::process(const RawEvent* rawEvent) {
1067 switch (rawEvent->type) {
1068 case EV_KEY:
1069 switch (rawEvent->scanCode) {
1070 case BTN_MOUSE:
1071 mAccumulator.fields |= Accumulator::FIELD_BTN_MOUSE;
1072 mAccumulator.btnMouse = rawEvent->value != 0;
Jeff Brownd64c8552010-08-17 20:38:35 -07001073 // Sync now since BTN_MOUSE is not necessarily followed by SYN_REPORT and
1074 // we need to ensure that we report the up/down promptly.
Jeff Browne57e8952010-07-23 21:28:06 -07001075 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07001076 break;
Jeff Browne839a582010-04-22 18:58:52 -07001077 }
Jeff Browne57e8952010-07-23 21:28:06 -07001078 break;
Jeff Browne839a582010-04-22 18:58:52 -07001079
Jeff Browne57e8952010-07-23 21:28:06 -07001080 case EV_REL:
1081 switch (rawEvent->scanCode) {
1082 case REL_X:
1083 mAccumulator.fields |= Accumulator::FIELD_REL_X;
1084 mAccumulator.relX = rawEvent->value;
1085 break;
1086 case REL_Y:
1087 mAccumulator.fields |= Accumulator::FIELD_REL_Y;
1088 mAccumulator.relY = rawEvent->value;
1089 break;
Jeff Browne839a582010-04-22 18:58:52 -07001090 }
Jeff Browne57e8952010-07-23 21:28:06 -07001091 break;
Jeff Browne839a582010-04-22 18:58:52 -07001092
Jeff Browne57e8952010-07-23 21:28:06 -07001093 case EV_SYN:
1094 switch (rawEvent->scanCode) {
1095 case SYN_REPORT:
Jeff Brownd64c8552010-08-17 20:38:35 -07001096 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07001097 break;
Jeff Browne839a582010-04-22 18:58:52 -07001098 }
Jeff Browne57e8952010-07-23 21:28:06 -07001099 break;
Jeff Browne839a582010-04-22 18:58:52 -07001100 }
Jeff Browne839a582010-04-22 18:58:52 -07001101}
1102
Jeff Browne57e8952010-07-23 21:28:06 -07001103void TrackballInputMapper::sync(nsecs_t when) {
Jeff Brownd64c8552010-08-17 20:38:35 -07001104 uint32_t fields = mAccumulator.fields;
1105 if (fields == 0) {
1106 return; // no new state changes, so nothing to do
1107 }
1108
Jeff Brownb51719b2010-07-29 18:18:33 -07001109 int motionEventAction;
1110 PointerCoords pointerCoords;
1111 nsecs_t downTime;
1112 { // acquire lock
1113 AutoMutex _l(mLock);
Jeff Browne839a582010-04-22 18:58:52 -07001114
Jeff Brownb51719b2010-07-29 18:18:33 -07001115 bool downChanged = fields & Accumulator::FIELD_BTN_MOUSE;
1116
1117 if (downChanged) {
1118 if (mAccumulator.btnMouse) {
1119 mLocked.down = true;
1120 mLocked.downTime = when;
1121 } else {
1122 mLocked.down = false;
1123 }
Jeff Browne57e8952010-07-23 21:28:06 -07001124 }
Jeff Browne839a582010-04-22 18:58:52 -07001125
Jeff Brownb51719b2010-07-29 18:18:33 -07001126 downTime = mLocked.downTime;
1127 float x = fields & Accumulator::FIELD_REL_X ? mAccumulator.relX * mXScale : 0.0f;
1128 float y = fields & Accumulator::FIELD_REL_Y ? mAccumulator.relY * mYScale : 0.0f;
Jeff Browne839a582010-04-22 18:58:52 -07001129
Jeff Brownb51719b2010-07-29 18:18:33 -07001130 if (downChanged) {
1131 motionEventAction = mLocked.down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
Jeff Browne57e8952010-07-23 21:28:06 -07001132 } else {
Jeff Brownb51719b2010-07-29 18:18:33 -07001133 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browne57e8952010-07-23 21:28:06 -07001134 }
Jeff Browne839a582010-04-22 18:58:52 -07001135
Jeff Brownb51719b2010-07-29 18:18:33 -07001136 pointerCoords.x = x;
1137 pointerCoords.y = y;
1138 pointerCoords.pressure = mLocked.down ? 1.0f : 0.0f;
1139 pointerCoords.size = 0;
1140 pointerCoords.touchMajor = 0;
1141 pointerCoords.touchMinor = 0;
1142 pointerCoords.toolMajor = 0;
1143 pointerCoords.toolMinor = 0;
1144 pointerCoords.orientation = 0;
Jeff Browne839a582010-04-22 18:58:52 -07001145
Jeff Brown66888372010-11-29 17:37:49 -08001146 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
1147 && (x != 0.0f || y != 0.0f)) {
Jeff Brownb51719b2010-07-29 18:18:33 -07001148 // Rotate motion based on display orientation if needed.
1149 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1150 int32_t orientation;
Jeff Brown66888372010-11-29 17:37:49 -08001151 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1152 NULL, NULL, & orientation)) {
Jeff Brownd4ecee92010-10-29 22:19:53 -07001153 orientation = InputReaderPolicyInterface::ROTATION_0;
Jeff Brownb51719b2010-07-29 18:18:33 -07001154 }
1155
1156 float temp;
1157 switch (orientation) {
1158 case InputReaderPolicyInterface::ROTATION_90:
1159 temp = pointerCoords.x;
1160 pointerCoords.x = pointerCoords.y;
1161 pointerCoords.y = - temp;
1162 break;
1163
1164 case InputReaderPolicyInterface::ROTATION_180:
1165 pointerCoords.x = - pointerCoords.x;
1166 pointerCoords.y = - pointerCoords.y;
1167 break;
1168
1169 case InputReaderPolicyInterface::ROTATION_270:
1170 temp = pointerCoords.x;
1171 pointerCoords.x = - pointerCoords.y;
1172 pointerCoords.y = temp;
1173 break;
1174 }
1175 }
1176 } // release lock
1177
Jeff Browne57e8952010-07-23 21:28:06 -07001178 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownb51719b2010-07-29 18:18:33 -07001179 int32_t pointerId = 0;
Jeff Brown90f0cee2010-10-08 22:31:17 -07001180 getDispatcher()->notifyMotion(when, getDeviceId(), AINPUT_SOURCE_TRACKBALL, 0,
Jeff Brownaf30ff62010-09-01 17:01:00 -07001181 motionEventAction, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brown90f0cee2010-10-08 22:31:17 -07001182 1, &pointerId, &pointerCoords, mXPrecision, mYPrecision, downTime);
1183
1184 mAccumulator.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07001185}
1186
Jeff Brown8d4dfd22010-08-10 15:47:53 -07001187int32_t TrackballInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1188 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
1189 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1190 } else {
1191 return AKEY_STATE_UNKNOWN;
1192 }
1193}
1194
Jeff Browne57e8952010-07-23 21:28:06 -07001195
1196// --- TouchInputMapper ---
1197
Jeff Brown66888372010-11-29 17:37:49 -08001198TouchInputMapper::TouchInputMapper(InputDevice* device) :
1199 InputMapper(device) {
Jeff Brownb51719b2010-07-29 18:18:33 -07001200 mLocked.surfaceOrientation = -1;
1201 mLocked.surfaceWidth = -1;
1202 mLocked.surfaceHeight = -1;
1203
1204 initializeLocked();
Jeff Browne57e8952010-07-23 21:28:06 -07001205}
1206
1207TouchInputMapper::~TouchInputMapper() {
1208}
1209
1210uint32_t TouchInputMapper::getSources() {
Jeff Brown66888372010-11-29 17:37:49 -08001211 switch (mParameters.deviceType) {
1212 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
1213 return AINPUT_SOURCE_TOUCHSCREEN;
1214 case Parameters::DEVICE_TYPE_TOUCH_PAD:
1215 return AINPUT_SOURCE_TOUCHPAD;
1216 default:
1217 assert(false);
1218 return AINPUT_SOURCE_UNKNOWN;
1219 }
Jeff Browne57e8952010-07-23 21:28:06 -07001220}
1221
1222void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1223 InputMapper::populateDeviceInfo(info);
1224
Jeff Brownb51719b2010-07-29 18:18:33 -07001225 { // acquire lock
1226 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07001227
Jeff Brownb51719b2010-07-29 18:18:33 -07001228 // Ensure surface information is up to date so that orientation changes are
1229 // noticed immediately.
1230 configureSurfaceLocked();
1231
1232 info->addMotionRange(AINPUT_MOTION_RANGE_X, mLocked.orientedRanges.x);
1233 info->addMotionRange(AINPUT_MOTION_RANGE_Y, mLocked.orientedRanges.y);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001234
1235 if (mLocked.orientedRanges.havePressure) {
1236 info->addMotionRange(AINPUT_MOTION_RANGE_PRESSURE,
1237 mLocked.orientedRanges.pressure);
1238 }
1239
1240 if (mLocked.orientedRanges.haveSize) {
1241 info->addMotionRange(AINPUT_MOTION_RANGE_SIZE,
1242 mLocked.orientedRanges.size);
1243 }
1244
Jeff Brown6b337e72010-10-14 21:42:15 -07001245 if (mLocked.orientedRanges.haveTouchSize) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001246 info->addMotionRange(AINPUT_MOTION_RANGE_TOUCH_MAJOR,
1247 mLocked.orientedRanges.touchMajor);
1248 info->addMotionRange(AINPUT_MOTION_RANGE_TOUCH_MINOR,
1249 mLocked.orientedRanges.touchMinor);
1250 }
1251
Jeff Brown6b337e72010-10-14 21:42:15 -07001252 if (mLocked.orientedRanges.haveToolSize) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001253 info->addMotionRange(AINPUT_MOTION_RANGE_TOOL_MAJOR,
1254 mLocked.orientedRanges.toolMajor);
1255 info->addMotionRange(AINPUT_MOTION_RANGE_TOOL_MINOR,
1256 mLocked.orientedRanges.toolMinor);
1257 }
1258
1259 if (mLocked.orientedRanges.haveOrientation) {
1260 info->addMotionRange(AINPUT_MOTION_RANGE_ORIENTATION,
1261 mLocked.orientedRanges.orientation);
1262 }
Jeff Brownb51719b2010-07-29 18:18:33 -07001263 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07001264}
1265
Jeff Brown26c94ff2010-09-30 14:33:04 -07001266void TouchInputMapper::dump(String8& dump) {
1267 { // acquire lock
1268 AutoMutex _l(mLock);
1269 dump.append(INDENT2 "Touch Input Mapper:\n");
Jeff Brown26c94ff2010-09-30 14:33:04 -07001270 dumpParameters(dump);
1271 dumpVirtualKeysLocked(dump);
1272 dumpRawAxes(dump);
1273 dumpCalibration(dump);
1274 dumpSurfaceLocked(dump);
Jeff Brown60b57762010-10-18 13:32:20 -07001275 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
Jeff Brown6b337e72010-10-14 21:42:15 -07001276 dump.appendFormat(INDENT4 "XOrigin: %d\n", mLocked.xOrigin);
1277 dump.appendFormat(INDENT4 "YOrigin: %d\n", mLocked.yOrigin);
1278 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mLocked.xScale);
1279 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mLocked.yScale);
1280 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mLocked.xPrecision);
1281 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mLocked.yPrecision);
1282 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mLocked.geometricScale);
1283 dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mLocked.toolSizeLinearScale);
1284 dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mLocked.toolSizeLinearBias);
1285 dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mLocked.toolSizeAreaScale);
1286 dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mLocked.toolSizeAreaBias);
1287 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mLocked.pressureScale);
1288 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mLocked.sizeScale);
1289 dump.appendFormat(INDENT4 "OrientationSCale: %0.3f\n", mLocked.orientationScale);
Jeff Brown26c94ff2010-09-30 14:33:04 -07001290 } // release lock
1291}
1292
Jeff Brownb51719b2010-07-29 18:18:33 -07001293void TouchInputMapper::initializeLocked() {
1294 mCurrentTouch.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07001295 mLastTouch.clear();
1296 mDownTime = 0;
Jeff Browne57e8952010-07-23 21:28:06 -07001297
1298 for (uint32_t i = 0; i < MAX_POINTERS; i++) {
1299 mAveragingTouchFilter.historyStart[i] = 0;
1300 mAveragingTouchFilter.historyEnd[i] = 0;
1301 }
1302
1303 mJumpyTouchFilter.jumpyPointsDropped = 0;
Jeff Brownb51719b2010-07-29 18:18:33 -07001304
1305 mLocked.currentVirtualKey.down = false;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001306
1307 mLocked.orientedRanges.havePressure = false;
1308 mLocked.orientedRanges.haveSize = false;
Jeff Brown6b337e72010-10-14 21:42:15 -07001309 mLocked.orientedRanges.haveTouchSize = false;
1310 mLocked.orientedRanges.haveToolSize = false;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001311 mLocked.orientedRanges.haveOrientation = false;
1312}
1313
Jeff Browne57e8952010-07-23 21:28:06 -07001314void TouchInputMapper::configure() {
1315 InputMapper::configure();
1316
1317 // Configure basic parameters.
Jeff Brown38a7fab2010-08-30 03:02:23 -07001318 configureParameters();
Jeff Browne57e8952010-07-23 21:28:06 -07001319
1320 // Configure absolute axis information.
Jeff Brown38a7fab2010-08-30 03:02:23 -07001321 configureRawAxes();
Jeff Brown38a7fab2010-08-30 03:02:23 -07001322
1323 // Prepare input device calibration.
1324 parseCalibration();
1325 resolveCalibration();
Jeff Browne57e8952010-07-23 21:28:06 -07001326
Jeff Brownb51719b2010-07-29 18:18:33 -07001327 { // acquire lock
1328 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07001329
Jeff Brown38a7fab2010-08-30 03:02:23 -07001330 // Configure surface dimensions and orientation.
Jeff Brownb51719b2010-07-29 18:18:33 -07001331 configureSurfaceLocked();
1332 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07001333}
1334
Jeff Brown38a7fab2010-08-30 03:02:23 -07001335void TouchInputMapper::configureParameters() {
1336 mParameters.useBadTouchFilter = getPolicy()->filterTouchEvents();
1337 mParameters.useAveragingTouchFilter = getPolicy()->filterTouchEvents();
1338 mParameters.useJumpyTouchFilter = getPolicy()->filterJumpyTouchEvents();
Jeff Brown66888372010-11-29 17:37:49 -08001339
1340 String8 deviceTypeString;
1341 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
1342 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
1343 deviceTypeString)) {
1344 if (deviceTypeString == "touchPad") {
1345 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
1346 } else if (deviceTypeString != "touchScreen") {
1347 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
1348 }
1349 }
1350 bool isTouchScreen = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
1351
1352 mParameters.orientationAware = isTouchScreen;
1353 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
1354 mParameters.orientationAware);
1355
1356 mParameters.associatedDisplayId = mParameters.orientationAware || isTouchScreen ? 0 : -1;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001357}
1358
Jeff Brown26c94ff2010-09-30 14:33:04 -07001359void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown66888372010-11-29 17:37:49 -08001360 dump.append(INDENT3 "Parameters:\n");
1361
1362 switch (mParameters.deviceType) {
1363 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
1364 dump.append(INDENT4 "DeviceType: touchScreen\n");
1365 break;
1366 case Parameters::DEVICE_TYPE_TOUCH_PAD:
1367 dump.append(INDENT4 "DeviceType: touchPad\n");
1368 break;
1369 default:
1370 assert(false);
1371 }
1372
1373 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1374 mParameters.associatedDisplayId);
1375 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1376 toString(mParameters.orientationAware));
1377
1378 dump.appendFormat(INDENT4 "UseBadTouchFilter: %s\n",
Jeff Brown26c94ff2010-09-30 14:33:04 -07001379 toString(mParameters.useBadTouchFilter));
Jeff Brown66888372010-11-29 17:37:49 -08001380 dump.appendFormat(INDENT4 "UseAveragingTouchFilter: %s\n",
Jeff Brown26c94ff2010-09-30 14:33:04 -07001381 toString(mParameters.useAveragingTouchFilter));
Jeff Brown66888372010-11-29 17:37:49 -08001382 dump.appendFormat(INDENT4 "UseJumpyTouchFilter: %s\n",
Jeff Brown26c94ff2010-09-30 14:33:04 -07001383 toString(mParameters.useJumpyTouchFilter));
Jeff Browna665ca82010-09-08 11:49:43 -07001384}
1385
Jeff Brown38a7fab2010-08-30 03:02:23 -07001386void TouchInputMapper::configureRawAxes() {
1387 mRawAxes.x.clear();
1388 mRawAxes.y.clear();
1389 mRawAxes.pressure.clear();
1390 mRawAxes.touchMajor.clear();
1391 mRawAxes.touchMinor.clear();
1392 mRawAxes.toolMajor.clear();
1393 mRawAxes.toolMinor.clear();
1394 mRawAxes.orientation.clear();
1395}
1396
Jeff Brown26c94ff2010-09-30 14:33:04 -07001397static void dumpAxisInfo(String8& dump, RawAbsoluteAxisInfo axis, const char* name) {
Jeff Browna665ca82010-09-08 11:49:43 -07001398 if (axis.valid) {
Jeff Brown26c94ff2010-09-30 14:33:04 -07001399 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d\n",
Jeff Browna665ca82010-09-08 11:49:43 -07001400 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz);
1401 } else {
Jeff Brown26c94ff2010-09-30 14:33:04 -07001402 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
Jeff Browna665ca82010-09-08 11:49:43 -07001403 }
1404}
1405
Jeff Brown26c94ff2010-09-30 14:33:04 -07001406void TouchInputMapper::dumpRawAxes(String8& dump) {
1407 dump.append(INDENT3 "Raw Axes:\n");
1408 dumpAxisInfo(dump, mRawAxes.x, "X");
1409 dumpAxisInfo(dump, mRawAxes.y, "Y");
1410 dumpAxisInfo(dump, mRawAxes.pressure, "Pressure");
1411 dumpAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor");
1412 dumpAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor");
1413 dumpAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor");
1414 dumpAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor");
1415 dumpAxisInfo(dump, mRawAxes.orientation, "Orientation");
Jeff Browne57e8952010-07-23 21:28:06 -07001416}
1417
Jeff Brownb51719b2010-07-29 18:18:33 -07001418bool TouchInputMapper::configureSurfaceLocked() {
Jeff Browne57e8952010-07-23 21:28:06 -07001419 // Update orientation and dimensions if needed.
Jeff Brown66888372010-11-29 17:37:49 -08001420 int32_t orientation = InputReaderPolicyInterface::ROTATION_0;
1421 int32_t width = mRawAxes.x.getRange();
1422 int32_t height = mRawAxes.y.getRange();
1423
1424 if (mParameters.associatedDisplayId >= 0) {
1425 bool wantSize = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
1426 bool wantOrientation = mParameters.orientationAware;
1427
Jeff Brownb51719b2010-07-29 18:18:33 -07001428 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown66888372010-11-29 17:37:49 -08001429 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1430 wantSize ? &width : NULL, wantSize ? &height : NULL,
1431 wantOrientation ? &orientation : NULL)) {
Jeff Browne57e8952010-07-23 21:28:06 -07001432 return false;
1433 }
Jeff Browne57e8952010-07-23 21:28:06 -07001434 }
1435
Jeff Brownb51719b2010-07-29 18:18:33 -07001436 bool orientationChanged = mLocked.surfaceOrientation != orientation;
Jeff Browne57e8952010-07-23 21:28:06 -07001437 if (orientationChanged) {
Jeff Brownb51719b2010-07-29 18:18:33 -07001438 mLocked.surfaceOrientation = orientation;
Jeff Browne57e8952010-07-23 21:28:06 -07001439 }
1440
Jeff Brownb51719b2010-07-29 18:18:33 -07001441 bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height;
Jeff Browne57e8952010-07-23 21:28:06 -07001442 if (sizeChanged) {
Jeff Browndb360642010-12-02 13:50:46 -08001443 LOGI("Device reconfigured: id=%d, name='%s', display size is now %dx%d",
Jeff Brown26c94ff2010-09-30 14:33:04 -07001444 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001445
Jeff Brownb51719b2010-07-29 18:18:33 -07001446 mLocked.surfaceWidth = width;
1447 mLocked.surfaceHeight = height;
Jeff Browne57e8952010-07-23 21:28:06 -07001448
Jeff Brown38a7fab2010-08-30 03:02:23 -07001449 // Configure X and Y factors.
1450 if (mRawAxes.x.valid && mRawAxes.y.valid) {
Jeff Brown60b57762010-10-18 13:32:20 -07001451 mLocked.xOrigin = mCalibration.haveXOrigin
1452 ? mCalibration.xOrigin
1453 : mRawAxes.x.minValue;
1454 mLocked.yOrigin = mCalibration.haveYOrigin
1455 ? mCalibration.yOrigin
1456 : mRawAxes.y.minValue;
1457 mLocked.xScale = mCalibration.haveXScale
1458 ? mCalibration.xScale
1459 : float(width) / mRawAxes.x.getRange();
1460 mLocked.yScale = mCalibration.haveYScale
1461 ? mCalibration.yScale
1462 : float(height) / mRawAxes.y.getRange();
Jeff Brownb51719b2010-07-29 18:18:33 -07001463 mLocked.xPrecision = 1.0f / mLocked.xScale;
1464 mLocked.yPrecision = 1.0f / mLocked.yScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001465
Jeff Brownb51719b2010-07-29 18:18:33 -07001466 configureVirtualKeysLocked();
Jeff Browne57e8952010-07-23 21:28:06 -07001467 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001468 LOGW(INDENT "Touch device did not report support for X or Y axis!");
Jeff Brownb51719b2010-07-29 18:18:33 -07001469 mLocked.xOrigin = 0;
1470 mLocked.yOrigin = 0;
1471 mLocked.xScale = 1.0f;
1472 mLocked.yScale = 1.0f;
1473 mLocked.xPrecision = 1.0f;
1474 mLocked.yPrecision = 1.0f;
Jeff Browne57e8952010-07-23 21:28:06 -07001475 }
1476
Jeff Brown38a7fab2010-08-30 03:02:23 -07001477 // Scale factor for terms that are not oriented in a particular axis.
1478 // If the pixels are square then xScale == yScale otherwise we fake it
1479 // by choosing an average.
1480 mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale);
Jeff Browne57e8952010-07-23 21:28:06 -07001481
Jeff Brown38a7fab2010-08-30 03:02:23 -07001482 // Size of diagonal axis.
1483 float diagonalSize = pythag(width, height);
Jeff Browne57e8952010-07-23 21:28:06 -07001484
Jeff Brown38a7fab2010-08-30 03:02:23 -07001485 // TouchMajor and TouchMinor factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001486 if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) {
1487 mLocked.orientedRanges.haveTouchSize = true;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001488 mLocked.orientedRanges.touchMajor.min = 0;
1489 mLocked.orientedRanges.touchMajor.max = diagonalSize;
1490 mLocked.orientedRanges.touchMajor.flat = 0;
1491 mLocked.orientedRanges.touchMajor.fuzz = 0;
1492 mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor;
1493 }
Jeff Brownb51719b2010-07-29 18:18:33 -07001494
Jeff Brown38a7fab2010-08-30 03:02:23 -07001495 // ToolMajor and ToolMinor factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001496 mLocked.toolSizeLinearScale = 0;
1497 mLocked.toolSizeLinearBias = 0;
1498 mLocked.toolSizeAreaScale = 0;
1499 mLocked.toolSizeAreaBias = 0;
1500 if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
1501 if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) {
1502 if (mCalibration.haveToolSizeLinearScale) {
1503 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001504 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
Jeff Brown6b337e72010-10-14 21:42:15 -07001505 mLocked.toolSizeLinearScale = float(min(width, height))
Jeff Brown38a7fab2010-08-30 03:02:23 -07001506 / mRawAxes.toolMajor.maxValue;
1507 }
1508
Jeff Brown6b337e72010-10-14 21:42:15 -07001509 if (mCalibration.haveToolSizeLinearBias) {
1510 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
1511 }
1512 } else if (mCalibration.toolSizeCalibration ==
1513 Calibration::TOOL_SIZE_CALIBRATION_AREA) {
1514 if (mCalibration.haveToolSizeLinearScale) {
1515 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
1516 } else {
1517 mLocked.toolSizeLinearScale = min(width, height);
1518 }
1519
1520 if (mCalibration.haveToolSizeLinearBias) {
1521 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
1522 }
1523
1524 if (mCalibration.haveToolSizeAreaScale) {
1525 mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale;
1526 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1527 mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue;
1528 }
1529
1530 if (mCalibration.haveToolSizeAreaBias) {
1531 mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001532 }
1533 }
1534
Jeff Brown6b337e72010-10-14 21:42:15 -07001535 mLocked.orientedRanges.haveToolSize = true;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001536 mLocked.orientedRanges.toolMajor.min = 0;
1537 mLocked.orientedRanges.toolMajor.max = diagonalSize;
1538 mLocked.orientedRanges.toolMajor.flat = 0;
1539 mLocked.orientedRanges.toolMajor.fuzz = 0;
1540 mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor;
1541 }
1542
1543 // Pressure factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001544 mLocked.pressureScale = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001545 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
1546 RawAbsoluteAxisInfo rawPressureAxis;
1547 switch (mCalibration.pressureSource) {
1548 case Calibration::PRESSURE_SOURCE_PRESSURE:
1549 rawPressureAxis = mRawAxes.pressure;
1550 break;
1551 case Calibration::PRESSURE_SOURCE_TOUCH:
1552 rawPressureAxis = mRawAxes.touchMajor;
1553 break;
1554 default:
1555 rawPressureAxis.clear();
1556 }
1557
Jeff Brown38a7fab2010-08-30 03:02:23 -07001558 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
1559 || mCalibration.pressureCalibration
1560 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
1561 if (mCalibration.havePressureScale) {
1562 mLocked.pressureScale = mCalibration.pressureScale;
1563 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
1564 mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue;
1565 }
1566 }
1567
1568 mLocked.orientedRanges.havePressure = true;
1569 mLocked.orientedRanges.pressure.min = 0;
1570 mLocked.orientedRanges.pressure.max = 1.0;
1571 mLocked.orientedRanges.pressure.flat = 0;
1572 mLocked.orientedRanges.pressure.fuzz = 0;
1573 }
1574
1575 // Size factors.
Jeff Brown6b337e72010-10-14 21:42:15 -07001576 mLocked.sizeScale = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001577 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001578 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
1579 if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1580 mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue;
1581 }
1582 }
1583
1584 mLocked.orientedRanges.haveSize = true;
1585 mLocked.orientedRanges.size.min = 0;
1586 mLocked.orientedRanges.size.max = 1.0;
1587 mLocked.orientedRanges.size.flat = 0;
1588 mLocked.orientedRanges.size.fuzz = 0;
1589 }
1590
1591 // Orientation
Jeff Brown6b337e72010-10-14 21:42:15 -07001592 mLocked.orientationScale = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001593 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001594 if (mCalibration.orientationCalibration
1595 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
1596 if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) {
1597 mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue;
1598 }
1599 }
1600
1601 mLocked.orientedRanges.orientation.min = - M_PI_2;
1602 mLocked.orientedRanges.orientation.max = M_PI_2;
1603 mLocked.orientedRanges.orientation.flat = 0;
1604 mLocked.orientedRanges.orientation.fuzz = 0;
1605 }
Jeff Browne57e8952010-07-23 21:28:06 -07001606 }
1607
1608 if (orientationChanged || sizeChanged) {
1609 // Compute oriented surface dimensions, precision, and scales.
1610 float orientedXScale, orientedYScale;
Jeff Brownb51719b2010-07-29 18:18:33 -07001611 switch (mLocked.surfaceOrientation) {
Jeff Browne57e8952010-07-23 21:28:06 -07001612 case InputReaderPolicyInterface::ROTATION_90:
1613 case InputReaderPolicyInterface::ROTATION_270:
Jeff Brownb51719b2010-07-29 18:18:33 -07001614 mLocked.orientedSurfaceWidth = mLocked.surfaceHeight;
1615 mLocked.orientedSurfaceHeight = mLocked.surfaceWidth;
1616 mLocked.orientedXPrecision = mLocked.yPrecision;
1617 mLocked.orientedYPrecision = mLocked.xPrecision;
1618 orientedXScale = mLocked.yScale;
1619 orientedYScale = mLocked.xScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001620 break;
1621 default:
Jeff Brownb51719b2010-07-29 18:18:33 -07001622 mLocked.orientedSurfaceWidth = mLocked.surfaceWidth;
1623 mLocked.orientedSurfaceHeight = mLocked.surfaceHeight;
1624 mLocked.orientedXPrecision = mLocked.xPrecision;
1625 mLocked.orientedYPrecision = mLocked.yPrecision;
1626 orientedXScale = mLocked.xScale;
1627 orientedYScale = mLocked.yScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001628 break;
1629 }
1630
1631 // Configure position ranges.
Jeff Brownb51719b2010-07-29 18:18:33 -07001632 mLocked.orientedRanges.x.min = 0;
1633 mLocked.orientedRanges.x.max = mLocked.orientedSurfaceWidth;
1634 mLocked.orientedRanges.x.flat = 0;
1635 mLocked.orientedRanges.x.fuzz = orientedXScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001636
Jeff Brownb51719b2010-07-29 18:18:33 -07001637 mLocked.orientedRanges.y.min = 0;
1638 mLocked.orientedRanges.y.max = mLocked.orientedSurfaceHeight;
1639 mLocked.orientedRanges.y.flat = 0;
1640 mLocked.orientedRanges.y.fuzz = orientedYScale;
Jeff Browne57e8952010-07-23 21:28:06 -07001641 }
1642
1643 return true;
1644}
1645
Jeff Brown26c94ff2010-09-30 14:33:04 -07001646void TouchInputMapper::dumpSurfaceLocked(String8& dump) {
1647 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth);
1648 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight);
1649 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation);
Jeff Browna665ca82010-09-08 11:49:43 -07001650}
1651
Jeff Brownb51719b2010-07-29 18:18:33 -07001652void TouchInputMapper::configureVirtualKeysLocked() {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001653 assert(mRawAxes.x.valid && mRawAxes.y.valid);
Jeff Browne57e8952010-07-23 21:28:06 -07001654
Jeff Brown38a7fab2010-08-30 03:02:23 -07001655 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Browndb360642010-12-02 13:50:46 -08001656 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Browne57e8952010-07-23 21:28:06 -07001657
Jeff Brownb51719b2010-07-29 18:18:33 -07001658 mLocked.virtualKeys.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07001659
Jeff Brownb51719b2010-07-29 18:18:33 -07001660 if (virtualKeyDefinitions.size() == 0) {
1661 return;
1662 }
Jeff Browne57e8952010-07-23 21:28:06 -07001663
Jeff Brownb51719b2010-07-29 18:18:33 -07001664 mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size());
1665
Jeff Brown38a7fab2010-08-30 03:02:23 -07001666 int32_t touchScreenLeft = mRawAxes.x.minValue;
1667 int32_t touchScreenTop = mRawAxes.y.minValue;
1668 int32_t touchScreenWidth = mRawAxes.x.getRange();
1669 int32_t touchScreenHeight = mRawAxes.y.getRange();
Jeff Brownb51719b2010-07-29 18:18:33 -07001670
1671 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001672 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brownb51719b2010-07-29 18:18:33 -07001673 virtualKeyDefinitions[i];
1674
1675 mLocked.virtualKeys.add();
1676 VirtualKey& virtualKey = mLocked.virtualKeys.editTop();
1677
1678 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1679 int32_t keyCode;
1680 uint32_t flags;
1681 if (getEventHub()->scancodeToKeycode(getDeviceId(), virtualKey.scanCode,
1682 & keyCode, & flags)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001683 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
1684 virtualKey.scanCode);
Jeff Brownb51719b2010-07-29 18:18:33 -07001685 mLocked.virtualKeys.pop(); // drop the key
1686 continue;
Jeff Browne57e8952010-07-23 21:28:06 -07001687 }
1688
Jeff Brownb51719b2010-07-29 18:18:33 -07001689 virtualKey.keyCode = keyCode;
1690 virtualKey.flags = flags;
Jeff Browne57e8952010-07-23 21:28:06 -07001691
Jeff Brownb51719b2010-07-29 18:18:33 -07001692 // convert the key definition's display coordinates into touch coordinates for a hit box
1693 int32_t halfWidth = virtualKeyDefinition.width / 2;
1694 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Browne57e8952010-07-23 21:28:06 -07001695
Jeff Brownb51719b2010-07-29 18:18:33 -07001696 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
1697 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1698 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
1699 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1700 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
1701 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
1702 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
1703 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
Jeff Browne57e8952010-07-23 21:28:06 -07001704
Jeff Brown26c94ff2010-09-30 14:33:04 -07001705 }
1706}
1707
1708void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) {
1709 if (!mLocked.virtualKeys.isEmpty()) {
1710 dump.append(INDENT3 "Virtual Keys:\n");
1711
1712 for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) {
1713 const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i);
1714 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
1715 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1716 i, virtualKey.scanCode, virtualKey.keyCode,
1717 virtualKey.hitLeft, virtualKey.hitRight,
1718 virtualKey.hitTop, virtualKey.hitBottom);
1719 }
Jeff Brownb51719b2010-07-29 18:18:33 -07001720 }
Jeff Browne57e8952010-07-23 21:28:06 -07001721}
1722
Jeff Brown38a7fab2010-08-30 03:02:23 -07001723void TouchInputMapper::parseCalibration() {
Jeff Brown66888372010-11-29 17:37:49 -08001724 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown38a7fab2010-08-30 03:02:23 -07001725 Calibration& out = mCalibration;
1726
Jeff Brown60b57762010-10-18 13:32:20 -07001727 // Position
1728 out.haveXOrigin = in.tryGetProperty(String8("touch.position.xOrigin"), out.xOrigin);
1729 out.haveYOrigin = in.tryGetProperty(String8("touch.position.yOrigin"), out.yOrigin);
1730 out.haveXScale = in.tryGetProperty(String8("touch.position.xScale"), out.xScale);
1731 out.haveYScale = in.tryGetProperty(String8("touch.position.yScale"), out.yScale);
1732
Jeff Brown6b337e72010-10-14 21:42:15 -07001733 // Touch Size
1734 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT;
1735 String8 touchSizeCalibrationString;
1736 if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) {
1737 if (touchSizeCalibrationString == "none") {
1738 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
1739 } else if (touchSizeCalibrationString == "geometric") {
1740 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC;
1741 } else if (touchSizeCalibrationString == "pressure") {
1742 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
1743 } else if (touchSizeCalibrationString != "default") {
1744 LOGW("Invalid value for touch.touchSize.calibration: '%s'",
1745 touchSizeCalibrationString.string());
Jeff Brown38a7fab2010-08-30 03:02:23 -07001746 }
1747 }
1748
Jeff Brown6b337e72010-10-14 21:42:15 -07001749 // Tool Size
1750 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT;
1751 String8 toolSizeCalibrationString;
1752 if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) {
1753 if (toolSizeCalibrationString == "none") {
1754 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
1755 } else if (toolSizeCalibrationString == "geometric") {
1756 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC;
1757 } else if (toolSizeCalibrationString == "linear") {
1758 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
1759 } else if (toolSizeCalibrationString == "area") {
1760 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA;
1761 } else if (toolSizeCalibrationString != "default") {
1762 LOGW("Invalid value for touch.toolSize.calibration: '%s'",
1763 toolSizeCalibrationString.string());
Jeff Brown38a7fab2010-08-30 03:02:23 -07001764 }
1765 }
1766
Jeff Brown6b337e72010-10-14 21:42:15 -07001767 out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"),
1768 out.toolSizeLinearScale);
1769 out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"),
1770 out.toolSizeLinearBias);
1771 out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"),
1772 out.toolSizeAreaScale);
1773 out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"),
1774 out.toolSizeAreaBias);
1775 out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"),
1776 out.toolSizeIsSummed);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001777
1778 // Pressure
1779 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
1780 String8 pressureCalibrationString;
Jeff Brown6b337e72010-10-14 21:42:15 -07001781 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001782 if (pressureCalibrationString == "none") {
1783 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
1784 } else if (pressureCalibrationString == "physical") {
1785 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
1786 } else if (pressureCalibrationString == "amplitude") {
1787 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
1788 } else if (pressureCalibrationString != "default") {
Jeff Brown6b337e72010-10-14 21:42:15 -07001789 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown38a7fab2010-08-30 03:02:23 -07001790 pressureCalibrationString.string());
1791 }
1792 }
1793
1794 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
1795 String8 pressureSourceString;
1796 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
1797 if (pressureSourceString == "pressure") {
1798 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
1799 } else if (pressureSourceString == "touch") {
1800 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
1801 } else if (pressureSourceString != "default") {
1802 LOGW("Invalid value for touch.pressure.source: '%s'",
1803 pressureSourceString.string());
1804 }
1805 }
1806
1807 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
1808 out.pressureScale);
1809
1810 // Size
1811 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
1812 String8 sizeCalibrationString;
Jeff Brown6b337e72010-10-14 21:42:15 -07001813 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001814 if (sizeCalibrationString == "none") {
1815 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
1816 } else if (sizeCalibrationString == "normalized") {
1817 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
1818 } else if (sizeCalibrationString != "default") {
Jeff Brown6b337e72010-10-14 21:42:15 -07001819 LOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Brown38a7fab2010-08-30 03:02:23 -07001820 sizeCalibrationString.string());
1821 }
1822 }
1823
1824 // Orientation
1825 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
1826 String8 orientationCalibrationString;
Jeff Brown6b337e72010-10-14 21:42:15 -07001827 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07001828 if (orientationCalibrationString == "none") {
1829 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
1830 } else if (orientationCalibrationString == "interpolated") {
1831 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
1832 } else if (orientationCalibrationString != "default") {
Jeff Brown6b337e72010-10-14 21:42:15 -07001833 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown38a7fab2010-08-30 03:02:23 -07001834 orientationCalibrationString.string());
1835 }
1836 }
1837}
1838
1839void TouchInputMapper::resolveCalibration() {
1840 // Pressure
1841 switch (mCalibration.pressureSource) {
1842 case Calibration::PRESSURE_SOURCE_DEFAULT:
1843 if (mRawAxes.pressure.valid) {
1844 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
1845 } else if (mRawAxes.touchMajor.valid) {
1846 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
1847 }
1848 break;
1849
1850 case Calibration::PRESSURE_SOURCE_PRESSURE:
1851 if (! mRawAxes.pressure.valid) {
1852 LOGW("Calibration property touch.pressure.source is 'pressure' but "
1853 "the pressure axis is not available.");
1854 }
1855 break;
1856
1857 case Calibration::PRESSURE_SOURCE_TOUCH:
1858 if (! mRawAxes.touchMajor.valid) {
1859 LOGW("Calibration property touch.pressure.source is 'touch' but "
1860 "the touchMajor axis is not available.");
1861 }
1862 break;
1863
1864 default:
1865 break;
1866 }
1867
1868 switch (mCalibration.pressureCalibration) {
1869 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
1870 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
1871 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
1872 } else {
1873 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
1874 }
1875 break;
1876
1877 default:
1878 break;
1879 }
1880
Jeff Brown6b337e72010-10-14 21:42:15 -07001881 // Tool Size
1882 switch (mCalibration.toolSizeCalibration) {
1883 case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT:
Jeff Brown38a7fab2010-08-30 03:02:23 -07001884 if (mRawAxes.toolMajor.valid) {
Jeff Brown6b337e72010-10-14 21:42:15 -07001885 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001886 } else {
Jeff Brown6b337e72010-10-14 21:42:15 -07001887 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001888 }
1889 break;
1890
1891 default:
1892 break;
1893 }
1894
Jeff Brown6b337e72010-10-14 21:42:15 -07001895 // Touch Size
1896 switch (mCalibration.touchSizeCalibration) {
1897 case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT:
Jeff Brown38a7fab2010-08-30 03:02:23 -07001898 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
Jeff Brown6b337e72010-10-14 21:42:15 -07001899 && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
1900 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001901 } else {
Jeff Brown6b337e72010-10-14 21:42:15 -07001902 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
Jeff Brown38a7fab2010-08-30 03:02:23 -07001903 }
1904 break;
1905
1906 default:
1907 break;
1908 }
1909
1910 // Size
1911 switch (mCalibration.sizeCalibration) {
1912 case Calibration::SIZE_CALIBRATION_DEFAULT:
1913 if (mRawAxes.toolMajor.valid) {
1914 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
1915 } else {
1916 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
1917 }
1918 break;
1919
1920 default:
1921 break;
1922 }
1923
1924 // Orientation
1925 switch (mCalibration.orientationCalibration) {
1926 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
1927 if (mRawAxes.orientation.valid) {
1928 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
1929 } else {
1930 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
1931 }
1932 break;
1933
1934 default:
1935 break;
1936 }
1937}
1938
Jeff Brown26c94ff2010-09-30 14:33:04 -07001939void TouchInputMapper::dumpCalibration(String8& dump) {
1940 dump.append(INDENT3 "Calibration:\n");
Jeff Browna665ca82010-09-08 11:49:43 -07001941
Jeff Brown60b57762010-10-18 13:32:20 -07001942 // Position
1943 if (mCalibration.haveXOrigin) {
1944 dump.appendFormat(INDENT4 "touch.position.xOrigin: %d\n", mCalibration.xOrigin);
1945 }
1946 if (mCalibration.haveYOrigin) {
1947 dump.appendFormat(INDENT4 "touch.position.yOrigin: %d\n", mCalibration.yOrigin);
1948 }
1949 if (mCalibration.haveXScale) {
1950 dump.appendFormat(INDENT4 "touch.position.xScale: %0.3f\n", mCalibration.xScale);
1951 }
1952 if (mCalibration.haveYScale) {
1953 dump.appendFormat(INDENT4 "touch.position.yScale: %0.3f\n", mCalibration.yScale);
1954 }
1955
Jeff Brown6b337e72010-10-14 21:42:15 -07001956 // Touch Size
1957 switch (mCalibration.touchSizeCalibration) {
1958 case Calibration::TOUCH_SIZE_CALIBRATION_NONE:
1959 dump.append(INDENT4 "touch.touchSize.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001960 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001961 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
1962 dump.append(INDENT4 "touch.touchSize.calibration: geometric\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001963 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001964 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
1965 dump.append(INDENT4 "touch.touchSize.calibration: pressure\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001966 break;
1967 default:
1968 assert(false);
1969 }
1970
Jeff Brown6b337e72010-10-14 21:42:15 -07001971 // Tool Size
1972 switch (mCalibration.toolSizeCalibration) {
1973 case Calibration::TOOL_SIZE_CALIBRATION_NONE:
1974 dump.append(INDENT4 "touch.toolSize.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001975 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001976 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
1977 dump.append(INDENT4 "touch.toolSize.calibration: geometric\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001978 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07001979 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
1980 dump.append(INDENT4 "touch.toolSize.calibration: linear\n");
1981 break;
1982 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
1983 dump.append(INDENT4 "touch.toolSize.calibration: area\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07001984 break;
1985 default:
1986 assert(false);
1987 }
1988
Jeff Brown6b337e72010-10-14 21:42:15 -07001989 if (mCalibration.haveToolSizeLinearScale) {
1990 dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n",
1991 mCalibration.toolSizeLinearScale);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001992 }
1993
Jeff Brown6b337e72010-10-14 21:42:15 -07001994 if (mCalibration.haveToolSizeLinearBias) {
1995 dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n",
1996 mCalibration.toolSizeLinearBias);
Jeff Brown38a7fab2010-08-30 03:02:23 -07001997 }
1998
Jeff Brown6b337e72010-10-14 21:42:15 -07001999 if (mCalibration.haveToolSizeAreaScale) {
2000 dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n",
2001 mCalibration.toolSizeAreaScale);
2002 }
2003
2004 if (mCalibration.haveToolSizeAreaBias) {
2005 dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n",
2006 mCalibration.toolSizeAreaBias);
2007 }
2008
2009 if (mCalibration.haveToolSizeIsSummed) {
Jeff Brown53c16642010-11-18 20:53:46 -08002010 dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %s\n",
Jeff Brown66888372010-11-29 17:37:49 -08002011 toString(mCalibration.toolSizeIsSummed));
Jeff Brown38a7fab2010-08-30 03:02:23 -07002012 }
2013
2014 // Pressure
2015 switch (mCalibration.pressureCalibration) {
2016 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002017 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002018 break;
2019 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002020 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002021 break;
2022 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002023 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002024 break;
2025 default:
2026 assert(false);
2027 }
2028
2029 switch (mCalibration.pressureSource) {
2030 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002031 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002032 break;
2033 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002034 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002035 break;
2036 case Calibration::PRESSURE_SOURCE_DEFAULT:
2037 break;
2038 default:
2039 assert(false);
2040 }
2041
2042 if (mCalibration.havePressureScale) {
Jeff Brown26c94ff2010-09-30 14:33:04 -07002043 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
2044 mCalibration.pressureScale);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002045 }
2046
2047 // Size
2048 switch (mCalibration.sizeCalibration) {
2049 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002050 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002051 break;
2052 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002053 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002054 break;
2055 default:
2056 assert(false);
2057 }
2058
2059 // Orientation
2060 switch (mCalibration.orientationCalibration) {
2061 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002062 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002063 break;
2064 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brown26c94ff2010-09-30 14:33:04 -07002065 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown38a7fab2010-08-30 03:02:23 -07002066 break;
2067 default:
2068 assert(false);
2069 }
2070}
2071
Jeff Browne57e8952010-07-23 21:28:06 -07002072void TouchInputMapper::reset() {
2073 // Synthesize touch up event if touch is currently down.
2074 // This will also take care of finishing virtual key processing if needed.
2075 if (mLastTouch.pointerCount != 0) {
2076 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
2077 mCurrentTouch.clear();
2078 syncTouch(when, true);
2079 }
2080
Jeff Brownb51719b2010-07-29 18:18:33 -07002081 { // acquire lock
2082 AutoMutex _l(mLock);
2083 initializeLocked();
2084 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07002085
Jeff Brownb51719b2010-07-29 18:18:33 -07002086 InputMapper::reset();
Jeff Browne57e8952010-07-23 21:28:06 -07002087}
2088
2089void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Browne839a582010-04-22 18:58:52 -07002090 uint32_t policyFlags = 0;
Jeff Browne839a582010-04-22 18:58:52 -07002091
Jeff Brownb51719b2010-07-29 18:18:33 -07002092 // Preprocess pointer data.
Jeff Browne839a582010-04-22 18:58:52 -07002093
Jeff Browne57e8952010-07-23 21:28:06 -07002094 if (mParameters.useBadTouchFilter) {
2095 if (applyBadTouchFilter()) {
Jeff Browne839a582010-04-22 18:58:52 -07002096 havePointerIds = false;
2097 }
2098 }
2099
Jeff Browne57e8952010-07-23 21:28:06 -07002100 if (mParameters.useJumpyTouchFilter) {
2101 if (applyJumpyTouchFilter()) {
Jeff Browne839a582010-04-22 18:58:52 -07002102 havePointerIds = false;
2103 }
2104 }
2105
2106 if (! havePointerIds) {
Jeff Browne57e8952010-07-23 21:28:06 -07002107 calculatePointerIds();
Jeff Browne839a582010-04-22 18:58:52 -07002108 }
2109
Jeff Browne57e8952010-07-23 21:28:06 -07002110 TouchData temp;
2111 TouchData* savedTouch;
2112 if (mParameters.useAveragingTouchFilter) {
2113 temp.copyFrom(mCurrentTouch);
Jeff Browne839a582010-04-22 18:58:52 -07002114 savedTouch = & temp;
2115
Jeff Browne57e8952010-07-23 21:28:06 -07002116 applyAveragingTouchFilter();
Jeff Browne839a582010-04-22 18:58:52 -07002117 } else {
Jeff Browne57e8952010-07-23 21:28:06 -07002118 savedTouch = & mCurrentTouch;
Jeff Browne839a582010-04-22 18:58:52 -07002119 }
2120
Jeff Brownb51719b2010-07-29 18:18:33 -07002121 // Process touches and virtual keys.
Jeff Browne839a582010-04-22 18:58:52 -07002122
Jeff Browne57e8952010-07-23 21:28:06 -07002123 TouchResult touchResult = consumeOffScreenTouches(when, policyFlags);
2124 if (touchResult == DISPATCH_TOUCH) {
2125 dispatchTouches(when, policyFlags);
Jeff Browne839a582010-04-22 18:58:52 -07002126 }
2127
Jeff Brownb51719b2010-07-29 18:18:33 -07002128 // Copy current touch to last touch in preparation for the next cycle.
Jeff Browne839a582010-04-22 18:58:52 -07002129
Jeff Browne57e8952010-07-23 21:28:06 -07002130 if (touchResult == DROP_STROKE) {
2131 mLastTouch.clear();
2132 } else {
2133 mLastTouch.copyFrom(*savedTouch);
Jeff Browne839a582010-04-22 18:58:52 -07002134 }
Jeff Browne839a582010-04-22 18:58:52 -07002135}
2136
Jeff Browne57e8952010-07-23 21:28:06 -07002137TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches(
2138 nsecs_t when, uint32_t policyFlags) {
2139 int32_t keyEventAction, keyEventFlags;
2140 int32_t keyCode, scanCode, downTime;
2141 TouchResult touchResult;
Jeff Brown50de30a2010-06-22 01:27:15 -07002142
Jeff Brownb51719b2010-07-29 18:18:33 -07002143 { // acquire lock
2144 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07002145
Jeff Brownb51719b2010-07-29 18:18:33 -07002146 // Update surface size and orientation, including virtual key positions.
2147 if (! configureSurfaceLocked()) {
2148 return DROP_STROKE;
2149 }
2150
2151 // Check for virtual key press.
2152 if (mLocked.currentVirtualKey.down) {
Jeff Browne57e8952010-07-23 21:28:06 -07002153 if (mCurrentTouch.pointerCount == 0) {
2154 // Pointer went up while virtual key was down.
Jeff Brownb51719b2010-07-29 18:18:33 -07002155 mLocked.currentVirtualKey.down = false;
Jeff Browne57e8952010-07-23 21:28:06 -07002156#if DEBUG_VIRTUAL_KEYS
2157 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brown3c3cc622010-10-20 15:33:38 -07002158 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Browne57e8952010-07-23 21:28:06 -07002159#endif
2160 keyEventAction = AKEY_EVENT_ACTION_UP;
2161 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2162 touchResult = SKIP_TOUCH;
2163 goto DispatchVirtualKey;
2164 }
2165
2166 if (mCurrentTouch.pointerCount == 1) {
2167 int32_t x = mCurrentTouch.pointers[0].x;
2168 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brownb51719b2010-07-29 18:18:33 -07002169 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
2170 if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07002171 // Pointer is still within the space of the virtual key.
2172 return SKIP_TOUCH;
2173 }
2174 }
2175
2176 // Pointer left virtual key area or another pointer also went down.
2177 // Send key cancellation and drop the stroke so subsequent motions will be
2178 // considered fresh downs. This is useful when the user swipes away from the
2179 // virtual key area into the main display surface.
Jeff Brownb51719b2010-07-29 18:18:33 -07002180 mLocked.currentVirtualKey.down = false;
Jeff Browne57e8952010-07-23 21:28:06 -07002181#if DEBUG_VIRTUAL_KEYS
2182 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brown3c3cc622010-10-20 15:33:38 -07002183 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Browne57e8952010-07-23 21:28:06 -07002184#endif
2185 keyEventAction = AKEY_EVENT_ACTION_UP;
2186 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
2187 | AKEY_EVENT_FLAG_CANCELED;
Jeff Brown3c3cc622010-10-20 15:33:38 -07002188
2189 // Check whether the pointer moved inside the display area where we should
2190 // start a new stroke.
2191 int32_t x = mCurrentTouch.pointers[0].x;
2192 int32_t y = mCurrentTouch.pointers[0].y;
2193 if (isPointInsideSurfaceLocked(x, y)) {
2194 mLastTouch.clear();
2195 touchResult = DISPATCH_TOUCH;
2196 } else {
2197 touchResult = DROP_STROKE;
2198 }
Jeff Browne57e8952010-07-23 21:28:06 -07002199 } else {
2200 if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) {
2201 // Pointer just went down. Handle off-screen touches, if needed.
2202 int32_t x = mCurrentTouch.pointers[0].x;
2203 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brownb51719b2010-07-29 18:18:33 -07002204 if (! isPointInsideSurfaceLocked(x, y)) {
Jeff Browne57e8952010-07-23 21:28:06 -07002205 // If exactly one pointer went down, check for virtual key hit.
2206 // Otherwise we will drop the entire stroke.
2207 if (mCurrentTouch.pointerCount == 1) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002208 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
Jeff Browne57e8952010-07-23 21:28:06 -07002209 if (virtualKey) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002210 mLocked.currentVirtualKey.down = true;
2211 mLocked.currentVirtualKey.downTime = when;
2212 mLocked.currentVirtualKey.keyCode = virtualKey->keyCode;
2213 mLocked.currentVirtualKey.scanCode = virtualKey->scanCode;
Jeff Browne57e8952010-07-23 21:28:06 -07002214#if DEBUG_VIRTUAL_KEYS
2215 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brown3c3cc622010-10-20 15:33:38 -07002216 mLocked.currentVirtualKey.keyCode,
2217 mLocked.currentVirtualKey.scanCode);
Jeff Browne57e8952010-07-23 21:28:06 -07002218#endif
2219 keyEventAction = AKEY_EVENT_ACTION_DOWN;
2220 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM
2221 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2222 touchResult = SKIP_TOUCH;
2223 goto DispatchVirtualKey;
2224 }
2225 }
2226 return DROP_STROKE;
2227 }
2228 }
2229 return DISPATCH_TOUCH;
2230 }
2231
2232 DispatchVirtualKey:
2233 // Collect remaining state needed to dispatch virtual key.
Jeff Brownb51719b2010-07-29 18:18:33 -07002234 keyCode = mLocked.currentVirtualKey.keyCode;
2235 scanCode = mLocked.currentVirtualKey.scanCode;
2236 downTime = mLocked.currentVirtualKey.downTime;
2237 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07002238
2239 // Dispatch virtual key.
2240 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown956c0fb2010-10-01 14:55:30 -07002241 policyFlags |= POLICY_FLAG_VIRTUAL;
Jeff Brown90f0cee2010-10-08 22:31:17 -07002242 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
2243 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
2244 return touchResult;
Jeff Browne839a582010-04-22 18:58:52 -07002245}
2246
Jeff Browne57e8952010-07-23 21:28:06 -07002247void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
2248 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2249 uint32_t lastPointerCount = mLastTouch.pointerCount;
Jeff Browne839a582010-04-22 18:58:52 -07002250 if (currentPointerCount == 0 && lastPointerCount == 0) {
2251 return; // nothing to do!
2252 }
2253
Jeff Browne57e8952010-07-23 21:28:06 -07002254 BitSet32 currentIdBits = mCurrentTouch.idBits;
2255 BitSet32 lastIdBits = mLastTouch.idBits;
Jeff Browne839a582010-04-22 18:58:52 -07002256
2257 if (currentIdBits == lastIdBits) {
2258 // No pointer id changes so this is a move event.
2259 // The dispatcher takes care of batching moves so we don't have to deal with that here.
Jeff Brown5c1ed842010-07-14 18:48:53 -07002260 int32_t motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browne57e8952010-07-23 21:28:06 -07002261 dispatchTouch(when, policyFlags, & mCurrentTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002262 currentIdBits, -1, currentPointerCount, motionEventAction);
Jeff Browne839a582010-04-22 18:58:52 -07002263 } else {
Jeff Brown3c3cc622010-10-20 15:33:38 -07002264 // There may be pointers going up and pointers going down and pointers moving
2265 // all at the same time.
Jeff Browne839a582010-04-22 18:58:52 -07002266 BitSet32 upIdBits(lastIdBits.value & ~ currentIdBits.value);
2267 BitSet32 downIdBits(currentIdBits.value & ~ lastIdBits.value);
2268 BitSet32 activeIdBits(lastIdBits.value);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002269 uint32_t pointerCount = lastPointerCount;
Jeff Browne839a582010-04-22 18:58:52 -07002270
Jeff Brown3c3cc622010-10-20 15:33:38 -07002271 // Produce an intermediate representation of the touch data that consists of the
2272 // old location of pointers that have just gone up and the new location of pointers that
2273 // have just moved but omits the location of pointers that have just gone down.
2274 TouchData interimTouch;
2275 interimTouch.copyFrom(mLastTouch);
2276
2277 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
2278 bool moveNeeded = false;
2279 while (!moveIdBits.isEmpty()) {
2280 uint32_t moveId = moveIdBits.firstMarkedBit();
2281 moveIdBits.clearBit(moveId);
2282
2283 int32_t oldIndex = mLastTouch.idToIndex[moveId];
2284 int32_t newIndex = mCurrentTouch.idToIndex[moveId];
2285 if (mLastTouch.pointers[oldIndex] != mCurrentTouch.pointers[newIndex]) {
2286 interimTouch.pointers[oldIndex] = mCurrentTouch.pointers[newIndex];
2287 moveNeeded = true;
2288 }
2289 }
2290
2291 // Dispatch pointer up events using the interim pointer locations.
2292 while (!upIdBits.isEmpty()) {
Jeff Browne839a582010-04-22 18:58:52 -07002293 uint32_t upId = upIdBits.firstMarkedBit();
2294 upIdBits.clearBit(upId);
2295 BitSet32 oldActiveIdBits = activeIdBits;
2296 activeIdBits.clearBit(upId);
2297
2298 int32_t motionEventAction;
2299 if (activeIdBits.isEmpty()) {
Jeff Brown5c1ed842010-07-14 18:48:53 -07002300 motionEventAction = AMOTION_EVENT_ACTION_UP;
Jeff Browne839a582010-04-22 18:58:52 -07002301 } else {
Jeff Brown3cf1c9b2010-07-16 15:01:56 -07002302 motionEventAction = AMOTION_EVENT_ACTION_POINTER_UP;
Jeff Browne839a582010-04-22 18:58:52 -07002303 }
2304
Jeff Brown3c3cc622010-10-20 15:33:38 -07002305 dispatchTouch(when, policyFlags, &interimTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002306 oldActiveIdBits, upId, pointerCount, motionEventAction);
2307 pointerCount -= 1;
Jeff Browne839a582010-04-22 18:58:52 -07002308 }
2309
Jeff Brown3c3cc622010-10-20 15:33:38 -07002310 // Dispatch move events if any of the remaining pointers moved from their old locations.
2311 // Although applications receive new locations as part of individual pointer up
2312 // events, they do not generally handle them except when presented in a move event.
2313 if (moveNeeded) {
2314 dispatchTouch(when, policyFlags, &mCurrentTouch,
2315 activeIdBits, -1, pointerCount, AMOTION_EVENT_ACTION_MOVE);
2316 }
2317
2318 // Dispatch pointer down events using the new pointer locations.
2319 while (!downIdBits.isEmpty()) {
Jeff Browne839a582010-04-22 18:58:52 -07002320 uint32_t downId = downIdBits.firstMarkedBit();
2321 downIdBits.clearBit(downId);
2322 BitSet32 oldActiveIdBits = activeIdBits;
2323 activeIdBits.markBit(downId);
2324
2325 int32_t motionEventAction;
2326 if (oldActiveIdBits.isEmpty()) {
Jeff Brown5c1ed842010-07-14 18:48:53 -07002327 motionEventAction = AMOTION_EVENT_ACTION_DOWN;
Jeff Browne57e8952010-07-23 21:28:06 -07002328 mDownTime = when;
Jeff Browne839a582010-04-22 18:58:52 -07002329 } else {
Jeff Brown3cf1c9b2010-07-16 15:01:56 -07002330 motionEventAction = AMOTION_EVENT_ACTION_POINTER_DOWN;
Jeff Browne839a582010-04-22 18:58:52 -07002331 }
2332
Jeff Brown38a7fab2010-08-30 03:02:23 -07002333 pointerCount += 1;
Jeff Brown3c3cc622010-10-20 15:33:38 -07002334 dispatchTouch(when, policyFlags, &mCurrentTouch,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002335 activeIdBits, downId, pointerCount, motionEventAction);
Jeff Browne839a582010-04-22 18:58:52 -07002336 }
2337 }
2338}
2339
Jeff Browne57e8952010-07-23 21:28:06 -07002340void TouchInputMapper::dispatchTouch(nsecs_t when, uint32_t policyFlags,
Jeff Brown38a7fab2010-08-30 03:02:23 -07002341 TouchData* touch, BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
Jeff Browne839a582010-04-22 18:58:52 -07002342 int32_t motionEventAction) {
Jeff Browne839a582010-04-22 18:58:52 -07002343 int32_t pointerIds[MAX_POINTERS];
2344 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Browne839a582010-04-22 18:58:52 -07002345 int32_t motionEventEdgeFlags = 0;
Jeff Brownb51719b2010-07-29 18:18:33 -07002346 float xPrecision, yPrecision;
2347
2348 { // acquire lock
2349 AutoMutex _l(mLock);
2350
2351 // Walk through the the active pointers and map touch screen coordinates (TouchData) into
2352 // display coordinates (PointerCoords) and adjust for display orientation.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002353 for (uint32_t outIndex = 0; ! idBits.isEmpty(); outIndex++) {
Jeff Brownb51719b2010-07-29 18:18:33 -07002354 uint32_t id = idBits.firstMarkedBit();
2355 idBits.clearBit(id);
Jeff Brown38a7fab2010-08-30 03:02:23 -07002356 uint32_t inIndex = touch->idToIndex[id];
Jeff Brownb51719b2010-07-29 18:18:33 -07002357
Jeff Brown38a7fab2010-08-30 03:02:23 -07002358 const PointerData& in = touch->pointers[inIndex];
Jeff Brownb51719b2010-07-29 18:18:33 -07002359
Jeff Brown38a7fab2010-08-30 03:02:23 -07002360 // X and Y
2361 float x = float(in.x - mLocked.xOrigin) * mLocked.xScale;
2362 float y = float(in.y - mLocked.yOrigin) * mLocked.yScale;
Jeff Brownb51719b2010-07-29 18:18:33 -07002363
Jeff Brown38a7fab2010-08-30 03:02:23 -07002364 // ToolMajor and ToolMinor
2365 float toolMajor, toolMinor;
Jeff Brown6b337e72010-10-14 21:42:15 -07002366 switch (mCalibration.toolSizeCalibration) {
2367 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002368 toolMajor = in.toolMajor * mLocked.geometricScale;
2369 if (mRawAxes.toolMinor.valid) {
2370 toolMinor = in.toolMinor * mLocked.geometricScale;
2371 } else {
2372 toolMinor = toolMajor;
2373 }
2374 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002375 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002376 toolMajor = in.toolMajor != 0
Jeff Brown6b337e72010-10-14 21:42:15 -07002377 ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias
Jeff Brown38a7fab2010-08-30 03:02:23 -07002378 : 0;
2379 if (mRawAxes.toolMinor.valid) {
2380 toolMinor = in.toolMinor != 0
Jeff Brown6b337e72010-10-14 21:42:15 -07002381 ? in.toolMinor * mLocked.toolSizeLinearScale
2382 + mLocked.toolSizeLinearBias
Jeff Brown38a7fab2010-08-30 03:02:23 -07002383 : 0;
2384 } else {
2385 toolMinor = toolMajor;
2386 }
2387 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002388 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2389 if (in.toolMajor != 0) {
2390 float diameter = sqrtf(in.toolMajor
2391 * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias);
2392 toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias;
2393 } else {
2394 toolMajor = 0;
2395 }
2396 toolMinor = toolMajor;
2397 break;
Jeff Brown38a7fab2010-08-30 03:02:23 -07002398 default:
2399 toolMajor = 0;
2400 toolMinor = 0;
2401 break;
Jeff Brownb51719b2010-07-29 18:18:33 -07002402 }
2403
Jeff Brown6b337e72010-10-14 21:42:15 -07002404 if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002405 toolMajor /= pointerCount;
2406 toolMinor /= pointerCount;
2407 }
2408
2409 // Pressure
2410 float rawPressure;
2411 switch (mCalibration.pressureSource) {
2412 case Calibration::PRESSURE_SOURCE_PRESSURE:
2413 rawPressure = in.pressure;
2414 break;
2415 case Calibration::PRESSURE_SOURCE_TOUCH:
2416 rawPressure = in.touchMajor;
2417 break;
2418 default:
2419 rawPressure = 0;
2420 }
2421
2422 float pressure;
2423 switch (mCalibration.pressureCalibration) {
2424 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
2425 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
2426 pressure = rawPressure * mLocked.pressureScale;
2427 break;
2428 default:
2429 pressure = 1;
2430 break;
2431 }
2432
2433 // TouchMajor and TouchMinor
2434 float touchMajor, touchMinor;
Jeff Brown6b337e72010-10-14 21:42:15 -07002435 switch (mCalibration.touchSizeCalibration) {
2436 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002437 touchMajor = in.touchMajor * mLocked.geometricScale;
2438 if (mRawAxes.touchMinor.valid) {
2439 touchMinor = in.touchMinor * mLocked.geometricScale;
2440 } else {
2441 touchMinor = touchMajor;
2442 }
2443 break;
Jeff Brown6b337e72010-10-14 21:42:15 -07002444 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
Jeff Brown38a7fab2010-08-30 03:02:23 -07002445 touchMajor = toolMajor * pressure;
2446 touchMinor = toolMinor * pressure;
2447 break;
2448 default:
2449 touchMajor = 0;
2450 touchMinor = 0;
2451 break;
2452 }
2453
2454 if (touchMajor > toolMajor) {
2455 touchMajor = toolMajor;
2456 }
2457 if (touchMinor > toolMinor) {
2458 touchMinor = toolMinor;
2459 }
2460
2461 // Size
2462 float size;
2463 switch (mCalibration.sizeCalibration) {
2464 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
2465 float rawSize = mRawAxes.toolMinor.valid
2466 ? avg(in.toolMajor, in.toolMinor)
2467 : in.toolMajor;
2468 size = rawSize * mLocked.sizeScale;
2469 break;
2470 }
2471 default:
2472 size = 0;
2473 break;
2474 }
2475
2476 // Orientation
2477 float orientation;
2478 switch (mCalibration.orientationCalibration) {
2479 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
2480 orientation = in.orientation * mLocked.orientationScale;
2481 break;
2482 default:
2483 orientation = 0;
2484 }
2485
2486 // Adjust coords for orientation.
Jeff Brownb51719b2010-07-29 18:18:33 -07002487 switch (mLocked.surfaceOrientation) {
2488 case InputReaderPolicyInterface::ROTATION_90: {
2489 float xTemp = x;
2490 x = y;
2491 y = mLocked.surfaceWidth - xTemp;
2492 orientation -= M_PI_2;
2493 if (orientation < - M_PI_2) {
2494 orientation += M_PI;
2495 }
2496 break;
2497 }
2498 case InputReaderPolicyInterface::ROTATION_180: {
2499 x = mLocked.surfaceWidth - x;
2500 y = mLocked.surfaceHeight - y;
2501 orientation = - orientation;
2502 break;
2503 }
2504 case InputReaderPolicyInterface::ROTATION_270: {
2505 float xTemp = x;
2506 x = mLocked.surfaceHeight - y;
2507 y = xTemp;
2508 orientation += M_PI_2;
2509 if (orientation > M_PI_2) {
2510 orientation -= M_PI;
2511 }
2512 break;
2513 }
2514 }
2515
Jeff Brown38a7fab2010-08-30 03:02:23 -07002516 // Write output coords.
2517 PointerCoords& out = pointerCoords[outIndex];
2518 out.x = x;
2519 out.y = y;
2520 out.pressure = pressure;
2521 out.size = size;
2522 out.touchMajor = touchMajor;
2523 out.touchMinor = touchMinor;
2524 out.toolMajor = toolMajor;
2525 out.toolMinor = toolMinor;
2526 out.orientation = orientation;
Jeff Brownb51719b2010-07-29 18:18:33 -07002527
Jeff Brown38a7fab2010-08-30 03:02:23 -07002528 pointerIds[outIndex] = int32_t(id);
Jeff Brownb51719b2010-07-29 18:18:33 -07002529
2530 if (id == changedId) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002531 motionEventAction |= outIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
Jeff Brownb51719b2010-07-29 18:18:33 -07002532 }
Jeff Browne839a582010-04-22 18:58:52 -07002533 }
Jeff Brownb51719b2010-07-29 18:18:33 -07002534
2535 // Check edge flags by looking only at the first pointer since the flags are
2536 // global to the event.
2537 if (motionEventAction == AMOTION_EVENT_ACTION_DOWN) {
2538 if (pointerCoords[0].x <= 0) {
2539 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_LEFT;
2540 } else if (pointerCoords[0].x >= mLocked.orientedSurfaceWidth) {
2541 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_RIGHT;
2542 }
2543 if (pointerCoords[0].y <= 0) {
2544 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_TOP;
2545 } else if (pointerCoords[0].y >= mLocked.orientedSurfaceHeight) {
2546 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_BOTTOM;
2547 }
Jeff Browne839a582010-04-22 18:58:52 -07002548 }
Jeff Brownb51719b2010-07-29 18:18:33 -07002549
2550 xPrecision = mLocked.orientedXPrecision;
2551 yPrecision = mLocked.orientedYPrecision;
2552 } // release lock
Jeff Browne839a582010-04-22 18:58:52 -07002553
Jeff Brown77e26fc2010-10-07 13:44:51 -07002554 getDispatcher()->notifyMotion(when, getDeviceId(), getSources(), policyFlags,
Jeff Brownaf30ff62010-09-01 17:01:00 -07002555 motionEventAction, 0, getContext()->getGlobalMetaState(), motionEventEdgeFlags,
Jeff Browne839a582010-04-22 18:58:52 -07002556 pointerCount, pointerIds, pointerCoords,
Jeff Brownb51719b2010-07-29 18:18:33 -07002557 xPrecision, yPrecision, mDownTime);
Jeff Browne839a582010-04-22 18:58:52 -07002558}
2559
Jeff Brownb51719b2010-07-29 18:18:33 -07002560bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002561 if (mRawAxes.x.valid && mRawAxes.y.valid) {
2562 return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue
2563 && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue;
Jeff Browne839a582010-04-22 18:58:52 -07002564 }
Jeff Browne57e8952010-07-23 21:28:06 -07002565 return true;
2566}
2567
Jeff Brownb51719b2010-07-29 18:18:33 -07002568const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked(
2569 int32_t x, int32_t y) {
2570 size_t numVirtualKeys = mLocked.virtualKeys.size();
2571 for (size_t i = 0; i < numVirtualKeys; i++) {
2572 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07002573
2574#if DEBUG_VIRTUAL_KEYS
2575 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
2576 "left=%d, top=%d, right=%d, bottom=%d",
2577 x, y,
2578 virtualKey.keyCode, virtualKey.scanCode,
2579 virtualKey.hitLeft, virtualKey.hitTop,
2580 virtualKey.hitRight, virtualKey.hitBottom);
2581#endif
2582
2583 if (virtualKey.isHit(x, y)) {
2584 return & virtualKey;
2585 }
2586 }
2587
2588 return NULL;
2589}
2590
2591void TouchInputMapper::calculatePointerIds() {
2592 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2593 uint32_t lastPointerCount = mLastTouch.pointerCount;
2594
2595 if (currentPointerCount == 0) {
2596 // No pointers to assign.
2597 mCurrentTouch.idBits.clear();
2598 } else if (lastPointerCount == 0) {
2599 // All pointers are new.
2600 mCurrentTouch.idBits.clear();
2601 for (uint32_t i = 0; i < currentPointerCount; i++) {
2602 mCurrentTouch.pointers[i].id = i;
2603 mCurrentTouch.idToIndex[i] = i;
2604 mCurrentTouch.idBits.markBit(i);
2605 }
2606 } else if (currentPointerCount == 1 && lastPointerCount == 1) {
2607 // Only one pointer and no change in count so it must have the same id as before.
2608 uint32_t id = mLastTouch.pointers[0].id;
2609 mCurrentTouch.pointers[0].id = id;
2610 mCurrentTouch.idToIndex[id] = 0;
2611 mCurrentTouch.idBits.value = BitSet32::valueForBit(id);
2612 } else {
2613 // General case.
2614 // We build a heap of squared euclidean distances between current and last pointers
2615 // associated with the current and last pointer indices. Then, we find the best
2616 // match (by distance) for each current pointer.
2617 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
2618
2619 uint32_t heapSize = 0;
2620 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
2621 currentPointerIndex++) {
2622 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
2623 lastPointerIndex++) {
2624 int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x
2625 - mLastTouch.pointers[lastPointerIndex].x;
2626 int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y
2627 - mLastTouch.pointers[lastPointerIndex].y;
2628
2629 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
2630
2631 // Insert new element into the heap (sift up).
2632 heap[heapSize].currentPointerIndex = currentPointerIndex;
2633 heap[heapSize].lastPointerIndex = lastPointerIndex;
2634 heap[heapSize].distance = distance;
2635 heapSize += 1;
2636 }
2637 }
2638
2639 // Heapify
2640 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
2641 startIndex -= 1;
2642 for (uint32_t parentIndex = startIndex; ;) {
2643 uint32_t childIndex = parentIndex * 2 + 1;
2644 if (childIndex >= heapSize) {
2645 break;
2646 }
2647
2648 if (childIndex + 1 < heapSize
2649 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2650 childIndex += 1;
2651 }
2652
2653 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2654 break;
2655 }
2656
2657 swap(heap[parentIndex], heap[childIndex]);
2658 parentIndex = childIndex;
2659 }
2660 }
2661
2662#if DEBUG_POINTER_ASSIGNMENT
2663 LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize);
2664 for (size_t i = 0; i < heapSize; i++) {
2665 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2666 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2667 heap[i].distance);
2668 }
2669#endif
2670
2671 // Pull matches out by increasing order of distance.
2672 // To avoid reassigning pointers that have already been matched, the loop keeps track
2673 // of which last and current pointers have been matched using the matchedXXXBits variables.
2674 // It also tracks the used pointer id bits.
2675 BitSet32 matchedLastBits(0);
2676 BitSet32 matchedCurrentBits(0);
2677 BitSet32 usedIdBits(0);
2678 bool first = true;
2679 for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) {
2680 for (;;) {
2681 if (first) {
2682 // The first time through the loop, we just consume the root element of
2683 // the heap (the one with smallest distance).
2684 first = false;
2685 } else {
2686 // Previous iterations consumed the root element of the heap.
2687 // Pop root element off of the heap (sift down).
2688 heapSize -= 1;
2689 assert(heapSize > 0);
2690
2691 // Sift down.
2692 heap[0] = heap[heapSize];
2693 for (uint32_t parentIndex = 0; ;) {
2694 uint32_t childIndex = parentIndex * 2 + 1;
2695 if (childIndex >= heapSize) {
2696 break;
2697 }
2698
2699 if (childIndex + 1 < heapSize
2700 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2701 childIndex += 1;
2702 }
2703
2704 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2705 break;
2706 }
2707
2708 swap(heap[parentIndex], heap[childIndex]);
2709 parentIndex = childIndex;
2710 }
2711
2712#if DEBUG_POINTER_ASSIGNMENT
2713 LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize);
2714 for (size_t i = 0; i < heapSize; i++) {
2715 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2716 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2717 heap[i].distance);
2718 }
2719#endif
2720 }
2721
2722 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
2723 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
2724
2725 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
2726 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
2727
2728 matchedCurrentBits.markBit(currentPointerIndex);
2729 matchedLastBits.markBit(lastPointerIndex);
2730
2731 uint32_t id = mLastTouch.pointers[lastPointerIndex].id;
2732 mCurrentTouch.pointers[currentPointerIndex].id = id;
2733 mCurrentTouch.idToIndex[id] = currentPointerIndex;
2734 usedIdBits.markBit(id);
2735
2736#if DEBUG_POINTER_ASSIGNMENT
2737 LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
2738 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
2739#endif
2740 break;
2741 }
2742 }
2743
2744 // Assign fresh ids to new pointers.
2745 if (currentPointerCount > lastPointerCount) {
2746 for (uint32_t i = currentPointerCount - lastPointerCount; ;) {
2747 uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit();
2748 uint32_t id = usedIdBits.firstUnmarkedBit();
2749
2750 mCurrentTouch.pointers[currentPointerIndex].id = id;
2751 mCurrentTouch.idToIndex[id] = currentPointerIndex;
2752 usedIdBits.markBit(id);
2753
2754#if DEBUG_POINTER_ASSIGNMENT
2755 LOGD("calculatePointerIds - assigned: cur=%d, id=%d",
2756 currentPointerIndex, id);
2757#endif
2758
2759 if (--i == 0) break; // done
2760 matchedCurrentBits.markBit(currentPointerIndex);
2761 }
2762 }
2763
2764 // Fix id bits.
2765 mCurrentTouch.idBits = usedIdBits;
2766 }
2767}
2768
2769/* Special hack for devices that have bad screen data: if one of the
2770 * points has moved more than a screen height from the last position,
2771 * then drop it. */
2772bool TouchInputMapper::applyBadTouchFilter() {
2773 // This hack requires valid axis parameters.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002774 if (! mRawAxes.y.valid) {
Jeff Browne57e8952010-07-23 21:28:06 -07002775 return false;
2776 }
2777
2778 uint32_t pointerCount = mCurrentTouch.pointerCount;
2779
2780 // Nothing to do if there are no points.
2781 if (pointerCount == 0) {
2782 return false;
2783 }
2784
2785 // Don't do anything if a finger is going down or up. We run
2786 // here before assigning pointer IDs, so there isn't a good
2787 // way to do per-finger matching.
2788 if (pointerCount != mLastTouch.pointerCount) {
2789 return false;
2790 }
2791
2792 // We consider a single movement across more than a 7/16 of
2793 // the long size of the screen to be bad. This was a magic value
2794 // determined by looking at the maximum distance it is feasible
2795 // to actually move in one sample.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002796 int32_t maxDeltaY = mRawAxes.y.getRange() * 7 / 16;
Jeff Browne57e8952010-07-23 21:28:06 -07002797
2798 // XXX The original code in InputDevice.java included commented out
2799 // code for testing the X axis. Note that when we drop a point
2800 // we don't actually restore the old X either. Strange.
2801 // The old code also tries to track when bad points were previously
2802 // detected but it turns out that due to the placement of a "break"
2803 // at the end of the loop, we never set mDroppedBadPoint to true
2804 // so it is effectively dead code.
2805 // Need to figure out if the old code is busted or just overcomplicated
2806 // but working as intended.
2807
2808 // Look through all new points and see if any are farther than
2809 // acceptable from all previous points.
2810 for (uint32_t i = pointerCount; i-- > 0; ) {
2811 int32_t y = mCurrentTouch.pointers[i].y;
2812 int32_t closestY = INT_MAX;
2813 int32_t closestDeltaY = 0;
2814
2815#if DEBUG_HACKS
2816 LOGD("BadTouchFilter: Looking at next point #%d: y=%d", i, y);
2817#endif
2818
2819 for (uint32_t j = pointerCount; j-- > 0; ) {
2820 int32_t lastY = mLastTouch.pointers[j].y;
2821 int32_t deltaY = abs(y - lastY);
2822
2823#if DEBUG_HACKS
2824 LOGD("BadTouchFilter: Comparing with last point #%d: y=%d deltaY=%d",
2825 j, lastY, deltaY);
2826#endif
2827
2828 if (deltaY < maxDeltaY) {
2829 goto SkipSufficientlyClosePoint;
2830 }
2831 if (deltaY < closestDeltaY) {
2832 closestDeltaY = deltaY;
2833 closestY = lastY;
2834 }
2835 }
2836
2837 // Must not have found a close enough match.
2838#if DEBUG_HACKS
2839 LOGD("BadTouchFilter: Dropping bad point #%d: newY=%d oldY=%d deltaY=%d maxDeltaY=%d",
2840 i, y, closestY, closestDeltaY, maxDeltaY);
2841#endif
2842
2843 mCurrentTouch.pointers[i].y = closestY;
2844 return true; // XXX original code only corrects one point
2845
2846 SkipSufficientlyClosePoint: ;
2847 }
2848
2849 // No change.
2850 return false;
2851}
2852
2853/* Special hack for devices that have bad screen data: drop points where
2854 * the coordinate value for one axis has jumped to the other pointer's location.
2855 */
2856bool TouchInputMapper::applyJumpyTouchFilter() {
2857 // This hack requires valid axis parameters.
Jeff Brown38a7fab2010-08-30 03:02:23 -07002858 if (! mRawAxes.y.valid) {
Jeff Browne57e8952010-07-23 21:28:06 -07002859 return false;
2860 }
2861
2862 uint32_t pointerCount = mCurrentTouch.pointerCount;
2863 if (mLastTouch.pointerCount != pointerCount) {
2864#if DEBUG_HACKS
2865 LOGD("JumpyTouchFilter: Different pointer count %d -> %d",
2866 mLastTouch.pointerCount, pointerCount);
2867 for (uint32_t i = 0; i < pointerCount; i++) {
2868 LOGD(" Pointer %d (%d, %d)", i,
2869 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
2870 }
2871#endif
2872
2873 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_TRANSITION_DROPS) {
2874 if (mLastTouch.pointerCount == 1 && pointerCount == 2) {
2875 // Just drop the first few events going from 1 to 2 pointers.
2876 // They're bad often enough that they're not worth considering.
2877 mCurrentTouch.pointerCount = 1;
2878 mJumpyTouchFilter.jumpyPointsDropped += 1;
2879
2880#if DEBUG_HACKS
2881 LOGD("JumpyTouchFilter: Pointer 2 dropped");
2882#endif
2883 return true;
2884 } else if (mLastTouch.pointerCount == 2 && pointerCount == 1) {
2885 // The event when we go from 2 -> 1 tends to be messed up too
2886 mCurrentTouch.pointerCount = 2;
2887 mCurrentTouch.pointers[0] = mLastTouch.pointers[0];
2888 mCurrentTouch.pointers[1] = mLastTouch.pointers[1];
2889 mJumpyTouchFilter.jumpyPointsDropped += 1;
2890
2891#if DEBUG_HACKS
2892 for (int32_t i = 0; i < 2; i++) {
2893 LOGD("JumpyTouchFilter: Pointer %d replaced (%d, %d)", i,
2894 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
2895 }
2896#endif
2897 return true;
2898 }
2899 }
2900 // Reset jumpy points dropped on other transitions or if limit exceeded.
2901 mJumpyTouchFilter.jumpyPointsDropped = 0;
2902
2903#if DEBUG_HACKS
2904 LOGD("JumpyTouchFilter: Transition - drop limit reset");
2905#endif
2906 return false;
2907 }
2908
2909 // We have the same number of pointers as last time.
2910 // A 'jumpy' point is one where the coordinate value for one axis
2911 // has jumped to the other pointer's location. No need to do anything
2912 // else if we only have one pointer.
2913 if (pointerCount < 2) {
2914 return false;
2915 }
2916
2917 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_DROP_LIMIT) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07002918 int jumpyEpsilon = mRawAxes.y.getRange() / JUMPY_EPSILON_DIVISOR;
Jeff Browne57e8952010-07-23 21:28:06 -07002919
2920 // We only replace the single worst jumpy point as characterized by pointer distance
2921 // in a single axis.
2922 int32_t badPointerIndex = -1;
2923 int32_t badPointerReplacementIndex = -1;
2924 int32_t badPointerDistance = INT_MIN; // distance to be corrected
2925
2926 for (uint32_t i = pointerCount; i-- > 0; ) {
2927 int32_t x = mCurrentTouch.pointers[i].x;
2928 int32_t y = mCurrentTouch.pointers[i].y;
2929
2930#if DEBUG_HACKS
2931 LOGD("JumpyTouchFilter: Point %d (%d, %d)", i, x, y);
2932#endif
2933
2934 // Check if a touch point is too close to another's coordinates
2935 bool dropX = false, dropY = false;
2936 for (uint32_t j = 0; j < pointerCount; j++) {
2937 if (i == j) {
2938 continue;
2939 }
2940
2941 if (abs(x - mCurrentTouch.pointers[j].x) <= jumpyEpsilon) {
2942 dropX = true;
2943 break;
2944 }
2945
2946 if (abs(y - mCurrentTouch.pointers[j].y) <= jumpyEpsilon) {
2947 dropY = true;
2948 break;
2949 }
2950 }
2951 if (! dropX && ! dropY) {
2952 continue; // not jumpy
2953 }
2954
2955 // Find a replacement candidate by comparing with older points on the
2956 // complementary (non-jumpy) axis.
2957 int32_t distance = INT_MIN; // distance to be corrected
2958 int32_t replacementIndex = -1;
2959
2960 if (dropX) {
2961 // X looks too close. Find an older replacement point with a close Y.
2962 int32_t smallestDeltaY = INT_MAX;
2963 for (uint32_t j = 0; j < pointerCount; j++) {
2964 int32_t deltaY = abs(y - mLastTouch.pointers[j].y);
2965 if (deltaY < smallestDeltaY) {
2966 smallestDeltaY = deltaY;
2967 replacementIndex = j;
2968 }
2969 }
2970 distance = abs(x - mLastTouch.pointers[replacementIndex].x);
2971 } else {
2972 // Y looks too close. Find an older replacement point with a close X.
2973 int32_t smallestDeltaX = INT_MAX;
2974 for (uint32_t j = 0; j < pointerCount; j++) {
2975 int32_t deltaX = abs(x - mLastTouch.pointers[j].x);
2976 if (deltaX < smallestDeltaX) {
2977 smallestDeltaX = deltaX;
2978 replacementIndex = j;
2979 }
2980 }
2981 distance = abs(y - mLastTouch.pointers[replacementIndex].y);
2982 }
2983
2984 // If replacing this pointer would correct a worse error than the previous ones
2985 // considered, then use this replacement instead.
2986 if (distance > badPointerDistance) {
2987 badPointerIndex = i;
2988 badPointerReplacementIndex = replacementIndex;
2989 badPointerDistance = distance;
2990 }
2991 }
2992
2993 // Correct the jumpy pointer if one was found.
2994 if (badPointerIndex >= 0) {
2995#if DEBUG_HACKS
2996 LOGD("JumpyTouchFilter: Replacing bad pointer %d with (%d, %d)",
2997 badPointerIndex,
2998 mLastTouch.pointers[badPointerReplacementIndex].x,
2999 mLastTouch.pointers[badPointerReplacementIndex].y);
3000#endif
3001
3002 mCurrentTouch.pointers[badPointerIndex].x =
3003 mLastTouch.pointers[badPointerReplacementIndex].x;
3004 mCurrentTouch.pointers[badPointerIndex].y =
3005 mLastTouch.pointers[badPointerReplacementIndex].y;
3006 mJumpyTouchFilter.jumpyPointsDropped += 1;
3007 return true;
3008 }
3009 }
3010
3011 mJumpyTouchFilter.jumpyPointsDropped = 0;
3012 return false;
3013}
3014
3015/* Special hack for devices that have bad screen data: aggregate and
3016 * compute averages of the coordinate data, to reduce the amount of
3017 * jitter seen by applications. */
3018void TouchInputMapper::applyAveragingTouchFilter() {
3019 for (uint32_t currentIndex = 0; currentIndex < mCurrentTouch.pointerCount; currentIndex++) {
3020 uint32_t id = mCurrentTouch.pointers[currentIndex].id;
3021 int32_t x = mCurrentTouch.pointers[currentIndex].x;
3022 int32_t y = mCurrentTouch.pointers[currentIndex].y;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003023 int32_t pressure;
3024 switch (mCalibration.pressureSource) {
3025 case Calibration::PRESSURE_SOURCE_PRESSURE:
3026 pressure = mCurrentTouch.pointers[currentIndex].pressure;
3027 break;
3028 case Calibration::PRESSURE_SOURCE_TOUCH:
3029 pressure = mCurrentTouch.pointers[currentIndex].touchMajor;
3030 break;
3031 default:
3032 pressure = 1;
3033 break;
3034 }
Jeff Browne57e8952010-07-23 21:28:06 -07003035
3036 if (mLastTouch.idBits.hasBit(id)) {
3037 // Pointer was down before and is still down now.
3038 // Compute average over history trace.
3039 uint32_t start = mAveragingTouchFilter.historyStart[id];
3040 uint32_t end = mAveragingTouchFilter.historyEnd[id];
3041
3042 int64_t deltaX = x - mAveragingTouchFilter.historyData[end].pointers[id].x;
3043 int64_t deltaY = y - mAveragingTouchFilter.historyData[end].pointers[id].y;
3044 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3045
3046#if DEBUG_HACKS
3047 LOGD("AveragingTouchFilter: Pointer id %d - Distance from last sample: %lld",
3048 id, distance);
3049#endif
3050
3051 if (distance < AVERAGING_DISTANCE_LIMIT) {
3052 // Increment end index in preparation for recording new historical data.
3053 end += 1;
3054 if (end > AVERAGING_HISTORY_SIZE) {
3055 end = 0;
3056 }
3057
3058 // If the end index has looped back to the start index then we have filled
3059 // the historical trace up to the desired size so we drop the historical
3060 // data at the start of the trace.
3061 if (end == start) {
3062 start += 1;
3063 if (start > AVERAGING_HISTORY_SIZE) {
3064 start = 0;
3065 }
3066 }
3067
3068 // Add the raw data to the historical trace.
3069 mAveragingTouchFilter.historyStart[id] = start;
3070 mAveragingTouchFilter.historyEnd[id] = end;
3071 mAveragingTouchFilter.historyData[end].pointers[id].x = x;
3072 mAveragingTouchFilter.historyData[end].pointers[id].y = y;
3073 mAveragingTouchFilter.historyData[end].pointers[id].pressure = pressure;
3074
3075 // Average over all historical positions in the trace by total pressure.
3076 int32_t averagedX = 0;
3077 int32_t averagedY = 0;
3078 int32_t totalPressure = 0;
3079 for (;;) {
3080 int32_t historicalX = mAveragingTouchFilter.historyData[start].pointers[id].x;
3081 int32_t historicalY = mAveragingTouchFilter.historyData[start].pointers[id].y;
3082 int32_t historicalPressure = mAveragingTouchFilter.historyData[start]
3083 .pointers[id].pressure;
3084
3085 averagedX += historicalX * historicalPressure;
3086 averagedY += historicalY * historicalPressure;
3087 totalPressure += historicalPressure;
3088
3089 if (start == end) {
3090 break;
3091 }
3092
3093 start += 1;
3094 if (start > AVERAGING_HISTORY_SIZE) {
3095 start = 0;
3096 }
3097 }
3098
Jeff Brown38a7fab2010-08-30 03:02:23 -07003099 if (totalPressure != 0) {
3100 averagedX /= totalPressure;
3101 averagedY /= totalPressure;
Jeff Browne57e8952010-07-23 21:28:06 -07003102
3103#if DEBUG_HACKS
Jeff Brown38a7fab2010-08-30 03:02:23 -07003104 LOGD("AveragingTouchFilter: Pointer id %d - "
3105 "totalPressure=%d, averagedX=%d, averagedY=%d", id, totalPressure,
3106 averagedX, averagedY);
Jeff Browne57e8952010-07-23 21:28:06 -07003107#endif
3108
Jeff Brown38a7fab2010-08-30 03:02:23 -07003109 mCurrentTouch.pointers[currentIndex].x = averagedX;
3110 mCurrentTouch.pointers[currentIndex].y = averagedY;
3111 }
Jeff Browne57e8952010-07-23 21:28:06 -07003112 } else {
3113#if DEBUG_HACKS
3114 LOGD("AveragingTouchFilter: Pointer id %d - Exceeded max distance", id);
3115#endif
3116 }
3117 } else {
3118#if DEBUG_HACKS
3119 LOGD("AveragingTouchFilter: Pointer id %d - Pointer went up", id);
3120#endif
3121 }
3122
3123 // Reset pointer history.
3124 mAveragingTouchFilter.historyStart[id] = 0;
3125 mAveragingTouchFilter.historyEnd[id] = 0;
3126 mAveragingTouchFilter.historyData[0].pointers[id].x = x;
3127 mAveragingTouchFilter.historyData[0].pointers[id].y = y;
3128 mAveragingTouchFilter.historyData[0].pointers[id].pressure = pressure;
3129 }
3130}
3131
3132int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownb51719b2010-07-29 18:18:33 -07003133 { // acquire lock
3134 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07003135
Jeff Brownb51719b2010-07-29 18:18:33 -07003136 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07003137 return AKEY_STATE_VIRTUAL;
3138 }
3139
Jeff Brownb51719b2010-07-29 18:18:33 -07003140 size_t numVirtualKeys = mLocked.virtualKeys.size();
3141 for (size_t i = 0; i < numVirtualKeys; i++) {
3142 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07003143 if (virtualKey.keyCode == keyCode) {
3144 return AKEY_STATE_UP;
3145 }
3146 }
Jeff Brownb51719b2010-07-29 18:18:33 -07003147 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07003148
3149 return AKEY_STATE_UNKNOWN;
3150}
3151
3152int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownb51719b2010-07-29 18:18:33 -07003153 { // acquire lock
3154 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07003155
Jeff Brownb51719b2010-07-29 18:18:33 -07003156 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) {
Jeff Browne57e8952010-07-23 21:28:06 -07003157 return AKEY_STATE_VIRTUAL;
3158 }
3159
Jeff Brownb51719b2010-07-29 18:18:33 -07003160 size_t numVirtualKeys = mLocked.virtualKeys.size();
3161 for (size_t i = 0; i < numVirtualKeys; i++) {
3162 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07003163 if (virtualKey.scanCode == scanCode) {
3164 return AKEY_STATE_UP;
3165 }
3166 }
Jeff Brownb51719b2010-07-29 18:18:33 -07003167 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07003168
3169 return AKEY_STATE_UNKNOWN;
3170}
3171
3172bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
3173 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownb51719b2010-07-29 18:18:33 -07003174 { // acquire lock
3175 AutoMutex _l(mLock);
Jeff Browne57e8952010-07-23 21:28:06 -07003176
Jeff Brownb51719b2010-07-29 18:18:33 -07003177 size_t numVirtualKeys = mLocked.virtualKeys.size();
3178 for (size_t i = 0; i < numVirtualKeys; i++) {
3179 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Browne57e8952010-07-23 21:28:06 -07003180
3181 for (size_t i = 0; i < numCodes; i++) {
3182 if (virtualKey.keyCode == keyCodes[i]) {
3183 outFlags[i] = 1;
3184 }
3185 }
3186 }
Jeff Brownb51719b2010-07-29 18:18:33 -07003187 } // release lock
Jeff Browne57e8952010-07-23 21:28:06 -07003188
3189 return true;
3190}
3191
3192
3193// --- SingleTouchInputMapper ---
3194
Jeff Brown66888372010-11-29 17:37:49 -08003195SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
3196 TouchInputMapper(device) {
Jeff Browne57e8952010-07-23 21:28:06 -07003197 initialize();
3198}
3199
3200SingleTouchInputMapper::~SingleTouchInputMapper() {
3201}
3202
3203void SingleTouchInputMapper::initialize() {
3204 mAccumulator.clear();
3205
3206 mDown = false;
3207 mX = 0;
3208 mY = 0;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003209 mPressure = 0; // default to 0 for devices that don't report pressure
3210 mToolWidth = 0; // default to 0 for devices that don't report tool width
Jeff Browne57e8952010-07-23 21:28:06 -07003211}
3212
3213void SingleTouchInputMapper::reset() {
3214 TouchInputMapper::reset();
3215
Jeff Browne57e8952010-07-23 21:28:06 -07003216 initialize();
3217 }
3218
3219void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
3220 switch (rawEvent->type) {
3221 case EV_KEY:
3222 switch (rawEvent->scanCode) {
3223 case BTN_TOUCH:
3224 mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH;
3225 mAccumulator.btnTouch = rawEvent->value != 0;
Jeff Brownd64c8552010-08-17 20:38:35 -07003226 // Don't sync immediately. Wait until the next SYN_REPORT since we might
3227 // not have received valid position information yet. This logic assumes that
3228 // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet.
Jeff Browne57e8952010-07-23 21:28:06 -07003229 break;
3230 }
3231 break;
3232
3233 case EV_ABS:
3234 switch (rawEvent->scanCode) {
3235 case ABS_X:
3236 mAccumulator.fields |= Accumulator::FIELD_ABS_X;
3237 mAccumulator.absX = rawEvent->value;
3238 break;
3239 case ABS_Y:
3240 mAccumulator.fields |= Accumulator::FIELD_ABS_Y;
3241 mAccumulator.absY = rawEvent->value;
3242 break;
3243 case ABS_PRESSURE:
3244 mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE;
3245 mAccumulator.absPressure = rawEvent->value;
3246 break;
3247 case ABS_TOOL_WIDTH:
3248 mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH;
3249 mAccumulator.absToolWidth = rawEvent->value;
3250 break;
3251 }
3252 break;
3253
3254 case EV_SYN:
3255 switch (rawEvent->scanCode) {
3256 case SYN_REPORT:
Jeff Brownd64c8552010-08-17 20:38:35 -07003257 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07003258 break;
3259 }
3260 break;
3261 }
3262}
3263
3264void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Browne57e8952010-07-23 21:28:06 -07003265 uint32_t fields = mAccumulator.fields;
Jeff Brownd64c8552010-08-17 20:38:35 -07003266 if (fields == 0) {
3267 return; // no new state changes, so nothing to do
3268 }
Jeff Browne57e8952010-07-23 21:28:06 -07003269
3270 if (fields & Accumulator::FIELD_BTN_TOUCH) {
3271 mDown = mAccumulator.btnTouch;
3272 }
3273
3274 if (fields & Accumulator::FIELD_ABS_X) {
3275 mX = mAccumulator.absX;
3276 }
3277
3278 if (fields & Accumulator::FIELD_ABS_Y) {
3279 mY = mAccumulator.absY;
3280 }
3281
3282 if (fields & Accumulator::FIELD_ABS_PRESSURE) {
3283 mPressure = mAccumulator.absPressure;
3284 }
3285
3286 if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003287 mToolWidth = mAccumulator.absToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -07003288 }
3289
3290 mCurrentTouch.clear();
3291
3292 if (mDown) {
3293 mCurrentTouch.pointerCount = 1;
3294 mCurrentTouch.pointers[0].id = 0;
3295 mCurrentTouch.pointers[0].x = mX;
3296 mCurrentTouch.pointers[0].y = mY;
3297 mCurrentTouch.pointers[0].pressure = mPressure;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003298 mCurrentTouch.pointers[0].touchMajor = 0;
3299 mCurrentTouch.pointers[0].touchMinor = 0;
3300 mCurrentTouch.pointers[0].toolMajor = mToolWidth;
3301 mCurrentTouch.pointers[0].toolMinor = mToolWidth;
Jeff Browne57e8952010-07-23 21:28:06 -07003302 mCurrentTouch.pointers[0].orientation = 0;
3303 mCurrentTouch.idToIndex[0] = 0;
3304 mCurrentTouch.idBits.markBit(0);
3305 }
3306
3307 syncTouch(when, true);
Jeff Brownd64c8552010-08-17 20:38:35 -07003308
3309 mAccumulator.clear();
Jeff Browne57e8952010-07-23 21:28:06 -07003310}
3311
Jeff Brown38a7fab2010-08-30 03:02:23 -07003312void SingleTouchInputMapper::configureRawAxes() {
3313 TouchInputMapper::configureRawAxes();
Jeff Browne57e8952010-07-23 21:28:06 -07003314
Jeff Brown38a7fab2010-08-30 03:02:23 -07003315 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x);
3316 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y);
3317 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure);
3318 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor);
Jeff Browne57e8952010-07-23 21:28:06 -07003319}
3320
3321
3322// --- MultiTouchInputMapper ---
3323
Jeff Brown66888372010-11-29 17:37:49 -08003324MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
3325 TouchInputMapper(device) {
Jeff Browne57e8952010-07-23 21:28:06 -07003326 initialize();
3327}
3328
3329MultiTouchInputMapper::~MultiTouchInputMapper() {
3330}
3331
3332void MultiTouchInputMapper::initialize() {
3333 mAccumulator.clear();
3334}
3335
3336void MultiTouchInputMapper::reset() {
3337 TouchInputMapper::reset();
3338
Jeff Browne57e8952010-07-23 21:28:06 -07003339 initialize();
3340}
3341
3342void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
3343 switch (rawEvent->type) {
3344 case EV_ABS: {
3345 uint32_t pointerIndex = mAccumulator.pointerCount;
3346 Accumulator::Pointer* pointer = & mAccumulator.pointers[pointerIndex];
3347
3348 switch (rawEvent->scanCode) {
3349 case ABS_MT_POSITION_X:
3350 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_X;
3351 pointer->absMTPositionX = rawEvent->value;
3352 break;
3353 case ABS_MT_POSITION_Y:
3354 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y;
3355 pointer->absMTPositionY = rawEvent->value;
3356 break;
3357 case ABS_MT_TOUCH_MAJOR:
3358 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR;
3359 pointer->absMTTouchMajor = rawEvent->value;
3360 break;
3361 case ABS_MT_TOUCH_MINOR:
3362 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR;
3363 pointer->absMTTouchMinor = rawEvent->value;
3364 break;
3365 case ABS_MT_WIDTH_MAJOR:
3366 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR;
3367 pointer->absMTWidthMajor = rawEvent->value;
3368 break;
3369 case ABS_MT_WIDTH_MINOR:
3370 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR;
3371 pointer->absMTWidthMinor = rawEvent->value;
3372 break;
3373 case ABS_MT_ORIENTATION:
3374 pointer->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION;
3375 pointer->absMTOrientation = rawEvent->value;
3376 break;
3377 case ABS_MT_TRACKING_ID:
3378 pointer->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID;
3379 pointer->absMTTrackingId = rawEvent->value;
3380 break;
Jeff Brown38a7fab2010-08-30 03:02:23 -07003381 case ABS_MT_PRESSURE:
3382 pointer->fields |= Accumulator::FIELD_ABS_MT_PRESSURE;
3383 pointer->absMTPressure = rawEvent->value;
3384 break;
Jeff Browne57e8952010-07-23 21:28:06 -07003385 }
3386 break;
3387 }
3388
3389 case EV_SYN:
3390 switch (rawEvent->scanCode) {
3391 case SYN_MT_REPORT: {
3392 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
3393 uint32_t pointerIndex = mAccumulator.pointerCount;
3394
3395 if (mAccumulator.pointers[pointerIndex].fields) {
3396 if (pointerIndex == MAX_POINTERS) {
3397 LOGW("MultiTouch device driver returned more than maximum of %d pointers.",
3398 MAX_POINTERS);
3399 } else {
3400 pointerIndex += 1;
3401 mAccumulator.pointerCount = pointerIndex;
3402 }
3403 }
3404
3405 mAccumulator.pointers[pointerIndex].clear();
3406 break;
3407 }
3408
3409 case SYN_REPORT:
Jeff Brownd64c8552010-08-17 20:38:35 -07003410 sync(rawEvent->when);
Jeff Browne57e8952010-07-23 21:28:06 -07003411 break;
3412 }
3413 break;
3414 }
3415}
3416
3417void MultiTouchInputMapper::sync(nsecs_t when) {
3418 static const uint32_t REQUIRED_FIELDS =
Jeff Brown38a7fab2010-08-30 03:02:23 -07003419 Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y;
Jeff Browne839a582010-04-22 18:58:52 -07003420
Jeff Browne57e8952010-07-23 21:28:06 -07003421 uint32_t inCount = mAccumulator.pointerCount;
3422 uint32_t outCount = 0;
3423 bool havePointerIds = true;
Jeff Browne839a582010-04-22 18:58:52 -07003424
Jeff Browne57e8952010-07-23 21:28:06 -07003425 mCurrentTouch.clear();
Jeff Browne839a582010-04-22 18:58:52 -07003426
Jeff Browne57e8952010-07-23 21:28:06 -07003427 for (uint32_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003428 const Accumulator::Pointer& inPointer = mAccumulator.pointers[inIndex];
3429 uint32_t fields = inPointer.fields;
Jeff Browne839a582010-04-22 18:58:52 -07003430
Jeff Browne57e8952010-07-23 21:28:06 -07003431 if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003432 // Some drivers send empty MT sync packets without X / Y to indicate a pointer up.
3433 // Drop this finger.
Jeff Browne839a582010-04-22 18:58:52 -07003434 continue;
3435 }
3436
Jeff Brownd64c8552010-08-17 20:38:35 -07003437 PointerData& outPointer = mCurrentTouch.pointers[outCount];
3438 outPointer.x = inPointer.absMTPositionX;
3439 outPointer.y = inPointer.absMTPositionY;
Jeff Browne839a582010-04-22 18:58:52 -07003440
Jeff Brown38a7fab2010-08-30 03:02:23 -07003441 if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) {
3442 if (inPointer.absMTPressure <= 0) {
Jeff Brown3c3cc622010-10-20 15:33:38 -07003443 // Some devices send sync packets with X / Y but with a 0 pressure to indicate
3444 // a pointer going up. Drop this finger.
Jeff Brownd64c8552010-08-17 20:38:35 -07003445 continue;
3446 }
Jeff Brown38a7fab2010-08-30 03:02:23 -07003447 outPointer.pressure = inPointer.absMTPressure;
3448 } else {
3449 // Default pressure to 0 if absent.
3450 outPointer.pressure = 0;
3451 }
3452
3453 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) {
3454 if (inPointer.absMTTouchMajor <= 0) {
3455 // Some devices send sync packets with X / Y but with a 0 touch major to indicate
3456 // a pointer going up. Drop this finger.
3457 continue;
3458 }
Jeff Brownd64c8552010-08-17 20:38:35 -07003459 outPointer.touchMajor = inPointer.absMTTouchMajor;
3460 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003461 // Default touch area to 0 if absent.
Jeff Brownd64c8552010-08-17 20:38:35 -07003462 outPointer.touchMajor = 0;
3463 }
Jeff Browne839a582010-04-22 18:58:52 -07003464
Jeff Brownd64c8552010-08-17 20:38:35 -07003465 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) {
3466 outPointer.touchMinor = inPointer.absMTTouchMinor;
3467 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003468 // Assume touch area is circular.
Jeff Brownd64c8552010-08-17 20:38:35 -07003469 outPointer.touchMinor = outPointer.touchMajor;
3470 }
Jeff Browne839a582010-04-22 18:58:52 -07003471
Jeff Brownd64c8552010-08-17 20:38:35 -07003472 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) {
3473 outPointer.toolMajor = inPointer.absMTWidthMajor;
3474 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003475 // Default tool area to 0 if absent.
3476 outPointer.toolMajor = 0;
Jeff Brownd64c8552010-08-17 20:38:35 -07003477 }
Jeff Browne839a582010-04-22 18:58:52 -07003478
Jeff Brownd64c8552010-08-17 20:38:35 -07003479 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) {
3480 outPointer.toolMinor = inPointer.absMTWidthMinor;
3481 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003482 // Assume tool area is circular.
Jeff Brownd64c8552010-08-17 20:38:35 -07003483 outPointer.toolMinor = outPointer.toolMajor;
3484 }
3485
3486 if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) {
3487 outPointer.orientation = inPointer.absMTOrientation;
3488 } else {
Jeff Brown38a7fab2010-08-30 03:02:23 -07003489 // Default orientation to vertical if absent.
Jeff Brownd64c8552010-08-17 20:38:35 -07003490 outPointer.orientation = 0;
3491 }
3492
Jeff Brown38a7fab2010-08-30 03:02:23 -07003493 // Assign pointer id using tracking id if available.
Jeff Browne57e8952010-07-23 21:28:06 -07003494 if (havePointerIds) {
Jeff Brownd64c8552010-08-17 20:38:35 -07003495 if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) {
3496 uint32_t id = uint32_t(inPointer.absMTTrackingId);
Jeff Browne839a582010-04-22 18:58:52 -07003497
Jeff Browne57e8952010-07-23 21:28:06 -07003498 if (id > MAX_POINTER_ID) {
3499#if DEBUG_POINTERS
3500 LOGD("Pointers: Ignoring driver provided pointer id %d because "
Jeff Brownd1b0a2b2010-09-26 22:20:12 -07003501 "it is larger than max supported id %d",
Jeff Browne57e8952010-07-23 21:28:06 -07003502 id, MAX_POINTER_ID);
3503#endif
3504 havePointerIds = false;
3505 }
3506 else {
Jeff Brownd64c8552010-08-17 20:38:35 -07003507 outPointer.id = id;
Jeff Browne57e8952010-07-23 21:28:06 -07003508 mCurrentTouch.idToIndex[id] = outCount;
3509 mCurrentTouch.idBits.markBit(id);
3510 }
3511 } else {
3512 havePointerIds = false;
Jeff Browne839a582010-04-22 18:58:52 -07003513 }
3514 }
Jeff Browne839a582010-04-22 18:58:52 -07003515
Jeff Browne57e8952010-07-23 21:28:06 -07003516 outCount += 1;
Jeff Browne839a582010-04-22 18:58:52 -07003517 }
3518
Jeff Browne57e8952010-07-23 21:28:06 -07003519 mCurrentTouch.pointerCount = outCount;
Jeff Browne839a582010-04-22 18:58:52 -07003520
Jeff Browne57e8952010-07-23 21:28:06 -07003521 syncTouch(when, havePointerIds);
Jeff Brownd64c8552010-08-17 20:38:35 -07003522
3523 mAccumulator.clear();
Jeff Browne839a582010-04-22 18:58:52 -07003524}
3525
Jeff Brown38a7fab2010-08-30 03:02:23 -07003526void MultiTouchInputMapper::configureRawAxes() {
3527 TouchInputMapper::configureRawAxes();
Jeff Browne839a582010-04-22 18:58:52 -07003528
Jeff Brown38a7fab2010-08-30 03:02:23 -07003529 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, & mRawAxes.x);
3530 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, & mRawAxes.y);
3531 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, & mRawAxes.touchMajor);
3532 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, & mRawAxes.touchMinor);
3533 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, & mRawAxes.toolMajor);
3534 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, & mRawAxes.toolMinor);
3535 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, & mRawAxes.orientation);
3536 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, & mRawAxes.pressure);
Jeff Brown54bc2812010-06-15 01:31:58 -07003537}
3538
Jeff Browne839a582010-04-22 18:58:52 -07003539
3540} // namespace android