blob: c9fac8194c015f389fe43adbaf7853f7488e5127 [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 -0800154static uint32_t getButtonStateForScanCode(int32_t scanCode) {
155 // Currently all buttons are mapped to the primary button.
156 switch (scanCode) {
157 case BTN_LEFT:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700158 return AMOTION_EVENT_BUTTON_PRIMARY;
Jeff Brownefd32662011-03-08 15:13:06 -0800159 case BTN_RIGHT:
Jeff Brown53ca3f12011-06-27 18:36:00 -0700160 case BTN_STYLUS:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700161 return AMOTION_EVENT_BUTTON_SECONDARY;
Jeff Brownefd32662011-03-08 15:13:06 -0800162 case BTN_MIDDLE:
Jeff Brown53ca3f12011-06-27 18:36:00 -0700163 case BTN_STYLUS2:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700164 return AMOTION_EVENT_BUTTON_TERTIARY;
Jeff Brownefd32662011-03-08 15:13:06 -0800165 case BTN_SIDE:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700166 return AMOTION_EVENT_BUTTON_BACK;
Jeff Brownefd32662011-03-08 15:13:06 -0800167 case BTN_FORWARD:
Jeff Brown53ca3f12011-06-27 18:36:00 -0700168 case BTN_EXTRA:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700169 return AMOTION_EVENT_BUTTON_FORWARD;
Jeff Brownefd32662011-03-08 15:13:06 -0800170 case BTN_BACK:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700171 return AMOTION_EVENT_BUTTON_BACK;
Jeff Brownefd32662011-03-08 15:13:06 -0800172 case BTN_TASK:
Jeff Brownefd32662011-03-08 15:13:06 -0800173 default:
174 return 0;
175 }
176}
177
178// Returns true if the pointer should be reported as being down given the specified
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700179// button states. This determines whether the event is reported as a touch event.
180static bool isPointerDown(int32_t buttonState) {
181 return buttonState &
182 (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY
Jeff Brown53ca3f12011-06-27 18:36:00 -0700183 | AMOTION_EVENT_BUTTON_TERTIARY);
Jeff Brownefd32662011-03-08 15:13:06 -0800184}
185
Jeff Brown2352b972011-04-12 22:39:53 -0700186static float calculateCommonVector(float a, float b) {
187 if (a > 0 && b > 0) {
188 return a < b ? a : b;
189 } else if (a < 0 && b < 0) {
190 return a > b ? a : b;
191 } else {
192 return 0;
193 }
194}
195
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700196static void synthesizeButtonKey(InputReaderContext* context, int32_t action,
197 nsecs_t when, int32_t deviceId, uint32_t source,
198 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState,
199 int32_t buttonState, int32_t keyCode) {
200 if (
201 (action == AKEY_EVENT_ACTION_DOWN
202 && !(lastButtonState & buttonState)
203 && (currentButtonState & buttonState))
204 || (action == AKEY_EVENT_ACTION_UP
205 && (lastButtonState & buttonState)
206 && !(currentButtonState & buttonState))) {
207 context->getDispatcher()->notifyKey(when, deviceId, source, policyFlags,
208 action, 0, keyCode, 0, context->getGlobalMetaState(), when);
209 }
210}
211
212static void synthesizeButtonKeys(InputReaderContext* context, int32_t action,
213 nsecs_t when, int32_t deviceId, uint32_t source,
214 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState) {
215 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
216 lastButtonState, currentButtonState,
217 AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK);
218 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
219 lastButtonState, currentButtonState,
220 AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD);
221}
222
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700223
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700224// --- InputReader ---
225
226InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700227 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700228 const sp<InputDispatcherInterface>& dispatcher) :
Jeff Brown6d0fec22010-07-23 21:28:06 -0700229 mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher),
Jeff Brown1a84fd12011-06-02 01:26:32 -0700230 mGlobalMetaState(0), mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX),
Jeff Brown474dcb52011-06-14 20:22:50 -0700231 mConfigurationChangesToRefresh(0) {
232 refreshConfiguration(0);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700233 updateGlobalMetaState();
234 updateInputConfiguration();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700235}
236
237InputReader::~InputReader() {
238 for (size_t i = 0; i < mDevices.size(); i++) {
239 delete mDevices.valueAt(i);
240 }
241}
242
243void InputReader::loopOnce() {
Jeff Brown474dcb52011-06-14 20:22:50 -0700244 uint32_t changes;
245 { // acquire lock
246 AutoMutex _l(mStateLock);
247
248 changes = mConfigurationChangesToRefresh;
249 mConfigurationChangesToRefresh = 0;
250 } // release lock
251
252 if (changes) {
253 refreshConfiguration(changes);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700254 }
255
Jeff Brownaa3855d2011-03-17 01:34:19 -0700256 int32_t timeoutMillis = -1;
257 if (mNextTimeout != LLONG_MAX) {
258 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
259 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
260 }
261
Jeff Brownb7198742011-03-18 18:14:26 -0700262 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
263 if (count) {
264 processEvents(mEventBuffer, count);
265 }
266 if (!count || timeoutMillis == 0) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700267 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
268#if DEBUG_RAW_EVENTS
269 LOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
270#endif
271 mNextTimeout = LLONG_MAX;
272 timeoutExpired(now);
273 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700274}
275
Jeff Brownb7198742011-03-18 18:14:26 -0700276void InputReader::processEvents(const RawEvent* rawEvents, size_t count) {
277 for (const RawEvent* rawEvent = rawEvents; count;) {
278 int32_t type = rawEvent->type;
279 size_t batchSize = 1;
280 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
281 int32_t deviceId = rawEvent->deviceId;
282 while (batchSize < count) {
283 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
284 || rawEvent[batchSize].deviceId != deviceId) {
285 break;
286 }
287 batchSize += 1;
288 }
289#if DEBUG_RAW_EVENTS
290 LOGD("BatchSize: %d Count: %d", batchSize, count);
291#endif
292 processEventsForDevice(deviceId, rawEvent, batchSize);
293 } else {
294 switch (rawEvent->type) {
295 case EventHubInterface::DEVICE_ADDED:
296 addDevice(rawEvent->deviceId);
297 break;
298 case EventHubInterface::DEVICE_REMOVED:
299 removeDevice(rawEvent->deviceId);
300 break;
301 case EventHubInterface::FINISHED_DEVICE_SCAN:
302 handleConfigurationChanged(rawEvent->when);
303 break;
304 default:
Jeff Brownb6110c22011-04-01 16:15:13 -0700305 LOG_ASSERT(false); // can't happen
Jeff Brownb7198742011-03-18 18:14:26 -0700306 break;
307 }
308 }
309 count -= batchSize;
310 rawEvent += batchSize;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700311 }
312}
313
Jeff Brown7342bb92010-10-01 18:55:43 -0700314void InputReader::addDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700315 String8 name = mEventHub->getDeviceName(deviceId);
316 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
317
318 InputDevice* device = createDevice(deviceId, name, classes);
Jeff Brown474dcb52011-06-14 20:22:50 -0700319 device->configure(&mConfig, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700320
Jeff Brown8d608662010-08-30 03:02:23 -0700321 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800322 LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700323 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800324 LOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700325 device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700326 }
327
Jeff Brown6d0fec22010-07-23 21:28:06 -0700328 bool added = false;
329 { // acquire device registry writer lock
330 RWLock::AutoWLock _wl(mDeviceRegistryLock);
331
332 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
333 if (deviceIndex < 0) {
334 mDevices.add(deviceId, device);
335 added = true;
336 }
337 } // release device registry writer lock
338
339 if (! added) {
340 LOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
341 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700342 return;
343 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700344}
345
Jeff Brown7342bb92010-10-01 18:55:43 -0700346void InputReader::removeDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700347 bool removed = false;
348 InputDevice* device = NULL;
349 { // acquire device registry writer lock
350 RWLock::AutoWLock _wl(mDeviceRegistryLock);
351
352 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
353 if (deviceIndex >= 0) {
354 device = mDevices.valueAt(deviceIndex);
355 mDevices.removeItemsAt(deviceIndex, 1);
356 removed = true;
357 }
358 } // release device registry writer lock
359
360 if (! removed) {
361 LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700362 return;
363 }
364
Jeff Brown6d0fec22010-07-23 21:28:06 -0700365 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800366 LOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700367 device->getId(), device->getName().string());
368 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800369 LOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700370 device->getId(), device->getName().string(), device->getSources());
371 }
372
Jeff Brown8d608662010-08-30 03:02:23 -0700373 device->reset();
374
Jeff Brown6d0fec22010-07-23 21:28:06 -0700375 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700376}
377
Jeff Brown6d0fec22010-07-23 21:28:06 -0700378InputDevice* InputReader::createDevice(int32_t deviceId, const String8& name, uint32_t classes) {
379 InputDevice* device = new InputDevice(this, deviceId, name);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700380
Jeff Brown56194eb2011-03-02 19:23:13 -0800381 // External devices.
382 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
383 device->setExternal(true);
384 }
385
Jeff Brown6d0fec22010-07-23 21:28:06 -0700386 // Switch-like devices.
387 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
388 device->addMapper(new SwitchInputMapper(device));
389 }
390
391 // Keyboard-like devices.
Jeff Brownefd32662011-03-08 15:13:06 -0800392 uint32_t keyboardSource = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700393 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
394 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800395 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700396 }
397 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
398 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
399 }
400 if (classes & INPUT_DEVICE_CLASS_DPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800401 keyboardSource |= AINPUT_SOURCE_DPAD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700402 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800403 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800404 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
Jeff Browncb1404e2011-01-15 18:14:15 -0800405 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700406
Jeff Brownefd32662011-03-08 15:13:06 -0800407 if (keyboardSource != 0) {
408 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700409 }
410
Jeff Brown83c09682010-12-23 17:50:18 -0800411 // Cursor-like devices.
412 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
413 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700414 }
415
Jeff Brown58a2da82011-01-25 16:02:22 -0800416 // Touchscreens and touchpad devices.
417 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800418 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800419 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800420 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700421 }
422
Jeff Browncb1404e2011-01-15 18:14:15 -0800423 // Joystick-like devices.
424 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
425 device->addMapper(new JoystickInputMapper(device));
426 }
427
Jeff Brown6d0fec22010-07-23 21:28:06 -0700428 return device;
429}
430
Jeff Brownb7198742011-03-18 18:14:26 -0700431void InputReader::processEventsForDevice(int32_t deviceId,
432 const RawEvent* rawEvents, size_t count) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700433 { // acquire device registry reader lock
434 RWLock::AutoRLock _rl(mDeviceRegistryLock);
435
436 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
437 if (deviceIndex < 0) {
438 LOGW("Discarding event for unknown deviceId %d.", deviceId);
439 return;
440 }
441
442 InputDevice* device = mDevices.valueAt(deviceIndex);
443 if (device->isIgnored()) {
444 //LOGD("Discarding event for ignored deviceId %d.", deviceId);
445 return;
446 }
447
Jeff Brownb7198742011-03-18 18:14:26 -0700448 device->process(rawEvents, count);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700449 } // release device registry reader lock
450}
451
Jeff Brownaa3855d2011-03-17 01:34:19 -0700452void InputReader::timeoutExpired(nsecs_t when) {
453 { // acquire device registry reader lock
454 RWLock::AutoRLock _rl(mDeviceRegistryLock);
455
456 for (size_t i = 0; i < mDevices.size(); i++) {
457 InputDevice* device = mDevices.valueAt(i);
458 if (!device->isIgnored()) {
459 device->timeoutExpired(when);
460 }
461 }
462 } // release device registry reader lock
463}
464
Jeff Brownc3db8582010-10-20 15:33:38 -0700465void InputReader::handleConfigurationChanged(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700466 // Reset global meta state because it depends on the list of all configured devices.
467 updateGlobalMetaState();
468
469 // Update input configuration.
470 updateInputConfiguration();
471
472 // Enqueue configuration changed.
473 mDispatcher->notifyConfigurationChanged(when);
474}
475
Jeff Brown474dcb52011-06-14 20:22:50 -0700476void InputReader::refreshConfiguration(uint32_t changes) {
Jeff Brown1a84fd12011-06-02 01:26:32 -0700477 mPolicy->getReaderConfiguration(&mConfig);
478 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
479
Jeff Brown474dcb52011-06-14 20:22:50 -0700480 if (changes) {
481 LOGI("Reconfiguring input devices. changes=0x%08x", changes);
482
483 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
484 mEventHub->requestReopenDevices();
485 } else {
486 { // acquire device registry reader lock
487 RWLock::AutoRLock _rl(mDeviceRegistryLock);
488
489 for (size_t i = 0; i < mDevices.size(); i++) {
490 InputDevice* device = mDevices.valueAt(i);
491 device->configure(&mConfig, changes);
492 }
493 } // release device registry reader lock
494 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700495 }
496}
497
498void InputReader::updateGlobalMetaState() {
499 { // acquire state lock
500 AutoMutex _l(mStateLock);
501
502 mGlobalMetaState = 0;
503
504 { // acquire device registry reader lock
505 RWLock::AutoRLock _rl(mDeviceRegistryLock);
506
507 for (size_t i = 0; i < mDevices.size(); i++) {
508 InputDevice* device = mDevices.valueAt(i);
509 mGlobalMetaState |= device->getMetaState();
510 }
511 } // release device registry reader lock
512 } // release state lock
513}
514
515int32_t InputReader::getGlobalMetaState() {
516 { // acquire state lock
517 AutoMutex _l(mStateLock);
518
519 return mGlobalMetaState;
520 } // release state lock
521}
522
523void InputReader::updateInputConfiguration() {
524 { // acquire state lock
525 AutoMutex _l(mStateLock);
526
527 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
528 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
529 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
530 { // acquire device registry reader lock
531 RWLock::AutoRLock _rl(mDeviceRegistryLock);
532
533 InputDeviceInfo deviceInfo;
534 for (size_t i = 0; i < mDevices.size(); i++) {
535 InputDevice* device = mDevices.valueAt(i);
536 device->getDeviceInfo(& deviceInfo);
537 uint32_t sources = deviceInfo.getSources();
538
539 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
540 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
541 }
542 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
543 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
544 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
545 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
546 }
547 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
548 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700549 }
550 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700551 } // release device registry reader lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700552
Jeff Brown6d0fec22010-07-23 21:28:06 -0700553 mInputConfiguration.touchScreen = touchScreenConfig;
554 mInputConfiguration.keyboard = keyboardConfig;
555 mInputConfiguration.navigation = navigationConfig;
556 } // release state lock
557}
558
Jeff Brownfe508922011-01-18 15:10:10 -0800559void InputReader::disableVirtualKeysUntil(nsecs_t time) {
560 mDisableVirtualKeysTimeout = time;
561}
562
563bool InputReader::shouldDropVirtualKey(nsecs_t now,
564 InputDevice* device, int32_t keyCode, int32_t scanCode) {
565 if (now < mDisableVirtualKeysTimeout) {
566 LOGI("Dropping virtual key from device %s because virtual keys are "
567 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
568 device->getName().string(),
569 (mDisableVirtualKeysTimeout - now) * 0.000001,
570 keyCode, scanCode);
571 return true;
572 } else {
573 return false;
574 }
575}
576
Jeff Brown05dc66a2011-03-02 14:41:58 -0800577void InputReader::fadePointer() {
578 { // acquire device registry reader lock
579 RWLock::AutoRLock _rl(mDeviceRegistryLock);
580
581 for (size_t i = 0; i < mDevices.size(); i++) {
582 InputDevice* device = mDevices.valueAt(i);
583 device->fadePointer();
584 }
585 } // release device registry reader lock
586}
587
Jeff Brownaa3855d2011-03-17 01:34:19 -0700588void InputReader::requestTimeoutAtTime(nsecs_t when) {
589 if (when < mNextTimeout) {
590 mNextTimeout = when;
591 }
592}
593
Jeff Brown6d0fec22010-07-23 21:28:06 -0700594void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
595 { // acquire state lock
596 AutoMutex _l(mStateLock);
597
598 *outConfiguration = mInputConfiguration;
599 } // release state lock
600}
601
602status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
603 { // acquire device registry reader lock
604 RWLock::AutoRLock _rl(mDeviceRegistryLock);
605
606 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
607 if (deviceIndex < 0) {
608 return NAME_NOT_FOUND;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700609 }
610
Jeff Brown6d0fec22010-07-23 21:28:06 -0700611 InputDevice* device = mDevices.valueAt(deviceIndex);
612 if (device->isIgnored()) {
613 return NAME_NOT_FOUND;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700614 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700615
616 device->getDeviceInfo(outDeviceInfo);
617 return OK;
618 } // release device registy reader lock
619}
620
621void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
622 outDeviceIds.clear();
623
624 { // acquire device registry reader lock
625 RWLock::AutoRLock _rl(mDeviceRegistryLock);
626
627 size_t numDevices = mDevices.size();
628 for (size_t i = 0; i < numDevices; i++) {
629 InputDevice* device = mDevices.valueAt(i);
630 if (! device->isIgnored()) {
631 outDeviceIds.add(device->getId());
632 }
633 }
634 } // release device registy reader lock
635}
636
637int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
638 int32_t keyCode) {
639 return getState(deviceId, sourceMask, keyCode, & InputDevice::getKeyCodeState);
640}
641
642int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
643 int32_t scanCode) {
644 return getState(deviceId, sourceMask, scanCode, & InputDevice::getScanCodeState);
645}
646
647int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
648 return getState(deviceId, sourceMask, switchCode, & InputDevice::getSwitchState);
649}
650
651int32_t InputReader::getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
652 GetStateFunc getStateFunc) {
653 { // acquire device registry reader lock
654 RWLock::AutoRLock _rl(mDeviceRegistryLock);
655
656 int32_t result = AKEY_STATE_UNKNOWN;
657 if (deviceId >= 0) {
658 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
659 if (deviceIndex >= 0) {
660 InputDevice* device = mDevices.valueAt(deviceIndex);
661 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
662 result = (device->*getStateFunc)(sourceMask, code);
663 }
664 }
665 } else {
666 size_t numDevices = mDevices.size();
667 for (size_t i = 0; i < numDevices; i++) {
668 InputDevice* device = mDevices.valueAt(i);
669 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
670 result = (device->*getStateFunc)(sourceMask, code);
671 if (result >= AKEY_STATE_DOWN) {
672 return result;
673 }
674 }
675 }
676 }
677 return result;
678 } // release device registy reader lock
679}
680
681bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
682 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
683 memset(outFlags, 0, numCodes);
684 return markSupportedKeyCodes(deviceId, sourceMask, numCodes, keyCodes, outFlags);
685}
686
687bool InputReader::markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
688 const int32_t* keyCodes, uint8_t* outFlags) {
689 { // acquire device registry reader lock
690 RWLock::AutoRLock _rl(mDeviceRegistryLock);
691 bool result = false;
692 if (deviceId >= 0) {
693 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
694 if (deviceIndex >= 0) {
695 InputDevice* device = mDevices.valueAt(deviceIndex);
696 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
697 result = device->markSupportedKeyCodes(sourceMask,
698 numCodes, keyCodes, outFlags);
699 }
700 }
701 } else {
702 size_t numDevices = mDevices.size();
703 for (size_t i = 0; i < numDevices; i++) {
704 InputDevice* device = mDevices.valueAt(i);
705 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
706 result |= device->markSupportedKeyCodes(sourceMask,
707 numCodes, keyCodes, outFlags);
708 }
709 }
710 }
711 return result;
712 } // release device registy reader lock
713}
714
Jeff Brown474dcb52011-06-14 20:22:50 -0700715void InputReader::requestRefreshConfiguration(uint32_t changes) {
716 if (changes) {
717 bool needWake;
718 { // acquire lock
719 AutoMutex _l(mStateLock);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700720
Jeff Brown474dcb52011-06-14 20:22:50 -0700721 needWake = !mConfigurationChangesToRefresh;
722 mConfigurationChangesToRefresh |= changes;
723 } // release lock
724
725 if (needWake) {
726 mEventHub->wake();
727 }
728 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700729}
730
Jeff Brownb88102f2010-09-08 11:49:43 -0700731void InputReader::dump(String8& dump) {
Jeff Brownf2f487182010-10-01 17:46:21 -0700732 mEventHub->dump(dump);
733 dump.append("\n");
734
735 dump.append("Input Reader State:\n");
736
Jeff Brownef3d7e82010-09-30 14:33:04 -0700737 { // acquire device registry reader lock
738 RWLock::AutoRLock _rl(mDeviceRegistryLock);
Jeff Brownb88102f2010-09-08 11:49:43 -0700739
Jeff Brownef3d7e82010-09-30 14:33:04 -0700740 for (size_t i = 0; i < mDevices.size(); i++) {
741 mDevices.valueAt(i)->dump(dump);
Jeff Brownb88102f2010-09-08 11:49:43 -0700742 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700743 } // release device registy reader lock
Jeff Brown214eaf42011-05-26 19:17:02 -0700744
745 dump.append(INDENT "Configuration:\n");
746 dump.append(INDENT2 "ExcludedDeviceNames: [");
747 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
748 if (i != 0) {
749 dump.append(", ");
750 }
751 dump.append(mConfig.excludedDeviceNames.itemAt(i).string());
752 }
753 dump.append("]\n");
Jeff Brown214eaf42011-05-26 19:17:02 -0700754 dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
755 mConfig.virtualKeyQuietTime * 0.000001f);
756
Jeff Brown19c97d462011-06-01 12:33:19 -0700757 dump.appendFormat(INDENT2 "PointerVelocityControlParameters: "
758 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
759 mConfig.pointerVelocityControlParameters.scale,
760 mConfig.pointerVelocityControlParameters.lowThreshold,
761 mConfig.pointerVelocityControlParameters.highThreshold,
762 mConfig.pointerVelocityControlParameters.acceleration);
763
764 dump.appendFormat(INDENT2 "WheelVelocityControlParameters: "
765 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
766 mConfig.wheelVelocityControlParameters.scale,
767 mConfig.wheelVelocityControlParameters.lowThreshold,
768 mConfig.wheelVelocityControlParameters.highThreshold,
769 mConfig.wheelVelocityControlParameters.acceleration);
770
Jeff Brown214eaf42011-05-26 19:17:02 -0700771 dump.appendFormat(INDENT2 "PointerGesture:\n");
Jeff Brown474dcb52011-06-14 20:22:50 -0700772 dump.appendFormat(INDENT3 "Enabled: %s\n",
773 toString(mConfig.pointerGesturesEnabled));
Jeff Brown214eaf42011-05-26 19:17:02 -0700774 dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n",
775 mConfig.pointerGestureQuietInterval * 0.000001f);
776 dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
777 mConfig.pointerGestureDragMinSwitchSpeed);
778 dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n",
779 mConfig.pointerGestureTapInterval * 0.000001f);
780 dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n",
781 mConfig.pointerGestureTapDragInterval * 0.000001f);
782 dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n",
783 mConfig.pointerGestureTapSlop);
784 dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
785 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700786 dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
787 mConfig.pointerGestureMultitouchMinDistance);
Jeff Brown214eaf42011-05-26 19:17:02 -0700788 dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
789 mConfig.pointerGestureSwipeTransitionAngleCosine);
790 dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
791 mConfig.pointerGestureSwipeMaxWidthRatio);
792 dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n",
793 mConfig.pointerGestureMovementSpeedRatio);
794 dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n",
795 mConfig.pointerGestureZoomSpeedRatio);
Jeff Brownb88102f2010-09-08 11:49:43 -0700796}
797
Jeff Brown6d0fec22010-07-23 21:28:06 -0700798
799// --- InputReaderThread ---
800
801InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
802 Thread(/*canCallJava*/ true), mReader(reader) {
803}
804
805InputReaderThread::~InputReaderThread() {
806}
807
808bool InputReaderThread::threadLoop() {
809 mReader->loopOnce();
810 return true;
811}
812
813
814// --- InputDevice ---
815
816InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) :
Jeff Brown80fd47c2011-05-24 01:07:44 -0700817 mContext(context), mId(id), mName(name), mSources(0),
818 mIsExternal(false), mDropUntilNextSync(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700819}
820
821InputDevice::~InputDevice() {
822 size_t numMappers = mMappers.size();
823 for (size_t i = 0; i < numMappers; i++) {
824 delete mMappers[i];
825 }
826 mMappers.clear();
827}
828
Jeff Brownef3d7e82010-09-30 14:33:04 -0700829void InputDevice::dump(String8& dump) {
830 InputDeviceInfo deviceInfo;
831 getDeviceInfo(& deviceInfo);
832
Jeff Brown90655042010-12-02 13:50:46 -0800833 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700834 deviceInfo.getName().string());
Jeff Brown56194eb2011-03-02 19:23:13 -0800835 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700836 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
837 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800838
Jeff Brownefd32662011-03-08 15:13:06 -0800839 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800840 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700841 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800842 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800843 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
844 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800845 char name[32];
846 if (label) {
847 strncpy(name, label, sizeof(name));
848 name[sizeof(name) - 1] = '\0';
849 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800850 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800851 }
Jeff Brownefd32662011-03-08 15:13:06 -0800852 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
853 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
854 name, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Browncc0c1592011-02-19 05:07:28 -0800855 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700856 }
857
858 size_t numMappers = mMappers.size();
859 for (size_t i = 0; i < numMappers; i++) {
860 InputMapper* mapper = mMappers[i];
861 mapper->dump(dump);
862 }
863}
864
Jeff Brown6d0fec22010-07-23 21:28:06 -0700865void InputDevice::addMapper(InputMapper* mapper) {
866 mMappers.add(mapper);
867}
868
Jeff Brown474dcb52011-06-14 20:22:50 -0700869void InputDevice::configure(const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700870 mSources = 0;
871
Jeff Brown474dcb52011-06-14 20:22:50 -0700872 if (!isIgnored()) {
873 if (!changes) { // first time only
874 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
875 }
876
877 size_t numMappers = mMappers.size();
878 for (size_t i = 0; i < numMappers; i++) {
879 InputMapper* mapper = mMappers[i];
880 mapper->configure(config, changes);
881 mSources |= mapper->getSources();
882 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700883 }
884}
885
Jeff Brown6d0fec22010-07-23 21:28:06 -0700886void InputDevice::reset() {
887 size_t numMappers = mMappers.size();
888 for (size_t i = 0; i < numMappers; i++) {
889 InputMapper* mapper = mMappers[i];
890 mapper->reset();
891 }
892}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700893
Jeff Brownb7198742011-03-18 18:14:26 -0700894void InputDevice::process(const RawEvent* rawEvents, size_t count) {
895 // Process all of the events in order for each mapper.
896 // We cannot simply ask each mapper to process them in bulk because mappers may
897 // have side-effects that must be interleaved. For example, joystick movement events and
898 // gamepad button presses are handled by different mappers but they should be dispatched
899 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700900 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700901 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
902#if DEBUG_RAW_EVENTS
903 LOGD("Input event: device=%d type=0x%04x scancode=0x%04x "
Jeff Brown2e45fb62011-06-29 21:19:05 -0700904 "keycode=0x%04x value=0x%08x flags=0x%08x",
Jeff Brownb7198742011-03-18 18:14:26 -0700905 rawEvent->deviceId, rawEvent->type, rawEvent->scanCode, rawEvent->keyCode,
906 rawEvent->value, rawEvent->flags);
907#endif
908
Jeff Brown80fd47c2011-05-24 01:07:44 -0700909 if (mDropUntilNextSync) {
910 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
911 mDropUntilNextSync = false;
912#if DEBUG_RAW_EVENTS
913 LOGD("Recovered from input event buffer overrun.");
914#endif
915 } else {
916#if DEBUG_RAW_EVENTS
917 LOGD("Dropped input event while waiting for next input sync.");
918#endif
919 }
920 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_DROPPED) {
921 LOGI("Detected input event buffer overrun for device %s.", mName.string());
922 mDropUntilNextSync = true;
923 reset();
924 } else {
925 for (size_t i = 0; i < numMappers; i++) {
926 InputMapper* mapper = mMappers[i];
927 mapper->process(rawEvent);
928 }
Jeff Brownb7198742011-03-18 18:14:26 -0700929 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700930 }
931}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700932
Jeff Brownaa3855d2011-03-17 01:34:19 -0700933void InputDevice::timeoutExpired(nsecs_t when) {
934 size_t numMappers = mMappers.size();
935 for (size_t i = 0; i < numMappers; i++) {
936 InputMapper* mapper = mMappers[i];
937 mapper->timeoutExpired(when);
938 }
939}
940
Jeff Brown6d0fec22010-07-23 21:28:06 -0700941void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
942 outDeviceInfo->initialize(mId, mName);
943
944 size_t numMappers = mMappers.size();
945 for (size_t i = 0; i < numMappers; i++) {
946 InputMapper* mapper = mMappers[i];
947 mapper->populateDeviceInfo(outDeviceInfo);
948 }
949}
950
951int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
952 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
953}
954
955int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
956 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
957}
958
959int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
960 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
961}
962
963int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
964 int32_t result = AKEY_STATE_UNKNOWN;
965 size_t numMappers = mMappers.size();
966 for (size_t i = 0; i < numMappers; i++) {
967 InputMapper* mapper = mMappers[i];
968 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
969 result = (mapper->*getStateFunc)(sourceMask, code);
970 if (result >= AKEY_STATE_DOWN) {
971 return result;
972 }
973 }
974 }
975 return result;
976}
977
978bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
979 const int32_t* keyCodes, uint8_t* outFlags) {
980 bool result = false;
981 size_t numMappers = mMappers.size();
982 for (size_t i = 0; i < numMappers; i++) {
983 InputMapper* mapper = mMappers[i];
984 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
985 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
986 }
987 }
988 return result;
989}
990
991int32_t InputDevice::getMetaState() {
992 int32_t result = 0;
993 size_t numMappers = mMappers.size();
994 for (size_t i = 0; i < numMappers; i++) {
995 InputMapper* mapper = mMappers[i];
996 result |= mapper->getMetaState();
997 }
998 return result;
999}
1000
Jeff Brown05dc66a2011-03-02 14:41:58 -08001001void InputDevice::fadePointer() {
1002 size_t numMappers = mMappers.size();
1003 for (size_t i = 0; i < numMappers; i++) {
1004 InputMapper* mapper = mMappers[i];
1005 mapper->fadePointer();
1006 }
1007}
1008
Jeff Brown6d0fec22010-07-23 21:28:06 -07001009
1010// --- InputMapper ---
1011
1012InputMapper::InputMapper(InputDevice* device) :
1013 mDevice(device), mContext(device->getContext()) {
1014}
1015
1016InputMapper::~InputMapper() {
1017}
1018
1019void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1020 info->addSource(getSources());
1021}
1022
Jeff Brownef3d7e82010-09-30 14:33:04 -07001023void InputMapper::dump(String8& dump) {
1024}
1025
Jeff Brown474dcb52011-06-14 20:22:50 -07001026void InputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001027}
1028
1029void InputMapper::reset() {
1030}
1031
Jeff Brownaa3855d2011-03-17 01:34:19 -07001032void InputMapper::timeoutExpired(nsecs_t when) {
1033}
1034
Jeff Brown6d0fec22010-07-23 21:28:06 -07001035int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1036 return AKEY_STATE_UNKNOWN;
1037}
1038
1039int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1040 return AKEY_STATE_UNKNOWN;
1041}
1042
1043int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1044 return AKEY_STATE_UNKNOWN;
1045}
1046
1047bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1048 const int32_t* keyCodes, uint8_t* outFlags) {
1049 return false;
1050}
1051
1052int32_t InputMapper::getMetaState() {
1053 return 0;
1054}
1055
Jeff Brown05dc66a2011-03-02 14:41:58 -08001056void InputMapper::fadePointer() {
1057}
1058
Jeff Browncb1404e2011-01-15 18:14:15 -08001059void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1060 const RawAbsoluteAxisInfo& axis, const char* name) {
1061 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001062 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1063 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001064 } else {
1065 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1066 }
1067}
1068
Jeff Brown6d0fec22010-07-23 21:28:06 -07001069
1070// --- SwitchInputMapper ---
1071
1072SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1073 InputMapper(device) {
1074}
1075
1076SwitchInputMapper::~SwitchInputMapper() {
1077}
1078
1079uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001080 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001081}
1082
1083void SwitchInputMapper::process(const RawEvent* rawEvent) {
1084 switch (rawEvent->type) {
1085 case EV_SW:
1086 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
1087 break;
1088 }
1089}
1090
1091void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownb6997262010-10-08 22:31:17 -07001092 getDispatcher()->notifySwitch(when, switchCode, switchValue, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001093}
1094
1095int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1096 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1097}
1098
1099
1100// --- KeyboardInputMapper ---
1101
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001102KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001103 uint32_t source, int32_t keyboardType) :
1104 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001105 mKeyboardType(keyboardType) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001106 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001107}
1108
1109KeyboardInputMapper::~KeyboardInputMapper() {
1110}
1111
Jeff Brown6328cdc2010-07-29 18:18:33 -07001112void KeyboardInputMapper::initializeLocked() {
1113 mLocked.metaState = AMETA_NONE;
1114 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001115}
1116
1117uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001118 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001119}
1120
1121void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1122 InputMapper::populateDeviceInfo(info);
1123
1124 info->setKeyboardType(mKeyboardType);
1125}
1126
Jeff Brownef3d7e82010-09-30 14:33:04 -07001127void KeyboardInputMapper::dump(String8& dump) {
1128 { // acquire lock
1129 AutoMutex _l(mLock);
1130 dump.append(INDENT2 "Keyboard Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001131 dumpParameters(dump);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001132 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
1133 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mLocked.keyDowns.size());
1134 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mLocked.metaState);
1135 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1136 } // release lock
1137}
1138
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001139
Jeff Brown474dcb52011-06-14 20:22:50 -07001140void KeyboardInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
1141 InputMapper::configure(config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001142
Jeff Brown474dcb52011-06-14 20:22:50 -07001143 if (!changes) { // first time only
1144 // Configure basic parameters.
1145 configureParameters();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001146
Jeff Brown474dcb52011-06-14 20:22:50 -07001147 // Reset LEDs.
1148 {
1149 AutoMutex _l(mLock);
1150 resetLedStateLocked();
1151 }
Jeff Brown49ed71d2010-12-06 17:13:33 -08001152 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001153}
1154
1155void KeyboardInputMapper::configureParameters() {
1156 mParameters.orientationAware = false;
1157 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
1158 mParameters.orientationAware);
1159
1160 mParameters.associatedDisplayId = mParameters.orientationAware ? 0 : -1;
1161}
1162
1163void KeyboardInputMapper::dumpParameters(String8& dump) {
1164 dump.append(INDENT3 "Parameters:\n");
1165 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1166 mParameters.associatedDisplayId);
1167 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1168 toString(mParameters.orientationAware));
1169}
1170
Jeff Brown6d0fec22010-07-23 21:28:06 -07001171void KeyboardInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001172 for (;;) {
1173 int32_t keyCode, scanCode;
1174 { // acquire lock
1175 AutoMutex _l(mLock);
1176
1177 // Synthesize key up event on reset if keys are currently down.
1178 if (mLocked.keyDowns.isEmpty()) {
1179 initializeLocked();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001180 resetLedStateLocked();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001181 break; // done
1182 }
1183
1184 const KeyDown& keyDown = mLocked.keyDowns.top();
1185 keyCode = keyDown.keyCode;
1186 scanCode = keyDown.scanCode;
1187 } // release lock
1188
Jeff Brown6d0fec22010-07-23 21:28:06 -07001189 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001190 processKey(when, false, keyCode, scanCode, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001191 }
1192
1193 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001194 getContext()->updateGlobalMetaState();
1195}
1196
1197void KeyboardInputMapper::process(const RawEvent* rawEvent) {
1198 switch (rawEvent->type) {
1199 case EV_KEY: {
1200 int32_t scanCode = rawEvent->scanCode;
1201 if (isKeyboardOrGamepadKey(scanCode)) {
1202 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
1203 rawEvent->flags);
1204 }
1205 break;
1206 }
1207 }
1208}
1209
1210bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
1211 return scanCode < BTN_MOUSE
1212 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001213 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08001214 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001215}
1216
Jeff Brown6328cdc2010-07-29 18:18:33 -07001217void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
1218 int32_t scanCode, uint32_t policyFlags) {
1219 int32_t newMetaState;
1220 nsecs_t downTime;
1221 bool metaStateChanged = false;
1222
1223 { // acquire lock
1224 AutoMutex _l(mLock);
1225
1226 if (down) {
1227 // Rotate key codes according to orientation if needed.
1228 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001229 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001230 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001231 if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1232 NULL, NULL, & orientation)) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001233 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001234 }
1235
1236 keyCode = rotateKeyCode(keyCode, orientation);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001237 }
1238
Jeff Brown6328cdc2010-07-29 18:18:33 -07001239 // Add key down.
1240 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
1241 if (keyDownIndex >= 0) {
1242 // key repeat, be sure to use same keycode as before in case of rotation
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001243 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001244 } else {
1245 // key down
Jeff Brownfe508922011-01-18 15:10:10 -08001246 if ((policyFlags & POLICY_FLAG_VIRTUAL)
1247 && mContext->shouldDropVirtualKey(when,
1248 getDevice(), keyCode, scanCode)) {
1249 return;
1250 }
1251
Jeff Brown6328cdc2010-07-29 18:18:33 -07001252 mLocked.keyDowns.push();
1253 KeyDown& keyDown = mLocked.keyDowns.editTop();
1254 keyDown.keyCode = keyCode;
1255 keyDown.scanCode = scanCode;
1256 }
1257
1258 mLocked.downTime = when;
1259 } else {
1260 // Remove key down.
1261 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
1262 if (keyDownIndex >= 0) {
1263 // key up, be sure to use same keycode as before in case of rotation
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001264 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001265 mLocked.keyDowns.removeAt(size_t(keyDownIndex));
1266 } else {
1267 // key was not actually down
1268 LOGI("Dropping key up from device %s because the key was not down. "
1269 "keyCode=%d, scanCode=%d",
1270 getDeviceName().string(), keyCode, scanCode);
1271 return;
1272 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001273 }
1274
Jeff Brown6328cdc2010-07-29 18:18:33 -07001275 int32_t oldMetaState = mLocked.metaState;
1276 newMetaState = updateMetaState(keyCode, down, oldMetaState);
1277 if (oldMetaState != newMetaState) {
1278 mLocked.metaState = newMetaState;
1279 metaStateChanged = true;
Jeff Brown497a92c2010-09-12 17:55:08 -07001280 updateLedStateLocked(false);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001281 }
Jeff Brownfd0358292010-06-30 16:10:35 -07001282
Jeff Brown6328cdc2010-07-29 18:18:33 -07001283 downTime = mLocked.downTime;
1284 } // release lock
1285
Jeff Brown56194eb2011-03-02 19:23:13 -08001286 // Key down on external an keyboard should wake the device.
1287 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
1288 // For internal keyboards, the key layout file should specify the policy flags for
1289 // each wake key individually.
1290 // TODO: Use the input device configuration to control this behavior more finely.
1291 if (down && getDevice()->isExternal()
1292 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1293 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1294 }
1295
Jeff Brown6328cdc2010-07-29 18:18:33 -07001296 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001297 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001298 }
1299
Jeff Brown05dc66a2011-03-02 14:41:58 -08001300 if (down && !isMetaKey(keyCode)) {
1301 getContext()->fadePointer();
1302 }
1303
Jeff Brownefd32662011-03-08 15:13:06 -08001304 getDispatcher()->notifyKey(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001305 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1306 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001307}
1308
Jeff Brown6328cdc2010-07-29 18:18:33 -07001309ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) {
1310 size_t n = mLocked.keyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001311 for (size_t i = 0; i < n; i++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001312 if (mLocked.keyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001313 return i;
1314 }
1315 }
1316 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001317}
1318
Jeff Brown6d0fec22010-07-23 21:28:06 -07001319int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1320 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1321}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001322
Jeff Brown6d0fec22010-07-23 21:28:06 -07001323int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1324 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1325}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001326
Jeff Brown6d0fec22010-07-23 21:28:06 -07001327bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1328 const int32_t* keyCodes, uint8_t* outFlags) {
1329 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1330}
1331
1332int32_t KeyboardInputMapper::getMetaState() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001333 { // acquire lock
1334 AutoMutex _l(mLock);
1335 return mLocked.metaState;
1336 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001337}
1338
Jeff Brown49ed71d2010-12-06 17:13:33 -08001339void KeyboardInputMapper::resetLedStateLocked() {
1340 initializeLedStateLocked(mLocked.capsLockLedState, LED_CAPSL);
1341 initializeLedStateLocked(mLocked.numLockLedState, LED_NUML);
1342 initializeLedStateLocked(mLocked.scrollLockLedState, LED_SCROLLL);
1343
1344 updateLedStateLocked(true);
1345}
1346
1347void KeyboardInputMapper::initializeLedStateLocked(LockedState::LedState& ledState, int32_t led) {
1348 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
1349 ledState.on = false;
1350}
1351
Jeff Brown497a92c2010-09-12 17:55:08 -07001352void KeyboardInputMapper::updateLedStateLocked(bool reset) {
1353 updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001354 AMETA_CAPS_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001355 updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001356 AMETA_NUM_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001357 updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001358 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001359}
1360
1361void KeyboardInputMapper::updateLedStateForModifierLocked(LockedState::LedState& ledState,
1362 int32_t led, int32_t modifier, bool reset) {
1363 if (ledState.avail) {
1364 bool desiredState = (mLocked.metaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001365 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001366 getEventHub()->setLedState(getDeviceId(), led, desiredState);
1367 ledState.on = desiredState;
1368 }
1369 }
1370}
1371
Jeff Brown6d0fec22010-07-23 21:28:06 -07001372
Jeff Brown83c09682010-12-23 17:50:18 -08001373// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07001374
Jeff Brown83c09682010-12-23 17:50:18 -08001375CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001376 InputMapper(device) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001377 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001378}
1379
Jeff Brown83c09682010-12-23 17:50:18 -08001380CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001381}
1382
Jeff Brown83c09682010-12-23 17:50:18 -08001383uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001384 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001385}
1386
Jeff Brown83c09682010-12-23 17:50:18 -08001387void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001388 InputMapper::populateDeviceInfo(info);
1389
Jeff Brown83c09682010-12-23 17:50:18 -08001390 if (mParameters.mode == Parameters::MODE_POINTER) {
1391 float minX, minY, maxX, maxY;
1392 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08001393 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
1394 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08001395 }
1396 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08001397 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
1398 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08001399 }
Jeff Brownefd32662011-03-08 15:13:06 -08001400 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001401
1402 if (mHaveVWheel) {
Jeff Brownefd32662011-03-08 15:13:06 -08001403 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001404 }
1405 if (mHaveHWheel) {
Jeff Brownefd32662011-03-08 15:13:06 -08001406 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001407 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001408}
1409
Jeff Brown83c09682010-12-23 17:50:18 -08001410void CursorInputMapper::dump(String8& dump) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07001411 { // acquire lock
1412 AutoMutex _l(mLock);
Jeff Brown83c09682010-12-23 17:50:18 -08001413 dump.append(INDENT2 "Cursor Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001414 dumpParameters(dump);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001415 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
1416 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001417 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1418 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001419 dump.appendFormat(INDENT3 "HaveVWheel: %s\n", toString(mHaveVWheel));
1420 dump.appendFormat(INDENT3 "HaveHWheel: %s\n", toString(mHaveHWheel));
1421 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
1422 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brownefd32662011-03-08 15:13:06 -08001423 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mLocked.buttonState);
1424 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mLocked.buttonState)));
Jeff Brownef3d7e82010-09-30 14:33:04 -07001425 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1426 } // release lock
1427}
1428
Jeff Brown474dcb52011-06-14 20:22:50 -07001429void CursorInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
1430 InputMapper::configure(config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001431
Jeff Brown474dcb52011-06-14 20:22:50 -07001432 if (!changes) { // first time only
1433 // Configure basic parameters.
1434 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08001435
Jeff Brown474dcb52011-06-14 20:22:50 -07001436 // Configure device mode.
1437 switch (mParameters.mode) {
1438 case Parameters::MODE_POINTER:
1439 mSource = AINPUT_SOURCE_MOUSE;
1440 mXPrecision = 1.0f;
1441 mYPrecision = 1.0f;
1442 mXScale = 1.0f;
1443 mYScale = 1.0f;
1444 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
1445 break;
1446 case Parameters::MODE_NAVIGATION:
1447 mSource = AINPUT_SOURCE_TRACKBALL;
1448 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1449 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1450 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1451 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1452 break;
1453 }
1454
1455 mVWheelScale = 1.0f;
1456 mHWheelScale = 1.0f;
1457
1458 mHaveVWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_WHEEL);
1459 mHaveHWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_HWHEEL);
Jeff Brown83c09682010-12-23 17:50:18 -08001460 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001461
Jeff Brown474dcb52011-06-14 20:22:50 -07001462 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
1463 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
1464 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
1465 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
1466 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001467}
1468
Jeff Brown83c09682010-12-23 17:50:18 -08001469void CursorInputMapper::configureParameters() {
1470 mParameters.mode = Parameters::MODE_POINTER;
1471 String8 cursorModeString;
1472 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
1473 if (cursorModeString == "navigation") {
1474 mParameters.mode = Parameters::MODE_NAVIGATION;
1475 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
1476 LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
1477 }
1478 }
1479
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001480 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08001481 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001482 mParameters.orientationAware);
1483
Jeff Brown83c09682010-12-23 17:50:18 -08001484 mParameters.associatedDisplayId = mParameters.mode == Parameters::MODE_POINTER
1485 || mParameters.orientationAware ? 0 : -1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001486}
1487
Jeff Brown83c09682010-12-23 17:50:18 -08001488void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001489 dump.append(INDENT3 "Parameters:\n");
1490 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1491 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08001492
1493 switch (mParameters.mode) {
1494 case Parameters::MODE_POINTER:
1495 dump.append(INDENT4 "Mode: pointer\n");
1496 break;
1497 case Parameters::MODE_NAVIGATION:
1498 dump.append(INDENT4 "Mode: navigation\n");
1499 break;
1500 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07001501 LOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08001502 }
1503
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001504 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1505 toString(mParameters.orientationAware));
1506}
1507
Jeff Brown83c09682010-12-23 17:50:18 -08001508void CursorInputMapper::initializeLocked() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001509 mAccumulator.clear();
1510
Jeff Brownefd32662011-03-08 15:13:06 -08001511 mLocked.buttonState = 0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001512 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001513}
1514
Jeff Brown83c09682010-12-23 17:50:18 -08001515void CursorInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001516 for (;;) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001517 int32_t buttonState;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001518 { // acquire lock
1519 AutoMutex _l(mLock);
1520
Jeff Brownefd32662011-03-08 15:13:06 -08001521 buttonState = mLocked.buttonState;
1522 if (!buttonState) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001523 initializeLocked();
1524 break; // done
1525 }
1526 } // release lock
1527
Jeff Brown19c97d462011-06-01 12:33:19 -07001528 // Reset velocity.
1529 mPointerVelocityControl.reset();
1530 mWheelXVelocityControl.reset();
1531 mWheelYVelocityControl.reset();
1532
Jeff Brown83c09682010-12-23 17:50:18 -08001533 // Synthesize button up event on reset.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001534 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownefd32662011-03-08 15:13:06 -08001535 mAccumulator.clear();
1536 mAccumulator.buttonDown = 0;
1537 mAccumulator.buttonUp = buttonState;
1538 mAccumulator.fields = Accumulator::FIELD_BUTTONS;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001539 sync(when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001540 }
1541
Jeff Brown6d0fec22010-07-23 21:28:06 -07001542 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001543}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001544
Jeff Brown83c09682010-12-23 17:50:18 -08001545void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001546 switch (rawEvent->type) {
Jeff Brownefd32662011-03-08 15:13:06 -08001547 case EV_KEY: {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001548 int32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode);
Jeff Brownefd32662011-03-08 15:13:06 -08001549 if (buttonState) {
1550 if (rawEvent->value) {
1551 mAccumulator.buttonDown = buttonState;
1552 mAccumulator.buttonUp = 0;
1553 } else {
1554 mAccumulator.buttonDown = 0;
1555 mAccumulator.buttonUp = buttonState;
1556 }
1557 mAccumulator.fields |= Accumulator::FIELD_BUTTONS;
1558
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001559 // Sync now since BTN_MOUSE is not necessarily followed by SYN_REPORT and
1560 // we need to ensure that we report the up/down promptly.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001561 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001562 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001563 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001564 break;
Jeff Brownefd32662011-03-08 15:13:06 -08001565 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001566
Jeff Brown6d0fec22010-07-23 21:28:06 -07001567 case EV_REL:
1568 switch (rawEvent->scanCode) {
1569 case REL_X:
1570 mAccumulator.fields |= Accumulator::FIELD_REL_X;
1571 mAccumulator.relX = rawEvent->value;
1572 break;
1573 case REL_Y:
1574 mAccumulator.fields |= Accumulator::FIELD_REL_Y;
1575 mAccumulator.relY = rawEvent->value;
1576 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001577 case REL_WHEEL:
1578 mAccumulator.fields |= Accumulator::FIELD_REL_WHEEL;
1579 mAccumulator.relWheel = rawEvent->value;
1580 break;
1581 case REL_HWHEEL:
1582 mAccumulator.fields |= Accumulator::FIELD_REL_HWHEEL;
1583 mAccumulator.relHWheel = rawEvent->value;
1584 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001585 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001586 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001587
Jeff Brown6d0fec22010-07-23 21:28:06 -07001588 case EV_SYN:
1589 switch (rawEvent->scanCode) {
1590 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001591 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001592 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001593 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001594 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001595 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001596}
1597
Jeff Brown83c09682010-12-23 17:50:18 -08001598void CursorInputMapper::sync(nsecs_t when) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001599 uint32_t fields = mAccumulator.fields;
1600 if (fields == 0) {
1601 return; // no new state changes, so nothing to do
1602 }
1603
Jeff Brown9626b142011-03-03 02:09:54 -08001604 int32_t motionEventAction;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001605 int32_t lastButtonState, currentButtonState;
1606 PointerProperties pointerProperties;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001607 PointerCoords pointerCoords;
1608 nsecs_t downTime;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001609 float vscroll, hscroll;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001610 { // acquire lock
1611 AutoMutex _l(mLock);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001612
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001613 lastButtonState = mLocked.buttonState;
1614
Jeff Brownefd32662011-03-08 15:13:06 -08001615 bool down, downChanged;
1616 bool wasDown = isPointerDown(mLocked.buttonState);
1617 bool buttonsChanged = fields & Accumulator::FIELD_BUTTONS;
1618 if (buttonsChanged) {
1619 mLocked.buttonState = (mLocked.buttonState | mAccumulator.buttonDown)
1620 & ~mAccumulator.buttonUp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001621
Jeff Brownefd32662011-03-08 15:13:06 -08001622 down = isPointerDown(mLocked.buttonState);
1623
1624 if (!wasDown && down) {
1625 mLocked.downTime = when;
1626 downChanged = true;
1627 } else if (wasDown && !down) {
1628 downChanged = true;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001629 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08001630 downChanged = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001631 }
Jeff Brownefd32662011-03-08 15:13:06 -08001632 } else {
1633 down = wasDown;
1634 downChanged = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001635 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001636
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001637 currentButtonState = mLocked.buttonState;
1638
Jeff Brown6328cdc2010-07-29 18:18:33 -07001639 downTime = mLocked.downTime;
Jeff Brown83c09682010-12-23 17:50:18 -08001640 float deltaX = fields & Accumulator::FIELD_REL_X ? mAccumulator.relX * mXScale : 0.0f;
1641 float deltaY = fields & Accumulator::FIELD_REL_Y ? mAccumulator.relY * mYScale : 0.0f;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001642
Jeff Brown6328cdc2010-07-29 18:18:33 -07001643 if (downChanged) {
Jeff Brownefd32662011-03-08 15:13:06 -08001644 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
1645 } else if (down || mPointerController == NULL) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001646 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browncc0c1592011-02-19 05:07:28 -08001647 } else {
1648 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001649 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001650
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001651 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
Jeff Brown83c09682010-12-23 17:50:18 -08001652 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001653 // Rotate motion based on display orientation if needed.
1654 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1655 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001656 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1657 NULL, NULL, & orientation)) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001658 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001659 }
1660
Jeff Brown612891e2011-07-15 20:44:17 -07001661 rotateDelta(orientation, &deltaX, &deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001662 }
Jeff Brown83c09682010-12-23 17:50:18 -08001663
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001664 pointerProperties.clear();
1665 pointerProperties.id = 0;
1666 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
1667
1668 pointerCoords.clear();
1669
Jeff Brown2352b972011-04-12 22:39:53 -07001670 if (mHaveVWheel && (fields & Accumulator::FIELD_REL_WHEEL)) {
1671 vscroll = mAccumulator.relWheel;
1672 } else {
1673 vscroll = 0;
1674 }
Jeff Brown19c97d462011-06-01 12:33:19 -07001675 mWheelYVelocityControl.move(when, NULL, &vscroll);
1676
Jeff Brown2352b972011-04-12 22:39:53 -07001677 if (mHaveHWheel && (fields & Accumulator::FIELD_REL_HWHEEL)) {
1678 hscroll = mAccumulator.relHWheel;
1679 } else {
1680 hscroll = 0;
1681 }
Jeff Brown19c97d462011-06-01 12:33:19 -07001682 mWheelXVelocityControl.move(when, &hscroll, NULL);
1683
1684 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown2352b972011-04-12 22:39:53 -07001685
Jeff Brown83c09682010-12-23 17:50:18 -08001686 if (mPointerController != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -07001687 if (deltaX != 0 || deltaY != 0 || vscroll != 0 || hscroll != 0
1688 || buttonsChanged) {
1689 mPointerController->setPresentation(
1690 PointerControllerInterface::PRESENTATION_POINTER);
1691
1692 if (deltaX != 0 || deltaY != 0) {
1693 mPointerController->move(deltaX, deltaY);
1694 }
1695
1696 if (buttonsChanged) {
1697 mPointerController->setButtonState(mLocked.buttonState);
1698 }
1699
Jeff Brown538881e2011-05-25 18:23:38 -07001700 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08001701 }
Jeff Brownefd32662011-03-08 15:13:06 -08001702
Jeff Brown91c69ab2011-02-14 17:03:18 -08001703 float x, y;
1704 mPointerController->getPosition(&x, &y);
Jeff Brownebbd5d12011-02-17 13:01:34 -08001705 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
1706 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown83c09682010-12-23 17:50:18 -08001707 } else {
Jeff Brownebbd5d12011-02-17 13:01:34 -08001708 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
1709 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
Jeff Brown83c09682010-12-23 17:50:18 -08001710 }
1711
Jeff Brownefd32662011-03-08 15:13:06 -08001712 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001713 } // release lock
1714
Jeff Brown56194eb2011-03-02 19:23:13 -08001715 // Moving an external trackball or mouse should wake the device.
1716 // We don't do this for internal cursor devices to prevent them from waking up
1717 // the device in your pocket.
1718 // TODO: Use the input device configuration to control this behavior more finely.
1719 uint32_t policyFlags = 0;
1720 if (getDevice()->isExternal()) {
1721 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1722 }
1723
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001724 // Synthesize key down from buttons if needed.
1725 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
1726 policyFlags, lastButtonState, currentButtonState);
1727
1728 // Send motion event.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001729 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownefd32662011-03-08 15:13:06 -08001730 getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags,
Jeff Browna6111372011-07-14 21:48:23 -07001731 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001732 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownb6997262010-10-08 22:31:17 -07001733
Jeff Browna032cc02011-03-07 16:56:21 -08001734 // Send hover move after UP to tell the application that the mouse is hovering now.
1735 if (motionEventAction == AMOTION_EVENT_ACTION_UP
1736 && mPointerController != NULL) {
1737 getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001738 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
1739 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
1740 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Browna032cc02011-03-07 16:56:21 -08001741 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001742
Jeff Browna032cc02011-03-07 16:56:21 -08001743 // Send scroll events.
Jeff Brown33bbfd22011-02-24 20:55:35 -08001744 if (vscroll != 0 || hscroll != 0) {
1745 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
1746 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
1747
Jeff Brownefd32662011-03-08 15:13:06 -08001748 getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001749 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
1750 AMOTION_EVENT_EDGE_FLAG_NONE,
1751 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brown33bbfd22011-02-24 20:55:35 -08001752 }
Jeff Browna032cc02011-03-07 16:56:21 -08001753
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001754 // Synthesize key up from buttons if needed.
1755 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
1756 policyFlags, lastButtonState, currentButtonState);
1757
Jeff Browna032cc02011-03-07 16:56:21 -08001758 mAccumulator.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001759}
1760
Jeff Brown83c09682010-12-23 17:50:18 -08001761int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07001762 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
1763 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1764 } else {
1765 return AKEY_STATE_UNKNOWN;
1766 }
1767}
1768
Jeff Brown05dc66a2011-03-02 14:41:58 -08001769void CursorInputMapper::fadePointer() {
1770 { // acquire lock
1771 AutoMutex _l(mLock);
Jeff Brownefd32662011-03-08 15:13:06 -08001772 if (mPointerController != NULL) {
Jeff Brown538881e2011-05-25 18:23:38 -07001773 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
Jeff Brownefd32662011-03-08 15:13:06 -08001774 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08001775 } // release lock
1776}
1777
Jeff Brown6d0fec22010-07-23 21:28:06 -07001778
1779// --- TouchInputMapper ---
1780
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001781TouchInputMapper::TouchInputMapper(InputDevice* device) :
1782 InputMapper(device) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001783 mLocked.surfaceOrientation = -1;
1784 mLocked.surfaceWidth = -1;
1785 mLocked.surfaceHeight = -1;
1786
1787 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001788}
1789
1790TouchInputMapper::~TouchInputMapper() {
1791}
1792
1793uint32_t TouchInputMapper::getSources() {
Jeff Brownace13b12011-03-09 17:39:48 -08001794 return mTouchSource | mPointerSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001795}
1796
1797void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1798 InputMapper::populateDeviceInfo(info);
1799
Jeff Brown6328cdc2010-07-29 18:18:33 -07001800 { // acquire lock
1801 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001802
Jeff Brown6328cdc2010-07-29 18:18:33 -07001803 // Ensure surface information is up to date so that orientation changes are
1804 // noticed immediately.
Jeff Brownefd32662011-03-08 15:13:06 -08001805 if (!configureSurfaceLocked()) {
1806 return;
1807 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001808
Jeff Brownefd32662011-03-08 15:13:06 -08001809 info->addMotionRange(mLocked.orientedRanges.x);
1810 info->addMotionRange(mLocked.orientedRanges.y);
Jeff Brown8d608662010-08-30 03:02:23 -07001811
1812 if (mLocked.orientedRanges.havePressure) {
Jeff Brownefd32662011-03-08 15:13:06 -08001813 info->addMotionRange(mLocked.orientedRanges.pressure);
Jeff Brown8d608662010-08-30 03:02:23 -07001814 }
1815
1816 if (mLocked.orientedRanges.haveSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08001817 info->addMotionRange(mLocked.orientedRanges.size);
Jeff Brown8d608662010-08-30 03:02:23 -07001818 }
1819
Jeff Brownc6d282b2010-10-14 21:42:15 -07001820 if (mLocked.orientedRanges.haveTouchSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08001821 info->addMotionRange(mLocked.orientedRanges.touchMajor);
1822 info->addMotionRange(mLocked.orientedRanges.touchMinor);
Jeff Brown8d608662010-08-30 03:02:23 -07001823 }
1824
Jeff Brownc6d282b2010-10-14 21:42:15 -07001825 if (mLocked.orientedRanges.haveToolSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08001826 info->addMotionRange(mLocked.orientedRanges.toolMajor);
1827 info->addMotionRange(mLocked.orientedRanges.toolMinor);
Jeff Brown8d608662010-08-30 03:02:23 -07001828 }
1829
1830 if (mLocked.orientedRanges.haveOrientation) {
Jeff Brownefd32662011-03-08 15:13:06 -08001831 info->addMotionRange(mLocked.orientedRanges.orientation);
Jeff Brown8d608662010-08-30 03:02:23 -07001832 }
Jeff Brownace13b12011-03-09 17:39:48 -08001833
Jeff Brown80fd47c2011-05-24 01:07:44 -07001834 if (mLocked.orientedRanges.haveDistance) {
1835 info->addMotionRange(mLocked.orientedRanges.distance);
1836 }
1837
Jeff Brownace13b12011-03-09 17:39:48 -08001838 if (mPointerController != NULL) {
1839 float minX, minY, maxX, maxY;
1840 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
1841 info->addMotionRange(AMOTION_EVENT_AXIS_X, mPointerSource,
1842 minX, maxX, 0.0f, 0.0f);
1843 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mPointerSource,
1844 minY, maxY, 0.0f, 0.0f);
1845 }
1846 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mPointerSource,
1847 0.0f, 1.0f, 0.0f, 0.0f);
1848 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001849 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001850}
1851
Jeff Brownef3d7e82010-09-30 14:33:04 -07001852void TouchInputMapper::dump(String8& dump) {
1853 { // acquire lock
1854 AutoMutex _l(mLock);
1855 dump.append(INDENT2 "Touch Input Mapper:\n");
Jeff Brownef3d7e82010-09-30 14:33:04 -07001856 dumpParameters(dump);
1857 dumpVirtualKeysLocked(dump);
1858 dumpRawAxes(dump);
1859 dumpCalibration(dump);
1860 dumpSurfaceLocked(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08001861
Jeff Brown511ee5f2010-10-18 13:32:20 -07001862 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
Jeff Brownc6d282b2010-10-14 21:42:15 -07001863 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mLocked.xScale);
1864 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mLocked.yScale);
1865 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mLocked.xPrecision);
1866 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mLocked.yPrecision);
1867 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mLocked.geometricScale);
1868 dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mLocked.toolSizeLinearScale);
1869 dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mLocked.toolSizeLinearBias);
1870 dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mLocked.toolSizeAreaScale);
1871 dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mLocked.toolSizeAreaBias);
1872 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mLocked.pressureScale);
1873 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mLocked.sizeScale);
Jeff Brownefd32662011-03-08 15:13:06 -08001874 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mLocked.orientationScale);
Jeff Brown80fd47c2011-05-24 01:07:44 -07001875 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mLocked.distanceScale);
Jeff Brownefd32662011-03-08 15:13:06 -08001876
1877 dump.appendFormat(INDENT3 "Last Touch:\n");
Jeff Brownace13b12011-03-09 17:39:48 -08001878 dump.appendFormat(INDENT4 "Button State: 0x%08x\n", mLastTouch.buttonState);
Jeff Brownaba321a2011-06-28 20:34:40 -07001879 dump.appendFormat(INDENT4 "Pointer Count: %d\n", mLastTouch.pointerCount);
1880 for (uint32_t i = 0; i < mLastTouch.pointerCount; i++) {
1881 const PointerData& pointer = mLastTouch.pointers[i];
1882 dump.appendFormat(INDENT5 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
1883 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
1884 "orientation=%d, distance=%d, isStylus=%s\n", i,
1885 pointer.id, pointer.x, pointer.y, pointer.pressure,
1886 pointer.touchMajor, pointer.touchMinor, pointer.toolMajor, pointer.toolMinor,
1887 pointer.orientation, pointer.distance, toString(pointer.isStylus));
1888 }
Jeff Brownace13b12011-03-09 17:39:48 -08001889
1890 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
1891 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
1892 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
1893 mLocked.pointerGestureXMovementScale);
1894 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
1895 mLocked.pointerGestureYMovementScale);
1896 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
1897 mLocked.pointerGestureXZoomScale);
1898 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
1899 mLocked.pointerGestureYZoomScale);
Jeff Brown2352b972011-04-12 22:39:53 -07001900 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
1901 mLocked.pointerGestureMaxSwipeWidth);
Jeff Brownace13b12011-03-09 17:39:48 -08001902 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07001903 } // release lock
1904}
1905
Jeff Brown6328cdc2010-07-29 18:18:33 -07001906void TouchInputMapper::initializeLocked() {
1907 mCurrentTouch.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001908 mLastTouch.clear();
1909 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001910
Jeff Brown6328cdc2010-07-29 18:18:33 -07001911 mLocked.currentVirtualKey.down = false;
Jeff Brown8d608662010-08-30 03:02:23 -07001912
1913 mLocked.orientedRanges.havePressure = false;
1914 mLocked.orientedRanges.haveSize = false;
Jeff Brownc6d282b2010-10-14 21:42:15 -07001915 mLocked.orientedRanges.haveTouchSize = false;
1916 mLocked.orientedRanges.haveToolSize = false;
Jeff Brown8d608662010-08-30 03:02:23 -07001917 mLocked.orientedRanges.haveOrientation = false;
Jeff Brown80fd47c2011-05-24 01:07:44 -07001918 mLocked.orientedRanges.haveDistance = false;
Jeff Brownace13b12011-03-09 17:39:48 -08001919
1920 mPointerGesture.reset();
Jeff Brown8d608662010-08-30 03:02:23 -07001921}
1922
Jeff Brown474dcb52011-06-14 20:22:50 -07001923void TouchInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
1924 InputMapper::configure(config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001925
Jeff Brown474dcb52011-06-14 20:22:50 -07001926 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001927
Jeff Brown474dcb52011-06-14 20:22:50 -07001928 if (!changes) { // first time only
1929 // Configure basic parameters.
1930 configureParameters();
1931
1932 // Configure sources.
1933 switch (mParameters.deviceType) {
1934 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
1935 mTouchSource = AINPUT_SOURCE_TOUCHSCREEN;
1936 mPointerSource = 0;
1937 break;
1938 case Parameters::DEVICE_TYPE_TOUCH_PAD:
1939 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
1940 mPointerSource = 0;
1941 break;
1942 case Parameters::DEVICE_TYPE_POINTER:
1943 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
1944 mPointerSource = AINPUT_SOURCE_MOUSE;
1945 break;
1946 default:
1947 LOG_ASSERT(false);
1948 }
1949
1950 // Configure absolute axis information.
1951 configureRawAxes();
1952
1953 // Prepare input device calibration.
1954 parseCalibration();
1955 resolveCalibration();
1956
1957 { // acquire lock
1958 AutoMutex _l(mLock);
1959
1960 // Configure surface dimensions and orientation.
1961 configureSurfaceLocked();
1962 } // release lock
Jeff Brown83c09682010-12-23 17:50:18 -08001963 }
1964
Jeff Brown474dcb52011-06-14 20:22:50 -07001965 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
1966 mPointerGesture.pointerVelocityControl.setParameters(
1967 mConfig.pointerVelocityControlParameters);
1968 }
Jeff Brown8d608662010-08-30 03:02:23 -07001969
Jeff Brown474dcb52011-06-14 20:22:50 -07001970 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT)) {
1971 // Reset the touch screen when pointer gesture enablement changes.
1972 reset();
1973 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001974}
1975
Jeff Brown8d608662010-08-30 03:02:23 -07001976void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07001977 // Use the pointer presentation mode for devices that do not support distinct
1978 // multitouch. The spot-based presentation relies on being able to accurately
1979 // locate two or more fingers on the touch pad.
1980 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
1981 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07001982
Jeff Brown538881e2011-05-25 18:23:38 -07001983 String8 gestureModeString;
1984 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
1985 gestureModeString)) {
1986 if (gestureModeString == "pointer") {
1987 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
1988 } else if (gestureModeString == "spots") {
1989 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
1990 } else if (gestureModeString != "default") {
1991 LOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
1992 }
1993 }
1994
Jeff Brownace13b12011-03-09 17:39:48 -08001995 if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
1996 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
1997 // The device is a cursor device with a touch pad attached.
1998 // By default don't use the touch pad to move the pointer.
1999 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002000 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2001 // The device is a pointing device like a track pad.
2002 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2003 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2004 // The device is a touch screen.
2005 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownace13b12011-03-09 17:39:48 -08002006 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002007 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002008 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2009 }
2010
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002011 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002012 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2013 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002014 if (deviceTypeString == "touchScreen") {
2015 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002016 } else if (deviceTypeString == "touchPad") {
2017 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002018 } else if (deviceTypeString == "pointer") {
2019 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002020 } else if (deviceTypeString != "default") {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002021 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
2022 }
2023 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002024
Jeff Brownefd32662011-03-08 15:13:06 -08002025 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002026 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2027 mParameters.orientationAware);
2028
Jeff Brownefd32662011-03-08 15:13:06 -08002029 mParameters.associatedDisplayId = mParameters.orientationAware
2030 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownace13b12011-03-09 17:39:48 -08002031 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
Jeff Brownefd32662011-03-08 15:13:06 -08002032 ? 0 : -1;
Jeff Brown8d608662010-08-30 03:02:23 -07002033}
2034
Jeff Brownef3d7e82010-09-30 14:33:04 -07002035void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002036 dump.append(INDENT3 "Parameters:\n");
2037
Jeff Brown538881e2011-05-25 18:23:38 -07002038 switch (mParameters.gestureMode) {
2039 case Parameters::GESTURE_MODE_POINTER:
2040 dump.append(INDENT4 "GestureMode: pointer\n");
2041 break;
2042 case Parameters::GESTURE_MODE_SPOTS:
2043 dump.append(INDENT4 "GestureMode: spots\n");
2044 break;
2045 default:
2046 assert(false);
2047 }
2048
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002049 switch (mParameters.deviceType) {
2050 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2051 dump.append(INDENT4 "DeviceType: touchScreen\n");
2052 break;
2053 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2054 dump.append(INDENT4 "DeviceType: touchPad\n");
2055 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002056 case Parameters::DEVICE_TYPE_POINTER:
2057 dump.append(INDENT4 "DeviceType: pointer\n");
2058 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002059 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002060 LOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002061 }
2062
2063 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2064 mParameters.associatedDisplayId);
2065 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2066 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002067}
2068
Jeff Brown8d608662010-08-30 03:02:23 -07002069void TouchInputMapper::configureRawAxes() {
2070 mRawAxes.x.clear();
2071 mRawAxes.y.clear();
2072 mRawAxes.pressure.clear();
2073 mRawAxes.touchMajor.clear();
2074 mRawAxes.touchMinor.clear();
2075 mRawAxes.toolMajor.clear();
2076 mRawAxes.toolMinor.clear();
2077 mRawAxes.orientation.clear();
Jeff Brown80fd47c2011-05-24 01:07:44 -07002078 mRawAxes.distance.clear();
2079 mRawAxes.trackingId.clear();
2080 mRawAxes.slot.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002081}
2082
Jeff Brownef3d7e82010-09-30 14:33:04 -07002083void TouchInputMapper::dumpRawAxes(String8& dump) {
2084 dump.append(INDENT3 "Raw Axes:\n");
Jeff Browncb1404e2011-01-15 18:14:15 -08002085 dumpRawAbsoluteAxisInfo(dump, mRawAxes.x, "X");
2086 dumpRawAbsoluteAxisInfo(dump, mRawAxes.y, "Y");
2087 dumpRawAbsoluteAxisInfo(dump, mRawAxes.pressure, "Pressure");
2088 dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor");
2089 dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor");
2090 dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor");
2091 dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor");
2092 dumpRawAbsoluteAxisInfo(dump, mRawAxes.orientation, "Orientation");
Jeff Brown80fd47c2011-05-24 01:07:44 -07002093 dumpRawAbsoluteAxisInfo(dump, mRawAxes.distance, "Distance");
2094 dumpRawAbsoluteAxisInfo(dump, mRawAxes.trackingId, "TrackingId");
2095 dumpRawAbsoluteAxisInfo(dump, mRawAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002096}
2097
Jeff Brown6328cdc2010-07-29 18:18:33 -07002098bool TouchInputMapper::configureSurfaceLocked() {
Jeff Brown9626b142011-03-03 02:09:54 -08002099 // Ensure we have valid X and Y axes.
2100 if (!mRawAxes.x.valid || !mRawAxes.y.valid) {
2101 LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
2102 "The device will be inoperable.", getDeviceName().string());
2103 return false;
2104 }
2105
Jeff Brown6d0fec22010-07-23 21:28:06 -07002106 // Update orientation and dimensions if needed.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002107 int32_t orientation = DISPLAY_ORIENTATION_0;
Jeff Brown9626b142011-03-03 02:09:54 -08002108 int32_t width = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2109 int32_t height = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002110
2111 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002112 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002113 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownefd32662011-03-08 15:13:06 -08002114 &mLocked.associatedDisplayWidth, &mLocked.associatedDisplayHeight,
2115 &mLocked.associatedDisplayOrientation)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002116 return false;
2117 }
Jeff Brownefd32662011-03-08 15:13:06 -08002118
2119 // A touch screen inherits the dimensions of the display.
2120 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
2121 width = mLocked.associatedDisplayWidth;
2122 height = mLocked.associatedDisplayHeight;
2123 }
2124
2125 // The device inherits the orientation of the display if it is orientation aware.
2126 if (mParameters.orientationAware) {
2127 orientation = mLocked.associatedDisplayOrientation;
2128 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002129 }
2130
Jeff Brownace13b12011-03-09 17:39:48 -08002131 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2132 && mPointerController == NULL) {
2133 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2134 }
2135
Jeff Brown6328cdc2010-07-29 18:18:33 -07002136 bool orientationChanged = mLocked.surfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002137 if (orientationChanged) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002138 mLocked.surfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002139 }
2140
Jeff Brown6328cdc2010-07-29 18:18:33 -07002141 bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002142 if (sizeChanged) {
Jeff Brownefd32662011-03-08 15:13:06 -08002143 LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002144 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown8d608662010-08-30 03:02:23 -07002145
Jeff Brown6328cdc2010-07-29 18:18:33 -07002146 mLocked.surfaceWidth = width;
2147 mLocked.surfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002148
Jeff Brown8d608662010-08-30 03:02:23 -07002149 // Configure X and Y factors.
Jeff Brown9626b142011-03-03 02:09:54 -08002150 mLocked.xScale = float(width) / (mRawAxes.x.maxValue - mRawAxes.x.minValue + 1);
2151 mLocked.yScale = float(height) / (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1);
2152 mLocked.xPrecision = 1.0f / mLocked.xScale;
2153 mLocked.yPrecision = 1.0f / mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002154
Jeff Brownefd32662011-03-08 15:13:06 -08002155 mLocked.orientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
2156 mLocked.orientedRanges.x.source = mTouchSource;
2157 mLocked.orientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
2158 mLocked.orientedRanges.y.source = mTouchSource;
2159
Jeff Brown9626b142011-03-03 02:09:54 -08002160 configureVirtualKeysLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002161
Jeff Brown8d608662010-08-30 03:02:23 -07002162 // Scale factor for terms that are not oriented in a particular axis.
2163 // If the pixels are square then xScale == yScale otherwise we fake it
2164 // by choosing an average.
2165 mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002166
Jeff Brown8d608662010-08-30 03:02:23 -07002167 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002168 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002169
Jeff Brown8d608662010-08-30 03:02:23 -07002170 // TouchMajor and TouchMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002171 if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) {
2172 mLocked.orientedRanges.haveTouchSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002173
2174 mLocked.orientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
2175 mLocked.orientedRanges.touchMajor.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002176 mLocked.orientedRanges.touchMajor.min = 0;
2177 mLocked.orientedRanges.touchMajor.max = diagonalSize;
2178 mLocked.orientedRanges.touchMajor.flat = 0;
2179 mLocked.orientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002180
Jeff Brown8d608662010-08-30 03:02:23 -07002181 mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor;
Jeff Brownefd32662011-03-08 15:13:06 -08002182 mLocked.orientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002183 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002184
Jeff Brown8d608662010-08-30 03:02:23 -07002185 // ToolMajor and ToolMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002186 mLocked.toolSizeLinearScale = 0;
2187 mLocked.toolSizeLinearBias = 0;
2188 mLocked.toolSizeAreaScale = 0;
2189 mLocked.toolSizeAreaBias = 0;
2190 if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
2191 if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) {
2192 if (mCalibration.haveToolSizeLinearScale) {
2193 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
Jeff Brown8d608662010-08-30 03:02:23 -07002194 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002195 mLocked.toolSizeLinearScale = float(min(width, height))
Jeff Brown8d608662010-08-30 03:02:23 -07002196 / mRawAxes.toolMajor.maxValue;
2197 }
2198
Jeff Brownc6d282b2010-10-14 21:42:15 -07002199 if (mCalibration.haveToolSizeLinearBias) {
2200 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
2201 }
2202 } else if (mCalibration.toolSizeCalibration ==
2203 Calibration::TOOL_SIZE_CALIBRATION_AREA) {
2204 if (mCalibration.haveToolSizeLinearScale) {
2205 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
2206 } else {
2207 mLocked.toolSizeLinearScale = min(width, height);
2208 }
2209
2210 if (mCalibration.haveToolSizeLinearBias) {
2211 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
2212 }
2213
2214 if (mCalibration.haveToolSizeAreaScale) {
2215 mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale;
2216 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
2217 mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue;
2218 }
2219
2220 if (mCalibration.haveToolSizeAreaBias) {
2221 mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -07002222 }
2223 }
2224
Jeff Brownc6d282b2010-10-14 21:42:15 -07002225 mLocked.orientedRanges.haveToolSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002226
2227 mLocked.orientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
2228 mLocked.orientedRanges.toolMajor.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002229 mLocked.orientedRanges.toolMajor.min = 0;
2230 mLocked.orientedRanges.toolMajor.max = diagonalSize;
2231 mLocked.orientedRanges.toolMajor.flat = 0;
2232 mLocked.orientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002233
Jeff Brown8d608662010-08-30 03:02:23 -07002234 mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor;
Jeff Brownefd32662011-03-08 15:13:06 -08002235 mLocked.orientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002236 }
2237
2238 // Pressure factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002239 mLocked.pressureScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002240 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
2241 RawAbsoluteAxisInfo rawPressureAxis;
2242 switch (mCalibration.pressureSource) {
2243 case Calibration::PRESSURE_SOURCE_PRESSURE:
2244 rawPressureAxis = mRawAxes.pressure;
2245 break;
2246 case Calibration::PRESSURE_SOURCE_TOUCH:
2247 rawPressureAxis = mRawAxes.touchMajor;
2248 break;
2249 default:
2250 rawPressureAxis.clear();
2251 }
2252
Jeff Brown8d608662010-08-30 03:02:23 -07002253 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2254 || mCalibration.pressureCalibration
2255 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2256 if (mCalibration.havePressureScale) {
2257 mLocked.pressureScale = mCalibration.pressureScale;
2258 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
2259 mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue;
2260 }
2261 }
2262
2263 mLocked.orientedRanges.havePressure = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002264
2265 mLocked.orientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2266 mLocked.orientedRanges.pressure.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002267 mLocked.orientedRanges.pressure.min = 0;
2268 mLocked.orientedRanges.pressure.max = 1.0;
2269 mLocked.orientedRanges.pressure.flat = 0;
2270 mLocked.orientedRanges.pressure.fuzz = 0;
2271 }
2272
2273 // Size factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002274 mLocked.sizeScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002275 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002276 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
2277 if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
2278 mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue;
2279 }
2280 }
2281
2282 mLocked.orientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002283
2284 mLocked.orientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
2285 mLocked.orientedRanges.size.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002286 mLocked.orientedRanges.size.min = 0;
2287 mLocked.orientedRanges.size.max = 1.0;
2288 mLocked.orientedRanges.size.flat = 0;
2289 mLocked.orientedRanges.size.fuzz = 0;
2290 }
2291
2292 // Orientation
Jeff Brownc6d282b2010-10-14 21:42:15 -07002293 mLocked.orientationScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002294 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002295 if (mCalibration.orientationCalibration
2296 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
2297 if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) {
2298 mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue;
2299 }
2300 }
2301
Jeff Brown80fd47c2011-05-24 01:07:44 -07002302 mLocked.orientedRanges.haveOrientation = true;
2303
Jeff Brownefd32662011-03-08 15:13:06 -08002304 mLocked.orientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2305 mLocked.orientedRanges.orientation.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002306 mLocked.orientedRanges.orientation.min = - M_PI_2;
2307 mLocked.orientedRanges.orientation.max = M_PI_2;
2308 mLocked.orientedRanges.orientation.flat = 0;
2309 mLocked.orientedRanges.orientation.fuzz = 0;
2310 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002311
2312 // Distance
2313 mLocked.distanceScale = 0;
2314 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
2315 if (mCalibration.distanceCalibration
2316 == Calibration::DISTANCE_CALIBRATION_SCALED) {
2317 if (mCalibration.haveDistanceScale) {
2318 mLocked.distanceScale = mCalibration.distanceScale;
2319 } else {
2320 mLocked.distanceScale = 1.0f;
2321 }
2322 }
2323
2324 mLocked.orientedRanges.haveDistance = true;
2325
2326 mLocked.orientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
2327 mLocked.orientedRanges.distance.source = mTouchSource;
2328 mLocked.orientedRanges.distance.min =
2329 mRawAxes.distance.minValue * mLocked.distanceScale;
2330 mLocked.orientedRanges.distance.max =
2331 mRawAxes.distance.minValue * mLocked.distanceScale;
2332 mLocked.orientedRanges.distance.flat = 0;
2333 mLocked.orientedRanges.distance.fuzz =
2334 mRawAxes.distance.fuzz * mLocked.distanceScale;
2335 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002336 }
2337
2338 if (orientationChanged || sizeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002339 // Compute oriented surface dimensions, precision, scales and ranges.
2340 // Note that the maximum value reported is an inclusive maximum value so it is one
2341 // unit less than the total width or height of surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002342 switch (mLocked.surfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002343 case DISPLAY_ORIENTATION_90:
2344 case DISPLAY_ORIENTATION_270:
Jeff Brown6328cdc2010-07-29 18:18:33 -07002345 mLocked.orientedSurfaceWidth = mLocked.surfaceHeight;
2346 mLocked.orientedSurfaceHeight = mLocked.surfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002347
Jeff Brown6328cdc2010-07-29 18:18:33 -07002348 mLocked.orientedXPrecision = mLocked.yPrecision;
2349 mLocked.orientedYPrecision = mLocked.xPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002350
2351 mLocked.orientedRanges.x.min = 0;
2352 mLocked.orientedRanges.x.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue)
2353 * mLocked.yScale;
2354 mLocked.orientedRanges.x.flat = 0;
2355 mLocked.orientedRanges.x.fuzz = mLocked.yScale;
2356
2357 mLocked.orientedRanges.y.min = 0;
2358 mLocked.orientedRanges.y.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue)
2359 * mLocked.xScale;
2360 mLocked.orientedRanges.y.flat = 0;
2361 mLocked.orientedRanges.y.fuzz = mLocked.xScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002362 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002363
Jeff Brown6d0fec22010-07-23 21:28:06 -07002364 default:
Jeff Brown6328cdc2010-07-29 18:18:33 -07002365 mLocked.orientedSurfaceWidth = mLocked.surfaceWidth;
2366 mLocked.orientedSurfaceHeight = mLocked.surfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002367
Jeff Brown6328cdc2010-07-29 18:18:33 -07002368 mLocked.orientedXPrecision = mLocked.xPrecision;
2369 mLocked.orientedYPrecision = mLocked.yPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002370
2371 mLocked.orientedRanges.x.min = 0;
2372 mLocked.orientedRanges.x.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue)
2373 * mLocked.xScale;
2374 mLocked.orientedRanges.x.flat = 0;
2375 mLocked.orientedRanges.x.fuzz = mLocked.xScale;
2376
2377 mLocked.orientedRanges.y.min = 0;
2378 mLocked.orientedRanges.y.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue)
2379 * mLocked.yScale;
2380 mLocked.orientedRanges.y.flat = 0;
2381 mLocked.orientedRanges.y.fuzz = mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002382 break;
2383 }
Jeff Brownace13b12011-03-09 17:39:48 -08002384
2385 // Compute pointer gesture detection parameters.
2386 // TODO: These factors should not be hardcoded.
2387 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2388 int32_t rawWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2389 int32_t rawHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07002390 float rawDiagonal = hypotf(rawWidth, rawHeight);
2391 float displayDiagonal = hypotf(mLocked.associatedDisplayWidth,
2392 mLocked.associatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08002393
Jeff Brown2352b972011-04-12 22:39:53 -07002394 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07002395 // given area relative to the diagonal size of the display when no acceleration
2396 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08002397 // Assume that the touch pad has a square aspect ratio such that movements in
2398 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown474dcb52011-06-14 20:22:50 -07002399 mLocked.pointerGestureXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002400 * displayDiagonal / rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08002401 mLocked.pointerGestureYMovementScale = mLocked.pointerGestureXMovementScale;
2402
2403 // Scale zooms to cover a smaller range of the display than movements do.
2404 // This value determines the area around the pointer that is affected by freeform
2405 // pointer gestures.
Jeff Brown474dcb52011-06-14 20:22:50 -07002406 mLocked.pointerGestureXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002407 * displayDiagonal / rawDiagonal;
2408 mLocked.pointerGestureYZoomScale = mLocked.pointerGestureXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002409
Jeff Brown2352b972011-04-12 22:39:53 -07002410 // Max width between pointers to detect a swipe gesture is more than some fraction
2411 // of the diagonal axis of the touch pad. Touches that are wider than this are
2412 // translated into freeform gestures.
Jeff Brown214eaf42011-05-26 19:17:02 -07002413 mLocked.pointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07002414 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brown2352b972011-04-12 22:39:53 -07002415
2416 // Reset the current pointer gesture.
2417 mPointerGesture.reset();
2418
2419 // Remove any current spots.
2420 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
2421 mPointerController->clearSpots();
2422 }
Jeff Brownace13b12011-03-09 17:39:48 -08002423 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002424 }
2425
2426 return true;
2427}
2428
Jeff Brownef3d7e82010-09-30 14:33:04 -07002429void TouchInputMapper::dumpSurfaceLocked(String8& dump) {
2430 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth);
2431 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight);
2432 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07002433}
2434
Jeff Brown6328cdc2010-07-29 18:18:33 -07002435void TouchInputMapper::configureVirtualKeysLocked() {
Jeff Brown8d608662010-08-30 03:02:23 -07002436 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08002437 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002438
Jeff Brown6328cdc2010-07-29 18:18:33 -07002439 mLocked.virtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002440
Jeff Brown6328cdc2010-07-29 18:18:33 -07002441 if (virtualKeyDefinitions.size() == 0) {
2442 return;
2443 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002444
Jeff Brown6328cdc2010-07-29 18:18:33 -07002445 mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size());
2446
Jeff Brown8d608662010-08-30 03:02:23 -07002447 int32_t touchScreenLeft = mRawAxes.x.minValue;
2448 int32_t touchScreenTop = mRawAxes.y.minValue;
Jeff Brown9626b142011-03-03 02:09:54 -08002449 int32_t touchScreenWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2450 int32_t touchScreenHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002451
2452 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07002453 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07002454 virtualKeyDefinitions[i];
2455
2456 mLocked.virtualKeys.add();
2457 VirtualKey& virtualKey = mLocked.virtualKeys.editTop();
2458
2459 virtualKey.scanCode = virtualKeyDefinition.scanCode;
2460 int32_t keyCode;
2461 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08002462 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07002463 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002464 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
2465 virtualKey.scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002466 mLocked.virtualKeys.pop(); // drop the key
2467 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002468 }
2469
Jeff Brown6328cdc2010-07-29 18:18:33 -07002470 virtualKey.keyCode = keyCode;
2471 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002472
Jeff Brown6328cdc2010-07-29 18:18:33 -07002473 // convert the key definition's display coordinates into touch coordinates for a hit box
2474 int32_t halfWidth = virtualKeyDefinition.width / 2;
2475 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002476
Jeff Brown6328cdc2010-07-29 18:18:33 -07002477 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
2478 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
2479 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
2480 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
2481 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
2482 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
2483 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
2484 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07002485 }
2486}
2487
2488void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) {
2489 if (!mLocked.virtualKeys.isEmpty()) {
2490 dump.append(INDENT3 "Virtual Keys:\n");
2491
2492 for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) {
2493 const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i);
2494 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
2495 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
2496 i, virtualKey.scanCode, virtualKey.keyCode,
2497 virtualKey.hitLeft, virtualKey.hitRight,
2498 virtualKey.hitTop, virtualKey.hitBottom);
2499 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002500 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002501}
2502
Jeff Brown8d608662010-08-30 03:02:23 -07002503void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002504 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07002505 Calibration& out = mCalibration;
2506
Jeff Brownc6d282b2010-10-14 21:42:15 -07002507 // Touch Size
2508 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT;
2509 String8 touchSizeCalibrationString;
2510 if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) {
2511 if (touchSizeCalibrationString == "none") {
2512 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
2513 } else if (touchSizeCalibrationString == "geometric") {
2514 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC;
2515 } else if (touchSizeCalibrationString == "pressure") {
2516 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
2517 } else if (touchSizeCalibrationString != "default") {
2518 LOGW("Invalid value for touch.touchSize.calibration: '%s'",
2519 touchSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002520 }
2521 }
2522
Jeff Brownc6d282b2010-10-14 21:42:15 -07002523 // Tool Size
2524 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT;
2525 String8 toolSizeCalibrationString;
2526 if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) {
2527 if (toolSizeCalibrationString == "none") {
2528 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
2529 } else if (toolSizeCalibrationString == "geometric") {
2530 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC;
2531 } else if (toolSizeCalibrationString == "linear") {
2532 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
2533 } else if (toolSizeCalibrationString == "area") {
2534 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA;
2535 } else if (toolSizeCalibrationString != "default") {
2536 LOGW("Invalid value for touch.toolSize.calibration: '%s'",
2537 toolSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002538 }
2539 }
2540
Jeff Brownc6d282b2010-10-14 21:42:15 -07002541 out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"),
2542 out.toolSizeLinearScale);
2543 out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"),
2544 out.toolSizeLinearBias);
2545 out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"),
2546 out.toolSizeAreaScale);
2547 out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"),
2548 out.toolSizeAreaBias);
2549 out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"),
2550 out.toolSizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07002551
2552 // Pressure
2553 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
2554 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002555 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002556 if (pressureCalibrationString == "none") {
2557 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2558 } else if (pressureCalibrationString == "physical") {
2559 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
2560 } else if (pressureCalibrationString == "amplitude") {
2561 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2562 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002563 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002564 pressureCalibrationString.string());
2565 }
2566 }
2567
2568 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
2569 String8 pressureSourceString;
2570 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
2571 if (pressureSourceString == "pressure") {
2572 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2573 } else if (pressureSourceString == "touch") {
2574 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2575 } else if (pressureSourceString != "default") {
2576 LOGW("Invalid value for touch.pressure.source: '%s'",
2577 pressureSourceString.string());
2578 }
2579 }
2580
2581 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
2582 out.pressureScale);
2583
2584 // Size
2585 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
2586 String8 sizeCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002587 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002588 if (sizeCalibrationString == "none") {
2589 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2590 } else if (sizeCalibrationString == "normalized") {
2591 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
2592 } else if (sizeCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002593 LOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002594 sizeCalibrationString.string());
2595 }
2596 }
2597
2598 // Orientation
2599 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
2600 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002601 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002602 if (orientationCalibrationString == "none") {
2603 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2604 } else if (orientationCalibrationString == "interpolated") {
2605 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002606 } else if (orientationCalibrationString == "vector") {
2607 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002608 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002609 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002610 orientationCalibrationString.string());
2611 }
2612 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002613
2614 // Distance
2615 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
2616 String8 distanceCalibrationString;
2617 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
2618 if (distanceCalibrationString == "none") {
2619 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
2620 } else if (distanceCalibrationString == "scaled") {
2621 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
2622 } else if (distanceCalibrationString != "default") {
2623 LOGW("Invalid value for touch.distance.calibration: '%s'",
2624 distanceCalibrationString.string());
2625 }
2626 }
2627
2628 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
2629 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002630}
2631
2632void TouchInputMapper::resolveCalibration() {
2633 // Pressure
2634 switch (mCalibration.pressureSource) {
2635 case Calibration::PRESSURE_SOURCE_DEFAULT:
2636 if (mRawAxes.pressure.valid) {
2637 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2638 } else if (mRawAxes.touchMajor.valid) {
2639 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2640 }
2641 break;
2642
2643 case Calibration::PRESSURE_SOURCE_PRESSURE:
2644 if (! mRawAxes.pressure.valid) {
2645 LOGW("Calibration property touch.pressure.source is 'pressure' but "
2646 "the pressure axis is not available.");
2647 }
2648 break;
2649
2650 case Calibration::PRESSURE_SOURCE_TOUCH:
2651 if (! mRawAxes.touchMajor.valid) {
2652 LOGW("Calibration property touch.pressure.source is 'touch' but "
2653 "the touchMajor axis is not available.");
2654 }
2655 break;
2656
2657 default:
2658 break;
2659 }
2660
2661 switch (mCalibration.pressureCalibration) {
2662 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
2663 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
2664 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2665 } else {
2666 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2667 }
2668 break;
2669
2670 default:
2671 break;
2672 }
2673
Jeff Brownc6d282b2010-10-14 21:42:15 -07002674 // Tool Size
2675 switch (mCalibration.toolSizeCalibration) {
2676 case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07002677 if (mRawAxes.toolMajor.valid) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002678 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
Jeff Brown8d608662010-08-30 03:02:23 -07002679 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002680 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002681 }
2682 break;
2683
2684 default:
2685 break;
2686 }
2687
Jeff Brownc6d282b2010-10-14 21:42:15 -07002688 // Touch Size
2689 switch (mCalibration.touchSizeCalibration) {
2690 case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07002691 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
Jeff Brownc6d282b2010-10-14 21:42:15 -07002692 && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
2693 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
Jeff Brown8d608662010-08-30 03:02:23 -07002694 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002695 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002696 }
2697 break;
2698
2699 default:
2700 break;
2701 }
2702
2703 // Size
2704 switch (mCalibration.sizeCalibration) {
2705 case Calibration::SIZE_CALIBRATION_DEFAULT:
2706 if (mRawAxes.toolMajor.valid) {
2707 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
2708 } else {
2709 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2710 }
2711 break;
2712
2713 default:
2714 break;
2715 }
2716
2717 // Orientation
2718 switch (mCalibration.orientationCalibration) {
2719 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
2720 if (mRawAxes.orientation.valid) {
2721 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
2722 } else {
2723 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2724 }
2725 break;
2726
2727 default:
2728 break;
2729 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002730
2731 // Distance
2732 switch (mCalibration.distanceCalibration) {
2733 case Calibration::DISTANCE_CALIBRATION_DEFAULT:
2734 if (mRawAxes.distance.valid) {
2735 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
2736 } else {
2737 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
2738 }
2739 break;
2740
2741 default:
2742 break;
2743 }
Jeff Brown8d608662010-08-30 03:02:23 -07002744}
2745
Jeff Brownef3d7e82010-09-30 14:33:04 -07002746void TouchInputMapper::dumpCalibration(String8& dump) {
2747 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07002748
Jeff Brownc6d282b2010-10-14 21:42:15 -07002749 // Touch Size
2750 switch (mCalibration.touchSizeCalibration) {
2751 case Calibration::TOUCH_SIZE_CALIBRATION_NONE:
2752 dump.append(INDENT4 "touch.touchSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002753 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002754 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
2755 dump.append(INDENT4 "touch.touchSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002756 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002757 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
2758 dump.append(INDENT4 "touch.touchSize.calibration: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002759 break;
2760 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002761 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002762 }
2763
Jeff Brownc6d282b2010-10-14 21:42:15 -07002764 // Tool Size
2765 switch (mCalibration.toolSizeCalibration) {
2766 case Calibration::TOOL_SIZE_CALIBRATION_NONE:
2767 dump.append(INDENT4 "touch.toolSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002768 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002769 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
2770 dump.append(INDENT4 "touch.toolSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002771 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002772 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
2773 dump.append(INDENT4 "touch.toolSize.calibration: linear\n");
2774 break;
2775 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2776 dump.append(INDENT4 "touch.toolSize.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002777 break;
2778 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002779 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002780 }
2781
Jeff Brownc6d282b2010-10-14 21:42:15 -07002782 if (mCalibration.haveToolSizeLinearScale) {
2783 dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n",
2784 mCalibration.toolSizeLinearScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002785 }
2786
Jeff Brownc6d282b2010-10-14 21:42:15 -07002787 if (mCalibration.haveToolSizeLinearBias) {
2788 dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n",
2789 mCalibration.toolSizeLinearBias);
Jeff Brown8d608662010-08-30 03:02:23 -07002790 }
2791
Jeff Brownc6d282b2010-10-14 21:42:15 -07002792 if (mCalibration.haveToolSizeAreaScale) {
2793 dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n",
2794 mCalibration.toolSizeAreaScale);
2795 }
2796
2797 if (mCalibration.haveToolSizeAreaBias) {
2798 dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n",
2799 mCalibration.toolSizeAreaBias);
2800 }
2801
2802 if (mCalibration.haveToolSizeIsSummed) {
Jeff Brown1f245102010-11-18 20:53:46 -08002803 dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %s\n",
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002804 toString(mCalibration.toolSizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07002805 }
2806
2807 // Pressure
2808 switch (mCalibration.pressureCalibration) {
2809 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002810 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002811 break;
2812 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002813 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002814 break;
2815 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002816 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002817 break;
2818 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002819 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002820 }
2821
2822 switch (mCalibration.pressureSource) {
2823 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002824 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002825 break;
2826 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002827 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002828 break;
2829 case Calibration::PRESSURE_SOURCE_DEFAULT:
2830 break;
2831 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002832 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002833 }
2834
2835 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07002836 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
2837 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002838 }
2839
2840 // Size
2841 switch (mCalibration.sizeCalibration) {
2842 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002843 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002844 break;
2845 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002846 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002847 break;
2848 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002849 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002850 }
2851
2852 // Orientation
2853 switch (mCalibration.orientationCalibration) {
2854 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002855 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002856 break;
2857 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002858 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002859 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002860 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
2861 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
2862 break;
Jeff Brown8d608662010-08-30 03:02:23 -07002863 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002864 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002865 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002866
2867 // Distance
2868 switch (mCalibration.distanceCalibration) {
2869 case Calibration::DISTANCE_CALIBRATION_NONE:
2870 dump.append(INDENT4 "touch.distance.calibration: none\n");
2871 break;
2872 case Calibration::DISTANCE_CALIBRATION_SCALED:
2873 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
2874 break;
2875 default:
2876 LOG_ASSERT(false);
2877 }
2878
2879 if (mCalibration.haveDistanceScale) {
2880 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
2881 mCalibration.distanceScale);
2882 }
Jeff Brown8d608662010-08-30 03:02:23 -07002883}
2884
Jeff Brown6d0fec22010-07-23 21:28:06 -07002885void TouchInputMapper::reset() {
2886 // Synthesize touch up event if touch is currently down.
2887 // This will also take care of finishing virtual key processing if needed.
2888 if (mLastTouch.pointerCount != 0) {
2889 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
2890 mCurrentTouch.clear();
2891 syncTouch(when, true);
2892 }
2893
Jeff Brown6328cdc2010-07-29 18:18:33 -07002894 { // acquire lock
2895 AutoMutex _l(mLock);
2896 initializeLocked();
Jeff Brown2352b972011-04-12 22:39:53 -07002897
2898 if (mPointerController != NULL
2899 && mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
Jeff Brown474dcb52011-06-14 20:22:50 -07002900 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
Jeff Brown2352b972011-04-12 22:39:53 -07002901 mPointerController->clearSpots();
2902 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002903 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07002904
Jeff Brown6328cdc2010-07-29 18:18:33 -07002905 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002906}
2907
2908void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Brownaa3855d2011-03-17 01:34:19 -07002909#if DEBUG_RAW_EVENTS
2910 if (!havePointerIds) {
2911 LOGD("syncTouch: pointerCount=%d, no pointer ids", mCurrentTouch.pointerCount);
2912 } else {
2913 LOGD("syncTouch: pointerCount=%d, up=0x%08x, down=0x%08x, move=0x%08x, "
2914 "last=0x%08x, current=0x%08x", mCurrentTouch.pointerCount,
2915 mLastTouch.idBits.value & ~mCurrentTouch.idBits.value,
2916 mCurrentTouch.idBits.value & ~mLastTouch.idBits.value,
2917 mLastTouch.idBits.value & mCurrentTouch.idBits.value,
2918 mLastTouch.idBits.value, mCurrentTouch.idBits.value);
2919 }
2920#endif
2921
Jeff Brown6328cdc2010-07-29 18:18:33 -07002922 // Preprocess pointer data.
Jeff Brownaa3855d2011-03-17 01:34:19 -07002923 if (!havePointerIds) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002924 calculatePointerIds();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002925 }
2926
Jeff Brown56194eb2011-03-02 19:23:13 -08002927 uint32_t policyFlags = 0;
Jeff Brown05dc66a2011-03-02 14:41:58 -08002928 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) {
Jeff Brownefd32662011-03-08 15:13:06 -08002929 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
2930 // If this is a touch screen, hide the pointer on an initial down.
2931 getContext()->fadePointer();
2932 }
Jeff Brown56194eb2011-03-02 19:23:13 -08002933
2934 // Initial downs on external touch devices should wake the device.
2935 // We don't do this for internal touch screens to prevent them from waking
2936 // up in your pocket.
2937 // TODO: Use the input device configuration to control this behavior more finely.
2938 if (getDevice()->isExternal()) {
2939 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2940 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002941 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002942
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002943 // Synthesize key down from buttons if needed.
2944 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mTouchSource,
2945 policyFlags, mLastTouch.buttonState, mCurrentTouch.buttonState);
2946
2947 // Send motion events.
Jeff Brown79ac9692011-04-19 21:20:10 -07002948 TouchResult touchResult;
2949 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount == 0
2950 && mLastTouch.buttonState == mCurrentTouch.buttonState) {
2951 // Drop spurious syncs.
2952 touchResult = DROP_STROKE;
2953 } else {
2954 // Process touches and virtual keys.
2955 touchResult = consumeOffScreenTouches(when, policyFlags);
2956 if (touchResult == DISPATCH_TOUCH) {
2957 suppressSwipeOntoVirtualKeys(when);
Jeff Brown474dcb52011-06-14 20:22:50 -07002958 if (mPointerController != NULL && mConfig.pointerGesturesEnabled) {
Jeff Brown79ac9692011-04-19 21:20:10 -07002959 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
2960 }
2961 dispatchTouches(when, policyFlags);
Jeff Brownace13b12011-03-09 17:39:48 -08002962 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002963 }
2964
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002965 // Synthesize key up from buttons if needed.
2966 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mTouchSource,
2967 policyFlags, mLastTouch.buttonState, mCurrentTouch.buttonState);
2968
Jeff Brown6328cdc2010-07-29 18:18:33 -07002969 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownace13b12011-03-09 17:39:48 -08002970 // Keep the button state so we can track edge-triggered button state changes.
Jeff Brown6d0fec22010-07-23 21:28:06 -07002971 if (touchResult == DROP_STROKE) {
2972 mLastTouch.clear();
Jeff Browna4d1bc52011-07-01 19:23:40 -07002973 mLastTouch.buttonState = mCurrentTouch.buttonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002974 } else {
Jeff Browna4d1bc52011-07-01 19:23:40 -07002975 mLastTouch.copyFrom(mCurrentTouch);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002976 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002977}
2978
Jeff Brown79ac9692011-04-19 21:20:10 -07002979void TouchInputMapper::timeoutExpired(nsecs_t when) {
2980 if (mPointerController != NULL) {
2981 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
2982 }
2983}
2984
Jeff Brown6d0fec22010-07-23 21:28:06 -07002985TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches(
2986 nsecs_t when, uint32_t policyFlags) {
2987 int32_t keyEventAction, keyEventFlags;
2988 int32_t keyCode, scanCode, downTime;
2989 TouchResult touchResult;
Jeff Brown349703e2010-06-22 01:27:15 -07002990
Jeff Brown6328cdc2010-07-29 18:18:33 -07002991 { // acquire lock
2992 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002993
Jeff Brown6328cdc2010-07-29 18:18:33 -07002994 // Update surface size and orientation, including virtual key positions.
2995 if (! configureSurfaceLocked()) {
2996 return DROP_STROKE;
2997 }
2998
2999 // Check for virtual key press.
3000 if (mLocked.currentVirtualKey.down) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003001 if (mCurrentTouch.pointerCount == 0) {
3002 // Pointer went up while virtual key was down.
Jeff Brown6328cdc2010-07-29 18:18:33 -07003003 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003004#if DEBUG_VIRTUAL_KEYS
3005 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07003006 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003007#endif
3008 keyEventAction = AKEY_EVENT_ACTION_UP;
3009 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
3010 touchResult = SKIP_TOUCH;
3011 goto DispatchVirtualKey;
3012 }
3013
3014 if (mCurrentTouch.pointerCount == 1) {
3015 int32_t x = mCurrentTouch.pointers[0].x;
3016 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003017 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
3018 if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003019 // Pointer is still within the space of the virtual key.
3020 return SKIP_TOUCH;
3021 }
3022 }
3023
3024 // Pointer left virtual key area or another pointer also went down.
3025 // Send key cancellation and drop the stroke so subsequent motions will be
3026 // considered fresh downs. This is useful when the user swipes away from the
3027 // virtual key area into the main display surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07003028 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003029#if DEBUG_VIRTUAL_KEYS
3030 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07003031 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003032#endif
3033 keyEventAction = AKEY_EVENT_ACTION_UP;
3034 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3035 | AKEY_EVENT_FLAG_CANCELED;
Jeff Brownc3db8582010-10-20 15:33:38 -07003036
3037 // Check whether the pointer moved inside the display area where we should
3038 // start a new stroke.
3039 int32_t x = mCurrentTouch.pointers[0].x;
3040 int32_t y = mCurrentTouch.pointers[0].y;
3041 if (isPointInsideSurfaceLocked(x, y)) {
3042 mLastTouch.clear();
3043 touchResult = DISPATCH_TOUCH;
3044 } else {
3045 touchResult = DROP_STROKE;
3046 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003047 } else {
3048 if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) {
3049 // Pointer just went down. Handle off-screen touches, if needed.
3050 int32_t x = mCurrentTouch.pointers[0].x;
3051 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003052 if (! isPointInsideSurfaceLocked(x, y)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003053 // If exactly one pointer went down, check for virtual key hit.
3054 // Otherwise we will drop the entire stroke.
3055 if (mCurrentTouch.pointerCount == 1) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003056 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003057 if (virtualKey) {
Jeff Brownfe508922011-01-18 15:10:10 -08003058 if (mContext->shouldDropVirtualKey(when, getDevice(),
3059 virtualKey->keyCode, virtualKey->scanCode)) {
3060 return DROP_STROKE;
3061 }
3062
Jeff Brown6328cdc2010-07-29 18:18:33 -07003063 mLocked.currentVirtualKey.down = true;
3064 mLocked.currentVirtualKey.downTime = when;
3065 mLocked.currentVirtualKey.keyCode = virtualKey->keyCode;
3066 mLocked.currentVirtualKey.scanCode = virtualKey->scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003067#if DEBUG_VIRTUAL_KEYS
3068 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07003069 mLocked.currentVirtualKey.keyCode,
3070 mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003071#endif
3072 keyEventAction = AKEY_EVENT_ACTION_DOWN;
3073 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM
3074 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
3075 touchResult = SKIP_TOUCH;
3076 goto DispatchVirtualKey;
3077 }
3078 }
3079 return DROP_STROKE;
3080 }
3081 }
3082 return DISPATCH_TOUCH;
3083 }
3084
3085 DispatchVirtualKey:
3086 // Collect remaining state needed to dispatch virtual key.
Jeff Brown6328cdc2010-07-29 18:18:33 -07003087 keyCode = mLocked.currentVirtualKey.keyCode;
3088 scanCode = mLocked.currentVirtualKey.scanCode;
3089 downTime = mLocked.currentVirtualKey.downTime;
3090 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07003091
3092 // Dispatch virtual key.
3093 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown0eaf3932010-10-01 14:55:30 -07003094 policyFlags |= POLICY_FLAG_VIRTUAL;
Jeff Brownb6997262010-10-08 22:31:17 -07003095 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3096 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3097 return touchResult;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003098}
3099
Jeff Brownefd32662011-03-08 15:13:06 -08003100void TouchInputMapper::suppressSwipeOntoVirtualKeys(nsecs_t when) {
Jeff Brownfe508922011-01-18 15:10:10 -08003101 // Disable all virtual key touches that happen within a short time interval of the
3102 // most recent touch. The idea is to filter out stray virtual key presses when
3103 // interacting with the touch screen.
3104 //
3105 // Problems we're trying to solve:
3106 //
3107 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3108 // virtual key area that is implemented by a separate touch panel and accidentally
3109 // triggers a virtual key.
3110 //
3111 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3112 // area and accidentally triggers a virtual key. This often happens when virtual keys
3113 // are layed out below the screen near to where the on screen keyboard's space bar
3114 // is displayed.
Jeff Brown474dcb52011-06-14 20:22:50 -07003115 if (mConfig.virtualKeyQuietTime > 0 && mCurrentTouch.pointerCount != 0) {
3116 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003117 }
3118}
3119
Jeff Brown6d0fec22010-07-23 21:28:06 -07003120void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
3121 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
3122 uint32_t lastPointerCount = mLastTouch.pointerCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003123 if (currentPointerCount == 0 && lastPointerCount == 0) {
3124 return; // nothing to do!
3125 }
3126
Jeff Brownace13b12011-03-09 17:39:48 -08003127 // Update current touch coordinates.
Jeff Brownace13b12011-03-09 17:39:48 -08003128 float xPrecision, yPrecision;
Jeff Browna6111372011-07-14 21:48:23 -07003129 prepareTouches(&xPrecision, &yPrecision);
Jeff Brownace13b12011-03-09 17:39:48 -08003130
3131 // Dispatch motions.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003132 BitSet32 currentIdBits = mCurrentTouch.idBits;
3133 BitSet32 lastIdBits = mLastTouch.idBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003134 int32_t metaState = getContext()->getGlobalMetaState();
3135 int32_t buttonState = mCurrentTouch.buttonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003136
3137 if (currentIdBits == lastIdBits) {
3138 // No pointer id changes so this is a move event.
3139 // The dispatcher takes care of batching moves so we don't have to deal with that here.
Jeff Brownace13b12011-03-09 17:39:48 -08003140 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003141 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3142 AMOTION_EVENT_EDGE_FLAG_NONE,
3143 mCurrentTouchProperties, mCurrentTouchCoords,
3144 mCurrentTouch.idToIndex, currentIdBits, -1,
Jeff Brownace13b12011-03-09 17:39:48 -08003145 xPrecision, yPrecision, mDownTime);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003146 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003147 // There may be pointers going up and pointers going down and pointers moving
3148 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003149 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3150 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003151 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003152 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003153
Jeff Brownace13b12011-03-09 17:39:48 -08003154 // Update last coordinates of pointers that have moved so that we observe the new
3155 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003156 bool moveNeeded = updateMovedPointers(
3157 mCurrentTouchProperties, mCurrentTouchCoords, mCurrentTouch.idToIndex,
3158 mLastTouchProperties, mLastTouchCoords, mLastTouch.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003159 moveIdBits);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003160 if (buttonState != mLastTouch.buttonState) {
3161 moveNeeded = true;
3162 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003163
Jeff Brownace13b12011-03-09 17:39:48 -08003164 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003165 while (!upIdBits.isEmpty()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003166 uint32_t upId = upIdBits.firstMarkedBit();
3167 upIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003168
Jeff Brownace13b12011-03-09 17:39:48 -08003169 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003170 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
3171 mLastTouchProperties, mLastTouchCoords,
3172 mLastTouch.idToIndex, dispatchedIdBits, upId,
Jeff Brownace13b12011-03-09 17:39:48 -08003173 xPrecision, yPrecision, mDownTime);
3174 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003175 }
3176
Jeff Brownc3db8582010-10-20 15:33:38 -07003177 // Dispatch move events if any of the remaining pointers moved from their old locations.
3178 // Although applications receive new locations as part of individual pointer up
3179 // events, they do not generally handle them except when presented in a move event.
3180 if (moveNeeded) {
Jeff Brownb6110c22011-04-01 16:15:13 -07003181 LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003182 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003183 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
3184 mCurrentTouchProperties, mCurrentTouchCoords,
3185 mCurrentTouch.idToIndex, dispatchedIdBits, -1,
Jeff Brownace13b12011-03-09 17:39:48 -08003186 xPrecision, yPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003187 }
3188
3189 // Dispatch pointer down events using the new pointer locations.
3190 while (!downIdBits.isEmpty()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003191 uint32_t downId = downIdBits.firstMarkedBit();
3192 downIdBits.clearBit(downId);
Jeff Brownace13b12011-03-09 17:39:48 -08003193 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003194
Jeff Brownace13b12011-03-09 17:39:48 -08003195 if (dispatchedIdBits.count() == 1) {
3196 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003197 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003198 }
3199
Jeff Brownace13b12011-03-09 17:39:48 -08003200 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Browna6111372011-07-14 21:48:23 -07003201 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003202 mCurrentTouchProperties, mCurrentTouchCoords,
3203 mCurrentTouch.idToIndex, dispatchedIdBits, downId,
Jeff Brownace13b12011-03-09 17:39:48 -08003204 xPrecision, yPrecision, mDownTime);
3205 }
3206 }
3207
3208 // Update state for next time.
3209 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003210 mLastTouchProperties[i].copyFrom(mCurrentTouchProperties[i]);
Jeff Brownace13b12011-03-09 17:39:48 -08003211 mLastTouchCoords[i].copyFrom(mCurrentTouchCoords[i]);
3212 }
3213}
3214
Jeff Browna6111372011-07-14 21:48:23 -07003215void TouchInputMapper::prepareTouches(float* outXPrecision, float* outYPrecision) {
Jeff Brownace13b12011-03-09 17:39:48 -08003216 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
3217 uint32_t lastPointerCount = mLastTouch.pointerCount;
3218
3219 AutoMutex _l(mLock);
3220
3221 // Walk through the the active pointers and map touch screen coordinates (TouchData) into
3222 // display or surface coordinates (PointerCoords) and adjust for display orientation.
3223 for (uint32_t i = 0; i < currentPointerCount; i++) {
3224 const PointerData& in = mCurrentTouch.pointers[i];
3225
3226 // ToolMajor and ToolMinor
3227 float toolMajor, toolMinor;
3228 switch (mCalibration.toolSizeCalibration) {
3229 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
3230 toolMajor = in.toolMajor * mLocked.geometricScale;
3231 if (mRawAxes.toolMinor.valid) {
3232 toolMinor = in.toolMinor * mLocked.geometricScale;
3233 } else {
3234 toolMinor = toolMajor;
3235 }
3236 break;
3237 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
3238 toolMajor = in.toolMajor != 0
3239 ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias
3240 : 0;
3241 if (mRawAxes.toolMinor.valid) {
3242 toolMinor = in.toolMinor != 0
3243 ? in.toolMinor * mLocked.toolSizeLinearScale
3244 + mLocked.toolSizeLinearBias
3245 : 0;
3246 } else {
3247 toolMinor = toolMajor;
3248 }
3249 break;
3250 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
3251 if (in.toolMajor != 0) {
3252 float diameter = sqrtf(in.toolMajor
3253 * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias);
3254 toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias;
3255 } else {
3256 toolMajor = 0;
3257 }
3258 toolMinor = toolMajor;
3259 break;
3260 default:
3261 toolMajor = 0;
3262 toolMinor = 0;
3263 break;
3264 }
3265
3266 if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) {
3267 toolMajor /= currentPointerCount;
3268 toolMinor /= currentPointerCount;
3269 }
3270
3271 // Pressure
3272 float rawPressure;
3273 switch (mCalibration.pressureSource) {
3274 case Calibration::PRESSURE_SOURCE_PRESSURE:
3275 rawPressure = in.pressure;
3276 break;
3277 case Calibration::PRESSURE_SOURCE_TOUCH:
3278 rawPressure = in.touchMajor;
3279 break;
3280 default:
3281 rawPressure = 0;
3282 }
3283
3284 float pressure;
3285 switch (mCalibration.pressureCalibration) {
3286 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3287 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
3288 pressure = rawPressure * mLocked.pressureScale;
3289 break;
3290 default:
3291 pressure = 1;
3292 break;
3293 }
3294
3295 // TouchMajor and TouchMinor
3296 float touchMajor, touchMinor;
3297 switch (mCalibration.touchSizeCalibration) {
3298 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
3299 touchMajor = in.touchMajor * mLocked.geometricScale;
3300 if (mRawAxes.touchMinor.valid) {
3301 touchMinor = in.touchMinor * mLocked.geometricScale;
3302 } else {
3303 touchMinor = touchMajor;
3304 }
3305 break;
3306 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
3307 touchMajor = toolMajor * pressure;
3308 touchMinor = toolMinor * pressure;
3309 break;
3310 default:
3311 touchMajor = 0;
3312 touchMinor = 0;
3313 break;
3314 }
3315
3316 if (touchMajor > toolMajor) {
3317 touchMajor = toolMajor;
3318 }
3319 if (touchMinor > toolMinor) {
3320 touchMinor = toolMinor;
3321 }
3322
3323 // Size
3324 float size;
3325 switch (mCalibration.sizeCalibration) {
3326 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
3327 float rawSize = mRawAxes.toolMinor.valid
3328 ? avg(in.toolMajor, in.toolMinor)
3329 : in.toolMajor;
3330 size = rawSize * mLocked.sizeScale;
3331 break;
3332 }
3333 default:
3334 size = 0;
3335 break;
3336 }
3337
3338 // Orientation
3339 float orientation;
3340 switch (mCalibration.orientationCalibration) {
3341 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
3342 orientation = in.orientation * mLocked.orientationScale;
3343 break;
3344 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3345 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3346 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3347 if (c1 != 0 || c2 != 0) {
3348 orientation = atan2f(c1, c2) * 0.5f;
Jeff Brown2352b972011-04-12 22:39:53 -07003349 float scale = 1.0f + hypotf(c1, c2) / 16.0f;
Jeff Brownace13b12011-03-09 17:39:48 -08003350 touchMajor *= scale;
3351 touchMinor /= scale;
3352 toolMajor *= scale;
3353 toolMinor /= scale;
3354 } else {
3355 orientation = 0;
3356 }
3357 break;
3358 }
3359 default:
3360 orientation = 0;
3361 }
3362
Jeff Brown80fd47c2011-05-24 01:07:44 -07003363 // Distance
3364 float distance;
3365 switch (mCalibration.distanceCalibration) {
3366 case Calibration::DISTANCE_CALIBRATION_SCALED:
3367 distance = in.distance * mLocked.distanceScale;
3368 break;
3369 default:
3370 distance = 0;
3371 }
3372
Jeff Brownace13b12011-03-09 17:39:48 -08003373 // X and Y
3374 // Adjust coords for surface orientation.
3375 float x, y;
3376 switch (mLocked.surfaceOrientation) {
3377 case DISPLAY_ORIENTATION_90:
3378 x = float(in.y - mRawAxes.y.minValue) * mLocked.yScale;
3379 y = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale;
3380 orientation -= M_PI_2;
3381 if (orientation < - M_PI_2) {
3382 orientation += M_PI;
3383 }
3384 break;
3385 case DISPLAY_ORIENTATION_180:
3386 x = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale;
3387 y = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale;
3388 break;
3389 case DISPLAY_ORIENTATION_270:
3390 x = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale;
3391 y = float(in.x - mRawAxes.x.minValue) * mLocked.xScale;
3392 orientation += M_PI_2;
3393 if (orientation > M_PI_2) {
3394 orientation -= M_PI;
3395 }
3396 break;
3397 default:
3398 x = float(in.x - mRawAxes.x.minValue) * mLocked.xScale;
3399 y = float(in.y - mRawAxes.y.minValue) * mLocked.yScale;
3400 break;
3401 }
3402
3403 // Write output coords.
3404 PointerCoords& out = mCurrentTouchCoords[i];
3405 out.clear();
3406 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3407 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3408 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3409 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3410 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3411 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3412 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3413 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3414 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown80fd47c2011-05-24 01:07:44 -07003415 if (distance != 0) {
3416 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
3417 }
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003418
3419 // Write output properties.
3420 PointerProperties& properties = mCurrentTouchProperties[i];
3421 properties.clear();
3422 properties.id = mCurrentTouch.pointers[i].id;
3423 properties.toolType = getTouchToolType(mCurrentTouch.pointers[i].isStylus);
Jeff Brownace13b12011-03-09 17:39:48 -08003424 }
3425
Jeff Brownace13b12011-03-09 17:39:48 -08003426 *outXPrecision = mLocked.orientedXPrecision;
3427 *outYPrecision = mLocked.orientedYPrecision;
3428}
3429
Jeff Brown79ac9692011-04-19 21:20:10 -07003430void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3431 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003432 // Update current gesture coordinates.
3433 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07003434 bool sendEvents = preparePointerGestures(when,
3435 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3436 if (!sendEvents) {
3437 return;
3438 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003439 if (finishPreviousGesture) {
3440 cancelPreviousGesture = false;
3441 }
Jeff Brownace13b12011-03-09 17:39:48 -08003442
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003443 // Update the pointer presentation and spots.
3444 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3445 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3446 if (finishPreviousGesture || cancelPreviousGesture) {
3447 mPointerController->clearSpots();
3448 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07003449 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
3450 mPointerGesture.currentGestureIdToIndex,
3451 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003452 } else {
3453 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
3454 }
Jeff Brown214eaf42011-05-26 19:17:02 -07003455
Jeff Brown538881e2011-05-25 18:23:38 -07003456 // Show or hide the pointer if needed.
3457 switch (mPointerGesture.currentGestureMode) {
3458 case PointerGesture::NEUTRAL:
3459 case PointerGesture::QUIET:
3460 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3461 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3462 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
3463 // Remind the user of where the pointer is after finishing a gesture with spots.
3464 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
3465 }
3466 break;
3467 case PointerGesture::TAP:
3468 case PointerGesture::TAP_DRAG:
3469 case PointerGesture::BUTTON_CLICK_OR_DRAG:
3470 case PointerGesture::HOVER:
3471 case PointerGesture::PRESS:
3472 // Unfade the pointer when the current gesture manipulates the
3473 // area directly under the pointer.
3474 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3475 break;
3476 case PointerGesture::SWIPE:
3477 case PointerGesture::FREEFORM:
3478 // Fade the pointer when the current gesture manipulates a different
3479 // area and there are spots to guide the user experience.
3480 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3481 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3482 } else {
3483 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3484 }
3485 break;
Jeff Brown2352b972011-04-12 22:39:53 -07003486 }
3487
Jeff Brownace13b12011-03-09 17:39:48 -08003488 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003489 int32_t metaState = getContext()->getGlobalMetaState();
3490 int32_t buttonState = mCurrentTouch.buttonState;
Jeff Brownace13b12011-03-09 17:39:48 -08003491
3492 // Update last coordinates of pointers that have moved so that we observe the new
3493 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07003494 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
3495 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
3496 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07003497 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08003498 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
3499 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
3500 bool moveNeeded = false;
3501 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07003502 && !mPointerGesture.lastGestureIdBits.isEmpty()
3503 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08003504 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
3505 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003506 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003507 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003508 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003509 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3510 movedGestureIdBits);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003511 if (buttonState != mLastTouch.buttonState) {
3512 moveNeeded = true;
3513 }
Jeff Brownace13b12011-03-09 17:39:48 -08003514 }
3515
3516 // Send motion events for all pointers that went up or were canceled.
3517 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
3518 if (!dispatchedGestureIdBits.isEmpty()) {
3519 if (cancelPreviousGesture) {
3520 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003521 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
3522 AMOTION_EVENT_EDGE_FLAG_NONE,
3523 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003524 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3525 dispatchedGestureIdBits, -1,
3526 0, 0, mPointerGesture.downTime);
3527
3528 dispatchedGestureIdBits.clear();
3529 } else {
3530 BitSet32 upGestureIdBits;
3531 if (finishPreviousGesture) {
3532 upGestureIdBits = dispatchedGestureIdBits;
3533 } else {
3534 upGestureIdBits.value = dispatchedGestureIdBits.value
3535 & ~mPointerGesture.currentGestureIdBits.value;
3536 }
3537 while (!upGestureIdBits.isEmpty()) {
3538 uint32_t id = upGestureIdBits.firstMarkedBit();
3539 upGestureIdBits.clearBit(id);
3540
3541 dispatchMotion(when, policyFlags, mPointerSource,
3542 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003543 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3544 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003545 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3546 dispatchedGestureIdBits, id,
3547 0, 0, mPointerGesture.downTime);
3548
3549 dispatchedGestureIdBits.clearBit(id);
3550 }
3551 }
3552 }
3553
3554 // Send motion events for all pointers that moved.
3555 if (moveNeeded) {
3556 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003557 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3558 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003559 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3560 dispatchedGestureIdBits, -1,
3561 0, 0, mPointerGesture.downTime);
3562 }
3563
3564 // Send motion events for all pointers that went down.
3565 if (down) {
3566 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
3567 & ~dispatchedGestureIdBits.value);
3568 while (!downGestureIdBits.isEmpty()) {
3569 uint32_t id = downGestureIdBits.firstMarkedBit();
3570 downGestureIdBits.clearBit(id);
3571 dispatchedGestureIdBits.markBit(id);
3572
Jeff Brownace13b12011-03-09 17:39:48 -08003573 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08003574 mPointerGesture.downTime = when;
3575 }
3576
3577 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Browna6111372011-07-14 21:48:23 -07003578 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003579 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003580 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3581 dispatchedGestureIdBits, id,
3582 0, 0, mPointerGesture.downTime);
3583 }
3584 }
3585
Jeff Brownace13b12011-03-09 17:39:48 -08003586 // Send motion events for hover.
3587 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
3588 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003589 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
3590 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3591 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003592 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3593 mPointerGesture.currentGestureIdBits, -1,
3594 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07003595 } else if (dispatchedGestureIdBits.isEmpty()
3596 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
3597 // Synthesize a hover move event after all pointers go up to indicate that
3598 // the pointer is hovering again even if the user is not currently touching
3599 // the touch pad. This ensures that a view will receive a fresh hover enter
3600 // event after a tap.
3601 float x, y;
3602 mPointerController->getPosition(&x, &y);
3603
3604 PointerProperties pointerProperties;
3605 pointerProperties.clear();
3606 pointerProperties.id = 0;
3607 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
3608
3609 PointerCoords pointerCoords;
3610 pointerCoords.clear();
3611 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3612 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3613
3614 getDispatcher()->notifyMotion(when, getDeviceId(), mPointerSource, policyFlags,
3615 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
3616 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3617 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003618 }
3619
3620 // Update state.
3621 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
3622 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08003623 mPointerGesture.lastGestureIdBits.clear();
3624 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08003625 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
3626 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
3627 uint32_t id = idBits.firstMarkedBit();
3628 idBits.clearBit(id);
3629 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003630 mPointerGesture.lastGestureProperties[index].copyFrom(
3631 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08003632 mPointerGesture.lastGestureCoords[index].copyFrom(
3633 mPointerGesture.currentGestureCoords[index]);
3634 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003635 }
3636 }
3637}
3638
Jeff Brown79ac9692011-04-19 21:20:10 -07003639bool TouchInputMapper::preparePointerGestures(nsecs_t when,
3640 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003641 *outCancelPreviousGesture = false;
3642 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003643
Jeff Brownace13b12011-03-09 17:39:48 -08003644 AutoMutex _l(mLock);
Jeff Brown6328cdc2010-07-29 18:18:33 -07003645
Jeff Brown79ac9692011-04-19 21:20:10 -07003646 // Handle TAP timeout.
3647 if (isTimeout) {
3648#if DEBUG_GESTURES
3649 LOGD("Gestures: Processing timeout");
3650#endif
3651
3652 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003653 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003654 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07003655 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07003656 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07003657 } else {
3658 // The tap is finished.
3659#if DEBUG_GESTURES
3660 LOGD("Gestures: TAP finished");
3661#endif
3662 *outFinishPreviousGesture = true;
3663
3664 mPointerGesture.activeGestureId = -1;
3665 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
3666 mPointerGesture.currentGestureIdBits.clear();
3667
Jeff Brown19c97d462011-06-01 12:33:19 -07003668 mPointerGesture.pointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07003669 return true;
3670 }
3671 }
3672
3673 // We did not handle this timeout.
3674 return false;
3675 }
3676
Jeff Brownace13b12011-03-09 17:39:48 -08003677 // Update the velocity tracker.
3678 {
3679 VelocityTracker::Position positions[MAX_POINTERS];
3680 uint32_t count = 0;
3681 for (BitSet32 idBits(mCurrentTouch.idBits); !idBits.isEmpty(); count++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003682 uint32_t id = idBits.firstMarkedBit();
3683 idBits.clearBit(id);
Jeff Brownace13b12011-03-09 17:39:48 -08003684 uint32_t index = mCurrentTouch.idToIndex[id];
3685 positions[count].x = mCurrentTouch.pointers[index].x
3686 * mLocked.pointerGestureXMovementScale;
3687 positions[count].y = mCurrentTouch.pointers[index].y
3688 * mLocked.pointerGestureYMovementScale;
3689 }
3690 mPointerGesture.velocityTracker.addMovement(when, mCurrentTouch.idBits, positions);
3691 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003692
Jeff Brownace13b12011-03-09 17:39:48 -08003693 // Pick a new active touch id if needed.
3694 // Choose an arbitrary pointer that just went down, if there is one.
3695 // Otherwise choose an arbitrary remaining pointer.
3696 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07003697 // We keep the same active touch id for as long as possible.
3698 bool activeTouchChanged = false;
3699 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
3700 int32_t activeTouchId = lastActiveTouchId;
3701 if (activeTouchId < 0) {
3702 if (!mCurrentTouch.idBits.isEmpty()) {
3703 activeTouchChanged = true;
3704 activeTouchId = mPointerGesture.activeTouchId = mCurrentTouch.idBits.firstMarkedBit();
3705 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08003706 }
Jeff Brown2352b972011-04-12 22:39:53 -07003707 } else if (!mCurrentTouch.idBits.hasBit(activeTouchId)) {
3708 activeTouchChanged = true;
3709 if (!mCurrentTouch.idBits.isEmpty()) {
3710 activeTouchId = mPointerGesture.activeTouchId = mCurrentTouch.idBits.firstMarkedBit();
3711 } else {
3712 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08003713 }
3714 }
3715
3716 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07003717 bool isQuietTime = false;
3718 if (activeTouchId < 0) {
3719 mPointerGesture.resetQuietTime();
3720 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07003721 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07003722 if (!isQuietTime) {
3723 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
3724 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3725 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
3726 && mCurrentTouch.pointerCount < 2) {
3727 // Enter quiet time when exiting swipe or freeform state.
3728 // This is to prevent accidentally entering the hover state and flinging the
3729 // pointer when finishing a swipe and there is still one pointer left onscreen.
3730 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07003731 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07003732 && mCurrentTouch.pointerCount >= 2
3733 && !isPointerDown(mCurrentTouch.buttonState)) {
3734 // Enter quiet time when releasing the button and there are still two or more
3735 // fingers down. This may indicate that one finger was used to press the button
3736 // but it has not gone up yet.
3737 isQuietTime = true;
3738 }
3739 if (isQuietTime) {
3740 mPointerGesture.quietTime = when;
3741 }
Jeff Brownace13b12011-03-09 17:39:48 -08003742 }
3743 }
3744
3745 // Switch states based on button and pointer state.
3746 if (isQuietTime) {
3747 // Case 1: Quiet time. (QUIET)
3748#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003749 LOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07003750 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08003751#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003752 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
3753 *outFinishPreviousGesture = true;
3754 }
Jeff Brownace13b12011-03-09 17:39:48 -08003755
3756 mPointerGesture.activeGestureId = -1;
3757 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08003758 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07003759
Jeff Brown19c97d462011-06-01 12:33:19 -07003760 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08003761 } else if (isPointerDown(mCurrentTouch.buttonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003762 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08003763 // The pointer follows the active touch point.
3764 // Emit DOWN, MOVE, UP events at the pointer location.
3765 //
3766 // Only the active touch matters; other fingers are ignored. This policy helps
3767 // to handle the case where the user places a second finger on the touch pad
3768 // to apply the necessary force to depress an integrated button below the surface.
3769 // We don't want the second finger to be delivered to applications.
3770 //
3771 // For this to work well, we need to make sure to track the pointer that is really
3772 // active. If the user first puts one finger down to click then adds another
3773 // finger to drag then the active pointer should switch to the finger that is
3774 // being dragged.
3775#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07003776 LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brownace13b12011-03-09 17:39:48 -08003777 "currentTouchPointerCount=%d", activeTouchId, mCurrentTouch.pointerCount);
3778#endif
3779 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07003780 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08003781 *outFinishPreviousGesture = true;
3782 mPointerGesture.activeGestureId = 0;
3783 }
3784
3785 // Switch pointers if needed.
3786 // Find the fastest pointer and follow it.
Jeff Brown19c97d462011-06-01 12:33:19 -07003787 if (activeTouchId >= 0 && mCurrentTouch.pointerCount > 1) {
3788 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07003789 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brown19c97d462011-06-01 12:33:19 -07003790 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
3791 uint32_t id = mCurrentTouch.pointers[i].id;
3792 float vx, vy;
3793 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
3794 float speed = hypotf(vx, vy);
3795 if (speed > bestSpeed) {
3796 bestId = id;
3797 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08003798 }
Jeff Brown8d608662010-08-30 03:02:23 -07003799 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003800 }
3801 if (bestId >= 0 && bestId != activeTouchId) {
3802 mPointerGesture.activeTouchId = activeTouchId = bestId;
3803 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08003804#if DEBUG_GESTURES
Jeff Brown19c97d462011-06-01 12:33:19 -07003805 LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
3806 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08003807#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07003808 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003809 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003810
Jeff Brown19c97d462011-06-01 12:33:19 -07003811 if (activeTouchId >= 0 && mLastTouch.idBits.hasBit(activeTouchId)) {
3812 const PointerData& currentPointer =
3813 mCurrentTouch.pointers[mCurrentTouch.idToIndex[activeTouchId]];
3814 const PointerData& lastPointer =
3815 mLastTouch.pointers[mLastTouch.idToIndex[activeTouchId]];
3816 float deltaX = (currentPointer.x - lastPointer.x)
3817 * mLocked.pointerGestureXMovementScale;
3818 float deltaY = (currentPointer.y - lastPointer.y)
3819 * mLocked.pointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07003820
Jeff Brown612891e2011-07-15 20:44:17 -07003821 rotateDelta(mLocked.surfaceOrientation, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07003822 mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
3823
3824 // Move the pointer using a relative motion.
3825 // When using spots, the click will occur at the position of the anchor
3826 // spot and all other spots will move there.
3827 mPointerController->move(deltaX, deltaY);
3828 } else {
3829 mPointerGesture.pointerVelocityControl.reset();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003830 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003831
Jeff Brownace13b12011-03-09 17:39:48 -08003832 float x, y;
3833 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08003834
Jeff Brown79ac9692011-04-19 21:20:10 -07003835 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08003836 mPointerGesture.currentGestureIdBits.clear();
3837 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3838 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003839 mPointerGesture.currentGestureProperties[0].clear();
3840 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3841 mPointerGesture.currentGestureProperties[0].toolType =
3842 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08003843 mPointerGesture.currentGestureCoords[0].clear();
3844 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3845 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3846 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
3847 } else if (mCurrentTouch.pointerCount == 0) {
3848 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003849 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
3850 *outFinishPreviousGesture = true;
3851 }
Jeff Brownace13b12011-03-09 17:39:48 -08003852
Jeff Brown79ac9692011-04-19 21:20:10 -07003853 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07003854 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08003855 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07003856 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
3857 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown2352b972011-04-12 22:39:53 -07003858 && mLastTouch.pointerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003859 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08003860 float x, y;
3861 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07003862 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
3863 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08003864#if DEBUG_GESTURES
3865 LOGD("Gestures: TAP");
3866#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07003867
3868 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07003869 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07003870 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07003871
Jeff Brownace13b12011-03-09 17:39:48 -08003872 mPointerGesture.activeGestureId = 0;
3873 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08003874 mPointerGesture.currentGestureIdBits.clear();
3875 mPointerGesture.currentGestureIdBits.markBit(
3876 mPointerGesture.activeGestureId);
3877 mPointerGesture.currentGestureIdToIndex[
3878 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003879 mPointerGesture.currentGestureProperties[0].clear();
3880 mPointerGesture.currentGestureProperties[0].id =
3881 mPointerGesture.activeGestureId;
3882 mPointerGesture.currentGestureProperties[0].toolType =
3883 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08003884 mPointerGesture.currentGestureCoords[0].clear();
3885 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07003886 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08003887 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07003888 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08003889 mPointerGesture.currentGestureCoords[0].setAxisValue(
3890 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07003891
Jeff Brownace13b12011-03-09 17:39:48 -08003892 tapped = true;
3893 } else {
3894#if DEBUG_GESTURES
3895 LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07003896 x - mPointerGesture.tapX,
3897 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08003898#endif
3899 }
3900 } else {
3901#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07003902 LOGD("Gestures: Not a TAP, %0.3fms since down",
3903 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08003904#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07003905 }
Jeff Brownace13b12011-03-09 17:39:48 -08003906 }
Jeff Brown2352b972011-04-12 22:39:53 -07003907
Jeff Brown19c97d462011-06-01 12:33:19 -07003908 mPointerGesture.pointerVelocityControl.reset();
3909
Jeff Brownace13b12011-03-09 17:39:48 -08003910 if (!tapped) {
3911#if DEBUG_GESTURES
3912 LOGD("Gestures: NEUTRAL");
3913#endif
3914 mPointerGesture.activeGestureId = -1;
3915 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08003916 mPointerGesture.currentGestureIdBits.clear();
3917 }
3918 } else if (mCurrentTouch.pointerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003919 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08003920 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07003921 // When in HOVER, emit HOVER_MOVE events at the pointer location.
3922 // When in TAP_DRAG, emit MOVE events at the pointer location.
Jeff Brownb6110c22011-04-01 16:15:13 -07003923 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08003924
Jeff Brown79ac9692011-04-19 21:20:10 -07003925 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
3926 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003927 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003928 float x, y;
3929 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07003930 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
3931 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003932 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
3933 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08003934#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07003935 LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
3936 x - mPointerGesture.tapX,
3937 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08003938#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07003939 }
3940 } else {
3941#if DEBUG_GESTURES
3942 LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
3943 (when - mPointerGesture.tapUpTime) * 0.000001f);
3944#endif
3945 }
3946 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
3947 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
3948 }
Jeff Brownace13b12011-03-09 17:39:48 -08003949
3950 if (mLastTouch.idBits.hasBit(activeTouchId)) {
3951 const PointerData& currentPointer =
3952 mCurrentTouch.pointers[mCurrentTouch.idToIndex[activeTouchId]];
3953 const PointerData& lastPointer =
3954 mLastTouch.pointers[mLastTouch.idToIndex[activeTouchId]];
3955 float deltaX = (currentPointer.x - lastPointer.x)
3956 * mLocked.pointerGestureXMovementScale;
3957 float deltaY = (currentPointer.y - lastPointer.y)
3958 * mLocked.pointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07003959
Jeff Brown612891e2011-07-15 20:44:17 -07003960 rotateDelta(mLocked.surfaceOrientation, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07003961 mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
3962
Jeff Brown2352b972011-04-12 22:39:53 -07003963 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07003964 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08003965 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07003966 } else {
3967 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08003968 }
3969
Jeff Brown79ac9692011-04-19 21:20:10 -07003970 bool down;
3971 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
3972#if DEBUG_GESTURES
3973 LOGD("Gestures: TAP_DRAG");
3974#endif
3975 down = true;
3976 } else {
3977#if DEBUG_GESTURES
3978 LOGD("Gestures: HOVER");
3979#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003980 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
3981 *outFinishPreviousGesture = true;
3982 }
Jeff Brown79ac9692011-04-19 21:20:10 -07003983 mPointerGesture.activeGestureId = 0;
3984 down = false;
3985 }
Jeff Brownace13b12011-03-09 17:39:48 -08003986
3987 float x, y;
3988 mPointerController->getPosition(&x, &y);
3989
Jeff Brownace13b12011-03-09 17:39:48 -08003990 mPointerGesture.currentGestureIdBits.clear();
3991 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3992 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003993 mPointerGesture.currentGestureProperties[0].clear();
3994 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3995 mPointerGesture.currentGestureProperties[0].toolType =
3996 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08003997 mPointerGesture.currentGestureCoords[0].clear();
3998 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3999 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004000 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4001 down ? 1.0f : 0.0f);
4002
Jeff Brownace13b12011-03-09 17:39:48 -08004003 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004004 mPointerGesture.resetTap();
4005 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004006 mPointerGesture.tapX = x;
4007 mPointerGesture.tapY = y;
4008 }
Jeff Brownace13b12011-03-09 17:39:48 -08004009 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004010 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4011 // We need to provide feedback for each finger that goes down so we cannot wait
4012 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004013 //
Jeff Brown2352b972011-04-12 22:39:53 -07004014 // The ambiguous case is deciding what to do when there are two fingers down but they
4015 // have not moved enough to determine whether they are part of a drag or part of a
4016 // freeform gesture, or just a press or long-press at the pointer location.
4017 //
4018 // When there are two fingers we start with the PRESS hypothesis and we generate a
4019 // down at the pointer location.
4020 //
4021 // When the two fingers move enough or when additional fingers are added, we make
4022 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Jeff Brownb6110c22011-04-01 16:15:13 -07004023 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004024
Jeff Brown214eaf42011-05-26 19:17:02 -07004025 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004026 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004027 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004028 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4029 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004030 *outFinishPreviousGesture = true;
Jeff Brown19c97d462011-06-01 12:33:19 -07004031 } else if (!settled && mCurrentTouch.pointerCount > mLastTouch.pointerCount) {
4032 // Additional pointers have gone down but not yet settled.
4033 // Reset the gesture.
4034#if DEBUG_GESTURES
4035 LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004036 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004037 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004038 * 0.000001f);
4039#endif
4040 *outCancelPreviousGesture = true;
4041 } else {
4042 // Continue previous gesture.
4043 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4044 }
4045
4046 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004047 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4048 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004049 mPointerGesture.referenceIdBits.clear();
Jeff Brown19c97d462011-06-01 12:33:19 -07004050 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004051
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004052 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004053#if DEBUG_GESTURES
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004054 LOGD("Gestures: Using centroid as reference for MULTITOUCH, "
4055 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004056 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004057 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004058#endif
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004059 mCurrentTouch.getCentroid(&mPointerGesture.referenceTouchX,
4060 &mPointerGesture.referenceTouchY);
4061 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4062 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004063 }
Jeff Brownace13b12011-03-09 17:39:48 -08004064
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004065 // Clear the reference deltas for fingers not yet included in the reference calculation.
4066 for (BitSet32 idBits(mCurrentTouch.idBits.value & ~mPointerGesture.referenceIdBits.value);
4067 !idBits.isEmpty(); ) {
4068 uint32_t id = idBits.firstMarkedBit();
4069 idBits.clearBit(id);
4070
4071 mPointerGesture.referenceDeltas[id].dx = 0;
4072 mPointerGesture.referenceDeltas[id].dy = 0;
4073 }
4074 mPointerGesture.referenceIdBits = mCurrentTouch.idBits;
4075
4076 // Add delta for all fingers and calculate a common movement delta.
4077 float commonDeltaX = 0, commonDeltaY = 0;
4078 BitSet32 commonIdBits(mLastTouch.idBits.value & mCurrentTouch.idBits.value);
4079 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4080 bool first = (idBits == commonIdBits);
4081 uint32_t id = idBits.firstMarkedBit();
4082 idBits.clearBit(id);
4083
4084 const PointerData& cpd = mCurrentTouch.pointers[mCurrentTouch.idToIndex[id]];
4085 const PointerData& lpd = mLastTouch.pointers[mLastTouch.idToIndex[id]];
4086 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4087 delta.dx += cpd.x - lpd.x;
4088 delta.dy += cpd.y - lpd.y;
4089
4090 if (first) {
4091 commonDeltaX = delta.dx;
4092 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004093 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004094 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4095 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4096 }
4097 }
Jeff Brownace13b12011-03-09 17:39:48 -08004098
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004099 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4100 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4101 float dist[MAX_POINTER_ID + 1];
4102 int32_t distOverThreshold = 0;
4103 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
4104 uint32_t id = idBits.firstMarkedBit();
4105 idBits.clearBit(id);
Jeff Brownace13b12011-03-09 17:39:48 -08004106
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004107 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4108 dist[id] = hypotf(delta.dx * mLocked.pointerGestureXZoomScale,
4109 delta.dy * mLocked.pointerGestureYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004110 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004111 distOverThreshold += 1;
4112 }
4113 }
4114
4115 // Only transition when at least two pointers have moved further than
4116 // the minimum distance threshold.
4117 if (distOverThreshold >= 2) {
4118 float d;
4119 if (mCurrentTouch.pointerCount > 2) {
4120 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004121#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004122 LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
4123 mCurrentTouch.pointerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004124#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004125 *outCancelPreviousGesture = true;
4126 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4127 } else if (((d = distance(
4128 mCurrentTouch.pointers[0].x, mCurrentTouch.pointers[0].y,
4129 mCurrentTouch.pointers[1].x, mCurrentTouch.pointers[1].y))
4130 > mLocked.pointerGestureMaxSwipeWidth)) {
4131 // There are two pointers but they are too far apart for a SWIPE,
4132 // switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004133#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004134 LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
4135 d, mLocked.pointerGestureMaxSwipeWidth);
Jeff Brown2352b972011-04-12 22:39:53 -07004136#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004137 *outCancelPreviousGesture = true;
4138 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4139 } else {
4140 // There are two pointers. Wait for both pointers to start moving
4141 // before deciding whether this is a SWIPE or FREEFORM gesture.
4142 uint32_t id1 = mCurrentTouch.pointers[0].id;
4143 uint32_t id2 = mCurrentTouch.pointers[1].id;
4144 float dist1 = dist[id1];
4145 float dist2 = dist[id2];
Jeff Brown474dcb52011-06-14 20:22:50 -07004146 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4147 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004148 // Calculate the dot product of the displacement vectors.
4149 // When the vectors are oriented in approximately the same direction,
4150 // the angle betweeen them is near zero and the cosine of the angle
4151 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4152 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4153 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
Jeff Brown6674d9b2011-06-07 16:50:14 -07004154 float dx1 = delta1.dx * mLocked.pointerGestureXZoomScale;
4155 float dy1 = delta1.dy * mLocked.pointerGestureYZoomScale;
4156 float dx2 = delta2.dx * mLocked.pointerGestureXZoomScale;
4157 float dy2 = delta2.dy * mLocked.pointerGestureYZoomScale;
4158 float dot = dx1 * dx2 + dy1 * dy2;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004159 float cosine = dot / (dist1 * dist2); // denominator always > 0
Jeff Brown474dcb52011-06-14 20:22:50 -07004160 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004161 // Pointers are moving in the same direction. Switch to SWIPE.
4162#if DEBUG_GESTURES
4163 LOGD("Gestures: PRESS transitioned to SWIPE, "
4164 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4165 "cosine %0.3f >= %0.3f",
Jeff Brown474dcb52011-06-14 20:22:50 -07004166 dist1, mConfig.pointerGestureMultitouchMinDistance,
4167 dist2, mConfig.pointerGestureMultitouchMinDistance,
4168 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004169#endif
4170 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4171 } else {
4172 // Pointers are moving in different directions. Switch to FREEFORM.
4173#if DEBUG_GESTURES
4174 LOGD("Gestures: PRESS transitioned to FREEFORM, "
4175 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4176 "cosine %0.3f < %0.3f",
Jeff Brown474dcb52011-06-14 20:22:50 -07004177 dist1, mConfig.pointerGestureMultitouchMinDistance,
4178 dist2, mConfig.pointerGestureMultitouchMinDistance,
4179 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004180#endif
4181 *outCancelPreviousGesture = true;
4182 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4183 }
Jeff Brownace13b12011-03-09 17:39:48 -08004184 }
4185 }
Jeff Brownace13b12011-03-09 17:39:48 -08004186 }
4187 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004188 // Switch from SWIPE to FREEFORM if additional pointers go down.
4189 // Cancel previous gesture.
4190 if (mCurrentTouch.pointerCount > 2) {
4191#if DEBUG_GESTURES
4192 LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
4193 mCurrentTouch.pointerCount);
4194#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004195 *outCancelPreviousGesture = true;
4196 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004197 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004198 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004199
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004200 // Move the reference points based on the overall group motion of the fingers
4201 // except in PRESS mode while waiting for a transition to occur.
4202 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4203 && (commonDeltaX || commonDeltaY)) {
4204 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brown2352b972011-04-12 22:39:53 -07004205 uint32_t id = idBits.firstMarkedBit();
4206 idBits.clearBit(id);
4207
Jeff Brown538881e2011-05-25 18:23:38 -07004208 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004209 delta.dx = 0;
4210 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004211 }
4212
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004213 mPointerGesture.referenceTouchX += commonDeltaX;
4214 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004215
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004216 commonDeltaX *= mLocked.pointerGestureXMovementScale;
4217 commonDeltaY *= mLocked.pointerGestureYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07004218
4219 rotateDelta(mLocked.surfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004220 mPointerGesture.pointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004221
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004222 mPointerGesture.referenceGestureX += commonDeltaX;
4223 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07004224 }
4225
4226 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07004227 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
4228 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4229 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004230#if DEBUG_GESTURES
Jeff Brown612891e2011-07-15 20:44:17 -07004231 LOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07004232 "activeGestureId=%d, currentTouchPointerCount=%d",
4233 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
4234#endif
4235 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
4236
4237 mPointerGesture.currentGestureIdBits.clear();
4238 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4239 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004240 mPointerGesture.currentGestureProperties[0].clear();
4241 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4242 mPointerGesture.currentGestureProperties[0].toolType =
4243 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004244 mPointerGesture.currentGestureCoords[0].clear();
4245 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4246 mPointerGesture.referenceGestureX);
4247 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4248 mPointerGesture.referenceGestureY);
4249 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08004250 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4251 // FREEFORM mode.
4252#if DEBUG_GESTURES
4253 LOGD("Gestures: FREEFORM activeTouchId=%d,"
4254 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown2352b972011-04-12 22:39:53 -07004255 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004256#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004257 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004258
Jeff Brownace13b12011-03-09 17:39:48 -08004259 mPointerGesture.currentGestureIdBits.clear();
4260
4261 BitSet32 mappedTouchIdBits;
4262 BitSet32 usedGestureIdBits;
4263 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4264 // Initially, assign the active gesture id to the active touch point
4265 // if there is one. No other touch id bits are mapped yet.
4266 if (!*outCancelPreviousGesture) {
4267 mappedTouchIdBits.markBit(activeTouchId);
4268 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4269 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4270 mPointerGesture.activeGestureId;
4271 } else {
4272 mPointerGesture.activeGestureId = -1;
4273 }
4274 } else {
4275 // Otherwise, assume we mapped all touches from the previous frame.
4276 // Reuse all mappings that are still applicable.
4277 mappedTouchIdBits.value = mLastTouch.idBits.value & mCurrentTouch.idBits.value;
4278 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4279
4280 // Check whether we need to choose a new active gesture id because the
4281 // current went went up.
4282 for (BitSet32 upTouchIdBits(mLastTouch.idBits.value & ~mCurrentTouch.idBits.value);
4283 !upTouchIdBits.isEmpty(); ) {
4284 uint32_t upTouchId = upTouchIdBits.firstMarkedBit();
4285 upTouchIdBits.clearBit(upTouchId);
4286 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4287 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4288 mPointerGesture.activeGestureId = -1;
4289 break;
4290 }
4291 }
4292 }
4293
4294#if DEBUG_GESTURES
4295 LOGD("Gestures: FREEFORM follow up "
4296 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4297 "activeGestureId=%d",
4298 mappedTouchIdBits.value, usedGestureIdBits.value,
4299 mPointerGesture.activeGestureId);
4300#endif
4301
Jeff Brown2352b972011-04-12 22:39:53 -07004302 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
Jeff Brownace13b12011-03-09 17:39:48 -08004303 uint32_t touchId = mCurrentTouch.pointers[i].id;
4304 uint32_t gestureId;
4305 if (!mappedTouchIdBits.hasBit(touchId)) {
4306 gestureId = usedGestureIdBits.firstUnmarkedBit();
4307 usedGestureIdBits.markBit(gestureId);
4308 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4309#if DEBUG_GESTURES
4310 LOGD("Gestures: FREEFORM "
4311 "new mapping for touch id %d -> gesture id %d",
4312 touchId, gestureId);
4313#endif
4314 } else {
4315 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4316#if DEBUG_GESTURES
4317 LOGD("Gestures: FREEFORM "
4318 "existing mapping for touch id %d -> gesture id %d",
4319 touchId, gestureId);
4320#endif
4321 }
4322 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4323 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4324
Jeff Brown612891e2011-07-15 20:44:17 -07004325 float deltaX = (mCurrentTouch.pointers[i].x - mPointerGesture.referenceTouchX)
4326 * mLocked.pointerGestureXZoomScale;
4327 float deltaY = (mCurrentTouch.pointers[i].y - mPointerGesture.referenceTouchY)
4328 * mLocked.pointerGestureYZoomScale;
4329 rotateDelta(mLocked.surfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004330
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004331 mPointerGesture.currentGestureProperties[i].clear();
4332 mPointerGesture.currentGestureProperties[i].id = gestureId;
4333 mPointerGesture.currentGestureProperties[i].toolType =
4334 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004335 mPointerGesture.currentGestureCoords[i].clear();
4336 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004337 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08004338 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004339 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004340 mPointerGesture.currentGestureCoords[i].setAxisValue(
4341 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4342 }
4343
4344 if (mPointerGesture.activeGestureId < 0) {
4345 mPointerGesture.activeGestureId =
4346 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4347#if DEBUG_GESTURES
4348 LOGD("Gestures: FREEFORM new "
4349 "activeGestureId=%d", mPointerGesture.activeGestureId);
4350#endif
4351 }
Jeff Brown2352b972011-04-12 22:39:53 -07004352 }
Jeff Brownace13b12011-03-09 17:39:48 -08004353 }
4354
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004355 mPointerController->setButtonState(mCurrentTouch.buttonState);
4356
Jeff Brownace13b12011-03-09 17:39:48 -08004357#if DEBUG_GESTURES
4358 LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004359 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4360 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004361 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004362 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4363 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004364 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
4365 uint32_t id = idBits.firstMarkedBit();
4366 idBits.clearBit(id);
4367 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004368 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004369 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004370 LOGD(" currentGesture[%d]: index=%d, toolType=%d, "
4371 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4372 id, index, properties.toolType,
4373 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004374 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4375 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4376 }
4377 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
4378 uint32_t id = idBits.firstMarkedBit();
4379 idBits.clearBit(id);
4380 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004381 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004382 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004383 LOGD(" lastGesture[%d]: index=%d, toolType=%d, "
4384 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4385 id, index, properties.toolType,
4386 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004387 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4388 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4389 }
4390#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004391 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004392}
4393
4394void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004395 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
4396 const PointerProperties* properties, const PointerCoords* coords,
4397 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08004398 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
4399 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004400 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08004401 uint32_t pointerCount = 0;
4402 while (!idBits.isEmpty()) {
4403 uint32_t id = idBits.firstMarkedBit();
4404 idBits.clearBit(id);
4405 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004406 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004407 pointerCoords[pointerCount].copyFrom(coords[index]);
4408
4409 if (changedId >= 0 && id == uint32_t(changedId)) {
4410 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
4411 }
4412
4413 pointerCount += 1;
4414 }
4415
Jeff Brownb6110c22011-04-01 16:15:13 -07004416 LOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004417
4418 if (changedId >= 0 && pointerCount == 1) {
4419 // Replace initial down and final up action.
4420 // We can compare the action without masking off the changed pointer index
4421 // because we know the index is 0.
4422 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
4423 action = AMOTION_EVENT_ACTION_DOWN;
4424 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
4425 action = AMOTION_EVENT_ACTION_UP;
4426 } else {
4427 // Can't happen.
Jeff Brownb6110c22011-04-01 16:15:13 -07004428 LOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08004429 }
4430 }
4431
4432 getDispatcher()->notifyMotion(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004433 action, flags, metaState, buttonState, edgeFlags,
4434 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownace13b12011-03-09 17:39:48 -08004435}
4436
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004437bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004438 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004439 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
4440 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08004441 bool changed = false;
4442 while (!idBits.isEmpty()) {
4443 uint32_t id = idBits.firstMarkedBit();
4444 idBits.clearBit(id);
4445
4446 uint32_t inIndex = inIdToIndex[id];
4447 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004448
4449 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004450 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004451 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004452 PointerCoords& curOutCoords = outCoords[outIndex];
4453
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004454 if (curInProperties != curOutProperties) {
4455 curOutProperties.copyFrom(curInProperties);
4456 changed = true;
4457 }
4458
Jeff Brownace13b12011-03-09 17:39:48 -08004459 if (curInCoords != curOutCoords) {
4460 curOutCoords.copyFrom(curInCoords);
4461 changed = true;
4462 }
4463 }
4464 return changed;
4465}
4466
4467void TouchInputMapper::fadePointer() {
4468 { // acquire lock
4469 AutoMutex _l(mLock);
4470 if (mPointerController != NULL) {
Jeff Brown538881e2011-05-25 18:23:38 -07004471 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
Jeff Brownace13b12011-03-09 17:39:48 -08004472 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004473 } // release lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004474}
4475
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004476int32_t TouchInputMapper::getTouchToolType(bool isStylus) const {
4477 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
4478 return isStylus ? AMOTION_EVENT_TOOL_TYPE_STYLUS : AMOTION_EVENT_TOOL_TYPE_FINGER;
4479 } else {
4480 return isStylus ? AMOTION_EVENT_TOOL_TYPE_INDIRECT_STYLUS
4481 : AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
4482 }
4483}
4484
Jeff Brown6328cdc2010-07-29 18:18:33 -07004485bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) {
Jeff Brown9626b142011-03-03 02:09:54 -08004486 return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue
4487 && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004488}
4489
Jeff Brown6328cdc2010-07-29 18:18:33 -07004490const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked(
4491 int32_t x, int32_t y) {
4492 size_t numVirtualKeys = mLocked.virtualKeys.size();
4493 for (size_t i = 0; i < numVirtualKeys; i++) {
4494 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004495
4496#if DEBUG_VIRTUAL_KEYS
4497 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
4498 "left=%d, top=%d, right=%d, bottom=%d",
4499 x, y,
4500 virtualKey.keyCode, virtualKey.scanCode,
4501 virtualKey.hitLeft, virtualKey.hitTop,
4502 virtualKey.hitRight, virtualKey.hitBottom);
4503#endif
4504
4505 if (virtualKey.isHit(x, y)) {
4506 return & virtualKey;
4507 }
4508 }
4509
4510 return NULL;
4511}
4512
4513void TouchInputMapper::calculatePointerIds() {
4514 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
4515 uint32_t lastPointerCount = mLastTouch.pointerCount;
4516
4517 if (currentPointerCount == 0) {
4518 // No pointers to assign.
4519 mCurrentTouch.idBits.clear();
4520 } else if (lastPointerCount == 0) {
4521 // All pointers are new.
4522 mCurrentTouch.idBits.clear();
4523 for (uint32_t i = 0; i < currentPointerCount; i++) {
4524 mCurrentTouch.pointers[i].id = i;
4525 mCurrentTouch.idToIndex[i] = i;
4526 mCurrentTouch.idBits.markBit(i);
4527 }
4528 } else if (currentPointerCount == 1 && lastPointerCount == 1) {
4529 // Only one pointer and no change in count so it must have the same id as before.
4530 uint32_t id = mLastTouch.pointers[0].id;
4531 mCurrentTouch.pointers[0].id = id;
4532 mCurrentTouch.idToIndex[id] = 0;
4533 mCurrentTouch.idBits.value = BitSet32::valueForBit(id);
4534 } else {
4535 // General case.
4536 // We build a heap of squared euclidean distances between current and last pointers
4537 // associated with the current and last pointer indices. Then, we find the best
4538 // match (by distance) for each current pointer.
4539 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
4540
4541 uint32_t heapSize = 0;
4542 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
4543 currentPointerIndex++) {
4544 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
4545 lastPointerIndex++) {
4546 int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x
4547 - mLastTouch.pointers[lastPointerIndex].x;
4548 int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y
4549 - mLastTouch.pointers[lastPointerIndex].y;
4550
4551 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
4552
4553 // Insert new element into the heap (sift up).
4554 heap[heapSize].currentPointerIndex = currentPointerIndex;
4555 heap[heapSize].lastPointerIndex = lastPointerIndex;
4556 heap[heapSize].distance = distance;
4557 heapSize += 1;
4558 }
4559 }
4560
4561 // Heapify
4562 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
4563 startIndex -= 1;
4564 for (uint32_t parentIndex = startIndex; ;) {
4565 uint32_t childIndex = parentIndex * 2 + 1;
4566 if (childIndex >= heapSize) {
4567 break;
4568 }
4569
4570 if (childIndex + 1 < heapSize
4571 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4572 childIndex += 1;
4573 }
4574
4575 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4576 break;
4577 }
4578
4579 swap(heap[parentIndex], heap[childIndex]);
4580 parentIndex = childIndex;
4581 }
4582 }
4583
4584#if DEBUG_POINTER_ASSIGNMENT
4585 LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize);
4586 for (size_t i = 0; i < heapSize; i++) {
4587 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4588 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4589 heap[i].distance);
4590 }
4591#endif
4592
4593 // Pull matches out by increasing order of distance.
4594 // To avoid reassigning pointers that have already been matched, the loop keeps track
4595 // of which last and current pointers have been matched using the matchedXXXBits variables.
4596 // It also tracks the used pointer id bits.
4597 BitSet32 matchedLastBits(0);
4598 BitSet32 matchedCurrentBits(0);
4599 BitSet32 usedIdBits(0);
4600 bool first = true;
4601 for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) {
4602 for (;;) {
4603 if (first) {
4604 // The first time through the loop, we just consume the root element of
4605 // the heap (the one with smallest distance).
4606 first = false;
4607 } else {
4608 // Previous iterations consumed the root element of the heap.
4609 // Pop root element off of the heap (sift down).
4610 heapSize -= 1;
Jeff Brownb6110c22011-04-01 16:15:13 -07004611 LOG_ASSERT(heapSize > 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004612
4613 // Sift down.
4614 heap[0] = heap[heapSize];
4615 for (uint32_t parentIndex = 0; ;) {
4616 uint32_t childIndex = parentIndex * 2 + 1;
4617 if (childIndex >= heapSize) {
4618 break;
4619 }
4620
4621 if (childIndex + 1 < heapSize
4622 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4623 childIndex += 1;
4624 }
4625
4626 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4627 break;
4628 }
4629
4630 swap(heap[parentIndex], heap[childIndex]);
4631 parentIndex = childIndex;
4632 }
4633
4634#if DEBUG_POINTER_ASSIGNMENT
4635 LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize);
4636 for (size_t i = 0; i < heapSize; i++) {
4637 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4638 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4639 heap[i].distance);
4640 }
4641#endif
4642 }
4643
4644 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
4645 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
4646
4647 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
4648 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
4649
4650 matchedCurrentBits.markBit(currentPointerIndex);
4651 matchedLastBits.markBit(lastPointerIndex);
4652
4653 uint32_t id = mLastTouch.pointers[lastPointerIndex].id;
4654 mCurrentTouch.pointers[currentPointerIndex].id = id;
4655 mCurrentTouch.idToIndex[id] = currentPointerIndex;
4656 usedIdBits.markBit(id);
4657
4658#if DEBUG_POINTER_ASSIGNMENT
4659 LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
4660 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
4661#endif
4662 break;
4663 }
4664 }
4665
4666 // Assign fresh ids to new pointers.
4667 if (currentPointerCount > lastPointerCount) {
4668 for (uint32_t i = currentPointerCount - lastPointerCount; ;) {
4669 uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit();
4670 uint32_t id = usedIdBits.firstUnmarkedBit();
4671
4672 mCurrentTouch.pointers[currentPointerIndex].id = id;
4673 mCurrentTouch.idToIndex[id] = currentPointerIndex;
4674 usedIdBits.markBit(id);
4675
4676#if DEBUG_POINTER_ASSIGNMENT
4677 LOGD("calculatePointerIds - assigned: cur=%d, id=%d",
4678 currentPointerIndex, id);
4679#endif
4680
4681 if (--i == 0) break; // done
4682 matchedCurrentBits.markBit(currentPointerIndex);
4683 }
4684 }
4685
4686 // Fix id bits.
4687 mCurrentTouch.idBits = usedIdBits;
4688 }
4689}
4690
Jeff Brown6d0fec22010-07-23 21:28:06 -07004691int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07004692 { // acquire lock
4693 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004694
Jeff Brown6328cdc2010-07-29 18:18:33 -07004695 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004696 return AKEY_STATE_VIRTUAL;
4697 }
4698
Jeff Brown6328cdc2010-07-29 18:18:33 -07004699 size_t numVirtualKeys = mLocked.virtualKeys.size();
4700 for (size_t i = 0; i < numVirtualKeys; i++) {
4701 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004702 if (virtualKey.keyCode == keyCode) {
4703 return AKEY_STATE_UP;
4704 }
4705 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004706 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07004707
4708 return AKEY_STATE_UNKNOWN;
4709}
4710
4711int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07004712 { // acquire lock
4713 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004714
Jeff Brown6328cdc2010-07-29 18:18:33 -07004715 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004716 return AKEY_STATE_VIRTUAL;
4717 }
4718
Jeff Brown6328cdc2010-07-29 18:18:33 -07004719 size_t numVirtualKeys = mLocked.virtualKeys.size();
4720 for (size_t i = 0; i < numVirtualKeys; i++) {
4721 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004722 if (virtualKey.scanCode == scanCode) {
4723 return AKEY_STATE_UP;
4724 }
4725 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004726 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07004727
4728 return AKEY_STATE_UNKNOWN;
4729}
4730
4731bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
4732 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07004733 { // acquire lock
4734 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004735
Jeff Brown6328cdc2010-07-29 18:18:33 -07004736 size_t numVirtualKeys = mLocked.virtualKeys.size();
4737 for (size_t i = 0; i < numVirtualKeys; i++) {
4738 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004739
4740 for (size_t i = 0; i < numCodes; i++) {
4741 if (virtualKey.keyCode == keyCodes[i]) {
4742 outFlags[i] = 1;
4743 }
4744 }
4745 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004746 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07004747
4748 return true;
4749}
4750
4751
4752// --- SingleTouchInputMapper ---
4753
Jeff Brown47e6b1b2010-11-29 17:37:49 -08004754SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
4755 TouchInputMapper(device) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07004756 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004757}
4758
4759SingleTouchInputMapper::~SingleTouchInputMapper() {
4760}
4761
Jeff Brown80fd47c2011-05-24 01:07:44 -07004762void SingleTouchInputMapper::clearState() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004763 mAccumulator.clear();
4764
4765 mDown = false;
4766 mX = 0;
4767 mY = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07004768 mPressure = 0; // default to 0 for devices that don't report pressure
4769 mToolWidth = 0; // default to 0 for devices that don't report tool width
Jeff Brownace13b12011-03-09 17:39:48 -08004770 mButtonState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004771}
4772
4773void SingleTouchInputMapper::reset() {
4774 TouchInputMapper::reset();
4775
Jeff Brown80fd47c2011-05-24 01:07:44 -07004776 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004777 }
4778
4779void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
4780 switch (rawEvent->type) {
4781 case EV_KEY:
4782 switch (rawEvent->scanCode) {
4783 case BTN_TOUCH:
4784 mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH;
4785 mAccumulator.btnTouch = rawEvent->value != 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07004786 // Don't sync immediately. Wait until the next SYN_REPORT since we might
4787 // not have received valid position information yet. This logic assumes that
4788 // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet.
Jeff Brown6d0fec22010-07-23 21:28:06 -07004789 break;
Jeff Brownace13b12011-03-09 17:39:48 -08004790 default:
4791 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004792 int32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode);
Jeff Brownace13b12011-03-09 17:39:48 -08004793 if (buttonState) {
4794 if (rawEvent->value) {
4795 mAccumulator.buttonDown |= buttonState;
4796 } else {
4797 mAccumulator.buttonUp |= buttonState;
4798 }
4799 mAccumulator.fields |= Accumulator::FIELD_BUTTONS;
4800 }
4801 }
4802 break;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004803 }
4804 break;
4805
4806 case EV_ABS:
4807 switch (rawEvent->scanCode) {
4808 case ABS_X:
4809 mAccumulator.fields |= Accumulator::FIELD_ABS_X;
4810 mAccumulator.absX = rawEvent->value;
4811 break;
4812 case ABS_Y:
4813 mAccumulator.fields |= Accumulator::FIELD_ABS_Y;
4814 mAccumulator.absY = rawEvent->value;
4815 break;
4816 case ABS_PRESSURE:
4817 mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE;
4818 mAccumulator.absPressure = rawEvent->value;
4819 break;
4820 case ABS_TOOL_WIDTH:
4821 mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH;
4822 mAccumulator.absToolWidth = rawEvent->value;
4823 break;
4824 }
4825 break;
4826
4827 case EV_SYN:
4828 switch (rawEvent->scanCode) {
4829 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07004830 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004831 break;
4832 }
4833 break;
4834 }
4835}
4836
4837void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004838 uint32_t fields = mAccumulator.fields;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07004839 if (fields == 0) {
4840 return; // no new state changes, so nothing to do
4841 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004842
4843 if (fields & Accumulator::FIELD_BTN_TOUCH) {
4844 mDown = mAccumulator.btnTouch;
4845 }
4846
4847 if (fields & Accumulator::FIELD_ABS_X) {
4848 mX = mAccumulator.absX;
4849 }
4850
4851 if (fields & Accumulator::FIELD_ABS_Y) {
4852 mY = mAccumulator.absY;
4853 }
4854
4855 if (fields & Accumulator::FIELD_ABS_PRESSURE) {
4856 mPressure = mAccumulator.absPressure;
4857 }
4858
4859 if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) {
Jeff Brown8d608662010-08-30 03:02:23 -07004860 mToolWidth = mAccumulator.absToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004861 }
4862
Jeff Brownace13b12011-03-09 17:39:48 -08004863 if (fields & Accumulator::FIELD_BUTTONS) {
4864 mButtonState = (mButtonState | mAccumulator.buttonDown) & ~mAccumulator.buttonUp;
4865 }
4866
Jeff Brown6d0fec22010-07-23 21:28:06 -07004867 mCurrentTouch.clear();
4868
4869 if (mDown) {
4870 mCurrentTouch.pointerCount = 1;
4871 mCurrentTouch.pointers[0].id = 0;
4872 mCurrentTouch.pointers[0].x = mX;
4873 mCurrentTouch.pointers[0].y = mY;
4874 mCurrentTouch.pointers[0].pressure = mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -07004875 mCurrentTouch.pointers[0].touchMajor = 0;
4876 mCurrentTouch.pointers[0].touchMinor = 0;
4877 mCurrentTouch.pointers[0].toolMajor = mToolWidth;
4878 mCurrentTouch.pointers[0].toolMinor = mToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004879 mCurrentTouch.pointers[0].orientation = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07004880 mCurrentTouch.pointers[0].distance = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004881 mCurrentTouch.pointers[0].isStylus = false; // TODO: Set stylus
Jeff Brown6d0fec22010-07-23 21:28:06 -07004882 mCurrentTouch.idToIndex[0] = 0;
4883 mCurrentTouch.idBits.markBit(0);
Jeff Brownace13b12011-03-09 17:39:48 -08004884 mCurrentTouch.buttonState = mButtonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004885 }
4886
4887 syncTouch(when, true);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07004888
4889 mAccumulator.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004890}
4891
Jeff Brown8d608662010-08-30 03:02:23 -07004892void SingleTouchInputMapper::configureRawAxes() {
4893 TouchInputMapper::configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004894
Jeff Brown8d608662010-08-30 03:02:23 -07004895 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x);
4896 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y);
4897 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure);
4898 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004899}
4900
4901
4902// --- MultiTouchInputMapper ---
4903
Jeff Brown47e6b1b2010-11-29 17:37:49 -08004904MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown80fd47c2011-05-24 01:07:44 -07004905 TouchInputMapper(device), mSlotCount(0), mUsingSlotsProtocol(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004906}
4907
4908MultiTouchInputMapper::~MultiTouchInputMapper() {
4909}
4910
Jeff Brown80fd47c2011-05-24 01:07:44 -07004911void MultiTouchInputMapper::clearState() {
Jeff Brown441a9c22011-06-02 18:22:25 -07004912 mAccumulator.clearSlots(mSlotCount);
4913 mAccumulator.clearButtons();
Jeff Brownace13b12011-03-09 17:39:48 -08004914 mButtonState = 0;
Jeff Brown6894a292011-07-01 17:59:27 -07004915 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07004916
4917 if (mUsingSlotsProtocol) {
4918 // Query the driver for the current slot index and use it as the initial slot
4919 // before we start reading events from the device. It is possible that the
4920 // current slot index will not be the same as it was when the first event was
4921 // written into the evdev buffer, which means the input mapper could start
4922 // out of sync with the initial state of the events in the evdev buffer.
4923 // In the extremely unlikely case that this happens, the data from
4924 // two slots will be confused until the next ABS_MT_SLOT event is received.
4925 // This can cause the touch point to "jump", but at least there will be
4926 // no stuck touches.
4927 status_t status = getEventHub()->getAbsoluteAxisValue(getDeviceId(), ABS_MT_SLOT,
4928 &mAccumulator.currentSlot);
4929 if (status) {
4930 LOGW("Could not retrieve current multitouch slot index. status=%d", status);
4931 mAccumulator.currentSlot = -1;
4932 }
4933 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004934}
4935
4936void MultiTouchInputMapper::reset() {
4937 TouchInputMapper::reset();
4938
Jeff Brown80fd47c2011-05-24 01:07:44 -07004939 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004940}
4941
4942void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
4943 switch (rawEvent->type) {
Jeff Brownace13b12011-03-09 17:39:48 -08004944 case EV_KEY: {
4945 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004946 int32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode);
Jeff Brownace13b12011-03-09 17:39:48 -08004947 if (buttonState) {
4948 if (rawEvent->value) {
4949 mAccumulator.buttonDown |= buttonState;
4950 } else {
4951 mAccumulator.buttonUp |= buttonState;
4952 }
4953 }
4954 }
4955 break;
4956 }
4957
Jeff Brown6d0fec22010-07-23 21:28:06 -07004958 case EV_ABS: {
Jeff Brown80fd47c2011-05-24 01:07:44 -07004959 bool newSlot = false;
4960 if (mUsingSlotsProtocol && rawEvent->scanCode == ABS_MT_SLOT) {
4961 mAccumulator.currentSlot = rawEvent->value;
4962 newSlot = true;
4963 }
4964
4965 if (mAccumulator.currentSlot < 0 || size_t(mAccumulator.currentSlot) >= mSlotCount) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07004966#if DEBUG_POINTERS
Jeff Brown441a9c22011-06-02 18:22:25 -07004967 if (newSlot) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07004968 LOGW("MultiTouch device %s emitted invalid slot index %d but it "
4969 "should be between 0 and %d; ignoring this slot.",
4970 getDeviceName().string(), mAccumulator.currentSlot, mSlotCount);
Jeff Brown80fd47c2011-05-24 01:07:44 -07004971 }
Jeff Brown441a9c22011-06-02 18:22:25 -07004972#endif
Jeff Brown80fd47c2011-05-24 01:07:44 -07004973 break;
4974 }
4975
4976 Accumulator::Slot* slot = &mAccumulator.slots[mAccumulator.currentSlot];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004977
4978 switch (rawEvent->scanCode) {
4979 case ABS_MT_POSITION_X:
Jeff Brown80fd47c2011-05-24 01:07:44 -07004980 slot->fields |= Accumulator::FIELD_ABS_MT_POSITION_X;
4981 slot->absMTPositionX = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004982 break;
4983 case ABS_MT_POSITION_Y:
Jeff Brown80fd47c2011-05-24 01:07:44 -07004984 slot->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y;
4985 slot->absMTPositionY = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004986 break;
4987 case ABS_MT_TOUCH_MAJOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07004988 slot->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR;
4989 slot->absMTTouchMajor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004990 break;
4991 case ABS_MT_TOUCH_MINOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07004992 slot->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR;
4993 slot->absMTTouchMinor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004994 break;
4995 case ABS_MT_WIDTH_MAJOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07004996 slot->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR;
4997 slot->absMTWidthMajor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004998 break;
4999 case ABS_MT_WIDTH_MINOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005000 slot->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR;
5001 slot->absMTWidthMinor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005002 break;
5003 case ABS_MT_ORIENTATION:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005004 slot->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION;
5005 slot->absMTOrientation = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005006 break;
5007 case ABS_MT_TRACKING_ID:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005008 if (mUsingSlotsProtocol && rawEvent->value < 0) {
5009 slot->clear();
5010 } else {
5011 slot->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID;
5012 slot->absMTTrackingId = rawEvent->value;
5013 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005014 break;
Jeff Brown8d608662010-08-30 03:02:23 -07005015 case ABS_MT_PRESSURE:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005016 slot->fields |= Accumulator::FIELD_ABS_MT_PRESSURE;
5017 slot->absMTPressure = rawEvent->value;
5018 break;
5019 case ABS_MT_TOOL_TYPE:
5020 slot->fields |= Accumulator::FIELD_ABS_MT_TOOL_TYPE;
5021 slot->absMTToolType = rawEvent->value;
Jeff Brown8d608662010-08-30 03:02:23 -07005022 break;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005023 }
5024 break;
5025 }
5026
5027 case EV_SYN:
5028 switch (rawEvent->scanCode) {
5029 case SYN_MT_REPORT: {
5030 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
Jeff Brown80fd47c2011-05-24 01:07:44 -07005031 mAccumulator.currentSlot += 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005032 break;
5033 }
5034
5035 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005036 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005037 break;
5038 }
5039 break;
5040 }
5041}
5042
5043void MultiTouchInputMapper::sync(nsecs_t when) {
5044 static const uint32_t REQUIRED_FIELDS =
Jeff Brown8d608662010-08-30 03:02:23 -07005045 Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005046
Jeff Brown80fd47c2011-05-24 01:07:44 -07005047 size_t inCount = mSlotCount;
5048 size_t outCount = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005049 bool havePointerIds = true;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005050
Jeff Brown6d0fec22010-07-23 21:28:06 -07005051 mCurrentTouch.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005052
Jeff Brown80fd47c2011-05-24 01:07:44 -07005053 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
5054 const Accumulator::Slot& inSlot = mAccumulator.slots[inIndex];
5055 uint32_t fields = inSlot.fields;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005056
Jeff Brown6d0fec22010-07-23 21:28:06 -07005057 if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005058 // Some drivers send empty MT sync packets without X / Y to indicate a pointer up.
Jeff Brown80fd47c2011-05-24 01:07:44 -07005059 // This may also indicate an unused slot.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005060 // Drop this finger.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005061 continue;
5062 }
5063
Jeff Brown80fd47c2011-05-24 01:07:44 -07005064 if (outCount >= MAX_POINTERS) {
5065#if DEBUG_POINTERS
5066 LOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
5067 "ignoring the rest.",
5068 getDeviceName().string(), MAX_POINTERS);
5069#endif
5070 break; // too many fingers!
5071 }
5072
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005073 PointerData& outPointer = mCurrentTouch.pointers[outCount];
Jeff Brown80fd47c2011-05-24 01:07:44 -07005074 outPointer.x = inSlot.absMTPositionX;
5075 outPointer.y = inSlot.absMTPositionY;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005076
Jeff Brown8d608662010-08-30 03:02:23 -07005077 if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005078 outPointer.pressure = inSlot.absMTPressure;
Jeff Brown8d608662010-08-30 03:02:23 -07005079 } else {
5080 // Default pressure to 0 if absent.
5081 outPointer.pressure = 0;
5082 }
5083
5084 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005085 if (inSlot.absMTTouchMajor <= 0) {
Jeff Brown8d608662010-08-30 03:02:23 -07005086 // Some devices send sync packets with X / Y but with a 0 touch major to indicate
5087 // a pointer going up. Drop this finger.
5088 continue;
5089 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07005090 outPointer.touchMajor = inSlot.absMTTouchMajor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005091 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005092 // Default touch area to 0 if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005093 outPointer.touchMajor = 0;
5094 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005095
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005096 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005097 outPointer.touchMinor = inSlot.absMTTouchMinor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005098 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005099 // Assume touch area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005100 outPointer.touchMinor = outPointer.touchMajor;
5101 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005102
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005103 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005104 outPointer.toolMajor = inSlot.absMTWidthMajor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005105 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005106 // Default tool area to 0 if absent.
5107 outPointer.toolMajor = 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005108 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005109
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005110 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005111 outPointer.toolMinor = inSlot.absMTWidthMinor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005112 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005113 // Assume tool area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005114 outPointer.toolMinor = outPointer.toolMajor;
5115 }
5116
5117 if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005118 outPointer.orientation = inSlot.absMTOrientation;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005119 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005120 // Default orientation to vertical if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005121 outPointer.orientation = 0;
5122 }
5123
Jeff Brown80fd47c2011-05-24 01:07:44 -07005124 if (fields & Accumulator::FIELD_ABS_MT_DISTANCE) {
5125 outPointer.distance = inSlot.absMTDistance;
5126 } else {
5127 // Default distance is 0 (direct contact).
5128 outPointer.distance = 0;
5129 }
5130
5131 if (fields & Accumulator::FIELD_ABS_MT_TOOL_TYPE) {
5132 outPointer.isStylus = (inSlot.absMTToolType == MT_TOOL_PEN);
5133 } else {
5134 // Assume this is not a stylus.
5135 outPointer.isStylus = false;
5136 }
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005137
Jeff Brown8d608662010-08-30 03:02:23 -07005138 // Assign pointer id using tracking id if available.
Jeff Brown6d0fec22010-07-23 21:28:06 -07005139 if (havePointerIds) {
Jeff Brown6894a292011-07-01 17:59:27 -07005140 int32_t id = -1;
5141 if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) {
5142 int32_t trackingId = inSlot.absMTTrackingId;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005143
Jeff Brown6894a292011-07-01 17:59:27 -07005144 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
5145 uint32_t n = idBits.firstMarkedBit();
5146 idBits.clearBit(n);
5147
5148 if (mPointerTrackingIdMap[n] == trackingId) {
5149 id = n;
5150 }
5151 }
5152
5153 if (id < 0 && !mPointerIdBits.isFull()) {
5154 id = mPointerIdBits.firstUnmarkedBit();
5155 mPointerIdBits.markBit(id);
5156 mPointerTrackingIdMap[id] = trackingId;
5157 }
5158 }
5159 if (id < 0) {
5160 havePointerIds = false;
5161 mCurrentTouch.idBits.clear();
5162 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005163 outPointer.id = id;
5164 mCurrentTouch.idToIndex[id] = outCount;
5165 mCurrentTouch.idBits.markBit(id);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005166 }
5167 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005168
Jeff Brown6d0fec22010-07-23 21:28:06 -07005169 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005170 }
5171
Jeff Brown6d0fec22010-07-23 21:28:06 -07005172 mCurrentTouch.pointerCount = outCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005173
Jeff Brownace13b12011-03-09 17:39:48 -08005174 mButtonState = (mButtonState | mAccumulator.buttonDown) & ~mAccumulator.buttonUp;
5175 mCurrentTouch.buttonState = mButtonState;
5176
Jeff Brown6894a292011-07-01 17:59:27 -07005177 mPointerIdBits = mCurrentTouch.idBits;
5178
Jeff Brown6d0fec22010-07-23 21:28:06 -07005179 syncTouch(when, havePointerIds);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005180
Jeff Brown441a9c22011-06-02 18:22:25 -07005181 if (!mUsingSlotsProtocol) {
5182 mAccumulator.clearSlots(mSlotCount);
5183 }
5184 mAccumulator.clearButtons();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005185}
5186
Jeff Brown8d608662010-08-30 03:02:23 -07005187void MultiTouchInputMapper::configureRawAxes() {
5188 TouchInputMapper::configureRawAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005189
Jeff Brown80fd47c2011-05-24 01:07:44 -07005190 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, &mRawAxes.x);
5191 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, &mRawAxes.y);
5192 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, &mRawAxes.touchMajor);
5193 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, &mRawAxes.touchMinor);
5194 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, &mRawAxes.toolMajor);
5195 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, &mRawAxes.toolMinor);
5196 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, &mRawAxes.orientation);
5197 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, &mRawAxes.pressure);
5198 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_DISTANCE, &mRawAxes.distance);
5199 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TRACKING_ID, &mRawAxes.trackingId);
5200 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_SLOT, &mRawAxes.slot);
5201
5202 if (mRawAxes.trackingId.valid
5203 && mRawAxes.slot.valid && mRawAxes.slot.minValue == 0 && mRawAxes.slot.maxValue > 0) {
5204 mSlotCount = mRawAxes.slot.maxValue + 1;
5205 if (mSlotCount > MAX_SLOTS) {
5206 LOGW("MultiTouch Device %s reported %d slots but the framework "
5207 "only supports a maximum of %d slots at this time.",
5208 getDeviceName().string(), mSlotCount, MAX_SLOTS);
5209 mSlotCount = MAX_SLOTS;
5210 }
5211 mUsingSlotsProtocol = true;
5212 } else {
5213 mSlotCount = MAX_POINTERS;
5214 mUsingSlotsProtocol = false;
5215 }
5216
5217 mAccumulator.allocateSlots(mSlotCount);
Jeff Brown2717eff2011-06-30 23:53:07 -07005218
5219 clearState();
Jeff Brown9c3cda02010-06-15 01:31:58 -07005220}
5221
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005222
Jeff Browncb1404e2011-01-15 18:14:15 -08005223// --- JoystickInputMapper ---
5224
5225JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5226 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005227}
5228
5229JoystickInputMapper::~JoystickInputMapper() {
5230}
5231
5232uint32_t JoystickInputMapper::getSources() {
5233 return AINPUT_SOURCE_JOYSTICK;
5234}
5235
5236void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5237 InputMapper::populateDeviceInfo(info);
5238
Jeff Brown6f2fba42011-02-19 01:08:02 -08005239 for (size_t i = 0; i < mAxes.size(); i++) {
5240 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005241 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5242 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005243 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005244 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5245 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005246 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005247 }
5248}
5249
5250void JoystickInputMapper::dump(String8& dump) {
5251 dump.append(INDENT2 "Joystick Input Mapper:\n");
5252
Jeff Brown6f2fba42011-02-19 01:08:02 -08005253 dump.append(INDENT3 "Axes:\n");
5254 size_t numAxes = mAxes.size();
5255 for (size_t i = 0; i < numAxes; i++) {
5256 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005257 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005258 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005259 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005260 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005261 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005262 }
Jeff Brown85297452011-03-04 13:07:49 -08005263 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5264 label = getAxisLabel(axis.axisInfo.highAxis);
5265 if (label) {
5266 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5267 } else {
5268 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5269 axis.axisInfo.splitValue);
5270 }
5271 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5272 dump.append(" (invert)");
5273 }
5274
5275 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5276 axis.min, axis.max, axis.flat, axis.fuzz);
5277 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5278 "highScale=%0.5f, highOffset=%0.5f\n",
5279 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005280 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5281 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005282 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07005283 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08005284 }
5285}
5286
Jeff Brown474dcb52011-06-14 20:22:50 -07005287void JoystickInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
5288 InputMapper::configure(config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08005289
Jeff Brown474dcb52011-06-14 20:22:50 -07005290 if (!changes) { // first time only
5291 // Collect all axes.
5292 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
5293 RawAbsoluteAxisInfo rawAxisInfo;
5294 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), abs, &rawAxisInfo);
5295 if (rawAxisInfo.valid) {
5296 // Map axis.
5297 AxisInfo axisInfo;
5298 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
5299 if (!explicitlyMapped) {
5300 // Axis is not explicitly mapped, will choose a generic axis later.
5301 axisInfo.mode = AxisInfo::MODE_NORMAL;
5302 axisInfo.axis = -1;
5303 }
5304
5305 // Apply flat override.
5306 int32_t rawFlat = axisInfo.flatOverride < 0
5307 ? rawAxisInfo.flat : axisInfo.flatOverride;
5308
5309 // Calculate scaling factors and limits.
5310 Axis axis;
5311 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5312 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5313 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5314 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5315 scale, 0.0f, highScale, 0.0f,
5316 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5317 } else if (isCenteredAxis(axisInfo.axis)) {
5318 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5319 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
5320 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5321 scale, offset, scale, offset,
5322 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5323 } else {
5324 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5325 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5326 scale, 0.0f, scale, 0.0f,
5327 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5328 }
5329
5330 // To eliminate noise while the joystick is at rest, filter out small variations
5331 // in axis values up front.
5332 axis.filter = axis.flat * 0.25f;
5333
5334 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005335 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005336 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005337
Jeff Brown474dcb52011-06-14 20:22:50 -07005338 // If there are too many axes, start dropping them.
5339 // Prefer to keep explicitly mapped axes.
5340 if (mAxes.size() > PointerCoords::MAX_AXES) {
5341 LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
5342 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5343 pruneAxes(true);
5344 pruneAxes(false);
5345 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005346
Jeff Brown474dcb52011-06-14 20:22:50 -07005347 // Assign generic axis ids to remaining axes.
5348 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5349 size_t numAxes = mAxes.size();
5350 for (size_t i = 0; i < numAxes; i++) {
5351 Axis& axis = mAxes.editValueAt(i);
5352 if (axis.axisInfo.axis < 0) {
5353 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5354 && haveAxis(nextGenericAxisId)) {
5355 nextGenericAxisId += 1;
5356 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005357
Jeff Brown474dcb52011-06-14 20:22:50 -07005358 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
5359 axis.axisInfo.axis = nextGenericAxisId;
5360 nextGenericAxisId += 1;
5361 } else {
5362 LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
5363 "have already been assigned to other axes.",
5364 getDeviceName().string(), mAxes.keyAt(i));
5365 mAxes.removeItemsAt(i--);
5366 numAxes -= 1;
5367 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005368 }
5369 }
5370 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005371}
5372
Jeff Brown85297452011-03-04 13:07:49 -08005373bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005374 size_t numAxes = mAxes.size();
5375 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005376 const Axis& axis = mAxes.valueAt(i);
5377 if (axis.axisInfo.axis == axisId
5378 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5379 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005380 return true;
5381 }
5382 }
5383 return false;
5384}
Jeff Browncb1404e2011-01-15 18:14:15 -08005385
Jeff Brown6f2fba42011-02-19 01:08:02 -08005386void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5387 size_t i = mAxes.size();
5388 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5389 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5390 continue;
5391 }
5392 LOGI("Discarding joystick '%s' axis %d because there are too many axes.",
5393 getDeviceName().string(), mAxes.keyAt(i));
5394 mAxes.removeItemsAt(i);
5395 }
5396}
5397
5398bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5399 switch (axis) {
5400 case AMOTION_EVENT_AXIS_X:
5401 case AMOTION_EVENT_AXIS_Y:
5402 case AMOTION_EVENT_AXIS_Z:
5403 case AMOTION_EVENT_AXIS_RX:
5404 case AMOTION_EVENT_AXIS_RY:
5405 case AMOTION_EVENT_AXIS_RZ:
5406 case AMOTION_EVENT_AXIS_HAT_X:
5407 case AMOTION_EVENT_AXIS_HAT_Y:
5408 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005409 case AMOTION_EVENT_AXIS_RUDDER:
5410 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005411 return true;
5412 default:
5413 return false;
5414 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005415}
5416
5417void JoystickInputMapper::reset() {
5418 // Recenter all axes.
5419 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Browncb1404e2011-01-15 18:14:15 -08005420
Jeff Brown6f2fba42011-02-19 01:08:02 -08005421 size_t numAxes = mAxes.size();
5422 for (size_t i = 0; i < numAxes; i++) {
5423 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005424 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005425 }
5426
5427 sync(when, true /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005428
5429 InputMapper::reset();
5430}
5431
5432void JoystickInputMapper::process(const RawEvent* rawEvent) {
5433 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005434 case EV_ABS: {
5435 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
5436 if (index >= 0) {
5437 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005438 float newValue, highNewValue;
5439 switch (axis.axisInfo.mode) {
5440 case AxisInfo::MODE_INVERT:
5441 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5442 * axis.scale + axis.offset;
5443 highNewValue = 0.0f;
5444 break;
5445 case AxisInfo::MODE_SPLIT:
5446 if (rawEvent->value < axis.axisInfo.splitValue) {
5447 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5448 * axis.scale + axis.offset;
5449 highNewValue = 0.0f;
5450 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5451 newValue = 0.0f;
5452 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5453 * axis.highScale + axis.highOffset;
5454 } else {
5455 newValue = 0.0f;
5456 highNewValue = 0.0f;
5457 }
5458 break;
5459 default:
5460 newValue = rawEvent->value * axis.scale + axis.offset;
5461 highNewValue = 0.0f;
5462 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005463 }
Jeff Brown85297452011-03-04 13:07:49 -08005464 axis.newValue = newValue;
5465 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005466 }
5467 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005468 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005469
5470 case EV_SYN:
5471 switch (rawEvent->scanCode) {
5472 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005473 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005474 break;
5475 }
5476 break;
5477 }
5478}
5479
Jeff Brown6f2fba42011-02-19 01:08:02 -08005480void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005481 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005482 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005483 }
5484
5485 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005486 int32_t buttonState = 0;
5487
5488 PointerProperties pointerProperties;
5489 pointerProperties.clear();
5490 pointerProperties.id = 0;
5491 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08005492
Jeff Brown6f2fba42011-02-19 01:08:02 -08005493 PointerCoords pointerCoords;
5494 pointerCoords.clear();
5495
5496 size_t numAxes = mAxes.size();
5497 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005498 const Axis& axis = mAxes.valueAt(i);
5499 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
5500 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5501 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
5502 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005503 }
5504
Jeff Brown56194eb2011-03-02 19:23:13 -08005505 // Moving a joystick axis should not wake the devide because joysticks can
5506 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
5507 // button will likely wake the device.
5508 // TODO: Use the input device configuration to control this behavior more finely.
5509 uint32_t policyFlags = 0;
5510
Jeff Brown56194eb2011-03-02 19:23:13 -08005511 getDispatcher()->notifyMotion(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005512 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
5513 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Browncb1404e2011-01-15 18:14:15 -08005514}
5515
Jeff Brown85297452011-03-04 13:07:49 -08005516bool JoystickInputMapper::filterAxes(bool force) {
5517 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005518 size_t numAxes = mAxes.size();
5519 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005520 Axis& axis = mAxes.editValueAt(i);
5521 if (force || hasValueChangedSignificantly(axis.filter,
5522 axis.newValue, axis.currentValue, axis.min, axis.max)) {
5523 axis.currentValue = axis.newValue;
5524 atLeastOneSignificantChange = true;
5525 }
5526 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5527 if (force || hasValueChangedSignificantly(axis.filter,
5528 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
5529 axis.highCurrentValue = axis.highNewValue;
5530 atLeastOneSignificantChange = true;
5531 }
5532 }
5533 }
5534 return atLeastOneSignificantChange;
5535}
5536
5537bool JoystickInputMapper::hasValueChangedSignificantly(
5538 float filter, float newValue, float currentValue, float min, float max) {
5539 if (newValue != currentValue) {
5540 // Filter out small changes in value unless the value is converging on the axis
5541 // bounds or center point. This is intended to reduce the amount of information
5542 // sent to applications by particularly noisy joysticks (such as PS3).
5543 if (fabs(newValue - currentValue) > filter
5544 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
5545 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
5546 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
5547 return true;
5548 }
5549 }
5550 return false;
5551}
5552
5553bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
5554 float filter, float newValue, float currentValue, float thresholdValue) {
5555 float newDistance = fabs(newValue - thresholdValue);
5556 if (newDistance < filter) {
5557 float oldDistance = fabs(currentValue - thresholdValue);
5558 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005559 return true;
5560 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005561 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005562 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005563}
5564
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005565} // namespace android