blob: 084264b4d15a1bbaa269e78f5ab7c0dad30ed0ec [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 Brownd41cff22011-03-03 02:09:54 -080091static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
92 const int32_t map[][4], size_t mapSize) {
93 if (orientation != DISPLAY_ORIENTATION_0) {
94 for (size_t i = 0; i < mapSize; i++) {
95 if (value == map[i][0]) {
96 return map[i][orientation];
97 }
98 }
99 }
100 return value;
101}
102
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700103static const int32_t keyCodeRotationMap[][4] = {
104 // key codes enumerated counter-clockwise with the original (unrotated) key first
105 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd035822010-06-30 16:10:35 -0700106 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
107 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
108 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
109 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700110};
Jeff Brownd41cff22011-03-03 02:09:54 -0800111static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700112 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
113
114int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brownd41cff22011-03-03 02:09:54 -0800115 return rotateValueUsingRotationMap(keyCode, orientation,
116 keyCodeRotationMap, keyCodeRotationMapSize);
117}
118
119static const int32_t edgeFlagRotationMap[][4] = {
120 // edge flags enumerated counter-clockwise with the original (unrotated) edge flag first
121 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
122 { AMOTION_EVENT_EDGE_FLAG_BOTTOM, AMOTION_EVENT_EDGE_FLAG_RIGHT,
123 AMOTION_EVENT_EDGE_FLAG_TOP, AMOTION_EVENT_EDGE_FLAG_LEFT },
124 { AMOTION_EVENT_EDGE_FLAG_RIGHT, AMOTION_EVENT_EDGE_FLAG_TOP,
125 AMOTION_EVENT_EDGE_FLAG_LEFT, AMOTION_EVENT_EDGE_FLAG_BOTTOM },
126 { AMOTION_EVENT_EDGE_FLAG_TOP, AMOTION_EVENT_EDGE_FLAG_LEFT,
127 AMOTION_EVENT_EDGE_FLAG_BOTTOM, AMOTION_EVENT_EDGE_FLAG_RIGHT },
128 { AMOTION_EVENT_EDGE_FLAG_LEFT, AMOTION_EVENT_EDGE_FLAG_BOTTOM,
129 AMOTION_EVENT_EDGE_FLAG_RIGHT, AMOTION_EVENT_EDGE_FLAG_TOP },
130};
131static const size_t edgeFlagRotationMapSize =
132 sizeof(edgeFlagRotationMap) / sizeof(edgeFlagRotationMap[0]);
133
134static int32_t rotateEdgeFlag(int32_t edgeFlag, int32_t orientation) {
135 return rotateValueUsingRotationMap(edgeFlag, orientation,
136 edgeFlagRotationMap, edgeFlagRotationMapSize);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700137}
138
Jeff Brown6d0fec22010-07-23 21:28:06 -0700139static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
140 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
141}
142
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700143
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700144// --- InputReader ---
145
146InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700147 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700148 const sp<InputDispatcherInterface>& dispatcher) :
Jeff Brown6d0fec22010-07-23 21:28:06 -0700149 mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher),
Jeff Brownfe508922011-01-18 15:10:10 -0800150 mGlobalMetaState(0), mDisableVirtualKeysTimeout(-1) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700151 configureExcludedDevices();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700152 updateGlobalMetaState();
153 updateInputConfiguration();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700154}
155
156InputReader::~InputReader() {
157 for (size_t i = 0; i < mDevices.size(); i++) {
158 delete mDevices.valueAt(i);
159 }
160}
161
162void InputReader::loopOnce() {
163 RawEvent rawEvent;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700164 mEventHub->getEvent(& rawEvent);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700165
166#if DEBUG_RAW_EVENTS
Jeff Brown90655042010-12-02 13:50:46 -0800167 LOGD("Input event: device=%d type=0x%x scancode=%d keycode=%d value=%d",
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700168 rawEvent.deviceId, rawEvent.type, rawEvent.scanCode, rawEvent.keyCode,
169 rawEvent.value);
170#endif
171
172 process(& rawEvent);
173}
174
175void InputReader::process(const RawEvent* rawEvent) {
176 switch (rawEvent->type) {
177 case EventHubInterface::DEVICE_ADDED:
Jeff Brown7342bb92010-10-01 18:55:43 -0700178 addDevice(rawEvent->deviceId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700179 break;
180
181 case EventHubInterface::DEVICE_REMOVED:
Jeff Brown7342bb92010-10-01 18:55:43 -0700182 removeDevice(rawEvent->deviceId);
183 break;
184
185 case EventHubInterface::FINISHED_DEVICE_SCAN:
Jeff Brownc3db8582010-10-20 15:33:38 -0700186 handleConfigurationChanged(rawEvent->when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700187 break;
188
Jeff Brown6d0fec22010-07-23 21:28:06 -0700189 default:
190 consumeEvent(rawEvent);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700191 break;
192 }
193}
194
Jeff Brown7342bb92010-10-01 18:55:43 -0700195void InputReader::addDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700196 String8 name = mEventHub->getDeviceName(deviceId);
197 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
198
199 InputDevice* device = createDevice(deviceId, name, classes);
200 device->configure();
201
Jeff Brown8d608662010-08-30 03:02:23 -0700202 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800203 LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700204 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800205 LOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700206 device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700207 }
208
Jeff Brown6d0fec22010-07-23 21:28:06 -0700209 bool added = false;
210 { // acquire device registry writer lock
211 RWLock::AutoWLock _wl(mDeviceRegistryLock);
212
213 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
214 if (deviceIndex < 0) {
215 mDevices.add(deviceId, device);
216 added = true;
217 }
218 } // release device registry writer lock
219
220 if (! added) {
221 LOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
222 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700223 return;
224 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700225}
226
Jeff Brown7342bb92010-10-01 18:55:43 -0700227void InputReader::removeDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700228 bool removed = false;
229 InputDevice* device = NULL;
230 { // acquire device registry writer lock
231 RWLock::AutoWLock _wl(mDeviceRegistryLock);
232
233 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
234 if (deviceIndex >= 0) {
235 device = mDevices.valueAt(deviceIndex);
236 mDevices.removeItemsAt(deviceIndex, 1);
237 removed = true;
238 }
239 } // release device registry writer lock
240
241 if (! removed) {
242 LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700243 return;
244 }
245
Jeff Brown6d0fec22010-07-23 21:28:06 -0700246 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800247 LOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700248 device->getId(), device->getName().string());
249 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800250 LOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700251 device->getId(), device->getName().string(), device->getSources());
252 }
253
Jeff Brown8d608662010-08-30 03:02:23 -0700254 device->reset();
255
Jeff Brown6d0fec22010-07-23 21:28:06 -0700256 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700257}
258
Jeff Brown6d0fec22010-07-23 21:28:06 -0700259InputDevice* InputReader::createDevice(int32_t deviceId, const String8& name, uint32_t classes) {
260 InputDevice* device = new InputDevice(this, deviceId, name);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700261
Jeff Brown56194eb2011-03-02 19:23:13 -0800262 // External devices.
263 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
264 device->setExternal(true);
265 }
266
Jeff Brown6d0fec22010-07-23 21:28:06 -0700267 // Switch-like devices.
268 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
269 device->addMapper(new SwitchInputMapper(device));
270 }
271
272 // Keyboard-like devices.
273 uint32_t keyboardSources = 0;
274 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
275 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
276 keyboardSources |= AINPUT_SOURCE_KEYBOARD;
277 }
278 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
279 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
280 }
281 if (classes & INPUT_DEVICE_CLASS_DPAD) {
282 keyboardSources |= AINPUT_SOURCE_DPAD;
283 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800284 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
285 keyboardSources |= AINPUT_SOURCE_GAMEPAD;
286 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700287
288 if (keyboardSources != 0) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800289 device->addMapper(new KeyboardInputMapper(device, keyboardSources, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700290 }
291
Jeff Brown83c09682010-12-23 17:50:18 -0800292 // Cursor-like devices.
293 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
294 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700295 }
296
Jeff Brown58a2da82011-01-25 16:02:22 -0800297 // Touchscreens and touchpad devices.
298 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800299 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800300 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800301 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700302 }
303
Jeff Browncb1404e2011-01-15 18:14:15 -0800304 // Joystick-like devices.
305 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
306 device->addMapper(new JoystickInputMapper(device));
307 }
308
Jeff Brown6d0fec22010-07-23 21:28:06 -0700309 return device;
310}
311
312void InputReader::consumeEvent(const RawEvent* rawEvent) {
313 int32_t deviceId = rawEvent->deviceId;
314
315 { // acquire device registry reader lock
316 RWLock::AutoRLock _rl(mDeviceRegistryLock);
317
318 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
319 if (deviceIndex < 0) {
320 LOGW("Discarding event for unknown deviceId %d.", deviceId);
321 return;
322 }
323
324 InputDevice* device = mDevices.valueAt(deviceIndex);
325 if (device->isIgnored()) {
326 //LOGD("Discarding event for ignored deviceId %d.", deviceId);
327 return;
328 }
329
330 device->process(rawEvent);
331 } // release device registry reader lock
332}
333
Jeff Brownc3db8582010-10-20 15:33:38 -0700334void InputReader::handleConfigurationChanged(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700335 // Reset global meta state because it depends on the list of all configured devices.
336 updateGlobalMetaState();
337
338 // Update input configuration.
339 updateInputConfiguration();
340
341 // Enqueue configuration changed.
342 mDispatcher->notifyConfigurationChanged(when);
343}
344
345void InputReader::configureExcludedDevices() {
346 Vector<String8> excludedDeviceNames;
347 mPolicy->getExcludedDeviceNames(excludedDeviceNames);
348
349 for (size_t i = 0; i < excludedDeviceNames.size(); i++) {
350 mEventHub->addExcludedDevice(excludedDeviceNames[i]);
351 }
352}
353
354void InputReader::updateGlobalMetaState() {
355 { // acquire state lock
356 AutoMutex _l(mStateLock);
357
358 mGlobalMetaState = 0;
359
360 { // acquire device registry reader lock
361 RWLock::AutoRLock _rl(mDeviceRegistryLock);
362
363 for (size_t i = 0; i < mDevices.size(); i++) {
364 InputDevice* device = mDevices.valueAt(i);
365 mGlobalMetaState |= device->getMetaState();
366 }
367 } // release device registry reader lock
368 } // release state lock
369}
370
371int32_t InputReader::getGlobalMetaState() {
372 { // acquire state lock
373 AutoMutex _l(mStateLock);
374
375 return mGlobalMetaState;
376 } // release state lock
377}
378
379void InputReader::updateInputConfiguration() {
380 { // acquire state lock
381 AutoMutex _l(mStateLock);
382
383 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
384 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
385 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
386 { // acquire device registry reader lock
387 RWLock::AutoRLock _rl(mDeviceRegistryLock);
388
389 InputDeviceInfo deviceInfo;
390 for (size_t i = 0; i < mDevices.size(); i++) {
391 InputDevice* device = mDevices.valueAt(i);
392 device->getDeviceInfo(& deviceInfo);
393 uint32_t sources = deviceInfo.getSources();
394
395 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
396 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
397 }
398 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
399 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
400 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
401 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
402 }
403 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
404 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700405 }
406 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700407 } // release device registry reader lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700408
Jeff Brown6d0fec22010-07-23 21:28:06 -0700409 mInputConfiguration.touchScreen = touchScreenConfig;
410 mInputConfiguration.keyboard = keyboardConfig;
411 mInputConfiguration.navigation = navigationConfig;
412 } // release state lock
413}
414
Jeff Brownfe508922011-01-18 15:10:10 -0800415void InputReader::disableVirtualKeysUntil(nsecs_t time) {
416 mDisableVirtualKeysTimeout = time;
417}
418
419bool InputReader::shouldDropVirtualKey(nsecs_t now,
420 InputDevice* device, int32_t keyCode, int32_t scanCode) {
421 if (now < mDisableVirtualKeysTimeout) {
422 LOGI("Dropping virtual key from device %s because virtual keys are "
423 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
424 device->getName().string(),
425 (mDisableVirtualKeysTimeout - now) * 0.000001,
426 keyCode, scanCode);
427 return true;
428 } else {
429 return false;
430 }
431}
432
Jeff Brown05dc66a2011-03-02 14:41:58 -0800433void InputReader::fadePointer() {
434 { // acquire device registry reader lock
435 RWLock::AutoRLock _rl(mDeviceRegistryLock);
436
437 for (size_t i = 0; i < mDevices.size(); i++) {
438 InputDevice* device = mDevices.valueAt(i);
439 device->fadePointer();
440 }
441 } // release device registry reader lock
442}
443
Jeff Brown6d0fec22010-07-23 21:28:06 -0700444void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
445 { // acquire state lock
446 AutoMutex _l(mStateLock);
447
448 *outConfiguration = mInputConfiguration;
449 } // release state lock
450}
451
452status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
453 { // acquire device registry reader lock
454 RWLock::AutoRLock _rl(mDeviceRegistryLock);
455
456 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
457 if (deviceIndex < 0) {
458 return NAME_NOT_FOUND;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700459 }
460
Jeff Brown6d0fec22010-07-23 21:28:06 -0700461 InputDevice* device = mDevices.valueAt(deviceIndex);
462 if (device->isIgnored()) {
463 return NAME_NOT_FOUND;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700464 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700465
466 device->getDeviceInfo(outDeviceInfo);
467 return OK;
468 } // release device registy reader lock
469}
470
471void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
472 outDeviceIds.clear();
473
474 { // acquire device registry reader lock
475 RWLock::AutoRLock _rl(mDeviceRegistryLock);
476
477 size_t numDevices = mDevices.size();
478 for (size_t i = 0; i < numDevices; i++) {
479 InputDevice* device = mDevices.valueAt(i);
480 if (! device->isIgnored()) {
481 outDeviceIds.add(device->getId());
482 }
483 }
484 } // release device registy reader lock
485}
486
487int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
488 int32_t keyCode) {
489 return getState(deviceId, sourceMask, keyCode, & InputDevice::getKeyCodeState);
490}
491
492int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
493 int32_t scanCode) {
494 return getState(deviceId, sourceMask, scanCode, & InputDevice::getScanCodeState);
495}
496
497int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
498 return getState(deviceId, sourceMask, switchCode, & InputDevice::getSwitchState);
499}
500
501int32_t InputReader::getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
502 GetStateFunc getStateFunc) {
503 { // acquire device registry reader lock
504 RWLock::AutoRLock _rl(mDeviceRegistryLock);
505
506 int32_t result = AKEY_STATE_UNKNOWN;
507 if (deviceId >= 0) {
508 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
509 if (deviceIndex >= 0) {
510 InputDevice* device = mDevices.valueAt(deviceIndex);
511 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
512 result = (device->*getStateFunc)(sourceMask, code);
513 }
514 }
515 } else {
516 size_t numDevices = mDevices.size();
517 for (size_t i = 0; i < numDevices; i++) {
518 InputDevice* device = mDevices.valueAt(i);
519 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
520 result = (device->*getStateFunc)(sourceMask, code);
521 if (result >= AKEY_STATE_DOWN) {
522 return result;
523 }
524 }
525 }
526 }
527 return result;
528 } // release device registy reader lock
529}
530
531bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
532 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
533 memset(outFlags, 0, numCodes);
534 return markSupportedKeyCodes(deviceId, sourceMask, numCodes, keyCodes, outFlags);
535}
536
537bool InputReader::markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
538 const int32_t* keyCodes, uint8_t* outFlags) {
539 { // acquire device registry reader lock
540 RWLock::AutoRLock _rl(mDeviceRegistryLock);
541 bool result = false;
542 if (deviceId >= 0) {
543 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
544 if (deviceIndex >= 0) {
545 InputDevice* device = mDevices.valueAt(deviceIndex);
546 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
547 result = device->markSupportedKeyCodes(sourceMask,
548 numCodes, keyCodes, outFlags);
549 }
550 }
551 } else {
552 size_t numDevices = mDevices.size();
553 for (size_t i = 0; i < numDevices; i++) {
554 InputDevice* device = mDevices.valueAt(i);
555 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
556 result |= device->markSupportedKeyCodes(sourceMask,
557 numCodes, keyCodes, outFlags);
558 }
559 }
560 }
561 return result;
562 } // release device registy reader lock
563}
564
Jeff Brownb88102f2010-09-08 11:49:43 -0700565void InputReader::dump(String8& dump) {
Jeff Brownf2f487182010-10-01 17:46:21 -0700566 mEventHub->dump(dump);
567 dump.append("\n");
568
569 dump.append("Input Reader State:\n");
570
Jeff Brownef3d7e82010-09-30 14:33:04 -0700571 { // acquire device registry reader lock
572 RWLock::AutoRLock _rl(mDeviceRegistryLock);
Jeff Brownb88102f2010-09-08 11:49:43 -0700573
Jeff Brownef3d7e82010-09-30 14:33:04 -0700574 for (size_t i = 0; i < mDevices.size(); i++) {
575 mDevices.valueAt(i)->dump(dump);
Jeff Brownb88102f2010-09-08 11:49:43 -0700576 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700577 } // release device registy reader lock
Jeff Brownb88102f2010-09-08 11:49:43 -0700578}
579
Jeff Brown6d0fec22010-07-23 21:28:06 -0700580
581// --- InputReaderThread ---
582
583InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
584 Thread(/*canCallJava*/ true), mReader(reader) {
585}
586
587InputReaderThread::~InputReaderThread() {
588}
589
590bool InputReaderThread::threadLoop() {
591 mReader->loopOnce();
592 return true;
593}
594
595
596// --- InputDevice ---
597
598InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) :
Jeff Brown56194eb2011-03-02 19:23:13 -0800599 mContext(context), mId(id), mName(name), mSources(0), mIsExternal(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700600}
601
602InputDevice::~InputDevice() {
603 size_t numMappers = mMappers.size();
604 for (size_t i = 0; i < numMappers; i++) {
605 delete mMappers[i];
606 }
607 mMappers.clear();
608}
609
Jeff Brownef3d7e82010-09-30 14:33:04 -0700610void InputDevice::dump(String8& dump) {
611 InputDeviceInfo deviceInfo;
612 getDeviceInfo(& deviceInfo);
613
Jeff Brown90655042010-12-02 13:50:46 -0800614 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700615 deviceInfo.getName().string());
Jeff Brown56194eb2011-03-02 19:23:13 -0800616 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700617 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
618 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800619
620 const KeyedVector<int32_t, InputDeviceInfo::MotionRange> ranges = deviceInfo.getMotionRanges();
621 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700622 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800623 for (size_t i = 0; i < ranges.size(); i++) {
624 int32_t axis = ranges.keyAt(i);
625 const char* label = getAxisLabel(axis);
626 char name[32];
627 if (label) {
628 strncpy(name, label, sizeof(name));
629 name[sizeof(name) - 1] = '\0';
630 } else {
631 snprintf(name, sizeof(name), "%d", axis);
632 }
633 const InputDeviceInfo::MotionRange& range = ranges.valueAt(i);
634 dump.appendFormat(INDENT3 "%s: min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
635 name, range.min, range.max, range.flat, range.fuzz);
636 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700637 }
638
639 size_t numMappers = mMappers.size();
640 for (size_t i = 0; i < numMappers; i++) {
641 InputMapper* mapper = mMappers[i];
642 mapper->dump(dump);
643 }
644}
645
Jeff Brown6d0fec22010-07-23 21:28:06 -0700646void InputDevice::addMapper(InputMapper* mapper) {
647 mMappers.add(mapper);
648}
649
650void InputDevice::configure() {
Jeff Brown8d608662010-08-30 03:02:23 -0700651 if (! isIgnored()) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800652 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
Jeff Brown8d608662010-08-30 03:02:23 -0700653 }
654
Jeff Brown6d0fec22010-07-23 21:28:06 -0700655 mSources = 0;
656
657 size_t numMappers = mMappers.size();
658 for (size_t i = 0; i < numMappers; i++) {
659 InputMapper* mapper = mMappers[i];
660 mapper->configure();
661 mSources |= mapper->getSources();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700662 }
663}
664
Jeff Brown6d0fec22010-07-23 21:28:06 -0700665void InputDevice::reset() {
666 size_t numMappers = mMappers.size();
667 for (size_t i = 0; i < numMappers; i++) {
668 InputMapper* mapper = mMappers[i];
669 mapper->reset();
670 }
671}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700672
Jeff Brown6d0fec22010-07-23 21:28:06 -0700673void InputDevice::process(const RawEvent* rawEvent) {
674 size_t numMappers = mMappers.size();
675 for (size_t i = 0; i < numMappers; i++) {
676 InputMapper* mapper = mMappers[i];
677 mapper->process(rawEvent);
678 }
679}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700680
Jeff Brown6d0fec22010-07-23 21:28:06 -0700681void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
682 outDeviceInfo->initialize(mId, mName);
683
684 size_t numMappers = mMappers.size();
685 for (size_t i = 0; i < numMappers; i++) {
686 InputMapper* mapper = mMappers[i];
687 mapper->populateDeviceInfo(outDeviceInfo);
688 }
689}
690
691int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
692 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
693}
694
695int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
696 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
697}
698
699int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
700 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
701}
702
703int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
704 int32_t result = AKEY_STATE_UNKNOWN;
705 size_t numMappers = mMappers.size();
706 for (size_t i = 0; i < numMappers; i++) {
707 InputMapper* mapper = mMappers[i];
708 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
709 result = (mapper->*getStateFunc)(sourceMask, code);
710 if (result >= AKEY_STATE_DOWN) {
711 return result;
712 }
713 }
714 }
715 return result;
716}
717
718bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
719 const int32_t* keyCodes, uint8_t* outFlags) {
720 bool result = false;
721 size_t numMappers = mMappers.size();
722 for (size_t i = 0; i < numMappers; i++) {
723 InputMapper* mapper = mMappers[i];
724 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
725 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
726 }
727 }
728 return result;
729}
730
731int32_t InputDevice::getMetaState() {
732 int32_t result = 0;
733 size_t numMappers = mMappers.size();
734 for (size_t i = 0; i < numMappers; i++) {
735 InputMapper* mapper = mMappers[i];
736 result |= mapper->getMetaState();
737 }
738 return result;
739}
740
Jeff Brown05dc66a2011-03-02 14:41:58 -0800741void InputDevice::fadePointer() {
742 size_t numMappers = mMappers.size();
743 for (size_t i = 0; i < numMappers; i++) {
744 InputMapper* mapper = mMappers[i];
745 mapper->fadePointer();
746 }
747}
748
Jeff Brown6d0fec22010-07-23 21:28:06 -0700749
750// --- InputMapper ---
751
752InputMapper::InputMapper(InputDevice* device) :
753 mDevice(device), mContext(device->getContext()) {
754}
755
756InputMapper::~InputMapper() {
757}
758
759void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
760 info->addSource(getSources());
761}
762
Jeff Brownef3d7e82010-09-30 14:33:04 -0700763void InputMapper::dump(String8& dump) {
764}
765
Jeff Brown6d0fec22010-07-23 21:28:06 -0700766void InputMapper::configure() {
767}
768
769void InputMapper::reset() {
770}
771
772int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
773 return AKEY_STATE_UNKNOWN;
774}
775
776int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
777 return AKEY_STATE_UNKNOWN;
778}
779
780int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
781 return AKEY_STATE_UNKNOWN;
782}
783
784bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
785 const int32_t* keyCodes, uint8_t* outFlags) {
786 return false;
787}
788
789int32_t InputMapper::getMetaState() {
790 return 0;
791}
792
Jeff Brown05dc66a2011-03-02 14:41:58 -0800793void InputMapper::fadePointer() {
794}
795
Jeff Browncb1404e2011-01-15 18:14:15 -0800796void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
797 const RawAbsoluteAxisInfo& axis, const char* name) {
798 if (axis.valid) {
799 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d\n",
800 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz);
801 } else {
802 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
803 }
804}
805
Jeff Brown6d0fec22010-07-23 21:28:06 -0700806
807// --- SwitchInputMapper ---
808
809SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
810 InputMapper(device) {
811}
812
813SwitchInputMapper::~SwitchInputMapper() {
814}
815
816uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -0800817 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700818}
819
820void SwitchInputMapper::process(const RawEvent* rawEvent) {
821 switch (rawEvent->type) {
822 case EV_SW:
823 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
824 break;
825 }
826}
827
828void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownb6997262010-10-08 22:31:17 -0700829 getDispatcher()->notifySwitch(when, switchCode, switchValue, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700830}
831
832int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
833 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
834}
835
836
837// --- KeyboardInputMapper ---
838
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800839KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700840 uint32_t sources, int32_t keyboardType) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800841 InputMapper(device), mSources(sources),
Jeff Brown6d0fec22010-07-23 21:28:06 -0700842 mKeyboardType(keyboardType) {
Jeff Brown6328cdc2010-07-29 18:18:33 -0700843 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700844}
845
846KeyboardInputMapper::~KeyboardInputMapper() {
847}
848
Jeff Brown6328cdc2010-07-29 18:18:33 -0700849void KeyboardInputMapper::initializeLocked() {
850 mLocked.metaState = AMETA_NONE;
851 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700852}
853
854uint32_t KeyboardInputMapper::getSources() {
855 return mSources;
856}
857
858void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
859 InputMapper::populateDeviceInfo(info);
860
861 info->setKeyboardType(mKeyboardType);
862}
863
Jeff Brownef3d7e82010-09-30 14:33:04 -0700864void KeyboardInputMapper::dump(String8& dump) {
865 { // acquire lock
866 AutoMutex _l(mLock);
867 dump.append(INDENT2 "Keyboard Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800868 dumpParameters(dump);
Jeff Brownef3d7e82010-09-30 14:33:04 -0700869 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
870 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mLocked.keyDowns.size());
871 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mLocked.metaState);
872 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
873 } // release lock
874}
875
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800876
877void KeyboardInputMapper::configure() {
878 InputMapper::configure();
879
880 // Configure basic parameters.
881 configureParameters();
Jeff Brown49ed71d2010-12-06 17:13:33 -0800882
883 // Reset LEDs.
884 {
885 AutoMutex _l(mLock);
886 resetLedStateLocked();
887 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800888}
889
890void KeyboardInputMapper::configureParameters() {
891 mParameters.orientationAware = false;
892 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
893 mParameters.orientationAware);
894
895 mParameters.associatedDisplayId = mParameters.orientationAware ? 0 : -1;
896}
897
898void KeyboardInputMapper::dumpParameters(String8& dump) {
899 dump.append(INDENT3 "Parameters:\n");
900 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
901 mParameters.associatedDisplayId);
902 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
903 toString(mParameters.orientationAware));
904}
905
Jeff Brown6d0fec22010-07-23 21:28:06 -0700906void KeyboardInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -0700907 for (;;) {
908 int32_t keyCode, scanCode;
909 { // acquire lock
910 AutoMutex _l(mLock);
911
912 // Synthesize key up event on reset if keys are currently down.
913 if (mLocked.keyDowns.isEmpty()) {
914 initializeLocked();
Jeff Brown49ed71d2010-12-06 17:13:33 -0800915 resetLedStateLocked();
Jeff Brown6328cdc2010-07-29 18:18:33 -0700916 break; // done
917 }
918
919 const KeyDown& keyDown = mLocked.keyDowns.top();
920 keyCode = keyDown.keyCode;
921 scanCode = keyDown.scanCode;
922 } // release lock
923
Jeff Brown6d0fec22010-07-23 21:28:06 -0700924 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown6328cdc2010-07-29 18:18:33 -0700925 processKey(when, false, keyCode, scanCode, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700926 }
927
928 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700929 getContext()->updateGlobalMetaState();
930}
931
932void KeyboardInputMapper::process(const RawEvent* rawEvent) {
933 switch (rawEvent->type) {
934 case EV_KEY: {
935 int32_t scanCode = rawEvent->scanCode;
936 if (isKeyboardOrGamepadKey(scanCode)) {
937 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
938 rawEvent->flags);
939 }
940 break;
941 }
942 }
943}
944
945bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
946 return scanCode < BTN_MOUSE
947 || scanCode >= KEY_OK
Jeff Browne7b20292011-03-03 03:39:29 -0800948 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -0800949 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700950}
951
Jeff Brown6328cdc2010-07-29 18:18:33 -0700952void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
953 int32_t scanCode, uint32_t policyFlags) {
954 int32_t newMetaState;
955 nsecs_t downTime;
956 bool metaStateChanged = false;
957
958 { // acquire lock
959 AutoMutex _l(mLock);
960
961 if (down) {
962 // Rotate key codes according to orientation if needed.
963 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800964 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -0700965 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800966 if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
967 NULL, NULL, & orientation)) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800968 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700969 }
970
971 keyCode = rotateKeyCode(keyCode, orientation);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700972 }
973
Jeff Brown6328cdc2010-07-29 18:18:33 -0700974 // Add key down.
975 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
976 if (keyDownIndex >= 0) {
977 // key repeat, be sure to use same keycode as before in case of rotation
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800978 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -0700979 } else {
980 // key down
Jeff Brownfe508922011-01-18 15:10:10 -0800981 if ((policyFlags & POLICY_FLAG_VIRTUAL)
982 && mContext->shouldDropVirtualKey(when,
983 getDevice(), keyCode, scanCode)) {
984 return;
985 }
986
Jeff Brown6328cdc2010-07-29 18:18:33 -0700987 mLocked.keyDowns.push();
988 KeyDown& keyDown = mLocked.keyDowns.editTop();
989 keyDown.keyCode = keyCode;
990 keyDown.scanCode = scanCode;
991 }
992
993 mLocked.downTime = when;
994 } else {
995 // Remove key down.
996 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
997 if (keyDownIndex >= 0) {
998 // key up, be sure to use same keycode as before in case of rotation
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800999 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001000 mLocked.keyDowns.removeAt(size_t(keyDownIndex));
1001 } else {
1002 // key was not actually down
1003 LOGI("Dropping key up from device %s because the key was not down. "
1004 "keyCode=%d, scanCode=%d",
1005 getDeviceName().string(), keyCode, scanCode);
1006 return;
1007 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001008 }
1009
Jeff Brown6328cdc2010-07-29 18:18:33 -07001010 int32_t oldMetaState = mLocked.metaState;
1011 newMetaState = updateMetaState(keyCode, down, oldMetaState);
1012 if (oldMetaState != newMetaState) {
1013 mLocked.metaState = newMetaState;
1014 metaStateChanged = true;
Jeff Brown497a92c2010-09-12 17:55:08 -07001015 updateLedStateLocked(false);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001016 }
Jeff Brownfd035822010-06-30 16:10:35 -07001017
Jeff Brown6328cdc2010-07-29 18:18:33 -07001018 downTime = mLocked.downTime;
1019 } // release lock
1020
Jeff Brown56194eb2011-03-02 19:23:13 -08001021 // Key down on external an keyboard should wake the device.
1022 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
1023 // For internal keyboards, the key layout file should specify the policy flags for
1024 // each wake key individually.
1025 // TODO: Use the input device configuration to control this behavior more finely.
1026 if (down && getDevice()->isExternal()
1027 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1028 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1029 }
1030
Jeff Brown6328cdc2010-07-29 18:18:33 -07001031 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001032 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001033 }
1034
Jeff Brown05dc66a2011-03-02 14:41:58 -08001035 if (down && !isMetaKey(keyCode)) {
1036 getContext()->fadePointer();
1037 }
1038
Jeff Brown497a92c2010-09-12 17:55:08 -07001039 if (policyFlags & POLICY_FLAG_FUNCTION) {
1040 newMetaState |= AMETA_FUNCTION_ON;
1041 }
Jeff Brown83c09682010-12-23 17:50:18 -08001042 getDispatcher()->notifyKey(when, getDeviceId(), mSources, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001043 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1044 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001045}
1046
Jeff Brown6328cdc2010-07-29 18:18:33 -07001047ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) {
1048 size_t n = mLocked.keyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001049 for (size_t i = 0; i < n; i++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001050 if (mLocked.keyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001051 return i;
1052 }
1053 }
1054 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001055}
1056
Jeff Brown6d0fec22010-07-23 21:28:06 -07001057int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1058 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1059}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001060
Jeff Brown6d0fec22010-07-23 21:28:06 -07001061int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1062 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1063}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001064
Jeff Brown6d0fec22010-07-23 21:28:06 -07001065bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1066 const int32_t* keyCodes, uint8_t* outFlags) {
1067 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1068}
1069
1070int32_t KeyboardInputMapper::getMetaState() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001071 { // acquire lock
1072 AutoMutex _l(mLock);
1073 return mLocked.metaState;
1074 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001075}
1076
Jeff Brown49ed71d2010-12-06 17:13:33 -08001077void KeyboardInputMapper::resetLedStateLocked() {
1078 initializeLedStateLocked(mLocked.capsLockLedState, LED_CAPSL);
1079 initializeLedStateLocked(mLocked.numLockLedState, LED_NUML);
1080 initializeLedStateLocked(mLocked.scrollLockLedState, LED_SCROLLL);
1081
1082 updateLedStateLocked(true);
1083}
1084
1085void KeyboardInputMapper::initializeLedStateLocked(LockedState::LedState& ledState, int32_t led) {
1086 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
1087 ledState.on = false;
1088}
1089
Jeff Brown497a92c2010-09-12 17:55:08 -07001090void KeyboardInputMapper::updateLedStateLocked(bool reset) {
1091 updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001092 AMETA_CAPS_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001093 updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001094 AMETA_NUM_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001095 updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001096 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001097}
1098
1099void KeyboardInputMapper::updateLedStateForModifierLocked(LockedState::LedState& ledState,
1100 int32_t led, int32_t modifier, bool reset) {
1101 if (ledState.avail) {
1102 bool desiredState = (mLocked.metaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001103 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001104 getEventHub()->setLedState(getDeviceId(), led, desiredState);
1105 ledState.on = desiredState;
1106 }
1107 }
1108}
1109
Jeff Brown6d0fec22010-07-23 21:28:06 -07001110
Jeff Brown83c09682010-12-23 17:50:18 -08001111// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07001112
Jeff Brown83c09682010-12-23 17:50:18 -08001113CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001114 InputMapper(device) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001115 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001116}
1117
Jeff Brown83c09682010-12-23 17:50:18 -08001118CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001119}
1120
Jeff Brown83c09682010-12-23 17:50:18 -08001121uint32_t CursorInputMapper::getSources() {
1122 return mSources;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001123}
1124
Jeff Brown83c09682010-12-23 17:50:18 -08001125void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001126 InputMapper::populateDeviceInfo(info);
1127
Jeff Brown83c09682010-12-23 17:50:18 -08001128 if (mParameters.mode == Parameters::MODE_POINTER) {
1129 float minX, minY, maxX, maxY;
1130 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001131 info->addMotionRange(AMOTION_EVENT_AXIS_X, minX, maxX, 0.0f, 0.0f);
1132 info->addMotionRange(AMOTION_EVENT_AXIS_Y, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08001133 }
1134 } else {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001135 info->addMotionRange(AMOTION_EVENT_AXIS_X, -1.0f, 1.0f, 0.0f, mXScale);
1136 info->addMotionRange(AMOTION_EVENT_AXIS_Y, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08001137 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001138 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, 0.0f, 1.0f, 0.0f, 0.0f);
1139
1140 if (mHaveVWheel) {
1141 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, -1.0f, 1.0f, 0.0f, 0.0f);
1142 }
1143 if (mHaveHWheel) {
1144 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, -1.0f, 1.0f, 0.0f, 0.0f);
1145 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001146}
1147
Jeff Brown83c09682010-12-23 17:50:18 -08001148void CursorInputMapper::dump(String8& dump) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07001149 { // acquire lock
1150 AutoMutex _l(mLock);
Jeff Brown83c09682010-12-23 17:50:18 -08001151 dump.append(INDENT2 "Cursor Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001152 dumpParameters(dump);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001153 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
1154 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001155 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1156 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001157 dump.appendFormat(INDENT3 "HaveVWheel: %s\n", toString(mHaveVWheel));
1158 dump.appendFormat(INDENT3 "HaveHWheel: %s\n", toString(mHaveHWheel));
1159 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
1160 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001161 dump.appendFormat(INDENT3 "Down: %s\n", toString(mLocked.down));
1162 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1163 } // release lock
1164}
1165
Jeff Brown83c09682010-12-23 17:50:18 -08001166void CursorInputMapper::configure() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001167 InputMapper::configure();
1168
1169 // Configure basic parameters.
1170 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08001171
1172 // Configure device mode.
1173 switch (mParameters.mode) {
1174 case Parameters::MODE_POINTER:
1175 mSources = AINPUT_SOURCE_MOUSE;
1176 mXPrecision = 1.0f;
1177 mYPrecision = 1.0f;
1178 mXScale = 1.0f;
1179 mYScale = 1.0f;
1180 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
1181 break;
1182 case Parameters::MODE_NAVIGATION:
1183 mSources = AINPUT_SOURCE_TRACKBALL;
1184 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1185 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1186 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1187 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1188 break;
1189 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001190
1191 mVWheelScale = 1.0f;
1192 mHWheelScale = 1.0f;
Jeff Browncc0c1592011-02-19 05:07:28 -08001193
1194 mHaveVWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_WHEEL);
1195 mHaveHWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_HWHEEL);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001196}
1197
Jeff Brown83c09682010-12-23 17:50:18 -08001198void CursorInputMapper::configureParameters() {
1199 mParameters.mode = Parameters::MODE_POINTER;
1200 String8 cursorModeString;
1201 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
1202 if (cursorModeString == "navigation") {
1203 mParameters.mode = Parameters::MODE_NAVIGATION;
1204 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
1205 LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
1206 }
1207 }
1208
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001209 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08001210 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001211 mParameters.orientationAware);
1212
Jeff Brown83c09682010-12-23 17:50:18 -08001213 mParameters.associatedDisplayId = mParameters.mode == Parameters::MODE_POINTER
1214 || mParameters.orientationAware ? 0 : -1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001215}
1216
Jeff Brown83c09682010-12-23 17:50:18 -08001217void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001218 dump.append(INDENT3 "Parameters:\n");
1219 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1220 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08001221
1222 switch (mParameters.mode) {
1223 case Parameters::MODE_POINTER:
1224 dump.append(INDENT4 "Mode: pointer\n");
1225 break;
1226 case Parameters::MODE_NAVIGATION:
1227 dump.append(INDENT4 "Mode: navigation\n");
1228 break;
1229 default:
1230 assert(false);
1231 }
1232
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001233 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1234 toString(mParameters.orientationAware));
1235}
1236
Jeff Brown83c09682010-12-23 17:50:18 -08001237void CursorInputMapper::initializeLocked() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001238 mAccumulator.clear();
1239
Jeff Brown6328cdc2010-07-29 18:18:33 -07001240 mLocked.down = false;
1241 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001242}
1243
Jeff Brown83c09682010-12-23 17:50:18 -08001244void CursorInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001245 for (;;) {
1246 { // acquire lock
1247 AutoMutex _l(mLock);
1248
1249 if (! mLocked.down) {
1250 initializeLocked();
1251 break; // done
1252 }
1253 } // release lock
1254
Jeff Brown83c09682010-12-23 17:50:18 -08001255 // Synthesize button up event on reset.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001256 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001257 mAccumulator.fields = Accumulator::FIELD_BTN_MOUSE;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001258 mAccumulator.btnMouse = false;
1259 sync(when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001260 }
1261
Jeff Brown6d0fec22010-07-23 21:28:06 -07001262 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001263}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001264
Jeff Brown83c09682010-12-23 17:50:18 -08001265void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001266 switch (rawEvent->type) {
1267 case EV_KEY:
1268 switch (rawEvent->scanCode) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08001269 case BTN_LEFT:
1270 case BTN_RIGHT:
1271 case BTN_MIDDLE:
1272 case BTN_SIDE:
1273 case BTN_EXTRA:
1274 case BTN_FORWARD:
1275 case BTN_BACK:
1276 case BTN_TASK:
Jeff Brown6d0fec22010-07-23 21:28:06 -07001277 mAccumulator.fields |= Accumulator::FIELD_BTN_MOUSE;
1278 mAccumulator.btnMouse = rawEvent->value != 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001279 // Sync now since BTN_MOUSE is not necessarily followed by SYN_REPORT and
1280 // we need to ensure that we report the up/down promptly.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001281 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001282 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001283 }
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 case EV_REL:
1287 switch (rawEvent->scanCode) {
1288 case REL_X:
1289 mAccumulator.fields |= Accumulator::FIELD_REL_X;
1290 mAccumulator.relX = rawEvent->value;
1291 break;
1292 case REL_Y:
1293 mAccumulator.fields |= Accumulator::FIELD_REL_Y;
1294 mAccumulator.relY = rawEvent->value;
1295 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001296 case REL_WHEEL:
1297 mAccumulator.fields |= Accumulator::FIELD_REL_WHEEL;
1298 mAccumulator.relWheel = rawEvent->value;
1299 break;
1300 case REL_HWHEEL:
1301 mAccumulator.fields |= Accumulator::FIELD_REL_HWHEEL;
1302 mAccumulator.relHWheel = rawEvent->value;
1303 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001304 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001305 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001306
Jeff Brown6d0fec22010-07-23 21:28:06 -07001307 case EV_SYN:
1308 switch (rawEvent->scanCode) {
1309 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001310 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001311 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001312 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001313 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001314 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001315}
1316
Jeff Brown83c09682010-12-23 17:50:18 -08001317void CursorInputMapper::sync(nsecs_t when) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001318 uint32_t fields = mAccumulator.fields;
1319 if (fields == 0) {
1320 return; // no new state changes, so nothing to do
1321 }
1322
Jeff Brownd41cff22011-03-03 02:09:54 -08001323 int32_t motionEventAction;
1324 int32_t motionEventEdgeFlags;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001325 PointerCoords pointerCoords;
1326 nsecs_t downTime;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001327 float vscroll, hscroll;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001328 { // acquire lock
1329 AutoMutex _l(mLock);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001330
Jeff Brown6328cdc2010-07-29 18:18:33 -07001331 bool downChanged = fields & Accumulator::FIELD_BTN_MOUSE;
1332
1333 if (downChanged) {
1334 if (mAccumulator.btnMouse) {
Jeff Brown1c9d06e2011-01-14 17:24:16 -08001335 if (!mLocked.down) {
1336 mLocked.down = true;
1337 mLocked.downTime = when;
1338 } else {
1339 downChanged = false;
1340 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001341 } else {
Jeff Brown1c9d06e2011-01-14 17:24:16 -08001342 if (mLocked.down) {
1343 mLocked.down = false;
1344 } else {
1345 downChanged = false;
1346 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001347 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001348 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001349
Jeff Brown6328cdc2010-07-29 18:18:33 -07001350 downTime = mLocked.downTime;
Jeff Brown83c09682010-12-23 17:50:18 -08001351 float deltaX = fields & Accumulator::FIELD_REL_X ? mAccumulator.relX * mXScale : 0.0f;
1352 float deltaY = fields & Accumulator::FIELD_REL_Y ? mAccumulator.relY * mYScale : 0.0f;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001353
Jeff Brown6328cdc2010-07-29 18:18:33 -07001354 if (downChanged) {
1355 motionEventAction = mLocked.down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
Jeff Browncc0c1592011-02-19 05:07:28 -08001356 } else if (mLocked.down || mPointerController == NULL) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001357 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browncc0c1592011-02-19 05:07:28 -08001358 } else {
1359 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001360 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001361
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001362 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
Jeff Brown83c09682010-12-23 17:50:18 -08001363 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001364 // Rotate motion based on display orientation if needed.
1365 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1366 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001367 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1368 NULL, NULL, & orientation)) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001369 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001370 }
1371
1372 float temp;
1373 switch (orientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001374 case DISPLAY_ORIENTATION_90:
Jeff Brown83c09682010-12-23 17:50:18 -08001375 temp = deltaX;
1376 deltaX = deltaY;
1377 deltaY = -temp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001378 break;
1379
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001380 case DISPLAY_ORIENTATION_180:
Jeff Brown83c09682010-12-23 17:50:18 -08001381 deltaX = -deltaX;
1382 deltaY = -deltaY;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001383 break;
1384
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001385 case DISPLAY_ORIENTATION_270:
Jeff Brown83c09682010-12-23 17:50:18 -08001386 temp = deltaX;
1387 deltaX = -deltaY;
1388 deltaY = temp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001389 break;
1390 }
1391 }
Jeff Brown83c09682010-12-23 17:50:18 -08001392
Jeff Brown91c69ab2011-02-14 17:03:18 -08001393 pointerCoords.clear();
1394
Jeff Brownd41cff22011-03-03 02:09:54 -08001395 motionEventEdgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
1396
Jeff Brown83c09682010-12-23 17:50:18 -08001397 if (mPointerController != NULL) {
1398 mPointerController->move(deltaX, deltaY);
1399 if (downChanged) {
1400 mPointerController->setButtonState(mLocked.down ? POINTER_BUTTON_1 : 0);
1401 }
Jeff Brown91c69ab2011-02-14 17:03:18 -08001402 float x, y;
1403 mPointerController->getPosition(&x, &y);
Jeff Brownebbd5d12011-02-17 13:01:34 -08001404 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
1405 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brownd41cff22011-03-03 02:09:54 -08001406
1407 if (motionEventAction == AMOTION_EVENT_ACTION_DOWN) {
1408 float minX, minY, maxX, maxY;
1409 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
1410 if (x <= minX) {
1411 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_LEFT;
1412 } else if (x >= maxX) {
1413 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_RIGHT;
1414 }
1415 if (y <= minY) {
1416 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_TOP;
1417 } else if (y >= maxY) {
1418 motionEventEdgeFlags |= AMOTION_EVENT_EDGE_FLAG_BOTTOM;
1419 }
1420 }
1421 }
Jeff Brown83c09682010-12-23 17:50:18 -08001422 } else {
Jeff Brownebbd5d12011-02-17 13:01:34 -08001423 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
1424 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
Jeff Brown83c09682010-12-23 17:50:18 -08001425 }
1426
Jeff Brownebbd5d12011-02-17 13:01:34 -08001427 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, mLocked.down ? 1.0f : 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001428
1429 if (mHaveVWheel && (fields & Accumulator::FIELD_REL_WHEEL)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08001430 vscroll = mAccumulator.relWheel;
1431 } else {
1432 vscroll = 0;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001433 }
1434 if (mHaveHWheel && (fields & Accumulator::FIELD_REL_HWHEEL)) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08001435 hscroll = mAccumulator.relHWheel;
1436 } else {
1437 hscroll = 0;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001438 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08001439 if (hscroll != 0 || vscroll != 0) {
1440 mPointerController->unfade();
1441 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001442 } // release lock
1443
Jeff Brown56194eb2011-03-02 19:23:13 -08001444 // Moving an external trackball or mouse should wake the device.
1445 // We don't do this for internal cursor devices to prevent them from waking up
1446 // the device in your pocket.
1447 // TODO: Use the input device configuration to control this behavior more finely.
1448 uint32_t policyFlags = 0;
1449 if (getDevice()->isExternal()) {
1450 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1451 }
1452
Jeff Brown6d0fec22010-07-23 21:28:06 -07001453 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001454 int32_t pointerId = 0;
Jeff Brown56194eb2011-03-02 19:23:13 -08001455 getDispatcher()->notifyMotion(when, getDeviceId(), mSources, policyFlags,
Jeff Brownd41cff22011-03-03 02:09:54 -08001456 motionEventAction, 0, metaState, motionEventEdgeFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001457 1, &pointerId, &pointerCoords, mXPrecision, mYPrecision, downTime);
1458
1459 mAccumulator.clear();
Jeff Brown33bbfd22011-02-24 20:55:35 -08001460
1461 if (vscroll != 0 || hscroll != 0) {
1462 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
1463 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
1464
Jeff Brown56194eb2011-03-02 19:23:13 -08001465 getDispatcher()->notifyMotion(when, getDeviceId(), mSources, policyFlags,
Jeff Brown33bbfd22011-02-24 20:55:35 -08001466 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE,
1467 1, &pointerId, &pointerCoords, mXPrecision, mYPrecision, downTime);
1468 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001469}
1470
Jeff Brown83c09682010-12-23 17:50:18 -08001471int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07001472 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
1473 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1474 } else {
1475 return AKEY_STATE_UNKNOWN;
1476 }
1477}
1478
Jeff Brown05dc66a2011-03-02 14:41:58 -08001479void CursorInputMapper::fadePointer() {
1480 { // acquire lock
1481 AutoMutex _l(mLock);
1482 mPointerController->fade();
1483 } // release lock
1484}
1485
Jeff Brown6d0fec22010-07-23 21:28:06 -07001486
1487// --- TouchInputMapper ---
1488
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001489TouchInputMapper::TouchInputMapper(InputDevice* device) :
1490 InputMapper(device) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001491 mLocked.surfaceOrientation = -1;
1492 mLocked.surfaceWidth = -1;
1493 mLocked.surfaceHeight = -1;
1494
1495 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001496}
1497
1498TouchInputMapper::~TouchInputMapper() {
1499}
1500
1501uint32_t TouchInputMapper::getSources() {
Jeff Brown83c09682010-12-23 17:50:18 -08001502 return mSources;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001503}
1504
1505void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1506 InputMapper::populateDeviceInfo(info);
1507
Jeff Brown6328cdc2010-07-29 18:18:33 -07001508 { // acquire lock
1509 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001510
Jeff Brown6328cdc2010-07-29 18:18:33 -07001511 // Ensure surface information is up to date so that orientation changes are
1512 // noticed immediately.
1513 configureSurfaceLocked();
1514
Jeff Brown6f2fba42011-02-19 01:08:02 -08001515 info->addMotionRange(AMOTION_EVENT_AXIS_X, mLocked.orientedRanges.x);
1516 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mLocked.orientedRanges.y);
Jeff Brown8d608662010-08-30 03:02:23 -07001517
1518 if (mLocked.orientedRanges.havePressure) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001519 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE,
Jeff Brown8d608662010-08-30 03:02:23 -07001520 mLocked.orientedRanges.pressure);
1521 }
1522
1523 if (mLocked.orientedRanges.haveSize) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001524 info->addMotionRange(AMOTION_EVENT_AXIS_SIZE,
Jeff Brown8d608662010-08-30 03:02:23 -07001525 mLocked.orientedRanges.size);
1526 }
1527
Jeff Brownc6d282b2010-10-14 21:42:15 -07001528 if (mLocked.orientedRanges.haveTouchSize) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001529 info->addMotionRange(AMOTION_EVENT_AXIS_TOUCH_MAJOR,
Jeff Brown8d608662010-08-30 03:02:23 -07001530 mLocked.orientedRanges.touchMajor);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001531 info->addMotionRange(AMOTION_EVENT_AXIS_TOUCH_MINOR,
Jeff Brown8d608662010-08-30 03:02:23 -07001532 mLocked.orientedRanges.touchMinor);
1533 }
1534
Jeff Brownc6d282b2010-10-14 21:42:15 -07001535 if (mLocked.orientedRanges.haveToolSize) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001536 info->addMotionRange(AMOTION_EVENT_AXIS_TOOL_MAJOR,
Jeff Brown8d608662010-08-30 03:02:23 -07001537 mLocked.orientedRanges.toolMajor);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001538 info->addMotionRange(AMOTION_EVENT_AXIS_TOOL_MINOR,
Jeff Brown8d608662010-08-30 03:02:23 -07001539 mLocked.orientedRanges.toolMinor);
1540 }
1541
1542 if (mLocked.orientedRanges.haveOrientation) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08001543 info->addMotionRange(AMOTION_EVENT_AXIS_ORIENTATION,
Jeff Brown8d608662010-08-30 03:02:23 -07001544 mLocked.orientedRanges.orientation);
1545 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001546 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001547}
1548
Jeff Brownef3d7e82010-09-30 14:33:04 -07001549void TouchInputMapper::dump(String8& dump) {
1550 { // acquire lock
1551 AutoMutex _l(mLock);
1552 dump.append(INDENT2 "Touch Input Mapper:\n");
Jeff Brownef3d7e82010-09-30 14:33:04 -07001553 dumpParameters(dump);
1554 dumpVirtualKeysLocked(dump);
1555 dumpRawAxes(dump);
1556 dumpCalibration(dump);
1557 dumpSurfaceLocked(dump);
Jeff Brown511ee5f2010-10-18 13:32:20 -07001558 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
Jeff Brownc6d282b2010-10-14 21:42:15 -07001559 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mLocked.xScale);
1560 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mLocked.yScale);
1561 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mLocked.xPrecision);
1562 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mLocked.yPrecision);
1563 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mLocked.geometricScale);
1564 dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mLocked.toolSizeLinearScale);
1565 dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mLocked.toolSizeLinearBias);
1566 dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mLocked.toolSizeAreaScale);
1567 dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mLocked.toolSizeAreaBias);
1568 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mLocked.pressureScale);
1569 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mLocked.sizeScale);
1570 dump.appendFormat(INDENT4 "OrientationSCale: %0.3f\n", mLocked.orientationScale);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001571 } // release lock
1572}
1573
Jeff Brown6328cdc2010-07-29 18:18:33 -07001574void TouchInputMapper::initializeLocked() {
1575 mCurrentTouch.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001576 mLastTouch.clear();
1577 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001578
1579 for (uint32_t i = 0; i < MAX_POINTERS; i++) {
1580 mAveragingTouchFilter.historyStart[i] = 0;
1581 mAveragingTouchFilter.historyEnd[i] = 0;
1582 }
1583
1584 mJumpyTouchFilter.jumpyPointsDropped = 0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001585
1586 mLocked.currentVirtualKey.down = false;
Jeff Brown8d608662010-08-30 03:02:23 -07001587
1588 mLocked.orientedRanges.havePressure = false;
1589 mLocked.orientedRanges.haveSize = false;
Jeff Brownc6d282b2010-10-14 21:42:15 -07001590 mLocked.orientedRanges.haveTouchSize = false;
1591 mLocked.orientedRanges.haveToolSize = false;
Jeff Brown8d608662010-08-30 03:02:23 -07001592 mLocked.orientedRanges.haveOrientation = false;
1593}
1594
Jeff Brown6d0fec22010-07-23 21:28:06 -07001595void TouchInputMapper::configure() {
1596 InputMapper::configure();
1597
1598 // Configure basic parameters.
Jeff Brown8d608662010-08-30 03:02:23 -07001599 configureParameters();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001600
Jeff Brown83c09682010-12-23 17:50:18 -08001601 // Configure sources.
1602 switch (mParameters.deviceType) {
1603 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
1604 mSources = AINPUT_SOURCE_TOUCHSCREEN;
1605 break;
1606 case Parameters::DEVICE_TYPE_TOUCH_PAD:
1607 mSources = AINPUT_SOURCE_TOUCHPAD;
1608 break;
1609 default:
1610 assert(false);
1611 }
1612
Jeff Brown6d0fec22010-07-23 21:28:06 -07001613 // Configure absolute axis information.
Jeff Brown8d608662010-08-30 03:02:23 -07001614 configureRawAxes();
Jeff Brown8d608662010-08-30 03:02:23 -07001615
1616 // Prepare input device calibration.
1617 parseCalibration();
1618 resolveCalibration();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001619
Jeff Brown6328cdc2010-07-29 18:18:33 -07001620 { // acquire lock
1621 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001622
Jeff Brown8d608662010-08-30 03:02:23 -07001623 // Configure surface dimensions and orientation.
Jeff Brown6328cdc2010-07-29 18:18:33 -07001624 configureSurfaceLocked();
1625 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001626}
1627
Jeff Brown8d608662010-08-30 03:02:23 -07001628void TouchInputMapper::configureParameters() {
1629 mParameters.useBadTouchFilter = getPolicy()->filterTouchEvents();
1630 mParameters.useAveragingTouchFilter = getPolicy()->filterTouchEvents();
1631 mParameters.useJumpyTouchFilter = getPolicy()->filterJumpyTouchEvents();
Jeff Brownfe508922011-01-18 15:10:10 -08001632 mParameters.virtualKeyQuietTime = getPolicy()->getVirtualKeyQuietTime();
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001633
1634 String8 deviceTypeString;
Jeff Brown58a2da82011-01-25 16:02:22 -08001635 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001636 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
1637 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08001638 if (deviceTypeString == "touchScreen") {
1639 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
1640 } else if (deviceTypeString != "touchPad") {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001641 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
1642 }
1643 }
1644 bool isTouchScreen = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
1645
1646 mParameters.orientationAware = isTouchScreen;
1647 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
1648 mParameters.orientationAware);
1649
1650 mParameters.associatedDisplayId = mParameters.orientationAware || isTouchScreen ? 0 : -1;
Jeff Brown8d608662010-08-30 03:02:23 -07001651}
1652
Jeff Brownef3d7e82010-09-30 14:33:04 -07001653void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001654 dump.append(INDENT3 "Parameters:\n");
1655
1656 switch (mParameters.deviceType) {
1657 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
1658 dump.append(INDENT4 "DeviceType: touchScreen\n");
1659 break;
1660 case Parameters::DEVICE_TYPE_TOUCH_PAD:
1661 dump.append(INDENT4 "DeviceType: touchPad\n");
1662 break;
1663 default:
1664 assert(false);
1665 }
1666
1667 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1668 mParameters.associatedDisplayId);
1669 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1670 toString(mParameters.orientationAware));
1671
1672 dump.appendFormat(INDENT4 "UseBadTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07001673 toString(mParameters.useBadTouchFilter));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001674 dump.appendFormat(INDENT4 "UseAveragingTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07001675 toString(mParameters.useAveragingTouchFilter));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001676 dump.appendFormat(INDENT4 "UseJumpyTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07001677 toString(mParameters.useJumpyTouchFilter));
Jeff Brownb88102f2010-09-08 11:49:43 -07001678}
1679
Jeff Brown8d608662010-08-30 03:02:23 -07001680void TouchInputMapper::configureRawAxes() {
1681 mRawAxes.x.clear();
1682 mRawAxes.y.clear();
1683 mRawAxes.pressure.clear();
1684 mRawAxes.touchMajor.clear();
1685 mRawAxes.touchMinor.clear();
1686 mRawAxes.toolMajor.clear();
1687 mRawAxes.toolMinor.clear();
1688 mRawAxes.orientation.clear();
1689}
1690
Jeff Brownef3d7e82010-09-30 14:33:04 -07001691void TouchInputMapper::dumpRawAxes(String8& dump) {
1692 dump.append(INDENT3 "Raw Axes:\n");
Jeff Browncb1404e2011-01-15 18:14:15 -08001693 dumpRawAbsoluteAxisInfo(dump, mRawAxes.x, "X");
1694 dumpRawAbsoluteAxisInfo(dump, mRawAxes.y, "Y");
1695 dumpRawAbsoluteAxisInfo(dump, mRawAxes.pressure, "Pressure");
1696 dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor");
1697 dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor");
1698 dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor");
1699 dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor");
1700 dumpRawAbsoluteAxisInfo(dump, mRawAxes.orientation, "Orientation");
Jeff Brown6d0fec22010-07-23 21:28:06 -07001701}
1702
Jeff Brown6328cdc2010-07-29 18:18:33 -07001703bool TouchInputMapper::configureSurfaceLocked() {
Jeff Brownd41cff22011-03-03 02:09:54 -08001704 // Ensure we have valid X and Y axes.
1705 if (!mRawAxes.x.valid || !mRawAxes.y.valid) {
1706 LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
1707 "The device will be inoperable.", getDeviceName().string());
1708 return false;
1709 }
1710
Jeff Brown6d0fec22010-07-23 21:28:06 -07001711 // Update orientation and dimensions if needed.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001712 int32_t orientation = DISPLAY_ORIENTATION_0;
Jeff Brownd41cff22011-03-03 02:09:54 -08001713 int32_t width = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
1714 int32_t height = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001715
1716 if (mParameters.associatedDisplayId >= 0) {
1717 bool wantSize = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
1718 bool wantOrientation = mParameters.orientationAware;
1719
Jeff Brown6328cdc2010-07-29 18:18:33 -07001720 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001721 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1722 wantSize ? &width : NULL, wantSize ? &height : NULL,
1723 wantOrientation ? &orientation : NULL)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001724 return false;
1725 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001726 }
1727
Jeff Brown6328cdc2010-07-29 18:18:33 -07001728 bool orientationChanged = mLocked.surfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001729 if (orientationChanged) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001730 mLocked.surfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001731 }
1732
Jeff Brown6328cdc2010-07-29 18:18:33 -07001733 bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001734 if (sizeChanged) {
Jeff Brown90655042010-12-02 13:50:46 -08001735 LOGI("Device reconfigured: id=%d, name='%s', display size is now %dx%d",
Jeff Brownef3d7e82010-09-30 14:33:04 -07001736 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown8d608662010-08-30 03:02:23 -07001737
Jeff Brown6328cdc2010-07-29 18:18:33 -07001738 mLocked.surfaceWidth = width;
1739 mLocked.surfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001740
Jeff Brown8d608662010-08-30 03:02:23 -07001741 // Configure X and Y factors.
Jeff Brownd41cff22011-03-03 02:09:54 -08001742 mLocked.xScale = float(width) / (mRawAxes.x.maxValue - mRawAxes.x.minValue + 1);
1743 mLocked.yScale = float(height) / (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1);
1744 mLocked.xPrecision = 1.0f / mLocked.xScale;
1745 mLocked.yPrecision = 1.0f / mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001746
Jeff Brownd41cff22011-03-03 02:09:54 -08001747 configureVirtualKeysLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001748
Jeff Brown8d608662010-08-30 03:02:23 -07001749 // Scale factor for terms that are not oriented in a particular axis.
1750 // If the pixels are square then xScale == yScale otherwise we fake it
1751 // by choosing an average.
1752 mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001753
Jeff Brown8d608662010-08-30 03:02:23 -07001754 // Size of diagonal axis.
1755 float diagonalSize = pythag(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001756
Jeff Brown8d608662010-08-30 03:02:23 -07001757 // TouchMajor and TouchMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07001758 if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) {
1759 mLocked.orientedRanges.haveTouchSize = true;
Jeff Brown8d608662010-08-30 03:02:23 -07001760 mLocked.orientedRanges.touchMajor.min = 0;
1761 mLocked.orientedRanges.touchMajor.max = diagonalSize;
1762 mLocked.orientedRanges.touchMajor.flat = 0;
1763 mLocked.orientedRanges.touchMajor.fuzz = 0;
1764 mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor;
1765 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001766
Jeff Brown8d608662010-08-30 03:02:23 -07001767 // ToolMajor and ToolMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07001768 mLocked.toolSizeLinearScale = 0;
1769 mLocked.toolSizeLinearBias = 0;
1770 mLocked.toolSizeAreaScale = 0;
1771 mLocked.toolSizeAreaBias = 0;
1772 if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
1773 if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) {
1774 if (mCalibration.haveToolSizeLinearScale) {
1775 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
Jeff Brown8d608662010-08-30 03:02:23 -07001776 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07001777 mLocked.toolSizeLinearScale = float(min(width, height))
Jeff Brown8d608662010-08-30 03:02:23 -07001778 / mRawAxes.toolMajor.maxValue;
1779 }
1780
Jeff Brownc6d282b2010-10-14 21:42:15 -07001781 if (mCalibration.haveToolSizeLinearBias) {
1782 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
1783 }
1784 } else if (mCalibration.toolSizeCalibration ==
1785 Calibration::TOOL_SIZE_CALIBRATION_AREA) {
1786 if (mCalibration.haveToolSizeLinearScale) {
1787 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
1788 } else {
1789 mLocked.toolSizeLinearScale = min(width, height);
1790 }
1791
1792 if (mCalibration.haveToolSizeLinearBias) {
1793 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
1794 }
1795
1796 if (mCalibration.haveToolSizeAreaScale) {
1797 mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale;
1798 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1799 mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue;
1800 }
1801
1802 if (mCalibration.haveToolSizeAreaBias) {
1803 mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -07001804 }
1805 }
1806
Jeff Brownc6d282b2010-10-14 21:42:15 -07001807 mLocked.orientedRanges.haveToolSize = true;
Jeff Brown8d608662010-08-30 03:02:23 -07001808 mLocked.orientedRanges.toolMajor.min = 0;
1809 mLocked.orientedRanges.toolMajor.max = diagonalSize;
1810 mLocked.orientedRanges.toolMajor.flat = 0;
1811 mLocked.orientedRanges.toolMajor.fuzz = 0;
1812 mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor;
1813 }
1814
1815 // Pressure factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07001816 mLocked.pressureScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07001817 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
1818 RawAbsoluteAxisInfo rawPressureAxis;
1819 switch (mCalibration.pressureSource) {
1820 case Calibration::PRESSURE_SOURCE_PRESSURE:
1821 rawPressureAxis = mRawAxes.pressure;
1822 break;
1823 case Calibration::PRESSURE_SOURCE_TOUCH:
1824 rawPressureAxis = mRawAxes.touchMajor;
1825 break;
1826 default:
1827 rawPressureAxis.clear();
1828 }
1829
Jeff Brown8d608662010-08-30 03:02:23 -07001830 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
1831 || mCalibration.pressureCalibration
1832 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
1833 if (mCalibration.havePressureScale) {
1834 mLocked.pressureScale = mCalibration.pressureScale;
1835 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
1836 mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue;
1837 }
1838 }
1839
1840 mLocked.orientedRanges.havePressure = true;
1841 mLocked.orientedRanges.pressure.min = 0;
1842 mLocked.orientedRanges.pressure.max = 1.0;
1843 mLocked.orientedRanges.pressure.flat = 0;
1844 mLocked.orientedRanges.pressure.fuzz = 0;
1845 }
1846
1847 // Size factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07001848 mLocked.sizeScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07001849 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07001850 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
1851 if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
1852 mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue;
1853 }
1854 }
1855
1856 mLocked.orientedRanges.haveSize = true;
1857 mLocked.orientedRanges.size.min = 0;
1858 mLocked.orientedRanges.size.max = 1.0;
1859 mLocked.orientedRanges.size.flat = 0;
1860 mLocked.orientedRanges.size.fuzz = 0;
1861 }
1862
1863 // Orientation
Jeff Brownc6d282b2010-10-14 21:42:15 -07001864 mLocked.orientationScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07001865 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07001866 if (mCalibration.orientationCalibration
1867 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
1868 if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) {
1869 mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue;
1870 }
1871 }
1872
1873 mLocked.orientedRanges.orientation.min = - M_PI_2;
1874 mLocked.orientedRanges.orientation.max = M_PI_2;
1875 mLocked.orientedRanges.orientation.flat = 0;
1876 mLocked.orientedRanges.orientation.fuzz = 0;
1877 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001878 }
1879
1880 if (orientationChanged || sizeChanged) {
Jeff Brownd41cff22011-03-03 02:09:54 -08001881 // Compute oriented surface dimensions, precision, scales and ranges.
1882 // Note that the maximum value reported is an inclusive maximum value so it is one
1883 // unit less than the total width or height of surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07001884 switch (mLocked.surfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001885 case DISPLAY_ORIENTATION_90:
1886 case DISPLAY_ORIENTATION_270:
Jeff Brown6328cdc2010-07-29 18:18:33 -07001887 mLocked.orientedSurfaceWidth = mLocked.surfaceHeight;
1888 mLocked.orientedSurfaceHeight = mLocked.surfaceWidth;
Jeff Brownd41cff22011-03-03 02:09:54 -08001889
Jeff Brown6328cdc2010-07-29 18:18:33 -07001890 mLocked.orientedXPrecision = mLocked.yPrecision;
1891 mLocked.orientedYPrecision = mLocked.xPrecision;
Jeff Brownd41cff22011-03-03 02:09:54 -08001892
1893 mLocked.orientedRanges.x.min = 0;
1894 mLocked.orientedRanges.x.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue)
1895 * mLocked.yScale;
1896 mLocked.orientedRanges.x.flat = 0;
1897 mLocked.orientedRanges.x.fuzz = mLocked.yScale;
1898
1899 mLocked.orientedRanges.y.min = 0;
1900 mLocked.orientedRanges.y.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue)
1901 * mLocked.xScale;
1902 mLocked.orientedRanges.y.flat = 0;
1903 mLocked.orientedRanges.y.fuzz = mLocked.xScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001904 break;
Jeff Brownd41cff22011-03-03 02:09:54 -08001905
Jeff Brown6d0fec22010-07-23 21:28:06 -07001906 default:
Jeff Brown6328cdc2010-07-29 18:18:33 -07001907 mLocked.orientedSurfaceWidth = mLocked.surfaceWidth;
1908 mLocked.orientedSurfaceHeight = mLocked.surfaceHeight;
Jeff Brownd41cff22011-03-03 02:09:54 -08001909
Jeff Brown6328cdc2010-07-29 18:18:33 -07001910 mLocked.orientedXPrecision = mLocked.xPrecision;
1911 mLocked.orientedYPrecision = mLocked.yPrecision;
Jeff Brownd41cff22011-03-03 02:09:54 -08001912
1913 mLocked.orientedRanges.x.min = 0;
1914 mLocked.orientedRanges.x.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue)
1915 * mLocked.xScale;
1916 mLocked.orientedRanges.x.flat = 0;
1917 mLocked.orientedRanges.x.fuzz = mLocked.xScale;
1918
1919 mLocked.orientedRanges.y.min = 0;
1920 mLocked.orientedRanges.y.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue)
1921 * mLocked.yScale;
1922 mLocked.orientedRanges.y.flat = 0;
1923 mLocked.orientedRanges.y.fuzz = mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001924 break;
1925 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001926 }
1927
1928 return true;
1929}
1930
Jeff Brownef3d7e82010-09-30 14:33:04 -07001931void TouchInputMapper::dumpSurfaceLocked(String8& dump) {
1932 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth);
1933 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight);
1934 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07001935}
1936
Jeff Brown6328cdc2010-07-29 18:18:33 -07001937void TouchInputMapper::configureVirtualKeysLocked() {
Jeff Brown8d608662010-08-30 03:02:23 -07001938 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08001939 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001940
Jeff Brown6328cdc2010-07-29 18:18:33 -07001941 mLocked.virtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001942
Jeff Brown6328cdc2010-07-29 18:18:33 -07001943 if (virtualKeyDefinitions.size() == 0) {
1944 return;
1945 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001946
Jeff Brown6328cdc2010-07-29 18:18:33 -07001947 mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size());
1948
Jeff Brown8d608662010-08-30 03:02:23 -07001949 int32_t touchScreenLeft = mRawAxes.x.minValue;
1950 int32_t touchScreenTop = mRawAxes.y.minValue;
Jeff Brownd41cff22011-03-03 02:09:54 -08001951 int32_t touchScreenWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
1952 int32_t touchScreenHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001953
1954 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07001955 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07001956 virtualKeyDefinitions[i];
1957
1958 mLocked.virtualKeys.add();
1959 VirtualKey& virtualKey = mLocked.virtualKeys.editTop();
1960
1961 virtualKey.scanCode = virtualKeyDefinition.scanCode;
1962 int32_t keyCode;
1963 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001964 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07001965 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07001966 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
1967 virtualKey.scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001968 mLocked.virtualKeys.pop(); // drop the key
1969 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001970 }
1971
Jeff Brown6328cdc2010-07-29 18:18:33 -07001972 virtualKey.keyCode = keyCode;
1973 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001974
Jeff Brown6328cdc2010-07-29 18:18:33 -07001975 // convert the key definition's display coordinates into touch coordinates for a hit box
1976 int32_t halfWidth = virtualKeyDefinition.width / 2;
1977 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001978
Jeff Brown6328cdc2010-07-29 18:18:33 -07001979 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
1980 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1981 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
1982 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
1983 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
1984 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
1985 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
1986 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07001987 }
1988}
1989
1990void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) {
1991 if (!mLocked.virtualKeys.isEmpty()) {
1992 dump.append(INDENT3 "Virtual Keys:\n");
1993
1994 for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) {
1995 const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i);
1996 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
1997 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
1998 i, virtualKey.scanCode, virtualKey.keyCode,
1999 virtualKey.hitLeft, virtualKey.hitRight,
2000 virtualKey.hitTop, virtualKey.hitBottom);
2001 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002002 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002003}
2004
Jeff Brown8d608662010-08-30 03:02:23 -07002005void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002006 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07002007 Calibration& out = mCalibration;
2008
Jeff Brownc6d282b2010-10-14 21:42:15 -07002009 // Touch Size
2010 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT;
2011 String8 touchSizeCalibrationString;
2012 if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) {
2013 if (touchSizeCalibrationString == "none") {
2014 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
2015 } else if (touchSizeCalibrationString == "geometric") {
2016 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC;
2017 } else if (touchSizeCalibrationString == "pressure") {
2018 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
2019 } else if (touchSizeCalibrationString != "default") {
2020 LOGW("Invalid value for touch.touchSize.calibration: '%s'",
2021 touchSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002022 }
2023 }
2024
Jeff Brownc6d282b2010-10-14 21:42:15 -07002025 // Tool Size
2026 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT;
2027 String8 toolSizeCalibrationString;
2028 if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) {
2029 if (toolSizeCalibrationString == "none") {
2030 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
2031 } else if (toolSizeCalibrationString == "geometric") {
2032 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC;
2033 } else if (toolSizeCalibrationString == "linear") {
2034 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
2035 } else if (toolSizeCalibrationString == "area") {
2036 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA;
2037 } else if (toolSizeCalibrationString != "default") {
2038 LOGW("Invalid value for touch.toolSize.calibration: '%s'",
2039 toolSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002040 }
2041 }
2042
Jeff Brownc6d282b2010-10-14 21:42:15 -07002043 out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"),
2044 out.toolSizeLinearScale);
2045 out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"),
2046 out.toolSizeLinearBias);
2047 out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"),
2048 out.toolSizeAreaScale);
2049 out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"),
2050 out.toolSizeAreaBias);
2051 out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"),
2052 out.toolSizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07002053
2054 // Pressure
2055 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
2056 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002057 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002058 if (pressureCalibrationString == "none") {
2059 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2060 } else if (pressureCalibrationString == "physical") {
2061 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
2062 } else if (pressureCalibrationString == "amplitude") {
2063 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2064 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002065 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002066 pressureCalibrationString.string());
2067 }
2068 }
2069
2070 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
2071 String8 pressureSourceString;
2072 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
2073 if (pressureSourceString == "pressure") {
2074 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2075 } else if (pressureSourceString == "touch") {
2076 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2077 } else if (pressureSourceString != "default") {
2078 LOGW("Invalid value for touch.pressure.source: '%s'",
2079 pressureSourceString.string());
2080 }
2081 }
2082
2083 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
2084 out.pressureScale);
2085
2086 // Size
2087 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
2088 String8 sizeCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002089 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002090 if (sizeCalibrationString == "none") {
2091 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2092 } else if (sizeCalibrationString == "normalized") {
2093 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
2094 } else if (sizeCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002095 LOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002096 sizeCalibrationString.string());
2097 }
2098 }
2099
2100 // Orientation
2101 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
2102 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002103 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002104 if (orientationCalibrationString == "none") {
2105 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2106 } else if (orientationCalibrationString == "interpolated") {
2107 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002108 } else if (orientationCalibrationString == "vector") {
2109 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002110 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002111 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002112 orientationCalibrationString.string());
2113 }
2114 }
2115}
2116
2117void TouchInputMapper::resolveCalibration() {
2118 // Pressure
2119 switch (mCalibration.pressureSource) {
2120 case Calibration::PRESSURE_SOURCE_DEFAULT:
2121 if (mRawAxes.pressure.valid) {
2122 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2123 } else if (mRawAxes.touchMajor.valid) {
2124 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2125 }
2126 break;
2127
2128 case Calibration::PRESSURE_SOURCE_PRESSURE:
2129 if (! mRawAxes.pressure.valid) {
2130 LOGW("Calibration property touch.pressure.source is 'pressure' but "
2131 "the pressure axis is not available.");
2132 }
2133 break;
2134
2135 case Calibration::PRESSURE_SOURCE_TOUCH:
2136 if (! mRawAxes.touchMajor.valid) {
2137 LOGW("Calibration property touch.pressure.source is 'touch' but "
2138 "the touchMajor axis is not available.");
2139 }
2140 break;
2141
2142 default:
2143 break;
2144 }
2145
2146 switch (mCalibration.pressureCalibration) {
2147 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
2148 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
2149 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2150 } else {
2151 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2152 }
2153 break;
2154
2155 default:
2156 break;
2157 }
2158
Jeff Brownc6d282b2010-10-14 21:42:15 -07002159 // Tool Size
2160 switch (mCalibration.toolSizeCalibration) {
2161 case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07002162 if (mRawAxes.toolMajor.valid) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002163 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
Jeff Brown8d608662010-08-30 03:02:23 -07002164 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002165 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002166 }
2167 break;
2168
2169 default:
2170 break;
2171 }
2172
Jeff Brownc6d282b2010-10-14 21:42:15 -07002173 // Touch Size
2174 switch (mCalibration.touchSizeCalibration) {
2175 case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07002176 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
Jeff Brownc6d282b2010-10-14 21:42:15 -07002177 && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
2178 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
Jeff Brown8d608662010-08-30 03:02:23 -07002179 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002180 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002181 }
2182 break;
2183
2184 default:
2185 break;
2186 }
2187
2188 // Size
2189 switch (mCalibration.sizeCalibration) {
2190 case Calibration::SIZE_CALIBRATION_DEFAULT:
2191 if (mRawAxes.toolMajor.valid) {
2192 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
2193 } else {
2194 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2195 }
2196 break;
2197
2198 default:
2199 break;
2200 }
2201
2202 // Orientation
2203 switch (mCalibration.orientationCalibration) {
2204 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
2205 if (mRawAxes.orientation.valid) {
2206 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
2207 } else {
2208 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2209 }
2210 break;
2211
2212 default:
2213 break;
2214 }
2215}
2216
Jeff Brownef3d7e82010-09-30 14:33:04 -07002217void TouchInputMapper::dumpCalibration(String8& dump) {
2218 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07002219
Jeff Brownc6d282b2010-10-14 21:42:15 -07002220 // Touch Size
2221 switch (mCalibration.touchSizeCalibration) {
2222 case Calibration::TOUCH_SIZE_CALIBRATION_NONE:
2223 dump.append(INDENT4 "touch.touchSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002224 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002225 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
2226 dump.append(INDENT4 "touch.touchSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002227 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002228 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
2229 dump.append(INDENT4 "touch.touchSize.calibration: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002230 break;
2231 default:
2232 assert(false);
2233 }
2234
Jeff Brownc6d282b2010-10-14 21:42:15 -07002235 // Tool Size
2236 switch (mCalibration.toolSizeCalibration) {
2237 case Calibration::TOOL_SIZE_CALIBRATION_NONE:
2238 dump.append(INDENT4 "touch.toolSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002239 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002240 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
2241 dump.append(INDENT4 "touch.toolSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002242 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002243 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
2244 dump.append(INDENT4 "touch.toolSize.calibration: linear\n");
2245 break;
2246 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2247 dump.append(INDENT4 "touch.toolSize.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002248 break;
2249 default:
2250 assert(false);
2251 }
2252
Jeff Brownc6d282b2010-10-14 21:42:15 -07002253 if (mCalibration.haveToolSizeLinearScale) {
2254 dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n",
2255 mCalibration.toolSizeLinearScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002256 }
2257
Jeff Brownc6d282b2010-10-14 21:42:15 -07002258 if (mCalibration.haveToolSizeLinearBias) {
2259 dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n",
2260 mCalibration.toolSizeLinearBias);
Jeff Brown8d608662010-08-30 03:02:23 -07002261 }
2262
Jeff Brownc6d282b2010-10-14 21:42:15 -07002263 if (mCalibration.haveToolSizeAreaScale) {
2264 dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n",
2265 mCalibration.toolSizeAreaScale);
2266 }
2267
2268 if (mCalibration.haveToolSizeAreaBias) {
2269 dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n",
2270 mCalibration.toolSizeAreaBias);
2271 }
2272
2273 if (mCalibration.haveToolSizeIsSummed) {
Jeff Brown1f245102010-11-18 20:53:46 -08002274 dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %s\n",
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002275 toString(mCalibration.toolSizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07002276 }
2277
2278 // Pressure
2279 switch (mCalibration.pressureCalibration) {
2280 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002281 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002282 break;
2283 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002284 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002285 break;
2286 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002287 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002288 break;
2289 default:
2290 assert(false);
2291 }
2292
2293 switch (mCalibration.pressureSource) {
2294 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002295 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002296 break;
2297 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002298 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002299 break;
2300 case Calibration::PRESSURE_SOURCE_DEFAULT:
2301 break;
2302 default:
2303 assert(false);
2304 }
2305
2306 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07002307 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
2308 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002309 }
2310
2311 // Size
2312 switch (mCalibration.sizeCalibration) {
2313 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002314 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002315 break;
2316 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002317 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002318 break;
2319 default:
2320 assert(false);
2321 }
2322
2323 // Orientation
2324 switch (mCalibration.orientationCalibration) {
2325 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002326 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002327 break;
2328 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002329 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002330 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002331 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
2332 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
2333 break;
Jeff Brown8d608662010-08-30 03:02:23 -07002334 default:
2335 assert(false);
2336 }
2337}
2338
Jeff Brown6d0fec22010-07-23 21:28:06 -07002339void TouchInputMapper::reset() {
2340 // Synthesize touch up event if touch is currently down.
2341 // This will also take care of finishing virtual key processing if needed.
2342 if (mLastTouch.pointerCount != 0) {
2343 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
2344 mCurrentTouch.clear();
2345 syncTouch(when, true);
2346 }
2347
Jeff Brown6328cdc2010-07-29 18:18:33 -07002348 { // acquire lock
2349 AutoMutex _l(mLock);
2350 initializeLocked();
2351 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07002352
Jeff Brown6328cdc2010-07-29 18:18:33 -07002353 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002354}
2355
2356void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002357 // Preprocess pointer data.
Jeff Brown6d0fec22010-07-23 21:28:06 -07002358 if (mParameters.useBadTouchFilter) {
2359 if (applyBadTouchFilter()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002360 havePointerIds = false;
2361 }
2362 }
2363
Jeff Brown6d0fec22010-07-23 21:28:06 -07002364 if (mParameters.useJumpyTouchFilter) {
2365 if (applyJumpyTouchFilter()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002366 havePointerIds = false;
2367 }
2368 }
2369
2370 if (! havePointerIds) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002371 calculatePointerIds();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002372 }
2373
Jeff Brown6d0fec22010-07-23 21:28:06 -07002374 TouchData temp;
2375 TouchData* savedTouch;
2376 if (mParameters.useAveragingTouchFilter) {
2377 temp.copyFrom(mCurrentTouch);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002378 savedTouch = & temp;
2379
Jeff Brown6d0fec22010-07-23 21:28:06 -07002380 applyAveragingTouchFilter();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002381 } else {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002382 savedTouch = & mCurrentTouch;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002383 }
2384
Jeff Brown56194eb2011-03-02 19:23:13 -08002385 uint32_t policyFlags = 0;
Jeff Brown05dc66a2011-03-02 14:41:58 -08002386 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) {
Jeff Brown56194eb2011-03-02 19:23:13 -08002387 // Hide the pointer on an initial down.
Jeff Brown05dc66a2011-03-02 14:41:58 -08002388 getContext()->fadePointer();
Jeff Brown56194eb2011-03-02 19:23:13 -08002389
2390 // Initial downs on external touch devices should wake the device.
2391 // We don't do this for internal touch screens to prevent them from waking
2392 // up in your pocket.
2393 // TODO: Use the input device configuration to control this behavior more finely.
2394 if (getDevice()->isExternal()) {
2395 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2396 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002397 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002398
Jeff Brown05dc66a2011-03-02 14:41:58 -08002399 // Process touches and virtual keys.
Jeff Brown6d0fec22010-07-23 21:28:06 -07002400 TouchResult touchResult = consumeOffScreenTouches(when, policyFlags);
2401 if (touchResult == DISPATCH_TOUCH) {
Jeff Brownfe508922011-01-18 15:10:10 -08002402 detectGestures(when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002403 dispatchTouches(when, policyFlags);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002404 }
2405
Jeff Brown6328cdc2010-07-29 18:18:33 -07002406 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brown6d0fec22010-07-23 21:28:06 -07002407 if (touchResult == DROP_STROKE) {
2408 mLastTouch.clear();
2409 } else {
2410 mLastTouch.copyFrom(*savedTouch);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002411 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002412}
2413
Jeff Brown6d0fec22010-07-23 21:28:06 -07002414TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches(
2415 nsecs_t when, uint32_t policyFlags) {
2416 int32_t keyEventAction, keyEventFlags;
2417 int32_t keyCode, scanCode, downTime;
2418 TouchResult touchResult;
Jeff Brown349703e2010-06-22 01:27:15 -07002419
Jeff Brown6328cdc2010-07-29 18:18:33 -07002420 { // acquire lock
2421 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002422
Jeff Brown6328cdc2010-07-29 18:18:33 -07002423 // Update surface size and orientation, including virtual key positions.
2424 if (! configureSurfaceLocked()) {
2425 return DROP_STROKE;
2426 }
2427
2428 // Check for virtual key press.
2429 if (mLocked.currentVirtualKey.down) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002430 if (mCurrentTouch.pointerCount == 0) {
2431 // Pointer went up while virtual key was down.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002432 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002433#if DEBUG_VIRTUAL_KEYS
2434 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07002435 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002436#endif
2437 keyEventAction = AKEY_EVENT_ACTION_UP;
2438 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2439 touchResult = SKIP_TOUCH;
2440 goto DispatchVirtualKey;
2441 }
2442
2443 if (mCurrentTouch.pointerCount == 1) {
2444 int32_t x = mCurrentTouch.pointers[0].x;
2445 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002446 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
2447 if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002448 // Pointer is still within the space of the virtual key.
2449 return SKIP_TOUCH;
2450 }
2451 }
2452
2453 // Pointer left virtual key area or another pointer also went down.
2454 // Send key cancellation and drop the stroke so subsequent motions will be
2455 // considered fresh downs. This is useful when the user swipes away from the
2456 // virtual key area into the main display surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002457 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002458#if DEBUG_VIRTUAL_KEYS
2459 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07002460 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002461#endif
2462 keyEventAction = AKEY_EVENT_ACTION_UP;
2463 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
2464 | AKEY_EVENT_FLAG_CANCELED;
Jeff Brownc3db8582010-10-20 15:33:38 -07002465
2466 // Check whether the pointer moved inside the display area where we should
2467 // start a new stroke.
2468 int32_t x = mCurrentTouch.pointers[0].x;
2469 int32_t y = mCurrentTouch.pointers[0].y;
2470 if (isPointInsideSurfaceLocked(x, y)) {
2471 mLastTouch.clear();
2472 touchResult = DISPATCH_TOUCH;
2473 } else {
2474 touchResult = DROP_STROKE;
2475 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002476 } else {
2477 if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) {
2478 // Pointer just went down. Handle off-screen touches, if needed.
2479 int32_t x = mCurrentTouch.pointers[0].x;
2480 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002481 if (! isPointInsideSurfaceLocked(x, y)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002482 // If exactly one pointer went down, check for virtual key hit.
2483 // Otherwise we will drop the entire stroke.
2484 if (mCurrentTouch.pointerCount == 1) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002485 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002486 if (virtualKey) {
Jeff Brownfe508922011-01-18 15:10:10 -08002487 if (mContext->shouldDropVirtualKey(when, getDevice(),
2488 virtualKey->keyCode, virtualKey->scanCode)) {
2489 return DROP_STROKE;
2490 }
2491
Jeff Brown6328cdc2010-07-29 18:18:33 -07002492 mLocked.currentVirtualKey.down = true;
2493 mLocked.currentVirtualKey.downTime = when;
2494 mLocked.currentVirtualKey.keyCode = virtualKey->keyCode;
2495 mLocked.currentVirtualKey.scanCode = virtualKey->scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002496#if DEBUG_VIRTUAL_KEYS
2497 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07002498 mLocked.currentVirtualKey.keyCode,
2499 mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002500#endif
2501 keyEventAction = AKEY_EVENT_ACTION_DOWN;
2502 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM
2503 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2504 touchResult = SKIP_TOUCH;
2505 goto DispatchVirtualKey;
2506 }
2507 }
2508 return DROP_STROKE;
2509 }
2510 }
2511 return DISPATCH_TOUCH;
2512 }
2513
2514 DispatchVirtualKey:
2515 // Collect remaining state needed to dispatch virtual key.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002516 keyCode = mLocked.currentVirtualKey.keyCode;
2517 scanCode = mLocked.currentVirtualKey.scanCode;
2518 downTime = mLocked.currentVirtualKey.downTime;
2519 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07002520
2521 // Dispatch virtual key.
2522 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown0eaf3932010-10-01 14:55:30 -07002523 policyFlags |= POLICY_FLAG_VIRTUAL;
Jeff Brownb6997262010-10-08 22:31:17 -07002524 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
2525 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
2526 return touchResult;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002527}
2528
Jeff Brownfe508922011-01-18 15:10:10 -08002529void TouchInputMapper::detectGestures(nsecs_t when) {
2530 // Disable all virtual key touches that happen within a short time interval of the
2531 // most recent touch. The idea is to filter out stray virtual key presses when
2532 // interacting with the touch screen.
2533 //
2534 // Problems we're trying to solve:
2535 //
2536 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
2537 // virtual key area that is implemented by a separate touch panel and accidentally
2538 // triggers a virtual key.
2539 //
2540 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
2541 // area and accidentally triggers a virtual key. This often happens when virtual keys
2542 // are layed out below the screen near to where the on screen keyboard's space bar
2543 // is displayed.
2544 if (mParameters.virtualKeyQuietTime > 0 && mCurrentTouch.pointerCount != 0) {
2545 mContext->disableVirtualKeysUntil(when + mParameters.virtualKeyQuietTime);
2546 }
2547}
2548
Jeff Brown6d0fec22010-07-23 21:28:06 -07002549void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
2550 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2551 uint32_t lastPointerCount = mLastTouch.pointerCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002552 if (currentPointerCount == 0 && lastPointerCount == 0) {
2553 return; // nothing to do!
2554 }
2555
Jeff Brown6d0fec22010-07-23 21:28:06 -07002556 BitSet32 currentIdBits = mCurrentTouch.idBits;
2557 BitSet32 lastIdBits = mLastTouch.idBits;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002558
2559 if (currentIdBits == lastIdBits) {
2560 // No pointer id changes so this is a move event.
2561 // The dispatcher takes care of batching moves so we don't have to deal with that here.
Jeff Brownc5ed5912010-07-14 18:48:53 -07002562 int32_t motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002563 dispatchTouch(when, policyFlags, & mCurrentTouch,
Jeff Brown8d608662010-08-30 03:02:23 -07002564 currentIdBits, -1, currentPointerCount, motionEventAction);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002565 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07002566 // There may be pointers going up and pointers going down and pointers moving
2567 // all at the same time.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002568 BitSet32 upIdBits(lastIdBits.value & ~ currentIdBits.value);
2569 BitSet32 downIdBits(currentIdBits.value & ~ lastIdBits.value);
2570 BitSet32 activeIdBits(lastIdBits.value);
Jeff Brown8d608662010-08-30 03:02:23 -07002571 uint32_t pointerCount = lastPointerCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002572
Jeff Brownc3db8582010-10-20 15:33:38 -07002573 // Produce an intermediate representation of the touch data that consists of the
2574 // old location of pointers that have just gone up and the new location of pointers that
2575 // have just moved but omits the location of pointers that have just gone down.
2576 TouchData interimTouch;
2577 interimTouch.copyFrom(mLastTouch);
2578
2579 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
2580 bool moveNeeded = false;
2581 while (!moveIdBits.isEmpty()) {
2582 uint32_t moveId = moveIdBits.firstMarkedBit();
2583 moveIdBits.clearBit(moveId);
2584
2585 int32_t oldIndex = mLastTouch.idToIndex[moveId];
2586 int32_t newIndex = mCurrentTouch.idToIndex[moveId];
2587 if (mLastTouch.pointers[oldIndex] != mCurrentTouch.pointers[newIndex]) {
2588 interimTouch.pointers[oldIndex] = mCurrentTouch.pointers[newIndex];
2589 moveNeeded = true;
2590 }
2591 }
2592
2593 // Dispatch pointer up events using the interim pointer locations.
2594 while (!upIdBits.isEmpty()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002595 uint32_t upId = upIdBits.firstMarkedBit();
2596 upIdBits.clearBit(upId);
2597 BitSet32 oldActiveIdBits = activeIdBits;
2598 activeIdBits.clearBit(upId);
2599
2600 int32_t motionEventAction;
2601 if (activeIdBits.isEmpty()) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07002602 motionEventAction = AMOTION_EVENT_ACTION_UP;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002603 } else {
Jeff Brown00ba8842010-07-16 15:01:56 -07002604 motionEventAction = AMOTION_EVENT_ACTION_POINTER_UP;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002605 }
2606
Jeff Brownc3db8582010-10-20 15:33:38 -07002607 dispatchTouch(when, policyFlags, &interimTouch,
Jeff Brown8d608662010-08-30 03:02:23 -07002608 oldActiveIdBits, upId, pointerCount, motionEventAction);
2609 pointerCount -= 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002610 }
2611
Jeff Brownc3db8582010-10-20 15:33:38 -07002612 // Dispatch move events if any of the remaining pointers moved from their old locations.
2613 // Although applications receive new locations as part of individual pointer up
2614 // events, they do not generally handle them except when presented in a move event.
2615 if (moveNeeded) {
2616 dispatchTouch(when, policyFlags, &mCurrentTouch,
2617 activeIdBits, -1, pointerCount, AMOTION_EVENT_ACTION_MOVE);
2618 }
2619
2620 // Dispatch pointer down events using the new pointer locations.
2621 while (!downIdBits.isEmpty()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002622 uint32_t downId = downIdBits.firstMarkedBit();
2623 downIdBits.clearBit(downId);
2624 BitSet32 oldActiveIdBits = activeIdBits;
2625 activeIdBits.markBit(downId);
2626
2627 int32_t motionEventAction;
2628 if (oldActiveIdBits.isEmpty()) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07002629 motionEventAction = AMOTION_EVENT_ACTION_DOWN;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002630 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002631 } else {
Jeff Brown00ba8842010-07-16 15:01:56 -07002632 motionEventAction = AMOTION_EVENT_ACTION_POINTER_DOWN;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002633 }
2634
Jeff Brown8d608662010-08-30 03:02:23 -07002635 pointerCount += 1;
Jeff Brownc3db8582010-10-20 15:33:38 -07002636 dispatchTouch(when, policyFlags, &mCurrentTouch,
Jeff Brown8d608662010-08-30 03:02:23 -07002637 activeIdBits, downId, pointerCount, motionEventAction);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002638 }
2639 }
2640}
2641
Jeff Brown6d0fec22010-07-23 21:28:06 -07002642void TouchInputMapper::dispatchTouch(nsecs_t when, uint32_t policyFlags,
Jeff Brown8d608662010-08-30 03:02:23 -07002643 TouchData* touch, BitSet32 idBits, uint32_t changedId, uint32_t pointerCount,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002644 int32_t motionEventAction) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002645 int32_t pointerIds[MAX_POINTERS];
2646 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownd41cff22011-03-03 02:09:54 -08002647 int32_t motionEventEdgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002648 float xPrecision, yPrecision;
2649
2650 { // acquire lock
2651 AutoMutex _l(mLock);
2652
2653 // Walk through the the active pointers and map touch screen coordinates (TouchData) into
2654 // display coordinates (PointerCoords) and adjust for display orientation.
Jeff Brown8d608662010-08-30 03:02:23 -07002655 for (uint32_t outIndex = 0; ! idBits.isEmpty(); outIndex++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002656 uint32_t id = idBits.firstMarkedBit();
2657 idBits.clearBit(id);
Jeff Brown8d608662010-08-30 03:02:23 -07002658 uint32_t inIndex = touch->idToIndex[id];
Jeff Brown6328cdc2010-07-29 18:18:33 -07002659
Jeff Brown8d608662010-08-30 03:02:23 -07002660 const PointerData& in = touch->pointers[inIndex];
Jeff Brown6328cdc2010-07-29 18:18:33 -07002661
Jeff Brown8d608662010-08-30 03:02:23 -07002662 // ToolMajor and ToolMinor
2663 float toolMajor, toolMinor;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002664 switch (mCalibration.toolSizeCalibration) {
2665 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brown8d608662010-08-30 03:02:23 -07002666 toolMajor = in.toolMajor * mLocked.geometricScale;
2667 if (mRawAxes.toolMinor.valid) {
2668 toolMinor = in.toolMinor * mLocked.geometricScale;
2669 } else {
2670 toolMinor = toolMajor;
2671 }
2672 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002673 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
Jeff Brown8d608662010-08-30 03:02:23 -07002674 toolMajor = in.toolMajor != 0
Jeff Brownc6d282b2010-10-14 21:42:15 -07002675 ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias
Jeff Brown8d608662010-08-30 03:02:23 -07002676 : 0;
2677 if (mRawAxes.toolMinor.valid) {
2678 toolMinor = in.toolMinor != 0
Jeff Brownc6d282b2010-10-14 21:42:15 -07002679 ? in.toolMinor * mLocked.toolSizeLinearScale
2680 + mLocked.toolSizeLinearBias
Jeff Brown8d608662010-08-30 03:02:23 -07002681 : 0;
2682 } else {
2683 toolMinor = toolMajor;
2684 }
2685 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002686 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2687 if (in.toolMajor != 0) {
2688 float diameter = sqrtf(in.toolMajor
2689 * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias);
2690 toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias;
2691 } else {
2692 toolMajor = 0;
2693 }
2694 toolMinor = toolMajor;
2695 break;
Jeff Brown8d608662010-08-30 03:02:23 -07002696 default:
2697 toolMajor = 0;
2698 toolMinor = 0;
2699 break;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002700 }
2701
Jeff Brownc6d282b2010-10-14 21:42:15 -07002702 if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) {
Jeff Brown8d608662010-08-30 03:02:23 -07002703 toolMajor /= pointerCount;
2704 toolMinor /= pointerCount;
2705 }
2706
2707 // Pressure
2708 float rawPressure;
2709 switch (mCalibration.pressureSource) {
2710 case Calibration::PRESSURE_SOURCE_PRESSURE:
2711 rawPressure = in.pressure;
2712 break;
2713 case Calibration::PRESSURE_SOURCE_TOUCH:
2714 rawPressure = in.touchMajor;
2715 break;
2716 default:
2717 rawPressure = 0;
2718 }
2719
2720 float pressure;
2721 switch (mCalibration.pressureCalibration) {
2722 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
2723 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
2724 pressure = rawPressure * mLocked.pressureScale;
2725 break;
2726 default:
2727 pressure = 1;
2728 break;
2729 }
2730
2731 // TouchMajor and TouchMinor
2732 float touchMajor, touchMinor;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002733 switch (mCalibration.touchSizeCalibration) {
2734 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brown8d608662010-08-30 03:02:23 -07002735 touchMajor = in.touchMajor * mLocked.geometricScale;
2736 if (mRawAxes.touchMinor.valid) {
2737 touchMinor = in.touchMinor * mLocked.geometricScale;
2738 } else {
2739 touchMinor = touchMajor;
2740 }
2741 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002742 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
Jeff Brown8d608662010-08-30 03:02:23 -07002743 touchMajor = toolMajor * pressure;
2744 touchMinor = toolMinor * pressure;
2745 break;
2746 default:
2747 touchMajor = 0;
2748 touchMinor = 0;
2749 break;
2750 }
2751
2752 if (touchMajor > toolMajor) {
2753 touchMajor = toolMajor;
2754 }
2755 if (touchMinor > toolMinor) {
2756 touchMinor = toolMinor;
2757 }
2758
2759 // Size
2760 float size;
2761 switch (mCalibration.sizeCalibration) {
2762 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
2763 float rawSize = mRawAxes.toolMinor.valid
2764 ? avg(in.toolMajor, in.toolMinor)
2765 : in.toolMajor;
2766 size = rawSize * mLocked.sizeScale;
2767 break;
2768 }
2769 default:
2770 size = 0;
2771 break;
2772 }
2773
2774 // Orientation
2775 float orientation;
2776 switch (mCalibration.orientationCalibration) {
2777 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
2778 orientation = in.orientation * mLocked.orientationScale;
2779 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002780 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
2781 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
2782 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
2783 if (c1 != 0 || c2 != 0) {
2784 orientation = atan2f(c1, c2) * 0.5f;
Jeff Brownc3451d42011-02-15 19:13:20 -08002785 float scale = 1.0f + pythag(c1, c2) / 16.0f;
2786 touchMajor *= scale;
2787 touchMinor /= scale;
2788 toolMajor *= scale;
2789 toolMinor /= scale;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002790 } else {
2791 orientation = 0;
2792 }
2793 break;
2794 }
Jeff Brown8d608662010-08-30 03:02:23 -07002795 default:
2796 orientation = 0;
2797 }
2798
Jeff Brownd41cff22011-03-03 02:09:54 -08002799 // X and Y
2800 // Adjust coords for surface orientation.
2801 float x, y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002802 switch (mLocked.surfaceOrientation) {
Jeff Brownd41cff22011-03-03 02:09:54 -08002803 case DISPLAY_ORIENTATION_90:
2804 x = float(in.y - mRawAxes.y.minValue) * mLocked.yScale;
2805 y = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002806 orientation -= M_PI_2;
2807 if (orientation < - M_PI_2) {
2808 orientation += M_PI;
2809 }
2810 break;
Jeff Brownd41cff22011-03-03 02:09:54 -08002811 case DISPLAY_ORIENTATION_180:
2812 x = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale;
2813 y = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002814 break;
Jeff Brownd41cff22011-03-03 02:09:54 -08002815 case DISPLAY_ORIENTATION_270:
2816 x = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale;
2817 y = float(in.x - mRawAxes.x.minValue) * mLocked.xScale;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002818 orientation += M_PI_2;
2819 if (orientation > M_PI_2) {
2820 orientation -= M_PI;
2821 }
2822 break;
Jeff Brownd41cff22011-03-03 02:09:54 -08002823 default:
2824 x = float(in.x - mRawAxes.x.minValue) * mLocked.xScale;
2825 y = float(in.y - mRawAxes.y.minValue) * mLocked.yScale;
2826 break;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002827 }
2828
Jeff Brown8d608662010-08-30 03:02:23 -07002829 // Write output coords.
2830 PointerCoords& out = pointerCoords[outIndex];
Jeff Brown91c69ab2011-02-14 17:03:18 -08002831 out.clear();
Jeff Brownebbd5d12011-02-17 13:01:34 -08002832 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2833 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2834 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
2835 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
2836 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
2837 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
2838 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
2839 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
2840 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002841
Jeff Brown8d608662010-08-30 03:02:23 -07002842 pointerIds[outIndex] = int32_t(id);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002843
2844 if (id == changedId) {
Jeff Brown8d608662010-08-30 03:02:23 -07002845 motionEventAction |= outIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002846 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002847 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002848
2849 // Check edge flags by looking only at the first pointer since the flags are
2850 // global to the event.
2851 if (motionEventAction == AMOTION_EVENT_ACTION_DOWN) {
Jeff Brownd41cff22011-03-03 02:09:54 -08002852 uint32_t inIndex = touch->idToIndex[pointerIds[0]];
2853 const PointerData& in = touch->pointers[inIndex];
Jeff Brown91c69ab2011-02-14 17:03:18 -08002854
Jeff Brownd41cff22011-03-03 02:09:54 -08002855 if (in.x <= mRawAxes.x.minValue) {
2856 motionEventEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_LEFT,
2857 mLocked.surfaceOrientation);
2858 } else if (in.x >= mRawAxes.x.maxValue) {
2859 motionEventEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_RIGHT,
2860 mLocked.surfaceOrientation);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002861 }
Jeff Brownd41cff22011-03-03 02:09:54 -08002862 if (in.y <= mRawAxes.y.minValue) {
2863 motionEventEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_TOP,
2864 mLocked.surfaceOrientation);
2865 } else if (in.y >= mRawAxes.y.maxValue) {
2866 motionEventEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_BOTTOM,
2867 mLocked.surfaceOrientation);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002868 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002869 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002870
2871 xPrecision = mLocked.orientedXPrecision;
2872 yPrecision = mLocked.orientedYPrecision;
2873 } // release lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002874
Jeff Brown83c09682010-12-23 17:50:18 -08002875 getDispatcher()->notifyMotion(when, getDeviceId(), mSources, policyFlags,
Jeff Brown85a31762010-09-01 17:01:00 -07002876 motionEventAction, 0, getContext()->getGlobalMetaState(), motionEventEdgeFlags,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002877 pointerCount, pointerIds, pointerCoords,
Jeff Brown6328cdc2010-07-29 18:18:33 -07002878 xPrecision, yPrecision, mDownTime);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002879}
2880
Jeff Brown6328cdc2010-07-29 18:18:33 -07002881bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) {
Jeff Brownd41cff22011-03-03 02:09:54 -08002882 return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue
2883 && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002884}
2885
Jeff Brown6328cdc2010-07-29 18:18:33 -07002886const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked(
2887 int32_t x, int32_t y) {
2888 size_t numVirtualKeys = mLocked.virtualKeys.size();
2889 for (size_t i = 0; i < numVirtualKeys; i++) {
2890 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07002891
2892#if DEBUG_VIRTUAL_KEYS
2893 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
2894 "left=%d, top=%d, right=%d, bottom=%d",
2895 x, y,
2896 virtualKey.keyCode, virtualKey.scanCode,
2897 virtualKey.hitLeft, virtualKey.hitTop,
2898 virtualKey.hitRight, virtualKey.hitBottom);
2899#endif
2900
2901 if (virtualKey.isHit(x, y)) {
2902 return & virtualKey;
2903 }
2904 }
2905
2906 return NULL;
2907}
2908
2909void TouchInputMapper::calculatePointerIds() {
2910 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2911 uint32_t lastPointerCount = mLastTouch.pointerCount;
2912
2913 if (currentPointerCount == 0) {
2914 // No pointers to assign.
2915 mCurrentTouch.idBits.clear();
2916 } else if (lastPointerCount == 0) {
2917 // All pointers are new.
2918 mCurrentTouch.idBits.clear();
2919 for (uint32_t i = 0; i < currentPointerCount; i++) {
2920 mCurrentTouch.pointers[i].id = i;
2921 mCurrentTouch.idToIndex[i] = i;
2922 mCurrentTouch.idBits.markBit(i);
2923 }
2924 } else if (currentPointerCount == 1 && lastPointerCount == 1) {
2925 // Only one pointer and no change in count so it must have the same id as before.
2926 uint32_t id = mLastTouch.pointers[0].id;
2927 mCurrentTouch.pointers[0].id = id;
2928 mCurrentTouch.idToIndex[id] = 0;
2929 mCurrentTouch.idBits.value = BitSet32::valueForBit(id);
2930 } else {
2931 // General case.
2932 // We build a heap of squared euclidean distances between current and last pointers
2933 // associated with the current and last pointer indices. Then, we find the best
2934 // match (by distance) for each current pointer.
2935 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
2936
2937 uint32_t heapSize = 0;
2938 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
2939 currentPointerIndex++) {
2940 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
2941 lastPointerIndex++) {
2942 int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x
2943 - mLastTouch.pointers[lastPointerIndex].x;
2944 int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y
2945 - mLastTouch.pointers[lastPointerIndex].y;
2946
2947 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
2948
2949 // Insert new element into the heap (sift up).
2950 heap[heapSize].currentPointerIndex = currentPointerIndex;
2951 heap[heapSize].lastPointerIndex = lastPointerIndex;
2952 heap[heapSize].distance = distance;
2953 heapSize += 1;
2954 }
2955 }
2956
2957 // Heapify
2958 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
2959 startIndex -= 1;
2960 for (uint32_t parentIndex = startIndex; ;) {
2961 uint32_t childIndex = parentIndex * 2 + 1;
2962 if (childIndex >= heapSize) {
2963 break;
2964 }
2965
2966 if (childIndex + 1 < heapSize
2967 && heap[childIndex + 1].distance < heap[childIndex].distance) {
2968 childIndex += 1;
2969 }
2970
2971 if (heap[parentIndex].distance <= heap[childIndex].distance) {
2972 break;
2973 }
2974
2975 swap(heap[parentIndex], heap[childIndex]);
2976 parentIndex = childIndex;
2977 }
2978 }
2979
2980#if DEBUG_POINTER_ASSIGNMENT
2981 LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize);
2982 for (size_t i = 0; i < heapSize; i++) {
2983 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
2984 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
2985 heap[i].distance);
2986 }
2987#endif
2988
2989 // Pull matches out by increasing order of distance.
2990 // To avoid reassigning pointers that have already been matched, the loop keeps track
2991 // of which last and current pointers have been matched using the matchedXXXBits variables.
2992 // It also tracks the used pointer id bits.
2993 BitSet32 matchedLastBits(0);
2994 BitSet32 matchedCurrentBits(0);
2995 BitSet32 usedIdBits(0);
2996 bool first = true;
2997 for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) {
2998 for (;;) {
2999 if (first) {
3000 // The first time through the loop, we just consume the root element of
3001 // the heap (the one with smallest distance).
3002 first = false;
3003 } else {
3004 // Previous iterations consumed the root element of the heap.
3005 // Pop root element off of the heap (sift down).
3006 heapSize -= 1;
3007 assert(heapSize > 0);
3008
3009 // Sift down.
3010 heap[0] = heap[heapSize];
3011 for (uint32_t parentIndex = 0; ;) {
3012 uint32_t childIndex = parentIndex * 2 + 1;
3013 if (childIndex >= heapSize) {
3014 break;
3015 }
3016
3017 if (childIndex + 1 < heapSize
3018 && heap[childIndex + 1].distance < heap[childIndex].distance) {
3019 childIndex += 1;
3020 }
3021
3022 if (heap[parentIndex].distance <= heap[childIndex].distance) {
3023 break;
3024 }
3025
3026 swap(heap[parentIndex], heap[childIndex]);
3027 parentIndex = childIndex;
3028 }
3029
3030#if DEBUG_POINTER_ASSIGNMENT
3031 LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize);
3032 for (size_t i = 0; i < heapSize; i++) {
3033 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
3034 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
3035 heap[i].distance);
3036 }
3037#endif
3038 }
3039
3040 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
3041 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
3042
3043 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
3044 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
3045
3046 matchedCurrentBits.markBit(currentPointerIndex);
3047 matchedLastBits.markBit(lastPointerIndex);
3048
3049 uint32_t id = mLastTouch.pointers[lastPointerIndex].id;
3050 mCurrentTouch.pointers[currentPointerIndex].id = id;
3051 mCurrentTouch.idToIndex[id] = currentPointerIndex;
3052 usedIdBits.markBit(id);
3053
3054#if DEBUG_POINTER_ASSIGNMENT
3055 LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
3056 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
3057#endif
3058 break;
3059 }
3060 }
3061
3062 // Assign fresh ids to new pointers.
3063 if (currentPointerCount > lastPointerCount) {
3064 for (uint32_t i = currentPointerCount - lastPointerCount; ;) {
3065 uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit();
3066 uint32_t id = usedIdBits.firstUnmarkedBit();
3067
3068 mCurrentTouch.pointers[currentPointerIndex].id = id;
3069 mCurrentTouch.idToIndex[id] = currentPointerIndex;
3070 usedIdBits.markBit(id);
3071
3072#if DEBUG_POINTER_ASSIGNMENT
3073 LOGD("calculatePointerIds - assigned: cur=%d, id=%d",
3074 currentPointerIndex, id);
3075#endif
3076
3077 if (--i == 0) break; // done
3078 matchedCurrentBits.markBit(currentPointerIndex);
3079 }
3080 }
3081
3082 // Fix id bits.
3083 mCurrentTouch.idBits = usedIdBits;
3084 }
3085}
3086
3087/* Special hack for devices that have bad screen data: if one of the
3088 * points has moved more than a screen height from the last position,
3089 * then drop it. */
3090bool TouchInputMapper::applyBadTouchFilter() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003091 uint32_t pointerCount = mCurrentTouch.pointerCount;
3092
3093 // Nothing to do if there are no points.
3094 if (pointerCount == 0) {
3095 return false;
3096 }
3097
3098 // Don't do anything if a finger is going down or up. We run
3099 // here before assigning pointer IDs, so there isn't a good
3100 // way to do per-finger matching.
3101 if (pointerCount != mLastTouch.pointerCount) {
3102 return false;
3103 }
3104
3105 // We consider a single movement across more than a 7/16 of
3106 // the long size of the screen to be bad. This was a magic value
3107 // determined by looking at the maximum distance it is feasible
3108 // to actually move in one sample.
Jeff Brownd41cff22011-03-03 02:09:54 -08003109 int32_t maxDeltaY = (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1) * 7 / 16;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003110
3111 // XXX The original code in InputDevice.java included commented out
3112 // code for testing the X axis. Note that when we drop a point
3113 // we don't actually restore the old X either. Strange.
3114 // The old code also tries to track when bad points were previously
3115 // detected but it turns out that due to the placement of a "break"
3116 // at the end of the loop, we never set mDroppedBadPoint to true
3117 // so it is effectively dead code.
3118 // Need to figure out if the old code is busted or just overcomplicated
3119 // but working as intended.
3120
3121 // Look through all new points and see if any are farther than
3122 // acceptable from all previous points.
3123 for (uint32_t i = pointerCount; i-- > 0; ) {
3124 int32_t y = mCurrentTouch.pointers[i].y;
3125 int32_t closestY = INT_MAX;
3126 int32_t closestDeltaY = 0;
3127
3128#if DEBUG_HACKS
3129 LOGD("BadTouchFilter: Looking at next point #%d: y=%d", i, y);
3130#endif
3131
3132 for (uint32_t j = pointerCount; j-- > 0; ) {
3133 int32_t lastY = mLastTouch.pointers[j].y;
3134 int32_t deltaY = abs(y - lastY);
3135
3136#if DEBUG_HACKS
3137 LOGD("BadTouchFilter: Comparing with last point #%d: y=%d deltaY=%d",
3138 j, lastY, deltaY);
3139#endif
3140
3141 if (deltaY < maxDeltaY) {
3142 goto SkipSufficientlyClosePoint;
3143 }
3144 if (deltaY < closestDeltaY) {
3145 closestDeltaY = deltaY;
3146 closestY = lastY;
3147 }
3148 }
3149
3150 // Must not have found a close enough match.
3151#if DEBUG_HACKS
3152 LOGD("BadTouchFilter: Dropping bad point #%d: newY=%d oldY=%d deltaY=%d maxDeltaY=%d",
3153 i, y, closestY, closestDeltaY, maxDeltaY);
3154#endif
3155
3156 mCurrentTouch.pointers[i].y = closestY;
3157 return true; // XXX original code only corrects one point
3158
3159 SkipSufficientlyClosePoint: ;
3160 }
3161
3162 // No change.
3163 return false;
3164}
3165
3166/* Special hack for devices that have bad screen data: drop points where
3167 * the coordinate value for one axis has jumped to the other pointer's location.
3168 */
3169bool TouchInputMapper::applyJumpyTouchFilter() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003170 uint32_t pointerCount = mCurrentTouch.pointerCount;
3171 if (mLastTouch.pointerCount != pointerCount) {
3172#if DEBUG_HACKS
3173 LOGD("JumpyTouchFilter: Different pointer count %d -> %d",
3174 mLastTouch.pointerCount, pointerCount);
3175 for (uint32_t i = 0; i < pointerCount; i++) {
3176 LOGD(" Pointer %d (%d, %d)", i,
3177 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
3178 }
3179#endif
3180
3181 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_TRANSITION_DROPS) {
3182 if (mLastTouch.pointerCount == 1 && pointerCount == 2) {
3183 // Just drop the first few events going from 1 to 2 pointers.
3184 // They're bad often enough that they're not worth considering.
3185 mCurrentTouch.pointerCount = 1;
3186 mJumpyTouchFilter.jumpyPointsDropped += 1;
3187
3188#if DEBUG_HACKS
3189 LOGD("JumpyTouchFilter: Pointer 2 dropped");
3190#endif
3191 return true;
3192 } else if (mLastTouch.pointerCount == 2 && pointerCount == 1) {
3193 // The event when we go from 2 -> 1 tends to be messed up too
3194 mCurrentTouch.pointerCount = 2;
3195 mCurrentTouch.pointers[0] = mLastTouch.pointers[0];
3196 mCurrentTouch.pointers[1] = mLastTouch.pointers[1];
3197 mJumpyTouchFilter.jumpyPointsDropped += 1;
3198
3199#if DEBUG_HACKS
3200 for (int32_t i = 0; i < 2; i++) {
3201 LOGD("JumpyTouchFilter: Pointer %d replaced (%d, %d)", i,
3202 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
3203 }
3204#endif
3205 return true;
3206 }
3207 }
3208 // Reset jumpy points dropped on other transitions or if limit exceeded.
3209 mJumpyTouchFilter.jumpyPointsDropped = 0;
3210
3211#if DEBUG_HACKS
3212 LOGD("JumpyTouchFilter: Transition - drop limit reset");
3213#endif
3214 return false;
3215 }
3216
3217 // We have the same number of pointers as last time.
3218 // A 'jumpy' point is one where the coordinate value for one axis
3219 // has jumped to the other pointer's location. No need to do anything
3220 // else if we only have one pointer.
3221 if (pointerCount < 2) {
3222 return false;
3223 }
3224
3225 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_DROP_LIMIT) {
Jeff Brownd41cff22011-03-03 02:09:54 -08003226 int jumpyEpsilon = (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1) / JUMPY_EPSILON_DIVISOR;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003227
3228 // We only replace the single worst jumpy point as characterized by pointer distance
3229 // in a single axis.
3230 int32_t badPointerIndex = -1;
3231 int32_t badPointerReplacementIndex = -1;
3232 int32_t badPointerDistance = INT_MIN; // distance to be corrected
3233
3234 for (uint32_t i = pointerCount; i-- > 0; ) {
3235 int32_t x = mCurrentTouch.pointers[i].x;
3236 int32_t y = mCurrentTouch.pointers[i].y;
3237
3238#if DEBUG_HACKS
3239 LOGD("JumpyTouchFilter: Point %d (%d, %d)", i, x, y);
3240#endif
3241
3242 // Check if a touch point is too close to another's coordinates
3243 bool dropX = false, dropY = false;
3244 for (uint32_t j = 0; j < pointerCount; j++) {
3245 if (i == j) {
3246 continue;
3247 }
3248
3249 if (abs(x - mCurrentTouch.pointers[j].x) <= jumpyEpsilon) {
3250 dropX = true;
3251 break;
3252 }
3253
3254 if (abs(y - mCurrentTouch.pointers[j].y) <= jumpyEpsilon) {
3255 dropY = true;
3256 break;
3257 }
3258 }
3259 if (! dropX && ! dropY) {
3260 continue; // not jumpy
3261 }
3262
3263 // Find a replacement candidate by comparing with older points on the
3264 // complementary (non-jumpy) axis.
3265 int32_t distance = INT_MIN; // distance to be corrected
3266 int32_t replacementIndex = -1;
3267
3268 if (dropX) {
3269 // X looks too close. Find an older replacement point with a close Y.
3270 int32_t smallestDeltaY = INT_MAX;
3271 for (uint32_t j = 0; j < pointerCount; j++) {
3272 int32_t deltaY = abs(y - mLastTouch.pointers[j].y);
3273 if (deltaY < smallestDeltaY) {
3274 smallestDeltaY = deltaY;
3275 replacementIndex = j;
3276 }
3277 }
3278 distance = abs(x - mLastTouch.pointers[replacementIndex].x);
3279 } else {
3280 // Y looks too close. Find an older replacement point with a close X.
3281 int32_t smallestDeltaX = INT_MAX;
3282 for (uint32_t j = 0; j < pointerCount; j++) {
3283 int32_t deltaX = abs(x - mLastTouch.pointers[j].x);
3284 if (deltaX < smallestDeltaX) {
3285 smallestDeltaX = deltaX;
3286 replacementIndex = j;
3287 }
3288 }
3289 distance = abs(y - mLastTouch.pointers[replacementIndex].y);
3290 }
3291
3292 // If replacing this pointer would correct a worse error than the previous ones
3293 // considered, then use this replacement instead.
3294 if (distance > badPointerDistance) {
3295 badPointerIndex = i;
3296 badPointerReplacementIndex = replacementIndex;
3297 badPointerDistance = distance;
3298 }
3299 }
3300
3301 // Correct the jumpy pointer if one was found.
3302 if (badPointerIndex >= 0) {
3303#if DEBUG_HACKS
3304 LOGD("JumpyTouchFilter: Replacing bad pointer %d with (%d, %d)",
3305 badPointerIndex,
3306 mLastTouch.pointers[badPointerReplacementIndex].x,
3307 mLastTouch.pointers[badPointerReplacementIndex].y);
3308#endif
3309
3310 mCurrentTouch.pointers[badPointerIndex].x =
3311 mLastTouch.pointers[badPointerReplacementIndex].x;
3312 mCurrentTouch.pointers[badPointerIndex].y =
3313 mLastTouch.pointers[badPointerReplacementIndex].y;
3314 mJumpyTouchFilter.jumpyPointsDropped += 1;
3315 return true;
3316 }
3317 }
3318
3319 mJumpyTouchFilter.jumpyPointsDropped = 0;
3320 return false;
3321}
3322
3323/* Special hack for devices that have bad screen data: aggregate and
3324 * compute averages of the coordinate data, to reduce the amount of
3325 * jitter seen by applications. */
3326void TouchInputMapper::applyAveragingTouchFilter() {
3327 for (uint32_t currentIndex = 0; currentIndex < mCurrentTouch.pointerCount; currentIndex++) {
3328 uint32_t id = mCurrentTouch.pointers[currentIndex].id;
3329 int32_t x = mCurrentTouch.pointers[currentIndex].x;
3330 int32_t y = mCurrentTouch.pointers[currentIndex].y;
Jeff Brown8d608662010-08-30 03:02:23 -07003331 int32_t pressure;
3332 switch (mCalibration.pressureSource) {
3333 case Calibration::PRESSURE_SOURCE_PRESSURE:
3334 pressure = mCurrentTouch.pointers[currentIndex].pressure;
3335 break;
3336 case Calibration::PRESSURE_SOURCE_TOUCH:
3337 pressure = mCurrentTouch.pointers[currentIndex].touchMajor;
3338 break;
3339 default:
3340 pressure = 1;
3341 break;
3342 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003343
3344 if (mLastTouch.idBits.hasBit(id)) {
3345 // Pointer was down before and is still down now.
3346 // Compute average over history trace.
3347 uint32_t start = mAveragingTouchFilter.historyStart[id];
3348 uint32_t end = mAveragingTouchFilter.historyEnd[id];
3349
3350 int64_t deltaX = x - mAveragingTouchFilter.historyData[end].pointers[id].x;
3351 int64_t deltaY = y - mAveragingTouchFilter.historyData[end].pointers[id].y;
3352 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
3353
3354#if DEBUG_HACKS
3355 LOGD("AveragingTouchFilter: Pointer id %d - Distance from last sample: %lld",
3356 id, distance);
3357#endif
3358
3359 if (distance < AVERAGING_DISTANCE_LIMIT) {
3360 // Increment end index in preparation for recording new historical data.
3361 end += 1;
3362 if (end > AVERAGING_HISTORY_SIZE) {
3363 end = 0;
3364 }
3365
3366 // If the end index has looped back to the start index then we have filled
3367 // the historical trace up to the desired size so we drop the historical
3368 // data at the start of the trace.
3369 if (end == start) {
3370 start += 1;
3371 if (start > AVERAGING_HISTORY_SIZE) {
3372 start = 0;
3373 }
3374 }
3375
3376 // Add the raw data to the historical trace.
3377 mAveragingTouchFilter.historyStart[id] = start;
3378 mAveragingTouchFilter.historyEnd[id] = end;
3379 mAveragingTouchFilter.historyData[end].pointers[id].x = x;
3380 mAveragingTouchFilter.historyData[end].pointers[id].y = y;
3381 mAveragingTouchFilter.historyData[end].pointers[id].pressure = pressure;
3382
3383 // Average over all historical positions in the trace by total pressure.
3384 int32_t averagedX = 0;
3385 int32_t averagedY = 0;
3386 int32_t totalPressure = 0;
3387 for (;;) {
3388 int32_t historicalX = mAveragingTouchFilter.historyData[start].pointers[id].x;
3389 int32_t historicalY = mAveragingTouchFilter.historyData[start].pointers[id].y;
3390 int32_t historicalPressure = mAveragingTouchFilter.historyData[start]
3391 .pointers[id].pressure;
3392
3393 averagedX += historicalX * historicalPressure;
3394 averagedY += historicalY * historicalPressure;
3395 totalPressure += historicalPressure;
3396
3397 if (start == end) {
3398 break;
3399 }
3400
3401 start += 1;
3402 if (start > AVERAGING_HISTORY_SIZE) {
3403 start = 0;
3404 }
3405 }
3406
Jeff Brown8d608662010-08-30 03:02:23 -07003407 if (totalPressure != 0) {
3408 averagedX /= totalPressure;
3409 averagedY /= totalPressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003410
3411#if DEBUG_HACKS
Jeff Brown8d608662010-08-30 03:02:23 -07003412 LOGD("AveragingTouchFilter: Pointer id %d - "
3413 "totalPressure=%d, averagedX=%d, averagedY=%d", id, totalPressure,
3414 averagedX, averagedY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003415#endif
3416
Jeff Brown8d608662010-08-30 03:02:23 -07003417 mCurrentTouch.pointers[currentIndex].x = averagedX;
3418 mCurrentTouch.pointers[currentIndex].y = averagedY;
3419 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003420 } else {
3421#if DEBUG_HACKS
3422 LOGD("AveragingTouchFilter: Pointer id %d - Exceeded max distance", id);
3423#endif
3424 }
3425 } else {
3426#if DEBUG_HACKS
3427 LOGD("AveragingTouchFilter: Pointer id %d - Pointer went up", id);
3428#endif
3429 }
3430
3431 // Reset pointer history.
3432 mAveragingTouchFilter.historyStart[id] = 0;
3433 mAveragingTouchFilter.historyEnd[id] = 0;
3434 mAveragingTouchFilter.historyData[0].pointers[id].x = x;
3435 mAveragingTouchFilter.historyData[0].pointers[id].y = y;
3436 mAveragingTouchFilter.historyData[0].pointers[id].pressure = pressure;
3437 }
3438}
3439
3440int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003441 { // acquire lock
3442 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003443
Jeff Brown6328cdc2010-07-29 18:18:33 -07003444 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003445 return AKEY_STATE_VIRTUAL;
3446 }
3447
Jeff Brown6328cdc2010-07-29 18:18:33 -07003448 size_t numVirtualKeys = mLocked.virtualKeys.size();
3449 for (size_t i = 0; i < numVirtualKeys; i++) {
3450 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07003451 if (virtualKey.keyCode == keyCode) {
3452 return AKEY_STATE_UP;
3453 }
3454 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003455 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07003456
3457 return AKEY_STATE_UNKNOWN;
3458}
3459
3460int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003461 { // acquire lock
3462 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003463
Jeff Brown6328cdc2010-07-29 18:18:33 -07003464 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003465 return AKEY_STATE_VIRTUAL;
3466 }
3467
Jeff Brown6328cdc2010-07-29 18:18:33 -07003468 size_t numVirtualKeys = mLocked.virtualKeys.size();
3469 for (size_t i = 0; i < numVirtualKeys; i++) {
3470 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07003471 if (virtualKey.scanCode == scanCode) {
3472 return AKEY_STATE_UP;
3473 }
3474 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003475 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07003476
3477 return AKEY_STATE_UNKNOWN;
3478}
3479
3480bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
3481 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003482 { // acquire lock
3483 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003484
Jeff Brown6328cdc2010-07-29 18:18:33 -07003485 size_t numVirtualKeys = mLocked.virtualKeys.size();
3486 for (size_t i = 0; i < numVirtualKeys; i++) {
3487 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07003488
3489 for (size_t i = 0; i < numCodes; i++) {
3490 if (virtualKey.keyCode == keyCodes[i]) {
3491 outFlags[i] = 1;
3492 }
3493 }
3494 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003495 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07003496
3497 return true;
3498}
3499
3500
3501// --- SingleTouchInputMapper ---
3502
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003503SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
3504 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003505 initialize();
3506}
3507
3508SingleTouchInputMapper::~SingleTouchInputMapper() {
3509}
3510
3511void SingleTouchInputMapper::initialize() {
3512 mAccumulator.clear();
3513
3514 mDown = false;
3515 mX = 0;
3516 mY = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07003517 mPressure = 0; // default to 0 for devices that don't report pressure
3518 mToolWidth = 0; // default to 0 for devices that don't report tool width
Jeff Brown6d0fec22010-07-23 21:28:06 -07003519}
3520
3521void SingleTouchInputMapper::reset() {
3522 TouchInputMapper::reset();
3523
Jeff Brown6d0fec22010-07-23 21:28:06 -07003524 initialize();
3525 }
3526
3527void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
3528 switch (rawEvent->type) {
3529 case EV_KEY:
3530 switch (rawEvent->scanCode) {
3531 case BTN_TOUCH:
3532 mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH;
3533 mAccumulator.btnTouch = rawEvent->value != 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003534 // Don't sync immediately. Wait until the next SYN_REPORT since we might
3535 // not have received valid position information yet. This logic assumes that
3536 // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003537 break;
3538 }
3539 break;
3540
3541 case EV_ABS:
3542 switch (rawEvent->scanCode) {
3543 case ABS_X:
3544 mAccumulator.fields |= Accumulator::FIELD_ABS_X;
3545 mAccumulator.absX = rawEvent->value;
3546 break;
3547 case ABS_Y:
3548 mAccumulator.fields |= Accumulator::FIELD_ABS_Y;
3549 mAccumulator.absY = rawEvent->value;
3550 break;
3551 case ABS_PRESSURE:
3552 mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE;
3553 mAccumulator.absPressure = rawEvent->value;
3554 break;
3555 case ABS_TOOL_WIDTH:
3556 mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH;
3557 mAccumulator.absToolWidth = rawEvent->value;
3558 break;
3559 }
3560 break;
3561
3562 case EV_SYN:
3563 switch (rawEvent->scanCode) {
3564 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003565 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003566 break;
3567 }
3568 break;
3569 }
3570}
3571
3572void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003573 uint32_t fields = mAccumulator.fields;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003574 if (fields == 0) {
3575 return; // no new state changes, so nothing to do
3576 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003577
3578 if (fields & Accumulator::FIELD_BTN_TOUCH) {
3579 mDown = mAccumulator.btnTouch;
3580 }
3581
3582 if (fields & Accumulator::FIELD_ABS_X) {
3583 mX = mAccumulator.absX;
3584 }
3585
3586 if (fields & Accumulator::FIELD_ABS_Y) {
3587 mY = mAccumulator.absY;
3588 }
3589
3590 if (fields & Accumulator::FIELD_ABS_PRESSURE) {
3591 mPressure = mAccumulator.absPressure;
3592 }
3593
3594 if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) {
Jeff Brown8d608662010-08-30 03:02:23 -07003595 mToolWidth = mAccumulator.absToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003596 }
3597
3598 mCurrentTouch.clear();
3599
3600 if (mDown) {
3601 mCurrentTouch.pointerCount = 1;
3602 mCurrentTouch.pointers[0].id = 0;
3603 mCurrentTouch.pointers[0].x = mX;
3604 mCurrentTouch.pointers[0].y = mY;
3605 mCurrentTouch.pointers[0].pressure = mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -07003606 mCurrentTouch.pointers[0].touchMajor = 0;
3607 mCurrentTouch.pointers[0].touchMinor = 0;
3608 mCurrentTouch.pointers[0].toolMajor = mToolWidth;
3609 mCurrentTouch.pointers[0].toolMinor = mToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003610 mCurrentTouch.pointers[0].orientation = 0;
3611 mCurrentTouch.idToIndex[0] = 0;
3612 mCurrentTouch.idBits.markBit(0);
3613 }
3614
3615 syncTouch(when, true);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003616
3617 mAccumulator.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003618}
3619
Jeff Brown8d608662010-08-30 03:02:23 -07003620void SingleTouchInputMapper::configureRawAxes() {
3621 TouchInputMapper::configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003622
Jeff Brown8d608662010-08-30 03:02:23 -07003623 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x);
3624 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y);
3625 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure);
3626 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003627}
3628
3629
3630// --- MultiTouchInputMapper ---
3631
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003632MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
3633 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003634 initialize();
3635}
3636
3637MultiTouchInputMapper::~MultiTouchInputMapper() {
3638}
3639
3640void MultiTouchInputMapper::initialize() {
3641 mAccumulator.clear();
3642}
3643
3644void MultiTouchInputMapper::reset() {
3645 TouchInputMapper::reset();
3646
Jeff Brown6d0fec22010-07-23 21:28:06 -07003647 initialize();
3648}
3649
3650void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
3651 switch (rawEvent->type) {
3652 case EV_ABS: {
3653 uint32_t pointerIndex = mAccumulator.pointerCount;
3654 Accumulator::Pointer* pointer = & mAccumulator.pointers[pointerIndex];
3655
3656 switch (rawEvent->scanCode) {
3657 case ABS_MT_POSITION_X:
3658 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_X;
3659 pointer->absMTPositionX = rawEvent->value;
3660 break;
3661 case ABS_MT_POSITION_Y:
3662 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y;
3663 pointer->absMTPositionY = rawEvent->value;
3664 break;
3665 case ABS_MT_TOUCH_MAJOR:
3666 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR;
3667 pointer->absMTTouchMajor = rawEvent->value;
3668 break;
3669 case ABS_MT_TOUCH_MINOR:
3670 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR;
3671 pointer->absMTTouchMinor = rawEvent->value;
3672 break;
3673 case ABS_MT_WIDTH_MAJOR:
3674 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR;
3675 pointer->absMTWidthMajor = rawEvent->value;
3676 break;
3677 case ABS_MT_WIDTH_MINOR:
3678 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR;
3679 pointer->absMTWidthMinor = rawEvent->value;
3680 break;
3681 case ABS_MT_ORIENTATION:
3682 pointer->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION;
3683 pointer->absMTOrientation = rawEvent->value;
3684 break;
3685 case ABS_MT_TRACKING_ID:
3686 pointer->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID;
3687 pointer->absMTTrackingId = rawEvent->value;
3688 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003689 case ABS_MT_PRESSURE:
3690 pointer->fields |= Accumulator::FIELD_ABS_MT_PRESSURE;
3691 pointer->absMTPressure = rawEvent->value;
3692 break;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003693 }
3694 break;
3695 }
3696
3697 case EV_SYN:
3698 switch (rawEvent->scanCode) {
3699 case SYN_MT_REPORT: {
3700 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
3701 uint32_t pointerIndex = mAccumulator.pointerCount;
3702
3703 if (mAccumulator.pointers[pointerIndex].fields) {
3704 if (pointerIndex == MAX_POINTERS) {
3705 LOGW("MultiTouch device driver returned more than maximum of %d pointers.",
3706 MAX_POINTERS);
3707 } else {
3708 pointerIndex += 1;
3709 mAccumulator.pointerCount = pointerIndex;
3710 }
3711 }
3712
3713 mAccumulator.pointers[pointerIndex].clear();
3714 break;
3715 }
3716
3717 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003718 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003719 break;
3720 }
3721 break;
3722 }
3723}
3724
3725void MultiTouchInputMapper::sync(nsecs_t when) {
3726 static const uint32_t REQUIRED_FIELDS =
Jeff Brown8d608662010-08-30 03:02:23 -07003727 Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003728
Jeff Brown6d0fec22010-07-23 21:28:06 -07003729 uint32_t inCount = mAccumulator.pointerCount;
3730 uint32_t outCount = 0;
3731 bool havePointerIds = true;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003732
Jeff Brown6d0fec22010-07-23 21:28:06 -07003733 mCurrentTouch.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003734
Jeff Brown6d0fec22010-07-23 21:28:06 -07003735 for (uint32_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003736 const Accumulator::Pointer& inPointer = mAccumulator.pointers[inIndex];
3737 uint32_t fields = inPointer.fields;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003738
Jeff Brown6d0fec22010-07-23 21:28:06 -07003739 if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003740 // Some drivers send empty MT sync packets without X / Y to indicate a pointer up.
3741 // Drop this finger.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003742 continue;
3743 }
3744
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003745 PointerData& outPointer = mCurrentTouch.pointers[outCount];
3746 outPointer.x = inPointer.absMTPositionX;
3747 outPointer.y = inPointer.absMTPositionY;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003748
Jeff Brown8d608662010-08-30 03:02:23 -07003749 if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) {
3750 if (inPointer.absMTPressure <= 0) {
Jeff Brownc3db8582010-10-20 15:33:38 -07003751 // Some devices send sync packets with X / Y but with a 0 pressure to indicate
3752 // a pointer going up. Drop this finger.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003753 continue;
3754 }
Jeff Brown8d608662010-08-30 03:02:23 -07003755 outPointer.pressure = inPointer.absMTPressure;
3756 } else {
3757 // Default pressure to 0 if absent.
3758 outPointer.pressure = 0;
3759 }
3760
3761 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) {
3762 if (inPointer.absMTTouchMajor <= 0) {
3763 // Some devices send sync packets with X / Y but with a 0 touch major to indicate
3764 // a pointer going up. Drop this finger.
3765 continue;
3766 }
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003767 outPointer.touchMajor = inPointer.absMTTouchMajor;
3768 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003769 // Default touch area to 0 if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003770 outPointer.touchMajor = 0;
3771 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003772
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003773 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) {
3774 outPointer.touchMinor = inPointer.absMTTouchMinor;
3775 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003776 // Assume touch area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003777 outPointer.touchMinor = outPointer.touchMajor;
3778 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003779
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003780 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) {
3781 outPointer.toolMajor = inPointer.absMTWidthMajor;
3782 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003783 // Default tool area to 0 if absent.
3784 outPointer.toolMajor = 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003785 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003786
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003787 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) {
3788 outPointer.toolMinor = inPointer.absMTWidthMinor;
3789 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003790 // Assume tool area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003791 outPointer.toolMinor = outPointer.toolMajor;
3792 }
3793
3794 if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) {
3795 outPointer.orientation = inPointer.absMTOrientation;
3796 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07003797 // Default orientation to vertical if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003798 outPointer.orientation = 0;
3799 }
3800
Jeff Brown8d608662010-08-30 03:02:23 -07003801 // Assign pointer id using tracking id if available.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003802 if (havePointerIds) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003803 if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) {
3804 uint32_t id = uint32_t(inPointer.absMTTrackingId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003805
Jeff Brown6d0fec22010-07-23 21:28:06 -07003806 if (id > MAX_POINTER_ID) {
3807#if DEBUG_POINTERS
3808 LOGD("Pointers: Ignoring driver provided pointer id %d because "
Jeff Brown01ce2e92010-09-26 22:20:12 -07003809 "it is larger than max supported id %d",
Jeff Brown6d0fec22010-07-23 21:28:06 -07003810 id, MAX_POINTER_ID);
3811#endif
3812 havePointerIds = false;
3813 }
3814 else {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003815 outPointer.id = id;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003816 mCurrentTouch.idToIndex[id] = outCount;
3817 mCurrentTouch.idBits.markBit(id);
3818 }
3819 } else {
3820 havePointerIds = false;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003821 }
3822 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003823
Jeff Brown6d0fec22010-07-23 21:28:06 -07003824 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003825 }
3826
Jeff Brown6d0fec22010-07-23 21:28:06 -07003827 mCurrentTouch.pointerCount = outCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003828
Jeff Brown6d0fec22010-07-23 21:28:06 -07003829 syncTouch(when, havePointerIds);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07003830
3831 mAccumulator.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003832}
3833
Jeff Brown8d608662010-08-30 03:02:23 -07003834void MultiTouchInputMapper::configureRawAxes() {
3835 TouchInputMapper::configureRawAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003836
Jeff Brown8d608662010-08-30 03:02:23 -07003837 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, & mRawAxes.x);
3838 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, & mRawAxes.y);
3839 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, & mRawAxes.touchMajor);
3840 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, & mRawAxes.touchMinor);
3841 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, & mRawAxes.toolMajor);
3842 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, & mRawAxes.toolMinor);
3843 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, & mRawAxes.orientation);
3844 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, & mRawAxes.pressure);
Jeff Brown9c3cda02010-06-15 01:31:58 -07003845}
3846
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003847
Jeff Browncb1404e2011-01-15 18:14:15 -08003848// --- JoystickInputMapper ---
3849
3850JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
3851 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08003852}
3853
3854JoystickInputMapper::~JoystickInputMapper() {
3855}
3856
3857uint32_t JoystickInputMapper::getSources() {
3858 return AINPUT_SOURCE_JOYSTICK;
3859}
3860
3861void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
3862 InputMapper::populateDeviceInfo(info);
3863
Jeff Brown6f2fba42011-02-19 01:08:02 -08003864 for (size_t i = 0; i < mAxes.size(); i++) {
3865 const Axis& axis = mAxes.valueAt(i);
3866 info->addMotionRange(axis.axis, axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Browncb1404e2011-01-15 18:14:15 -08003867 }
3868}
3869
3870void JoystickInputMapper::dump(String8& dump) {
3871 dump.append(INDENT2 "Joystick Input Mapper:\n");
3872
Jeff Brown6f2fba42011-02-19 01:08:02 -08003873 dump.append(INDENT3 "Axes:\n");
3874 size_t numAxes = mAxes.size();
3875 for (size_t i = 0; i < numAxes; i++) {
3876 const Axis& axis = mAxes.valueAt(i);
3877 const char* label = getAxisLabel(axis.axis);
3878 char name[32];
3879 if (label) {
3880 strncpy(name, label, sizeof(name));
3881 name[sizeof(name) - 1] = '\0';
3882 } else {
3883 snprintf(name, sizeof(name), "%d", axis.axis);
3884 }
Jeff Browncb1404e2011-01-15 18:14:15 -08003885 dump.appendFormat(INDENT4 "%s: min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, "
Jeff Brown6f2fba42011-02-19 01:08:02 -08003886 "scale=%0.3f, offset=%0.3f\n",
Jeff Browncb1404e2011-01-15 18:14:15 -08003887 name, axis.min, axis.max, axis.flat, axis.fuzz,
Jeff Brown6f2fba42011-02-19 01:08:02 -08003888 axis.scale, axis.offset);
3889 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, rawFlat=%d, rawFuzz=%d\n",
3890 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
3891 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz);
Jeff Browncb1404e2011-01-15 18:14:15 -08003892 }
3893}
3894
3895void JoystickInputMapper::configure() {
3896 InputMapper::configure();
3897
Jeff Brown6f2fba42011-02-19 01:08:02 -08003898 // Collect all axes.
3899 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
3900 RawAbsoluteAxisInfo rawAxisInfo;
3901 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), abs, &rawAxisInfo);
3902 if (rawAxisInfo.valid) {
3903 int32_t axisId;
3904 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisId);
3905 if (!explicitlyMapped) {
3906 // Axis is not explicitly mapped, will choose a generic axis later.
3907 axisId = -1;
3908 }
Jeff Browncb1404e2011-01-15 18:14:15 -08003909
Jeff Brown6f2fba42011-02-19 01:08:02 -08003910 Axis axis;
3911 if (isCenteredAxis(axisId)) {
3912 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
3913 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
3914 axis.initialize(rawAxisInfo, axisId, explicitlyMapped,
3915 scale, offset, -1.0f, 1.0f,
3916 rawAxisInfo.flat * scale, rawAxisInfo.fuzz * scale);
3917 } else {
3918 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
3919 axis.initialize(rawAxisInfo, axisId, explicitlyMapped,
3920 scale, 0.0f, 0.0f, 1.0f,
3921 rawAxisInfo.flat * scale, rawAxisInfo.fuzz * scale);
3922 }
3923
3924 // To eliminate noise while the joystick is at rest, filter out small variations
3925 // in axis values up front.
3926 axis.filter = axis.flat * 0.25f;
3927
3928 mAxes.add(abs, axis);
3929 }
3930 }
3931
3932 // If there are too many axes, start dropping them.
3933 // Prefer to keep explicitly mapped axes.
3934 if (mAxes.size() > PointerCoords::MAX_AXES) {
3935 LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
3936 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
3937 pruneAxes(true);
3938 pruneAxes(false);
3939 }
3940
3941 // Assign generic axis ids to remaining axes.
3942 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
3943 size_t numAxes = mAxes.size();
3944 for (size_t i = 0; i < numAxes; i++) {
3945 Axis& axis = mAxes.editValueAt(i);
3946 if (axis.axis < 0) {
3947 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
3948 && haveAxis(nextGenericAxisId)) {
3949 nextGenericAxisId += 1;
3950 }
3951
3952 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
3953 axis.axis = nextGenericAxisId;
3954 nextGenericAxisId += 1;
3955 } else {
3956 LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
3957 "have already been assigned to other axes.",
3958 getDeviceName().string(), mAxes.keyAt(i));
3959 mAxes.removeItemsAt(i--);
3960 numAxes -= 1;
3961 }
3962 }
3963 }
Jeff Browncb1404e2011-01-15 18:14:15 -08003964}
3965
Jeff Brown6f2fba42011-02-19 01:08:02 -08003966bool JoystickInputMapper::haveAxis(int32_t axis) {
3967 size_t numAxes = mAxes.size();
3968 for (size_t i = 0; i < numAxes; i++) {
3969 if (mAxes.valueAt(i).axis == axis) {
3970 return true;
3971 }
3972 }
3973 return false;
3974}
Jeff Browncb1404e2011-01-15 18:14:15 -08003975
Jeff Brown6f2fba42011-02-19 01:08:02 -08003976void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
3977 size_t i = mAxes.size();
3978 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
3979 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
3980 continue;
3981 }
3982 LOGI("Discarding joystick '%s' axis %d because there are too many axes.",
3983 getDeviceName().string(), mAxes.keyAt(i));
3984 mAxes.removeItemsAt(i);
3985 }
3986}
3987
3988bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
3989 switch (axis) {
3990 case AMOTION_EVENT_AXIS_X:
3991 case AMOTION_EVENT_AXIS_Y:
3992 case AMOTION_EVENT_AXIS_Z:
3993 case AMOTION_EVENT_AXIS_RX:
3994 case AMOTION_EVENT_AXIS_RY:
3995 case AMOTION_EVENT_AXIS_RZ:
3996 case AMOTION_EVENT_AXIS_HAT_X:
3997 case AMOTION_EVENT_AXIS_HAT_Y:
3998 case AMOTION_EVENT_AXIS_ORIENTATION:
3999 return true;
4000 default:
4001 return false;
4002 }
Jeff Browncb1404e2011-01-15 18:14:15 -08004003}
4004
4005void JoystickInputMapper::reset() {
4006 // Recenter all axes.
4007 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Browncb1404e2011-01-15 18:14:15 -08004008
Jeff Brown6f2fba42011-02-19 01:08:02 -08004009 size_t numAxes = mAxes.size();
4010 for (size_t i = 0; i < numAxes; i++) {
4011 Axis& axis = mAxes.editValueAt(i);
4012 axis.newValue = 0;
4013 }
4014
4015 sync(when, true /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08004016
4017 InputMapper::reset();
4018}
4019
4020void JoystickInputMapper::process(const RawEvent* rawEvent) {
4021 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08004022 case EV_ABS: {
4023 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
4024 if (index >= 0) {
4025 Axis& axis = mAxes.editValueAt(index);
4026 float newValue = rawEvent->value * axis.scale + axis.offset;
4027 if (newValue != axis.newValue) {
4028 axis.newValue = newValue;
4029 }
Jeff Browncb1404e2011-01-15 18:14:15 -08004030 }
4031 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08004032 }
Jeff Browncb1404e2011-01-15 18:14:15 -08004033
4034 case EV_SYN:
4035 switch (rawEvent->scanCode) {
4036 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08004037 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08004038 break;
4039 }
4040 break;
4041 }
4042}
4043
Jeff Brown6f2fba42011-02-19 01:08:02 -08004044void JoystickInputMapper::sync(nsecs_t when, bool force) {
4045 if (!force && !haveAxesChangedSignificantly()) {
4046 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08004047 }
4048
4049 int32_t metaState = mContext->getGlobalMetaState();
4050
Jeff Brown6f2fba42011-02-19 01:08:02 -08004051 PointerCoords pointerCoords;
4052 pointerCoords.clear();
4053
4054 size_t numAxes = mAxes.size();
4055 for (size_t i = 0; i < numAxes; i++) {
4056 Axis& axis = mAxes.editValueAt(i);
4057 pointerCoords.setAxisValue(axis.axis, axis.newValue);
4058 axis.oldValue = axis.newValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08004059 }
4060
Jeff Brown56194eb2011-03-02 19:23:13 -08004061 // Moving a joystick axis should not wake the devide because joysticks can
4062 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
4063 // button will likely wake the device.
4064 // TODO: Use the input device configuration to control this behavior more finely.
4065 uint32_t policyFlags = 0;
4066
Jeff Brown6f2fba42011-02-19 01:08:02 -08004067 int32_t pointerId = 0;
Jeff Brown56194eb2011-03-02 19:23:13 -08004068 getDispatcher()->notifyMotion(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brown6f2fba42011-02-19 01:08:02 -08004069 AMOTION_EVENT_ACTION_MOVE, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE,
4070 1, &pointerId, &pointerCoords, 0, 0, 0);
Jeff Browncb1404e2011-01-15 18:14:15 -08004071}
4072
Jeff Brown6f2fba42011-02-19 01:08:02 -08004073bool JoystickInputMapper::haveAxesChangedSignificantly() {
4074 size_t numAxes = mAxes.size();
4075 for (size_t i = 0; i < numAxes; i++) {
4076 const Axis& axis = mAxes.valueAt(i);
4077 if (axis.newValue != axis.oldValue
4078 && fabs(axis.newValue - axis.oldValue) > axis.filter) {
4079 return true;
4080 }
Jeff Browncb1404e2011-01-15 18:14:15 -08004081 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08004082 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08004083}
4084
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004085} // namespace android