blob: b92c3b57d6b23956ae3124f0ab0fee52f20bece0 [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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-04-22 18:58:52 -070041
42#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070043#include <stdlib.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070044#include <unistd.h>
Jeff Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown517bb4c2011-01-14 19:09:23 -080083inline static int32_t signExtendNybble(int32_t value) {
84 return value >= 8 ? value - 16 : value;
85}
86
Jeff Brownef3d7e82010-09-30 14:33:04 -070087static inline const char* toString(bool value) {
88 return value ? "true" : "false";
89}
90
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070091static const int32_t keyCodeRotationMap[][4] = {
92 // key codes enumerated counter-clockwise with the original (unrotated) key first
93 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd0358292010-06-30 16:10:35 -070094 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
95 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
96 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
97 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070098};
99static const int keyCodeRotationMapSize =
100 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
101
102int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800103 if (orientation != DISPLAY_ORIENTATION_0) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700104 for (int i = 0; i < keyCodeRotationMapSize; i++) {
105 if (keyCode == keyCodeRotationMap[i][0]) {
106 return keyCodeRotationMap[i][orientation];
107 }
108 }
109 }
110 return keyCode;
111}
112
Jeff Brown6d0fec22010-07-23 21:28:06 -0700113static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
114 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
115}
116
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700117
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700118// --- InputReader ---
119
120InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700121 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700122 const sp<InputDispatcherInterface>& dispatcher) :
Jeff Brown6d0fec22010-07-23 21:28:06 -0700123 mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher),
Jeff Brownfe508922011-01-18 15:10:10 -0800124 mGlobalMetaState(0), mDisableVirtualKeysTimeout(-1) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700125 configureExcludedDevices();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700126 updateGlobalMetaState();
127 updateInputConfiguration();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700128}
129
130InputReader::~InputReader() {
131 for (size_t i = 0; i < mDevices.size(); i++) {
132 delete mDevices.valueAt(i);
133 }
134}
135
136void InputReader::loopOnce() {
137 RawEvent rawEvent;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700138 mEventHub->getEvent(& rawEvent);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700139
140#if DEBUG_RAW_EVENTS
Jeff Brown90655042010-12-02 13:50:46 -0800141 LOGD("Input event: device=%d type=0x%x scancode=%d keycode=%d value=%d",
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700142 rawEvent.deviceId, rawEvent.type, rawEvent.scanCode, rawEvent.keyCode,
143 rawEvent.value);
144#endif
145
146 process(& rawEvent);
147}
148
149void InputReader::process(const RawEvent* rawEvent) {
150 switch (rawEvent->type) {
151 case EventHubInterface::DEVICE_ADDED:
Jeff Brown7342bb92010-10-01 18:55:43 -0700152 addDevice(rawEvent->deviceId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700153 break;
154
155 case EventHubInterface::DEVICE_REMOVED:
Jeff Brown7342bb92010-10-01 18:55:43 -0700156 removeDevice(rawEvent->deviceId);
157 break;
158
159 case EventHubInterface::FINISHED_DEVICE_SCAN:
Jeff Brownc3db8582010-10-20 15:33:38 -0700160 handleConfigurationChanged(rawEvent->when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700161 break;
162
Jeff Brown6d0fec22010-07-23 21:28:06 -0700163 default:
164 consumeEvent(rawEvent);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700165 break;
166 }
167}
168
Jeff Brown7342bb92010-10-01 18:55:43 -0700169void InputReader::addDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700170 String8 name = mEventHub->getDeviceName(deviceId);
171 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
172
173 InputDevice* device = createDevice(deviceId, name, classes);
174 device->configure();
175
Jeff Brown8d608662010-08-30 03:02:23 -0700176 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800177 LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700178 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800179 LOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700180 device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700181 }
182
Jeff Brown6d0fec22010-07-23 21:28:06 -0700183 bool added = false;
184 { // acquire device registry writer lock
185 RWLock::AutoWLock _wl(mDeviceRegistryLock);
186
187 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
188 if (deviceIndex < 0) {
189 mDevices.add(deviceId, device);
190 added = true;
191 }
192 } // release device registry writer lock
193
194 if (! added) {
195 LOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
196 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700197 return;
198 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700199}
200
Jeff Brown7342bb92010-10-01 18:55:43 -0700201void InputReader::removeDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700202 bool removed = false;
203 InputDevice* device = NULL;
204 { // acquire device registry writer lock
205 RWLock::AutoWLock _wl(mDeviceRegistryLock);
206
207 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
208 if (deviceIndex >= 0) {
209 device = mDevices.valueAt(deviceIndex);
210 mDevices.removeItemsAt(deviceIndex, 1);
211 removed = true;
212 }
213 } // release device registry writer lock
214
215 if (! removed) {
216 LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700217 return;
218 }
219
Jeff Brown6d0fec22010-07-23 21:28:06 -0700220 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800221 LOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700222 device->getId(), device->getName().string());
223 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800224 LOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700225 device->getId(), device->getName().string(), device->getSources());
226 }
227
Jeff Brown8d608662010-08-30 03:02:23 -0700228 device->reset();
229
Jeff Brown6d0fec22010-07-23 21:28:06 -0700230 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700231}
232
Jeff Brown6d0fec22010-07-23 21:28:06 -0700233InputDevice* InputReader::createDevice(int32_t deviceId, const String8& name, uint32_t classes) {
234 InputDevice* device = new InputDevice(this, deviceId, name);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700235
Jeff Brown56194eb2011-03-02 19:23:13 -0800236 // External devices.
237 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
238 device->setExternal(true);
239 }
240
Jeff Brown6d0fec22010-07-23 21:28:06 -0700241 // Switch-like devices.
242 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
243 device->addMapper(new SwitchInputMapper(device));
244 }
245
246 // Keyboard-like devices.
247 uint32_t keyboardSources = 0;
248 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
249 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
250 keyboardSources |= AINPUT_SOURCE_KEYBOARD;
251 }
252 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
253 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
254 }
255 if (classes & INPUT_DEVICE_CLASS_DPAD) {
256 keyboardSources |= AINPUT_SOURCE_DPAD;
257 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800258 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
259 keyboardSources |= AINPUT_SOURCE_GAMEPAD;
260 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700261
262 if (keyboardSources != 0) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800263 device->addMapper(new KeyboardInputMapper(device, keyboardSources, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700264 }
265
Jeff Brown83c09682010-12-23 17:50:18 -0800266 // Cursor-like devices.
267 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
268 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700269 }
270
Jeff Brown58a2da82011-01-25 16:02:22 -0800271 // Touchscreens and touchpad devices.
272 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800273 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800274 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800275 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700276 }
277
Jeff Browncb1404e2011-01-15 18:14:15 -0800278 // Joystick-like devices.
279 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
280 device->addMapper(new JoystickInputMapper(device));
281 }
282
Jeff Brown6d0fec22010-07-23 21:28:06 -0700283 return device;
284}
285
286void InputReader::consumeEvent(const RawEvent* rawEvent) {
287 int32_t deviceId = rawEvent->deviceId;
288
289 { // acquire device registry reader lock
290 RWLock::AutoRLock _rl(mDeviceRegistryLock);
291
292 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
293 if (deviceIndex < 0) {
294 LOGW("Discarding event for unknown deviceId %d.", deviceId);
295 return;
296 }
297
298 InputDevice* device = mDevices.valueAt(deviceIndex);
299 if (device->isIgnored()) {
300 //LOGD("Discarding event for ignored deviceId %d.", deviceId);
301 return;
302 }
303
304 device->process(rawEvent);
305 } // release device registry reader lock
306}
307
Jeff Brownc3db8582010-10-20 15:33:38 -0700308void InputReader::handleConfigurationChanged(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700309 // Reset global meta state because it depends on the list of all configured devices.
310 updateGlobalMetaState();
311
312 // Update input configuration.
313 updateInputConfiguration();
314
315 // Enqueue configuration changed.
316 mDispatcher->notifyConfigurationChanged(when);
317}
318
319void InputReader::configureExcludedDevices() {
320 Vector<String8> excludedDeviceNames;
321 mPolicy->getExcludedDeviceNames(excludedDeviceNames);
322
323 for (size_t i = 0; i < excludedDeviceNames.size(); i++) {
324 mEventHub->addExcludedDevice(excludedDeviceNames[i]);
325 }
326}
327
328void InputReader::updateGlobalMetaState() {
329 { // acquire state lock
330 AutoMutex _l(mStateLock);
331
332 mGlobalMetaState = 0;
333
334 { // acquire device registry reader lock
335 RWLock::AutoRLock _rl(mDeviceRegistryLock);
336
337 for (size_t i = 0; i < mDevices.size(); i++) {
338 InputDevice* device = mDevices.valueAt(i);
339 mGlobalMetaState |= device->getMetaState();
340 }
341 } // release device registry reader lock
342 } // release state lock
343}
344
345int32_t InputReader::getGlobalMetaState() {
346 { // acquire state lock
347 AutoMutex _l(mStateLock);
348
349 return mGlobalMetaState;
350 } // release state lock
351}
352
353void InputReader::updateInputConfiguration() {
354 { // acquire state lock
355 AutoMutex _l(mStateLock);
356
357 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
358 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
359 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
360 { // acquire device registry reader lock
361 RWLock::AutoRLock _rl(mDeviceRegistryLock);
362
363 InputDeviceInfo deviceInfo;
364 for (size_t i = 0; i < mDevices.size(); i++) {
365 InputDevice* device = mDevices.valueAt(i);
366 device->getDeviceInfo(& deviceInfo);
367 uint32_t sources = deviceInfo.getSources();
368
369 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
370 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
371 }
372 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
373 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
374 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
375 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
376 }
377 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
378 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700379 }
380 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700381 } // release device registry reader lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700382
Jeff Brown6d0fec22010-07-23 21:28:06 -0700383 mInputConfiguration.touchScreen = touchScreenConfig;
384 mInputConfiguration.keyboard = keyboardConfig;
385 mInputConfiguration.navigation = navigationConfig;
386 } // release state lock
387}
388
Jeff Brownfe508922011-01-18 15:10:10 -0800389void InputReader::disableVirtualKeysUntil(nsecs_t time) {
390 mDisableVirtualKeysTimeout = time;
391}
392
393bool InputReader::shouldDropVirtualKey(nsecs_t now,
394 InputDevice* device, int32_t keyCode, int32_t scanCode) {
395 if (now < mDisableVirtualKeysTimeout) {
396 LOGI("Dropping virtual key from device %s because virtual keys are "
397 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
398 device->getName().string(),
399 (mDisableVirtualKeysTimeout - now) * 0.000001,
400 keyCode, scanCode);
401 return true;
402 } else {
403 return false;
404 }
405}
406
Jeff Brown05dc66a2011-03-02 14:41:58 -0800407void InputReader::fadePointer() {
408 { // acquire device registry reader lock
409 RWLock::AutoRLock _rl(mDeviceRegistryLock);
410
411 for (size_t i = 0; i < mDevices.size(); i++) {
412 InputDevice* device = mDevices.valueAt(i);
413 device->fadePointer();
414 }
415 } // release device registry reader lock
416}
417
Jeff Brown6d0fec22010-07-23 21:28:06 -0700418void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
419 { // acquire state lock
420 AutoMutex _l(mStateLock);
421
422 *outConfiguration = mInputConfiguration;
423 } // release state lock
424}
425
426status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
427 { // acquire device registry reader lock
428 RWLock::AutoRLock _rl(mDeviceRegistryLock);
429
430 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
431 if (deviceIndex < 0) {
432 return NAME_NOT_FOUND;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700433 }
434
Jeff Brown6d0fec22010-07-23 21:28:06 -0700435 InputDevice* device = mDevices.valueAt(deviceIndex);
436 if (device->isIgnored()) {
437 return NAME_NOT_FOUND;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700438 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700439
440 device->getDeviceInfo(outDeviceInfo);
441 return OK;
442 } // release device registy reader lock
443}
444
445void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
446 outDeviceIds.clear();
447
448 { // acquire device registry reader lock
449 RWLock::AutoRLock _rl(mDeviceRegistryLock);
450
451 size_t numDevices = mDevices.size();
452 for (size_t i = 0; i < numDevices; i++) {
453 InputDevice* device = mDevices.valueAt(i);
454 if (! device->isIgnored()) {
455 outDeviceIds.add(device->getId());
456 }
457 }
458 } // release device registy reader lock
459}
460
461int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
462 int32_t keyCode) {
463 return getState(deviceId, sourceMask, keyCode, & InputDevice::getKeyCodeState);
464}
465
466int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
467 int32_t scanCode) {
468 return getState(deviceId, sourceMask, scanCode, & InputDevice::getScanCodeState);
469}
470
471int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
472 return getState(deviceId, sourceMask, switchCode, & InputDevice::getSwitchState);
473}
474
475int32_t InputReader::getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
476 GetStateFunc getStateFunc) {
477 { // acquire device registry reader lock
478 RWLock::AutoRLock _rl(mDeviceRegistryLock);
479
480 int32_t result = AKEY_STATE_UNKNOWN;
481 if (deviceId >= 0) {
482 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
483 if (deviceIndex >= 0) {
484 InputDevice* device = mDevices.valueAt(deviceIndex);
485 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
486 result = (device->*getStateFunc)(sourceMask, code);
487 }
488 }
489 } else {
490 size_t numDevices = mDevices.size();
491 for (size_t i = 0; i < numDevices; i++) {
492 InputDevice* device = mDevices.valueAt(i);
493 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
494 result = (device->*getStateFunc)(sourceMask, code);
495 if (result >= AKEY_STATE_DOWN) {
496 return result;
497 }
498 }
499 }
500 }
501 return result;
502 } // release device registy reader lock
503}
504
505bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
506 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
507 memset(outFlags, 0, numCodes);
508 return markSupportedKeyCodes(deviceId, sourceMask, numCodes, keyCodes, outFlags);
509}
510
511bool InputReader::markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
512 const int32_t* keyCodes, uint8_t* outFlags) {
513 { // acquire device registry reader lock
514 RWLock::AutoRLock _rl(mDeviceRegistryLock);
515 bool result = false;
516 if (deviceId >= 0) {
517 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
518 if (deviceIndex >= 0) {
519 InputDevice* device = mDevices.valueAt(deviceIndex);
520 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
521 result = device->markSupportedKeyCodes(sourceMask,
522 numCodes, keyCodes, outFlags);
523 }
524 }
525 } else {
526 size_t numDevices = mDevices.size();
527 for (size_t i = 0; i < numDevices; i++) {
528 InputDevice* device = mDevices.valueAt(i);
529 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
530 result |= device->markSupportedKeyCodes(sourceMask,
531 numCodes, keyCodes, outFlags);
532 }
533 }
534 }
535 return result;
536 } // release device registy reader lock
537}
538
Jeff Brownb88102f2010-09-08 11:49:43 -0700539void InputReader::dump(String8& dump) {
Jeff Brownf2f487182010-10-01 17:46:21 -0700540 mEventHub->dump(dump);
541 dump.append("\n");
542
543 dump.append("Input Reader State:\n");
544
Jeff Brownef3d7e82010-09-30 14:33:04 -0700545 { // acquire device registry reader lock
546 RWLock::AutoRLock _rl(mDeviceRegistryLock);
Jeff Brownb88102f2010-09-08 11:49:43 -0700547
Jeff Brownef3d7e82010-09-30 14:33:04 -0700548 for (size_t i = 0; i < mDevices.size(); i++) {
549 mDevices.valueAt(i)->dump(dump);
Jeff Brownb88102f2010-09-08 11:49:43 -0700550 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700551 } // release device registy reader lock
Jeff Brownb88102f2010-09-08 11:49:43 -0700552}
553
Jeff Brown6d0fec22010-07-23 21:28:06 -0700554
555// --- InputReaderThread ---
556
557InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
558 Thread(/*canCallJava*/ true), mReader(reader) {
559}
560
561InputReaderThread::~InputReaderThread() {
562}
563
564bool InputReaderThread::threadLoop() {
565 mReader->loopOnce();
566 return true;
567}
568
569
570// --- InputDevice ---
571
572InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) :
Jeff Brown56194eb2011-03-02 19:23:13 -0800573 mContext(context), mId(id), mName(name), mSources(0), mIsExternal(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700574}
575
576InputDevice::~InputDevice() {
577 size_t numMappers = mMappers.size();
578 for (size_t i = 0; i < numMappers; i++) {
579 delete mMappers[i];
580 }
581 mMappers.clear();
582}
583
Jeff Brownef3d7e82010-09-30 14:33:04 -0700584void InputDevice::dump(String8& dump) {
585 InputDeviceInfo deviceInfo;
586 getDeviceInfo(& deviceInfo);
587
Jeff Brown90655042010-12-02 13:50:46 -0800588 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700589 deviceInfo.getName().string());
Jeff Brown56194eb2011-03-02 19:23:13 -0800590 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700591 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
592 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800593
594 const KeyedVector<int32_t, InputDeviceInfo::MotionRange> ranges = deviceInfo.getMotionRanges();
595 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700596 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800597 for (size_t i = 0; i < ranges.size(); i++) {
598 int32_t axis = ranges.keyAt(i);
599 const char* label = getAxisLabel(axis);
600 char name[32];
601 if (label) {
602 strncpy(name, label, sizeof(name));
603 name[sizeof(name) - 1] = '\0';
604 } else {
605 snprintf(name, sizeof(name), "%d", axis);
606 }
607 const InputDeviceInfo::MotionRange& range = ranges.valueAt(i);
608 dump.appendFormat(INDENT3 "%s: min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
609 name, range.min, range.max, range.flat, range.fuzz);
610 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700611 }
612
613 size_t numMappers = mMappers.size();
614 for (size_t i = 0; i < numMappers; i++) {
615 InputMapper* mapper = mMappers[i];
616 mapper->dump(dump);
617 }
618}
619
Jeff Brown6d0fec22010-07-23 21:28:06 -0700620void InputDevice::addMapper(InputMapper* mapper) {
621 mMappers.add(mapper);
622}
623
624void InputDevice::configure() {
Jeff Brown8d608662010-08-30 03:02:23 -0700625 if (! isIgnored()) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800626 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
Jeff Brown8d608662010-08-30 03:02:23 -0700627 }
628
Jeff Brown6d0fec22010-07-23 21:28:06 -0700629 mSources = 0;
630
631 size_t numMappers = mMappers.size();
632 for (size_t i = 0; i < numMappers; i++) {
633 InputMapper* mapper = mMappers[i];
634 mapper->configure();
635 mSources |= mapper->getSources();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700636 }
637}
638
Jeff Brown6d0fec22010-07-23 21:28:06 -0700639void InputDevice::reset() {
640 size_t numMappers = mMappers.size();
641 for (size_t i = 0; i < numMappers; i++) {
642 InputMapper* mapper = mMappers[i];
643 mapper->reset();
644 }
645}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700646
Jeff Brown6d0fec22010-07-23 21:28:06 -0700647void InputDevice::process(const RawEvent* rawEvent) {
648 size_t numMappers = mMappers.size();
649 for (size_t i = 0; i < numMappers; i++) {
650 InputMapper* mapper = mMappers[i];
651 mapper->process(rawEvent);
652 }
653}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700654
Jeff Brown6d0fec22010-07-23 21:28:06 -0700655void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
656 outDeviceInfo->initialize(mId, mName);
657
658 size_t numMappers = mMappers.size();
659 for (size_t i = 0; i < numMappers; i++) {
660 InputMapper* mapper = mMappers[i];
661 mapper->populateDeviceInfo(outDeviceInfo);
662 }
663}
664
665int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
666 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
667}
668
669int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
670 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
671}
672
673int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
674 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
675}
676
677int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
678 int32_t result = AKEY_STATE_UNKNOWN;
679 size_t numMappers = mMappers.size();
680 for (size_t i = 0; i < numMappers; i++) {
681 InputMapper* mapper = mMappers[i];
682 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
683 result = (mapper->*getStateFunc)(sourceMask, code);
684 if (result >= AKEY_STATE_DOWN) {
685 return result;
686 }
687 }
688 }
689 return result;
690}
691
692bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
693 const int32_t* keyCodes, uint8_t* outFlags) {
694 bool result = false;
695 size_t numMappers = mMappers.size();
696 for (size_t i = 0; i < numMappers; i++) {
697 InputMapper* mapper = mMappers[i];
698 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
699 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
700 }
701 }
702 return result;
703}
704
705int32_t InputDevice::getMetaState() {
706 int32_t result = 0;
707 size_t numMappers = mMappers.size();
708 for (size_t i = 0; i < numMappers; i++) {
709 InputMapper* mapper = mMappers[i];
710 result |= mapper->getMetaState();
711 }
712 return result;
713}
714
Jeff Brown05dc66a2011-03-02 14:41:58 -0800715void InputDevice::fadePointer() {
716 size_t numMappers = mMappers.size();
717 for (size_t i = 0; i < numMappers; i++) {
718 InputMapper* mapper = mMappers[i];
719 mapper->fadePointer();
720 }
721}
722
Jeff Brown6d0fec22010-07-23 21:28:06 -0700723
724// --- InputMapper ---
725
726InputMapper::InputMapper(InputDevice* device) :
727 mDevice(device), mContext(device->getContext()) {
728}
729
730InputMapper::~InputMapper() {
731}
732
733void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
734 info->addSource(getSources());
735}
736
Jeff Brownef3d7e82010-09-30 14:33:04 -0700737void InputMapper::dump(String8& dump) {
738}
739
Jeff Brown6d0fec22010-07-23 21:28:06 -0700740void InputMapper::configure() {
741}
742
743void InputMapper::reset() {
744}
745
746int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
747 return AKEY_STATE_UNKNOWN;
748}
749
750int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
751 return AKEY_STATE_UNKNOWN;
752}
753
754int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
755 return AKEY_STATE_UNKNOWN;
756}
757
758bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
759 const int32_t* keyCodes, uint8_t* outFlags) {
760 return false;
761}
762
763int32_t InputMapper::getMetaState() {
764 return 0;
765}
766
Jeff Brown05dc66a2011-03-02 14:41:58 -0800767void InputMapper::fadePointer() {
768}
769
Jeff Browncb1404e2011-01-15 18:14:15 -0800770void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
771 const RawAbsoluteAxisInfo& axis, const char* name) {
772 if (axis.valid) {
773 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d\n",
774 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz);
775 } else {
776 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
777 }
778}
779
Jeff Brown6d0fec22010-07-23 21:28:06 -0700780
781// --- SwitchInputMapper ---
782
783SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
784 InputMapper(device) {
785}
786
787SwitchInputMapper::~SwitchInputMapper() {
788}
789
790uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -0800791 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700792}
793
794void SwitchInputMapper::process(const RawEvent* rawEvent) {
795 switch (rawEvent->type) {
796 case EV_SW:
797 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
798 break;
799 }
800}
801
802void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownb6997262010-10-08 22:31:17 -0700803 getDispatcher()->notifySwitch(when, switchCode, switchValue, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700804}
805
806int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
807 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
808}
809
810
811// --- KeyboardInputMapper ---
812
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800813KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700814 uint32_t sources, int32_t keyboardType) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800815 InputMapper(device), mSources(sources),
Jeff Brown6d0fec22010-07-23 21:28:06 -0700816 mKeyboardType(keyboardType) {
Jeff Brown6328cdc2010-07-29 18:18:33 -0700817 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700818}
819
820KeyboardInputMapper::~KeyboardInputMapper() {
821}
822
Jeff Brown6328cdc2010-07-29 18:18:33 -0700823void KeyboardInputMapper::initializeLocked() {
824 mLocked.metaState = AMETA_NONE;
825 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700826}
827
828uint32_t KeyboardInputMapper::getSources() {
829 return mSources;
830}
831
832void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
833 InputMapper::populateDeviceInfo(info);
834
835 info->setKeyboardType(mKeyboardType);
836}
837
Jeff Brownef3d7e82010-09-30 14:33:04 -0700838void KeyboardInputMapper::dump(String8& dump) {
839 { // acquire lock
840 AutoMutex _l(mLock);
841 dump.append(INDENT2 "Keyboard Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800842 dumpParameters(dump);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700843 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
844 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mLocked.keyDowns.size());
845 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mLocked.metaState);
846 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
847 } // release lock
848}
849
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800850
851void KeyboardInputMapper::configure() {
852 InputMapper::configure();
853
854 // Configure basic parameters.
855 configureParameters();
Jeff Brown49ed71d2010-12-06 17:13:33 -0800856
857 // Reset LEDs.
858 {
859 AutoMutex _l(mLock);
860 resetLedStateLocked();
861 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800862}
863
864void KeyboardInputMapper::configureParameters() {
865 mParameters.orientationAware = false;
866 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
867 mParameters.orientationAware);
868
869 mParameters.associatedDisplayId = mParameters.orientationAware ? 0 : -1;
870}
871
872void KeyboardInputMapper::dumpParameters(String8& dump) {
873 dump.append(INDENT3 "Parameters:\n");
874 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
875 mParameters.associatedDisplayId);
876 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
877 toString(mParameters.orientationAware));
878}
879
Jeff Brown6d0fec22010-07-23 21:28:06 -0700880void KeyboardInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -0700881 for (;;) {
882 int32_t keyCode, scanCode;
883 { // acquire lock
884 AutoMutex _l(mLock);
885
886 // Synthesize key up event on reset if keys are currently down.
887 if (mLocked.keyDowns.isEmpty()) {
888 initializeLocked();
Jeff Brown49ed71d2010-12-06 17:13:33 -0800889 resetLedStateLocked();
Jeff Brown6328cdc2010-07-29 18:18:33 -0700890 break; // done
891 }
892
893 const KeyDown& keyDown = mLocked.keyDowns.top();
894 keyCode = keyDown.keyCode;
895 scanCode = keyDown.scanCode;
896 } // release lock
897
Jeff Brown6d0fec22010-07-23 21:28:06 -0700898 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700899 processKey(when, false, keyCode, scanCode, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700900 }
901
902 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700903 getContext()->updateGlobalMetaState();
904}
905
906void KeyboardInputMapper::process(const RawEvent* rawEvent) {
907 switch (rawEvent->type) {
908 case EV_KEY: {
909 int32_t scanCode = rawEvent->scanCode;
910 if (isKeyboardOrGamepadKey(scanCode)) {
911 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
912 rawEvent->flags);
913 }
914 break;
915 }
916 }
917}
918
919bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
920 return scanCode < BTN_MOUSE
921 || scanCode >= KEY_OK
Jeff Browncb1404e2011-01-15 18:14:15 -0800922 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700923}
924
Jeff Brown6328cdc2010-07-29 18:18:33 -0700925void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
926 int32_t scanCode, uint32_t policyFlags) {
927 int32_t newMetaState;
928 nsecs_t downTime;
929 bool metaStateChanged = false;
930
931 { // acquire lock
932 AutoMutex _l(mLock);
933
934 if (down) {
935 // Rotate key codes according to orientation if needed.
936 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800937 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -0700938 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800939 if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
940 NULL, NULL, & orientation)) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800941 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700942 }
943
944 keyCode = rotateKeyCode(keyCode, orientation);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700945 }
946
Jeff Brown6328cdc2010-07-29 18:18:33 -0700947 // Add key down.
948 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
949 if (keyDownIndex >= 0) {
950 // key repeat, be sure to use same keycode as before in case of rotation
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800951 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700952 } else {
953 // key down
Jeff Brownfe508922011-01-18 15:10:10 -0800954 if ((policyFlags & POLICY_FLAG_VIRTUAL)
955 && mContext->shouldDropVirtualKey(when,
956 getDevice(), keyCode, scanCode)) {
957 return;
958 }
959
Jeff Brown6328cdc2010-07-29 18:18:33 -0700960 mLocked.keyDowns.push();
961 KeyDown& keyDown = mLocked.keyDowns.editTop();
962 keyDown.keyCode = keyCode;
963 keyDown.scanCode = scanCode;
964 }
965
966 mLocked.downTime = when;
967 } else {
968 // Remove key down.
969 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
970 if (keyDownIndex >= 0) {
971 // key up, be sure to use same keycode as before in case of rotation
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800972 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700973 mLocked.keyDowns.removeAt(size_t(keyDownIndex));
974 } else {
975 // key was not actually down
976 LOGI("Dropping key up from device %s because the key was not down. "
977 "keyCode=%d, scanCode=%d",
978 getDeviceName().string(), keyCode, scanCode);
979 return;
980 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700981 }
982
Jeff Brown6328cdc2010-07-29 18:18:33 -0700983 int32_t oldMetaState = mLocked.metaState;
984 newMetaState = updateMetaState(keyCode, down, oldMetaState);
985 if (oldMetaState != newMetaState) {
986 mLocked.metaState = newMetaState;
987 metaStateChanged = true;
Jeff Brown497a92c2010-09-12 17:55:08 -0700988 updateLedStateLocked(false);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700989 }
Jeff Brownfd0358292010-06-30 16:10:35 -0700990
Jeff Brown6328cdc2010-07-29 18:18:33 -0700991 downTime = mLocked.downTime;
992 } // release lock
993
Jeff Brown56194eb2011-03-02 19:23:13 -0800994 // Key down on external an keyboard should wake the device.
995 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
996 // For internal keyboards, the key layout file should specify the policy flags for
997 // each wake key individually.
998 // TODO: Use the input device configuration to control this behavior more finely.
999 if (down && getDevice()->isExternal()
1000 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1001 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1002 }
1003
Jeff Brown6328cdc2010-07-29 18:18:33 -07001004 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001005 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001006 }
1007
Jeff Brown05dc66a2011-03-02 14:41:58 -08001008 if (down && !isMetaKey(keyCode)) {
1009 getContext()->fadePointer();
1010 }
1011
Jeff Brown497a92c2010-09-12 17:55:08 -07001012 if (policyFlags & POLICY_FLAG_FUNCTION) {
1013 newMetaState |= AMETA_FUNCTION_ON;
1014 }
Jeff Brown83c09682010-12-23 17:50:18 -08001015 getDispatcher()->notifyKey(when, getDeviceId(), mSources, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001016 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1017 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001018}
1019
Jeff Brown6328cdc2010-07-29 18:18:33 -07001020ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) {
1021 size_t n = mLocked.keyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001022 for (size_t i = 0; i < n; i++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001023 if (mLocked.keyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001024 return i;
1025 }
1026 }
1027 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001028}
1029
Jeff Brown6d0fec22010-07-23 21:28:06 -07001030int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1031 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1032}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001033
Jeff Brown6d0fec22010-07-23 21:28:06 -07001034int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1035 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1036}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001037
Jeff Brown6d0fec22010-07-23 21:28:06 -07001038bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1039 const int32_t* keyCodes, uint8_t* outFlags) {
1040 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1041}
1042
1043int32_t KeyboardInputMapper::getMetaState() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001044 { // acquire lock
1045 AutoMutex _l(mLock);
1046 return mLocked.metaState;
1047 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001048}
1049
Jeff Brown49ed71d2010-12-06 17:13:33 -08001050void KeyboardInputMapper::resetLedStateLocked() {
1051 initializeLedStateLocked(mLocked.capsLockLedState, LED_CAPSL);
1052 initializeLedStateLocked(mLocked.numLockLedState, LED_NUML);
1053 initializeLedStateLocked(mLocked.scrollLockLedState, LED_SCROLLL);
1054
1055 updateLedStateLocked(true);
1056}
1057
1058void KeyboardInputMapper::initializeLedStateLocked(LockedState::LedState& ledState, int32_t led) {
1059 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
1060 ledState.on = false;
1061}
1062
Jeff Brown497a92c2010-09-12 17:55:08 -07001063void KeyboardInputMapper::updateLedStateLocked(bool reset) {
1064 updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001065 AMETA_CAPS_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001066 updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001067 AMETA_NUM_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001068 updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001069 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001070}
1071
1072void KeyboardInputMapper::updateLedStateForModifierLocked(LockedState::LedState& ledState,
1073 int32_t led, int32_t modifier, bool reset) {
1074 if (ledState.avail) {
1075 bool desiredState = (mLocked.metaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001076 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001077 getEventHub()->setLedState(getDeviceId(), led, desiredState);
1078 ledState.on = desiredState;
1079 }
1080 }
1081}
1082
Jeff Brown6d0fec22010-07-23 21:28:06 -07001083
Jeff Brown83c09682010-12-23 17:50:18 -08001084// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07001085
Jeff Brown83c09682010-12-23 17:50:18 -08001086CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001087 InputMapper(device) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001088 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001089}
1090
Jeff Brown83c09682010-12-23 17:50:18 -08001091CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001092}
1093
Jeff Brown83c09682010-12-23 17:50:18 -08001094uint32_t CursorInputMapper::getSources() {
1095 return mSources;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001096}
1097
Jeff Brown83c09682010-12-23 17:50:18 -08001098void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001099 InputMapper::populateDeviceInfo(info);
1100
Jeff Brown83c09682010-12-23 17:50:18 -08001101 if (mParameters.mode == Parameters::MODE_POINTER) {
1102 float minX, minY, maxX, maxY;
1103 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001104 info->addMotionRange(AMOTION_EVENT_AXIS_X, minX, maxX, 0.0f, 0.0f);
1105 info->addMotionRange(AMOTION_EVENT_AXIS_Y, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08001106 }
1107 } else {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001108 info->addMotionRange(AMOTION_EVENT_AXIS_X, -1.0f, 1.0f, 0.0f, mXScale);
1109 info->addMotionRange(AMOTION_EVENT_AXIS_Y, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08001110 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001111 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, 0.0f, 1.0f, 0.0f, 0.0f);
1112
1113 if (mHaveVWheel) {
1114 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, -1.0f, 1.0f, 0.0f, 0.0f);
1115 }
1116 if (mHaveHWheel) {
1117 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, -1.0f, 1.0f, 0.0f, 0.0f);
1118 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001119}
1120
Jeff Brown83c09682010-12-23 17:50:18 -08001121void CursorInputMapper::dump(String8& dump) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07001122 { // acquire lock
1123 AutoMutex _l(mLock);
Jeff Brown83c09682010-12-23 17:50:18 -08001124 dump.append(INDENT2 "Cursor Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001125 dumpParameters(dump);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001126 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
1127 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001128 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1129 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001130 dump.appendFormat(INDENT3 "HaveVWheel: %s\n", toString(mHaveVWheel));
1131 dump.appendFormat(INDENT3 "HaveHWheel: %s\n", toString(mHaveHWheel));
1132 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
1133 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001134 dump.appendFormat(INDENT3 "Down: %s\n", toString(mLocked.down));
1135 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1136 } // release lock
1137}
1138
Jeff Brown83c09682010-12-23 17:50:18 -08001139void CursorInputMapper::configure() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001140 InputMapper::configure();
1141
1142 // Configure basic parameters.
1143 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08001144
1145 // Configure device mode.
1146 switch (mParameters.mode) {
1147 case Parameters::MODE_POINTER:
1148 mSources = AINPUT_SOURCE_MOUSE;
1149 mXPrecision = 1.0f;
1150 mYPrecision = 1.0f;
1151 mXScale = 1.0f;
1152 mYScale = 1.0f;
1153 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
1154 break;
1155 case Parameters::MODE_NAVIGATION:
1156 mSources = AINPUT_SOURCE_TRACKBALL;
1157 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1158 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1159 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1160 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1161 break;
1162 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001163
1164 mVWheelScale = 1.0f;
1165 mHWheelScale = 1.0f;
Jeff Browncc0c1592011-02-19 05:07:28 -08001166
1167 mHaveVWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_WHEEL);
1168 mHaveHWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_HWHEEL);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001169}
1170
Jeff Brown83c09682010-12-23 17:50:18 -08001171void CursorInputMapper::configureParameters() {
1172 mParameters.mode = Parameters::MODE_POINTER;
1173 String8 cursorModeString;
1174 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
1175 if (cursorModeString == "navigation") {
1176 mParameters.mode = Parameters::MODE_NAVIGATION;
1177 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
1178 LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
1179 }
1180 }
1181
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001182 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08001183 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001184 mParameters.orientationAware);
1185
Jeff Brown83c09682010-12-23 17:50:18 -08001186 mParameters.associatedDisplayId = mParameters.mode == Parameters::MODE_POINTER
1187 || mParameters.orientationAware ? 0 : -1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001188}
1189
Jeff Brown83c09682010-12-23 17:50:18 -08001190void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001191 dump.append(INDENT3 "Parameters:\n");
1192 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1193 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08001194
1195 switch (mParameters.mode) {
1196 case Parameters::MODE_POINTER:
1197 dump.append(INDENT4 "Mode: pointer\n");
1198 break;
1199 case Parameters::MODE_NAVIGATION:
1200 dump.append(INDENT4 "Mode: navigation\n");
1201 break;
1202 default:
1203 assert(false);
1204 }
1205
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001206 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1207 toString(mParameters.orientationAware));
1208}
1209
Jeff Brown83c09682010-12-23 17:50:18 -08001210void CursorInputMapper::initializeLocked() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001211 mAccumulator.clear();
1212
Jeff Brown6328cdc2010-07-29 18:18:33 -07001213 mLocked.down = false;
1214 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001215}
1216
Jeff Brown83c09682010-12-23 17:50:18 -08001217void CursorInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001218 for (;;) {
1219 { // acquire lock
1220 AutoMutex _l(mLock);
1221
1222 if (! mLocked.down) {
1223 initializeLocked();
1224 break; // done
1225 }
1226 } // release lock
1227
Jeff Brown83c09682010-12-23 17:50:18 -08001228 // Synthesize button up event on reset.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001229 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001230 mAccumulator.fields = Accumulator::FIELD_BTN_MOUSE;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001231 mAccumulator.btnMouse = false;
1232 sync(when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001233 }
1234
Jeff Brown6d0fec22010-07-23 21:28:06 -07001235 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001236}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001237
Jeff Brown83c09682010-12-23 17:50:18 -08001238void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001239 switch (rawEvent->type) {
1240 case EV_KEY:
1241 switch (rawEvent->scanCode) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08001242 case BTN_LEFT:
1243 case BTN_RIGHT:
1244 case BTN_MIDDLE:
1245 case BTN_SIDE:
1246 case BTN_EXTRA:
1247 case BTN_FORWARD:
1248 case BTN_BACK:
1249 case BTN_TASK:
Jeff Brown6d0fec22010-07-23 21:28:06 -07001250 mAccumulator.fields |= Accumulator::FIELD_BTN_MOUSE;
1251 mAccumulator.btnMouse = rawEvent->value != 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001252 // Sync now since BTN_MOUSE is not necessarily followed by SYN_REPORT and
1253 // we need to ensure that we report the up/down promptly.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001254 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001255 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001256 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001257 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001258
Jeff Brown6d0fec22010-07-23 21:28:06 -07001259 case EV_REL:
1260 switch (rawEvent->scanCode) {
1261 case REL_X:
1262 mAccumulator.fields |= Accumulator::FIELD_REL_X;
1263 mAccumulator.relX = rawEvent->value;
1264 break;
1265 case REL_Y:
1266 mAccumulator.fields |= Accumulator::FIELD_REL_Y;
1267 mAccumulator.relY = rawEvent->value;
1268 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001269 case REL_WHEEL:
1270 mAccumulator.fields |= Accumulator::FIELD_REL_WHEEL;
1271 mAccumulator.relWheel = rawEvent->value;
1272 break;
1273 case REL_HWHEEL:
1274 mAccumulator.fields |= Accumulator::FIELD_REL_HWHEEL;
1275 mAccumulator.relHWheel = rawEvent->value;
1276 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001277 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001278 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001279
Jeff Brown6d0fec22010-07-23 21:28:06 -07001280 case EV_SYN:
1281 switch (rawEvent->scanCode) {
1282 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001283 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001284 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001285 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001286 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001287 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001288}
1289
Jeff Brown83c09682010-12-23 17:50:18 -08001290void CursorInputMapper::sync(nsecs_t when) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001291 uint32_t fields = mAccumulator.fields;
1292 if (fields == 0) {
1293 return; // no new state changes, so nothing to do
1294 }
1295
Jeff Brown6328cdc2010-07-29 18:18:33 -07001296 int motionEventAction;
1297 PointerCoords pointerCoords;
1298 nsecs_t downTime;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001299 float vscroll, hscroll;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001300 { // acquire lock
1301 AutoMutex _l(mLock);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001302
Jeff Brown6328cdc2010-07-29 18:18:33 -07001303 bool downChanged = fields & Accumulator::FIELD_BTN_MOUSE;
1304
1305 if (downChanged) {
1306 if (mAccumulator.btnMouse) {
Jeff Brown1c9d06e2011-01-14 17:24:16 -08001307 if (!mLocked.down) {
1308 mLocked.down = true;
1309 mLocked.downTime = when;
1310 } else {
1311 downChanged = false;
1312 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001313 } else {
Jeff Brown1c9d06e2011-01-14 17:24:16 -08001314 if (mLocked.down) {
1315 mLocked.down = false;
1316 } else {
1317 downChanged = false;
1318 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001319 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001320 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001321
Jeff Brown6328cdc2010-07-29 18:18:33 -07001322 downTime = mLocked.downTime;
Jeff Brown83c09682010-12-23 17:50:18 -08001323 float deltaX = fields & Accumulator::FIELD_REL_X ? mAccumulator.relX * mXScale : 0.0f;
1324 float deltaY = fields & Accumulator::FIELD_REL_Y ? mAccumulator.relY * mYScale : 0.0f;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001325
Jeff Brown6328cdc2010-07-29 18:18:33 -07001326 if (downChanged) {
1327 motionEventAction = mLocked.down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
Jeff Browncc0c1592011-02-19 05:07:28 -08001328 } else if (mLocked.down || mPointerController == NULL) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001329 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browncc0c1592011-02-19 05:07:28 -08001330 } else {
1331 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001332 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001333
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001334 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
Jeff Brown83c09682010-12-23 17:50:18 -08001335 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001336 // Rotate motion based on display orientation if needed.
1337 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1338 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001339 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1340 NULL, NULL, & orientation)) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001341 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001342 }
1343
1344 float temp;
1345 switch (orientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001346 case DISPLAY_ORIENTATION_90:
Jeff Brown83c09682010-12-23 17:50:18 -08001347 temp = deltaX;
1348 deltaX = deltaY;
1349 deltaY = -temp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001350 break;
1351
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001352 case DISPLAY_ORIENTATION_180:
Jeff Brown83c09682010-12-23 17:50:18 -08001353 deltaX = -deltaX;
1354 deltaY = -deltaY;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001355 break;
1356
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001357 case DISPLAY_ORIENTATION_270:
Jeff Brown83c09682010-12-23 17:50:18 -08001358 temp = deltaX;
1359 deltaX = -deltaY;
1360 deltaY = temp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001361 break;
1362 }
1363 }
Jeff Brown83c09682010-12-23 17:50:18 -08001364
Jeff Brown91c69ab2011-02-14 17:03:18 -08001365 pointerCoords.clear();
1366
Jeff Brown83c09682010-12-23 17:50:18 -08001367 if (mPointerController != NULL) {
1368 mPointerController->move(deltaX, deltaY);
1369 if (downChanged) {
1370 mPointerController->setButtonState(mLocked.down ? POINTER_BUTTON_1 : 0);
1371 }
Jeff Brown91c69ab2011-02-14 17:03:18 -08001372 float x, y;
1373 mPointerController->getPosition(&x, &y);
Jeff Brownebbd5d12011-02-17 13:01:34 -08001374 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
1375 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown83c09682010-12-23 17:50:18 -08001376 } else {
Jeff Brownebbd5d12011-02-17 13:01:34 -08001377 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
1378 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
Jeff Brown83c09682010-12-23 17:50:18 -08001379 }
1380
Jeff Brownebbd5d12011-02-17 13:01:34 -08001381 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, mLocked.down ? 1.0f : 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001382
1383 if (mHaveVWheel && (fields & Accumulator::FIELD_REL_WHEEL)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08001384 vscroll = mAccumulator.relWheel;
1385 } else {
1386 vscroll = 0;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001387 }
1388 if (mHaveHWheel && (fields & Accumulator::FIELD_REL_HWHEEL)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08001389 hscroll = mAccumulator.relHWheel;
1390 } else {
1391 hscroll = 0;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001392 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08001393 if (hscroll != 0 || vscroll != 0) {
1394 mPointerController->unfade();
1395 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001396 } // release lock
1397
Jeff Brown56194eb2011-03-02 19:23:13 -08001398 // Moving an external trackball or mouse should wake the device.
1399 // We don't do this for internal cursor devices to prevent them from waking up
1400 // the device in your pocket.
1401 // TODO: Use the input device configuration to control this behavior more finely.
1402 uint32_t policyFlags = 0;
1403 if (getDevice()->isExternal()) {
1404 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1405 }
1406
Jeff Brown6d0fec22010-07-23 21:28:06 -07001407 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001408 int32_t pointerId = 0;
Jeff Brown56194eb2011-03-02 19:23:13 -08001409 getDispatcher()->notifyMotion(when, getDeviceId(), mSources, policyFlags,
Jeff Brown85a31762010-09-01 17:01:00 -07001410 motionEventAction, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE,
Jeff Brownb6997262010-10-08 22:31:17 -07001411 1, &pointerId, &pointerCoords, mXPrecision, mYPrecision, downTime);
1412
1413 mAccumulator.clear();
Jeff Brown33bbfd22011-02-24 20:55:35 -08001414
1415 if (vscroll != 0 || hscroll != 0) {
1416 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
1417 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
1418
Jeff Brown56194eb2011-03-02 19:23:13 -08001419 getDispatcher()->notifyMotion(when, getDeviceId(), mSources, policyFlags,
Jeff Brown33bbfd22011-02-24 20:55:35 -08001420 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE,
1421 1, &pointerId, &pointerCoords, mXPrecision, mYPrecision, downTime);
1422 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001423}
1424
Jeff Brown83c09682010-12-23 17:50:18 -08001425int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07001426 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
1427 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1428 } else {
1429 return AKEY_STATE_UNKNOWN;
1430 }
1431}
1432
Jeff Brown05dc66a2011-03-02 14:41:58 -08001433void CursorInputMapper::fadePointer() {
1434 { // acquire lock
1435 AutoMutex _l(mLock);
1436 mPointerController->fade();
1437 } // release lock
1438}
1439
Jeff Brown6d0fec22010-07-23 21:28:06 -07001440
1441// --- TouchInputMapper ---
1442
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001443TouchInputMapper::TouchInputMapper(InputDevice* device) :
1444 InputMapper(device) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001445 mLocked.surfaceOrientation = -1;
1446 mLocked.surfaceWidth = -1;
1447 mLocked.surfaceHeight = -1;
1448
1449 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001450}
1451
1452TouchInputMapper::~TouchInputMapper() {
1453}
1454
1455uint32_t TouchInputMapper::getSources() {
Jeff Brown83c09682010-12-23 17:50:18 -08001456 return mSources;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001457}
1458
1459void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1460 InputMapper::populateDeviceInfo(info);
1461
Jeff Brown6328cdc2010-07-29 18:18:33 -07001462 { // acquire lock
1463 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001464
Jeff Brown6328cdc2010-07-29 18:18:33 -07001465 // Ensure surface information is up to date so that orientation changes are
1466 // noticed immediately.
1467 configureSurfaceLocked();
1468
Jeff Brown6f2fba42011-02-19 01:08:02 -08001469 info->addMotionRange(AMOTION_EVENT_AXIS_X, mLocked.orientedRanges.x);
1470 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mLocked.orientedRanges.y);
Jeff Brown8d608662010-08-30 03:02:23 -07001471
1472 if (mLocked.orientedRanges.havePressure) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001473 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE,
Jeff Brown8d608662010-08-30 03:02:23 -07001474 mLocked.orientedRanges.pressure);
1475 }
1476
1477 if (mLocked.orientedRanges.haveSize) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001478 info->addMotionRange(AMOTION_EVENT_AXIS_SIZE,
Jeff Brown8d608662010-08-30 03:02:23 -07001479 mLocked.orientedRanges.size);
1480 }
1481
Jeff Brownc6d282b2010-10-14 21:42:15 -07001482 if (mLocked.orientedRanges.haveTouchSize) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001483 info->addMotionRange(AMOTION_EVENT_AXIS_TOUCH_MAJOR,
Jeff Brown8d608662010-08-30 03:02:23 -07001484 mLocked.orientedRanges.touchMajor);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001485 info->addMotionRange(AMOTION_EVENT_AXIS_TOUCH_MINOR,
Jeff Brown8d608662010-08-30 03:02:23 -07001486 mLocked.orientedRanges.touchMinor);
1487 }
1488
Jeff Brownc6d282b2010-10-14 21:42:15 -07001489 if (mLocked.orientedRanges.haveToolSize) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001490 info->addMotionRange(AMOTION_EVENT_AXIS_TOOL_MAJOR,
Jeff Brown8d608662010-08-30 03:02:23 -07001491 mLocked.orientedRanges.toolMajor);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001492 info->addMotionRange(AMOTION_EVENT_AXIS_TOOL_MINOR,
Jeff Brown8d608662010-08-30 03:02:23 -07001493 mLocked.orientedRanges.toolMinor);
1494 }
1495
1496 if (mLocked.orientedRanges.haveOrientation) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001497 info->addMotionRange(AMOTION_EVENT_AXIS_ORIENTATION,
Jeff Brown8d608662010-08-30 03:02:23 -07001498 mLocked.orientedRanges.orientation);
1499 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001500 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001501}
1502
Jeff Brownef3d7e82010-09-30 14:33:04 -07001503void TouchInputMapper::dump(String8& dump) {
1504 { // acquire lock
1505 AutoMutex _l(mLock);
1506 dump.append(INDENT2 "Touch Input Mapper:\n");
Jeff Brownef3d7e82010-09-30 14:33:04 -07001507 dumpParameters(dump);
1508 dumpVirtualKeysLocked(dump);
1509 dumpRawAxes(dump);
1510 dumpCalibration(dump);
1511 dumpSurfaceLocked(dump);
Jeff Brown511ee5f2010-10-18 13:32:20 -07001512 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
Jeff Brownc6d282b2010-10-14 21:42:15 -07001513 dump.appendFormat(INDENT4 "XOrigin: %d\n", mLocked.xOrigin);
1514 dump.appendFormat(INDENT4 "YOrigin: %d\n", mLocked.yOrigin);
1515 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mLocked.xScale);
1516 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mLocked.yScale);
1517 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mLocked.xPrecision);
1518 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mLocked.yPrecision);
1519 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mLocked.geometricScale);
1520 dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mLocked.toolSizeLinearScale);
1521 dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mLocked.toolSizeLinearBias);
1522 dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mLocked.toolSizeAreaScale);
1523 dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mLocked.toolSizeAreaBias);
1524 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mLocked.pressureScale);
1525 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mLocked.sizeScale);
1526 dump.appendFormat(INDENT4 "OrientationSCale: %0.3f\n", mLocked.orientationScale);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001527 } // release lock
1528}
1529
Jeff Brown6328cdc2010-07-29 18:18:33 -07001530void TouchInputMapper::initializeLocked() {
1531 mCurrentTouch.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001532 mLastTouch.clear();
1533 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001534
1535 for (uint32_t i = 0; i < MAX_POINTERS; i++) {
1536 mAveragingTouchFilter.historyStart[i] = 0;
1537 mAveragingTouchFilter.historyEnd[i] = 0;
1538 }
1539
1540 mJumpyTouchFilter.jumpyPointsDropped = 0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001541
1542 mLocked.currentVirtualKey.down = false;
Jeff Brown8d608662010-08-30 03:02:23 -07001543
1544 mLocked.orientedRanges.havePressure = false;
1545 mLocked.orientedRanges.haveSize = false;
Jeff Brownc6d282b2010-10-14 21:42:15 -07001546 mLocked.orientedRanges.haveTouchSize = false;
1547 mLocked.orientedRanges.haveToolSize = false;
Jeff Brown8d608662010-08-30 03:02:23 -07001548 mLocked.orientedRanges.haveOrientation = false;
1549}
1550
Jeff Brown6d0fec22010-07-23 21:28:06 -07001551void TouchInputMapper::configure() {
1552 InputMapper::configure();
1553
1554 // Configure basic parameters.
Jeff Brown8d608662010-08-30 03:02:23 -07001555 configureParameters();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001556
Jeff Brown83c09682010-12-23 17:50:18 -08001557 // Configure sources.
1558 switch (mParameters.deviceType) {
1559 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
1560 mSources = AINPUT_SOURCE_TOUCHSCREEN;
1561 break;
1562 case Parameters::DEVICE_TYPE_TOUCH_PAD:
1563 mSources = AINPUT_SOURCE_TOUCHPAD;
1564 break;
1565 default:
1566 assert(false);
1567 }
1568
Jeff Brown6d0fec22010-07-23 21:28:06 -07001569 // Configure absolute axis information.
Jeff Brown8d608662010-08-30 03:02:23 -07001570 configureRawAxes();
Jeff Brown8d608662010-08-30 03:02:23 -07001571
1572 // Prepare input device calibration.
1573 parseCalibration();
1574 resolveCalibration();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001575
Jeff Brown6328cdc2010-07-29 18:18:33 -07001576 { // acquire lock
1577 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001578
Jeff Brown8d608662010-08-30 03:02:23 -07001579 // Configure surface dimensions and orientation.
Jeff Brown6328cdc2010-07-29 18:18:33 -07001580 configureSurfaceLocked();
1581 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001582}
1583
Jeff Brown8d608662010-08-30 03:02:23 -07001584void TouchInputMapper::configureParameters() {
1585 mParameters.useBadTouchFilter = getPolicy()->filterTouchEvents();
1586 mParameters.useAveragingTouchFilter = getPolicy()->filterTouchEvents();
1587 mParameters.useJumpyTouchFilter = getPolicy()->filterJumpyTouchEvents();
Jeff Brownfe508922011-01-18 15:10:10 -08001588 mParameters.virtualKeyQuietTime = getPolicy()->getVirtualKeyQuietTime();
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001589
1590 String8 deviceTypeString;
Jeff Brown58a2da82011-01-25 16:02:22 -08001591 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001592 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
1593 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08001594 if (deviceTypeString == "touchScreen") {
1595 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
1596 } else if (deviceTypeString != "touchPad") {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001597 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
1598 }
1599 }
1600 bool isTouchScreen = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
1601
1602 mParameters.orientationAware = isTouchScreen;
1603 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
1604 mParameters.orientationAware);
1605
1606 mParameters.associatedDisplayId = mParameters.orientationAware || isTouchScreen ? 0 : -1;
Jeff Brown8d608662010-08-30 03:02:23 -07001607}
1608
Jeff Brownef3d7e82010-09-30 14:33:04 -07001609void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001610 dump.append(INDENT3 "Parameters:\n");
1611
1612 switch (mParameters.deviceType) {
1613 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
1614 dump.append(INDENT4 "DeviceType: touchScreen\n");
1615 break;
1616 case Parameters::DEVICE_TYPE_TOUCH_PAD:
1617 dump.append(INDENT4 "DeviceType: touchPad\n");
1618 break;
1619 default:
1620 assert(false);
1621 }
1622
1623 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1624 mParameters.associatedDisplayId);
1625 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1626 toString(mParameters.orientationAware));
1627
1628 dump.appendFormat(INDENT4 "UseBadTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07001629 toString(mParameters.useBadTouchFilter));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001630 dump.appendFormat(INDENT4 "UseAveragingTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07001631 toString(mParameters.useAveragingTouchFilter));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001632 dump.appendFormat(INDENT4 "UseJumpyTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07001633 toString(mParameters.useJumpyTouchFilter));
Jeff Brownb88102f2010-09-08 11:49:43 -07001634}
1635
Jeff Brown8d608662010-08-30 03:02:23 -07001636void TouchInputMapper::configureRawAxes() {
1637 mRawAxes.x.clear();
1638 mRawAxes.y.clear();
1639 mRawAxes.pressure.clear();
1640 mRawAxes.touchMajor.clear();
1641 mRawAxes.touchMinor.clear();
1642 mRawAxes.toolMajor.clear();
1643 mRawAxes.toolMinor.clear();
1644 mRawAxes.orientation.clear();
1645}
1646
Jeff Brownef3d7e82010-09-30 14:33:04 -07001647void TouchInputMapper::dumpRawAxes(String8& dump) {
1648 dump.append(INDENT3 "Raw Axes:\n");
Jeff Browncb1404e2011-01-15 18:14:15 -08001649 dumpRawAbsoluteAxisInfo(dump, mRawAxes.x, "X");
1650 dumpRawAbsoluteAxisInfo(dump, mRawAxes.y, "Y");
1651 dumpRawAbsoluteAxisInfo(dump, mRawAxes.pressure, "Pressure");
1652 dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor");
1653 dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor");
1654 dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor");
1655 dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor");
1656 dumpRawAbsoluteAxisInfo(dump, mRawAxes.orientation, "Orientation");
Jeff Brown6d0fec22010-07-23 21:28:06 -07001657}
1658
Jeff Brown6328cdc2010-07-29 18:18:33 -07001659bool TouchInputMapper::configureSurfaceLocked() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001660 // Update orientation and dimensions if needed.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001661 int32_t orientation = DISPLAY_ORIENTATION_0;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001662 int32_t width = mRawAxes.x.getRange();
1663 int32_t height = mRawAxes.y.getRange();
1664
1665 if (mParameters.associatedDisplayId >= 0) {
1666 bool wantSize = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
1667 bool wantOrientation = mParameters.orientationAware;
1668
Jeff Brown6328cdc2010-07-29 18:18:33 -07001669 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001670 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1671 wantSize ? &width : NULL, wantSize ? &height : NULL,
1672 wantOrientation ? &orientation : NULL)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001673 return false;
1674 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001675 }
1676
Jeff Brown6328cdc2010-07-29 18:18:33 -07001677 bool orientationChanged = mLocked.surfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001678 if (orientationChanged) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001679 mLocked.surfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001680 }
1681
Jeff Brown6328cdc2010-07-29 18:18:33 -07001682 bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001683 if (sizeChanged) {
Jeff Brown90655042010-12-02 13:50:46 -08001684 LOGI("Device reconfigured: id=%d, name='%s', display size is now %dx%d",
Jeff Brownef3d7e82010-09-30 14:33:04 -07001685 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown8d608662010-08-30 03:02:23 -07001686
Jeff Brown6328cdc2010-07-29 18:18:33 -07001687 mLocked.surfaceWidth = width;
1688 mLocked.surfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001689
Jeff Brown8d608662010-08-30 03:02:23 -07001690 // Configure X and Y factors.
1691 if (mRawAxes.x.valid && mRawAxes.y.valid) {
Jeff Brown511ee5f2010-10-18 13:32:20 -07001692 mLocked.xOrigin = mCalibration.haveXOrigin
1693 ? mCalibration.xOrigin
1694 : mRawAxes.x.minValue;
1695 mLocked.yOrigin = mCalibration.haveYOrigin
1696 ? mCalibration.yOrigin
1697 : mRawAxes.y.minValue;
1698 mLocked.xScale = mCalibration.haveXScale
1699 ? mCalibration.xScale
1700 : float(width) / mRawAxes.x.getRange();
1701 mLocked.yScale = mCalibration.haveYScale
1702 ? mCalibration.yScale
1703 : float(height) / mRawAxes.y.getRange();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001704 mLocked.xPrecision = 1.0f / mLocked.xScale;
1705 mLocked.yPrecision = 1.0f / mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001706
Jeff Brown6328cdc2010-07-29 18:18:33 -07001707 configureVirtualKeysLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001708 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07001709 LOGW(INDENT "Touch device did not report support for X or Y axis!");
Jeff Brown6328cdc2010-07-29 18:18:33 -07001710 mLocked.xOrigin = 0;
1711 mLocked.yOrigin = 0;
1712 mLocked.xScale = 1.0f;
1713 mLocked.yScale = 1.0f;
1714 mLocked.xPrecision = 1.0f;
1715 mLocked.yPrecision = 1.0f;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001716 }
1717
Jeff Brown8d608662010-08-30 03:02:23 -07001718 // Scale factor for terms that are not oriented in a particular axis.
1719 // If the pixels are square then xScale == yScale otherwise we fake it
1720 // by choosing an average.
1721 mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001722
Jeff Brown8d608662010-08-30 03:02:23 -07001723 // Size of diagonal axis.
1724 float diagonalSize = pythag(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001725
Jeff Brown8d608662010-08-30 03:02:23 -07001726 // TouchMajor and TouchMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07001727 if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) {
1728 mLocked.orientedRanges.haveTouchSize = true;
Jeff Brown8d608662010-08-30 03:02:23 -07001729 mLocked.orientedRanges.touchMajor.min = 0;
1730 mLocked.orientedRanges.touchMajor.max = diagonalSize;
1731 mLocked.orientedRanges.touchMajor.flat = 0;
1732 mLocked.orientedRanges.touchMajor.fuzz = 0;
1733 mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor;
1734 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001735
Jeff Brown8d608662010-08-30 03:02:23 -07001736 // ToolMajor and ToolMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07001737 mLocked.toolSizeLinearScale = 0;
1738 mLocked.toolSizeLinearBias = 0;
1739 mLocked.toolSizeAreaScale = 0;
1740 mLocked.toolSizeAreaBias = 0;
1741 if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
1742 if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) {
1743 if (mCalibration.haveToolSizeLinearScale) {
1744 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
Jeff Brown8d608662010-08-30 03:02:23 -07001745 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07001746 mLocked.toolSizeLinearScale = float(min(width, height))
Jeff Brown8d608662010-08-30 03:02:23 -07001747 / mRawAxes.toolMajor.maxValue;
1748 }
1749
Jeff Brownc6d282b2010-10-14 21:42:15 -07001750 if (mCalibration.haveToolSizeLinearBias) {
1751 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
1752 }
1753 } else if (mCalibration.toolSizeCalibration ==
1754 Calibration::TOOL_SIZE_CALIBRATION_AREA) {
1755 if (mCalibration.haveToolSizeLinearScale) {
1756 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
1757 } else {
1758 mLocked.toolSizeLinearScale = min(width, height);
1759 }
1760
1761 if (mCalibration.haveToolSizeLinearBias) {
1762 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
1763 }
1764
1765 if (mCalibration.haveToolSizeAreaScale) {
1766 mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale;
1767 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1768 mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue;
1769 }
1770
1771 if (mCalibration.haveToolSizeAreaBias) {
1772 mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -07001773 }
1774 }
1775
Jeff Brownc6d282b2010-10-14 21:42:15 -07001776 mLocked.orientedRanges.haveToolSize = true;
Jeff Brown8d608662010-08-30 03:02:23 -07001777 mLocked.orientedRanges.toolMajor.min = 0;
1778 mLocked.orientedRanges.toolMajor.max = diagonalSize;
1779 mLocked.orientedRanges.toolMajor.flat = 0;
1780 mLocked.orientedRanges.toolMajor.fuzz = 0;
1781 mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor;
1782 }
1783
1784 // Pressure factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07001785 mLocked.pressureScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07001786 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
1787 RawAbsoluteAxisInfo rawPressureAxis;
1788 switch (mCalibration.pressureSource) {
1789 case Calibration::PRESSURE_SOURCE_PRESSURE:
1790 rawPressureAxis = mRawAxes.pressure;
1791 break;
1792 case Calibration::PRESSURE_SOURCE_TOUCH:
1793 rawPressureAxis = mRawAxes.touchMajor;
1794 break;
1795 default:
1796 rawPressureAxis.clear();
1797 }
1798
Jeff Brown8d608662010-08-30 03:02:23 -07001799 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
1800 || mCalibration.pressureCalibration
1801 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
1802 if (mCalibration.havePressureScale) {
1803 mLocked.pressureScale = mCalibration.pressureScale;
1804 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
1805 mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue;
1806 }
1807 }
1808
1809 mLocked.orientedRanges.havePressure = true;
1810 mLocked.orientedRanges.pressure.min = 0;
1811 mLocked.orientedRanges.pressure.max = 1.0;
1812 mLocked.orientedRanges.pressure.flat = 0;
1813 mLocked.orientedRanges.pressure.fuzz = 0;
1814 }
1815
1816 // Size factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07001817 mLocked.sizeScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07001818 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07001819 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
1820 if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1821 mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue;
1822 }
1823 }
1824
1825 mLocked.orientedRanges.haveSize = true;
1826 mLocked.orientedRanges.size.min = 0;
1827 mLocked.orientedRanges.size.max = 1.0;
1828 mLocked.orientedRanges.size.flat = 0;
1829 mLocked.orientedRanges.size.fuzz = 0;
1830 }
1831
1832 // Orientation
Jeff Brownc6d282b2010-10-14 21:42:15 -07001833 mLocked.orientationScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07001834 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07001835 if (mCalibration.orientationCalibration
1836 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
1837 if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) {
1838 mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue;
1839 }
1840 }
1841
1842 mLocked.orientedRanges.orientation.min = - M_PI_2;
1843 mLocked.orientedRanges.orientation.max = M_PI_2;
1844 mLocked.orientedRanges.orientation.flat = 0;
1845 mLocked.orientedRanges.orientation.fuzz = 0;
1846 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001847 }
1848
1849 if (orientationChanged || sizeChanged) {
1850 // Compute oriented surface dimensions, precision, and scales.
1851 float orientedXScale, orientedYScale;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001852 switch (mLocked.surfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001853 case DISPLAY_ORIENTATION_90:
1854 case DISPLAY_ORIENTATION_270:
Jeff Brown6328cdc2010-07-29 18:18:33 -07001855 mLocked.orientedSurfaceWidth = mLocked.surfaceHeight;
1856 mLocked.orientedSurfaceHeight = mLocked.surfaceWidth;
1857 mLocked.orientedXPrecision = mLocked.yPrecision;
1858 mLocked.orientedYPrecision = mLocked.xPrecision;
1859 orientedXScale = mLocked.yScale;
1860 orientedYScale = mLocked.xScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001861 break;
1862 default:
Jeff Brown6328cdc2010-07-29 18:18:33 -07001863 mLocked.orientedSurfaceWidth = mLocked.surfaceWidth;
1864 mLocked.orientedSurfaceHeight = mLocked.surfaceHeight;
1865 mLocked.orientedXPrecision = mLocked.xPrecision;
1866 mLocked.orientedYPrecision = mLocked.yPrecision;
1867 orientedXScale = mLocked.xScale;
1868 orientedYScale = mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001869 break;
1870 }
1871
1872 // Configure position ranges.
Jeff Brown6328cdc2010-07-29 18:18:33 -07001873 mLocked.orientedRanges.x.min = 0;
1874 mLocked.orientedRanges.x.max = mLocked.orientedSurfaceWidth;
1875 mLocked.orientedRanges.x.flat = 0;
1876 mLocked.orientedRanges.x.fuzz = orientedXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001877
Jeff Brown6328cdc2010-07-29 18:18:33 -07001878 mLocked.orientedRanges.y.min = 0;
1879 mLocked.orientedRanges.y.max = mLocked.orientedSurfaceHeight;
1880 mLocked.orientedRanges.y.flat = 0;
1881 mLocked.orientedRanges.y.fuzz = orientedYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001882 }
1883
1884 return true;
1885}
1886
Jeff Brownef3d7e82010-09-30 14:33:04 -07001887void TouchInputMapper::dumpSurfaceLocked(String8& dump) {
1888 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth);
1889 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight);
1890 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07001891}
1892
Jeff Brown6328cdc2010-07-29 18:18:33 -07001893void TouchInputMapper::configureVirtualKeysLocked() {
Jeff Brown8d608662010-08-30 03:02:23 -07001894 assert(mRawAxes.x.valid && mRawAxes.y.valid);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001895
Jeff Brown8d608662010-08-30 03:02:23 -07001896 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08001897 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001898
Jeff Brown6328cdc2010-07-29 18:18:33 -07001899 mLocked.virtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001900
Jeff Brown6328cdc2010-07-29 18:18:33 -07001901 if (virtualKeyDefinitions.size() == 0) {
1902 return;
1903 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001904
Jeff Brown6328cdc2010-07-29 18:18:33 -07001905 mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size());
1906
Jeff Brown8d608662010-08-30 03:02:23 -07001907 int32_t touchScreenLeft = mRawAxes.x.minValue;
1908 int32_t touchScreenTop = mRawAxes.y.minValue;
1909 int32_t touchScreenWidth = mRawAxes.x.getRange();
1910 int32_t touchScreenHeight = mRawAxes.y.getRange();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001911
1912 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07001913 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07001914 virtualKeyDefinitions[i];
1915
1916 mLocked.virtualKeys.add();
1917 VirtualKey& virtualKey = mLocked.virtualKeys.editTop();
1918
1919 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1920 int32_t keyCode;
1921 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001922 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07001923 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07001924 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
1925 virtualKey.scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001926 mLocked.virtualKeys.pop(); // drop the key
1927 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001928 }
1929
Jeff Brown6328cdc2010-07-29 18:18:33 -07001930 virtualKey.keyCode = keyCode;
1931 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001932
Jeff Brown6328cdc2010-07-29 18:18:33 -07001933 // convert the key definition's display coordinates into touch coordinates for a hit box
1934 int32_t halfWidth = virtualKeyDefinition.width / 2;
1935 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001936
Jeff Brown6328cdc2010-07-29 18:18:33 -07001937 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
1938 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1939 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
1940 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1941 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
1942 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
1943 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
1944 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001945
Jeff Brownef3d7e82010-09-30 14:33:04 -07001946 }
1947}
1948
1949void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) {
1950 if (!mLocked.virtualKeys.isEmpty()) {
1951 dump.append(INDENT3 "Virtual Keys:\n");
1952
1953 for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) {
1954 const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i);
1955 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
1956 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1957 i, virtualKey.scanCode, virtualKey.keyCode,
1958 virtualKey.hitLeft, virtualKey.hitRight,
1959 virtualKey.hitTop, virtualKey.hitBottom);
1960 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001961 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001962}
1963
Jeff Brown8d608662010-08-30 03:02:23 -07001964void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001965 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07001966 Calibration& out = mCalibration;
1967
Jeff Brown511ee5f2010-10-18 13:32:20 -07001968 // Position
1969 out.haveXOrigin = in.tryGetProperty(String8("touch.position.xOrigin"), out.xOrigin);
1970 out.haveYOrigin = in.tryGetProperty(String8("touch.position.yOrigin"), out.yOrigin);
1971 out.haveXScale = in.tryGetProperty(String8("touch.position.xScale"), out.xScale);
1972 out.haveYScale = in.tryGetProperty(String8("touch.position.yScale"), out.yScale);
1973
Jeff Brownc6d282b2010-10-14 21:42:15 -07001974 // Touch Size
1975 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT;
1976 String8 touchSizeCalibrationString;
1977 if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) {
1978 if (touchSizeCalibrationString == "none") {
1979 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
1980 } else if (touchSizeCalibrationString == "geometric") {
1981 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC;
1982 } else if (touchSizeCalibrationString == "pressure") {
1983 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
1984 } else if (touchSizeCalibrationString != "default") {
1985 LOGW("Invalid value for touch.touchSize.calibration: '%s'",
1986 touchSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07001987 }
1988 }
1989
Jeff Brownc6d282b2010-10-14 21:42:15 -07001990 // Tool Size
1991 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT;
1992 String8 toolSizeCalibrationString;
1993 if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) {
1994 if (toolSizeCalibrationString == "none") {
1995 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
1996 } else if (toolSizeCalibrationString == "geometric") {
1997 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC;
1998 } else if (toolSizeCalibrationString == "linear") {
1999 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
2000 } else if (toolSizeCalibrationString == "area") {
2001 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA;
2002 } else if (toolSizeCalibrationString != "default") {
2003 LOGW("Invalid value for touch.toolSize.calibration: '%s'",
2004 toolSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002005 }
2006 }
2007
Jeff Brownc6d282b2010-10-14 21:42:15 -07002008 out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"),
2009 out.toolSizeLinearScale);
2010 out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"),
2011 out.toolSizeLinearBias);
2012 out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"),
2013 out.toolSizeAreaScale);
2014 out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"),
2015 out.toolSizeAreaBias);
2016 out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"),
2017 out.toolSizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07002018
2019 // Pressure
2020 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
2021 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002022 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002023 if (pressureCalibrationString == "none") {
2024 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2025 } else if (pressureCalibrationString == "physical") {
2026 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
2027 } else if (pressureCalibrationString == "amplitude") {
2028 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2029 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002030 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002031 pressureCalibrationString.string());
2032 }
2033 }
2034
2035 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
2036 String8 pressureSourceString;
2037 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
2038 if (pressureSourceString == "pressure") {
2039 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2040 } else if (pressureSourceString == "touch") {
2041 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2042 } else if (pressureSourceString != "default") {
2043 LOGW("Invalid value for touch.pressure.source: '%s'",
2044 pressureSourceString.string());
2045 }
2046 }
2047
2048 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
2049 out.pressureScale);
2050
2051 // Size
2052 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
2053 String8 sizeCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002054 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002055 if (sizeCalibrationString == "none") {
2056 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2057 } else if (sizeCalibrationString == "normalized") {
2058 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
2059 } else if (sizeCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002060 LOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002061 sizeCalibrationString.string());
2062 }
2063 }
2064
2065 // Orientation
2066 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
2067 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002068 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002069 if (orientationCalibrationString == "none") {
2070 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2071 } else if (orientationCalibrationString == "interpolated") {
2072 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002073 } else if (orientationCalibrationString == "vector") {
2074 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002075 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002076 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002077 orientationCalibrationString.string());
2078 }
2079 }
2080}
2081
2082void TouchInputMapper::resolveCalibration() {
2083 // Pressure
2084 switch (mCalibration.pressureSource) {
2085 case Calibration::PRESSURE_SOURCE_DEFAULT:
2086 if (mRawAxes.pressure.valid) {
2087 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2088 } else if (mRawAxes.touchMajor.valid) {
2089 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2090 }
2091 break;
2092
2093 case Calibration::PRESSURE_SOURCE_PRESSURE:
2094 if (! mRawAxes.pressure.valid) {
2095 LOGW("Calibration property touch.pressure.source is 'pressure' but "
2096 "the pressure axis is not available.");
2097 }
2098 break;
2099
2100 case Calibration::PRESSURE_SOURCE_TOUCH:
2101 if (! mRawAxes.touchMajor.valid) {
2102 LOGW("Calibration property touch.pressure.source is 'touch' but "
2103 "the touchMajor axis is not available.");
2104 }
2105 break;
2106
2107 default:
2108 break;
2109 }
2110
2111 switch (mCalibration.pressureCalibration) {
2112 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
2113 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
2114 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2115 } else {
2116 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2117 }
2118 break;
2119
2120 default:
2121 break;
2122 }
2123
Jeff Brownc6d282b2010-10-14 21:42:15 -07002124 // Tool Size
2125 switch (mCalibration.toolSizeCalibration) {
2126 case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07002127 if (mRawAxes.toolMajor.valid) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002128 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
Jeff Brown8d608662010-08-30 03:02:23 -07002129 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002130 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002131 }
2132 break;
2133
2134 default:
2135 break;
2136 }
2137
Jeff Brownc6d282b2010-10-14 21:42:15 -07002138 // Touch Size
2139 switch (mCalibration.touchSizeCalibration) {
2140 case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07002141 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
Jeff Brownc6d282b2010-10-14 21:42:15 -07002142 && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
2143 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
Jeff Brown8d608662010-08-30 03:02:23 -07002144 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002145 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002146 }
2147 break;
2148
2149 default:
2150 break;
2151 }
2152
2153 // Size
2154 switch (mCalibration.sizeCalibration) {
2155 case Calibration::SIZE_CALIBRATION_DEFAULT:
2156 if (mRawAxes.toolMajor.valid) {
2157 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
2158 } else {
2159 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2160 }
2161 break;
2162
2163 default:
2164 break;
2165 }
2166
2167 // Orientation
2168 switch (mCalibration.orientationCalibration) {
2169 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
2170 if (mRawAxes.orientation.valid) {
2171 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
2172 } else {
2173 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2174 }
2175 break;
2176
2177 default:
2178 break;
2179 }
2180}
2181
Jeff Brownef3d7e82010-09-30 14:33:04 -07002182void TouchInputMapper::dumpCalibration(String8& dump) {
2183 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07002184
Jeff Brown511ee5f2010-10-18 13:32:20 -07002185 // Position
2186 if (mCalibration.haveXOrigin) {
2187 dump.appendFormat(INDENT4 "touch.position.xOrigin: %d\n", mCalibration.xOrigin);
2188 }
2189 if (mCalibration.haveYOrigin) {
2190 dump.appendFormat(INDENT4 "touch.position.yOrigin: %d\n", mCalibration.yOrigin);
2191 }
2192 if (mCalibration.haveXScale) {
2193 dump.appendFormat(INDENT4 "touch.position.xScale: %0.3f\n", mCalibration.xScale);
2194 }
2195 if (mCalibration.haveYScale) {
2196 dump.appendFormat(INDENT4 "touch.position.yScale: %0.3f\n", mCalibration.yScale);
2197 }
2198
Jeff Brownc6d282b2010-10-14 21:42:15 -07002199 // Touch Size
2200 switch (mCalibration.touchSizeCalibration) {
2201 case Calibration::TOUCH_SIZE_CALIBRATION_NONE:
2202 dump.append(INDENT4 "touch.touchSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002203 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002204 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
2205 dump.append(INDENT4 "touch.touchSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002206 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002207 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
2208 dump.append(INDENT4 "touch.touchSize.calibration: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002209 break;
2210 default:
2211 assert(false);
2212 }
2213
Jeff Brownc6d282b2010-10-14 21:42:15 -07002214 // Tool Size
2215 switch (mCalibration.toolSizeCalibration) {
2216 case Calibration::TOOL_SIZE_CALIBRATION_NONE:
2217 dump.append(INDENT4 "touch.toolSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002218 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002219 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
2220 dump.append(INDENT4 "touch.toolSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002221 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002222 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
2223 dump.append(INDENT4 "touch.toolSize.calibration: linear\n");
2224 break;
2225 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2226 dump.append(INDENT4 "touch.toolSize.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002227 break;
2228 default:
2229 assert(false);
2230 }
2231
Jeff Brownc6d282b2010-10-14 21:42:15 -07002232 if (mCalibration.haveToolSizeLinearScale) {
2233 dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n",
2234 mCalibration.toolSizeLinearScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002235 }
2236
Jeff Brownc6d282b2010-10-14 21:42:15 -07002237 if (mCalibration.haveToolSizeLinearBias) {
2238 dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n",
2239 mCalibration.toolSizeLinearBias);
Jeff Brown8d608662010-08-30 03:02:23 -07002240 }
2241
Jeff Brownc6d282b2010-10-14 21:42:15 -07002242 if (mCalibration.haveToolSizeAreaScale) {
2243 dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n",
2244 mCalibration.toolSizeAreaScale);
2245 }
2246
2247 if (mCalibration.haveToolSizeAreaBias) {
2248 dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n",
2249 mCalibration.toolSizeAreaBias);
2250 }
2251
2252 if (mCalibration.haveToolSizeIsSummed) {
Jeff Brown1f245102010-11-18 20:53:46 -08002253 dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %s\n",
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002254 toString(mCalibration.toolSizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07002255 }
2256
2257 // Pressure
2258 switch (mCalibration.pressureCalibration) {
2259 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002260 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002261 break;
2262 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002263 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002264 break;
2265 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002266 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002267 break;
2268 default:
2269 assert(false);
2270 }
2271
2272 switch (mCalibration.pressureSource) {
2273 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002274 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002275 break;
2276 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002277 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002278 break;
2279 case Calibration::PRESSURE_SOURCE_DEFAULT:
2280 break;
2281 default:
2282 assert(false);
2283 }
2284
2285 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07002286 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
2287 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002288 }
2289
2290 // Size
2291 switch (mCalibration.sizeCalibration) {
2292 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002293 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002294 break;
2295 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002296 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002297 break;
2298 default:
2299 assert(false);
2300 }
2301
2302 // Orientation
2303 switch (mCalibration.orientationCalibration) {
2304 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002305 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002306 break;
2307 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002308 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002309 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002310 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
2311 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
2312 break;
Jeff Brown8d608662010-08-30 03:02:23 -07002313 default:
2314 assert(false);
2315 }
2316}
2317
Jeff Brown6d0fec22010-07-23 21:28:06 -07002318void TouchInputMapper::reset() {
2319 // Synthesize touch up event if touch is currently down.
2320 // This will also take care of finishing virtual key processing if needed.
2321 if (mLastTouch.pointerCount != 0) {
2322 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
2323 mCurrentTouch.clear();
2324 syncTouch(when, true);
2325 }
2326
Jeff Brown6328cdc2010-07-29 18:18:33 -07002327 { // acquire lock
2328 AutoMutex _l(mLock);
2329 initializeLocked();
2330 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07002331
Jeff Brown6328cdc2010-07-29 18:18:33 -07002332 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002333}
2334
2335void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002336 // Preprocess pointer data.
Jeff Brown6d0fec22010-07-23 21:28:06 -07002337 if (mParameters.useBadTouchFilter) {
2338 if (applyBadTouchFilter()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002339 havePointerIds = false;
2340 }
2341 }
2342
Jeff Brown6d0fec22010-07-23 21:28:06 -07002343 if (mParameters.useJumpyTouchFilter) {
2344 if (applyJumpyTouchFilter()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002345 havePointerIds = false;
2346 }
2347 }
2348
2349 if (! havePointerIds) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002350 calculatePointerIds();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002351 }
2352
Jeff Brown6d0fec22010-07-23 21:28:06 -07002353 TouchData temp;
2354 TouchData* savedTouch;
2355 if (mParameters.useAveragingTouchFilter) {
2356 temp.copyFrom(mCurrentTouch);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002357 savedTouch = & temp;
2358
Jeff Brown6d0fec22010-07-23 21:28:06 -07002359 applyAveragingTouchFilter();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002360 } else {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002361 savedTouch = & mCurrentTouch;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002362 }
2363
Jeff Brown56194eb2011-03-02 19:23:13 -08002364 uint32_t policyFlags = 0;
Jeff Brown05dc66a2011-03-02 14:41:58 -08002365 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) {
Jeff Brown56194eb2011-03-02 19:23:13 -08002366 // Hide the pointer on an initial down.
Jeff Brown05dc66a2011-03-02 14:41:58 -08002367 getContext()->fadePointer();
Jeff Brown56194eb2011-03-02 19:23:13 -08002368
2369 // Initial downs on external touch devices should wake the device.
2370 // We don't do this for internal touch screens to prevent them from waking
2371 // up in your pocket.
2372 // TODO: Use the input device configuration to control this behavior more finely.
2373 if (getDevice()->isExternal()) {
2374 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2375 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002376 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002377
Jeff Brown05dc66a2011-03-02 14:41:58 -08002378 // Process touches and virtual keys.
Jeff Brown6d0fec22010-07-23 21:28:06 -07002379 TouchResult touchResult = consumeOffScreenTouches(when, policyFlags);
2380 if (touchResult == DISPATCH_TOUCH) {
Jeff Brownfe508922011-01-18 15:10:10 -08002381 detectGestures(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002382 dispatchTouches(when, policyFlags);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002383 }
2384
Jeff Brown6328cdc2010-07-29 18:18:33 -07002385 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brown6d0fec22010-07-23 21:28:06 -07002386 if (touchResult == DROP_STROKE) {
2387 mLastTouch.clear();
2388 } else {
2389 mLastTouch.copyFrom(*savedTouch);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002390 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002391}
2392
Jeff Brown6d0fec22010-07-23 21:28:06 -07002393TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches(
2394 nsecs_t when, uint32_t policyFlags) {
2395 int32_t keyEventAction, keyEventFlags;
2396 int32_t keyCode, scanCode, downTime;
2397 TouchResult touchResult;
Jeff Brown349703e2010-06-22 01:27:15 -07002398
Jeff Brown6328cdc2010-07-29 18:18:33 -07002399 { // acquire lock
2400 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002401
Jeff Brown6328cdc2010-07-29 18:18:33 -07002402 // Update surface size and orientation, including virtual key positions.
2403 if (! configureSurfaceLocked()) {
2404 return DROP_STROKE;
2405 }
2406
2407 // Check for virtual key press.
2408 if (mLocked.currentVirtualKey.down) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002409 if (mCurrentTouch.pointerCount == 0) {
2410 // Pointer went up while virtual key was down.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002411 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002412#if DEBUG_VIRTUAL_KEYS
2413 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07002414 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002415#endif
2416 keyEventAction = AKEY_EVENT_ACTION_UP;
2417 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2418 touchResult = SKIP_TOUCH;
2419 goto DispatchVirtualKey;
2420 }
2421
2422 if (mCurrentTouch.pointerCount == 1) {
2423 int32_t x = mCurrentTouch.pointers[0].x;
2424 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002425 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
2426 if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002427 // Pointer is still within the space of the virtual key.
2428 return SKIP_TOUCH;
2429 }
2430 }
2431
2432 // Pointer left virtual key area or another pointer also went down.
2433 // Send key cancellation and drop the stroke so subsequent motions will be
2434 // considered fresh downs. This is useful when the user swipes away from the
2435 // virtual key area into the main display surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002436 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002437#if DEBUG_VIRTUAL_KEYS
2438 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07002439 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002440#endif
2441 keyEventAction = AKEY_EVENT_ACTION_UP;
2442 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
2443 | AKEY_EVENT_FLAG_CANCELED;
Jeff Brownc3db8582010-10-20 15:33:38 -07002444
2445 // Check whether the pointer moved inside the display area where we should
2446 // start a new stroke.
2447 int32_t x = mCurrentTouch.pointers[0].x;
2448 int32_t y = mCurrentTouch.pointers[0].y;
2449 if (isPointInsideSurfaceLocked(x, y)) {
2450 mLastTouch.clear();
2451 touchResult = DISPATCH_TOUCH;
2452 } else {
2453 touchResult = DROP_STROKE;
2454 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002455 } else {
2456 if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) {
2457 // Pointer just went down. Handle off-screen touches, if needed.
2458 int32_t x = mCurrentTouch.pointers[0].x;
2459 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002460 if (! isPointInsideSurfaceLocked(x, y)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002461 // If exactly one pointer went down, check for virtual key hit.
2462 // Otherwise we will drop the entire stroke.
2463 if (mCurrentTouch.pointerCount == 1) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002464 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002465 if (virtualKey) {
Jeff Brownfe508922011-01-18 15:10:10 -08002466 if (mContext->shouldDropVirtualKey(when, getDevice(),
2467 virtualKey->keyCode, virtualKey->scanCode)) {
2468 return DROP_STROKE;
2469 }
2470
Jeff Brown6328cdc2010-07-29 18:18:33 -07002471 mLocked.currentVirtualKey.down = true;
2472 mLocked.currentVirtualKey.downTime = when;
2473 mLocked.currentVirtualKey.keyCode = virtualKey->keyCode;
2474 mLocked.currentVirtualKey.scanCode = virtualKey->scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002475#if DEBUG_VIRTUAL_KEYS
2476 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07002477 mLocked.currentVirtualKey.keyCode,
2478 mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002479#endif
2480 keyEventAction = AKEY_EVENT_ACTION_DOWN;
2481 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM
2482 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2483 touchResult = SKIP_TOUCH;
2484 goto DispatchVirtualKey;
2485 }
2486 }
2487 return DROP_STROKE;
2488 }
2489 }
2490 return DISPATCH_TOUCH;
2491 }
2492
2493 DispatchVirtualKey:
2494 // Collect remaining state needed to dispatch virtual key.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002495 keyCode = mLocked.currentVirtualKey.keyCode;
2496 scanCode = mLocked.currentVirtualKey.scanCode;
2497 downTime = mLocked.currentVirtualKey.downTime;
2498 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07002499
2500 // Dispatch virtual key.
2501 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown0eaf3932010-10-01 14:55:30 -07002502 policyFlags |= POLICY_FLAG_VIRTUAL;
Jeff Brownb6997262010-10-08 22:31:17 -07002503 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
2504 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
2505 return touchResult;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002506}
2507
Jeff Brownfe508922011-01-18 15:10:10 -08002508void TouchInputMapper::detectGestures(nsecs_t when) {
2509 // Disable all virtual key touches that happen within a short time interval of the
2510 // most recent touch. The idea is to filter out stray virtual key presses when
2511 // interacting with the touch screen.
2512 //
2513 // Problems we're trying to solve:
2514 //
2515 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
2516 // virtual key area that is implemented by a separate touch panel and accidentally
2517 // triggers a virtual key.
2518 //
2519 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
2520 // area and accidentally triggers a virtual key. This often happens when virtual keys
2521 // are layed out below the screen near to where the on screen keyboard's space bar
2522 // is displayed.
2523 if (mParameters.virtualKeyQuietTime > 0 && mCurrentTouch.pointerCount != 0) {
2524 mContext->disableVirtualKeysUntil(when + mParameters.virtualKeyQuietTime);
2525 }
2526}
2527
Jeff Brown6d0fec22010-07-23 21:28:06 -07002528void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
2529 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2530 uint32_t lastPointerCount = mLastTouch.pointerCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002531 if (currentPointerCount == 0 && lastPointerCount == 0) {
2532 return; // nothing to do!
2533 }
2534
Jeff Brown6d0fec22010-07-23 21:28:06 -07002535 BitSet32 currentIdBits = mCurrentTouch.idBits;
2536 BitSet32 lastIdBits = mLastTouch.idBits;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002537
2538 if (currentIdBits == lastIdBits) {
2539 // No pointer id changes so this is a move event.
2540 // The dispatcher takes care of batching moves so we don't have to deal with that here.
Jeff Brownc5ed5912010-07-14 18:48:53 -07002541 int32_t motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002542 dispatchTouch(when, policyFlags, & mCurrentTouch,
Jeff Brown8d608662010-08-30 03:02:23 -07002543 currentIdBits, -1, currentPointerCount, motionEventAction);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002544 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07002545 // There may be pointers going up and pointers going down and pointers moving
2546 // all at the same time.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002547 BitSet32 upIdBits(lastIdBits.value & ~ currentIdBits.value);
2548 BitSet32 downIdBits(currentIdBits.value & ~ lastIdBits.value);
2549 BitSet32 activeIdBits(lastIdBits.value);
Jeff Brown8d608662010-08-30 03:02:23 -07002550 uint32_t pointerCount = lastPointerCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002551
Jeff Brownc3db8582010-10-20 15:33:38 -07002552 // Produce an intermediate representation of the touch data that consists of the
2553 // old location of pointers that have just gone up and the new location of pointers that
2554 // have just moved but omits the location of pointers that have just gone down.
2555 TouchData interimTouch;
2556 interimTouch.copyFrom(mLastTouch);
2557
2558 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
2559 bool moveNeeded = false;
2560 while (!moveIdBits.isEmpty()) {
2561 uint32_t moveId = moveIdBits.firstMarkedBit();
2562 moveIdBits.clearBit(moveId);
2563
2564 int32_t oldIndex = mLastTouch.idToIndex[moveId];
2565 int32_t newIndex = mCurrentTouch.idToIndex[moveId];
2566 if (mLastTouch.pointers[oldIndex] != mCurrentTouch.pointers[newIndex]) {
2567 interimTouch.pointers[oldIndex] = mCurrentTouch.pointers[newIndex];
2568 moveNeeded = true;
2569 }
2570 }
2571
2572 // Dispatch pointer up events using the interim pointer locations.
2573 while (!upIdBits.isEmpty()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002574 uint32_t upId = upIdBits.firstMarkedBit();
2575 upIdBits.clearBit(upId);
2576 BitSet32 oldActiveIdBits = activeIdBits;
2577 activeIdBits.clearBit(upId);
2578
2579 int32_t motionEventAction;
2580 if (activeIdBits.isEmpty()) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07002581 motionEventAction = AMOTION_EVENT_ACTION_UP;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002582 } else {
Jeff Brown00ba8842010-07-16 15:01:56 -07002583 motionEventAction = AMOTION_EVENT_ACTION_POINTER_UP;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002584 }
2585
Jeff Brownc3db8582010-10-20 15:33:38 -07002586 dispatchTouch(when, policyFlags, &interimTouch,
Jeff Brown8d608662010-08-30 03:02:23 -07002587 oldActiveIdBits, upId, pointerCount, motionEventAction);
2588 pointerCount -= 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002589 }
2590
Jeff Brownc3db8582010-10-20 15:33:38 -07002591 // Dispatch move events if any of the remaining pointers moved from their old locations.
2592 // Although applications receive new locations as part of individual pointer up
2593 // events, they do not generally handle them except when presented in a move event.
2594 if (moveNeeded) {
2595 dispatchTouch(when, policyFlags, &mCurrentTouch,
2596 activeIdBits, -1, pointerCount, AMOTION_EVENT_ACTION_MOVE);
2597 }
2598
2599 // Dispatch pointer down events using the new pointer locations.
2600 while (!downIdBits.isEmpty()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002601 uint32_t downId = downIdBits.firstMarkedBit();
2602 downIdBits.clearBit(downId);
2603 BitSet32 oldActiveIdBits = activeIdBits;
2604 activeIdBits.markBit(downId);
2605
2606 int32_t motionEventAction;
2607 if (oldActiveIdBits.isEmpty()) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07002608 motionEventAction = AMOTION_EVENT_ACTION_DOWN;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002609 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002610 } else {
Jeff Brown00ba8842010-07-16 15:01:56 -07002611 motionEventAction = AMOTION_EVENT_ACTION_POINTER_DOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002612 }
2613
Jeff Brown8d608662010-08-30 03:02:23 -07002614 pointerCount += 1;
Jeff Brownc3db8582010-10-20 15:33:38 -07002615 dispatchTouch(when, policyFlags, &mCurrentTouch,
Jeff Brown8d608662010-08-30 03:02:23 -07002616 activeIdBits, downId, pointerCount, motionEventAction);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002617 }
2618 }
2619}
2620
Jeff Brown6d0fec22010-07-23 21:28:06 -07002621void TouchInputMapper::dispatchTouch(nsecs_t when, uint32_t policyFlags,
Jeff Brown8d608662010-08-30 03:02:23 -07002622 TouchData* touch, BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002623 int32_t motionEventAction) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002624 int32_t pointerIds[MAX_POINTERS];
2625 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002626 int32_t motionEventEdgeFlags = 0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002627 float xPrecision, yPrecision;
2628
2629 { // acquire lock
2630 AutoMutex _l(mLock);
2631
2632 // Walk through the the active pointers and map touch screen coordinates (TouchData) into
2633 // display coordinates (PointerCoords) and adjust for display orientation.
Jeff Brown8d608662010-08-30 03:02:23 -07002634 for (uint32_t outIndex = 0; ! idBits.isEmpty(); outIndex++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002635 uint32_t id = idBits.firstMarkedBit();
2636 idBits.clearBit(id);
Jeff Brown8d608662010-08-30 03:02:23 -07002637 uint32_t inIndex = touch->idToIndex[id];
Jeff Brown6328cdc2010-07-29 18:18:33 -07002638
Jeff Brown8d608662010-08-30 03:02:23 -07002639 const PointerData& in = touch->pointers[inIndex];
Jeff Brown6328cdc2010-07-29 18:18:33 -07002640
Jeff Brown8d608662010-08-30 03:02:23 -07002641 // X and Y
2642 float x = float(in.x - mLocked.xOrigin) * mLocked.xScale;
2643 float y = float(in.y - mLocked.yOrigin) * mLocked.yScale;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002644
Jeff Brown8d608662010-08-30 03:02:23 -07002645 // ToolMajor and ToolMinor
2646 float toolMajor, toolMinor;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002647 switch (mCalibration.toolSizeCalibration) {
2648 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brown8d608662010-08-30 03:02:23 -07002649 toolMajor = in.toolMajor * mLocked.geometricScale;
2650 if (mRawAxes.toolMinor.valid) {
2651 toolMinor = in.toolMinor * mLocked.geometricScale;
2652 } else {
2653 toolMinor = toolMajor;
2654 }
2655 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002656 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
Jeff Brown8d608662010-08-30 03:02:23 -07002657 toolMajor = in.toolMajor != 0
Jeff Brownc6d282b2010-10-14 21:42:15 -07002658 ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias
Jeff Brown8d608662010-08-30 03:02:23 -07002659 : 0;
2660 if (mRawAxes.toolMinor.valid) {
2661 toolMinor = in.toolMinor != 0
Jeff Brownc6d282b2010-10-14 21:42:15 -07002662 ? in.toolMinor * mLocked.toolSizeLinearScale
2663 + mLocked.toolSizeLinearBias
Jeff Brown8d608662010-08-30 03:02:23 -07002664 : 0;
2665 } else {
2666 toolMinor = toolMajor;
2667 }
2668 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002669 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2670 if (in.toolMajor != 0) {
2671 float diameter = sqrtf(in.toolMajor
2672 * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias);
2673 toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias;
2674 } else {
2675 toolMajor = 0;
2676 }
2677 toolMinor = toolMajor;
2678 break;
Jeff Brown8d608662010-08-30 03:02:23 -07002679 default:
2680 toolMajor = 0;
2681 toolMinor = 0;
2682 break;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002683 }
2684
Jeff Brownc6d282b2010-10-14 21:42:15 -07002685 if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) {
Jeff Brown8d608662010-08-30 03:02:23 -07002686 toolMajor /= pointerCount;
2687 toolMinor /= pointerCount;
2688 }
2689
2690 // Pressure
2691 float rawPressure;
2692 switch (mCalibration.pressureSource) {
2693 case Calibration::PRESSURE_SOURCE_PRESSURE:
2694 rawPressure = in.pressure;
2695 break;
2696 case Calibration::PRESSURE_SOURCE_TOUCH:
2697 rawPressure = in.touchMajor;
2698 break;
2699 default:
2700 rawPressure = 0;
2701 }
2702
2703 float pressure;
2704 switch (mCalibration.pressureCalibration) {
2705 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
2706 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
2707 pressure = rawPressure * mLocked.pressureScale;
2708 break;
2709 default:
2710 pressure = 1;
2711 break;
2712 }
2713
2714 // TouchMajor and TouchMinor
2715 float touchMajor, touchMinor;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002716 switch (mCalibration.touchSizeCalibration) {
2717 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brown8d608662010-08-30 03:02:23 -07002718 touchMajor = in.touchMajor * mLocked.geometricScale;
2719 if (mRawAxes.touchMinor.valid) {
2720 touchMinor = in.touchMinor * mLocked.geometricScale;
2721 } else {
2722 touchMinor = touchMajor;
2723 }
2724 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002725 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
Jeff Brown8d608662010-08-30 03:02:23 -07002726 touchMajor = toolMajor * pressure;
2727 touchMinor = toolMinor * pressure;
2728 break;
2729 default:
2730 touchMajor = 0;
2731 touchMinor = 0;
2732 break;
2733 }
2734
2735 if (touchMajor > toolMajor) {
2736 touchMajor = toolMajor;
2737 }
2738 if (touchMinor > toolMinor) {
2739 touchMinor = toolMinor;
2740 }
2741
2742 // Size
2743 float size;
2744 switch (mCalibration.sizeCalibration) {
2745 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
2746 float rawSize = mRawAxes.toolMinor.valid
2747 ? avg(in.toolMajor, in.toolMinor)
2748 : in.toolMajor;
2749 size = rawSize * mLocked.sizeScale;
2750 break;
2751 }
2752 default:
2753 size = 0;
2754 break;
2755 }
2756
2757 // Orientation
2758 float orientation;
2759 switch (mCalibration.orientationCalibration) {
2760 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
2761 orientation = in.orientation * mLocked.orientationScale;
2762 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002763 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
2764 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
2765 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
2766 if (c1 != 0 || c2 != 0) {
2767 orientation = atan2f(c1, c2) * 0.5f;
Jeff Brownc3451d42011-02-15 19:13:20 -08002768 float scale = 1.0f + pythag(c1, c2) / 16.0f;
2769 touchMajor *= scale;
2770 touchMinor /= scale;
2771 toolMajor *= scale;
2772 toolMinor /= scale;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002773 } else {
2774 orientation = 0;
2775 }
2776 break;
2777 }
Jeff Brown8d608662010-08-30 03:02:23 -07002778 default:
2779 orientation = 0;
2780 }
2781
2782 // Adjust coords for orientation.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002783 switch (mLocked.surfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002784 case DISPLAY_ORIENTATION_90: {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002785 float xTemp = x;
2786 x = y;
2787 y = mLocked.surfaceWidth - xTemp;
2788 orientation -= M_PI_2;
2789 if (orientation < - M_PI_2) {
2790 orientation += M_PI;
2791 }
2792 break;
2793 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002794 case DISPLAY_ORIENTATION_180: {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002795 x = mLocked.surfaceWidth - x;
2796 y = mLocked.surfaceHeight - y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002797 break;
2798 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002799 case DISPLAY_ORIENTATION_270: {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002800 float xTemp = x;
2801 x = mLocked.surfaceHeight - y;
2802 y = xTemp;
2803 orientation += M_PI_2;
2804 if (orientation > M_PI_2) {
2805 orientation -= M_PI;
2806 }
2807 break;
2808 }
2809 }
2810
Jeff Brown8d608662010-08-30 03:02:23 -07002811 // Write output coords.
2812 PointerCoords& out = pointerCoords[outIndex];
Jeff Brown91c69ab2011-02-14 17:03:18 -08002813 out.clear();
Jeff Brownebbd5d12011-02-17 13:01:34 -08002814 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2815 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2816 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
2817 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
2818 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
2819 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
2820 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
2821 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
2822 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002823
Jeff Brown8d608662010-08-30 03:02:23 -07002824 pointerIds[outIndex] = int32_t(id);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002825
2826 if (id == changedId) {
Jeff Brown8d608662010-08-30 03:02:23 -07002827 motionEventAction |= outIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002828 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002829 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002830
2831 // Check edge flags by looking only at the first pointer since the flags are
2832 // global to the event.
2833 if (motionEventAction == AMOTION_EVENT_ACTION_DOWN) {
Jeff Brownebbd5d12011-02-17 13:01:34 -08002834 float x = pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X);
2835 float y = pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08002836
2837 if (x <= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002838 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_LEFT;
Jeff Brown91c69ab2011-02-14 17:03:18 -08002839 } else if (x >= mLocked.orientedSurfaceWidth) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002840 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_RIGHT;
2841 }
Jeff Brown91c69ab2011-02-14 17:03:18 -08002842 if (y <= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002843 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_TOP;
Jeff Brown91c69ab2011-02-14 17:03:18 -08002844 } else if (y >= mLocked.orientedSurfaceHeight) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002845 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_BOTTOM;
2846 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002847 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002848
2849 xPrecision = mLocked.orientedXPrecision;
2850 yPrecision = mLocked.orientedYPrecision;
2851 } // release lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002852
Jeff Brown83c09682010-12-23 17:50:18 -08002853 getDispatcher()->notifyMotion(when, getDeviceId(), mSources, policyFlags,
Jeff Brown85a31762010-09-01 17:01:00 -07002854 motionEventAction, 0, getContext()->getGlobalMetaState(), motionEventEdgeFlags,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002855 pointerCount, pointerIds, pointerCoords,
Jeff Brown6328cdc2010-07-29 18:18:33 -07002856 xPrecision, yPrecision, mDownTime);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002857}
2858
Jeff Brown6328cdc2010-07-29 18:18:33 -07002859bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) {
Jeff Brown8d608662010-08-30 03:02:23 -07002860 if (mRawAxes.x.valid && mRawAxes.y.valid) {
2861 return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue
2862 && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002863 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002864 return true;
2865}
2866
Jeff Brown6328cdc2010-07-29 18:18:33 -07002867const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked(
2868 int32_t x, int32_t y) {
2869 size_t numVirtualKeys = mLocked.virtualKeys.size();
2870 for (size_t i = 0; i < numVirtualKeys; i++) {
2871 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07002872
2873#if DEBUG_VIRTUAL_KEYS
2874 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
2875 "left=%d, top=%d, right=%d, bottom=%d",
2876 x, y,
2877 virtualKey.keyCode, virtualKey.scanCode,
2878 virtualKey.hitLeft, virtualKey.hitTop,
2879 virtualKey.hitRight, virtualKey.hitBottom);
2880#endif
2881
2882 if (virtualKey.isHit(x, y)) {
2883 return & virtualKey;
2884 }
2885 }
2886
2887 return NULL;
2888}
2889
2890void TouchInputMapper::calculatePointerIds() {
2891 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2892 uint32_t lastPointerCount = mLastTouch.pointerCount;
2893
2894 if (currentPointerCount == 0) {
2895 // No pointers to assign.
2896 mCurrentTouch.idBits.clear();
2897 } else if (lastPointerCount == 0) {
2898 // All pointers are new.
2899 mCurrentTouch.idBits.clear();
2900 for (uint32_t i = 0; i < currentPointerCount; i++) {
2901 mCurrentTouch.pointers[i].id = i;
2902 mCurrentTouch.idToIndex[i] = i;
2903 mCurrentTouch.idBits.markBit(i);
2904 }
2905 } else if (currentPointerCount == 1 && lastPointerCount == 1) {
2906 // Only one pointer and no change in count so it must have the same id as before.
2907 uint32_t id = mLastTouch.pointers[0].id;
2908 mCurrentTouch.pointers[0].id = id;
2909 mCurrentTouch.idToIndex[id] = 0;
2910 mCurrentTouch.idBits.value = BitSet32::valueForBit(id);
2911 } else {
2912 // General case.
2913 // We build a heap of squared euclidean distances between current and last pointers
2914 // associated with the current and last pointer indices. Then, we find the best
2915 // match (by distance) for each current pointer.
2916 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
2917
2918 uint32_t heapSize = 0;
2919 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
2920 currentPointerIndex++) {
2921 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
2922 lastPointerIndex++) {
2923 int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x
2924 - mLastTouch.pointers[lastPointerIndex].x;
2925 int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y
2926 - mLastTouch.pointers[lastPointerIndex].y;
2927
2928 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
2929
2930 // Insert new element into the heap (sift up).
2931 heap[heapSize].currentPointerIndex = currentPointerIndex;
2932 heap[heapSize].lastPointerIndex = lastPointerIndex;
2933 heap[heapSize].distance = distance;
2934 heapSize += 1;
2935 }
2936 }
2937
2938 // Heapify
2939 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
2940 startIndex -= 1;
2941 for (uint32_t parentIndex = startIndex; ;) {
2942 uint32_t childIndex = parentIndex * 2 + 1;
2943 if (childIndex >= heapSize) {
2944 break;
2945 }
2946
2947 if (childIndex + 1 < heapSize
2948 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2949 childIndex += 1;
2950 }
2951
2952 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2953 break;
2954 }
2955
2956 swap(heap[parentIndex], heap[childIndex]);
2957 parentIndex = childIndex;
2958 }
2959 }
2960
2961#if DEBUG_POINTER_ASSIGNMENT
2962 LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize);
2963 for (size_t i = 0; i < heapSize; i++) {
2964 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2965 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2966 heap[i].distance);
2967 }
2968#endif
2969
2970 // Pull matches out by increasing order of distance.
2971 // To avoid reassigning pointers that have already been matched, the loop keeps track
2972 // of which last and current pointers have been matched using the matchedXXXBits variables.
2973 // It also tracks the used pointer id bits.
2974 BitSet32 matchedLastBits(0);
2975 BitSet32 matchedCurrentBits(0);
2976 BitSet32 usedIdBits(0);
2977 bool first = true;
2978 for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) {
2979 for (;;) {
2980 if (first) {
2981 // The first time through the loop, we just consume the root element of
2982 // the heap (the one with smallest distance).
2983 first = false;
2984 } else {
2985 // Previous iterations consumed the root element of the heap.
2986 // Pop root element off of the heap (sift down).
2987 heapSize -= 1;
2988 assert(heapSize > 0);
2989
2990 // Sift down.
2991 heap[0] = heap[heapSize];
2992 for (uint32_t parentIndex = 0; ;) {
2993 uint32_t childIndex = parentIndex * 2 + 1;
2994 if (childIndex >= heapSize) {
2995 break;
2996 }
2997
2998 if (childIndex + 1 < heapSize
2999 && heap[childIndex + 1].distance < heap[childIndex].distance) {
3000 childIndex += 1;
3001 }
3002
3003 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3004 break;
3005 }
3006
3007 swap(heap[parentIndex], heap[childIndex]);
3008 parentIndex = childIndex;
3009 }
3010
3011#if DEBUG_POINTER_ASSIGNMENT
3012 LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize);
3013 for (size_t i = 0; i < heapSize; i++) {
3014 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
3015 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
3016 heap[i].distance);
3017 }
3018#endif
3019 }
3020
3021 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
3022 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
3023
3024 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
3025 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
3026
3027 matchedCurrentBits.markBit(currentPointerIndex);
3028 matchedLastBits.markBit(lastPointerIndex);
3029
3030 uint32_t id = mLastTouch.pointers[lastPointerIndex].id;
3031 mCurrentTouch.pointers[currentPointerIndex].id = id;
3032 mCurrentTouch.idToIndex[id] = currentPointerIndex;
3033 usedIdBits.markBit(id);
3034
3035#if DEBUG_POINTER_ASSIGNMENT
3036 LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
3037 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
3038#endif
3039 break;
3040 }
3041 }
3042
3043 // Assign fresh ids to new pointers.
3044 if (currentPointerCount > lastPointerCount) {
3045 for (uint32_t i = currentPointerCount - lastPointerCount; ;) {
3046 uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit();
3047 uint32_t id = usedIdBits.firstUnmarkedBit();
3048
3049 mCurrentTouch.pointers[currentPointerIndex].id = id;
3050 mCurrentTouch.idToIndex[id] = currentPointerIndex;
3051 usedIdBits.markBit(id);
3052
3053#if DEBUG_POINTER_ASSIGNMENT
3054 LOGD("calculatePointerIds - assigned: cur=%d, id=%d",
3055 currentPointerIndex, id);
3056#endif
3057
3058 if (--i == 0) break; // done
3059 matchedCurrentBits.markBit(currentPointerIndex);
3060 }
3061 }
3062
3063 // Fix id bits.
3064 mCurrentTouch.idBits = usedIdBits;
3065 }
3066}
3067
3068/* Special hack for devices that have bad screen data: if one of the
3069 * points has moved more than a screen height from the last position,
3070 * then drop it. */
3071bool TouchInputMapper::applyBadTouchFilter() {
3072 // This hack requires valid axis parameters.
Jeff Brown8d608662010-08-30 03:02:23 -07003073 if (! mRawAxes.y.valid) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003074 return false;
3075 }
3076
3077 uint32_t pointerCount = mCurrentTouch.pointerCount;
3078
3079 // Nothing to do if there are no points.
3080 if (pointerCount == 0) {
3081 return false;
3082 }
3083
3084 // Don't do anything if a finger is going down or up. We run
3085 // here before assigning pointer IDs, so there isn't a good
3086 // way to do per-finger matching.
3087 if (pointerCount != mLastTouch.pointerCount) {
3088 return false;
3089 }
3090
3091 // We consider a single movement across more than a 7/16 of
3092 // the long size of the screen to be bad. This was a magic value
3093 // determined by looking at the maximum distance it is feasible
3094 // to actually move in one sample.
Jeff Brown8d608662010-08-30 03:02:23 -07003095 int32_t maxDeltaY = mRawAxes.y.getRange() * 7 / 16;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003096
3097 // XXX The original code in InputDevice.java included commented out
3098 // code for testing the X axis. Note that when we drop a point
3099 // we don't actually restore the old X either. Strange.
3100 // The old code also tries to track when bad points were previously
3101 // detected but it turns out that due to the placement of a "break"
3102 // at the end of the loop, we never set mDroppedBadPoint to true
3103 // so it is effectively dead code.
3104 // Need to figure out if the old code is busted or just overcomplicated
3105 // but working as intended.
3106
3107 // Look through all new points and see if any are farther than
3108 // acceptable from all previous points.
3109 for (uint32_t i = pointerCount; i-- > 0; ) {
3110 int32_t y = mCurrentTouch.pointers[i].y;
3111 int32_t closestY = INT_MAX;
3112 int32_t closestDeltaY = 0;
3113
3114#if DEBUG_HACKS
3115 LOGD("BadTouchFilter: Looking at next point #%d: y=%d", i, y);
3116#endif
3117
3118 for (uint32_t j = pointerCount; j-- > 0; ) {
3119 int32_t lastY = mLastTouch.pointers[j].y;
3120 int32_t deltaY = abs(y - lastY);
3121
3122#if DEBUG_HACKS
3123 LOGD("BadTouchFilter: Comparing with last point #%d: y=%d deltaY=%d",
3124 j, lastY, deltaY);
3125#endif
3126
3127 if (deltaY < maxDeltaY) {
3128 goto SkipSufficientlyClosePoint;
3129 }
3130 if (deltaY < closestDeltaY) {
3131 closestDeltaY = deltaY;
3132 closestY = lastY;
3133 }
3134 }
3135
3136 // Must not have found a close enough match.
3137#if DEBUG_HACKS
3138 LOGD("BadTouchFilter: Dropping bad point #%d: newY=%d oldY=%d deltaY=%d maxDeltaY=%d",
3139 i, y, closestY, closestDeltaY, maxDeltaY);
3140#endif
3141
3142 mCurrentTouch.pointers[i].y = closestY;
3143 return true; // XXX original code only corrects one point
3144
3145 SkipSufficientlyClosePoint: ;
3146 }
3147
3148 // No change.
3149 return false;
3150}
3151
3152/* Special hack for devices that have bad screen data: drop points where
3153 * the coordinate value for one axis has jumped to the other pointer's location.
3154 */
3155bool TouchInputMapper::applyJumpyTouchFilter() {
3156 // This hack requires valid axis parameters.
Jeff Brown8d608662010-08-30 03:02:23 -07003157 if (! mRawAxes.y.valid) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003158 return false;
3159 }
3160
3161 uint32_t pointerCount = mCurrentTouch.pointerCount;
3162 if (mLastTouch.pointerCount != pointerCount) {
3163#if DEBUG_HACKS
3164 LOGD("JumpyTouchFilter: Different pointer count %d -> %d",
3165 mLastTouch.pointerCount, pointerCount);
3166 for (uint32_t i = 0; i < pointerCount; i++) {
3167 LOGD(" Pointer %d (%d, %d)", i,
3168 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
3169 }
3170#endif
3171
3172 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_TRANSITION_DROPS) {
3173 if (mLastTouch.pointerCount == 1 && pointerCount == 2) {
3174 // Just drop the first few events going from 1 to 2 pointers.
3175 // They're bad often enough that they're not worth considering.
3176 mCurrentTouch.pointerCount = 1;
3177 mJumpyTouchFilter.jumpyPointsDropped += 1;
3178
3179#if DEBUG_HACKS
3180 LOGD("JumpyTouchFilter: Pointer 2 dropped");
3181#endif
3182 return true;
3183 } else if (mLastTouch.pointerCount == 2 && pointerCount == 1) {
3184 // The event when we go from 2 -> 1 tends to be messed up too
3185 mCurrentTouch.pointerCount = 2;
3186 mCurrentTouch.pointers[0] = mLastTouch.pointers[0];
3187 mCurrentTouch.pointers[1] = mLastTouch.pointers[1];
3188 mJumpyTouchFilter.jumpyPointsDropped += 1;
3189
3190#if DEBUG_HACKS
3191 for (int32_t i = 0; i < 2; i++) {
3192 LOGD("JumpyTouchFilter: Pointer %d replaced (%d, %d)", i,
3193 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
3194 }
3195#endif
3196 return true;
3197 }
3198 }
3199 // Reset jumpy points dropped on other transitions or if limit exceeded.
3200 mJumpyTouchFilter.jumpyPointsDropped = 0;
3201
3202#if DEBUG_HACKS
3203 LOGD("JumpyTouchFilter: Transition - drop limit reset");
3204#endif
3205 return false;
3206 }
3207
3208 // We have the same number of pointers as last time.
3209 // A 'jumpy' point is one where the coordinate value for one axis
3210 // has jumped to the other pointer's location. No need to do anything
3211 // else if we only have one pointer.
3212 if (pointerCount < 2) {
3213 return false;
3214 }
3215
3216 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_DROP_LIMIT) {
Jeff Brown8d608662010-08-30 03:02:23 -07003217 int jumpyEpsilon = mRawAxes.y.getRange() / JUMPY_EPSILON_DIVISOR;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003218
3219 // We only replace the single worst jumpy point as characterized by pointer distance
3220 // in a single axis.
3221 int32_t badPointerIndex = -1;
3222 int32_t badPointerReplacementIndex = -1;
3223 int32_t badPointerDistance = INT_MIN; // distance to be corrected
3224
3225 for (uint32_t i = pointerCount; i-- > 0; ) {
3226 int32_t x = mCurrentTouch.pointers[i].x;
3227 int32_t y = mCurrentTouch.pointers[i].y;
3228
3229#if DEBUG_HACKS
3230 LOGD("JumpyTouchFilter: Point %d (%d, %d)", i, x, y);
3231#endif
3232
3233 // Check if a touch point is too close to another's coordinates
3234 bool dropX = false, dropY = false;
3235 for (uint32_t j = 0; j < pointerCount; j++) {
3236 if (i == j) {
3237 continue;
3238 }
3239
3240 if (abs(x - mCurrentTouch.pointers[j].x) <= jumpyEpsilon) {
3241 dropX = true;
3242 break;
3243 }
3244
3245 if (abs(y - mCurrentTouch.pointers[j].y) <= jumpyEpsilon) {
3246 dropY = true;
3247 break;
3248 }
3249 }
3250 if (! dropX && ! dropY) {
3251 continue; // not jumpy
3252 }
3253
3254 // Find a replacement candidate by comparing with older points on the
3255 // complementary (non-jumpy) axis.
3256 int32_t distance = INT_MIN; // distance to be corrected
3257 int32_t replacementIndex = -1;
3258
3259 if (dropX) {
3260 // X looks too close. Find an older replacement point with a close Y.
3261 int32_t smallestDeltaY = INT_MAX;
3262 for (uint32_t j = 0; j < pointerCount; j++) {
3263 int32_t deltaY = abs(y - mLastTouch.pointers[j].y);
3264 if (deltaY < smallestDeltaY) {
3265 smallestDeltaY = deltaY;
3266 replacementIndex = j;
3267 }
3268 }
3269 distance = abs(x - mLastTouch.pointers[replacementIndex].x);
3270 } else {
3271 // Y looks too close. Find an older replacement point with a close X.
3272 int32_t smallestDeltaX = INT_MAX;
3273 for (uint32_t j = 0; j < pointerCount; j++) {
3274 int32_t deltaX = abs(x - mLastTouch.pointers[j].x);
3275 if (deltaX < smallestDeltaX) {
3276 smallestDeltaX = deltaX;
3277 replacementIndex = j;
3278 }
3279 }
3280 distance = abs(y - mLastTouch.pointers[replacementIndex].y);
3281 }
3282
3283 // If replacing this pointer would correct a worse error than the previous ones
3284 // considered, then use this replacement instead.
3285 if (distance > badPointerDistance) {
3286 badPointerIndex = i;
3287 badPointerReplacementIndex = replacementIndex;
3288 badPointerDistance = distance;
3289 }
3290 }
3291
3292 // Correct the jumpy pointer if one was found.
3293 if (badPointerIndex >= 0) {
3294#if DEBUG_HACKS
3295 LOGD("JumpyTouchFilter: Replacing bad pointer %d with (%d, %d)",
3296 badPointerIndex,
3297 mLastTouch.pointers[badPointerReplacementIndex].x,
3298 mLastTouch.pointers[badPointerReplacementIndex].y);
3299#endif
3300
3301 mCurrentTouch.pointers[badPointerIndex].x =
3302 mLastTouch.pointers[badPointerReplacementIndex].x;
3303 mCurrentTouch.pointers[badPointerIndex].y =
3304 mLastTouch.pointers[badPointerReplacementIndex].y;
3305 mJumpyTouchFilter.jumpyPointsDropped += 1;
3306 return true;
3307 }
3308 }
3309
3310 mJumpyTouchFilter.jumpyPointsDropped = 0;
3311 return false;
3312}
3313
3314/* Special hack for devices that have bad screen data: aggregate and
3315 * compute averages of the coordinate data, to reduce the amount of
3316 * jitter seen by applications. */
3317void TouchInputMapper::applyAveragingTouchFilter() {
3318 for (uint32_t currentIndex = 0; currentIndex < mCurrentTouch.pointerCount; currentIndex++) {
3319 uint32_t id = mCurrentTouch.pointers[currentIndex].id;
3320 int32_t x = mCurrentTouch.pointers[currentIndex].x;
3321 int32_t y = mCurrentTouch.pointers[currentIndex].y;
Jeff Brown8d608662010-08-30 03:02:23 -07003322 int32_t pressure;
3323 switch (mCalibration.pressureSource) {
3324 case Calibration::PRESSURE_SOURCE_PRESSURE:
3325 pressure = mCurrentTouch.pointers[currentIndex].pressure;
3326 break;
3327 case Calibration::PRESSURE_SOURCE_TOUCH:
3328 pressure = mCurrentTouch.pointers[currentIndex].touchMajor;
3329 break;
3330 default:
3331 pressure = 1;
3332 break;
3333 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003334
3335 if (mLastTouch.idBits.hasBit(id)) {
3336 // Pointer was down before and is still down now.
3337 // Compute average over history trace.
3338 uint32_t start = mAveragingTouchFilter.historyStart[id];
3339 uint32_t end = mAveragingTouchFilter.historyEnd[id];
3340
3341 int64_t deltaX = x - mAveragingTouchFilter.historyData[end].pointers[id].x;
3342 int64_t deltaY = y - mAveragingTouchFilter.historyData[end].pointers[id].y;
3343 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3344
3345#if DEBUG_HACKS
3346 LOGD("AveragingTouchFilter: Pointer id %d - Distance from last sample: %lld",
3347 id, distance);
3348#endif
3349
3350 if (distance < AVERAGING_DISTANCE_LIMIT) {
3351 // Increment end index in preparation for recording new historical data.
3352 end += 1;
3353 if (end > AVERAGING_HISTORY_SIZE) {
3354 end = 0;
3355 }
3356
3357 // If the end index has looped back to the start index then we have filled
3358 // the historical trace up to the desired size so we drop the historical
3359 // data at the start of the trace.
3360 if (end == start) {
3361 start += 1;
3362 if (start > AVERAGING_HISTORY_SIZE) {
3363 start = 0;
3364 }
3365 }
3366
3367 // Add the raw data to the historical trace.
3368 mAveragingTouchFilter.historyStart[id] = start;
3369 mAveragingTouchFilter.historyEnd[id] = end;
3370 mAveragingTouchFilter.historyData[end].pointers[id].x = x;
3371 mAveragingTouchFilter.historyData[end].pointers[id].y = y;
3372 mAveragingTouchFilter.historyData[end].pointers[id].pressure = pressure;
3373
3374 // Average over all historical positions in the trace by total pressure.
3375 int32_t averagedX = 0;
3376 int32_t averagedY = 0;
3377 int32_t totalPressure = 0;
3378 for (;;) {
3379 int32_t historicalX = mAveragingTouchFilter.historyData[start].pointers[id].x;
3380 int32_t historicalY = mAveragingTouchFilter.historyData[start].pointers[id].y;
3381 int32_t historicalPressure = mAveragingTouchFilter.historyData[start]
3382 .pointers[id].pressure;
3383
3384 averagedX += historicalX * historicalPressure;
3385 averagedY += historicalY * historicalPressure;
3386 totalPressure += historicalPressure;
3387
3388 if (start == end) {
3389 break;
3390 }
3391
3392 start += 1;
3393 if (start > AVERAGING_HISTORY_SIZE) {
3394 start = 0;
3395 }
3396 }
3397
Jeff Brown8d608662010-08-30 03:02:23 -07003398 if (totalPressure != 0) {
3399 averagedX /= totalPressure;
3400 averagedY /= totalPressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003401
3402#if DEBUG_HACKS
Jeff Brown8d608662010-08-30 03:02:23 -07003403 LOGD("AveragingTouchFilter: Pointer id %d - "
3404 "totalPressure=%d, averagedX=%d, averagedY=%d", id, totalPressure,
3405 averagedX, averagedY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003406#endif
3407
Jeff Brown8d608662010-08-30 03:02:23 -07003408 mCurrentTouch.pointers[currentIndex].x = averagedX;
3409 mCurrentTouch.pointers[currentIndex].y = averagedY;
3410 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003411 } else {
3412#if DEBUG_HACKS
3413 LOGD("AveragingTouchFilter: Pointer id %d - Exceeded max distance", id);
3414#endif
3415 }
3416 } else {
3417#if DEBUG_HACKS
3418 LOGD("AveragingTouchFilter: Pointer id %d - Pointer went up", id);
3419#endif
3420 }
3421
3422 // Reset pointer history.
3423 mAveragingTouchFilter.historyStart[id] = 0;
3424 mAveragingTouchFilter.historyEnd[id] = 0;
3425 mAveragingTouchFilter.historyData[0].pointers[id].x = x;
3426 mAveragingTouchFilter.historyData[0].pointers[id].y = y;
3427 mAveragingTouchFilter.historyData[0].pointers[id].pressure = pressure;
3428 }
3429}
3430
3431int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003432 { // acquire lock
3433 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003434
Jeff Brown6328cdc2010-07-29 18:18:33 -07003435 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003436 return AKEY_STATE_VIRTUAL;
3437 }
3438
Jeff Brown6328cdc2010-07-29 18:18:33 -07003439 size_t numVirtualKeys = mLocked.virtualKeys.size();
3440 for (size_t i = 0; i < numVirtualKeys; i++) {
3441 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07003442 if (virtualKey.keyCode == keyCode) {
3443 return AKEY_STATE_UP;
3444 }
3445 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003446 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07003447
3448 return AKEY_STATE_UNKNOWN;
3449}
3450
3451int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003452 { // acquire lock
3453 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003454
Jeff Brown6328cdc2010-07-29 18:18:33 -07003455 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003456 return AKEY_STATE_VIRTUAL;
3457 }
3458
Jeff Brown6328cdc2010-07-29 18:18:33 -07003459 size_t numVirtualKeys = mLocked.virtualKeys.size();
3460 for (size_t i = 0; i < numVirtualKeys; i++) {
3461 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07003462 if (virtualKey.scanCode == scanCode) {
3463 return AKEY_STATE_UP;
3464 }
3465 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003466 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07003467
3468 return AKEY_STATE_UNKNOWN;
3469}
3470
3471bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
3472 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003473 { // acquire lock
3474 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003475
Jeff Brown6328cdc2010-07-29 18:18:33 -07003476 size_t numVirtualKeys = mLocked.virtualKeys.size();
3477 for (size_t i = 0; i < numVirtualKeys; i++) {
3478 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07003479
3480 for (size_t i = 0; i < numCodes; i++) {
3481 if (virtualKey.keyCode == keyCodes[i]) {
3482 outFlags[i] = 1;
3483 }
3484 }
3485 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003486 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07003487
3488 return true;
3489}
3490
3491
3492// --- SingleTouchInputMapper ---
3493
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003494SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
3495 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003496 initialize();
3497}
3498
3499SingleTouchInputMapper::~SingleTouchInputMapper() {
3500}
3501
3502void SingleTouchInputMapper::initialize() {
3503 mAccumulator.clear();
3504
3505 mDown = false;
3506 mX = 0;
3507 mY = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003508 mPressure = 0; // default to 0 for devices that don't report pressure
3509 mToolWidth = 0; // default to 0 for devices that don't report tool width
Jeff Brown6d0fec22010-07-23 21:28:06 -07003510}
3511
3512void SingleTouchInputMapper::reset() {
3513 TouchInputMapper::reset();
3514
Jeff Brown6d0fec22010-07-23 21:28:06 -07003515 initialize();
3516 }
3517
3518void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
3519 switch (rawEvent->type) {
3520 case EV_KEY:
3521 switch (rawEvent->scanCode) {
3522 case BTN_TOUCH:
3523 mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH;
3524 mAccumulator.btnTouch = rawEvent->value != 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003525 // Don't sync immediately. Wait until the next SYN_REPORT since we might
3526 // not have received valid position information yet. This logic assumes that
3527 // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003528 break;
3529 }
3530 break;
3531
3532 case EV_ABS:
3533 switch (rawEvent->scanCode) {
3534 case ABS_X:
3535 mAccumulator.fields |= Accumulator::FIELD_ABS_X;
3536 mAccumulator.absX = rawEvent->value;
3537 break;
3538 case ABS_Y:
3539 mAccumulator.fields |= Accumulator::FIELD_ABS_Y;
3540 mAccumulator.absY = rawEvent->value;
3541 break;
3542 case ABS_PRESSURE:
3543 mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE;
3544 mAccumulator.absPressure = rawEvent->value;
3545 break;
3546 case ABS_TOOL_WIDTH:
3547 mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH;
3548 mAccumulator.absToolWidth = rawEvent->value;
3549 break;
3550 }
3551 break;
3552
3553 case EV_SYN:
3554 switch (rawEvent->scanCode) {
3555 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003556 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003557 break;
3558 }
3559 break;
3560 }
3561}
3562
3563void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003564 uint32_t fields = mAccumulator.fields;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003565 if (fields == 0) {
3566 return; // no new state changes, so nothing to do
3567 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003568
3569 if (fields & Accumulator::FIELD_BTN_TOUCH) {
3570 mDown = mAccumulator.btnTouch;
3571 }
3572
3573 if (fields & Accumulator::FIELD_ABS_X) {
3574 mX = mAccumulator.absX;
3575 }
3576
3577 if (fields & Accumulator::FIELD_ABS_Y) {
3578 mY = mAccumulator.absY;
3579 }
3580
3581 if (fields & Accumulator::FIELD_ABS_PRESSURE) {
3582 mPressure = mAccumulator.absPressure;
3583 }
3584
3585 if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) {
Jeff Brown8d608662010-08-30 03:02:23 -07003586 mToolWidth = mAccumulator.absToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003587 }
3588
3589 mCurrentTouch.clear();
3590
3591 if (mDown) {
3592 mCurrentTouch.pointerCount = 1;
3593 mCurrentTouch.pointers[0].id = 0;
3594 mCurrentTouch.pointers[0].x = mX;
3595 mCurrentTouch.pointers[0].y = mY;
3596 mCurrentTouch.pointers[0].pressure = mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -07003597 mCurrentTouch.pointers[0].touchMajor = 0;
3598 mCurrentTouch.pointers[0].touchMinor = 0;
3599 mCurrentTouch.pointers[0].toolMajor = mToolWidth;
3600 mCurrentTouch.pointers[0].toolMinor = mToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003601 mCurrentTouch.pointers[0].orientation = 0;
3602 mCurrentTouch.idToIndex[0] = 0;
3603 mCurrentTouch.idBits.markBit(0);
3604 }
3605
3606 syncTouch(when, true);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003607
3608 mAccumulator.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003609}
3610
Jeff Brown8d608662010-08-30 03:02:23 -07003611void SingleTouchInputMapper::configureRawAxes() {
3612 TouchInputMapper::configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003613
Jeff Brown8d608662010-08-30 03:02:23 -07003614 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x);
3615 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y);
3616 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure);
3617 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003618}
3619
3620
3621// --- MultiTouchInputMapper ---
3622
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003623MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
3624 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003625 initialize();
3626}
3627
3628MultiTouchInputMapper::~MultiTouchInputMapper() {
3629}
3630
3631void MultiTouchInputMapper::initialize() {
3632 mAccumulator.clear();
3633}
3634
3635void MultiTouchInputMapper::reset() {
3636 TouchInputMapper::reset();
3637
Jeff Brown6d0fec22010-07-23 21:28:06 -07003638 initialize();
3639}
3640
3641void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
3642 switch (rawEvent->type) {
3643 case EV_ABS: {
3644 uint32_t pointerIndex = mAccumulator.pointerCount;
3645 Accumulator::Pointer* pointer = & mAccumulator.pointers[pointerIndex];
3646
3647 switch (rawEvent->scanCode) {
3648 case ABS_MT_POSITION_X:
3649 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_X;
3650 pointer->absMTPositionX = rawEvent->value;
3651 break;
3652 case ABS_MT_POSITION_Y:
3653 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y;
3654 pointer->absMTPositionY = rawEvent->value;
3655 break;
3656 case ABS_MT_TOUCH_MAJOR:
3657 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR;
3658 pointer->absMTTouchMajor = rawEvent->value;
3659 break;
3660 case ABS_MT_TOUCH_MINOR:
3661 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR;
3662 pointer->absMTTouchMinor = rawEvent->value;
3663 break;
3664 case ABS_MT_WIDTH_MAJOR:
3665 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR;
3666 pointer->absMTWidthMajor = rawEvent->value;
3667 break;
3668 case ABS_MT_WIDTH_MINOR:
3669 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR;
3670 pointer->absMTWidthMinor = rawEvent->value;
3671 break;
3672 case ABS_MT_ORIENTATION:
3673 pointer->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION;
3674 pointer->absMTOrientation = rawEvent->value;
3675 break;
3676 case ABS_MT_TRACKING_ID:
3677 pointer->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID;
3678 pointer->absMTTrackingId = rawEvent->value;
3679 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003680 case ABS_MT_PRESSURE:
3681 pointer->fields |= Accumulator::FIELD_ABS_MT_PRESSURE;
3682 pointer->absMTPressure = rawEvent->value;
3683 break;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003684 }
3685 break;
3686 }
3687
3688 case EV_SYN:
3689 switch (rawEvent->scanCode) {
3690 case SYN_MT_REPORT: {
3691 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
3692 uint32_t pointerIndex = mAccumulator.pointerCount;
3693
3694 if (mAccumulator.pointers[pointerIndex].fields) {
3695 if (pointerIndex == MAX_POINTERS) {
3696 LOGW("MultiTouch device driver returned more than maximum of %d pointers.",
3697 MAX_POINTERS);
3698 } else {
3699 pointerIndex += 1;
3700 mAccumulator.pointerCount = pointerIndex;
3701 }
3702 }
3703
3704 mAccumulator.pointers[pointerIndex].clear();
3705 break;
3706 }
3707
3708 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003709 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003710 break;
3711 }
3712 break;
3713 }
3714}
3715
3716void MultiTouchInputMapper::sync(nsecs_t when) {
3717 static const uint32_t REQUIRED_FIELDS =
Jeff Brown8d608662010-08-30 03:02:23 -07003718 Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003719
Jeff Brown6d0fec22010-07-23 21:28:06 -07003720 uint32_t inCount = mAccumulator.pointerCount;
3721 uint32_t outCount = 0;
3722 bool havePointerIds = true;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003723
Jeff Brown6d0fec22010-07-23 21:28:06 -07003724 mCurrentTouch.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003725
Jeff Brown6d0fec22010-07-23 21:28:06 -07003726 for (uint32_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003727 const Accumulator::Pointer& inPointer = mAccumulator.pointers[inIndex];
3728 uint32_t fields = inPointer.fields;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003729
Jeff Brown6d0fec22010-07-23 21:28:06 -07003730 if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003731 // Some drivers send empty MT sync packets without X / Y to indicate a pointer up.
3732 // Drop this finger.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003733 continue;
3734 }
3735
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003736 PointerData& outPointer = mCurrentTouch.pointers[outCount];
3737 outPointer.x = inPointer.absMTPositionX;
3738 outPointer.y = inPointer.absMTPositionY;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003739
Jeff Brown8d608662010-08-30 03:02:23 -07003740 if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) {
3741 if (inPointer.absMTPressure <= 0) {
Jeff Brownc3db8582010-10-20 15:33:38 -07003742 // Some devices send sync packets with X / Y but with a 0 pressure to indicate
3743 // a pointer going up. Drop this finger.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003744 continue;
3745 }
Jeff Brown8d608662010-08-30 03:02:23 -07003746 outPointer.pressure = inPointer.absMTPressure;
3747 } else {
3748 // Default pressure to 0 if absent.
3749 outPointer.pressure = 0;
3750 }
3751
3752 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) {
3753 if (inPointer.absMTTouchMajor <= 0) {
3754 // Some devices send sync packets with X / Y but with a 0 touch major to indicate
3755 // a pointer going up. Drop this finger.
3756 continue;
3757 }
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003758 outPointer.touchMajor = inPointer.absMTTouchMajor;
3759 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003760 // Default touch area to 0 if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003761 outPointer.touchMajor = 0;
3762 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003763
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003764 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) {
3765 outPointer.touchMinor = inPointer.absMTTouchMinor;
3766 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003767 // Assume touch area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003768 outPointer.touchMinor = outPointer.touchMajor;
3769 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003770
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003771 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) {
3772 outPointer.toolMajor = inPointer.absMTWidthMajor;
3773 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003774 // Default tool area to 0 if absent.
3775 outPointer.toolMajor = 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003776 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003777
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003778 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) {
3779 outPointer.toolMinor = inPointer.absMTWidthMinor;
3780 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003781 // Assume tool area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003782 outPointer.toolMinor = outPointer.toolMajor;
3783 }
3784
3785 if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) {
3786 outPointer.orientation = inPointer.absMTOrientation;
3787 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003788 // Default orientation to vertical if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003789 outPointer.orientation = 0;
3790 }
3791
Jeff Brown8d608662010-08-30 03:02:23 -07003792 // Assign pointer id using tracking id if available.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003793 if (havePointerIds) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003794 if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) {
3795 uint32_t id = uint32_t(inPointer.absMTTrackingId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003796
Jeff Brown6d0fec22010-07-23 21:28:06 -07003797 if (id > MAX_POINTER_ID) {
3798#if DEBUG_POINTERS
3799 LOGD("Pointers: Ignoring driver provided pointer id %d because "
Jeff Brown01ce2e92010-09-26 22:20:12 -07003800 "it is larger than max supported id %d",
Jeff Brown6d0fec22010-07-23 21:28:06 -07003801 id, MAX_POINTER_ID);
3802#endif
3803 havePointerIds = false;
3804 }
3805 else {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003806 outPointer.id = id;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003807 mCurrentTouch.idToIndex[id] = outCount;
3808 mCurrentTouch.idBits.markBit(id);
3809 }
3810 } else {
3811 havePointerIds = false;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003812 }
3813 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003814
Jeff Brown6d0fec22010-07-23 21:28:06 -07003815 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003816 }
3817
Jeff Brown6d0fec22010-07-23 21:28:06 -07003818 mCurrentTouch.pointerCount = outCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003819
Jeff Brown6d0fec22010-07-23 21:28:06 -07003820 syncTouch(when, havePointerIds);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003821
3822 mAccumulator.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003823}
3824
Jeff Brown8d608662010-08-30 03:02:23 -07003825void MultiTouchInputMapper::configureRawAxes() {
3826 TouchInputMapper::configureRawAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003827
Jeff Brown8d608662010-08-30 03:02:23 -07003828 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, & mRawAxes.x);
3829 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, & mRawAxes.y);
3830 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, & mRawAxes.touchMajor);
3831 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, & mRawAxes.touchMinor);
3832 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, & mRawAxes.toolMajor);
3833 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, & mRawAxes.toolMinor);
3834 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, & mRawAxes.orientation);
3835 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, & mRawAxes.pressure);
Jeff Brown9c3cda02010-06-15 01:31:58 -07003836}
3837
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003838
Jeff Browncb1404e2011-01-15 18:14:15 -08003839// --- JoystickInputMapper ---
3840
3841JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
3842 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08003843}
3844
3845JoystickInputMapper::~JoystickInputMapper() {
3846}
3847
3848uint32_t JoystickInputMapper::getSources() {
3849 return AINPUT_SOURCE_JOYSTICK;
3850}
3851
3852void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
3853 InputMapper::populateDeviceInfo(info);
3854
Jeff Brown6f2fba42011-02-19 01:08:02 -08003855 for (size_t i = 0; i < mAxes.size(); i++) {
3856 const Axis& axis = mAxes.valueAt(i);
3857 info->addMotionRange(axis.axis, axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Browncb1404e2011-01-15 18:14:15 -08003858 }
3859}
3860
3861void JoystickInputMapper::dump(String8& dump) {
3862 dump.append(INDENT2 "Joystick Input Mapper:\n");
3863
Jeff Brown6f2fba42011-02-19 01:08:02 -08003864 dump.append(INDENT3 "Axes:\n");
3865 size_t numAxes = mAxes.size();
3866 for (size_t i = 0; i < numAxes; i++) {
3867 const Axis& axis = mAxes.valueAt(i);
3868 const char* label = getAxisLabel(axis.axis);
3869 char name[32];
3870 if (label) {
3871 strncpy(name, label, sizeof(name));
3872 name[sizeof(name) - 1] = '\0';
3873 } else {
3874 snprintf(name, sizeof(name), "%d", axis.axis);
3875 }
Jeff Browncb1404e2011-01-15 18:14:15 -08003876 dump.appendFormat(INDENT4 "%s: min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, "
Jeff Brown6f2fba42011-02-19 01:08:02 -08003877 "scale=%0.3f, offset=%0.3f\n",
Jeff Browncb1404e2011-01-15 18:14:15 -08003878 name, axis.min, axis.max, axis.flat, axis.fuzz,
Jeff Brown6f2fba42011-02-19 01:08:02 -08003879 axis.scale, axis.offset);
3880 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, rawFlat=%d, rawFuzz=%d\n",
3881 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
3882 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz);
Jeff Browncb1404e2011-01-15 18:14:15 -08003883 }
3884}
3885
3886void JoystickInputMapper::configure() {
3887 InputMapper::configure();
3888
Jeff Brown6f2fba42011-02-19 01:08:02 -08003889 // Collect all axes.
3890 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
3891 RawAbsoluteAxisInfo rawAxisInfo;
3892 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), abs, &rawAxisInfo);
3893 if (rawAxisInfo.valid) {
3894 int32_t axisId;
3895 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisId);
3896 if (!explicitlyMapped) {
3897 // Axis is not explicitly mapped, will choose a generic axis later.
3898 axisId = -1;
3899 }
Jeff Browncb1404e2011-01-15 18:14:15 -08003900
Jeff Brown6f2fba42011-02-19 01:08:02 -08003901 Axis axis;
3902 if (isCenteredAxis(axisId)) {
3903 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
3904 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
3905 axis.initialize(rawAxisInfo, axisId, explicitlyMapped,
3906 scale, offset, -1.0f, 1.0f,
3907 rawAxisInfo.flat * scale, rawAxisInfo.fuzz * scale);
3908 } else {
3909 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
3910 axis.initialize(rawAxisInfo, axisId, explicitlyMapped,
3911 scale, 0.0f, 0.0f, 1.0f,
3912 rawAxisInfo.flat * scale, rawAxisInfo.fuzz * scale);
3913 }
3914
3915 // To eliminate noise while the joystick is at rest, filter out small variations
3916 // in axis values up front.
3917 axis.filter = axis.flat * 0.25f;
3918
3919 mAxes.add(abs, axis);
3920 }
3921 }
3922
3923 // If there are too many axes, start dropping them.
3924 // Prefer to keep explicitly mapped axes.
3925 if (mAxes.size() > PointerCoords::MAX_AXES) {
3926 LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
3927 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
3928 pruneAxes(true);
3929 pruneAxes(false);
3930 }
3931
3932 // Assign generic axis ids to remaining axes.
3933 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
3934 size_t numAxes = mAxes.size();
3935 for (size_t i = 0; i < numAxes; i++) {
3936 Axis& axis = mAxes.editValueAt(i);
3937 if (axis.axis < 0) {
3938 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
3939 && haveAxis(nextGenericAxisId)) {
3940 nextGenericAxisId += 1;
3941 }
3942
3943 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
3944 axis.axis = nextGenericAxisId;
3945 nextGenericAxisId += 1;
3946 } else {
3947 LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
3948 "have already been assigned to other axes.",
3949 getDeviceName().string(), mAxes.keyAt(i));
3950 mAxes.removeItemsAt(i--);
3951 numAxes -= 1;
3952 }
3953 }
3954 }
Jeff Browncb1404e2011-01-15 18:14:15 -08003955}
3956
Jeff Brown6f2fba42011-02-19 01:08:02 -08003957bool JoystickInputMapper::haveAxis(int32_t axis) {
3958 size_t numAxes = mAxes.size();
3959 for (size_t i = 0; i < numAxes; i++) {
3960 if (mAxes.valueAt(i).axis == axis) {
3961 return true;
3962 }
3963 }
3964 return false;
3965}
Jeff Browncb1404e2011-01-15 18:14:15 -08003966
Jeff Brown6f2fba42011-02-19 01:08:02 -08003967void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
3968 size_t i = mAxes.size();
3969 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
3970 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
3971 continue;
3972 }
3973 LOGI("Discarding joystick '%s' axis %d because there are too many axes.",
3974 getDeviceName().string(), mAxes.keyAt(i));
3975 mAxes.removeItemsAt(i);
3976 }
3977}
3978
3979bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
3980 switch (axis) {
3981 case AMOTION_EVENT_AXIS_X:
3982 case AMOTION_EVENT_AXIS_Y:
3983 case AMOTION_EVENT_AXIS_Z:
3984 case AMOTION_EVENT_AXIS_RX:
3985 case AMOTION_EVENT_AXIS_RY:
3986 case AMOTION_EVENT_AXIS_RZ:
3987 case AMOTION_EVENT_AXIS_HAT_X:
3988 case AMOTION_EVENT_AXIS_HAT_Y:
3989 case AMOTION_EVENT_AXIS_ORIENTATION:
3990 return true;
3991 default:
3992 return false;
3993 }
Jeff Browncb1404e2011-01-15 18:14:15 -08003994}
3995
3996void JoystickInputMapper::reset() {
3997 // Recenter all axes.
3998 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Browncb1404e2011-01-15 18:14:15 -08003999
Jeff Brown6f2fba42011-02-19 01:08:02 -08004000 size_t numAxes = mAxes.size();
4001 for (size_t i = 0; i < numAxes; i++) {
4002 Axis& axis = mAxes.editValueAt(i);
4003 axis.newValue = 0;
4004 }
4005
4006 sync(when, true /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08004007
4008 InputMapper::reset();
4009}
4010
4011void JoystickInputMapper::process(const RawEvent* rawEvent) {
4012 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08004013 case EV_ABS: {
4014 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
4015 if (index >= 0) {
4016 Axis& axis = mAxes.editValueAt(index);
4017 float newValue = rawEvent->value * axis.scale + axis.offset;
4018 if (newValue != axis.newValue) {
4019 axis.newValue = newValue;
4020 }
Jeff Browncb1404e2011-01-15 18:14:15 -08004021 }
4022 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08004023 }
Jeff Browncb1404e2011-01-15 18:14:15 -08004024
4025 case EV_SYN:
4026 switch (rawEvent->scanCode) {
4027 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08004028 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08004029 break;
4030 }
4031 break;
4032 }
4033}
4034
Jeff Brown6f2fba42011-02-19 01:08:02 -08004035void JoystickInputMapper::sync(nsecs_t when, bool force) {
4036 if (!force && !haveAxesChangedSignificantly()) {
4037 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08004038 }
4039
4040 int32_t metaState = mContext->getGlobalMetaState();
4041
Jeff Brown6f2fba42011-02-19 01:08:02 -08004042 PointerCoords pointerCoords;
4043 pointerCoords.clear();
4044
4045 size_t numAxes = mAxes.size();
4046 for (size_t i = 0; i < numAxes; i++) {
4047 Axis& axis = mAxes.editValueAt(i);
4048 pointerCoords.setAxisValue(axis.axis, axis.newValue);
4049 axis.oldValue = axis.newValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08004050 }
4051
Jeff Brown56194eb2011-03-02 19:23:13 -08004052 // Moving a joystick axis should not wake the devide because joysticks can
4053 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
4054 // button will likely wake the device.
4055 // TODO: Use the input device configuration to control this behavior more finely.
4056 uint32_t policyFlags = 0;
4057
Jeff Brown6f2fba42011-02-19 01:08:02 -08004058 int32_t pointerId = 0;
Jeff Brown56194eb2011-03-02 19:23:13 -08004059 getDispatcher()->notifyMotion(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brown6f2fba42011-02-19 01:08:02 -08004060 AMOTION_EVENT_ACTION_MOVE, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE,
4061 1, &pointerId, &pointerCoords, 0, 0, 0);
Jeff Browncb1404e2011-01-15 18:14:15 -08004062}
4063
Jeff Brown6f2fba42011-02-19 01:08:02 -08004064bool JoystickInputMapper::haveAxesChangedSignificantly() {
4065 size_t numAxes = mAxes.size();
4066 for (size_t i = 0; i < numAxes; i++) {
4067 const Axis& axis = mAxes.valueAt(i);
4068 if (axis.newValue != axis.oldValue
4069 && fabs(axis.newValue - axis.oldValue) > axis.filter) {
4070 return true;
4071 }
Jeff Browncb1404e2011-01-15 18:14:15 -08004072 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08004073 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08004074}
4075
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004076} // namespace android