blob: 724af39e998b501c0fc452c31e256d92dd1890f8 [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Brown46b9ac02010-04-22 18:58:52 -070017#define LOG_TAG "InputReader"
18
19//#define LOG_NDEBUG 0
20
21// Log debug messages for each raw event received from the EventHub.
22#define DEBUG_RAW_EVENTS 0
23
24// Log debug messages about touch screen filtering hacks.
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_HACKS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070026
27// Log debug messages about virtual key processing.
Jeff Brown349703e2010-06-22 01:27:15 -070028#define DEBUG_VIRTUAL_KEYS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070029
30// Log debug messages about pointers.
Jeff Brown349703e2010-06-22 01:27:15 -070031#define DEBUG_POINTERS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070032
Jeff Brown5c225b12010-06-16 01:53:36 -070033// Log debug messages about pointer assignment calculations.
34#define DEBUG_POINTER_ASSIGNMENT 0
35
Jeff Brown96ad3972011-03-09 17:39:48 -080036// Log debug messages about gesture detection.
37#define DEBUG_GESTURES 0
38
Jeff Brownb4ff35d2011-01-02 16:37:43 -080039#include "InputReader.h"
40
Jeff Brown46b9ac02010-04-22 18:58:52 -070041#include <cutils/log.h>
Jeff Brown6b53e8d2010-11-10 16:03:06 -080042#include <ui/Keyboard.h>
Jeff Brown90655042010-12-02 13:50:46 -080043#include <ui/VirtualKeyMap.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070044
45#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070046#include <stdlib.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070047#include <unistd.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070048#include <errno.h>
49#include <limits.h>
Jeff Brownc5ed5912010-07-14 18:48:53 -070050#include <math.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070051
Jeff Brown8d608662010-08-30 03:02:23 -070052#define INDENT " "
Jeff Brownef3d7e82010-09-30 14:33:04 -070053#define INDENT2 " "
54#define INDENT3 " "
55#define INDENT4 " "
Jeff Brown8d608662010-08-30 03:02:23 -070056
Jeff Brown46b9ac02010-04-22 18:58:52 -070057namespace android {
58
59// --- Static Functions ---
60
61template<typename T>
62inline static T abs(const T& value) {
63 return value < 0 ? - value : value;
64}
65
66template<typename T>
67inline static T min(const T& a, const T& b) {
68 return a < b ? a : b;
69}
70
Jeff Brown5c225b12010-06-16 01:53:36 -070071template<typename T>
72inline static void swap(T& a, T& b) {
73 T temp = a;
74 a = b;
75 b = temp;
76}
77
Jeff Brown8d608662010-08-30 03:02:23 -070078inline static float avg(float x, float y) {
79 return (x + y) / 2;
80}
81
Jeff Brown86ea1f52011-04-12 22:39:53 -070082inline static float distance(float x1, float y1, float x2, float y2) {
83 return hypotf(x1 - x2, y1 - y2);
Jeff Brown96ad3972011-03-09 17:39:48 -080084}
85
Jeff Brown517bb4c2011-01-14 19:09:23 -080086inline static int32_t signExtendNybble(int32_t value) {
87 return value >= 8 ? value - 16 : value;
88}
89
Jeff Brownef3d7e82010-09-30 14:33:04 -070090static inline const char* toString(bool value) {
91 return value ? "true" : "false";
92}
93
Jeff Brown9626b142011-03-03 02:09:54 -080094static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
95 const int32_t map[][4], size_t mapSize) {
96 if (orientation != DISPLAY_ORIENTATION_0) {
97 for (size_t i = 0; i < mapSize; i++) {
98 if (value == map[i][0]) {
99 return map[i][orientation];
100 }
101 }
102 }
103 return value;
104}
105
Jeff Brown46b9ac02010-04-22 18:58:52 -0700106static const int32_t keyCodeRotationMap[][4] = {
107 // key codes enumerated counter-clockwise with the original (unrotated) key first
108 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd035822010-06-30 16:10:35 -0700109 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
110 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
111 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
112 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac02010-04-22 18:58:52 -0700113};
Jeff Brown9626b142011-03-03 02:09:54 -0800114static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac02010-04-22 18:58:52 -0700115 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
116
117int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brown9626b142011-03-03 02:09:54 -0800118 return rotateValueUsingRotationMap(keyCode, orientation,
119 keyCodeRotationMap, keyCodeRotationMapSize);
120}
121
122static const int32_t edgeFlagRotationMap[][4] = {
123 // edge flags enumerated counter-clockwise with the original (unrotated) edge flag first
124 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
125 { AMOTION_EVENT_EDGE_FLAG_BOTTOM, AMOTION_EVENT_EDGE_FLAG_RIGHT,
126 AMOTION_EVENT_EDGE_FLAG_TOP, AMOTION_EVENT_EDGE_FLAG_LEFT },
127 { AMOTION_EVENT_EDGE_FLAG_RIGHT, AMOTION_EVENT_EDGE_FLAG_TOP,
128 AMOTION_EVENT_EDGE_FLAG_LEFT, AMOTION_EVENT_EDGE_FLAG_BOTTOM },
129 { AMOTION_EVENT_EDGE_FLAG_TOP, AMOTION_EVENT_EDGE_FLAG_LEFT,
130 AMOTION_EVENT_EDGE_FLAG_BOTTOM, AMOTION_EVENT_EDGE_FLAG_RIGHT },
131 { AMOTION_EVENT_EDGE_FLAG_LEFT, AMOTION_EVENT_EDGE_FLAG_BOTTOM,
132 AMOTION_EVENT_EDGE_FLAG_RIGHT, AMOTION_EVENT_EDGE_FLAG_TOP },
133};
134static const size_t edgeFlagRotationMapSize =
135 sizeof(edgeFlagRotationMap) / sizeof(edgeFlagRotationMap[0]);
136
137static int32_t rotateEdgeFlag(int32_t edgeFlag, int32_t orientation) {
138 return rotateValueUsingRotationMap(edgeFlag, orientation,
139 edgeFlagRotationMap, edgeFlagRotationMapSize);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700140}
141
Jeff Brown6d0fec22010-07-23 21:28:06 -0700142static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
143 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
144}
145
Jeff Brownefd32662011-03-08 15:13:06 -0800146static uint32_t getButtonStateForScanCode(int32_t scanCode) {
147 // Currently all buttons are mapped to the primary button.
148 switch (scanCode) {
149 case BTN_LEFT:
150 case BTN_RIGHT:
151 case BTN_MIDDLE:
152 case BTN_SIDE:
153 case BTN_EXTRA:
154 case BTN_FORWARD:
155 case BTN_BACK:
156 case BTN_TASK:
157 return BUTTON_STATE_PRIMARY;
158 default:
159 return 0;
160 }
161}
162
163// Returns true if the pointer should be reported as being down given the specified
164// button states.
165static bool isPointerDown(uint32_t buttonState) {
166 return buttonState & BUTTON_STATE_PRIMARY;
167}
168
169static int32_t calculateEdgeFlagsUsingPointerBounds(
170 const sp<PointerControllerInterface>& pointerController, float x, float y) {
171 int32_t edgeFlags = 0;
172 float minX, minY, maxX, maxY;
173 if (pointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
174 if (x <= minX) {
175 edgeFlags |= AMOTION_EVENT_EDGE_FLAG_LEFT;
176 } else if (x >= maxX) {
177 edgeFlags |= AMOTION_EVENT_EDGE_FLAG_RIGHT;
178 }
179 if (y <= minY) {
180 edgeFlags |= AMOTION_EVENT_EDGE_FLAG_TOP;
181 } else if (y >= maxY) {
182 edgeFlags |= AMOTION_EVENT_EDGE_FLAG_BOTTOM;
183 }
184 }
185 return edgeFlags;
186}
187
Jeff Brown86ea1f52011-04-12 22:39:53 -0700188static void clampPositionUsingPointerBounds(
189 const sp<PointerControllerInterface>& pointerController, float* x, float* y) {
190 float minX, minY, maxX, maxY;
191 if (pointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
192 if (*x < minX) {
193 *x = minX;
194 } else if (*x > maxX) {
195 *x = maxX;
196 }
197 if (*y < minY) {
198 *y = minY;
199 } else if (*y > maxY) {
200 *y = maxY;
201 }
202 }
203}
204
205static float calculateCommonVector(float a, float b) {
206 if (a > 0 && b > 0) {
207 return a < b ? a : b;
208 } else if (a < 0 && b < 0) {
209 return a > b ? a : b;
210 } else {
211 return 0;
212 }
213}
214
Jeff Brown46b9ac02010-04-22 18:58:52 -0700215
Jeff Brown46b9ac02010-04-22 18:58:52 -0700216// --- InputReader ---
217
218InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700219 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700220 const sp<InputDispatcherInterface>& dispatcher) :
Jeff Brown6d0fec22010-07-23 21:28:06 -0700221 mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher),
Jeff Brown68d60752011-03-17 01:34:19 -0700222 mGlobalMetaState(0), mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX) {
Jeff Brown214eaf42011-05-26 19:17:02 -0700223 mPolicy->getReaderConfiguration(&mConfig);
224
Jeff Brown9c3cda02010-06-15 01:31:58 -0700225 configureExcludedDevices();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700226 updateGlobalMetaState();
227 updateInputConfiguration();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700228}
229
230InputReader::~InputReader() {
231 for (size_t i = 0; i < mDevices.size(); i++) {
232 delete mDevices.valueAt(i);
233 }
234}
235
236void InputReader::loopOnce() {
Jeff Brown68d60752011-03-17 01:34:19 -0700237 int32_t timeoutMillis = -1;
238 if (mNextTimeout != LLONG_MAX) {
239 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
240 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
241 }
242
Jeff Browndbf8d272011-03-18 18:14:26 -0700243 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
244 if (count) {
245 processEvents(mEventBuffer, count);
246 }
247 if (!count || timeoutMillis == 0) {
Jeff Brown68d60752011-03-17 01:34:19 -0700248 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
249#if DEBUG_RAW_EVENTS
250 LOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
251#endif
252 mNextTimeout = LLONG_MAX;
253 timeoutExpired(now);
254 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700255}
256
Jeff Browndbf8d272011-03-18 18:14:26 -0700257void InputReader::processEvents(const RawEvent* rawEvents, size_t count) {
258 for (const RawEvent* rawEvent = rawEvents; count;) {
259 int32_t type = rawEvent->type;
260 size_t batchSize = 1;
261 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
262 int32_t deviceId = rawEvent->deviceId;
263 while (batchSize < count) {
264 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
265 || rawEvent[batchSize].deviceId != deviceId) {
266 break;
267 }
268 batchSize += 1;
269 }
270#if DEBUG_RAW_EVENTS
271 LOGD("BatchSize: %d Count: %d", batchSize, count);
272#endif
273 processEventsForDevice(deviceId, rawEvent, batchSize);
274 } else {
275 switch (rawEvent->type) {
276 case EventHubInterface::DEVICE_ADDED:
277 addDevice(rawEvent->deviceId);
278 break;
279 case EventHubInterface::DEVICE_REMOVED:
280 removeDevice(rawEvent->deviceId);
281 break;
282 case EventHubInterface::FINISHED_DEVICE_SCAN:
283 handleConfigurationChanged(rawEvent->when);
284 break;
285 default:
286 assert(false); // can't happen
287 break;
288 }
289 }
290 count -= batchSize;
291 rawEvent += batchSize;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700292 }
293}
294
Jeff Brown7342bb92010-10-01 18:55:43 -0700295void InputReader::addDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700296 String8 name = mEventHub->getDeviceName(deviceId);
297 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
298
299 InputDevice* device = createDevice(deviceId, name, classes);
300 device->configure();
301
Jeff Brown8d608662010-08-30 03:02:23 -0700302 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800303 LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700304 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800305 LOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700306 device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700307 }
308
Jeff Brown6d0fec22010-07-23 21:28:06 -0700309 bool added = false;
310 { // acquire device registry writer lock
311 RWLock::AutoWLock _wl(mDeviceRegistryLock);
312
313 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
314 if (deviceIndex < 0) {
315 mDevices.add(deviceId, device);
316 added = true;
317 }
318 } // release device registry writer lock
319
320 if (! added) {
321 LOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
322 delete device;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700323 return;
324 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700325}
326
Jeff Brown7342bb92010-10-01 18:55:43 -0700327void InputReader::removeDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700328 bool removed = false;
329 InputDevice* device = NULL;
330 { // acquire device registry writer lock
331 RWLock::AutoWLock _wl(mDeviceRegistryLock);
332
333 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
334 if (deviceIndex >= 0) {
335 device = mDevices.valueAt(deviceIndex);
336 mDevices.removeItemsAt(deviceIndex, 1);
337 removed = true;
338 }
339 } // release device registry writer lock
340
341 if (! removed) {
342 LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700343 return;
344 }
345
Jeff Brown6d0fec22010-07-23 21:28:06 -0700346 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800347 LOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700348 device->getId(), device->getName().string());
349 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800350 LOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700351 device->getId(), device->getName().string(), device->getSources());
352 }
353
Jeff Brown8d608662010-08-30 03:02:23 -0700354 device->reset();
355
Jeff Brown6d0fec22010-07-23 21:28:06 -0700356 delete device;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700357}
358
Jeff Brown6d0fec22010-07-23 21:28:06 -0700359InputDevice* InputReader::createDevice(int32_t deviceId, const String8& name, uint32_t classes) {
360 InputDevice* device = new InputDevice(this, deviceId, name);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700361
Jeff Brown56194eb2011-03-02 19:23:13 -0800362 // External devices.
363 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
364 device->setExternal(true);
365 }
366
Jeff Brown6d0fec22010-07-23 21:28:06 -0700367 // Switch-like devices.
368 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
369 device->addMapper(new SwitchInputMapper(device));
370 }
371
372 // Keyboard-like devices.
Jeff Brownefd32662011-03-08 15:13:06 -0800373 uint32_t keyboardSource = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700374 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
375 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800376 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700377 }
378 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
379 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
380 }
381 if (classes & INPUT_DEVICE_CLASS_DPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800382 keyboardSource |= AINPUT_SOURCE_DPAD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700383 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800384 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800385 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
Jeff Browncb1404e2011-01-15 18:14:15 -0800386 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700387
Jeff Brownefd32662011-03-08 15:13:06 -0800388 if (keyboardSource != 0) {
389 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700390 }
391
Jeff Brown83c09682010-12-23 17:50:18 -0800392 // Cursor-like devices.
393 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
394 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700395 }
396
Jeff Brown58a2da82011-01-25 16:02:22 -0800397 // Touchscreens and touchpad devices.
398 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800399 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800400 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800401 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700402 }
403
Jeff Browncb1404e2011-01-15 18:14:15 -0800404 // Joystick-like devices.
405 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
406 device->addMapper(new JoystickInputMapper(device));
407 }
408
Jeff Brown6d0fec22010-07-23 21:28:06 -0700409 return device;
410}
411
Jeff Browndbf8d272011-03-18 18:14:26 -0700412void InputReader::processEventsForDevice(int32_t deviceId,
413 const RawEvent* rawEvents, size_t count) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700414 { // acquire device registry reader lock
415 RWLock::AutoRLock _rl(mDeviceRegistryLock);
416
417 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
418 if (deviceIndex < 0) {
419 LOGW("Discarding event for unknown deviceId %d.", deviceId);
420 return;
421 }
422
423 InputDevice* device = mDevices.valueAt(deviceIndex);
424 if (device->isIgnored()) {
425 //LOGD("Discarding event for ignored deviceId %d.", deviceId);
426 return;
427 }
428
Jeff Browndbf8d272011-03-18 18:14:26 -0700429 device->process(rawEvents, count);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700430 } // release device registry reader lock
431}
432
Jeff Brown68d60752011-03-17 01:34:19 -0700433void InputReader::timeoutExpired(nsecs_t when) {
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 if (!device->isIgnored()) {
440 device->timeoutExpired(when);
441 }
442 }
443 } // release device registry reader lock
444}
445
Jeff Brownc3db8582010-10-20 15:33:38 -0700446void InputReader::handleConfigurationChanged(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700447 // Reset global meta state because it depends on the list of all configured devices.
448 updateGlobalMetaState();
449
450 // Update input configuration.
451 updateInputConfiguration();
452
453 // Enqueue configuration changed.
454 mDispatcher->notifyConfigurationChanged(when);
455}
456
457void InputReader::configureExcludedDevices() {
Jeff Brown214eaf42011-05-26 19:17:02 -0700458 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
459 mEventHub->addExcludedDevice(mConfig.excludedDeviceNames[i]);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700460 }
461}
462
463void InputReader::updateGlobalMetaState() {
464 { // acquire state lock
465 AutoMutex _l(mStateLock);
466
467 mGlobalMetaState = 0;
468
469 { // acquire device registry reader lock
470 RWLock::AutoRLock _rl(mDeviceRegistryLock);
471
472 for (size_t i = 0; i < mDevices.size(); i++) {
473 InputDevice* device = mDevices.valueAt(i);
474 mGlobalMetaState |= device->getMetaState();
475 }
476 } // release device registry reader lock
477 } // release state lock
478}
479
480int32_t InputReader::getGlobalMetaState() {
481 { // acquire state lock
482 AutoMutex _l(mStateLock);
483
484 return mGlobalMetaState;
485 } // release state lock
486}
487
488void InputReader::updateInputConfiguration() {
489 { // acquire state lock
490 AutoMutex _l(mStateLock);
491
492 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
493 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
494 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
495 { // acquire device registry reader lock
496 RWLock::AutoRLock _rl(mDeviceRegistryLock);
497
498 InputDeviceInfo deviceInfo;
499 for (size_t i = 0; i < mDevices.size(); i++) {
500 InputDevice* device = mDevices.valueAt(i);
501 device->getDeviceInfo(& deviceInfo);
502 uint32_t sources = deviceInfo.getSources();
503
504 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
505 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
506 }
507 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
508 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
509 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
510 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
511 }
512 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
513 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700514 }
515 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700516 } // release device registry reader lock
Jeff Brown46b9ac02010-04-22 18:58:52 -0700517
Jeff Brown6d0fec22010-07-23 21:28:06 -0700518 mInputConfiguration.touchScreen = touchScreenConfig;
519 mInputConfiguration.keyboard = keyboardConfig;
520 mInputConfiguration.navigation = navigationConfig;
521 } // release state lock
522}
523
Jeff Brownfe508922011-01-18 15:10:10 -0800524void InputReader::disableVirtualKeysUntil(nsecs_t time) {
525 mDisableVirtualKeysTimeout = time;
526}
527
528bool InputReader::shouldDropVirtualKey(nsecs_t now,
529 InputDevice* device, int32_t keyCode, int32_t scanCode) {
530 if (now < mDisableVirtualKeysTimeout) {
531 LOGI("Dropping virtual key from device %s because virtual keys are "
532 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
533 device->getName().string(),
534 (mDisableVirtualKeysTimeout - now) * 0.000001,
535 keyCode, scanCode);
536 return true;
537 } else {
538 return false;
539 }
540}
541
Jeff Brown05dc66a2011-03-02 14:41:58 -0800542void InputReader::fadePointer() {
543 { // acquire device registry reader lock
544 RWLock::AutoRLock _rl(mDeviceRegistryLock);
545
546 for (size_t i = 0; i < mDevices.size(); i++) {
547 InputDevice* device = mDevices.valueAt(i);
548 device->fadePointer();
549 }
550 } // release device registry reader lock
551}
552
Jeff Brown68d60752011-03-17 01:34:19 -0700553void InputReader::requestTimeoutAtTime(nsecs_t when) {
554 if (when < mNextTimeout) {
555 mNextTimeout = when;
556 }
557}
558
Jeff Brown6d0fec22010-07-23 21:28:06 -0700559void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
560 { // acquire state lock
561 AutoMutex _l(mStateLock);
562
563 *outConfiguration = mInputConfiguration;
564 } // release state lock
565}
566
567status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
568 { // acquire device registry reader lock
569 RWLock::AutoRLock _rl(mDeviceRegistryLock);
570
571 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
572 if (deviceIndex < 0) {
573 return NAME_NOT_FOUND;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700574 }
575
Jeff Brown6d0fec22010-07-23 21:28:06 -0700576 InputDevice* device = mDevices.valueAt(deviceIndex);
577 if (device->isIgnored()) {
578 return NAME_NOT_FOUND;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700579 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700580
581 device->getDeviceInfo(outDeviceInfo);
582 return OK;
583 } // release device registy reader lock
584}
585
586void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
587 outDeviceIds.clear();
588
589 { // acquire device registry reader lock
590 RWLock::AutoRLock _rl(mDeviceRegistryLock);
591
592 size_t numDevices = mDevices.size();
593 for (size_t i = 0; i < numDevices; i++) {
594 InputDevice* device = mDevices.valueAt(i);
595 if (! device->isIgnored()) {
596 outDeviceIds.add(device->getId());
597 }
598 }
599 } // release device registy reader lock
600}
601
602int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
603 int32_t keyCode) {
604 return getState(deviceId, sourceMask, keyCode, & InputDevice::getKeyCodeState);
605}
606
607int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
608 int32_t scanCode) {
609 return getState(deviceId, sourceMask, scanCode, & InputDevice::getScanCodeState);
610}
611
612int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
613 return getState(deviceId, sourceMask, switchCode, & InputDevice::getSwitchState);
614}
615
616int32_t InputReader::getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
617 GetStateFunc getStateFunc) {
618 { // acquire device registry reader lock
619 RWLock::AutoRLock _rl(mDeviceRegistryLock);
620
621 int32_t result = AKEY_STATE_UNKNOWN;
622 if (deviceId >= 0) {
623 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
624 if (deviceIndex >= 0) {
625 InputDevice* device = mDevices.valueAt(deviceIndex);
626 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
627 result = (device->*getStateFunc)(sourceMask, code);
628 }
629 }
630 } else {
631 size_t numDevices = mDevices.size();
632 for (size_t i = 0; i < numDevices; i++) {
633 InputDevice* device = mDevices.valueAt(i);
634 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
635 result = (device->*getStateFunc)(sourceMask, code);
636 if (result >= AKEY_STATE_DOWN) {
637 return result;
638 }
639 }
640 }
641 }
642 return result;
643 } // release device registy reader lock
644}
645
646bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
647 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
648 memset(outFlags, 0, numCodes);
649 return markSupportedKeyCodes(deviceId, sourceMask, numCodes, keyCodes, outFlags);
650}
651
652bool InputReader::markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
653 const int32_t* keyCodes, uint8_t* outFlags) {
654 { // acquire device registry reader lock
655 RWLock::AutoRLock _rl(mDeviceRegistryLock);
656 bool result = false;
657 if (deviceId >= 0) {
658 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
659 if (deviceIndex >= 0) {
660 InputDevice* device = mDevices.valueAt(deviceIndex);
661 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
662 result = device->markSupportedKeyCodes(sourceMask,
663 numCodes, keyCodes, outFlags);
664 }
665 }
666 } else {
667 size_t numDevices = mDevices.size();
668 for (size_t i = 0; i < numDevices; i++) {
669 InputDevice* device = mDevices.valueAt(i);
670 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
671 result |= device->markSupportedKeyCodes(sourceMask,
672 numCodes, keyCodes, outFlags);
673 }
674 }
675 }
676 return result;
677 } // release device registy reader lock
678}
679
Jeff Brownb88102f2010-09-08 11:49:43 -0700680void InputReader::dump(String8& dump) {
Jeff Brownf2f48712010-10-01 17:46:21 -0700681 mEventHub->dump(dump);
682 dump.append("\n");
683
684 dump.append("Input Reader State:\n");
685
Jeff Brownef3d7e82010-09-30 14:33:04 -0700686 { // acquire device registry reader lock
687 RWLock::AutoRLock _rl(mDeviceRegistryLock);
Jeff Brownb88102f2010-09-08 11:49:43 -0700688
Jeff Brownef3d7e82010-09-30 14:33:04 -0700689 for (size_t i = 0; i < mDevices.size(); i++) {
690 mDevices.valueAt(i)->dump(dump);
Jeff Brownb88102f2010-09-08 11:49:43 -0700691 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700692 } // release device registy reader lock
Jeff Brown214eaf42011-05-26 19:17:02 -0700693
694 dump.append(INDENT "Configuration:\n");
695 dump.append(INDENT2 "ExcludedDeviceNames: [");
696 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
697 if (i != 0) {
698 dump.append(", ");
699 }
700 dump.append(mConfig.excludedDeviceNames.itemAt(i).string());
701 }
702 dump.append("]\n");
703 dump.appendFormat(INDENT2 "FilterTouchEvents: %s\n",
704 toString(mConfig.filterTouchEvents));
705 dump.appendFormat(INDENT2 "FilterJumpyTouchEvents: %s\n",
706 toString(mConfig.filterJumpyTouchEvents));
707 dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
708 mConfig.virtualKeyQuietTime * 0.000001f);
709
Jeff Brown19c97d462011-06-01 12:33:19 -0700710 dump.appendFormat(INDENT2 "PointerVelocityControlParameters: "
711 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
712 mConfig.pointerVelocityControlParameters.scale,
713 mConfig.pointerVelocityControlParameters.lowThreshold,
714 mConfig.pointerVelocityControlParameters.highThreshold,
715 mConfig.pointerVelocityControlParameters.acceleration);
716
717 dump.appendFormat(INDENT2 "WheelVelocityControlParameters: "
718 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
719 mConfig.wheelVelocityControlParameters.scale,
720 mConfig.wheelVelocityControlParameters.lowThreshold,
721 mConfig.wheelVelocityControlParameters.highThreshold,
722 mConfig.wheelVelocityControlParameters.acceleration);
723
Jeff Brown214eaf42011-05-26 19:17:02 -0700724 dump.appendFormat(INDENT2 "PointerGesture:\n");
725 dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n",
726 mConfig.pointerGestureQuietInterval * 0.000001f);
727 dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
728 mConfig.pointerGestureDragMinSwitchSpeed);
729 dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n",
730 mConfig.pointerGestureTapInterval * 0.000001f);
731 dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n",
732 mConfig.pointerGestureTapDragInterval * 0.000001f);
733 dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n",
734 mConfig.pointerGestureTapSlop);
735 dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
736 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
737 dump.appendFormat(INDENT3 "MultitouchMinSpeed: %0.1fpx/s\n",
738 mConfig.pointerGestureMultitouchMinSpeed);
739 dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
740 mConfig.pointerGestureSwipeTransitionAngleCosine);
741 dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
742 mConfig.pointerGestureSwipeMaxWidthRatio);
743 dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n",
744 mConfig.pointerGestureMovementSpeedRatio);
745 dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n",
746 mConfig.pointerGestureZoomSpeedRatio);
Jeff Brownb88102f2010-09-08 11:49:43 -0700747}
748
Jeff Brown6d0fec22010-07-23 21:28:06 -0700749
750// --- InputReaderThread ---
751
752InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
753 Thread(/*canCallJava*/ true), mReader(reader) {
754}
755
756InputReaderThread::~InputReaderThread() {
757}
758
759bool InputReaderThread::threadLoop() {
760 mReader->loopOnce();
761 return true;
762}
763
764
765// --- InputDevice ---
766
767InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) :
Jeff Brown56194eb2011-03-02 19:23:13 -0800768 mContext(context), mId(id), mName(name), mSources(0), mIsExternal(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700769}
770
771InputDevice::~InputDevice() {
772 size_t numMappers = mMappers.size();
773 for (size_t i = 0; i < numMappers; i++) {
774 delete mMappers[i];
775 }
776 mMappers.clear();
777}
778
Jeff Brownef3d7e82010-09-30 14:33:04 -0700779void InputDevice::dump(String8& dump) {
780 InputDeviceInfo deviceInfo;
781 getDeviceInfo(& deviceInfo);
782
Jeff Brown90655042010-12-02 13:50:46 -0800783 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700784 deviceInfo.getName().string());
Jeff Brown56194eb2011-03-02 19:23:13 -0800785 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700786 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
787 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800788
Jeff Brownefd32662011-03-08 15:13:06 -0800789 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800790 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700791 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800792 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800793 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
794 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800795 char name[32];
796 if (label) {
797 strncpy(name, label, sizeof(name));
798 name[sizeof(name) - 1] = '\0';
799 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800800 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800801 }
Jeff Brownefd32662011-03-08 15:13:06 -0800802 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
803 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
804 name, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Browncc0c1592011-02-19 05:07:28 -0800805 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700806 }
807
808 size_t numMappers = mMappers.size();
809 for (size_t i = 0; i < numMappers; i++) {
810 InputMapper* mapper = mMappers[i];
811 mapper->dump(dump);
812 }
813}
814
Jeff Brown6d0fec22010-07-23 21:28:06 -0700815void InputDevice::addMapper(InputMapper* mapper) {
816 mMappers.add(mapper);
817}
818
819void InputDevice::configure() {
Jeff Brown8d608662010-08-30 03:02:23 -0700820 if (! isIgnored()) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800821 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
Jeff Brown8d608662010-08-30 03:02:23 -0700822 }
823
Jeff Brown6d0fec22010-07-23 21:28:06 -0700824 mSources = 0;
825
826 size_t numMappers = mMappers.size();
827 for (size_t i = 0; i < numMappers; i++) {
828 InputMapper* mapper = mMappers[i];
829 mapper->configure();
830 mSources |= mapper->getSources();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700831 }
832}
833
Jeff Brown6d0fec22010-07-23 21:28:06 -0700834void InputDevice::reset() {
835 size_t numMappers = mMappers.size();
836 for (size_t i = 0; i < numMappers; i++) {
837 InputMapper* mapper = mMappers[i];
838 mapper->reset();
839 }
840}
Jeff Brown46b9ac02010-04-22 18:58:52 -0700841
Jeff Browndbf8d272011-03-18 18:14:26 -0700842void InputDevice::process(const RawEvent* rawEvents, size_t count) {
843 // Process all of the events in order for each mapper.
844 // We cannot simply ask each mapper to process them in bulk because mappers may
845 // have side-effects that must be interleaved. For example, joystick movement events and
846 // gamepad button presses are handled by different mappers but they should be dispatched
847 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700848 size_t numMappers = mMappers.size();
Jeff Browndbf8d272011-03-18 18:14:26 -0700849 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
850#if DEBUG_RAW_EVENTS
851 LOGD("Input event: device=%d type=0x%04x scancode=0x%04x "
852 "keycode=0x%04x value=0x%04x flags=0x%08x",
853 rawEvent->deviceId, rawEvent->type, rawEvent->scanCode, rawEvent->keyCode,
854 rawEvent->value, rawEvent->flags);
855#endif
856
857 for (size_t i = 0; i < numMappers; i++) {
858 InputMapper* mapper = mMappers[i];
859 mapper->process(rawEvent);
860 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700861 }
862}
Jeff Brown46b9ac02010-04-22 18:58:52 -0700863
Jeff Brown68d60752011-03-17 01:34:19 -0700864void InputDevice::timeoutExpired(nsecs_t when) {
865 size_t numMappers = mMappers.size();
866 for (size_t i = 0; i < numMappers; i++) {
867 InputMapper* mapper = mMappers[i];
868 mapper->timeoutExpired(when);
869 }
870}
871
Jeff Brown6d0fec22010-07-23 21:28:06 -0700872void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
873 outDeviceInfo->initialize(mId, mName);
874
875 size_t numMappers = mMappers.size();
876 for (size_t i = 0; i < numMappers; i++) {
877 InputMapper* mapper = mMappers[i];
878 mapper->populateDeviceInfo(outDeviceInfo);
879 }
880}
881
882int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
883 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
884}
885
886int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
887 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
888}
889
890int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
891 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
892}
893
894int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
895 int32_t result = AKEY_STATE_UNKNOWN;
896 size_t numMappers = mMappers.size();
897 for (size_t i = 0; i < numMappers; i++) {
898 InputMapper* mapper = mMappers[i];
899 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
900 result = (mapper->*getStateFunc)(sourceMask, code);
901 if (result >= AKEY_STATE_DOWN) {
902 return result;
903 }
904 }
905 }
906 return result;
907}
908
909bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
910 const int32_t* keyCodes, uint8_t* outFlags) {
911 bool result = false;
912 size_t numMappers = mMappers.size();
913 for (size_t i = 0; i < numMappers; i++) {
914 InputMapper* mapper = mMappers[i];
915 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
916 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
917 }
918 }
919 return result;
920}
921
922int32_t InputDevice::getMetaState() {
923 int32_t result = 0;
924 size_t numMappers = mMappers.size();
925 for (size_t i = 0; i < numMappers; i++) {
926 InputMapper* mapper = mMappers[i];
927 result |= mapper->getMetaState();
928 }
929 return result;
930}
931
Jeff Brown05dc66a2011-03-02 14:41:58 -0800932void InputDevice::fadePointer() {
933 size_t numMappers = mMappers.size();
934 for (size_t i = 0; i < numMappers; i++) {
935 InputMapper* mapper = mMappers[i];
936 mapper->fadePointer();
937 }
938}
939
Jeff Brown6d0fec22010-07-23 21:28:06 -0700940
941// --- InputMapper ---
942
943InputMapper::InputMapper(InputDevice* device) :
944 mDevice(device), mContext(device->getContext()) {
945}
946
947InputMapper::~InputMapper() {
948}
949
950void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
951 info->addSource(getSources());
952}
953
Jeff Brownef3d7e82010-09-30 14:33:04 -0700954void InputMapper::dump(String8& dump) {
955}
956
Jeff Brown6d0fec22010-07-23 21:28:06 -0700957void InputMapper::configure() {
958}
959
960void InputMapper::reset() {
961}
962
Jeff Brown68d60752011-03-17 01:34:19 -0700963void InputMapper::timeoutExpired(nsecs_t when) {
964}
965
Jeff Brown6d0fec22010-07-23 21:28:06 -0700966int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
967 return AKEY_STATE_UNKNOWN;
968}
969
970int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
971 return AKEY_STATE_UNKNOWN;
972}
973
974int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
975 return AKEY_STATE_UNKNOWN;
976}
977
978bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
979 const int32_t* keyCodes, uint8_t* outFlags) {
980 return false;
981}
982
983int32_t InputMapper::getMetaState() {
984 return 0;
985}
986
Jeff Brown05dc66a2011-03-02 14:41:58 -0800987void InputMapper::fadePointer() {
988}
989
Jeff Browncb1404e2011-01-15 18:14:15 -0800990void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
991 const RawAbsoluteAxisInfo& axis, const char* name) {
992 if (axis.valid) {
993 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d\n",
994 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz);
995 } else {
996 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
997 }
998}
999
Jeff Brown6d0fec22010-07-23 21:28:06 -07001000
1001// --- SwitchInputMapper ---
1002
1003SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1004 InputMapper(device) {
1005}
1006
1007SwitchInputMapper::~SwitchInputMapper() {
1008}
1009
1010uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001011 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001012}
1013
1014void SwitchInputMapper::process(const RawEvent* rawEvent) {
1015 switch (rawEvent->type) {
1016 case EV_SW:
1017 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
1018 break;
1019 }
1020}
1021
1022void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownb6997262010-10-08 22:31:17 -07001023 getDispatcher()->notifySwitch(when, switchCode, switchValue, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001024}
1025
1026int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1027 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1028}
1029
1030
1031// --- KeyboardInputMapper ---
1032
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001033KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001034 uint32_t source, int32_t keyboardType) :
1035 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001036 mKeyboardType(keyboardType) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001037 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001038}
1039
1040KeyboardInputMapper::~KeyboardInputMapper() {
1041}
1042
Jeff Brown6328cdc2010-07-29 18:18:33 -07001043void KeyboardInputMapper::initializeLocked() {
1044 mLocked.metaState = AMETA_NONE;
1045 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001046}
1047
1048uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001049 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001050}
1051
1052void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1053 InputMapper::populateDeviceInfo(info);
1054
1055 info->setKeyboardType(mKeyboardType);
1056}
1057
Jeff Brownef3d7e82010-09-30 14:33:04 -07001058void KeyboardInputMapper::dump(String8& dump) {
1059 { // acquire lock
1060 AutoMutex _l(mLock);
1061 dump.append(INDENT2 "Keyboard Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001062 dumpParameters(dump);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001063 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
1064 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mLocked.keyDowns.size());
1065 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mLocked.metaState);
1066 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1067 } // release lock
1068}
1069
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001070
1071void KeyboardInputMapper::configure() {
1072 InputMapper::configure();
1073
1074 // Configure basic parameters.
1075 configureParameters();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001076
1077 // Reset LEDs.
1078 {
1079 AutoMutex _l(mLock);
1080 resetLedStateLocked();
1081 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001082}
1083
1084void KeyboardInputMapper::configureParameters() {
1085 mParameters.orientationAware = false;
1086 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
1087 mParameters.orientationAware);
1088
1089 mParameters.associatedDisplayId = mParameters.orientationAware ? 0 : -1;
1090}
1091
1092void KeyboardInputMapper::dumpParameters(String8& dump) {
1093 dump.append(INDENT3 "Parameters:\n");
1094 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1095 mParameters.associatedDisplayId);
1096 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1097 toString(mParameters.orientationAware));
1098}
1099
Jeff Brown6d0fec22010-07-23 21:28:06 -07001100void KeyboardInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001101 for (;;) {
1102 int32_t keyCode, scanCode;
1103 { // acquire lock
1104 AutoMutex _l(mLock);
1105
1106 // Synthesize key up event on reset if keys are currently down.
1107 if (mLocked.keyDowns.isEmpty()) {
1108 initializeLocked();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001109 resetLedStateLocked();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001110 break; // done
1111 }
1112
1113 const KeyDown& keyDown = mLocked.keyDowns.top();
1114 keyCode = keyDown.keyCode;
1115 scanCode = keyDown.scanCode;
1116 } // release lock
1117
Jeff Brown6d0fec22010-07-23 21:28:06 -07001118 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001119 processKey(when, false, keyCode, scanCode, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001120 }
1121
1122 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001123 getContext()->updateGlobalMetaState();
1124}
1125
1126void KeyboardInputMapper::process(const RawEvent* rawEvent) {
1127 switch (rawEvent->type) {
1128 case EV_KEY: {
1129 int32_t scanCode = rawEvent->scanCode;
1130 if (isKeyboardOrGamepadKey(scanCode)) {
1131 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
1132 rawEvent->flags);
1133 }
1134 break;
1135 }
1136 }
1137}
1138
1139bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
1140 return scanCode < BTN_MOUSE
1141 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001142 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08001143 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001144}
1145
Jeff Brown6328cdc2010-07-29 18:18:33 -07001146void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
1147 int32_t scanCode, uint32_t policyFlags) {
1148 int32_t newMetaState;
1149 nsecs_t downTime;
1150 bool metaStateChanged = false;
1151
1152 { // acquire lock
1153 AutoMutex _l(mLock);
1154
1155 if (down) {
1156 // Rotate key codes according to orientation if needed.
1157 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001158 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001159 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001160 if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1161 NULL, NULL, & orientation)) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001162 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001163 }
1164
1165 keyCode = rotateKeyCode(keyCode, orientation);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001166 }
1167
Jeff Brown6328cdc2010-07-29 18:18:33 -07001168 // Add key down.
1169 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
1170 if (keyDownIndex >= 0) {
1171 // key repeat, be sure to use same keycode as before in case of rotation
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001172 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001173 } else {
1174 // key down
Jeff Brownfe508922011-01-18 15:10:10 -08001175 if ((policyFlags & POLICY_FLAG_VIRTUAL)
1176 && mContext->shouldDropVirtualKey(when,
1177 getDevice(), keyCode, scanCode)) {
1178 return;
1179 }
1180
Jeff Brown6328cdc2010-07-29 18:18:33 -07001181 mLocked.keyDowns.push();
1182 KeyDown& keyDown = mLocked.keyDowns.editTop();
1183 keyDown.keyCode = keyCode;
1184 keyDown.scanCode = scanCode;
1185 }
1186
1187 mLocked.downTime = when;
1188 } else {
1189 // Remove key down.
1190 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
1191 if (keyDownIndex >= 0) {
1192 // key up, be sure to use same keycode as before in case of rotation
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001193 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001194 mLocked.keyDowns.removeAt(size_t(keyDownIndex));
1195 } else {
1196 // key was not actually down
1197 LOGI("Dropping key up from device %s because the key was not down. "
1198 "keyCode=%d, scanCode=%d",
1199 getDeviceName().string(), keyCode, scanCode);
1200 return;
1201 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001202 }
1203
Jeff Brown6328cdc2010-07-29 18:18:33 -07001204 int32_t oldMetaState = mLocked.metaState;
1205 newMetaState = updateMetaState(keyCode, down, oldMetaState);
1206 if (oldMetaState != newMetaState) {
1207 mLocked.metaState = newMetaState;
1208 metaStateChanged = true;
Jeff Brown497a92c2010-09-12 17:55:08 -07001209 updateLedStateLocked(false);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001210 }
Jeff Brownfd035822010-06-30 16:10:35 -07001211
Jeff Brown6328cdc2010-07-29 18:18:33 -07001212 downTime = mLocked.downTime;
1213 } // release lock
1214
Jeff Brown56194eb2011-03-02 19:23:13 -08001215 // Key down on external an keyboard should wake the device.
1216 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
1217 // For internal keyboards, the key layout file should specify the policy flags for
1218 // each wake key individually.
1219 // TODO: Use the input device configuration to control this behavior more finely.
1220 if (down && getDevice()->isExternal()
1221 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1222 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1223 }
1224
Jeff Brown6328cdc2010-07-29 18:18:33 -07001225 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001226 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001227 }
1228
Jeff Brown05dc66a2011-03-02 14:41:58 -08001229 if (down && !isMetaKey(keyCode)) {
1230 getContext()->fadePointer();
1231 }
1232
Jeff Brownefd32662011-03-08 15:13:06 -08001233 getDispatcher()->notifyKey(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001234 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1235 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001236}
1237
Jeff Brown6328cdc2010-07-29 18:18:33 -07001238ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) {
1239 size_t n = mLocked.keyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001240 for (size_t i = 0; i < n; i++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001241 if (mLocked.keyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001242 return i;
1243 }
1244 }
1245 return -1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001246}
1247
Jeff Brown6d0fec22010-07-23 21:28:06 -07001248int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1249 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1250}
Jeff Brown46b9ac02010-04-22 18:58:52 -07001251
Jeff Brown6d0fec22010-07-23 21:28:06 -07001252int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1253 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1254}
Jeff Brown46b9ac02010-04-22 18:58:52 -07001255
Jeff Brown6d0fec22010-07-23 21:28:06 -07001256bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1257 const int32_t* keyCodes, uint8_t* outFlags) {
1258 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1259}
1260
1261int32_t KeyboardInputMapper::getMetaState() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001262 { // acquire lock
1263 AutoMutex _l(mLock);
1264 return mLocked.metaState;
1265 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001266}
1267
Jeff Brown49ed71d2010-12-06 17:13:33 -08001268void KeyboardInputMapper::resetLedStateLocked() {
1269 initializeLedStateLocked(mLocked.capsLockLedState, LED_CAPSL);
1270 initializeLedStateLocked(mLocked.numLockLedState, LED_NUML);
1271 initializeLedStateLocked(mLocked.scrollLockLedState, LED_SCROLLL);
1272
1273 updateLedStateLocked(true);
1274}
1275
1276void KeyboardInputMapper::initializeLedStateLocked(LockedState::LedState& ledState, int32_t led) {
1277 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
1278 ledState.on = false;
1279}
1280
Jeff Brown497a92c2010-09-12 17:55:08 -07001281void KeyboardInputMapper::updateLedStateLocked(bool reset) {
1282 updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001283 AMETA_CAPS_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001284 updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001285 AMETA_NUM_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001286 updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001287 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001288}
1289
1290void KeyboardInputMapper::updateLedStateForModifierLocked(LockedState::LedState& ledState,
1291 int32_t led, int32_t modifier, bool reset) {
1292 if (ledState.avail) {
1293 bool desiredState = (mLocked.metaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001294 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001295 getEventHub()->setLedState(getDeviceId(), led, desiredState);
1296 ledState.on = desiredState;
1297 }
1298 }
1299}
1300
Jeff Brown6d0fec22010-07-23 21:28:06 -07001301
Jeff Brown83c09682010-12-23 17:50:18 -08001302// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07001303
Jeff Brown83c09682010-12-23 17:50:18 -08001304CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001305 InputMapper(device) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001306 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001307}
1308
Jeff Brown83c09682010-12-23 17:50:18 -08001309CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001310}
1311
Jeff Brown83c09682010-12-23 17:50:18 -08001312uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001313 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001314}
1315
Jeff Brown83c09682010-12-23 17:50:18 -08001316void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001317 InputMapper::populateDeviceInfo(info);
1318
Jeff Brown83c09682010-12-23 17:50:18 -08001319 if (mParameters.mode == Parameters::MODE_POINTER) {
1320 float minX, minY, maxX, maxY;
1321 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08001322 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
1323 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08001324 }
1325 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08001326 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
1327 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08001328 }
Jeff Brownefd32662011-03-08 15:13:06 -08001329 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001330
1331 if (mHaveVWheel) {
Jeff Brownefd32662011-03-08 15:13:06 -08001332 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001333 }
1334 if (mHaveHWheel) {
Jeff Brownefd32662011-03-08 15:13:06 -08001335 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001336 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001337}
1338
Jeff Brown83c09682010-12-23 17:50:18 -08001339void CursorInputMapper::dump(String8& dump) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07001340 { // acquire lock
1341 AutoMutex _l(mLock);
Jeff Brown83c09682010-12-23 17:50:18 -08001342 dump.append(INDENT2 "Cursor Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001343 dumpParameters(dump);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001344 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
1345 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001346 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1347 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001348 dump.appendFormat(INDENT3 "HaveVWheel: %s\n", toString(mHaveVWheel));
1349 dump.appendFormat(INDENT3 "HaveHWheel: %s\n", toString(mHaveHWheel));
1350 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
1351 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brownefd32662011-03-08 15:13:06 -08001352 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mLocked.buttonState);
1353 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mLocked.buttonState)));
Jeff Brownef3d7e82010-09-30 14:33:04 -07001354 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1355 } // release lock
1356}
1357
Jeff Brown83c09682010-12-23 17:50:18 -08001358void CursorInputMapper::configure() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001359 InputMapper::configure();
1360
1361 // Configure basic parameters.
1362 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08001363
1364 // Configure device mode.
1365 switch (mParameters.mode) {
1366 case Parameters::MODE_POINTER:
Jeff Brownefd32662011-03-08 15:13:06 -08001367 mSource = AINPUT_SOURCE_MOUSE;
Jeff Brown83c09682010-12-23 17:50:18 -08001368 mXPrecision = 1.0f;
1369 mYPrecision = 1.0f;
1370 mXScale = 1.0f;
1371 mYScale = 1.0f;
1372 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
1373 break;
1374 case Parameters::MODE_NAVIGATION:
Jeff Brownefd32662011-03-08 15:13:06 -08001375 mSource = AINPUT_SOURCE_TRACKBALL;
Jeff Brown83c09682010-12-23 17:50:18 -08001376 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1377 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1378 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1379 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1380 break;
1381 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001382
1383 mVWheelScale = 1.0f;
1384 mHWheelScale = 1.0f;
Jeff Browncc0c1592011-02-19 05:07:28 -08001385
1386 mHaveVWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_WHEEL);
1387 mHaveHWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_HWHEEL);
Jeff Brown19c97d462011-06-01 12:33:19 -07001388
1389 mPointerVelocityControl.setParameters(getConfig()->pointerVelocityControlParameters);
1390 mWheelXVelocityControl.setParameters(getConfig()->wheelVelocityControlParameters);
1391 mWheelYVelocityControl.setParameters(getConfig()->wheelVelocityControlParameters);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001392}
1393
Jeff Brown83c09682010-12-23 17:50:18 -08001394void CursorInputMapper::configureParameters() {
1395 mParameters.mode = Parameters::MODE_POINTER;
1396 String8 cursorModeString;
1397 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
1398 if (cursorModeString == "navigation") {
1399 mParameters.mode = Parameters::MODE_NAVIGATION;
1400 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
1401 LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
1402 }
1403 }
1404
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001405 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08001406 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001407 mParameters.orientationAware);
1408
Jeff Brown83c09682010-12-23 17:50:18 -08001409 mParameters.associatedDisplayId = mParameters.mode == Parameters::MODE_POINTER
1410 || mParameters.orientationAware ? 0 : -1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001411}
1412
Jeff Brown83c09682010-12-23 17:50:18 -08001413void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001414 dump.append(INDENT3 "Parameters:\n");
1415 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1416 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08001417
1418 switch (mParameters.mode) {
1419 case Parameters::MODE_POINTER:
1420 dump.append(INDENT4 "Mode: pointer\n");
1421 break;
1422 case Parameters::MODE_NAVIGATION:
1423 dump.append(INDENT4 "Mode: navigation\n");
1424 break;
1425 default:
1426 assert(false);
1427 }
1428
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001429 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1430 toString(mParameters.orientationAware));
1431}
1432
Jeff Brown83c09682010-12-23 17:50:18 -08001433void CursorInputMapper::initializeLocked() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001434 mAccumulator.clear();
1435
Jeff Brownefd32662011-03-08 15:13:06 -08001436 mLocked.buttonState = 0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001437 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001438}
1439
Jeff Brown83c09682010-12-23 17:50:18 -08001440void CursorInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001441 for (;;) {
Jeff Brownefd32662011-03-08 15:13:06 -08001442 uint32_t buttonState;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001443 { // acquire lock
1444 AutoMutex _l(mLock);
1445
Jeff Brownefd32662011-03-08 15:13:06 -08001446 buttonState = mLocked.buttonState;
1447 if (!buttonState) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001448 initializeLocked();
1449 break; // done
1450 }
1451 } // release lock
1452
Jeff Brown19c97d462011-06-01 12:33:19 -07001453 // Reset velocity.
1454 mPointerVelocityControl.reset();
1455 mWheelXVelocityControl.reset();
1456 mWheelYVelocityControl.reset();
1457
Jeff Brown83c09682010-12-23 17:50:18 -08001458 // Synthesize button up event on reset.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001459 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownefd32662011-03-08 15:13:06 -08001460 mAccumulator.clear();
1461 mAccumulator.buttonDown = 0;
1462 mAccumulator.buttonUp = buttonState;
1463 mAccumulator.fields = Accumulator::FIELD_BUTTONS;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001464 sync(when);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001465 }
1466
Jeff Brown6d0fec22010-07-23 21:28:06 -07001467 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001468}
Jeff Brown46b9ac02010-04-22 18:58:52 -07001469
Jeff Brown83c09682010-12-23 17:50:18 -08001470void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001471 switch (rawEvent->type) {
Jeff Brownefd32662011-03-08 15:13:06 -08001472 case EV_KEY: {
1473 uint32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode);
1474 if (buttonState) {
1475 if (rawEvent->value) {
1476 mAccumulator.buttonDown = buttonState;
1477 mAccumulator.buttonUp = 0;
1478 } else {
1479 mAccumulator.buttonDown = 0;
1480 mAccumulator.buttonUp = buttonState;
1481 }
1482 mAccumulator.fields |= Accumulator::FIELD_BUTTONS;
1483
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001484 // Sync now since BTN_MOUSE is not necessarily followed by SYN_REPORT and
1485 // we need to ensure that we report the up/down promptly.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001486 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001487 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001488 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001489 break;
Jeff Brownefd32662011-03-08 15:13:06 -08001490 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001491
Jeff Brown6d0fec22010-07-23 21:28:06 -07001492 case EV_REL:
1493 switch (rawEvent->scanCode) {
1494 case REL_X:
1495 mAccumulator.fields |= Accumulator::FIELD_REL_X;
1496 mAccumulator.relX = rawEvent->value;
1497 break;
1498 case REL_Y:
1499 mAccumulator.fields |= Accumulator::FIELD_REL_Y;
1500 mAccumulator.relY = rawEvent->value;
1501 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001502 case REL_WHEEL:
1503 mAccumulator.fields |= Accumulator::FIELD_REL_WHEEL;
1504 mAccumulator.relWheel = rawEvent->value;
1505 break;
1506 case REL_HWHEEL:
1507 mAccumulator.fields |= Accumulator::FIELD_REL_HWHEEL;
1508 mAccumulator.relHWheel = rawEvent->value;
1509 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001510 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001511 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001512
Jeff Brown6d0fec22010-07-23 21:28:06 -07001513 case EV_SYN:
1514 switch (rawEvent->scanCode) {
1515 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001516 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001517 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001518 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001519 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001520 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001521}
1522
Jeff Brown83c09682010-12-23 17:50:18 -08001523void CursorInputMapper::sync(nsecs_t when) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001524 uint32_t fields = mAccumulator.fields;
1525 if (fields == 0) {
1526 return; // no new state changes, so nothing to do
1527 }
1528
Jeff Brown9626b142011-03-03 02:09:54 -08001529 int32_t motionEventAction;
1530 int32_t motionEventEdgeFlags;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001531 PointerCoords pointerCoords;
1532 nsecs_t downTime;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001533 float vscroll, hscroll;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001534 { // acquire lock
1535 AutoMutex _l(mLock);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001536
Jeff Brownefd32662011-03-08 15:13:06 -08001537 bool down, downChanged;
1538 bool wasDown = isPointerDown(mLocked.buttonState);
1539 bool buttonsChanged = fields & Accumulator::FIELD_BUTTONS;
1540 if (buttonsChanged) {
1541 mLocked.buttonState = (mLocked.buttonState | mAccumulator.buttonDown)
1542 & ~mAccumulator.buttonUp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001543
Jeff Brownefd32662011-03-08 15:13:06 -08001544 down = isPointerDown(mLocked.buttonState);
1545
1546 if (!wasDown && down) {
1547 mLocked.downTime = when;
1548 downChanged = true;
1549 } else if (wasDown && !down) {
1550 downChanged = true;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001551 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08001552 downChanged = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001553 }
Jeff Brownefd32662011-03-08 15:13:06 -08001554 } else {
1555 down = wasDown;
1556 downChanged = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001557 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001558
Jeff Brown6328cdc2010-07-29 18:18:33 -07001559 downTime = mLocked.downTime;
Jeff Brown83c09682010-12-23 17:50:18 -08001560 float deltaX = fields & Accumulator::FIELD_REL_X ? mAccumulator.relX * mXScale : 0.0f;
1561 float deltaY = fields & Accumulator::FIELD_REL_Y ? mAccumulator.relY * mYScale : 0.0f;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001562
Jeff Brown6328cdc2010-07-29 18:18:33 -07001563 if (downChanged) {
Jeff Brownefd32662011-03-08 15:13:06 -08001564 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
1565 } else if (down || mPointerController == NULL) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001566 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browncc0c1592011-02-19 05:07:28 -08001567 } else {
1568 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001569 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001570
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001571 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
Jeff Brown83c09682010-12-23 17:50:18 -08001572 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001573 // Rotate motion based on display orientation if needed.
1574 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1575 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001576 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1577 NULL, NULL, & orientation)) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001578 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001579 }
1580
1581 float temp;
1582 switch (orientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001583 case DISPLAY_ORIENTATION_90:
Jeff Brown83c09682010-12-23 17:50:18 -08001584 temp = deltaX;
1585 deltaX = deltaY;
1586 deltaY = -temp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001587 break;
1588
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001589 case DISPLAY_ORIENTATION_180:
Jeff Brown83c09682010-12-23 17:50:18 -08001590 deltaX = -deltaX;
1591 deltaY = -deltaY;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001592 break;
1593
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001594 case DISPLAY_ORIENTATION_270:
Jeff Brown83c09682010-12-23 17:50:18 -08001595 temp = deltaX;
1596 deltaX = -deltaY;
1597 deltaY = temp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001598 break;
1599 }
1600 }
Jeff Brown83c09682010-12-23 17:50:18 -08001601
Jeff Brown91c69ab2011-02-14 17:03:18 -08001602 pointerCoords.clear();
1603
Jeff Brown9626b142011-03-03 02:09:54 -08001604 motionEventEdgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
1605
Jeff Brown86ea1f52011-04-12 22:39:53 -07001606 if (mHaveVWheel && (fields & Accumulator::FIELD_REL_WHEEL)) {
1607 vscroll = mAccumulator.relWheel;
1608 } else {
1609 vscroll = 0;
1610 }
Jeff Brown19c97d462011-06-01 12:33:19 -07001611 mWheelYVelocityControl.move(when, NULL, &vscroll);
1612
Jeff Brown86ea1f52011-04-12 22:39:53 -07001613 if (mHaveHWheel && (fields & Accumulator::FIELD_REL_HWHEEL)) {
1614 hscroll = mAccumulator.relHWheel;
1615 } else {
1616 hscroll = 0;
1617 }
Jeff Brown19c97d462011-06-01 12:33:19 -07001618 mWheelXVelocityControl.move(when, &hscroll, NULL);
1619
1620 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown86ea1f52011-04-12 22:39:53 -07001621
Jeff Brown83c09682010-12-23 17:50:18 -08001622 if (mPointerController != NULL) {
Jeff Brown86ea1f52011-04-12 22:39:53 -07001623 if (deltaX != 0 || deltaY != 0 || vscroll != 0 || hscroll != 0
1624 || buttonsChanged) {
1625 mPointerController->setPresentation(
1626 PointerControllerInterface::PRESENTATION_POINTER);
1627
1628 if (deltaX != 0 || deltaY != 0) {
1629 mPointerController->move(deltaX, deltaY);
1630 }
1631
1632 if (buttonsChanged) {
1633 mPointerController->setButtonState(mLocked.buttonState);
1634 }
1635
Jeff Brown538881e2011-05-25 18:23:38 -07001636 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08001637 }
Jeff Brownefd32662011-03-08 15:13:06 -08001638
Jeff Brown91c69ab2011-02-14 17:03:18 -08001639 float x, y;
1640 mPointerController->getPosition(&x, &y);
Jeff Brownebbd5d12011-02-17 13:01:34 -08001641 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
1642 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown9626b142011-03-03 02:09:54 -08001643
1644 if (motionEventAction == AMOTION_EVENT_ACTION_DOWN) {
Jeff Brownefd32662011-03-08 15:13:06 -08001645 motionEventEdgeFlags = calculateEdgeFlagsUsingPointerBounds(
1646 mPointerController, x, y);
Jeff Brown9626b142011-03-03 02:09:54 -08001647 }
Jeff Brown83c09682010-12-23 17:50:18 -08001648 } else {
Jeff Brownebbd5d12011-02-17 13:01:34 -08001649 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
1650 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
Jeff Brown83c09682010-12-23 17:50:18 -08001651 }
1652
Jeff Brownefd32662011-03-08 15:13:06 -08001653 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001654 } // release lock
1655
Jeff Brown56194eb2011-03-02 19:23:13 -08001656 // Moving an external trackball or mouse should wake the device.
1657 // We don't do this for internal cursor devices to prevent them from waking up
1658 // the device in your pocket.
1659 // TODO: Use the input device configuration to control this behavior more finely.
1660 uint32_t policyFlags = 0;
1661 if (getDevice()->isExternal()) {
1662 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1663 }
1664
Jeff Brown6d0fec22010-07-23 21:28:06 -07001665 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001666 int32_t pointerId = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08001667 getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags,
Jeff Brown9626b142011-03-03 02:09:54 -08001668 motionEventAction, 0, metaState, motionEventEdgeFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001669 1, &pointerId, &pointerCoords, mXPrecision, mYPrecision, downTime);
1670
1671 mAccumulator.clear();
Jeff Brown33bbfd22011-02-24 20:55:35 -08001672
1673 if (vscroll != 0 || hscroll != 0) {
1674 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
1675 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
1676
Jeff Brownefd32662011-03-08 15:13:06 -08001677 getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags,
Jeff Brown33bbfd22011-02-24 20:55:35 -08001678 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE,
1679 1, &pointerId, &pointerCoords, mXPrecision, mYPrecision, downTime);
1680 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001681}
1682
Jeff Brown83c09682010-12-23 17:50:18 -08001683int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07001684 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
1685 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1686 } else {
1687 return AKEY_STATE_UNKNOWN;
1688 }
1689}
1690
Jeff Brown05dc66a2011-03-02 14:41:58 -08001691void CursorInputMapper::fadePointer() {
1692 { // acquire lock
1693 AutoMutex _l(mLock);
Jeff Brownefd32662011-03-08 15:13:06 -08001694 if (mPointerController != NULL) {
Jeff Brown538881e2011-05-25 18:23:38 -07001695 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
Jeff Brownefd32662011-03-08 15:13:06 -08001696 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08001697 } // release lock
1698}
1699
Jeff Brown6d0fec22010-07-23 21:28:06 -07001700
1701// --- TouchInputMapper ---
1702
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001703TouchInputMapper::TouchInputMapper(InputDevice* device) :
1704 InputMapper(device) {
Jeff Brown214eaf42011-05-26 19:17:02 -07001705 mConfig = getConfig();
1706
Jeff Brown6328cdc2010-07-29 18:18:33 -07001707 mLocked.surfaceOrientation = -1;
1708 mLocked.surfaceWidth = -1;
1709 mLocked.surfaceHeight = -1;
1710
1711 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001712}
1713
1714TouchInputMapper::~TouchInputMapper() {
1715}
1716
1717uint32_t TouchInputMapper::getSources() {
Jeff Brown96ad3972011-03-09 17:39:48 -08001718 return mTouchSource | mPointerSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001719}
1720
1721void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1722 InputMapper::populateDeviceInfo(info);
1723
Jeff Brown6328cdc2010-07-29 18:18:33 -07001724 { // acquire lock
1725 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001726
Jeff Brown6328cdc2010-07-29 18:18:33 -07001727 // Ensure surface information is up to date so that orientation changes are
1728 // noticed immediately.
Jeff Brownefd32662011-03-08 15:13:06 -08001729 if (!configureSurfaceLocked()) {
1730 return;
1731 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001732
Jeff Brownefd32662011-03-08 15:13:06 -08001733 info->addMotionRange(mLocked.orientedRanges.x);
1734 info->addMotionRange(mLocked.orientedRanges.y);
Jeff Brown8d608662010-08-30 03:02:23 -07001735
1736 if (mLocked.orientedRanges.havePressure) {
Jeff Brownefd32662011-03-08 15:13:06 -08001737 info->addMotionRange(mLocked.orientedRanges.pressure);
Jeff Brown8d608662010-08-30 03:02:23 -07001738 }
1739
1740 if (mLocked.orientedRanges.haveSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08001741 info->addMotionRange(mLocked.orientedRanges.size);
Jeff Brown8d608662010-08-30 03:02:23 -07001742 }
1743
Jeff Brownc6d282b2010-10-14 21:42:15 -07001744 if (mLocked.orientedRanges.haveTouchSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08001745 info->addMotionRange(mLocked.orientedRanges.touchMajor);
1746 info->addMotionRange(mLocked.orientedRanges.touchMinor);
Jeff Brown8d608662010-08-30 03:02:23 -07001747 }
1748
Jeff Brownc6d282b2010-10-14 21:42:15 -07001749 if (mLocked.orientedRanges.haveToolSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08001750 info->addMotionRange(mLocked.orientedRanges.toolMajor);
1751 info->addMotionRange(mLocked.orientedRanges.toolMinor);
Jeff Brown8d608662010-08-30 03:02:23 -07001752 }
1753
1754 if (mLocked.orientedRanges.haveOrientation) {
Jeff Brownefd32662011-03-08 15:13:06 -08001755 info->addMotionRange(mLocked.orientedRanges.orientation);
Jeff Brown8d608662010-08-30 03:02:23 -07001756 }
Jeff Brown96ad3972011-03-09 17:39:48 -08001757
1758 if (mPointerController != NULL) {
1759 float minX, minY, maxX, maxY;
1760 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
1761 info->addMotionRange(AMOTION_EVENT_AXIS_X, mPointerSource,
1762 minX, maxX, 0.0f, 0.0f);
1763 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mPointerSource,
1764 minY, maxY, 0.0f, 0.0f);
1765 }
1766 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mPointerSource,
1767 0.0f, 1.0f, 0.0f, 0.0f);
1768 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001769 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001770}
1771
Jeff Brownef3d7e82010-09-30 14:33:04 -07001772void TouchInputMapper::dump(String8& dump) {
1773 { // acquire lock
1774 AutoMutex _l(mLock);
1775 dump.append(INDENT2 "Touch Input Mapper:\n");
Jeff Brownef3d7e82010-09-30 14:33:04 -07001776 dumpParameters(dump);
1777 dumpVirtualKeysLocked(dump);
1778 dumpRawAxes(dump);
1779 dumpCalibration(dump);
1780 dumpSurfaceLocked(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08001781
Jeff Brown511ee5f2010-10-18 13:32:20 -07001782 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
Jeff Brownc6d282b2010-10-14 21:42:15 -07001783 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mLocked.xScale);
1784 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mLocked.yScale);
1785 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mLocked.xPrecision);
1786 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mLocked.yPrecision);
1787 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mLocked.geometricScale);
1788 dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mLocked.toolSizeLinearScale);
1789 dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mLocked.toolSizeLinearBias);
1790 dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mLocked.toolSizeAreaScale);
1791 dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mLocked.toolSizeAreaBias);
1792 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mLocked.pressureScale);
1793 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mLocked.sizeScale);
Jeff Brownefd32662011-03-08 15:13:06 -08001794 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mLocked.orientationScale);
1795
1796 dump.appendFormat(INDENT3 "Last Touch:\n");
1797 dump.appendFormat(INDENT4 "Pointer Count: %d\n", mLastTouch.pointerCount);
Jeff Brown96ad3972011-03-09 17:39:48 -08001798 dump.appendFormat(INDENT4 "Button State: 0x%08x\n", mLastTouch.buttonState);
1799
1800 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
1801 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
1802 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
1803 mLocked.pointerGestureXMovementScale);
1804 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
1805 mLocked.pointerGestureYMovementScale);
1806 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
1807 mLocked.pointerGestureXZoomScale);
1808 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
1809 mLocked.pointerGestureYZoomScale);
Jeff Brown86ea1f52011-04-12 22:39:53 -07001810 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
1811 mLocked.pointerGestureMaxSwipeWidth);
Jeff Brown96ad3972011-03-09 17:39:48 -08001812 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07001813 } // release lock
1814}
1815
Jeff Brown6328cdc2010-07-29 18:18:33 -07001816void TouchInputMapper::initializeLocked() {
1817 mCurrentTouch.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001818 mLastTouch.clear();
1819 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001820
1821 for (uint32_t i = 0; i < MAX_POINTERS; i++) {
1822 mAveragingTouchFilter.historyStart[i] = 0;
1823 mAveragingTouchFilter.historyEnd[i] = 0;
1824 }
1825
1826 mJumpyTouchFilter.jumpyPointsDropped = 0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001827
1828 mLocked.currentVirtualKey.down = false;
Jeff Brown8d608662010-08-30 03:02:23 -07001829
1830 mLocked.orientedRanges.havePressure = false;
1831 mLocked.orientedRanges.haveSize = false;
Jeff Brownc6d282b2010-10-14 21:42:15 -07001832 mLocked.orientedRanges.haveTouchSize = false;
1833 mLocked.orientedRanges.haveToolSize = false;
Jeff Brown8d608662010-08-30 03:02:23 -07001834 mLocked.orientedRanges.haveOrientation = false;
Jeff Brown96ad3972011-03-09 17:39:48 -08001835
1836 mPointerGesture.reset();
Jeff Brown19c97d462011-06-01 12:33:19 -07001837 mPointerGesture.pointerVelocityControl.setParameters(mConfig->pointerVelocityControlParameters);
Jeff Brown8d608662010-08-30 03:02:23 -07001838}
1839
Jeff Brown6d0fec22010-07-23 21:28:06 -07001840void TouchInputMapper::configure() {
1841 InputMapper::configure();
1842
1843 // Configure basic parameters.
Jeff Brown8d608662010-08-30 03:02:23 -07001844 configureParameters();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001845
Jeff Brown83c09682010-12-23 17:50:18 -08001846 // Configure sources.
1847 switch (mParameters.deviceType) {
1848 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
Jeff Brownefd32662011-03-08 15:13:06 -08001849 mTouchSource = AINPUT_SOURCE_TOUCHSCREEN;
Jeff Brown96ad3972011-03-09 17:39:48 -08001850 mPointerSource = 0;
Jeff Brown83c09682010-12-23 17:50:18 -08001851 break;
1852 case Parameters::DEVICE_TYPE_TOUCH_PAD:
Jeff Brownefd32662011-03-08 15:13:06 -08001853 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
Jeff Brown96ad3972011-03-09 17:39:48 -08001854 mPointerSource = 0;
1855 break;
1856 case Parameters::DEVICE_TYPE_POINTER:
1857 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
1858 mPointerSource = AINPUT_SOURCE_MOUSE;
Jeff Brown83c09682010-12-23 17:50:18 -08001859 break;
1860 default:
1861 assert(false);
1862 }
1863
Jeff Brown6d0fec22010-07-23 21:28:06 -07001864 // Configure absolute axis information.
Jeff Brown8d608662010-08-30 03:02:23 -07001865 configureRawAxes();
Jeff Brown8d608662010-08-30 03:02:23 -07001866
1867 // Prepare input device calibration.
1868 parseCalibration();
1869 resolveCalibration();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001870
Jeff Brown6328cdc2010-07-29 18:18:33 -07001871 { // acquire lock
1872 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001873
Jeff Brown8d608662010-08-30 03:02:23 -07001874 // Configure surface dimensions and orientation.
Jeff Brown6328cdc2010-07-29 18:18:33 -07001875 configureSurfaceLocked();
1876 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001877}
1878
Jeff Brown8d608662010-08-30 03:02:23 -07001879void TouchInputMapper::configureParameters() {
Jeff Brown214eaf42011-05-26 19:17:02 -07001880 mParameters.useBadTouchFilter = mConfig->filterTouchEvents;
1881 mParameters.useAveragingTouchFilter = mConfig->filterTouchEvents;
1882 mParameters.useJumpyTouchFilter = mConfig->filterJumpyTouchEvents;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001883
Jeff Brown538881e2011-05-25 18:23:38 -07001884 // TODO: select the default gesture mode based on whether the device supports
1885 // distinct multitouch
Jeff Brown86ea1f52011-04-12 22:39:53 -07001886 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
1887
Jeff Brown538881e2011-05-25 18:23:38 -07001888 String8 gestureModeString;
1889 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
1890 gestureModeString)) {
1891 if (gestureModeString == "pointer") {
1892 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
1893 } else if (gestureModeString == "spots") {
1894 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
1895 } else if (gestureModeString != "default") {
1896 LOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
1897 }
1898 }
1899
Jeff Brown96ad3972011-03-09 17:39:48 -08001900 if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
1901 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
1902 // The device is a cursor device with a touch pad attached.
1903 // By default don't use the touch pad to move the pointer.
1904 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
1905 } else {
1906 // The device is just a touch pad.
1907 // By default use the touch pad to move the pointer and to perform related gestures.
1908 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
1909 }
1910
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001911 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001912 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
1913 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08001914 if (deviceTypeString == "touchScreen") {
1915 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08001916 } else if (deviceTypeString == "touchPad") {
1917 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brown96ad3972011-03-09 17:39:48 -08001918 } else if (deviceTypeString == "pointer") {
1919 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07001920 } else if (deviceTypeString != "default") {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001921 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
1922 }
1923 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001924
Jeff Brownefd32662011-03-08 15:13:06 -08001925 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001926 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
1927 mParameters.orientationAware);
1928
Jeff Brownefd32662011-03-08 15:13:06 -08001929 mParameters.associatedDisplayId = mParameters.orientationAware
1930 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brown96ad3972011-03-09 17:39:48 -08001931 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
Jeff Brownefd32662011-03-08 15:13:06 -08001932 ? 0 : -1;
Jeff Brown8d608662010-08-30 03:02:23 -07001933}
1934
Jeff Brownef3d7e82010-09-30 14:33:04 -07001935void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001936 dump.append(INDENT3 "Parameters:\n");
1937
Jeff Brown538881e2011-05-25 18:23:38 -07001938 switch (mParameters.gestureMode) {
1939 case Parameters::GESTURE_MODE_POINTER:
1940 dump.append(INDENT4 "GestureMode: pointer\n");
1941 break;
1942 case Parameters::GESTURE_MODE_SPOTS:
1943 dump.append(INDENT4 "GestureMode: spots\n");
1944 break;
1945 default:
1946 assert(false);
1947 }
1948
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001949 switch (mParameters.deviceType) {
1950 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
1951 dump.append(INDENT4 "DeviceType: touchScreen\n");
1952 break;
1953 case Parameters::DEVICE_TYPE_TOUCH_PAD:
1954 dump.append(INDENT4 "DeviceType: touchPad\n");
1955 break;
Jeff Brown96ad3972011-03-09 17:39:48 -08001956 case Parameters::DEVICE_TYPE_POINTER:
1957 dump.append(INDENT4 "DeviceType: pointer\n");
1958 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001959 default:
1960 assert(false);
1961 }
1962
1963 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1964 mParameters.associatedDisplayId);
1965 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1966 toString(mParameters.orientationAware));
1967
1968 dump.appendFormat(INDENT4 "UseBadTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07001969 toString(mParameters.useBadTouchFilter));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001970 dump.appendFormat(INDENT4 "UseAveragingTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07001971 toString(mParameters.useAveragingTouchFilter));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001972 dump.appendFormat(INDENT4 "UseJumpyTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07001973 toString(mParameters.useJumpyTouchFilter));
Jeff Brownb88102f2010-09-08 11:49:43 -07001974}
1975
Jeff Brown8d608662010-08-30 03:02:23 -07001976void TouchInputMapper::configureRawAxes() {
1977 mRawAxes.x.clear();
1978 mRawAxes.y.clear();
1979 mRawAxes.pressure.clear();
1980 mRawAxes.touchMajor.clear();
1981 mRawAxes.touchMinor.clear();
1982 mRawAxes.toolMajor.clear();
1983 mRawAxes.toolMinor.clear();
1984 mRawAxes.orientation.clear();
1985}
1986
Jeff Brownef3d7e82010-09-30 14:33:04 -07001987void TouchInputMapper::dumpRawAxes(String8& dump) {
1988 dump.append(INDENT3 "Raw Axes:\n");
Jeff Browncb1404e2011-01-15 18:14:15 -08001989 dumpRawAbsoluteAxisInfo(dump, mRawAxes.x, "X");
1990 dumpRawAbsoluteAxisInfo(dump, mRawAxes.y, "Y");
1991 dumpRawAbsoluteAxisInfo(dump, mRawAxes.pressure, "Pressure");
1992 dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor");
1993 dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor");
1994 dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor");
1995 dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor");
1996 dumpRawAbsoluteAxisInfo(dump, mRawAxes.orientation, "Orientation");
Jeff Brown6d0fec22010-07-23 21:28:06 -07001997}
1998
Jeff Brown6328cdc2010-07-29 18:18:33 -07001999bool TouchInputMapper::configureSurfaceLocked() {
Jeff Brown9626b142011-03-03 02:09:54 -08002000 // Ensure we have valid X and Y axes.
2001 if (!mRawAxes.x.valid || !mRawAxes.y.valid) {
2002 LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
2003 "The device will be inoperable.", getDeviceName().string());
2004 return false;
2005 }
2006
Jeff Brown6d0fec22010-07-23 21:28:06 -07002007 // Update orientation and dimensions if needed.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002008 int32_t orientation = DISPLAY_ORIENTATION_0;
Jeff Brown9626b142011-03-03 02:09:54 -08002009 int32_t width = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2010 int32_t height = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002011
2012 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002013 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002014 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownefd32662011-03-08 15:13:06 -08002015 &mLocked.associatedDisplayWidth, &mLocked.associatedDisplayHeight,
2016 &mLocked.associatedDisplayOrientation)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002017 return false;
2018 }
Jeff Brownefd32662011-03-08 15:13:06 -08002019
2020 // A touch screen inherits the dimensions of the display.
2021 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
2022 width = mLocked.associatedDisplayWidth;
2023 height = mLocked.associatedDisplayHeight;
2024 }
2025
2026 // The device inherits the orientation of the display if it is orientation aware.
2027 if (mParameters.orientationAware) {
2028 orientation = mLocked.associatedDisplayOrientation;
2029 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002030 }
2031
Jeff Brown96ad3972011-03-09 17:39:48 -08002032 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2033 && mPointerController == NULL) {
2034 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2035 }
2036
Jeff Brown6328cdc2010-07-29 18:18:33 -07002037 bool orientationChanged = mLocked.surfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002038 if (orientationChanged) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002039 mLocked.surfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002040 }
2041
Jeff Brown6328cdc2010-07-29 18:18:33 -07002042 bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002043 if (sizeChanged) {
Jeff Brownefd32662011-03-08 15:13:06 -08002044 LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002045 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown8d608662010-08-30 03:02:23 -07002046
Jeff Brown6328cdc2010-07-29 18:18:33 -07002047 mLocked.surfaceWidth = width;
2048 mLocked.surfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002049
Jeff Brown8d608662010-08-30 03:02:23 -07002050 // Configure X and Y factors.
Jeff Brown9626b142011-03-03 02:09:54 -08002051 mLocked.xScale = float(width) / (mRawAxes.x.maxValue - mRawAxes.x.minValue + 1);
2052 mLocked.yScale = float(height) / (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1);
2053 mLocked.xPrecision = 1.0f / mLocked.xScale;
2054 mLocked.yPrecision = 1.0f / mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002055
Jeff Brownefd32662011-03-08 15:13:06 -08002056 mLocked.orientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
2057 mLocked.orientedRanges.x.source = mTouchSource;
2058 mLocked.orientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
2059 mLocked.orientedRanges.y.source = mTouchSource;
2060
Jeff Brown9626b142011-03-03 02:09:54 -08002061 configureVirtualKeysLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002062
Jeff Brown8d608662010-08-30 03:02:23 -07002063 // Scale factor for terms that are not oriented in a particular axis.
2064 // If the pixels are square then xScale == yScale otherwise we fake it
2065 // by choosing an average.
2066 mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002067
Jeff Brown8d608662010-08-30 03:02:23 -07002068 // Size of diagonal axis.
Jeff Brown86ea1f52011-04-12 22:39:53 -07002069 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002070
Jeff Brown8d608662010-08-30 03:02:23 -07002071 // TouchMajor and TouchMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002072 if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) {
2073 mLocked.orientedRanges.haveTouchSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002074
2075 mLocked.orientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
2076 mLocked.orientedRanges.touchMajor.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002077 mLocked.orientedRanges.touchMajor.min = 0;
2078 mLocked.orientedRanges.touchMajor.max = diagonalSize;
2079 mLocked.orientedRanges.touchMajor.flat = 0;
2080 mLocked.orientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002081
Jeff Brown8d608662010-08-30 03:02:23 -07002082 mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor;
Jeff Brownefd32662011-03-08 15:13:06 -08002083 mLocked.orientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002084 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002085
Jeff Brown8d608662010-08-30 03:02:23 -07002086 // ToolMajor and ToolMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002087 mLocked.toolSizeLinearScale = 0;
2088 mLocked.toolSizeLinearBias = 0;
2089 mLocked.toolSizeAreaScale = 0;
2090 mLocked.toolSizeAreaBias = 0;
2091 if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
2092 if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) {
2093 if (mCalibration.haveToolSizeLinearScale) {
2094 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
Jeff Brown8d608662010-08-30 03:02:23 -07002095 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002096 mLocked.toolSizeLinearScale = float(min(width, height))
Jeff Brown8d608662010-08-30 03:02:23 -07002097 / mRawAxes.toolMajor.maxValue;
2098 }
2099
Jeff Brownc6d282b2010-10-14 21:42:15 -07002100 if (mCalibration.haveToolSizeLinearBias) {
2101 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
2102 }
2103 } else if (mCalibration.toolSizeCalibration ==
2104 Calibration::TOOL_SIZE_CALIBRATION_AREA) {
2105 if (mCalibration.haveToolSizeLinearScale) {
2106 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
2107 } else {
2108 mLocked.toolSizeLinearScale = min(width, height);
2109 }
2110
2111 if (mCalibration.haveToolSizeLinearBias) {
2112 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
2113 }
2114
2115 if (mCalibration.haveToolSizeAreaScale) {
2116 mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale;
2117 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
2118 mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue;
2119 }
2120
2121 if (mCalibration.haveToolSizeAreaBias) {
2122 mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -07002123 }
2124 }
2125
Jeff Brownc6d282b2010-10-14 21:42:15 -07002126 mLocked.orientedRanges.haveToolSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002127
2128 mLocked.orientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
2129 mLocked.orientedRanges.toolMajor.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002130 mLocked.orientedRanges.toolMajor.min = 0;
2131 mLocked.orientedRanges.toolMajor.max = diagonalSize;
2132 mLocked.orientedRanges.toolMajor.flat = 0;
2133 mLocked.orientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002134
Jeff Brown8d608662010-08-30 03:02:23 -07002135 mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor;
Jeff Brownefd32662011-03-08 15:13:06 -08002136 mLocked.orientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002137 }
2138
2139 // Pressure factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002140 mLocked.pressureScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002141 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
2142 RawAbsoluteAxisInfo rawPressureAxis;
2143 switch (mCalibration.pressureSource) {
2144 case Calibration::PRESSURE_SOURCE_PRESSURE:
2145 rawPressureAxis = mRawAxes.pressure;
2146 break;
2147 case Calibration::PRESSURE_SOURCE_TOUCH:
2148 rawPressureAxis = mRawAxes.touchMajor;
2149 break;
2150 default:
2151 rawPressureAxis.clear();
2152 }
2153
Jeff Brown8d608662010-08-30 03:02:23 -07002154 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2155 || mCalibration.pressureCalibration
2156 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2157 if (mCalibration.havePressureScale) {
2158 mLocked.pressureScale = mCalibration.pressureScale;
2159 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
2160 mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue;
2161 }
2162 }
2163
2164 mLocked.orientedRanges.havePressure = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002165
2166 mLocked.orientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2167 mLocked.orientedRanges.pressure.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002168 mLocked.orientedRanges.pressure.min = 0;
2169 mLocked.orientedRanges.pressure.max = 1.0;
2170 mLocked.orientedRanges.pressure.flat = 0;
2171 mLocked.orientedRanges.pressure.fuzz = 0;
2172 }
2173
2174 // Size factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002175 mLocked.sizeScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002176 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002177 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
2178 if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
2179 mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue;
2180 }
2181 }
2182
2183 mLocked.orientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002184
2185 mLocked.orientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
2186 mLocked.orientedRanges.size.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002187 mLocked.orientedRanges.size.min = 0;
2188 mLocked.orientedRanges.size.max = 1.0;
2189 mLocked.orientedRanges.size.flat = 0;
2190 mLocked.orientedRanges.size.fuzz = 0;
2191 }
2192
2193 // Orientation
Jeff Brownc6d282b2010-10-14 21:42:15 -07002194 mLocked.orientationScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002195 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002196 if (mCalibration.orientationCalibration
2197 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
2198 if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) {
2199 mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue;
2200 }
2201 }
2202
Jeff Brownb416e242011-05-24 15:17:57 -07002203 mLocked.orientedRanges.haveOrientation = true;
2204
Jeff Brownefd32662011-03-08 15:13:06 -08002205 mLocked.orientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2206 mLocked.orientedRanges.orientation.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002207 mLocked.orientedRanges.orientation.min = - M_PI_2;
2208 mLocked.orientedRanges.orientation.max = M_PI_2;
2209 mLocked.orientedRanges.orientation.flat = 0;
2210 mLocked.orientedRanges.orientation.fuzz = 0;
2211 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002212 }
2213
2214 if (orientationChanged || sizeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002215 // Compute oriented surface dimensions, precision, scales and ranges.
2216 // Note that the maximum value reported is an inclusive maximum value so it is one
2217 // unit less than the total width or height of surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002218 switch (mLocked.surfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002219 case DISPLAY_ORIENTATION_90:
2220 case DISPLAY_ORIENTATION_270:
Jeff Brown6328cdc2010-07-29 18:18:33 -07002221 mLocked.orientedSurfaceWidth = mLocked.surfaceHeight;
2222 mLocked.orientedSurfaceHeight = mLocked.surfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002223
Jeff Brown6328cdc2010-07-29 18:18:33 -07002224 mLocked.orientedXPrecision = mLocked.yPrecision;
2225 mLocked.orientedYPrecision = mLocked.xPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002226
2227 mLocked.orientedRanges.x.min = 0;
2228 mLocked.orientedRanges.x.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue)
2229 * mLocked.yScale;
2230 mLocked.orientedRanges.x.flat = 0;
2231 mLocked.orientedRanges.x.fuzz = mLocked.yScale;
2232
2233 mLocked.orientedRanges.y.min = 0;
2234 mLocked.orientedRanges.y.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue)
2235 * mLocked.xScale;
2236 mLocked.orientedRanges.y.flat = 0;
2237 mLocked.orientedRanges.y.fuzz = mLocked.xScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002238 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002239
Jeff Brown6d0fec22010-07-23 21:28:06 -07002240 default:
Jeff Brown6328cdc2010-07-29 18:18:33 -07002241 mLocked.orientedSurfaceWidth = mLocked.surfaceWidth;
2242 mLocked.orientedSurfaceHeight = mLocked.surfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002243
Jeff Brown6328cdc2010-07-29 18:18:33 -07002244 mLocked.orientedXPrecision = mLocked.xPrecision;
2245 mLocked.orientedYPrecision = mLocked.yPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002246
2247 mLocked.orientedRanges.x.min = 0;
2248 mLocked.orientedRanges.x.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue)
2249 * mLocked.xScale;
2250 mLocked.orientedRanges.x.flat = 0;
2251 mLocked.orientedRanges.x.fuzz = mLocked.xScale;
2252
2253 mLocked.orientedRanges.y.min = 0;
2254 mLocked.orientedRanges.y.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue)
2255 * mLocked.yScale;
2256 mLocked.orientedRanges.y.flat = 0;
2257 mLocked.orientedRanges.y.fuzz = mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002258 break;
2259 }
Jeff Brown96ad3972011-03-09 17:39:48 -08002260
2261 // Compute pointer gesture detection parameters.
2262 // TODO: These factors should not be hardcoded.
2263 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2264 int32_t rawWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2265 int32_t rawHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown86ea1f52011-04-12 22:39:53 -07002266 float rawDiagonal = hypotf(rawWidth, rawHeight);
2267 float displayDiagonal = hypotf(mLocked.associatedDisplayWidth,
2268 mLocked.associatedDisplayHeight);
Jeff Brown96ad3972011-03-09 17:39:48 -08002269
Jeff Brown86ea1f52011-04-12 22:39:53 -07002270 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07002271 // given area relative to the diagonal size of the display when no acceleration
2272 // is applied.
Jeff Brown96ad3972011-03-09 17:39:48 -08002273 // Assume that the touch pad has a square aspect ratio such that movements in
2274 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown214eaf42011-05-26 19:17:02 -07002275 mLocked.pointerGestureXMovementScale = mConfig->pointerGestureMovementSpeedRatio
Jeff Brown86ea1f52011-04-12 22:39:53 -07002276 * displayDiagonal / rawDiagonal;
Jeff Brown96ad3972011-03-09 17:39:48 -08002277 mLocked.pointerGestureYMovementScale = mLocked.pointerGestureXMovementScale;
2278
2279 // Scale zooms to cover a smaller range of the display than movements do.
2280 // This value determines the area around the pointer that is affected by freeform
2281 // pointer gestures.
Jeff Brown214eaf42011-05-26 19:17:02 -07002282 mLocked.pointerGestureXZoomScale = mConfig->pointerGestureZoomSpeedRatio
Jeff Brown86ea1f52011-04-12 22:39:53 -07002283 * displayDiagonal / rawDiagonal;
2284 mLocked.pointerGestureYZoomScale = mLocked.pointerGestureXZoomScale;
Jeff Brown96ad3972011-03-09 17:39:48 -08002285
Jeff Brown86ea1f52011-04-12 22:39:53 -07002286 // Max width between pointers to detect a swipe gesture is more than some fraction
2287 // of the diagonal axis of the touch pad. Touches that are wider than this are
2288 // translated into freeform gestures.
Jeff Brown214eaf42011-05-26 19:17:02 -07002289 mLocked.pointerGestureMaxSwipeWidth =
2290 mConfig->pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brown86ea1f52011-04-12 22:39:53 -07002291
2292 // Reset the current pointer gesture.
2293 mPointerGesture.reset();
2294
2295 // Remove any current spots.
2296 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
2297 mPointerController->clearSpots();
2298 }
Jeff Brown96ad3972011-03-09 17:39:48 -08002299 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002300 }
2301
2302 return true;
2303}
2304
Jeff Brownef3d7e82010-09-30 14:33:04 -07002305void TouchInputMapper::dumpSurfaceLocked(String8& dump) {
2306 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth);
2307 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight);
2308 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07002309}
2310
Jeff Brown6328cdc2010-07-29 18:18:33 -07002311void TouchInputMapper::configureVirtualKeysLocked() {
Jeff Brown8d608662010-08-30 03:02:23 -07002312 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08002313 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002314
Jeff Brown6328cdc2010-07-29 18:18:33 -07002315 mLocked.virtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002316
Jeff Brown6328cdc2010-07-29 18:18:33 -07002317 if (virtualKeyDefinitions.size() == 0) {
2318 return;
2319 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002320
Jeff Brown6328cdc2010-07-29 18:18:33 -07002321 mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size());
2322
Jeff Brown8d608662010-08-30 03:02:23 -07002323 int32_t touchScreenLeft = mRawAxes.x.minValue;
2324 int32_t touchScreenTop = mRawAxes.y.minValue;
Jeff Brown9626b142011-03-03 02:09:54 -08002325 int32_t touchScreenWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2326 int32_t touchScreenHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002327
2328 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07002329 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07002330 virtualKeyDefinitions[i];
2331
2332 mLocked.virtualKeys.add();
2333 VirtualKey& virtualKey = mLocked.virtualKeys.editTop();
2334
2335 virtualKey.scanCode = virtualKeyDefinition.scanCode;
2336 int32_t keyCode;
2337 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08002338 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07002339 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002340 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
2341 virtualKey.scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002342 mLocked.virtualKeys.pop(); // drop the key
2343 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002344 }
2345
Jeff Brown6328cdc2010-07-29 18:18:33 -07002346 virtualKey.keyCode = keyCode;
2347 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002348
Jeff Brown6328cdc2010-07-29 18:18:33 -07002349 // convert the key definition's display coordinates into touch coordinates for a hit box
2350 int32_t halfWidth = virtualKeyDefinition.width / 2;
2351 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002352
Jeff Brown6328cdc2010-07-29 18:18:33 -07002353 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
2354 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
2355 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
2356 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
2357 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
2358 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
2359 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
2360 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07002361 }
2362}
2363
2364void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) {
2365 if (!mLocked.virtualKeys.isEmpty()) {
2366 dump.append(INDENT3 "Virtual Keys:\n");
2367
2368 for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) {
2369 const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i);
2370 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
2371 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
2372 i, virtualKey.scanCode, virtualKey.keyCode,
2373 virtualKey.hitLeft, virtualKey.hitRight,
2374 virtualKey.hitTop, virtualKey.hitBottom);
2375 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002376 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002377}
2378
Jeff Brown8d608662010-08-30 03:02:23 -07002379void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002380 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07002381 Calibration& out = mCalibration;
2382
Jeff Brownc6d282b2010-10-14 21:42:15 -07002383 // Touch Size
2384 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT;
2385 String8 touchSizeCalibrationString;
2386 if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) {
2387 if (touchSizeCalibrationString == "none") {
2388 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
2389 } else if (touchSizeCalibrationString == "geometric") {
2390 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC;
2391 } else if (touchSizeCalibrationString == "pressure") {
2392 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
2393 } else if (touchSizeCalibrationString != "default") {
2394 LOGW("Invalid value for touch.touchSize.calibration: '%s'",
2395 touchSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002396 }
2397 }
2398
Jeff Brownc6d282b2010-10-14 21:42:15 -07002399 // Tool Size
2400 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT;
2401 String8 toolSizeCalibrationString;
2402 if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) {
2403 if (toolSizeCalibrationString == "none") {
2404 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
2405 } else if (toolSizeCalibrationString == "geometric") {
2406 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC;
2407 } else if (toolSizeCalibrationString == "linear") {
2408 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
2409 } else if (toolSizeCalibrationString == "area") {
2410 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA;
2411 } else if (toolSizeCalibrationString != "default") {
2412 LOGW("Invalid value for touch.toolSize.calibration: '%s'",
2413 toolSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002414 }
2415 }
2416
Jeff Brownc6d282b2010-10-14 21:42:15 -07002417 out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"),
2418 out.toolSizeLinearScale);
2419 out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"),
2420 out.toolSizeLinearBias);
2421 out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"),
2422 out.toolSizeAreaScale);
2423 out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"),
2424 out.toolSizeAreaBias);
2425 out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"),
2426 out.toolSizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07002427
2428 // Pressure
2429 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
2430 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002431 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002432 if (pressureCalibrationString == "none") {
2433 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2434 } else if (pressureCalibrationString == "physical") {
2435 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
2436 } else if (pressureCalibrationString == "amplitude") {
2437 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2438 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002439 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002440 pressureCalibrationString.string());
2441 }
2442 }
2443
2444 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
2445 String8 pressureSourceString;
2446 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
2447 if (pressureSourceString == "pressure") {
2448 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2449 } else if (pressureSourceString == "touch") {
2450 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2451 } else if (pressureSourceString != "default") {
2452 LOGW("Invalid value for touch.pressure.source: '%s'",
2453 pressureSourceString.string());
2454 }
2455 }
2456
2457 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
2458 out.pressureScale);
2459
2460 // Size
2461 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
2462 String8 sizeCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002463 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002464 if (sizeCalibrationString == "none") {
2465 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2466 } else if (sizeCalibrationString == "normalized") {
2467 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
2468 } else if (sizeCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002469 LOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002470 sizeCalibrationString.string());
2471 }
2472 }
2473
2474 // Orientation
2475 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
2476 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002477 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002478 if (orientationCalibrationString == "none") {
2479 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2480 } else if (orientationCalibrationString == "interpolated") {
2481 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002482 } else if (orientationCalibrationString == "vector") {
2483 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002484 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002485 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002486 orientationCalibrationString.string());
2487 }
2488 }
2489}
2490
2491void TouchInputMapper::resolveCalibration() {
2492 // Pressure
2493 switch (mCalibration.pressureSource) {
2494 case Calibration::PRESSURE_SOURCE_DEFAULT:
2495 if (mRawAxes.pressure.valid) {
2496 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2497 } else if (mRawAxes.touchMajor.valid) {
2498 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2499 }
2500 break;
2501
2502 case Calibration::PRESSURE_SOURCE_PRESSURE:
2503 if (! mRawAxes.pressure.valid) {
2504 LOGW("Calibration property touch.pressure.source is 'pressure' but "
2505 "the pressure axis is not available.");
2506 }
2507 break;
2508
2509 case Calibration::PRESSURE_SOURCE_TOUCH:
2510 if (! mRawAxes.touchMajor.valid) {
2511 LOGW("Calibration property touch.pressure.source is 'touch' but "
2512 "the touchMajor axis is not available.");
2513 }
2514 break;
2515
2516 default:
2517 break;
2518 }
2519
2520 switch (mCalibration.pressureCalibration) {
2521 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
2522 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
2523 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2524 } else {
2525 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2526 }
2527 break;
2528
2529 default:
2530 break;
2531 }
2532
Jeff Brownc6d282b2010-10-14 21:42:15 -07002533 // Tool Size
2534 switch (mCalibration.toolSizeCalibration) {
2535 case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07002536 if (mRawAxes.toolMajor.valid) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002537 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
Jeff Brown8d608662010-08-30 03:02:23 -07002538 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002539 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002540 }
2541 break;
2542
2543 default:
2544 break;
2545 }
2546
Jeff Brownc6d282b2010-10-14 21:42:15 -07002547 // Touch Size
2548 switch (mCalibration.touchSizeCalibration) {
2549 case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07002550 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
Jeff Brownc6d282b2010-10-14 21:42:15 -07002551 && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
2552 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
Jeff Brown8d608662010-08-30 03:02:23 -07002553 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002554 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002555 }
2556 break;
2557
2558 default:
2559 break;
2560 }
2561
2562 // Size
2563 switch (mCalibration.sizeCalibration) {
2564 case Calibration::SIZE_CALIBRATION_DEFAULT:
2565 if (mRawAxes.toolMajor.valid) {
2566 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
2567 } else {
2568 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2569 }
2570 break;
2571
2572 default:
2573 break;
2574 }
2575
2576 // Orientation
2577 switch (mCalibration.orientationCalibration) {
2578 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
2579 if (mRawAxes.orientation.valid) {
2580 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
2581 } else {
2582 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2583 }
2584 break;
2585
2586 default:
2587 break;
2588 }
2589}
2590
Jeff Brownef3d7e82010-09-30 14:33:04 -07002591void TouchInputMapper::dumpCalibration(String8& dump) {
2592 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07002593
Jeff Brownc6d282b2010-10-14 21:42:15 -07002594 // Touch Size
2595 switch (mCalibration.touchSizeCalibration) {
2596 case Calibration::TOUCH_SIZE_CALIBRATION_NONE:
2597 dump.append(INDENT4 "touch.touchSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002598 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002599 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
2600 dump.append(INDENT4 "touch.touchSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002601 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002602 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
2603 dump.append(INDENT4 "touch.touchSize.calibration: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002604 break;
2605 default:
2606 assert(false);
2607 }
2608
Jeff Brownc6d282b2010-10-14 21:42:15 -07002609 // Tool Size
2610 switch (mCalibration.toolSizeCalibration) {
2611 case Calibration::TOOL_SIZE_CALIBRATION_NONE:
2612 dump.append(INDENT4 "touch.toolSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002613 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002614 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
2615 dump.append(INDENT4 "touch.toolSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002616 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002617 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
2618 dump.append(INDENT4 "touch.toolSize.calibration: linear\n");
2619 break;
2620 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2621 dump.append(INDENT4 "touch.toolSize.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002622 break;
2623 default:
2624 assert(false);
2625 }
2626
Jeff Brownc6d282b2010-10-14 21:42:15 -07002627 if (mCalibration.haveToolSizeLinearScale) {
2628 dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n",
2629 mCalibration.toolSizeLinearScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002630 }
2631
Jeff Brownc6d282b2010-10-14 21:42:15 -07002632 if (mCalibration.haveToolSizeLinearBias) {
2633 dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n",
2634 mCalibration.toolSizeLinearBias);
Jeff Brown8d608662010-08-30 03:02:23 -07002635 }
2636
Jeff Brownc6d282b2010-10-14 21:42:15 -07002637 if (mCalibration.haveToolSizeAreaScale) {
2638 dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n",
2639 mCalibration.toolSizeAreaScale);
2640 }
2641
2642 if (mCalibration.haveToolSizeAreaBias) {
2643 dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n",
2644 mCalibration.toolSizeAreaBias);
2645 }
2646
2647 if (mCalibration.haveToolSizeIsSummed) {
Jeff Brown1f245102010-11-18 20:53:46 -08002648 dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %s\n",
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002649 toString(mCalibration.toolSizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07002650 }
2651
2652 // Pressure
2653 switch (mCalibration.pressureCalibration) {
2654 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002655 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002656 break;
2657 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002658 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002659 break;
2660 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002661 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002662 break;
2663 default:
2664 assert(false);
2665 }
2666
2667 switch (mCalibration.pressureSource) {
2668 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002669 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002670 break;
2671 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002672 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002673 break;
2674 case Calibration::PRESSURE_SOURCE_DEFAULT:
2675 break;
2676 default:
2677 assert(false);
2678 }
2679
2680 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07002681 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
2682 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002683 }
2684
2685 // Size
2686 switch (mCalibration.sizeCalibration) {
2687 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002688 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002689 break;
2690 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002691 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002692 break;
2693 default:
2694 assert(false);
2695 }
2696
2697 // Orientation
2698 switch (mCalibration.orientationCalibration) {
2699 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002700 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002701 break;
2702 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002703 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002704 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002705 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
2706 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
2707 break;
Jeff Brown8d608662010-08-30 03:02:23 -07002708 default:
2709 assert(false);
2710 }
2711}
2712
Jeff Brown6d0fec22010-07-23 21:28:06 -07002713void TouchInputMapper::reset() {
2714 // Synthesize touch up event if touch is currently down.
2715 // This will also take care of finishing virtual key processing if needed.
2716 if (mLastTouch.pointerCount != 0) {
2717 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
2718 mCurrentTouch.clear();
2719 syncTouch(when, true);
2720 }
2721
Jeff Brown6328cdc2010-07-29 18:18:33 -07002722 { // acquire lock
2723 AutoMutex _l(mLock);
2724 initializeLocked();
Jeff Brown86ea1f52011-04-12 22:39:53 -07002725
2726 if (mPointerController != NULL
2727 && mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
2728 mPointerController->clearSpots();
2729 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002730 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07002731
Jeff Brown6328cdc2010-07-29 18:18:33 -07002732 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002733}
2734
2735void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Brown68d60752011-03-17 01:34:19 -07002736#if DEBUG_RAW_EVENTS
2737 if (!havePointerIds) {
2738 LOGD("syncTouch: pointerCount=%d, no pointer ids", mCurrentTouch.pointerCount);
2739 } else {
2740 LOGD("syncTouch: pointerCount=%d, up=0x%08x, down=0x%08x, move=0x%08x, "
2741 "last=0x%08x, current=0x%08x", mCurrentTouch.pointerCount,
2742 mLastTouch.idBits.value & ~mCurrentTouch.idBits.value,
2743 mCurrentTouch.idBits.value & ~mLastTouch.idBits.value,
2744 mLastTouch.idBits.value & mCurrentTouch.idBits.value,
2745 mLastTouch.idBits.value, mCurrentTouch.idBits.value);
2746 }
2747#endif
2748
Jeff Brown6328cdc2010-07-29 18:18:33 -07002749 // Preprocess pointer data.
Jeff Brown6d0fec22010-07-23 21:28:06 -07002750 if (mParameters.useBadTouchFilter) {
2751 if (applyBadTouchFilter()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002752 havePointerIds = false;
2753 }
2754 }
2755
Jeff Brown6d0fec22010-07-23 21:28:06 -07002756 if (mParameters.useJumpyTouchFilter) {
2757 if (applyJumpyTouchFilter()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002758 havePointerIds = false;
2759 }
2760 }
2761
Jeff Brown68d60752011-03-17 01:34:19 -07002762 if (!havePointerIds) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002763 calculatePointerIds();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002764 }
2765
Jeff Brown6d0fec22010-07-23 21:28:06 -07002766 TouchData temp;
2767 TouchData* savedTouch;
2768 if (mParameters.useAveragingTouchFilter) {
2769 temp.copyFrom(mCurrentTouch);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002770 savedTouch = & temp;
2771
Jeff Brown6d0fec22010-07-23 21:28:06 -07002772 applyAveragingTouchFilter();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002773 } else {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002774 savedTouch = & mCurrentTouch;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002775 }
2776
Jeff Brown56194eb2011-03-02 19:23:13 -08002777 uint32_t policyFlags = 0;
Jeff Brown05dc66a2011-03-02 14:41:58 -08002778 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) {
Jeff Brownefd32662011-03-08 15:13:06 -08002779 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
2780 // If this is a touch screen, hide the pointer on an initial down.
2781 getContext()->fadePointer();
2782 }
Jeff Brown56194eb2011-03-02 19:23:13 -08002783
2784 // Initial downs on external touch devices should wake the device.
2785 // We don't do this for internal touch screens to prevent them from waking
2786 // up in your pocket.
2787 // TODO: Use the input device configuration to control this behavior more finely.
2788 if (getDevice()->isExternal()) {
2789 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2790 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002791 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002792
Jeff Brown325bd072011-04-19 21:20:10 -07002793 TouchResult touchResult;
2794 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount == 0
2795 && mLastTouch.buttonState == mCurrentTouch.buttonState) {
2796 // Drop spurious syncs.
2797 touchResult = DROP_STROKE;
2798 } else {
2799 // Process touches and virtual keys.
2800 touchResult = consumeOffScreenTouches(when, policyFlags);
2801 if (touchResult == DISPATCH_TOUCH) {
2802 suppressSwipeOntoVirtualKeys(when);
2803 if (mPointerController != NULL) {
2804 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
2805 }
2806 dispatchTouches(when, policyFlags);
Jeff Brown96ad3972011-03-09 17:39:48 -08002807 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002808 }
2809
Jeff Brown6328cdc2010-07-29 18:18:33 -07002810 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brown96ad3972011-03-09 17:39:48 -08002811 // Keep the button state so we can track edge-triggered button state changes.
Jeff Brown6d0fec22010-07-23 21:28:06 -07002812 if (touchResult == DROP_STROKE) {
2813 mLastTouch.clear();
Jeff Brown96ad3972011-03-09 17:39:48 -08002814 mLastTouch.buttonState = savedTouch->buttonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002815 } else {
2816 mLastTouch.copyFrom(*savedTouch);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002817 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002818}
2819
Jeff Brown325bd072011-04-19 21:20:10 -07002820void TouchInputMapper::timeoutExpired(nsecs_t when) {
2821 if (mPointerController != NULL) {
2822 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
2823 }
2824}
2825
Jeff Brown6d0fec22010-07-23 21:28:06 -07002826TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches(
2827 nsecs_t when, uint32_t policyFlags) {
2828 int32_t keyEventAction, keyEventFlags;
2829 int32_t keyCode, scanCode, downTime;
2830 TouchResult touchResult;
Jeff Brown349703e2010-06-22 01:27:15 -07002831
Jeff Brown6328cdc2010-07-29 18:18:33 -07002832 { // acquire lock
2833 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002834
Jeff Brown6328cdc2010-07-29 18:18:33 -07002835 // Update surface size and orientation, including virtual key positions.
2836 if (! configureSurfaceLocked()) {
2837 return DROP_STROKE;
2838 }
2839
2840 // Check for virtual key press.
2841 if (mLocked.currentVirtualKey.down) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002842 if (mCurrentTouch.pointerCount == 0) {
2843 // Pointer went up while virtual key was down.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002844 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002845#if DEBUG_VIRTUAL_KEYS
2846 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07002847 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002848#endif
2849 keyEventAction = AKEY_EVENT_ACTION_UP;
2850 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2851 touchResult = SKIP_TOUCH;
2852 goto DispatchVirtualKey;
2853 }
2854
2855 if (mCurrentTouch.pointerCount == 1) {
2856 int32_t x = mCurrentTouch.pointers[0].x;
2857 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002858 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
2859 if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002860 // Pointer is still within the space of the virtual key.
2861 return SKIP_TOUCH;
2862 }
2863 }
2864
2865 // Pointer left virtual key area or another pointer also went down.
2866 // Send key cancellation and drop the stroke so subsequent motions will be
2867 // considered fresh downs. This is useful when the user swipes away from the
2868 // virtual key area into the main display surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002869 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002870#if DEBUG_VIRTUAL_KEYS
2871 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07002872 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002873#endif
2874 keyEventAction = AKEY_EVENT_ACTION_UP;
2875 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
2876 | AKEY_EVENT_FLAG_CANCELED;
Jeff Brownc3db8582010-10-20 15:33:38 -07002877
2878 // Check whether the pointer moved inside the display area where we should
2879 // start a new stroke.
2880 int32_t x = mCurrentTouch.pointers[0].x;
2881 int32_t y = mCurrentTouch.pointers[0].y;
2882 if (isPointInsideSurfaceLocked(x, y)) {
2883 mLastTouch.clear();
2884 touchResult = DISPATCH_TOUCH;
2885 } else {
2886 touchResult = DROP_STROKE;
2887 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002888 } else {
2889 if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) {
2890 // Pointer just went down. Handle off-screen touches, if needed.
2891 int32_t x = mCurrentTouch.pointers[0].x;
2892 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002893 if (! isPointInsideSurfaceLocked(x, y)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002894 // If exactly one pointer went down, check for virtual key hit.
2895 // Otherwise we will drop the entire stroke.
2896 if (mCurrentTouch.pointerCount == 1) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002897 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002898 if (virtualKey) {
Jeff Brownfe508922011-01-18 15:10:10 -08002899 if (mContext->shouldDropVirtualKey(when, getDevice(),
2900 virtualKey->keyCode, virtualKey->scanCode)) {
2901 return DROP_STROKE;
2902 }
2903
Jeff Brown6328cdc2010-07-29 18:18:33 -07002904 mLocked.currentVirtualKey.down = true;
2905 mLocked.currentVirtualKey.downTime = when;
2906 mLocked.currentVirtualKey.keyCode = virtualKey->keyCode;
2907 mLocked.currentVirtualKey.scanCode = virtualKey->scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002908#if DEBUG_VIRTUAL_KEYS
2909 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07002910 mLocked.currentVirtualKey.keyCode,
2911 mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002912#endif
2913 keyEventAction = AKEY_EVENT_ACTION_DOWN;
2914 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM
2915 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2916 touchResult = SKIP_TOUCH;
2917 goto DispatchVirtualKey;
2918 }
2919 }
2920 return DROP_STROKE;
2921 }
2922 }
2923 return DISPATCH_TOUCH;
2924 }
2925
2926 DispatchVirtualKey:
2927 // Collect remaining state needed to dispatch virtual key.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002928 keyCode = mLocked.currentVirtualKey.keyCode;
2929 scanCode = mLocked.currentVirtualKey.scanCode;
2930 downTime = mLocked.currentVirtualKey.downTime;
2931 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07002932
2933 // Dispatch virtual key.
2934 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown0eaf3932010-10-01 14:55:30 -07002935 policyFlags |= POLICY_FLAG_VIRTUAL;
Jeff Brownb6997262010-10-08 22:31:17 -07002936 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
2937 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
2938 return touchResult;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002939}
2940
Jeff Brownefd32662011-03-08 15:13:06 -08002941void TouchInputMapper::suppressSwipeOntoVirtualKeys(nsecs_t when) {
Jeff Brownfe508922011-01-18 15:10:10 -08002942 // Disable all virtual key touches that happen within a short time interval of the
2943 // most recent touch. The idea is to filter out stray virtual key presses when
2944 // interacting with the touch screen.
2945 //
2946 // Problems we're trying to solve:
2947 //
2948 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
2949 // virtual key area that is implemented by a separate touch panel and accidentally
2950 // triggers a virtual key.
2951 //
2952 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
2953 // area and accidentally triggers a virtual key. This often happens when virtual keys
2954 // are layed out below the screen near to where the on screen keyboard's space bar
2955 // is displayed.
Jeff Brown214eaf42011-05-26 19:17:02 -07002956 if (mConfig->virtualKeyQuietTime > 0 && mCurrentTouch.pointerCount != 0) {
2957 mContext->disableVirtualKeysUntil(when + mConfig->virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08002958 }
2959}
2960
Jeff Brown6d0fec22010-07-23 21:28:06 -07002961void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
2962 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
2963 uint32_t lastPointerCount = mLastTouch.pointerCount;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002964 if (currentPointerCount == 0 && lastPointerCount == 0) {
2965 return; // nothing to do!
2966 }
2967
Jeff Brown96ad3972011-03-09 17:39:48 -08002968 // Update current touch coordinates.
2969 int32_t edgeFlags;
2970 float xPrecision, yPrecision;
2971 prepareTouches(&edgeFlags, &xPrecision, &yPrecision);
2972
2973 // Dispatch motions.
Jeff Brown6d0fec22010-07-23 21:28:06 -07002974 BitSet32 currentIdBits = mCurrentTouch.idBits;
2975 BitSet32 lastIdBits = mLastTouch.idBits;
Jeff Brown96ad3972011-03-09 17:39:48 -08002976 uint32_t metaState = getContext()->getGlobalMetaState();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002977
2978 if (currentIdBits == lastIdBits) {
2979 // No pointer id changes so this is a move event.
2980 // The dispatcher takes care of batching moves so we don't have to deal with that here.
Jeff Brown96ad3972011-03-09 17:39:48 -08002981 dispatchMotion(when, policyFlags, mTouchSource,
2982 AMOTION_EVENT_ACTION_MOVE, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE,
2983 mCurrentTouchCoords, mCurrentTouch.idToIndex, currentIdBits, -1,
2984 xPrecision, yPrecision, mDownTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002985 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07002986 // There may be pointers going up and pointers going down and pointers moving
2987 // all at the same time.
Jeff Brown96ad3972011-03-09 17:39:48 -08002988 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
2989 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07002990 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brown96ad3972011-03-09 17:39:48 -08002991 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07002992
Jeff Brown96ad3972011-03-09 17:39:48 -08002993 // Update last coordinates of pointers that have moved so that we observe the new
2994 // pointer positions at the same time as other pointers that have just gone up.
2995 bool moveNeeded = updateMovedPointerCoords(
2996 mCurrentTouchCoords, mCurrentTouch.idToIndex,
2997 mLastTouchCoords, mLastTouch.idToIndex,
2998 moveIdBits);
Jeff Brownc3db8582010-10-20 15:33:38 -07002999
Jeff Brown96ad3972011-03-09 17:39:48 -08003000 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003001 while (!upIdBits.isEmpty()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003002 uint32_t upId = upIdBits.firstMarkedBit();
3003 upIdBits.clearBit(upId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003004
Jeff Brown96ad3972011-03-09 17:39:48 -08003005 dispatchMotion(when, policyFlags, mTouchSource,
3006 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, 0,
3007 mLastTouchCoords, mLastTouch.idToIndex, dispatchedIdBits, upId,
3008 xPrecision, yPrecision, mDownTime);
3009 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003010 }
3011
Jeff Brownc3db8582010-10-20 15:33:38 -07003012 // Dispatch move events if any of the remaining pointers moved from their old locations.
3013 // Although applications receive new locations as part of individual pointer up
3014 // events, they do not generally handle them except when presented in a move event.
3015 if (moveNeeded) {
Jeff Brown96ad3972011-03-09 17:39:48 -08003016 assert(moveIdBits.value == dispatchedIdBits.value);
3017 dispatchMotion(when, policyFlags, mTouchSource,
3018 AMOTION_EVENT_ACTION_MOVE, 0, metaState, 0,
3019 mCurrentTouchCoords, mCurrentTouch.idToIndex, dispatchedIdBits, -1,
3020 xPrecision, yPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003021 }
3022
3023 // Dispatch pointer down events using the new pointer locations.
3024 while (!downIdBits.isEmpty()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003025 uint32_t downId = downIdBits.firstMarkedBit();
3026 downIdBits.clearBit(downId);
Jeff Brown96ad3972011-03-09 17:39:48 -08003027 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003028
Jeff Brown96ad3972011-03-09 17:39:48 -08003029 if (dispatchedIdBits.count() == 1) {
3030 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003031 mDownTime = when;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003032 } else {
Jeff Brown96ad3972011-03-09 17:39:48 -08003033 // Only send edge flags with first pointer down.
3034 edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003035 }
3036
Jeff Brown96ad3972011-03-09 17:39:48 -08003037 dispatchMotion(when, policyFlags, mTouchSource,
3038 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, edgeFlags,
3039 mCurrentTouchCoords, mCurrentTouch.idToIndex, dispatchedIdBits, downId,
3040 xPrecision, yPrecision, mDownTime);
3041 }
3042 }
3043
3044 // Update state for next time.
3045 for (uint32_t i = 0; i < currentPointerCount; i++) {
3046 mLastTouchCoords[i].copyFrom(mCurrentTouchCoords[i]);
3047 }
3048}
3049
3050void TouchInputMapper::prepareTouches(int32_t* outEdgeFlags,
3051 float* outXPrecision, float* outYPrecision) {
3052 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
3053 uint32_t lastPointerCount = mLastTouch.pointerCount;
3054
3055 AutoMutex _l(mLock);
3056
3057 // Walk through the the active pointers and map touch screen coordinates (TouchData) into
3058 // display or surface coordinates (PointerCoords) and adjust for display orientation.
3059 for (uint32_t i = 0; i < currentPointerCount; i++) {
3060 const PointerData& in = mCurrentTouch.pointers[i];
3061
3062 // ToolMajor and ToolMinor
3063 float toolMajor, toolMinor;
3064 switch (mCalibration.toolSizeCalibration) {
3065 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
3066 toolMajor = in.toolMajor * mLocked.geometricScale;
3067 if (mRawAxes.toolMinor.valid) {
3068 toolMinor = in.toolMinor * mLocked.geometricScale;
3069 } else {
3070 toolMinor = toolMajor;
3071 }
3072 break;
3073 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
3074 toolMajor = in.toolMajor != 0
3075 ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias
3076 : 0;
3077 if (mRawAxes.toolMinor.valid) {
3078 toolMinor = in.toolMinor != 0
3079 ? in.toolMinor * mLocked.toolSizeLinearScale
3080 + mLocked.toolSizeLinearBias
3081 : 0;
3082 } else {
3083 toolMinor = toolMajor;
3084 }
3085 break;
3086 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
3087 if (in.toolMajor != 0) {
3088 float diameter = sqrtf(in.toolMajor
3089 * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias);
3090 toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias;
3091 } else {
3092 toolMajor = 0;
3093 }
3094 toolMinor = toolMajor;
3095 break;
3096 default:
3097 toolMajor = 0;
3098 toolMinor = 0;
3099 break;
3100 }
3101
3102 if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) {
3103 toolMajor /= currentPointerCount;
3104 toolMinor /= currentPointerCount;
3105 }
3106
3107 // Pressure
3108 float rawPressure;
3109 switch (mCalibration.pressureSource) {
3110 case Calibration::PRESSURE_SOURCE_PRESSURE:
3111 rawPressure = in.pressure;
3112 break;
3113 case Calibration::PRESSURE_SOURCE_TOUCH:
3114 rawPressure = in.touchMajor;
3115 break;
3116 default:
3117 rawPressure = 0;
3118 }
3119
3120 float pressure;
3121 switch (mCalibration.pressureCalibration) {
3122 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3123 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
3124 pressure = rawPressure * mLocked.pressureScale;
3125 break;
3126 default:
3127 pressure = 1;
3128 break;
3129 }
3130
3131 // TouchMajor and TouchMinor
3132 float touchMajor, touchMinor;
3133 switch (mCalibration.touchSizeCalibration) {
3134 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
3135 touchMajor = in.touchMajor * mLocked.geometricScale;
3136 if (mRawAxes.touchMinor.valid) {
3137 touchMinor = in.touchMinor * mLocked.geometricScale;
3138 } else {
3139 touchMinor = touchMajor;
3140 }
3141 break;
3142 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
3143 touchMajor = toolMajor * pressure;
3144 touchMinor = toolMinor * pressure;
3145 break;
3146 default:
3147 touchMajor = 0;
3148 touchMinor = 0;
3149 break;
3150 }
3151
3152 if (touchMajor > toolMajor) {
3153 touchMajor = toolMajor;
3154 }
3155 if (touchMinor > toolMinor) {
3156 touchMinor = toolMinor;
3157 }
3158
3159 // Size
3160 float size;
3161 switch (mCalibration.sizeCalibration) {
3162 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
3163 float rawSize = mRawAxes.toolMinor.valid
3164 ? avg(in.toolMajor, in.toolMinor)
3165 : in.toolMajor;
3166 size = rawSize * mLocked.sizeScale;
3167 break;
3168 }
3169 default:
3170 size = 0;
3171 break;
3172 }
3173
3174 // Orientation
3175 float orientation;
3176 switch (mCalibration.orientationCalibration) {
3177 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
3178 orientation = in.orientation * mLocked.orientationScale;
3179 break;
3180 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3181 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3182 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3183 if (c1 != 0 || c2 != 0) {
3184 orientation = atan2f(c1, c2) * 0.5f;
Jeff Brown86ea1f52011-04-12 22:39:53 -07003185 float scale = 1.0f + hypotf(c1, c2) / 16.0f;
Jeff Brown96ad3972011-03-09 17:39:48 -08003186 touchMajor *= scale;
3187 touchMinor /= scale;
3188 toolMajor *= scale;
3189 toolMinor /= scale;
3190 } else {
3191 orientation = 0;
3192 }
3193 break;
3194 }
3195 default:
3196 orientation = 0;
3197 }
3198
3199 // X and Y
3200 // Adjust coords for surface orientation.
3201 float x, y;
3202 switch (mLocked.surfaceOrientation) {
3203 case DISPLAY_ORIENTATION_90:
3204 x = float(in.y - mRawAxes.y.minValue) * mLocked.yScale;
3205 y = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale;
3206 orientation -= M_PI_2;
3207 if (orientation < - M_PI_2) {
3208 orientation += M_PI;
3209 }
3210 break;
3211 case DISPLAY_ORIENTATION_180:
3212 x = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale;
3213 y = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale;
3214 break;
3215 case DISPLAY_ORIENTATION_270:
3216 x = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale;
3217 y = float(in.x - mRawAxes.x.minValue) * mLocked.xScale;
3218 orientation += M_PI_2;
3219 if (orientation > M_PI_2) {
3220 orientation -= M_PI;
3221 }
3222 break;
3223 default:
3224 x = float(in.x - mRawAxes.x.minValue) * mLocked.xScale;
3225 y = float(in.y - mRawAxes.y.minValue) * mLocked.yScale;
3226 break;
3227 }
3228
3229 // Write output coords.
3230 PointerCoords& out = mCurrentTouchCoords[i];
3231 out.clear();
3232 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3233 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3234 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3235 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3236 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3237 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3238 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3239 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3240 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
3241 }
3242
3243 // Check edge flags by looking only at the first pointer since the flags are
3244 // global to the event.
3245 *outEdgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
3246 if (lastPointerCount == 0 && currentPointerCount > 0) {
3247 const PointerData& in = mCurrentTouch.pointers[0];
3248
3249 if (in.x <= mRawAxes.x.minValue) {
3250 *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_LEFT,
3251 mLocked.surfaceOrientation);
3252 } else if (in.x >= mRawAxes.x.maxValue) {
3253 *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_RIGHT,
3254 mLocked.surfaceOrientation);
3255 }
3256 if (in.y <= mRawAxes.y.minValue) {
3257 *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_TOP,
3258 mLocked.surfaceOrientation);
3259 } else if (in.y >= mRawAxes.y.maxValue) {
3260 *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_BOTTOM,
3261 mLocked.surfaceOrientation);
3262 }
3263 }
3264
3265 *outXPrecision = mLocked.orientedXPrecision;
3266 *outYPrecision = mLocked.orientedYPrecision;
3267}
3268
Jeff Brown325bd072011-04-19 21:20:10 -07003269void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3270 bool isTimeout) {
Jeff Brown96ad3972011-03-09 17:39:48 -08003271 // Update current gesture coordinates.
3272 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown325bd072011-04-19 21:20:10 -07003273 bool sendEvents = preparePointerGestures(when,
3274 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3275 if (!sendEvents) {
3276 return;
3277 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003278 if (finishPreviousGesture) {
3279 cancelPreviousGesture = false;
3280 }
Jeff Brown96ad3972011-03-09 17:39:48 -08003281
Jeff Brown214eaf42011-05-26 19:17:02 -07003282 // Switch pointer presentation.
3283 mPointerController->setPresentation(
3284 mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3285 ? PointerControllerInterface::PRESENTATION_SPOT
3286 : PointerControllerInterface::PRESENTATION_POINTER);
3287
Jeff Brown538881e2011-05-25 18:23:38 -07003288 // Show or hide the pointer if needed.
3289 switch (mPointerGesture.currentGestureMode) {
3290 case PointerGesture::NEUTRAL:
3291 case PointerGesture::QUIET:
3292 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3293 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3294 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
3295 // Remind the user of where the pointer is after finishing a gesture with spots.
3296 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
3297 }
3298 break;
3299 case PointerGesture::TAP:
3300 case PointerGesture::TAP_DRAG:
3301 case PointerGesture::BUTTON_CLICK_OR_DRAG:
3302 case PointerGesture::HOVER:
3303 case PointerGesture::PRESS:
3304 // Unfade the pointer when the current gesture manipulates the
3305 // area directly under the pointer.
3306 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3307 break;
3308 case PointerGesture::SWIPE:
3309 case PointerGesture::FREEFORM:
3310 // Fade the pointer when the current gesture manipulates a different
3311 // area and there are spots to guide the user experience.
3312 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3313 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3314 } else {
3315 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3316 }
3317 break;
Jeff Brown86ea1f52011-04-12 22:39:53 -07003318 }
3319
Jeff Brown96ad3972011-03-09 17:39:48 -08003320 // Send events!
3321 uint32_t metaState = getContext()->getGlobalMetaState();
3322
3323 // Update last coordinates of pointers that have moved so that we observe the new
3324 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown325bd072011-04-19 21:20:10 -07003325 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
3326 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
3327 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown86ea1f52011-04-12 22:39:53 -07003328 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brown96ad3972011-03-09 17:39:48 -08003329 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
3330 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
3331 bool moveNeeded = false;
3332 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown86ea1f52011-04-12 22:39:53 -07003333 && !mPointerGesture.lastGestureIdBits.isEmpty()
3334 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brown96ad3972011-03-09 17:39:48 -08003335 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
3336 & mPointerGesture.lastGestureIdBits.value);
3337 moveNeeded = updateMovedPointerCoords(
3338 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3339 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3340 movedGestureIdBits);
3341 }
3342
3343 // Send motion events for all pointers that went up or were canceled.
3344 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
3345 if (!dispatchedGestureIdBits.isEmpty()) {
3346 if (cancelPreviousGesture) {
3347 dispatchMotion(when, policyFlags, mPointerSource,
3348 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE,
3349 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3350 dispatchedGestureIdBits, -1,
3351 0, 0, mPointerGesture.downTime);
3352
3353 dispatchedGestureIdBits.clear();
3354 } else {
3355 BitSet32 upGestureIdBits;
3356 if (finishPreviousGesture) {
3357 upGestureIdBits = dispatchedGestureIdBits;
3358 } else {
3359 upGestureIdBits.value = dispatchedGestureIdBits.value
3360 & ~mPointerGesture.currentGestureIdBits.value;
3361 }
3362 while (!upGestureIdBits.isEmpty()) {
3363 uint32_t id = upGestureIdBits.firstMarkedBit();
3364 upGestureIdBits.clearBit(id);
3365
3366 dispatchMotion(when, policyFlags, mPointerSource,
3367 AMOTION_EVENT_ACTION_POINTER_UP, 0,
3368 metaState, AMOTION_EVENT_EDGE_FLAG_NONE,
3369 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3370 dispatchedGestureIdBits, id,
3371 0, 0, mPointerGesture.downTime);
3372
3373 dispatchedGestureIdBits.clearBit(id);
3374 }
3375 }
3376 }
3377
3378 // Send motion events for all pointers that moved.
3379 if (moveNeeded) {
3380 dispatchMotion(when, policyFlags, mPointerSource,
3381 AMOTION_EVENT_ACTION_MOVE, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE,
3382 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3383 dispatchedGestureIdBits, -1,
3384 0, 0, mPointerGesture.downTime);
3385 }
3386
3387 // Send motion events for all pointers that went down.
3388 if (down) {
3389 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
3390 & ~dispatchedGestureIdBits.value);
3391 while (!downGestureIdBits.isEmpty()) {
3392 uint32_t id = downGestureIdBits.firstMarkedBit();
3393 downGestureIdBits.clearBit(id);
3394 dispatchedGestureIdBits.markBit(id);
3395
3396 int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
3397 if (dispatchedGestureIdBits.count() == 1) {
3398 // First pointer is going down. Calculate edge flags and set down time.
3399 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
3400 const PointerCoords& downCoords = mPointerGesture.currentGestureCoords[index];
3401 edgeFlags = calculateEdgeFlagsUsingPointerBounds(mPointerController,
3402 downCoords.getAxisValue(AMOTION_EVENT_AXIS_X),
3403 downCoords.getAxisValue(AMOTION_EVENT_AXIS_Y));
3404 mPointerGesture.downTime = when;
3405 }
3406
3407 dispatchMotion(when, policyFlags, mPointerSource,
3408 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, edgeFlags,
3409 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3410 dispatchedGestureIdBits, id,
3411 0, 0, mPointerGesture.downTime);
3412 }
3413 }
3414
Jeff Brown96ad3972011-03-09 17:39:48 -08003415 // Send motion events for hover.
3416 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
3417 dispatchMotion(when, policyFlags, mPointerSource,
3418 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE,
3419 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3420 mPointerGesture.currentGestureIdBits, -1,
3421 0, 0, mPointerGesture.downTime);
3422 }
3423
3424 // Update state.
3425 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
3426 if (!down) {
Jeff Brown96ad3972011-03-09 17:39:48 -08003427 mPointerGesture.lastGestureIdBits.clear();
3428 } else {
Jeff Brown96ad3972011-03-09 17:39:48 -08003429 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
3430 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
3431 uint32_t id = idBits.firstMarkedBit();
3432 idBits.clearBit(id);
3433 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
3434 mPointerGesture.lastGestureCoords[index].copyFrom(
3435 mPointerGesture.currentGestureCoords[index]);
3436 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003437 }
3438 }
3439}
3440
Jeff Brown325bd072011-04-19 21:20:10 -07003441bool TouchInputMapper::preparePointerGestures(nsecs_t when,
3442 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brown96ad3972011-03-09 17:39:48 -08003443 *outCancelPreviousGesture = false;
3444 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003445
Jeff Brown96ad3972011-03-09 17:39:48 -08003446 AutoMutex _l(mLock);
Jeff Brown6328cdc2010-07-29 18:18:33 -07003447
Jeff Brown325bd072011-04-19 21:20:10 -07003448 // Handle TAP timeout.
3449 if (isTimeout) {
3450#if DEBUG_GESTURES
3451 LOGD("Gestures: Processing timeout");
3452#endif
3453
3454 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown214eaf42011-05-26 19:17:02 -07003455 if (when <= mPointerGesture.tapUpTime + mConfig->pointerGestureTapDragInterval) {
Jeff Brown325bd072011-04-19 21:20:10 -07003456 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07003457 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
3458 + mConfig->pointerGestureTapDragInterval);
Jeff Brown325bd072011-04-19 21:20:10 -07003459 } else {
3460 // The tap is finished.
3461#if DEBUG_GESTURES
3462 LOGD("Gestures: TAP finished");
3463#endif
3464 *outFinishPreviousGesture = true;
3465
3466 mPointerGesture.activeGestureId = -1;
3467 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
3468 mPointerGesture.currentGestureIdBits.clear();
3469
Jeff Brown19c97d462011-06-01 12:33:19 -07003470 mPointerGesture.pointerVelocityControl.reset();
3471
Jeff Brown325bd072011-04-19 21:20:10 -07003472 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3473 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_NEUTRAL;
3474 mPointerGesture.spotIdBits.clear();
3475 moveSpotsLocked();
3476 }
3477 return true;
3478 }
3479 }
3480
3481 // We did not handle this timeout.
3482 return false;
3483 }
3484
Jeff Brown96ad3972011-03-09 17:39:48 -08003485 // Update the velocity tracker.
3486 {
3487 VelocityTracker::Position positions[MAX_POINTERS];
3488 uint32_t count = 0;
3489 for (BitSet32 idBits(mCurrentTouch.idBits); !idBits.isEmpty(); count++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003490 uint32_t id = idBits.firstMarkedBit();
3491 idBits.clearBit(id);
Jeff Brown96ad3972011-03-09 17:39:48 -08003492 uint32_t index = mCurrentTouch.idToIndex[id];
3493 positions[count].x = mCurrentTouch.pointers[index].x
3494 * mLocked.pointerGestureXMovementScale;
3495 positions[count].y = mCurrentTouch.pointers[index].y
3496 * mLocked.pointerGestureYMovementScale;
3497 }
3498 mPointerGesture.velocityTracker.addMovement(when, mCurrentTouch.idBits, positions);
3499 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003500
Jeff Brown96ad3972011-03-09 17:39:48 -08003501 // Pick a new active touch id if needed.
3502 // Choose an arbitrary pointer that just went down, if there is one.
3503 // Otherwise choose an arbitrary remaining pointer.
3504 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown86ea1f52011-04-12 22:39:53 -07003505 // We keep the same active touch id for as long as possible.
3506 bool activeTouchChanged = false;
3507 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
3508 int32_t activeTouchId = lastActiveTouchId;
3509 if (activeTouchId < 0) {
3510 if (!mCurrentTouch.idBits.isEmpty()) {
3511 activeTouchChanged = true;
3512 activeTouchId = mPointerGesture.activeTouchId = mCurrentTouch.idBits.firstMarkedBit();
3513 mPointerGesture.firstTouchTime = when;
Jeff Brown96ad3972011-03-09 17:39:48 -08003514 }
Jeff Brown86ea1f52011-04-12 22:39:53 -07003515 } else if (!mCurrentTouch.idBits.hasBit(activeTouchId)) {
3516 activeTouchChanged = true;
3517 if (!mCurrentTouch.idBits.isEmpty()) {
3518 activeTouchId = mPointerGesture.activeTouchId = mCurrentTouch.idBits.firstMarkedBit();
3519 } else {
3520 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brown96ad3972011-03-09 17:39:48 -08003521 }
3522 }
3523
3524 // Determine whether we are in quiet time.
Jeff Brown86ea1f52011-04-12 22:39:53 -07003525 bool isQuietTime = false;
3526 if (activeTouchId < 0) {
3527 mPointerGesture.resetQuietTime();
3528 } else {
Jeff Brown214eaf42011-05-26 19:17:02 -07003529 isQuietTime = when < mPointerGesture.quietTime + mConfig->pointerGestureQuietInterval;
Jeff Brown86ea1f52011-04-12 22:39:53 -07003530 if (!isQuietTime) {
3531 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
3532 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3533 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
3534 && mCurrentTouch.pointerCount < 2) {
3535 // Enter quiet time when exiting swipe or freeform state.
3536 // This is to prevent accidentally entering the hover state and flinging the
3537 // pointer when finishing a swipe and there is still one pointer left onscreen.
3538 isQuietTime = true;
Jeff Brown325bd072011-04-19 21:20:10 -07003539 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown86ea1f52011-04-12 22:39:53 -07003540 && mCurrentTouch.pointerCount >= 2
3541 && !isPointerDown(mCurrentTouch.buttonState)) {
3542 // Enter quiet time when releasing the button and there are still two or more
3543 // fingers down. This may indicate that one finger was used to press the button
3544 // but it has not gone up yet.
3545 isQuietTime = true;
3546 }
3547 if (isQuietTime) {
3548 mPointerGesture.quietTime = when;
3549 }
Jeff Brown96ad3972011-03-09 17:39:48 -08003550 }
3551 }
3552
3553 // Switch states based on button and pointer state.
3554 if (isQuietTime) {
3555 // Case 1: Quiet time. (QUIET)
3556#if DEBUG_GESTURES
3557 LOGD("Gestures: QUIET for next %0.3fms",
3558 (mPointerGesture.quietTime + QUIET_INTERVAL - when) * 0.000001f);
3559#endif
3560 *outFinishPreviousGesture = true;
3561
3562 mPointerGesture.activeGestureId = -1;
3563 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brown96ad3972011-03-09 17:39:48 -08003564 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown86ea1f52011-04-12 22:39:53 -07003565
Jeff Brown19c97d462011-06-01 12:33:19 -07003566 mPointerGesture.pointerVelocityControl.reset();
3567
Jeff Brown86ea1f52011-04-12 22:39:53 -07003568 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3569 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_NEUTRAL;
3570 mPointerGesture.spotIdBits.clear();
3571 moveSpotsLocked();
3572 }
Jeff Brown96ad3972011-03-09 17:39:48 -08003573 } else if (isPointerDown(mCurrentTouch.buttonState)) {
Jeff Brown325bd072011-04-19 21:20:10 -07003574 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brown96ad3972011-03-09 17:39:48 -08003575 // The pointer follows the active touch point.
3576 // Emit DOWN, MOVE, UP events at the pointer location.
3577 //
3578 // Only the active touch matters; other fingers are ignored. This policy helps
3579 // to handle the case where the user places a second finger on the touch pad
3580 // to apply the necessary force to depress an integrated button below the surface.
3581 // We don't want the second finger to be delivered to applications.
3582 //
3583 // For this to work well, we need to make sure to track the pointer that is really
3584 // active. If the user first puts one finger down to click then adds another
3585 // finger to drag then the active pointer should switch to the finger that is
3586 // being dragged.
3587#if DEBUG_GESTURES
Jeff Brown325bd072011-04-19 21:20:10 -07003588 LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brown96ad3972011-03-09 17:39:48 -08003589 "currentTouchPointerCount=%d", activeTouchId, mCurrentTouch.pointerCount);
3590#endif
3591 // Reset state when just starting.
Jeff Brown325bd072011-04-19 21:20:10 -07003592 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brown96ad3972011-03-09 17:39:48 -08003593 *outFinishPreviousGesture = true;
3594 mPointerGesture.activeGestureId = 0;
3595 }
3596
3597 // Switch pointers if needed.
3598 // Find the fastest pointer and follow it.
Jeff Brown19c97d462011-06-01 12:33:19 -07003599 if (activeTouchId >= 0 && mCurrentTouch.pointerCount > 1) {
3600 int32_t bestId = -1;
3601 float bestSpeed = mConfig->pointerGestureDragMinSwitchSpeed;
3602 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
3603 uint32_t id = mCurrentTouch.pointers[i].id;
3604 float vx, vy;
3605 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
3606 float speed = hypotf(vx, vy);
3607 if (speed > bestSpeed) {
3608 bestId = id;
3609 bestSpeed = speed;
Jeff Brown96ad3972011-03-09 17:39:48 -08003610 }
Jeff Brown8d608662010-08-30 03:02:23 -07003611 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003612 }
3613 if (bestId >= 0 && bestId != activeTouchId) {
3614 mPointerGesture.activeTouchId = activeTouchId = bestId;
3615 activeTouchChanged = true;
Jeff Brown96ad3972011-03-09 17:39:48 -08003616#if DEBUG_GESTURES
Jeff Brown19c97d462011-06-01 12:33:19 -07003617 LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
3618 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brown96ad3972011-03-09 17:39:48 -08003619#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07003620 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003621 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003622
Jeff Brown19c97d462011-06-01 12:33:19 -07003623 if (activeTouchId >= 0 && mLastTouch.idBits.hasBit(activeTouchId)) {
3624 const PointerData& currentPointer =
3625 mCurrentTouch.pointers[mCurrentTouch.idToIndex[activeTouchId]];
3626 const PointerData& lastPointer =
3627 mLastTouch.pointers[mLastTouch.idToIndex[activeTouchId]];
3628 float deltaX = (currentPointer.x - lastPointer.x)
3629 * mLocked.pointerGestureXMovementScale;
3630 float deltaY = (currentPointer.y - lastPointer.y)
3631 * mLocked.pointerGestureYMovementScale;
Jeff Brown86ea1f52011-04-12 22:39:53 -07003632
Jeff Brown19c97d462011-06-01 12:33:19 -07003633 mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
3634
3635 // Move the pointer using a relative motion.
3636 // When using spots, the click will occur at the position of the anchor
3637 // spot and all other spots will move there.
3638 mPointerController->move(deltaX, deltaY);
3639 } else {
3640 mPointerGesture.pointerVelocityControl.reset();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003641 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003642
Jeff Brown96ad3972011-03-09 17:39:48 -08003643 float x, y;
3644 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08003645
Jeff Brown325bd072011-04-19 21:20:10 -07003646 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brown96ad3972011-03-09 17:39:48 -08003647 mPointerGesture.currentGestureIdBits.clear();
3648 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3649 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3650 mPointerGesture.currentGestureCoords[0].clear();
3651 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3652 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3653 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown86ea1f52011-04-12 22:39:53 -07003654
Jeff Brown86ea1f52011-04-12 22:39:53 -07003655 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3656 if (activeTouchId >= 0) {
3657 // Collapse all spots into one point at the pointer location.
3658 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_BUTTON_DRAG;
3659 mPointerGesture.spotIdBits.clear();
3660 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
3661 uint32_t id = mCurrentTouch.pointers[i].id;
3662 mPointerGesture.spotIdBits.markBit(id);
3663 mPointerGesture.spotIdToIndex[id] = i;
3664 mPointerGesture.spotCoords[i] = mPointerGesture.currentGestureCoords[0];
3665 }
3666 } else {
3667 // No fingers. Generate a spot at the pointer location so the
3668 // anchor appears to be pressed.
3669 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_BUTTON_CLICK;
3670 mPointerGesture.spotIdBits.clear();
3671 mPointerGesture.spotIdBits.markBit(0);
3672 mPointerGesture.spotIdToIndex[0] = 0;
3673 mPointerGesture.spotCoords[0] = mPointerGesture.currentGestureCoords[0];
3674 }
3675 moveSpotsLocked();
3676 }
Jeff Brown96ad3972011-03-09 17:39:48 -08003677 } else if (mCurrentTouch.pointerCount == 0) {
3678 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
3679 *outFinishPreviousGesture = true;
3680
Jeff Brown325bd072011-04-19 21:20:10 -07003681 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07003682 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brown96ad3972011-03-09 17:39:48 -08003683 bool tapped = false;
Jeff Brown325bd072011-04-19 21:20:10 -07003684 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
3685 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown86ea1f52011-04-12 22:39:53 -07003686 && mLastTouch.pointerCount == 1) {
Jeff Brown214eaf42011-05-26 19:17:02 -07003687 if (when <= mPointerGesture.tapDownTime + mConfig->pointerGestureTapInterval) {
Jeff Brown96ad3972011-03-09 17:39:48 -08003688 float x, y;
3689 mPointerController->getPosition(&x, &y);
Jeff Brown214eaf42011-05-26 19:17:02 -07003690 if (fabs(x - mPointerGesture.tapX) <= mConfig->pointerGestureTapSlop
3691 && fabs(y - mPointerGesture.tapY) <= mConfig->pointerGestureTapSlop) {
Jeff Brown96ad3972011-03-09 17:39:48 -08003692#if DEBUG_GESTURES
3693 LOGD("Gestures: TAP");
3694#endif
Jeff Brown325bd072011-04-19 21:20:10 -07003695
3696 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07003697 getContext()->requestTimeoutAtTime(when
3698 + mConfig->pointerGestureTapDragInterval);
Jeff Brown325bd072011-04-19 21:20:10 -07003699
Jeff Brown96ad3972011-03-09 17:39:48 -08003700 mPointerGesture.activeGestureId = 0;
3701 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brown96ad3972011-03-09 17:39:48 -08003702 mPointerGesture.currentGestureIdBits.clear();
3703 mPointerGesture.currentGestureIdBits.markBit(
3704 mPointerGesture.activeGestureId);
3705 mPointerGesture.currentGestureIdToIndex[
3706 mPointerGesture.activeGestureId] = 0;
3707 mPointerGesture.currentGestureCoords[0].clear();
3708 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown86ea1f52011-04-12 22:39:53 -07003709 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brown96ad3972011-03-09 17:39:48 -08003710 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown86ea1f52011-04-12 22:39:53 -07003711 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brown96ad3972011-03-09 17:39:48 -08003712 mPointerGesture.currentGestureCoords[0].setAxisValue(
3713 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown86ea1f52011-04-12 22:39:53 -07003714
Jeff Brown86ea1f52011-04-12 22:39:53 -07003715 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3716 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_TAP;
3717 mPointerGesture.spotIdBits.clear();
3718 mPointerGesture.spotIdBits.markBit(lastActiveTouchId);
3719 mPointerGesture.spotIdToIndex[lastActiveTouchId] = 0;
3720 mPointerGesture.spotCoords[0] = mPointerGesture.currentGestureCoords[0];
3721 moveSpotsLocked();
3722 }
3723
Jeff Brown96ad3972011-03-09 17:39:48 -08003724 tapped = true;
3725 } else {
3726#if DEBUG_GESTURES
3727 LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown86ea1f52011-04-12 22:39:53 -07003728 x - mPointerGesture.tapX,
3729 y - mPointerGesture.tapY);
Jeff Brown96ad3972011-03-09 17:39:48 -08003730#endif
3731 }
3732 } else {
3733#if DEBUG_GESTURES
Jeff Brown325bd072011-04-19 21:20:10 -07003734 LOGD("Gestures: Not a TAP, %0.3fms since down",
3735 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brown96ad3972011-03-09 17:39:48 -08003736#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07003737 }
Jeff Brown96ad3972011-03-09 17:39:48 -08003738 }
Jeff Brown86ea1f52011-04-12 22:39:53 -07003739
Jeff Brown19c97d462011-06-01 12:33:19 -07003740 mPointerGesture.pointerVelocityControl.reset();
3741
Jeff Brown96ad3972011-03-09 17:39:48 -08003742 if (!tapped) {
3743#if DEBUG_GESTURES
3744 LOGD("Gestures: NEUTRAL");
3745#endif
3746 mPointerGesture.activeGestureId = -1;
3747 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brown96ad3972011-03-09 17:39:48 -08003748 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown86ea1f52011-04-12 22:39:53 -07003749
Jeff Brown86ea1f52011-04-12 22:39:53 -07003750 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3751 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_NEUTRAL;
3752 mPointerGesture.spotIdBits.clear();
3753 moveSpotsLocked();
3754 }
Jeff Brown96ad3972011-03-09 17:39:48 -08003755 }
3756 } else if (mCurrentTouch.pointerCount == 1) {
Jeff Brown325bd072011-04-19 21:20:10 -07003757 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brown96ad3972011-03-09 17:39:48 -08003758 // The pointer follows the active touch point.
Jeff Brown325bd072011-04-19 21:20:10 -07003759 // When in HOVER, emit HOVER_MOVE events at the pointer location.
3760 // When in TAP_DRAG, emit MOVE events at the pointer location.
3761 LOG_ASSERT(activeTouchId >= 0);
Jeff Brown96ad3972011-03-09 17:39:48 -08003762
Jeff Brown325bd072011-04-19 21:20:10 -07003763 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
3764 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown214eaf42011-05-26 19:17:02 -07003765 if (when <= mPointerGesture.tapUpTime + mConfig->pointerGestureTapDragInterval) {
Jeff Brown325bd072011-04-19 21:20:10 -07003766 float x, y;
3767 mPointerController->getPosition(&x, &y);
Jeff Brown214eaf42011-05-26 19:17:02 -07003768 if (fabs(x - mPointerGesture.tapX) <= mConfig->pointerGestureTapSlop
3769 && fabs(y - mPointerGesture.tapY) <= mConfig->pointerGestureTapSlop) {
Jeff Brown325bd072011-04-19 21:20:10 -07003770 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
3771 } else {
Jeff Brown96ad3972011-03-09 17:39:48 -08003772#if DEBUG_GESTURES
Jeff Brown325bd072011-04-19 21:20:10 -07003773 LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
3774 x - mPointerGesture.tapX,
3775 y - mPointerGesture.tapY);
Jeff Brown96ad3972011-03-09 17:39:48 -08003776#endif
Jeff Brown325bd072011-04-19 21:20:10 -07003777 }
3778 } else {
3779#if DEBUG_GESTURES
3780 LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
3781 (when - mPointerGesture.tapUpTime) * 0.000001f);
3782#endif
3783 }
3784 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
3785 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
3786 }
Jeff Brown96ad3972011-03-09 17:39:48 -08003787
3788 if (mLastTouch.idBits.hasBit(activeTouchId)) {
3789 const PointerData& currentPointer =
3790 mCurrentTouch.pointers[mCurrentTouch.idToIndex[activeTouchId]];
3791 const PointerData& lastPointer =
3792 mLastTouch.pointers[mLastTouch.idToIndex[activeTouchId]];
3793 float deltaX = (currentPointer.x - lastPointer.x)
3794 * mLocked.pointerGestureXMovementScale;
3795 float deltaY = (currentPointer.y - lastPointer.y)
3796 * mLocked.pointerGestureYMovementScale;
Jeff Brown86ea1f52011-04-12 22:39:53 -07003797
Jeff Brown19c97d462011-06-01 12:33:19 -07003798 mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
3799
Jeff Brown86ea1f52011-04-12 22:39:53 -07003800 // Move the pointer using a relative motion.
Jeff Brown325bd072011-04-19 21:20:10 -07003801 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brown96ad3972011-03-09 17:39:48 -08003802 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07003803 } else {
3804 mPointerGesture.pointerVelocityControl.reset();
Jeff Brown96ad3972011-03-09 17:39:48 -08003805 }
3806
Jeff Brown325bd072011-04-19 21:20:10 -07003807 bool down;
3808 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
3809#if DEBUG_GESTURES
3810 LOGD("Gestures: TAP_DRAG");
3811#endif
3812 down = true;
3813 } else {
3814#if DEBUG_GESTURES
3815 LOGD("Gestures: HOVER");
3816#endif
3817 *outFinishPreviousGesture = true;
3818 mPointerGesture.activeGestureId = 0;
3819 down = false;
3820 }
Jeff Brown96ad3972011-03-09 17:39:48 -08003821
3822 float x, y;
3823 mPointerController->getPosition(&x, &y);
3824
Jeff Brown96ad3972011-03-09 17:39:48 -08003825 mPointerGesture.currentGestureIdBits.clear();
3826 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3827 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
3828 mPointerGesture.currentGestureCoords[0].clear();
3829 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3830 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown325bd072011-04-19 21:20:10 -07003831 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3832 down ? 1.0f : 0.0f);
3833
Jeff Brown96ad3972011-03-09 17:39:48 -08003834 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) {
Jeff Brown325bd072011-04-19 21:20:10 -07003835 mPointerGesture.resetTap();
3836 mPointerGesture.tapDownTime = when;
Jeff Brown86ea1f52011-04-12 22:39:53 -07003837 mPointerGesture.tapX = x;
3838 mPointerGesture.tapY = y;
3839 }
3840
Jeff Brown86ea1f52011-04-12 22:39:53 -07003841 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
Jeff Brown325bd072011-04-19 21:20:10 -07003842 mPointerGesture.spotGesture = down ? PointerControllerInterface::SPOT_GESTURE_DRAG
3843 : PointerControllerInterface::SPOT_GESTURE_HOVER;
Jeff Brown86ea1f52011-04-12 22:39:53 -07003844 mPointerGesture.spotIdBits.clear();
3845 mPointerGesture.spotIdBits.markBit(activeTouchId);
3846 mPointerGesture.spotIdToIndex[activeTouchId] = 0;
3847 mPointerGesture.spotCoords[0] = mPointerGesture.currentGestureCoords[0];
3848 moveSpotsLocked();
Jeff Brown96ad3972011-03-09 17:39:48 -08003849 }
3850 } else {
Jeff Brown86ea1f52011-04-12 22:39:53 -07003851 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
3852 // We need to provide feedback for each finger that goes down so we cannot wait
3853 // for the fingers to move before deciding what to do.
Jeff Brown96ad3972011-03-09 17:39:48 -08003854 //
Jeff Brown86ea1f52011-04-12 22:39:53 -07003855 // The ambiguous case is deciding what to do when there are two fingers down but they
3856 // have not moved enough to determine whether they are part of a drag or part of a
3857 // freeform gesture, or just a press or long-press at the pointer location.
3858 //
3859 // When there are two fingers we start with the PRESS hypothesis and we generate a
3860 // down at the pointer location.
3861 //
3862 // When the two fingers move enough or when additional fingers are added, we make
3863 // a decision to transition into SWIPE or FREEFORM mode accordingly.
3864 LOG_ASSERT(activeTouchId >= 0);
Jeff Brown96ad3972011-03-09 17:39:48 -08003865
Jeff Brown214eaf42011-05-26 19:17:02 -07003866 bool settled = when >= mPointerGesture.firstTouchTime
3867 + mConfig->pointerGestureMultitouchSettleInterval;
Jeff Brown86ea1f52011-04-12 22:39:53 -07003868 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brown96ad3972011-03-09 17:39:48 -08003869 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
3870 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brown96ad3972011-03-09 17:39:48 -08003871 *outFinishPreviousGesture = true;
Jeff Brown19c97d462011-06-01 12:33:19 -07003872 } else if (!settled && mCurrentTouch.pointerCount > mLastTouch.pointerCount) {
3873 // Additional pointers have gone down but not yet settled.
3874 // Reset the gesture.
3875#if DEBUG_GESTURES
3876 LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
3877 "settle time remaining %0.3fms",
3878 (mPointerGesture.firstTouchTime + MULTITOUCH_SETTLE_INTERVAL - when)
3879 * 0.000001f);
3880#endif
3881 *outCancelPreviousGesture = true;
3882 } else {
3883 // Continue previous gesture.
3884 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
3885 }
3886
3887 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown86ea1f52011-04-12 22:39:53 -07003888 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
3889 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07003890 mPointerGesture.referenceIdBits.clear();
Jeff Brown19c97d462011-06-01 12:33:19 -07003891 mPointerGesture.pointerVelocityControl.reset();
Jeff Brown96ad3972011-03-09 17:39:48 -08003892
Jeff Brown86ea1f52011-04-12 22:39:53 -07003893 if (settled && mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3894 && mLastTouch.idBits.hasBit(mPointerGesture.activeTouchId)) {
3895 // The spot is already visible and has settled, use it as the reference point
3896 // for the gesture. Other spots will be positioned relative to this one.
3897#if DEBUG_GESTURES
3898 LOGD("Gestures: Using active spot as reference for MULTITOUCH, "
3899 "settle time expired %0.3fms ago",
3900 (when - mPointerGesture.firstTouchTime - MULTITOUCH_SETTLE_INTERVAL)
3901 * 0.000001f);
3902#endif
3903 const PointerData& d = mLastTouch.pointers[mLastTouch.idToIndex[
3904 mPointerGesture.activeTouchId]];
3905 mPointerGesture.referenceTouchX = d.x;
3906 mPointerGesture.referenceTouchY = d.y;
3907 const PointerCoords& c = mPointerGesture.spotCoords[mPointerGesture.spotIdToIndex[
3908 mPointerGesture.activeTouchId]];
3909 mPointerGesture.referenceGestureX = c.getAxisValue(AMOTION_EVENT_AXIS_X);
3910 mPointerGesture.referenceGestureY = c.getAxisValue(AMOTION_EVENT_AXIS_Y);
3911 } else {
Jeff Brown19c97d462011-06-01 12:33:19 -07003912 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown86ea1f52011-04-12 22:39:53 -07003913#if DEBUG_GESTURES
3914 LOGD("Gestures: Using centroid as reference for MULTITOUCH, "
3915 "settle time remaining %0.3fms",
3916 (mPointerGesture.firstTouchTime + MULTITOUCH_SETTLE_INTERVAL - when)
3917 * 0.000001f);
3918#endif
Jeff Brown19c97d462011-06-01 12:33:19 -07003919 mCurrentTouch.getCentroid(&mPointerGesture.referenceTouchX,
3920 &mPointerGesture.referenceTouchY);
3921 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
3922 &mPointerGesture.referenceGestureY);
Jeff Brown96ad3972011-03-09 17:39:48 -08003923 }
Jeff Brown86ea1f52011-04-12 22:39:53 -07003924 }
Jeff Brown96ad3972011-03-09 17:39:48 -08003925
Jeff Brown86ea1f52011-04-12 22:39:53 -07003926 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
3927 float d;
3928 if (mCurrentTouch.pointerCount > 2) {
3929 // There are more than two pointers, switch to FREEFORM.
3930#if DEBUG_GESTURES
3931 LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
3932 mCurrentTouch.pointerCount);
3933#endif
3934 *outCancelPreviousGesture = true;
Jeff Brown96ad3972011-03-09 17:39:48 -08003935 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown86ea1f52011-04-12 22:39:53 -07003936 } else if (((d = distance(
3937 mCurrentTouch.pointers[0].x, mCurrentTouch.pointers[0].y,
3938 mCurrentTouch.pointers[1].x, mCurrentTouch.pointers[1].y))
3939 > mLocked.pointerGestureMaxSwipeWidth)) {
3940 // There are two pointers but they are too far apart, switch to FREEFORM.
3941#if DEBUG_GESTURES
3942 LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
3943 d, mLocked.pointerGestureMaxSwipeWidth);
3944#endif
3945 *outCancelPreviousGesture = true;
3946 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
3947 } else {
3948 // There are two pointers. Wait for both pointers to start moving
3949 // before deciding whether this is a SWIPE or FREEFORM gesture.
3950 uint32_t id1 = mCurrentTouch.pointers[0].id;
3951 uint32_t id2 = mCurrentTouch.pointers[1].id;
Jeff Brown96ad3972011-03-09 17:39:48 -08003952
Jeff Brown86ea1f52011-04-12 22:39:53 -07003953 float vx1, vy1, vx2, vy2;
3954 mPointerGesture.velocityTracker.getVelocity(id1, &vx1, &vy1);
3955 mPointerGesture.velocityTracker.getVelocity(id2, &vx2, &vy2);
Jeff Brown96ad3972011-03-09 17:39:48 -08003956
Jeff Brown86ea1f52011-04-12 22:39:53 -07003957 float speed1 = hypotf(vx1, vy1);
3958 float speed2 = hypotf(vx2, vy2);
Jeff Brown214eaf42011-05-26 19:17:02 -07003959 if (speed1 >= mConfig->pointerGestureMultitouchMinSpeed
3960 && speed2 >= mConfig->pointerGestureMultitouchMinSpeed) {
Jeff Brown86ea1f52011-04-12 22:39:53 -07003961 // Calculate the dot product of the velocity vectors.
Jeff Brown96ad3972011-03-09 17:39:48 -08003962 // When the vectors are oriented in approximately the same direction,
3963 // the angle betweeen them is near zero and the cosine of the angle
3964 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
Jeff Brown86ea1f52011-04-12 22:39:53 -07003965 float dot = vx1 * vx2 + vy1 * vy2;
3966 float cosine = dot / (speed1 * speed2); // denominator always > 0
Jeff Brown214eaf42011-05-26 19:17:02 -07003967 if (cosine >= mConfig->pointerGestureSwipeTransitionAngleCosine) {
Jeff Brown86ea1f52011-04-12 22:39:53 -07003968 // Pointers are moving in the same direction. Switch to SWIPE.
3969#if DEBUG_GESTURES
3970 LOGD("Gestures: PRESS transitioned to SWIPE, "
3971 "speed1 %0.3f >= %0.3f, speed2 %0.3f >= %0.3f, "
3972 "cosine %0.3f >= %0.3f",
3973 speed1, MULTITOUCH_MIN_SPEED, speed2, MULTITOUCH_MIN_SPEED,
3974 cosine, SWIPE_TRANSITION_ANGLE_COSINE);
3975#endif
Jeff Brown96ad3972011-03-09 17:39:48 -08003976 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
Jeff Brown86ea1f52011-04-12 22:39:53 -07003977 } else {
3978 // Pointers are moving in different directions. Switch to FREEFORM.
3979#if DEBUG_GESTURES
3980 LOGD("Gestures: PRESS transitioned to FREEFORM, "
3981 "speed1 %0.3f >= %0.3f, speed2 %0.3f >= %0.3f, "
3982 "cosine %0.3f < %0.3f",
3983 speed1, MULTITOUCH_MIN_SPEED, speed2, MULTITOUCH_MIN_SPEED,
3984 cosine, SWIPE_TRANSITION_ANGLE_COSINE);
3985#endif
3986 *outCancelPreviousGesture = true;
3987 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown96ad3972011-03-09 17:39:48 -08003988 }
3989 }
Jeff Brown96ad3972011-03-09 17:39:48 -08003990 }
3991 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown86ea1f52011-04-12 22:39:53 -07003992 // Switch from SWIPE to FREEFORM if additional pointers go down.
3993 // Cancel previous gesture.
3994 if (mCurrentTouch.pointerCount > 2) {
3995#if DEBUG_GESTURES
3996 LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
3997 mCurrentTouch.pointerCount);
3998#endif
Jeff Brown96ad3972011-03-09 17:39:48 -08003999 *outCancelPreviousGesture = true;
4000 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004001 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07004002 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004003
Jeff Brown538881e2011-05-25 18:23:38 -07004004 // Clear the reference deltas for fingers not yet included in the reference calculation.
4005 for (BitSet32 idBits(mCurrentTouch.idBits.value & ~mPointerGesture.referenceIdBits.value);
4006 !idBits.isEmpty(); ) {
4007 uint32_t id = idBits.firstMarkedBit();
4008 idBits.clearBit(id);
4009
4010 mPointerGesture.referenceDeltas[id].dx = 0;
4011 mPointerGesture.referenceDeltas[id].dy = 0;
4012 }
4013 mPointerGesture.referenceIdBits = mCurrentTouch.idBits;
4014
Jeff Brown86ea1f52011-04-12 22:39:53 -07004015 // Move the reference points based on the overall group motion of the fingers.
4016 // The objective is to calculate a vector delta that is common to the movement
4017 // of all fingers.
4018 BitSet32 commonIdBits(mLastTouch.idBits.value & mCurrentTouch.idBits.value);
4019 if (!commonIdBits.isEmpty()) {
4020 float commonDeltaX = 0, commonDeltaY = 0;
4021 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4022 bool first = (idBits == commonIdBits);
4023 uint32_t id = idBits.firstMarkedBit();
4024 idBits.clearBit(id);
4025
4026 const PointerData& cpd = mCurrentTouch.pointers[mCurrentTouch.idToIndex[id]];
4027 const PointerData& lpd = mLastTouch.pointers[mLastTouch.idToIndex[id]];
Jeff Brown538881e2011-05-25 18:23:38 -07004028 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4029 delta.dx += cpd.x - lpd.x;
4030 delta.dy += cpd.y - lpd.y;
Jeff Brown86ea1f52011-04-12 22:39:53 -07004031
4032 if (first) {
Jeff Brown538881e2011-05-25 18:23:38 -07004033 commonDeltaX = delta.dx;
4034 commonDeltaY = delta.dy;
Jeff Brown86ea1f52011-04-12 22:39:53 -07004035 } else {
Jeff Brown538881e2011-05-25 18:23:38 -07004036 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4037 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
Jeff Brown86ea1f52011-04-12 22:39:53 -07004038 }
4039 }
4040
Jeff Brown538881e2011-05-25 18:23:38 -07004041 if (commonDeltaX || commonDeltaY) {
4042 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4043 uint32_t id = idBits.firstMarkedBit();
4044 idBits.clearBit(id);
4045
4046 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4047 delta.dx = 0;
4048 delta.dy = 0;
4049 }
4050
4051 mPointerGesture.referenceTouchX += commonDeltaX;
4052 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown19c97d462011-06-01 12:33:19 -07004053
4054 commonDeltaX *= mLocked.pointerGestureXMovementScale;
4055 commonDeltaY *= mLocked.pointerGestureYMovementScale;
4056 mPointerGesture.pointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
4057
4058 mPointerGesture.referenceGestureX += commonDeltaX;
4059 mPointerGesture.referenceGestureY += commonDeltaY;
4060
Jeff Brown538881e2011-05-25 18:23:38 -07004061 clampPositionUsingPointerBounds(mPointerController,
4062 &mPointerGesture.referenceGestureX,
4063 &mPointerGesture.referenceGestureY);
4064 }
Jeff Brown86ea1f52011-04-12 22:39:53 -07004065 }
4066
4067 // Report gestures.
4068 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4069 // PRESS mode.
Jeff Brown96ad3972011-03-09 17:39:48 -08004070#if DEBUG_GESTURES
Jeff Brown86ea1f52011-04-12 22:39:53 -07004071 LOGD("Gestures: PRESS activeTouchId=%d,"
Jeff Brown96ad3972011-03-09 17:39:48 -08004072 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown86ea1f52011-04-12 22:39:53 -07004073 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
Jeff Brown96ad3972011-03-09 17:39:48 -08004074#endif
Jeff Brown86ea1f52011-04-12 22:39:53 -07004075 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brown96ad3972011-03-09 17:39:48 -08004076
Jeff Brown96ad3972011-03-09 17:39:48 -08004077 mPointerGesture.currentGestureIdBits.clear();
4078 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4079 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
4080 mPointerGesture.currentGestureCoords[0].clear();
Jeff Brown86ea1f52011-04-12 22:39:53 -07004081 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4082 mPointerGesture.referenceGestureX);
4083 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4084 mPointerGesture.referenceGestureY);
Jeff Brown96ad3972011-03-09 17:39:48 -08004085 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown86ea1f52011-04-12 22:39:53 -07004086
Jeff Brown86ea1f52011-04-12 22:39:53 -07004087 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4088 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_PRESS;
4089 }
4090 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4091 // SWIPE mode.
4092#if DEBUG_GESTURES
4093 LOGD("Gestures: SWIPE activeTouchId=%d,"
4094 "activeGestureId=%d, currentTouchPointerCount=%d",
4095 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
4096#endif
4097 assert(mPointerGesture.activeGestureId >= 0);
4098
4099 mPointerGesture.currentGestureIdBits.clear();
4100 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4101 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
4102 mPointerGesture.currentGestureCoords[0].clear();
4103 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4104 mPointerGesture.referenceGestureX);
4105 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4106 mPointerGesture.referenceGestureY);
4107 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4108
Jeff Brown86ea1f52011-04-12 22:39:53 -07004109 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4110 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_SWIPE;
4111 }
Jeff Brown96ad3972011-03-09 17:39:48 -08004112 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4113 // FREEFORM mode.
4114#if DEBUG_GESTURES
4115 LOGD("Gestures: FREEFORM activeTouchId=%d,"
4116 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown86ea1f52011-04-12 22:39:53 -07004117 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
Jeff Brown96ad3972011-03-09 17:39:48 -08004118#endif
4119 assert(mPointerGesture.activeGestureId >= 0);
4120
Jeff Brown96ad3972011-03-09 17:39:48 -08004121 mPointerGesture.currentGestureIdBits.clear();
4122
4123 BitSet32 mappedTouchIdBits;
4124 BitSet32 usedGestureIdBits;
4125 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4126 // Initially, assign the active gesture id to the active touch point
4127 // if there is one. No other touch id bits are mapped yet.
4128 if (!*outCancelPreviousGesture) {
4129 mappedTouchIdBits.markBit(activeTouchId);
4130 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4131 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4132 mPointerGesture.activeGestureId;
4133 } else {
4134 mPointerGesture.activeGestureId = -1;
4135 }
4136 } else {
4137 // Otherwise, assume we mapped all touches from the previous frame.
4138 // Reuse all mappings that are still applicable.
4139 mappedTouchIdBits.value = mLastTouch.idBits.value & mCurrentTouch.idBits.value;
4140 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4141
4142 // Check whether we need to choose a new active gesture id because the
4143 // current went went up.
4144 for (BitSet32 upTouchIdBits(mLastTouch.idBits.value & ~mCurrentTouch.idBits.value);
4145 !upTouchIdBits.isEmpty(); ) {
4146 uint32_t upTouchId = upTouchIdBits.firstMarkedBit();
4147 upTouchIdBits.clearBit(upTouchId);
4148 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4149 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4150 mPointerGesture.activeGestureId = -1;
4151 break;
4152 }
4153 }
4154 }
4155
4156#if DEBUG_GESTURES
4157 LOGD("Gestures: FREEFORM follow up "
4158 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4159 "activeGestureId=%d",
4160 mappedTouchIdBits.value, usedGestureIdBits.value,
4161 mPointerGesture.activeGestureId);
4162#endif
4163
Jeff Brown86ea1f52011-04-12 22:39:53 -07004164 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
Jeff Brown96ad3972011-03-09 17:39:48 -08004165 uint32_t touchId = mCurrentTouch.pointers[i].id;
4166 uint32_t gestureId;
4167 if (!mappedTouchIdBits.hasBit(touchId)) {
4168 gestureId = usedGestureIdBits.firstUnmarkedBit();
4169 usedGestureIdBits.markBit(gestureId);
4170 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4171#if DEBUG_GESTURES
4172 LOGD("Gestures: FREEFORM "
4173 "new mapping for touch id %d -> gesture id %d",
4174 touchId, gestureId);
4175#endif
4176 } else {
4177 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4178#if DEBUG_GESTURES
4179 LOGD("Gestures: FREEFORM "
4180 "existing mapping for touch id %d -> gesture id %d",
4181 touchId, gestureId);
4182#endif
4183 }
4184 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4185 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4186
Jeff Brown86ea1f52011-04-12 22:39:53 -07004187 float x = (mCurrentTouch.pointers[i].x - mPointerGesture.referenceTouchX)
4188 * mLocked.pointerGestureXZoomScale + mPointerGesture.referenceGestureX;
4189 float y = (mCurrentTouch.pointers[i].y - mPointerGesture.referenceTouchY)
4190 * mLocked.pointerGestureYZoomScale + mPointerGesture.referenceGestureY;
Jeff Brown96ad3972011-03-09 17:39:48 -08004191
4192 mPointerGesture.currentGestureCoords[i].clear();
4193 mPointerGesture.currentGestureCoords[i].setAxisValue(
4194 AMOTION_EVENT_AXIS_X, x);
4195 mPointerGesture.currentGestureCoords[i].setAxisValue(
4196 AMOTION_EVENT_AXIS_Y, y);
4197 mPointerGesture.currentGestureCoords[i].setAxisValue(
4198 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4199 }
4200
4201 if (mPointerGesture.activeGestureId < 0) {
4202 mPointerGesture.activeGestureId =
4203 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4204#if DEBUG_GESTURES
4205 LOGD("Gestures: FREEFORM new "
4206 "activeGestureId=%d", mPointerGesture.activeGestureId);
4207#endif
4208 }
Jeff Brown96ad3972011-03-09 17:39:48 -08004209
Jeff Brown86ea1f52011-04-12 22:39:53 -07004210 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4211 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_FREEFORM;
4212 }
4213 }
4214
4215 // Update spot locations for PRESS, SWIPE and FREEFORM.
4216 // We use the same calculation as we do to calculate the gesture pointers
4217 // for FREEFORM so that the spots smoothly track gestures.
4218 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4219 mPointerGesture.spotIdBits.clear();
4220 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
4221 uint32_t id = mCurrentTouch.pointers[i].id;
4222 mPointerGesture.spotIdBits.markBit(id);
4223 mPointerGesture.spotIdToIndex[id] = i;
4224
4225 float x = (mCurrentTouch.pointers[i].x - mPointerGesture.referenceTouchX)
4226 * mLocked.pointerGestureXZoomScale + mPointerGesture.referenceGestureX;
4227 float y = (mCurrentTouch.pointers[i].y - mPointerGesture.referenceTouchY)
4228 * mLocked.pointerGestureYZoomScale + mPointerGesture.referenceGestureY;
4229
4230 mPointerGesture.spotCoords[i].clear();
4231 mPointerGesture.spotCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4232 mPointerGesture.spotCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4233 mPointerGesture.spotCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4234 }
4235 moveSpotsLocked();
4236 }
Jeff Brown96ad3972011-03-09 17:39:48 -08004237 }
4238
Jeff Brown4e3f7202011-05-31 15:00:18 -07004239 mPointerController->setButtonState(mCurrentTouch.buttonState);
4240
Jeff Brown96ad3972011-03-09 17:39:48 -08004241#if DEBUG_GESTURES
4242 LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown86ea1f52011-04-12 22:39:53 -07004243 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4244 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brown96ad3972011-03-09 17:39:48 -08004245 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown86ea1f52011-04-12 22:39:53 -07004246 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4247 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brown96ad3972011-03-09 17:39:48 -08004248 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
4249 uint32_t id = idBits.firstMarkedBit();
4250 idBits.clearBit(id);
4251 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
4252 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
4253 LOGD(" currentGesture[%d]: index=%d, x=%0.3f, y=%0.3f, pressure=%0.3f",
4254 id, index, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
4255 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4256 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4257 }
4258 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
4259 uint32_t id = idBits.firstMarkedBit();
4260 idBits.clearBit(id);
4261 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
4262 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
4263 LOGD(" lastGesture[%d]: index=%d, x=%0.3f, y=%0.3f, pressure=%0.3f",
4264 id, index, coords.getAxisValue(AMOTION_EVENT_AXIS_X),
4265 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4266 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4267 }
4268#endif
Jeff Brown325bd072011-04-19 21:20:10 -07004269 return true;
Jeff Brown96ad3972011-03-09 17:39:48 -08004270}
4271
Jeff Brown86ea1f52011-04-12 22:39:53 -07004272void TouchInputMapper::moveSpotsLocked() {
4273 mPointerController->setSpots(mPointerGesture.spotGesture,
4274 mPointerGesture.spotCoords, mPointerGesture.spotIdToIndex, mPointerGesture.spotIdBits);
4275}
4276
Jeff Brown96ad3972011-03-09 17:39:48 -08004277void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
4278 int32_t action, int32_t flags, uint32_t metaState, int32_t edgeFlags,
4279 const PointerCoords* coords, const uint32_t* idToIndex, BitSet32 idBits,
4280 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
4281 PointerCoords pointerCoords[MAX_POINTERS];
4282 int32_t pointerIds[MAX_POINTERS];
4283 uint32_t pointerCount = 0;
4284 while (!idBits.isEmpty()) {
4285 uint32_t id = idBits.firstMarkedBit();
4286 idBits.clearBit(id);
4287 uint32_t index = idToIndex[id];
4288 pointerIds[pointerCount] = id;
4289 pointerCoords[pointerCount].copyFrom(coords[index]);
4290
4291 if (changedId >= 0 && id == uint32_t(changedId)) {
4292 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
4293 }
4294
4295 pointerCount += 1;
4296 }
4297
4298 assert(pointerCount != 0);
4299
4300 if (changedId >= 0 && pointerCount == 1) {
4301 // Replace initial down and final up action.
4302 // We can compare the action without masking off the changed pointer index
4303 // because we know the index is 0.
4304 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
4305 action = AMOTION_EVENT_ACTION_DOWN;
4306 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
4307 action = AMOTION_EVENT_ACTION_UP;
4308 } else {
4309 // Can't happen.
4310 assert(false);
4311 }
4312 }
4313
4314 getDispatcher()->notifyMotion(when, getDeviceId(), source, policyFlags,
4315 action, flags, metaState, edgeFlags,
4316 pointerCount, pointerIds, pointerCoords, xPrecision, yPrecision, downTime);
4317}
4318
4319bool TouchInputMapper::updateMovedPointerCoords(
4320 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
4321 PointerCoords* outCoords, const uint32_t* outIdToIndex, BitSet32 idBits) const {
4322 bool changed = false;
4323 while (!idBits.isEmpty()) {
4324 uint32_t id = idBits.firstMarkedBit();
4325 idBits.clearBit(id);
4326
4327 uint32_t inIndex = inIdToIndex[id];
4328 uint32_t outIndex = outIdToIndex[id];
4329 const PointerCoords& curInCoords = inCoords[inIndex];
4330 PointerCoords& curOutCoords = outCoords[outIndex];
4331
4332 if (curInCoords != curOutCoords) {
4333 curOutCoords.copyFrom(curInCoords);
4334 changed = true;
4335 }
4336 }
4337 return changed;
4338}
4339
4340void TouchInputMapper::fadePointer() {
4341 { // acquire lock
4342 AutoMutex _l(mLock);
4343 if (mPointerController != NULL) {
Jeff Brown538881e2011-05-25 18:23:38 -07004344 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
Jeff Brown96ad3972011-03-09 17:39:48 -08004345 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004346 } // release lock
Jeff Brown46b9ac02010-04-22 18:58:52 -07004347}
4348
Jeff Brown6328cdc2010-07-29 18:18:33 -07004349bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) {
Jeff Brown9626b142011-03-03 02:09:54 -08004350 return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue
4351 && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004352}
4353
Jeff Brown6328cdc2010-07-29 18:18:33 -07004354const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked(
4355 int32_t x, int32_t y) {
4356 size_t numVirtualKeys = mLocked.virtualKeys.size();
4357 for (size_t i = 0; i < numVirtualKeys; i++) {
4358 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004359
4360#if DEBUG_VIRTUAL_KEYS
4361 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
4362 "left=%d, top=%d, right=%d, bottom=%d",
4363 x, y,
4364 virtualKey.keyCode, virtualKey.scanCode,
4365 virtualKey.hitLeft, virtualKey.hitTop,
4366 virtualKey.hitRight, virtualKey.hitBottom);
4367#endif
4368
4369 if (virtualKey.isHit(x, y)) {
4370 return & virtualKey;
4371 }
4372 }
4373
4374 return NULL;
4375}
4376
4377void TouchInputMapper::calculatePointerIds() {
4378 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
4379 uint32_t lastPointerCount = mLastTouch.pointerCount;
4380
4381 if (currentPointerCount == 0) {
4382 // No pointers to assign.
4383 mCurrentTouch.idBits.clear();
4384 } else if (lastPointerCount == 0) {
4385 // All pointers are new.
4386 mCurrentTouch.idBits.clear();
4387 for (uint32_t i = 0; i < currentPointerCount; i++) {
4388 mCurrentTouch.pointers[i].id = i;
4389 mCurrentTouch.idToIndex[i] = i;
4390 mCurrentTouch.idBits.markBit(i);
4391 }
4392 } else if (currentPointerCount == 1 && lastPointerCount == 1) {
4393 // Only one pointer and no change in count so it must have the same id as before.
4394 uint32_t id = mLastTouch.pointers[0].id;
4395 mCurrentTouch.pointers[0].id = id;
4396 mCurrentTouch.idToIndex[id] = 0;
4397 mCurrentTouch.idBits.value = BitSet32::valueForBit(id);
4398 } else {
4399 // General case.
4400 // We build a heap of squared euclidean distances between current and last pointers
4401 // associated with the current and last pointer indices. Then, we find the best
4402 // match (by distance) for each current pointer.
4403 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
4404
4405 uint32_t heapSize = 0;
4406 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
4407 currentPointerIndex++) {
4408 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
4409 lastPointerIndex++) {
4410 int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x
4411 - mLastTouch.pointers[lastPointerIndex].x;
4412 int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y
4413 - mLastTouch.pointers[lastPointerIndex].y;
4414
4415 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
4416
4417 // Insert new element into the heap (sift up).
4418 heap[heapSize].currentPointerIndex = currentPointerIndex;
4419 heap[heapSize].lastPointerIndex = lastPointerIndex;
4420 heap[heapSize].distance = distance;
4421 heapSize += 1;
4422 }
4423 }
4424
4425 // Heapify
4426 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
4427 startIndex -= 1;
4428 for (uint32_t parentIndex = startIndex; ;) {
4429 uint32_t childIndex = parentIndex * 2 + 1;
4430 if (childIndex >= heapSize) {
4431 break;
4432 }
4433
4434 if (childIndex + 1 < heapSize
4435 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4436 childIndex += 1;
4437 }
4438
4439 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4440 break;
4441 }
4442
4443 swap(heap[parentIndex], heap[childIndex]);
4444 parentIndex = childIndex;
4445 }
4446 }
4447
4448#if DEBUG_POINTER_ASSIGNMENT
4449 LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize);
4450 for (size_t i = 0; i < heapSize; i++) {
4451 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4452 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4453 heap[i].distance);
4454 }
4455#endif
4456
4457 // Pull matches out by increasing order of distance.
4458 // To avoid reassigning pointers that have already been matched, the loop keeps track
4459 // of which last and current pointers have been matched using the matchedXXXBits variables.
4460 // It also tracks the used pointer id bits.
4461 BitSet32 matchedLastBits(0);
4462 BitSet32 matchedCurrentBits(0);
4463 BitSet32 usedIdBits(0);
4464 bool first = true;
4465 for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) {
4466 for (;;) {
4467 if (first) {
4468 // The first time through the loop, we just consume the root element of
4469 // the heap (the one with smallest distance).
4470 first = false;
4471 } else {
4472 // Previous iterations consumed the root element of the heap.
4473 // Pop root element off of the heap (sift down).
4474 heapSize -= 1;
4475 assert(heapSize > 0);
4476
4477 // Sift down.
4478 heap[0] = heap[heapSize];
4479 for (uint32_t parentIndex = 0; ;) {
4480 uint32_t childIndex = parentIndex * 2 + 1;
4481 if (childIndex >= heapSize) {
4482 break;
4483 }
4484
4485 if (childIndex + 1 < heapSize
4486 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4487 childIndex += 1;
4488 }
4489
4490 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4491 break;
4492 }
4493
4494 swap(heap[parentIndex], heap[childIndex]);
4495 parentIndex = childIndex;
4496 }
4497
4498#if DEBUG_POINTER_ASSIGNMENT
4499 LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize);
4500 for (size_t i = 0; i < heapSize; i++) {
4501 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4502 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4503 heap[i].distance);
4504 }
4505#endif
4506 }
4507
4508 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
4509 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
4510
4511 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
4512 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
4513
4514 matchedCurrentBits.markBit(currentPointerIndex);
4515 matchedLastBits.markBit(lastPointerIndex);
4516
4517 uint32_t id = mLastTouch.pointers[lastPointerIndex].id;
4518 mCurrentTouch.pointers[currentPointerIndex].id = id;
4519 mCurrentTouch.idToIndex[id] = currentPointerIndex;
4520 usedIdBits.markBit(id);
4521
4522#if DEBUG_POINTER_ASSIGNMENT
4523 LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
4524 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
4525#endif
4526 break;
4527 }
4528 }
4529
4530 // Assign fresh ids to new pointers.
4531 if (currentPointerCount > lastPointerCount) {
4532 for (uint32_t i = currentPointerCount - lastPointerCount; ;) {
4533 uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit();
4534 uint32_t id = usedIdBits.firstUnmarkedBit();
4535
4536 mCurrentTouch.pointers[currentPointerIndex].id = id;
4537 mCurrentTouch.idToIndex[id] = currentPointerIndex;
4538 usedIdBits.markBit(id);
4539
4540#if DEBUG_POINTER_ASSIGNMENT
4541 LOGD("calculatePointerIds - assigned: cur=%d, id=%d",
4542 currentPointerIndex, id);
4543#endif
4544
4545 if (--i == 0) break; // done
4546 matchedCurrentBits.markBit(currentPointerIndex);
4547 }
4548 }
4549
4550 // Fix id bits.
4551 mCurrentTouch.idBits = usedIdBits;
4552 }
4553}
4554
4555/* Special hack for devices that have bad screen data: if one of the
4556 * points has moved more than a screen height from the last position,
4557 * then drop it. */
4558bool TouchInputMapper::applyBadTouchFilter() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004559 uint32_t pointerCount = mCurrentTouch.pointerCount;
4560
4561 // Nothing to do if there are no points.
4562 if (pointerCount == 0) {
4563 return false;
4564 }
4565
4566 // Don't do anything if a finger is going down or up. We run
4567 // here before assigning pointer IDs, so there isn't a good
4568 // way to do per-finger matching.
4569 if (pointerCount != mLastTouch.pointerCount) {
4570 return false;
4571 }
4572
4573 // We consider a single movement across more than a 7/16 of
4574 // the long size of the screen to be bad. This was a magic value
4575 // determined by looking at the maximum distance it is feasible
4576 // to actually move in one sample.
Jeff Brown9626b142011-03-03 02:09:54 -08004577 int32_t maxDeltaY = (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1) * 7 / 16;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004578
4579 // XXX The original code in InputDevice.java included commented out
4580 // code for testing the X axis. Note that when we drop a point
4581 // we don't actually restore the old X either. Strange.
4582 // The old code also tries to track when bad points were previously
4583 // detected but it turns out that due to the placement of a "break"
4584 // at the end of the loop, we never set mDroppedBadPoint to true
4585 // so it is effectively dead code.
4586 // Need to figure out if the old code is busted or just overcomplicated
4587 // but working as intended.
4588
4589 // Look through all new points and see if any are farther than
4590 // acceptable from all previous points.
4591 for (uint32_t i = pointerCount; i-- > 0; ) {
4592 int32_t y = mCurrentTouch.pointers[i].y;
4593 int32_t closestY = INT_MAX;
4594 int32_t closestDeltaY = 0;
4595
4596#if DEBUG_HACKS
4597 LOGD("BadTouchFilter: Looking at next point #%d: y=%d", i, y);
4598#endif
4599
4600 for (uint32_t j = pointerCount; j-- > 0; ) {
4601 int32_t lastY = mLastTouch.pointers[j].y;
4602 int32_t deltaY = abs(y - lastY);
4603
4604#if DEBUG_HACKS
4605 LOGD("BadTouchFilter: Comparing with last point #%d: y=%d deltaY=%d",
4606 j, lastY, deltaY);
4607#endif
4608
4609 if (deltaY < maxDeltaY) {
4610 goto SkipSufficientlyClosePoint;
4611 }
4612 if (deltaY < closestDeltaY) {
4613 closestDeltaY = deltaY;
4614 closestY = lastY;
4615 }
4616 }
4617
4618 // Must not have found a close enough match.
4619#if DEBUG_HACKS
4620 LOGD("BadTouchFilter: Dropping bad point #%d: newY=%d oldY=%d deltaY=%d maxDeltaY=%d",
4621 i, y, closestY, closestDeltaY, maxDeltaY);
4622#endif
4623
4624 mCurrentTouch.pointers[i].y = closestY;
4625 return true; // XXX original code only corrects one point
4626
4627 SkipSufficientlyClosePoint: ;
4628 }
4629
4630 // No change.
4631 return false;
4632}
4633
4634/* Special hack for devices that have bad screen data: drop points where
4635 * the coordinate value for one axis has jumped to the other pointer's location.
4636 */
4637bool TouchInputMapper::applyJumpyTouchFilter() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004638 uint32_t pointerCount = mCurrentTouch.pointerCount;
4639 if (mLastTouch.pointerCount != pointerCount) {
4640#if DEBUG_HACKS
4641 LOGD("JumpyTouchFilter: Different pointer count %d -> %d",
4642 mLastTouch.pointerCount, pointerCount);
4643 for (uint32_t i = 0; i < pointerCount; i++) {
4644 LOGD(" Pointer %d (%d, %d)", i,
4645 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
4646 }
4647#endif
4648
4649 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_TRANSITION_DROPS) {
4650 if (mLastTouch.pointerCount == 1 && pointerCount == 2) {
4651 // Just drop the first few events going from 1 to 2 pointers.
4652 // They're bad often enough that they're not worth considering.
4653 mCurrentTouch.pointerCount = 1;
4654 mJumpyTouchFilter.jumpyPointsDropped += 1;
4655
4656#if DEBUG_HACKS
4657 LOGD("JumpyTouchFilter: Pointer 2 dropped");
4658#endif
4659 return true;
4660 } else if (mLastTouch.pointerCount == 2 && pointerCount == 1) {
4661 // The event when we go from 2 -> 1 tends to be messed up too
4662 mCurrentTouch.pointerCount = 2;
4663 mCurrentTouch.pointers[0] = mLastTouch.pointers[0];
4664 mCurrentTouch.pointers[1] = mLastTouch.pointers[1];
4665 mJumpyTouchFilter.jumpyPointsDropped += 1;
4666
4667#if DEBUG_HACKS
4668 for (int32_t i = 0; i < 2; i++) {
4669 LOGD("JumpyTouchFilter: Pointer %d replaced (%d, %d)", i,
4670 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
4671 }
4672#endif
4673 return true;
4674 }
4675 }
4676 // Reset jumpy points dropped on other transitions or if limit exceeded.
4677 mJumpyTouchFilter.jumpyPointsDropped = 0;
4678
4679#if DEBUG_HACKS
4680 LOGD("JumpyTouchFilter: Transition - drop limit reset");
4681#endif
4682 return false;
4683 }
4684
4685 // We have the same number of pointers as last time.
4686 // A 'jumpy' point is one where the coordinate value for one axis
4687 // has jumped to the other pointer's location. No need to do anything
4688 // else if we only have one pointer.
4689 if (pointerCount < 2) {
4690 return false;
4691 }
4692
4693 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_DROP_LIMIT) {
Jeff Brown9626b142011-03-03 02:09:54 -08004694 int jumpyEpsilon = (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1) / JUMPY_EPSILON_DIVISOR;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004695
4696 // We only replace the single worst jumpy point as characterized by pointer distance
4697 // in a single axis.
4698 int32_t badPointerIndex = -1;
4699 int32_t badPointerReplacementIndex = -1;
4700 int32_t badPointerDistance = INT_MIN; // distance to be corrected
4701
4702 for (uint32_t i = pointerCount; i-- > 0; ) {
4703 int32_t x = mCurrentTouch.pointers[i].x;
4704 int32_t y = mCurrentTouch.pointers[i].y;
4705
4706#if DEBUG_HACKS
4707 LOGD("JumpyTouchFilter: Point %d (%d, %d)", i, x, y);
4708#endif
4709
4710 // Check if a touch point is too close to another's coordinates
4711 bool dropX = false, dropY = false;
4712 for (uint32_t j = 0; j < pointerCount; j++) {
4713 if (i == j) {
4714 continue;
4715 }
4716
4717 if (abs(x - mCurrentTouch.pointers[j].x) <= jumpyEpsilon) {
4718 dropX = true;
4719 break;
4720 }
4721
4722 if (abs(y - mCurrentTouch.pointers[j].y) <= jumpyEpsilon) {
4723 dropY = true;
4724 break;
4725 }
4726 }
4727 if (! dropX && ! dropY) {
4728 continue; // not jumpy
4729 }
4730
4731 // Find a replacement candidate by comparing with older points on the
4732 // complementary (non-jumpy) axis.
4733 int32_t distance = INT_MIN; // distance to be corrected
4734 int32_t replacementIndex = -1;
4735
4736 if (dropX) {
4737 // X looks too close. Find an older replacement point with a close Y.
4738 int32_t smallestDeltaY = INT_MAX;
4739 for (uint32_t j = 0; j < pointerCount; j++) {
4740 int32_t deltaY = abs(y - mLastTouch.pointers[j].y);
4741 if (deltaY < smallestDeltaY) {
4742 smallestDeltaY = deltaY;
4743 replacementIndex = j;
4744 }
4745 }
4746 distance = abs(x - mLastTouch.pointers[replacementIndex].x);
4747 } else {
4748 // Y looks too close. Find an older replacement point with a close X.
4749 int32_t smallestDeltaX = INT_MAX;
4750 for (uint32_t j = 0; j < pointerCount; j++) {
4751 int32_t deltaX = abs(x - mLastTouch.pointers[j].x);
4752 if (deltaX < smallestDeltaX) {
4753 smallestDeltaX = deltaX;
4754 replacementIndex = j;
4755 }
4756 }
4757 distance = abs(y - mLastTouch.pointers[replacementIndex].y);
4758 }
4759
4760 // If replacing this pointer would correct a worse error than the previous ones
4761 // considered, then use this replacement instead.
4762 if (distance > badPointerDistance) {
4763 badPointerIndex = i;
4764 badPointerReplacementIndex = replacementIndex;
4765 badPointerDistance = distance;
4766 }
4767 }
4768
4769 // Correct the jumpy pointer if one was found.
4770 if (badPointerIndex >= 0) {
4771#if DEBUG_HACKS
4772 LOGD("JumpyTouchFilter: Replacing bad pointer %d with (%d, %d)",
4773 badPointerIndex,
4774 mLastTouch.pointers[badPointerReplacementIndex].x,
4775 mLastTouch.pointers[badPointerReplacementIndex].y);
4776#endif
4777
4778 mCurrentTouch.pointers[badPointerIndex].x =
4779 mLastTouch.pointers[badPointerReplacementIndex].x;
4780 mCurrentTouch.pointers[badPointerIndex].y =
4781 mLastTouch.pointers[badPointerReplacementIndex].y;
4782 mJumpyTouchFilter.jumpyPointsDropped += 1;
4783 return true;
4784 }
4785 }
4786
4787 mJumpyTouchFilter.jumpyPointsDropped = 0;
4788 return false;
4789}
4790
4791/* Special hack for devices that have bad screen data: aggregate and
4792 * compute averages of the coordinate data, to reduce the amount of
4793 * jitter seen by applications. */
4794void TouchInputMapper::applyAveragingTouchFilter() {
4795 for (uint32_t currentIndex = 0; currentIndex < mCurrentTouch.pointerCount; currentIndex++) {
4796 uint32_t id = mCurrentTouch.pointers[currentIndex].id;
4797 int32_t x = mCurrentTouch.pointers[currentIndex].x;
4798 int32_t y = mCurrentTouch.pointers[currentIndex].y;
Jeff Brown8d608662010-08-30 03:02:23 -07004799 int32_t pressure;
4800 switch (mCalibration.pressureSource) {
4801 case Calibration::PRESSURE_SOURCE_PRESSURE:
4802 pressure = mCurrentTouch.pointers[currentIndex].pressure;
4803 break;
4804 case Calibration::PRESSURE_SOURCE_TOUCH:
4805 pressure = mCurrentTouch.pointers[currentIndex].touchMajor;
4806 break;
4807 default:
4808 pressure = 1;
4809 break;
4810 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004811
4812 if (mLastTouch.idBits.hasBit(id)) {
4813 // Pointer was down before and is still down now.
4814 // Compute average over history trace.
4815 uint32_t start = mAveragingTouchFilter.historyStart[id];
4816 uint32_t end = mAveragingTouchFilter.historyEnd[id];
4817
4818 int64_t deltaX = x - mAveragingTouchFilter.historyData[end].pointers[id].x;
4819 int64_t deltaY = y - mAveragingTouchFilter.historyData[end].pointers[id].y;
4820 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
4821
4822#if DEBUG_HACKS
4823 LOGD("AveragingTouchFilter: Pointer id %d - Distance from last sample: %lld",
4824 id, distance);
4825#endif
4826
4827 if (distance < AVERAGING_DISTANCE_LIMIT) {
4828 // Increment end index in preparation for recording new historical data.
4829 end += 1;
4830 if (end > AVERAGING_HISTORY_SIZE) {
4831 end = 0;
4832 }
4833
4834 // If the end index has looped back to the start index then we have filled
4835 // the historical trace up to the desired size so we drop the historical
4836 // data at the start of the trace.
4837 if (end == start) {
4838 start += 1;
4839 if (start > AVERAGING_HISTORY_SIZE) {
4840 start = 0;
4841 }
4842 }
4843
4844 // Add the raw data to the historical trace.
4845 mAveragingTouchFilter.historyStart[id] = start;
4846 mAveragingTouchFilter.historyEnd[id] = end;
4847 mAveragingTouchFilter.historyData[end].pointers[id].x = x;
4848 mAveragingTouchFilter.historyData[end].pointers[id].y = y;
4849 mAveragingTouchFilter.historyData[end].pointers[id].pressure = pressure;
4850
4851 // Average over all historical positions in the trace by total pressure.
4852 int32_t averagedX = 0;
4853 int32_t averagedY = 0;
4854 int32_t totalPressure = 0;
4855 for (;;) {
4856 int32_t historicalX = mAveragingTouchFilter.historyData[start].pointers[id].x;
4857 int32_t historicalY = mAveragingTouchFilter.historyData[start].pointers[id].y;
4858 int32_t historicalPressure = mAveragingTouchFilter.historyData[start]
4859 .pointers[id].pressure;
4860
4861 averagedX += historicalX * historicalPressure;
4862 averagedY += historicalY * historicalPressure;
4863 totalPressure += historicalPressure;
4864
4865 if (start == end) {
4866 break;
4867 }
4868
4869 start += 1;
4870 if (start > AVERAGING_HISTORY_SIZE) {
4871 start = 0;
4872 }
4873 }
4874
Jeff Brown8d608662010-08-30 03:02:23 -07004875 if (totalPressure != 0) {
4876 averagedX /= totalPressure;
4877 averagedY /= totalPressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004878
4879#if DEBUG_HACKS
Jeff Brown8d608662010-08-30 03:02:23 -07004880 LOGD("AveragingTouchFilter: Pointer id %d - "
4881 "totalPressure=%d, averagedX=%d, averagedY=%d", id, totalPressure,
4882 averagedX, averagedY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004883#endif
4884
Jeff Brown8d608662010-08-30 03:02:23 -07004885 mCurrentTouch.pointers[currentIndex].x = averagedX;
4886 mCurrentTouch.pointers[currentIndex].y = averagedY;
4887 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004888 } else {
4889#if DEBUG_HACKS
4890 LOGD("AveragingTouchFilter: Pointer id %d - Exceeded max distance", id);
4891#endif
4892 }
4893 } else {
4894#if DEBUG_HACKS
4895 LOGD("AveragingTouchFilter: Pointer id %d - Pointer went up", id);
4896#endif
4897 }
4898
4899 // Reset pointer history.
4900 mAveragingTouchFilter.historyStart[id] = 0;
4901 mAveragingTouchFilter.historyEnd[id] = 0;
4902 mAveragingTouchFilter.historyData[0].pointers[id].x = x;
4903 mAveragingTouchFilter.historyData[0].pointers[id].y = y;
4904 mAveragingTouchFilter.historyData[0].pointers[id].pressure = pressure;
4905 }
4906}
4907
4908int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07004909 { // acquire lock
4910 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004911
Jeff Brown6328cdc2010-07-29 18:18:33 -07004912 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004913 return AKEY_STATE_VIRTUAL;
4914 }
4915
Jeff Brown6328cdc2010-07-29 18:18:33 -07004916 size_t numVirtualKeys = mLocked.virtualKeys.size();
4917 for (size_t i = 0; i < numVirtualKeys; i++) {
4918 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004919 if (virtualKey.keyCode == keyCode) {
4920 return AKEY_STATE_UP;
4921 }
4922 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004923 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07004924
4925 return AKEY_STATE_UNKNOWN;
4926}
4927
4928int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07004929 { // acquire lock
4930 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004931
Jeff Brown6328cdc2010-07-29 18:18:33 -07004932 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004933 return AKEY_STATE_VIRTUAL;
4934 }
4935
Jeff Brown6328cdc2010-07-29 18:18:33 -07004936 size_t numVirtualKeys = mLocked.virtualKeys.size();
4937 for (size_t i = 0; i < numVirtualKeys; i++) {
4938 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004939 if (virtualKey.scanCode == scanCode) {
4940 return AKEY_STATE_UP;
4941 }
4942 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004943 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07004944
4945 return AKEY_STATE_UNKNOWN;
4946}
4947
4948bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
4949 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07004950 { // acquire lock
4951 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004952
Jeff Brown6328cdc2010-07-29 18:18:33 -07004953 size_t numVirtualKeys = mLocked.virtualKeys.size();
4954 for (size_t i = 0; i < numVirtualKeys; i++) {
4955 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004956
4957 for (size_t i = 0; i < numCodes; i++) {
4958 if (virtualKey.keyCode == keyCodes[i]) {
4959 outFlags[i] = 1;
4960 }
4961 }
4962 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004963 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07004964
4965 return true;
4966}
4967
4968
4969// --- SingleTouchInputMapper ---
4970
Jeff Brown47e6b1b2010-11-29 17:37:49 -08004971SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
4972 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004973 initialize();
4974}
4975
4976SingleTouchInputMapper::~SingleTouchInputMapper() {
4977}
4978
4979void SingleTouchInputMapper::initialize() {
4980 mAccumulator.clear();
4981
4982 mDown = false;
4983 mX = 0;
4984 mY = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07004985 mPressure = 0; // default to 0 for devices that don't report pressure
4986 mToolWidth = 0; // default to 0 for devices that don't report tool width
Jeff Brown96ad3972011-03-09 17:39:48 -08004987 mButtonState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004988}
4989
4990void SingleTouchInputMapper::reset() {
4991 TouchInputMapper::reset();
4992
Jeff Brown6d0fec22010-07-23 21:28:06 -07004993 initialize();
4994 }
4995
4996void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
4997 switch (rawEvent->type) {
4998 case EV_KEY:
4999 switch (rawEvent->scanCode) {
5000 case BTN_TOUCH:
5001 mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH;
5002 mAccumulator.btnTouch = rawEvent->value != 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005003 // Don't sync immediately. Wait until the next SYN_REPORT since we might
5004 // not have received valid position information yet. This logic assumes that
5005 // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet.
Jeff Brown6d0fec22010-07-23 21:28:06 -07005006 break;
Jeff Brown96ad3972011-03-09 17:39:48 -08005007 default:
5008 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
5009 uint32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode);
5010 if (buttonState) {
5011 if (rawEvent->value) {
5012 mAccumulator.buttonDown |= buttonState;
5013 } else {
5014 mAccumulator.buttonUp |= buttonState;
5015 }
5016 mAccumulator.fields |= Accumulator::FIELD_BUTTONS;
5017 }
5018 }
5019 break;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005020 }
5021 break;
5022
5023 case EV_ABS:
5024 switch (rawEvent->scanCode) {
5025 case ABS_X:
5026 mAccumulator.fields |= Accumulator::FIELD_ABS_X;
5027 mAccumulator.absX = rawEvent->value;
5028 break;
5029 case ABS_Y:
5030 mAccumulator.fields |= Accumulator::FIELD_ABS_Y;
5031 mAccumulator.absY = rawEvent->value;
5032 break;
5033 case ABS_PRESSURE:
5034 mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE;
5035 mAccumulator.absPressure = rawEvent->value;
5036 break;
5037 case ABS_TOOL_WIDTH:
5038 mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH;
5039 mAccumulator.absToolWidth = rawEvent->value;
5040 break;
5041 }
5042 break;
5043
5044 case EV_SYN:
5045 switch (rawEvent->scanCode) {
5046 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005047 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005048 break;
5049 }
5050 break;
5051 }
5052}
5053
5054void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005055 uint32_t fields = mAccumulator.fields;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005056 if (fields == 0) {
5057 return; // no new state changes, so nothing to do
5058 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005059
5060 if (fields & Accumulator::FIELD_BTN_TOUCH) {
5061 mDown = mAccumulator.btnTouch;
5062 }
5063
5064 if (fields & Accumulator::FIELD_ABS_X) {
5065 mX = mAccumulator.absX;
5066 }
5067
5068 if (fields & Accumulator::FIELD_ABS_Y) {
5069 mY = mAccumulator.absY;
5070 }
5071
5072 if (fields & Accumulator::FIELD_ABS_PRESSURE) {
5073 mPressure = mAccumulator.absPressure;
5074 }
5075
5076 if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) {
Jeff Brown8d608662010-08-30 03:02:23 -07005077 mToolWidth = mAccumulator.absToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005078 }
5079
Jeff Brown96ad3972011-03-09 17:39:48 -08005080 if (fields & Accumulator::FIELD_BUTTONS) {
5081 mButtonState = (mButtonState | mAccumulator.buttonDown) & ~mAccumulator.buttonUp;
5082 }
5083
Jeff Brown6d0fec22010-07-23 21:28:06 -07005084 mCurrentTouch.clear();
5085
5086 if (mDown) {
5087 mCurrentTouch.pointerCount = 1;
5088 mCurrentTouch.pointers[0].id = 0;
5089 mCurrentTouch.pointers[0].x = mX;
5090 mCurrentTouch.pointers[0].y = mY;
5091 mCurrentTouch.pointers[0].pressure = mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -07005092 mCurrentTouch.pointers[0].touchMajor = 0;
5093 mCurrentTouch.pointers[0].touchMinor = 0;
5094 mCurrentTouch.pointers[0].toolMajor = mToolWidth;
5095 mCurrentTouch.pointers[0].toolMinor = mToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005096 mCurrentTouch.pointers[0].orientation = 0;
5097 mCurrentTouch.idToIndex[0] = 0;
5098 mCurrentTouch.idBits.markBit(0);
Jeff Brown96ad3972011-03-09 17:39:48 -08005099 mCurrentTouch.buttonState = mButtonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005100 }
5101
5102 syncTouch(when, true);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005103
5104 mAccumulator.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005105}
5106
Jeff Brown8d608662010-08-30 03:02:23 -07005107void SingleTouchInputMapper::configureRawAxes() {
5108 TouchInputMapper::configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005109
Jeff Brown8d608662010-08-30 03:02:23 -07005110 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x);
5111 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y);
5112 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure);
5113 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005114}
5115
5116
5117// --- MultiTouchInputMapper ---
5118
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005119MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
5120 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005121 initialize();
5122}
5123
5124MultiTouchInputMapper::~MultiTouchInputMapper() {
5125}
5126
5127void MultiTouchInputMapper::initialize() {
5128 mAccumulator.clear();
Jeff Brown96ad3972011-03-09 17:39:48 -08005129 mButtonState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005130}
5131
5132void MultiTouchInputMapper::reset() {
5133 TouchInputMapper::reset();
5134
Jeff Brown6d0fec22010-07-23 21:28:06 -07005135 initialize();
5136}
5137
5138void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
5139 switch (rawEvent->type) {
Jeff Brown96ad3972011-03-09 17:39:48 -08005140 case EV_KEY: {
5141 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
5142 uint32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode);
5143 if (buttonState) {
5144 if (rawEvent->value) {
5145 mAccumulator.buttonDown |= buttonState;
5146 } else {
5147 mAccumulator.buttonUp |= buttonState;
5148 }
5149 }
5150 }
5151 break;
5152 }
5153
Jeff Brown6d0fec22010-07-23 21:28:06 -07005154 case EV_ABS: {
5155 uint32_t pointerIndex = mAccumulator.pointerCount;
5156 Accumulator::Pointer* pointer = & mAccumulator.pointers[pointerIndex];
5157
5158 switch (rawEvent->scanCode) {
5159 case ABS_MT_POSITION_X:
5160 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_X;
5161 pointer->absMTPositionX = rawEvent->value;
5162 break;
5163 case ABS_MT_POSITION_Y:
5164 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y;
5165 pointer->absMTPositionY = rawEvent->value;
5166 break;
5167 case ABS_MT_TOUCH_MAJOR:
5168 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR;
5169 pointer->absMTTouchMajor = rawEvent->value;
5170 break;
5171 case ABS_MT_TOUCH_MINOR:
5172 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR;
5173 pointer->absMTTouchMinor = rawEvent->value;
5174 break;
5175 case ABS_MT_WIDTH_MAJOR:
5176 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR;
5177 pointer->absMTWidthMajor = rawEvent->value;
5178 break;
5179 case ABS_MT_WIDTH_MINOR:
5180 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR;
5181 pointer->absMTWidthMinor = rawEvent->value;
5182 break;
5183 case ABS_MT_ORIENTATION:
5184 pointer->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION;
5185 pointer->absMTOrientation = rawEvent->value;
5186 break;
5187 case ABS_MT_TRACKING_ID:
5188 pointer->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID;
5189 pointer->absMTTrackingId = rawEvent->value;
5190 break;
Jeff Brown8d608662010-08-30 03:02:23 -07005191 case ABS_MT_PRESSURE:
5192 pointer->fields |= Accumulator::FIELD_ABS_MT_PRESSURE;
5193 pointer->absMTPressure = rawEvent->value;
5194 break;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005195 }
5196 break;
5197 }
5198
5199 case EV_SYN:
5200 switch (rawEvent->scanCode) {
5201 case SYN_MT_REPORT: {
5202 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
5203 uint32_t pointerIndex = mAccumulator.pointerCount;
5204
5205 if (mAccumulator.pointers[pointerIndex].fields) {
5206 if (pointerIndex == MAX_POINTERS) {
5207 LOGW("MultiTouch device driver returned more than maximum of %d pointers.",
5208 MAX_POINTERS);
5209 } else {
5210 pointerIndex += 1;
5211 mAccumulator.pointerCount = pointerIndex;
5212 }
5213 }
5214
5215 mAccumulator.pointers[pointerIndex].clear();
5216 break;
5217 }
5218
5219 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005220 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005221 break;
5222 }
5223 break;
5224 }
5225}
5226
5227void MultiTouchInputMapper::sync(nsecs_t when) {
5228 static const uint32_t REQUIRED_FIELDS =
Jeff Brown8d608662010-08-30 03:02:23 -07005229 Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005230
Jeff Brown6d0fec22010-07-23 21:28:06 -07005231 uint32_t inCount = mAccumulator.pointerCount;
5232 uint32_t outCount = 0;
5233 bool havePointerIds = true;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005234
Jeff Brown6d0fec22010-07-23 21:28:06 -07005235 mCurrentTouch.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005236
Jeff Brown6d0fec22010-07-23 21:28:06 -07005237 for (uint32_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005238 const Accumulator::Pointer& inPointer = mAccumulator.pointers[inIndex];
5239 uint32_t fields = inPointer.fields;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005240
Jeff Brown6d0fec22010-07-23 21:28:06 -07005241 if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005242 // Some drivers send empty MT sync packets without X / Y to indicate a pointer up.
5243 // Drop this finger.
Jeff Brown46b9ac02010-04-22 18:58:52 -07005244 continue;
5245 }
5246
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005247 PointerData& outPointer = mCurrentTouch.pointers[outCount];
5248 outPointer.x = inPointer.absMTPositionX;
5249 outPointer.y = inPointer.absMTPositionY;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005250
Jeff Brown8d608662010-08-30 03:02:23 -07005251 if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) {
5252 if (inPointer.absMTPressure <= 0) {
Jeff Brownc3db8582010-10-20 15:33:38 -07005253 // Some devices send sync packets with X / Y but with a 0 pressure to indicate
5254 // a pointer going up. Drop this finger.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005255 continue;
5256 }
Jeff Brown8d608662010-08-30 03:02:23 -07005257 outPointer.pressure = inPointer.absMTPressure;
5258 } else {
5259 // Default pressure to 0 if absent.
5260 outPointer.pressure = 0;
5261 }
5262
5263 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) {
5264 if (inPointer.absMTTouchMajor <= 0) {
5265 // Some devices send sync packets with X / Y but with a 0 touch major to indicate
5266 // a pointer going up. Drop this finger.
5267 continue;
5268 }
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005269 outPointer.touchMajor = inPointer.absMTTouchMajor;
5270 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005271 // Default touch area to 0 if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005272 outPointer.touchMajor = 0;
5273 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005274
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005275 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) {
5276 outPointer.touchMinor = inPointer.absMTTouchMinor;
5277 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005278 // Assume touch area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005279 outPointer.touchMinor = outPointer.touchMajor;
5280 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005281
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005282 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) {
5283 outPointer.toolMajor = inPointer.absMTWidthMajor;
5284 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005285 // Default tool area to 0 if absent.
5286 outPointer.toolMajor = 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005287 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005288
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005289 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) {
5290 outPointer.toolMinor = inPointer.absMTWidthMinor;
5291 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005292 // Assume tool area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005293 outPointer.toolMinor = outPointer.toolMajor;
5294 }
5295
5296 if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) {
5297 outPointer.orientation = inPointer.absMTOrientation;
5298 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005299 // Default orientation to vertical if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005300 outPointer.orientation = 0;
5301 }
5302
Jeff Brown8d608662010-08-30 03:02:23 -07005303 // Assign pointer id using tracking id if available.
Jeff Brown6d0fec22010-07-23 21:28:06 -07005304 if (havePointerIds) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005305 if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) {
5306 uint32_t id = uint32_t(inPointer.absMTTrackingId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07005307
Jeff Brown6d0fec22010-07-23 21:28:06 -07005308 if (id > MAX_POINTER_ID) {
5309#if DEBUG_POINTERS
5310 LOGD("Pointers: Ignoring driver provided pointer id %d because "
Jeff Brown01ce2e92010-09-26 22:20:12 -07005311 "it is larger than max supported id %d",
Jeff Brown6d0fec22010-07-23 21:28:06 -07005312 id, MAX_POINTER_ID);
5313#endif
5314 havePointerIds = false;
5315 }
5316 else {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005317 outPointer.id = id;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005318 mCurrentTouch.idToIndex[id] = outCount;
5319 mCurrentTouch.idBits.markBit(id);
5320 }
5321 } else {
5322 havePointerIds = false;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005323 }
5324 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005325
Jeff Brown6d0fec22010-07-23 21:28:06 -07005326 outCount += 1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005327 }
5328
Jeff Brown6d0fec22010-07-23 21:28:06 -07005329 mCurrentTouch.pointerCount = outCount;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005330
Jeff Brown96ad3972011-03-09 17:39:48 -08005331 mButtonState = (mButtonState | mAccumulator.buttonDown) & ~mAccumulator.buttonUp;
5332 mCurrentTouch.buttonState = mButtonState;
5333
Jeff Brown6d0fec22010-07-23 21:28:06 -07005334 syncTouch(when, havePointerIds);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005335
5336 mAccumulator.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005337}
5338
Jeff Brown8d608662010-08-30 03:02:23 -07005339void MultiTouchInputMapper::configureRawAxes() {
5340 TouchInputMapper::configureRawAxes();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005341
Jeff Brown8d608662010-08-30 03:02:23 -07005342 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, & mRawAxes.x);
5343 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, & mRawAxes.y);
5344 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, & mRawAxes.touchMajor);
5345 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, & mRawAxes.touchMinor);
5346 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, & mRawAxes.toolMajor);
5347 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, & mRawAxes.toolMinor);
5348 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, & mRawAxes.orientation);
5349 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, & mRawAxes.pressure);
Jeff Brown9c3cda02010-06-15 01:31:58 -07005350}
5351
Jeff Brown46b9ac02010-04-22 18:58:52 -07005352
Jeff Browncb1404e2011-01-15 18:14:15 -08005353// --- JoystickInputMapper ---
5354
5355JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5356 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005357}
5358
5359JoystickInputMapper::~JoystickInputMapper() {
5360}
5361
5362uint32_t JoystickInputMapper::getSources() {
5363 return AINPUT_SOURCE_JOYSTICK;
5364}
5365
5366void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5367 InputMapper::populateDeviceInfo(info);
5368
Jeff Brown6f2fba42011-02-19 01:08:02 -08005369 for (size_t i = 0; i < mAxes.size(); i++) {
5370 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005371 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5372 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005373 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005374 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5375 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005376 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005377 }
5378}
5379
5380void JoystickInputMapper::dump(String8& dump) {
5381 dump.append(INDENT2 "Joystick Input Mapper:\n");
5382
Jeff Brown6f2fba42011-02-19 01:08:02 -08005383 dump.append(INDENT3 "Axes:\n");
5384 size_t numAxes = mAxes.size();
5385 for (size_t i = 0; i < numAxes; i++) {
5386 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005387 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005388 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005389 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005390 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005391 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005392 }
Jeff Brown85297452011-03-04 13:07:49 -08005393 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5394 label = getAxisLabel(axis.axisInfo.highAxis);
5395 if (label) {
5396 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5397 } else {
5398 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5399 axis.axisInfo.splitValue);
5400 }
5401 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5402 dump.append(" (invert)");
5403 }
5404
5405 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5406 axis.min, axis.max, axis.flat, axis.fuzz);
5407 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5408 "highScale=%0.5f, highOffset=%0.5f\n",
5409 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005410 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, rawFlat=%d, rawFuzz=%d\n",
5411 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
5412 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz);
Jeff Browncb1404e2011-01-15 18:14:15 -08005413 }
5414}
5415
5416void JoystickInputMapper::configure() {
5417 InputMapper::configure();
5418
Jeff Brown6f2fba42011-02-19 01:08:02 -08005419 // Collect all axes.
5420 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
5421 RawAbsoluteAxisInfo rawAxisInfo;
5422 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), abs, &rawAxisInfo);
5423 if (rawAxisInfo.valid) {
Jeff Brown85297452011-03-04 13:07:49 -08005424 // Map axis.
5425 AxisInfo axisInfo;
5426 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005427 if (!explicitlyMapped) {
5428 // Axis is not explicitly mapped, will choose a generic axis later.
Jeff Brown85297452011-03-04 13:07:49 -08005429 axisInfo.mode = AxisInfo::MODE_NORMAL;
5430 axisInfo.axis = -1;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005431 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005432
Jeff Brown85297452011-03-04 13:07:49 -08005433 // Apply flat override.
5434 int32_t rawFlat = axisInfo.flatOverride < 0
5435 ? rawAxisInfo.flat : axisInfo.flatOverride;
5436
5437 // Calculate scaling factors and limits.
Jeff Brown6f2fba42011-02-19 01:08:02 -08005438 Axis axis;
Jeff Brown85297452011-03-04 13:07:49 -08005439 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5440 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5441 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5442 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5443 scale, 0.0f, highScale, 0.0f,
5444 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5445 } else if (isCenteredAxis(axisInfo.axis)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005446 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5447 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
Jeff Brown85297452011-03-04 13:07:49 -08005448 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5449 scale, offset, scale, offset,
5450 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005451 } else {
5452 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
Jeff Brown85297452011-03-04 13:07:49 -08005453 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5454 scale, 0.0f, scale, 0.0f,
5455 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005456 }
5457
5458 // To eliminate noise while the joystick is at rest, filter out small variations
5459 // in axis values up front.
5460 axis.filter = axis.flat * 0.25f;
5461
5462 mAxes.add(abs, axis);
5463 }
5464 }
5465
5466 // If there are too many axes, start dropping them.
5467 // Prefer to keep explicitly mapped axes.
5468 if (mAxes.size() > PointerCoords::MAX_AXES) {
5469 LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
5470 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5471 pruneAxes(true);
5472 pruneAxes(false);
5473 }
5474
5475 // Assign generic axis ids to remaining axes.
5476 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5477 size_t numAxes = mAxes.size();
5478 for (size_t i = 0; i < numAxes; i++) {
5479 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005480 if (axis.axisInfo.axis < 0) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005481 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5482 && haveAxis(nextGenericAxisId)) {
5483 nextGenericAxisId += 1;
5484 }
5485
5486 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
Jeff Brown85297452011-03-04 13:07:49 -08005487 axis.axisInfo.axis = nextGenericAxisId;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005488 nextGenericAxisId += 1;
5489 } else {
5490 LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
5491 "have already been assigned to other axes.",
5492 getDeviceName().string(), mAxes.keyAt(i));
5493 mAxes.removeItemsAt(i--);
5494 numAxes -= 1;
5495 }
5496 }
5497 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005498}
5499
Jeff Brown85297452011-03-04 13:07:49 -08005500bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005501 size_t numAxes = mAxes.size();
5502 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005503 const Axis& axis = mAxes.valueAt(i);
5504 if (axis.axisInfo.axis == axisId
5505 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5506 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005507 return true;
5508 }
5509 }
5510 return false;
5511}
Jeff Browncb1404e2011-01-15 18:14:15 -08005512
Jeff Brown6f2fba42011-02-19 01:08:02 -08005513void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5514 size_t i = mAxes.size();
5515 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5516 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5517 continue;
5518 }
5519 LOGI("Discarding joystick '%s' axis %d because there are too many axes.",
5520 getDeviceName().string(), mAxes.keyAt(i));
5521 mAxes.removeItemsAt(i);
5522 }
5523}
5524
5525bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5526 switch (axis) {
5527 case AMOTION_EVENT_AXIS_X:
5528 case AMOTION_EVENT_AXIS_Y:
5529 case AMOTION_EVENT_AXIS_Z:
5530 case AMOTION_EVENT_AXIS_RX:
5531 case AMOTION_EVENT_AXIS_RY:
5532 case AMOTION_EVENT_AXIS_RZ:
5533 case AMOTION_EVENT_AXIS_HAT_X:
5534 case AMOTION_EVENT_AXIS_HAT_Y:
5535 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005536 case AMOTION_EVENT_AXIS_RUDDER:
5537 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005538 return true;
5539 default:
5540 return false;
5541 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005542}
5543
5544void JoystickInputMapper::reset() {
5545 // Recenter all axes.
5546 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Browncb1404e2011-01-15 18:14:15 -08005547
Jeff Brown6f2fba42011-02-19 01:08:02 -08005548 size_t numAxes = mAxes.size();
5549 for (size_t i = 0; i < numAxes; i++) {
5550 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005551 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005552 }
5553
5554 sync(when, true /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005555
5556 InputMapper::reset();
5557}
5558
5559void JoystickInputMapper::process(const RawEvent* rawEvent) {
5560 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005561 case EV_ABS: {
5562 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
5563 if (index >= 0) {
5564 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005565 float newValue, highNewValue;
5566 switch (axis.axisInfo.mode) {
5567 case AxisInfo::MODE_INVERT:
5568 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5569 * axis.scale + axis.offset;
5570 highNewValue = 0.0f;
5571 break;
5572 case AxisInfo::MODE_SPLIT:
5573 if (rawEvent->value < axis.axisInfo.splitValue) {
5574 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5575 * axis.scale + axis.offset;
5576 highNewValue = 0.0f;
5577 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5578 newValue = 0.0f;
5579 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5580 * axis.highScale + axis.highOffset;
5581 } else {
5582 newValue = 0.0f;
5583 highNewValue = 0.0f;
5584 }
5585 break;
5586 default:
5587 newValue = rawEvent->value * axis.scale + axis.offset;
5588 highNewValue = 0.0f;
5589 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005590 }
Jeff Brown85297452011-03-04 13:07:49 -08005591 axis.newValue = newValue;
5592 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005593 }
5594 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005595 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005596
5597 case EV_SYN:
5598 switch (rawEvent->scanCode) {
5599 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005600 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005601 break;
5602 }
5603 break;
5604 }
5605}
5606
Jeff Brown6f2fba42011-02-19 01:08:02 -08005607void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005608 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005609 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005610 }
5611
5612 int32_t metaState = mContext->getGlobalMetaState();
5613
Jeff Brown6f2fba42011-02-19 01:08:02 -08005614 PointerCoords pointerCoords;
5615 pointerCoords.clear();
5616
5617 size_t numAxes = mAxes.size();
5618 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005619 const Axis& axis = mAxes.valueAt(i);
5620 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
5621 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5622 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
5623 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005624 }
5625
Jeff Brown56194eb2011-03-02 19:23:13 -08005626 // Moving a joystick axis should not wake the devide because joysticks can
5627 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
5628 // button will likely wake the device.
5629 // TODO: Use the input device configuration to control this behavior more finely.
5630 uint32_t policyFlags = 0;
5631
Jeff Brown6f2fba42011-02-19 01:08:02 -08005632 int32_t pointerId = 0;
Jeff Brown56194eb2011-03-02 19:23:13 -08005633 getDispatcher()->notifyMotion(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brown6f2fba42011-02-19 01:08:02 -08005634 AMOTION_EVENT_ACTION_MOVE, 0, metaState, AMOTION_EVENT_EDGE_FLAG_NONE,
5635 1, &pointerId, &pointerCoords, 0, 0, 0);
Jeff Browncb1404e2011-01-15 18:14:15 -08005636}
5637
Jeff Brown85297452011-03-04 13:07:49 -08005638bool JoystickInputMapper::filterAxes(bool force) {
5639 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005640 size_t numAxes = mAxes.size();
5641 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005642 Axis& axis = mAxes.editValueAt(i);
5643 if (force || hasValueChangedSignificantly(axis.filter,
5644 axis.newValue, axis.currentValue, axis.min, axis.max)) {
5645 axis.currentValue = axis.newValue;
5646 atLeastOneSignificantChange = true;
5647 }
5648 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5649 if (force || hasValueChangedSignificantly(axis.filter,
5650 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
5651 axis.highCurrentValue = axis.highNewValue;
5652 atLeastOneSignificantChange = true;
5653 }
5654 }
5655 }
5656 return atLeastOneSignificantChange;
5657}
5658
5659bool JoystickInputMapper::hasValueChangedSignificantly(
5660 float filter, float newValue, float currentValue, float min, float max) {
5661 if (newValue != currentValue) {
5662 // Filter out small changes in value unless the value is converging on the axis
5663 // bounds or center point. This is intended to reduce the amount of information
5664 // sent to applications by particularly noisy joysticks (such as PS3).
5665 if (fabs(newValue - currentValue) > filter
5666 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
5667 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
5668 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
5669 return true;
5670 }
5671 }
5672 return false;
5673}
5674
5675bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
5676 float filter, float newValue, float currentValue, float thresholdValue) {
5677 float newDistance = fabs(newValue - thresholdValue);
5678 if (newDistance < filter) {
5679 float oldDistance = fabs(currentValue - thresholdValue);
5680 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005681 return true;
5682 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005683 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005684 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005685}
5686
Jeff Brown46b9ac02010-04-22 18:58:52 -07005687} // namespace android