blob: db312ad3652e93eb4a14bb27ac4a670145bf6a06 [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070017#define LOG_TAG "InputReader"
18
19//#define LOG_NDEBUG 0
20
21// Log debug messages for each raw event received from the EventHub.
22#define DEBUG_RAW_EVENTS 0
23
24// Log debug messages about touch screen filtering hacks.
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_HACKS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070026
27// Log debug messages about virtual key processing.
Jeff Brown349703e2010-06-22 01:27:15 -070028#define DEBUG_VIRTUAL_KEYS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070029
30// Log debug messages about pointers.
Jeff Brown9f2106f2011-05-24 14:40:35 -070031#define DEBUG_POINTERS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070032
Jeff Brown5c225b12010-06-16 01:53:36 -070033// Log debug messages about pointer assignment calculations.
34#define DEBUG_POINTER_ASSIGNMENT 0
35
Jeff Brownace13b12011-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-04-22 18:58:52 -070044
45#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070046#include <stdlib.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070047#include <unistd.h>
Jeff Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brownaba321a2011-06-28 20:34:40 -070056#define INDENT5 " "
Jeff Brown8d608662010-08-30 03:02:23 -070057
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070058namespace android {
59
Jeff Brownace13b12011-03-09 17:39:48 -080060// --- Constants ---
61
Jeff Brown80fd47c2011-05-24 01:07:44 -070062// Maximum number of slots supported when using the slot-based Multitouch Protocol B.
63static const size_t MAX_SLOTS = 32;
64
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070065// --- Static Functions ---
66
67template<typename T>
68inline static T abs(const T& value) {
69 return value < 0 ? - value : value;
70}
71
72template<typename T>
73inline static T min(const T& a, const T& b) {
74 return a < b ? a : b;
75}
76
Jeff Brown5c225b12010-06-16 01:53:36 -070077template<typename T>
78inline static void swap(T& a, T& b) {
79 T temp = a;
80 a = b;
81 b = temp;
82}
83
Jeff Brown8d608662010-08-30 03:02:23 -070084inline static float avg(float x, float y) {
85 return (x + y) / 2;
86}
87
Jeff Brown2352b972011-04-12 22:39:53 -070088inline static float distance(float x1, float y1, float x2, float y2) {
89 return hypotf(x1 - x2, y1 - y2);
Jeff Brownace13b12011-03-09 17:39:48 -080090}
91
Jeff Brown517bb4c2011-01-14 19:09:23 -080092inline static int32_t signExtendNybble(int32_t value) {
93 return value >= 8 ? value - 16 : value;
94}
95
Jeff Brownef3d7e82010-09-30 14:33:04 -070096static inline const char* toString(bool value) {
97 return value ? "true" : "false";
98}
99
Jeff Brown9626b142011-03-03 02:09:54 -0800100static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
101 const int32_t map[][4], size_t mapSize) {
102 if (orientation != DISPLAY_ORIENTATION_0) {
103 for (size_t i = 0; i < mapSize; i++) {
104 if (value == map[i][0]) {
105 return map[i][orientation];
106 }
107 }
108 }
109 return value;
110}
111
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700112static const int32_t keyCodeRotationMap[][4] = {
113 // key codes enumerated counter-clockwise with the original (unrotated) key first
114 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd0358292010-06-30 16:10:35 -0700115 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
116 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
117 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
118 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700119};
Jeff Brown9626b142011-03-03 02:09:54 -0800120static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700121 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
122
Jeff Brown60691392011-07-15 19:08:26 -0700123static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brown9626b142011-03-03 02:09:54 -0800124 return rotateValueUsingRotationMap(keyCode, orientation,
125 keyCodeRotationMap, keyCodeRotationMapSize);
126}
127
Jeff Brown612891e2011-07-15 20:44:17 -0700128static void rotateDelta(int32_t orientation, float* deltaX, float* deltaY) {
129 float temp;
130 switch (orientation) {
131 case DISPLAY_ORIENTATION_90:
132 temp = *deltaX;
133 *deltaX = *deltaY;
134 *deltaY = -temp;
135 break;
136
137 case DISPLAY_ORIENTATION_180:
138 *deltaX = -*deltaX;
139 *deltaY = -*deltaY;
140 break;
141
142 case DISPLAY_ORIENTATION_270:
143 temp = *deltaX;
144 *deltaX = -*deltaY;
145 *deltaY = temp;
146 break;
147 }
148}
149
Jeff Brown6d0fec22010-07-23 21:28:06 -0700150static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
151 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
152}
153
Jeff Brownefd32662011-03-08 15:13:06 -0800154// Returns true if the pointer should be reported as being down given the specified
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700155// button states. This determines whether the event is reported as a touch event.
156static bool isPointerDown(int32_t buttonState) {
157 return buttonState &
158 (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY
Jeff Brown53ca3f12011-06-27 18:36:00 -0700159 | AMOTION_EVENT_BUTTON_TERTIARY);
Jeff Brownefd32662011-03-08 15:13:06 -0800160}
161
Jeff Brown2352b972011-04-12 22:39:53 -0700162static float calculateCommonVector(float a, float b) {
163 if (a > 0 && b > 0) {
164 return a < b ? a : b;
165 } else if (a < 0 && b < 0) {
166 return a > b ? a : b;
167 } else {
168 return 0;
169 }
170}
171
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700172static void synthesizeButtonKey(InputReaderContext* context, int32_t action,
173 nsecs_t when, int32_t deviceId, uint32_t source,
174 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState,
175 int32_t buttonState, int32_t keyCode) {
176 if (
177 (action == AKEY_EVENT_ACTION_DOWN
178 && !(lastButtonState & buttonState)
179 && (currentButtonState & buttonState))
180 || (action == AKEY_EVENT_ACTION_UP
181 && (lastButtonState & buttonState)
182 && !(currentButtonState & buttonState))) {
183 context->getDispatcher()->notifyKey(when, deviceId, source, policyFlags,
184 action, 0, keyCode, 0, context->getGlobalMetaState(), when);
185 }
186}
187
188static void synthesizeButtonKeys(InputReaderContext* context, int32_t action,
189 nsecs_t when, int32_t deviceId, uint32_t source,
190 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState) {
191 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
192 lastButtonState, currentButtonState,
193 AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK);
194 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
195 lastButtonState, currentButtonState,
196 AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD);
197}
198
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700199
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700200// --- InputReader ---
201
202InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700203 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700204 const sp<InputDispatcherInterface>& dispatcher) :
Jeff Brown6d0fec22010-07-23 21:28:06 -0700205 mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher),
Jeff Brown1a84fd12011-06-02 01:26:32 -0700206 mGlobalMetaState(0), mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX),
Jeff Brown474dcb52011-06-14 20:22:50 -0700207 mConfigurationChangesToRefresh(0) {
208 refreshConfiguration(0);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700209 updateGlobalMetaState();
210 updateInputConfiguration();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700211}
212
213InputReader::~InputReader() {
214 for (size_t i = 0; i < mDevices.size(); i++) {
215 delete mDevices.valueAt(i);
216 }
217}
218
219void InputReader::loopOnce() {
Jeff Brown474dcb52011-06-14 20:22:50 -0700220 uint32_t changes;
221 { // acquire lock
222 AutoMutex _l(mStateLock);
223
224 changes = mConfigurationChangesToRefresh;
225 mConfigurationChangesToRefresh = 0;
226 } // release lock
227
228 if (changes) {
229 refreshConfiguration(changes);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700230 }
231
Jeff Brownaa3855d2011-03-17 01:34:19 -0700232 int32_t timeoutMillis = -1;
233 if (mNextTimeout != LLONG_MAX) {
234 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
235 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
236 }
237
Jeff Brownb7198742011-03-18 18:14:26 -0700238 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
239 if (count) {
240 processEvents(mEventBuffer, count);
241 }
242 if (!count || timeoutMillis == 0) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700243 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
244#if DEBUG_RAW_EVENTS
245 LOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
246#endif
247 mNextTimeout = LLONG_MAX;
248 timeoutExpired(now);
249 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700250}
251
Jeff Brownb7198742011-03-18 18:14:26 -0700252void InputReader::processEvents(const RawEvent* rawEvents, size_t count) {
253 for (const RawEvent* rawEvent = rawEvents; count;) {
254 int32_t type = rawEvent->type;
255 size_t batchSize = 1;
256 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
257 int32_t deviceId = rawEvent->deviceId;
258 while (batchSize < count) {
259 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
260 || rawEvent[batchSize].deviceId != deviceId) {
261 break;
262 }
263 batchSize += 1;
264 }
265#if DEBUG_RAW_EVENTS
266 LOGD("BatchSize: %d Count: %d", batchSize, count);
267#endif
268 processEventsForDevice(deviceId, rawEvent, batchSize);
269 } else {
270 switch (rawEvent->type) {
271 case EventHubInterface::DEVICE_ADDED:
272 addDevice(rawEvent->deviceId);
273 break;
274 case EventHubInterface::DEVICE_REMOVED:
275 removeDevice(rawEvent->deviceId);
276 break;
277 case EventHubInterface::FINISHED_DEVICE_SCAN:
278 handleConfigurationChanged(rawEvent->when);
279 break;
280 default:
Jeff Brownb6110c22011-04-01 16:15:13 -0700281 LOG_ASSERT(false); // can't happen
Jeff Brownb7198742011-03-18 18:14:26 -0700282 break;
283 }
284 }
285 count -= batchSize;
286 rawEvent += batchSize;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700287 }
288}
289
Jeff Brown7342bb92010-10-01 18:55:43 -0700290void InputReader::addDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700291 String8 name = mEventHub->getDeviceName(deviceId);
292 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
293
294 InputDevice* device = createDevice(deviceId, name, classes);
Jeff Brown474dcb52011-06-14 20:22:50 -0700295 device->configure(&mConfig, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700296
Jeff Brown8d608662010-08-30 03:02:23 -0700297 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800298 LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700299 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800300 LOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700301 device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700302 }
303
Jeff Brown6d0fec22010-07-23 21:28:06 -0700304 bool added = false;
305 { // acquire device registry writer lock
306 RWLock::AutoWLock _wl(mDeviceRegistryLock);
307
308 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
309 if (deviceIndex < 0) {
310 mDevices.add(deviceId, device);
311 added = true;
312 }
313 } // release device registry writer lock
314
315 if (! added) {
316 LOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
317 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700318 return;
319 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700320}
321
Jeff Brown7342bb92010-10-01 18:55:43 -0700322void InputReader::removeDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700323 bool removed = false;
324 InputDevice* device = NULL;
325 { // acquire device registry writer lock
326 RWLock::AutoWLock _wl(mDeviceRegistryLock);
327
328 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
329 if (deviceIndex >= 0) {
330 device = mDevices.valueAt(deviceIndex);
331 mDevices.removeItemsAt(deviceIndex, 1);
332 removed = true;
333 }
334 } // release device registry writer lock
335
336 if (! removed) {
337 LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700338 return;
339 }
340
Jeff Brown6d0fec22010-07-23 21:28:06 -0700341 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800342 LOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700343 device->getId(), device->getName().string());
344 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800345 LOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700346 device->getId(), device->getName().string(), device->getSources());
347 }
348
Jeff Brown8d608662010-08-30 03:02:23 -0700349 device->reset();
350
Jeff Brown6d0fec22010-07-23 21:28:06 -0700351 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700352}
353
Jeff Brown6d0fec22010-07-23 21:28:06 -0700354InputDevice* InputReader::createDevice(int32_t deviceId, const String8& name, uint32_t classes) {
355 InputDevice* device = new InputDevice(this, deviceId, name);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700356
Jeff Brown56194eb2011-03-02 19:23:13 -0800357 // External devices.
358 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
359 device->setExternal(true);
360 }
361
Jeff Brown6d0fec22010-07-23 21:28:06 -0700362 // Switch-like devices.
363 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
364 device->addMapper(new SwitchInputMapper(device));
365 }
366
367 // Keyboard-like devices.
Jeff Brownefd32662011-03-08 15:13:06 -0800368 uint32_t keyboardSource = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700369 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
370 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800371 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700372 }
373 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
374 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
375 }
376 if (classes & INPUT_DEVICE_CLASS_DPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800377 keyboardSource |= AINPUT_SOURCE_DPAD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700378 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800379 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800380 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
Jeff Browncb1404e2011-01-15 18:14:15 -0800381 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700382
Jeff Brownefd32662011-03-08 15:13:06 -0800383 if (keyboardSource != 0) {
384 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700385 }
386
Jeff Brown83c09682010-12-23 17:50:18 -0800387 // Cursor-like devices.
388 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
389 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700390 }
391
Jeff Brown58a2da82011-01-25 16:02:22 -0800392 // Touchscreens and touchpad devices.
393 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800394 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800395 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800396 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700397 }
398
Jeff Browncb1404e2011-01-15 18:14:15 -0800399 // Joystick-like devices.
400 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
401 device->addMapper(new JoystickInputMapper(device));
402 }
403
Jeff Brown6d0fec22010-07-23 21:28:06 -0700404 return device;
405}
406
Jeff Brownb7198742011-03-18 18:14:26 -0700407void InputReader::processEventsForDevice(int32_t deviceId,
408 const RawEvent* rawEvents, size_t count) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700409 { // acquire device registry reader lock
410 RWLock::AutoRLock _rl(mDeviceRegistryLock);
411
412 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
413 if (deviceIndex < 0) {
414 LOGW("Discarding event for unknown deviceId %d.", deviceId);
415 return;
416 }
417
418 InputDevice* device = mDevices.valueAt(deviceIndex);
419 if (device->isIgnored()) {
420 //LOGD("Discarding event for ignored deviceId %d.", deviceId);
421 return;
422 }
423
Jeff Brownb7198742011-03-18 18:14:26 -0700424 device->process(rawEvents, count);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700425 } // release device registry reader lock
426}
427
Jeff Brownaa3855d2011-03-17 01:34:19 -0700428void InputReader::timeoutExpired(nsecs_t when) {
429 { // acquire device registry reader lock
430 RWLock::AutoRLock _rl(mDeviceRegistryLock);
431
432 for (size_t i = 0; i < mDevices.size(); i++) {
433 InputDevice* device = mDevices.valueAt(i);
434 if (!device->isIgnored()) {
435 device->timeoutExpired(when);
436 }
437 }
438 } // release device registry reader lock
439}
440
Jeff Brownc3db8582010-10-20 15:33:38 -0700441void InputReader::handleConfigurationChanged(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700442 // Reset global meta state because it depends on the list of all configured devices.
443 updateGlobalMetaState();
444
445 // Update input configuration.
446 updateInputConfiguration();
447
448 // Enqueue configuration changed.
449 mDispatcher->notifyConfigurationChanged(when);
450}
451
Jeff Brown474dcb52011-06-14 20:22:50 -0700452void InputReader::refreshConfiguration(uint32_t changes) {
Jeff Brown1a84fd12011-06-02 01:26:32 -0700453 mPolicy->getReaderConfiguration(&mConfig);
454 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
455
Jeff Brown474dcb52011-06-14 20:22:50 -0700456 if (changes) {
457 LOGI("Reconfiguring input devices. changes=0x%08x", changes);
458
459 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
460 mEventHub->requestReopenDevices();
461 } else {
462 { // acquire device registry reader lock
463 RWLock::AutoRLock _rl(mDeviceRegistryLock);
464
465 for (size_t i = 0; i < mDevices.size(); i++) {
466 InputDevice* device = mDevices.valueAt(i);
467 device->configure(&mConfig, changes);
468 }
469 } // release device registry reader lock
470 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700471 }
472}
473
474void InputReader::updateGlobalMetaState() {
475 { // acquire state lock
476 AutoMutex _l(mStateLock);
477
478 mGlobalMetaState = 0;
479
480 { // acquire device registry reader lock
481 RWLock::AutoRLock _rl(mDeviceRegistryLock);
482
483 for (size_t i = 0; i < mDevices.size(); i++) {
484 InputDevice* device = mDevices.valueAt(i);
485 mGlobalMetaState |= device->getMetaState();
486 }
487 } // release device registry reader lock
488 } // release state lock
489}
490
491int32_t InputReader::getGlobalMetaState() {
492 { // acquire state lock
493 AutoMutex _l(mStateLock);
494
495 return mGlobalMetaState;
496 } // release state lock
497}
498
499void InputReader::updateInputConfiguration() {
500 { // acquire state lock
501 AutoMutex _l(mStateLock);
502
503 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
504 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
505 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
506 { // acquire device registry reader lock
507 RWLock::AutoRLock _rl(mDeviceRegistryLock);
508
509 InputDeviceInfo deviceInfo;
510 for (size_t i = 0; i < mDevices.size(); i++) {
511 InputDevice* device = mDevices.valueAt(i);
512 device->getDeviceInfo(& deviceInfo);
513 uint32_t sources = deviceInfo.getSources();
514
515 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
516 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
517 }
518 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
519 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
520 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
521 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
522 }
523 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
524 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700525 }
526 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700527 } // release device registry reader lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700528
Jeff Brown6d0fec22010-07-23 21:28:06 -0700529 mInputConfiguration.touchScreen = touchScreenConfig;
530 mInputConfiguration.keyboard = keyboardConfig;
531 mInputConfiguration.navigation = navigationConfig;
532 } // release state lock
533}
534
Jeff Brownfe508922011-01-18 15:10:10 -0800535void InputReader::disableVirtualKeysUntil(nsecs_t time) {
536 mDisableVirtualKeysTimeout = time;
537}
538
539bool InputReader::shouldDropVirtualKey(nsecs_t now,
540 InputDevice* device, int32_t keyCode, int32_t scanCode) {
541 if (now < mDisableVirtualKeysTimeout) {
542 LOGI("Dropping virtual key from device %s because virtual keys are "
543 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
544 device->getName().string(),
545 (mDisableVirtualKeysTimeout - now) * 0.000001,
546 keyCode, scanCode);
547 return true;
548 } else {
549 return false;
550 }
551}
552
Jeff Brown05dc66a2011-03-02 14:41:58 -0800553void InputReader::fadePointer() {
554 { // acquire device registry reader lock
555 RWLock::AutoRLock _rl(mDeviceRegistryLock);
556
557 for (size_t i = 0; i < mDevices.size(); i++) {
558 InputDevice* device = mDevices.valueAt(i);
559 device->fadePointer();
560 }
561 } // release device registry reader lock
562}
563
Jeff Brownaa3855d2011-03-17 01:34:19 -0700564void InputReader::requestTimeoutAtTime(nsecs_t when) {
565 if (when < mNextTimeout) {
566 mNextTimeout = when;
567 }
568}
569
Jeff Brown6d0fec22010-07-23 21:28:06 -0700570void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
571 { // acquire state lock
572 AutoMutex _l(mStateLock);
573
574 *outConfiguration = mInputConfiguration;
575 } // release state lock
576}
577
578status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
579 { // acquire device registry reader lock
580 RWLock::AutoRLock _rl(mDeviceRegistryLock);
581
582 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
583 if (deviceIndex < 0) {
584 return NAME_NOT_FOUND;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700585 }
586
Jeff Brown6d0fec22010-07-23 21:28:06 -0700587 InputDevice* device = mDevices.valueAt(deviceIndex);
588 if (device->isIgnored()) {
589 return NAME_NOT_FOUND;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700590 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700591
592 device->getDeviceInfo(outDeviceInfo);
593 return OK;
594 } // release device registy reader lock
595}
596
597void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
598 outDeviceIds.clear();
599
600 { // acquire device registry reader lock
601 RWLock::AutoRLock _rl(mDeviceRegistryLock);
602
603 size_t numDevices = mDevices.size();
604 for (size_t i = 0; i < numDevices; i++) {
605 InputDevice* device = mDevices.valueAt(i);
606 if (! device->isIgnored()) {
607 outDeviceIds.add(device->getId());
608 }
609 }
610 } // release device registy reader lock
611}
612
613int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
614 int32_t keyCode) {
615 return getState(deviceId, sourceMask, keyCode, & InputDevice::getKeyCodeState);
616}
617
618int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
619 int32_t scanCode) {
620 return getState(deviceId, sourceMask, scanCode, & InputDevice::getScanCodeState);
621}
622
623int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
624 return getState(deviceId, sourceMask, switchCode, & InputDevice::getSwitchState);
625}
626
627int32_t InputReader::getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
628 GetStateFunc getStateFunc) {
629 { // acquire device registry reader lock
630 RWLock::AutoRLock _rl(mDeviceRegistryLock);
631
632 int32_t result = AKEY_STATE_UNKNOWN;
633 if (deviceId >= 0) {
634 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
635 if (deviceIndex >= 0) {
636 InputDevice* device = mDevices.valueAt(deviceIndex);
637 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
638 result = (device->*getStateFunc)(sourceMask, code);
639 }
640 }
641 } else {
642 size_t numDevices = mDevices.size();
643 for (size_t i = 0; i < numDevices; i++) {
644 InputDevice* device = mDevices.valueAt(i);
645 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
646 result = (device->*getStateFunc)(sourceMask, code);
647 if (result >= AKEY_STATE_DOWN) {
648 return result;
649 }
650 }
651 }
652 }
653 return result;
654 } // release device registy reader lock
655}
656
657bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
658 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
659 memset(outFlags, 0, numCodes);
660 return markSupportedKeyCodes(deviceId, sourceMask, numCodes, keyCodes, outFlags);
661}
662
663bool InputReader::markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
664 const int32_t* keyCodes, uint8_t* outFlags) {
665 { // acquire device registry reader lock
666 RWLock::AutoRLock _rl(mDeviceRegistryLock);
667 bool result = false;
668 if (deviceId >= 0) {
669 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
670 if (deviceIndex >= 0) {
671 InputDevice* device = mDevices.valueAt(deviceIndex);
672 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
673 result = device->markSupportedKeyCodes(sourceMask,
674 numCodes, keyCodes, outFlags);
675 }
676 }
677 } else {
678 size_t numDevices = mDevices.size();
679 for (size_t i = 0; i < numDevices; i++) {
680 InputDevice* device = mDevices.valueAt(i);
681 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
682 result |= device->markSupportedKeyCodes(sourceMask,
683 numCodes, keyCodes, outFlags);
684 }
685 }
686 }
687 return result;
688 } // release device registy reader lock
689}
690
Jeff Brown474dcb52011-06-14 20:22:50 -0700691void InputReader::requestRefreshConfiguration(uint32_t changes) {
692 if (changes) {
693 bool needWake;
694 { // acquire lock
695 AutoMutex _l(mStateLock);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700696
Jeff Brown474dcb52011-06-14 20:22:50 -0700697 needWake = !mConfigurationChangesToRefresh;
698 mConfigurationChangesToRefresh |= changes;
699 } // release lock
700
701 if (needWake) {
702 mEventHub->wake();
703 }
704 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700705}
706
Jeff Brownb88102f2010-09-08 11:49:43 -0700707void InputReader::dump(String8& dump) {
Jeff Brownf2f487182010-10-01 17:46:21 -0700708 mEventHub->dump(dump);
709 dump.append("\n");
710
711 dump.append("Input Reader State:\n");
712
Jeff Brownef3d7e82010-09-30 14:33:04 -0700713 { // acquire device registry reader lock
714 RWLock::AutoRLock _rl(mDeviceRegistryLock);
Jeff Brownb88102f2010-09-08 11:49:43 -0700715
Jeff Brownef3d7e82010-09-30 14:33:04 -0700716 for (size_t i = 0; i < mDevices.size(); i++) {
717 mDevices.valueAt(i)->dump(dump);
Jeff Brownb88102f2010-09-08 11:49:43 -0700718 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700719 } // release device registy reader lock
Jeff Brown214eaf42011-05-26 19:17:02 -0700720
721 dump.append(INDENT "Configuration:\n");
722 dump.append(INDENT2 "ExcludedDeviceNames: [");
723 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
724 if (i != 0) {
725 dump.append(", ");
726 }
727 dump.append(mConfig.excludedDeviceNames.itemAt(i).string());
728 }
729 dump.append("]\n");
Jeff Brown214eaf42011-05-26 19:17:02 -0700730 dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
731 mConfig.virtualKeyQuietTime * 0.000001f);
732
Jeff Brown19c97d462011-06-01 12:33:19 -0700733 dump.appendFormat(INDENT2 "PointerVelocityControlParameters: "
734 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
735 mConfig.pointerVelocityControlParameters.scale,
736 mConfig.pointerVelocityControlParameters.lowThreshold,
737 mConfig.pointerVelocityControlParameters.highThreshold,
738 mConfig.pointerVelocityControlParameters.acceleration);
739
740 dump.appendFormat(INDENT2 "WheelVelocityControlParameters: "
741 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
742 mConfig.wheelVelocityControlParameters.scale,
743 mConfig.wheelVelocityControlParameters.lowThreshold,
744 mConfig.wheelVelocityControlParameters.highThreshold,
745 mConfig.wheelVelocityControlParameters.acceleration);
746
Jeff Brown214eaf42011-05-26 19:17:02 -0700747 dump.appendFormat(INDENT2 "PointerGesture:\n");
Jeff Brown474dcb52011-06-14 20:22:50 -0700748 dump.appendFormat(INDENT3 "Enabled: %s\n",
749 toString(mConfig.pointerGesturesEnabled));
Jeff Brown214eaf42011-05-26 19:17:02 -0700750 dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n",
751 mConfig.pointerGestureQuietInterval * 0.000001f);
752 dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
753 mConfig.pointerGestureDragMinSwitchSpeed);
754 dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n",
755 mConfig.pointerGestureTapInterval * 0.000001f);
756 dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n",
757 mConfig.pointerGestureTapDragInterval * 0.000001f);
758 dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n",
759 mConfig.pointerGestureTapSlop);
760 dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
761 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700762 dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
763 mConfig.pointerGestureMultitouchMinDistance);
Jeff Brown214eaf42011-05-26 19:17:02 -0700764 dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
765 mConfig.pointerGestureSwipeTransitionAngleCosine);
766 dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
767 mConfig.pointerGestureSwipeMaxWidthRatio);
768 dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n",
769 mConfig.pointerGestureMovementSpeedRatio);
770 dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n",
771 mConfig.pointerGestureZoomSpeedRatio);
Jeff Brownb88102f2010-09-08 11:49:43 -0700772}
773
Jeff Brown6d0fec22010-07-23 21:28:06 -0700774
775// --- InputReaderThread ---
776
777InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
778 Thread(/*canCallJava*/ true), mReader(reader) {
779}
780
781InputReaderThread::~InputReaderThread() {
782}
783
784bool InputReaderThread::threadLoop() {
785 mReader->loopOnce();
786 return true;
787}
788
789
790// --- InputDevice ---
791
792InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) :
Jeff Brown80fd47c2011-05-24 01:07:44 -0700793 mContext(context), mId(id), mName(name), mSources(0),
794 mIsExternal(false), mDropUntilNextSync(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700795}
796
797InputDevice::~InputDevice() {
798 size_t numMappers = mMappers.size();
799 for (size_t i = 0; i < numMappers; i++) {
800 delete mMappers[i];
801 }
802 mMappers.clear();
803}
804
Jeff Brownef3d7e82010-09-30 14:33:04 -0700805void InputDevice::dump(String8& dump) {
806 InputDeviceInfo deviceInfo;
807 getDeviceInfo(& deviceInfo);
808
Jeff Brown90655042010-12-02 13:50:46 -0800809 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700810 deviceInfo.getName().string());
Jeff Brown56194eb2011-03-02 19:23:13 -0800811 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700812 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
813 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800814
Jeff Brownefd32662011-03-08 15:13:06 -0800815 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800816 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700817 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800818 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800819 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
820 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800821 char name[32];
822 if (label) {
823 strncpy(name, label, sizeof(name));
824 name[sizeof(name) - 1] = '\0';
825 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800826 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800827 }
Jeff Brownefd32662011-03-08 15:13:06 -0800828 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
829 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
830 name, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Browncc0c1592011-02-19 05:07:28 -0800831 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700832 }
833
834 size_t numMappers = mMappers.size();
835 for (size_t i = 0; i < numMappers; i++) {
836 InputMapper* mapper = mMappers[i];
837 mapper->dump(dump);
838 }
839}
840
Jeff Brown6d0fec22010-07-23 21:28:06 -0700841void InputDevice::addMapper(InputMapper* mapper) {
842 mMappers.add(mapper);
843}
844
Jeff Brown474dcb52011-06-14 20:22:50 -0700845void InputDevice::configure(const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700846 mSources = 0;
847
Jeff Brown474dcb52011-06-14 20:22:50 -0700848 if (!isIgnored()) {
849 if (!changes) { // first time only
850 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
851 }
852
853 size_t numMappers = mMappers.size();
854 for (size_t i = 0; i < numMappers; i++) {
855 InputMapper* mapper = mMappers[i];
856 mapper->configure(config, changes);
857 mSources |= mapper->getSources();
858 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700859 }
860}
861
Jeff Brown6d0fec22010-07-23 21:28:06 -0700862void InputDevice::reset() {
863 size_t numMappers = mMappers.size();
864 for (size_t i = 0; i < numMappers; i++) {
865 InputMapper* mapper = mMappers[i];
866 mapper->reset();
867 }
868}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700869
Jeff Brownb7198742011-03-18 18:14:26 -0700870void InputDevice::process(const RawEvent* rawEvents, size_t count) {
871 // Process all of the events in order for each mapper.
872 // We cannot simply ask each mapper to process them in bulk because mappers may
873 // have side-effects that must be interleaved. For example, joystick movement events and
874 // gamepad button presses are handled by different mappers but they should be dispatched
875 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700876 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700877 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
878#if DEBUG_RAW_EVENTS
879 LOGD("Input event: device=%d type=0x%04x scancode=0x%04x "
Jeff Brown2e45fb62011-06-29 21:19:05 -0700880 "keycode=0x%04x value=0x%08x flags=0x%08x",
Jeff Brownb7198742011-03-18 18:14:26 -0700881 rawEvent->deviceId, rawEvent->type, rawEvent->scanCode, rawEvent->keyCode,
882 rawEvent->value, rawEvent->flags);
883#endif
884
Jeff Brown80fd47c2011-05-24 01:07:44 -0700885 if (mDropUntilNextSync) {
886 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
887 mDropUntilNextSync = false;
888#if DEBUG_RAW_EVENTS
889 LOGD("Recovered from input event buffer overrun.");
890#endif
891 } else {
892#if DEBUG_RAW_EVENTS
893 LOGD("Dropped input event while waiting for next input sync.");
894#endif
895 }
896 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_DROPPED) {
897 LOGI("Detected input event buffer overrun for device %s.", mName.string());
898 mDropUntilNextSync = true;
899 reset();
900 } else {
901 for (size_t i = 0; i < numMappers; i++) {
902 InputMapper* mapper = mMappers[i];
903 mapper->process(rawEvent);
904 }
Jeff Brownb7198742011-03-18 18:14:26 -0700905 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700906 }
907}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700908
Jeff Brownaa3855d2011-03-17 01:34:19 -0700909void InputDevice::timeoutExpired(nsecs_t when) {
910 size_t numMappers = mMappers.size();
911 for (size_t i = 0; i < numMappers; i++) {
912 InputMapper* mapper = mMappers[i];
913 mapper->timeoutExpired(when);
914 }
915}
916
Jeff Brown6d0fec22010-07-23 21:28:06 -0700917void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
918 outDeviceInfo->initialize(mId, mName);
919
920 size_t numMappers = mMappers.size();
921 for (size_t i = 0; i < numMappers; i++) {
922 InputMapper* mapper = mMappers[i];
923 mapper->populateDeviceInfo(outDeviceInfo);
924 }
925}
926
927int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
928 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
929}
930
931int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
932 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
933}
934
935int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
936 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
937}
938
939int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
940 int32_t result = AKEY_STATE_UNKNOWN;
941 size_t numMappers = mMappers.size();
942 for (size_t i = 0; i < numMappers; i++) {
943 InputMapper* mapper = mMappers[i];
944 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
945 result = (mapper->*getStateFunc)(sourceMask, code);
946 if (result >= AKEY_STATE_DOWN) {
947 return result;
948 }
949 }
950 }
951 return result;
952}
953
954bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
955 const int32_t* keyCodes, uint8_t* outFlags) {
956 bool result = false;
957 size_t numMappers = mMappers.size();
958 for (size_t i = 0; i < numMappers; i++) {
959 InputMapper* mapper = mMappers[i];
960 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
961 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
962 }
963 }
964 return result;
965}
966
967int32_t InputDevice::getMetaState() {
968 int32_t result = 0;
969 size_t numMappers = mMappers.size();
970 for (size_t i = 0; i < numMappers; i++) {
971 InputMapper* mapper = mMappers[i];
972 result |= mapper->getMetaState();
973 }
974 return result;
975}
976
Jeff Brown05dc66a2011-03-02 14:41:58 -0800977void InputDevice::fadePointer() {
978 size_t numMappers = mMappers.size();
979 for (size_t i = 0; i < numMappers; i++) {
980 InputMapper* mapper = mMappers[i];
981 mapper->fadePointer();
982 }
983}
984
Jeff Brown6d0fec22010-07-23 21:28:06 -0700985
Jeff Brown49754db2011-07-01 17:37:58 -0700986// --- CursorButtonAccumulator ---
987
988CursorButtonAccumulator::CursorButtonAccumulator() {
989 clearButtons();
990}
991
992void CursorButtonAccumulator::clearButtons() {
993 mBtnLeft = 0;
994 mBtnRight = 0;
995 mBtnMiddle = 0;
996 mBtnBack = 0;
997 mBtnSide = 0;
998 mBtnForward = 0;
999 mBtnExtra = 0;
1000 mBtnTask = 0;
1001}
1002
1003void CursorButtonAccumulator::process(const RawEvent* rawEvent) {
1004 if (rawEvent->type == EV_KEY) {
1005 switch (rawEvent->scanCode) {
1006 case BTN_LEFT:
1007 mBtnLeft = rawEvent->value;
1008 break;
1009 case BTN_RIGHT:
1010 mBtnRight = rawEvent->value;
1011 break;
1012 case BTN_MIDDLE:
1013 mBtnMiddle = rawEvent->value;
1014 break;
1015 case BTN_BACK:
1016 mBtnBack = rawEvent->value;
1017 break;
1018 case BTN_SIDE:
1019 mBtnSide = rawEvent->value;
1020 break;
1021 case BTN_FORWARD:
1022 mBtnForward = rawEvent->value;
1023 break;
1024 case BTN_EXTRA:
1025 mBtnExtra = rawEvent->value;
1026 break;
1027 case BTN_TASK:
1028 mBtnTask = rawEvent->value;
1029 break;
1030 }
1031 }
1032}
1033
1034uint32_t CursorButtonAccumulator::getButtonState() const {
1035 uint32_t result = 0;
1036 if (mBtnLeft) {
1037 result |= AMOTION_EVENT_BUTTON_PRIMARY;
1038 }
1039 if (mBtnRight) {
1040 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1041 }
1042 if (mBtnMiddle) {
1043 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1044 }
1045 if (mBtnBack || mBtnSide) {
1046 result |= AMOTION_EVENT_BUTTON_BACK;
1047 }
1048 if (mBtnForward || mBtnExtra) {
1049 result |= AMOTION_EVENT_BUTTON_FORWARD;
1050 }
1051 return result;
1052}
1053
1054
1055// --- CursorMotionAccumulator ---
1056
1057CursorMotionAccumulator::CursorMotionAccumulator() :
1058 mHaveRelWheel(false), mHaveRelHWheel(false) {
1059 clearRelativeAxes();
1060}
1061
1062void CursorMotionAccumulator::configure(InputDevice* device) {
1063 mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL);
1064 mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL);
1065}
1066
1067void CursorMotionAccumulator::clearRelativeAxes() {
1068 mRelX = 0;
1069 mRelY = 0;
1070 mRelWheel = 0;
1071 mRelHWheel = 0;
1072}
1073
1074void CursorMotionAccumulator::process(const RawEvent* rawEvent) {
1075 if (rawEvent->type == EV_REL) {
1076 switch (rawEvent->scanCode) {
1077 case REL_X:
1078 mRelX = rawEvent->value;
1079 break;
1080 case REL_Y:
1081 mRelY = rawEvent->value;
1082 break;
1083 case REL_WHEEL:
1084 mRelWheel = rawEvent->value;
1085 break;
1086 case REL_HWHEEL:
1087 mRelHWheel = rawEvent->value;
1088 break;
1089 }
1090 }
1091}
1092
1093
1094// --- TouchButtonAccumulator ---
1095
1096TouchButtonAccumulator::TouchButtonAccumulator() :
1097 mHaveBtnTouch(false) {
1098 clearButtons();
1099}
1100
1101void TouchButtonAccumulator::configure(InputDevice* device) {
1102 mHaveBtnTouch = device->getEventHub()->hasScanCode(device->getId(), BTN_TOUCH);
1103}
1104
1105void TouchButtonAccumulator::clearButtons() {
1106 mBtnTouch = 0;
1107 mBtnStylus = 0;
1108 mBtnStylus2 = 0;
1109 mBtnToolFinger = 0;
1110 mBtnToolPen = 0;
1111 mBtnToolRubber = 0;
1112}
1113
1114void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
1115 if (rawEvent->type == EV_KEY) {
1116 switch (rawEvent->scanCode) {
1117 case BTN_TOUCH:
1118 mBtnTouch = rawEvent->value;
1119 break;
1120 case BTN_STYLUS:
1121 mBtnStylus = rawEvent->value;
1122 break;
1123 case BTN_STYLUS2:
1124 mBtnStylus2 = rawEvent->value;
1125 break;
1126 case BTN_TOOL_FINGER:
1127 mBtnToolFinger = rawEvent->value;
1128 break;
1129 case BTN_TOOL_PEN:
1130 mBtnToolPen = rawEvent->value;
1131 break;
1132 case BTN_TOOL_RUBBER:
1133 mBtnToolRubber = rawEvent->value;
1134 break;
1135 }
1136 }
1137}
1138
1139uint32_t TouchButtonAccumulator::getButtonState() const {
1140 uint32_t result = 0;
1141 if (mBtnStylus) {
1142 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1143 }
1144 if (mBtnStylus2) {
1145 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1146 }
1147 return result;
1148}
1149
1150int32_t TouchButtonAccumulator::getToolType() const {
1151 if (mBtnToolRubber) {
1152 return AMOTION_EVENT_TOOL_TYPE_ERASER;
1153 }
1154 if (mBtnToolPen) {
1155 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1156 }
1157 if (mBtnToolFinger) {
1158 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1159 }
1160 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1161}
1162
1163bool TouchButtonAccumulator::isActive() const {
1164 return mBtnTouch || mBtnToolFinger || mBtnToolPen
1165 || mBtnToolRubber || mBtnStylus || mBtnStylus2;
1166}
1167
1168bool TouchButtonAccumulator::isHovering() const {
1169 return mHaveBtnTouch && !mBtnTouch;
1170}
1171
1172
1173// --- SingleTouchMotionAccumulator ---
1174
1175SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() {
1176 clearAbsoluteAxes();
1177}
1178
1179void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
1180 mAbsX = 0;
1181 mAbsY = 0;
1182 mAbsPressure = 0;
1183 mAbsToolWidth = 0;
1184 mAbsDistance = 0;
1185}
1186
1187void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1188 if (rawEvent->type == EV_ABS) {
1189 switch (rawEvent->scanCode) {
1190 case ABS_X:
1191 mAbsX = rawEvent->value;
1192 break;
1193 case ABS_Y:
1194 mAbsY = rawEvent->value;
1195 break;
1196 case ABS_PRESSURE:
1197 mAbsPressure = rawEvent->value;
1198 break;
1199 case ABS_TOOL_WIDTH:
1200 mAbsToolWidth = rawEvent->value;
1201 break;
1202 case ABS_DISTANCE:
1203 mAbsDistance = rawEvent->value;
1204 break;
1205 }
1206 }
1207}
1208
1209
1210// --- MultiTouchMotionAccumulator ---
1211
1212MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
1213 mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false) {
1214}
1215
1216MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
1217 delete[] mSlots;
1218}
1219
1220void MultiTouchMotionAccumulator::configure(size_t slotCount, bool usingSlotsProtocol) {
1221 mSlotCount = slotCount;
1222 mUsingSlotsProtocol = usingSlotsProtocol;
1223
1224 delete[] mSlots;
1225 mSlots = new Slot[slotCount];
1226}
1227
1228void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
1229 for (size_t i = 0; i < mSlotCount; i++) {
1230 mSlots[i].clearIfInUse();
1231 }
1232 mCurrentSlot = initialSlot;
1233}
1234
1235void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1236 if (rawEvent->type == EV_ABS) {
1237 bool newSlot = false;
1238 if (mUsingSlotsProtocol) {
1239 if (rawEvent->scanCode == ABS_MT_SLOT) {
1240 mCurrentSlot = rawEvent->value;
1241 newSlot = true;
1242 }
1243 } else if (mCurrentSlot < 0) {
1244 mCurrentSlot = 0;
1245 }
1246
1247 if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) {
1248#if DEBUG_POINTERS
1249 if (newSlot) {
1250 LOGW("MultiTouch device emitted invalid slot index %d but it "
1251 "should be between 0 and %d; ignoring this slot.",
1252 mCurrentSlot, mSlotCount - 1);
1253 }
1254#endif
1255 } else {
1256 Slot* slot = &mSlots[mCurrentSlot];
1257
1258 switch (rawEvent->scanCode) {
1259 case ABS_MT_POSITION_X:
1260 slot->mInUse = true;
1261 slot->mAbsMTPositionX = rawEvent->value;
1262 break;
1263 case ABS_MT_POSITION_Y:
1264 slot->mInUse = true;
1265 slot->mAbsMTPositionY = rawEvent->value;
1266 break;
1267 case ABS_MT_TOUCH_MAJOR:
1268 slot->mInUse = true;
1269 slot->mAbsMTTouchMajor = rawEvent->value;
1270 break;
1271 case ABS_MT_TOUCH_MINOR:
1272 slot->mInUse = true;
1273 slot->mAbsMTTouchMinor = rawEvent->value;
1274 slot->mHaveAbsMTTouchMinor = true;
1275 break;
1276 case ABS_MT_WIDTH_MAJOR:
1277 slot->mInUse = true;
1278 slot->mAbsMTWidthMajor = rawEvent->value;
1279 break;
1280 case ABS_MT_WIDTH_MINOR:
1281 slot->mInUse = true;
1282 slot->mAbsMTWidthMinor = rawEvent->value;
1283 slot->mHaveAbsMTWidthMinor = true;
1284 break;
1285 case ABS_MT_ORIENTATION:
1286 slot->mInUse = true;
1287 slot->mAbsMTOrientation = rawEvent->value;
1288 break;
1289 case ABS_MT_TRACKING_ID:
1290 if (mUsingSlotsProtocol && rawEvent->value < 0) {
1291 slot->clearIfInUse();
1292 } else {
1293 slot->mInUse = true;
1294 slot->mAbsMTTrackingId = rawEvent->value;
1295 }
1296 break;
1297 case ABS_MT_PRESSURE:
1298 slot->mInUse = true;
1299 slot->mAbsMTPressure = rawEvent->value;
1300 break;
1301 case ABS_MT_TOOL_TYPE:
1302 slot->mInUse = true;
1303 slot->mAbsMTToolType = rawEvent->value;
1304 slot->mHaveAbsMTToolType = true;
1305 break;
1306 }
1307 }
1308 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_MT_REPORT) {
1309 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
1310 mCurrentSlot += 1;
1311 }
1312}
1313
1314
1315// --- MultiTouchMotionAccumulator::Slot ---
1316
1317MultiTouchMotionAccumulator::Slot::Slot() {
1318 clear();
1319}
1320
1321void MultiTouchMotionAccumulator::Slot::clearIfInUse() {
1322 if (mInUse) {
1323 clear();
1324 }
1325}
1326
1327void MultiTouchMotionAccumulator::Slot::clear() {
1328 mInUse = false;
1329 mHaveAbsMTTouchMinor = false;
1330 mHaveAbsMTWidthMinor = false;
1331 mHaveAbsMTToolType = false;
1332 mAbsMTPositionX = 0;
1333 mAbsMTPositionY = 0;
1334 mAbsMTTouchMajor = 0;
1335 mAbsMTTouchMinor = 0;
1336 mAbsMTWidthMajor = 0;
1337 mAbsMTWidthMinor = 0;
1338 mAbsMTOrientation = 0;
1339 mAbsMTTrackingId = -1;
1340 mAbsMTPressure = 0;
1341 mAbsMTToolType = 0;
1342 mAbsMTDistance = 0;
1343}
1344
1345int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
1346 if (mHaveAbsMTToolType) {
1347 switch (mAbsMTToolType) {
1348 case MT_TOOL_FINGER:
1349 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1350 case MT_TOOL_PEN:
1351 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1352 }
1353 }
1354 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1355}
1356
1357
Jeff Brown6d0fec22010-07-23 21:28:06 -07001358// --- InputMapper ---
1359
1360InputMapper::InputMapper(InputDevice* device) :
1361 mDevice(device), mContext(device->getContext()) {
1362}
1363
1364InputMapper::~InputMapper() {
1365}
1366
1367void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1368 info->addSource(getSources());
1369}
1370
Jeff Brownef3d7e82010-09-30 14:33:04 -07001371void InputMapper::dump(String8& dump) {
1372}
1373
Jeff Brown474dcb52011-06-14 20:22:50 -07001374void InputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001375}
1376
1377void InputMapper::reset() {
1378}
1379
Jeff Brownaa3855d2011-03-17 01:34:19 -07001380void InputMapper::timeoutExpired(nsecs_t when) {
1381}
1382
Jeff Brown6d0fec22010-07-23 21:28:06 -07001383int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1384 return AKEY_STATE_UNKNOWN;
1385}
1386
1387int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1388 return AKEY_STATE_UNKNOWN;
1389}
1390
1391int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1392 return AKEY_STATE_UNKNOWN;
1393}
1394
1395bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1396 const int32_t* keyCodes, uint8_t* outFlags) {
1397 return false;
1398}
1399
1400int32_t InputMapper::getMetaState() {
1401 return 0;
1402}
1403
Jeff Brown05dc66a2011-03-02 14:41:58 -08001404void InputMapper::fadePointer() {
1405}
1406
Jeff Browncb1404e2011-01-15 18:14:15 -08001407void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1408 const RawAbsoluteAxisInfo& axis, const char* name) {
1409 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001410 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1411 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001412 } else {
1413 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1414 }
1415}
1416
Jeff Brown6d0fec22010-07-23 21:28:06 -07001417
1418// --- SwitchInputMapper ---
1419
1420SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1421 InputMapper(device) {
1422}
1423
1424SwitchInputMapper::~SwitchInputMapper() {
1425}
1426
1427uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001428 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001429}
1430
1431void SwitchInputMapper::process(const RawEvent* rawEvent) {
1432 switch (rawEvent->type) {
1433 case EV_SW:
1434 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
1435 break;
1436 }
1437}
1438
1439void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownb6997262010-10-08 22:31:17 -07001440 getDispatcher()->notifySwitch(when, switchCode, switchValue, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001441}
1442
1443int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1444 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1445}
1446
1447
1448// --- KeyboardInputMapper ---
1449
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001450KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001451 uint32_t source, int32_t keyboardType) :
1452 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001453 mKeyboardType(keyboardType) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001454 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001455}
1456
1457KeyboardInputMapper::~KeyboardInputMapper() {
1458}
1459
Jeff Brown6328cdc2010-07-29 18:18:33 -07001460void KeyboardInputMapper::initializeLocked() {
1461 mLocked.metaState = AMETA_NONE;
1462 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001463}
1464
1465uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001466 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001467}
1468
1469void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1470 InputMapper::populateDeviceInfo(info);
1471
1472 info->setKeyboardType(mKeyboardType);
1473}
1474
Jeff Brownef3d7e82010-09-30 14:33:04 -07001475void KeyboardInputMapper::dump(String8& dump) {
1476 { // acquire lock
1477 AutoMutex _l(mLock);
1478 dump.append(INDENT2 "Keyboard Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001479 dumpParameters(dump);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001480 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
1481 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mLocked.keyDowns.size());
1482 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mLocked.metaState);
1483 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1484 } // release lock
1485}
1486
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001487
Jeff Brown474dcb52011-06-14 20:22:50 -07001488void KeyboardInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
1489 InputMapper::configure(config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001490
Jeff Brown474dcb52011-06-14 20:22:50 -07001491 if (!changes) { // first time only
1492 // Configure basic parameters.
1493 configureParameters();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001494
Jeff Brown474dcb52011-06-14 20:22:50 -07001495 // Reset LEDs.
1496 {
1497 AutoMutex _l(mLock);
1498 resetLedStateLocked();
1499 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001500 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001501}
1502
1503void KeyboardInputMapper::configureParameters() {
1504 mParameters.orientationAware = false;
1505 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
1506 mParameters.orientationAware);
1507
Jeff Brownbc68a592011-07-25 12:58:12 -07001508 mParameters.associatedDisplayId = -1;
1509 if (mParameters.orientationAware) {
1510 mParameters.associatedDisplayId = 0;
1511 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001512}
1513
1514void KeyboardInputMapper::dumpParameters(String8& dump) {
1515 dump.append(INDENT3 "Parameters:\n");
1516 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1517 mParameters.associatedDisplayId);
1518 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1519 toString(mParameters.orientationAware));
1520}
1521
Jeff Brown6d0fec22010-07-23 21:28:06 -07001522void KeyboardInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001523 for (;;) {
1524 int32_t keyCode, scanCode;
1525 { // acquire lock
1526 AutoMutex _l(mLock);
1527
1528 // Synthesize key up event on reset if keys are currently down.
1529 if (mLocked.keyDowns.isEmpty()) {
1530 initializeLocked();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001531 resetLedStateLocked();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001532 break; // done
1533 }
1534
1535 const KeyDown& keyDown = mLocked.keyDowns.top();
1536 keyCode = keyDown.keyCode;
1537 scanCode = keyDown.scanCode;
1538 } // release lock
1539
Jeff Brown6d0fec22010-07-23 21:28:06 -07001540 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001541 processKey(when, false, keyCode, scanCode, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001542 }
1543
1544 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001545 getContext()->updateGlobalMetaState();
1546}
1547
1548void KeyboardInputMapper::process(const RawEvent* rawEvent) {
1549 switch (rawEvent->type) {
1550 case EV_KEY: {
1551 int32_t scanCode = rawEvent->scanCode;
1552 if (isKeyboardOrGamepadKey(scanCode)) {
1553 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
1554 rawEvent->flags);
1555 }
1556 break;
1557 }
1558 }
1559}
1560
1561bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
1562 return scanCode < BTN_MOUSE
1563 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001564 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08001565 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001566}
1567
Jeff Brown6328cdc2010-07-29 18:18:33 -07001568void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
1569 int32_t scanCode, uint32_t policyFlags) {
1570 int32_t newMetaState;
1571 nsecs_t downTime;
1572 bool metaStateChanged = false;
1573
1574 { // acquire lock
1575 AutoMutex _l(mLock);
1576
1577 if (down) {
1578 // Rotate key codes according to orientation if needed.
1579 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001580 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001581 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001582 if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownbc68a592011-07-25 12:58:12 -07001583 false /*external*/, NULL, NULL, & orientation)) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001584 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001585 }
1586
1587 keyCode = rotateKeyCode(keyCode, orientation);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001588 }
1589
Jeff Brown6328cdc2010-07-29 18:18:33 -07001590 // Add key down.
1591 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
1592 if (keyDownIndex >= 0) {
1593 // key repeat, be sure to use same keycode as before in case of rotation
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001594 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001595 } else {
1596 // key down
Jeff Brownfe508922011-01-18 15:10:10 -08001597 if ((policyFlags & POLICY_FLAG_VIRTUAL)
1598 && mContext->shouldDropVirtualKey(when,
1599 getDevice(), keyCode, scanCode)) {
1600 return;
1601 }
1602
Jeff Brown6328cdc2010-07-29 18:18:33 -07001603 mLocked.keyDowns.push();
1604 KeyDown& keyDown = mLocked.keyDowns.editTop();
1605 keyDown.keyCode = keyCode;
1606 keyDown.scanCode = scanCode;
1607 }
1608
1609 mLocked.downTime = when;
1610 } else {
1611 // Remove key down.
1612 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
1613 if (keyDownIndex >= 0) {
1614 // key up, be sure to use same keycode as before in case of rotation
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001615 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001616 mLocked.keyDowns.removeAt(size_t(keyDownIndex));
1617 } else {
1618 // key was not actually down
1619 LOGI("Dropping key up from device %s because the key was not down. "
1620 "keyCode=%d, scanCode=%d",
1621 getDeviceName().string(), keyCode, scanCode);
1622 return;
1623 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001624 }
1625
Jeff Brown6328cdc2010-07-29 18:18:33 -07001626 int32_t oldMetaState = mLocked.metaState;
1627 newMetaState = updateMetaState(keyCode, down, oldMetaState);
1628 if (oldMetaState != newMetaState) {
1629 mLocked.metaState = newMetaState;
1630 metaStateChanged = true;
Jeff Brown497a92c2010-09-12 17:55:08 -07001631 updateLedStateLocked(false);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001632 }
Jeff Brownfd0358292010-06-30 16:10:35 -07001633
Jeff Brown6328cdc2010-07-29 18:18:33 -07001634 downTime = mLocked.downTime;
1635 } // release lock
1636
Jeff Brown56194eb2011-03-02 19:23:13 -08001637 // Key down on external an keyboard should wake the device.
1638 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
1639 // For internal keyboards, the key layout file should specify the policy flags for
1640 // each wake key individually.
1641 // TODO: Use the input device configuration to control this behavior more finely.
1642 if (down && getDevice()->isExternal()
1643 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1644 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1645 }
1646
Jeff Brown6328cdc2010-07-29 18:18:33 -07001647 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001648 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001649 }
1650
Jeff Brown05dc66a2011-03-02 14:41:58 -08001651 if (down && !isMetaKey(keyCode)) {
1652 getContext()->fadePointer();
1653 }
1654
Jeff Brownefd32662011-03-08 15:13:06 -08001655 getDispatcher()->notifyKey(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001656 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1657 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001658}
1659
Jeff Brown6328cdc2010-07-29 18:18:33 -07001660ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) {
1661 size_t n = mLocked.keyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001662 for (size_t i = 0; i < n; i++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001663 if (mLocked.keyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001664 return i;
1665 }
1666 }
1667 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001668}
1669
Jeff Brown6d0fec22010-07-23 21:28:06 -07001670int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1671 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1672}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001673
Jeff Brown6d0fec22010-07-23 21:28:06 -07001674int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1675 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1676}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001677
Jeff Brown6d0fec22010-07-23 21:28:06 -07001678bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1679 const int32_t* keyCodes, uint8_t* outFlags) {
1680 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1681}
1682
1683int32_t KeyboardInputMapper::getMetaState() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001684 { // acquire lock
1685 AutoMutex _l(mLock);
1686 return mLocked.metaState;
1687 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001688}
1689
Jeff Brown49ed71d2010-12-06 17:13:33 -08001690void KeyboardInputMapper::resetLedStateLocked() {
1691 initializeLedStateLocked(mLocked.capsLockLedState, LED_CAPSL);
1692 initializeLedStateLocked(mLocked.numLockLedState, LED_NUML);
1693 initializeLedStateLocked(mLocked.scrollLockLedState, LED_SCROLLL);
1694
1695 updateLedStateLocked(true);
1696}
1697
1698void KeyboardInputMapper::initializeLedStateLocked(LockedState::LedState& ledState, int32_t led) {
1699 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
1700 ledState.on = false;
1701}
1702
Jeff Brown497a92c2010-09-12 17:55:08 -07001703void KeyboardInputMapper::updateLedStateLocked(bool reset) {
1704 updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001705 AMETA_CAPS_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001706 updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001707 AMETA_NUM_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001708 updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001709 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001710}
1711
1712void KeyboardInputMapper::updateLedStateForModifierLocked(LockedState::LedState& ledState,
1713 int32_t led, int32_t modifier, bool reset) {
1714 if (ledState.avail) {
1715 bool desiredState = (mLocked.metaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001716 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001717 getEventHub()->setLedState(getDeviceId(), led, desiredState);
1718 ledState.on = desiredState;
1719 }
1720 }
1721}
1722
Jeff Brown6d0fec22010-07-23 21:28:06 -07001723
Jeff Brown83c09682010-12-23 17:50:18 -08001724// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07001725
Jeff Brown83c09682010-12-23 17:50:18 -08001726CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001727 InputMapper(device) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001728 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001729}
1730
Jeff Brown83c09682010-12-23 17:50:18 -08001731CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001732}
1733
Jeff Brown83c09682010-12-23 17:50:18 -08001734uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001735 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001736}
1737
Jeff Brown83c09682010-12-23 17:50:18 -08001738void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001739 InputMapper::populateDeviceInfo(info);
1740
Jeff Brown83c09682010-12-23 17:50:18 -08001741 if (mParameters.mode == Parameters::MODE_POINTER) {
1742 float minX, minY, maxX, maxY;
1743 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08001744 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
1745 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08001746 }
1747 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08001748 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
1749 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08001750 }
Jeff Brownefd32662011-03-08 15:13:06 -08001751 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001752
Jeff Brown49754db2011-07-01 17:37:58 -07001753 if (mCursorMotionAccumulator.haveRelativeVWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08001754 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001755 }
Jeff Brown49754db2011-07-01 17:37:58 -07001756 if (mCursorMotionAccumulator.haveRelativeHWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08001757 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001758 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001759}
1760
Jeff Brown83c09682010-12-23 17:50:18 -08001761void CursorInputMapper::dump(String8& dump) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07001762 { // acquire lock
1763 AutoMutex _l(mLock);
Jeff Brown83c09682010-12-23 17:50:18 -08001764 dump.append(INDENT2 "Cursor Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001765 dumpParameters(dump);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001766 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
1767 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001768 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1769 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
Jeff Brown49754db2011-07-01 17:37:58 -07001770 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
1771 toString(mCursorMotionAccumulator.haveRelativeVWheel()));
1772 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
1773 toString(mCursorMotionAccumulator.haveRelativeHWheel()));
Jeff Brown6f2fba42011-02-19 01:08:02 -08001774 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
1775 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brownefd32662011-03-08 15:13:06 -08001776 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mLocked.buttonState);
1777 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mLocked.buttonState)));
Jeff Brownef3d7e82010-09-30 14:33:04 -07001778 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1779 } // release lock
1780}
1781
Jeff Brown474dcb52011-06-14 20:22:50 -07001782void CursorInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
1783 InputMapper::configure(config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001784
Jeff Brown474dcb52011-06-14 20:22:50 -07001785 if (!changes) { // first time only
Jeff Brown49754db2011-07-01 17:37:58 -07001786 mCursorMotionAccumulator.configure(getDevice());
1787
Jeff Brown474dcb52011-06-14 20:22:50 -07001788 // Configure basic parameters.
1789 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08001790
Jeff Brown474dcb52011-06-14 20:22:50 -07001791 // Configure device mode.
1792 switch (mParameters.mode) {
1793 case Parameters::MODE_POINTER:
1794 mSource = AINPUT_SOURCE_MOUSE;
1795 mXPrecision = 1.0f;
1796 mYPrecision = 1.0f;
1797 mXScale = 1.0f;
1798 mYScale = 1.0f;
1799 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
1800 break;
1801 case Parameters::MODE_NAVIGATION:
1802 mSource = AINPUT_SOURCE_TRACKBALL;
1803 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1804 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1805 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1806 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1807 break;
1808 }
1809
1810 mVWheelScale = 1.0f;
1811 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08001812 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001813
Jeff Brown474dcb52011-06-14 20:22:50 -07001814 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
1815 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
1816 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
1817 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
1818 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001819}
1820
Jeff Brown83c09682010-12-23 17:50:18 -08001821void CursorInputMapper::configureParameters() {
1822 mParameters.mode = Parameters::MODE_POINTER;
1823 String8 cursorModeString;
1824 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
1825 if (cursorModeString == "navigation") {
1826 mParameters.mode = Parameters::MODE_NAVIGATION;
1827 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
1828 LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
1829 }
1830 }
1831
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001832 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08001833 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001834 mParameters.orientationAware);
1835
Jeff Brownbc68a592011-07-25 12:58:12 -07001836 mParameters.associatedDisplayId = -1;
1837 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
1838 mParameters.associatedDisplayId = 0;
1839 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001840}
1841
Jeff Brown83c09682010-12-23 17:50:18 -08001842void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001843 dump.append(INDENT3 "Parameters:\n");
1844 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1845 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08001846
1847 switch (mParameters.mode) {
1848 case Parameters::MODE_POINTER:
1849 dump.append(INDENT4 "Mode: pointer\n");
1850 break;
1851 case Parameters::MODE_NAVIGATION:
1852 dump.append(INDENT4 "Mode: navigation\n");
1853 break;
1854 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07001855 LOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08001856 }
1857
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001858 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1859 toString(mParameters.orientationAware));
1860}
1861
Jeff Brown83c09682010-12-23 17:50:18 -08001862void CursorInputMapper::initializeLocked() {
Jeff Brown49754db2011-07-01 17:37:58 -07001863 mCursorButtonAccumulator.clearButtons();
1864 mCursorMotionAccumulator.clearRelativeAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001865
Jeff Brownefd32662011-03-08 15:13:06 -08001866 mLocked.buttonState = 0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001867 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001868}
1869
Jeff Brown83c09682010-12-23 17:50:18 -08001870void CursorInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001871 for (;;) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001872 int32_t buttonState;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001873 { // acquire lock
1874 AutoMutex _l(mLock);
1875
Jeff Brownefd32662011-03-08 15:13:06 -08001876 buttonState = mLocked.buttonState;
1877 if (!buttonState) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001878 initializeLocked();
1879 break; // done
1880 }
1881 } // release lock
1882
Jeff Brown19c97d462011-06-01 12:33:19 -07001883 // Reset velocity.
1884 mPointerVelocityControl.reset();
1885 mWheelXVelocityControl.reset();
1886 mWheelYVelocityControl.reset();
1887
Jeff Brown83c09682010-12-23 17:50:18 -08001888 // Synthesize button up event on reset.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001889 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown49754db2011-07-01 17:37:58 -07001890 mCursorButtonAccumulator.clearButtons();
1891 mCursorMotionAccumulator.clearRelativeAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001892 sync(when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001893 }
1894
Jeff Brown6d0fec22010-07-23 21:28:06 -07001895 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001896}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001897
Jeff Brown83c09682010-12-23 17:50:18 -08001898void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07001899 mCursorButtonAccumulator.process(rawEvent);
1900 mCursorMotionAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08001901
Jeff Brown49754db2011-07-01 17:37:58 -07001902 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
1903 sync(rawEvent->when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001904 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001905}
1906
Jeff Brown83c09682010-12-23 17:50:18 -08001907void CursorInputMapper::sync(nsecs_t when) {
Jeff Brown9626b142011-03-03 02:09:54 -08001908 int32_t motionEventAction;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001909 int32_t lastButtonState, currentButtonState;
1910 PointerProperties pointerProperties;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001911 PointerCoords pointerCoords;
1912 nsecs_t downTime;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001913 float vscroll, hscroll;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001914 { // acquire lock
1915 AutoMutex _l(mLock);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001916
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001917 lastButtonState = mLocked.buttonState;
Jeff Brown49754db2011-07-01 17:37:58 -07001918 currentButtonState = mCursorButtonAccumulator.getButtonState();
1919 mLocked.buttonState = currentButtonState;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001920
Jeff Brown49754db2011-07-01 17:37:58 -07001921 bool wasDown = isPointerDown(lastButtonState);
1922 bool down = isPointerDown(currentButtonState);
1923 bool downChanged;
1924 if (!wasDown && down) {
1925 mLocked.downTime = when;
1926 downChanged = true;
1927 } else if (wasDown && !down) {
1928 downChanged = true;
Jeff Brownefd32662011-03-08 15:13:06 -08001929 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08001930 downChanged = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001931 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001932 downTime = mLocked.downTime;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001933
Jeff Brown6328cdc2010-07-29 18:18:33 -07001934 if (downChanged) {
Jeff Brownefd32662011-03-08 15:13:06 -08001935 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
1936 } else if (down || mPointerController == NULL) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001937 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browncc0c1592011-02-19 05:07:28 -08001938 } else {
1939 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001940 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001941
Jeff Brown49754db2011-07-01 17:37:58 -07001942 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
1943 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
1944
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001945 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
Jeff Brown83c09682010-12-23 17:50:18 -08001946 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001947 // Rotate motion based on display orientation if needed.
1948 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1949 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001950 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownbc68a592011-07-25 12:58:12 -07001951 false /*external*/, NULL, NULL, & orientation)) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001952 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001953 }
1954
Jeff Brown612891e2011-07-15 20:44:17 -07001955 rotateDelta(orientation, &deltaX, &deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001956 }
Jeff Brown83c09682010-12-23 17:50:18 -08001957
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001958 pointerProperties.clear();
1959 pointerProperties.id = 0;
1960 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
1961
1962 pointerCoords.clear();
1963
Jeff Brown49754db2011-07-01 17:37:58 -07001964 vscroll = mCursorMotionAccumulator.getRelativeVWheel();
1965 hscroll = mCursorMotionAccumulator.getRelativeHWheel();
Jeff Brown19c97d462011-06-01 12:33:19 -07001966
Jeff Brown49754db2011-07-01 17:37:58 -07001967 mWheelYVelocityControl.move(when, NULL, &vscroll);
Jeff Brown19c97d462011-06-01 12:33:19 -07001968 mWheelXVelocityControl.move(when, &hscroll, NULL);
1969
1970 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown2352b972011-04-12 22:39:53 -07001971
Jeff Brown83c09682010-12-23 17:50:18 -08001972 if (mPointerController != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -07001973 if (deltaX != 0 || deltaY != 0 || vscroll != 0 || hscroll != 0
Jeff Brown49754db2011-07-01 17:37:58 -07001974 || currentButtonState != lastButtonState) {
Jeff Brown2352b972011-04-12 22:39:53 -07001975 mPointerController->setPresentation(
1976 PointerControllerInterface::PRESENTATION_POINTER);
1977
1978 if (deltaX != 0 || deltaY != 0) {
1979 mPointerController->move(deltaX, deltaY);
1980 }
1981
Jeff Brown49754db2011-07-01 17:37:58 -07001982 if (currentButtonState != lastButtonState) {
1983 mPointerController->setButtonState(currentButtonState);
Jeff Brown2352b972011-04-12 22:39:53 -07001984 }
1985
Jeff Brown538881e2011-05-25 18:23:38 -07001986 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08001987 }
Jeff Brownefd32662011-03-08 15:13:06 -08001988
Jeff Brown91c69ab2011-02-14 17:03:18 -08001989 float x, y;
1990 mPointerController->getPosition(&x, &y);
Jeff Brownebbd5d12011-02-17 13:01:34 -08001991 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
1992 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown83c09682010-12-23 17:50:18 -08001993 } else {
Jeff Brownebbd5d12011-02-17 13:01:34 -08001994 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
1995 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
Jeff Brown83c09682010-12-23 17:50:18 -08001996 }
1997
Jeff Brownefd32662011-03-08 15:13:06 -08001998 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001999 } // release lock
2000
Jeff Brown56194eb2011-03-02 19:23:13 -08002001 // Moving an external trackball or mouse should wake the device.
2002 // We don't do this for internal cursor devices to prevent them from waking up
2003 // the device in your pocket.
2004 // TODO: Use the input device configuration to control this behavior more finely.
2005 uint32_t policyFlags = 0;
2006 if (getDevice()->isExternal()) {
2007 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2008 }
2009
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002010 // Synthesize key down from buttons if needed.
2011 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2012 policyFlags, lastButtonState, currentButtonState);
2013
2014 // Send motion event.
Jeff Brown6d0fec22010-07-23 21:28:06 -07002015 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownefd32662011-03-08 15:13:06 -08002016 getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags,
Jeff Browna6111372011-07-14 21:48:23 -07002017 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002018 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownb6997262010-10-08 22:31:17 -07002019
Jeff Browna032cc02011-03-07 16:56:21 -08002020 // Send hover move after UP to tell the application that the mouse is hovering now.
2021 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2022 && mPointerController != NULL) {
2023 getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002024 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2025 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2026 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Browna032cc02011-03-07 16:56:21 -08002027 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002028
Jeff Browna032cc02011-03-07 16:56:21 -08002029 // Send scroll events.
Jeff Brown33bbfd22011-02-24 20:55:35 -08002030 if (vscroll != 0 || hscroll != 0) {
2031 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2032 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2033
Jeff Brownefd32662011-03-08 15:13:06 -08002034 getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002035 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2036 AMOTION_EVENT_EDGE_FLAG_NONE,
2037 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002038 }
Jeff Browna032cc02011-03-07 16:56:21 -08002039
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002040 // Synthesize key up from buttons if needed.
2041 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2042 policyFlags, lastButtonState, currentButtonState);
2043
Jeff Brown49754db2011-07-01 17:37:58 -07002044 mCursorMotionAccumulator.clearRelativeAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002045}
2046
Jeff Brown83c09682010-12-23 17:50:18 -08002047int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002048 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2049 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2050 } else {
2051 return AKEY_STATE_UNKNOWN;
2052 }
2053}
2054
Jeff Brown05dc66a2011-03-02 14:41:58 -08002055void CursorInputMapper::fadePointer() {
2056 { // acquire lock
2057 AutoMutex _l(mLock);
Jeff Brownefd32662011-03-08 15:13:06 -08002058 if (mPointerController != NULL) {
Jeff Brown538881e2011-05-25 18:23:38 -07002059 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
Jeff Brownefd32662011-03-08 15:13:06 -08002060 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002061 } // release lock
2062}
2063
Jeff Brown6d0fec22010-07-23 21:28:06 -07002064
2065// --- TouchInputMapper ---
2066
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002067TouchInputMapper::TouchInputMapper(InputDevice* device) :
2068 InputMapper(device) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002069 mLocked.surfaceOrientation = -1;
2070 mLocked.surfaceWidth = -1;
2071 mLocked.surfaceHeight = -1;
2072
2073 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002074}
2075
2076TouchInputMapper::~TouchInputMapper() {
2077}
2078
2079uint32_t TouchInputMapper::getSources() {
Jeff Brownace13b12011-03-09 17:39:48 -08002080 return mTouchSource | mPointerSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002081}
2082
2083void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2084 InputMapper::populateDeviceInfo(info);
2085
Jeff Brown6328cdc2010-07-29 18:18:33 -07002086 { // acquire lock
2087 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002088
Jeff Brown6328cdc2010-07-29 18:18:33 -07002089 // Ensure surface information is up to date so that orientation changes are
2090 // noticed immediately.
Jeff Brownefd32662011-03-08 15:13:06 -08002091 if (!configureSurfaceLocked()) {
2092 return;
2093 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002094
Jeff Brownefd32662011-03-08 15:13:06 -08002095 info->addMotionRange(mLocked.orientedRanges.x);
2096 info->addMotionRange(mLocked.orientedRanges.y);
Jeff Brown8d608662010-08-30 03:02:23 -07002097
2098 if (mLocked.orientedRanges.havePressure) {
Jeff Brownefd32662011-03-08 15:13:06 -08002099 info->addMotionRange(mLocked.orientedRanges.pressure);
Jeff Brown8d608662010-08-30 03:02:23 -07002100 }
2101
2102 if (mLocked.orientedRanges.haveSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08002103 info->addMotionRange(mLocked.orientedRanges.size);
Jeff Brown8d608662010-08-30 03:02:23 -07002104 }
2105
Jeff Brownc6d282b2010-10-14 21:42:15 -07002106 if (mLocked.orientedRanges.haveTouchSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08002107 info->addMotionRange(mLocked.orientedRanges.touchMajor);
2108 info->addMotionRange(mLocked.orientedRanges.touchMinor);
Jeff Brown8d608662010-08-30 03:02:23 -07002109 }
2110
Jeff Brownc6d282b2010-10-14 21:42:15 -07002111 if (mLocked.orientedRanges.haveToolSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08002112 info->addMotionRange(mLocked.orientedRanges.toolMajor);
2113 info->addMotionRange(mLocked.orientedRanges.toolMinor);
Jeff Brown8d608662010-08-30 03:02:23 -07002114 }
2115
2116 if (mLocked.orientedRanges.haveOrientation) {
Jeff Brownefd32662011-03-08 15:13:06 -08002117 info->addMotionRange(mLocked.orientedRanges.orientation);
Jeff Brown8d608662010-08-30 03:02:23 -07002118 }
Jeff Brownace13b12011-03-09 17:39:48 -08002119
Jeff Brown80fd47c2011-05-24 01:07:44 -07002120 if (mLocked.orientedRanges.haveDistance) {
2121 info->addMotionRange(mLocked.orientedRanges.distance);
2122 }
2123
Jeff Brownace13b12011-03-09 17:39:48 -08002124 if (mPointerController != NULL) {
2125 float minX, minY, maxX, maxY;
2126 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
2127 info->addMotionRange(AMOTION_EVENT_AXIS_X, mPointerSource,
2128 minX, maxX, 0.0f, 0.0f);
2129 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mPointerSource,
2130 minY, maxY, 0.0f, 0.0f);
2131 }
2132 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mPointerSource,
2133 0.0f, 1.0f, 0.0f, 0.0f);
2134 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002135 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07002136}
2137
Jeff Brownef3d7e82010-09-30 14:33:04 -07002138void TouchInputMapper::dump(String8& dump) {
2139 { // acquire lock
2140 AutoMutex _l(mLock);
2141 dump.append(INDENT2 "Touch Input Mapper:\n");
Jeff Brownef3d7e82010-09-30 14:33:04 -07002142 dumpParameters(dump);
2143 dumpVirtualKeysLocked(dump);
2144 dumpRawAxes(dump);
2145 dumpCalibration(dump);
2146 dumpSurfaceLocked(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002147
Jeff Brown511ee5f2010-10-18 13:32:20 -07002148 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
Jeff Brownc6d282b2010-10-14 21:42:15 -07002149 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mLocked.xScale);
2150 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mLocked.yScale);
2151 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mLocked.xPrecision);
2152 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mLocked.yPrecision);
2153 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mLocked.geometricScale);
2154 dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mLocked.toolSizeLinearScale);
2155 dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mLocked.toolSizeLinearBias);
2156 dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mLocked.toolSizeAreaScale);
2157 dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mLocked.toolSizeAreaBias);
2158 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mLocked.pressureScale);
2159 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mLocked.sizeScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002160 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mLocked.orientationScale);
Jeff Brown80fd47c2011-05-24 01:07:44 -07002161 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mLocked.distanceScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002162
2163 dump.appendFormat(INDENT3 "Last Touch:\n");
Jeff Brownace13b12011-03-09 17:39:48 -08002164 dump.appendFormat(INDENT4 "Button State: 0x%08x\n", mLastTouch.buttonState);
Jeff Brownaba321a2011-06-28 20:34:40 -07002165 dump.appendFormat(INDENT4 "Pointer Count: %d\n", mLastTouch.pointerCount);
2166 for (uint32_t i = 0; i < mLastTouch.pointerCount; i++) {
2167 const PointerData& pointer = mLastTouch.pointers[i];
2168 dump.appendFormat(INDENT5 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2169 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
Jeff Brown49754db2011-07-01 17:37:58 -07002170 "orientation=%d, distance=%d, toolType=%d, isHovering=%s\n", i,
Jeff Brownaba321a2011-06-28 20:34:40 -07002171 pointer.id, pointer.x, pointer.y, pointer.pressure,
2172 pointer.touchMajor, pointer.touchMinor, pointer.toolMajor, pointer.toolMinor,
Jeff Brown49754db2011-07-01 17:37:58 -07002173 pointer.orientation, pointer.distance,
2174 pointer.toolType, toString(pointer.isHovering));
Jeff Brownaba321a2011-06-28 20:34:40 -07002175 }
Jeff Brownace13b12011-03-09 17:39:48 -08002176
2177 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2178 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2179 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
2180 mLocked.pointerGestureXMovementScale);
2181 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
2182 mLocked.pointerGestureYMovementScale);
2183 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
2184 mLocked.pointerGestureXZoomScale);
2185 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
2186 mLocked.pointerGestureYZoomScale);
Jeff Brown2352b972011-04-12 22:39:53 -07002187 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2188 mLocked.pointerGestureMaxSwipeWidth);
Jeff Brownace13b12011-03-09 17:39:48 -08002189 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002190 } // release lock
2191}
2192
Jeff Brown6328cdc2010-07-29 18:18:33 -07002193void TouchInputMapper::initializeLocked() {
2194 mCurrentTouch.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002195 mLastTouch.clear();
2196 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002197
Jeff Brown6328cdc2010-07-29 18:18:33 -07002198 mLocked.currentVirtualKey.down = false;
Jeff Brown8d608662010-08-30 03:02:23 -07002199
2200 mLocked.orientedRanges.havePressure = false;
2201 mLocked.orientedRanges.haveSize = false;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002202 mLocked.orientedRanges.haveTouchSize = false;
2203 mLocked.orientedRanges.haveToolSize = false;
Jeff Brown8d608662010-08-30 03:02:23 -07002204 mLocked.orientedRanges.haveOrientation = false;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002205 mLocked.orientedRanges.haveDistance = false;
Jeff Brownace13b12011-03-09 17:39:48 -08002206
2207 mPointerGesture.reset();
Jeff Brown8d608662010-08-30 03:02:23 -07002208}
2209
Jeff Brown474dcb52011-06-14 20:22:50 -07002210void TouchInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
2211 InputMapper::configure(config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002212
Jeff Brown474dcb52011-06-14 20:22:50 -07002213 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002214
Jeff Brown474dcb52011-06-14 20:22:50 -07002215 if (!changes) { // first time only
2216 // Configure basic parameters.
2217 configureParameters();
2218
2219 // Configure sources.
2220 switch (mParameters.deviceType) {
2221 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2222 mTouchSource = AINPUT_SOURCE_TOUCHSCREEN;
2223 mPointerSource = 0;
2224 break;
2225 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2226 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
2227 mPointerSource = 0;
2228 break;
2229 case Parameters::DEVICE_TYPE_POINTER:
2230 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
2231 mPointerSource = AINPUT_SOURCE_MOUSE;
2232 break;
2233 default:
2234 LOG_ASSERT(false);
2235 }
2236
2237 // Configure absolute axis information.
2238 configureRawAxes();
2239
2240 // Prepare input device calibration.
2241 parseCalibration();
2242 resolveCalibration();
2243
2244 { // acquire lock
2245 AutoMutex _l(mLock);
2246
2247 // Configure surface dimensions and orientation.
2248 configureSurfaceLocked();
2249 } // release lock
Jeff Brown83c09682010-12-23 17:50:18 -08002250 }
2251
Jeff Brown474dcb52011-06-14 20:22:50 -07002252 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2253 mPointerGesture.pointerVelocityControl.setParameters(
2254 mConfig.pointerVelocityControlParameters);
2255 }
Jeff Brown8d608662010-08-30 03:02:23 -07002256
Jeff Brown474dcb52011-06-14 20:22:50 -07002257 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT)) {
2258 // Reset the touch screen when pointer gesture enablement changes.
2259 reset();
2260 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002261}
2262
Jeff Brown8d608662010-08-30 03:02:23 -07002263void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002264 // Use the pointer presentation mode for devices that do not support distinct
2265 // multitouch. The spot-based presentation relies on being able to accurately
2266 // locate two or more fingers on the touch pad.
2267 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2268 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002269
Jeff Brown538881e2011-05-25 18:23:38 -07002270 String8 gestureModeString;
2271 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2272 gestureModeString)) {
2273 if (gestureModeString == "pointer") {
2274 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2275 } else if (gestureModeString == "spots") {
2276 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2277 } else if (gestureModeString != "default") {
2278 LOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
2279 }
2280 }
2281
Jeff Brownace13b12011-03-09 17:39:48 -08002282 if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
2283 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2284 // The device is a cursor device with a touch pad attached.
2285 // By default don't use the touch pad to move the pointer.
2286 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002287 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2288 // The device is a pointing device like a track pad.
2289 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2290 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2291 // The device is a touch screen.
2292 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownace13b12011-03-09 17:39:48 -08002293 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002294 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002295 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2296 }
2297
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002298 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002299 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2300 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002301 if (deviceTypeString == "touchScreen") {
2302 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002303 } else if (deviceTypeString == "touchPad") {
2304 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002305 } else if (deviceTypeString == "pointer") {
2306 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002307 } else if (deviceTypeString != "default") {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002308 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
2309 }
2310 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002311
Jeff Brownefd32662011-03-08 15:13:06 -08002312 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002313 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2314 mParameters.orientationAware);
2315
Jeff Brownbc68a592011-07-25 12:58:12 -07002316 mParameters.associatedDisplayId = -1;
2317 mParameters.associatedDisplayIsExternal = false;
2318 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002319 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002320 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2321 mParameters.associatedDisplayIsExternal =
2322 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2323 && getDevice()->isExternal();
2324 mParameters.associatedDisplayId = 0;
2325 }
Jeff Brown8d608662010-08-30 03:02:23 -07002326}
2327
Jeff Brownef3d7e82010-09-30 14:33:04 -07002328void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002329 dump.append(INDENT3 "Parameters:\n");
2330
Jeff Brown538881e2011-05-25 18:23:38 -07002331 switch (mParameters.gestureMode) {
2332 case Parameters::GESTURE_MODE_POINTER:
2333 dump.append(INDENT4 "GestureMode: pointer\n");
2334 break;
2335 case Parameters::GESTURE_MODE_SPOTS:
2336 dump.append(INDENT4 "GestureMode: spots\n");
2337 break;
2338 default:
2339 assert(false);
2340 }
2341
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002342 switch (mParameters.deviceType) {
2343 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2344 dump.append(INDENT4 "DeviceType: touchScreen\n");
2345 break;
2346 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2347 dump.append(INDENT4 "DeviceType: touchPad\n");
2348 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002349 case Parameters::DEVICE_TYPE_POINTER:
2350 dump.append(INDENT4 "DeviceType: pointer\n");
2351 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002352 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002353 LOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002354 }
2355
2356 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2357 mParameters.associatedDisplayId);
2358 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2359 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002360}
2361
Jeff Brown8d608662010-08-30 03:02:23 -07002362void TouchInputMapper::configureRawAxes() {
2363 mRawAxes.x.clear();
2364 mRawAxes.y.clear();
2365 mRawAxes.pressure.clear();
2366 mRawAxes.touchMajor.clear();
2367 mRawAxes.touchMinor.clear();
2368 mRawAxes.toolMajor.clear();
2369 mRawAxes.toolMinor.clear();
2370 mRawAxes.orientation.clear();
Jeff Brown80fd47c2011-05-24 01:07:44 -07002371 mRawAxes.distance.clear();
2372 mRawAxes.trackingId.clear();
2373 mRawAxes.slot.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002374}
2375
Jeff Brownef3d7e82010-09-30 14:33:04 -07002376void TouchInputMapper::dumpRawAxes(String8& dump) {
2377 dump.append(INDENT3 "Raw Axes:\n");
Jeff Browncb1404e2011-01-15 18:14:15 -08002378 dumpRawAbsoluteAxisInfo(dump, mRawAxes.x, "X");
2379 dumpRawAbsoluteAxisInfo(dump, mRawAxes.y, "Y");
2380 dumpRawAbsoluteAxisInfo(dump, mRawAxes.pressure, "Pressure");
2381 dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor");
2382 dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor");
2383 dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor");
2384 dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor");
2385 dumpRawAbsoluteAxisInfo(dump, mRawAxes.orientation, "Orientation");
Jeff Brown80fd47c2011-05-24 01:07:44 -07002386 dumpRawAbsoluteAxisInfo(dump, mRawAxes.distance, "Distance");
2387 dumpRawAbsoluteAxisInfo(dump, mRawAxes.trackingId, "TrackingId");
2388 dumpRawAbsoluteAxisInfo(dump, mRawAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002389}
2390
Jeff Brown6328cdc2010-07-29 18:18:33 -07002391bool TouchInputMapper::configureSurfaceLocked() {
Jeff Brown9626b142011-03-03 02:09:54 -08002392 // Ensure we have valid X and Y axes.
2393 if (!mRawAxes.x.valid || !mRawAxes.y.valid) {
2394 LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
2395 "The device will be inoperable.", getDeviceName().string());
2396 return false;
2397 }
2398
Jeff Brown6d0fec22010-07-23 21:28:06 -07002399 // Update orientation and dimensions if needed.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002400 int32_t orientation = DISPLAY_ORIENTATION_0;
Jeff Brown9626b142011-03-03 02:09:54 -08002401 int32_t width = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2402 int32_t height = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002403
2404 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002405 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002406 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownbc68a592011-07-25 12:58:12 -07002407 mParameters.associatedDisplayIsExternal,
Jeff Brownefd32662011-03-08 15:13:06 -08002408 &mLocked.associatedDisplayWidth, &mLocked.associatedDisplayHeight,
2409 &mLocked.associatedDisplayOrientation)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002410 return false;
2411 }
Jeff Brownefd32662011-03-08 15:13:06 -08002412
2413 // A touch screen inherits the dimensions of the display.
2414 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
2415 width = mLocked.associatedDisplayWidth;
2416 height = mLocked.associatedDisplayHeight;
2417 }
2418
2419 // The device inherits the orientation of the display if it is orientation aware.
2420 if (mParameters.orientationAware) {
2421 orientation = mLocked.associatedDisplayOrientation;
2422 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002423 }
2424
Jeff Brownace13b12011-03-09 17:39:48 -08002425 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2426 && mPointerController == NULL) {
2427 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2428 }
2429
Jeff Brown6328cdc2010-07-29 18:18:33 -07002430 bool orientationChanged = mLocked.surfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002431 if (orientationChanged) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002432 mLocked.surfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002433 }
2434
Jeff Brown6328cdc2010-07-29 18:18:33 -07002435 bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002436 if (sizeChanged) {
Jeff Brownefd32662011-03-08 15:13:06 -08002437 LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002438 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown8d608662010-08-30 03:02:23 -07002439
Jeff Brown6328cdc2010-07-29 18:18:33 -07002440 mLocked.surfaceWidth = width;
2441 mLocked.surfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002442
Jeff Brown8d608662010-08-30 03:02:23 -07002443 // Configure X and Y factors.
Jeff Brown9626b142011-03-03 02:09:54 -08002444 mLocked.xScale = float(width) / (mRawAxes.x.maxValue - mRawAxes.x.minValue + 1);
2445 mLocked.yScale = float(height) / (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1);
2446 mLocked.xPrecision = 1.0f / mLocked.xScale;
2447 mLocked.yPrecision = 1.0f / mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002448
Jeff Brownefd32662011-03-08 15:13:06 -08002449 mLocked.orientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
2450 mLocked.orientedRanges.x.source = mTouchSource;
2451 mLocked.orientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
2452 mLocked.orientedRanges.y.source = mTouchSource;
2453
Jeff Brown9626b142011-03-03 02:09:54 -08002454 configureVirtualKeysLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002455
Jeff Brown8d608662010-08-30 03:02:23 -07002456 // Scale factor for terms that are not oriented in a particular axis.
2457 // If the pixels are square then xScale == yScale otherwise we fake it
2458 // by choosing an average.
2459 mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002460
Jeff Brown8d608662010-08-30 03:02:23 -07002461 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002462 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002463
Jeff Brown8d608662010-08-30 03:02:23 -07002464 // TouchMajor and TouchMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002465 if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) {
2466 mLocked.orientedRanges.haveTouchSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002467
2468 mLocked.orientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
2469 mLocked.orientedRanges.touchMajor.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002470 mLocked.orientedRanges.touchMajor.min = 0;
2471 mLocked.orientedRanges.touchMajor.max = diagonalSize;
2472 mLocked.orientedRanges.touchMajor.flat = 0;
2473 mLocked.orientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002474
Jeff Brown8d608662010-08-30 03:02:23 -07002475 mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor;
Jeff Brownefd32662011-03-08 15:13:06 -08002476 mLocked.orientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002477 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002478
Jeff Brown8d608662010-08-30 03:02:23 -07002479 // ToolMajor and ToolMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002480 mLocked.toolSizeLinearScale = 0;
2481 mLocked.toolSizeLinearBias = 0;
2482 mLocked.toolSizeAreaScale = 0;
2483 mLocked.toolSizeAreaBias = 0;
2484 if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
2485 if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) {
2486 if (mCalibration.haveToolSizeLinearScale) {
2487 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
Jeff Brown8d608662010-08-30 03:02:23 -07002488 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002489 mLocked.toolSizeLinearScale = float(min(width, height))
Jeff Brown8d608662010-08-30 03:02:23 -07002490 / mRawAxes.toolMajor.maxValue;
2491 }
2492
Jeff Brownc6d282b2010-10-14 21:42:15 -07002493 if (mCalibration.haveToolSizeLinearBias) {
2494 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
2495 }
2496 } else if (mCalibration.toolSizeCalibration ==
2497 Calibration::TOOL_SIZE_CALIBRATION_AREA) {
2498 if (mCalibration.haveToolSizeLinearScale) {
2499 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
2500 } else {
2501 mLocked.toolSizeLinearScale = min(width, height);
2502 }
2503
2504 if (mCalibration.haveToolSizeLinearBias) {
2505 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
2506 }
2507
2508 if (mCalibration.haveToolSizeAreaScale) {
2509 mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale;
2510 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
2511 mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue;
2512 }
2513
2514 if (mCalibration.haveToolSizeAreaBias) {
2515 mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -07002516 }
2517 }
2518
Jeff Brownc6d282b2010-10-14 21:42:15 -07002519 mLocked.orientedRanges.haveToolSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002520
2521 mLocked.orientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
2522 mLocked.orientedRanges.toolMajor.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002523 mLocked.orientedRanges.toolMajor.min = 0;
2524 mLocked.orientedRanges.toolMajor.max = diagonalSize;
2525 mLocked.orientedRanges.toolMajor.flat = 0;
2526 mLocked.orientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002527
Jeff Brown8d608662010-08-30 03:02:23 -07002528 mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor;
Jeff Brownefd32662011-03-08 15:13:06 -08002529 mLocked.orientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002530 }
2531
2532 // Pressure factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002533 mLocked.pressureScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002534 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
2535 RawAbsoluteAxisInfo rawPressureAxis;
2536 switch (mCalibration.pressureSource) {
2537 case Calibration::PRESSURE_SOURCE_PRESSURE:
2538 rawPressureAxis = mRawAxes.pressure;
2539 break;
2540 case Calibration::PRESSURE_SOURCE_TOUCH:
2541 rawPressureAxis = mRawAxes.touchMajor;
2542 break;
2543 default:
2544 rawPressureAxis.clear();
2545 }
2546
Jeff Brown8d608662010-08-30 03:02:23 -07002547 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2548 || mCalibration.pressureCalibration
2549 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2550 if (mCalibration.havePressureScale) {
2551 mLocked.pressureScale = mCalibration.pressureScale;
2552 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
2553 mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue;
2554 }
2555 }
2556
2557 mLocked.orientedRanges.havePressure = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002558
2559 mLocked.orientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2560 mLocked.orientedRanges.pressure.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002561 mLocked.orientedRanges.pressure.min = 0;
2562 mLocked.orientedRanges.pressure.max = 1.0;
2563 mLocked.orientedRanges.pressure.flat = 0;
2564 mLocked.orientedRanges.pressure.fuzz = 0;
2565 }
2566
2567 // Size factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002568 mLocked.sizeScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002569 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002570 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
2571 if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
2572 mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue;
2573 }
2574 }
2575
2576 mLocked.orientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002577
2578 mLocked.orientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
2579 mLocked.orientedRanges.size.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002580 mLocked.orientedRanges.size.min = 0;
2581 mLocked.orientedRanges.size.max = 1.0;
2582 mLocked.orientedRanges.size.flat = 0;
2583 mLocked.orientedRanges.size.fuzz = 0;
2584 }
2585
2586 // Orientation
Jeff Brownc6d282b2010-10-14 21:42:15 -07002587 mLocked.orientationScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002588 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002589 if (mCalibration.orientationCalibration
2590 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
2591 if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) {
2592 mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue;
2593 }
2594 }
2595
Jeff Brown80fd47c2011-05-24 01:07:44 -07002596 mLocked.orientedRanges.haveOrientation = true;
2597
Jeff Brownefd32662011-03-08 15:13:06 -08002598 mLocked.orientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2599 mLocked.orientedRanges.orientation.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002600 mLocked.orientedRanges.orientation.min = - M_PI_2;
2601 mLocked.orientedRanges.orientation.max = M_PI_2;
2602 mLocked.orientedRanges.orientation.flat = 0;
2603 mLocked.orientedRanges.orientation.fuzz = 0;
2604 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002605
2606 // Distance
2607 mLocked.distanceScale = 0;
2608 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
2609 if (mCalibration.distanceCalibration
2610 == Calibration::DISTANCE_CALIBRATION_SCALED) {
2611 if (mCalibration.haveDistanceScale) {
2612 mLocked.distanceScale = mCalibration.distanceScale;
2613 } else {
2614 mLocked.distanceScale = 1.0f;
2615 }
2616 }
2617
2618 mLocked.orientedRanges.haveDistance = true;
2619
2620 mLocked.orientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
2621 mLocked.orientedRanges.distance.source = mTouchSource;
2622 mLocked.orientedRanges.distance.min =
2623 mRawAxes.distance.minValue * mLocked.distanceScale;
2624 mLocked.orientedRanges.distance.max =
2625 mRawAxes.distance.minValue * mLocked.distanceScale;
2626 mLocked.orientedRanges.distance.flat = 0;
2627 mLocked.orientedRanges.distance.fuzz =
2628 mRawAxes.distance.fuzz * mLocked.distanceScale;
2629 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002630 }
2631
2632 if (orientationChanged || sizeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002633 // Compute oriented surface dimensions, precision, scales and ranges.
2634 // Note that the maximum value reported is an inclusive maximum value so it is one
2635 // unit less than the total width or height of surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002636 switch (mLocked.surfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002637 case DISPLAY_ORIENTATION_90:
2638 case DISPLAY_ORIENTATION_270:
Jeff Brown6328cdc2010-07-29 18:18:33 -07002639 mLocked.orientedSurfaceWidth = mLocked.surfaceHeight;
2640 mLocked.orientedSurfaceHeight = mLocked.surfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002641
Jeff Brown6328cdc2010-07-29 18:18:33 -07002642 mLocked.orientedXPrecision = mLocked.yPrecision;
2643 mLocked.orientedYPrecision = mLocked.xPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002644
2645 mLocked.orientedRanges.x.min = 0;
2646 mLocked.orientedRanges.x.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue)
2647 * mLocked.yScale;
2648 mLocked.orientedRanges.x.flat = 0;
2649 mLocked.orientedRanges.x.fuzz = mLocked.yScale;
2650
2651 mLocked.orientedRanges.y.min = 0;
2652 mLocked.orientedRanges.y.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue)
2653 * mLocked.xScale;
2654 mLocked.orientedRanges.y.flat = 0;
2655 mLocked.orientedRanges.y.fuzz = mLocked.xScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002656 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002657
Jeff Brown6d0fec22010-07-23 21:28:06 -07002658 default:
Jeff Brown6328cdc2010-07-29 18:18:33 -07002659 mLocked.orientedSurfaceWidth = mLocked.surfaceWidth;
2660 mLocked.orientedSurfaceHeight = mLocked.surfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002661
Jeff Brown6328cdc2010-07-29 18:18:33 -07002662 mLocked.orientedXPrecision = mLocked.xPrecision;
2663 mLocked.orientedYPrecision = mLocked.yPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002664
2665 mLocked.orientedRanges.x.min = 0;
2666 mLocked.orientedRanges.x.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue)
2667 * mLocked.xScale;
2668 mLocked.orientedRanges.x.flat = 0;
2669 mLocked.orientedRanges.x.fuzz = mLocked.xScale;
2670
2671 mLocked.orientedRanges.y.min = 0;
2672 mLocked.orientedRanges.y.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue)
2673 * mLocked.yScale;
2674 mLocked.orientedRanges.y.flat = 0;
2675 mLocked.orientedRanges.y.fuzz = mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002676 break;
2677 }
Jeff Brownace13b12011-03-09 17:39:48 -08002678
2679 // Compute pointer gesture detection parameters.
2680 // TODO: These factors should not be hardcoded.
2681 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2682 int32_t rawWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2683 int32_t rawHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07002684 float rawDiagonal = hypotf(rawWidth, rawHeight);
2685 float displayDiagonal = hypotf(mLocked.associatedDisplayWidth,
2686 mLocked.associatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08002687
Jeff Brown2352b972011-04-12 22:39:53 -07002688 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07002689 // given area relative to the diagonal size of the display when no acceleration
2690 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08002691 // Assume that the touch pad has a square aspect ratio such that movements in
2692 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown474dcb52011-06-14 20:22:50 -07002693 mLocked.pointerGestureXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002694 * displayDiagonal / rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08002695 mLocked.pointerGestureYMovementScale = mLocked.pointerGestureXMovementScale;
2696
2697 // Scale zooms to cover a smaller range of the display than movements do.
2698 // This value determines the area around the pointer that is affected by freeform
2699 // pointer gestures.
Jeff Brown474dcb52011-06-14 20:22:50 -07002700 mLocked.pointerGestureXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002701 * displayDiagonal / rawDiagonal;
2702 mLocked.pointerGestureYZoomScale = mLocked.pointerGestureXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002703
Jeff Brown2352b972011-04-12 22:39:53 -07002704 // Max width between pointers to detect a swipe gesture is more than some fraction
2705 // of the diagonal axis of the touch pad. Touches that are wider than this are
2706 // translated into freeform gestures.
Jeff Brown214eaf42011-05-26 19:17:02 -07002707 mLocked.pointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07002708 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brown2352b972011-04-12 22:39:53 -07002709
2710 // Reset the current pointer gesture.
2711 mPointerGesture.reset();
2712
2713 // Remove any current spots.
2714 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
2715 mPointerController->clearSpots();
2716 }
Jeff Brownace13b12011-03-09 17:39:48 -08002717 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002718 }
2719
2720 return true;
2721}
2722
Jeff Brownef3d7e82010-09-30 14:33:04 -07002723void TouchInputMapper::dumpSurfaceLocked(String8& dump) {
2724 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth);
2725 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight);
2726 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07002727}
2728
Jeff Brown6328cdc2010-07-29 18:18:33 -07002729void TouchInputMapper::configureVirtualKeysLocked() {
Jeff Brown8d608662010-08-30 03:02:23 -07002730 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08002731 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002732
Jeff Brown6328cdc2010-07-29 18:18:33 -07002733 mLocked.virtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002734
Jeff Brown6328cdc2010-07-29 18:18:33 -07002735 if (virtualKeyDefinitions.size() == 0) {
2736 return;
2737 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002738
Jeff Brown6328cdc2010-07-29 18:18:33 -07002739 mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size());
2740
Jeff Brown8d608662010-08-30 03:02:23 -07002741 int32_t touchScreenLeft = mRawAxes.x.minValue;
2742 int32_t touchScreenTop = mRawAxes.y.minValue;
Jeff Brown9626b142011-03-03 02:09:54 -08002743 int32_t touchScreenWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2744 int32_t touchScreenHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002745
2746 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07002747 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07002748 virtualKeyDefinitions[i];
2749
2750 mLocked.virtualKeys.add();
2751 VirtualKey& virtualKey = mLocked.virtualKeys.editTop();
2752
2753 virtualKey.scanCode = virtualKeyDefinition.scanCode;
2754 int32_t keyCode;
2755 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08002756 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07002757 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002758 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
2759 virtualKey.scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002760 mLocked.virtualKeys.pop(); // drop the key
2761 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002762 }
2763
Jeff Brown6328cdc2010-07-29 18:18:33 -07002764 virtualKey.keyCode = keyCode;
2765 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002766
Jeff Brown6328cdc2010-07-29 18:18:33 -07002767 // convert the key definition's display coordinates into touch coordinates for a hit box
2768 int32_t halfWidth = virtualKeyDefinition.width / 2;
2769 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002770
Jeff Brown6328cdc2010-07-29 18:18:33 -07002771 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
2772 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
2773 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
2774 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
2775 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
2776 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
2777 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
2778 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07002779 }
2780}
2781
2782void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) {
2783 if (!mLocked.virtualKeys.isEmpty()) {
2784 dump.append(INDENT3 "Virtual Keys:\n");
2785
2786 for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) {
2787 const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i);
2788 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
2789 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
2790 i, virtualKey.scanCode, virtualKey.keyCode,
2791 virtualKey.hitLeft, virtualKey.hitRight,
2792 virtualKey.hitTop, virtualKey.hitBottom);
2793 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002794 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002795}
2796
Jeff Brown8d608662010-08-30 03:02:23 -07002797void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002798 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07002799 Calibration& out = mCalibration;
2800
Jeff Brownc6d282b2010-10-14 21:42:15 -07002801 // Touch Size
2802 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT;
2803 String8 touchSizeCalibrationString;
2804 if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) {
2805 if (touchSizeCalibrationString == "none") {
2806 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
2807 } else if (touchSizeCalibrationString == "geometric") {
2808 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC;
2809 } else if (touchSizeCalibrationString == "pressure") {
2810 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
2811 } else if (touchSizeCalibrationString != "default") {
2812 LOGW("Invalid value for touch.touchSize.calibration: '%s'",
2813 touchSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002814 }
2815 }
2816
Jeff Brownc6d282b2010-10-14 21:42:15 -07002817 // Tool Size
2818 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT;
2819 String8 toolSizeCalibrationString;
2820 if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) {
2821 if (toolSizeCalibrationString == "none") {
2822 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
2823 } else if (toolSizeCalibrationString == "geometric") {
2824 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC;
2825 } else if (toolSizeCalibrationString == "linear") {
2826 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
2827 } else if (toolSizeCalibrationString == "area") {
2828 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA;
2829 } else if (toolSizeCalibrationString != "default") {
2830 LOGW("Invalid value for touch.toolSize.calibration: '%s'",
2831 toolSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002832 }
2833 }
2834
Jeff Brownc6d282b2010-10-14 21:42:15 -07002835 out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"),
2836 out.toolSizeLinearScale);
2837 out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"),
2838 out.toolSizeLinearBias);
2839 out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"),
2840 out.toolSizeAreaScale);
2841 out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"),
2842 out.toolSizeAreaBias);
2843 out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"),
2844 out.toolSizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07002845
2846 // Pressure
2847 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
2848 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002849 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002850 if (pressureCalibrationString == "none") {
2851 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2852 } else if (pressureCalibrationString == "physical") {
2853 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
2854 } else if (pressureCalibrationString == "amplitude") {
2855 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2856 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002857 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002858 pressureCalibrationString.string());
2859 }
2860 }
2861
2862 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
2863 String8 pressureSourceString;
2864 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
2865 if (pressureSourceString == "pressure") {
2866 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2867 } else if (pressureSourceString == "touch") {
2868 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2869 } else if (pressureSourceString != "default") {
2870 LOGW("Invalid value for touch.pressure.source: '%s'",
2871 pressureSourceString.string());
2872 }
2873 }
2874
2875 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
2876 out.pressureScale);
2877
2878 // Size
2879 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
2880 String8 sizeCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002881 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002882 if (sizeCalibrationString == "none") {
2883 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2884 } else if (sizeCalibrationString == "normalized") {
2885 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
2886 } else if (sizeCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002887 LOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002888 sizeCalibrationString.string());
2889 }
2890 }
2891
2892 // Orientation
2893 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
2894 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002895 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002896 if (orientationCalibrationString == "none") {
2897 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2898 } else if (orientationCalibrationString == "interpolated") {
2899 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002900 } else if (orientationCalibrationString == "vector") {
2901 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002902 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002903 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002904 orientationCalibrationString.string());
2905 }
2906 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002907
2908 // Distance
2909 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
2910 String8 distanceCalibrationString;
2911 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
2912 if (distanceCalibrationString == "none") {
2913 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
2914 } else if (distanceCalibrationString == "scaled") {
2915 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
2916 } else if (distanceCalibrationString != "default") {
2917 LOGW("Invalid value for touch.distance.calibration: '%s'",
2918 distanceCalibrationString.string());
2919 }
2920 }
2921
2922 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
2923 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002924}
2925
2926void TouchInputMapper::resolveCalibration() {
2927 // Pressure
2928 switch (mCalibration.pressureSource) {
2929 case Calibration::PRESSURE_SOURCE_DEFAULT:
2930 if (mRawAxes.pressure.valid) {
2931 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2932 } else if (mRawAxes.touchMajor.valid) {
2933 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2934 }
2935 break;
2936
2937 case Calibration::PRESSURE_SOURCE_PRESSURE:
2938 if (! mRawAxes.pressure.valid) {
2939 LOGW("Calibration property touch.pressure.source is 'pressure' but "
2940 "the pressure axis is not available.");
2941 }
2942 break;
2943
2944 case Calibration::PRESSURE_SOURCE_TOUCH:
2945 if (! mRawAxes.touchMajor.valid) {
2946 LOGW("Calibration property touch.pressure.source is 'touch' but "
2947 "the touchMajor axis is not available.");
2948 }
2949 break;
2950
2951 default:
2952 break;
2953 }
2954
2955 switch (mCalibration.pressureCalibration) {
2956 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
2957 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
2958 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2959 } else {
2960 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2961 }
2962 break;
2963
2964 default:
2965 break;
2966 }
2967
Jeff Brownc6d282b2010-10-14 21:42:15 -07002968 // Tool Size
2969 switch (mCalibration.toolSizeCalibration) {
2970 case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07002971 if (mRawAxes.toolMajor.valid) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002972 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
Jeff Brown8d608662010-08-30 03:02:23 -07002973 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002974 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002975 }
2976 break;
2977
2978 default:
2979 break;
2980 }
2981
Jeff Brownc6d282b2010-10-14 21:42:15 -07002982 // Touch Size
2983 switch (mCalibration.touchSizeCalibration) {
2984 case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07002985 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
Jeff Brownc6d282b2010-10-14 21:42:15 -07002986 && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
2987 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
Jeff Brown8d608662010-08-30 03:02:23 -07002988 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002989 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002990 }
2991 break;
2992
2993 default:
2994 break;
2995 }
2996
2997 // Size
2998 switch (mCalibration.sizeCalibration) {
2999 case Calibration::SIZE_CALIBRATION_DEFAULT:
3000 if (mRawAxes.toolMajor.valid) {
3001 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
3002 } else {
3003 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3004 }
3005 break;
3006
3007 default:
3008 break;
3009 }
3010
3011 // Orientation
3012 switch (mCalibration.orientationCalibration) {
3013 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
3014 if (mRawAxes.orientation.valid) {
3015 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
3016 } else {
3017 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3018 }
3019 break;
3020
3021 default:
3022 break;
3023 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003024
3025 // Distance
3026 switch (mCalibration.distanceCalibration) {
3027 case Calibration::DISTANCE_CALIBRATION_DEFAULT:
3028 if (mRawAxes.distance.valid) {
3029 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3030 } else {
3031 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3032 }
3033 break;
3034
3035 default:
3036 break;
3037 }
Jeff Brown8d608662010-08-30 03:02:23 -07003038}
3039
Jeff Brownef3d7e82010-09-30 14:33:04 -07003040void TouchInputMapper::dumpCalibration(String8& dump) {
3041 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003042
Jeff Brownc6d282b2010-10-14 21:42:15 -07003043 // Touch Size
3044 switch (mCalibration.touchSizeCalibration) {
3045 case Calibration::TOUCH_SIZE_CALIBRATION_NONE:
3046 dump.append(INDENT4 "touch.touchSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003047 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003048 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
3049 dump.append(INDENT4 "touch.touchSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003050 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003051 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
3052 dump.append(INDENT4 "touch.touchSize.calibration: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003053 break;
3054 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003055 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003056 }
3057
Jeff Brownc6d282b2010-10-14 21:42:15 -07003058 // Tool Size
3059 switch (mCalibration.toolSizeCalibration) {
3060 case Calibration::TOOL_SIZE_CALIBRATION_NONE:
3061 dump.append(INDENT4 "touch.toolSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003062 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003063 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
3064 dump.append(INDENT4 "touch.toolSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003065 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003066 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
3067 dump.append(INDENT4 "touch.toolSize.calibration: linear\n");
3068 break;
3069 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
3070 dump.append(INDENT4 "touch.toolSize.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003071 break;
3072 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003073 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003074 }
3075
Jeff Brownc6d282b2010-10-14 21:42:15 -07003076 if (mCalibration.haveToolSizeLinearScale) {
3077 dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n",
3078 mCalibration.toolSizeLinearScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003079 }
3080
Jeff Brownc6d282b2010-10-14 21:42:15 -07003081 if (mCalibration.haveToolSizeLinearBias) {
3082 dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n",
3083 mCalibration.toolSizeLinearBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003084 }
3085
Jeff Brownc6d282b2010-10-14 21:42:15 -07003086 if (mCalibration.haveToolSizeAreaScale) {
3087 dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n",
3088 mCalibration.toolSizeAreaScale);
3089 }
3090
3091 if (mCalibration.haveToolSizeAreaBias) {
3092 dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n",
3093 mCalibration.toolSizeAreaBias);
3094 }
3095
3096 if (mCalibration.haveToolSizeIsSummed) {
Jeff Brown1f245102010-11-18 20:53:46 -08003097 dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %s\n",
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003098 toString(mCalibration.toolSizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003099 }
3100
3101 // Pressure
3102 switch (mCalibration.pressureCalibration) {
3103 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003104 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003105 break;
3106 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003107 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003108 break;
3109 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003110 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003111 break;
3112 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003113 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003114 }
3115
3116 switch (mCalibration.pressureSource) {
3117 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003118 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003119 break;
3120 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003121 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003122 break;
3123 case Calibration::PRESSURE_SOURCE_DEFAULT:
3124 break;
3125 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003126 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003127 }
3128
3129 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003130 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3131 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003132 }
3133
3134 // Size
3135 switch (mCalibration.sizeCalibration) {
3136 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003137 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003138 break;
3139 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003140 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003141 break;
3142 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003143 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003144 }
3145
3146 // Orientation
3147 switch (mCalibration.orientationCalibration) {
3148 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003149 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003150 break;
3151 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003152 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003153 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003154 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3155 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3156 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003157 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003158 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003159 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003160
3161 // Distance
3162 switch (mCalibration.distanceCalibration) {
3163 case Calibration::DISTANCE_CALIBRATION_NONE:
3164 dump.append(INDENT4 "touch.distance.calibration: none\n");
3165 break;
3166 case Calibration::DISTANCE_CALIBRATION_SCALED:
3167 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3168 break;
3169 default:
3170 LOG_ASSERT(false);
3171 }
3172
3173 if (mCalibration.haveDistanceScale) {
3174 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3175 mCalibration.distanceScale);
3176 }
Jeff Brown8d608662010-08-30 03:02:23 -07003177}
3178
Jeff Brown6d0fec22010-07-23 21:28:06 -07003179void TouchInputMapper::reset() {
3180 // Synthesize touch up event if touch is currently down.
3181 // This will also take care of finishing virtual key processing if needed.
3182 if (mLastTouch.pointerCount != 0) {
3183 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
3184 mCurrentTouch.clear();
3185 syncTouch(when, true);
3186 }
3187
Jeff Brown6328cdc2010-07-29 18:18:33 -07003188 { // acquire lock
3189 AutoMutex _l(mLock);
3190 initializeLocked();
Jeff Brown2352b972011-04-12 22:39:53 -07003191
3192 if (mPointerController != NULL
3193 && mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003194 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
Jeff Brown2352b972011-04-12 22:39:53 -07003195 mPointerController->clearSpots();
3196 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003197 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07003198
Jeff Brown6328cdc2010-07-29 18:18:33 -07003199 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003200}
3201
3202void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Brownaa3855d2011-03-17 01:34:19 -07003203#if DEBUG_RAW_EVENTS
3204 if (!havePointerIds) {
3205 LOGD("syncTouch: pointerCount=%d, no pointer ids", mCurrentTouch.pointerCount);
3206 } else {
3207 LOGD("syncTouch: pointerCount=%d, up=0x%08x, down=0x%08x, move=0x%08x, "
3208 "last=0x%08x, current=0x%08x", mCurrentTouch.pointerCount,
3209 mLastTouch.idBits.value & ~mCurrentTouch.idBits.value,
3210 mCurrentTouch.idBits.value & ~mLastTouch.idBits.value,
3211 mLastTouch.idBits.value & mCurrentTouch.idBits.value,
3212 mLastTouch.idBits.value, mCurrentTouch.idBits.value);
3213 }
3214#endif
3215
Jeff Brown6328cdc2010-07-29 18:18:33 -07003216 // Preprocess pointer data.
Jeff Brownaa3855d2011-03-17 01:34:19 -07003217 if (!havePointerIds) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003218 calculatePointerIds();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003219 }
3220
Jeff Brown49754db2011-07-01 17:37:58 -07003221 // Handle initial down events.
Jeff Brown56194eb2011-03-02 19:23:13 -08003222 uint32_t policyFlags = 0;
Jeff Brown05dc66a2011-03-02 14:41:58 -08003223 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) {
Jeff Brownefd32662011-03-08 15:13:06 -08003224 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
3225 // If this is a touch screen, hide the pointer on an initial down.
3226 getContext()->fadePointer();
3227 }
Jeff Brown56194eb2011-03-02 19:23:13 -08003228
3229 // Initial downs on external touch devices should wake the device.
3230 // We don't do this for internal touch screens to prevent them from waking
3231 // up in your pocket.
3232 // TODO: Use the input device configuration to control this behavior more finely.
3233 if (getDevice()->isExternal()) {
3234 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3235 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08003236 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003237
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003238 // Synthesize key down from buttons if needed.
3239 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mTouchSource,
3240 policyFlags, mLastTouch.buttonState, mCurrentTouch.buttonState);
3241
3242 // Send motion events.
Jeff Brown79ac9692011-04-19 21:20:10 -07003243 TouchResult touchResult;
3244 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount == 0
3245 && mLastTouch.buttonState == mCurrentTouch.buttonState) {
3246 // Drop spurious syncs.
3247 touchResult = DROP_STROKE;
3248 } else {
3249 // Process touches and virtual keys.
3250 touchResult = consumeOffScreenTouches(when, policyFlags);
3251 if (touchResult == DISPATCH_TOUCH) {
3252 suppressSwipeOntoVirtualKeys(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07003253 if (mPointerController != NULL && mConfig.pointerGesturesEnabled) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003254 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
3255 }
3256 dispatchTouches(when, policyFlags);
Jeff Brownace13b12011-03-09 17:39:48 -08003257 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003258 }
3259
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003260 // Synthesize key up from buttons if needed.
3261 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mTouchSource,
3262 policyFlags, mLastTouch.buttonState, mCurrentTouch.buttonState);
3263
Jeff Brown6328cdc2010-07-29 18:18:33 -07003264 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownace13b12011-03-09 17:39:48 -08003265 // Keep the button state so we can track edge-triggered button state changes.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003266 if (touchResult == DROP_STROKE) {
3267 mLastTouch.clear();
Jeff Browna4d1bc52011-07-01 19:23:40 -07003268 mLastTouch.buttonState = mCurrentTouch.buttonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003269 } else {
Jeff Browna4d1bc52011-07-01 19:23:40 -07003270 mLastTouch.copyFrom(mCurrentTouch);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003271 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003272}
3273
Jeff Brown79ac9692011-04-19 21:20:10 -07003274void TouchInputMapper::timeoutExpired(nsecs_t when) {
3275 if (mPointerController != NULL) {
3276 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3277 }
3278}
3279
Jeff Brown6d0fec22010-07-23 21:28:06 -07003280TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches(
3281 nsecs_t when, uint32_t policyFlags) {
3282 int32_t keyEventAction, keyEventFlags;
3283 int32_t keyCode, scanCode, downTime;
3284 TouchResult touchResult;
Jeff Brown349703e2010-06-22 01:27:15 -07003285
Jeff Brown6328cdc2010-07-29 18:18:33 -07003286 { // acquire lock
3287 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003288
Jeff Brown6328cdc2010-07-29 18:18:33 -07003289 // Update surface size and orientation, including virtual key positions.
3290 if (! configureSurfaceLocked()) {
3291 return DROP_STROKE;
3292 }
3293
3294 // Check for virtual key press.
3295 if (mLocked.currentVirtualKey.down) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003296 if (mCurrentTouch.pointerCount == 0) {
3297 // Pointer went up while virtual key was down.
Jeff Brown6328cdc2010-07-29 18:18:33 -07003298 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003299#if DEBUG_VIRTUAL_KEYS
3300 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07003301 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003302#endif
3303 keyEventAction = AKEY_EVENT_ACTION_UP;
3304 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
3305 touchResult = SKIP_TOUCH;
3306 goto DispatchVirtualKey;
3307 }
3308
3309 if (mCurrentTouch.pointerCount == 1) {
3310 int32_t x = mCurrentTouch.pointers[0].x;
3311 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003312 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
3313 if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003314 // Pointer is still within the space of the virtual key.
3315 return SKIP_TOUCH;
3316 }
3317 }
3318
3319 // Pointer left virtual key area or another pointer also went down.
3320 // Send key cancellation and drop the stroke so subsequent motions will be
3321 // considered fresh downs. This is useful when the user swipes away from the
3322 // virtual key area into the main display surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07003323 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003324#if DEBUG_VIRTUAL_KEYS
3325 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07003326 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003327#endif
3328 keyEventAction = AKEY_EVENT_ACTION_UP;
3329 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3330 | AKEY_EVENT_FLAG_CANCELED;
Jeff Brownc3db8582010-10-20 15:33:38 -07003331
3332 // Check whether the pointer moved inside the display area where we should
3333 // start a new stroke.
3334 int32_t x = mCurrentTouch.pointers[0].x;
3335 int32_t y = mCurrentTouch.pointers[0].y;
3336 if (isPointInsideSurfaceLocked(x, y)) {
3337 mLastTouch.clear();
3338 touchResult = DISPATCH_TOUCH;
3339 } else {
3340 touchResult = DROP_STROKE;
3341 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003342 } else {
3343 if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) {
3344 // Pointer just went down. Handle off-screen touches, if needed.
3345 int32_t x = mCurrentTouch.pointers[0].x;
3346 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003347 if (! isPointInsideSurfaceLocked(x, y)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003348 // If exactly one pointer went down, check for virtual key hit.
3349 // Otherwise we will drop the entire stroke.
3350 if (mCurrentTouch.pointerCount == 1) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003351 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003352 if (virtualKey) {
Jeff Brownfe508922011-01-18 15:10:10 -08003353 if (mContext->shouldDropVirtualKey(when, getDevice(),
3354 virtualKey->keyCode, virtualKey->scanCode)) {
3355 return DROP_STROKE;
3356 }
3357
Jeff Brown6328cdc2010-07-29 18:18:33 -07003358 mLocked.currentVirtualKey.down = true;
3359 mLocked.currentVirtualKey.downTime = when;
3360 mLocked.currentVirtualKey.keyCode = virtualKey->keyCode;
3361 mLocked.currentVirtualKey.scanCode = virtualKey->scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003362#if DEBUG_VIRTUAL_KEYS
3363 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07003364 mLocked.currentVirtualKey.keyCode,
3365 mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003366#endif
3367 keyEventAction = AKEY_EVENT_ACTION_DOWN;
3368 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM
3369 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
3370 touchResult = SKIP_TOUCH;
3371 goto DispatchVirtualKey;
3372 }
3373 }
3374 return DROP_STROKE;
3375 }
3376 }
3377 return DISPATCH_TOUCH;
3378 }
3379
3380 DispatchVirtualKey:
3381 // Collect remaining state needed to dispatch virtual key.
Jeff Brown6328cdc2010-07-29 18:18:33 -07003382 keyCode = mLocked.currentVirtualKey.keyCode;
3383 scanCode = mLocked.currentVirtualKey.scanCode;
3384 downTime = mLocked.currentVirtualKey.downTime;
3385 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07003386
3387 // Dispatch virtual key.
3388 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown0eaf3932010-10-01 14:55:30 -07003389 policyFlags |= POLICY_FLAG_VIRTUAL;
Jeff Brownb6997262010-10-08 22:31:17 -07003390 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3391 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3392 return touchResult;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003393}
3394
Jeff Brownefd32662011-03-08 15:13:06 -08003395void TouchInputMapper::suppressSwipeOntoVirtualKeys(nsecs_t when) {
Jeff Brownfe508922011-01-18 15:10:10 -08003396 // Disable all virtual key touches that happen within a short time interval of the
3397 // most recent touch. The idea is to filter out stray virtual key presses when
3398 // interacting with the touch screen.
3399 //
3400 // Problems we're trying to solve:
3401 //
3402 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3403 // virtual key area that is implemented by a separate touch panel and accidentally
3404 // triggers a virtual key.
3405 //
3406 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3407 // area and accidentally triggers a virtual key. This often happens when virtual keys
3408 // are layed out below the screen near to where the on screen keyboard's space bar
3409 // is displayed.
Jeff Brown474dcb52011-06-14 20:22:50 -07003410 if (mConfig.virtualKeyQuietTime > 0 && mCurrentTouch.pointerCount != 0) {
3411 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003412 }
3413}
3414
Jeff Brown6d0fec22010-07-23 21:28:06 -07003415void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
3416 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
3417 uint32_t lastPointerCount = mLastTouch.pointerCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003418 if (currentPointerCount == 0 && lastPointerCount == 0) {
3419 return; // nothing to do!
3420 }
3421
Jeff Brownace13b12011-03-09 17:39:48 -08003422 // Update current touch coordinates.
Jeff Brownace13b12011-03-09 17:39:48 -08003423 float xPrecision, yPrecision;
Jeff Browna6111372011-07-14 21:48:23 -07003424 prepareTouches(&xPrecision, &yPrecision);
Jeff Brownace13b12011-03-09 17:39:48 -08003425
3426 // Dispatch motions.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003427 BitSet32 currentIdBits = mCurrentTouch.idBits;
3428 BitSet32 lastIdBits = mLastTouch.idBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003429 int32_t metaState = getContext()->getGlobalMetaState();
3430 int32_t buttonState = mCurrentTouch.buttonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003431
3432 if (currentIdBits == lastIdBits) {
3433 // No pointer id changes so this is a move event.
3434 // The dispatcher takes care of batching moves so we don't have to deal with that here.
Jeff Brownace13b12011-03-09 17:39:48 -08003435 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003436 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3437 AMOTION_EVENT_EDGE_FLAG_NONE,
3438 mCurrentTouchProperties, mCurrentTouchCoords,
3439 mCurrentTouch.idToIndex, currentIdBits, -1,
Jeff Brownace13b12011-03-09 17:39:48 -08003440 xPrecision, yPrecision, mDownTime);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003441 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003442 // There may be pointers going up and pointers going down and pointers moving
3443 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003444 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3445 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003446 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003447 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003448
Jeff Brownace13b12011-03-09 17:39:48 -08003449 // Update last coordinates of pointers that have moved so that we observe the new
3450 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003451 bool moveNeeded = updateMovedPointers(
3452 mCurrentTouchProperties, mCurrentTouchCoords, mCurrentTouch.idToIndex,
3453 mLastTouchProperties, mLastTouchCoords, mLastTouch.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003454 moveIdBits);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003455 if (buttonState != mLastTouch.buttonState) {
3456 moveNeeded = true;
3457 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003458
Jeff Brownace13b12011-03-09 17:39:48 -08003459 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003460 while (!upIdBits.isEmpty()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003461 uint32_t upId = upIdBits.firstMarkedBit();
3462 upIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003463
Jeff Brownace13b12011-03-09 17:39:48 -08003464 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003465 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
3466 mLastTouchProperties, mLastTouchCoords,
3467 mLastTouch.idToIndex, dispatchedIdBits, upId,
Jeff Brownace13b12011-03-09 17:39:48 -08003468 xPrecision, yPrecision, mDownTime);
3469 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003470 }
3471
Jeff Brownc3db8582010-10-20 15:33:38 -07003472 // Dispatch move events if any of the remaining pointers moved from their old locations.
3473 // Although applications receive new locations as part of individual pointer up
3474 // events, they do not generally handle them except when presented in a move event.
3475 if (moveNeeded) {
Jeff Brownb6110c22011-04-01 16:15:13 -07003476 LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003477 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003478 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
3479 mCurrentTouchProperties, mCurrentTouchCoords,
3480 mCurrentTouch.idToIndex, dispatchedIdBits, -1,
Jeff Brownace13b12011-03-09 17:39:48 -08003481 xPrecision, yPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003482 }
3483
3484 // Dispatch pointer down events using the new pointer locations.
3485 while (!downIdBits.isEmpty()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003486 uint32_t downId = downIdBits.firstMarkedBit();
3487 downIdBits.clearBit(downId);
Jeff Brownace13b12011-03-09 17:39:48 -08003488 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003489
Jeff Brownace13b12011-03-09 17:39:48 -08003490 if (dispatchedIdBits.count() == 1) {
3491 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003492 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003493 }
3494
Jeff Brownace13b12011-03-09 17:39:48 -08003495 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Browna6111372011-07-14 21:48:23 -07003496 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003497 mCurrentTouchProperties, mCurrentTouchCoords,
3498 mCurrentTouch.idToIndex, dispatchedIdBits, downId,
Jeff Brownace13b12011-03-09 17:39:48 -08003499 xPrecision, yPrecision, mDownTime);
3500 }
3501 }
3502
3503 // Update state for next time.
3504 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003505 mLastTouchProperties[i].copyFrom(mCurrentTouchProperties[i]);
Jeff Brownace13b12011-03-09 17:39:48 -08003506 mLastTouchCoords[i].copyFrom(mCurrentTouchCoords[i]);
3507 }
3508}
3509
Jeff Browna6111372011-07-14 21:48:23 -07003510void TouchInputMapper::prepareTouches(float* outXPrecision, float* outYPrecision) {
Jeff Brownace13b12011-03-09 17:39:48 -08003511 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
3512 uint32_t lastPointerCount = mLastTouch.pointerCount;
3513
3514 AutoMutex _l(mLock);
3515
3516 // Walk through the the active pointers and map touch screen coordinates (TouchData) into
3517 // display or surface coordinates (PointerCoords) and adjust for display orientation.
3518 for (uint32_t i = 0; i < currentPointerCount; i++) {
3519 const PointerData& in = mCurrentTouch.pointers[i];
3520
3521 // ToolMajor and ToolMinor
3522 float toolMajor, toolMinor;
3523 switch (mCalibration.toolSizeCalibration) {
3524 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
3525 toolMajor = in.toolMajor * mLocked.geometricScale;
3526 if (mRawAxes.toolMinor.valid) {
3527 toolMinor = in.toolMinor * mLocked.geometricScale;
3528 } else {
3529 toolMinor = toolMajor;
3530 }
3531 break;
3532 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
3533 toolMajor = in.toolMajor != 0
3534 ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias
3535 : 0;
3536 if (mRawAxes.toolMinor.valid) {
3537 toolMinor = in.toolMinor != 0
3538 ? in.toolMinor * mLocked.toolSizeLinearScale
3539 + mLocked.toolSizeLinearBias
3540 : 0;
3541 } else {
3542 toolMinor = toolMajor;
3543 }
3544 break;
3545 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
3546 if (in.toolMajor != 0) {
3547 float diameter = sqrtf(in.toolMajor
3548 * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias);
3549 toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias;
3550 } else {
3551 toolMajor = 0;
3552 }
3553 toolMinor = toolMajor;
3554 break;
3555 default:
3556 toolMajor = 0;
3557 toolMinor = 0;
3558 break;
3559 }
3560
3561 if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) {
3562 toolMajor /= currentPointerCount;
3563 toolMinor /= currentPointerCount;
3564 }
3565
3566 // Pressure
3567 float rawPressure;
3568 switch (mCalibration.pressureSource) {
3569 case Calibration::PRESSURE_SOURCE_PRESSURE:
3570 rawPressure = in.pressure;
3571 break;
3572 case Calibration::PRESSURE_SOURCE_TOUCH:
3573 rawPressure = in.touchMajor;
3574 break;
3575 default:
3576 rawPressure = 0;
3577 }
3578
3579 float pressure;
3580 switch (mCalibration.pressureCalibration) {
3581 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3582 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
3583 pressure = rawPressure * mLocked.pressureScale;
3584 break;
3585 default:
3586 pressure = 1;
3587 break;
3588 }
3589
3590 // TouchMajor and TouchMinor
3591 float touchMajor, touchMinor;
3592 switch (mCalibration.touchSizeCalibration) {
3593 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
3594 touchMajor = in.touchMajor * mLocked.geometricScale;
3595 if (mRawAxes.touchMinor.valid) {
3596 touchMinor = in.touchMinor * mLocked.geometricScale;
3597 } else {
3598 touchMinor = touchMajor;
3599 }
3600 break;
3601 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
3602 touchMajor = toolMajor * pressure;
3603 touchMinor = toolMinor * pressure;
3604 break;
3605 default:
3606 touchMajor = 0;
3607 touchMinor = 0;
3608 break;
3609 }
3610
3611 if (touchMajor > toolMajor) {
3612 touchMajor = toolMajor;
3613 }
3614 if (touchMinor > toolMinor) {
3615 touchMinor = toolMinor;
3616 }
3617
3618 // Size
3619 float size;
3620 switch (mCalibration.sizeCalibration) {
3621 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
3622 float rawSize = mRawAxes.toolMinor.valid
3623 ? avg(in.toolMajor, in.toolMinor)
3624 : in.toolMajor;
3625 size = rawSize * mLocked.sizeScale;
3626 break;
3627 }
3628 default:
3629 size = 0;
3630 break;
3631 }
3632
3633 // Orientation
3634 float orientation;
3635 switch (mCalibration.orientationCalibration) {
3636 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
3637 orientation = in.orientation * mLocked.orientationScale;
3638 break;
3639 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3640 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3641 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3642 if (c1 != 0 || c2 != 0) {
3643 orientation = atan2f(c1, c2) * 0.5f;
Jeff Brown2352b972011-04-12 22:39:53 -07003644 float scale = 1.0f + hypotf(c1, c2) / 16.0f;
Jeff Brownace13b12011-03-09 17:39:48 -08003645 touchMajor *= scale;
3646 touchMinor /= scale;
3647 toolMajor *= scale;
3648 toolMinor /= scale;
3649 } else {
3650 orientation = 0;
3651 }
3652 break;
3653 }
3654 default:
3655 orientation = 0;
3656 }
3657
Jeff Brown80fd47c2011-05-24 01:07:44 -07003658 // Distance
3659 float distance;
3660 switch (mCalibration.distanceCalibration) {
3661 case Calibration::DISTANCE_CALIBRATION_SCALED:
3662 distance = in.distance * mLocked.distanceScale;
3663 break;
3664 default:
3665 distance = 0;
3666 }
3667
Jeff Brownace13b12011-03-09 17:39:48 -08003668 // X and Y
3669 // Adjust coords for surface orientation.
3670 float x, y;
3671 switch (mLocked.surfaceOrientation) {
3672 case DISPLAY_ORIENTATION_90:
3673 x = float(in.y - mRawAxes.y.minValue) * mLocked.yScale;
3674 y = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale;
3675 orientation -= M_PI_2;
3676 if (orientation < - M_PI_2) {
3677 orientation += M_PI;
3678 }
3679 break;
3680 case DISPLAY_ORIENTATION_180:
3681 x = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale;
3682 y = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale;
3683 break;
3684 case DISPLAY_ORIENTATION_270:
3685 x = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale;
3686 y = float(in.x - mRawAxes.x.minValue) * mLocked.xScale;
3687 orientation += M_PI_2;
3688 if (orientation > M_PI_2) {
3689 orientation -= M_PI;
3690 }
3691 break;
3692 default:
3693 x = float(in.x - mRawAxes.x.minValue) * mLocked.xScale;
3694 y = float(in.y - mRawAxes.y.minValue) * mLocked.yScale;
3695 break;
3696 }
3697
3698 // Write output coords.
3699 PointerCoords& out = mCurrentTouchCoords[i];
3700 out.clear();
3701 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3702 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3703 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3704 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3705 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3706 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3707 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3708 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3709 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown80fd47c2011-05-24 01:07:44 -07003710 if (distance != 0) {
3711 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
3712 }
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003713
3714 // Write output properties.
3715 PointerProperties& properties = mCurrentTouchProperties[i];
3716 properties.clear();
3717 properties.id = mCurrentTouch.pointers[i].id;
Jeff Brown49754db2011-07-01 17:37:58 -07003718 properties.toolType = mCurrentTouch.pointers[i].toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08003719 }
3720
Jeff Brownace13b12011-03-09 17:39:48 -08003721 *outXPrecision = mLocked.orientedXPrecision;
3722 *outYPrecision = mLocked.orientedYPrecision;
3723}
3724
Jeff Brown79ac9692011-04-19 21:20:10 -07003725void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3726 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003727 // Update current gesture coordinates.
3728 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07003729 bool sendEvents = preparePointerGestures(when,
3730 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3731 if (!sendEvents) {
3732 return;
3733 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003734 if (finishPreviousGesture) {
3735 cancelPreviousGesture = false;
3736 }
Jeff Brownace13b12011-03-09 17:39:48 -08003737
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003738 // Update the pointer presentation and spots.
3739 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3740 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3741 if (finishPreviousGesture || cancelPreviousGesture) {
3742 mPointerController->clearSpots();
3743 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07003744 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
3745 mPointerGesture.currentGestureIdToIndex,
3746 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003747 } else {
3748 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
3749 }
Jeff Brown214eaf42011-05-26 19:17:02 -07003750
Jeff Brown538881e2011-05-25 18:23:38 -07003751 // Show or hide the pointer if needed.
3752 switch (mPointerGesture.currentGestureMode) {
3753 case PointerGesture::NEUTRAL:
3754 case PointerGesture::QUIET:
3755 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3756 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3757 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
3758 // Remind the user of where the pointer is after finishing a gesture with spots.
3759 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
3760 }
3761 break;
3762 case PointerGesture::TAP:
3763 case PointerGesture::TAP_DRAG:
3764 case PointerGesture::BUTTON_CLICK_OR_DRAG:
3765 case PointerGesture::HOVER:
3766 case PointerGesture::PRESS:
3767 // Unfade the pointer when the current gesture manipulates the
3768 // area directly under the pointer.
3769 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3770 break;
3771 case PointerGesture::SWIPE:
3772 case PointerGesture::FREEFORM:
3773 // Fade the pointer when the current gesture manipulates a different
3774 // area and there are spots to guide the user experience.
3775 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3776 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3777 } else {
3778 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3779 }
3780 break;
Jeff Brown2352b972011-04-12 22:39:53 -07003781 }
3782
Jeff Brownace13b12011-03-09 17:39:48 -08003783 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003784 int32_t metaState = getContext()->getGlobalMetaState();
3785 int32_t buttonState = mCurrentTouch.buttonState;
Jeff Brownace13b12011-03-09 17:39:48 -08003786
3787 // Update last coordinates of pointers that have moved so that we observe the new
3788 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07003789 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
3790 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
3791 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07003792 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08003793 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
3794 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
3795 bool moveNeeded = false;
3796 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07003797 && !mPointerGesture.lastGestureIdBits.isEmpty()
3798 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08003799 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
3800 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003801 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003802 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003803 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003804 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3805 movedGestureIdBits);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003806 if (buttonState != mLastTouch.buttonState) {
3807 moveNeeded = true;
3808 }
Jeff Brownace13b12011-03-09 17:39:48 -08003809 }
3810
3811 // Send motion events for all pointers that went up or were canceled.
3812 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
3813 if (!dispatchedGestureIdBits.isEmpty()) {
3814 if (cancelPreviousGesture) {
3815 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003816 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
3817 AMOTION_EVENT_EDGE_FLAG_NONE,
3818 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003819 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3820 dispatchedGestureIdBits, -1,
3821 0, 0, mPointerGesture.downTime);
3822
3823 dispatchedGestureIdBits.clear();
3824 } else {
3825 BitSet32 upGestureIdBits;
3826 if (finishPreviousGesture) {
3827 upGestureIdBits = dispatchedGestureIdBits;
3828 } else {
3829 upGestureIdBits.value = dispatchedGestureIdBits.value
3830 & ~mPointerGesture.currentGestureIdBits.value;
3831 }
3832 while (!upGestureIdBits.isEmpty()) {
3833 uint32_t id = upGestureIdBits.firstMarkedBit();
3834 upGestureIdBits.clearBit(id);
3835
3836 dispatchMotion(when, policyFlags, mPointerSource,
3837 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003838 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3839 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003840 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3841 dispatchedGestureIdBits, id,
3842 0, 0, mPointerGesture.downTime);
3843
3844 dispatchedGestureIdBits.clearBit(id);
3845 }
3846 }
3847 }
3848
3849 // Send motion events for all pointers that moved.
3850 if (moveNeeded) {
3851 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003852 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3853 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003854 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3855 dispatchedGestureIdBits, -1,
3856 0, 0, mPointerGesture.downTime);
3857 }
3858
3859 // Send motion events for all pointers that went down.
3860 if (down) {
3861 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
3862 & ~dispatchedGestureIdBits.value);
3863 while (!downGestureIdBits.isEmpty()) {
3864 uint32_t id = downGestureIdBits.firstMarkedBit();
3865 downGestureIdBits.clearBit(id);
3866 dispatchedGestureIdBits.markBit(id);
3867
Jeff Brownace13b12011-03-09 17:39:48 -08003868 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08003869 mPointerGesture.downTime = when;
3870 }
3871
3872 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Browna6111372011-07-14 21:48:23 -07003873 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003874 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003875 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3876 dispatchedGestureIdBits, id,
3877 0, 0, mPointerGesture.downTime);
3878 }
3879 }
3880
Jeff Brownace13b12011-03-09 17:39:48 -08003881 // Send motion events for hover.
3882 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
3883 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003884 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
3885 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3886 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003887 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3888 mPointerGesture.currentGestureIdBits, -1,
3889 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07003890 } else if (dispatchedGestureIdBits.isEmpty()
3891 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
3892 // Synthesize a hover move event after all pointers go up to indicate that
3893 // the pointer is hovering again even if the user is not currently touching
3894 // the touch pad. This ensures that a view will receive a fresh hover enter
3895 // event after a tap.
3896 float x, y;
3897 mPointerController->getPosition(&x, &y);
3898
3899 PointerProperties pointerProperties;
3900 pointerProperties.clear();
3901 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07003902 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07003903
3904 PointerCoords pointerCoords;
3905 pointerCoords.clear();
3906 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3907 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3908
3909 getDispatcher()->notifyMotion(when, getDeviceId(), mPointerSource, policyFlags,
3910 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
3911 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3912 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003913 }
3914
3915 // Update state.
3916 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
3917 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08003918 mPointerGesture.lastGestureIdBits.clear();
3919 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08003920 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
3921 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
3922 uint32_t id = idBits.firstMarkedBit();
3923 idBits.clearBit(id);
3924 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003925 mPointerGesture.lastGestureProperties[index].copyFrom(
3926 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08003927 mPointerGesture.lastGestureCoords[index].copyFrom(
3928 mPointerGesture.currentGestureCoords[index]);
3929 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003930 }
3931 }
3932}
3933
Jeff Brown79ac9692011-04-19 21:20:10 -07003934bool TouchInputMapper::preparePointerGestures(nsecs_t when,
3935 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003936 *outCancelPreviousGesture = false;
3937 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003938
Jeff Brownace13b12011-03-09 17:39:48 -08003939 AutoMutex _l(mLock);
Jeff Brown6328cdc2010-07-29 18:18:33 -07003940
Jeff Brown79ac9692011-04-19 21:20:10 -07003941 // Handle TAP timeout.
3942 if (isTimeout) {
3943#if DEBUG_GESTURES
3944 LOGD("Gestures: Processing timeout");
3945#endif
3946
3947 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003948 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003949 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07003950 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07003951 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07003952 } else {
3953 // The tap is finished.
3954#if DEBUG_GESTURES
3955 LOGD("Gestures: TAP finished");
3956#endif
3957 *outFinishPreviousGesture = true;
3958
3959 mPointerGesture.activeGestureId = -1;
3960 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
3961 mPointerGesture.currentGestureIdBits.clear();
3962
Jeff Brown19c97d462011-06-01 12:33:19 -07003963 mPointerGesture.pointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07003964 return true;
3965 }
3966 }
3967
3968 // We did not handle this timeout.
3969 return false;
3970 }
3971
Jeff Brownace13b12011-03-09 17:39:48 -08003972 // Update the velocity tracker.
3973 {
3974 VelocityTracker::Position positions[MAX_POINTERS];
3975 uint32_t count = 0;
3976 for (BitSet32 idBits(mCurrentTouch.idBits); !idBits.isEmpty(); count++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003977 uint32_t id = idBits.firstMarkedBit();
3978 idBits.clearBit(id);
Jeff Brownace13b12011-03-09 17:39:48 -08003979 uint32_t index = mCurrentTouch.idToIndex[id];
3980 positions[count].x = mCurrentTouch.pointers[index].x
3981 * mLocked.pointerGestureXMovementScale;
3982 positions[count].y = mCurrentTouch.pointers[index].y
3983 * mLocked.pointerGestureYMovementScale;
3984 }
3985 mPointerGesture.velocityTracker.addMovement(when, mCurrentTouch.idBits, positions);
3986 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003987
Jeff Brownace13b12011-03-09 17:39:48 -08003988 // Pick a new active touch id if needed.
3989 // Choose an arbitrary pointer that just went down, if there is one.
3990 // Otherwise choose an arbitrary remaining pointer.
3991 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07003992 // We keep the same active touch id for as long as possible.
3993 bool activeTouchChanged = false;
3994 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
3995 int32_t activeTouchId = lastActiveTouchId;
3996 if (activeTouchId < 0) {
3997 if (!mCurrentTouch.idBits.isEmpty()) {
3998 activeTouchChanged = true;
3999 activeTouchId = mPointerGesture.activeTouchId = mCurrentTouch.idBits.firstMarkedBit();
4000 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004001 }
Jeff Brown2352b972011-04-12 22:39:53 -07004002 } else if (!mCurrentTouch.idBits.hasBit(activeTouchId)) {
4003 activeTouchChanged = true;
4004 if (!mCurrentTouch.idBits.isEmpty()) {
4005 activeTouchId = mPointerGesture.activeTouchId = mCurrentTouch.idBits.firstMarkedBit();
4006 } else {
4007 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004008 }
4009 }
4010
4011 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004012 bool isQuietTime = false;
4013 if (activeTouchId < 0) {
4014 mPointerGesture.resetQuietTime();
4015 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004016 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004017 if (!isQuietTime) {
4018 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4019 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4020 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
4021 && mCurrentTouch.pointerCount < 2) {
4022 // Enter quiet time when exiting swipe or freeform state.
4023 // This is to prevent accidentally entering the hover state and flinging the
4024 // pointer when finishing a swipe and there is still one pointer left onscreen.
4025 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004026 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07004027 && mCurrentTouch.pointerCount >= 2
4028 && !isPointerDown(mCurrentTouch.buttonState)) {
4029 // Enter quiet time when releasing the button and there are still two or more
4030 // fingers down. This may indicate that one finger was used to press the button
4031 // but it has not gone up yet.
4032 isQuietTime = true;
4033 }
4034 if (isQuietTime) {
4035 mPointerGesture.quietTime = when;
4036 }
Jeff Brownace13b12011-03-09 17:39:48 -08004037 }
4038 }
4039
4040 // Switch states based on button and pointer state.
4041 if (isQuietTime) {
4042 // Case 1: Quiet time. (QUIET)
4043#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004044 LOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004045 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004046#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004047 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4048 *outFinishPreviousGesture = true;
4049 }
Jeff Brownace13b12011-03-09 17:39:48 -08004050
4051 mPointerGesture.activeGestureId = -1;
4052 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004053 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004054
Jeff Brown19c97d462011-06-01 12:33:19 -07004055 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004056 } else if (isPointerDown(mCurrentTouch.buttonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004057 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004058 // The pointer follows the active touch point.
4059 // Emit DOWN, MOVE, UP events at the pointer location.
4060 //
4061 // Only the active touch matters; other fingers are ignored. This policy helps
4062 // to handle the case where the user places a second finger on the touch pad
4063 // to apply the necessary force to depress an integrated button below the surface.
4064 // We don't want the second finger to be delivered to applications.
4065 //
4066 // For this to work well, we need to make sure to track the pointer that is really
4067 // active. If the user first puts one finger down to click then adds another
4068 // finger to drag then the active pointer should switch to the finger that is
4069 // being dragged.
4070#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004071 LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brownace13b12011-03-09 17:39:48 -08004072 "currentTouchPointerCount=%d", activeTouchId, mCurrentTouch.pointerCount);
4073#endif
4074 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004075 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004076 *outFinishPreviousGesture = true;
4077 mPointerGesture.activeGestureId = 0;
4078 }
4079
4080 // Switch pointers if needed.
4081 // Find the fastest pointer and follow it.
Jeff Brown19c97d462011-06-01 12:33:19 -07004082 if (activeTouchId >= 0 && mCurrentTouch.pointerCount > 1) {
4083 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004084 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown19c97d462011-06-01 12:33:19 -07004085 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
4086 uint32_t id = mCurrentTouch.pointers[i].id;
4087 float vx, vy;
4088 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4089 float speed = hypotf(vx, vy);
4090 if (speed > bestSpeed) {
4091 bestId = id;
4092 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004093 }
Jeff Brown8d608662010-08-30 03:02:23 -07004094 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004095 }
4096 if (bestId >= 0 && bestId != activeTouchId) {
4097 mPointerGesture.activeTouchId = activeTouchId = bestId;
4098 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004099#if DEBUG_GESTURES
Jeff Brown19c97d462011-06-01 12:33:19 -07004100 LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
4101 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004102#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004103 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004104 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004105
Jeff Brown19c97d462011-06-01 12:33:19 -07004106 if (activeTouchId >= 0 && mLastTouch.idBits.hasBit(activeTouchId)) {
4107 const PointerData& currentPointer =
4108 mCurrentTouch.pointers[mCurrentTouch.idToIndex[activeTouchId]];
4109 const PointerData& lastPointer =
4110 mLastTouch.pointers[mLastTouch.idToIndex[activeTouchId]];
4111 float deltaX = (currentPointer.x - lastPointer.x)
4112 * mLocked.pointerGestureXMovementScale;
4113 float deltaY = (currentPointer.y - lastPointer.y)
4114 * mLocked.pointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004115
Jeff Brown612891e2011-07-15 20:44:17 -07004116 rotateDelta(mLocked.surfaceOrientation, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004117 mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
4118
4119 // Move the pointer using a relative motion.
4120 // When using spots, the click will occur at the position of the anchor
4121 // spot and all other spots will move there.
4122 mPointerController->move(deltaX, deltaY);
4123 } else {
4124 mPointerGesture.pointerVelocityControl.reset();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004125 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004126
Jeff Brownace13b12011-03-09 17:39:48 -08004127 float x, y;
4128 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004129
Jeff Brown79ac9692011-04-19 21:20:10 -07004130 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004131 mPointerGesture.currentGestureIdBits.clear();
4132 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4133 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004134 mPointerGesture.currentGestureProperties[0].clear();
4135 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004136 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004137 mPointerGesture.currentGestureCoords[0].clear();
4138 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4139 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4140 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4141 } else if (mCurrentTouch.pointerCount == 0) {
4142 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004143 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4144 *outFinishPreviousGesture = true;
4145 }
Jeff Brownace13b12011-03-09 17:39:48 -08004146
Jeff Brown79ac9692011-04-19 21:20:10 -07004147 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004148 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004149 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004150 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4151 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown2352b972011-04-12 22:39:53 -07004152 && mLastTouch.pointerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004153 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004154 float x, y;
4155 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004156 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4157 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004158#if DEBUG_GESTURES
4159 LOGD("Gestures: TAP");
4160#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004161
4162 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004163 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004164 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004165
Jeff Brownace13b12011-03-09 17:39:48 -08004166 mPointerGesture.activeGestureId = 0;
4167 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004168 mPointerGesture.currentGestureIdBits.clear();
4169 mPointerGesture.currentGestureIdBits.markBit(
4170 mPointerGesture.activeGestureId);
4171 mPointerGesture.currentGestureIdToIndex[
4172 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004173 mPointerGesture.currentGestureProperties[0].clear();
4174 mPointerGesture.currentGestureProperties[0].id =
4175 mPointerGesture.activeGestureId;
4176 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004177 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004178 mPointerGesture.currentGestureCoords[0].clear();
4179 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004180 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004181 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004182 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004183 mPointerGesture.currentGestureCoords[0].setAxisValue(
4184 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004185
Jeff Brownace13b12011-03-09 17:39:48 -08004186 tapped = true;
4187 } else {
4188#if DEBUG_GESTURES
4189 LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004190 x - mPointerGesture.tapX,
4191 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004192#endif
4193 }
4194 } else {
4195#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004196 LOGD("Gestures: Not a TAP, %0.3fms since down",
4197 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004198#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004199 }
Jeff Brownace13b12011-03-09 17:39:48 -08004200 }
Jeff Brown2352b972011-04-12 22:39:53 -07004201
Jeff Brown19c97d462011-06-01 12:33:19 -07004202 mPointerGesture.pointerVelocityControl.reset();
4203
Jeff Brownace13b12011-03-09 17:39:48 -08004204 if (!tapped) {
4205#if DEBUG_GESTURES
4206 LOGD("Gestures: NEUTRAL");
4207#endif
4208 mPointerGesture.activeGestureId = -1;
4209 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004210 mPointerGesture.currentGestureIdBits.clear();
4211 }
4212 } else if (mCurrentTouch.pointerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004213 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004214 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004215 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4216 // When in TAP_DRAG, emit MOVE events at the pointer location.
Jeff Brownb6110c22011-04-01 16:15:13 -07004217 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004218
Jeff Brown79ac9692011-04-19 21:20:10 -07004219 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4220 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004221 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004222 float x, y;
4223 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004224 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4225 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004226 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4227 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004228#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004229 LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
4230 x - mPointerGesture.tapX,
4231 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004232#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004233 }
4234 } else {
4235#if DEBUG_GESTURES
4236 LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
4237 (when - mPointerGesture.tapUpTime) * 0.000001f);
4238#endif
4239 }
4240 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4241 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4242 }
Jeff Brownace13b12011-03-09 17:39:48 -08004243
4244 if (mLastTouch.idBits.hasBit(activeTouchId)) {
4245 const PointerData& currentPointer =
4246 mCurrentTouch.pointers[mCurrentTouch.idToIndex[activeTouchId]];
4247 const PointerData& lastPointer =
4248 mLastTouch.pointers[mLastTouch.idToIndex[activeTouchId]];
4249 float deltaX = (currentPointer.x - lastPointer.x)
4250 * mLocked.pointerGestureXMovementScale;
4251 float deltaY = (currentPointer.y - lastPointer.y)
4252 * mLocked.pointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004253
Jeff Brown612891e2011-07-15 20:44:17 -07004254 rotateDelta(mLocked.surfaceOrientation, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004255 mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
4256
Jeff Brown2352b972011-04-12 22:39:53 -07004257 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004258 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004259 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004260 } else {
4261 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004262 }
4263
Jeff Brown79ac9692011-04-19 21:20:10 -07004264 bool down;
4265 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4266#if DEBUG_GESTURES
4267 LOGD("Gestures: TAP_DRAG");
4268#endif
4269 down = true;
4270 } else {
4271#if DEBUG_GESTURES
4272 LOGD("Gestures: HOVER");
4273#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004274 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4275 *outFinishPreviousGesture = true;
4276 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004277 mPointerGesture.activeGestureId = 0;
4278 down = false;
4279 }
Jeff Brownace13b12011-03-09 17:39:48 -08004280
4281 float x, y;
4282 mPointerController->getPosition(&x, &y);
4283
Jeff Brownace13b12011-03-09 17:39:48 -08004284 mPointerGesture.currentGestureIdBits.clear();
4285 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4286 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004287 mPointerGesture.currentGestureProperties[0].clear();
4288 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4289 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004290 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004291 mPointerGesture.currentGestureCoords[0].clear();
4292 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4293 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004294 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4295 down ? 1.0f : 0.0f);
4296
Jeff Brownace13b12011-03-09 17:39:48 -08004297 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004298 mPointerGesture.resetTap();
4299 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004300 mPointerGesture.tapX = x;
4301 mPointerGesture.tapY = y;
4302 }
Jeff Brownace13b12011-03-09 17:39:48 -08004303 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004304 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4305 // We need to provide feedback for each finger that goes down so we cannot wait
4306 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004307 //
Jeff Brown2352b972011-04-12 22:39:53 -07004308 // The ambiguous case is deciding what to do when there are two fingers down but they
4309 // have not moved enough to determine whether they are part of a drag or part of a
4310 // freeform gesture, or just a press or long-press at the pointer location.
4311 //
4312 // When there are two fingers we start with the PRESS hypothesis and we generate a
4313 // down at the pointer location.
4314 //
4315 // When the two fingers move enough or when additional fingers are added, we make
4316 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Jeff Brownb6110c22011-04-01 16:15:13 -07004317 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004318
Jeff Brown214eaf42011-05-26 19:17:02 -07004319 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004320 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004321 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004322 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4323 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004324 *outFinishPreviousGesture = true;
Jeff Brown19c97d462011-06-01 12:33:19 -07004325 } else if (!settled && mCurrentTouch.pointerCount > mLastTouch.pointerCount) {
4326 // Additional pointers have gone down but not yet settled.
4327 // Reset the gesture.
4328#if DEBUG_GESTURES
4329 LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004330 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004331 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004332 * 0.000001f);
4333#endif
4334 *outCancelPreviousGesture = true;
4335 } else {
4336 // Continue previous gesture.
4337 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4338 }
4339
4340 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004341 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4342 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004343 mPointerGesture.referenceIdBits.clear();
Jeff Brown19c97d462011-06-01 12:33:19 -07004344 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004345
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004346 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004347#if DEBUG_GESTURES
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004348 LOGD("Gestures: Using centroid as reference for MULTITOUCH, "
4349 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004350 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004351 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004352#endif
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004353 mCurrentTouch.getCentroid(&mPointerGesture.referenceTouchX,
4354 &mPointerGesture.referenceTouchY);
4355 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4356 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004357 }
Jeff Brownace13b12011-03-09 17:39:48 -08004358
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004359 // Clear the reference deltas for fingers not yet included in the reference calculation.
4360 for (BitSet32 idBits(mCurrentTouch.idBits.value & ~mPointerGesture.referenceIdBits.value);
4361 !idBits.isEmpty(); ) {
4362 uint32_t id = idBits.firstMarkedBit();
4363 idBits.clearBit(id);
4364
4365 mPointerGesture.referenceDeltas[id].dx = 0;
4366 mPointerGesture.referenceDeltas[id].dy = 0;
4367 }
4368 mPointerGesture.referenceIdBits = mCurrentTouch.idBits;
4369
4370 // Add delta for all fingers and calculate a common movement delta.
4371 float commonDeltaX = 0, commonDeltaY = 0;
4372 BitSet32 commonIdBits(mLastTouch.idBits.value & mCurrentTouch.idBits.value);
4373 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4374 bool first = (idBits == commonIdBits);
4375 uint32_t id = idBits.firstMarkedBit();
4376 idBits.clearBit(id);
4377
4378 const PointerData& cpd = mCurrentTouch.pointers[mCurrentTouch.idToIndex[id]];
4379 const PointerData& lpd = mLastTouch.pointers[mLastTouch.idToIndex[id]];
4380 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4381 delta.dx += cpd.x - lpd.x;
4382 delta.dy += cpd.y - lpd.y;
4383
4384 if (first) {
4385 commonDeltaX = delta.dx;
4386 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004387 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004388 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4389 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4390 }
4391 }
Jeff Brownace13b12011-03-09 17:39:48 -08004392
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004393 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4394 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4395 float dist[MAX_POINTER_ID + 1];
4396 int32_t distOverThreshold = 0;
4397 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
4398 uint32_t id = idBits.firstMarkedBit();
4399 idBits.clearBit(id);
Jeff Brownace13b12011-03-09 17:39:48 -08004400
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004401 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4402 dist[id] = hypotf(delta.dx * mLocked.pointerGestureXZoomScale,
4403 delta.dy * mLocked.pointerGestureYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004404 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004405 distOverThreshold += 1;
4406 }
4407 }
4408
4409 // Only transition when at least two pointers have moved further than
4410 // the minimum distance threshold.
4411 if (distOverThreshold >= 2) {
4412 float d;
4413 if (mCurrentTouch.pointerCount > 2) {
4414 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004415#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004416 LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
4417 mCurrentTouch.pointerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004418#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004419 *outCancelPreviousGesture = true;
4420 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4421 } else if (((d = distance(
4422 mCurrentTouch.pointers[0].x, mCurrentTouch.pointers[0].y,
4423 mCurrentTouch.pointers[1].x, mCurrentTouch.pointers[1].y))
4424 > mLocked.pointerGestureMaxSwipeWidth)) {
4425 // There are two pointers but they are too far apart for a SWIPE,
4426 // switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004427#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004428 LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
4429 d, mLocked.pointerGestureMaxSwipeWidth);
Jeff Brown2352b972011-04-12 22:39:53 -07004430#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004431 *outCancelPreviousGesture = true;
4432 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4433 } else {
4434 // There are two pointers. Wait for both pointers to start moving
4435 // before deciding whether this is a SWIPE or FREEFORM gesture.
4436 uint32_t id1 = mCurrentTouch.pointers[0].id;
4437 uint32_t id2 = mCurrentTouch.pointers[1].id;
4438 float dist1 = dist[id1];
4439 float dist2 = dist[id2];
Jeff Brown474dcb52011-06-14 20:22:50 -07004440 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4441 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004442 // Calculate the dot product of the displacement vectors.
4443 // When the vectors are oriented in approximately the same direction,
4444 // the angle betweeen them is near zero and the cosine of the angle
4445 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4446 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4447 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown6674d9b2011-06-07 16:50:14 -07004448 float dx1 = delta1.dx * mLocked.pointerGestureXZoomScale;
4449 float dy1 = delta1.dy * mLocked.pointerGestureYZoomScale;
4450 float dx2 = delta2.dx * mLocked.pointerGestureXZoomScale;
4451 float dy2 = delta2.dy * mLocked.pointerGestureYZoomScale;
4452 float dot = dx1 * dx2 + dy1 * dy2;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004453 float cosine = dot / (dist1 * dist2); // denominator always > 0
Jeff Brown474dcb52011-06-14 20:22:50 -07004454 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004455 // Pointers are moving in the same direction. Switch to SWIPE.
4456#if DEBUG_GESTURES
4457 LOGD("Gestures: PRESS transitioned to SWIPE, "
4458 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4459 "cosine %0.3f >= %0.3f",
Jeff Brown474dcb52011-06-14 20:22:50 -07004460 dist1, mConfig.pointerGestureMultitouchMinDistance,
4461 dist2, mConfig.pointerGestureMultitouchMinDistance,
4462 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004463#endif
4464 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4465 } else {
4466 // Pointers are moving in different directions. Switch to FREEFORM.
4467#if DEBUG_GESTURES
4468 LOGD("Gestures: PRESS transitioned to FREEFORM, "
4469 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4470 "cosine %0.3f < %0.3f",
Jeff Brown474dcb52011-06-14 20:22:50 -07004471 dist1, mConfig.pointerGestureMultitouchMinDistance,
4472 dist2, mConfig.pointerGestureMultitouchMinDistance,
4473 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004474#endif
4475 *outCancelPreviousGesture = true;
4476 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4477 }
Jeff Brownace13b12011-03-09 17:39:48 -08004478 }
4479 }
Jeff Brownace13b12011-03-09 17:39:48 -08004480 }
4481 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004482 // Switch from SWIPE to FREEFORM if additional pointers go down.
4483 // Cancel previous gesture.
4484 if (mCurrentTouch.pointerCount > 2) {
4485#if DEBUG_GESTURES
4486 LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
4487 mCurrentTouch.pointerCount);
4488#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004489 *outCancelPreviousGesture = true;
4490 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004491 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004492 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004493
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004494 // Move the reference points based on the overall group motion of the fingers
4495 // except in PRESS mode while waiting for a transition to occur.
4496 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4497 && (commonDeltaX || commonDeltaY)) {
4498 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brown2352b972011-04-12 22:39:53 -07004499 uint32_t id = idBits.firstMarkedBit();
4500 idBits.clearBit(id);
4501
Jeff Brown538881e2011-05-25 18:23:38 -07004502 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004503 delta.dx = 0;
4504 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004505 }
4506
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004507 mPointerGesture.referenceTouchX += commonDeltaX;
4508 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004509
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004510 commonDeltaX *= mLocked.pointerGestureXMovementScale;
4511 commonDeltaY *= mLocked.pointerGestureYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07004512
4513 rotateDelta(mLocked.surfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004514 mPointerGesture.pointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004515
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004516 mPointerGesture.referenceGestureX += commonDeltaX;
4517 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07004518 }
4519
4520 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07004521 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
4522 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4523 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004524#if DEBUG_GESTURES
Jeff Brown612891e2011-07-15 20:44:17 -07004525 LOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07004526 "activeGestureId=%d, currentTouchPointerCount=%d",
4527 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
4528#endif
4529 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
4530
4531 mPointerGesture.currentGestureIdBits.clear();
4532 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4533 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004534 mPointerGesture.currentGestureProperties[0].clear();
4535 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4536 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004537 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004538 mPointerGesture.currentGestureCoords[0].clear();
4539 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4540 mPointerGesture.referenceGestureX);
4541 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4542 mPointerGesture.referenceGestureY);
4543 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08004544 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4545 // FREEFORM mode.
4546#if DEBUG_GESTURES
4547 LOGD("Gestures: FREEFORM activeTouchId=%d,"
4548 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown2352b972011-04-12 22:39:53 -07004549 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004550#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004551 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004552
Jeff Brownace13b12011-03-09 17:39:48 -08004553 mPointerGesture.currentGestureIdBits.clear();
4554
4555 BitSet32 mappedTouchIdBits;
4556 BitSet32 usedGestureIdBits;
4557 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4558 // Initially, assign the active gesture id to the active touch point
4559 // if there is one. No other touch id bits are mapped yet.
4560 if (!*outCancelPreviousGesture) {
4561 mappedTouchIdBits.markBit(activeTouchId);
4562 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4563 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4564 mPointerGesture.activeGestureId;
4565 } else {
4566 mPointerGesture.activeGestureId = -1;
4567 }
4568 } else {
4569 // Otherwise, assume we mapped all touches from the previous frame.
4570 // Reuse all mappings that are still applicable.
4571 mappedTouchIdBits.value = mLastTouch.idBits.value & mCurrentTouch.idBits.value;
4572 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4573
4574 // Check whether we need to choose a new active gesture id because the
4575 // current went went up.
4576 for (BitSet32 upTouchIdBits(mLastTouch.idBits.value & ~mCurrentTouch.idBits.value);
4577 !upTouchIdBits.isEmpty(); ) {
4578 uint32_t upTouchId = upTouchIdBits.firstMarkedBit();
4579 upTouchIdBits.clearBit(upTouchId);
4580 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4581 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4582 mPointerGesture.activeGestureId = -1;
4583 break;
4584 }
4585 }
4586 }
4587
4588#if DEBUG_GESTURES
4589 LOGD("Gestures: FREEFORM follow up "
4590 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4591 "activeGestureId=%d",
4592 mappedTouchIdBits.value, usedGestureIdBits.value,
4593 mPointerGesture.activeGestureId);
4594#endif
4595
Jeff Brown2352b972011-04-12 22:39:53 -07004596 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
Jeff Brownace13b12011-03-09 17:39:48 -08004597 uint32_t touchId = mCurrentTouch.pointers[i].id;
4598 uint32_t gestureId;
4599 if (!mappedTouchIdBits.hasBit(touchId)) {
4600 gestureId = usedGestureIdBits.firstUnmarkedBit();
4601 usedGestureIdBits.markBit(gestureId);
4602 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4603#if DEBUG_GESTURES
4604 LOGD("Gestures: FREEFORM "
4605 "new mapping for touch id %d -> gesture id %d",
4606 touchId, gestureId);
4607#endif
4608 } else {
4609 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4610#if DEBUG_GESTURES
4611 LOGD("Gestures: FREEFORM "
4612 "existing mapping for touch id %d -> gesture id %d",
4613 touchId, gestureId);
4614#endif
4615 }
4616 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4617 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4618
Jeff Brown612891e2011-07-15 20:44:17 -07004619 float deltaX = (mCurrentTouch.pointers[i].x - mPointerGesture.referenceTouchX)
4620 * mLocked.pointerGestureXZoomScale;
4621 float deltaY = (mCurrentTouch.pointers[i].y - mPointerGesture.referenceTouchY)
4622 * mLocked.pointerGestureYZoomScale;
4623 rotateDelta(mLocked.surfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004624
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004625 mPointerGesture.currentGestureProperties[i].clear();
4626 mPointerGesture.currentGestureProperties[i].id = gestureId;
4627 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004628 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004629 mPointerGesture.currentGestureCoords[i].clear();
4630 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004631 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08004632 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004633 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004634 mPointerGesture.currentGestureCoords[i].setAxisValue(
4635 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4636 }
4637
4638 if (mPointerGesture.activeGestureId < 0) {
4639 mPointerGesture.activeGestureId =
4640 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4641#if DEBUG_GESTURES
4642 LOGD("Gestures: FREEFORM new "
4643 "activeGestureId=%d", mPointerGesture.activeGestureId);
4644#endif
4645 }
Jeff Brown2352b972011-04-12 22:39:53 -07004646 }
Jeff Brownace13b12011-03-09 17:39:48 -08004647 }
4648
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004649 mPointerController->setButtonState(mCurrentTouch.buttonState);
4650
Jeff Brownace13b12011-03-09 17:39:48 -08004651#if DEBUG_GESTURES
4652 LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004653 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4654 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004655 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004656 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4657 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004658 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
4659 uint32_t id = idBits.firstMarkedBit();
4660 idBits.clearBit(id);
4661 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004662 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004663 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004664 LOGD(" currentGesture[%d]: index=%d, toolType=%d, "
4665 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4666 id, index, properties.toolType,
4667 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004668 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4669 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4670 }
4671 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
4672 uint32_t id = idBits.firstMarkedBit();
4673 idBits.clearBit(id);
4674 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004675 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004676 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004677 LOGD(" lastGesture[%d]: index=%d, toolType=%d, "
4678 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4679 id, index, properties.toolType,
4680 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004681 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4682 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4683 }
4684#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004685 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004686}
4687
4688void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004689 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
4690 const PointerProperties* properties, const PointerCoords* coords,
4691 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08004692 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
4693 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004694 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08004695 uint32_t pointerCount = 0;
4696 while (!idBits.isEmpty()) {
4697 uint32_t id = idBits.firstMarkedBit();
4698 idBits.clearBit(id);
4699 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004700 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004701 pointerCoords[pointerCount].copyFrom(coords[index]);
4702
4703 if (changedId >= 0 && id == uint32_t(changedId)) {
4704 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
4705 }
4706
4707 pointerCount += 1;
4708 }
4709
Jeff Brownb6110c22011-04-01 16:15:13 -07004710 LOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004711
4712 if (changedId >= 0 && pointerCount == 1) {
4713 // Replace initial down and final up action.
4714 // We can compare the action without masking off the changed pointer index
4715 // because we know the index is 0.
4716 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
4717 action = AMOTION_EVENT_ACTION_DOWN;
4718 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
4719 action = AMOTION_EVENT_ACTION_UP;
4720 } else {
4721 // Can't happen.
Jeff Brownb6110c22011-04-01 16:15:13 -07004722 LOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08004723 }
4724 }
4725
4726 getDispatcher()->notifyMotion(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004727 action, flags, metaState, buttonState, edgeFlags,
4728 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownace13b12011-03-09 17:39:48 -08004729}
4730
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004731bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004732 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004733 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
4734 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08004735 bool changed = false;
4736 while (!idBits.isEmpty()) {
4737 uint32_t id = idBits.firstMarkedBit();
4738 idBits.clearBit(id);
4739
4740 uint32_t inIndex = inIdToIndex[id];
4741 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004742
4743 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004744 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004745 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004746 PointerCoords& curOutCoords = outCoords[outIndex];
4747
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004748 if (curInProperties != curOutProperties) {
4749 curOutProperties.copyFrom(curInProperties);
4750 changed = true;
4751 }
4752
Jeff Brownace13b12011-03-09 17:39:48 -08004753 if (curInCoords != curOutCoords) {
4754 curOutCoords.copyFrom(curInCoords);
4755 changed = true;
4756 }
4757 }
4758 return changed;
4759}
4760
4761void TouchInputMapper::fadePointer() {
4762 { // acquire lock
4763 AutoMutex _l(mLock);
4764 if (mPointerController != NULL) {
Jeff Brown538881e2011-05-25 18:23:38 -07004765 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
Jeff Brownace13b12011-03-09 17:39:48 -08004766 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004767 } // release lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004768}
4769
Jeff Brown6328cdc2010-07-29 18:18:33 -07004770bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) {
Jeff Brown9626b142011-03-03 02:09:54 -08004771 return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue
4772 && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004773}
4774
Jeff Brown6328cdc2010-07-29 18:18:33 -07004775const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked(
4776 int32_t x, int32_t y) {
4777 size_t numVirtualKeys = mLocked.virtualKeys.size();
4778 for (size_t i = 0; i < numVirtualKeys; i++) {
4779 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004780
4781#if DEBUG_VIRTUAL_KEYS
4782 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
4783 "left=%d, top=%d, right=%d, bottom=%d",
4784 x, y,
4785 virtualKey.keyCode, virtualKey.scanCode,
4786 virtualKey.hitLeft, virtualKey.hitTop,
4787 virtualKey.hitRight, virtualKey.hitBottom);
4788#endif
4789
4790 if (virtualKey.isHit(x, y)) {
4791 return & virtualKey;
4792 }
4793 }
4794
4795 return NULL;
4796}
4797
4798void TouchInputMapper::calculatePointerIds() {
4799 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
4800 uint32_t lastPointerCount = mLastTouch.pointerCount;
4801
4802 if (currentPointerCount == 0) {
4803 // No pointers to assign.
4804 mCurrentTouch.idBits.clear();
4805 } else if (lastPointerCount == 0) {
4806 // All pointers are new.
4807 mCurrentTouch.idBits.clear();
4808 for (uint32_t i = 0; i < currentPointerCount; i++) {
4809 mCurrentTouch.pointers[i].id = i;
4810 mCurrentTouch.idToIndex[i] = i;
4811 mCurrentTouch.idBits.markBit(i);
4812 }
4813 } else if (currentPointerCount == 1 && lastPointerCount == 1) {
4814 // Only one pointer and no change in count so it must have the same id as before.
4815 uint32_t id = mLastTouch.pointers[0].id;
4816 mCurrentTouch.pointers[0].id = id;
4817 mCurrentTouch.idToIndex[id] = 0;
4818 mCurrentTouch.idBits.value = BitSet32::valueForBit(id);
4819 } else {
4820 // General case.
4821 // We build a heap of squared euclidean distances between current and last pointers
4822 // associated with the current and last pointer indices. Then, we find the best
4823 // match (by distance) for each current pointer.
4824 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
4825
4826 uint32_t heapSize = 0;
4827 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
4828 currentPointerIndex++) {
4829 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
4830 lastPointerIndex++) {
4831 int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x
4832 - mLastTouch.pointers[lastPointerIndex].x;
4833 int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y
4834 - mLastTouch.pointers[lastPointerIndex].y;
4835
4836 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
4837
4838 // Insert new element into the heap (sift up).
4839 heap[heapSize].currentPointerIndex = currentPointerIndex;
4840 heap[heapSize].lastPointerIndex = lastPointerIndex;
4841 heap[heapSize].distance = distance;
4842 heapSize += 1;
4843 }
4844 }
4845
4846 // Heapify
4847 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
4848 startIndex -= 1;
4849 for (uint32_t parentIndex = startIndex; ;) {
4850 uint32_t childIndex = parentIndex * 2 + 1;
4851 if (childIndex >= heapSize) {
4852 break;
4853 }
4854
4855 if (childIndex + 1 < heapSize
4856 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4857 childIndex += 1;
4858 }
4859
4860 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4861 break;
4862 }
4863
4864 swap(heap[parentIndex], heap[childIndex]);
4865 parentIndex = childIndex;
4866 }
4867 }
4868
4869#if DEBUG_POINTER_ASSIGNMENT
4870 LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize);
4871 for (size_t i = 0; i < heapSize; i++) {
4872 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4873 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4874 heap[i].distance);
4875 }
4876#endif
4877
4878 // Pull matches out by increasing order of distance.
4879 // To avoid reassigning pointers that have already been matched, the loop keeps track
4880 // of which last and current pointers have been matched using the matchedXXXBits variables.
4881 // It also tracks the used pointer id bits.
4882 BitSet32 matchedLastBits(0);
4883 BitSet32 matchedCurrentBits(0);
4884 BitSet32 usedIdBits(0);
4885 bool first = true;
4886 for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) {
4887 for (;;) {
4888 if (first) {
4889 // The first time through the loop, we just consume the root element of
4890 // the heap (the one with smallest distance).
4891 first = false;
4892 } else {
4893 // Previous iterations consumed the root element of the heap.
4894 // Pop root element off of the heap (sift down).
4895 heapSize -= 1;
Jeff Brownb6110c22011-04-01 16:15:13 -07004896 LOG_ASSERT(heapSize > 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004897
4898 // Sift down.
4899 heap[0] = heap[heapSize];
4900 for (uint32_t parentIndex = 0; ;) {
4901 uint32_t childIndex = parentIndex * 2 + 1;
4902 if (childIndex >= heapSize) {
4903 break;
4904 }
4905
4906 if (childIndex + 1 < heapSize
4907 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4908 childIndex += 1;
4909 }
4910
4911 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4912 break;
4913 }
4914
4915 swap(heap[parentIndex], heap[childIndex]);
4916 parentIndex = childIndex;
4917 }
4918
4919#if DEBUG_POINTER_ASSIGNMENT
4920 LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize);
4921 for (size_t i = 0; i < heapSize; i++) {
4922 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4923 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4924 heap[i].distance);
4925 }
4926#endif
4927 }
4928
4929 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
4930 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
4931
4932 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
4933 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
4934
4935 matchedCurrentBits.markBit(currentPointerIndex);
4936 matchedLastBits.markBit(lastPointerIndex);
4937
4938 uint32_t id = mLastTouch.pointers[lastPointerIndex].id;
4939 mCurrentTouch.pointers[currentPointerIndex].id = id;
4940 mCurrentTouch.idToIndex[id] = currentPointerIndex;
4941 usedIdBits.markBit(id);
4942
4943#if DEBUG_POINTER_ASSIGNMENT
4944 LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
4945 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
4946#endif
4947 break;
4948 }
4949 }
4950
4951 // Assign fresh ids to new pointers.
4952 if (currentPointerCount > lastPointerCount) {
4953 for (uint32_t i = currentPointerCount - lastPointerCount; ;) {
4954 uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit();
4955 uint32_t id = usedIdBits.firstUnmarkedBit();
4956
4957 mCurrentTouch.pointers[currentPointerIndex].id = id;
4958 mCurrentTouch.idToIndex[id] = currentPointerIndex;
4959 usedIdBits.markBit(id);
4960
4961#if DEBUG_POINTER_ASSIGNMENT
4962 LOGD("calculatePointerIds - assigned: cur=%d, id=%d",
4963 currentPointerIndex, id);
4964#endif
4965
4966 if (--i == 0) break; // done
4967 matchedCurrentBits.markBit(currentPointerIndex);
4968 }
4969 }
4970
4971 // Fix id bits.
4972 mCurrentTouch.idBits = usedIdBits;
4973 }
4974}
4975
Jeff Brown6d0fec22010-07-23 21:28:06 -07004976int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07004977 { // acquire lock
4978 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004979
Jeff Brown6328cdc2010-07-29 18:18:33 -07004980 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004981 return AKEY_STATE_VIRTUAL;
4982 }
4983
Jeff Brown6328cdc2010-07-29 18:18:33 -07004984 size_t numVirtualKeys = mLocked.virtualKeys.size();
4985 for (size_t i = 0; i < numVirtualKeys; i++) {
4986 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004987 if (virtualKey.keyCode == keyCode) {
4988 return AKEY_STATE_UP;
4989 }
4990 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004991 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07004992
4993 return AKEY_STATE_UNKNOWN;
4994}
4995
4996int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07004997 { // acquire lock
4998 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004999
Jeff Brown6328cdc2010-07-29 18:18:33 -07005000 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005001 return AKEY_STATE_VIRTUAL;
5002 }
5003
Jeff Brown6328cdc2010-07-29 18:18:33 -07005004 size_t numVirtualKeys = mLocked.virtualKeys.size();
5005 for (size_t i = 0; i < numVirtualKeys; i++) {
5006 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005007 if (virtualKey.scanCode == scanCode) {
5008 return AKEY_STATE_UP;
5009 }
5010 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07005011 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07005012
5013 return AKEY_STATE_UNKNOWN;
5014}
5015
5016bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5017 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07005018 { // acquire lock
5019 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005020
Jeff Brown6328cdc2010-07-29 18:18:33 -07005021 size_t numVirtualKeys = mLocked.virtualKeys.size();
5022 for (size_t i = 0; i < numVirtualKeys; i++) {
5023 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005024
5025 for (size_t i = 0; i < numCodes; i++) {
5026 if (virtualKey.keyCode == keyCodes[i]) {
5027 outFlags[i] = 1;
5028 }
5029 }
5030 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07005031 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07005032
5033 return true;
5034}
5035
5036
5037// --- SingleTouchInputMapper ---
5038
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005039SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5040 TouchInputMapper(device) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005041 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005042}
5043
5044SingleTouchInputMapper::~SingleTouchInputMapper() {
5045}
5046
Jeff Brown80fd47c2011-05-24 01:07:44 -07005047void SingleTouchInputMapper::clearState() {
Jeff Brown49754db2011-07-01 17:37:58 -07005048 mCursorButtonAccumulator.clearButtons();
5049 mTouchButtonAccumulator.clearButtons();
5050 mSingleTouchMotionAccumulator.clearAbsoluteAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005051}
5052
5053void SingleTouchInputMapper::reset() {
5054 TouchInputMapper::reset();
5055
Jeff Brown80fd47c2011-05-24 01:07:44 -07005056 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005057 }
5058
5059void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07005060 mCursorButtonAccumulator.process(rawEvent);
5061 mTouchButtonAccumulator.process(rawEvent);
5062 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005063
Jeff Brown49754db2011-07-01 17:37:58 -07005064 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
5065 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005066 }
5067}
5068
5069void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005070 mCurrentTouch.clear();
5071
Jeff Brown49754db2011-07-01 17:37:58 -07005072 if (mTouchButtonAccumulator.isActive()) {
5073 uint32_t buttonState = mTouchButtonAccumulator.getButtonState();
5074 bool isHovering = mTouchButtonAccumulator.isHovering();
5075 if (mSingleTouchMotionAccumulator.getAbsoluteDistance() > 0) {
5076 isHovering = true;
5077 }
5078
Jeff Brown6d0fec22010-07-23 21:28:06 -07005079 mCurrentTouch.pointerCount = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005080 mCurrentTouch.idToIndex[0] = 0;
5081 mCurrentTouch.idBits.markBit(0);
Jeff Brown49754db2011-07-01 17:37:58 -07005082 mCurrentTouch.buttonState = buttonState;
5083
5084 PointerData& outPointer = mCurrentTouch.pointers[0];
5085 outPointer.id = 0;
5086 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5087 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5088 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5089 outPointer.touchMajor = 0;
5090 outPointer.touchMinor = 0;
5091 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5092 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5093 outPointer.orientation = 0;
5094 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
5095 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5096 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5097 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5098 }
5099 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005100 }
5101
5102 syncTouch(when, true);
5103}
5104
Jeff Brown8d608662010-08-30 03:02:23 -07005105void SingleTouchInputMapper::configureRawAxes() {
5106 TouchInputMapper::configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005107
Jeff Brown49754db2011-07-01 17:37:58 -07005108 mTouchButtonAccumulator.configure(getDevice());
5109
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 Brown49754db2011-07-01 17:37:58 -07005114 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_DISTANCE, & mRawAxes.distance);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005115}
5116
5117
5118// --- MultiTouchInputMapper ---
5119
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005120MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005121 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005122}
5123
5124MultiTouchInputMapper::~MultiTouchInputMapper() {
5125}
5126
Jeff Brown80fd47c2011-05-24 01:07:44 -07005127void MultiTouchInputMapper::clearState() {
Jeff Brown49754db2011-07-01 17:37:58 -07005128 mCursorButtonAccumulator.clearButtons();
5129 mTouchButtonAccumulator.clearButtons();
Jeff Brown6894a292011-07-01 17:59:27 -07005130 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005131
Jeff Brown49754db2011-07-01 17:37:58 -07005132 if (mMultiTouchMotionAccumulator.isUsingSlotsProtocol()) {
Jeff Brown2717eff2011-06-30 23:53:07 -07005133 // Query the driver for the current slot index and use it as the initial slot
5134 // before we start reading events from the device. It is possible that the
5135 // current slot index will not be the same as it was when the first event was
5136 // written into the evdev buffer, which means the input mapper could start
5137 // out of sync with the initial state of the events in the evdev buffer.
5138 // In the extremely unlikely case that this happens, the data from
5139 // two slots will be confused until the next ABS_MT_SLOT event is received.
5140 // This can cause the touch point to "jump", but at least there will be
5141 // no stuck touches.
Jeff Brown49754db2011-07-01 17:37:58 -07005142 int32_t initialSlot;
Jeff Brown2717eff2011-06-30 23:53:07 -07005143 status_t status = getEventHub()->getAbsoluteAxisValue(getDeviceId(), ABS_MT_SLOT,
Jeff Brown49754db2011-07-01 17:37:58 -07005144 &initialSlot);
Jeff Brown2717eff2011-06-30 23:53:07 -07005145 if (status) {
5146 LOGW("Could not retrieve current multitouch slot index. status=%d", status);
Jeff Brown49754db2011-07-01 17:37:58 -07005147 initialSlot = -1;
Jeff Brown2717eff2011-06-30 23:53:07 -07005148 }
Jeff Brown49754db2011-07-01 17:37:58 -07005149 mMultiTouchMotionAccumulator.clearSlots(initialSlot);
5150 } else {
5151 mMultiTouchMotionAccumulator.clearSlots(-1);
Jeff Brown2717eff2011-06-30 23:53:07 -07005152 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005153}
5154
5155void MultiTouchInputMapper::reset() {
5156 TouchInputMapper::reset();
5157
Jeff Brown80fd47c2011-05-24 01:07:44 -07005158 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005159}
5160
5161void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07005162 mCursorButtonAccumulator.process(rawEvent);
5163 mTouchButtonAccumulator.process(rawEvent);
5164 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005165
Jeff Brown49754db2011-07-01 17:37:58 -07005166 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
5167 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005168 }
5169}
5170
5171void MultiTouchInputMapper::sync(nsecs_t when) {
Jeff Brown49754db2011-07-01 17:37:58 -07005172 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005173 size_t outCount = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005174 bool havePointerIds = true;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005175
Jeff Brown6d0fec22010-07-23 21:28:06 -07005176 mCurrentTouch.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005177
Jeff Brown80fd47c2011-05-24 01:07:44 -07005178 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005179 const MultiTouchMotionAccumulator::Slot* inSlot =
5180 mMultiTouchMotionAccumulator.getSlot(inIndex);
5181 if (!inSlot->isInUse()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005182 continue;
5183 }
5184
Jeff Brown80fd47c2011-05-24 01:07:44 -07005185 if (outCount >= MAX_POINTERS) {
5186#if DEBUG_POINTERS
5187 LOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
5188 "ignoring the rest.",
5189 getDeviceName().string(), MAX_POINTERS);
5190#endif
5191 break; // too many fingers!
5192 }
5193
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005194 PointerData& outPointer = mCurrentTouch.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005195 outPointer.x = inSlot->getX();
5196 outPointer.y = inSlot->getY();
5197 outPointer.pressure = inSlot->getPressure();
5198 outPointer.touchMajor = inSlot->getTouchMajor();
5199 outPointer.touchMinor = inSlot->getTouchMinor();
5200 outPointer.toolMajor = inSlot->getToolMajor();
5201 outPointer.toolMinor = inSlot->getToolMinor();
5202 outPointer.orientation = inSlot->getOrientation();
5203 outPointer.distance = inSlot->getDistance();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005204
Jeff Brown49754db2011-07-01 17:37:58 -07005205 outPointer.toolType = inSlot->getToolType();
5206 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5207 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5208 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5209 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5210 }
Jeff Brown8d608662010-08-30 03:02:23 -07005211 }
5212
Jeff Brown49754db2011-07-01 17:37:58 -07005213 outPointer.isHovering = mTouchButtonAccumulator.isHovering()
5214 || inSlot->getDistance() > 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005215
Jeff Brown8d608662010-08-30 03:02:23 -07005216 // Assign pointer id using tracking id if available.
Jeff Brown6d0fec22010-07-23 21:28:06 -07005217 if (havePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005218 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005219 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005220 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005221 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
5222 uint32_t n = idBits.firstMarkedBit();
5223 idBits.clearBit(n);
5224
5225 if (mPointerTrackingIdMap[n] == trackingId) {
5226 id = n;
5227 }
5228 }
5229
5230 if (id < 0 && !mPointerIdBits.isFull()) {
5231 id = mPointerIdBits.firstUnmarkedBit();
5232 mPointerIdBits.markBit(id);
5233 mPointerTrackingIdMap[id] = trackingId;
5234 }
5235 }
5236 if (id < 0) {
5237 havePointerIds = false;
5238 mCurrentTouch.idBits.clear();
5239 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005240 outPointer.id = id;
5241 mCurrentTouch.idToIndex[id] = outCount;
5242 mCurrentTouch.idBits.markBit(id);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005243 }
5244 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005245
Jeff Brown6d0fec22010-07-23 21:28:06 -07005246 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005247 }
5248
Jeff Brown6d0fec22010-07-23 21:28:06 -07005249 mCurrentTouch.pointerCount = outCount;
Jeff Brown49754db2011-07-01 17:37:58 -07005250 mCurrentTouch.buttonState = mTouchButtonAccumulator.getButtonState();
Jeff Brownace13b12011-03-09 17:39:48 -08005251
Jeff Brown6894a292011-07-01 17:59:27 -07005252 mPointerIdBits = mCurrentTouch.idBits;
5253
Jeff Brown6d0fec22010-07-23 21:28:06 -07005254 syncTouch(when, havePointerIds);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005255
Jeff Brown49754db2011-07-01 17:37:58 -07005256 if (!mMultiTouchMotionAccumulator.isUsingSlotsProtocol()) {
5257 mMultiTouchMotionAccumulator.clearSlots(-1);
Jeff Brown441a9c22011-06-02 18:22:25 -07005258 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005259}
5260
Jeff Brown8d608662010-08-30 03:02:23 -07005261void MultiTouchInputMapper::configureRawAxes() {
5262 TouchInputMapper::configureRawAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005263
Jeff Brown49754db2011-07-01 17:37:58 -07005264 mTouchButtonAccumulator.configure(getDevice());
5265
Jeff Brown80fd47c2011-05-24 01:07:44 -07005266 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, &mRawAxes.x);
5267 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, &mRawAxes.y);
5268 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, &mRawAxes.touchMajor);
5269 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, &mRawAxes.touchMinor);
5270 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, &mRawAxes.toolMajor);
5271 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, &mRawAxes.toolMinor);
5272 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, &mRawAxes.orientation);
5273 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, &mRawAxes.pressure);
5274 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_DISTANCE, &mRawAxes.distance);
5275 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TRACKING_ID, &mRawAxes.trackingId);
5276 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_SLOT, &mRawAxes.slot);
5277
5278 if (mRawAxes.trackingId.valid
5279 && mRawAxes.slot.valid && mRawAxes.slot.minValue == 0 && mRawAxes.slot.maxValue > 0) {
Jeff Brown49754db2011-07-01 17:37:58 -07005280 size_t slotCount = mRawAxes.slot.maxValue + 1;
5281 if (slotCount > MAX_SLOTS) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005282 LOGW("MultiTouch Device %s reported %d slots but the framework "
5283 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07005284 getDeviceName().string(), slotCount, MAX_SLOTS);
5285 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005286 }
Jeff Brown49754db2011-07-01 17:37:58 -07005287 mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005288 } else {
Jeff Brown49754db2011-07-01 17:37:58 -07005289 mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005290 }
5291
Jeff Brown2717eff2011-06-30 23:53:07 -07005292 clearState();
Jeff Brown9c3cda02010-06-15 01:31:58 -07005293}
5294
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005295
Jeff Browncb1404e2011-01-15 18:14:15 -08005296// --- JoystickInputMapper ---
5297
5298JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5299 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005300}
5301
5302JoystickInputMapper::~JoystickInputMapper() {
5303}
5304
5305uint32_t JoystickInputMapper::getSources() {
5306 return AINPUT_SOURCE_JOYSTICK;
5307}
5308
5309void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5310 InputMapper::populateDeviceInfo(info);
5311
Jeff Brown6f2fba42011-02-19 01:08:02 -08005312 for (size_t i = 0; i < mAxes.size(); i++) {
5313 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005314 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5315 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005316 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005317 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5318 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005319 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005320 }
5321}
5322
5323void JoystickInputMapper::dump(String8& dump) {
5324 dump.append(INDENT2 "Joystick Input Mapper:\n");
5325
Jeff Brown6f2fba42011-02-19 01:08:02 -08005326 dump.append(INDENT3 "Axes:\n");
5327 size_t numAxes = mAxes.size();
5328 for (size_t i = 0; i < numAxes; i++) {
5329 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005330 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005331 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005332 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005333 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005334 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005335 }
Jeff Brown85297452011-03-04 13:07:49 -08005336 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5337 label = getAxisLabel(axis.axisInfo.highAxis);
5338 if (label) {
5339 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5340 } else {
5341 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5342 axis.axisInfo.splitValue);
5343 }
5344 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5345 dump.append(" (invert)");
5346 }
5347
5348 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5349 axis.min, axis.max, axis.flat, axis.fuzz);
5350 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5351 "highScale=%0.5f, highOffset=%0.5f\n",
5352 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005353 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5354 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005355 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07005356 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08005357 }
5358}
5359
Jeff Brown474dcb52011-06-14 20:22:50 -07005360void JoystickInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
5361 InputMapper::configure(config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08005362
Jeff Brown474dcb52011-06-14 20:22:50 -07005363 if (!changes) { // first time only
5364 // Collect all axes.
5365 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
5366 RawAbsoluteAxisInfo rawAxisInfo;
5367 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), abs, &rawAxisInfo);
5368 if (rawAxisInfo.valid) {
5369 // Map axis.
5370 AxisInfo axisInfo;
5371 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
5372 if (!explicitlyMapped) {
5373 // Axis is not explicitly mapped, will choose a generic axis later.
5374 axisInfo.mode = AxisInfo::MODE_NORMAL;
5375 axisInfo.axis = -1;
5376 }
5377
5378 // Apply flat override.
5379 int32_t rawFlat = axisInfo.flatOverride < 0
5380 ? rawAxisInfo.flat : axisInfo.flatOverride;
5381
5382 // Calculate scaling factors and limits.
5383 Axis axis;
5384 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5385 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5386 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5387 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5388 scale, 0.0f, highScale, 0.0f,
5389 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5390 } else if (isCenteredAxis(axisInfo.axis)) {
5391 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5392 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
5393 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5394 scale, offset, scale, offset,
5395 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5396 } else {
5397 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5398 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5399 scale, 0.0f, scale, 0.0f,
5400 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5401 }
5402
5403 // To eliminate noise while the joystick is at rest, filter out small variations
5404 // in axis values up front.
5405 axis.filter = axis.flat * 0.25f;
5406
5407 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005408 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005409 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005410
Jeff Brown474dcb52011-06-14 20:22:50 -07005411 // If there are too many axes, start dropping them.
5412 // Prefer to keep explicitly mapped axes.
5413 if (mAxes.size() > PointerCoords::MAX_AXES) {
5414 LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
5415 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5416 pruneAxes(true);
5417 pruneAxes(false);
5418 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005419
Jeff Brown474dcb52011-06-14 20:22:50 -07005420 // Assign generic axis ids to remaining axes.
5421 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5422 size_t numAxes = mAxes.size();
5423 for (size_t i = 0; i < numAxes; i++) {
5424 Axis& axis = mAxes.editValueAt(i);
5425 if (axis.axisInfo.axis < 0) {
5426 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5427 && haveAxis(nextGenericAxisId)) {
5428 nextGenericAxisId += 1;
5429 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005430
Jeff Brown474dcb52011-06-14 20:22:50 -07005431 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
5432 axis.axisInfo.axis = nextGenericAxisId;
5433 nextGenericAxisId += 1;
5434 } else {
5435 LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
5436 "have already been assigned to other axes.",
5437 getDeviceName().string(), mAxes.keyAt(i));
5438 mAxes.removeItemsAt(i--);
5439 numAxes -= 1;
5440 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005441 }
5442 }
5443 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005444}
5445
Jeff Brown85297452011-03-04 13:07:49 -08005446bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005447 size_t numAxes = mAxes.size();
5448 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005449 const Axis& axis = mAxes.valueAt(i);
5450 if (axis.axisInfo.axis == axisId
5451 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5452 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005453 return true;
5454 }
5455 }
5456 return false;
5457}
Jeff Browncb1404e2011-01-15 18:14:15 -08005458
Jeff Brown6f2fba42011-02-19 01:08:02 -08005459void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5460 size_t i = mAxes.size();
5461 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5462 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5463 continue;
5464 }
5465 LOGI("Discarding joystick '%s' axis %d because there are too many axes.",
5466 getDeviceName().string(), mAxes.keyAt(i));
5467 mAxes.removeItemsAt(i);
5468 }
5469}
5470
5471bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5472 switch (axis) {
5473 case AMOTION_EVENT_AXIS_X:
5474 case AMOTION_EVENT_AXIS_Y:
5475 case AMOTION_EVENT_AXIS_Z:
5476 case AMOTION_EVENT_AXIS_RX:
5477 case AMOTION_EVENT_AXIS_RY:
5478 case AMOTION_EVENT_AXIS_RZ:
5479 case AMOTION_EVENT_AXIS_HAT_X:
5480 case AMOTION_EVENT_AXIS_HAT_Y:
5481 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005482 case AMOTION_EVENT_AXIS_RUDDER:
5483 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005484 return true;
5485 default:
5486 return false;
5487 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005488}
5489
5490void JoystickInputMapper::reset() {
5491 // Recenter all axes.
5492 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Browncb1404e2011-01-15 18:14:15 -08005493
Jeff Brown6f2fba42011-02-19 01:08:02 -08005494 size_t numAxes = mAxes.size();
5495 for (size_t i = 0; i < numAxes; i++) {
5496 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005497 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005498 }
5499
5500 sync(when, true /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005501
5502 InputMapper::reset();
5503}
5504
5505void JoystickInputMapper::process(const RawEvent* rawEvent) {
5506 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005507 case EV_ABS: {
5508 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
5509 if (index >= 0) {
5510 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005511 float newValue, highNewValue;
5512 switch (axis.axisInfo.mode) {
5513 case AxisInfo::MODE_INVERT:
5514 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5515 * axis.scale + axis.offset;
5516 highNewValue = 0.0f;
5517 break;
5518 case AxisInfo::MODE_SPLIT:
5519 if (rawEvent->value < axis.axisInfo.splitValue) {
5520 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5521 * axis.scale + axis.offset;
5522 highNewValue = 0.0f;
5523 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5524 newValue = 0.0f;
5525 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5526 * axis.highScale + axis.highOffset;
5527 } else {
5528 newValue = 0.0f;
5529 highNewValue = 0.0f;
5530 }
5531 break;
5532 default:
5533 newValue = rawEvent->value * axis.scale + axis.offset;
5534 highNewValue = 0.0f;
5535 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005536 }
Jeff Brown85297452011-03-04 13:07:49 -08005537 axis.newValue = newValue;
5538 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005539 }
5540 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005541 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005542
5543 case EV_SYN:
5544 switch (rawEvent->scanCode) {
5545 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005546 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005547 break;
5548 }
5549 break;
5550 }
5551}
5552
Jeff Brown6f2fba42011-02-19 01:08:02 -08005553void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005554 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005555 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005556 }
5557
5558 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005559 int32_t buttonState = 0;
5560
5561 PointerProperties pointerProperties;
5562 pointerProperties.clear();
5563 pointerProperties.id = 0;
5564 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08005565
Jeff Brown6f2fba42011-02-19 01:08:02 -08005566 PointerCoords pointerCoords;
5567 pointerCoords.clear();
5568
5569 size_t numAxes = mAxes.size();
5570 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005571 const Axis& axis = mAxes.valueAt(i);
5572 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
5573 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5574 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
5575 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005576 }
5577
Jeff Brown56194eb2011-03-02 19:23:13 -08005578 // Moving a joystick axis should not wake the devide because joysticks can
5579 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
5580 // button will likely wake the device.
5581 // TODO: Use the input device configuration to control this behavior more finely.
5582 uint32_t policyFlags = 0;
5583
Jeff Brown56194eb2011-03-02 19:23:13 -08005584 getDispatcher()->notifyMotion(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005585 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
5586 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Browncb1404e2011-01-15 18:14:15 -08005587}
5588
Jeff Brown85297452011-03-04 13:07:49 -08005589bool JoystickInputMapper::filterAxes(bool force) {
5590 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005591 size_t numAxes = mAxes.size();
5592 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005593 Axis& axis = mAxes.editValueAt(i);
5594 if (force || hasValueChangedSignificantly(axis.filter,
5595 axis.newValue, axis.currentValue, axis.min, axis.max)) {
5596 axis.currentValue = axis.newValue;
5597 atLeastOneSignificantChange = true;
5598 }
5599 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5600 if (force || hasValueChangedSignificantly(axis.filter,
5601 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
5602 axis.highCurrentValue = axis.highNewValue;
5603 atLeastOneSignificantChange = true;
5604 }
5605 }
5606 }
5607 return atLeastOneSignificantChange;
5608}
5609
5610bool JoystickInputMapper::hasValueChangedSignificantly(
5611 float filter, float newValue, float currentValue, float min, float max) {
5612 if (newValue != currentValue) {
5613 // Filter out small changes in value unless the value is converging on the axis
5614 // bounds or center point. This is intended to reduce the amount of information
5615 // sent to applications by particularly noisy joysticks (such as PS3).
5616 if (fabs(newValue - currentValue) > filter
5617 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
5618 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
5619 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
5620 return true;
5621 }
5622 }
5623 return false;
5624}
5625
5626bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
5627 float filter, float newValue, float currentValue, float thresholdValue) {
5628 float newDistance = fabs(newValue - thresholdValue);
5629 if (newDistance < filter) {
5630 float oldDistance = fabs(currentValue - thresholdValue);
5631 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005632 return true;
5633 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005634 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005635 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005636}
5637
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005638} // namespace android