blob: 7a45de6fb35a8d1fdd46a3ee10035d81703b4907 [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Brown46b9ac02010-04-22 18:58:52 -070017#define LOG_TAG "InputReader"
18
19//#define LOG_NDEBUG 0
20
21// Log debug messages for each raw event received from the EventHub.
22#define DEBUG_RAW_EVENTS 0
23
24// Log debug messages about touch screen filtering hacks.
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_HACKS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070026
27// Log debug messages about virtual key processing.
Jeff Brown349703e2010-06-22 01:27:15 -070028#define DEBUG_VIRTUAL_KEYS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070029
30// Log debug messages about pointers.
Jeff Brown349703e2010-06-22 01:27:15 -070031#define DEBUG_POINTERS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070032
Jeff Brown5c225b12010-06-16 01:53:36 -070033// Log debug messages about pointer assignment calculations.
34#define DEBUG_POINTER_ASSIGNMENT 0
35
Jeff Brownb4ff35d2011-01-02 16:37:43 -080036#include "InputReader.h"
37
Jeff Brown46b9ac02010-04-22 18:58:52 -070038#include <cutils/log.h>
Jeff Brown6b53e8d2010-11-10 16:03:06 -080039#include <ui/Keyboard.h>
Jeff Brown90655042010-12-02 13:50:46 -080040#include <ui/VirtualKeyMap.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070041
42#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070043#include <stdlib.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070044#include <unistd.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070045#include <errno.h>
46#include <limits.h>
Jeff Brownc5ed5912010-07-14 18:48:53 -070047#include <math.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070048
Jeff Brown8d608662010-08-30 03:02:23 -070049#define INDENT " "
Jeff Brownef3d7e82010-09-30 14:33:04 -070050#define INDENT2 " "
51#define INDENT3 " "
52#define INDENT4 " "
Jeff Brown8d608662010-08-30 03:02:23 -070053
Jeff Brown46b9ac02010-04-22 18:58:52 -070054namespace android {
55
56// --- Static Functions ---
57
58template<typename T>
59inline static T abs(const T& value) {
60 return value < 0 ? - value : value;
61}
62
63template<typename T>
64inline static T min(const T& a, const T& b) {
65 return a < b ? a : b;
66}
67
Jeff Brown5c225b12010-06-16 01:53:36 -070068template<typename T>
69inline static void swap(T& a, T& b) {
70 T temp = a;
71 a = b;
72 b = temp;
73}
74
Jeff Brown8d608662010-08-30 03:02:23 -070075inline static float avg(float x, float y) {
76 return (x + y) / 2;
77}
78
79inline static float pythag(float x, float y) {
80 return sqrtf(x * x + y * y);
81}
82
Jeff Brownef3d7e82010-09-30 14:33:04 -070083static inline const char* toString(bool value) {
84 return value ? "true" : "false";
85}
86
Jeff Brown46b9ac02010-04-22 18:58:52 -070087static const int32_t keyCodeRotationMap[][4] = {
88 // key codes enumerated counter-clockwise with the original (unrotated) key first
89 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd035822010-06-30 16:10:35 -070090 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
91 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
92 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
93 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac02010-04-22 18:58:52 -070094};
95static const int keyCodeRotationMapSize =
96 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
97
98int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -080099 if (orientation != DISPLAY_ORIENTATION_0) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700100 for (int i = 0; i < keyCodeRotationMapSize; i++) {
101 if (keyCode == keyCodeRotationMap[i][0]) {
102 return keyCodeRotationMap[i][orientation];
103 }
104 }
105 }
106 return keyCode;
107}
108
Jeff Brown6d0fec22010-07-23 21:28:06 -0700109static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
110 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
111}
112
Jeff Brown46b9ac02010-04-22 18:58:52 -0700113
Jeff Brown46b9ac02010-04-22 18:58:52 -0700114// --- InputReader ---
115
116InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700117 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700118 const sp<InputDispatcherInterface>& dispatcher) :
Jeff Brown6d0fec22010-07-23 21:28:06 -0700119 mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher),
120 mGlobalMetaState(0) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700121 configureExcludedDevices();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700122 updateGlobalMetaState();
123 updateInputConfiguration();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700124}
125
126InputReader::~InputReader() {
127 for (size_t i = 0; i < mDevices.size(); i++) {
128 delete mDevices.valueAt(i);
129 }
130}
131
132void InputReader::loopOnce() {
133 RawEvent rawEvent;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700134 mEventHub->getEvent(& rawEvent);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700135
136#if DEBUG_RAW_EVENTS
Jeff Brown90655042010-12-02 13:50:46 -0800137 LOGD("Input event: device=%d type=0x%x scancode=%d keycode=%d value=%d",
Jeff Brown46b9ac02010-04-22 18:58:52 -0700138 rawEvent.deviceId, rawEvent.type, rawEvent.scanCode, rawEvent.keyCode,
139 rawEvent.value);
140#endif
141
142 process(& rawEvent);
143}
144
145void InputReader::process(const RawEvent* rawEvent) {
146 switch (rawEvent->type) {
147 case EventHubInterface::DEVICE_ADDED:
Jeff Brown7342bb92010-10-01 18:55:43 -0700148 addDevice(rawEvent->deviceId);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700149 break;
150
151 case EventHubInterface::DEVICE_REMOVED:
Jeff Brown7342bb92010-10-01 18:55:43 -0700152 removeDevice(rawEvent->deviceId);
153 break;
154
155 case EventHubInterface::FINISHED_DEVICE_SCAN:
Jeff Brownc3db8582010-10-20 15:33:38 -0700156 handleConfigurationChanged(rawEvent->when);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700157 break;
158
Jeff Brown6d0fec22010-07-23 21:28:06 -0700159 default:
160 consumeEvent(rawEvent);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700161 break;
162 }
163}
164
Jeff Brown7342bb92010-10-01 18:55:43 -0700165void InputReader::addDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700166 String8 name = mEventHub->getDeviceName(deviceId);
167 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
168
169 InputDevice* device = createDevice(deviceId, name, classes);
170 device->configure();
171
Jeff Brown8d608662010-08-30 03:02:23 -0700172 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800173 LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700174 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800175 LOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700176 device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700177 }
178
Jeff Brown6d0fec22010-07-23 21:28:06 -0700179 bool added = false;
180 { // acquire device registry writer lock
181 RWLock::AutoWLock _wl(mDeviceRegistryLock);
182
183 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
184 if (deviceIndex < 0) {
185 mDevices.add(deviceId, device);
186 added = true;
187 }
188 } // release device registry writer lock
189
190 if (! added) {
191 LOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
192 delete device;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700193 return;
194 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700195}
196
Jeff Brown7342bb92010-10-01 18:55:43 -0700197void InputReader::removeDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700198 bool removed = false;
199 InputDevice* device = NULL;
200 { // acquire device registry writer lock
201 RWLock::AutoWLock _wl(mDeviceRegistryLock);
202
203 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
204 if (deviceIndex >= 0) {
205 device = mDevices.valueAt(deviceIndex);
206 mDevices.removeItemsAt(deviceIndex, 1);
207 removed = true;
208 }
209 } // release device registry writer lock
210
211 if (! removed) {
212 LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700213 return;
214 }
215
Jeff Brown6d0fec22010-07-23 21:28:06 -0700216 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800217 LOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700218 device->getId(), device->getName().string());
219 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800220 LOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700221 device->getId(), device->getName().string(), device->getSources());
222 }
223
Jeff Brown8d608662010-08-30 03:02:23 -0700224 device->reset();
225
Jeff Brown6d0fec22010-07-23 21:28:06 -0700226 delete device;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700227}
228
Jeff Brown6d0fec22010-07-23 21:28:06 -0700229InputDevice* InputReader::createDevice(int32_t deviceId, const String8& name, uint32_t classes) {
230 InputDevice* device = new InputDevice(this, deviceId, name);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700231
Jeff Brown6d0fec22010-07-23 21:28:06 -0700232 // Switch-like devices.
233 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
234 device->addMapper(new SwitchInputMapper(device));
235 }
236
237 // Keyboard-like devices.
238 uint32_t keyboardSources = 0;
239 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
240 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
241 keyboardSources |= AINPUT_SOURCE_KEYBOARD;
242 }
243 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
244 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
245 }
246 if (classes & INPUT_DEVICE_CLASS_DPAD) {
247 keyboardSources |= AINPUT_SOURCE_DPAD;
248 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700249
250 if (keyboardSources != 0) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800251 device->addMapper(new KeyboardInputMapper(device, keyboardSources, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700252 }
253
Jeff Brown83c09682010-12-23 17:50:18 -0800254 // Cursor-like devices.
255 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
256 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700257 }
258
259 // Touchscreen-like devices.
260 if (classes & INPUT_DEVICE_CLASS_TOUCHSCREEN_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800261 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700262 } else if (classes & INPUT_DEVICE_CLASS_TOUCHSCREEN) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800263 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700264 }
265
266 return device;
267}
268
269void InputReader::consumeEvent(const RawEvent* rawEvent) {
270 int32_t deviceId = rawEvent->deviceId;
271
272 { // acquire device registry reader lock
273 RWLock::AutoRLock _rl(mDeviceRegistryLock);
274
275 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
276 if (deviceIndex < 0) {
277 LOGW("Discarding event for unknown deviceId %d.", deviceId);
278 return;
279 }
280
281 InputDevice* device = mDevices.valueAt(deviceIndex);
282 if (device->isIgnored()) {
283 //LOGD("Discarding event for ignored deviceId %d.", deviceId);
284 return;
285 }
286
287 device->process(rawEvent);
288 } // release device registry reader lock
289}
290
Jeff Brownc3db8582010-10-20 15:33:38 -0700291void InputReader::handleConfigurationChanged(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700292 // Reset global meta state because it depends on the list of all configured devices.
293 updateGlobalMetaState();
294
295 // Update input configuration.
296 updateInputConfiguration();
297
298 // Enqueue configuration changed.
299 mDispatcher->notifyConfigurationChanged(when);
300}
301
302void InputReader::configureExcludedDevices() {
303 Vector<String8> excludedDeviceNames;
304 mPolicy->getExcludedDeviceNames(excludedDeviceNames);
305
306 for (size_t i = 0; i < excludedDeviceNames.size(); i++) {
307 mEventHub->addExcludedDevice(excludedDeviceNames[i]);
308 }
309}
310
311void InputReader::updateGlobalMetaState() {
312 { // acquire state lock
313 AutoMutex _l(mStateLock);
314
315 mGlobalMetaState = 0;
316
317 { // acquire device registry reader lock
318 RWLock::AutoRLock _rl(mDeviceRegistryLock);
319
320 for (size_t i = 0; i < mDevices.size(); i++) {
321 InputDevice* device = mDevices.valueAt(i);
322 mGlobalMetaState |= device->getMetaState();
323 }
324 } // release device registry reader lock
325 } // release state lock
326}
327
328int32_t InputReader::getGlobalMetaState() {
329 { // acquire state lock
330 AutoMutex _l(mStateLock);
331
332 return mGlobalMetaState;
333 } // release state lock
334}
335
336void InputReader::updateInputConfiguration() {
337 { // acquire state lock
338 AutoMutex _l(mStateLock);
339
340 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
341 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
342 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
343 { // acquire device registry reader lock
344 RWLock::AutoRLock _rl(mDeviceRegistryLock);
345
346 InputDeviceInfo deviceInfo;
347 for (size_t i = 0; i < mDevices.size(); i++) {
348 InputDevice* device = mDevices.valueAt(i);
349 device->getDeviceInfo(& deviceInfo);
350 uint32_t sources = deviceInfo.getSources();
351
352 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
353 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
354 }
355 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
356 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
357 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
358 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
359 }
360 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
361 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700362 }
363 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700364 } // release device registry reader lock
Jeff Brown46b9ac02010-04-22 18:58:52 -0700365
Jeff Brown6d0fec22010-07-23 21:28:06 -0700366 mInputConfiguration.touchScreen = touchScreenConfig;
367 mInputConfiguration.keyboard = keyboardConfig;
368 mInputConfiguration.navigation = navigationConfig;
369 } // release state lock
370}
371
372void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
373 { // acquire state lock
374 AutoMutex _l(mStateLock);
375
376 *outConfiguration = mInputConfiguration;
377 } // release state lock
378}
379
380status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
381 { // acquire device registry reader lock
382 RWLock::AutoRLock _rl(mDeviceRegistryLock);
383
384 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
385 if (deviceIndex < 0) {
386 return NAME_NOT_FOUND;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700387 }
388
Jeff Brown6d0fec22010-07-23 21:28:06 -0700389 InputDevice* device = mDevices.valueAt(deviceIndex);
390 if (device->isIgnored()) {
391 return NAME_NOT_FOUND;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700392 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700393
394 device->getDeviceInfo(outDeviceInfo);
395 return OK;
396 } // release device registy reader lock
397}
398
399void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
400 outDeviceIds.clear();
401
402 { // acquire device registry reader lock
403 RWLock::AutoRLock _rl(mDeviceRegistryLock);
404
405 size_t numDevices = mDevices.size();
406 for (size_t i = 0; i < numDevices; i++) {
407 InputDevice* device = mDevices.valueAt(i);
408 if (! device->isIgnored()) {
409 outDeviceIds.add(device->getId());
410 }
411 }
412 } // release device registy reader lock
413}
414
415int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
416 int32_t keyCode) {
417 return getState(deviceId, sourceMask, keyCode, & InputDevice::getKeyCodeState);
418}
419
420int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
421 int32_t scanCode) {
422 return getState(deviceId, sourceMask, scanCode, & InputDevice::getScanCodeState);
423}
424
425int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
426 return getState(deviceId, sourceMask, switchCode, & InputDevice::getSwitchState);
427}
428
429int32_t InputReader::getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
430 GetStateFunc getStateFunc) {
431 { // acquire device registry reader lock
432 RWLock::AutoRLock _rl(mDeviceRegistryLock);
433
434 int32_t result = AKEY_STATE_UNKNOWN;
435 if (deviceId >= 0) {
436 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
437 if (deviceIndex >= 0) {
438 InputDevice* device = mDevices.valueAt(deviceIndex);
439 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
440 result = (device->*getStateFunc)(sourceMask, code);
441 }
442 }
443 } else {
444 size_t numDevices = mDevices.size();
445 for (size_t i = 0; i < numDevices; i++) {
446 InputDevice* device = mDevices.valueAt(i);
447 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
448 result = (device->*getStateFunc)(sourceMask, code);
449 if (result >= AKEY_STATE_DOWN) {
450 return result;
451 }
452 }
453 }
454 }
455 return result;
456 } // release device registy reader lock
457}
458
459bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
460 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
461 memset(outFlags, 0, numCodes);
462 return markSupportedKeyCodes(deviceId, sourceMask, numCodes, keyCodes, outFlags);
463}
464
465bool InputReader::markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
466 const int32_t* keyCodes, uint8_t* outFlags) {
467 { // acquire device registry reader lock
468 RWLock::AutoRLock _rl(mDeviceRegistryLock);
469 bool result = false;
470 if (deviceId >= 0) {
471 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
472 if (deviceIndex >= 0) {
473 InputDevice* device = mDevices.valueAt(deviceIndex);
474 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
475 result = device->markSupportedKeyCodes(sourceMask,
476 numCodes, keyCodes, outFlags);
477 }
478 }
479 } else {
480 size_t numDevices = mDevices.size();
481 for (size_t i = 0; i < numDevices; i++) {
482 InputDevice* device = mDevices.valueAt(i);
483 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
484 result |= device->markSupportedKeyCodes(sourceMask,
485 numCodes, keyCodes, outFlags);
486 }
487 }
488 }
489 return result;
490 } // release device registy reader lock
491}
492
Jeff Brownb88102f2010-09-08 11:49:43 -0700493void InputReader::dump(String8& dump) {
Jeff Brownf2f48712010-10-01 17:46:21 -0700494 mEventHub->dump(dump);
495 dump.append("\n");
496
497 dump.append("Input Reader State:\n");
498
Jeff Brownef3d7e82010-09-30 14:33:04 -0700499 { // acquire device registry reader lock
500 RWLock::AutoRLock _rl(mDeviceRegistryLock);
Jeff Brownb88102f2010-09-08 11:49:43 -0700501
Jeff Brownef3d7e82010-09-30 14:33:04 -0700502 for (size_t i = 0; i < mDevices.size(); i++) {
503 mDevices.valueAt(i)->dump(dump);
Jeff Brownb88102f2010-09-08 11:49:43 -0700504 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700505 } // release device registy reader lock
Jeff Brownb88102f2010-09-08 11:49:43 -0700506}
507
Jeff Brown6d0fec22010-07-23 21:28:06 -0700508
509// --- InputReaderThread ---
510
511InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
512 Thread(/*canCallJava*/ true), mReader(reader) {
513}
514
515InputReaderThread::~InputReaderThread() {
516}
517
518bool InputReaderThread::threadLoop() {
519 mReader->loopOnce();
520 return true;
521}
522
523
524// --- InputDevice ---
525
526InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) :
527 mContext(context), mId(id), mName(name), mSources(0) {
528}
529
530InputDevice::~InputDevice() {
531 size_t numMappers = mMappers.size();
532 for (size_t i = 0; i < numMappers; i++) {
533 delete mMappers[i];
534 }
535 mMappers.clear();
536}
537
Jeff Brownef3d7e82010-09-30 14:33:04 -0700538static void dumpMotionRange(String8& dump, const InputDeviceInfo& deviceInfo,
539 int32_t rangeType, const char* name) {
540 const InputDeviceInfo::MotionRange* range = deviceInfo.getMotionRange(rangeType);
541 if (range) {
542 dump.appendFormat(INDENT3 "%s: min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
543 name, range->min, range->max, range->flat, range->fuzz);
544 }
545}
546
547void InputDevice::dump(String8& dump) {
548 InputDeviceInfo deviceInfo;
549 getDeviceInfo(& deviceInfo);
550
Jeff Brown90655042010-12-02 13:50:46 -0800551 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700552 deviceInfo.getName().string());
553 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
554 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
555 if (!deviceInfo.getMotionRanges().isEmpty()) {
556 dump.append(INDENT2 "Motion Ranges:\n");
557 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_X, "X");
558 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_Y, "Y");
559 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_PRESSURE, "Pressure");
560 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_SIZE, "Size");
561 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOUCH_MAJOR, "TouchMajor");
562 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOUCH_MINOR, "TouchMinor");
563 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOOL_MAJOR, "ToolMajor");
564 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_TOOL_MINOR, "ToolMinor");
565 dumpMotionRange(dump, deviceInfo, AINPUT_MOTION_RANGE_ORIENTATION, "Orientation");
566 }
567
568 size_t numMappers = mMappers.size();
569 for (size_t i = 0; i < numMappers; i++) {
570 InputMapper* mapper = mMappers[i];
571 mapper->dump(dump);
572 }
573}
574
Jeff Brown6d0fec22010-07-23 21:28:06 -0700575void InputDevice::addMapper(InputMapper* mapper) {
576 mMappers.add(mapper);
577}
578
579void InputDevice::configure() {
Jeff Brown8d608662010-08-30 03:02:23 -0700580 if (! isIgnored()) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800581 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
Jeff Brown8d608662010-08-30 03:02:23 -0700582 }
583
Jeff Brown6d0fec22010-07-23 21:28:06 -0700584 mSources = 0;
585
586 size_t numMappers = mMappers.size();
587 for (size_t i = 0; i < numMappers; i++) {
588 InputMapper* mapper = mMappers[i];
589 mapper->configure();
590 mSources |= mapper->getSources();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700591 }
592}
593
Jeff Brown6d0fec22010-07-23 21:28:06 -0700594void InputDevice::reset() {
595 size_t numMappers = mMappers.size();
596 for (size_t i = 0; i < numMappers; i++) {
597 InputMapper* mapper = mMappers[i];
598 mapper->reset();
599 }
600}
Jeff Brown46b9ac02010-04-22 18:58:52 -0700601
Jeff Brown6d0fec22010-07-23 21:28:06 -0700602void InputDevice::process(const RawEvent* rawEvent) {
603 size_t numMappers = mMappers.size();
604 for (size_t i = 0; i < numMappers; i++) {
605 InputMapper* mapper = mMappers[i];
606 mapper->process(rawEvent);
607 }
608}
Jeff Brown46b9ac02010-04-22 18:58:52 -0700609
Jeff Brown6d0fec22010-07-23 21:28:06 -0700610void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
611 outDeviceInfo->initialize(mId, mName);
612
613 size_t numMappers = mMappers.size();
614 for (size_t i = 0; i < numMappers; i++) {
615 InputMapper* mapper = mMappers[i];
616 mapper->populateDeviceInfo(outDeviceInfo);
617 }
618}
619
620int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
621 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
622}
623
624int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
625 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
626}
627
628int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
629 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
630}
631
632int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
633 int32_t result = AKEY_STATE_UNKNOWN;
634 size_t numMappers = mMappers.size();
635 for (size_t i = 0; i < numMappers; i++) {
636 InputMapper* mapper = mMappers[i];
637 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
638 result = (mapper->*getStateFunc)(sourceMask, code);
639 if (result >= AKEY_STATE_DOWN) {
640 return result;
641 }
642 }
643 }
644 return result;
645}
646
647bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
648 const int32_t* keyCodes, uint8_t* outFlags) {
649 bool result = false;
650 size_t numMappers = mMappers.size();
651 for (size_t i = 0; i < numMappers; i++) {
652 InputMapper* mapper = mMappers[i];
653 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
654 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
655 }
656 }
657 return result;
658}
659
660int32_t InputDevice::getMetaState() {
661 int32_t result = 0;
662 size_t numMappers = mMappers.size();
663 for (size_t i = 0; i < numMappers; i++) {
664 InputMapper* mapper = mMappers[i];
665 result |= mapper->getMetaState();
666 }
667 return result;
668}
669
670
671// --- InputMapper ---
672
673InputMapper::InputMapper(InputDevice* device) :
674 mDevice(device), mContext(device->getContext()) {
675}
676
677InputMapper::~InputMapper() {
678}
679
680void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
681 info->addSource(getSources());
682}
683
Jeff Brownef3d7e82010-09-30 14:33:04 -0700684void InputMapper::dump(String8& dump) {
685}
686
Jeff Brown6d0fec22010-07-23 21:28:06 -0700687void InputMapper::configure() {
688}
689
690void InputMapper::reset() {
691}
692
693int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
694 return AKEY_STATE_UNKNOWN;
695}
696
697int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
698 return AKEY_STATE_UNKNOWN;
699}
700
701int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
702 return AKEY_STATE_UNKNOWN;
703}
704
705bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
706 const int32_t* keyCodes, uint8_t* outFlags) {
707 return false;
708}
709
710int32_t InputMapper::getMetaState() {
711 return 0;
712}
713
Jeff Brown6d0fec22010-07-23 21:28:06 -0700714
715// --- SwitchInputMapper ---
716
717SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
718 InputMapper(device) {
719}
720
721SwitchInputMapper::~SwitchInputMapper() {
722}
723
724uint32_t SwitchInputMapper::getSources() {
725 return 0;
726}
727
728void SwitchInputMapper::process(const RawEvent* rawEvent) {
729 switch (rawEvent->type) {
730 case EV_SW:
731 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
732 break;
733 }
734}
735
736void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownb6997262010-10-08 22:31:17 -0700737 getDispatcher()->notifySwitch(when, switchCode, switchValue, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700738}
739
740int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
741 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
742}
743
744
745// --- KeyboardInputMapper ---
746
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800747KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700748 uint32_t sources, int32_t keyboardType) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800749 InputMapper(device), mSources(sources),
Jeff Brown6d0fec22010-07-23 21:28:06 -0700750 mKeyboardType(keyboardType) {
Jeff Brown6328cdc2010-07-29 18:18:33 -0700751 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700752}
753
754KeyboardInputMapper::~KeyboardInputMapper() {
755}
756
Jeff Brown6328cdc2010-07-29 18:18:33 -0700757void KeyboardInputMapper::initializeLocked() {
758 mLocked.metaState = AMETA_NONE;
759 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700760}
761
762uint32_t KeyboardInputMapper::getSources() {
763 return mSources;
764}
765
766void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
767 InputMapper::populateDeviceInfo(info);
768
769 info->setKeyboardType(mKeyboardType);
770}
771
Jeff Brownef3d7e82010-09-30 14:33:04 -0700772void KeyboardInputMapper::dump(String8& dump) {
773 { // acquire lock
774 AutoMutex _l(mLock);
775 dump.append(INDENT2 "Keyboard Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800776 dumpParameters(dump);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700777 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
778 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mLocked.keyDowns.size());
779 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mLocked.metaState);
780 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
781 } // release lock
782}
783
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800784
785void KeyboardInputMapper::configure() {
786 InputMapper::configure();
787
788 // Configure basic parameters.
789 configureParameters();
Jeff Brown49ed71d2010-12-06 17:13:33 -0800790
791 // Reset LEDs.
792 {
793 AutoMutex _l(mLock);
794 resetLedStateLocked();
795 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800796}
797
798void KeyboardInputMapper::configureParameters() {
799 mParameters.orientationAware = false;
800 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
801 mParameters.orientationAware);
802
803 mParameters.associatedDisplayId = mParameters.orientationAware ? 0 : -1;
804}
805
806void KeyboardInputMapper::dumpParameters(String8& dump) {
807 dump.append(INDENT3 "Parameters:\n");
808 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
809 mParameters.associatedDisplayId);
810 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
811 toString(mParameters.orientationAware));
812}
813
Jeff Brown6d0fec22010-07-23 21:28:06 -0700814void KeyboardInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -0700815 for (;;) {
816 int32_t keyCode, scanCode;
817 { // acquire lock
818 AutoMutex _l(mLock);
819
820 // Synthesize key up event on reset if keys are currently down.
821 if (mLocked.keyDowns.isEmpty()) {
822 initializeLocked();
Jeff Brown49ed71d2010-12-06 17:13:33 -0800823 resetLedStateLocked();
Jeff Brown6328cdc2010-07-29 18:18:33 -0700824 break; // done
825 }
826
827 const KeyDown& keyDown = mLocked.keyDowns.top();
828 keyCode = keyDown.keyCode;
829 scanCode = keyDown.scanCode;
830 } // release lock
831
Jeff Brown6d0fec22010-07-23 21:28:06 -0700832 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700833 processKey(when, false, keyCode, scanCode, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700834 }
835
836 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700837 getContext()->updateGlobalMetaState();
838}
839
840void KeyboardInputMapper::process(const RawEvent* rawEvent) {
841 switch (rawEvent->type) {
842 case EV_KEY: {
843 int32_t scanCode = rawEvent->scanCode;
844 if (isKeyboardOrGamepadKey(scanCode)) {
845 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
846 rawEvent->flags);
847 }
848 break;
849 }
850 }
851}
852
853bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
854 return scanCode < BTN_MOUSE
855 || scanCode >= KEY_OK
856 || (scanCode >= BTN_GAMEPAD && scanCode < BTN_DIGI);
857}
858
Jeff Brown6328cdc2010-07-29 18:18:33 -0700859void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
860 int32_t scanCode, uint32_t policyFlags) {
861 int32_t newMetaState;
862 nsecs_t downTime;
863 bool metaStateChanged = false;
864
865 { // acquire lock
866 AutoMutex _l(mLock);
867
868 if (down) {
869 // Rotate key codes according to orientation if needed.
870 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800871 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -0700872 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800873 if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
874 NULL, NULL, & orientation)) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800875 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700876 }
877
878 keyCode = rotateKeyCode(keyCode, orientation);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700879 }
880
Jeff Brown6328cdc2010-07-29 18:18:33 -0700881 // Add key down.
882 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
883 if (keyDownIndex >= 0) {
884 // key repeat, be sure to use same keycode as before in case of rotation
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800885 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700886 } else {
887 // key down
888 mLocked.keyDowns.push();
889 KeyDown& keyDown = mLocked.keyDowns.editTop();
890 keyDown.keyCode = keyCode;
891 keyDown.scanCode = scanCode;
892 }
893
894 mLocked.downTime = when;
895 } else {
896 // Remove key down.
897 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
898 if (keyDownIndex >= 0) {
899 // key up, be sure to use same keycode as before in case of rotation
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800900 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700901 mLocked.keyDowns.removeAt(size_t(keyDownIndex));
902 } else {
903 // key was not actually down
904 LOGI("Dropping key up from device %s because the key was not down. "
905 "keyCode=%d, scanCode=%d",
906 getDeviceName().string(), keyCode, scanCode);
907 return;
908 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700909 }
910
Jeff Brown6328cdc2010-07-29 18:18:33 -0700911 int32_t oldMetaState = mLocked.metaState;
912 newMetaState = updateMetaState(keyCode, down, oldMetaState);
913 if (oldMetaState != newMetaState) {
914 mLocked.metaState = newMetaState;
915 metaStateChanged = true;
Jeff Brown497a92c2010-09-12 17:55:08 -0700916 updateLedStateLocked(false);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700917 }
Jeff Brownfd035822010-06-30 16:10:35 -0700918
Jeff Brown6328cdc2010-07-29 18:18:33 -0700919 downTime = mLocked.downTime;
920 } // release lock
921
922 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700923 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700924 }
925
Jeff Brown497a92c2010-09-12 17:55:08 -0700926 if (policyFlags & POLICY_FLAG_FUNCTION) {
927 newMetaState |= AMETA_FUNCTION_ON;
928 }
Jeff Brown83c09682010-12-23 17:50:18 -0800929 getDispatcher()->notifyKey(when, getDeviceId(), mSources, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -0700930 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
931 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700932}
933
Jeff Brown6328cdc2010-07-29 18:18:33 -0700934ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) {
935 size_t n = mLocked.keyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700936 for (size_t i = 0; i < n; i++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -0700937 if (mLocked.keyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700938 return i;
939 }
940 }
941 return -1;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700942}
943
Jeff Brown6d0fec22010-07-23 21:28:06 -0700944int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
945 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
946}
Jeff Brown46b9ac02010-04-22 18:58:52 -0700947
Jeff Brown6d0fec22010-07-23 21:28:06 -0700948int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
949 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
950}
Jeff Brown46b9ac02010-04-22 18:58:52 -0700951
Jeff Brown6d0fec22010-07-23 21:28:06 -0700952bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
953 const int32_t* keyCodes, uint8_t* outFlags) {
954 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
955}
956
957int32_t KeyboardInputMapper::getMetaState() {
Jeff Brown6328cdc2010-07-29 18:18:33 -0700958 { // acquire lock
959 AutoMutex _l(mLock);
960 return mLocked.metaState;
961 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -0700962}
963
Jeff Brown49ed71d2010-12-06 17:13:33 -0800964void KeyboardInputMapper::resetLedStateLocked() {
965 initializeLedStateLocked(mLocked.capsLockLedState, LED_CAPSL);
966 initializeLedStateLocked(mLocked.numLockLedState, LED_NUML);
967 initializeLedStateLocked(mLocked.scrollLockLedState, LED_SCROLLL);
968
969 updateLedStateLocked(true);
970}
971
972void KeyboardInputMapper::initializeLedStateLocked(LockedState::LedState& ledState, int32_t led) {
973 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
974 ledState.on = false;
975}
976
Jeff Brown497a92c2010-09-12 17:55:08 -0700977void KeyboardInputMapper::updateLedStateLocked(bool reset) {
978 updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL,
Jeff Brown51e7fe72010-10-29 22:19:53 -0700979 AMETA_CAPS_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -0700980 updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML,
Jeff Brown51e7fe72010-10-29 22:19:53 -0700981 AMETA_NUM_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -0700982 updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe72010-10-29 22:19:53 -0700983 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -0700984}
985
986void KeyboardInputMapper::updateLedStateForModifierLocked(LockedState::LedState& ledState,
987 int32_t led, int32_t modifier, bool reset) {
988 if (ledState.avail) {
989 bool desiredState = (mLocked.metaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -0800990 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -0700991 getEventHub()->setLedState(getDeviceId(), led, desiredState);
992 ledState.on = desiredState;
993 }
994 }
995}
996
Jeff Brown6d0fec22010-07-23 21:28:06 -0700997
Jeff Brown83c09682010-12-23 17:50:18 -0800998// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -0700999
Jeff Brown83c09682010-12-23 17:50:18 -08001000CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001001 InputMapper(device) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001002 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001003}
1004
Jeff Brown83c09682010-12-23 17:50:18 -08001005CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001006}
1007
Jeff Brown83c09682010-12-23 17:50:18 -08001008uint32_t CursorInputMapper::getSources() {
1009 return mSources;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001010}
1011
Jeff Brown83c09682010-12-23 17:50:18 -08001012void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001013 InputMapper::populateDeviceInfo(info);
1014
Jeff Brown83c09682010-12-23 17:50:18 -08001015 if (mParameters.mode == Parameters::MODE_POINTER) {
1016 float minX, minY, maxX, maxY;
1017 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
1018 info->addMotionRange(AINPUT_MOTION_RANGE_X, minX, maxX, 0.0f, 0.0f);
1019 info->addMotionRange(AINPUT_MOTION_RANGE_Y, minY, maxY, 0.0f, 0.0f);
1020 }
1021 } else {
1022 info->addMotionRange(AINPUT_MOTION_RANGE_X, -1.0f, 1.0f, 0.0f, mXScale);
1023 info->addMotionRange(AINPUT_MOTION_RANGE_Y, -1.0f, 1.0f, 0.0f, mYScale);
1024 }
1025 info->addMotionRange(AINPUT_MOTION_RANGE_PRESSURE, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001026}
1027
Jeff Brown83c09682010-12-23 17:50:18 -08001028void CursorInputMapper::dump(String8& dump) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07001029 { // acquire lock
1030 AutoMutex _l(mLock);
Jeff Brown83c09682010-12-23 17:50:18 -08001031 dump.append(INDENT2 "Cursor Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001032 dumpParameters(dump);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001033 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1034 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
1035 dump.appendFormat(INDENT3 "Down: %s\n", toString(mLocked.down));
1036 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1037 } // release lock
1038}
1039
Jeff Brown83c09682010-12-23 17:50:18 -08001040void CursorInputMapper::configure() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001041 InputMapper::configure();
1042
1043 // Configure basic parameters.
1044 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08001045
1046 // Configure device mode.
1047 switch (mParameters.mode) {
1048 case Parameters::MODE_POINTER:
1049 mSources = AINPUT_SOURCE_MOUSE;
1050 mXPrecision = 1.0f;
1051 mYPrecision = 1.0f;
1052 mXScale = 1.0f;
1053 mYScale = 1.0f;
1054 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
1055 break;
1056 case Parameters::MODE_NAVIGATION:
1057 mSources = AINPUT_SOURCE_TRACKBALL;
1058 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1059 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1060 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1061 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1062 break;
1063 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001064}
1065
Jeff Brown83c09682010-12-23 17:50:18 -08001066void CursorInputMapper::configureParameters() {
1067 mParameters.mode = Parameters::MODE_POINTER;
1068 String8 cursorModeString;
1069 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
1070 if (cursorModeString == "navigation") {
1071 mParameters.mode = Parameters::MODE_NAVIGATION;
1072 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
1073 LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
1074 }
1075 }
1076
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001077 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08001078 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001079 mParameters.orientationAware);
1080
Jeff Brown83c09682010-12-23 17:50:18 -08001081 mParameters.associatedDisplayId = mParameters.mode == Parameters::MODE_POINTER
1082 || mParameters.orientationAware ? 0 : -1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001083}
1084
Jeff Brown83c09682010-12-23 17:50:18 -08001085void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001086 dump.append(INDENT3 "Parameters:\n");
1087 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1088 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08001089
1090 switch (mParameters.mode) {
1091 case Parameters::MODE_POINTER:
1092 dump.append(INDENT4 "Mode: pointer\n");
1093 break;
1094 case Parameters::MODE_NAVIGATION:
1095 dump.append(INDENT4 "Mode: navigation\n");
1096 break;
1097 default:
1098 assert(false);
1099 }
1100
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001101 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1102 toString(mParameters.orientationAware));
1103}
1104
Jeff Brown83c09682010-12-23 17:50:18 -08001105void CursorInputMapper::initializeLocked() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001106 mAccumulator.clear();
1107
Jeff Brown6328cdc2010-07-29 18:18:33 -07001108 mLocked.down = false;
1109 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001110}
1111
Jeff Brown83c09682010-12-23 17:50:18 -08001112void CursorInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001113 for (;;) {
1114 { // acquire lock
1115 AutoMutex _l(mLock);
1116
1117 if (! mLocked.down) {
1118 initializeLocked();
1119 break; // done
1120 }
1121 } // release lock
1122
Jeff Brown83c09682010-12-23 17:50:18 -08001123 // Synthesize button up event on reset.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001124 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001125 mAccumulator.fields = Accumulator::FIELD_BTN_MOUSE;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001126 mAccumulator.btnMouse = false;
1127 sync(when);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001128 }
1129
Jeff Brown6d0fec22010-07-23 21:28:06 -07001130 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001131}
Jeff Brown46b9ac02010-04-22 18:58:52 -07001132
Jeff Brown83c09682010-12-23 17:50:18 -08001133void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001134 switch (rawEvent->type) {
1135 case EV_KEY:
1136 switch (rawEvent->scanCode) {
1137 case BTN_MOUSE:
1138 mAccumulator.fields |= Accumulator::FIELD_BTN_MOUSE;
1139 mAccumulator.btnMouse = rawEvent->value != 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001140 // Sync now since BTN_MOUSE is not necessarily followed by SYN_REPORT and
1141 // we need to ensure that we report the up/down promptly.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001142 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001143 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001144 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001145 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001146
Jeff Brown6d0fec22010-07-23 21:28:06 -07001147 case EV_REL:
1148 switch (rawEvent->scanCode) {
1149 case REL_X:
1150 mAccumulator.fields |= Accumulator::FIELD_REL_X;
1151 mAccumulator.relX = rawEvent->value;
1152 break;
1153 case REL_Y:
1154 mAccumulator.fields |= Accumulator::FIELD_REL_Y;
1155 mAccumulator.relY = rawEvent->value;
1156 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001157 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001158 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001159
Jeff Brown6d0fec22010-07-23 21:28:06 -07001160 case EV_SYN:
1161 switch (rawEvent->scanCode) {
1162 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001163 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001164 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001165 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001166 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001167 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001168}
1169
Jeff Brown83c09682010-12-23 17:50:18 -08001170void CursorInputMapper::sync(nsecs_t when) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001171 uint32_t fields = mAccumulator.fields;
1172 if (fields == 0) {
1173 return; // no new state changes, so nothing to do
1174 }
1175
Jeff Brown6328cdc2010-07-29 18:18:33 -07001176 int motionEventAction;
1177 PointerCoords pointerCoords;
1178 nsecs_t downTime;
1179 { // acquire lock
1180 AutoMutex _l(mLock);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001181
Jeff Brown6328cdc2010-07-29 18:18:33 -07001182 bool downChanged = fields & Accumulator::FIELD_BTN_MOUSE;
1183
1184 if (downChanged) {
1185 if (mAccumulator.btnMouse) {
1186 mLocked.down = true;
1187 mLocked.downTime = when;
1188 } else {
1189 mLocked.down = false;
1190 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001191 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001192
Jeff Brown6328cdc2010-07-29 18:18:33 -07001193 downTime = mLocked.downTime;
Jeff Brown83c09682010-12-23 17:50:18 -08001194 float deltaX = fields & Accumulator::FIELD_REL_X ? mAccumulator.relX * mXScale : 0.0f;
1195 float deltaY = fields & Accumulator::FIELD_REL_Y ? mAccumulator.relY * mYScale : 0.0f;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001196
Jeff Brown6328cdc2010-07-29 18:18:33 -07001197 if (downChanged) {
1198 motionEventAction = mLocked.down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001199 } else {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001200 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001201 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001202
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001203 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
Jeff Brown83c09682010-12-23 17:50:18 -08001204 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001205 // Rotate motion based on display orientation if needed.
1206 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1207 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001208 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1209 NULL, NULL, & orientation)) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001210 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001211 }
1212
1213 float temp;
1214 switch (orientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001215 case DISPLAY_ORIENTATION_90:
Jeff Brown83c09682010-12-23 17:50:18 -08001216 temp = deltaX;
1217 deltaX = deltaY;
1218 deltaY = -temp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001219 break;
1220
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001221 case DISPLAY_ORIENTATION_180:
Jeff Brown83c09682010-12-23 17:50:18 -08001222 deltaX = -deltaX;
1223 deltaY = -deltaY;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001224 break;
1225
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001226 case DISPLAY_ORIENTATION_270:
Jeff Brown83c09682010-12-23 17:50:18 -08001227 temp = deltaX;
1228 deltaX = -deltaY;
1229 deltaY = temp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001230 break;
1231 }
1232 }
Jeff Brown83c09682010-12-23 17:50:18 -08001233
1234 if (mPointerController != NULL) {
1235 mPointerController->move(deltaX, deltaY);
1236 if (downChanged) {
1237 mPointerController->setButtonState(mLocked.down ? POINTER_BUTTON_1 : 0);
1238 }
1239 mPointerController->getPosition(&pointerCoords.x, &pointerCoords.y);
1240 } else {
1241 pointerCoords.x = deltaX;
1242 pointerCoords.y = deltaY;
1243 }
1244
1245 pointerCoords.pressure = mLocked.down ? 1.0f : 0.0f;
1246 pointerCoords.size = 0;
1247 pointerCoords.touchMajor = 0;
1248 pointerCoords.touchMinor = 0;
1249 pointerCoords.toolMajor = 0;
1250 pointerCoords.toolMinor = 0;
1251 pointerCoords.orientation = 0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001252 } // release lock
1253
Jeff Brown6d0fec22010-07-23 21:28:06 -07001254 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001255 int32_t pointerId = 0;
Jeff Brown83c09682010-12-23 17:50:18 -08001256 getDispatcher()->notifyMotion(when, getDeviceId(), mSources, 0,
Jeff Brown85a31762010-09-01 17:01:00 -07001257 motionEventAction, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brownb6997262010-10-08 22:31:17 -07001258 1, &pointerId, &pointerCoords, mXPrecision, mYPrecision, downTime);
1259
1260 mAccumulator.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001261}
1262
Jeff Brown83c09682010-12-23 17:50:18 -08001263int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07001264 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
1265 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1266 } else {
1267 return AKEY_STATE_UNKNOWN;
1268 }
1269}
1270
Jeff Brown6d0fec22010-07-23 21:28:06 -07001271
1272// --- TouchInputMapper ---
1273
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001274TouchInputMapper::TouchInputMapper(InputDevice* device) :
1275 InputMapper(device) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001276 mLocked.surfaceOrientation = -1;
1277 mLocked.surfaceWidth = -1;
1278 mLocked.surfaceHeight = -1;
1279
1280 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001281}
1282
1283TouchInputMapper::~TouchInputMapper() {
1284}
1285
1286uint32_t TouchInputMapper::getSources() {
Jeff Brown83c09682010-12-23 17:50:18 -08001287 return mSources;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001288}
1289
1290void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1291 InputMapper::populateDeviceInfo(info);
1292
Jeff Brown6328cdc2010-07-29 18:18:33 -07001293 { // acquire lock
1294 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001295
Jeff Brown6328cdc2010-07-29 18:18:33 -07001296 // Ensure surface information is up to date so that orientation changes are
1297 // noticed immediately.
1298 configureSurfaceLocked();
1299
1300 info->addMotionRange(AINPUT_MOTION_RANGE_X, mLocked.orientedRanges.x);
1301 info->addMotionRange(AINPUT_MOTION_RANGE_Y, mLocked.orientedRanges.y);
Jeff Brown8d608662010-08-30 03:02:23 -07001302
1303 if (mLocked.orientedRanges.havePressure) {
1304 info->addMotionRange(AINPUT_MOTION_RANGE_PRESSURE,
1305 mLocked.orientedRanges.pressure);
1306 }
1307
1308 if (mLocked.orientedRanges.haveSize) {
1309 info->addMotionRange(AINPUT_MOTION_RANGE_SIZE,
1310 mLocked.orientedRanges.size);
1311 }
1312
Jeff Brownc6d282b2010-10-14 21:42:15 -07001313 if (mLocked.orientedRanges.haveTouchSize) {
Jeff Brown8d608662010-08-30 03:02:23 -07001314 info->addMotionRange(AINPUT_MOTION_RANGE_TOUCH_MAJOR,
1315 mLocked.orientedRanges.touchMajor);
1316 info->addMotionRange(AINPUT_MOTION_RANGE_TOUCH_MINOR,
1317 mLocked.orientedRanges.touchMinor);
1318 }
1319
Jeff Brownc6d282b2010-10-14 21:42:15 -07001320 if (mLocked.orientedRanges.haveToolSize) {
Jeff Brown8d608662010-08-30 03:02:23 -07001321 info->addMotionRange(AINPUT_MOTION_RANGE_TOOL_MAJOR,
1322 mLocked.orientedRanges.toolMajor);
1323 info->addMotionRange(AINPUT_MOTION_RANGE_TOOL_MINOR,
1324 mLocked.orientedRanges.toolMinor);
1325 }
1326
1327 if (mLocked.orientedRanges.haveOrientation) {
1328 info->addMotionRange(AINPUT_MOTION_RANGE_ORIENTATION,
1329 mLocked.orientedRanges.orientation);
1330 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001331 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001332}
1333
Jeff Brownef3d7e82010-09-30 14:33:04 -07001334void TouchInputMapper::dump(String8& dump) {
1335 { // acquire lock
1336 AutoMutex _l(mLock);
1337 dump.append(INDENT2 "Touch Input Mapper:\n");
Jeff Brownef3d7e82010-09-30 14:33:04 -07001338 dumpParameters(dump);
1339 dumpVirtualKeysLocked(dump);
1340 dumpRawAxes(dump);
1341 dumpCalibration(dump);
1342 dumpSurfaceLocked(dump);
Jeff Brown511ee5f2010-10-18 13:32:20 -07001343 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
Jeff Brownc6d282b2010-10-14 21:42:15 -07001344 dump.appendFormat(INDENT4 "XOrigin: %d\n", mLocked.xOrigin);
1345 dump.appendFormat(INDENT4 "YOrigin: %d\n", mLocked.yOrigin);
1346 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mLocked.xScale);
1347 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mLocked.yScale);
1348 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mLocked.xPrecision);
1349 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mLocked.yPrecision);
1350 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mLocked.geometricScale);
1351 dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mLocked.toolSizeLinearScale);
1352 dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mLocked.toolSizeLinearBias);
1353 dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mLocked.toolSizeAreaScale);
1354 dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mLocked.toolSizeAreaBias);
1355 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mLocked.pressureScale);
1356 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mLocked.sizeScale);
1357 dump.appendFormat(INDENT4 "OrientationSCale: %0.3f\n", mLocked.orientationScale);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001358 } // release lock
1359}
1360
Jeff Brown6328cdc2010-07-29 18:18:33 -07001361void TouchInputMapper::initializeLocked() {
1362 mCurrentTouch.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001363 mLastTouch.clear();
1364 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001365
1366 for (uint32_t i = 0; i < MAX_POINTERS; i++) {
1367 mAveragingTouchFilter.historyStart[i] = 0;
1368 mAveragingTouchFilter.historyEnd[i] = 0;
1369 }
1370
1371 mJumpyTouchFilter.jumpyPointsDropped = 0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001372
1373 mLocked.currentVirtualKey.down = false;
Jeff Brown8d608662010-08-30 03:02:23 -07001374
1375 mLocked.orientedRanges.havePressure = false;
1376 mLocked.orientedRanges.haveSize = false;
Jeff Brownc6d282b2010-10-14 21:42:15 -07001377 mLocked.orientedRanges.haveTouchSize = false;
1378 mLocked.orientedRanges.haveToolSize = false;
Jeff Brown8d608662010-08-30 03:02:23 -07001379 mLocked.orientedRanges.haveOrientation = false;
1380}
1381
Jeff Brown6d0fec22010-07-23 21:28:06 -07001382void TouchInputMapper::configure() {
1383 InputMapper::configure();
1384
1385 // Configure basic parameters.
Jeff Brown8d608662010-08-30 03:02:23 -07001386 configureParameters();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001387
Jeff Brown83c09682010-12-23 17:50:18 -08001388 // Configure sources.
1389 switch (mParameters.deviceType) {
1390 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
1391 mSources = AINPUT_SOURCE_TOUCHSCREEN;
1392 break;
1393 case Parameters::DEVICE_TYPE_TOUCH_PAD:
1394 mSources = AINPUT_SOURCE_TOUCHPAD;
1395 break;
1396 default:
1397 assert(false);
1398 }
1399
Jeff Brown6d0fec22010-07-23 21:28:06 -07001400 // Configure absolute axis information.
Jeff Brown8d608662010-08-30 03:02:23 -07001401 configureRawAxes();
Jeff Brown8d608662010-08-30 03:02:23 -07001402
1403 // Prepare input device calibration.
1404 parseCalibration();
1405 resolveCalibration();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001406
Jeff Brown6328cdc2010-07-29 18:18:33 -07001407 { // acquire lock
1408 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001409
Jeff Brown8d608662010-08-30 03:02:23 -07001410 // Configure surface dimensions and orientation.
Jeff Brown6328cdc2010-07-29 18:18:33 -07001411 configureSurfaceLocked();
1412 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001413}
1414
Jeff Brown8d608662010-08-30 03:02:23 -07001415void TouchInputMapper::configureParameters() {
1416 mParameters.useBadTouchFilter = getPolicy()->filterTouchEvents();
1417 mParameters.useAveragingTouchFilter = getPolicy()->filterTouchEvents();
1418 mParameters.useJumpyTouchFilter = getPolicy()->filterJumpyTouchEvents();
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001419
1420 String8 deviceTypeString;
1421 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
1422 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
1423 deviceTypeString)) {
1424 if (deviceTypeString == "touchPad") {
1425 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
1426 } else if (deviceTypeString != "touchScreen") {
1427 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
1428 }
1429 }
1430 bool isTouchScreen = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
1431
1432 mParameters.orientationAware = isTouchScreen;
1433 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
1434 mParameters.orientationAware);
1435
1436 mParameters.associatedDisplayId = mParameters.orientationAware || isTouchScreen ? 0 : -1;
Jeff Brown8d608662010-08-30 03:02:23 -07001437}
1438
Jeff Brownef3d7e82010-09-30 14:33:04 -07001439void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001440 dump.append(INDENT3 "Parameters:\n");
1441
1442 switch (mParameters.deviceType) {
1443 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
1444 dump.append(INDENT4 "DeviceType: touchScreen\n");
1445 break;
1446 case Parameters::DEVICE_TYPE_TOUCH_PAD:
1447 dump.append(INDENT4 "DeviceType: touchPad\n");
1448 break;
1449 default:
1450 assert(false);
1451 }
1452
1453 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1454 mParameters.associatedDisplayId);
1455 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1456 toString(mParameters.orientationAware));
1457
1458 dump.appendFormat(INDENT4 "UseBadTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07001459 toString(mParameters.useBadTouchFilter));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001460 dump.appendFormat(INDENT4 "UseAveragingTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07001461 toString(mParameters.useAveragingTouchFilter));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001462 dump.appendFormat(INDENT4 "UseJumpyTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07001463 toString(mParameters.useJumpyTouchFilter));
Jeff Brownb88102f2010-09-08 11:49:43 -07001464}
1465
Jeff Brown8d608662010-08-30 03:02:23 -07001466void TouchInputMapper::configureRawAxes() {
1467 mRawAxes.x.clear();
1468 mRawAxes.y.clear();
1469 mRawAxes.pressure.clear();
1470 mRawAxes.touchMajor.clear();
1471 mRawAxes.touchMinor.clear();
1472 mRawAxes.toolMajor.clear();
1473 mRawAxes.toolMinor.clear();
1474 mRawAxes.orientation.clear();
1475}
1476
Jeff Brownef3d7e82010-09-30 14:33:04 -07001477static void dumpAxisInfo(String8& dump, RawAbsoluteAxisInfo axis, const char* name) {
Jeff Brownb88102f2010-09-08 11:49:43 -07001478 if (axis.valid) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07001479 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d\n",
Jeff Brownb88102f2010-09-08 11:49:43 -07001480 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz);
1481 } else {
Jeff Brownef3d7e82010-09-30 14:33:04 -07001482 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
Jeff Brownb88102f2010-09-08 11:49:43 -07001483 }
1484}
1485
Jeff Brownef3d7e82010-09-30 14:33:04 -07001486void TouchInputMapper::dumpRawAxes(String8& dump) {
1487 dump.append(INDENT3 "Raw Axes:\n");
1488 dumpAxisInfo(dump, mRawAxes.x, "X");
1489 dumpAxisInfo(dump, mRawAxes.y, "Y");
1490 dumpAxisInfo(dump, mRawAxes.pressure, "Pressure");
1491 dumpAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor");
1492 dumpAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor");
1493 dumpAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor");
1494 dumpAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor");
1495 dumpAxisInfo(dump, mRawAxes.orientation, "Orientation");
Jeff Brown6d0fec22010-07-23 21:28:06 -07001496}
1497
Jeff Brown6328cdc2010-07-29 18:18:33 -07001498bool TouchInputMapper::configureSurfaceLocked() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001499 // Update orientation and dimensions if needed.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001500 int32_t orientation = DISPLAY_ORIENTATION_0;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001501 int32_t width = mRawAxes.x.getRange();
1502 int32_t height = mRawAxes.y.getRange();
1503
1504 if (mParameters.associatedDisplayId >= 0) {
1505 bool wantSize = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
1506 bool wantOrientation = mParameters.orientationAware;
1507
Jeff Brown6328cdc2010-07-29 18:18:33 -07001508 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001509 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1510 wantSize ? &width : NULL, wantSize ? &height : NULL,
1511 wantOrientation ? &orientation : NULL)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001512 return false;
1513 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001514 }
1515
Jeff Brown6328cdc2010-07-29 18:18:33 -07001516 bool orientationChanged = mLocked.surfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001517 if (orientationChanged) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001518 mLocked.surfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001519 }
1520
Jeff Brown6328cdc2010-07-29 18:18:33 -07001521 bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001522 if (sizeChanged) {
Jeff Brown90655042010-12-02 13:50:46 -08001523 LOGI("Device reconfigured: id=%d, name='%s', display size is now %dx%d",
Jeff Brownef3d7e82010-09-30 14:33:04 -07001524 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown8d608662010-08-30 03:02:23 -07001525
Jeff Brown6328cdc2010-07-29 18:18:33 -07001526 mLocked.surfaceWidth = width;
1527 mLocked.surfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001528
Jeff Brown8d608662010-08-30 03:02:23 -07001529 // Configure X and Y factors.
1530 if (mRawAxes.x.valid && mRawAxes.y.valid) {
Jeff Brown511ee5f2010-10-18 13:32:20 -07001531 mLocked.xOrigin = mCalibration.haveXOrigin
1532 ? mCalibration.xOrigin
1533 : mRawAxes.x.minValue;
1534 mLocked.yOrigin = mCalibration.haveYOrigin
1535 ? mCalibration.yOrigin
1536 : mRawAxes.y.minValue;
1537 mLocked.xScale = mCalibration.haveXScale
1538 ? mCalibration.xScale
1539 : float(width) / mRawAxes.x.getRange();
1540 mLocked.yScale = mCalibration.haveYScale
1541 ? mCalibration.yScale
1542 : float(height) / mRawAxes.y.getRange();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001543 mLocked.xPrecision = 1.0f / mLocked.xScale;
1544 mLocked.yPrecision = 1.0f / mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001545
Jeff Brown6328cdc2010-07-29 18:18:33 -07001546 configureVirtualKeysLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001547 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07001548 LOGW(INDENT "Touch device did not report support for X or Y axis!");
Jeff Brown6328cdc2010-07-29 18:18:33 -07001549 mLocked.xOrigin = 0;
1550 mLocked.yOrigin = 0;
1551 mLocked.xScale = 1.0f;
1552 mLocked.yScale = 1.0f;
1553 mLocked.xPrecision = 1.0f;
1554 mLocked.yPrecision = 1.0f;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001555 }
1556
Jeff Brown8d608662010-08-30 03:02:23 -07001557 // Scale factor for terms that are not oriented in a particular axis.
1558 // If the pixels are square then xScale == yScale otherwise we fake it
1559 // by choosing an average.
1560 mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001561
Jeff Brown8d608662010-08-30 03:02:23 -07001562 // Size of diagonal axis.
1563 float diagonalSize = pythag(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001564
Jeff Brown8d608662010-08-30 03:02:23 -07001565 // TouchMajor and TouchMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07001566 if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) {
1567 mLocked.orientedRanges.haveTouchSize = true;
Jeff Brown8d608662010-08-30 03:02:23 -07001568 mLocked.orientedRanges.touchMajor.min = 0;
1569 mLocked.orientedRanges.touchMajor.max = diagonalSize;
1570 mLocked.orientedRanges.touchMajor.flat = 0;
1571 mLocked.orientedRanges.touchMajor.fuzz = 0;
1572 mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor;
1573 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001574
Jeff Brown8d608662010-08-30 03:02:23 -07001575 // ToolMajor and ToolMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07001576 mLocked.toolSizeLinearScale = 0;
1577 mLocked.toolSizeLinearBias = 0;
1578 mLocked.toolSizeAreaScale = 0;
1579 mLocked.toolSizeAreaBias = 0;
1580 if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
1581 if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) {
1582 if (mCalibration.haveToolSizeLinearScale) {
1583 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
Jeff Brown8d608662010-08-30 03:02:23 -07001584 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07001585 mLocked.toolSizeLinearScale = float(min(width, height))
Jeff Brown8d608662010-08-30 03:02:23 -07001586 / mRawAxes.toolMajor.maxValue;
1587 }
1588
Jeff Brownc6d282b2010-10-14 21:42:15 -07001589 if (mCalibration.haveToolSizeLinearBias) {
1590 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
1591 }
1592 } else if (mCalibration.toolSizeCalibration ==
1593 Calibration::TOOL_SIZE_CALIBRATION_AREA) {
1594 if (mCalibration.haveToolSizeLinearScale) {
1595 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
1596 } else {
1597 mLocked.toolSizeLinearScale = min(width, height);
1598 }
1599
1600 if (mCalibration.haveToolSizeLinearBias) {
1601 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
1602 }
1603
1604 if (mCalibration.haveToolSizeAreaScale) {
1605 mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale;
1606 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1607 mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue;
1608 }
1609
1610 if (mCalibration.haveToolSizeAreaBias) {
1611 mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -07001612 }
1613 }
1614
Jeff Brownc6d282b2010-10-14 21:42:15 -07001615 mLocked.orientedRanges.haveToolSize = true;
Jeff Brown8d608662010-08-30 03:02:23 -07001616 mLocked.orientedRanges.toolMajor.min = 0;
1617 mLocked.orientedRanges.toolMajor.max = diagonalSize;
1618 mLocked.orientedRanges.toolMajor.flat = 0;
1619 mLocked.orientedRanges.toolMajor.fuzz = 0;
1620 mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor;
1621 }
1622
1623 // Pressure factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07001624 mLocked.pressureScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07001625 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
1626 RawAbsoluteAxisInfo rawPressureAxis;
1627 switch (mCalibration.pressureSource) {
1628 case Calibration::PRESSURE_SOURCE_PRESSURE:
1629 rawPressureAxis = mRawAxes.pressure;
1630 break;
1631 case Calibration::PRESSURE_SOURCE_TOUCH:
1632 rawPressureAxis = mRawAxes.touchMajor;
1633 break;
1634 default:
1635 rawPressureAxis.clear();
1636 }
1637
Jeff Brown8d608662010-08-30 03:02:23 -07001638 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
1639 || mCalibration.pressureCalibration
1640 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
1641 if (mCalibration.havePressureScale) {
1642 mLocked.pressureScale = mCalibration.pressureScale;
1643 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
1644 mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue;
1645 }
1646 }
1647
1648 mLocked.orientedRanges.havePressure = true;
1649 mLocked.orientedRanges.pressure.min = 0;
1650 mLocked.orientedRanges.pressure.max = 1.0;
1651 mLocked.orientedRanges.pressure.flat = 0;
1652 mLocked.orientedRanges.pressure.fuzz = 0;
1653 }
1654
1655 // Size factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07001656 mLocked.sizeScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07001657 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07001658 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
1659 if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1660 mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue;
1661 }
1662 }
1663
1664 mLocked.orientedRanges.haveSize = true;
1665 mLocked.orientedRanges.size.min = 0;
1666 mLocked.orientedRanges.size.max = 1.0;
1667 mLocked.orientedRanges.size.flat = 0;
1668 mLocked.orientedRanges.size.fuzz = 0;
1669 }
1670
1671 // Orientation
Jeff Brownc6d282b2010-10-14 21:42:15 -07001672 mLocked.orientationScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07001673 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07001674 if (mCalibration.orientationCalibration
1675 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
1676 if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) {
1677 mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue;
1678 }
1679 }
1680
1681 mLocked.orientedRanges.orientation.min = - M_PI_2;
1682 mLocked.orientedRanges.orientation.max = M_PI_2;
1683 mLocked.orientedRanges.orientation.flat = 0;
1684 mLocked.orientedRanges.orientation.fuzz = 0;
1685 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001686 }
1687
1688 if (orientationChanged || sizeChanged) {
1689 // Compute oriented surface dimensions, precision, and scales.
1690 float orientedXScale, orientedYScale;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001691 switch (mLocked.surfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001692 case DISPLAY_ORIENTATION_90:
1693 case DISPLAY_ORIENTATION_270:
Jeff Brown6328cdc2010-07-29 18:18:33 -07001694 mLocked.orientedSurfaceWidth = mLocked.surfaceHeight;
1695 mLocked.orientedSurfaceHeight = mLocked.surfaceWidth;
1696 mLocked.orientedXPrecision = mLocked.yPrecision;
1697 mLocked.orientedYPrecision = mLocked.xPrecision;
1698 orientedXScale = mLocked.yScale;
1699 orientedYScale = mLocked.xScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001700 break;
1701 default:
Jeff Brown6328cdc2010-07-29 18:18:33 -07001702 mLocked.orientedSurfaceWidth = mLocked.surfaceWidth;
1703 mLocked.orientedSurfaceHeight = mLocked.surfaceHeight;
1704 mLocked.orientedXPrecision = mLocked.xPrecision;
1705 mLocked.orientedYPrecision = mLocked.yPrecision;
1706 orientedXScale = mLocked.xScale;
1707 orientedYScale = mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001708 break;
1709 }
1710
1711 // Configure position ranges.
Jeff Brown6328cdc2010-07-29 18:18:33 -07001712 mLocked.orientedRanges.x.min = 0;
1713 mLocked.orientedRanges.x.max = mLocked.orientedSurfaceWidth;
1714 mLocked.orientedRanges.x.flat = 0;
1715 mLocked.orientedRanges.x.fuzz = orientedXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001716
Jeff Brown6328cdc2010-07-29 18:18:33 -07001717 mLocked.orientedRanges.y.min = 0;
1718 mLocked.orientedRanges.y.max = mLocked.orientedSurfaceHeight;
1719 mLocked.orientedRanges.y.flat = 0;
1720 mLocked.orientedRanges.y.fuzz = orientedYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001721 }
1722
1723 return true;
1724}
1725
Jeff Brownef3d7e82010-09-30 14:33:04 -07001726void TouchInputMapper::dumpSurfaceLocked(String8& dump) {
1727 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth);
1728 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight);
1729 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07001730}
1731
Jeff Brown6328cdc2010-07-29 18:18:33 -07001732void TouchInputMapper::configureVirtualKeysLocked() {
Jeff Brown8d608662010-08-30 03:02:23 -07001733 assert(mRawAxes.x.valid && mRawAxes.y.valid);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001734
Jeff Brown8d608662010-08-30 03:02:23 -07001735 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08001736 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001737
Jeff Brown6328cdc2010-07-29 18:18:33 -07001738 mLocked.virtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001739
Jeff Brown6328cdc2010-07-29 18:18:33 -07001740 if (virtualKeyDefinitions.size() == 0) {
1741 return;
1742 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001743
Jeff Brown6328cdc2010-07-29 18:18:33 -07001744 mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size());
1745
Jeff Brown8d608662010-08-30 03:02:23 -07001746 int32_t touchScreenLeft = mRawAxes.x.minValue;
1747 int32_t touchScreenTop = mRawAxes.y.minValue;
1748 int32_t touchScreenWidth = mRawAxes.x.getRange();
1749 int32_t touchScreenHeight = mRawAxes.y.getRange();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001750
1751 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07001752 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07001753 virtualKeyDefinitions[i];
1754
1755 mLocked.virtualKeys.add();
1756 VirtualKey& virtualKey = mLocked.virtualKeys.editTop();
1757
1758 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1759 int32_t keyCode;
1760 uint32_t flags;
1761 if (getEventHub()->scancodeToKeycode(getDeviceId(), virtualKey.scanCode,
1762 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07001763 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
1764 virtualKey.scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001765 mLocked.virtualKeys.pop(); // drop the key
1766 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001767 }
1768
Jeff Brown6328cdc2010-07-29 18:18:33 -07001769 virtualKey.keyCode = keyCode;
1770 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001771
Jeff Brown6328cdc2010-07-29 18:18:33 -07001772 // convert the key definition's display coordinates into touch coordinates for a hit box
1773 int32_t halfWidth = virtualKeyDefinition.width / 2;
1774 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001775
Jeff Brown6328cdc2010-07-29 18:18:33 -07001776 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
1777 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1778 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
1779 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1780 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
1781 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
1782 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
1783 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001784
Jeff Brownef3d7e82010-09-30 14:33:04 -07001785 }
1786}
1787
1788void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) {
1789 if (!mLocked.virtualKeys.isEmpty()) {
1790 dump.append(INDENT3 "Virtual Keys:\n");
1791
1792 for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) {
1793 const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i);
1794 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
1795 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1796 i, virtualKey.scanCode, virtualKey.keyCode,
1797 virtualKey.hitLeft, virtualKey.hitRight,
1798 virtualKey.hitTop, virtualKey.hitBottom);
1799 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001800 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001801}
1802
Jeff Brown8d608662010-08-30 03:02:23 -07001803void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001804 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07001805 Calibration& out = mCalibration;
1806
Jeff Brown511ee5f2010-10-18 13:32:20 -07001807 // Position
1808 out.haveXOrigin = in.tryGetProperty(String8("touch.position.xOrigin"), out.xOrigin);
1809 out.haveYOrigin = in.tryGetProperty(String8("touch.position.yOrigin"), out.yOrigin);
1810 out.haveXScale = in.tryGetProperty(String8("touch.position.xScale"), out.xScale);
1811 out.haveYScale = in.tryGetProperty(String8("touch.position.yScale"), out.yScale);
1812
Jeff Brownc6d282b2010-10-14 21:42:15 -07001813 // Touch Size
1814 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT;
1815 String8 touchSizeCalibrationString;
1816 if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) {
1817 if (touchSizeCalibrationString == "none") {
1818 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
1819 } else if (touchSizeCalibrationString == "geometric") {
1820 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC;
1821 } else if (touchSizeCalibrationString == "pressure") {
1822 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
1823 } else if (touchSizeCalibrationString != "default") {
1824 LOGW("Invalid value for touch.touchSize.calibration: '%s'",
1825 touchSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07001826 }
1827 }
1828
Jeff Brownc6d282b2010-10-14 21:42:15 -07001829 // Tool Size
1830 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT;
1831 String8 toolSizeCalibrationString;
1832 if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) {
1833 if (toolSizeCalibrationString == "none") {
1834 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
1835 } else if (toolSizeCalibrationString == "geometric") {
1836 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC;
1837 } else if (toolSizeCalibrationString == "linear") {
1838 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
1839 } else if (toolSizeCalibrationString == "area") {
1840 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA;
1841 } else if (toolSizeCalibrationString != "default") {
1842 LOGW("Invalid value for touch.toolSize.calibration: '%s'",
1843 toolSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07001844 }
1845 }
1846
Jeff Brownc6d282b2010-10-14 21:42:15 -07001847 out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"),
1848 out.toolSizeLinearScale);
1849 out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"),
1850 out.toolSizeLinearBias);
1851 out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"),
1852 out.toolSizeAreaScale);
1853 out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"),
1854 out.toolSizeAreaBias);
1855 out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"),
1856 out.toolSizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07001857
1858 // Pressure
1859 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
1860 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07001861 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07001862 if (pressureCalibrationString == "none") {
1863 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
1864 } else if (pressureCalibrationString == "physical") {
1865 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
1866 } else if (pressureCalibrationString == "amplitude") {
1867 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
1868 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07001869 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07001870 pressureCalibrationString.string());
1871 }
1872 }
1873
1874 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
1875 String8 pressureSourceString;
1876 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
1877 if (pressureSourceString == "pressure") {
1878 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
1879 } else if (pressureSourceString == "touch") {
1880 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
1881 } else if (pressureSourceString != "default") {
1882 LOGW("Invalid value for touch.pressure.source: '%s'",
1883 pressureSourceString.string());
1884 }
1885 }
1886
1887 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
1888 out.pressureScale);
1889
1890 // Size
1891 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
1892 String8 sizeCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07001893 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07001894 if (sizeCalibrationString == "none") {
1895 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
1896 } else if (sizeCalibrationString == "normalized") {
1897 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
1898 } else if (sizeCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07001899 LOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07001900 sizeCalibrationString.string());
1901 }
1902 }
1903
1904 // Orientation
1905 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
1906 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07001907 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07001908 if (orientationCalibrationString == "none") {
1909 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
1910 } else if (orientationCalibrationString == "interpolated") {
1911 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
1912 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07001913 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07001914 orientationCalibrationString.string());
1915 }
1916 }
1917}
1918
1919void TouchInputMapper::resolveCalibration() {
1920 // Pressure
1921 switch (mCalibration.pressureSource) {
1922 case Calibration::PRESSURE_SOURCE_DEFAULT:
1923 if (mRawAxes.pressure.valid) {
1924 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
1925 } else if (mRawAxes.touchMajor.valid) {
1926 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
1927 }
1928 break;
1929
1930 case Calibration::PRESSURE_SOURCE_PRESSURE:
1931 if (! mRawAxes.pressure.valid) {
1932 LOGW("Calibration property touch.pressure.source is 'pressure' but "
1933 "the pressure axis is not available.");
1934 }
1935 break;
1936
1937 case Calibration::PRESSURE_SOURCE_TOUCH:
1938 if (! mRawAxes.touchMajor.valid) {
1939 LOGW("Calibration property touch.pressure.source is 'touch' but "
1940 "the touchMajor axis is not available.");
1941 }
1942 break;
1943
1944 default:
1945 break;
1946 }
1947
1948 switch (mCalibration.pressureCalibration) {
1949 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
1950 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
1951 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
1952 } else {
1953 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
1954 }
1955 break;
1956
1957 default:
1958 break;
1959 }
1960
Jeff Brownc6d282b2010-10-14 21:42:15 -07001961 // Tool Size
1962 switch (mCalibration.toolSizeCalibration) {
1963 case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07001964 if (mRawAxes.toolMajor.valid) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07001965 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
Jeff Brown8d608662010-08-30 03:02:23 -07001966 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07001967 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07001968 }
1969 break;
1970
1971 default:
1972 break;
1973 }
1974
Jeff Brownc6d282b2010-10-14 21:42:15 -07001975 // Touch Size
1976 switch (mCalibration.touchSizeCalibration) {
1977 case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07001978 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
Jeff Brownc6d282b2010-10-14 21:42:15 -07001979 && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
1980 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
Jeff Brown8d608662010-08-30 03:02:23 -07001981 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07001982 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07001983 }
1984 break;
1985
1986 default:
1987 break;
1988 }
1989
1990 // Size
1991 switch (mCalibration.sizeCalibration) {
1992 case Calibration::SIZE_CALIBRATION_DEFAULT:
1993 if (mRawAxes.toolMajor.valid) {
1994 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
1995 } else {
1996 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
1997 }
1998 break;
1999
2000 default:
2001 break;
2002 }
2003
2004 // Orientation
2005 switch (mCalibration.orientationCalibration) {
2006 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
2007 if (mRawAxes.orientation.valid) {
2008 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
2009 } else {
2010 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2011 }
2012 break;
2013
2014 default:
2015 break;
2016 }
2017}
2018
Jeff Brownef3d7e82010-09-30 14:33:04 -07002019void TouchInputMapper::dumpCalibration(String8& dump) {
2020 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07002021
Jeff Brown511ee5f2010-10-18 13:32:20 -07002022 // Position
2023 if (mCalibration.haveXOrigin) {
2024 dump.appendFormat(INDENT4 "touch.position.xOrigin: %d\n", mCalibration.xOrigin);
2025 }
2026 if (mCalibration.haveYOrigin) {
2027 dump.appendFormat(INDENT4 "touch.position.yOrigin: %d\n", mCalibration.yOrigin);
2028 }
2029 if (mCalibration.haveXScale) {
2030 dump.appendFormat(INDENT4 "touch.position.xScale: %0.3f\n", mCalibration.xScale);
2031 }
2032 if (mCalibration.haveYScale) {
2033 dump.appendFormat(INDENT4 "touch.position.yScale: %0.3f\n", mCalibration.yScale);
2034 }
2035
Jeff Brownc6d282b2010-10-14 21:42:15 -07002036 // Touch Size
2037 switch (mCalibration.touchSizeCalibration) {
2038 case Calibration::TOUCH_SIZE_CALIBRATION_NONE:
2039 dump.append(INDENT4 "touch.touchSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002040 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002041 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
2042 dump.append(INDENT4 "touch.touchSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002043 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002044 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
2045 dump.append(INDENT4 "touch.touchSize.calibration: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002046 break;
2047 default:
2048 assert(false);
2049 }
2050
Jeff Brownc6d282b2010-10-14 21:42:15 -07002051 // Tool Size
2052 switch (mCalibration.toolSizeCalibration) {
2053 case Calibration::TOOL_SIZE_CALIBRATION_NONE:
2054 dump.append(INDENT4 "touch.toolSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002055 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002056 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
2057 dump.append(INDENT4 "touch.toolSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002058 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002059 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
2060 dump.append(INDENT4 "touch.toolSize.calibration: linear\n");
2061 break;
2062 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2063 dump.append(INDENT4 "touch.toolSize.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002064 break;
2065 default:
2066 assert(false);
2067 }
2068
Jeff Brownc6d282b2010-10-14 21:42:15 -07002069 if (mCalibration.haveToolSizeLinearScale) {
2070 dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n",
2071 mCalibration.toolSizeLinearScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002072 }
2073
Jeff Brownc6d282b2010-10-14 21:42:15 -07002074 if (mCalibration.haveToolSizeLinearBias) {
2075 dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n",
2076 mCalibration.toolSizeLinearBias);
Jeff Brown8d608662010-08-30 03:02:23 -07002077 }
2078
Jeff Brownc6d282b2010-10-14 21:42:15 -07002079 if (mCalibration.haveToolSizeAreaScale) {
2080 dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n",
2081 mCalibration.toolSizeAreaScale);
2082 }
2083
2084 if (mCalibration.haveToolSizeAreaBias) {
2085 dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n",
2086 mCalibration.toolSizeAreaBias);
2087 }
2088
2089 if (mCalibration.haveToolSizeIsSummed) {
Jeff Brown1f245102010-11-18 20:53:46 -08002090 dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %s\n",
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002091 toString(mCalibration.toolSizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07002092 }
2093
2094 // Pressure
2095 switch (mCalibration.pressureCalibration) {
2096 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002097 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002098 break;
2099 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002100 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002101 break;
2102 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002103 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002104 break;
2105 default:
2106 assert(false);
2107 }
2108
2109 switch (mCalibration.pressureSource) {
2110 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002111 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002112 break;
2113 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002114 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002115 break;
2116 case Calibration::PRESSURE_SOURCE_DEFAULT:
2117 break;
2118 default:
2119 assert(false);
2120 }
2121
2122 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07002123 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
2124 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002125 }
2126
2127 // Size
2128 switch (mCalibration.sizeCalibration) {
2129 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002130 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002131 break;
2132 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002133 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002134 break;
2135 default:
2136 assert(false);
2137 }
2138
2139 // Orientation
2140 switch (mCalibration.orientationCalibration) {
2141 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002142 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002143 break;
2144 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002145 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002146 break;
2147 default:
2148 assert(false);
2149 }
2150}
2151
Jeff Brown6d0fec22010-07-23 21:28:06 -07002152void TouchInputMapper::reset() {
2153 // Synthesize touch up event if touch is currently down.
2154 // This will also take care of finishing virtual key processing if needed.
2155 if (mLastTouch.pointerCount != 0) {
2156 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
2157 mCurrentTouch.clear();
2158 syncTouch(when, true);
2159 }
2160
Jeff Brown6328cdc2010-07-29 18:18:33 -07002161 { // acquire lock
2162 AutoMutex _l(mLock);
2163 initializeLocked();
2164 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07002165
Jeff Brown6328cdc2010-07-29 18:18:33 -07002166 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002167}
2168
2169void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002170 uint32_t policyFlags = 0;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002171
Jeff Brown6328cdc2010-07-29 18:18:33 -07002172 // Preprocess pointer data.
Jeff Brown46b9ac02010-04-22 18:58:52 -07002173
Jeff Brown6d0fec22010-07-23 21:28:06 -07002174 if (mParameters.useBadTouchFilter) {
2175 if (applyBadTouchFilter()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002176 havePointerIds = false;
2177 }
2178 }
2179
Jeff Brown6d0fec22010-07-23 21:28:06 -07002180 if (mParameters.useJumpyTouchFilter) {
2181 if (applyJumpyTouchFilter()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002182 havePointerIds = false;
2183 }
2184 }
2185
2186 if (! havePointerIds) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002187 calculatePointerIds();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002188 }
2189
Jeff Brown6d0fec22010-07-23 21:28:06 -07002190 TouchData temp;
2191 TouchData* savedTouch;
2192 if (mParameters.useAveragingTouchFilter) {
2193 temp.copyFrom(mCurrentTouch);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002194 savedTouch = & temp;
2195
Jeff Brown6d0fec22010-07-23 21:28:06 -07002196 applyAveragingTouchFilter();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002197 } else {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002198 savedTouch = & mCurrentTouch;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002199 }
2200
Jeff Brown6328cdc2010-07-29 18:18:33 -07002201 // Process touches and virtual keys.
Jeff Brown46b9ac02010-04-22 18:58:52 -07002202
Jeff Brown6d0fec22010-07-23 21:28:06 -07002203 TouchResult touchResult = consumeOffScreenTouches(when, policyFlags);
2204 if (touchResult == DISPATCH_TOUCH) {
2205 dispatchTouches(when, policyFlags);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002206 }
2207
Jeff Brown6328cdc2010-07-29 18:18:33 -07002208 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brown46b9ac02010-04-22 18:58:52 -07002209
Jeff Brown6d0fec22010-07-23 21:28:06 -07002210 if (touchResult == DROP_STROKE) {
2211 mLastTouch.clear();
2212 } else {
2213 mLastTouch.copyFrom(*savedTouch);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002214 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002215}
2216
Jeff Brown6d0fec22010-07-23 21:28:06 -07002217TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches(
2218 nsecs_t when, uint32_t policyFlags) {
2219 int32_t keyEventAction, keyEventFlags;
2220 int32_t keyCode, scanCode, downTime;
2221 TouchResult touchResult;
Jeff Brown349703e2010-06-22 01:27:15 -07002222
Jeff Brown6328cdc2010-07-29 18:18:33 -07002223 { // acquire lock
2224 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002225
Jeff Brown6328cdc2010-07-29 18:18:33 -07002226 // Update surface size and orientation, including virtual key positions.
2227 if (! configureSurfaceLocked()) {
2228 return DROP_STROKE;
2229 }
2230
2231 // Check for virtual key press.
2232 if (mLocked.currentVirtualKey.down) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002233 if (mCurrentTouch.pointerCount == 0) {
2234 // Pointer went up while virtual key was down.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002235 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002236#if DEBUG_VIRTUAL_KEYS
2237 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07002238 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002239#endif
2240 keyEventAction = AKEY_EVENT_ACTION_UP;
2241 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2242 touchResult = SKIP_TOUCH;
2243 goto DispatchVirtualKey;
2244 }
2245
2246 if (mCurrentTouch.pointerCount == 1) {
2247 int32_t x = mCurrentTouch.pointers[0].x;
2248 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002249 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
2250 if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002251 // Pointer is still within the space of the virtual key.
2252 return SKIP_TOUCH;
2253 }
2254 }
2255
2256 // Pointer left virtual key area or another pointer also went down.
2257 // Send key cancellation and drop the stroke so subsequent motions will be
2258 // considered fresh downs. This is useful when the user swipes away from the
2259 // virtual key area into the main display surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002260 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002261#if DEBUG_VIRTUAL_KEYS
2262 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07002263 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002264#endif
2265 keyEventAction = AKEY_EVENT_ACTION_UP;
2266 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
2267 | AKEY_EVENT_FLAG_CANCELED;
Jeff Brownc3db8582010-10-20 15:33:38 -07002268
2269 // Check whether the pointer moved inside the display area where we should
2270 // start a new stroke.
2271 int32_t x = mCurrentTouch.pointers[0].x;
2272 int32_t y = mCurrentTouch.pointers[0].y;
2273 if (isPointInsideSurfaceLocked(x, y)) {
2274 mLastTouch.clear();
2275 touchResult = DISPATCH_TOUCH;
2276 } else {
2277 touchResult = DROP_STROKE;
2278 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002279 } else {
2280 if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) {
2281 // Pointer just went down. Handle off-screen touches, if needed.
2282 int32_t x = mCurrentTouch.pointers[0].x;
2283 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002284 if (! isPointInsideSurfaceLocked(x, y)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002285 // If exactly one pointer went down, check for virtual key hit.
2286 // Otherwise we will drop the entire stroke.
2287 if (mCurrentTouch.pointerCount == 1) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002288 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002289 if (virtualKey) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002290 mLocked.currentVirtualKey.down = true;
2291 mLocked.currentVirtualKey.downTime = when;
2292 mLocked.currentVirtualKey.keyCode = virtualKey->keyCode;
2293 mLocked.currentVirtualKey.scanCode = virtualKey->scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002294#if DEBUG_VIRTUAL_KEYS
2295 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07002296 mLocked.currentVirtualKey.keyCode,
2297 mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002298#endif
2299 keyEventAction = AKEY_EVENT_ACTION_DOWN;
2300 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM
2301 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2302 touchResult = SKIP_TOUCH;
2303 goto DispatchVirtualKey;
2304 }
2305 }
2306 return DROP_STROKE;
2307 }
2308 }
2309 return DISPATCH_TOUCH;
2310 }
2311
2312 DispatchVirtualKey:
2313 // Collect remaining state needed to dispatch virtual key.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002314 keyCode = mLocked.currentVirtualKey.keyCode;
2315 scanCode = mLocked.currentVirtualKey.scanCode;
2316 downTime = mLocked.currentVirtualKey.downTime;
2317 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07002318
2319 // Dispatch virtual key.
2320 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown0eaf3932010-10-01 14:55:30 -07002321 policyFlags |= POLICY_FLAG_VIRTUAL;
Jeff Brownb6997262010-10-08 22:31:17 -07002322 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
2323 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
2324 return touchResult;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002325}
2326
Jeff Brown6d0fec22010-07-23 21:28:06 -07002327void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
2328 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2329 uint32_t lastPointerCount = mLastTouch.pointerCount;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002330 if (currentPointerCount == 0 && lastPointerCount == 0) {
2331 return; // nothing to do!
2332 }
2333
Jeff Brown6d0fec22010-07-23 21:28:06 -07002334 BitSet32 currentIdBits = mCurrentTouch.idBits;
2335 BitSet32 lastIdBits = mLastTouch.idBits;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002336
2337 if (currentIdBits == lastIdBits) {
2338 // No pointer id changes so this is a move event.
2339 // The dispatcher takes care of batching moves so we don't have to deal with that here.
Jeff Brownc5ed5912010-07-14 18:48:53 -07002340 int32_t motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002341 dispatchTouch(when, policyFlags, & mCurrentTouch,
Jeff Brown8d608662010-08-30 03:02:23 -07002342 currentIdBits, -1, currentPointerCount, motionEventAction);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002343 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07002344 // There may be pointers going up and pointers going down and pointers moving
2345 // all at the same time.
Jeff Brown46b9ac02010-04-22 18:58:52 -07002346 BitSet32 upIdBits(lastIdBits.value & ~ currentIdBits.value);
2347 BitSet32 downIdBits(currentIdBits.value & ~ lastIdBits.value);
2348 BitSet32 activeIdBits(lastIdBits.value);
Jeff Brown8d608662010-08-30 03:02:23 -07002349 uint32_t pointerCount = lastPointerCount;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002350
Jeff Brownc3db8582010-10-20 15:33:38 -07002351 // Produce an intermediate representation of the touch data that consists of the
2352 // old location of pointers that have just gone up and the new location of pointers that
2353 // have just moved but omits the location of pointers that have just gone down.
2354 TouchData interimTouch;
2355 interimTouch.copyFrom(mLastTouch);
2356
2357 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
2358 bool moveNeeded = false;
2359 while (!moveIdBits.isEmpty()) {
2360 uint32_t moveId = moveIdBits.firstMarkedBit();
2361 moveIdBits.clearBit(moveId);
2362
2363 int32_t oldIndex = mLastTouch.idToIndex[moveId];
2364 int32_t newIndex = mCurrentTouch.idToIndex[moveId];
2365 if (mLastTouch.pointers[oldIndex] != mCurrentTouch.pointers[newIndex]) {
2366 interimTouch.pointers[oldIndex] = mCurrentTouch.pointers[newIndex];
2367 moveNeeded = true;
2368 }
2369 }
2370
2371 // Dispatch pointer up events using the interim pointer locations.
2372 while (!upIdBits.isEmpty()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002373 uint32_t upId = upIdBits.firstMarkedBit();
2374 upIdBits.clearBit(upId);
2375 BitSet32 oldActiveIdBits = activeIdBits;
2376 activeIdBits.clearBit(upId);
2377
2378 int32_t motionEventAction;
2379 if (activeIdBits.isEmpty()) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07002380 motionEventAction = AMOTION_EVENT_ACTION_UP;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002381 } else {
Jeff Brown00ba8842010-07-16 15:01:56 -07002382 motionEventAction = AMOTION_EVENT_ACTION_POINTER_UP;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002383 }
2384
Jeff Brownc3db8582010-10-20 15:33:38 -07002385 dispatchTouch(when, policyFlags, &interimTouch,
Jeff Brown8d608662010-08-30 03:02:23 -07002386 oldActiveIdBits, upId, pointerCount, motionEventAction);
2387 pointerCount -= 1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002388 }
2389
Jeff Brownc3db8582010-10-20 15:33:38 -07002390 // Dispatch move events if any of the remaining pointers moved from their old locations.
2391 // Although applications receive new locations as part of individual pointer up
2392 // events, they do not generally handle them except when presented in a move event.
2393 if (moveNeeded) {
2394 dispatchTouch(when, policyFlags, &mCurrentTouch,
2395 activeIdBits, -1, pointerCount, AMOTION_EVENT_ACTION_MOVE);
2396 }
2397
2398 // Dispatch pointer down events using the new pointer locations.
2399 while (!downIdBits.isEmpty()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002400 uint32_t downId = downIdBits.firstMarkedBit();
2401 downIdBits.clearBit(downId);
2402 BitSet32 oldActiveIdBits = activeIdBits;
2403 activeIdBits.markBit(downId);
2404
2405 int32_t motionEventAction;
2406 if (oldActiveIdBits.isEmpty()) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07002407 motionEventAction = AMOTION_EVENT_ACTION_DOWN;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002408 mDownTime = when;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002409 } else {
Jeff Brown00ba8842010-07-16 15:01:56 -07002410 motionEventAction = AMOTION_EVENT_ACTION_POINTER_DOWN;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002411 }
2412
Jeff Brown8d608662010-08-30 03:02:23 -07002413 pointerCount += 1;
Jeff Brownc3db8582010-10-20 15:33:38 -07002414 dispatchTouch(when, policyFlags, &mCurrentTouch,
Jeff Brown8d608662010-08-30 03:02:23 -07002415 activeIdBits, downId, pointerCount, motionEventAction);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002416 }
2417 }
2418}
2419
Jeff Brown6d0fec22010-07-23 21:28:06 -07002420void TouchInputMapper::dispatchTouch(nsecs_t when, uint32_t policyFlags,
Jeff Brown8d608662010-08-30 03:02:23 -07002421 TouchData* touch, BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
Jeff Brown46b9ac02010-04-22 18:58:52 -07002422 int32_t motionEventAction) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002423 int32_t pointerIds[MAX_POINTERS];
2424 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brown46b9ac02010-04-22 18:58:52 -07002425 int32_t motionEventEdgeFlags = 0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002426 float xPrecision, yPrecision;
2427
2428 { // acquire lock
2429 AutoMutex _l(mLock);
2430
2431 // Walk through the the active pointers and map touch screen coordinates (TouchData) into
2432 // display coordinates (PointerCoords) and adjust for display orientation.
Jeff Brown8d608662010-08-30 03:02:23 -07002433 for (uint32_t outIndex = 0; ! idBits.isEmpty(); outIndex++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002434 uint32_t id = idBits.firstMarkedBit();
2435 idBits.clearBit(id);
Jeff Brown8d608662010-08-30 03:02:23 -07002436 uint32_t inIndex = touch->idToIndex[id];
Jeff Brown6328cdc2010-07-29 18:18:33 -07002437
Jeff Brown8d608662010-08-30 03:02:23 -07002438 const PointerData& in = touch->pointers[inIndex];
Jeff Brown6328cdc2010-07-29 18:18:33 -07002439
Jeff Brown8d608662010-08-30 03:02:23 -07002440 // X and Y
2441 float x = float(in.x - mLocked.xOrigin) * mLocked.xScale;
2442 float y = float(in.y - mLocked.yOrigin) * mLocked.yScale;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002443
Jeff Brown8d608662010-08-30 03:02:23 -07002444 // ToolMajor and ToolMinor
2445 float toolMajor, toolMinor;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002446 switch (mCalibration.toolSizeCalibration) {
2447 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brown8d608662010-08-30 03:02:23 -07002448 toolMajor = in.toolMajor * mLocked.geometricScale;
2449 if (mRawAxes.toolMinor.valid) {
2450 toolMinor = in.toolMinor * mLocked.geometricScale;
2451 } else {
2452 toolMinor = toolMajor;
2453 }
2454 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002455 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
Jeff Brown8d608662010-08-30 03:02:23 -07002456 toolMajor = in.toolMajor != 0
Jeff Brownc6d282b2010-10-14 21:42:15 -07002457 ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias
Jeff Brown8d608662010-08-30 03:02:23 -07002458 : 0;
2459 if (mRawAxes.toolMinor.valid) {
2460 toolMinor = in.toolMinor != 0
Jeff Brownc6d282b2010-10-14 21:42:15 -07002461 ? in.toolMinor * mLocked.toolSizeLinearScale
2462 + mLocked.toolSizeLinearBias
Jeff Brown8d608662010-08-30 03:02:23 -07002463 : 0;
2464 } else {
2465 toolMinor = toolMajor;
2466 }
2467 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002468 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2469 if (in.toolMajor != 0) {
2470 float diameter = sqrtf(in.toolMajor
2471 * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias);
2472 toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias;
2473 } else {
2474 toolMajor = 0;
2475 }
2476 toolMinor = toolMajor;
2477 break;
Jeff Brown8d608662010-08-30 03:02:23 -07002478 default:
2479 toolMajor = 0;
2480 toolMinor = 0;
2481 break;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002482 }
2483
Jeff Brownc6d282b2010-10-14 21:42:15 -07002484 if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) {
Jeff Brown8d608662010-08-30 03:02:23 -07002485 toolMajor /= pointerCount;
2486 toolMinor /= pointerCount;
2487 }
2488
2489 // Pressure
2490 float rawPressure;
2491 switch (mCalibration.pressureSource) {
2492 case Calibration::PRESSURE_SOURCE_PRESSURE:
2493 rawPressure = in.pressure;
2494 break;
2495 case Calibration::PRESSURE_SOURCE_TOUCH:
2496 rawPressure = in.touchMajor;
2497 break;
2498 default:
2499 rawPressure = 0;
2500 }
2501
2502 float pressure;
2503 switch (mCalibration.pressureCalibration) {
2504 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
2505 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
2506 pressure = rawPressure * mLocked.pressureScale;
2507 break;
2508 default:
2509 pressure = 1;
2510 break;
2511 }
2512
2513 // TouchMajor and TouchMinor
2514 float touchMajor, touchMinor;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002515 switch (mCalibration.touchSizeCalibration) {
2516 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brown8d608662010-08-30 03:02:23 -07002517 touchMajor = in.touchMajor * mLocked.geometricScale;
2518 if (mRawAxes.touchMinor.valid) {
2519 touchMinor = in.touchMinor * mLocked.geometricScale;
2520 } else {
2521 touchMinor = touchMajor;
2522 }
2523 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002524 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
Jeff Brown8d608662010-08-30 03:02:23 -07002525 touchMajor = toolMajor * pressure;
2526 touchMinor = toolMinor * pressure;
2527 break;
2528 default:
2529 touchMajor = 0;
2530 touchMinor = 0;
2531 break;
2532 }
2533
2534 if (touchMajor > toolMajor) {
2535 touchMajor = toolMajor;
2536 }
2537 if (touchMinor > toolMinor) {
2538 touchMinor = toolMinor;
2539 }
2540
2541 // Size
2542 float size;
2543 switch (mCalibration.sizeCalibration) {
2544 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
2545 float rawSize = mRawAxes.toolMinor.valid
2546 ? avg(in.toolMajor, in.toolMinor)
2547 : in.toolMajor;
2548 size = rawSize * mLocked.sizeScale;
2549 break;
2550 }
2551 default:
2552 size = 0;
2553 break;
2554 }
2555
2556 // Orientation
2557 float orientation;
2558 switch (mCalibration.orientationCalibration) {
2559 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
2560 orientation = in.orientation * mLocked.orientationScale;
2561 break;
2562 default:
2563 orientation = 0;
2564 }
2565
2566 // Adjust coords for orientation.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002567 switch (mLocked.surfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002568 case DISPLAY_ORIENTATION_90: {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002569 float xTemp = x;
2570 x = y;
2571 y = mLocked.surfaceWidth - xTemp;
2572 orientation -= M_PI_2;
2573 if (orientation < - M_PI_2) {
2574 orientation += M_PI;
2575 }
2576 break;
2577 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002578 case DISPLAY_ORIENTATION_180: {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002579 x = mLocked.surfaceWidth - x;
2580 y = mLocked.surfaceHeight - y;
2581 orientation = - orientation;
2582 break;
2583 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002584 case DISPLAY_ORIENTATION_270: {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002585 float xTemp = x;
2586 x = mLocked.surfaceHeight - y;
2587 y = xTemp;
2588 orientation += M_PI_2;
2589 if (orientation > M_PI_2) {
2590 orientation -= M_PI;
2591 }
2592 break;
2593 }
2594 }
2595
Jeff Brown8d608662010-08-30 03:02:23 -07002596 // Write output coords.
2597 PointerCoords& out = pointerCoords[outIndex];
2598 out.x = x;
2599 out.y = y;
2600 out.pressure = pressure;
2601 out.size = size;
2602 out.touchMajor = touchMajor;
2603 out.touchMinor = touchMinor;
2604 out.toolMajor = toolMajor;
2605 out.toolMinor = toolMinor;
2606 out.orientation = orientation;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002607
Jeff Brown8d608662010-08-30 03:02:23 -07002608 pointerIds[outIndex] = int32_t(id);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002609
2610 if (id == changedId) {
Jeff Brown8d608662010-08-30 03:02:23 -07002611 motionEventAction |= outIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002612 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002613 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002614
2615 // Check edge flags by looking only at the first pointer since the flags are
2616 // global to the event.
2617 if (motionEventAction == AMOTION_EVENT_ACTION_DOWN) {
2618 if (pointerCoords[0].x <= 0) {
2619 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_LEFT;
2620 } else if (pointerCoords[0].x >= mLocked.orientedSurfaceWidth) {
2621 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_RIGHT;
2622 }
2623 if (pointerCoords[0].y <= 0) {
2624 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_TOP;
2625 } else if (pointerCoords[0].y >= mLocked.orientedSurfaceHeight) {
2626 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_BOTTOM;
2627 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002628 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002629
2630 xPrecision = mLocked.orientedXPrecision;
2631 yPrecision = mLocked.orientedYPrecision;
2632 } // release lock
Jeff Brown46b9ac02010-04-22 18:58:52 -07002633
Jeff Brown83c09682010-12-23 17:50:18 -08002634 getDispatcher()->notifyMotion(when, getDeviceId(), mSources, policyFlags,
Jeff Brown85a31762010-09-01 17:01:00 -07002635 motionEventAction, 0, getContext()->getGlobalMetaState(), motionEventEdgeFlags,
Jeff Brown46b9ac02010-04-22 18:58:52 -07002636 pointerCount, pointerIds, pointerCoords,
Jeff Brown6328cdc2010-07-29 18:18:33 -07002637 xPrecision, yPrecision, mDownTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002638}
2639
Jeff Brown6328cdc2010-07-29 18:18:33 -07002640bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) {
Jeff Brown8d608662010-08-30 03:02:23 -07002641 if (mRawAxes.x.valid && mRawAxes.y.valid) {
2642 return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue
2643 && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002644 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002645 return true;
2646}
2647
Jeff Brown6328cdc2010-07-29 18:18:33 -07002648const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked(
2649 int32_t x, int32_t y) {
2650 size_t numVirtualKeys = mLocked.virtualKeys.size();
2651 for (size_t i = 0; i < numVirtualKeys; i++) {
2652 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07002653
2654#if DEBUG_VIRTUAL_KEYS
2655 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
2656 "left=%d, top=%d, right=%d, bottom=%d",
2657 x, y,
2658 virtualKey.keyCode, virtualKey.scanCode,
2659 virtualKey.hitLeft, virtualKey.hitTop,
2660 virtualKey.hitRight, virtualKey.hitBottom);
2661#endif
2662
2663 if (virtualKey.isHit(x, y)) {
2664 return & virtualKey;
2665 }
2666 }
2667
2668 return NULL;
2669}
2670
2671void TouchInputMapper::calculatePointerIds() {
2672 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2673 uint32_t lastPointerCount = mLastTouch.pointerCount;
2674
2675 if (currentPointerCount == 0) {
2676 // No pointers to assign.
2677 mCurrentTouch.idBits.clear();
2678 } else if (lastPointerCount == 0) {
2679 // All pointers are new.
2680 mCurrentTouch.idBits.clear();
2681 for (uint32_t i = 0; i < currentPointerCount; i++) {
2682 mCurrentTouch.pointers[i].id = i;
2683 mCurrentTouch.idToIndex[i] = i;
2684 mCurrentTouch.idBits.markBit(i);
2685 }
2686 } else if (currentPointerCount == 1 && lastPointerCount == 1) {
2687 // Only one pointer and no change in count so it must have the same id as before.
2688 uint32_t id = mLastTouch.pointers[0].id;
2689 mCurrentTouch.pointers[0].id = id;
2690 mCurrentTouch.idToIndex[id] = 0;
2691 mCurrentTouch.idBits.value = BitSet32::valueForBit(id);
2692 } else {
2693 // General case.
2694 // We build a heap of squared euclidean distances between current and last pointers
2695 // associated with the current and last pointer indices. Then, we find the best
2696 // match (by distance) for each current pointer.
2697 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
2698
2699 uint32_t heapSize = 0;
2700 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
2701 currentPointerIndex++) {
2702 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
2703 lastPointerIndex++) {
2704 int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x
2705 - mLastTouch.pointers[lastPointerIndex].x;
2706 int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y
2707 - mLastTouch.pointers[lastPointerIndex].y;
2708
2709 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
2710
2711 // Insert new element into the heap (sift up).
2712 heap[heapSize].currentPointerIndex = currentPointerIndex;
2713 heap[heapSize].lastPointerIndex = lastPointerIndex;
2714 heap[heapSize].distance = distance;
2715 heapSize += 1;
2716 }
2717 }
2718
2719 // Heapify
2720 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
2721 startIndex -= 1;
2722 for (uint32_t parentIndex = startIndex; ;) {
2723 uint32_t childIndex = parentIndex * 2 + 1;
2724 if (childIndex >= heapSize) {
2725 break;
2726 }
2727
2728 if (childIndex + 1 < heapSize
2729 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2730 childIndex += 1;
2731 }
2732
2733 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2734 break;
2735 }
2736
2737 swap(heap[parentIndex], heap[childIndex]);
2738 parentIndex = childIndex;
2739 }
2740 }
2741
2742#if DEBUG_POINTER_ASSIGNMENT
2743 LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize);
2744 for (size_t i = 0; i < heapSize; i++) {
2745 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2746 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2747 heap[i].distance);
2748 }
2749#endif
2750
2751 // Pull matches out by increasing order of distance.
2752 // To avoid reassigning pointers that have already been matched, the loop keeps track
2753 // of which last and current pointers have been matched using the matchedXXXBits variables.
2754 // It also tracks the used pointer id bits.
2755 BitSet32 matchedLastBits(0);
2756 BitSet32 matchedCurrentBits(0);
2757 BitSet32 usedIdBits(0);
2758 bool first = true;
2759 for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) {
2760 for (;;) {
2761 if (first) {
2762 // The first time through the loop, we just consume the root element of
2763 // the heap (the one with smallest distance).
2764 first = false;
2765 } else {
2766 // Previous iterations consumed the root element of the heap.
2767 // Pop root element off of the heap (sift down).
2768 heapSize -= 1;
2769 assert(heapSize > 0);
2770
2771 // Sift down.
2772 heap[0] = heap[heapSize];
2773 for (uint32_t parentIndex = 0; ;) {
2774 uint32_t childIndex = parentIndex * 2 + 1;
2775 if (childIndex >= heapSize) {
2776 break;
2777 }
2778
2779 if (childIndex + 1 < heapSize
2780 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2781 childIndex += 1;
2782 }
2783
2784 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2785 break;
2786 }
2787
2788 swap(heap[parentIndex], heap[childIndex]);
2789 parentIndex = childIndex;
2790 }
2791
2792#if DEBUG_POINTER_ASSIGNMENT
2793 LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize);
2794 for (size_t i = 0; i < heapSize; i++) {
2795 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2796 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2797 heap[i].distance);
2798 }
2799#endif
2800 }
2801
2802 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
2803 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
2804
2805 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
2806 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
2807
2808 matchedCurrentBits.markBit(currentPointerIndex);
2809 matchedLastBits.markBit(lastPointerIndex);
2810
2811 uint32_t id = mLastTouch.pointers[lastPointerIndex].id;
2812 mCurrentTouch.pointers[currentPointerIndex].id = id;
2813 mCurrentTouch.idToIndex[id] = currentPointerIndex;
2814 usedIdBits.markBit(id);
2815
2816#if DEBUG_POINTER_ASSIGNMENT
2817 LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
2818 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
2819#endif
2820 break;
2821 }
2822 }
2823
2824 // Assign fresh ids to new pointers.
2825 if (currentPointerCount > lastPointerCount) {
2826 for (uint32_t i = currentPointerCount - lastPointerCount; ;) {
2827 uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit();
2828 uint32_t id = usedIdBits.firstUnmarkedBit();
2829
2830 mCurrentTouch.pointers[currentPointerIndex].id = id;
2831 mCurrentTouch.idToIndex[id] = currentPointerIndex;
2832 usedIdBits.markBit(id);
2833
2834#if DEBUG_POINTER_ASSIGNMENT
2835 LOGD("calculatePointerIds - assigned: cur=%d, id=%d",
2836 currentPointerIndex, id);
2837#endif
2838
2839 if (--i == 0) break; // done
2840 matchedCurrentBits.markBit(currentPointerIndex);
2841 }
2842 }
2843
2844 // Fix id bits.
2845 mCurrentTouch.idBits = usedIdBits;
2846 }
2847}
2848
2849/* Special hack for devices that have bad screen data: if one of the
2850 * points has moved more than a screen height from the last position,
2851 * then drop it. */
2852bool TouchInputMapper::applyBadTouchFilter() {
2853 // This hack requires valid axis parameters.
Jeff Brown8d608662010-08-30 03:02:23 -07002854 if (! mRawAxes.y.valid) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002855 return false;
2856 }
2857
2858 uint32_t pointerCount = mCurrentTouch.pointerCount;
2859
2860 // Nothing to do if there are no points.
2861 if (pointerCount == 0) {
2862 return false;
2863 }
2864
2865 // Don't do anything if a finger is going down or up. We run
2866 // here before assigning pointer IDs, so there isn't a good
2867 // way to do per-finger matching.
2868 if (pointerCount != mLastTouch.pointerCount) {
2869 return false;
2870 }
2871
2872 // We consider a single movement across more than a 7/16 of
2873 // the long size of the screen to be bad. This was a magic value
2874 // determined by looking at the maximum distance it is feasible
2875 // to actually move in one sample.
Jeff Brown8d608662010-08-30 03:02:23 -07002876 int32_t maxDeltaY = mRawAxes.y.getRange() * 7 / 16;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002877
2878 // XXX The original code in InputDevice.java included commented out
2879 // code for testing the X axis. Note that when we drop a point
2880 // we don't actually restore the old X either. Strange.
2881 // The old code also tries to track when bad points were previously
2882 // detected but it turns out that due to the placement of a "break"
2883 // at the end of the loop, we never set mDroppedBadPoint to true
2884 // so it is effectively dead code.
2885 // Need to figure out if the old code is busted or just overcomplicated
2886 // but working as intended.
2887
2888 // Look through all new points and see if any are farther than
2889 // acceptable from all previous points.
2890 for (uint32_t i = pointerCount; i-- > 0; ) {
2891 int32_t y = mCurrentTouch.pointers[i].y;
2892 int32_t closestY = INT_MAX;
2893 int32_t closestDeltaY = 0;
2894
2895#if DEBUG_HACKS
2896 LOGD("BadTouchFilter: Looking at next point #%d: y=%d", i, y);
2897#endif
2898
2899 for (uint32_t j = pointerCount; j-- > 0; ) {
2900 int32_t lastY = mLastTouch.pointers[j].y;
2901 int32_t deltaY = abs(y - lastY);
2902
2903#if DEBUG_HACKS
2904 LOGD("BadTouchFilter: Comparing with last point #%d: y=%d deltaY=%d",
2905 j, lastY, deltaY);
2906#endif
2907
2908 if (deltaY < maxDeltaY) {
2909 goto SkipSufficientlyClosePoint;
2910 }
2911 if (deltaY < closestDeltaY) {
2912 closestDeltaY = deltaY;
2913 closestY = lastY;
2914 }
2915 }
2916
2917 // Must not have found a close enough match.
2918#if DEBUG_HACKS
2919 LOGD("BadTouchFilter: Dropping bad point #%d: newY=%d oldY=%d deltaY=%d maxDeltaY=%d",
2920 i, y, closestY, closestDeltaY, maxDeltaY);
2921#endif
2922
2923 mCurrentTouch.pointers[i].y = closestY;
2924 return true; // XXX original code only corrects one point
2925
2926 SkipSufficientlyClosePoint: ;
2927 }
2928
2929 // No change.
2930 return false;
2931}
2932
2933/* Special hack for devices that have bad screen data: drop points where
2934 * the coordinate value for one axis has jumped to the other pointer's location.
2935 */
2936bool TouchInputMapper::applyJumpyTouchFilter() {
2937 // This hack requires valid axis parameters.
Jeff Brown8d608662010-08-30 03:02:23 -07002938 if (! mRawAxes.y.valid) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002939 return false;
2940 }
2941
2942 uint32_t pointerCount = mCurrentTouch.pointerCount;
2943 if (mLastTouch.pointerCount != pointerCount) {
2944#if DEBUG_HACKS
2945 LOGD("JumpyTouchFilter: Different pointer count %d -> %d",
2946 mLastTouch.pointerCount, pointerCount);
2947 for (uint32_t i = 0; i < pointerCount; i++) {
2948 LOGD(" Pointer %d (%d, %d)", i,
2949 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
2950 }
2951#endif
2952
2953 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_TRANSITION_DROPS) {
2954 if (mLastTouch.pointerCount == 1 && pointerCount == 2) {
2955 // Just drop the first few events going from 1 to 2 pointers.
2956 // They're bad often enough that they're not worth considering.
2957 mCurrentTouch.pointerCount = 1;
2958 mJumpyTouchFilter.jumpyPointsDropped += 1;
2959
2960#if DEBUG_HACKS
2961 LOGD("JumpyTouchFilter: Pointer 2 dropped");
2962#endif
2963 return true;
2964 } else if (mLastTouch.pointerCount == 2 && pointerCount == 1) {
2965 // The event when we go from 2 -> 1 tends to be messed up too
2966 mCurrentTouch.pointerCount = 2;
2967 mCurrentTouch.pointers[0] = mLastTouch.pointers[0];
2968 mCurrentTouch.pointers[1] = mLastTouch.pointers[1];
2969 mJumpyTouchFilter.jumpyPointsDropped += 1;
2970
2971#if DEBUG_HACKS
2972 for (int32_t i = 0; i < 2; i++) {
2973 LOGD("JumpyTouchFilter: Pointer %d replaced (%d, %d)", i,
2974 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
2975 }
2976#endif
2977 return true;
2978 }
2979 }
2980 // Reset jumpy points dropped on other transitions or if limit exceeded.
2981 mJumpyTouchFilter.jumpyPointsDropped = 0;
2982
2983#if DEBUG_HACKS
2984 LOGD("JumpyTouchFilter: Transition - drop limit reset");
2985#endif
2986 return false;
2987 }
2988
2989 // We have the same number of pointers as last time.
2990 // A 'jumpy' point is one where the coordinate value for one axis
2991 // has jumped to the other pointer's location. No need to do anything
2992 // else if we only have one pointer.
2993 if (pointerCount < 2) {
2994 return false;
2995 }
2996
2997 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_DROP_LIMIT) {
Jeff Brown8d608662010-08-30 03:02:23 -07002998 int jumpyEpsilon = mRawAxes.y.getRange() / JUMPY_EPSILON_DIVISOR;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002999
3000 // We only replace the single worst jumpy point as characterized by pointer distance
3001 // in a single axis.
3002 int32_t badPointerIndex = -1;
3003 int32_t badPointerReplacementIndex = -1;
3004 int32_t badPointerDistance = INT_MIN; // distance to be corrected
3005
3006 for (uint32_t i = pointerCount; i-- > 0; ) {
3007 int32_t x = mCurrentTouch.pointers[i].x;
3008 int32_t y = mCurrentTouch.pointers[i].y;
3009
3010#if DEBUG_HACKS
3011 LOGD("JumpyTouchFilter: Point %d (%d, %d)", i, x, y);
3012#endif
3013
3014 // Check if a touch point is too close to another's coordinates
3015 bool dropX = false, dropY = false;
3016 for (uint32_t j = 0; j < pointerCount; j++) {
3017 if (i == j) {
3018 continue;
3019 }
3020
3021 if (abs(x - mCurrentTouch.pointers[j].x) <= jumpyEpsilon) {
3022 dropX = true;
3023 break;
3024 }
3025
3026 if (abs(y - mCurrentTouch.pointers[j].y) <= jumpyEpsilon) {
3027 dropY = true;
3028 break;
3029 }
3030 }
3031 if (! dropX && ! dropY) {
3032 continue; // not jumpy
3033 }
3034
3035 // Find a replacement candidate by comparing with older points on the
3036 // complementary (non-jumpy) axis.
3037 int32_t distance = INT_MIN; // distance to be corrected
3038 int32_t replacementIndex = -1;
3039
3040 if (dropX) {
3041 // X looks too close. Find an older replacement point with a close Y.
3042 int32_t smallestDeltaY = INT_MAX;
3043 for (uint32_t j = 0; j < pointerCount; j++) {
3044 int32_t deltaY = abs(y - mLastTouch.pointers[j].y);
3045 if (deltaY < smallestDeltaY) {
3046 smallestDeltaY = deltaY;
3047 replacementIndex = j;
3048 }
3049 }
3050 distance = abs(x - mLastTouch.pointers[replacementIndex].x);
3051 } else {
3052 // Y looks too close. Find an older replacement point with a close X.
3053 int32_t smallestDeltaX = INT_MAX;
3054 for (uint32_t j = 0; j < pointerCount; j++) {
3055 int32_t deltaX = abs(x - mLastTouch.pointers[j].x);
3056 if (deltaX < smallestDeltaX) {
3057 smallestDeltaX = deltaX;
3058 replacementIndex = j;
3059 }
3060 }
3061 distance = abs(y - mLastTouch.pointers[replacementIndex].y);
3062 }
3063
3064 // If replacing this pointer would correct a worse error than the previous ones
3065 // considered, then use this replacement instead.
3066 if (distance > badPointerDistance) {
3067 badPointerIndex = i;
3068 badPointerReplacementIndex = replacementIndex;
3069 badPointerDistance = distance;
3070 }
3071 }
3072
3073 // Correct the jumpy pointer if one was found.
3074 if (badPointerIndex >= 0) {
3075#if DEBUG_HACKS
3076 LOGD("JumpyTouchFilter: Replacing bad pointer %d with (%d, %d)",
3077 badPointerIndex,
3078 mLastTouch.pointers[badPointerReplacementIndex].x,
3079 mLastTouch.pointers[badPointerReplacementIndex].y);
3080#endif
3081
3082 mCurrentTouch.pointers[badPointerIndex].x =
3083 mLastTouch.pointers[badPointerReplacementIndex].x;
3084 mCurrentTouch.pointers[badPointerIndex].y =
3085 mLastTouch.pointers[badPointerReplacementIndex].y;
3086 mJumpyTouchFilter.jumpyPointsDropped += 1;
3087 return true;
3088 }
3089 }
3090
3091 mJumpyTouchFilter.jumpyPointsDropped = 0;
3092 return false;
3093}
3094
3095/* Special hack for devices that have bad screen data: aggregate and
3096 * compute averages of the coordinate data, to reduce the amount of
3097 * jitter seen by applications. */
3098void TouchInputMapper::applyAveragingTouchFilter() {
3099 for (uint32_t currentIndex = 0; currentIndex < mCurrentTouch.pointerCount; currentIndex++) {
3100 uint32_t id = mCurrentTouch.pointers[currentIndex].id;
3101 int32_t x = mCurrentTouch.pointers[currentIndex].x;
3102 int32_t y = mCurrentTouch.pointers[currentIndex].y;
Jeff Brown8d608662010-08-30 03:02:23 -07003103 int32_t pressure;
3104 switch (mCalibration.pressureSource) {
3105 case Calibration::PRESSURE_SOURCE_PRESSURE:
3106 pressure = mCurrentTouch.pointers[currentIndex].pressure;
3107 break;
3108 case Calibration::PRESSURE_SOURCE_TOUCH:
3109 pressure = mCurrentTouch.pointers[currentIndex].touchMajor;
3110 break;
3111 default:
3112 pressure = 1;
3113 break;
3114 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003115
3116 if (mLastTouch.idBits.hasBit(id)) {
3117 // Pointer was down before and is still down now.
3118 // Compute average over history trace.
3119 uint32_t start = mAveragingTouchFilter.historyStart[id];
3120 uint32_t end = mAveragingTouchFilter.historyEnd[id];
3121
3122 int64_t deltaX = x - mAveragingTouchFilter.historyData[end].pointers[id].x;
3123 int64_t deltaY = y - mAveragingTouchFilter.historyData[end].pointers[id].y;
3124 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3125
3126#if DEBUG_HACKS
3127 LOGD("AveragingTouchFilter: Pointer id %d - Distance from last sample: %lld",
3128 id, distance);
3129#endif
3130
3131 if (distance < AVERAGING_DISTANCE_LIMIT) {
3132 // Increment end index in preparation for recording new historical data.
3133 end += 1;
3134 if (end > AVERAGING_HISTORY_SIZE) {
3135 end = 0;
3136 }
3137
3138 // If the end index has looped back to the start index then we have filled
3139 // the historical trace up to the desired size so we drop the historical
3140 // data at the start of the trace.
3141 if (end == start) {
3142 start += 1;
3143 if (start > AVERAGING_HISTORY_SIZE) {
3144 start = 0;
3145 }
3146 }
3147
3148 // Add the raw data to the historical trace.
3149 mAveragingTouchFilter.historyStart[id] = start;
3150 mAveragingTouchFilter.historyEnd[id] = end;
3151 mAveragingTouchFilter.historyData[end].pointers[id].x = x;
3152 mAveragingTouchFilter.historyData[end].pointers[id].y = y;
3153 mAveragingTouchFilter.historyData[end].pointers[id].pressure = pressure;
3154
3155 // Average over all historical positions in the trace by total pressure.
3156 int32_t averagedX = 0;
3157 int32_t averagedY = 0;
3158 int32_t totalPressure = 0;
3159 for (;;) {
3160 int32_t historicalX = mAveragingTouchFilter.historyData[start].pointers[id].x;
3161 int32_t historicalY = mAveragingTouchFilter.historyData[start].pointers[id].y;
3162 int32_t historicalPressure = mAveragingTouchFilter.historyData[start]
3163 .pointers[id].pressure;
3164
3165 averagedX += historicalX * historicalPressure;
3166 averagedY += historicalY * historicalPressure;
3167 totalPressure += historicalPressure;
3168
3169 if (start == end) {
3170 break;
3171 }
3172
3173 start += 1;
3174 if (start > AVERAGING_HISTORY_SIZE) {
3175 start = 0;
3176 }
3177 }
3178
Jeff Brown8d608662010-08-30 03:02:23 -07003179 if (totalPressure != 0) {
3180 averagedX /= totalPressure;
3181 averagedY /= totalPressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003182
3183#if DEBUG_HACKS
Jeff Brown8d608662010-08-30 03:02:23 -07003184 LOGD("AveragingTouchFilter: Pointer id %d - "
3185 "totalPressure=%d, averagedX=%d, averagedY=%d", id, totalPressure,
3186 averagedX, averagedY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003187#endif
3188
Jeff Brown8d608662010-08-30 03:02:23 -07003189 mCurrentTouch.pointers[currentIndex].x = averagedX;
3190 mCurrentTouch.pointers[currentIndex].y = averagedY;
3191 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003192 } else {
3193#if DEBUG_HACKS
3194 LOGD("AveragingTouchFilter: Pointer id %d - Exceeded max distance", id);
3195#endif
3196 }
3197 } else {
3198#if DEBUG_HACKS
3199 LOGD("AveragingTouchFilter: Pointer id %d - Pointer went up", id);
3200#endif
3201 }
3202
3203 // Reset pointer history.
3204 mAveragingTouchFilter.historyStart[id] = 0;
3205 mAveragingTouchFilter.historyEnd[id] = 0;
3206 mAveragingTouchFilter.historyData[0].pointers[id].x = x;
3207 mAveragingTouchFilter.historyData[0].pointers[id].y = y;
3208 mAveragingTouchFilter.historyData[0].pointers[id].pressure = pressure;
3209 }
3210}
3211
3212int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003213 { // acquire lock
3214 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003215
Jeff Brown6328cdc2010-07-29 18:18:33 -07003216 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003217 return AKEY_STATE_VIRTUAL;
3218 }
3219
Jeff Brown6328cdc2010-07-29 18:18:33 -07003220 size_t numVirtualKeys = mLocked.virtualKeys.size();
3221 for (size_t i = 0; i < numVirtualKeys; i++) {
3222 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07003223 if (virtualKey.keyCode == keyCode) {
3224 return AKEY_STATE_UP;
3225 }
3226 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003227 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07003228
3229 return AKEY_STATE_UNKNOWN;
3230}
3231
3232int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003233 { // acquire lock
3234 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003235
Jeff Brown6328cdc2010-07-29 18:18:33 -07003236 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003237 return AKEY_STATE_VIRTUAL;
3238 }
3239
Jeff Brown6328cdc2010-07-29 18:18:33 -07003240 size_t numVirtualKeys = mLocked.virtualKeys.size();
3241 for (size_t i = 0; i < numVirtualKeys; i++) {
3242 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07003243 if (virtualKey.scanCode == scanCode) {
3244 return AKEY_STATE_UP;
3245 }
3246 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003247 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07003248
3249 return AKEY_STATE_UNKNOWN;
3250}
3251
3252bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
3253 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003254 { // acquire lock
3255 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003256
Jeff Brown6328cdc2010-07-29 18:18:33 -07003257 size_t numVirtualKeys = mLocked.virtualKeys.size();
3258 for (size_t i = 0; i < numVirtualKeys; i++) {
3259 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07003260
3261 for (size_t i = 0; i < numCodes; i++) {
3262 if (virtualKey.keyCode == keyCodes[i]) {
3263 outFlags[i] = 1;
3264 }
3265 }
3266 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003267 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07003268
3269 return true;
3270}
3271
3272
3273// --- SingleTouchInputMapper ---
3274
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003275SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
3276 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003277 initialize();
3278}
3279
3280SingleTouchInputMapper::~SingleTouchInputMapper() {
3281}
3282
3283void SingleTouchInputMapper::initialize() {
3284 mAccumulator.clear();
3285
3286 mDown = false;
3287 mX = 0;
3288 mY = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003289 mPressure = 0; // default to 0 for devices that don't report pressure
3290 mToolWidth = 0; // default to 0 for devices that don't report tool width
Jeff Brown6d0fec22010-07-23 21:28:06 -07003291}
3292
3293void SingleTouchInputMapper::reset() {
3294 TouchInputMapper::reset();
3295
Jeff Brown6d0fec22010-07-23 21:28:06 -07003296 initialize();
3297 }
3298
3299void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
3300 switch (rawEvent->type) {
3301 case EV_KEY:
3302 switch (rawEvent->scanCode) {
3303 case BTN_TOUCH:
3304 mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH;
3305 mAccumulator.btnTouch = rawEvent->value != 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003306 // Don't sync immediately. Wait until the next SYN_REPORT since we might
3307 // not have received valid position information yet. This logic assumes that
3308 // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003309 break;
3310 }
3311 break;
3312
3313 case EV_ABS:
3314 switch (rawEvent->scanCode) {
3315 case ABS_X:
3316 mAccumulator.fields |= Accumulator::FIELD_ABS_X;
3317 mAccumulator.absX = rawEvent->value;
3318 break;
3319 case ABS_Y:
3320 mAccumulator.fields |= Accumulator::FIELD_ABS_Y;
3321 mAccumulator.absY = rawEvent->value;
3322 break;
3323 case ABS_PRESSURE:
3324 mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE;
3325 mAccumulator.absPressure = rawEvent->value;
3326 break;
3327 case ABS_TOOL_WIDTH:
3328 mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH;
3329 mAccumulator.absToolWidth = rawEvent->value;
3330 break;
3331 }
3332 break;
3333
3334 case EV_SYN:
3335 switch (rawEvent->scanCode) {
3336 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003337 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003338 break;
3339 }
3340 break;
3341 }
3342}
3343
3344void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003345 uint32_t fields = mAccumulator.fields;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003346 if (fields == 0) {
3347 return; // no new state changes, so nothing to do
3348 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003349
3350 if (fields & Accumulator::FIELD_BTN_TOUCH) {
3351 mDown = mAccumulator.btnTouch;
3352 }
3353
3354 if (fields & Accumulator::FIELD_ABS_X) {
3355 mX = mAccumulator.absX;
3356 }
3357
3358 if (fields & Accumulator::FIELD_ABS_Y) {
3359 mY = mAccumulator.absY;
3360 }
3361
3362 if (fields & Accumulator::FIELD_ABS_PRESSURE) {
3363 mPressure = mAccumulator.absPressure;
3364 }
3365
3366 if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) {
Jeff Brown8d608662010-08-30 03:02:23 -07003367 mToolWidth = mAccumulator.absToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003368 }
3369
3370 mCurrentTouch.clear();
3371
3372 if (mDown) {
3373 mCurrentTouch.pointerCount = 1;
3374 mCurrentTouch.pointers[0].id = 0;
3375 mCurrentTouch.pointers[0].x = mX;
3376 mCurrentTouch.pointers[0].y = mY;
3377 mCurrentTouch.pointers[0].pressure = mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -07003378 mCurrentTouch.pointers[0].touchMajor = 0;
3379 mCurrentTouch.pointers[0].touchMinor = 0;
3380 mCurrentTouch.pointers[0].toolMajor = mToolWidth;
3381 mCurrentTouch.pointers[0].toolMinor = mToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003382 mCurrentTouch.pointers[0].orientation = 0;
3383 mCurrentTouch.idToIndex[0] = 0;
3384 mCurrentTouch.idBits.markBit(0);
3385 }
3386
3387 syncTouch(when, true);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003388
3389 mAccumulator.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003390}
3391
Jeff Brown8d608662010-08-30 03:02:23 -07003392void SingleTouchInputMapper::configureRawAxes() {
3393 TouchInputMapper::configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003394
Jeff Brown8d608662010-08-30 03:02:23 -07003395 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x);
3396 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y);
3397 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure);
3398 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003399}
3400
3401
3402// --- MultiTouchInputMapper ---
3403
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003404MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
3405 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003406 initialize();
3407}
3408
3409MultiTouchInputMapper::~MultiTouchInputMapper() {
3410}
3411
3412void MultiTouchInputMapper::initialize() {
3413 mAccumulator.clear();
3414}
3415
3416void MultiTouchInputMapper::reset() {
3417 TouchInputMapper::reset();
3418
Jeff Brown6d0fec22010-07-23 21:28:06 -07003419 initialize();
3420}
3421
3422void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
3423 switch (rawEvent->type) {
3424 case EV_ABS: {
3425 uint32_t pointerIndex = mAccumulator.pointerCount;
3426 Accumulator::Pointer* pointer = & mAccumulator.pointers[pointerIndex];
3427
3428 switch (rawEvent->scanCode) {
3429 case ABS_MT_POSITION_X:
3430 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_X;
3431 pointer->absMTPositionX = rawEvent->value;
3432 break;
3433 case ABS_MT_POSITION_Y:
3434 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y;
3435 pointer->absMTPositionY = rawEvent->value;
3436 break;
3437 case ABS_MT_TOUCH_MAJOR:
3438 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR;
3439 pointer->absMTTouchMajor = rawEvent->value;
3440 break;
3441 case ABS_MT_TOUCH_MINOR:
3442 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR;
3443 pointer->absMTTouchMinor = rawEvent->value;
3444 break;
3445 case ABS_MT_WIDTH_MAJOR:
3446 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR;
3447 pointer->absMTWidthMajor = rawEvent->value;
3448 break;
3449 case ABS_MT_WIDTH_MINOR:
3450 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR;
3451 pointer->absMTWidthMinor = rawEvent->value;
3452 break;
3453 case ABS_MT_ORIENTATION:
3454 pointer->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION;
3455 pointer->absMTOrientation = rawEvent->value;
3456 break;
3457 case ABS_MT_TRACKING_ID:
3458 pointer->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID;
3459 pointer->absMTTrackingId = rawEvent->value;
3460 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003461 case ABS_MT_PRESSURE:
3462 pointer->fields |= Accumulator::FIELD_ABS_MT_PRESSURE;
3463 pointer->absMTPressure = rawEvent->value;
3464 break;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003465 }
3466 break;
3467 }
3468
3469 case EV_SYN:
3470 switch (rawEvent->scanCode) {
3471 case SYN_MT_REPORT: {
3472 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
3473 uint32_t pointerIndex = mAccumulator.pointerCount;
3474
3475 if (mAccumulator.pointers[pointerIndex].fields) {
3476 if (pointerIndex == MAX_POINTERS) {
3477 LOGW("MultiTouch device driver returned more than maximum of %d pointers.",
3478 MAX_POINTERS);
3479 } else {
3480 pointerIndex += 1;
3481 mAccumulator.pointerCount = pointerIndex;
3482 }
3483 }
3484
3485 mAccumulator.pointers[pointerIndex].clear();
3486 break;
3487 }
3488
3489 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003490 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003491 break;
3492 }
3493 break;
3494 }
3495}
3496
3497void MultiTouchInputMapper::sync(nsecs_t when) {
3498 static const uint32_t REQUIRED_FIELDS =
Jeff Brown8d608662010-08-30 03:02:23 -07003499 Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003500
Jeff Brown6d0fec22010-07-23 21:28:06 -07003501 uint32_t inCount = mAccumulator.pointerCount;
3502 uint32_t outCount = 0;
3503 bool havePointerIds = true;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003504
Jeff Brown6d0fec22010-07-23 21:28:06 -07003505 mCurrentTouch.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003506
Jeff Brown6d0fec22010-07-23 21:28:06 -07003507 for (uint32_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003508 const Accumulator::Pointer& inPointer = mAccumulator.pointers[inIndex];
3509 uint32_t fields = inPointer.fields;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003510
Jeff Brown6d0fec22010-07-23 21:28:06 -07003511 if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003512 // Some drivers send empty MT sync packets without X / Y to indicate a pointer up.
3513 // Drop this finger.
Jeff Brown46b9ac02010-04-22 18:58:52 -07003514 continue;
3515 }
3516
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003517 PointerData& outPointer = mCurrentTouch.pointers[outCount];
3518 outPointer.x = inPointer.absMTPositionX;
3519 outPointer.y = inPointer.absMTPositionY;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003520
Jeff Brown8d608662010-08-30 03:02:23 -07003521 if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) {
3522 if (inPointer.absMTPressure <= 0) {
Jeff Brownc3db8582010-10-20 15:33:38 -07003523 // Some devices send sync packets with X / Y but with a 0 pressure to indicate
3524 // a pointer going up. Drop this finger.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003525 continue;
3526 }
Jeff Brown8d608662010-08-30 03:02:23 -07003527 outPointer.pressure = inPointer.absMTPressure;
3528 } else {
3529 // Default pressure to 0 if absent.
3530 outPointer.pressure = 0;
3531 }
3532
3533 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) {
3534 if (inPointer.absMTTouchMajor <= 0) {
3535 // Some devices send sync packets with X / Y but with a 0 touch major to indicate
3536 // a pointer going up. Drop this finger.
3537 continue;
3538 }
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003539 outPointer.touchMajor = inPointer.absMTTouchMajor;
3540 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003541 // Default touch area to 0 if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003542 outPointer.touchMajor = 0;
3543 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003544
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003545 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) {
3546 outPointer.touchMinor = inPointer.absMTTouchMinor;
3547 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003548 // Assume touch area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003549 outPointer.touchMinor = outPointer.touchMajor;
3550 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003551
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003552 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) {
3553 outPointer.toolMajor = inPointer.absMTWidthMajor;
3554 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003555 // Default tool area to 0 if absent.
3556 outPointer.toolMajor = 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003557 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003558
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003559 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) {
3560 outPointer.toolMinor = inPointer.absMTWidthMinor;
3561 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003562 // Assume tool area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003563 outPointer.toolMinor = outPointer.toolMajor;
3564 }
3565
3566 if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) {
3567 outPointer.orientation = inPointer.absMTOrientation;
3568 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003569 // Default orientation to vertical if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003570 outPointer.orientation = 0;
3571 }
3572
Jeff Brown8d608662010-08-30 03:02:23 -07003573 // Assign pointer id using tracking id if available.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003574 if (havePointerIds) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003575 if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) {
3576 uint32_t id = uint32_t(inPointer.absMTTrackingId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003577
Jeff Brown6d0fec22010-07-23 21:28:06 -07003578 if (id > MAX_POINTER_ID) {
3579#if DEBUG_POINTERS
3580 LOGD("Pointers: Ignoring driver provided pointer id %d because "
Jeff Brown01ce2e92010-09-26 22:20:12 -07003581 "it is larger than max supported id %d",
Jeff Brown6d0fec22010-07-23 21:28:06 -07003582 id, MAX_POINTER_ID);
3583#endif
3584 havePointerIds = false;
3585 }
3586 else {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003587 outPointer.id = id;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003588 mCurrentTouch.idToIndex[id] = outCount;
3589 mCurrentTouch.idBits.markBit(id);
3590 }
3591 } else {
3592 havePointerIds = false;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003593 }
3594 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003595
Jeff Brown6d0fec22010-07-23 21:28:06 -07003596 outCount += 1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003597 }
3598
Jeff Brown6d0fec22010-07-23 21:28:06 -07003599 mCurrentTouch.pointerCount = outCount;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003600
Jeff Brown6d0fec22010-07-23 21:28:06 -07003601 syncTouch(when, havePointerIds);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003602
3603 mAccumulator.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003604}
3605
Jeff Brown8d608662010-08-30 03:02:23 -07003606void MultiTouchInputMapper::configureRawAxes() {
3607 TouchInputMapper::configureRawAxes();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003608
Jeff Brown8d608662010-08-30 03:02:23 -07003609 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, & mRawAxes.x);
3610 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, & mRawAxes.y);
3611 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, & mRawAxes.touchMajor);
3612 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, & mRawAxes.touchMinor);
3613 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, & mRawAxes.toolMajor);
3614 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, & mRawAxes.toolMinor);
3615 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, & mRawAxes.orientation);
3616 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, & mRawAxes.pressure);
Jeff Brown9c3cda02010-06-15 01:31:58 -07003617}
3618
Jeff Brown46b9ac02010-04-22 18:58:52 -07003619
3620} // namespace android