blob: dacc73f41d25abf772601f12bb264c791529c17d [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070017#define LOG_TAG "InputReader"
18
19//#define LOG_NDEBUG 0
20
21// Log debug messages for each raw event received from the EventHub.
22#define DEBUG_RAW_EVENTS 0
23
24// Log debug messages about touch screen filtering hacks.
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_HACKS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070026
27// Log debug messages about virtual key processing.
Jeff Brown349703e2010-06-22 01:27:15 -070028#define DEBUG_VIRTUAL_KEYS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070029
30// Log debug messages about pointers.
Jeff Brown9f2106f2011-05-24 14:40:35 -070031#define DEBUG_POINTERS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070032
Jeff Brown5c225b12010-06-16 01:53:36 -070033// Log debug messages about pointer assignment calculations.
34#define DEBUG_POINTER_ASSIGNMENT 0
35
Jeff Brownace13b12011-03-09 17:39:48 -080036// Log debug messages about gesture detection.
37#define DEBUG_GESTURES 0
38
Jeff Brownb4ff35d2011-01-02 16:37:43 -080039#include "InputReader.h"
40
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070041#include <cutils/log.h>
Jeff Brown6b53e8d2010-11-10 16:03:06 -080042#include <ui/Keyboard.h>
Jeff Brown90655042010-12-02 13:50:46 -080043#include <ui/VirtualKeyMap.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070044
45#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070046#include <stdlib.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070047#include <unistd.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070048#include <errno.h>
49#include <limits.h>
Jeff Brownc5ed5912010-07-14 18:48:53 -070050#include <math.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070051
Jeff Brown8d608662010-08-30 03:02:23 -070052#define INDENT " "
Jeff Brownef3d7e82010-09-30 14:33:04 -070053#define INDENT2 " "
54#define INDENT3 " "
55#define INDENT4 " "
Jeff Brownaba321a2011-06-28 20:34:40 -070056#define INDENT5 " "
Jeff Brown8d608662010-08-30 03:02:23 -070057
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070058namespace android {
59
Jeff Brownace13b12011-03-09 17:39:48 -080060// --- Constants ---
61
Jeff Brown80fd47c2011-05-24 01:07:44 -070062// Maximum number of slots supported when using the slot-based Multitouch Protocol B.
63static const size_t MAX_SLOTS = 32;
64
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070065// --- Static Functions ---
66
67template<typename T>
68inline static T abs(const T& value) {
69 return value < 0 ? - value : value;
70}
71
72template<typename T>
73inline static T min(const T& a, const T& b) {
74 return a < b ? a : b;
75}
76
Jeff Brown5c225b12010-06-16 01:53:36 -070077template<typename T>
78inline static void swap(T& a, T& b) {
79 T temp = a;
80 a = b;
81 b = temp;
82}
83
Jeff Brown8d608662010-08-30 03:02:23 -070084inline static float avg(float x, float y) {
85 return (x + y) / 2;
86}
87
Jeff Brown2352b972011-04-12 22:39:53 -070088inline static float distance(float x1, float y1, float x2, float y2) {
89 return hypotf(x1 - x2, y1 - y2);
Jeff Brownace13b12011-03-09 17:39:48 -080090}
91
Jeff Brown517bb4c2011-01-14 19:09:23 -080092inline static int32_t signExtendNybble(int32_t value) {
93 return value >= 8 ? value - 16 : value;
94}
95
Jeff Brownef3d7e82010-09-30 14:33:04 -070096static inline const char* toString(bool value) {
97 return value ? "true" : "false";
98}
99
Jeff Brown9626b142011-03-03 02:09:54 -0800100static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
101 const int32_t map[][4], size_t mapSize) {
102 if (orientation != DISPLAY_ORIENTATION_0) {
103 for (size_t i = 0; i < mapSize; i++) {
104 if (value == map[i][0]) {
105 return map[i][orientation];
106 }
107 }
108 }
109 return value;
110}
111
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700112static const int32_t keyCodeRotationMap[][4] = {
113 // key codes enumerated counter-clockwise with the original (unrotated) key first
114 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd0358292010-06-30 16:10:35 -0700115 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
116 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
117 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
118 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700119};
Jeff Brown9626b142011-03-03 02:09:54 -0800120static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700121 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
122
Jeff Brown60691392011-07-15 19:08:26 -0700123static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brown9626b142011-03-03 02:09:54 -0800124 return rotateValueUsingRotationMap(keyCode, orientation,
125 keyCodeRotationMap, keyCodeRotationMapSize);
126}
127
Jeff Brown612891e2011-07-15 20:44:17 -0700128static void rotateDelta(int32_t orientation, float* deltaX, float* deltaY) {
129 float temp;
130 switch (orientation) {
131 case DISPLAY_ORIENTATION_90:
132 temp = *deltaX;
133 *deltaX = *deltaY;
134 *deltaY = -temp;
135 break;
136
137 case DISPLAY_ORIENTATION_180:
138 *deltaX = -*deltaX;
139 *deltaY = -*deltaY;
140 break;
141
142 case DISPLAY_ORIENTATION_270:
143 temp = *deltaX;
144 *deltaX = -*deltaY;
145 *deltaY = temp;
146 break;
147 }
148}
149
Jeff Brown6d0fec22010-07-23 21:28:06 -0700150static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
151 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
152}
153
Jeff Brownefd32662011-03-08 15:13:06 -0800154// Returns true if the pointer should be reported as being down given the specified
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700155// button states. This determines whether the event is reported as a touch event.
156static bool isPointerDown(int32_t buttonState) {
157 return buttonState &
158 (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY
Jeff Brown53ca3f12011-06-27 18:36:00 -0700159 | AMOTION_EVENT_BUTTON_TERTIARY);
Jeff Brownefd32662011-03-08 15:13:06 -0800160}
161
Jeff Brown2352b972011-04-12 22:39:53 -0700162static float calculateCommonVector(float a, float b) {
163 if (a > 0 && b > 0) {
164 return a < b ? a : b;
165 } else if (a < 0 && b < 0) {
166 return a > b ? a : b;
167 } else {
168 return 0;
169 }
170}
171
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700172static void synthesizeButtonKey(InputReaderContext* context, int32_t action,
173 nsecs_t when, int32_t deviceId, uint32_t source,
174 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState,
175 int32_t buttonState, int32_t keyCode) {
176 if (
177 (action == AKEY_EVENT_ACTION_DOWN
178 && !(lastButtonState & buttonState)
179 && (currentButtonState & buttonState))
180 || (action == AKEY_EVENT_ACTION_UP
181 && (lastButtonState & buttonState)
182 && !(currentButtonState & buttonState))) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700183 NotifyKeyArgs args(when, deviceId, source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700184 action, 0, keyCode, 0, context->getGlobalMetaState(), when);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700185 context->getListener()->notifyKey(&args);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700186 }
187}
188
189static void synthesizeButtonKeys(InputReaderContext* context, int32_t action,
190 nsecs_t when, int32_t deviceId, uint32_t source,
191 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState) {
192 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
193 lastButtonState, currentButtonState,
194 AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK);
195 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
196 lastButtonState, currentButtonState,
197 AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD);
198}
199
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700200
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700201// --- InputReader ---
202
203InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700204 const sp<InputReaderPolicyInterface>& policy,
Jeff Brownbe1aa822011-07-27 16:04:54 -0700205 const sp<InputListenerInterface>& listener) :
206 mContext(this), mEventHub(eventHub), mPolicy(policy),
Jeff Brown1a84fd12011-06-02 01:26:32 -0700207 mGlobalMetaState(0), mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX),
Jeff Brown474dcb52011-06-14 20:22:50 -0700208 mConfigurationChangesToRefresh(0) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700209 mQueuedListener = new QueuedInputListener(listener);
210
211 { // acquire lock
212 AutoMutex _l(mLock);
213
214 refreshConfigurationLocked(0);
215 updateGlobalMetaStateLocked();
216 updateInputConfigurationLocked();
217 } // release lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700218}
219
220InputReader::~InputReader() {
221 for (size_t i = 0; i < mDevices.size(); i++) {
222 delete mDevices.valueAt(i);
223 }
224}
225
226void InputReader::loopOnce() {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700227 int32_t timeoutMillis;
Jeff Brown474dcb52011-06-14 20:22:50 -0700228 { // acquire lock
Jeff Brownbe1aa822011-07-27 16:04:54 -0700229 AutoMutex _l(mLock);
Jeff Brown474dcb52011-06-14 20:22:50 -0700230
Jeff Brownbe1aa822011-07-27 16:04:54 -0700231 uint32_t changes = mConfigurationChangesToRefresh;
232 if (changes) {
233 mConfigurationChangesToRefresh = 0;
234 refreshConfigurationLocked(changes);
235 }
236
237 timeoutMillis = -1;
238 if (mNextTimeout != LLONG_MAX) {
239 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
240 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
241 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700242 } // release lock
243
Jeff Brownb7198742011-03-18 18:14:26 -0700244 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700245
246 { // acquire lock
247 AutoMutex _l(mLock);
248
249 if (count) {
250 processEventsLocked(mEventBuffer, count);
251 }
252 if (!count || timeoutMillis == 0) {
253 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700254#if DEBUG_RAW_EVENTS
Jeff Brownbe1aa822011-07-27 16:04:54 -0700255 LOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700256#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -0700257 mNextTimeout = LLONG_MAX;
258 timeoutExpiredLocked(now);
259 }
260 } // release lock
261
262 // Flush queued events out to the listener.
263 // This must happen outside of the lock because the listener could potentially call
264 // back into the InputReader's methods, such as getScanCodeState, or become blocked
265 // on another thread similarly waiting to acquire the InputReader lock thereby
266 // resulting in a deadlock. This situation is actually quite plausible because the
267 // listener is actually the input dispatcher, which calls into the window manager,
268 // which occasionally calls into the input reader.
269 mQueuedListener->flush();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700270}
271
Jeff Brownbe1aa822011-07-27 16:04:54 -0700272void InputReader::processEventsLocked(const RawEvent* rawEvents, size_t count) {
Jeff Brownb7198742011-03-18 18:14:26 -0700273 for (const RawEvent* rawEvent = rawEvents; count;) {
274 int32_t type = rawEvent->type;
275 size_t batchSize = 1;
276 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
277 int32_t deviceId = rawEvent->deviceId;
278 while (batchSize < count) {
279 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
280 || rawEvent[batchSize].deviceId != deviceId) {
281 break;
282 }
283 batchSize += 1;
284 }
285#if DEBUG_RAW_EVENTS
286 LOGD("BatchSize: %d Count: %d", batchSize, count);
287#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -0700288 processEventsForDeviceLocked(deviceId, rawEvent, batchSize);
Jeff Brownb7198742011-03-18 18:14:26 -0700289 } else {
290 switch (rawEvent->type) {
291 case EventHubInterface::DEVICE_ADDED:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700292 addDeviceLocked(rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700293 break;
294 case EventHubInterface::DEVICE_REMOVED:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700295 removeDeviceLocked(rawEvent->deviceId);
Jeff Brownb7198742011-03-18 18:14:26 -0700296 break;
297 case EventHubInterface::FINISHED_DEVICE_SCAN:
Jeff Brownbe1aa822011-07-27 16:04:54 -0700298 handleConfigurationChangedLocked(rawEvent->when);
Jeff Brownb7198742011-03-18 18:14:26 -0700299 break;
300 default:
Jeff Brownb6110c22011-04-01 16:15:13 -0700301 LOG_ASSERT(false); // can't happen
Jeff Brownb7198742011-03-18 18:14:26 -0700302 break;
303 }
304 }
305 count -= batchSize;
306 rawEvent += batchSize;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700307 }
308}
309
Jeff Brownbe1aa822011-07-27 16:04:54 -0700310void InputReader::addDeviceLocked(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700311 String8 name = mEventHub->getDeviceName(deviceId);
312 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
313
Jeff Brownbe1aa822011-07-27 16:04:54 -0700314 InputDevice* device = createDeviceLocked(deviceId, name, classes);
Jeff Brown474dcb52011-06-14 20:22:50 -0700315 device->configure(&mConfig, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700316
Jeff Brown8d608662010-08-30 03:02:23 -0700317 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800318 LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700319 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800320 LOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700321 device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700322 }
323
Jeff Brownbe1aa822011-07-27 16:04:54 -0700324 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
325 if (deviceIndex < 0) {
326 mDevices.add(deviceId, device);
327 } else {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700328 LOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
329 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700330 return;
331 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700332}
333
Jeff Brownbe1aa822011-07-27 16:04:54 -0700334void InputReader::removeDeviceLocked(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700335 InputDevice* device = NULL;
Jeff Brownbe1aa822011-07-27 16:04:54 -0700336 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
337 if (deviceIndex >= 0) {
338 device = mDevices.valueAt(deviceIndex);
339 mDevices.removeItemsAt(deviceIndex, 1);
340 } else {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700341 LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700342 return;
343 }
344
Jeff Brown6d0fec22010-07-23 21:28:06 -0700345 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800346 LOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700347 device->getId(), device->getName().string());
348 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800349 LOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700350 device->getId(), device->getName().string(), device->getSources());
351 }
352
Jeff Brown8d608662010-08-30 03:02:23 -0700353 device->reset();
354
Jeff Brown6d0fec22010-07-23 21:28:06 -0700355 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700356}
357
Jeff Brownbe1aa822011-07-27 16:04:54 -0700358InputDevice* InputReader::createDeviceLocked(int32_t deviceId,
359 const String8& name, uint32_t classes) {
360 InputDevice* device = new InputDevice(&mContext, deviceId, name);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700361
Jeff Brown56194eb2011-03-02 19:23:13 -0800362 // External devices.
363 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
364 device->setExternal(true);
365 }
366
Jeff Brown6d0fec22010-07-23 21:28:06 -0700367 // Switch-like devices.
368 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
369 device->addMapper(new SwitchInputMapper(device));
370 }
371
372 // Keyboard-like devices.
Jeff Brownefd32662011-03-08 15:13:06 -0800373 uint32_t keyboardSource = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700374 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
375 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800376 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700377 }
378 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
379 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
380 }
381 if (classes & INPUT_DEVICE_CLASS_DPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800382 keyboardSource |= AINPUT_SOURCE_DPAD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700383 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800384 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800385 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
Jeff Browncb1404e2011-01-15 18:14:15 -0800386 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700387
Jeff Brownefd32662011-03-08 15:13:06 -0800388 if (keyboardSource != 0) {
389 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700390 }
391
Jeff Brown83c09682010-12-23 17:50:18 -0800392 // Cursor-like devices.
393 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
394 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700395 }
396
Jeff Brown58a2da82011-01-25 16:02:22 -0800397 // Touchscreens and touchpad devices.
398 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800399 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800400 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800401 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700402 }
403
Jeff Browncb1404e2011-01-15 18:14:15 -0800404 // Joystick-like devices.
405 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
406 device->addMapper(new JoystickInputMapper(device));
407 }
408
Jeff Brown6d0fec22010-07-23 21:28:06 -0700409 return device;
410}
411
Jeff Brownbe1aa822011-07-27 16:04:54 -0700412void InputReader::processEventsForDeviceLocked(int32_t deviceId,
Jeff Brownb7198742011-03-18 18:14:26 -0700413 const RawEvent* rawEvents, size_t count) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700414 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
415 if (deviceIndex < 0) {
416 LOGW("Discarding event for unknown deviceId %d.", deviceId);
417 return;
418 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700419
Jeff Brownbe1aa822011-07-27 16:04:54 -0700420 InputDevice* device = mDevices.valueAt(deviceIndex);
421 if (device->isIgnored()) {
422 //LOGD("Discarding event for ignored deviceId %d.", deviceId);
423 return;
424 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700425
Jeff Brownbe1aa822011-07-27 16:04:54 -0700426 device->process(rawEvents, count);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700427}
428
Jeff Brownbe1aa822011-07-27 16:04:54 -0700429void InputReader::timeoutExpiredLocked(nsecs_t when) {
430 for (size_t i = 0; i < mDevices.size(); i++) {
431 InputDevice* device = mDevices.valueAt(i);
432 if (!device->isIgnored()) {
433 device->timeoutExpired(when);
Jeff Brownaa3855d2011-03-17 01:34:19 -0700434 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700435 }
Jeff Brownaa3855d2011-03-17 01:34:19 -0700436}
437
Jeff Brownbe1aa822011-07-27 16:04:54 -0700438void InputReader::handleConfigurationChangedLocked(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700439 // Reset global meta state because it depends on the list of all configured devices.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700440 updateGlobalMetaStateLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700441
442 // Update input configuration.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700443 updateInputConfigurationLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700444
445 // Enqueue configuration changed.
Jeff Brownbe1aa822011-07-27 16:04:54 -0700446 NotifyConfigurationChangedArgs args(when);
447 mQueuedListener->notifyConfigurationChanged(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700448}
449
Jeff Brownbe1aa822011-07-27 16:04:54 -0700450void InputReader::refreshConfigurationLocked(uint32_t changes) {
Jeff Brown1a84fd12011-06-02 01:26:32 -0700451 mPolicy->getReaderConfiguration(&mConfig);
452 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
453
Jeff Brown474dcb52011-06-14 20:22:50 -0700454 if (changes) {
455 LOGI("Reconfiguring input devices. changes=0x%08x", changes);
456
457 if (changes & InputReaderConfiguration::CHANGE_MUST_REOPEN) {
458 mEventHub->requestReopenDevices();
459 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700460 for (size_t i = 0; i < mDevices.size(); i++) {
461 InputDevice* device = mDevices.valueAt(i);
462 device->configure(&mConfig, changes);
463 }
Jeff Brown474dcb52011-06-14 20:22:50 -0700464 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700465 }
466}
467
Jeff Brownbe1aa822011-07-27 16:04:54 -0700468void InputReader::updateGlobalMetaStateLocked() {
469 mGlobalMetaState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700470
Jeff Brownbe1aa822011-07-27 16:04:54 -0700471 for (size_t i = 0; i < mDevices.size(); i++) {
472 InputDevice* device = mDevices.valueAt(i);
473 mGlobalMetaState |= device->getMetaState();
474 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700475}
476
Jeff Brownbe1aa822011-07-27 16:04:54 -0700477int32_t InputReader::getGlobalMetaStateLocked() {
478 return mGlobalMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700479}
480
Jeff Brownbe1aa822011-07-27 16:04:54 -0700481void InputReader::updateInputConfigurationLocked() {
482 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
483 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
484 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
485 InputDeviceInfo deviceInfo;
486 for (size_t i = 0; i < mDevices.size(); i++) {
487 InputDevice* device = mDevices.valueAt(i);
488 device->getDeviceInfo(& deviceInfo);
489 uint32_t sources = deviceInfo.getSources();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700490
Jeff Brownbe1aa822011-07-27 16:04:54 -0700491 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
492 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
493 }
494 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
495 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
496 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
497 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
498 }
499 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
500 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
501 }
502 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700503
Jeff Brownbe1aa822011-07-27 16:04:54 -0700504 mInputConfiguration.touchScreen = touchScreenConfig;
505 mInputConfiguration.keyboard = keyboardConfig;
506 mInputConfiguration.navigation = navigationConfig;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700507}
508
Jeff Brownbe1aa822011-07-27 16:04:54 -0700509void InputReader::disableVirtualKeysUntilLocked(nsecs_t time) {
Jeff Brownfe508922011-01-18 15:10:10 -0800510 mDisableVirtualKeysTimeout = time;
511}
512
Jeff Brownbe1aa822011-07-27 16:04:54 -0700513bool InputReader::shouldDropVirtualKeyLocked(nsecs_t now,
Jeff Brownfe508922011-01-18 15:10:10 -0800514 InputDevice* device, int32_t keyCode, int32_t scanCode) {
515 if (now < mDisableVirtualKeysTimeout) {
516 LOGI("Dropping virtual key from device %s because virtual keys are "
517 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
518 device->getName().string(),
519 (mDisableVirtualKeysTimeout - now) * 0.000001,
520 keyCode, scanCode);
521 return true;
522 } else {
523 return false;
524 }
525}
526
Jeff Brownbe1aa822011-07-27 16:04:54 -0700527void InputReader::fadePointerLocked() {
528 for (size_t i = 0; i < mDevices.size(); i++) {
529 InputDevice* device = mDevices.valueAt(i);
530 device->fadePointer();
531 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800532}
533
Jeff Brownbe1aa822011-07-27 16:04:54 -0700534void InputReader::requestTimeoutAtTimeLocked(nsecs_t when) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700535 if (when < mNextTimeout) {
536 mNextTimeout = when;
537 }
538}
539
Jeff Brown6d0fec22010-07-23 21:28:06 -0700540void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700541 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700542
Jeff Brownbe1aa822011-07-27 16:04:54 -0700543 *outConfiguration = mInputConfiguration;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700544}
545
546status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700547 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700548
Jeff Brownbe1aa822011-07-27 16:04:54 -0700549 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
550 if (deviceIndex < 0) {
551 return NAME_NOT_FOUND;
552 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700553
Jeff Brownbe1aa822011-07-27 16:04:54 -0700554 InputDevice* device = mDevices.valueAt(deviceIndex);
555 if (device->isIgnored()) {
556 return NAME_NOT_FOUND;
557 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700558
Jeff Brownbe1aa822011-07-27 16:04:54 -0700559 device->getDeviceInfo(outDeviceInfo);
560 return OK;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700561}
562
563void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700564 AutoMutex _l(mLock);
565
Jeff Brown6d0fec22010-07-23 21:28:06 -0700566 outDeviceIds.clear();
567
Jeff Brownbe1aa822011-07-27 16:04:54 -0700568 size_t numDevices = mDevices.size();
569 for (size_t i = 0; i < numDevices; i++) {
570 InputDevice* device = mDevices.valueAt(i);
571 if (!device->isIgnored()) {
572 outDeviceIds.add(device->getId());
Jeff Brown6d0fec22010-07-23 21:28:06 -0700573 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700574 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700575}
576
577int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
578 int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700579 AutoMutex _l(mLock);
580
581 return getStateLocked(deviceId, sourceMask, keyCode, &InputDevice::getKeyCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700582}
583
584int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
585 int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700586 AutoMutex _l(mLock);
587
588 return getStateLocked(deviceId, sourceMask, scanCode, &InputDevice::getScanCodeState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700589}
590
591int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700592 AutoMutex _l(mLock);
593
594 return getStateLocked(deviceId, sourceMask, switchCode, &InputDevice::getSwitchState);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700595}
596
Jeff Brownbe1aa822011-07-27 16:04:54 -0700597int32_t InputReader::getStateLocked(int32_t deviceId, uint32_t sourceMask, int32_t code,
Jeff Brown6d0fec22010-07-23 21:28:06 -0700598 GetStateFunc getStateFunc) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700599 int32_t result = AKEY_STATE_UNKNOWN;
600 if (deviceId >= 0) {
601 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
602 if (deviceIndex >= 0) {
603 InputDevice* device = mDevices.valueAt(deviceIndex);
604 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
605 result = (device->*getStateFunc)(sourceMask, code);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700606 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700607 }
608 } else {
609 size_t numDevices = mDevices.size();
610 for (size_t i = 0; i < numDevices; i++) {
611 InputDevice* device = mDevices.valueAt(i);
612 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
613 result = (device->*getStateFunc)(sourceMask, code);
614 if (result >= AKEY_STATE_DOWN) {
615 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700616 }
617 }
618 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700619 }
620 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700621}
622
623bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
624 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700625 AutoMutex _l(mLock);
626
Jeff Brown6d0fec22010-07-23 21:28:06 -0700627 memset(outFlags, 0, numCodes);
Jeff Brownbe1aa822011-07-27 16:04:54 -0700628 return markSupportedKeyCodesLocked(deviceId, sourceMask, numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700629}
630
Jeff Brownbe1aa822011-07-27 16:04:54 -0700631bool InputReader::markSupportedKeyCodesLocked(int32_t deviceId, uint32_t sourceMask,
632 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
633 bool result = false;
634 if (deviceId >= 0) {
635 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
636 if (deviceIndex >= 0) {
637 InputDevice* device = mDevices.valueAt(deviceIndex);
638 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
639 result = device->markSupportedKeyCodes(sourceMask,
640 numCodes, keyCodes, outFlags);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700641 }
642 }
Jeff Brownbe1aa822011-07-27 16:04:54 -0700643 } else {
644 size_t numDevices = mDevices.size();
645 for (size_t i = 0; i < numDevices; i++) {
646 InputDevice* device = mDevices.valueAt(i);
647 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
648 result |= device->markSupportedKeyCodes(sourceMask,
649 numCodes, keyCodes, outFlags);
650 }
651 }
652 }
653 return result;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700654}
655
Jeff Brown474dcb52011-06-14 20:22:50 -0700656void InputReader::requestRefreshConfiguration(uint32_t changes) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700657 AutoMutex _l(mLock);
Jeff Brown93fa9b32011-06-14 17:09:25 -0700658
Jeff Brownbe1aa822011-07-27 16:04:54 -0700659 if (changes) {
660 bool needWake = !mConfigurationChangesToRefresh;
661 mConfigurationChangesToRefresh |= changes;
Jeff Brown474dcb52011-06-14 20:22:50 -0700662
663 if (needWake) {
664 mEventHub->wake();
665 }
666 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700667}
668
Jeff Brownb88102f2010-09-08 11:49:43 -0700669void InputReader::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -0700670 AutoMutex _l(mLock);
671
Jeff Brownf2f487182010-10-01 17:46:21 -0700672 mEventHub->dump(dump);
673 dump.append("\n");
674
675 dump.append("Input Reader State:\n");
676
Jeff Brownbe1aa822011-07-27 16:04:54 -0700677 for (size_t i = 0; i < mDevices.size(); i++) {
678 mDevices.valueAt(i)->dump(dump);
679 }
Jeff Brown214eaf42011-05-26 19:17:02 -0700680
681 dump.append(INDENT "Configuration:\n");
682 dump.append(INDENT2 "ExcludedDeviceNames: [");
683 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
684 if (i != 0) {
685 dump.append(", ");
686 }
687 dump.append(mConfig.excludedDeviceNames.itemAt(i).string());
688 }
689 dump.append("]\n");
Jeff Brown214eaf42011-05-26 19:17:02 -0700690 dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
691 mConfig.virtualKeyQuietTime * 0.000001f);
692
Jeff Brown19c97d462011-06-01 12:33:19 -0700693 dump.appendFormat(INDENT2 "PointerVelocityControlParameters: "
694 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
695 mConfig.pointerVelocityControlParameters.scale,
696 mConfig.pointerVelocityControlParameters.lowThreshold,
697 mConfig.pointerVelocityControlParameters.highThreshold,
698 mConfig.pointerVelocityControlParameters.acceleration);
699
700 dump.appendFormat(INDENT2 "WheelVelocityControlParameters: "
701 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
702 mConfig.wheelVelocityControlParameters.scale,
703 mConfig.wheelVelocityControlParameters.lowThreshold,
704 mConfig.wheelVelocityControlParameters.highThreshold,
705 mConfig.wheelVelocityControlParameters.acceleration);
706
Jeff Brown214eaf42011-05-26 19:17:02 -0700707 dump.appendFormat(INDENT2 "PointerGesture:\n");
Jeff Brown474dcb52011-06-14 20:22:50 -0700708 dump.appendFormat(INDENT3 "Enabled: %s\n",
709 toString(mConfig.pointerGesturesEnabled));
Jeff Brown214eaf42011-05-26 19:17:02 -0700710 dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n",
711 mConfig.pointerGestureQuietInterval * 0.000001f);
712 dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
713 mConfig.pointerGestureDragMinSwitchSpeed);
714 dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n",
715 mConfig.pointerGestureTapInterval * 0.000001f);
716 dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n",
717 mConfig.pointerGestureTapDragInterval * 0.000001f);
718 dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n",
719 mConfig.pointerGestureTapSlop);
720 dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
721 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700722 dump.appendFormat(INDENT3 "MultitouchMinDistance: %0.1fpx\n",
723 mConfig.pointerGestureMultitouchMinDistance);
Jeff Brown214eaf42011-05-26 19:17:02 -0700724 dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
725 mConfig.pointerGestureSwipeTransitionAngleCosine);
726 dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
727 mConfig.pointerGestureSwipeMaxWidthRatio);
728 dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n",
729 mConfig.pointerGestureMovementSpeedRatio);
730 dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n",
731 mConfig.pointerGestureZoomSpeedRatio);
Jeff Brownb88102f2010-09-08 11:49:43 -0700732}
733
Jeff Brown6d0fec22010-07-23 21:28:06 -0700734
Jeff Brownbe1aa822011-07-27 16:04:54 -0700735// --- InputReader::ContextImpl ---
736
737InputReader::ContextImpl::ContextImpl(InputReader* reader) :
738 mReader(reader) {
739}
740
741void InputReader::ContextImpl::updateGlobalMetaState() {
742 // lock is already held by the input loop
743 mReader->updateGlobalMetaStateLocked();
744}
745
746int32_t InputReader::ContextImpl::getGlobalMetaState() {
747 // lock is already held by the input loop
748 return mReader->getGlobalMetaStateLocked();
749}
750
751void InputReader::ContextImpl::disableVirtualKeysUntil(nsecs_t time) {
752 // lock is already held by the input loop
753 mReader->disableVirtualKeysUntilLocked(time);
754}
755
756bool InputReader::ContextImpl::shouldDropVirtualKey(nsecs_t now,
757 InputDevice* device, int32_t keyCode, int32_t scanCode) {
758 // lock is already held by the input loop
759 return mReader->shouldDropVirtualKeyLocked(now, device, keyCode, scanCode);
760}
761
762void InputReader::ContextImpl::fadePointer() {
763 // lock is already held by the input loop
764 mReader->fadePointerLocked();
765}
766
767void InputReader::ContextImpl::requestTimeoutAtTime(nsecs_t when) {
768 // lock is already held by the input loop
769 mReader->requestTimeoutAtTimeLocked(when);
770}
771
772InputReaderPolicyInterface* InputReader::ContextImpl::getPolicy() {
773 return mReader->mPolicy.get();
774}
775
776InputListenerInterface* InputReader::ContextImpl::getListener() {
777 return mReader->mQueuedListener.get();
778}
779
780EventHubInterface* InputReader::ContextImpl::getEventHub() {
781 return mReader->mEventHub.get();
782}
783
784
Jeff Brown6d0fec22010-07-23 21:28:06 -0700785// --- InputReaderThread ---
786
787InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
788 Thread(/*canCallJava*/ true), mReader(reader) {
789}
790
791InputReaderThread::~InputReaderThread() {
792}
793
794bool InputReaderThread::threadLoop() {
795 mReader->loopOnce();
796 return true;
797}
798
799
800// --- InputDevice ---
801
802InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) :
Jeff Brown80fd47c2011-05-24 01:07:44 -0700803 mContext(context), mId(id), mName(name), mSources(0),
804 mIsExternal(false), mDropUntilNextSync(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700805}
806
807InputDevice::~InputDevice() {
808 size_t numMappers = mMappers.size();
809 for (size_t i = 0; i < numMappers; i++) {
810 delete mMappers[i];
811 }
812 mMappers.clear();
813}
814
Jeff Brownef3d7e82010-09-30 14:33:04 -0700815void InputDevice::dump(String8& dump) {
816 InputDeviceInfo deviceInfo;
817 getDeviceInfo(& deviceInfo);
818
Jeff Brown90655042010-12-02 13:50:46 -0800819 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700820 deviceInfo.getName().string());
Jeff Brown56194eb2011-03-02 19:23:13 -0800821 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700822 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
823 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800824
Jeff Brownefd32662011-03-08 15:13:06 -0800825 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800826 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700827 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800828 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800829 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
830 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800831 char name[32];
832 if (label) {
833 strncpy(name, label, sizeof(name));
834 name[sizeof(name) - 1] = '\0';
835 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800836 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800837 }
Jeff Brownefd32662011-03-08 15:13:06 -0800838 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
839 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
840 name, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Browncc0c1592011-02-19 05:07:28 -0800841 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700842 }
843
844 size_t numMappers = mMappers.size();
845 for (size_t i = 0; i < numMappers; i++) {
846 InputMapper* mapper = mMappers[i];
847 mapper->dump(dump);
848 }
849}
850
Jeff Brown6d0fec22010-07-23 21:28:06 -0700851void InputDevice::addMapper(InputMapper* mapper) {
852 mMappers.add(mapper);
853}
854
Jeff Brown474dcb52011-06-14 20:22:50 -0700855void InputDevice::configure(const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700856 mSources = 0;
857
Jeff Brown474dcb52011-06-14 20:22:50 -0700858 if (!isIgnored()) {
859 if (!changes) { // first time only
860 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
861 }
862
863 size_t numMappers = mMappers.size();
864 for (size_t i = 0; i < numMappers; i++) {
865 InputMapper* mapper = mMappers[i];
866 mapper->configure(config, changes);
867 mSources |= mapper->getSources();
868 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700869 }
870}
871
Jeff Brown6d0fec22010-07-23 21:28:06 -0700872void InputDevice::reset() {
873 size_t numMappers = mMappers.size();
874 for (size_t i = 0; i < numMappers; i++) {
875 InputMapper* mapper = mMappers[i];
876 mapper->reset();
877 }
878}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700879
Jeff Brownb7198742011-03-18 18:14:26 -0700880void InputDevice::process(const RawEvent* rawEvents, size_t count) {
881 // Process all of the events in order for each mapper.
882 // We cannot simply ask each mapper to process them in bulk because mappers may
883 // have side-effects that must be interleaved. For example, joystick movement events and
884 // gamepad button presses are handled by different mappers but they should be dispatched
885 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700886 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700887 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
888#if DEBUG_RAW_EVENTS
889 LOGD("Input event: device=%d type=0x%04x scancode=0x%04x "
Jeff Brown2e45fb62011-06-29 21:19:05 -0700890 "keycode=0x%04x value=0x%08x flags=0x%08x",
Jeff Brownb7198742011-03-18 18:14:26 -0700891 rawEvent->deviceId, rawEvent->type, rawEvent->scanCode, rawEvent->keyCode,
892 rawEvent->value, rawEvent->flags);
893#endif
894
Jeff Brown80fd47c2011-05-24 01:07:44 -0700895 if (mDropUntilNextSync) {
896 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
897 mDropUntilNextSync = false;
898#if DEBUG_RAW_EVENTS
899 LOGD("Recovered from input event buffer overrun.");
900#endif
901 } else {
902#if DEBUG_RAW_EVENTS
903 LOGD("Dropped input event while waiting for next input sync.");
904#endif
905 }
906 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_DROPPED) {
907 LOGI("Detected input event buffer overrun for device %s.", mName.string());
908 mDropUntilNextSync = true;
909 reset();
910 } else {
911 for (size_t i = 0; i < numMappers; i++) {
912 InputMapper* mapper = mMappers[i];
913 mapper->process(rawEvent);
914 }
Jeff Brownb7198742011-03-18 18:14:26 -0700915 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700916 }
917}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700918
Jeff Brownaa3855d2011-03-17 01:34:19 -0700919void InputDevice::timeoutExpired(nsecs_t when) {
920 size_t numMappers = mMappers.size();
921 for (size_t i = 0; i < numMappers; i++) {
922 InputMapper* mapper = mMappers[i];
923 mapper->timeoutExpired(when);
924 }
925}
926
Jeff Brown6d0fec22010-07-23 21:28:06 -0700927void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
928 outDeviceInfo->initialize(mId, mName);
929
930 size_t numMappers = mMappers.size();
931 for (size_t i = 0; i < numMappers; i++) {
932 InputMapper* mapper = mMappers[i];
933 mapper->populateDeviceInfo(outDeviceInfo);
934 }
935}
936
937int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
938 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
939}
940
941int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
942 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
943}
944
945int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
946 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
947}
948
949int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
950 int32_t result = AKEY_STATE_UNKNOWN;
951 size_t numMappers = mMappers.size();
952 for (size_t i = 0; i < numMappers; i++) {
953 InputMapper* mapper = mMappers[i];
954 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
955 result = (mapper->*getStateFunc)(sourceMask, code);
956 if (result >= AKEY_STATE_DOWN) {
957 return result;
958 }
959 }
960 }
961 return result;
962}
963
964bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
965 const int32_t* keyCodes, uint8_t* outFlags) {
966 bool result = false;
967 size_t numMappers = mMappers.size();
968 for (size_t i = 0; i < numMappers; i++) {
969 InputMapper* mapper = mMappers[i];
970 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
971 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
972 }
973 }
974 return result;
975}
976
977int32_t InputDevice::getMetaState() {
978 int32_t result = 0;
979 size_t numMappers = mMappers.size();
980 for (size_t i = 0; i < numMappers; i++) {
981 InputMapper* mapper = mMappers[i];
982 result |= mapper->getMetaState();
983 }
984 return result;
985}
986
Jeff Brown05dc66a2011-03-02 14:41:58 -0800987void InputDevice::fadePointer() {
988 size_t numMappers = mMappers.size();
989 for (size_t i = 0; i < numMappers; i++) {
990 InputMapper* mapper = mMappers[i];
991 mapper->fadePointer();
992 }
993}
994
Jeff Brown6d0fec22010-07-23 21:28:06 -0700995
Jeff Brown49754db2011-07-01 17:37:58 -0700996// --- CursorButtonAccumulator ---
997
998CursorButtonAccumulator::CursorButtonAccumulator() {
999 clearButtons();
1000}
1001
1002void CursorButtonAccumulator::clearButtons() {
1003 mBtnLeft = 0;
1004 mBtnRight = 0;
1005 mBtnMiddle = 0;
1006 mBtnBack = 0;
1007 mBtnSide = 0;
1008 mBtnForward = 0;
1009 mBtnExtra = 0;
1010 mBtnTask = 0;
1011}
1012
1013void CursorButtonAccumulator::process(const RawEvent* rawEvent) {
1014 if (rawEvent->type == EV_KEY) {
1015 switch (rawEvent->scanCode) {
1016 case BTN_LEFT:
1017 mBtnLeft = rawEvent->value;
1018 break;
1019 case BTN_RIGHT:
1020 mBtnRight = rawEvent->value;
1021 break;
1022 case BTN_MIDDLE:
1023 mBtnMiddle = rawEvent->value;
1024 break;
1025 case BTN_BACK:
1026 mBtnBack = rawEvent->value;
1027 break;
1028 case BTN_SIDE:
1029 mBtnSide = rawEvent->value;
1030 break;
1031 case BTN_FORWARD:
1032 mBtnForward = rawEvent->value;
1033 break;
1034 case BTN_EXTRA:
1035 mBtnExtra = rawEvent->value;
1036 break;
1037 case BTN_TASK:
1038 mBtnTask = rawEvent->value;
1039 break;
1040 }
1041 }
1042}
1043
1044uint32_t CursorButtonAccumulator::getButtonState() const {
1045 uint32_t result = 0;
1046 if (mBtnLeft) {
1047 result |= AMOTION_EVENT_BUTTON_PRIMARY;
1048 }
1049 if (mBtnRight) {
1050 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1051 }
1052 if (mBtnMiddle) {
1053 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1054 }
1055 if (mBtnBack || mBtnSide) {
1056 result |= AMOTION_EVENT_BUTTON_BACK;
1057 }
1058 if (mBtnForward || mBtnExtra) {
1059 result |= AMOTION_EVENT_BUTTON_FORWARD;
1060 }
1061 return result;
1062}
1063
1064
1065// --- CursorMotionAccumulator ---
1066
1067CursorMotionAccumulator::CursorMotionAccumulator() :
1068 mHaveRelWheel(false), mHaveRelHWheel(false) {
1069 clearRelativeAxes();
1070}
1071
1072void CursorMotionAccumulator::configure(InputDevice* device) {
1073 mHaveRelWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_WHEEL);
1074 mHaveRelHWheel = device->getEventHub()->hasRelativeAxis(device->getId(), REL_HWHEEL);
1075}
1076
1077void CursorMotionAccumulator::clearRelativeAxes() {
1078 mRelX = 0;
1079 mRelY = 0;
1080 mRelWheel = 0;
1081 mRelHWheel = 0;
1082}
1083
1084void CursorMotionAccumulator::process(const RawEvent* rawEvent) {
1085 if (rawEvent->type == EV_REL) {
1086 switch (rawEvent->scanCode) {
1087 case REL_X:
1088 mRelX = rawEvent->value;
1089 break;
1090 case REL_Y:
1091 mRelY = rawEvent->value;
1092 break;
1093 case REL_WHEEL:
1094 mRelWheel = rawEvent->value;
1095 break;
1096 case REL_HWHEEL:
1097 mRelHWheel = rawEvent->value;
1098 break;
1099 }
1100 }
1101}
1102
1103
1104// --- TouchButtonAccumulator ---
1105
1106TouchButtonAccumulator::TouchButtonAccumulator() :
1107 mHaveBtnTouch(false) {
1108 clearButtons();
1109}
1110
1111void TouchButtonAccumulator::configure(InputDevice* device) {
1112 mHaveBtnTouch = device->getEventHub()->hasScanCode(device->getId(), BTN_TOUCH);
1113}
1114
1115void TouchButtonAccumulator::clearButtons() {
1116 mBtnTouch = 0;
1117 mBtnStylus = 0;
1118 mBtnStylus2 = 0;
1119 mBtnToolFinger = 0;
1120 mBtnToolPen = 0;
1121 mBtnToolRubber = 0;
1122}
1123
1124void TouchButtonAccumulator::process(const RawEvent* rawEvent) {
1125 if (rawEvent->type == EV_KEY) {
1126 switch (rawEvent->scanCode) {
1127 case BTN_TOUCH:
1128 mBtnTouch = rawEvent->value;
1129 break;
1130 case BTN_STYLUS:
1131 mBtnStylus = rawEvent->value;
1132 break;
1133 case BTN_STYLUS2:
1134 mBtnStylus2 = rawEvent->value;
1135 break;
1136 case BTN_TOOL_FINGER:
1137 mBtnToolFinger = rawEvent->value;
1138 break;
1139 case BTN_TOOL_PEN:
1140 mBtnToolPen = rawEvent->value;
1141 break;
1142 case BTN_TOOL_RUBBER:
1143 mBtnToolRubber = rawEvent->value;
1144 break;
1145 }
1146 }
1147}
1148
1149uint32_t TouchButtonAccumulator::getButtonState() const {
1150 uint32_t result = 0;
1151 if (mBtnStylus) {
1152 result |= AMOTION_EVENT_BUTTON_SECONDARY;
1153 }
1154 if (mBtnStylus2) {
1155 result |= AMOTION_EVENT_BUTTON_TERTIARY;
1156 }
1157 return result;
1158}
1159
1160int32_t TouchButtonAccumulator::getToolType() const {
1161 if (mBtnToolRubber) {
1162 return AMOTION_EVENT_TOOL_TYPE_ERASER;
1163 }
1164 if (mBtnToolPen) {
1165 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1166 }
1167 if (mBtnToolFinger) {
1168 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1169 }
1170 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1171}
1172
Jeff Brownd87c6d52011-08-10 14:55:59 -07001173bool TouchButtonAccumulator::isToolActive() const {
1174 return mBtnTouch || mBtnToolFinger || mBtnToolPen || mBtnToolRubber;
Jeff Brown49754db2011-07-01 17:37:58 -07001175}
1176
1177bool TouchButtonAccumulator::isHovering() const {
1178 return mHaveBtnTouch && !mBtnTouch;
1179}
1180
1181
Jeff Brownbe1aa822011-07-27 16:04:54 -07001182// --- RawPointerAxes ---
1183
1184RawPointerAxes::RawPointerAxes() {
1185 clear();
1186}
1187
1188void RawPointerAxes::clear() {
1189 x.clear();
1190 y.clear();
1191 pressure.clear();
1192 touchMajor.clear();
1193 touchMinor.clear();
1194 toolMajor.clear();
1195 toolMinor.clear();
1196 orientation.clear();
1197 distance.clear();
1198 trackingId.clear();
1199 slot.clear();
1200}
1201
1202
1203// --- RawPointerData ---
1204
1205RawPointerData::RawPointerData() {
1206 clear();
1207}
1208
1209void RawPointerData::clear() {
1210 pointerCount = 0;
1211 clearIdBits();
1212}
1213
1214void RawPointerData::copyFrom(const RawPointerData& other) {
1215 pointerCount = other.pointerCount;
1216 hoveringIdBits = other.hoveringIdBits;
1217 touchingIdBits = other.touchingIdBits;
1218
1219 for (uint32_t i = 0; i < pointerCount; i++) {
1220 pointers[i] = other.pointers[i];
1221
1222 int id = pointers[i].id;
1223 idToIndex[id] = other.idToIndex[id];
1224 }
1225}
1226
1227void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
1228 float x = 0, y = 0;
1229 uint32_t count = touchingIdBits.count();
1230 if (count) {
1231 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) {
1232 uint32_t id = idBits.clearFirstMarkedBit();
1233 const Pointer& pointer = pointerForId(id);
1234 x += pointer.x;
1235 y += pointer.y;
1236 }
1237 x /= count;
1238 y /= count;
1239 }
1240 *outX = x;
1241 *outY = y;
1242}
1243
1244
1245// --- CookedPointerData ---
1246
1247CookedPointerData::CookedPointerData() {
1248 clear();
1249}
1250
1251void CookedPointerData::clear() {
1252 pointerCount = 0;
1253 hoveringIdBits.clear();
1254 touchingIdBits.clear();
1255}
1256
1257void CookedPointerData::copyFrom(const CookedPointerData& other) {
1258 pointerCount = other.pointerCount;
1259 hoveringIdBits = other.hoveringIdBits;
1260 touchingIdBits = other.touchingIdBits;
1261
1262 for (uint32_t i = 0; i < pointerCount; i++) {
1263 pointerProperties[i].copyFrom(other.pointerProperties[i]);
1264 pointerCoords[i].copyFrom(other.pointerCoords[i]);
1265
1266 int id = pointerProperties[i].id;
1267 idToIndex[id] = other.idToIndex[id];
1268 }
1269}
1270
1271
Jeff Brown49754db2011-07-01 17:37:58 -07001272// --- SingleTouchMotionAccumulator ---
1273
1274SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() {
1275 clearAbsoluteAxes();
1276}
1277
1278void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
1279 mAbsX = 0;
1280 mAbsY = 0;
1281 mAbsPressure = 0;
1282 mAbsToolWidth = 0;
1283 mAbsDistance = 0;
1284}
1285
1286void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1287 if (rawEvent->type == EV_ABS) {
1288 switch (rawEvent->scanCode) {
1289 case ABS_X:
1290 mAbsX = rawEvent->value;
1291 break;
1292 case ABS_Y:
1293 mAbsY = rawEvent->value;
1294 break;
1295 case ABS_PRESSURE:
1296 mAbsPressure = rawEvent->value;
1297 break;
1298 case ABS_TOOL_WIDTH:
1299 mAbsToolWidth = rawEvent->value;
1300 break;
1301 case ABS_DISTANCE:
1302 mAbsDistance = rawEvent->value;
1303 break;
1304 }
1305 }
1306}
1307
1308
1309// --- MultiTouchMotionAccumulator ---
1310
1311MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
1312 mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false) {
1313}
1314
1315MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
1316 delete[] mSlots;
1317}
1318
1319void MultiTouchMotionAccumulator::configure(size_t slotCount, bool usingSlotsProtocol) {
1320 mSlotCount = slotCount;
1321 mUsingSlotsProtocol = usingSlotsProtocol;
1322
1323 delete[] mSlots;
1324 mSlots = new Slot[slotCount];
1325}
1326
1327void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
1328 for (size_t i = 0; i < mSlotCount; i++) {
1329 mSlots[i].clearIfInUse();
1330 }
1331 mCurrentSlot = initialSlot;
1332}
1333
1334void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1335 if (rawEvent->type == EV_ABS) {
1336 bool newSlot = false;
1337 if (mUsingSlotsProtocol) {
1338 if (rawEvent->scanCode == ABS_MT_SLOT) {
1339 mCurrentSlot = rawEvent->value;
1340 newSlot = true;
1341 }
1342 } else if (mCurrentSlot < 0) {
1343 mCurrentSlot = 0;
1344 }
1345
1346 if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) {
1347#if DEBUG_POINTERS
1348 if (newSlot) {
1349 LOGW("MultiTouch device emitted invalid slot index %d but it "
1350 "should be between 0 and %d; ignoring this slot.",
1351 mCurrentSlot, mSlotCount - 1);
1352 }
1353#endif
1354 } else {
1355 Slot* slot = &mSlots[mCurrentSlot];
1356
1357 switch (rawEvent->scanCode) {
1358 case ABS_MT_POSITION_X:
1359 slot->mInUse = true;
1360 slot->mAbsMTPositionX = rawEvent->value;
1361 break;
1362 case ABS_MT_POSITION_Y:
1363 slot->mInUse = true;
1364 slot->mAbsMTPositionY = rawEvent->value;
1365 break;
1366 case ABS_MT_TOUCH_MAJOR:
1367 slot->mInUse = true;
1368 slot->mAbsMTTouchMajor = rawEvent->value;
1369 break;
1370 case ABS_MT_TOUCH_MINOR:
1371 slot->mInUse = true;
1372 slot->mAbsMTTouchMinor = rawEvent->value;
1373 slot->mHaveAbsMTTouchMinor = true;
1374 break;
1375 case ABS_MT_WIDTH_MAJOR:
1376 slot->mInUse = true;
1377 slot->mAbsMTWidthMajor = rawEvent->value;
1378 break;
1379 case ABS_MT_WIDTH_MINOR:
1380 slot->mInUse = true;
1381 slot->mAbsMTWidthMinor = rawEvent->value;
1382 slot->mHaveAbsMTWidthMinor = true;
1383 break;
1384 case ABS_MT_ORIENTATION:
1385 slot->mInUse = true;
1386 slot->mAbsMTOrientation = rawEvent->value;
1387 break;
1388 case ABS_MT_TRACKING_ID:
1389 if (mUsingSlotsProtocol && rawEvent->value < 0) {
1390 slot->clearIfInUse();
1391 } else {
1392 slot->mInUse = true;
1393 slot->mAbsMTTrackingId = rawEvent->value;
1394 }
1395 break;
1396 case ABS_MT_PRESSURE:
1397 slot->mInUse = true;
1398 slot->mAbsMTPressure = rawEvent->value;
1399 break;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001400 case ABS_MT_DISTANCE:
1401 slot->mInUse = true;
1402 slot->mAbsMTDistance = rawEvent->value;
1403 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001404 case ABS_MT_TOOL_TYPE:
1405 slot->mInUse = true;
1406 slot->mAbsMTToolType = rawEvent->value;
1407 slot->mHaveAbsMTToolType = true;
1408 break;
1409 }
1410 }
1411 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_MT_REPORT) {
1412 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
1413 mCurrentSlot += 1;
1414 }
1415}
1416
1417
1418// --- MultiTouchMotionAccumulator::Slot ---
1419
1420MultiTouchMotionAccumulator::Slot::Slot() {
1421 clear();
1422}
1423
1424void MultiTouchMotionAccumulator::Slot::clearIfInUse() {
1425 if (mInUse) {
1426 clear();
1427 }
1428}
1429
1430void MultiTouchMotionAccumulator::Slot::clear() {
1431 mInUse = false;
1432 mHaveAbsMTTouchMinor = false;
1433 mHaveAbsMTWidthMinor = false;
1434 mHaveAbsMTToolType = false;
1435 mAbsMTPositionX = 0;
1436 mAbsMTPositionY = 0;
1437 mAbsMTTouchMajor = 0;
1438 mAbsMTTouchMinor = 0;
1439 mAbsMTWidthMajor = 0;
1440 mAbsMTWidthMinor = 0;
1441 mAbsMTOrientation = 0;
1442 mAbsMTTrackingId = -1;
1443 mAbsMTPressure = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001444 mAbsMTDistance = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001445 mAbsMTToolType = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001446}
1447
1448int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
1449 if (mHaveAbsMTToolType) {
1450 switch (mAbsMTToolType) {
1451 case MT_TOOL_FINGER:
1452 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1453 case MT_TOOL_PEN:
1454 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1455 }
1456 }
1457 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1458}
1459
1460
Jeff Brown6d0fec22010-07-23 21:28:06 -07001461// --- InputMapper ---
1462
1463InputMapper::InputMapper(InputDevice* device) :
1464 mDevice(device), mContext(device->getContext()) {
1465}
1466
1467InputMapper::~InputMapper() {
1468}
1469
1470void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1471 info->addSource(getSources());
1472}
1473
Jeff Brownef3d7e82010-09-30 14:33:04 -07001474void InputMapper::dump(String8& dump) {
1475}
1476
Jeff Brown474dcb52011-06-14 20:22:50 -07001477void InputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001478}
1479
1480void InputMapper::reset() {
1481}
1482
Jeff Brownaa3855d2011-03-17 01:34:19 -07001483void InputMapper::timeoutExpired(nsecs_t when) {
1484}
1485
Jeff Brown6d0fec22010-07-23 21:28:06 -07001486int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1487 return AKEY_STATE_UNKNOWN;
1488}
1489
1490int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1491 return AKEY_STATE_UNKNOWN;
1492}
1493
1494int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1495 return AKEY_STATE_UNKNOWN;
1496}
1497
1498bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1499 const int32_t* keyCodes, uint8_t* outFlags) {
1500 return false;
1501}
1502
1503int32_t InputMapper::getMetaState() {
1504 return 0;
1505}
1506
Jeff Brown05dc66a2011-03-02 14:41:58 -08001507void InputMapper::fadePointer() {
1508}
1509
Jeff Brownbe1aa822011-07-27 16:04:54 -07001510status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) {
1511 return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo);
1512}
1513
Jeff Browncb1404e2011-01-15 18:14:15 -08001514void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1515 const RawAbsoluteAxisInfo& axis, const char* name) {
1516 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001517 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1518 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001519 } else {
1520 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1521 }
1522}
1523
Jeff Brown6d0fec22010-07-23 21:28:06 -07001524
1525// --- SwitchInputMapper ---
1526
1527SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1528 InputMapper(device) {
1529}
1530
1531SwitchInputMapper::~SwitchInputMapper() {
1532}
1533
1534uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001535 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001536}
1537
1538void SwitchInputMapper::process(const RawEvent* rawEvent) {
1539 switch (rawEvent->type) {
1540 case EV_SW:
1541 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
1542 break;
1543 }
1544}
1545
1546void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001547 NotifySwitchArgs args(when, 0, switchCode, switchValue);
1548 getListener()->notifySwitch(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001549}
1550
1551int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1552 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1553}
1554
1555
1556// --- KeyboardInputMapper ---
1557
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001558KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001559 uint32_t source, int32_t keyboardType) :
1560 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001561 mKeyboardType(keyboardType) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001562 initialize();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001563}
1564
1565KeyboardInputMapper::~KeyboardInputMapper() {
1566}
1567
Jeff Brownbe1aa822011-07-27 16:04:54 -07001568void KeyboardInputMapper::initialize() {
1569 mMetaState = AMETA_NONE;
1570 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001571}
1572
1573uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001574 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001575}
1576
1577void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1578 InputMapper::populateDeviceInfo(info);
1579
1580 info->setKeyboardType(mKeyboardType);
1581}
1582
Jeff Brownef3d7e82010-09-30 14:33:04 -07001583void KeyboardInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001584 dump.append(INDENT2 "Keyboard Input Mapper:\n");
1585 dumpParameters(dump);
1586 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
1587 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size());
1588 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
1589 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001590}
1591
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001592
Jeff Brown474dcb52011-06-14 20:22:50 -07001593void KeyboardInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
1594 InputMapper::configure(config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001595
Jeff Brown474dcb52011-06-14 20:22:50 -07001596 if (!changes) { // first time only
1597 // Configure basic parameters.
1598 configureParameters();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001599
Jeff Brown474dcb52011-06-14 20:22:50 -07001600 // Reset LEDs.
Jeff Brownbe1aa822011-07-27 16:04:54 -07001601 resetLedState();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001602 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001603}
1604
1605void KeyboardInputMapper::configureParameters() {
1606 mParameters.orientationAware = false;
1607 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
1608 mParameters.orientationAware);
1609
Jeff Brownbc68a592011-07-25 12:58:12 -07001610 mParameters.associatedDisplayId = -1;
1611 if (mParameters.orientationAware) {
1612 mParameters.associatedDisplayId = 0;
1613 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001614}
1615
1616void KeyboardInputMapper::dumpParameters(String8& dump) {
1617 dump.append(INDENT3 "Parameters:\n");
1618 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1619 mParameters.associatedDisplayId);
1620 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1621 toString(mParameters.orientationAware));
1622}
1623
Jeff Brown6d0fec22010-07-23 21:28:06 -07001624void KeyboardInputMapper::reset() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001625 // Synthesize key up event on reset if keys are currently down.
1626 while (!mKeyDowns.isEmpty()) {
1627 const KeyDown& keyDown = mKeyDowns.top();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001628 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001629 processKey(when, false, keyDown.keyCode, keyDown.scanCode, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001630 }
1631
Jeff Brownbe1aa822011-07-27 16:04:54 -07001632 initialize();
1633 resetLedState();
1634
Jeff Brown6d0fec22010-07-23 21:28:06 -07001635 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001636 getContext()->updateGlobalMetaState();
1637}
1638
1639void KeyboardInputMapper::process(const RawEvent* rawEvent) {
1640 switch (rawEvent->type) {
1641 case EV_KEY: {
1642 int32_t scanCode = rawEvent->scanCode;
1643 if (isKeyboardOrGamepadKey(scanCode)) {
1644 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
1645 rawEvent->flags);
1646 }
1647 break;
1648 }
1649 }
1650}
1651
1652bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
1653 return scanCode < BTN_MOUSE
1654 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001655 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08001656 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001657}
1658
Jeff Brown6328cdc2010-07-29 18:18:33 -07001659void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
1660 int32_t scanCode, uint32_t policyFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001661
Jeff Brownbe1aa822011-07-27 16:04:54 -07001662 if (down) {
1663 // Rotate key codes according to orientation if needed.
1664 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1665 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
1666 int32_t orientation;
1667 if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1668 false /*external*/, NULL, NULL, & orientation)) {
1669 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001670 }
1671
Jeff Brownbe1aa822011-07-27 16:04:54 -07001672 keyCode = rotateKeyCode(keyCode, orientation);
1673 }
Jeff Brownfe508922011-01-18 15:10:10 -08001674
Jeff Brownbe1aa822011-07-27 16:04:54 -07001675 // Add key down.
1676 ssize_t keyDownIndex = findKeyDown(scanCode);
1677 if (keyDownIndex >= 0) {
1678 // key repeat, be sure to use same keycode as before in case of rotation
1679 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001680 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001681 // key down
1682 if ((policyFlags & POLICY_FLAG_VIRTUAL)
1683 && mContext->shouldDropVirtualKey(when,
1684 getDevice(), keyCode, scanCode)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001685 return;
1686 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001687
1688 mKeyDowns.push();
1689 KeyDown& keyDown = mKeyDowns.editTop();
1690 keyDown.keyCode = keyCode;
1691 keyDown.scanCode = scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001692 }
1693
Jeff Brownbe1aa822011-07-27 16:04:54 -07001694 mDownTime = when;
1695 } else {
1696 // Remove key down.
1697 ssize_t keyDownIndex = findKeyDown(scanCode);
1698 if (keyDownIndex >= 0) {
1699 // key up, be sure to use same keycode as before in case of rotation
1700 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
1701 mKeyDowns.removeAt(size_t(keyDownIndex));
1702 } else {
1703 // key was not actually down
1704 LOGI("Dropping key up from device %s because the key was not down. "
1705 "keyCode=%d, scanCode=%d",
1706 getDeviceName().string(), keyCode, scanCode);
1707 return;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001708 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001709 }
Jeff Brownfd0358292010-06-30 16:10:35 -07001710
Jeff Brownbe1aa822011-07-27 16:04:54 -07001711 bool metaStateChanged = false;
1712 int32_t oldMetaState = mMetaState;
1713 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
1714 if (oldMetaState != newMetaState) {
1715 mMetaState = newMetaState;
1716 metaStateChanged = true;
1717 updateLedState(false);
1718 }
1719
1720 nsecs_t downTime = mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001721
Jeff Brown56194eb2011-03-02 19:23:13 -08001722 // Key down on external an keyboard should wake the device.
1723 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
1724 // For internal keyboards, the key layout file should specify the policy flags for
1725 // each wake key individually.
1726 // TODO: Use the input device configuration to control this behavior more finely.
1727 if (down && getDevice()->isExternal()
1728 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1729 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1730 }
1731
Jeff Brown6328cdc2010-07-29 18:18:33 -07001732 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001733 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001734 }
1735
Jeff Brown05dc66a2011-03-02 14:41:58 -08001736 if (down && !isMetaKey(keyCode)) {
1737 getContext()->fadePointer();
1738 }
1739
Jeff Brownbe1aa822011-07-27 16:04:54 -07001740 NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001741 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1742 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001743 getListener()->notifyKey(&args);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001744}
1745
Jeff Brownbe1aa822011-07-27 16:04:54 -07001746ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
1747 size_t n = mKeyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001748 for (size_t i = 0; i < n; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001749 if (mKeyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001750 return i;
1751 }
1752 }
1753 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001754}
1755
Jeff Brown6d0fec22010-07-23 21:28:06 -07001756int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1757 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1758}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001759
Jeff Brown6d0fec22010-07-23 21:28:06 -07001760int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1761 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1762}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001763
Jeff Brown6d0fec22010-07-23 21:28:06 -07001764bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1765 const int32_t* keyCodes, uint8_t* outFlags) {
1766 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1767}
1768
1769int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001770 return mMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001771}
1772
Jeff Brownbe1aa822011-07-27 16:04:54 -07001773void KeyboardInputMapper::resetLedState() {
1774 initializeLedState(mCapsLockLedState, LED_CAPSL);
1775 initializeLedState(mNumLockLedState, LED_NUML);
1776 initializeLedState(mScrollLockLedState, LED_SCROLLL);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001777
Jeff Brownbe1aa822011-07-27 16:04:54 -07001778 updateLedState(true);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001779}
1780
Jeff Brownbe1aa822011-07-27 16:04:54 -07001781void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08001782 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
1783 ledState.on = false;
1784}
1785
Jeff Brownbe1aa822011-07-27 16:04:54 -07001786void KeyboardInputMapper::updateLedState(bool reset) {
1787 updateLedStateForModifier(mCapsLockLedState, LED_CAPSL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001788 AMETA_CAPS_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001789 updateLedStateForModifier(mNumLockLedState, LED_NUML,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001790 AMETA_NUM_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001791 updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001792 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001793}
1794
Jeff Brownbe1aa822011-07-27 16:04:54 -07001795void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
Jeff Brown497a92c2010-09-12 17:55:08 -07001796 int32_t led, int32_t modifier, bool reset) {
1797 if (ledState.avail) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001798 bool desiredState = (mMetaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001799 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001800 getEventHub()->setLedState(getDeviceId(), led, desiredState);
1801 ledState.on = desiredState;
1802 }
1803 }
1804}
1805
Jeff Brown6d0fec22010-07-23 21:28:06 -07001806
Jeff Brown83c09682010-12-23 17:50:18 -08001807// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07001808
Jeff Brown83c09682010-12-23 17:50:18 -08001809CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001810 InputMapper(device) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001811 initialize();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001812}
1813
Jeff Brown83c09682010-12-23 17:50:18 -08001814CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001815}
1816
Jeff Brown83c09682010-12-23 17:50:18 -08001817uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001818 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001819}
1820
Jeff Brown83c09682010-12-23 17:50:18 -08001821void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001822 InputMapper::populateDeviceInfo(info);
1823
Jeff Brown83c09682010-12-23 17:50:18 -08001824 if (mParameters.mode == Parameters::MODE_POINTER) {
1825 float minX, minY, maxX, maxY;
1826 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08001827 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
1828 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08001829 }
1830 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08001831 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
1832 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08001833 }
Jeff Brownefd32662011-03-08 15:13:06 -08001834 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001835
Jeff Brown49754db2011-07-01 17:37:58 -07001836 if (mCursorMotionAccumulator.haveRelativeVWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08001837 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001838 }
Jeff Brown49754db2011-07-01 17:37:58 -07001839 if (mCursorMotionAccumulator.haveRelativeHWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08001840 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001841 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001842}
1843
Jeff Brown83c09682010-12-23 17:50:18 -08001844void CursorInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001845 dump.append(INDENT2 "Cursor Input Mapper:\n");
1846 dumpParameters(dump);
1847 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
1848 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
1849 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1850 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
1851 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
1852 toString(mCursorMotionAccumulator.haveRelativeVWheel()));
1853 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
1854 toString(mCursorMotionAccumulator.haveRelativeHWheel()));
1855 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
1856 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
1857 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
1858 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
1859 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001860}
1861
Jeff Brown474dcb52011-06-14 20:22:50 -07001862void CursorInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
1863 InputMapper::configure(config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001864
Jeff Brown474dcb52011-06-14 20:22:50 -07001865 if (!changes) { // first time only
Jeff Brown49754db2011-07-01 17:37:58 -07001866 mCursorMotionAccumulator.configure(getDevice());
1867
Jeff Brown474dcb52011-06-14 20:22:50 -07001868 // Configure basic parameters.
1869 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08001870
Jeff Brown474dcb52011-06-14 20:22:50 -07001871 // Configure device mode.
1872 switch (mParameters.mode) {
1873 case Parameters::MODE_POINTER:
1874 mSource = AINPUT_SOURCE_MOUSE;
1875 mXPrecision = 1.0f;
1876 mYPrecision = 1.0f;
1877 mXScale = 1.0f;
1878 mYScale = 1.0f;
1879 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
1880 break;
1881 case Parameters::MODE_NAVIGATION:
1882 mSource = AINPUT_SOURCE_TRACKBALL;
1883 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1884 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1885 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1886 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1887 break;
1888 }
1889
1890 mVWheelScale = 1.0f;
1891 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08001892 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001893
Jeff Brown474dcb52011-06-14 20:22:50 -07001894 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
1895 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
1896 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
1897 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
1898 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001899}
1900
Jeff Brown83c09682010-12-23 17:50:18 -08001901void CursorInputMapper::configureParameters() {
1902 mParameters.mode = Parameters::MODE_POINTER;
1903 String8 cursorModeString;
1904 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
1905 if (cursorModeString == "navigation") {
1906 mParameters.mode = Parameters::MODE_NAVIGATION;
1907 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
1908 LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
1909 }
1910 }
1911
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001912 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08001913 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001914 mParameters.orientationAware);
1915
Jeff Brownbc68a592011-07-25 12:58:12 -07001916 mParameters.associatedDisplayId = -1;
1917 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
1918 mParameters.associatedDisplayId = 0;
1919 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001920}
1921
Jeff Brown83c09682010-12-23 17:50:18 -08001922void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001923 dump.append(INDENT3 "Parameters:\n");
1924 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1925 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08001926
1927 switch (mParameters.mode) {
1928 case Parameters::MODE_POINTER:
1929 dump.append(INDENT4 "Mode: pointer\n");
1930 break;
1931 case Parameters::MODE_NAVIGATION:
1932 dump.append(INDENT4 "Mode: navigation\n");
1933 break;
1934 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07001935 LOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08001936 }
1937
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001938 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1939 toString(mParameters.orientationAware));
1940}
1941
Jeff Brownbe1aa822011-07-27 16:04:54 -07001942void CursorInputMapper::initialize() {
Jeff Brown49754db2011-07-01 17:37:58 -07001943 mCursorButtonAccumulator.clearButtons();
1944 mCursorMotionAccumulator.clearRelativeAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001945
Jeff Brownbe1aa822011-07-27 16:04:54 -07001946 mButtonState = 0;
1947 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001948}
1949
Jeff Brown83c09682010-12-23 17:50:18 -08001950void CursorInputMapper::reset() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001951 // Reset velocity.
1952 mPointerVelocityControl.reset();
1953 mWheelXVelocityControl.reset();
1954 mWheelYVelocityControl.reset();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001955
Jeff Brownbe1aa822011-07-27 16:04:54 -07001956 // Synthesize button up event on reset.
1957 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
1958 mCursorButtonAccumulator.clearButtons();
1959 mCursorMotionAccumulator.clearRelativeAxes();
1960 sync(when);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001961
Jeff Brownbe1aa822011-07-27 16:04:54 -07001962 initialize();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001963
Jeff Brown6d0fec22010-07-23 21:28:06 -07001964 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001965}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001966
Jeff Brown83c09682010-12-23 17:50:18 -08001967void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07001968 mCursorButtonAccumulator.process(rawEvent);
1969 mCursorMotionAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08001970
Jeff Brown49754db2011-07-01 17:37:58 -07001971 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
1972 sync(rawEvent->when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001973 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001974}
1975
Jeff Brown83c09682010-12-23 17:50:18 -08001976void CursorInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001977 int32_t lastButtonState = mButtonState;
1978 int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
1979 mButtonState = currentButtonState;
1980
1981 bool wasDown = isPointerDown(lastButtonState);
1982 bool down = isPointerDown(currentButtonState);
1983 bool downChanged;
1984 if (!wasDown && down) {
1985 mDownTime = when;
1986 downChanged = true;
1987 } else if (wasDown && !down) {
1988 downChanged = true;
1989 } else {
1990 downChanged = false;
1991 }
1992 nsecs_t downTime = mDownTime;
1993 bool buttonsChanged = currentButtonState != lastButtonState;
1994
1995 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
1996 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
1997 bool moved = deltaX != 0 || deltaY != 0;
1998
1999 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
2000 && (deltaX != 0.0f || deltaY != 0.0f)) {
2001 // Rotate motion based on display orientation if needed.
2002 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
2003 int32_t orientation;
2004 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
2005 false /*external*/, NULL, NULL, & orientation)) {
2006 orientation = DISPLAY_ORIENTATION_0;
2007 }
2008
2009 rotateDelta(orientation, &deltaX, &deltaY);
2010 }
2011
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002012 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002013 pointerProperties.clear();
2014 pointerProperties.id = 0;
2015 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2016
Jeff Brown6328cdc2010-07-29 18:18:33 -07002017 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002018 pointerCoords.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002019
Jeff Brownbe1aa822011-07-27 16:04:54 -07002020 float vscroll = mCursorMotionAccumulator.getRelativeVWheel();
2021 float hscroll = mCursorMotionAccumulator.getRelativeHWheel();
2022 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002023
Jeff Brownbe1aa822011-07-27 16:04:54 -07002024 mWheelYVelocityControl.move(when, NULL, &vscroll);
2025 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002026
Jeff Brownbe1aa822011-07-27 16:04:54 -07002027 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002028
Jeff Brownbe1aa822011-07-27 16:04:54 -07002029 if (mPointerController != NULL) {
2030 if (moved || scrolled || buttonsChanged) {
2031 mPointerController->setPresentation(
2032 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002033
Jeff Brownbe1aa822011-07-27 16:04:54 -07002034 if (moved) {
2035 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002036 }
2037
Jeff Brownbe1aa822011-07-27 16:04:54 -07002038 if (buttonsChanged) {
2039 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002040 }
Jeff Brownefd32662011-03-08 15:13:06 -08002041
Jeff Brownbe1aa822011-07-27 16:04:54 -07002042 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002043 }
2044
Jeff Brownbe1aa822011-07-27 16:04:54 -07002045 float x, y;
2046 mPointerController->getPosition(&x, &y);
2047 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2048 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2049 } else {
2050 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2051 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
2052 }
2053
2054 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002055
Jeff Brown56194eb2011-03-02 19:23:13 -08002056 // Moving an external trackball or mouse should wake the device.
2057 // We don't do this for internal cursor devices to prevent them from waking up
2058 // the device in your pocket.
2059 // TODO: Use the input device configuration to control this behavior more finely.
2060 uint32_t policyFlags = 0;
2061 if (getDevice()->isExternal()) {
2062 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2063 }
2064
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002065 // Synthesize key down from buttons if needed.
2066 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2067 policyFlags, lastButtonState, currentButtonState);
2068
2069 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002070 if (downChanged || moved || scrolled || buttonsChanged) {
2071 int32_t metaState = mContext->getGlobalMetaState();
2072 int32_t motionEventAction;
2073 if (downChanged) {
2074 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2075 } else if (down || mPointerController == NULL) {
2076 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2077 } else {
2078 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2079 }
Jeff Brownb6997262010-10-08 22:31:17 -07002080
Jeff Brownbe1aa822011-07-27 16:04:54 -07002081 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2082 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002083 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002084 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002085
Jeff Brownbe1aa822011-07-27 16:04:54 -07002086 // Send hover move after UP to tell the application that the mouse is hovering now.
2087 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2088 && mPointerController != NULL) {
2089 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2090 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2091 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2092 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2093 getListener()->notifyMotion(&hoverArgs);
2094 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002095
Jeff Brownbe1aa822011-07-27 16:04:54 -07002096 // Send scroll events.
2097 if (scrolled) {
2098 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2099 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2100
2101 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2102 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2103 AMOTION_EVENT_EDGE_FLAG_NONE,
2104 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2105 getListener()->notifyMotion(&scrollArgs);
2106 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002107 }
Jeff Browna032cc02011-03-07 16:56:21 -08002108
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002109 // Synthesize key up from buttons if needed.
2110 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2111 policyFlags, lastButtonState, currentButtonState);
2112
Jeff Brown49754db2011-07-01 17:37:58 -07002113 mCursorMotionAccumulator.clearRelativeAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002114}
2115
Jeff Brown83c09682010-12-23 17:50:18 -08002116int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002117 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2118 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2119 } else {
2120 return AKEY_STATE_UNKNOWN;
2121 }
2122}
2123
Jeff Brown05dc66a2011-03-02 14:41:58 -08002124void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002125 if (mPointerController != NULL) {
2126 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2127 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002128}
2129
Jeff Brown6d0fec22010-07-23 21:28:06 -07002130
2131// --- TouchInputMapper ---
2132
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002133TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002134 InputMapper(device),
2135 mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) {
2136 initialize();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002137}
2138
2139TouchInputMapper::~TouchInputMapper() {
2140}
2141
2142uint32_t TouchInputMapper::getSources() {
Jeff Brownace13b12011-03-09 17:39:48 -08002143 return mTouchSource | mPointerSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002144}
2145
2146void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2147 InputMapper::populateDeviceInfo(info);
2148
Jeff Brownbe1aa822011-07-27 16:04:54 -07002149 // Ensure surface information is up to date so that orientation changes are
2150 // noticed immediately.
2151 if (!configureSurface()) {
2152 return;
2153 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002154
Jeff Brownbe1aa822011-07-27 16:04:54 -07002155 info->addMotionRange(mOrientedRanges.x);
2156 info->addMotionRange(mOrientedRanges.y);
2157
2158 if (mOrientedRanges.havePressure) {
2159 info->addMotionRange(mOrientedRanges.pressure);
2160 }
2161
2162 if (mOrientedRanges.haveSize) {
2163 info->addMotionRange(mOrientedRanges.size);
2164 }
2165
2166 if (mOrientedRanges.haveTouchSize) {
2167 info->addMotionRange(mOrientedRanges.touchMajor);
2168 info->addMotionRange(mOrientedRanges.touchMinor);
2169 }
2170
2171 if (mOrientedRanges.haveToolSize) {
2172 info->addMotionRange(mOrientedRanges.toolMajor);
2173 info->addMotionRange(mOrientedRanges.toolMinor);
2174 }
2175
2176 if (mOrientedRanges.haveOrientation) {
2177 info->addMotionRange(mOrientedRanges.orientation);
2178 }
2179
2180 if (mOrientedRanges.haveDistance) {
2181 info->addMotionRange(mOrientedRanges.distance);
2182 }
2183
2184 if (mPointerController != NULL) {
2185 float minX, minY, maxX, maxY;
2186 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
2187 info->addMotionRange(AMOTION_EVENT_AXIS_X, mPointerSource,
2188 minX, maxX, 0.0f, 0.0f);
2189 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mPointerSource,
2190 minY, maxY, 0.0f, 0.0f);
Jeff Brownefd32662011-03-08 15:13:06 -08002191 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002192 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mPointerSource,
2193 0.0f, 1.0f, 0.0f, 0.0f);
2194 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002195}
2196
Jeff Brownef3d7e82010-09-30 14:33:04 -07002197void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002198 dump.append(INDENT2 "Touch Input Mapper:\n");
2199 dumpParameters(dump);
2200 dumpVirtualKeys(dump);
2201 dumpRawPointerAxes(dump);
2202 dumpCalibration(dump);
2203 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002204
Jeff Brownbe1aa822011-07-27 16:04:54 -07002205 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
2206 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2207 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2208 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2209 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2210 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
2211 dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mToolSizeLinearScale);
2212 dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mToolSizeLinearBias);
2213 dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mToolSizeAreaScale);
2214 dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mToolSizeAreaBias);
2215 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2216 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
2217 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2218 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002219
Jeff Brownbe1aa822011-07-27 16:04:54 -07002220 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002221
Jeff Brownbe1aa822011-07-27 16:04:54 -07002222 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2223 mLastRawPointerData.pointerCount);
2224 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2225 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2226 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2227 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
2228 "orientation=%d, distance=%d, toolType=%d, isHovering=%s\n", i,
2229 pointer.id, pointer.x, pointer.y, pointer.pressure,
2230 pointer.touchMajor, pointer.touchMinor,
2231 pointer.toolMajor, pointer.toolMinor,
2232 pointer.orientation, pointer.distance,
2233 pointer.toolType, toString(pointer.isHovering));
2234 }
2235
2236 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2237 mLastCookedPointerData.pointerCount);
2238 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2239 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2240 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2241 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2242 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
2243 "orientation=%0.3f, distance=%0.3f, toolType=%d, isHovering=%s\n", i,
2244 pointerProperties.id,
2245 pointerCoords.getX(),
2246 pointerCoords.getY(),
2247 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2248 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2249 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2250 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2251 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2252 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
2253 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2254 pointerProperties.toolType,
2255 toString(mLastCookedPointerData.isHovering(i)));
2256 }
2257
2258 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2259 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2260 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
2261 mPointerGestureXMovementScale);
2262 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
2263 mPointerGestureYMovementScale);
2264 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
2265 mPointerGestureXZoomScale);
2266 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
2267 mPointerGestureYZoomScale);
2268 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2269 mPointerGestureMaxSwipeWidth);
2270 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002271}
2272
Jeff Brownbe1aa822011-07-27 16:04:54 -07002273void TouchInputMapper::initialize() {
2274 mCurrentRawPointerData.clear();
2275 mLastRawPointerData.clear();
2276 mCurrentCookedPointerData.clear();
2277 mLastCookedPointerData.clear();
2278 mCurrentButtonState = 0;
2279 mLastButtonState = 0;
2280 mSentHoverEnter = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002281 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002282
Jeff Brownbe1aa822011-07-27 16:04:54 -07002283 mCurrentVirtualKey.down = false;
Jeff Brown8d608662010-08-30 03:02:23 -07002284
Jeff Brownbe1aa822011-07-27 16:04:54 -07002285 mOrientedRanges.havePressure = false;
2286 mOrientedRanges.haveSize = false;
2287 mOrientedRanges.haveTouchSize = false;
2288 mOrientedRanges.haveToolSize = false;
2289 mOrientedRanges.haveOrientation = false;
2290 mOrientedRanges.haveDistance = false;
Jeff Brownace13b12011-03-09 17:39:48 -08002291
2292 mPointerGesture.reset();
Jeff Brown8d608662010-08-30 03:02:23 -07002293}
2294
Jeff Brown474dcb52011-06-14 20:22:50 -07002295void TouchInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
2296 InputMapper::configure(config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002297
Jeff Brown474dcb52011-06-14 20:22:50 -07002298 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002299
Jeff Brown474dcb52011-06-14 20:22:50 -07002300 if (!changes) { // first time only
2301 // Configure basic parameters.
2302 configureParameters();
2303
2304 // Configure sources.
2305 switch (mParameters.deviceType) {
2306 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2307 mTouchSource = AINPUT_SOURCE_TOUCHSCREEN;
2308 mPointerSource = 0;
2309 break;
2310 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2311 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
2312 mPointerSource = 0;
2313 break;
2314 case Parameters::DEVICE_TYPE_POINTER:
2315 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
2316 mPointerSource = AINPUT_SOURCE_MOUSE;
2317 break;
2318 default:
2319 LOG_ASSERT(false);
2320 }
2321
2322 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002323 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002324
2325 // Prepare input device calibration.
2326 parseCalibration();
2327 resolveCalibration();
2328
Jeff Brownbe1aa822011-07-27 16:04:54 -07002329 // Configure surface dimensions and orientation.
2330 configureSurface();
Jeff Brown83c09682010-12-23 17:50:18 -08002331 }
2332
Jeff Brown474dcb52011-06-14 20:22:50 -07002333 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2334 mPointerGesture.pointerVelocityControl.setParameters(
2335 mConfig.pointerVelocityControlParameters);
2336 }
Jeff Brown8d608662010-08-30 03:02:23 -07002337
Jeff Brown474dcb52011-06-14 20:22:50 -07002338 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT)) {
2339 // Reset the touch screen when pointer gesture enablement changes.
2340 reset();
2341 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002342}
2343
Jeff Brown8d608662010-08-30 03:02:23 -07002344void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002345 // Use the pointer presentation mode for devices that do not support distinct
2346 // multitouch. The spot-based presentation relies on being able to accurately
2347 // locate two or more fingers on the touch pad.
2348 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2349 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002350
Jeff Brown538881e2011-05-25 18:23:38 -07002351 String8 gestureModeString;
2352 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2353 gestureModeString)) {
2354 if (gestureModeString == "pointer") {
2355 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2356 } else if (gestureModeString == "spots") {
2357 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2358 } else if (gestureModeString != "default") {
2359 LOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
2360 }
2361 }
2362
Jeff Brownace13b12011-03-09 17:39:48 -08002363 if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
2364 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2365 // The device is a cursor device with a touch pad attached.
2366 // By default don't use the touch pad to move the pointer.
2367 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002368 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2369 // The device is a pointing device like a track pad.
2370 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2371 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2372 // The device is a touch screen.
2373 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownace13b12011-03-09 17:39:48 -08002374 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002375 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002376 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2377 }
2378
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002379 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002380 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2381 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002382 if (deviceTypeString == "touchScreen") {
2383 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002384 } else if (deviceTypeString == "touchPad") {
2385 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002386 } else if (deviceTypeString == "pointer") {
2387 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002388 } else if (deviceTypeString != "default") {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002389 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
2390 }
2391 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002392
Jeff Brownefd32662011-03-08 15:13:06 -08002393 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002394 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2395 mParameters.orientationAware);
2396
Jeff Brownbc68a592011-07-25 12:58:12 -07002397 mParameters.associatedDisplayId = -1;
2398 mParameters.associatedDisplayIsExternal = false;
2399 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002400 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002401 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2402 mParameters.associatedDisplayIsExternal =
2403 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2404 && getDevice()->isExternal();
2405 mParameters.associatedDisplayId = 0;
2406 }
Jeff Brown8d608662010-08-30 03:02:23 -07002407}
2408
Jeff Brownef3d7e82010-09-30 14:33:04 -07002409void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002410 dump.append(INDENT3 "Parameters:\n");
2411
Jeff Brown538881e2011-05-25 18:23:38 -07002412 switch (mParameters.gestureMode) {
2413 case Parameters::GESTURE_MODE_POINTER:
2414 dump.append(INDENT4 "GestureMode: pointer\n");
2415 break;
2416 case Parameters::GESTURE_MODE_SPOTS:
2417 dump.append(INDENT4 "GestureMode: spots\n");
2418 break;
2419 default:
2420 assert(false);
2421 }
2422
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002423 switch (mParameters.deviceType) {
2424 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2425 dump.append(INDENT4 "DeviceType: touchScreen\n");
2426 break;
2427 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2428 dump.append(INDENT4 "DeviceType: touchPad\n");
2429 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002430 case Parameters::DEVICE_TYPE_POINTER:
2431 dump.append(INDENT4 "DeviceType: pointer\n");
2432 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002433 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002434 LOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002435 }
2436
2437 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2438 mParameters.associatedDisplayId);
2439 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2440 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002441}
2442
Jeff Brownbe1aa822011-07-27 16:04:54 -07002443void TouchInputMapper::configureRawPointerAxes() {
2444 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002445}
2446
Jeff Brownbe1aa822011-07-27 16:04:54 -07002447void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2448 dump.append(INDENT3 "Raw Touch Axes:\n");
2449 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2450 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2451 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2452 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2453 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2454 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2455 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2456 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2457 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
2458 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2459 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002460}
2461
Jeff Brownbe1aa822011-07-27 16:04:54 -07002462bool TouchInputMapper::configureSurface() {
Jeff Brown9626b142011-03-03 02:09:54 -08002463 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002464 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Jeff Brown9626b142011-03-03 02:09:54 -08002465 LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
2466 "The device will be inoperable.", getDeviceName().string());
2467 return false;
2468 }
2469
Jeff Brown6d0fec22010-07-23 21:28:06 -07002470 // Update orientation and dimensions if needed.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002471 int32_t orientation = DISPLAY_ORIENTATION_0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002472 int32_t width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2473 int32_t height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002474
2475 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002476 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002477 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownbc68a592011-07-25 12:58:12 -07002478 mParameters.associatedDisplayIsExternal,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002479 &mAssociatedDisplayWidth, &mAssociatedDisplayHeight,
2480 &mAssociatedDisplayOrientation)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002481 return false;
2482 }
Jeff Brownefd32662011-03-08 15:13:06 -08002483
2484 // A touch screen inherits the dimensions of the display.
2485 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002486 width = mAssociatedDisplayWidth;
2487 height = mAssociatedDisplayHeight;
Jeff Brownefd32662011-03-08 15:13:06 -08002488 }
2489
2490 // The device inherits the orientation of the display if it is orientation aware.
2491 if (mParameters.orientationAware) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002492 orientation = mAssociatedDisplayOrientation;
Jeff Brownefd32662011-03-08 15:13:06 -08002493 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002494 }
2495
Jeff Brownace13b12011-03-09 17:39:48 -08002496 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2497 && mPointerController == NULL) {
2498 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2499 }
2500
Jeff Brownbe1aa822011-07-27 16:04:54 -07002501 bool orientationChanged = mSurfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002502 if (orientationChanged) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002503 mSurfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002504 }
2505
Jeff Brownbe1aa822011-07-27 16:04:54 -07002506 bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002507 if (sizeChanged) {
Jeff Brownefd32662011-03-08 15:13:06 -08002508 LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002509 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown8d608662010-08-30 03:02:23 -07002510
Jeff Brownbe1aa822011-07-27 16:04:54 -07002511 mSurfaceWidth = width;
2512 mSurfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002513
Jeff Brown8d608662010-08-30 03:02:23 -07002514 // Configure X and Y factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002515 mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1);
2516 mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1);
2517 mXPrecision = 1.0f / mXScale;
2518 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002519
Jeff Brownbe1aa822011-07-27 16:04:54 -07002520 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
2521 mOrientedRanges.x.source = mTouchSource;
2522 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
2523 mOrientedRanges.y.source = mTouchSource;
Jeff Brownefd32662011-03-08 15:13:06 -08002524
Jeff Brownbe1aa822011-07-27 16:04:54 -07002525 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002526
Jeff Brown8d608662010-08-30 03:02:23 -07002527 // Scale factor for terms that are not oriented in a particular axis.
2528 // If the pixels are square then xScale == yScale otherwise we fake it
2529 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002530 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002531
Jeff Brown8d608662010-08-30 03:02:23 -07002532 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002533 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002534
Jeff Brown8d608662010-08-30 03:02:23 -07002535 // TouchMajor and TouchMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002536 if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002537 mOrientedRanges.haveTouchSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002538
Jeff Brownbe1aa822011-07-27 16:04:54 -07002539 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
2540 mOrientedRanges.touchMajor.source = mTouchSource;
2541 mOrientedRanges.touchMajor.min = 0;
2542 mOrientedRanges.touchMajor.max = diagonalSize;
2543 mOrientedRanges.touchMajor.flat = 0;
2544 mOrientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002545
Jeff Brownbe1aa822011-07-27 16:04:54 -07002546 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
2547 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002548 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002549
Jeff Brown8d608662010-08-30 03:02:23 -07002550 // ToolMajor and ToolMinor factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002551 mToolSizeLinearScale = 0;
2552 mToolSizeLinearBias = 0;
2553 mToolSizeAreaScale = 0;
2554 mToolSizeAreaBias = 0;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002555 if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
2556 if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) {
2557 if (mCalibration.haveToolSizeLinearScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002558 mToolSizeLinearScale = mCalibration.toolSizeLinearScale;
2559 } else if (mRawPointerAxes.toolMajor.valid
2560 && mRawPointerAxes.toolMajor.maxValue != 0) {
2561 mToolSizeLinearScale = float(min(width, height))
2562 / mRawPointerAxes.toolMajor.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07002563 }
2564
Jeff Brownc6d282b2010-10-14 21:42:15 -07002565 if (mCalibration.haveToolSizeLinearBias) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002566 mToolSizeLinearBias = mCalibration.toolSizeLinearBias;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002567 }
2568 } else if (mCalibration.toolSizeCalibration ==
2569 Calibration::TOOL_SIZE_CALIBRATION_AREA) {
2570 if (mCalibration.haveToolSizeLinearScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002571 mToolSizeLinearScale = mCalibration.toolSizeLinearScale;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002572 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002573 mToolSizeLinearScale = min(width, height);
Jeff Brownc6d282b2010-10-14 21:42:15 -07002574 }
2575
2576 if (mCalibration.haveToolSizeLinearBias) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002577 mToolSizeLinearBias = mCalibration.toolSizeLinearBias;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002578 }
2579
2580 if (mCalibration.haveToolSizeAreaScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002581 mToolSizeAreaScale = mCalibration.toolSizeAreaScale;
2582 } else if (mRawPointerAxes.toolMajor.valid
2583 && mRawPointerAxes.toolMajor.maxValue != 0) {
2584 mToolSizeAreaScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002585 }
2586
2587 if (mCalibration.haveToolSizeAreaBias) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002588 mToolSizeAreaBias = mCalibration.toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -07002589 }
2590 }
2591
Jeff Brownbe1aa822011-07-27 16:04:54 -07002592 mOrientedRanges.haveToolSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002593
Jeff Brownbe1aa822011-07-27 16:04:54 -07002594 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
2595 mOrientedRanges.toolMajor.source = mTouchSource;
2596 mOrientedRanges.toolMajor.min = 0;
2597 mOrientedRanges.toolMajor.max = diagonalSize;
2598 mOrientedRanges.toolMajor.flat = 0;
2599 mOrientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002600
Jeff Brownbe1aa822011-07-27 16:04:54 -07002601 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
2602 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002603 }
2604
2605 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002606 mPressureScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002607 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
2608 RawAbsoluteAxisInfo rawPressureAxis;
2609 switch (mCalibration.pressureSource) {
2610 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002611 rawPressureAxis = mRawPointerAxes.pressure;
Jeff Brown8d608662010-08-30 03:02:23 -07002612 break;
2613 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002614 rawPressureAxis = mRawPointerAxes.touchMajor;
Jeff Brown8d608662010-08-30 03:02:23 -07002615 break;
2616 default:
2617 rawPressureAxis.clear();
2618 }
2619
Jeff Brown8d608662010-08-30 03:02:23 -07002620 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2621 || mCalibration.pressureCalibration
2622 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2623 if (mCalibration.havePressureScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002624 mPressureScale = mCalibration.pressureScale;
Jeff Brown8d608662010-08-30 03:02:23 -07002625 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002626 mPressureScale = 1.0f / rawPressureAxis.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07002627 }
2628 }
2629
Jeff Brownbe1aa822011-07-27 16:04:54 -07002630 mOrientedRanges.havePressure = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002631
Jeff Brownbe1aa822011-07-27 16:04:54 -07002632 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2633 mOrientedRanges.pressure.source = mTouchSource;
2634 mOrientedRanges.pressure.min = 0;
2635 mOrientedRanges.pressure.max = 1.0;
2636 mOrientedRanges.pressure.flat = 0;
2637 mOrientedRanges.pressure.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002638 }
2639
2640 // Size factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002641 mSizeScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002642 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002643 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002644 if (mRawPointerAxes.toolMajor.valid && mRawPointerAxes.toolMajor.maxValue != 0) {
2645 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07002646 }
2647 }
2648
Jeff Brownbe1aa822011-07-27 16:04:54 -07002649 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002650
Jeff Brownbe1aa822011-07-27 16:04:54 -07002651 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
2652 mOrientedRanges.size.source = mTouchSource;
2653 mOrientedRanges.size.min = 0;
2654 mOrientedRanges.size.max = 1.0;
2655 mOrientedRanges.size.flat = 0;
2656 mOrientedRanges.size.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002657 }
2658
2659 // Orientation
Jeff Brownbe1aa822011-07-27 16:04:54 -07002660 mOrientationScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002661 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002662 if (mCalibration.orientationCalibration
2663 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002664 if (mRawPointerAxes.orientation.valid && mRawPointerAxes.orientation.maxValue != 0) {
2665 mOrientationScale = float(M_PI_2) / mRawPointerAxes.orientation.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07002666 }
2667 }
2668
Jeff Brownbe1aa822011-07-27 16:04:54 -07002669 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002670
Jeff Brownbe1aa822011-07-27 16:04:54 -07002671 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2672 mOrientedRanges.orientation.source = mTouchSource;
2673 mOrientedRanges.orientation.min = - M_PI_2;
2674 mOrientedRanges.orientation.max = M_PI_2;
2675 mOrientedRanges.orientation.flat = 0;
2676 mOrientedRanges.orientation.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002677 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002678
2679 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07002680 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002681 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
2682 if (mCalibration.distanceCalibration
2683 == Calibration::DISTANCE_CALIBRATION_SCALED) {
2684 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002685 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002686 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002687 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002688 }
2689 }
2690
Jeff Brownbe1aa822011-07-27 16:04:54 -07002691 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002692
Jeff Brownbe1aa822011-07-27 16:04:54 -07002693 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
2694 mOrientedRanges.distance.source = mTouchSource;
2695 mOrientedRanges.distance.min =
2696 mRawPointerAxes.distance.minValue * mDistanceScale;
2697 mOrientedRanges.distance.max =
2698 mRawPointerAxes.distance.minValue * mDistanceScale;
2699 mOrientedRanges.distance.flat = 0;
2700 mOrientedRanges.distance.fuzz =
2701 mRawPointerAxes.distance.fuzz * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002702 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002703 }
2704
2705 if (orientationChanged || sizeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002706 // Compute oriented surface dimensions, precision, scales and ranges.
2707 // Note that the maximum value reported is an inclusive maximum value so it is one
2708 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002709 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002710 case DISPLAY_ORIENTATION_90:
2711 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002712 mOrientedSurfaceWidth = mSurfaceHeight;
2713 mOrientedSurfaceHeight = mSurfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002714
Jeff Brownbe1aa822011-07-27 16:04:54 -07002715 mOrientedXPrecision = mYPrecision;
2716 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002717
Jeff Brownbe1aa822011-07-27 16:04:54 -07002718 mOrientedRanges.x.min = 0;
2719 mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2720 * mYScale;
2721 mOrientedRanges.x.flat = 0;
2722 mOrientedRanges.x.fuzz = mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002723
Jeff Brownbe1aa822011-07-27 16:04:54 -07002724 mOrientedRanges.y.min = 0;
2725 mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2726 * mXScale;
2727 mOrientedRanges.y.flat = 0;
2728 mOrientedRanges.y.fuzz = mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002729 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002730
Jeff Brown6d0fec22010-07-23 21:28:06 -07002731 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002732 mOrientedSurfaceWidth = mSurfaceWidth;
2733 mOrientedSurfaceHeight = mSurfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002734
Jeff Brownbe1aa822011-07-27 16:04:54 -07002735 mOrientedXPrecision = mXPrecision;
2736 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002737
Jeff Brownbe1aa822011-07-27 16:04:54 -07002738 mOrientedRanges.x.min = 0;
2739 mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2740 * mXScale;
2741 mOrientedRanges.x.flat = 0;
2742 mOrientedRanges.x.fuzz = mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002743
Jeff Brownbe1aa822011-07-27 16:04:54 -07002744 mOrientedRanges.y.min = 0;
2745 mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2746 * mYScale;
2747 mOrientedRanges.y.flat = 0;
2748 mOrientedRanges.y.fuzz = mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002749 break;
2750 }
Jeff Brownace13b12011-03-09 17:39:48 -08002751
2752 // Compute pointer gesture detection parameters.
Jeff Brownace13b12011-03-09 17:39:48 -08002753 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002754 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2755 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07002756 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002757 float displayDiagonal = hypotf(mAssociatedDisplayWidth,
2758 mAssociatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08002759
Jeff Brown2352b972011-04-12 22:39:53 -07002760 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07002761 // given area relative to the diagonal size of the display when no acceleration
2762 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08002763 // Assume that the touch pad has a square aspect ratio such that movements in
2764 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002765 mPointerGestureXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002766 * displayDiagonal / rawDiagonal;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002767 mPointerGestureYMovementScale = mPointerGestureXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002768
2769 // Scale zooms to cover a smaller range of the display than movements do.
2770 // This value determines the area around the pointer that is affected by freeform
2771 // pointer gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002772 mPointerGestureXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002773 * displayDiagonal / rawDiagonal;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002774 mPointerGestureYZoomScale = mPointerGestureXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002775
Jeff Brown2352b972011-04-12 22:39:53 -07002776 // Max width between pointers to detect a swipe gesture is more than some fraction
2777 // of the diagonal axis of the touch pad. Touches that are wider than this are
2778 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002779 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07002780 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brown2352b972011-04-12 22:39:53 -07002781
2782 // Reset the current pointer gesture.
2783 mPointerGesture.reset();
2784
2785 // Remove any current spots.
2786 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
2787 mPointerController->clearSpots();
2788 }
Jeff Brownace13b12011-03-09 17:39:48 -08002789 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002790 }
2791
2792 return true;
2793}
2794
Jeff Brownbe1aa822011-07-27 16:04:54 -07002795void TouchInputMapper::dumpSurface(String8& dump) {
2796 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
2797 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
2798 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07002799}
2800
Jeff Brownbe1aa822011-07-27 16:04:54 -07002801void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07002802 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08002803 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002804
Jeff Brownbe1aa822011-07-27 16:04:54 -07002805 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002806
Jeff Brown6328cdc2010-07-29 18:18:33 -07002807 if (virtualKeyDefinitions.size() == 0) {
2808 return;
2809 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002810
Jeff Brownbe1aa822011-07-27 16:04:54 -07002811 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002812
Jeff Brownbe1aa822011-07-27 16:04:54 -07002813 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
2814 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
2815 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2816 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002817
2818 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07002819 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07002820 virtualKeyDefinitions[i];
2821
Jeff Brownbe1aa822011-07-27 16:04:54 -07002822 mVirtualKeys.add();
2823 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002824
2825 virtualKey.scanCode = virtualKeyDefinition.scanCode;
2826 int32_t keyCode;
2827 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08002828 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07002829 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002830 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
2831 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002832 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07002833 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002834 }
2835
Jeff Brown6328cdc2010-07-29 18:18:33 -07002836 virtualKey.keyCode = keyCode;
2837 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002838
Jeff Brown6328cdc2010-07-29 18:18:33 -07002839 // convert the key definition's display coordinates into touch coordinates for a hit box
2840 int32_t halfWidth = virtualKeyDefinition.width / 2;
2841 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002842
Jeff Brown6328cdc2010-07-29 18:18:33 -07002843 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07002844 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002845 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07002846 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002847 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07002848 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002849 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07002850 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07002851 }
2852}
2853
Jeff Brownbe1aa822011-07-27 16:04:54 -07002854void TouchInputMapper::dumpVirtualKeys(String8& dump) {
2855 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07002856 dump.append(INDENT3 "Virtual Keys:\n");
2857
Jeff Brownbe1aa822011-07-27 16:04:54 -07002858 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
2859 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002860 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
2861 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
2862 i, virtualKey.scanCode, virtualKey.keyCode,
2863 virtualKey.hitLeft, virtualKey.hitRight,
2864 virtualKey.hitTop, virtualKey.hitBottom);
2865 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002866 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002867}
2868
Jeff Brown8d608662010-08-30 03:02:23 -07002869void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002870 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07002871 Calibration& out = mCalibration;
2872
Jeff Brownc6d282b2010-10-14 21:42:15 -07002873 // Touch Size
2874 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT;
2875 String8 touchSizeCalibrationString;
2876 if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) {
2877 if (touchSizeCalibrationString == "none") {
2878 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
2879 } else if (touchSizeCalibrationString == "geometric") {
2880 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC;
2881 } else if (touchSizeCalibrationString == "pressure") {
2882 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
2883 } else if (touchSizeCalibrationString != "default") {
2884 LOGW("Invalid value for touch.touchSize.calibration: '%s'",
2885 touchSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002886 }
2887 }
2888
Jeff Brownc6d282b2010-10-14 21:42:15 -07002889 // Tool Size
2890 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT;
2891 String8 toolSizeCalibrationString;
2892 if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) {
2893 if (toolSizeCalibrationString == "none") {
2894 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
2895 } else if (toolSizeCalibrationString == "geometric") {
2896 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC;
2897 } else if (toolSizeCalibrationString == "linear") {
2898 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
2899 } else if (toolSizeCalibrationString == "area") {
2900 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA;
2901 } else if (toolSizeCalibrationString != "default") {
2902 LOGW("Invalid value for touch.toolSize.calibration: '%s'",
2903 toolSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002904 }
2905 }
2906
Jeff Brownc6d282b2010-10-14 21:42:15 -07002907 out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"),
2908 out.toolSizeLinearScale);
2909 out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"),
2910 out.toolSizeLinearBias);
2911 out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"),
2912 out.toolSizeAreaScale);
2913 out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"),
2914 out.toolSizeAreaBias);
2915 out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"),
2916 out.toolSizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07002917
2918 // Pressure
2919 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
2920 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002921 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002922 if (pressureCalibrationString == "none") {
2923 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2924 } else if (pressureCalibrationString == "physical") {
2925 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
2926 } else if (pressureCalibrationString == "amplitude") {
2927 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2928 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002929 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002930 pressureCalibrationString.string());
2931 }
2932 }
2933
2934 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
2935 String8 pressureSourceString;
2936 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
2937 if (pressureSourceString == "pressure") {
2938 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2939 } else if (pressureSourceString == "touch") {
2940 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2941 } else if (pressureSourceString != "default") {
2942 LOGW("Invalid value for touch.pressure.source: '%s'",
2943 pressureSourceString.string());
2944 }
2945 }
2946
2947 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
2948 out.pressureScale);
2949
2950 // Size
2951 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
2952 String8 sizeCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002953 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002954 if (sizeCalibrationString == "none") {
2955 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2956 } else if (sizeCalibrationString == "normalized") {
2957 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
2958 } else if (sizeCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002959 LOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002960 sizeCalibrationString.string());
2961 }
2962 }
2963
2964 // Orientation
2965 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
2966 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002967 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002968 if (orientationCalibrationString == "none") {
2969 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2970 } else if (orientationCalibrationString == "interpolated") {
2971 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002972 } else if (orientationCalibrationString == "vector") {
2973 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002974 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002975 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002976 orientationCalibrationString.string());
2977 }
2978 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002979
2980 // Distance
2981 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
2982 String8 distanceCalibrationString;
2983 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
2984 if (distanceCalibrationString == "none") {
2985 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
2986 } else if (distanceCalibrationString == "scaled") {
2987 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
2988 } else if (distanceCalibrationString != "default") {
2989 LOGW("Invalid value for touch.distance.calibration: '%s'",
2990 distanceCalibrationString.string());
2991 }
2992 }
2993
2994 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
2995 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002996}
2997
2998void TouchInputMapper::resolveCalibration() {
2999 // Pressure
3000 switch (mCalibration.pressureSource) {
3001 case Calibration::PRESSURE_SOURCE_DEFAULT:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003002 if (mRawPointerAxes.pressure.valid) {
Jeff Brown8d608662010-08-30 03:02:23 -07003003 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003004 } else if (mRawPointerAxes.touchMajor.valid) {
Jeff Brown8d608662010-08-30 03:02:23 -07003005 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
3006 }
3007 break;
3008
3009 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003010 if (! mRawPointerAxes.pressure.valid) {
Jeff Brown8d608662010-08-30 03:02:23 -07003011 LOGW("Calibration property touch.pressure.source is 'pressure' but "
3012 "the pressure axis is not available.");
3013 }
3014 break;
3015
3016 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003017 if (! mRawPointerAxes.touchMajor.valid) {
Jeff Brown8d608662010-08-30 03:02:23 -07003018 LOGW("Calibration property touch.pressure.source is 'touch' but "
3019 "the touchMajor axis is not available.");
3020 }
3021 break;
3022
3023 default:
3024 break;
3025 }
3026
3027 switch (mCalibration.pressureCalibration) {
3028 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
3029 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
3030 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3031 } else {
3032 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3033 }
3034 break;
3035
3036 default:
3037 break;
3038 }
3039
Jeff Brownc6d282b2010-10-14 21:42:15 -07003040 // Tool Size
3041 switch (mCalibration.toolSizeCalibration) {
3042 case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003043 if (mRawPointerAxes.toolMajor.valid) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07003044 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
Jeff Brown8d608662010-08-30 03:02:23 -07003045 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07003046 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003047 }
3048 break;
3049
3050 default:
3051 break;
3052 }
3053
Jeff Brownc6d282b2010-10-14 21:42:15 -07003054 // Touch Size
3055 switch (mCalibration.touchSizeCalibration) {
3056 case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07003057 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
Jeff Brownc6d282b2010-10-14 21:42:15 -07003058 && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
3059 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
Jeff Brown8d608662010-08-30 03:02:23 -07003060 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07003061 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003062 }
3063 break;
3064
3065 default:
3066 break;
3067 }
3068
3069 // Size
3070 switch (mCalibration.sizeCalibration) {
3071 case Calibration::SIZE_CALIBRATION_DEFAULT:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003072 if (mRawPointerAxes.toolMajor.valid) {
Jeff Brown8d608662010-08-30 03:02:23 -07003073 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
3074 } else {
3075 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3076 }
3077 break;
3078
3079 default:
3080 break;
3081 }
3082
3083 // Orientation
3084 switch (mCalibration.orientationCalibration) {
3085 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003086 if (mRawPointerAxes.orientation.valid) {
Jeff Brown8d608662010-08-30 03:02:23 -07003087 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
3088 } else {
3089 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3090 }
3091 break;
3092
3093 default:
3094 break;
3095 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003096
3097 // Distance
3098 switch (mCalibration.distanceCalibration) {
3099 case Calibration::DISTANCE_CALIBRATION_DEFAULT:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003100 if (mRawPointerAxes.distance.valid) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003101 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3102 } else {
3103 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3104 }
3105 break;
3106
3107 default:
3108 break;
3109 }
Jeff Brown8d608662010-08-30 03:02:23 -07003110}
3111
Jeff Brownef3d7e82010-09-30 14:33:04 -07003112void TouchInputMapper::dumpCalibration(String8& dump) {
3113 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003114
Jeff Brownc6d282b2010-10-14 21:42:15 -07003115 // Touch Size
3116 switch (mCalibration.touchSizeCalibration) {
3117 case Calibration::TOUCH_SIZE_CALIBRATION_NONE:
3118 dump.append(INDENT4 "touch.touchSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003119 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003120 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
3121 dump.append(INDENT4 "touch.touchSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003122 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003123 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
3124 dump.append(INDENT4 "touch.touchSize.calibration: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003125 break;
3126 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003127 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003128 }
3129
Jeff Brownc6d282b2010-10-14 21:42:15 -07003130 // Tool Size
3131 switch (mCalibration.toolSizeCalibration) {
3132 case Calibration::TOOL_SIZE_CALIBRATION_NONE:
3133 dump.append(INDENT4 "touch.toolSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003134 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003135 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
3136 dump.append(INDENT4 "touch.toolSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003137 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003138 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
3139 dump.append(INDENT4 "touch.toolSize.calibration: linear\n");
3140 break;
3141 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
3142 dump.append(INDENT4 "touch.toolSize.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003143 break;
3144 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003145 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003146 }
3147
Jeff Brownc6d282b2010-10-14 21:42:15 -07003148 if (mCalibration.haveToolSizeLinearScale) {
3149 dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n",
3150 mCalibration.toolSizeLinearScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003151 }
3152
Jeff Brownc6d282b2010-10-14 21:42:15 -07003153 if (mCalibration.haveToolSizeLinearBias) {
3154 dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n",
3155 mCalibration.toolSizeLinearBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003156 }
3157
Jeff Brownc6d282b2010-10-14 21:42:15 -07003158 if (mCalibration.haveToolSizeAreaScale) {
3159 dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n",
3160 mCalibration.toolSizeAreaScale);
3161 }
3162
3163 if (mCalibration.haveToolSizeAreaBias) {
3164 dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n",
3165 mCalibration.toolSizeAreaBias);
3166 }
3167
3168 if (mCalibration.haveToolSizeIsSummed) {
Jeff Brown1f245102010-11-18 20:53:46 -08003169 dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %s\n",
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003170 toString(mCalibration.toolSizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003171 }
3172
3173 // Pressure
3174 switch (mCalibration.pressureCalibration) {
3175 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003176 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003177 break;
3178 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003179 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003180 break;
3181 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003182 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003183 break;
3184 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003185 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003186 }
3187
3188 switch (mCalibration.pressureSource) {
3189 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003190 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003191 break;
3192 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003193 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003194 break;
3195 case Calibration::PRESSURE_SOURCE_DEFAULT:
3196 break;
3197 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003198 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003199 }
3200
3201 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003202 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3203 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003204 }
3205
3206 // Size
3207 switch (mCalibration.sizeCalibration) {
3208 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003209 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003210 break;
3211 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003212 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003213 break;
3214 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003215 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003216 }
3217
3218 // Orientation
3219 switch (mCalibration.orientationCalibration) {
3220 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003221 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003222 break;
3223 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003224 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003225 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003226 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3227 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3228 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003229 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003230 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003231 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003232
3233 // Distance
3234 switch (mCalibration.distanceCalibration) {
3235 case Calibration::DISTANCE_CALIBRATION_NONE:
3236 dump.append(INDENT4 "touch.distance.calibration: none\n");
3237 break;
3238 case Calibration::DISTANCE_CALIBRATION_SCALED:
3239 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3240 break;
3241 default:
3242 LOG_ASSERT(false);
3243 }
3244
3245 if (mCalibration.haveDistanceScale) {
3246 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3247 mCalibration.distanceScale);
3248 }
Jeff Brown8d608662010-08-30 03:02:23 -07003249}
3250
Jeff Brown6d0fec22010-07-23 21:28:06 -07003251void TouchInputMapper::reset() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003252 // Synthesize touch up event.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003253 // This will also take care of finishing virtual key processing if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003254 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
3255 mCurrentRawPointerData.clear();
3256 mCurrentButtonState = 0;
3257 syncTouch(when, true);
3258
3259 initialize();
3260
3261 if (mPointerController != NULL
3262 && mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3263 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3264 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003265 }
3266
Jeff Brown6328cdc2010-07-29 18:18:33 -07003267 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003268}
3269
3270void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Brownaa3855d2011-03-17 01:34:19 -07003271#if DEBUG_RAW_EVENTS
3272 if (!havePointerIds) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003273 LOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
3274 mLastRawPointerData.pointerCount,
3275 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003276 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003277 LOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
3278 "hovering ids 0x%08x -> 0x%08x",
3279 mLastRawPointerData.pointerCount,
3280 mCurrentRawPointerData.pointerCount,
3281 mLastRawPointerData.touchingIdBits.value,
3282 mCurrentRawPointerData.touchingIdBits.value,
3283 mLastRawPointerData.hoveringIdBits.value,
3284 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003285 }
3286#endif
3287
Jeff Brownbe1aa822011-07-27 16:04:54 -07003288 // Configure the surface now, if possible.
3289 if (!configureSurface()) {
3290 mLastRawPointerData.clear();
3291 mLastCookedPointerData.clear();
3292 mLastButtonState = 0;
3293 return;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003294 }
3295
Jeff Brownbe1aa822011-07-27 16:04:54 -07003296 // Preprocess pointer data.
3297 if (!havePointerIds) {
3298 assignPointerIds();
3299 }
3300
3301 // Handle policy on initial down or hover events.
Jeff Brown56194eb2011-03-02 19:23:13 -08003302 uint32_t policyFlags = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003303 if (mLastRawPointerData.pointerCount == 0 && mCurrentRawPointerData.pointerCount != 0) {
Jeff Brownefd32662011-03-08 15:13:06 -08003304 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
3305 // If this is a touch screen, hide the pointer on an initial down.
3306 getContext()->fadePointer();
3307 }
Jeff Brown56194eb2011-03-02 19:23:13 -08003308
3309 // Initial downs on external touch devices should wake the device.
3310 // We don't do this for internal touch screens to prevent them from waking
3311 // up in your pocket.
3312 // TODO: Use the input device configuration to control this behavior more finely.
3313 if (getDevice()->isExternal()) {
3314 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3315 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08003316 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003317
Jeff Brownbe1aa822011-07-27 16:04:54 -07003318 // Synthesize key down from raw buttons if needed.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003319 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mTouchSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003320 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003321
Jeff Brownbe1aa822011-07-27 16:04:54 -07003322 if (consumeRawTouches(when, policyFlags)) {
3323 mCurrentRawPointerData.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003324 }
3325
Jeff Brownbe1aa822011-07-27 16:04:54 -07003326 if (mPointerController != NULL && mConfig.pointerGesturesEnabled) {
3327 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
3328 }
3329
3330 cookPointerData();
3331 dispatchHoverExit(when, policyFlags);
3332 dispatchTouches(when, policyFlags);
3333 dispatchHoverEnterAndMove(when, policyFlags);
3334
3335 // Synthesize key up from raw buttons if needed.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003336 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mTouchSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003337 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003338
Jeff Brown6328cdc2010-07-29 18:18:33 -07003339 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003340 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3341 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3342 mLastButtonState = mCurrentButtonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003343}
3344
Jeff Brown79ac9692011-04-19 21:20:10 -07003345void TouchInputMapper::timeoutExpired(nsecs_t when) {
3346 if (mPointerController != NULL) {
3347 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3348 }
3349}
3350
Jeff Brownbe1aa822011-07-27 16:04:54 -07003351bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3352 // Check for release of a virtual key.
3353 if (mCurrentVirtualKey.down) {
3354 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3355 // Pointer went up while virtual key was down.
3356 mCurrentVirtualKey.down = false;
3357 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003358#if DEBUG_VIRTUAL_KEYS
3359 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003360 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003361#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003362 dispatchVirtualKey(when, policyFlags,
3363 AKEY_EVENT_ACTION_UP,
3364 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003365 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003366 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003367 }
3368
Jeff Brownbe1aa822011-07-27 16:04:54 -07003369 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3370 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3371 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3372 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3373 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3374 // Pointer is still within the space of the virtual key.
3375 return true;
3376 }
3377 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003378
Jeff Brownbe1aa822011-07-27 16:04:54 -07003379 // Pointer left virtual key area or another pointer also went down.
3380 // Send key cancellation but do not consume the touch yet.
3381 // This is useful when the user swipes through from the virtual key area
3382 // into the main display surface.
3383 mCurrentVirtualKey.down = false;
3384 if (!mCurrentVirtualKey.ignored) {
3385#if DEBUG_VIRTUAL_KEYS
3386 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
3387 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3388#endif
3389 dispatchVirtualKey(when, policyFlags,
3390 AKEY_EVENT_ACTION_UP,
3391 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3392 | AKEY_EVENT_FLAG_CANCELED);
3393 }
3394 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003395
Jeff Brownbe1aa822011-07-27 16:04:54 -07003396 if (mLastRawPointerData.touchingIdBits.isEmpty()
3397 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3398 // Pointer just went down. Check for virtual key press or off-screen touches.
3399 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3400 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3401 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3402 // If exactly one pointer went down, check for virtual key hit.
3403 // Otherwise we will drop the entire stroke.
3404 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3405 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3406 if (virtualKey) {
3407 mCurrentVirtualKey.down = true;
3408 mCurrentVirtualKey.downTime = when;
3409 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3410 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3411 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3412 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3413
3414 if (!mCurrentVirtualKey.ignored) {
3415#if DEBUG_VIRTUAL_KEYS
3416 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
3417 mCurrentVirtualKey.keyCode,
3418 mCurrentVirtualKey.scanCode);
3419#endif
3420 dispatchVirtualKey(when, policyFlags,
3421 AKEY_EVENT_ACTION_DOWN,
3422 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3423 }
3424 }
3425 }
3426 return true;
3427 }
3428 }
3429
Jeff Brownfe508922011-01-18 15:10:10 -08003430 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003431 // most recent touch within the screen area. The idea is to filter out stray
3432 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003433 //
3434 // Problems we're trying to solve:
3435 //
3436 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3437 // virtual key area that is implemented by a separate touch panel and accidentally
3438 // triggers a virtual key.
3439 //
3440 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3441 // area and accidentally triggers a virtual key. This often happens when virtual keys
3442 // are layed out below the screen near to where the on screen keyboard's space bar
3443 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003444 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003445 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003446 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003447 return false;
3448}
3449
3450void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3451 int32_t keyEventAction, int32_t keyEventFlags) {
3452 int32_t keyCode = mCurrentVirtualKey.keyCode;
3453 int32_t scanCode = mCurrentVirtualKey.scanCode;
3454 nsecs_t downTime = mCurrentVirtualKey.downTime;
3455 int32_t metaState = mContext->getGlobalMetaState();
3456 policyFlags |= POLICY_FLAG_VIRTUAL;
3457
3458 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3459 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3460 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003461}
3462
Jeff Brown6d0fec22010-07-23 21:28:06 -07003463void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003464 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3465 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003466 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003467 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003468
3469 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003470 if (!currentIdBits.isEmpty()) {
3471 // No pointer id changes so this is a move event.
3472 // The listener takes care of batching moves so we don't have to deal with that here.
3473 dispatchMotion(when, policyFlags, mTouchSource,
3474 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3475 AMOTION_EVENT_EDGE_FLAG_NONE,
3476 mCurrentCookedPointerData.pointerProperties,
3477 mCurrentCookedPointerData.pointerCoords,
3478 mCurrentCookedPointerData.idToIndex,
3479 currentIdBits, -1,
3480 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3481 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003482 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003483 // There may be pointers going up and pointers going down and pointers moving
3484 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003485 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3486 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003487 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003488 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003489
Jeff Brownace13b12011-03-09 17:39:48 -08003490 // Update last coordinates of pointers that have moved so that we observe the new
3491 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003492 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003493 mCurrentCookedPointerData.pointerProperties,
3494 mCurrentCookedPointerData.pointerCoords,
3495 mCurrentCookedPointerData.idToIndex,
3496 mLastCookedPointerData.pointerProperties,
3497 mLastCookedPointerData.pointerCoords,
3498 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003499 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003500 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003501 moveNeeded = true;
3502 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003503
Jeff Brownace13b12011-03-09 17:39:48 -08003504 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003505 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003506 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003507
Jeff Brownace13b12011-03-09 17:39:48 -08003508 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003509 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003510 mLastCookedPointerData.pointerProperties,
3511 mLastCookedPointerData.pointerCoords,
3512 mLastCookedPointerData.idToIndex,
3513 dispatchedIdBits, upId,
3514 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003515 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003516 }
3517
Jeff Brownc3db8582010-10-20 15:33:38 -07003518 // Dispatch move events if any of the remaining pointers moved from their old locations.
3519 // Although applications receive new locations as part of individual pointer up
3520 // events, they do not generally handle them except when presented in a move event.
3521 if (moveNeeded) {
Jeff Brownb6110c22011-04-01 16:15:13 -07003522 LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003523 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003524 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003525 mCurrentCookedPointerData.pointerProperties,
3526 mCurrentCookedPointerData.pointerCoords,
3527 mCurrentCookedPointerData.idToIndex,
3528 dispatchedIdBits, -1,
3529 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003530 }
3531
3532 // Dispatch pointer down events using the new pointer locations.
3533 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003534 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003535 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003536
Jeff Brownace13b12011-03-09 17:39:48 -08003537 if (dispatchedIdBits.count() == 1) {
3538 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003539 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003540 }
3541
Jeff Brownace13b12011-03-09 17:39:48 -08003542 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Browna6111372011-07-14 21:48:23 -07003543 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003544 mCurrentCookedPointerData.pointerProperties,
3545 mCurrentCookedPointerData.pointerCoords,
3546 mCurrentCookedPointerData.idToIndex,
3547 dispatchedIdBits, downId,
3548 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003549 }
3550 }
Jeff Brownace13b12011-03-09 17:39:48 -08003551}
3552
Jeff Brownbe1aa822011-07-27 16:04:54 -07003553void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
3554 if (mSentHoverEnter &&
3555 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
3556 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
3557 int32_t metaState = getContext()->getGlobalMetaState();
3558 dispatchMotion(when, policyFlags, mTouchSource,
3559 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
3560 mLastCookedPointerData.pointerProperties,
3561 mLastCookedPointerData.pointerCoords,
3562 mLastCookedPointerData.idToIndex,
3563 mLastCookedPointerData.hoveringIdBits, -1,
3564 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3565 mSentHoverEnter = false;
3566 }
3567}
Jeff Brownace13b12011-03-09 17:39:48 -08003568
Jeff Brownbe1aa822011-07-27 16:04:54 -07003569void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
3570 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
3571 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
3572 int32_t metaState = getContext()->getGlobalMetaState();
3573 if (!mSentHoverEnter) {
3574 dispatchMotion(when, policyFlags, mTouchSource,
3575 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
3576 mCurrentCookedPointerData.pointerProperties,
3577 mCurrentCookedPointerData.pointerCoords,
3578 mCurrentCookedPointerData.idToIndex,
3579 mCurrentCookedPointerData.hoveringIdBits, -1,
3580 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3581 mSentHoverEnter = true;
3582 }
Jeff Brownace13b12011-03-09 17:39:48 -08003583
Jeff Brownbe1aa822011-07-27 16:04:54 -07003584 dispatchMotion(when, policyFlags, mTouchSource,
3585 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
3586 mCurrentCookedPointerData.pointerProperties,
3587 mCurrentCookedPointerData.pointerCoords,
3588 mCurrentCookedPointerData.idToIndex,
3589 mCurrentCookedPointerData.hoveringIdBits, -1,
3590 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3591 }
3592}
3593
3594void TouchInputMapper::cookPointerData() {
3595 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
3596
3597 mCurrentCookedPointerData.clear();
3598 mCurrentCookedPointerData.pointerCount = currentPointerCount;
3599 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
3600 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
3601
3602 // Walk through the the active pointers and map device coordinates onto
3603 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08003604 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003605 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003606
3607 // ToolMajor and ToolMinor
3608 float toolMajor, toolMinor;
3609 switch (mCalibration.toolSizeCalibration) {
3610 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003611 toolMajor = in.toolMajor * mGeometricScale;
3612 if (mRawPointerAxes.toolMinor.valid) {
3613 toolMinor = in.toolMinor * mGeometricScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003614 } else {
3615 toolMinor = toolMajor;
3616 }
3617 break;
3618 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
3619 toolMajor = in.toolMajor != 0
Jeff Brownbe1aa822011-07-27 16:04:54 -07003620 ? in.toolMajor * mToolSizeLinearScale + mToolSizeLinearBias
Jeff Brownace13b12011-03-09 17:39:48 -08003621 : 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003622 if (mRawPointerAxes.toolMinor.valid) {
Jeff Brownace13b12011-03-09 17:39:48 -08003623 toolMinor = in.toolMinor != 0
Jeff Brownbe1aa822011-07-27 16:04:54 -07003624 ? in.toolMinor * mToolSizeLinearScale
3625 + mToolSizeLinearBias
Jeff Brownace13b12011-03-09 17:39:48 -08003626 : 0;
3627 } else {
3628 toolMinor = toolMajor;
3629 }
3630 break;
3631 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
3632 if (in.toolMajor != 0) {
3633 float diameter = sqrtf(in.toolMajor
Jeff Brownbe1aa822011-07-27 16:04:54 -07003634 * mToolSizeAreaScale + mToolSizeAreaBias);
3635 toolMajor = diameter * mToolSizeLinearScale + mToolSizeLinearBias;
Jeff Brownace13b12011-03-09 17:39:48 -08003636 } else {
3637 toolMajor = 0;
3638 }
3639 toolMinor = toolMajor;
3640 break;
3641 default:
3642 toolMajor = 0;
3643 toolMinor = 0;
3644 break;
3645 }
3646
3647 if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003648 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
3649 toolMajor /= touchingCount;
3650 toolMinor /= touchingCount;
Jeff Brownace13b12011-03-09 17:39:48 -08003651 }
3652
3653 // Pressure
3654 float rawPressure;
3655 switch (mCalibration.pressureSource) {
3656 case Calibration::PRESSURE_SOURCE_PRESSURE:
3657 rawPressure = in.pressure;
3658 break;
3659 case Calibration::PRESSURE_SOURCE_TOUCH:
3660 rawPressure = in.touchMajor;
3661 break;
3662 default:
3663 rawPressure = 0;
3664 }
3665
3666 float pressure;
3667 switch (mCalibration.pressureCalibration) {
3668 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3669 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003670 pressure = rawPressure * mPressureScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003671 break;
3672 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003673 pressure = in.isHovering ? 0 : 1;
Jeff Brownace13b12011-03-09 17:39:48 -08003674 break;
3675 }
3676
3677 // TouchMajor and TouchMinor
3678 float touchMajor, touchMinor;
3679 switch (mCalibration.touchSizeCalibration) {
3680 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003681 touchMajor = in.touchMajor * mGeometricScale;
3682 if (mRawPointerAxes.touchMinor.valid) {
3683 touchMinor = in.touchMinor * mGeometricScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003684 } else {
3685 touchMinor = touchMajor;
3686 }
3687 break;
3688 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
3689 touchMajor = toolMajor * pressure;
3690 touchMinor = toolMinor * pressure;
3691 break;
3692 default:
3693 touchMajor = 0;
3694 touchMinor = 0;
3695 break;
3696 }
3697
3698 if (touchMajor > toolMajor) {
3699 touchMajor = toolMajor;
3700 }
3701 if (touchMinor > toolMinor) {
3702 touchMinor = toolMinor;
3703 }
3704
3705 // Size
3706 float size;
3707 switch (mCalibration.sizeCalibration) {
3708 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003709 float rawSize = mRawPointerAxes.toolMinor.valid
Jeff Brownace13b12011-03-09 17:39:48 -08003710 ? avg(in.toolMajor, in.toolMinor)
3711 : in.toolMajor;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003712 size = rawSize * mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003713 break;
3714 }
3715 default:
3716 size = 0;
3717 break;
3718 }
3719
3720 // Orientation
3721 float orientation;
3722 switch (mCalibration.orientationCalibration) {
3723 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003724 orientation = in.orientation * mOrientationScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003725 break;
3726 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3727 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3728 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3729 if (c1 != 0 || c2 != 0) {
3730 orientation = atan2f(c1, c2) * 0.5f;
Jeff Brown2352b972011-04-12 22:39:53 -07003731 float scale = 1.0f + hypotf(c1, c2) / 16.0f;
Jeff Brownace13b12011-03-09 17:39:48 -08003732 touchMajor *= scale;
3733 touchMinor /= scale;
3734 toolMajor *= scale;
3735 toolMinor /= scale;
3736 } else {
3737 orientation = 0;
3738 }
3739 break;
3740 }
3741 default:
3742 orientation = 0;
3743 }
3744
Jeff Brown80fd47c2011-05-24 01:07:44 -07003745 // Distance
3746 float distance;
3747 switch (mCalibration.distanceCalibration) {
3748 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003749 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003750 break;
3751 default:
3752 distance = 0;
3753 }
3754
Jeff Brownace13b12011-03-09 17:39:48 -08003755 // X and Y
3756 // Adjust coords for surface orientation.
3757 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003758 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08003759 case DISPLAY_ORIENTATION_90:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003760 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
3761 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003762 orientation -= M_PI_2;
3763 if (orientation < - M_PI_2) {
3764 orientation += M_PI;
3765 }
3766 break;
3767 case DISPLAY_ORIENTATION_180:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003768 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
3769 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003770 break;
3771 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003772 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
3773 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003774 orientation += M_PI_2;
3775 if (orientation > M_PI_2) {
3776 orientation -= M_PI;
3777 }
3778 break;
3779 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003780 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
3781 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003782 break;
3783 }
3784
3785 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003786 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003787 out.clear();
3788 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3789 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3790 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3791 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3792 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3793 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3794 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3795 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3796 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003797 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003798
3799 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003800 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
3801 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003802 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003803 properties.id = id;
3804 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08003805
Jeff Brownbe1aa822011-07-27 16:04:54 -07003806 // Write id index.
3807 mCurrentCookedPointerData.idToIndex[id] = i;
3808 }
Jeff Brownace13b12011-03-09 17:39:48 -08003809}
3810
Jeff Brown79ac9692011-04-19 21:20:10 -07003811void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3812 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003813 // Update current gesture coordinates.
3814 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07003815 bool sendEvents = preparePointerGestures(when,
3816 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3817 if (!sendEvents) {
3818 return;
3819 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003820 if (finishPreviousGesture) {
3821 cancelPreviousGesture = false;
3822 }
Jeff Brownace13b12011-03-09 17:39:48 -08003823
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003824 // Update the pointer presentation and spots.
3825 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3826 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3827 if (finishPreviousGesture || cancelPreviousGesture) {
3828 mPointerController->clearSpots();
3829 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07003830 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
3831 mPointerGesture.currentGestureIdToIndex,
3832 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003833 } else {
3834 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
3835 }
Jeff Brown214eaf42011-05-26 19:17:02 -07003836
Jeff Brown538881e2011-05-25 18:23:38 -07003837 // Show or hide the pointer if needed.
3838 switch (mPointerGesture.currentGestureMode) {
3839 case PointerGesture::NEUTRAL:
3840 case PointerGesture::QUIET:
3841 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3842 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3843 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
3844 // Remind the user of where the pointer is after finishing a gesture with spots.
3845 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
3846 }
3847 break;
3848 case PointerGesture::TAP:
3849 case PointerGesture::TAP_DRAG:
3850 case PointerGesture::BUTTON_CLICK_OR_DRAG:
3851 case PointerGesture::HOVER:
3852 case PointerGesture::PRESS:
3853 // Unfade the pointer when the current gesture manipulates the
3854 // area directly under the pointer.
3855 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3856 break;
3857 case PointerGesture::SWIPE:
3858 case PointerGesture::FREEFORM:
3859 // Fade the pointer when the current gesture manipulates a different
3860 // area and there are spots to guide the user experience.
3861 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3862 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3863 } else {
3864 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3865 }
3866 break;
Jeff Brown2352b972011-04-12 22:39:53 -07003867 }
3868
Jeff Brownace13b12011-03-09 17:39:48 -08003869 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003870 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003871 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08003872
3873 // Update last coordinates of pointers that have moved so that we observe the new
3874 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07003875 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
3876 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
3877 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07003878 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08003879 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
3880 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
3881 bool moveNeeded = false;
3882 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07003883 && !mPointerGesture.lastGestureIdBits.isEmpty()
3884 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08003885 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
3886 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003887 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003888 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003889 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003890 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3891 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003892 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003893 moveNeeded = true;
3894 }
Jeff Brownace13b12011-03-09 17:39:48 -08003895 }
3896
3897 // Send motion events for all pointers that went up or were canceled.
3898 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
3899 if (!dispatchedGestureIdBits.isEmpty()) {
3900 if (cancelPreviousGesture) {
3901 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003902 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
3903 AMOTION_EVENT_EDGE_FLAG_NONE,
3904 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003905 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3906 dispatchedGestureIdBits, -1,
3907 0, 0, mPointerGesture.downTime);
3908
3909 dispatchedGestureIdBits.clear();
3910 } else {
3911 BitSet32 upGestureIdBits;
3912 if (finishPreviousGesture) {
3913 upGestureIdBits = dispatchedGestureIdBits;
3914 } else {
3915 upGestureIdBits.value = dispatchedGestureIdBits.value
3916 & ~mPointerGesture.currentGestureIdBits.value;
3917 }
3918 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003919 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003920
3921 dispatchMotion(when, policyFlags, mPointerSource,
3922 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003923 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3924 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003925 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3926 dispatchedGestureIdBits, id,
3927 0, 0, mPointerGesture.downTime);
3928
3929 dispatchedGestureIdBits.clearBit(id);
3930 }
3931 }
3932 }
3933
3934 // Send motion events for all pointers that moved.
3935 if (moveNeeded) {
3936 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003937 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3938 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003939 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3940 dispatchedGestureIdBits, -1,
3941 0, 0, mPointerGesture.downTime);
3942 }
3943
3944 // Send motion events for all pointers that went down.
3945 if (down) {
3946 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
3947 & ~dispatchedGestureIdBits.value);
3948 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003949 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003950 dispatchedGestureIdBits.markBit(id);
3951
Jeff Brownace13b12011-03-09 17:39:48 -08003952 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08003953 mPointerGesture.downTime = when;
3954 }
3955
3956 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Browna6111372011-07-14 21:48:23 -07003957 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003958 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003959 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3960 dispatchedGestureIdBits, id,
3961 0, 0, mPointerGesture.downTime);
3962 }
3963 }
3964
Jeff Brownace13b12011-03-09 17:39:48 -08003965 // Send motion events for hover.
3966 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
3967 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003968 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
3969 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3970 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003971 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3972 mPointerGesture.currentGestureIdBits, -1,
3973 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07003974 } else if (dispatchedGestureIdBits.isEmpty()
3975 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
3976 // Synthesize a hover move event after all pointers go up to indicate that
3977 // the pointer is hovering again even if the user is not currently touching
3978 // the touch pad. This ensures that a view will receive a fresh hover enter
3979 // event after a tap.
3980 float x, y;
3981 mPointerController->getPosition(&x, &y);
3982
3983 PointerProperties pointerProperties;
3984 pointerProperties.clear();
3985 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07003986 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07003987
3988 PointerCoords pointerCoords;
3989 pointerCoords.clear();
3990 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3991 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3992
Jeff Brownbe1aa822011-07-27 16:04:54 -07003993 NotifyMotionArgs args(when, getDeviceId(), mPointerSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07003994 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
3995 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3996 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003997 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08003998 }
3999
4000 // Update state.
4001 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4002 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004003 mPointerGesture.lastGestureIdBits.clear();
4004 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004005 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4006 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004007 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004008 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004009 mPointerGesture.lastGestureProperties[index].copyFrom(
4010 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004011 mPointerGesture.lastGestureCoords[index].copyFrom(
4012 mPointerGesture.currentGestureCoords[index]);
4013 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004014 }
4015 }
4016}
4017
Jeff Brown79ac9692011-04-19 21:20:10 -07004018bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4019 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004020 *outCancelPreviousGesture = false;
4021 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004022
Jeff Brown79ac9692011-04-19 21:20:10 -07004023 // Handle TAP timeout.
4024 if (isTimeout) {
4025#if DEBUG_GESTURES
4026 LOGD("Gestures: Processing timeout");
4027#endif
4028
4029 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004030 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004031 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004032 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004033 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004034 } else {
4035 // The tap is finished.
4036#if DEBUG_GESTURES
4037 LOGD("Gestures: TAP finished");
4038#endif
4039 *outFinishPreviousGesture = true;
4040
4041 mPointerGesture.activeGestureId = -1;
4042 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4043 mPointerGesture.currentGestureIdBits.clear();
4044
Jeff Brown19c97d462011-06-01 12:33:19 -07004045 mPointerGesture.pointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004046 return true;
4047 }
4048 }
4049
4050 // We did not handle this timeout.
4051 return false;
4052 }
4053
Jeff Brownace13b12011-03-09 17:39:48 -08004054 // Update the velocity tracker.
4055 {
4056 VelocityTracker::Position positions[MAX_POINTERS];
4057 uint32_t count = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004058 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); count++) {
4059 uint32_t id = idBits.clearFirstMarkedBit();
4060 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
4061 positions[count].x = pointer.x * mPointerGestureXMovementScale;
4062 positions[count].y = pointer.y * mPointerGestureYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004063 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004064 mPointerGesture.velocityTracker.addMovement(when,
4065 mCurrentRawPointerData.touchingIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004066 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004067
Jeff Brownace13b12011-03-09 17:39:48 -08004068 // Pick a new active touch id if needed.
4069 // Choose an arbitrary pointer that just went down, if there is one.
4070 // Otherwise choose an arbitrary remaining pointer.
4071 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004072 // We keep the same active touch id for as long as possible.
4073 bool activeTouchChanged = false;
4074 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4075 int32_t activeTouchId = lastActiveTouchId;
4076 if (activeTouchId < 0) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004077 if (!mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004078 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004079 activeTouchId = mPointerGesture.activeTouchId =
4080 mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004081 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004082 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004083 } else if (!mCurrentRawPointerData.touchingIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004084 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004085 if (!mCurrentRawPointerData.touchingIdBits.isEmpty()) {
4086 activeTouchId = mPointerGesture.activeTouchId =
4087 mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004088 } else {
4089 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004090 }
4091 }
4092
Jeff Brownbe1aa822011-07-27 16:04:54 -07004093 uint32_t currentTouchingPointerCount = mCurrentRawPointerData.touchingIdBits.count();
4094 uint32_t lastTouchingPointerCount = mLastRawPointerData.touchingIdBits.count();
4095
Jeff Brownace13b12011-03-09 17:39:48 -08004096 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004097 bool isQuietTime = false;
4098 if (activeTouchId < 0) {
4099 mPointerGesture.resetQuietTime();
4100 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004101 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004102 if (!isQuietTime) {
4103 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4104 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4105 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brownbe1aa822011-07-27 16:04:54 -07004106 && currentTouchingPointerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004107 // Enter quiet time when exiting swipe or freeform state.
4108 // This is to prevent accidentally entering the hover state and flinging the
4109 // pointer when finishing a swipe and there is still one pointer left onscreen.
4110 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004111 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brownbe1aa822011-07-27 16:04:54 -07004112 && currentTouchingPointerCount >= 2
4113 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004114 // Enter quiet time when releasing the button and there are still two or more
4115 // fingers down. This may indicate that one finger was used to press the button
4116 // but it has not gone up yet.
4117 isQuietTime = true;
4118 }
4119 if (isQuietTime) {
4120 mPointerGesture.quietTime = when;
4121 }
Jeff Brownace13b12011-03-09 17:39:48 -08004122 }
4123 }
4124
4125 // Switch states based on button and pointer state.
4126 if (isQuietTime) {
4127 // Case 1: Quiet time. (QUIET)
4128#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004129 LOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004130 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004131#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004132 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4133 *outFinishPreviousGesture = true;
4134 }
Jeff Brownace13b12011-03-09 17:39:48 -08004135
4136 mPointerGesture.activeGestureId = -1;
4137 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004138 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004139
Jeff Brown19c97d462011-06-01 12:33:19 -07004140 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004141 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004142 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004143 // The pointer follows the active touch point.
4144 // Emit DOWN, MOVE, UP events at the pointer location.
4145 //
4146 // Only the active touch matters; other fingers are ignored. This policy helps
4147 // to handle the case where the user places a second finger on the touch pad
4148 // to apply the necessary force to depress an integrated button below the surface.
4149 // We don't want the second finger to be delivered to applications.
4150 //
4151 // For this to work well, we need to make sure to track the pointer that is really
4152 // active. If the user first puts one finger down to click then adds another
4153 // finger to drag then the active pointer should switch to the finger that is
4154 // being dragged.
4155#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004156 LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07004157 "currentTouchingPointerCount=%d", activeTouchId, currentTouchingPointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004158#endif
4159 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004160 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004161 *outFinishPreviousGesture = true;
4162 mPointerGesture.activeGestureId = 0;
4163 }
4164
4165 // Switch pointers if needed.
4166 // Find the fastest pointer and follow it.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004167 if (activeTouchId >= 0 && currentTouchingPointerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004168 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004169 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004170 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
4171 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07004172 float vx, vy;
4173 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4174 float speed = hypotf(vx, vy);
4175 if (speed > bestSpeed) {
4176 bestId = id;
4177 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004178 }
Jeff Brown8d608662010-08-30 03:02:23 -07004179 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004180 }
4181 if (bestId >= 0 && bestId != activeTouchId) {
4182 mPointerGesture.activeTouchId = activeTouchId = bestId;
4183 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004184#if DEBUG_GESTURES
Jeff Brown19c97d462011-06-01 12:33:19 -07004185 LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
4186 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004187#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004188 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004189 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004190
Jeff Brownbe1aa822011-07-27 16:04:54 -07004191 if (activeTouchId >= 0 && mLastRawPointerData.touchingIdBits.hasBit(activeTouchId)) {
4192 const RawPointerData::Pointer& currentPointer =
4193 mCurrentRawPointerData.pointerForId(activeTouchId);
4194 const RawPointerData::Pointer& lastPointer =
4195 mLastRawPointerData.pointerForId(activeTouchId);
4196 float deltaX = (currentPointer.x - lastPointer.x) * mPointerGestureXMovementScale;
4197 float deltaY = (currentPointer.y - lastPointer.y) * mPointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004198
Jeff Brownbe1aa822011-07-27 16:04:54 -07004199 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004200 mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
4201
4202 // Move the pointer using a relative motion.
4203 // When using spots, the click will occur at the position of the anchor
4204 // spot and all other spots will move there.
4205 mPointerController->move(deltaX, deltaY);
4206 } else {
4207 mPointerGesture.pointerVelocityControl.reset();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004208 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004209
Jeff Brownace13b12011-03-09 17:39:48 -08004210 float x, y;
4211 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004212
Jeff Brown79ac9692011-04-19 21:20:10 -07004213 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004214 mPointerGesture.currentGestureIdBits.clear();
4215 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4216 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004217 mPointerGesture.currentGestureProperties[0].clear();
4218 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004219 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004220 mPointerGesture.currentGestureCoords[0].clear();
4221 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4222 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4223 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004224 } else if (currentTouchingPointerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004225 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004226 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4227 *outFinishPreviousGesture = true;
4228 }
Jeff Brownace13b12011-03-09 17:39:48 -08004229
Jeff Brown79ac9692011-04-19 21:20:10 -07004230 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004231 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004232 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004233 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4234 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brownbe1aa822011-07-27 16:04:54 -07004235 && lastTouchingPointerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004236 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004237 float x, y;
4238 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004239 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4240 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004241#if DEBUG_GESTURES
4242 LOGD("Gestures: TAP");
4243#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004244
4245 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004246 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004247 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004248
Jeff Brownace13b12011-03-09 17:39:48 -08004249 mPointerGesture.activeGestureId = 0;
4250 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004251 mPointerGesture.currentGestureIdBits.clear();
4252 mPointerGesture.currentGestureIdBits.markBit(
4253 mPointerGesture.activeGestureId);
4254 mPointerGesture.currentGestureIdToIndex[
4255 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004256 mPointerGesture.currentGestureProperties[0].clear();
4257 mPointerGesture.currentGestureProperties[0].id =
4258 mPointerGesture.activeGestureId;
4259 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004260 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004261 mPointerGesture.currentGestureCoords[0].clear();
4262 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004263 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004264 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004265 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004266 mPointerGesture.currentGestureCoords[0].setAxisValue(
4267 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004268
Jeff Brownace13b12011-03-09 17:39:48 -08004269 tapped = true;
4270 } else {
4271#if DEBUG_GESTURES
4272 LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004273 x - mPointerGesture.tapX,
4274 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004275#endif
4276 }
4277 } else {
4278#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004279 LOGD("Gestures: Not a TAP, %0.3fms since down",
4280 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004281#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004282 }
Jeff Brownace13b12011-03-09 17:39:48 -08004283 }
Jeff Brown2352b972011-04-12 22:39:53 -07004284
Jeff Brown19c97d462011-06-01 12:33:19 -07004285 mPointerGesture.pointerVelocityControl.reset();
4286
Jeff Brownace13b12011-03-09 17:39:48 -08004287 if (!tapped) {
4288#if DEBUG_GESTURES
4289 LOGD("Gestures: NEUTRAL");
4290#endif
4291 mPointerGesture.activeGestureId = -1;
4292 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004293 mPointerGesture.currentGestureIdBits.clear();
4294 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004295 } else if (currentTouchingPointerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004296 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004297 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004298 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4299 // When in TAP_DRAG, emit MOVE events at the pointer location.
Jeff Brownb6110c22011-04-01 16:15:13 -07004300 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004301
Jeff Brown79ac9692011-04-19 21:20:10 -07004302 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4303 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004304 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004305 float x, y;
4306 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004307 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4308 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004309 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4310 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004311#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004312 LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
4313 x - mPointerGesture.tapX,
4314 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004315#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004316 }
4317 } else {
4318#if DEBUG_GESTURES
4319 LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
4320 (when - mPointerGesture.tapUpTime) * 0.000001f);
4321#endif
4322 }
4323 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4324 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4325 }
Jeff Brownace13b12011-03-09 17:39:48 -08004326
Jeff Brownbe1aa822011-07-27 16:04:54 -07004327 if (mLastRawPointerData.touchingIdBits.hasBit(activeTouchId)) {
4328 const RawPointerData::Pointer& currentPointer =
4329 mCurrentRawPointerData.pointerForId(activeTouchId);
4330 const RawPointerData::Pointer& lastPointer =
4331 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004332 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brownbe1aa822011-07-27 16:04:54 -07004333 * mPointerGestureXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004334 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brownbe1aa822011-07-27 16:04:54 -07004335 * mPointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004336
Jeff Brownbe1aa822011-07-27 16:04:54 -07004337 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004338 mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
4339
Jeff Brown2352b972011-04-12 22:39:53 -07004340 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004341 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004342 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004343 } else {
4344 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004345 }
4346
Jeff Brown79ac9692011-04-19 21:20:10 -07004347 bool down;
4348 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4349#if DEBUG_GESTURES
4350 LOGD("Gestures: TAP_DRAG");
4351#endif
4352 down = true;
4353 } else {
4354#if DEBUG_GESTURES
4355 LOGD("Gestures: HOVER");
4356#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004357 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4358 *outFinishPreviousGesture = true;
4359 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004360 mPointerGesture.activeGestureId = 0;
4361 down = false;
4362 }
Jeff Brownace13b12011-03-09 17:39:48 -08004363
4364 float x, y;
4365 mPointerController->getPosition(&x, &y);
4366
Jeff Brownace13b12011-03-09 17:39:48 -08004367 mPointerGesture.currentGestureIdBits.clear();
4368 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4369 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004370 mPointerGesture.currentGestureProperties[0].clear();
4371 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4372 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004373 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004374 mPointerGesture.currentGestureCoords[0].clear();
4375 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4376 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004377 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4378 down ? 1.0f : 0.0f);
4379
Jeff Brownbe1aa822011-07-27 16:04:54 -07004380 if (lastTouchingPointerCount == 0 && currentTouchingPointerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004381 mPointerGesture.resetTap();
4382 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004383 mPointerGesture.tapX = x;
4384 mPointerGesture.tapY = y;
4385 }
Jeff Brownace13b12011-03-09 17:39:48 -08004386 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004387 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4388 // We need to provide feedback for each finger that goes down so we cannot wait
4389 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004390 //
Jeff Brown2352b972011-04-12 22:39:53 -07004391 // The ambiguous case is deciding what to do when there are two fingers down but they
4392 // have not moved enough to determine whether they are part of a drag or part of a
4393 // freeform gesture, or just a press or long-press at the pointer location.
4394 //
4395 // When there are two fingers we start with the PRESS hypothesis and we generate a
4396 // down at the pointer location.
4397 //
4398 // When the two fingers move enough or when additional fingers are added, we make
4399 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Jeff Brownb6110c22011-04-01 16:15:13 -07004400 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004401
Jeff Brown214eaf42011-05-26 19:17:02 -07004402 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004403 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004404 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004405 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4406 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004407 *outFinishPreviousGesture = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004408 } else if (!settled && currentTouchingPointerCount > lastTouchingPointerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004409 // Additional pointers have gone down but not yet settled.
4410 // Reset the gesture.
4411#if DEBUG_GESTURES
4412 LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004413 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004414 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004415 * 0.000001f);
4416#endif
4417 *outCancelPreviousGesture = true;
4418 } else {
4419 // Continue previous gesture.
4420 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4421 }
4422
4423 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004424 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4425 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004426 mPointerGesture.referenceIdBits.clear();
Jeff Brown19c97d462011-06-01 12:33:19 -07004427 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004428
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004429 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004430#if DEBUG_GESTURES
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004431 LOGD("Gestures: Using centroid as reference for MULTITOUCH, "
4432 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004433 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004434 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004435#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004436 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4437 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004438 &mPointerGesture.referenceTouchY);
4439 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4440 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004441 }
Jeff Brownace13b12011-03-09 17:39:48 -08004442
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004443 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004444 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits.value
4445 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4446 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004447 mPointerGesture.referenceDeltas[id].dx = 0;
4448 mPointerGesture.referenceDeltas[id].dy = 0;
4449 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004450 mPointerGesture.referenceIdBits = mCurrentRawPointerData.touchingIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004451
4452 // Add delta for all fingers and calculate a common movement delta.
4453 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004454 BitSet32 commonIdBits(mLastRawPointerData.touchingIdBits.value
4455 & mCurrentRawPointerData.touchingIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004456 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4457 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004458 uint32_t id = idBits.clearFirstMarkedBit();
4459 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4460 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004461 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4462 delta.dx += cpd.x - lpd.x;
4463 delta.dy += cpd.y - lpd.y;
4464
4465 if (first) {
4466 commonDeltaX = delta.dx;
4467 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004468 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004469 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4470 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4471 }
4472 }
Jeff Brownace13b12011-03-09 17:39:48 -08004473
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004474 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4475 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4476 float dist[MAX_POINTER_ID + 1];
4477 int32_t distOverThreshold = 0;
4478 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004479 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004480 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbe1aa822011-07-27 16:04:54 -07004481 dist[id] = hypotf(delta.dx * mPointerGestureXZoomScale,
4482 delta.dy * mPointerGestureYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004483 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004484 distOverThreshold += 1;
4485 }
4486 }
4487
4488 // Only transition when at least two pointers have moved further than
4489 // the minimum distance threshold.
4490 if (distOverThreshold >= 2) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004491 if (currentTouchingPointerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004492 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004493#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004494 LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004495 currentTouchingPointerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004496#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004497 *outCancelPreviousGesture = true;
4498 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4499 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004500 // There are exactly two pointers.
4501 BitSet32 idBits(mCurrentRawPointerData.touchingIdBits);
4502 uint32_t id1 = idBits.clearFirstMarkedBit();
4503 uint32_t id2 = idBits.firstMarkedBit();
4504 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4505 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4506 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4507 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4508 // There are two pointers but they are too far apart for a SWIPE,
4509 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004510#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004511 LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
4512 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004513#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004514 *outCancelPreviousGesture = true;
4515 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4516 } else {
4517 // There are two pointers. Wait for both pointers to start moving
4518 // before deciding whether this is a SWIPE or FREEFORM gesture.
4519 float dist1 = dist[id1];
4520 float dist2 = dist[id2];
4521 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4522 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
4523 // Calculate the dot product of the displacement vectors.
4524 // When the vectors are oriented in approximately the same direction,
4525 // the angle betweeen them is near zero and the cosine of the angle
4526 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4527 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4528 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
4529 float dx1 = delta1.dx * mPointerGestureXZoomScale;
4530 float dy1 = delta1.dy * mPointerGestureYZoomScale;
4531 float dx2 = delta2.dx * mPointerGestureXZoomScale;
4532 float dy2 = delta2.dy * mPointerGestureYZoomScale;
4533 float dot = dx1 * dx2 + dy1 * dy2;
4534 float cosine = dot / (dist1 * dist2); // denominator always > 0
4535 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
4536 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004537#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004538 LOGD("Gestures: PRESS transitioned to SWIPE, "
4539 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4540 "cosine %0.3f >= %0.3f",
4541 dist1, mConfig.pointerGestureMultitouchMinDistance,
4542 dist2, mConfig.pointerGestureMultitouchMinDistance,
4543 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004544#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004545 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4546 } else {
4547 // Pointers are moving in different directions. Switch to FREEFORM.
4548#if DEBUG_GESTURES
4549 LOGD("Gestures: PRESS transitioned to FREEFORM, "
4550 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4551 "cosine %0.3f < %0.3f",
4552 dist1, mConfig.pointerGestureMultitouchMinDistance,
4553 dist2, mConfig.pointerGestureMultitouchMinDistance,
4554 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
4555#endif
4556 *outCancelPreviousGesture = true;
4557 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4558 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004559 }
Jeff Brownace13b12011-03-09 17:39:48 -08004560 }
4561 }
Jeff Brownace13b12011-03-09 17:39:48 -08004562 }
4563 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004564 // Switch from SWIPE to FREEFORM if additional pointers go down.
4565 // Cancel previous gesture.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004566 if (currentTouchingPointerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004567#if DEBUG_GESTURES
4568 LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004569 currentTouchingPointerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004570#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004571 *outCancelPreviousGesture = true;
4572 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004573 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004574 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004575
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004576 // Move the reference points based on the overall group motion of the fingers
4577 // except in PRESS mode while waiting for a transition to occur.
4578 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4579 && (commonDeltaX || commonDeltaY)) {
4580 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004581 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07004582 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004583 delta.dx = 0;
4584 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004585 }
4586
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004587 mPointerGesture.referenceTouchX += commonDeltaX;
4588 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004589
Jeff Brownbe1aa822011-07-27 16:04:54 -07004590 commonDeltaX *= mPointerGestureXMovementScale;
4591 commonDeltaY *= mPointerGestureYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07004592
Jeff Brownbe1aa822011-07-27 16:04:54 -07004593 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004594 mPointerGesture.pointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004595
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004596 mPointerGesture.referenceGestureX += commonDeltaX;
4597 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07004598 }
4599
4600 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07004601 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
4602 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4603 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004604#if DEBUG_GESTURES
Jeff Brown612891e2011-07-15 20:44:17 -07004605 LOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07004606 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004607 activeTouchId, mPointerGesture.activeGestureId, currentTouchingPointerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004608#endif
4609 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
4610
4611 mPointerGesture.currentGestureIdBits.clear();
4612 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4613 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004614 mPointerGesture.currentGestureProperties[0].clear();
4615 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4616 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004617 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004618 mPointerGesture.currentGestureCoords[0].clear();
4619 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4620 mPointerGesture.referenceGestureX);
4621 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4622 mPointerGesture.referenceGestureY);
4623 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08004624 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4625 // FREEFORM mode.
4626#if DEBUG_GESTURES
4627 LOGD("Gestures: FREEFORM activeTouchId=%d,"
4628 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004629 activeTouchId, mPointerGesture.activeGestureId, currentTouchingPointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004630#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004631 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004632
Jeff Brownace13b12011-03-09 17:39:48 -08004633 mPointerGesture.currentGestureIdBits.clear();
4634
4635 BitSet32 mappedTouchIdBits;
4636 BitSet32 usedGestureIdBits;
4637 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4638 // Initially, assign the active gesture id to the active touch point
4639 // if there is one. No other touch id bits are mapped yet.
4640 if (!*outCancelPreviousGesture) {
4641 mappedTouchIdBits.markBit(activeTouchId);
4642 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4643 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4644 mPointerGesture.activeGestureId;
4645 } else {
4646 mPointerGesture.activeGestureId = -1;
4647 }
4648 } else {
4649 // Otherwise, assume we mapped all touches from the previous frame.
4650 // Reuse all mappings that are still applicable.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004651 mappedTouchIdBits.value = mLastRawPointerData.touchingIdBits.value
4652 & mCurrentRawPointerData.touchingIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08004653 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4654
4655 // Check whether we need to choose a new active gesture id because the
4656 // current went went up.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004657 for (BitSet32 upTouchIdBits(mLastRawPointerData.touchingIdBits.value
4658 & ~mCurrentRawPointerData.touchingIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004659 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004660 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004661 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4662 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4663 mPointerGesture.activeGestureId = -1;
4664 break;
4665 }
4666 }
4667 }
4668
4669#if DEBUG_GESTURES
4670 LOGD("Gestures: FREEFORM follow up "
4671 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4672 "activeGestureId=%d",
4673 mappedTouchIdBits.value, usedGestureIdBits.value,
4674 mPointerGesture.activeGestureId);
4675#endif
4676
Jeff Brownbe1aa822011-07-27 16:04:54 -07004677 BitSet32 idBits(mCurrentRawPointerData.touchingIdBits);
4678 for (uint32_t i = 0; i < currentTouchingPointerCount; i++) {
4679 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004680 uint32_t gestureId;
4681 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004682 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004683 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4684#if DEBUG_GESTURES
4685 LOGD("Gestures: FREEFORM "
4686 "new mapping for touch id %d -> gesture id %d",
4687 touchId, gestureId);
4688#endif
4689 } else {
4690 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4691#if DEBUG_GESTURES
4692 LOGD("Gestures: FREEFORM "
4693 "existing mapping for touch id %d -> gesture id %d",
4694 touchId, gestureId);
4695#endif
4696 }
4697 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4698 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4699
Jeff Brownbe1aa822011-07-27 16:04:54 -07004700 const RawPointerData::Pointer& pointer =
4701 mCurrentRawPointerData.pointerForId(touchId);
4702 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
4703 * mPointerGestureXZoomScale;
4704 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
4705 * mPointerGestureYZoomScale;
4706 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004707
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004708 mPointerGesture.currentGestureProperties[i].clear();
4709 mPointerGesture.currentGestureProperties[i].id = gestureId;
4710 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004711 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004712 mPointerGesture.currentGestureCoords[i].clear();
4713 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004714 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08004715 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004716 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004717 mPointerGesture.currentGestureCoords[i].setAxisValue(
4718 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4719 }
4720
4721 if (mPointerGesture.activeGestureId < 0) {
4722 mPointerGesture.activeGestureId =
4723 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4724#if DEBUG_GESTURES
4725 LOGD("Gestures: FREEFORM new "
4726 "activeGestureId=%d", mPointerGesture.activeGestureId);
4727#endif
4728 }
Jeff Brown2352b972011-04-12 22:39:53 -07004729 }
Jeff Brownace13b12011-03-09 17:39:48 -08004730 }
4731
Jeff Brownbe1aa822011-07-27 16:04:54 -07004732 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004733
Jeff Brownace13b12011-03-09 17:39:48 -08004734#if DEBUG_GESTURES
4735 LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004736 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4737 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004738 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004739 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4740 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004741 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004742 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004743 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004744 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004745 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004746 LOGD(" currentGesture[%d]: index=%d, toolType=%d, "
4747 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4748 id, index, properties.toolType,
4749 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004750 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4751 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4752 }
4753 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004754 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004755 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004756 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004757 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004758 LOGD(" lastGesture[%d]: index=%d, toolType=%d, "
4759 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4760 id, index, properties.toolType,
4761 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004762 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4763 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4764 }
4765#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004766 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004767}
4768
4769void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004770 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
4771 const PointerProperties* properties, const PointerCoords* coords,
4772 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08004773 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
4774 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004775 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08004776 uint32_t pointerCount = 0;
4777 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004778 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004779 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004780 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004781 pointerCoords[pointerCount].copyFrom(coords[index]);
4782
4783 if (changedId >= 0 && id == uint32_t(changedId)) {
4784 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
4785 }
4786
4787 pointerCount += 1;
4788 }
4789
Jeff Brownb6110c22011-04-01 16:15:13 -07004790 LOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004791
4792 if (changedId >= 0 && pointerCount == 1) {
4793 // Replace initial down and final up action.
4794 // We can compare the action without masking off the changed pointer index
4795 // because we know the index is 0.
4796 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
4797 action = AMOTION_EVENT_ACTION_DOWN;
4798 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
4799 action = AMOTION_EVENT_ACTION_UP;
4800 } else {
4801 // Can't happen.
Jeff Brownb6110c22011-04-01 16:15:13 -07004802 LOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08004803 }
4804 }
4805
Jeff Brownbe1aa822011-07-27 16:04:54 -07004806 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004807 action, flags, metaState, buttonState, edgeFlags,
4808 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004809 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004810}
4811
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004812bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004813 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004814 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
4815 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08004816 bool changed = false;
4817 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004818 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004819 uint32_t inIndex = inIdToIndex[id];
4820 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004821
4822 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004823 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004824 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004825 PointerCoords& curOutCoords = outCoords[outIndex];
4826
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004827 if (curInProperties != curOutProperties) {
4828 curOutProperties.copyFrom(curInProperties);
4829 changed = true;
4830 }
4831
Jeff Brownace13b12011-03-09 17:39:48 -08004832 if (curInCoords != curOutCoords) {
4833 curOutCoords.copyFrom(curInCoords);
4834 changed = true;
4835 }
4836 }
4837 return changed;
4838}
4839
4840void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004841 if (mPointerController != NULL) {
4842 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4843 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004844}
4845
Jeff Brownbe1aa822011-07-27 16:04:54 -07004846bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
4847 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
4848 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004849}
4850
Jeff Brownbe1aa822011-07-27 16:04:54 -07004851const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07004852 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004853 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07004854 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004855 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004856
4857#if DEBUG_VIRTUAL_KEYS
4858 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
4859 "left=%d, top=%d, right=%d, bottom=%d",
4860 x, y,
4861 virtualKey.keyCode, virtualKey.scanCode,
4862 virtualKey.hitLeft, virtualKey.hitTop,
4863 virtualKey.hitRight, virtualKey.hitBottom);
4864#endif
4865
4866 if (virtualKey.isHit(x, y)) {
4867 return & virtualKey;
4868 }
4869 }
4870
4871 return NULL;
4872}
4873
Jeff Brownbe1aa822011-07-27 16:04:54 -07004874void TouchInputMapper::assignPointerIds() {
4875 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
4876 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
4877
4878 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004879
4880 if (currentPointerCount == 0) {
4881 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004882 return;
4883 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004884
Jeff Brownbe1aa822011-07-27 16:04:54 -07004885 if (lastPointerCount == 0) {
4886 // All pointers are new.
4887 for (uint32_t i = 0; i < currentPointerCount; i++) {
4888 uint32_t id = i;
4889 mCurrentRawPointerData.pointers[i].id = id;
4890 mCurrentRawPointerData.idToIndex[id] = i;
4891 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
4892 }
4893 return;
4894 }
4895
4896 if (currentPointerCount == 1 && lastPointerCount == 1
4897 && mCurrentRawPointerData.pointers[0].toolType
4898 == mLastRawPointerData.pointers[0].toolType) {
4899 // Only one pointer and no change in count so it must have the same id as before.
4900 uint32_t id = mLastRawPointerData.pointers[0].id;
4901 mCurrentRawPointerData.pointers[0].id = id;
4902 mCurrentRawPointerData.idToIndex[id] = 0;
4903 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
4904 return;
4905 }
4906
4907 // General case.
4908 // We build a heap of squared euclidean distances between current and last pointers
4909 // associated with the current and last pointer indices. Then, we find the best
4910 // match (by distance) for each current pointer.
4911 // The pointers must have the same tool type but it is possible for them to
4912 // transition from hovering to touching or vice-versa while retaining the same id.
4913 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
4914
4915 uint32_t heapSize = 0;
4916 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
4917 currentPointerIndex++) {
4918 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
4919 lastPointerIndex++) {
4920 const RawPointerData::Pointer& currentPointer =
4921 mCurrentRawPointerData.pointers[currentPointerIndex];
4922 const RawPointerData::Pointer& lastPointer =
4923 mLastRawPointerData.pointers[lastPointerIndex];
4924 if (currentPointer.toolType == lastPointer.toolType) {
4925 int64_t deltaX = currentPointer.x - lastPointer.x;
4926 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004927
4928 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
4929
4930 // Insert new element into the heap (sift up).
4931 heap[heapSize].currentPointerIndex = currentPointerIndex;
4932 heap[heapSize].lastPointerIndex = lastPointerIndex;
4933 heap[heapSize].distance = distance;
4934 heapSize += 1;
4935 }
4936 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004937 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004938
Jeff Brownbe1aa822011-07-27 16:04:54 -07004939 // Heapify
4940 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
4941 startIndex -= 1;
4942 for (uint32_t parentIndex = startIndex; ;) {
4943 uint32_t childIndex = parentIndex * 2 + 1;
4944 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004945 break;
4946 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004947
4948 if (childIndex + 1 < heapSize
4949 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4950 childIndex += 1;
4951 }
4952
4953 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4954 break;
4955 }
4956
4957 swap(heap[parentIndex], heap[childIndex]);
4958 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004959 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004960 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004961
4962#if DEBUG_POINTER_ASSIGNMENT
Jeff Brownbe1aa822011-07-27 16:04:54 -07004963 LOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
4964 for (size_t i = 0; i < heapSize; i++) {
4965 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4966 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4967 heap[i].distance);
4968 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004969#endif
4970
Jeff Brownbe1aa822011-07-27 16:04:54 -07004971 // Pull matches out by increasing order of distance.
4972 // To avoid reassigning pointers that have already been matched, the loop keeps track
4973 // of which last and current pointers have been matched using the matchedXXXBits variables.
4974 // It also tracks the used pointer id bits.
4975 BitSet32 matchedLastBits(0);
4976 BitSet32 matchedCurrentBits(0);
4977 BitSet32 usedIdBits(0);
4978 bool first = true;
4979 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
4980 while (heapSize > 0) {
4981 if (first) {
4982 // The first time through the loop, we just consume the root element of
4983 // the heap (the one with smallest distance).
4984 first = false;
4985 } else {
4986 // Previous iterations consumed the root element of the heap.
4987 // Pop root element off of the heap (sift down).
4988 heap[0] = heap[heapSize];
4989 for (uint32_t parentIndex = 0; ;) {
4990 uint32_t childIndex = parentIndex * 2 + 1;
4991 if (childIndex >= heapSize) {
4992 break;
4993 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004994
Jeff Brownbe1aa822011-07-27 16:04:54 -07004995 if (childIndex + 1 < heapSize
4996 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4997 childIndex += 1;
4998 }
4999
5000 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5001 break;
5002 }
5003
5004 swap(heap[parentIndex], heap[childIndex]);
5005 parentIndex = childIndex;
5006 }
5007
5008#if DEBUG_POINTER_ASSIGNMENT
5009 LOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
5010 for (size_t i = 0; i < heapSize; i++) {
5011 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
5012 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5013 heap[i].distance);
5014 }
5015#endif
5016 }
5017
5018 heapSize -= 1;
5019
5020 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5021 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5022
5023 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5024 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5025
5026 matchedCurrentBits.markBit(currentPointerIndex);
5027 matchedLastBits.markBit(lastPointerIndex);
5028
5029 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5030 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5031 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5032 mCurrentRawPointerData.markIdBit(id,
5033 mCurrentRawPointerData.isHovering(currentPointerIndex));
5034 usedIdBits.markBit(id);
5035
5036#if DEBUG_POINTER_ASSIGNMENT
5037 LOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
5038 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5039#endif
5040 break;
5041 }
5042 }
5043
5044 // Assign fresh ids to pointers that were not matched in the process.
5045 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5046 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5047 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5048
5049 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5050 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5051 mCurrentRawPointerData.markIdBit(id,
5052 mCurrentRawPointerData.isHovering(currentPointerIndex));
5053
5054#if DEBUG_POINTER_ASSIGNMENT
5055 LOGD("assignPointerIds - assigned: cur=%d, id=%d",
5056 currentPointerIndex, id);
5057#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005058 }
5059}
5060
Jeff Brown6d0fec22010-07-23 21:28:06 -07005061int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005062 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5063 return AKEY_STATE_VIRTUAL;
5064 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005065
Jeff Brownbe1aa822011-07-27 16:04:54 -07005066 size_t numVirtualKeys = mVirtualKeys.size();
5067 for (size_t i = 0; i < numVirtualKeys; i++) {
5068 const VirtualKey& virtualKey = mVirtualKeys[i];
5069 if (virtualKey.keyCode == keyCode) {
5070 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005071 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005072 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005073
5074 return AKEY_STATE_UNKNOWN;
5075}
5076
5077int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005078 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5079 return AKEY_STATE_VIRTUAL;
5080 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005081
Jeff Brownbe1aa822011-07-27 16:04:54 -07005082 size_t numVirtualKeys = mVirtualKeys.size();
5083 for (size_t i = 0; i < numVirtualKeys; i++) {
5084 const VirtualKey& virtualKey = mVirtualKeys[i];
5085 if (virtualKey.scanCode == scanCode) {
5086 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005087 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005088 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005089
5090 return AKEY_STATE_UNKNOWN;
5091}
5092
5093bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5094 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005095 size_t numVirtualKeys = mVirtualKeys.size();
5096 for (size_t i = 0; i < numVirtualKeys; i++) {
5097 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005098
Jeff Brownbe1aa822011-07-27 16:04:54 -07005099 for (size_t i = 0; i < numCodes; i++) {
5100 if (virtualKey.keyCode == keyCodes[i]) {
5101 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005102 }
5103 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005104 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005105
5106 return true;
5107}
5108
5109
5110// --- SingleTouchInputMapper ---
5111
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005112SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5113 TouchInputMapper(device) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005114 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005115}
5116
5117SingleTouchInputMapper::~SingleTouchInputMapper() {
5118}
5119
Jeff Brown80fd47c2011-05-24 01:07:44 -07005120void SingleTouchInputMapper::clearState() {
Jeff Brown49754db2011-07-01 17:37:58 -07005121 mCursorButtonAccumulator.clearButtons();
5122 mTouchButtonAccumulator.clearButtons();
5123 mSingleTouchMotionAccumulator.clearAbsoluteAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005124}
5125
5126void SingleTouchInputMapper::reset() {
5127 TouchInputMapper::reset();
5128
Jeff Brown80fd47c2011-05-24 01:07:44 -07005129 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005130 }
5131
5132void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07005133 mCursorButtonAccumulator.process(rawEvent);
5134 mTouchButtonAccumulator.process(rawEvent);
5135 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005136
Jeff Brown49754db2011-07-01 17:37:58 -07005137 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
5138 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005139 }
5140}
5141
5142void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005143 mCurrentRawPointerData.clear();
5144 mCurrentButtonState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005145
Jeff Brownd87c6d52011-08-10 14:55:59 -07005146 if (mTouchButtonAccumulator.isToolActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005147 mCurrentRawPointerData.pointerCount = 1;
5148 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005149
Jeff Brownbe1aa822011-07-27 16:04:54 -07005150 bool isHovering = mTouchButtonAccumulator.isHovering()
5151 || mSingleTouchMotionAccumulator.getAbsoluteDistance() > 0;
5152 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005153
Jeff Brownbe1aa822011-07-27 16:04:54 -07005154 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005155 outPointer.id = 0;
5156 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5157 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5158 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5159 outPointer.touchMajor = 0;
5160 outPointer.touchMinor = 0;
5161 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5162 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5163 outPointer.orientation = 0;
5164 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
5165 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5166 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5167 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5168 }
5169 outPointer.isHovering = isHovering;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005170 }
5171
Jeff Brownd87c6d52011-08-10 14:55:59 -07005172 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
5173 | mCursorButtonAccumulator.getButtonState();
5174
Jeff Brown6d0fec22010-07-23 21:28:06 -07005175 syncTouch(when, true);
5176}
5177
Jeff Brownbe1aa822011-07-27 16:04:54 -07005178void SingleTouchInputMapper::configureRawPointerAxes() {
5179 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005180
Jeff Brown49754db2011-07-01 17:37:58 -07005181 mTouchButtonAccumulator.configure(getDevice());
5182
Jeff Brownbe1aa822011-07-27 16:04:54 -07005183 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5184 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5185 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5186 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5187 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005188}
5189
5190
5191// --- MultiTouchInputMapper ---
5192
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005193MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005194 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005195}
5196
5197MultiTouchInputMapper::~MultiTouchInputMapper() {
5198}
5199
Jeff Brown80fd47c2011-05-24 01:07:44 -07005200void MultiTouchInputMapper::clearState() {
Jeff Brown49754db2011-07-01 17:37:58 -07005201 mCursorButtonAccumulator.clearButtons();
5202 mTouchButtonAccumulator.clearButtons();
Jeff Brown6894a292011-07-01 17:59:27 -07005203 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005204
Jeff Brown49754db2011-07-01 17:37:58 -07005205 if (mMultiTouchMotionAccumulator.isUsingSlotsProtocol()) {
Jeff Brown2717eff2011-06-30 23:53:07 -07005206 // Query the driver for the current slot index and use it as the initial slot
5207 // before we start reading events from the device. It is possible that the
5208 // current slot index will not be the same as it was when the first event was
5209 // written into the evdev buffer, which means the input mapper could start
5210 // out of sync with the initial state of the events in the evdev buffer.
5211 // In the extremely unlikely case that this happens, the data from
5212 // two slots will be confused until the next ABS_MT_SLOT event is received.
5213 // This can cause the touch point to "jump", but at least there will be
5214 // no stuck touches.
Jeff Brown49754db2011-07-01 17:37:58 -07005215 int32_t initialSlot;
Jeff Brown2717eff2011-06-30 23:53:07 -07005216 status_t status = getEventHub()->getAbsoluteAxisValue(getDeviceId(), ABS_MT_SLOT,
Jeff Brown49754db2011-07-01 17:37:58 -07005217 &initialSlot);
Jeff Brown2717eff2011-06-30 23:53:07 -07005218 if (status) {
5219 LOGW("Could not retrieve current multitouch slot index. status=%d", status);
Jeff Brown49754db2011-07-01 17:37:58 -07005220 initialSlot = -1;
Jeff Brown2717eff2011-06-30 23:53:07 -07005221 }
Jeff Brown49754db2011-07-01 17:37:58 -07005222 mMultiTouchMotionAccumulator.clearSlots(initialSlot);
5223 } else {
5224 mMultiTouchMotionAccumulator.clearSlots(-1);
Jeff Brown2717eff2011-06-30 23:53:07 -07005225 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005226}
5227
5228void MultiTouchInputMapper::reset() {
5229 TouchInputMapper::reset();
5230
Jeff Brown80fd47c2011-05-24 01:07:44 -07005231 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005232}
5233
5234void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07005235 mCursorButtonAccumulator.process(rawEvent);
5236 mTouchButtonAccumulator.process(rawEvent);
5237 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005238
Jeff Brown49754db2011-07-01 17:37:58 -07005239 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
5240 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005241 }
5242}
5243
5244void MultiTouchInputMapper::sync(nsecs_t when) {
Jeff Brown49754db2011-07-01 17:37:58 -07005245 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005246 size_t outCount = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005247 bool havePointerIds = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005248 BitSet32 newPointerIdBits;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005249
Jeff Brownbe1aa822011-07-27 16:04:54 -07005250 mCurrentRawPointerData.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005251
Jeff Brown80fd47c2011-05-24 01:07:44 -07005252 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005253 const MultiTouchMotionAccumulator::Slot* inSlot =
5254 mMultiTouchMotionAccumulator.getSlot(inIndex);
5255 if (!inSlot->isInUse()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005256 continue;
5257 }
5258
Jeff Brown80fd47c2011-05-24 01:07:44 -07005259 if (outCount >= MAX_POINTERS) {
5260#if DEBUG_POINTERS
5261 LOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
5262 "ignoring the rest.",
5263 getDeviceName().string(), MAX_POINTERS);
5264#endif
5265 break; // too many fingers!
5266 }
5267
Jeff Brownbe1aa822011-07-27 16:04:54 -07005268 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005269 outPointer.x = inSlot->getX();
5270 outPointer.y = inSlot->getY();
5271 outPointer.pressure = inSlot->getPressure();
5272 outPointer.touchMajor = inSlot->getTouchMajor();
5273 outPointer.touchMinor = inSlot->getTouchMinor();
5274 outPointer.toolMajor = inSlot->getToolMajor();
5275 outPointer.toolMinor = inSlot->getToolMinor();
5276 outPointer.orientation = inSlot->getOrientation();
5277 outPointer.distance = inSlot->getDistance();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005278
Jeff Brown49754db2011-07-01 17:37:58 -07005279 outPointer.toolType = inSlot->getToolType();
5280 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5281 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5282 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5283 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5284 }
Jeff Brown8d608662010-08-30 03:02:23 -07005285 }
5286
Jeff Brownbe1aa822011-07-27 16:04:54 -07005287 bool isHovering = mTouchButtonAccumulator.isHovering()
Jeff Brown49754db2011-07-01 17:37:58 -07005288 || inSlot->getDistance() > 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005289 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005290
Jeff Brown8d608662010-08-30 03:02:23 -07005291 // Assign pointer id using tracking id if available.
Jeff Brown6d0fec22010-07-23 21:28:06 -07005292 if (havePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005293 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005294 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005295 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005296 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005297 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005298 if (mPointerTrackingIdMap[n] == trackingId) {
5299 id = n;
5300 }
5301 }
5302
5303 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005304 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005305 mPointerTrackingIdMap[id] = trackingId;
5306 }
5307 }
5308 if (id < 0) {
5309 havePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005310 mCurrentRawPointerData.clearIdBits();
5311 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005312 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005313 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005314 mCurrentRawPointerData.idToIndex[id] = outCount;
5315 mCurrentRawPointerData.markIdBit(id, isHovering);
5316 newPointerIdBits.markBit(id);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005317 }
5318 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005319
Jeff Brown6d0fec22010-07-23 21:28:06 -07005320 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005321 }
5322
Jeff Brownbe1aa822011-07-27 16:04:54 -07005323 mCurrentRawPointerData.pointerCount = outCount;
5324 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
5325 | mCursorButtonAccumulator.getButtonState();
Jeff Brownace13b12011-03-09 17:39:48 -08005326
Jeff Brownbe1aa822011-07-27 16:04:54 -07005327 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005328
Jeff Brown6d0fec22010-07-23 21:28:06 -07005329 syncTouch(when, havePointerIds);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005330
Jeff Brown49754db2011-07-01 17:37:58 -07005331 if (!mMultiTouchMotionAccumulator.isUsingSlotsProtocol()) {
5332 mMultiTouchMotionAccumulator.clearSlots(-1);
Jeff Brown441a9c22011-06-02 18:22:25 -07005333 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005334}
5335
Jeff Brownbe1aa822011-07-27 16:04:54 -07005336void MultiTouchInputMapper::configureRawPointerAxes() {
5337 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005338
Jeff Brown49754db2011-07-01 17:37:58 -07005339 mTouchButtonAccumulator.configure(getDevice());
5340
Jeff Brownbe1aa822011-07-27 16:04:54 -07005341 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5342 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5343 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5344 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5345 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5346 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5347 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
5348 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
5349 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
5350 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
5351 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005352
Jeff Brownbe1aa822011-07-27 16:04:54 -07005353 if (mRawPointerAxes.trackingId.valid
5354 && mRawPointerAxes.slot.valid
5355 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
5356 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07005357 if (slotCount > MAX_SLOTS) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005358 LOGW("MultiTouch Device %s reported %d slots but the framework "
5359 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07005360 getDeviceName().string(), slotCount, MAX_SLOTS);
5361 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005362 }
Jeff Brown49754db2011-07-01 17:37:58 -07005363 mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005364 } else {
Jeff Brown49754db2011-07-01 17:37:58 -07005365 mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005366 }
5367
Jeff Brown2717eff2011-06-30 23:53:07 -07005368 clearState();
Jeff Brown9c3cda02010-06-15 01:31:58 -07005369}
5370
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005371
Jeff Browncb1404e2011-01-15 18:14:15 -08005372// --- JoystickInputMapper ---
5373
5374JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5375 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005376}
5377
5378JoystickInputMapper::~JoystickInputMapper() {
5379}
5380
5381uint32_t JoystickInputMapper::getSources() {
5382 return AINPUT_SOURCE_JOYSTICK;
5383}
5384
5385void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5386 InputMapper::populateDeviceInfo(info);
5387
Jeff Brown6f2fba42011-02-19 01:08:02 -08005388 for (size_t i = 0; i < mAxes.size(); i++) {
5389 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005390 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5391 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005392 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005393 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5394 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005395 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005396 }
5397}
5398
5399void JoystickInputMapper::dump(String8& dump) {
5400 dump.append(INDENT2 "Joystick Input Mapper:\n");
5401
Jeff Brown6f2fba42011-02-19 01:08:02 -08005402 dump.append(INDENT3 "Axes:\n");
5403 size_t numAxes = mAxes.size();
5404 for (size_t i = 0; i < numAxes; i++) {
5405 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005406 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005407 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005408 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005409 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005410 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005411 }
Jeff Brown85297452011-03-04 13:07:49 -08005412 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5413 label = getAxisLabel(axis.axisInfo.highAxis);
5414 if (label) {
5415 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5416 } else {
5417 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5418 axis.axisInfo.splitValue);
5419 }
5420 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5421 dump.append(" (invert)");
5422 }
5423
5424 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5425 axis.min, axis.max, axis.flat, axis.fuzz);
5426 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5427 "highScale=%0.5f, highOffset=%0.5f\n",
5428 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005429 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5430 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005431 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07005432 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08005433 }
5434}
5435
Jeff Brown474dcb52011-06-14 20:22:50 -07005436void JoystickInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
5437 InputMapper::configure(config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08005438
Jeff Brown474dcb52011-06-14 20:22:50 -07005439 if (!changes) { // first time only
5440 // Collect all axes.
5441 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
5442 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005443 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07005444 if (rawAxisInfo.valid) {
5445 // Map axis.
5446 AxisInfo axisInfo;
5447 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
5448 if (!explicitlyMapped) {
5449 // Axis is not explicitly mapped, will choose a generic axis later.
5450 axisInfo.mode = AxisInfo::MODE_NORMAL;
5451 axisInfo.axis = -1;
5452 }
5453
5454 // Apply flat override.
5455 int32_t rawFlat = axisInfo.flatOverride < 0
5456 ? rawAxisInfo.flat : axisInfo.flatOverride;
5457
5458 // Calculate scaling factors and limits.
5459 Axis axis;
5460 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5461 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5462 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5463 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5464 scale, 0.0f, highScale, 0.0f,
5465 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5466 } else if (isCenteredAxis(axisInfo.axis)) {
5467 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5468 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
5469 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5470 scale, offset, scale, offset,
5471 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5472 } else {
5473 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5474 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5475 scale, 0.0f, scale, 0.0f,
5476 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5477 }
5478
5479 // To eliminate noise while the joystick is at rest, filter out small variations
5480 // in axis values up front.
5481 axis.filter = axis.flat * 0.25f;
5482
5483 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005484 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005485 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005486
Jeff Brown474dcb52011-06-14 20:22:50 -07005487 // If there are too many axes, start dropping them.
5488 // Prefer to keep explicitly mapped axes.
5489 if (mAxes.size() > PointerCoords::MAX_AXES) {
5490 LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
5491 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5492 pruneAxes(true);
5493 pruneAxes(false);
5494 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005495
Jeff Brown474dcb52011-06-14 20:22:50 -07005496 // Assign generic axis ids to remaining axes.
5497 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5498 size_t numAxes = mAxes.size();
5499 for (size_t i = 0; i < numAxes; i++) {
5500 Axis& axis = mAxes.editValueAt(i);
5501 if (axis.axisInfo.axis < 0) {
5502 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5503 && haveAxis(nextGenericAxisId)) {
5504 nextGenericAxisId += 1;
5505 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005506
Jeff Brown474dcb52011-06-14 20:22:50 -07005507 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
5508 axis.axisInfo.axis = nextGenericAxisId;
5509 nextGenericAxisId += 1;
5510 } else {
5511 LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
5512 "have already been assigned to other axes.",
5513 getDeviceName().string(), mAxes.keyAt(i));
5514 mAxes.removeItemsAt(i--);
5515 numAxes -= 1;
5516 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005517 }
5518 }
5519 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005520}
5521
Jeff Brown85297452011-03-04 13:07:49 -08005522bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005523 size_t numAxes = mAxes.size();
5524 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005525 const Axis& axis = mAxes.valueAt(i);
5526 if (axis.axisInfo.axis == axisId
5527 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5528 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005529 return true;
5530 }
5531 }
5532 return false;
5533}
Jeff Browncb1404e2011-01-15 18:14:15 -08005534
Jeff Brown6f2fba42011-02-19 01:08:02 -08005535void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5536 size_t i = mAxes.size();
5537 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5538 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5539 continue;
5540 }
5541 LOGI("Discarding joystick '%s' axis %d because there are too many axes.",
5542 getDeviceName().string(), mAxes.keyAt(i));
5543 mAxes.removeItemsAt(i);
5544 }
5545}
5546
5547bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5548 switch (axis) {
5549 case AMOTION_EVENT_AXIS_X:
5550 case AMOTION_EVENT_AXIS_Y:
5551 case AMOTION_EVENT_AXIS_Z:
5552 case AMOTION_EVENT_AXIS_RX:
5553 case AMOTION_EVENT_AXIS_RY:
5554 case AMOTION_EVENT_AXIS_RZ:
5555 case AMOTION_EVENT_AXIS_HAT_X:
5556 case AMOTION_EVENT_AXIS_HAT_Y:
5557 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005558 case AMOTION_EVENT_AXIS_RUDDER:
5559 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005560 return true;
5561 default:
5562 return false;
5563 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005564}
5565
5566void JoystickInputMapper::reset() {
5567 // Recenter all axes.
5568 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Browncb1404e2011-01-15 18:14:15 -08005569
Jeff Brown6f2fba42011-02-19 01:08:02 -08005570 size_t numAxes = mAxes.size();
5571 for (size_t i = 0; i < numAxes; i++) {
5572 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005573 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005574 }
5575
5576 sync(when, true /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005577
5578 InputMapper::reset();
5579}
5580
5581void JoystickInputMapper::process(const RawEvent* rawEvent) {
5582 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005583 case EV_ABS: {
5584 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
5585 if (index >= 0) {
5586 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005587 float newValue, highNewValue;
5588 switch (axis.axisInfo.mode) {
5589 case AxisInfo::MODE_INVERT:
5590 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5591 * axis.scale + axis.offset;
5592 highNewValue = 0.0f;
5593 break;
5594 case AxisInfo::MODE_SPLIT:
5595 if (rawEvent->value < axis.axisInfo.splitValue) {
5596 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5597 * axis.scale + axis.offset;
5598 highNewValue = 0.0f;
5599 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5600 newValue = 0.0f;
5601 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5602 * axis.highScale + axis.highOffset;
5603 } else {
5604 newValue = 0.0f;
5605 highNewValue = 0.0f;
5606 }
5607 break;
5608 default:
5609 newValue = rawEvent->value * axis.scale + axis.offset;
5610 highNewValue = 0.0f;
5611 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005612 }
Jeff Brown85297452011-03-04 13:07:49 -08005613 axis.newValue = newValue;
5614 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005615 }
5616 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005617 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005618
5619 case EV_SYN:
5620 switch (rawEvent->scanCode) {
5621 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005622 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005623 break;
5624 }
5625 break;
5626 }
5627}
5628
Jeff Brown6f2fba42011-02-19 01:08:02 -08005629void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005630 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005631 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005632 }
5633
5634 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005635 int32_t buttonState = 0;
5636
5637 PointerProperties pointerProperties;
5638 pointerProperties.clear();
5639 pointerProperties.id = 0;
5640 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08005641
Jeff Brown6f2fba42011-02-19 01:08:02 -08005642 PointerCoords pointerCoords;
5643 pointerCoords.clear();
5644
5645 size_t numAxes = mAxes.size();
5646 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005647 const Axis& axis = mAxes.valueAt(i);
5648 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
5649 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5650 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
5651 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005652 }
5653
Jeff Brown56194eb2011-03-02 19:23:13 -08005654 // Moving a joystick axis should not wake the devide because joysticks can
5655 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
5656 // button will likely wake the device.
5657 // TODO: Use the input device configuration to control this behavior more finely.
5658 uint32_t policyFlags = 0;
5659
Jeff Brownbe1aa822011-07-27 16:04:54 -07005660 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005661 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
5662 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005663 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08005664}
5665
Jeff Brown85297452011-03-04 13:07:49 -08005666bool JoystickInputMapper::filterAxes(bool force) {
5667 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005668 size_t numAxes = mAxes.size();
5669 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005670 Axis& axis = mAxes.editValueAt(i);
5671 if (force || hasValueChangedSignificantly(axis.filter,
5672 axis.newValue, axis.currentValue, axis.min, axis.max)) {
5673 axis.currentValue = axis.newValue;
5674 atLeastOneSignificantChange = true;
5675 }
5676 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5677 if (force || hasValueChangedSignificantly(axis.filter,
5678 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
5679 axis.highCurrentValue = axis.highNewValue;
5680 atLeastOneSignificantChange = true;
5681 }
5682 }
5683 }
5684 return atLeastOneSignificantChange;
5685}
5686
5687bool JoystickInputMapper::hasValueChangedSignificantly(
5688 float filter, float newValue, float currentValue, float min, float max) {
5689 if (newValue != currentValue) {
5690 // Filter out small changes in value unless the value is converging on the axis
5691 // bounds or center point. This is intended to reduce the amount of information
5692 // sent to applications by particularly noisy joysticks (such as PS3).
5693 if (fabs(newValue - currentValue) > filter
5694 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
5695 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
5696 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
5697 return true;
5698 }
5699 }
5700 return false;
5701}
5702
5703bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
5704 float filter, float newValue, float currentValue, float thresholdValue) {
5705 float newDistance = fabs(newValue - thresholdValue);
5706 if (newDistance < filter) {
5707 float oldDistance = fabs(currentValue - thresholdValue);
5708 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005709 return true;
5710 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005711 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005712 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005713}
5714
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005715} // namespace android