blob: 8786c24da624ce84949846d73f08cb6de6ea7864 [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Brown46b9ac02010-04-22 18:58:52 -070017#define LOG_TAG "InputReader"
18
19//#define LOG_NDEBUG 0
20
21// Log debug messages for each raw event received from the EventHub.
22#define DEBUG_RAW_EVENTS 0
23
24// Log debug messages about touch screen filtering hacks.
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_HACKS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070026
27// Log debug messages about virtual key processing.
Jeff Brown349703e2010-06-22 01:27:15 -070028#define DEBUG_VIRTUAL_KEYS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070029
30// Log debug messages about pointers.
Jeff Brown9f2106f2011-05-24 14:40:35 -070031#define DEBUG_POINTERS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070032
Jeff Brown5c225b12010-06-16 01:53:36 -070033// Log debug messages about pointer assignment calculations.
34#define DEBUG_POINTER_ASSIGNMENT 0
35
Jeff 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 Brown46b9ac02010-04-22 18:58:52 -070041#include <cutils/log.h>
Jeff Brown6b53e8d2010-11-10 16:03:06 -080042#include <ui/Keyboard.h>
Jeff Brown90655042010-12-02 13:50:46 -080043#include <ui/VirtualKeyMap.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070044
45#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070046#include <stdlib.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070047#include <unistd.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070048#include <errno.h>
49#include <limits.h>
Jeff Brownc5ed5912010-07-14 18:48:53 -070050#include <math.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070051
Jeff Brown8d608662010-08-30 03:02:23 -070052#define INDENT " "
Jeff Brownef3d7e82010-09-30 14:33:04 -070053#define INDENT2 " "
54#define INDENT3 " "
55#define INDENT4 " "
Jeff Brownaba321a2011-06-28 20:34:40 -070056#define INDENT5 " "
Jeff Brown8d608662010-08-30 03:02:23 -070057
Jeff Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brownfd035822010-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 Brown46b9ac02010-04-22 18:58:52 -0700119};
Jeff Brown9626b142011-03-03 02:09:54 -0800120static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac02010-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 Brown46b9ac02010-04-22 18:58:52 -0700200
Jeff Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-04-22 18:58:52 -0700330 return;
331 }
Jeff Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-04-22 18:58:52 -0700361
Jeff Brown56194eb2011-03-02 19:23:13 -0800362 // External devices.
363 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
364 device->setExternal(true);
365 }
366
Jeff Brown6d0fec22010-07-23 21:28:06 -0700367 // Switch-like devices.
368 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
369 device->addMapper(new SwitchInputMapper(device));
370 }
371
372 // Keyboard-like devices.
Jeff Brownefd32662011-03-08 15:13:06 -0800373 uint32_t keyboardSource = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700374 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
375 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800376 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700377 }
378 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
379 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
380 }
381 if (classes & INPUT_DEVICE_CLASS_DPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800382 keyboardSource |= AINPUT_SOURCE_DPAD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700383 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800384 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800385 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
Jeff Browncb1404e2011-01-15 18:14:15 -0800386 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700387
Jeff Brownefd32662011-03-08 15:13:06 -0800388 if (keyboardSource != 0) {
389 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700390 }
391
Jeff Brown83c09682010-12-23 17:50:18 -0800392 // Cursor-like devices.
393 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
394 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700395 }
396
Jeff Brown58a2da82011-01-25 16:02:22 -0800397 // Touchscreens and touchpad devices.
398 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800399 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800400 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800401 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700402 }
403
Jeff Browncb1404e2011-01-15 18:14:15 -0800404 // Joystick-like devices.
405 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
406 device->addMapper(new JoystickInputMapper(device));
407 }
408
Jeff Brown6d0fec22010-07-23 21:28:06 -0700409 return device;
410}
411
Jeff 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 Brown46b9ac02010-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 Brownf2f48712010-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 Brown46b9ac02010-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 Brown46b9ac02010-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 Brown46b9ac02010-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
1173bool TouchButtonAccumulator::isActive() const {
1174 return mBtnTouch || mBtnToolFinger || mBtnToolPen
1175 || mBtnToolRubber || mBtnStylus || mBtnStylus2;
1176}
1177
1178bool TouchButtonAccumulator::isHovering() const {
1179 return mHaveBtnTouch && !mBtnTouch;
1180}
1181
1182
Jeff Brownbe1aa822011-07-27 16:04:54 -07001183// --- RawPointerAxes ---
1184
1185RawPointerAxes::RawPointerAxes() {
1186 clear();
1187}
1188
1189void RawPointerAxes::clear() {
1190 x.clear();
1191 y.clear();
1192 pressure.clear();
1193 touchMajor.clear();
1194 touchMinor.clear();
1195 toolMajor.clear();
1196 toolMinor.clear();
1197 orientation.clear();
1198 distance.clear();
1199 trackingId.clear();
1200 slot.clear();
1201}
1202
1203
1204// --- RawPointerData ---
1205
1206RawPointerData::RawPointerData() {
1207 clear();
1208}
1209
1210void RawPointerData::clear() {
1211 pointerCount = 0;
1212 clearIdBits();
1213}
1214
1215void RawPointerData::copyFrom(const RawPointerData& other) {
1216 pointerCount = other.pointerCount;
1217 hoveringIdBits = other.hoveringIdBits;
1218 touchingIdBits = other.touchingIdBits;
1219
1220 for (uint32_t i = 0; i < pointerCount; i++) {
1221 pointers[i] = other.pointers[i];
1222
1223 int id = pointers[i].id;
1224 idToIndex[id] = other.idToIndex[id];
1225 }
1226}
1227
1228void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
1229 float x = 0, y = 0;
1230 uint32_t count = touchingIdBits.count();
1231 if (count) {
1232 for (BitSet32 idBits(touchingIdBits); !idBits.isEmpty(); ) {
1233 uint32_t id = idBits.clearFirstMarkedBit();
1234 const Pointer& pointer = pointerForId(id);
1235 x += pointer.x;
1236 y += pointer.y;
1237 }
1238 x /= count;
1239 y /= count;
1240 }
1241 *outX = x;
1242 *outY = y;
1243}
1244
1245
1246// --- CookedPointerData ---
1247
1248CookedPointerData::CookedPointerData() {
1249 clear();
1250}
1251
1252void CookedPointerData::clear() {
1253 pointerCount = 0;
1254 hoveringIdBits.clear();
1255 touchingIdBits.clear();
1256}
1257
1258void CookedPointerData::copyFrom(const CookedPointerData& other) {
1259 pointerCount = other.pointerCount;
1260 hoveringIdBits = other.hoveringIdBits;
1261 touchingIdBits = other.touchingIdBits;
1262
1263 for (uint32_t i = 0; i < pointerCount; i++) {
1264 pointerProperties[i].copyFrom(other.pointerProperties[i]);
1265 pointerCoords[i].copyFrom(other.pointerCoords[i]);
1266
1267 int id = pointerProperties[i].id;
1268 idToIndex[id] = other.idToIndex[id];
1269 }
1270}
1271
1272
Jeff Brown49754db2011-07-01 17:37:58 -07001273// --- SingleTouchMotionAccumulator ---
1274
1275SingleTouchMotionAccumulator::SingleTouchMotionAccumulator() {
1276 clearAbsoluteAxes();
1277}
1278
1279void SingleTouchMotionAccumulator::clearAbsoluteAxes() {
1280 mAbsX = 0;
1281 mAbsY = 0;
1282 mAbsPressure = 0;
1283 mAbsToolWidth = 0;
1284 mAbsDistance = 0;
1285}
1286
1287void SingleTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1288 if (rawEvent->type == EV_ABS) {
1289 switch (rawEvent->scanCode) {
1290 case ABS_X:
1291 mAbsX = rawEvent->value;
1292 break;
1293 case ABS_Y:
1294 mAbsY = rawEvent->value;
1295 break;
1296 case ABS_PRESSURE:
1297 mAbsPressure = rawEvent->value;
1298 break;
1299 case ABS_TOOL_WIDTH:
1300 mAbsToolWidth = rawEvent->value;
1301 break;
1302 case ABS_DISTANCE:
1303 mAbsDistance = rawEvent->value;
1304 break;
1305 }
1306 }
1307}
1308
1309
1310// --- MultiTouchMotionAccumulator ---
1311
1312MultiTouchMotionAccumulator::MultiTouchMotionAccumulator() :
1313 mCurrentSlot(-1), mSlots(NULL), mSlotCount(0), mUsingSlotsProtocol(false) {
1314}
1315
1316MultiTouchMotionAccumulator::~MultiTouchMotionAccumulator() {
1317 delete[] mSlots;
1318}
1319
1320void MultiTouchMotionAccumulator::configure(size_t slotCount, bool usingSlotsProtocol) {
1321 mSlotCount = slotCount;
1322 mUsingSlotsProtocol = usingSlotsProtocol;
1323
1324 delete[] mSlots;
1325 mSlots = new Slot[slotCount];
1326}
1327
1328void MultiTouchMotionAccumulator::clearSlots(int32_t initialSlot) {
1329 for (size_t i = 0; i < mSlotCount; i++) {
1330 mSlots[i].clearIfInUse();
1331 }
1332 mCurrentSlot = initialSlot;
1333}
1334
1335void MultiTouchMotionAccumulator::process(const RawEvent* rawEvent) {
1336 if (rawEvent->type == EV_ABS) {
1337 bool newSlot = false;
1338 if (mUsingSlotsProtocol) {
1339 if (rawEvent->scanCode == ABS_MT_SLOT) {
1340 mCurrentSlot = rawEvent->value;
1341 newSlot = true;
1342 }
1343 } else if (mCurrentSlot < 0) {
1344 mCurrentSlot = 0;
1345 }
1346
1347 if (mCurrentSlot < 0 || size_t(mCurrentSlot) >= mSlotCount) {
1348#if DEBUG_POINTERS
1349 if (newSlot) {
1350 LOGW("MultiTouch device emitted invalid slot index %d but it "
1351 "should be between 0 and %d; ignoring this slot.",
1352 mCurrentSlot, mSlotCount - 1);
1353 }
1354#endif
1355 } else {
1356 Slot* slot = &mSlots[mCurrentSlot];
1357
1358 switch (rawEvent->scanCode) {
1359 case ABS_MT_POSITION_X:
1360 slot->mInUse = true;
1361 slot->mAbsMTPositionX = rawEvent->value;
1362 break;
1363 case ABS_MT_POSITION_Y:
1364 slot->mInUse = true;
1365 slot->mAbsMTPositionY = rawEvent->value;
1366 break;
1367 case ABS_MT_TOUCH_MAJOR:
1368 slot->mInUse = true;
1369 slot->mAbsMTTouchMajor = rawEvent->value;
1370 break;
1371 case ABS_MT_TOUCH_MINOR:
1372 slot->mInUse = true;
1373 slot->mAbsMTTouchMinor = rawEvent->value;
1374 slot->mHaveAbsMTTouchMinor = true;
1375 break;
1376 case ABS_MT_WIDTH_MAJOR:
1377 slot->mInUse = true;
1378 slot->mAbsMTWidthMajor = rawEvent->value;
1379 break;
1380 case ABS_MT_WIDTH_MINOR:
1381 slot->mInUse = true;
1382 slot->mAbsMTWidthMinor = rawEvent->value;
1383 slot->mHaveAbsMTWidthMinor = true;
1384 break;
1385 case ABS_MT_ORIENTATION:
1386 slot->mInUse = true;
1387 slot->mAbsMTOrientation = rawEvent->value;
1388 break;
1389 case ABS_MT_TRACKING_ID:
1390 if (mUsingSlotsProtocol && rawEvent->value < 0) {
1391 slot->clearIfInUse();
1392 } else {
1393 slot->mInUse = true;
1394 slot->mAbsMTTrackingId = rawEvent->value;
1395 }
1396 break;
1397 case ABS_MT_PRESSURE:
1398 slot->mInUse = true;
1399 slot->mAbsMTPressure = rawEvent->value;
1400 break;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001401 case ABS_MT_DISTANCE:
1402 slot->mInUse = true;
1403 slot->mAbsMTDistance = rawEvent->value;
1404 break;
Jeff Brown49754db2011-07-01 17:37:58 -07001405 case ABS_MT_TOOL_TYPE:
1406 slot->mInUse = true;
1407 slot->mAbsMTToolType = rawEvent->value;
1408 slot->mHaveAbsMTToolType = true;
1409 break;
1410 }
1411 }
1412 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_MT_REPORT) {
1413 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
1414 mCurrentSlot += 1;
1415 }
1416}
1417
1418
1419// --- MultiTouchMotionAccumulator::Slot ---
1420
1421MultiTouchMotionAccumulator::Slot::Slot() {
1422 clear();
1423}
1424
1425void MultiTouchMotionAccumulator::Slot::clearIfInUse() {
1426 if (mInUse) {
1427 clear();
1428 }
1429}
1430
1431void MultiTouchMotionAccumulator::Slot::clear() {
1432 mInUse = false;
1433 mHaveAbsMTTouchMinor = false;
1434 mHaveAbsMTWidthMinor = false;
1435 mHaveAbsMTToolType = false;
1436 mAbsMTPositionX = 0;
1437 mAbsMTPositionY = 0;
1438 mAbsMTTouchMajor = 0;
1439 mAbsMTTouchMinor = 0;
1440 mAbsMTWidthMajor = 0;
1441 mAbsMTWidthMinor = 0;
1442 mAbsMTOrientation = 0;
1443 mAbsMTTrackingId = -1;
1444 mAbsMTPressure = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001445 mAbsMTDistance = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07001446 mAbsMTToolType = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07001447}
1448
1449int32_t MultiTouchMotionAccumulator::Slot::getToolType() const {
1450 if (mHaveAbsMTToolType) {
1451 switch (mAbsMTToolType) {
1452 case MT_TOOL_FINGER:
1453 return AMOTION_EVENT_TOOL_TYPE_FINGER;
1454 case MT_TOOL_PEN:
1455 return AMOTION_EVENT_TOOL_TYPE_STYLUS;
1456 }
1457 }
1458 return AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
1459}
1460
1461
Jeff Brown6d0fec22010-07-23 21:28:06 -07001462// --- InputMapper ---
1463
1464InputMapper::InputMapper(InputDevice* device) :
1465 mDevice(device), mContext(device->getContext()) {
1466}
1467
1468InputMapper::~InputMapper() {
1469}
1470
1471void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1472 info->addSource(getSources());
1473}
1474
Jeff Brownef3d7e82010-09-30 14:33:04 -07001475void InputMapper::dump(String8& dump) {
1476}
1477
Jeff Brown474dcb52011-06-14 20:22:50 -07001478void InputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001479}
1480
1481void InputMapper::reset() {
1482}
1483
Jeff Brownaa3855d2011-03-17 01:34:19 -07001484void InputMapper::timeoutExpired(nsecs_t when) {
1485}
1486
Jeff Brown6d0fec22010-07-23 21:28:06 -07001487int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1488 return AKEY_STATE_UNKNOWN;
1489}
1490
1491int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1492 return AKEY_STATE_UNKNOWN;
1493}
1494
1495int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1496 return AKEY_STATE_UNKNOWN;
1497}
1498
1499bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1500 const int32_t* keyCodes, uint8_t* outFlags) {
1501 return false;
1502}
1503
1504int32_t InputMapper::getMetaState() {
1505 return 0;
1506}
1507
Jeff Brown05dc66a2011-03-02 14:41:58 -08001508void InputMapper::fadePointer() {
1509}
1510
Jeff Brownbe1aa822011-07-27 16:04:54 -07001511status_t InputMapper::getAbsoluteAxisInfo(int32_t axis, RawAbsoluteAxisInfo* axisInfo) {
1512 return getEventHub()->getAbsoluteAxisInfo(getDeviceId(), axis, axisInfo);
1513}
1514
Jeff Browncb1404e2011-01-15 18:14:15 -08001515void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1516 const RawAbsoluteAxisInfo& axis, const char* name) {
1517 if (axis.valid) {
Jeff Brownb3a2d132011-06-12 18:14:50 -07001518 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d, resolution=%d\n",
1519 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz, axis.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08001520 } else {
1521 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1522 }
1523}
1524
Jeff Brown6d0fec22010-07-23 21:28:06 -07001525
1526// --- SwitchInputMapper ---
1527
1528SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1529 InputMapper(device) {
1530}
1531
1532SwitchInputMapper::~SwitchInputMapper() {
1533}
1534
1535uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001536 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001537}
1538
1539void SwitchInputMapper::process(const RawEvent* rawEvent) {
1540 switch (rawEvent->type) {
1541 case EV_SW:
1542 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
1543 break;
1544 }
1545}
1546
1547void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001548 NotifySwitchArgs args(when, 0, switchCode, switchValue);
1549 getListener()->notifySwitch(&args);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001550}
1551
1552int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1553 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1554}
1555
1556
1557// --- KeyboardInputMapper ---
1558
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001559KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001560 uint32_t source, int32_t keyboardType) :
1561 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001562 mKeyboardType(keyboardType) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001563 initialize();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001564}
1565
1566KeyboardInputMapper::~KeyboardInputMapper() {
1567}
1568
Jeff Brownbe1aa822011-07-27 16:04:54 -07001569void KeyboardInputMapper::initialize() {
1570 mMetaState = AMETA_NONE;
1571 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001572}
1573
1574uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001575 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001576}
1577
1578void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1579 InputMapper::populateDeviceInfo(info);
1580
1581 info->setKeyboardType(mKeyboardType);
1582}
1583
Jeff Brownef3d7e82010-09-30 14:33:04 -07001584void KeyboardInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001585 dump.append(INDENT2 "Keyboard Input Mapper:\n");
1586 dumpParameters(dump);
1587 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
1588 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mKeyDowns.size());
1589 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mMetaState);
1590 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001591}
1592
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001593
Jeff Brown474dcb52011-06-14 20:22:50 -07001594void KeyboardInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
1595 InputMapper::configure(config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001596
Jeff Brown474dcb52011-06-14 20:22:50 -07001597 if (!changes) { // first time only
1598 // Configure basic parameters.
1599 configureParameters();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001600
Jeff Brown474dcb52011-06-14 20:22:50 -07001601 // Reset LEDs.
Jeff Brownbe1aa822011-07-27 16:04:54 -07001602 resetLedState();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001603 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001604}
1605
1606void KeyboardInputMapper::configureParameters() {
1607 mParameters.orientationAware = false;
1608 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
1609 mParameters.orientationAware);
1610
Jeff Brownbc68a592011-07-25 12:58:12 -07001611 mParameters.associatedDisplayId = -1;
1612 if (mParameters.orientationAware) {
1613 mParameters.associatedDisplayId = 0;
1614 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001615}
1616
1617void KeyboardInputMapper::dumpParameters(String8& dump) {
1618 dump.append(INDENT3 "Parameters:\n");
1619 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1620 mParameters.associatedDisplayId);
1621 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1622 toString(mParameters.orientationAware));
1623}
1624
Jeff Brown6d0fec22010-07-23 21:28:06 -07001625void KeyboardInputMapper::reset() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001626 // Synthesize key up event on reset if keys are currently down.
1627 while (!mKeyDowns.isEmpty()) {
1628 const KeyDown& keyDown = mKeyDowns.top();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001629 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001630 processKey(when, false, keyDown.keyCode, keyDown.scanCode, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001631 }
1632
Jeff Brownbe1aa822011-07-27 16:04:54 -07001633 initialize();
1634 resetLedState();
1635
Jeff Brown6d0fec22010-07-23 21:28:06 -07001636 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001637 getContext()->updateGlobalMetaState();
1638}
1639
1640void KeyboardInputMapper::process(const RawEvent* rawEvent) {
1641 switch (rawEvent->type) {
1642 case EV_KEY: {
1643 int32_t scanCode = rawEvent->scanCode;
1644 if (isKeyboardOrGamepadKey(scanCode)) {
1645 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
1646 rawEvent->flags);
1647 }
1648 break;
1649 }
1650 }
1651}
1652
1653bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
1654 return scanCode < BTN_MOUSE
1655 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001656 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08001657 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001658}
1659
Jeff Brown6328cdc2010-07-29 18:18:33 -07001660void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
1661 int32_t scanCode, uint32_t policyFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001662
Jeff Brownbe1aa822011-07-27 16:04:54 -07001663 if (down) {
1664 // Rotate key codes according to orientation if needed.
1665 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1666 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
1667 int32_t orientation;
1668 if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1669 false /*external*/, NULL, NULL, & orientation)) {
1670 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001671 }
1672
Jeff Brownbe1aa822011-07-27 16:04:54 -07001673 keyCode = rotateKeyCode(keyCode, orientation);
1674 }
Jeff Brownfe508922011-01-18 15:10:10 -08001675
Jeff Brownbe1aa822011-07-27 16:04:54 -07001676 // Add key down.
1677 ssize_t keyDownIndex = findKeyDown(scanCode);
1678 if (keyDownIndex >= 0) {
1679 // key repeat, be sure to use same keycode as before in case of rotation
1680 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001681 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001682 // key down
1683 if ((policyFlags & POLICY_FLAG_VIRTUAL)
1684 && mContext->shouldDropVirtualKey(when,
1685 getDevice(), keyCode, scanCode)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001686 return;
1687 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001688
1689 mKeyDowns.push();
1690 KeyDown& keyDown = mKeyDowns.editTop();
1691 keyDown.keyCode = keyCode;
1692 keyDown.scanCode = scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001693 }
1694
Jeff Brownbe1aa822011-07-27 16:04:54 -07001695 mDownTime = when;
1696 } else {
1697 // Remove key down.
1698 ssize_t keyDownIndex = findKeyDown(scanCode);
1699 if (keyDownIndex >= 0) {
1700 // key up, be sure to use same keycode as before in case of rotation
1701 keyCode = mKeyDowns.itemAt(keyDownIndex).keyCode;
1702 mKeyDowns.removeAt(size_t(keyDownIndex));
1703 } else {
1704 // key was not actually down
1705 LOGI("Dropping key up from device %s because the key was not down. "
1706 "keyCode=%d, scanCode=%d",
1707 getDeviceName().string(), keyCode, scanCode);
1708 return;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001709 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07001710 }
Jeff Brownfd035822010-06-30 16:10:35 -07001711
Jeff Brownbe1aa822011-07-27 16:04:54 -07001712 bool metaStateChanged = false;
1713 int32_t oldMetaState = mMetaState;
1714 int32_t newMetaState = updateMetaState(keyCode, down, oldMetaState);
1715 if (oldMetaState != newMetaState) {
1716 mMetaState = newMetaState;
1717 metaStateChanged = true;
1718 updateLedState(false);
1719 }
1720
1721 nsecs_t downTime = mDownTime;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001722
Jeff Brown56194eb2011-03-02 19:23:13 -08001723 // Key down on external an keyboard should wake the device.
1724 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
1725 // For internal keyboards, the key layout file should specify the policy flags for
1726 // each wake key individually.
1727 // TODO: Use the input device configuration to control this behavior more finely.
1728 if (down && getDevice()->isExternal()
1729 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1730 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1731 }
1732
Jeff Brown6328cdc2010-07-29 18:18:33 -07001733 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001734 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001735 }
1736
Jeff Brown05dc66a2011-03-02 14:41:58 -08001737 if (down && !isMetaKey(keyCode)) {
1738 getContext()->fadePointer();
1739 }
1740
Jeff Brownbe1aa822011-07-27 16:04:54 -07001741 NotifyKeyArgs args(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001742 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1743 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001744 getListener()->notifyKey(&args);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001745}
1746
Jeff Brownbe1aa822011-07-27 16:04:54 -07001747ssize_t KeyboardInputMapper::findKeyDown(int32_t scanCode) {
1748 size_t n = mKeyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001749 for (size_t i = 0; i < n; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001750 if (mKeyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001751 return i;
1752 }
1753 }
1754 return -1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001755}
1756
Jeff Brown6d0fec22010-07-23 21:28:06 -07001757int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1758 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1759}
Jeff Brown46b9ac02010-04-22 18:58:52 -07001760
Jeff Brown6d0fec22010-07-23 21:28:06 -07001761int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1762 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1763}
Jeff Brown46b9ac02010-04-22 18:58:52 -07001764
Jeff Brown6d0fec22010-07-23 21:28:06 -07001765bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1766 const int32_t* keyCodes, uint8_t* outFlags) {
1767 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1768}
1769
1770int32_t KeyboardInputMapper::getMetaState() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001771 return mMetaState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001772}
1773
Jeff Brownbe1aa822011-07-27 16:04:54 -07001774void KeyboardInputMapper::resetLedState() {
1775 initializeLedState(mCapsLockLedState, LED_CAPSL);
1776 initializeLedState(mNumLockLedState, LED_NUML);
1777 initializeLedState(mScrollLockLedState, LED_SCROLLL);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001778
Jeff Brownbe1aa822011-07-27 16:04:54 -07001779 updateLedState(true);
Jeff Brown49ed71d2010-12-06 17:13:33 -08001780}
1781
Jeff Brownbe1aa822011-07-27 16:04:54 -07001782void KeyboardInputMapper::initializeLedState(LedState& ledState, int32_t led) {
Jeff Brown49ed71d2010-12-06 17:13:33 -08001783 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
1784 ledState.on = false;
1785}
1786
Jeff Brownbe1aa822011-07-27 16:04:54 -07001787void KeyboardInputMapper::updateLedState(bool reset) {
1788 updateLedStateForModifier(mCapsLockLedState, LED_CAPSL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001789 AMETA_CAPS_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001790 updateLedStateForModifier(mNumLockLedState, LED_NUML,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001791 AMETA_NUM_LOCK_ON, reset);
Jeff Brownbe1aa822011-07-27 16:04:54 -07001792 updateLedStateForModifier(mScrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001793 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001794}
1795
Jeff Brownbe1aa822011-07-27 16:04:54 -07001796void KeyboardInputMapper::updateLedStateForModifier(LedState& ledState,
Jeff Brown497a92c2010-09-12 17:55:08 -07001797 int32_t led, int32_t modifier, bool reset) {
1798 if (ledState.avail) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001799 bool desiredState = (mMetaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001800 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001801 getEventHub()->setLedState(getDeviceId(), led, desiredState);
1802 ledState.on = desiredState;
1803 }
1804 }
1805}
1806
Jeff Brown6d0fec22010-07-23 21:28:06 -07001807
Jeff Brown83c09682010-12-23 17:50:18 -08001808// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07001809
Jeff Brown83c09682010-12-23 17:50:18 -08001810CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001811 InputMapper(device) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001812 initialize();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001813}
1814
Jeff Brown83c09682010-12-23 17:50:18 -08001815CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001816}
1817
Jeff Brown83c09682010-12-23 17:50:18 -08001818uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001819 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001820}
1821
Jeff Brown83c09682010-12-23 17:50:18 -08001822void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001823 InputMapper::populateDeviceInfo(info);
1824
Jeff Brown83c09682010-12-23 17:50:18 -08001825 if (mParameters.mode == Parameters::MODE_POINTER) {
1826 float minX, minY, maxX, maxY;
1827 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08001828 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
1829 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08001830 }
1831 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08001832 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
1833 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08001834 }
Jeff Brownefd32662011-03-08 15:13:06 -08001835 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001836
Jeff Brown49754db2011-07-01 17:37:58 -07001837 if (mCursorMotionAccumulator.haveRelativeVWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08001838 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001839 }
Jeff Brown49754db2011-07-01 17:37:58 -07001840 if (mCursorMotionAccumulator.haveRelativeHWheel()) {
Jeff Brownefd32662011-03-08 15:13:06 -08001841 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001842 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001843}
1844
Jeff Brown83c09682010-12-23 17:50:18 -08001845void CursorInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001846 dump.append(INDENT2 "Cursor Input Mapper:\n");
1847 dumpParameters(dump);
1848 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
1849 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
1850 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1851 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
1852 dump.appendFormat(INDENT3 "HaveVWheel: %s\n",
1853 toString(mCursorMotionAccumulator.haveRelativeVWheel()));
1854 dump.appendFormat(INDENT3 "HaveHWheel: %s\n",
1855 toString(mCursorMotionAccumulator.haveRelativeHWheel()));
1856 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
1857 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
1858 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mButtonState);
1859 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mButtonState)));
1860 dump.appendFormat(INDENT3 "DownTime: %lld\n", mDownTime);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001861}
1862
Jeff Brown474dcb52011-06-14 20:22:50 -07001863void CursorInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
1864 InputMapper::configure(config, changes);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001865
Jeff Brown474dcb52011-06-14 20:22:50 -07001866 if (!changes) { // first time only
Jeff Brown49754db2011-07-01 17:37:58 -07001867 mCursorMotionAccumulator.configure(getDevice());
1868
Jeff Brown474dcb52011-06-14 20:22:50 -07001869 // Configure basic parameters.
1870 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08001871
Jeff Brown474dcb52011-06-14 20:22:50 -07001872 // Configure device mode.
1873 switch (mParameters.mode) {
1874 case Parameters::MODE_POINTER:
1875 mSource = AINPUT_SOURCE_MOUSE;
1876 mXPrecision = 1.0f;
1877 mYPrecision = 1.0f;
1878 mXScale = 1.0f;
1879 mYScale = 1.0f;
1880 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
1881 break;
1882 case Parameters::MODE_NAVIGATION:
1883 mSource = AINPUT_SOURCE_TRACKBALL;
1884 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1885 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1886 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1887 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1888 break;
1889 }
1890
1891 mVWheelScale = 1.0f;
1892 mHWheelScale = 1.0f;
Jeff Brown83c09682010-12-23 17:50:18 -08001893 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001894
Jeff Brown474dcb52011-06-14 20:22:50 -07001895 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
1896 mPointerVelocityControl.setParameters(config->pointerVelocityControlParameters);
1897 mWheelXVelocityControl.setParameters(config->wheelVelocityControlParameters);
1898 mWheelYVelocityControl.setParameters(config->wheelVelocityControlParameters);
1899 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001900}
1901
Jeff Brown83c09682010-12-23 17:50:18 -08001902void CursorInputMapper::configureParameters() {
1903 mParameters.mode = Parameters::MODE_POINTER;
1904 String8 cursorModeString;
1905 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
1906 if (cursorModeString == "navigation") {
1907 mParameters.mode = Parameters::MODE_NAVIGATION;
1908 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
1909 LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
1910 }
1911 }
1912
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001913 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08001914 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001915 mParameters.orientationAware);
1916
Jeff Brownbc68a592011-07-25 12:58:12 -07001917 mParameters.associatedDisplayId = -1;
1918 if (mParameters.mode == Parameters::MODE_POINTER || mParameters.orientationAware) {
1919 mParameters.associatedDisplayId = 0;
1920 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001921}
1922
Jeff Brown83c09682010-12-23 17:50:18 -08001923void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001924 dump.append(INDENT3 "Parameters:\n");
1925 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1926 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08001927
1928 switch (mParameters.mode) {
1929 case Parameters::MODE_POINTER:
1930 dump.append(INDENT4 "Mode: pointer\n");
1931 break;
1932 case Parameters::MODE_NAVIGATION:
1933 dump.append(INDENT4 "Mode: navigation\n");
1934 break;
1935 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07001936 LOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08001937 }
1938
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001939 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1940 toString(mParameters.orientationAware));
1941}
1942
Jeff Brownbe1aa822011-07-27 16:04:54 -07001943void CursorInputMapper::initialize() {
Jeff Brown49754db2011-07-01 17:37:58 -07001944 mCursorButtonAccumulator.clearButtons();
1945 mCursorMotionAccumulator.clearRelativeAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001946
Jeff Brownbe1aa822011-07-27 16:04:54 -07001947 mButtonState = 0;
1948 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001949}
1950
Jeff Brown83c09682010-12-23 17:50:18 -08001951void CursorInputMapper::reset() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001952 // Reset velocity.
1953 mPointerVelocityControl.reset();
1954 mWheelXVelocityControl.reset();
1955 mWheelYVelocityControl.reset();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001956
Jeff Brownbe1aa822011-07-27 16:04:54 -07001957 // Synthesize button up event on reset.
1958 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
1959 mCursorButtonAccumulator.clearButtons();
1960 mCursorMotionAccumulator.clearRelativeAxes();
1961 sync(when);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001962
Jeff Brownbe1aa822011-07-27 16:04:54 -07001963 initialize();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001964
Jeff Brown6d0fec22010-07-23 21:28:06 -07001965 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001966}
Jeff Brown46b9ac02010-04-22 18:58:52 -07001967
Jeff Brown83c09682010-12-23 17:50:18 -08001968void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07001969 mCursorButtonAccumulator.process(rawEvent);
1970 mCursorMotionAccumulator.process(rawEvent);
Jeff Brownefd32662011-03-08 15:13:06 -08001971
Jeff Brown49754db2011-07-01 17:37:58 -07001972 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
1973 sync(rawEvent->when);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001974 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001975}
1976
Jeff Brown83c09682010-12-23 17:50:18 -08001977void CursorInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07001978 int32_t lastButtonState = mButtonState;
1979 int32_t currentButtonState = mCursorButtonAccumulator.getButtonState();
1980 mButtonState = currentButtonState;
1981
1982 bool wasDown = isPointerDown(lastButtonState);
1983 bool down = isPointerDown(currentButtonState);
1984 bool downChanged;
1985 if (!wasDown && down) {
1986 mDownTime = when;
1987 downChanged = true;
1988 } else if (wasDown && !down) {
1989 downChanged = true;
1990 } else {
1991 downChanged = false;
1992 }
1993 nsecs_t downTime = mDownTime;
1994 bool buttonsChanged = currentButtonState != lastButtonState;
1995
1996 float deltaX = mCursorMotionAccumulator.getRelativeX() * mXScale;
1997 float deltaY = mCursorMotionAccumulator.getRelativeY() * mYScale;
1998 bool moved = deltaX != 0 || deltaY != 0;
1999
2000 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
2001 && (deltaX != 0.0f || deltaY != 0.0f)) {
2002 // Rotate motion based on display orientation if needed.
2003 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
2004 int32_t orientation;
2005 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
2006 false /*external*/, NULL, NULL, & orientation)) {
2007 orientation = DISPLAY_ORIENTATION_0;
2008 }
2009
2010 rotateDelta(orientation, &deltaX, &deltaY);
2011 }
2012
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002013 PointerProperties pointerProperties;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002014 pointerProperties.clear();
2015 pointerProperties.id = 0;
2016 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
2017
Jeff Brown6328cdc2010-07-29 18:18:33 -07002018 PointerCoords pointerCoords;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002019 pointerCoords.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002020
Jeff Brownbe1aa822011-07-27 16:04:54 -07002021 float vscroll = mCursorMotionAccumulator.getRelativeVWheel();
2022 float hscroll = mCursorMotionAccumulator.getRelativeHWheel();
2023 bool scrolled = vscroll != 0 || hscroll != 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002024
Jeff Brownbe1aa822011-07-27 16:04:54 -07002025 mWheelYVelocityControl.move(when, NULL, &vscroll);
2026 mWheelXVelocityControl.move(when, &hscroll, NULL);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002027
Jeff Brownbe1aa822011-07-27 16:04:54 -07002028 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002029
Jeff Brownbe1aa822011-07-27 16:04:54 -07002030 if (mPointerController != NULL) {
2031 if (moved || scrolled || buttonsChanged) {
2032 mPointerController->setPresentation(
2033 PointerControllerInterface::PRESENTATION_POINTER);
Jeff Brown49754db2011-07-01 17:37:58 -07002034
Jeff Brownbe1aa822011-07-27 16:04:54 -07002035 if (moved) {
2036 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002037 }
2038
Jeff Brownbe1aa822011-07-27 16:04:54 -07002039 if (buttonsChanged) {
2040 mPointerController->setButtonState(currentButtonState);
Jeff Brown83c09682010-12-23 17:50:18 -08002041 }
Jeff Brownefd32662011-03-08 15:13:06 -08002042
Jeff Brownbe1aa822011-07-27 16:04:54 -07002043 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08002044 }
2045
Jeff Brownbe1aa822011-07-27 16:04:54 -07002046 float x, y;
2047 mPointerController->getPosition(&x, &y);
2048 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
2049 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
2050 } else {
2051 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
2052 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
2053 }
2054
2055 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002056
Jeff Brown56194eb2011-03-02 19:23:13 -08002057 // Moving an external trackball or mouse should wake the device.
2058 // We don't do this for internal cursor devices to prevent them from waking up
2059 // the device in your pocket.
2060 // TODO: Use the input device configuration to control this behavior more finely.
2061 uint32_t policyFlags = 0;
2062 if (getDevice()->isExternal()) {
2063 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2064 }
2065
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002066 // Synthesize key down from buttons if needed.
2067 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
2068 policyFlags, lastButtonState, currentButtonState);
2069
2070 // Send motion event.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002071 if (downChanged || moved || scrolled || buttonsChanged) {
2072 int32_t metaState = mContext->getGlobalMetaState();
2073 int32_t motionEventAction;
2074 if (downChanged) {
2075 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
2076 } else if (down || mPointerController == NULL) {
2077 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
2078 } else {
2079 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
2080 }
Jeff Brownb6997262010-10-08 22:31:17 -07002081
Jeff Brownbe1aa822011-07-27 16:04:54 -07002082 NotifyMotionArgs args(when, getDeviceId(), mSource, policyFlags,
2083 motionEventAction, 0, metaState, currentButtonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002084 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002085 getListener()->notifyMotion(&args);
Jeff Brown33bbfd22011-02-24 20:55:35 -08002086
Jeff Brownbe1aa822011-07-27 16:04:54 -07002087 // Send hover move after UP to tell the application that the mouse is hovering now.
2088 if (motionEventAction == AMOTION_EVENT_ACTION_UP
2089 && mPointerController != NULL) {
2090 NotifyMotionArgs hoverArgs(when, getDeviceId(), mSource, policyFlags,
2091 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
2092 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
2093 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2094 getListener()->notifyMotion(&hoverArgs);
2095 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002096
Jeff Brownbe1aa822011-07-27 16:04:54 -07002097 // Send scroll events.
2098 if (scrolled) {
2099 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
2100 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
2101
2102 NotifyMotionArgs scrollArgs(when, getDeviceId(), mSource, policyFlags,
2103 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
2104 AMOTION_EVENT_EDGE_FLAG_NONE,
2105 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
2106 getListener()->notifyMotion(&scrollArgs);
2107 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08002108 }
Jeff Browna032cc02011-03-07 16:56:21 -08002109
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002110 // Synthesize key up from buttons if needed.
2111 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
2112 policyFlags, lastButtonState, currentButtonState);
2113
Jeff Brown49754db2011-07-01 17:37:58 -07002114 mCursorMotionAccumulator.clearRelativeAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002115}
2116
Jeff Brown83c09682010-12-23 17:50:18 -08002117int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07002118 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
2119 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
2120 } else {
2121 return AKEY_STATE_UNKNOWN;
2122 }
2123}
2124
Jeff Brown05dc66a2011-03-02 14:41:58 -08002125void CursorInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002126 if (mPointerController != NULL) {
2127 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
2128 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002129}
2130
Jeff Brown6d0fec22010-07-23 21:28:06 -07002131
2132// --- TouchInputMapper ---
2133
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002134TouchInputMapper::TouchInputMapper(InputDevice* device) :
Jeff Brownbe1aa822011-07-27 16:04:54 -07002135 InputMapper(device),
2136 mSurfaceOrientation(-1), mSurfaceWidth(-1), mSurfaceHeight(-1) {
2137 initialize();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002138}
2139
2140TouchInputMapper::~TouchInputMapper() {
2141}
2142
2143uint32_t TouchInputMapper::getSources() {
Jeff Brownace13b12011-03-09 17:39:48 -08002144 return mTouchSource | mPointerSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002145}
2146
2147void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
2148 InputMapper::populateDeviceInfo(info);
2149
Jeff Brownbe1aa822011-07-27 16:04:54 -07002150 // Ensure surface information is up to date so that orientation changes are
2151 // noticed immediately.
2152 if (!configureSurface()) {
2153 return;
2154 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002155
Jeff Brownbe1aa822011-07-27 16:04:54 -07002156 info->addMotionRange(mOrientedRanges.x);
2157 info->addMotionRange(mOrientedRanges.y);
2158
2159 if (mOrientedRanges.havePressure) {
2160 info->addMotionRange(mOrientedRanges.pressure);
2161 }
2162
2163 if (mOrientedRanges.haveSize) {
2164 info->addMotionRange(mOrientedRanges.size);
2165 }
2166
2167 if (mOrientedRanges.haveTouchSize) {
2168 info->addMotionRange(mOrientedRanges.touchMajor);
2169 info->addMotionRange(mOrientedRanges.touchMinor);
2170 }
2171
2172 if (mOrientedRanges.haveToolSize) {
2173 info->addMotionRange(mOrientedRanges.toolMajor);
2174 info->addMotionRange(mOrientedRanges.toolMinor);
2175 }
2176
2177 if (mOrientedRanges.haveOrientation) {
2178 info->addMotionRange(mOrientedRanges.orientation);
2179 }
2180
2181 if (mOrientedRanges.haveDistance) {
2182 info->addMotionRange(mOrientedRanges.distance);
2183 }
2184
2185 if (mPointerController != NULL) {
2186 float minX, minY, maxX, maxY;
2187 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
2188 info->addMotionRange(AMOTION_EVENT_AXIS_X, mPointerSource,
2189 minX, maxX, 0.0f, 0.0f);
2190 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mPointerSource,
2191 minY, maxY, 0.0f, 0.0f);
Jeff Brownefd32662011-03-08 15:13:06 -08002192 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07002193 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mPointerSource,
2194 0.0f, 1.0f, 0.0f, 0.0f);
2195 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002196}
2197
Jeff Brownef3d7e82010-09-30 14:33:04 -07002198void TouchInputMapper::dump(String8& dump) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002199 dump.append(INDENT2 "Touch Input Mapper:\n");
2200 dumpParameters(dump);
2201 dumpVirtualKeys(dump);
2202 dumpRawPointerAxes(dump);
2203 dumpCalibration(dump);
2204 dumpSurface(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08002205
Jeff Brownbe1aa822011-07-27 16:04:54 -07002206 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
2207 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mXScale);
2208 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mYScale);
2209 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mXPrecision);
2210 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mYPrecision);
2211 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mGeometricScale);
2212 dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mToolSizeLinearScale);
2213 dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mToolSizeLinearBias);
2214 dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mToolSizeAreaScale);
2215 dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mToolSizeAreaBias);
2216 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mPressureScale);
2217 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mSizeScale);
2218 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mOrientationScale);
2219 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mDistanceScale);
Jeff Brownefd32662011-03-08 15:13:06 -08002220
Jeff Brownbe1aa822011-07-27 16:04:54 -07002221 dump.appendFormat(INDENT3 "Last Button State: 0x%08x\n", mLastButtonState);
Jeff Brownace13b12011-03-09 17:39:48 -08002222
Jeff Brownbe1aa822011-07-27 16:04:54 -07002223 dump.appendFormat(INDENT3 "Last Raw Touch: pointerCount=%d\n",
2224 mLastRawPointerData.pointerCount);
2225 for (uint32_t i = 0; i < mLastRawPointerData.pointerCount; i++) {
2226 const RawPointerData::Pointer& pointer = mLastRawPointerData.pointers[i];
2227 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%d, y=%d, pressure=%d, "
2228 "touchMajor=%d, touchMinor=%d, toolMajor=%d, toolMinor=%d, "
2229 "orientation=%d, distance=%d, toolType=%d, isHovering=%s\n", i,
2230 pointer.id, pointer.x, pointer.y, pointer.pressure,
2231 pointer.touchMajor, pointer.touchMinor,
2232 pointer.toolMajor, pointer.toolMinor,
2233 pointer.orientation, pointer.distance,
2234 pointer.toolType, toString(pointer.isHovering));
2235 }
2236
2237 dump.appendFormat(INDENT3 "Last Cooked Touch: pointerCount=%d\n",
2238 mLastCookedPointerData.pointerCount);
2239 for (uint32_t i = 0; i < mLastCookedPointerData.pointerCount; i++) {
2240 const PointerProperties& pointerProperties = mLastCookedPointerData.pointerProperties[i];
2241 const PointerCoords& pointerCoords = mLastCookedPointerData.pointerCoords[i];
2242 dump.appendFormat(INDENT4 "[%d]: id=%d, x=%0.3f, y=%0.3f, pressure=%0.3f, "
2243 "touchMajor=%0.3f, touchMinor=%0.3f, toolMajor=%0.3f, toolMinor=%0.3f, "
2244 "orientation=%0.3f, distance=%0.3f, toolType=%d, isHovering=%s\n", i,
2245 pointerProperties.id,
2246 pointerCoords.getX(),
2247 pointerCoords.getY(),
2248 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE),
2249 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2250 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2251 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2252 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2253 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION),
2254 pointerCoords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE),
2255 pointerProperties.toolType,
2256 toString(mLastCookedPointerData.isHovering(i)));
2257 }
2258
2259 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2260 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
2261 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
2262 mPointerGestureXMovementScale);
2263 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
2264 mPointerGestureYMovementScale);
2265 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
2266 mPointerGestureXZoomScale);
2267 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
2268 mPointerGestureYZoomScale);
2269 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
2270 mPointerGestureMaxSwipeWidth);
2271 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07002272}
2273
Jeff Brownbe1aa822011-07-27 16:04:54 -07002274void TouchInputMapper::initialize() {
2275 mCurrentRawPointerData.clear();
2276 mLastRawPointerData.clear();
2277 mCurrentCookedPointerData.clear();
2278 mLastCookedPointerData.clear();
2279 mCurrentButtonState = 0;
2280 mLastButtonState = 0;
2281 mSentHoverEnter = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002282 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002283
Jeff Brownbe1aa822011-07-27 16:04:54 -07002284 mCurrentVirtualKey.down = false;
Jeff Brown8d608662010-08-30 03:02:23 -07002285
Jeff Brownbe1aa822011-07-27 16:04:54 -07002286 mOrientedRanges.havePressure = false;
2287 mOrientedRanges.haveSize = false;
2288 mOrientedRanges.haveTouchSize = false;
2289 mOrientedRanges.haveToolSize = false;
2290 mOrientedRanges.haveOrientation = false;
2291 mOrientedRanges.haveDistance = false;
Jeff Brownace13b12011-03-09 17:39:48 -08002292
2293 mPointerGesture.reset();
Jeff Brown8d608662010-08-30 03:02:23 -07002294}
2295
Jeff Brown474dcb52011-06-14 20:22:50 -07002296void TouchInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
2297 InputMapper::configure(config, changes);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002298
Jeff Brown474dcb52011-06-14 20:22:50 -07002299 mConfig = *config;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002300
Jeff Brown474dcb52011-06-14 20:22:50 -07002301 if (!changes) { // first time only
2302 // Configure basic parameters.
2303 configureParameters();
2304
2305 // Configure sources.
2306 switch (mParameters.deviceType) {
2307 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2308 mTouchSource = AINPUT_SOURCE_TOUCHSCREEN;
2309 mPointerSource = 0;
2310 break;
2311 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2312 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
2313 mPointerSource = 0;
2314 break;
2315 case Parameters::DEVICE_TYPE_POINTER:
2316 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
2317 mPointerSource = AINPUT_SOURCE_MOUSE;
2318 break;
2319 default:
2320 LOG_ASSERT(false);
2321 }
2322
2323 // Configure absolute axis information.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002324 configureRawPointerAxes();
Jeff Brown474dcb52011-06-14 20:22:50 -07002325
2326 // Prepare input device calibration.
2327 parseCalibration();
2328 resolveCalibration();
2329
Jeff Brownbe1aa822011-07-27 16:04:54 -07002330 // Configure surface dimensions and orientation.
2331 configureSurface();
Jeff Brown83c09682010-12-23 17:50:18 -08002332 }
2333
Jeff Brown474dcb52011-06-14 20:22:50 -07002334 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_SPEED)) {
2335 mPointerGesture.pointerVelocityControl.setParameters(
2336 mConfig.pointerVelocityControlParameters);
2337 }
Jeff Brown8d608662010-08-30 03:02:23 -07002338
Jeff Brown474dcb52011-06-14 20:22:50 -07002339 if (!changes || (changes & InputReaderConfiguration::CHANGE_POINTER_GESTURE_ENABLEMENT)) {
2340 // Reset the touch screen when pointer gesture enablement changes.
2341 reset();
2342 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002343}
2344
Jeff Brown8d608662010-08-30 03:02:23 -07002345void TouchInputMapper::configureParameters() {
Jeff Brownb1268222011-06-03 17:06:16 -07002346 // Use the pointer presentation mode for devices that do not support distinct
2347 // multitouch. The spot-based presentation relies on being able to accurately
2348 // locate two or more fingers on the touch pad.
2349 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
2350 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07002351
Jeff Brown538881e2011-05-25 18:23:38 -07002352 String8 gestureModeString;
2353 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
2354 gestureModeString)) {
2355 if (gestureModeString == "pointer") {
2356 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2357 } else if (gestureModeString == "spots") {
2358 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2359 } else if (gestureModeString != "default") {
2360 LOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
2361 }
2362 }
2363
Jeff Brownace13b12011-03-09 17:39:48 -08002364 if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
2365 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2366 // The device is a cursor device with a touch pad attached.
2367 // By default don't use the touch pad to move the pointer.
2368 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002369 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2370 // The device is a pointing device like a track pad.
2371 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2372 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2373 // The device is a touch screen.
2374 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownace13b12011-03-09 17:39:48 -08002375 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002376 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002377 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2378 }
2379
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002380 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002381 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2382 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002383 if (deviceTypeString == "touchScreen") {
2384 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002385 } else if (deviceTypeString == "touchPad") {
2386 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002387 } else if (deviceTypeString == "pointer") {
2388 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002389 } else if (deviceTypeString != "default") {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002390 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
2391 }
2392 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002393
Jeff Brownefd32662011-03-08 15:13:06 -08002394 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002395 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2396 mParameters.orientationAware);
2397
Jeff Brownbc68a592011-07-25 12:58:12 -07002398 mParameters.associatedDisplayId = -1;
2399 mParameters.associatedDisplayIsExternal = false;
2400 if (mParameters.orientationAware
Jeff Brownefd32662011-03-08 15:13:06 -08002401 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownbc68a592011-07-25 12:58:12 -07002402 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2403 mParameters.associatedDisplayIsExternal =
2404 mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
2405 && getDevice()->isExternal();
2406 mParameters.associatedDisplayId = 0;
2407 }
Jeff Brown8d608662010-08-30 03:02:23 -07002408}
2409
Jeff Brownef3d7e82010-09-30 14:33:04 -07002410void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002411 dump.append(INDENT3 "Parameters:\n");
2412
Jeff Brown538881e2011-05-25 18:23:38 -07002413 switch (mParameters.gestureMode) {
2414 case Parameters::GESTURE_MODE_POINTER:
2415 dump.append(INDENT4 "GestureMode: pointer\n");
2416 break;
2417 case Parameters::GESTURE_MODE_SPOTS:
2418 dump.append(INDENT4 "GestureMode: spots\n");
2419 break;
2420 default:
2421 assert(false);
2422 }
2423
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002424 switch (mParameters.deviceType) {
2425 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2426 dump.append(INDENT4 "DeviceType: touchScreen\n");
2427 break;
2428 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2429 dump.append(INDENT4 "DeviceType: touchPad\n");
2430 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002431 case Parameters::DEVICE_TYPE_POINTER:
2432 dump.append(INDENT4 "DeviceType: pointer\n");
2433 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002434 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002435 LOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002436 }
2437
2438 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2439 mParameters.associatedDisplayId);
2440 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2441 toString(mParameters.orientationAware));
Jeff Brownb88102f2010-09-08 11:49:43 -07002442}
2443
Jeff Brownbe1aa822011-07-27 16:04:54 -07002444void TouchInputMapper::configureRawPointerAxes() {
2445 mRawPointerAxes.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002446}
2447
Jeff Brownbe1aa822011-07-27 16:04:54 -07002448void TouchInputMapper::dumpRawPointerAxes(String8& dump) {
2449 dump.append(INDENT3 "Raw Touch Axes:\n");
2450 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.x, "X");
2451 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.y, "Y");
2452 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.pressure, "Pressure");
2453 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMajor, "TouchMajor");
2454 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.touchMinor, "TouchMinor");
2455 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMajor, "ToolMajor");
2456 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.toolMinor, "ToolMinor");
2457 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.orientation, "Orientation");
2458 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.distance, "Distance");
2459 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.trackingId, "TrackingId");
2460 dumpRawAbsoluteAxisInfo(dump, mRawPointerAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002461}
2462
Jeff Brownbe1aa822011-07-27 16:04:54 -07002463bool TouchInputMapper::configureSurface() {
Jeff Brown9626b142011-03-03 02:09:54 -08002464 // Ensure we have valid X and Y axes.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002465 if (!mRawPointerAxes.x.valid || !mRawPointerAxes.y.valid) {
Jeff Brown9626b142011-03-03 02:09:54 -08002466 LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
2467 "The device will be inoperable.", getDeviceName().string());
2468 return false;
2469 }
2470
Jeff Brown6d0fec22010-07-23 21:28:06 -07002471 // Update orientation and dimensions if needed.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002472 int32_t orientation = DISPLAY_ORIENTATION_0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002473 int32_t width = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2474 int32_t height = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002475
2476 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002477 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002478 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownbc68a592011-07-25 12:58:12 -07002479 mParameters.associatedDisplayIsExternal,
Jeff Brownbe1aa822011-07-27 16:04:54 -07002480 &mAssociatedDisplayWidth, &mAssociatedDisplayHeight,
2481 &mAssociatedDisplayOrientation)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002482 return false;
2483 }
Jeff Brownefd32662011-03-08 15:13:06 -08002484
2485 // A touch screen inherits the dimensions of the display.
2486 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002487 width = mAssociatedDisplayWidth;
2488 height = mAssociatedDisplayHeight;
Jeff Brownefd32662011-03-08 15:13:06 -08002489 }
2490
2491 // The device inherits the orientation of the display if it is orientation aware.
2492 if (mParameters.orientationAware) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002493 orientation = mAssociatedDisplayOrientation;
Jeff Brownefd32662011-03-08 15:13:06 -08002494 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002495 }
2496
Jeff Brownace13b12011-03-09 17:39:48 -08002497 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2498 && mPointerController == NULL) {
2499 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2500 }
2501
Jeff Brownbe1aa822011-07-27 16:04:54 -07002502 bool orientationChanged = mSurfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002503 if (orientationChanged) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002504 mSurfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002505 }
2506
Jeff Brownbe1aa822011-07-27 16:04:54 -07002507 bool sizeChanged = mSurfaceWidth != width || mSurfaceHeight != height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002508 if (sizeChanged) {
Jeff Brownefd32662011-03-08 15:13:06 -08002509 LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002510 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown8d608662010-08-30 03:02:23 -07002511
Jeff Brownbe1aa822011-07-27 16:04:54 -07002512 mSurfaceWidth = width;
2513 mSurfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002514
Jeff Brown8d608662010-08-30 03:02:23 -07002515 // Configure X and Y factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002516 mXScale = float(width) / (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1);
2517 mYScale = float(height) / (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1);
2518 mXPrecision = 1.0f / mXScale;
2519 mYPrecision = 1.0f / mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002520
Jeff Brownbe1aa822011-07-27 16:04:54 -07002521 mOrientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
2522 mOrientedRanges.x.source = mTouchSource;
2523 mOrientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
2524 mOrientedRanges.y.source = mTouchSource;
Jeff Brownefd32662011-03-08 15:13:06 -08002525
Jeff Brownbe1aa822011-07-27 16:04:54 -07002526 configureVirtualKeys();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002527
Jeff Brown8d608662010-08-30 03:02:23 -07002528 // Scale factor for terms that are not oriented in a particular axis.
2529 // If the pixels are square then xScale == yScale otherwise we fake it
2530 // by choosing an average.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002531 mGeometricScale = avg(mXScale, mYScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002532
Jeff Brown8d608662010-08-30 03:02:23 -07002533 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002534 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002535
Jeff Brown8d608662010-08-30 03:02:23 -07002536 // TouchMajor and TouchMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002537 if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002538 mOrientedRanges.haveTouchSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002539
Jeff Brownbe1aa822011-07-27 16:04:54 -07002540 mOrientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
2541 mOrientedRanges.touchMajor.source = mTouchSource;
2542 mOrientedRanges.touchMajor.min = 0;
2543 mOrientedRanges.touchMajor.max = diagonalSize;
2544 mOrientedRanges.touchMajor.flat = 0;
2545 mOrientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002546
Jeff Brownbe1aa822011-07-27 16:04:54 -07002547 mOrientedRanges.touchMinor = mOrientedRanges.touchMajor;
2548 mOrientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002549 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002550
Jeff Brown8d608662010-08-30 03:02:23 -07002551 // ToolMajor and ToolMinor factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002552 mToolSizeLinearScale = 0;
2553 mToolSizeLinearBias = 0;
2554 mToolSizeAreaScale = 0;
2555 mToolSizeAreaBias = 0;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002556 if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
2557 if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) {
2558 if (mCalibration.haveToolSizeLinearScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002559 mToolSizeLinearScale = mCalibration.toolSizeLinearScale;
2560 } else if (mRawPointerAxes.toolMajor.valid
2561 && mRawPointerAxes.toolMajor.maxValue != 0) {
2562 mToolSizeLinearScale = float(min(width, height))
2563 / mRawPointerAxes.toolMajor.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07002564 }
2565
Jeff Brownc6d282b2010-10-14 21:42:15 -07002566 if (mCalibration.haveToolSizeLinearBias) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002567 mToolSizeLinearBias = mCalibration.toolSizeLinearBias;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002568 }
2569 } else if (mCalibration.toolSizeCalibration ==
2570 Calibration::TOOL_SIZE_CALIBRATION_AREA) {
2571 if (mCalibration.haveToolSizeLinearScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002572 mToolSizeLinearScale = mCalibration.toolSizeLinearScale;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002573 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002574 mToolSizeLinearScale = min(width, height);
Jeff Brownc6d282b2010-10-14 21:42:15 -07002575 }
2576
2577 if (mCalibration.haveToolSizeLinearBias) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002578 mToolSizeLinearBias = mCalibration.toolSizeLinearBias;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002579 }
2580
2581 if (mCalibration.haveToolSizeAreaScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002582 mToolSizeAreaScale = mCalibration.toolSizeAreaScale;
2583 } else if (mRawPointerAxes.toolMajor.valid
2584 && mRawPointerAxes.toolMajor.maxValue != 0) {
2585 mToolSizeAreaScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002586 }
2587
2588 if (mCalibration.haveToolSizeAreaBias) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002589 mToolSizeAreaBias = mCalibration.toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -07002590 }
2591 }
2592
Jeff Brownbe1aa822011-07-27 16:04:54 -07002593 mOrientedRanges.haveToolSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002594
Jeff Brownbe1aa822011-07-27 16:04:54 -07002595 mOrientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
2596 mOrientedRanges.toolMajor.source = mTouchSource;
2597 mOrientedRanges.toolMajor.min = 0;
2598 mOrientedRanges.toolMajor.max = diagonalSize;
2599 mOrientedRanges.toolMajor.flat = 0;
2600 mOrientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002601
Jeff Brownbe1aa822011-07-27 16:04:54 -07002602 mOrientedRanges.toolMinor = mOrientedRanges.toolMajor;
2603 mOrientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002604 }
2605
2606 // Pressure factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002607 mPressureScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002608 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
2609 RawAbsoluteAxisInfo rawPressureAxis;
2610 switch (mCalibration.pressureSource) {
2611 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002612 rawPressureAxis = mRawPointerAxes.pressure;
Jeff Brown8d608662010-08-30 03:02:23 -07002613 break;
2614 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002615 rawPressureAxis = mRawPointerAxes.touchMajor;
Jeff Brown8d608662010-08-30 03:02:23 -07002616 break;
2617 default:
2618 rawPressureAxis.clear();
2619 }
2620
Jeff Brown8d608662010-08-30 03:02:23 -07002621 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2622 || mCalibration.pressureCalibration
2623 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2624 if (mCalibration.havePressureScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002625 mPressureScale = mCalibration.pressureScale;
Jeff Brown8d608662010-08-30 03:02:23 -07002626 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002627 mPressureScale = 1.0f / rawPressureAxis.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07002628 }
2629 }
2630
Jeff Brownbe1aa822011-07-27 16:04:54 -07002631 mOrientedRanges.havePressure = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002632
Jeff Brownbe1aa822011-07-27 16:04:54 -07002633 mOrientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2634 mOrientedRanges.pressure.source = mTouchSource;
2635 mOrientedRanges.pressure.min = 0;
2636 mOrientedRanges.pressure.max = 1.0;
2637 mOrientedRanges.pressure.flat = 0;
2638 mOrientedRanges.pressure.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002639 }
2640
2641 // Size factors.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002642 mSizeScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002643 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002644 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002645 if (mRawPointerAxes.toolMajor.valid && mRawPointerAxes.toolMajor.maxValue != 0) {
2646 mSizeScale = 1.0f / mRawPointerAxes.toolMajor.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07002647 }
2648 }
2649
Jeff Brownbe1aa822011-07-27 16:04:54 -07002650 mOrientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002651
Jeff Brownbe1aa822011-07-27 16:04:54 -07002652 mOrientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
2653 mOrientedRanges.size.source = mTouchSource;
2654 mOrientedRanges.size.min = 0;
2655 mOrientedRanges.size.max = 1.0;
2656 mOrientedRanges.size.flat = 0;
2657 mOrientedRanges.size.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002658 }
2659
2660 // Orientation
Jeff Brownbe1aa822011-07-27 16:04:54 -07002661 mOrientationScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002662 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002663 if (mCalibration.orientationCalibration
2664 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002665 if (mRawPointerAxes.orientation.valid && mRawPointerAxes.orientation.maxValue != 0) {
2666 mOrientationScale = float(M_PI_2) / mRawPointerAxes.orientation.maxValue;
Jeff Brown8d608662010-08-30 03:02:23 -07002667 }
2668 }
2669
Jeff Brownbe1aa822011-07-27 16:04:54 -07002670 mOrientedRanges.haveOrientation = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002671
Jeff Brownbe1aa822011-07-27 16:04:54 -07002672 mOrientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2673 mOrientedRanges.orientation.source = mTouchSource;
2674 mOrientedRanges.orientation.min = - M_PI_2;
2675 mOrientedRanges.orientation.max = M_PI_2;
2676 mOrientedRanges.orientation.flat = 0;
2677 mOrientedRanges.orientation.fuzz = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002678 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002679
2680 // Distance
Jeff Brownbe1aa822011-07-27 16:04:54 -07002681 mDistanceScale = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002682 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
2683 if (mCalibration.distanceCalibration
2684 == Calibration::DISTANCE_CALIBRATION_SCALED) {
2685 if (mCalibration.haveDistanceScale) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002686 mDistanceScale = mCalibration.distanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002687 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002688 mDistanceScale = 1.0f;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002689 }
2690 }
2691
Jeff Brownbe1aa822011-07-27 16:04:54 -07002692 mOrientedRanges.haveDistance = true;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002693
Jeff Brownbe1aa822011-07-27 16:04:54 -07002694 mOrientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
2695 mOrientedRanges.distance.source = mTouchSource;
2696 mOrientedRanges.distance.min =
2697 mRawPointerAxes.distance.minValue * mDistanceScale;
2698 mOrientedRanges.distance.max =
2699 mRawPointerAxes.distance.minValue * mDistanceScale;
2700 mOrientedRanges.distance.flat = 0;
2701 mOrientedRanges.distance.fuzz =
2702 mRawPointerAxes.distance.fuzz * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002703 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002704 }
2705
2706 if (orientationChanged || sizeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002707 // Compute oriented surface dimensions, precision, scales and ranges.
2708 // Note that the maximum value reported is an inclusive maximum value so it is one
2709 // unit less than the total width or height of surface.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002710 switch (mSurfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002711 case DISPLAY_ORIENTATION_90:
2712 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002713 mOrientedSurfaceWidth = mSurfaceHeight;
2714 mOrientedSurfaceHeight = mSurfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002715
Jeff Brownbe1aa822011-07-27 16:04:54 -07002716 mOrientedXPrecision = mYPrecision;
2717 mOrientedYPrecision = mXPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002718
Jeff Brownbe1aa822011-07-27 16:04:54 -07002719 mOrientedRanges.x.min = 0;
2720 mOrientedRanges.x.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2721 * mYScale;
2722 mOrientedRanges.x.flat = 0;
2723 mOrientedRanges.x.fuzz = mYScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002724
Jeff Brownbe1aa822011-07-27 16:04:54 -07002725 mOrientedRanges.y.min = 0;
2726 mOrientedRanges.y.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2727 * mXScale;
2728 mOrientedRanges.y.flat = 0;
2729 mOrientedRanges.y.fuzz = mXScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002730 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002731
Jeff Brown6d0fec22010-07-23 21:28:06 -07002732 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07002733 mOrientedSurfaceWidth = mSurfaceWidth;
2734 mOrientedSurfaceHeight = mSurfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002735
Jeff Brownbe1aa822011-07-27 16:04:54 -07002736 mOrientedXPrecision = mXPrecision;
2737 mOrientedYPrecision = mYPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002738
Jeff Brownbe1aa822011-07-27 16:04:54 -07002739 mOrientedRanges.x.min = 0;
2740 mOrientedRanges.x.max = (mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue)
2741 * mXScale;
2742 mOrientedRanges.x.flat = 0;
2743 mOrientedRanges.x.fuzz = mXScale;
Jeff Brown9626b142011-03-03 02:09:54 -08002744
Jeff Brownbe1aa822011-07-27 16:04:54 -07002745 mOrientedRanges.y.min = 0;
2746 mOrientedRanges.y.max = (mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue)
2747 * mYScale;
2748 mOrientedRanges.y.flat = 0;
2749 mOrientedRanges.y.fuzz = mYScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002750 break;
2751 }
Jeff Brownace13b12011-03-09 17:39:48 -08002752
2753 // Compute pointer gesture detection parameters.
Jeff Brownace13b12011-03-09 17:39:48 -08002754 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07002755 int32_t rawWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2756 int32_t rawHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07002757 float rawDiagonal = hypotf(rawWidth, rawHeight);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002758 float displayDiagonal = hypotf(mAssociatedDisplayWidth,
2759 mAssociatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08002760
Jeff Brown2352b972011-04-12 22:39:53 -07002761 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07002762 // given area relative to the diagonal size of the display when no acceleration
2763 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08002764 // Assume that the touch pad has a square aspect ratio such that movements in
2765 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002766 mPointerGestureXMovementScale = mConfig.pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002767 * displayDiagonal / rawDiagonal;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002768 mPointerGestureYMovementScale = mPointerGestureXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002769
2770 // Scale zooms to cover a smaller range of the display than movements do.
2771 // This value determines the area around the pointer that is affected by freeform
2772 // pointer gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002773 mPointerGestureXZoomScale = mConfig.pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002774 * displayDiagonal / rawDiagonal;
Jeff Brownbe1aa822011-07-27 16:04:54 -07002775 mPointerGestureYZoomScale = mPointerGestureXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002776
Jeff Brown2352b972011-04-12 22:39:53 -07002777 // Max width between pointers to detect a swipe gesture is more than some fraction
2778 // of the diagonal axis of the touch pad. Touches that are wider than this are
2779 // translated into freeform gestures.
Jeff Brownbe1aa822011-07-27 16:04:54 -07002780 mPointerGestureMaxSwipeWidth =
Jeff Brown474dcb52011-06-14 20:22:50 -07002781 mConfig.pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brown2352b972011-04-12 22:39:53 -07002782
2783 // Reset the current pointer gesture.
2784 mPointerGesture.reset();
2785
2786 // Remove any current spots.
2787 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
2788 mPointerController->clearSpots();
2789 }
Jeff Brownace13b12011-03-09 17:39:48 -08002790 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002791 }
2792
2793 return true;
2794}
2795
Jeff Brownbe1aa822011-07-27 16:04:54 -07002796void TouchInputMapper::dumpSurface(String8& dump) {
2797 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mSurfaceWidth);
2798 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mSurfaceHeight);
2799 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mSurfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07002800}
2801
Jeff Brownbe1aa822011-07-27 16:04:54 -07002802void TouchInputMapper::configureVirtualKeys() {
Jeff Brown8d608662010-08-30 03:02:23 -07002803 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08002804 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002805
Jeff Brownbe1aa822011-07-27 16:04:54 -07002806 mVirtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002807
Jeff Brown6328cdc2010-07-29 18:18:33 -07002808 if (virtualKeyDefinitions.size() == 0) {
2809 return;
2810 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002811
Jeff Brownbe1aa822011-07-27 16:04:54 -07002812 mVirtualKeys.setCapacity(virtualKeyDefinitions.size());
Jeff Brown6328cdc2010-07-29 18:18:33 -07002813
Jeff Brownbe1aa822011-07-27 16:04:54 -07002814 int32_t touchScreenLeft = mRawPointerAxes.x.minValue;
2815 int32_t touchScreenTop = mRawPointerAxes.y.minValue;
2816 int32_t touchScreenWidth = mRawPointerAxes.x.maxValue - mRawPointerAxes.x.minValue + 1;
2817 int32_t touchScreenHeight = mRawPointerAxes.y.maxValue - mRawPointerAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002818
2819 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07002820 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07002821 virtualKeyDefinitions[i];
2822
Jeff Brownbe1aa822011-07-27 16:04:54 -07002823 mVirtualKeys.add();
2824 VirtualKey& virtualKey = mVirtualKeys.editTop();
Jeff Brown6328cdc2010-07-29 18:18:33 -07002825
2826 virtualKey.scanCode = virtualKeyDefinition.scanCode;
2827 int32_t keyCode;
2828 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08002829 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07002830 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002831 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
2832 virtualKey.scanCode);
Jeff Brownbe1aa822011-07-27 16:04:54 -07002833 mVirtualKeys.pop(); // drop the key
Jeff Brown6328cdc2010-07-29 18:18:33 -07002834 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002835 }
2836
Jeff Brown6328cdc2010-07-29 18:18:33 -07002837 virtualKey.keyCode = keyCode;
2838 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002839
Jeff Brown6328cdc2010-07-29 18:18:33 -07002840 // convert the key definition's display coordinates into touch coordinates for a hit box
2841 int32_t halfWidth = virtualKeyDefinition.width / 2;
2842 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002843
Jeff Brown6328cdc2010-07-29 18:18:33 -07002844 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07002845 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002846 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
Jeff Brownbe1aa822011-07-27 16:04:54 -07002847 * touchScreenWidth / mSurfaceWidth + touchScreenLeft;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002848 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07002849 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002850 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
Jeff Brownbe1aa822011-07-27 16:04:54 -07002851 * touchScreenHeight / mSurfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07002852 }
2853}
2854
Jeff Brownbe1aa822011-07-27 16:04:54 -07002855void TouchInputMapper::dumpVirtualKeys(String8& dump) {
2856 if (!mVirtualKeys.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07002857 dump.append(INDENT3 "Virtual Keys:\n");
2858
Jeff Brownbe1aa822011-07-27 16:04:54 -07002859 for (size_t i = 0; i < mVirtualKeys.size(); i++) {
2860 const VirtualKey& virtualKey = mVirtualKeys.itemAt(i);
Jeff Brownef3d7e82010-09-30 14:33:04 -07002861 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
2862 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
2863 i, virtualKey.scanCode, virtualKey.keyCode,
2864 virtualKey.hitLeft, virtualKey.hitRight,
2865 virtualKey.hitTop, virtualKey.hitBottom);
2866 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002867 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002868}
2869
Jeff Brown8d608662010-08-30 03:02:23 -07002870void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002871 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07002872 Calibration& out = mCalibration;
2873
Jeff Brownc6d282b2010-10-14 21:42:15 -07002874 // Touch Size
2875 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT;
2876 String8 touchSizeCalibrationString;
2877 if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) {
2878 if (touchSizeCalibrationString == "none") {
2879 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
2880 } else if (touchSizeCalibrationString == "geometric") {
2881 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC;
2882 } else if (touchSizeCalibrationString == "pressure") {
2883 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
2884 } else if (touchSizeCalibrationString != "default") {
2885 LOGW("Invalid value for touch.touchSize.calibration: '%s'",
2886 touchSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002887 }
2888 }
2889
Jeff Brownc6d282b2010-10-14 21:42:15 -07002890 // Tool Size
2891 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT;
2892 String8 toolSizeCalibrationString;
2893 if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) {
2894 if (toolSizeCalibrationString == "none") {
2895 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
2896 } else if (toolSizeCalibrationString == "geometric") {
2897 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC;
2898 } else if (toolSizeCalibrationString == "linear") {
2899 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
2900 } else if (toolSizeCalibrationString == "area") {
2901 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA;
2902 } else if (toolSizeCalibrationString != "default") {
2903 LOGW("Invalid value for touch.toolSize.calibration: '%s'",
2904 toolSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002905 }
2906 }
2907
Jeff Brownc6d282b2010-10-14 21:42:15 -07002908 out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"),
2909 out.toolSizeLinearScale);
2910 out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"),
2911 out.toolSizeLinearBias);
2912 out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"),
2913 out.toolSizeAreaScale);
2914 out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"),
2915 out.toolSizeAreaBias);
2916 out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"),
2917 out.toolSizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07002918
2919 // Pressure
2920 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
2921 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002922 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002923 if (pressureCalibrationString == "none") {
2924 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2925 } else if (pressureCalibrationString == "physical") {
2926 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
2927 } else if (pressureCalibrationString == "amplitude") {
2928 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2929 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002930 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002931 pressureCalibrationString.string());
2932 }
2933 }
2934
2935 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
2936 String8 pressureSourceString;
2937 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
2938 if (pressureSourceString == "pressure") {
2939 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2940 } else if (pressureSourceString == "touch") {
2941 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2942 } else if (pressureSourceString != "default") {
2943 LOGW("Invalid value for touch.pressure.source: '%s'",
2944 pressureSourceString.string());
2945 }
2946 }
2947
2948 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
2949 out.pressureScale);
2950
2951 // Size
2952 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
2953 String8 sizeCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002954 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002955 if (sizeCalibrationString == "none") {
2956 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2957 } else if (sizeCalibrationString == "normalized") {
2958 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
2959 } else if (sizeCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002960 LOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002961 sizeCalibrationString.string());
2962 }
2963 }
2964
2965 // Orientation
2966 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
2967 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002968 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002969 if (orientationCalibrationString == "none") {
2970 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2971 } else if (orientationCalibrationString == "interpolated") {
2972 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002973 } else if (orientationCalibrationString == "vector") {
2974 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002975 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002976 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002977 orientationCalibrationString.string());
2978 }
2979 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002980
2981 // Distance
2982 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
2983 String8 distanceCalibrationString;
2984 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
2985 if (distanceCalibrationString == "none") {
2986 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
2987 } else if (distanceCalibrationString == "scaled") {
2988 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
2989 } else if (distanceCalibrationString != "default") {
2990 LOGW("Invalid value for touch.distance.calibration: '%s'",
2991 distanceCalibrationString.string());
2992 }
2993 }
2994
2995 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
2996 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002997}
2998
2999void TouchInputMapper::resolveCalibration() {
3000 // Pressure
3001 switch (mCalibration.pressureSource) {
3002 case Calibration::PRESSURE_SOURCE_DEFAULT:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003003 if (mRawPointerAxes.pressure.valid) {
Jeff Brown8d608662010-08-30 03:02:23 -07003004 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003005 } else if (mRawPointerAxes.touchMajor.valid) {
Jeff Brown8d608662010-08-30 03:02:23 -07003006 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
3007 }
3008 break;
3009
3010 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003011 if (! mRawPointerAxes.pressure.valid) {
Jeff Brown8d608662010-08-30 03:02:23 -07003012 LOGW("Calibration property touch.pressure.source is 'pressure' but "
3013 "the pressure axis is not available.");
3014 }
3015 break;
3016
3017 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003018 if (! mRawPointerAxes.touchMajor.valid) {
Jeff Brown8d608662010-08-30 03:02:23 -07003019 LOGW("Calibration property touch.pressure.source is 'touch' but "
3020 "the touchMajor axis is not available.");
3021 }
3022 break;
3023
3024 default:
3025 break;
3026 }
3027
3028 switch (mCalibration.pressureCalibration) {
3029 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
3030 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
3031 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
3032 } else {
3033 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
3034 }
3035 break;
3036
3037 default:
3038 break;
3039 }
3040
Jeff Brownc6d282b2010-10-14 21:42:15 -07003041 // Tool Size
3042 switch (mCalibration.toolSizeCalibration) {
3043 case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003044 if (mRawPointerAxes.toolMajor.valid) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07003045 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
Jeff Brown8d608662010-08-30 03:02:23 -07003046 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07003047 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003048 }
3049 break;
3050
3051 default:
3052 break;
3053 }
3054
Jeff Brownc6d282b2010-10-14 21:42:15 -07003055 // Touch Size
3056 switch (mCalibration.touchSizeCalibration) {
3057 case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07003058 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
Jeff Brownc6d282b2010-10-14 21:42:15 -07003059 && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
3060 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
Jeff Brown8d608662010-08-30 03:02:23 -07003061 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07003062 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07003063 }
3064 break;
3065
3066 default:
3067 break;
3068 }
3069
3070 // Size
3071 switch (mCalibration.sizeCalibration) {
3072 case Calibration::SIZE_CALIBRATION_DEFAULT:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003073 if (mRawPointerAxes.toolMajor.valid) {
Jeff Brown8d608662010-08-30 03:02:23 -07003074 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
3075 } else {
3076 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
3077 }
3078 break;
3079
3080 default:
3081 break;
3082 }
3083
3084 // Orientation
3085 switch (mCalibration.orientationCalibration) {
3086 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003087 if (mRawPointerAxes.orientation.valid) {
Jeff Brown8d608662010-08-30 03:02:23 -07003088 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
3089 } else {
3090 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
3091 }
3092 break;
3093
3094 default:
3095 break;
3096 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003097
3098 // Distance
3099 switch (mCalibration.distanceCalibration) {
3100 case Calibration::DISTANCE_CALIBRATION_DEFAULT:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003101 if (mRawPointerAxes.distance.valid) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07003102 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
3103 } else {
3104 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
3105 }
3106 break;
3107
3108 default:
3109 break;
3110 }
Jeff Brown8d608662010-08-30 03:02:23 -07003111}
3112
Jeff Brownef3d7e82010-09-30 14:33:04 -07003113void TouchInputMapper::dumpCalibration(String8& dump) {
3114 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07003115
Jeff Brownc6d282b2010-10-14 21:42:15 -07003116 // Touch Size
3117 switch (mCalibration.touchSizeCalibration) {
3118 case Calibration::TOUCH_SIZE_CALIBRATION_NONE:
3119 dump.append(INDENT4 "touch.touchSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003120 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003121 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
3122 dump.append(INDENT4 "touch.touchSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003123 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003124 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
3125 dump.append(INDENT4 "touch.touchSize.calibration: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003126 break;
3127 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003128 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003129 }
3130
Jeff Brownc6d282b2010-10-14 21:42:15 -07003131 // Tool Size
3132 switch (mCalibration.toolSizeCalibration) {
3133 case Calibration::TOOL_SIZE_CALIBRATION_NONE:
3134 dump.append(INDENT4 "touch.toolSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003135 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003136 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
3137 dump.append(INDENT4 "touch.toolSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003138 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07003139 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
3140 dump.append(INDENT4 "touch.toolSize.calibration: linear\n");
3141 break;
3142 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
3143 dump.append(INDENT4 "touch.toolSize.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003144 break;
3145 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003146 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003147 }
3148
Jeff Brownc6d282b2010-10-14 21:42:15 -07003149 if (mCalibration.haveToolSizeLinearScale) {
3150 dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n",
3151 mCalibration.toolSizeLinearScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003152 }
3153
Jeff Brownc6d282b2010-10-14 21:42:15 -07003154 if (mCalibration.haveToolSizeLinearBias) {
3155 dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n",
3156 mCalibration.toolSizeLinearBias);
Jeff Brown8d608662010-08-30 03:02:23 -07003157 }
3158
Jeff Brownc6d282b2010-10-14 21:42:15 -07003159 if (mCalibration.haveToolSizeAreaScale) {
3160 dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n",
3161 mCalibration.toolSizeAreaScale);
3162 }
3163
3164 if (mCalibration.haveToolSizeAreaBias) {
3165 dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n",
3166 mCalibration.toolSizeAreaBias);
3167 }
3168
3169 if (mCalibration.haveToolSizeIsSummed) {
Jeff Brown1f245102010-11-18 20:53:46 -08003170 dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %s\n",
Jeff Brown47e6b1b2010-11-29 17:37:49 -08003171 toString(mCalibration.toolSizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07003172 }
3173
3174 // Pressure
3175 switch (mCalibration.pressureCalibration) {
3176 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003177 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003178 break;
3179 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003180 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003181 break;
3182 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003183 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003184 break;
3185 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003186 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003187 }
3188
3189 switch (mCalibration.pressureSource) {
3190 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003191 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003192 break;
3193 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003194 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003195 break;
3196 case Calibration::PRESSURE_SOURCE_DEFAULT:
3197 break;
3198 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003199 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003200 }
3201
3202 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07003203 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
3204 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07003205 }
3206
3207 // Size
3208 switch (mCalibration.sizeCalibration) {
3209 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003210 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003211 break;
3212 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003213 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003214 break;
3215 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003216 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003217 }
3218
3219 // Orientation
3220 switch (mCalibration.orientationCalibration) {
3221 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003222 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003223 break;
3224 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07003225 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07003226 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08003227 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
3228 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
3229 break;
Jeff Brown8d608662010-08-30 03:02:23 -07003230 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07003231 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07003232 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07003233
3234 // Distance
3235 switch (mCalibration.distanceCalibration) {
3236 case Calibration::DISTANCE_CALIBRATION_NONE:
3237 dump.append(INDENT4 "touch.distance.calibration: none\n");
3238 break;
3239 case Calibration::DISTANCE_CALIBRATION_SCALED:
3240 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
3241 break;
3242 default:
3243 LOG_ASSERT(false);
3244 }
3245
3246 if (mCalibration.haveDistanceScale) {
3247 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
3248 mCalibration.distanceScale);
3249 }
Jeff Brown8d608662010-08-30 03:02:23 -07003250}
3251
Jeff Brown6d0fec22010-07-23 21:28:06 -07003252void TouchInputMapper::reset() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003253 // Synthesize touch up event.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003254 // This will also take care of finishing virtual key processing if needed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003255 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
3256 mCurrentRawPointerData.clear();
3257 mCurrentButtonState = 0;
3258 syncTouch(when, true);
3259
3260 initialize();
3261
3262 if (mPointerController != NULL
3263 && mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3264 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3265 mPointerController->clearSpots();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003266 }
3267
Jeff Brown6328cdc2010-07-29 18:18:33 -07003268 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07003269}
3270
3271void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Brownaa3855d2011-03-17 01:34:19 -07003272#if DEBUG_RAW_EVENTS
3273 if (!havePointerIds) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003274 LOGD("syncTouch: pointerCount %d -> %d, no pointer ids",
3275 mLastRawPointerData.pointerCount,
3276 mCurrentRawPointerData.pointerCount);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003277 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003278 LOGD("syncTouch: pointerCount %d -> %d, touching ids 0x%08x -> 0x%08x, "
3279 "hovering ids 0x%08x -> 0x%08x",
3280 mLastRawPointerData.pointerCount,
3281 mCurrentRawPointerData.pointerCount,
3282 mLastRawPointerData.touchingIdBits.value,
3283 mCurrentRawPointerData.touchingIdBits.value,
3284 mLastRawPointerData.hoveringIdBits.value,
3285 mCurrentRawPointerData.hoveringIdBits.value);
Jeff Brownaa3855d2011-03-17 01:34:19 -07003286 }
3287#endif
3288
Jeff Brownbe1aa822011-07-27 16:04:54 -07003289 // Configure the surface now, if possible.
3290 if (!configureSurface()) {
3291 mLastRawPointerData.clear();
3292 mLastCookedPointerData.clear();
3293 mLastButtonState = 0;
3294 return;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003295 }
3296
Jeff Brownbe1aa822011-07-27 16:04:54 -07003297 // Preprocess pointer data.
3298 if (!havePointerIds) {
3299 assignPointerIds();
3300 }
3301
3302 // Handle policy on initial down or hover events.
Jeff Brown56194eb2011-03-02 19:23:13 -08003303 uint32_t policyFlags = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003304 if (mLastRawPointerData.pointerCount == 0 && mCurrentRawPointerData.pointerCount != 0) {
Jeff Brownefd32662011-03-08 15:13:06 -08003305 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
3306 // If this is a touch screen, hide the pointer on an initial down.
3307 getContext()->fadePointer();
3308 }
Jeff Brown56194eb2011-03-02 19:23:13 -08003309
3310 // Initial downs on external touch devices should wake the device.
3311 // We don't do this for internal touch screens to prevent them from waking
3312 // up in your pocket.
3313 // TODO: Use the input device configuration to control this behavior more finely.
3314 if (getDevice()->isExternal()) {
3315 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
3316 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08003317 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003318
Jeff Brownbe1aa822011-07-27 16:04:54 -07003319 // Synthesize key down from raw buttons if needed.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003320 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mTouchSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003321 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003322
Jeff Brownbe1aa822011-07-27 16:04:54 -07003323 if (consumeRawTouches(when, policyFlags)) {
3324 mCurrentRawPointerData.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003325 }
3326
Jeff Brownbe1aa822011-07-27 16:04:54 -07003327 if (mPointerController != NULL && mConfig.pointerGesturesEnabled) {
3328 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
3329 }
3330
3331 cookPointerData();
3332 dispatchHoverExit(when, policyFlags);
3333 dispatchTouches(when, policyFlags);
3334 dispatchHoverEnterAndMove(when, policyFlags);
3335
3336 // Synthesize key up from raw buttons if needed.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003337 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mTouchSource,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003338 policyFlags, mLastButtonState, mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003339
Jeff Brown6328cdc2010-07-29 18:18:33 -07003340 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003341 mLastRawPointerData.copyFrom(mCurrentRawPointerData);
3342 mLastCookedPointerData.copyFrom(mCurrentCookedPointerData);
3343 mLastButtonState = mCurrentButtonState;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003344}
3345
Jeff Brown79ac9692011-04-19 21:20:10 -07003346void TouchInputMapper::timeoutExpired(nsecs_t when) {
3347 if (mPointerController != NULL) {
3348 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3349 }
3350}
3351
Jeff Brownbe1aa822011-07-27 16:04:54 -07003352bool TouchInputMapper::consumeRawTouches(nsecs_t when, uint32_t policyFlags) {
3353 // Check for release of a virtual key.
3354 if (mCurrentVirtualKey.down) {
3355 if (mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3356 // Pointer went up while virtual key was down.
3357 mCurrentVirtualKey.down = false;
3358 if (!mCurrentVirtualKey.ignored) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003359#if DEBUG_VIRTUAL_KEYS
3360 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07003361 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003362#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07003363 dispatchVirtualKey(when, policyFlags,
3364 AKEY_EVENT_ACTION_UP,
3365 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003366 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003367 return true;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003368 }
3369
Jeff Brownbe1aa822011-07-27 16:04:54 -07003370 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3371 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3372 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3373 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3374 if (virtualKey && virtualKey->keyCode == mCurrentVirtualKey.keyCode) {
3375 // Pointer is still within the space of the virtual key.
3376 return true;
3377 }
3378 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003379
Jeff Brownbe1aa822011-07-27 16:04:54 -07003380 // Pointer left virtual key area or another pointer also went down.
3381 // Send key cancellation but do not consume the touch yet.
3382 // This is useful when the user swipes through from the virtual key area
3383 // into the main display surface.
3384 mCurrentVirtualKey.down = false;
3385 if (!mCurrentVirtualKey.ignored) {
3386#if DEBUG_VIRTUAL_KEYS
3387 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
3388 mCurrentVirtualKey.keyCode, mCurrentVirtualKey.scanCode);
3389#endif
3390 dispatchVirtualKey(when, policyFlags,
3391 AKEY_EVENT_ACTION_UP,
3392 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3393 | AKEY_EVENT_FLAG_CANCELED);
3394 }
3395 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003396
Jeff Brownbe1aa822011-07-27 16:04:54 -07003397 if (mLastRawPointerData.touchingIdBits.isEmpty()
3398 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
3399 // Pointer just went down. Check for virtual key press or off-screen touches.
3400 uint32_t id = mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
3401 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
3402 if (!isPointInsideSurface(pointer.x, pointer.y)) {
3403 // If exactly one pointer went down, check for virtual key hit.
3404 // Otherwise we will drop the entire stroke.
3405 if (mCurrentRawPointerData.touchingIdBits.count() == 1) {
3406 const VirtualKey* virtualKey = findVirtualKeyHit(pointer.x, pointer.y);
3407 if (virtualKey) {
3408 mCurrentVirtualKey.down = true;
3409 mCurrentVirtualKey.downTime = when;
3410 mCurrentVirtualKey.keyCode = virtualKey->keyCode;
3411 mCurrentVirtualKey.scanCode = virtualKey->scanCode;
3412 mCurrentVirtualKey.ignored = mContext->shouldDropVirtualKey(
3413 when, getDevice(), virtualKey->keyCode, virtualKey->scanCode);
3414
3415 if (!mCurrentVirtualKey.ignored) {
3416#if DEBUG_VIRTUAL_KEYS
3417 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
3418 mCurrentVirtualKey.keyCode,
3419 mCurrentVirtualKey.scanCode);
3420#endif
3421 dispatchVirtualKey(when, policyFlags,
3422 AKEY_EVENT_ACTION_DOWN,
3423 AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY);
3424 }
3425 }
3426 }
3427 return true;
3428 }
3429 }
3430
Jeff Brownfe508922011-01-18 15:10:10 -08003431 // Disable all virtual key touches that happen within a short time interval of the
Jeff Brownbe1aa822011-07-27 16:04:54 -07003432 // most recent touch within the screen area. The idea is to filter out stray
3433 // virtual key presses when interacting with the touch screen.
Jeff Brownfe508922011-01-18 15:10:10 -08003434 //
3435 // Problems we're trying to solve:
3436 //
3437 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3438 // virtual key area that is implemented by a separate touch panel and accidentally
3439 // triggers a virtual key.
3440 //
3441 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3442 // area and accidentally triggers a virtual key. This often happens when virtual keys
3443 // are layed out below the screen near to where the on screen keyboard's space bar
3444 // is displayed.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003445 if (mConfig.virtualKeyQuietTime > 0 && !mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown474dcb52011-06-14 20:22:50 -07003446 mContext->disableVirtualKeysUntil(when + mConfig.virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003447 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07003448 return false;
3449}
3450
3451void TouchInputMapper::dispatchVirtualKey(nsecs_t when, uint32_t policyFlags,
3452 int32_t keyEventAction, int32_t keyEventFlags) {
3453 int32_t keyCode = mCurrentVirtualKey.keyCode;
3454 int32_t scanCode = mCurrentVirtualKey.scanCode;
3455 nsecs_t downTime = mCurrentVirtualKey.downTime;
3456 int32_t metaState = mContext->getGlobalMetaState();
3457 policyFlags |= POLICY_FLAG_VIRTUAL;
3458
3459 NotifyKeyArgs args(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3460 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3461 getListener()->notifyKey(&args);
Jeff Brownfe508922011-01-18 15:10:10 -08003462}
3463
Jeff Brown6d0fec22010-07-23 21:28:06 -07003464void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003465 BitSet32 currentIdBits = mCurrentCookedPointerData.touchingIdBits;
3466 BitSet32 lastIdBits = mLastCookedPointerData.touchingIdBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003467 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003468 int32_t buttonState = mCurrentButtonState;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003469
3470 if (currentIdBits == lastIdBits) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003471 if (!currentIdBits.isEmpty()) {
3472 // No pointer id changes so this is a move event.
3473 // The listener takes care of batching moves so we don't have to deal with that here.
3474 dispatchMotion(when, policyFlags, mTouchSource,
3475 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3476 AMOTION_EVENT_EDGE_FLAG_NONE,
3477 mCurrentCookedPointerData.pointerProperties,
3478 mCurrentCookedPointerData.pointerCoords,
3479 mCurrentCookedPointerData.idToIndex,
3480 currentIdBits, -1,
3481 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3482 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003483 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003484 // There may be pointers going up and pointers going down and pointers moving
3485 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003486 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3487 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003488 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003489 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003490
Jeff Brownace13b12011-03-09 17:39:48 -08003491 // Update last coordinates of pointers that have moved so that we observe the new
3492 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003493 bool moveNeeded = updateMovedPointers(
Jeff Brownbe1aa822011-07-27 16:04:54 -07003494 mCurrentCookedPointerData.pointerProperties,
3495 mCurrentCookedPointerData.pointerCoords,
3496 mCurrentCookedPointerData.idToIndex,
3497 mLastCookedPointerData.pointerProperties,
3498 mLastCookedPointerData.pointerCoords,
3499 mLastCookedPointerData.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003500 moveIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003501 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003502 moveNeeded = true;
3503 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003504
Jeff Brownace13b12011-03-09 17:39:48 -08003505 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003506 while (!upIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003507 uint32_t upId = upIdBits.clearFirstMarkedBit();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003508
Jeff Brownace13b12011-03-09 17:39:48 -08003509 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003510 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003511 mLastCookedPointerData.pointerProperties,
3512 mLastCookedPointerData.pointerCoords,
3513 mLastCookedPointerData.idToIndex,
3514 dispatchedIdBits, upId,
3515 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003516 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003517 }
3518
Jeff Brownc3db8582010-10-20 15:33:38 -07003519 // Dispatch move events if any of the remaining pointers moved from their old locations.
3520 // Although applications receive new locations as part of individual pointer up
3521 // events, they do not generally handle them except when presented in a move event.
3522 if (moveNeeded) {
Jeff Brownb6110c22011-04-01 16:15:13 -07003523 LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003524 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003525 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003526 mCurrentCookedPointerData.pointerProperties,
3527 mCurrentCookedPointerData.pointerCoords,
3528 mCurrentCookedPointerData.idToIndex,
3529 dispatchedIdBits, -1,
3530 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003531 }
3532
3533 // Dispatch pointer down events using the new pointer locations.
3534 while (!downIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003535 uint32_t downId = downIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003536 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003537
Jeff Brownace13b12011-03-09 17:39:48 -08003538 if (dispatchedIdBits.count() == 1) {
3539 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003540 mDownTime = when;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003541 }
3542
Jeff Brownace13b12011-03-09 17:39:48 -08003543 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Browna6111372011-07-14 21:48:23 -07003544 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownbe1aa822011-07-27 16:04:54 -07003545 mCurrentCookedPointerData.pointerProperties,
3546 mCurrentCookedPointerData.pointerCoords,
3547 mCurrentCookedPointerData.idToIndex,
3548 dispatchedIdBits, downId,
3549 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
Jeff Brownace13b12011-03-09 17:39:48 -08003550 }
3551 }
Jeff Brownace13b12011-03-09 17:39:48 -08003552}
3553
Jeff Brownbe1aa822011-07-27 16:04:54 -07003554void TouchInputMapper::dispatchHoverExit(nsecs_t when, uint32_t policyFlags) {
3555 if (mSentHoverEnter &&
3556 (mCurrentCookedPointerData.hoveringIdBits.isEmpty()
3557 || !mCurrentCookedPointerData.touchingIdBits.isEmpty())) {
3558 int32_t metaState = getContext()->getGlobalMetaState();
3559 dispatchMotion(when, policyFlags, mTouchSource,
3560 AMOTION_EVENT_ACTION_HOVER_EXIT, 0, metaState, mLastButtonState, 0,
3561 mLastCookedPointerData.pointerProperties,
3562 mLastCookedPointerData.pointerCoords,
3563 mLastCookedPointerData.idToIndex,
3564 mLastCookedPointerData.hoveringIdBits, -1,
3565 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3566 mSentHoverEnter = false;
3567 }
3568}
Jeff Brownace13b12011-03-09 17:39:48 -08003569
Jeff Brownbe1aa822011-07-27 16:04:54 -07003570void TouchInputMapper::dispatchHoverEnterAndMove(nsecs_t when, uint32_t policyFlags) {
3571 if (mCurrentCookedPointerData.touchingIdBits.isEmpty()
3572 && !mCurrentCookedPointerData.hoveringIdBits.isEmpty()) {
3573 int32_t metaState = getContext()->getGlobalMetaState();
3574 if (!mSentHoverEnter) {
3575 dispatchMotion(when, policyFlags, mTouchSource,
3576 AMOTION_EVENT_ACTION_HOVER_ENTER, 0, metaState, mCurrentButtonState, 0,
3577 mCurrentCookedPointerData.pointerProperties,
3578 mCurrentCookedPointerData.pointerCoords,
3579 mCurrentCookedPointerData.idToIndex,
3580 mCurrentCookedPointerData.hoveringIdBits, -1,
3581 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3582 mSentHoverEnter = true;
3583 }
Jeff Brownace13b12011-03-09 17:39:48 -08003584
Jeff Brownbe1aa822011-07-27 16:04:54 -07003585 dispatchMotion(when, policyFlags, mTouchSource,
3586 AMOTION_EVENT_ACTION_HOVER_MOVE, 0, metaState, mCurrentButtonState, 0,
3587 mCurrentCookedPointerData.pointerProperties,
3588 mCurrentCookedPointerData.pointerCoords,
3589 mCurrentCookedPointerData.idToIndex,
3590 mCurrentCookedPointerData.hoveringIdBits, -1,
3591 mOrientedXPrecision, mOrientedYPrecision, mDownTime);
3592 }
3593}
3594
3595void TouchInputMapper::cookPointerData() {
3596 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
3597
3598 mCurrentCookedPointerData.clear();
3599 mCurrentCookedPointerData.pointerCount = currentPointerCount;
3600 mCurrentCookedPointerData.hoveringIdBits = mCurrentRawPointerData.hoveringIdBits;
3601 mCurrentCookedPointerData.touchingIdBits = mCurrentRawPointerData.touchingIdBits;
3602
3603 // Walk through the the active pointers and map device coordinates onto
3604 // surface coordinates and adjust for display orientation.
Jeff Brownace13b12011-03-09 17:39:48 -08003605 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003606 const RawPointerData::Pointer& in = mCurrentRawPointerData.pointers[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003607
3608 // ToolMajor and ToolMinor
3609 float toolMajor, toolMinor;
3610 switch (mCalibration.toolSizeCalibration) {
3611 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003612 toolMajor = in.toolMajor * mGeometricScale;
3613 if (mRawPointerAxes.toolMinor.valid) {
3614 toolMinor = in.toolMinor * mGeometricScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003615 } else {
3616 toolMinor = toolMajor;
3617 }
3618 break;
3619 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
3620 toolMajor = in.toolMajor != 0
Jeff Brownbe1aa822011-07-27 16:04:54 -07003621 ? in.toolMajor * mToolSizeLinearScale + mToolSizeLinearBias
Jeff Brownace13b12011-03-09 17:39:48 -08003622 : 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003623 if (mRawPointerAxes.toolMinor.valid) {
Jeff Brownace13b12011-03-09 17:39:48 -08003624 toolMinor = in.toolMinor != 0
Jeff Brownbe1aa822011-07-27 16:04:54 -07003625 ? in.toolMinor * mToolSizeLinearScale
3626 + mToolSizeLinearBias
Jeff Brownace13b12011-03-09 17:39:48 -08003627 : 0;
3628 } else {
3629 toolMinor = toolMajor;
3630 }
3631 break;
3632 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
3633 if (in.toolMajor != 0) {
3634 float diameter = sqrtf(in.toolMajor
Jeff Brownbe1aa822011-07-27 16:04:54 -07003635 * mToolSizeAreaScale + mToolSizeAreaBias);
3636 toolMajor = diameter * mToolSizeLinearScale + mToolSizeLinearBias;
Jeff Brownace13b12011-03-09 17:39:48 -08003637 } else {
3638 toolMajor = 0;
3639 }
3640 toolMinor = toolMajor;
3641 break;
3642 default:
3643 toolMajor = 0;
3644 toolMinor = 0;
3645 break;
3646 }
3647
3648 if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003649 uint32_t touchingCount = mCurrentRawPointerData.touchingIdBits.count();
3650 toolMajor /= touchingCount;
3651 toolMinor /= touchingCount;
Jeff Brownace13b12011-03-09 17:39:48 -08003652 }
3653
3654 // Pressure
3655 float rawPressure;
3656 switch (mCalibration.pressureSource) {
3657 case Calibration::PRESSURE_SOURCE_PRESSURE:
3658 rawPressure = in.pressure;
3659 break;
3660 case Calibration::PRESSURE_SOURCE_TOUCH:
3661 rawPressure = in.touchMajor;
3662 break;
3663 default:
3664 rawPressure = 0;
3665 }
3666
3667 float pressure;
3668 switch (mCalibration.pressureCalibration) {
3669 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3670 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003671 pressure = rawPressure * mPressureScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003672 break;
3673 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003674 pressure = in.isHovering ? 0 : 1;
Jeff Brownace13b12011-03-09 17:39:48 -08003675 break;
3676 }
3677
3678 // TouchMajor and TouchMinor
3679 float touchMajor, touchMinor;
3680 switch (mCalibration.touchSizeCalibration) {
3681 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003682 touchMajor = in.touchMajor * mGeometricScale;
3683 if (mRawPointerAxes.touchMinor.valid) {
3684 touchMinor = in.touchMinor * mGeometricScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003685 } else {
3686 touchMinor = touchMajor;
3687 }
3688 break;
3689 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
3690 touchMajor = toolMajor * pressure;
3691 touchMinor = toolMinor * pressure;
3692 break;
3693 default:
3694 touchMajor = 0;
3695 touchMinor = 0;
3696 break;
3697 }
3698
3699 if (touchMajor > toolMajor) {
3700 touchMajor = toolMajor;
3701 }
3702 if (touchMinor > toolMinor) {
3703 touchMinor = toolMinor;
3704 }
3705
3706 // Size
3707 float size;
3708 switch (mCalibration.sizeCalibration) {
3709 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003710 float rawSize = mRawPointerAxes.toolMinor.valid
Jeff Brownace13b12011-03-09 17:39:48 -08003711 ? avg(in.toolMajor, in.toolMinor)
3712 : in.toolMajor;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003713 size = rawSize * mSizeScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003714 break;
3715 }
3716 default:
3717 size = 0;
3718 break;
3719 }
3720
3721 // Orientation
3722 float orientation;
3723 switch (mCalibration.orientationCalibration) {
3724 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003725 orientation = in.orientation * mOrientationScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003726 break;
3727 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3728 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3729 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3730 if (c1 != 0 || c2 != 0) {
3731 orientation = atan2f(c1, c2) * 0.5f;
Jeff Brown2352b972011-04-12 22:39:53 -07003732 float scale = 1.0f + hypotf(c1, c2) / 16.0f;
Jeff Brownace13b12011-03-09 17:39:48 -08003733 touchMajor *= scale;
3734 touchMinor /= scale;
3735 toolMajor *= scale;
3736 toolMinor /= scale;
3737 } else {
3738 orientation = 0;
3739 }
3740 break;
3741 }
3742 default:
3743 orientation = 0;
3744 }
3745
Jeff Brown80fd47c2011-05-24 01:07:44 -07003746 // Distance
3747 float distance;
3748 switch (mCalibration.distanceCalibration) {
3749 case Calibration::DISTANCE_CALIBRATION_SCALED:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003750 distance = in.distance * mDistanceScale;
Jeff Brown80fd47c2011-05-24 01:07:44 -07003751 break;
3752 default:
3753 distance = 0;
3754 }
3755
Jeff Brownace13b12011-03-09 17:39:48 -08003756 // X and Y
3757 // Adjust coords for surface orientation.
3758 float x, y;
Jeff Brownbe1aa822011-07-27 16:04:54 -07003759 switch (mSurfaceOrientation) {
Jeff Brownace13b12011-03-09 17:39:48 -08003760 case DISPLAY_ORIENTATION_90:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003761 x = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
3762 y = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003763 orientation -= M_PI_2;
3764 if (orientation < - M_PI_2) {
3765 orientation += M_PI;
3766 }
3767 break;
3768 case DISPLAY_ORIENTATION_180:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003769 x = float(mRawPointerAxes.x.maxValue - in.x) * mXScale;
3770 y = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003771 break;
3772 case DISPLAY_ORIENTATION_270:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003773 x = float(mRawPointerAxes.y.maxValue - in.y) * mYScale;
3774 y = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003775 orientation += M_PI_2;
3776 if (orientation > M_PI_2) {
3777 orientation -= M_PI;
3778 }
3779 break;
3780 default:
Jeff Brownbe1aa822011-07-27 16:04:54 -07003781 x = float(in.x - mRawPointerAxes.x.minValue) * mXScale;
3782 y = float(in.y - mRawPointerAxes.y.minValue) * mYScale;
Jeff Brownace13b12011-03-09 17:39:48 -08003783 break;
3784 }
3785
3786 // Write output coords.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003787 PointerCoords& out = mCurrentCookedPointerData.pointerCoords[i];
Jeff Brownace13b12011-03-09 17:39:48 -08003788 out.clear();
3789 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3790 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3791 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3792 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3793 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3794 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3795 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3796 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3797 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003798 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003799
3800 // Write output properties.
Jeff Brownbe1aa822011-07-27 16:04:54 -07003801 PointerProperties& properties = mCurrentCookedPointerData.pointerProperties[i];
3802 uint32_t id = in.id;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003803 properties.clear();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003804 properties.id = id;
3805 properties.toolType = in.toolType;
Jeff Brownace13b12011-03-09 17:39:48 -08003806
Jeff Brownbe1aa822011-07-27 16:04:54 -07003807 // Write id index.
3808 mCurrentCookedPointerData.idToIndex[id] = i;
3809 }
Jeff Brownace13b12011-03-09 17:39:48 -08003810}
3811
Jeff Brown79ac9692011-04-19 21:20:10 -07003812void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3813 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003814 // Update current gesture coordinates.
3815 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07003816 bool sendEvents = preparePointerGestures(when,
3817 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3818 if (!sendEvents) {
3819 return;
3820 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003821 if (finishPreviousGesture) {
3822 cancelPreviousGesture = false;
3823 }
Jeff Brownace13b12011-03-09 17:39:48 -08003824
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003825 // Update the pointer presentation and spots.
3826 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3827 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_SPOT);
3828 if (finishPreviousGesture || cancelPreviousGesture) {
3829 mPointerController->clearSpots();
3830 }
Jeff Browncb5ffcf2011-06-06 20:03:18 -07003831 mPointerController->setSpots(mPointerGesture.currentGestureCoords,
3832 mPointerGesture.currentGestureIdToIndex,
3833 mPointerGesture.currentGestureIdBits);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07003834 } else {
3835 mPointerController->setPresentation(PointerControllerInterface::PRESENTATION_POINTER);
3836 }
Jeff Brown214eaf42011-05-26 19:17:02 -07003837
Jeff Brown538881e2011-05-25 18:23:38 -07003838 // Show or hide the pointer if needed.
3839 switch (mPointerGesture.currentGestureMode) {
3840 case PointerGesture::NEUTRAL:
3841 case PointerGesture::QUIET:
3842 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3843 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3844 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
3845 // Remind the user of where the pointer is after finishing a gesture with spots.
3846 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
3847 }
3848 break;
3849 case PointerGesture::TAP:
3850 case PointerGesture::TAP_DRAG:
3851 case PointerGesture::BUTTON_CLICK_OR_DRAG:
3852 case PointerGesture::HOVER:
3853 case PointerGesture::PRESS:
3854 // Unfade the pointer when the current gesture manipulates the
3855 // area directly under the pointer.
3856 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3857 break;
3858 case PointerGesture::SWIPE:
3859 case PointerGesture::FREEFORM:
3860 // Fade the pointer when the current gesture manipulates a different
3861 // area and there are spots to guide the user experience.
3862 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3863 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3864 } else {
3865 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3866 }
3867 break;
Jeff Brown2352b972011-04-12 22:39:53 -07003868 }
3869
Jeff Brownace13b12011-03-09 17:39:48 -08003870 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003871 int32_t metaState = getContext()->getGlobalMetaState();
Jeff Brownbe1aa822011-07-27 16:04:54 -07003872 int32_t buttonState = mCurrentButtonState;
Jeff Brownace13b12011-03-09 17:39:48 -08003873
3874 // Update last coordinates of pointers that have moved so that we observe the new
3875 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07003876 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
3877 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
3878 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07003879 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08003880 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
3881 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
3882 bool moveNeeded = false;
3883 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07003884 && !mPointerGesture.lastGestureIdBits.isEmpty()
3885 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08003886 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
3887 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003888 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003889 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003890 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003891 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3892 movedGestureIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003893 if (buttonState != mLastButtonState) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003894 moveNeeded = true;
3895 }
Jeff Brownace13b12011-03-09 17:39:48 -08003896 }
3897
3898 // Send motion events for all pointers that went up or were canceled.
3899 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
3900 if (!dispatchedGestureIdBits.isEmpty()) {
3901 if (cancelPreviousGesture) {
3902 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003903 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
3904 AMOTION_EVENT_EDGE_FLAG_NONE,
3905 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003906 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3907 dispatchedGestureIdBits, -1,
3908 0, 0, mPointerGesture.downTime);
3909
3910 dispatchedGestureIdBits.clear();
3911 } else {
3912 BitSet32 upGestureIdBits;
3913 if (finishPreviousGesture) {
3914 upGestureIdBits = dispatchedGestureIdBits;
3915 } else {
3916 upGestureIdBits.value = dispatchedGestureIdBits.value
3917 & ~mPointerGesture.currentGestureIdBits.value;
3918 }
3919 while (!upGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003920 uint32_t id = upGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003921
3922 dispatchMotion(when, policyFlags, mPointerSource,
3923 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003924 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3925 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003926 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3927 dispatchedGestureIdBits, id,
3928 0, 0, mPointerGesture.downTime);
3929
3930 dispatchedGestureIdBits.clearBit(id);
3931 }
3932 }
3933 }
3934
3935 // Send motion events for all pointers that moved.
3936 if (moveNeeded) {
3937 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003938 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3939 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003940 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3941 dispatchedGestureIdBits, -1,
3942 0, 0, mPointerGesture.downTime);
3943 }
3944
3945 // Send motion events for all pointers that went down.
3946 if (down) {
3947 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
3948 & ~dispatchedGestureIdBits.value);
3949 while (!downGestureIdBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07003950 uint32_t id = downGestureIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08003951 dispatchedGestureIdBits.markBit(id);
3952
Jeff Brownace13b12011-03-09 17:39:48 -08003953 if (dispatchedGestureIdBits.count() == 1) {
Jeff Brownace13b12011-03-09 17:39:48 -08003954 mPointerGesture.downTime = when;
3955 }
3956
3957 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Browna6111372011-07-14 21:48:23 -07003958 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003959 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003960 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3961 dispatchedGestureIdBits, id,
3962 0, 0, mPointerGesture.downTime);
3963 }
3964 }
3965
Jeff Brownace13b12011-03-09 17:39:48 -08003966 // Send motion events for hover.
3967 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
3968 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003969 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
3970 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3971 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003972 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3973 mPointerGesture.currentGestureIdBits, -1,
3974 0, 0, mPointerGesture.downTime);
Jeff Brown81346812011-06-28 20:08:48 -07003975 } else if (dispatchedGestureIdBits.isEmpty()
3976 && !mPointerGesture.lastGestureIdBits.isEmpty()) {
3977 // Synthesize a hover move event after all pointers go up to indicate that
3978 // the pointer is hovering again even if the user is not currently touching
3979 // the touch pad. This ensures that a view will receive a fresh hover enter
3980 // event after a tap.
3981 float x, y;
3982 mPointerController->getPosition(&x, &y);
3983
3984 PointerProperties pointerProperties;
3985 pointerProperties.clear();
3986 pointerProperties.id = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07003987 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown81346812011-06-28 20:08:48 -07003988
3989 PointerCoords pointerCoords;
3990 pointerCoords.clear();
3991 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3992 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3993
Jeff Brownbe1aa822011-07-27 16:04:54 -07003994 NotifyMotionArgs args(when, getDeviceId(), mPointerSource, policyFlags,
Jeff Brown81346812011-06-28 20:08:48 -07003995 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
3996 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3997 1, &pointerProperties, &pointerCoords, 0, 0, mPointerGesture.downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07003998 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08003999 }
4000
4001 // Update state.
4002 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
4003 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08004004 mPointerGesture.lastGestureIdBits.clear();
4005 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004006 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
4007 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004008 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004009 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004010 mPointerGesture.lastGestureProperties[index].copyFrom(
4011 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004012 mPointerGesture.lastGestureCoords[index].copyFrom(
4013 mPointerGesture.currentGestureCoords[index]);
4014 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac02010-04-22 18:58:52 -07004015 }
4016 }
4017}
4018
Jeff Brown79ac9692011-04-19 21:20:10 -07004019bool TouchInputMapper::preparePointerGestures(nsecs_t when,
4020 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08004021 *outCancelPreviousGesture = false;
4022 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004023
Jeff Brown79ac9692011-04-19 21:20:10 -07004024 // Handle TAP timeout.
4025 if (isTimeout) {
4026#if DEBUG_GESTURES
4027 LOGD("Gestures: Processing timeout");
4028#endif
4029
4030 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004031 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004032 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07004033 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004034 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004035 } else {
4036 // The tap is finished.
4037#if DEBUG_GESTURES
4038 LOGD("Gestures: TAP finished");
4039#endif
4040 *outFinishPreviousGesture = true;
4041
4042 mPointerGesture.activeGestureId = -1;
4043 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
4044 mPointerGesture.currentGestureIdBits.clear();
4045
Jeff Brown19c97d462011-06-01 12:33:19 -07004046 mPointerGesture.pointerVelocityControl.reset();
Jeff Brown79ac9692011-04-19 21:20:10 -07004047 return true;
4048 }
4049 }
4050
4051 // We did not handle this timeout.
4052 return false;
4053 }
4054
Jeff Brownace13b12011-03-09 17:39:48 -08004055 // Update the velocity tracker.
4056 {
4057 VelocityTracker::Position positions[MAX_POINTERS];
4058 uint32_t count = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004059 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); count++) {
4060 uint32_t id = idBits.clearFirstMarkedBit();
4061 const RawPointerData::Pointer& pointer = mCurrentRawPointerData.pointerForId(id);
4062 positions[count].x = pointer.x * mPointerGestureXMovementScale;
4063 positions[count].y = pointer.y * mPointerGestureYMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004064 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004065 mPointerGesture.velocityTracker.addMovement(when,
4066 mCurrentRawPointerData.touchingIdBits, positions);
Jeff Brownace13b12011-03-09 17:39:48 -08004067 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004068
Jeff Brownace13b12011-03-09 17:39:48 -08004069 // Pick a new active touch id if needed.
4070 // Choose an arbitrary pointer that just went down, if there is one.
4071 // Otherwise choose an arbitrary remaining pointer.
4072 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07004073 // We keep the same active touch id for as long as possible.
4074 bool activeTouchChanged = false;
4075 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
4076 int32_t activeTouchId = lastActiveTouchId;
4077 if (activeTouchId < 0) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004078 if (!mCurrentRawPointerData.touchingIdBits.isEmpty()) {
Jeff Brown2352b972011-04-12 22:39:53 -07004079 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004080 activeTouchId = mPointerGesture.activeTouchId =
4081 mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004082 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08004083 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004084 } else if (!mCurrentRawPointerData.touchingIdBits.hasBit(activeTouchId)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004085 activeTouchChanged = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004086 if (!mCurrentRawPointerData.touchingIdBits.isEmpty()) {
4087 activeTouchId = mPointerGesture.activeTouchId =
4088 mCurrentRawPointerData.touchingIdBits.firstMarkedBit();
Jeff Brown2352b972011-04-12 22:39:53 -07004089 } else {
4090 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08004091 }
4092 }
4093
Jeff Brownbe1aa822011-07-27 16:04:54 -07004094 uint32_t currentTouchingPointerCount = mCurrentRawPointerData.touchingIdBits.count();
4095 uint32_t lastTouchingPointerCount = mLastRawPointerData.touchingIdBits.count();
4096
Jeff Brownace13b12011-03-09 17:39:48 -08004097 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07004098 bool isQuietTime = false;
4099 if (activeTouchId < 0) {
4100 mPointerGesture.resetQuietTime();
4101 } else {
Jeff Brown474dcb52011-06-14 20:22:50 -07004102 isQuietTime = when < mPointerGesture.quietTime + mConfig.pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004103 if (!isQuietTime) {
4104 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
4105 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
4106 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
Jeff Brownbe1aa822011-07-27 16:04:54 -07004107 && currentTouchingPointerCount < 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004108 // Enter quiet time when exiting swipe or freeform state.
4109 // This is to prevent accidentally entering the hover state and flinging the
4110 // pointer when finishing a swipe and there is still one pointer left onscreen.
4111 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07004112 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brownbe1aa822011-07-27 16:04:54 -07004113 && currentTouchingPointerCount >= 2
4114 && !isPointerDown(mCurrentButtonState)) {
Jeff Brown2352b972011-04-12 22:39:53 -07004115 // Enter quiet time when releasing the button and there are still two or more
4116 // fingers down. This may indicate that one finger was used to press the button
4117 // but it has not gone up yet.
4118 isQuietTime = true;
4119 }
4120 if (isQuietTime) {
4121 mPointerGesture.quietTime = when;
4122 }
Jeff Brownace13b12011-03-09 17:39:48 -08004123 }
4124 }
4125
4126 // Switch states based on button and pointer state.
4127 if (isQuietTime) {
4128 // Case 1: Quiet time. (QUIET)
4129#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004130 LOGD("Gestures: QUIET for next %0.3fms", (mPointerGesture.quietTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004131 + mConfig.pointerGestureQuietInterval - when) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004132#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004133 if (mPointerGesture.lastGestureMode != PointerGesture::QUIET) {
4134 *outFinishPreviousGesture = true;
4135 }
Jeff Brownace13b12011-03-09 17:39:48 -08004136
4137 mPointerGesture.activeGestureId = -1;
4138 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08004139 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004140
Jeff Brown19c97d462011-06-01 12:33:19 -07004141 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownbe1aa822011-07-27 16:04:54 -07004142 } else if (isPointerDown(mCurrentButtonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004143 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004144 // The pointer follows the active touch point.
4145 // Emit DOWN, MOVE, UP events at the pointer location.
4146 //
4147 // Only the active touch matters; other fingers are ignored. This policy helps
4148 // to handle the case where the user places a second finger on the touch pad
4149 // to apply the necessary force to depress an integrated button below the surface.
4150 // We don't want the second finger to be delivered to applications.
4151 //
4152 // For this to work well, we need to make sure to track the pointer that is really
4153 // active. If the user first puts one finger down to click then adds another
4154 // finger to drag then the active pointer should switch to the finger that is
4155 // being dragged.
4156#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004157 LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brownbe1aa822011-07-27 16:04:54 -07004158 "currentTouchingPointerCount=%d", activeTouchId, currentTouchingPointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004159#endif
4160 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07004161 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08004162 *outFinishPreviousGesture = true;
4163 mPointerGesture.activeGestureId = 0;
4164 }
4165
4166 // Switch pointers if needed.
4167 // Find the fastest pointer and follow it.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004168 if (activeTouchId >= 0 && currentTouchingPointerCount > 1) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004169 int32_t bestId = -1;
Jeff Brown474dcb52011-06-14 20:22:50 -07004170 float bestSpeed = mConfig.pointerGestureDragMinSwitchSpeed;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004171 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits); !idBits.isEmpty(); ) {
4172 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown19c97d462011-06-01 12:33:19 -07004173 float vx, vy;
4174 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
4175 float speed = hypotf(vx, vy);
4176 if (speed > bestSpeed) {
4177 bestId = id;
4178 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08004179 }
Jeff Brown8d608662010-08-30 03:02:23 -07004180 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004181 }
4182 if (bestId >= 0 && bestId != activeTouchId) {
4183 mPointerGesture.activeTouchId = activeTouchId = bestId;
4184 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004185#if DEBUG_GESTURES
Jeff Brown19c97d462011-06-01 12:33:19 -07004186 LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
4187 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08004188#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004189 }
Jeff Brown19c97d462011-06-01 12:33:19 -07004190 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004191
Jeff Brownbe1aa822011-07-27 16:04:54 -07004192 if (activeTouchId >= 0 && mLastRawPointerData.touchingIdBits.hasBit(activeTouchId)) {
4193 const RawPointerData::Pointer& currentPointer =
4194 mCurrentRawPointerData.pointerForId(activeTouchId);
4195 const RawPointerData::Pointer& lastPointer =
4196 mLastRawPointerData.pointerForId(activeTouchId);
4197 float deltaX = (currentPointer.x - lastPointer.x) * mPointerGestureXMovementScale;
4198 float deltaY = (currentPointer.y - lastPointer.y) * mPointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004199
Jeff Brownbe1aa822011-07-27 16:04:54 -07004200 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004201 mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
4202
4203 // Move the pointer using a relative motion.
4204 // When using spots, the click will occur at the position of the anchor
4205 // spot and all other spots will move there.
4206 mPointerController->move(deltaX, deltaY);
4207 } else {
4208 mPointerGesture.pointerVelocityControl.reset();
Jeff Brown46b9ac02010-04-22 18:58:52 -07004209 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004210
Jeff Brownace13b12011-03-09 17:39:48 -08004211 float x, y;
4212 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08004213
Jeff Brown79ac9692011-04-19 21:20:10 -07004214 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08004215 mPointerGesture.currentGestureIdBits.clear();
4216 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4217 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004218 mPointerGesture.currentGestureProperties[0].clear();
4219 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
Jeff Brown49754db2011-07-01 17:37:58 -07004220 mPointerGesture.currentGestureProperties[0].toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004221 mPointerGesture.currentGestureCoords[0].clear();
4222 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4223 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4224 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004225 } else if (currentTouchingPointerCount == 0) {
Jeff Brownace13b12011-03-09 17:39:48 -08004226 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004227 if (mPointerGesture.lastGestureMode != PointerGesture::NEUTRAL) {
4228 *outFinishPreviousGesture = true;
4229 }
Jeff Brownace13b12011-03-09 17:39:48 -08004230
Jeff Brown79ac9692011-04-19 21:20:10 -07004231 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07004232 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08004233 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07004234 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
4235 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brownbe1aa822011-07-27 16:04:54 -07004236 && lastTouchingPointerCount == 1) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004237 if (when <= mPointerGesture.tapDownTime + mConfig.pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08004238 float x, y;
4239 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004240 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4241 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08004242#if DEBUG_GESTURES
4243 LOGD("Gestures: TAP");
4244#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004245
4246 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07004247 getContext()->requestTimeoutAtTime(when
Jeff Brown474dcb52011-06-14 20:22:50 -07004248 + mConfig.pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07004249
Jeff Brownace13b12011-03-09 17:39:48 -08004250 mPointerGesture.activeGestureId = 0;
4251 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08004252 mPointerGesture.currentGestureIdBits.clear();
4253 mPointerGesture.currentGestureIdBits.markBit(
4254 mPointerGesture.activeGestureId);
4255 mPointerGesture.currentGestureIdToIndex[
4256 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004257 mPointerGesture.currentGestureProperties[0].clear();
4258 mPointerGesture.currentGestureProperties[0].id =
4259 mPointerGesture.activeGestureId;
4260 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004261 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004262 mPointerGesture.currentGestureCoords[0].clear();
4263 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004264 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08004265 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07004266 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004267 mPointerGesture.currentGestureCoords[0].setAxisValue(
4268 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004269
Jeff Brownace13b12011-03-09 17:39:48 -08004270 tapped = true;
4271 } else {
4272#if DEBUG_GESTURES
4273 LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07004274 x - mPointerGesture.tapX,
4275 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004276#endif
4277 }
4278 } else {
4279#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004280 LOGD("Gestures: Not a TAP, %0.3fms since down",
4281 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08004282#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07004283 }
Jeff Brownace13b12011-03-09 17:39:48 -08004284 }
Jeff Brown2352b972011-04-12 22:39:53 -07004285
Jeff Brown19c97d462011-06-01 12:33:19 -07004286 mPointerGesture.pointerVelocityControl.reset();
4287
Jeff Brownace13b12011-03-09 17:39:48 -08004288 if (!tapped) {
4289#if DEBUG_GESTURES
4290 LOGD("Gestures: NEUTRAL");
4291#endif
4292 mPointerGesture.activeGestureId = -1;
4293 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004294 mPointerGesture.currentGestureIdBits.clear();
4295 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004296 } else if (currentTouchingPointerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004297 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004298 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004299 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4300 // When in TAP_DRAG, emit MOVE events at the pointer location.
Jeff Brownb6110c22011-04-01 16:15:13 -07004301 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004302
Jeff Brown79ac9692011-04-19 21:20:10 -07004303 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4304 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown474dcb52011-06-14 20:22:50 -07004305 if (when <= mPointerGesture.tapUpTime + mConfig.pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004306 float x, y;
4307 mPointerController->getPosition(&x, &y);
Jeff Brown474dcb52011-06-14 20:22:50 -07004308 if (fabs(x - mPointerGesture.tapX) <= mConfig.pointerGestureTapSlop
4309 && fabs(y - mPointerGesture.tapY) <= mConfig.pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004310 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4311 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004312#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004313 LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
4314 x - mPointerGesture.tapX,
4315 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004316#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004317 }
4318 } else {
4319#if DEBUG_GESTURES
4320 LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
4321 (when - mPointerGesture.tapUpTime) * 0.000001f);
4322#endif
4323 }
4324 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4325 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4326 }
Jeff Brownace13b12011-03-09 17:39:48 -08004327
Jeff Brownbe1aa822011-07-27 16:04:54 -07004328 if (mLastRawPointerData.touchingIdBits.hasBit(activeTouchId)) {
4329 const RawPointerData::Pointer& currentPointer =
4330 mCurrentRawPointerData.pointerForId(activeTouchId);
4331 const RawPointerData::Pointer& lastPointer =
4332 mLastRawPointerData.pointerForId(activeTouchId);
Jeff Brownace13b12011-03-09 17:39:48 -08004333 float deltaX = (currentPointer.x - lastPointer.x)
Jeff Brownbe1aa822011-07-27 16:04:54 -07004334 * mPointerGestureXMovementScale;
Jeff Brownace13b12011-03-09 17:39:48 -08004335 float deltaY = (currentPointer.y - lastPointer.y)
Jeff Brownbe1aa822011-07-27 16:04:54 -07004336 * mPointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004337
Jeff Brownbe1aa822011-07-27 16:04:54 -07004338 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004339 mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
4340
Jeff Brown2352b972011-04-12 22:39:53 -07004341 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004342 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004343 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004344 } else {
4345 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004346 }
4347
Jeff Brown79ac9692011-04-19 21:20:10 -07004348 bool down;
4349 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4350#if DEBUG_GESTURES
4351 LOGD("Gestures: TAP_DRAG");
4352#endif
4353 down = true;
4354 } else {
4355#if DEBUG_GESTURES
4356 LOGD("Gestures: HOVER");
4357#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004358 if (mPointerGesture.lastGestureMode != PointerGesture::HOVER) {
4359 *outFinishPreviousGesture = true;
4360 }
Jeff Brown79ac9692011-04-19 21:20:10 -07004361 mPointerGesture.activeGestureId = 0;
4362 down = false;
4363 }
Jeff Brownace13b12011-03-09 17:39:48 -08004364
4365 float x, y;
4366 mPointerController->getPosition(&x, &y);
4367
Jeff Brownace13b12011-03-09 17:39:48 -08004368 mPointerGesture.currentGestureIdBits.clear();
4369 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4370 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004371 mPointerGesture.currentGestureProperties[0].clear();
4372 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4373 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004374 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004375 mPointerGesture.currentGestureCoords[0].clear();
4376 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4377 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004378 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4379 down ? 1.0f : 0.0f);
4380
Jeff Brownbe1aa822011-07-27 16:04:54 -07004381 if (lastTouchingPointerCount == 0 && currentTouchingPointerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004382 mPointerGesture.resetTap();
4383 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004384 mPointerGesture.tapX = x;
4385 mPointerGesture.tapY = y;
4386 }
Jeff Brownace13b12011-03-09 17:39:48 -08004387 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004388 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4389 // We need to provide feedback for each finger that goes down so we cannot wait
4390 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004391 //
Jeff Brown2352b972011-04-12 22:39:53 -07004392 // The ambiguous case is deciding what to do when there are two fingers down but they
4393 // have not moved enough to determine whether they are part of a drag or part of a
4394 // freeform gesture, or just a press or long-press at the pointer location.
4395 //
4396 // When there are two fingers we start with the PRESS hypothesis and we generate a
4397 // down at the pointer location.
4398 //
4399 // When the two fingers move enough or when additional fingers are added, we make
4400 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Jeff Brownb6110c22011-04-01 16:15:13 -07004401 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004402
Jeff Brown214eaf42011-05-26 19:17:02 -07004403 bool settled = when >= mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004404 + mConfig.pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004405 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004406 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4407 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004408 *outFinishPreviousGesture = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004409 } else if (!settled && currentTouchingPointerCount > lastTouchingPointerCount) {
Jeff Brown19c97d462011-06-01 12:33:19 -07004410 // Additional pointers have gone down but not yet settled.
4411 // Reset the gesture.
4412#if DEBUG_GESTURES
4413 LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004414 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004415 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Brown19c97d462011-06-01 12:33:19 -07004416 * 0.000001f);
4417#endif
4418 *outCancelPreviousGesture = true;
4419 } else {
4420 // Continue previous gesture.
4421 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4422 }
4423
4424 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004425 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4426 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004427 mPointerGesture.referenceIdBits.clear();
Jeff Brown19c97d462011-06-01 12:33:19 -07004428 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004429
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004430 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004431#if DEBUG_GESTURES
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004432 LOGD("Gestures: Using centroid as reference for MULTITOUCH, "
4433 "settle time remaining %0.3fms", (mPointerGesture.firstTouchTime
Jeff Brown474dcb52011-06-14 20:22:50 -07004434 + mConfig.pointerGestureMultitouchSettleInterval - when)
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004435 * 0.000001f);
Jeff Brown2352b972011-04-12 22:39:53 -07004436#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004437 mCurrentRawPointerData.getCentroidOfTouchingPointers(
4438 &mPointerGesture.referenceTouchX,
Jeff Browncb5ffcf2011-06-06 20:03:18 -07004439 &mPointerGesture.referenceTouchY);
4440 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4441 &mPointerGesture.referenceGestureY);
Jeff Brown2352b972011-04-12 22:39:53 -07004442 }
Jeff Brownace13b12011-03-09 17:39:48 -08004443
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004444 // Clear the reference deltas for fingers not yet included in the reference calculation.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004445 for (BitSet32 idBits(mCurrentRawPointerData.touchingIdBits.value
4446 & ~mPointerGesture.referenceIdBits.value); !idBits.isEmpty(); ) {
4447 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004448 mPointerGesture.referenceDeltas[id].dx = 0;
4449 mPointerGesture.referenceDeltas[id].dy = 0;
4450 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004451 mPointerGesture.referenceIdBits = mCurrentRawPointerData.touchingIdBits;
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004452
4453 // Add delta for all fingers and calculate a common movement delta.
4454 float commonDeltaX = 0, commonDeltaY = 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07004455 BitSet32 commonIdBits(mLastRawPointerData.touchingIdBits.value
4456 & mCurrentRawPointerData.touchingIdBits.value);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004457 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4458 bool first = (idBits == commonIdBits);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004459 uint32_t id = idBits.clearFirstMarkedBit();
4460 const RawPointerData::Pointer& cpd = mCurrentRawPointerData.pointerForId(id);
4461 const RawPointerData::Pointer& lpd = mLastRawPointerData.pointerForId(id);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004462 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4463 delta.dx += cpd.x - lpd.x;
4464 delta.dy += cpd.y - lpd.y;
4465
4466 if (first) {
4467 commonDeltaX = delta.dx;
4468 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004469 } else {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004470 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4471 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
4472 }
4473 }
Jeff Brownace13b12011-03-09 17:39:48 -08004474
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004475 // Consider transitions from PRESS to SWIPE or MULTITOUCH.
4476 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4477 float dist[MAX_POINTER_ID + 1];
4478 int32_t distOverThreshold = 0;
4479 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004480 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004481 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbe1aa822011-07-27 16:04:54 -07004482 dist[id] = hypotf(delta.dx * mPointerGestureXZoomScale,
4483 delta.dy * mPointerGestureYZoomScale);
Jeff Brown474dcb52011-06-14 20:22:50 -07004484 if (dist[id] > mConfig.pointerGestureMultitouchMinDistance) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004485 distOverThreshold += 1;
4486 }
4487 }
4488
4489 // Only transition when at least two pointers have moved further than
4490 // the minimum distance threshold.
4491 if (distOverThreshold >= 2) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004492 if (currentTouchingPointerCount > 2) {
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004493 // There are more than two pointers, switch to FREEFORM.
Jeff Brown2352b972011-04-12 22:39:53 -07004494#if DEBUG_GESTURES
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004495 LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004496 currentTouchingPointerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004497#endif
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004498 *outCancelPreviousGesture = true;
4499 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4500 } else {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004501 // There are exactly two pointers.
4502 BitSet32 idBits(mCurrentRawPointerData.touchingIdBits);
4503 uint32_t id1 = idBits.clearFirstMarkedBit();
4504 uint32_t id2 = idBits.firstMarkedBit();
4505 const RawPointerData::Pointer& p1 = mCurrentRawPointerData.pointerForId(id1);
4506 const RawPointerData::Pointer& p2 = mCurrentRawPointerData.pointerForId(id2);
4507 float mutualDistance = distance(p1.x, p1.y, p2.x, p2.y);
4508 if (mutualDistance > mPointerGestureMaxSwipeWidth) {
4509 // There are two pointers but they are too far apart for a SWIPE,
4510 // switch to FREEFORM.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004511#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004512 LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
4513 mutualDistance, mPointerGestureMaxSwipeWidth);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004514#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004515 *outCancelPreviousGesture = true;
4516 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4517 } else {
4518 // There are two pointers. Wait for both pointers to start moving
4519 // before deciding whether this is a SWIPE or FREEFORM gesture.
4520 float dist1 = dist[id1];
4521 float dist2 = dist[id2];
4522 if (dist1 >= mConfig.pointerGestureMultitouchMinDistance
4523 && dist2 >= mConfig.pointerGestureMultitouchMinDistance) {
4524 // Calculate the dot product of the displacement vectors.
4525 // When the vectors are oriented in approximately the same direction,
4526 // the angle betweeen them is near zero and the cosine of the angle
4527 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
4528 PointerGesture::Delta& delta1 = mPointerGesture.referenceDeltas[id1];
4529 PointerGesture::Delta& delta2 = mPointerGesture.referenceDeltas[id2];
4530 float dx1 = delta1.dx * mPointerGestureXZoomScale;
4531 float dy1 = delta1.dy * mPointerGestureYZoomScale;
4532 float dx2 = delta2.dx * mPointerGestureXZoomScale;
4533 float dy2 = delta2.dy * mPointerGestureYZoomScale;
4534 float dot = dx1 * dx2 + dy1 * dy2;
4535 float cosine = dot / (dist1 * dist2); // denominator always > 0
4536 if (cosine >= mConfig.pointerGestureSwipeTransitionAngleCosine) {
4537 // Pointers are moving in the same direction. Switch to SWIPE.
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004538#if DEBUG_GESTURES
Jeff Brownbe1aa822011-07-27 16:04:54 -07004539 LOGD("Gestures: PRESS transitioned to SWIPE, "
4540 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4541 "cosine %0.3f >= %0.3f",
4542 dist1, mConfig.pointerGestureMultitouchMinDistance,
4543 dist2, mConfig.pointerGestureMultitouchMinDistance,
4544 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004545#endif
Jeff Brownbe1aa822011-07-27 16:04:54 -07004546 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
4547 } else {
4548 // Pointers are moving in different directions. Switch to FREEFORM.
4549#if DEBUG_GESTURES
4550 LOGD("Gestures: PRESS transitioned to FREEFORM, "
4551 "dist1 %0.3f >= %0.3f, dist2 %0.3f >= %0.3f, "
4552 "cosine %0.3f < %0.3f",
4553 dist1, mConfig.pointerGestureMultitouchMinDistance,
4554 dist2, mConfig.pointerGestureMultitouchMinDistance,
4555 cosine, mConfig.pointerGestureSwipeTransitionAngleCosine);
4556#endif
4557 *outCancelPreviousGesture = true;
4558 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4559 }
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004560 }
Jeff Brownace13b12011-03-09 17:39:48 -08004561 }
4562 }
Jeff Brownace13b12011-03-09 17:39:48 -08004563 }
4564 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004565 // Switch from SWIPE to FREEFORM if additional pointers go down.
4566 // Cancel previous gesture.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004567 if (currentTouchingPointerCount > 2) {
Jeff Brown2352b972011-04-12 22:39:53 -07004568#if DEBUG_GESTURES
4569 LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004570 currentTouchingPointerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004571#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004572 *outCancelPreviousGesture = true;
4573 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004574 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07004575 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004576
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004577 // Move the reference points based on the overall group motion of the fingers
4578 // except in PRESS mode while waiting for a transition to occur.
4579 if (mPointerGesture.currentGestureMode != PointerGesture::PRESS
4580 && (commonDeltaX || commonDeltaY)) {
4581 for (BitSet32 idBits(mPointerGesture.referenceIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004582 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brown538881e2011-05-25 18:23:38 -07004583 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004584 delta.dx = 0;
4585 delta.dy = 0;
Jeff Brown2352b972011-04-12 22:39:53 -07004586 }
4587
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004588 mPointerGesture.referenceTouchX += commonDeltaX;
4589 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown538881e2011-05-25 18:23:38 -07004590
Jeff Brownbe1aa822011-07-27 16:04:54 -07004591 commonDeltaX *= mPointerGestureXMovementScale;
4592 commonDeltaY *= mPointerGestureYMovementScale;
Jeff Brown612891e2011-07-15 20:44:17 -07004593
Jeff Brownbe1aa822011-07-27 16:04:54 -07004594 rotateDelta(mSurfaceOrientation, &commonDeltaX, &commonDeltaY);
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004595 mPointerGesture.pointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
Jeff Brown538881e2011-05-25 18:23:38 -07004596
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -07004597 mPointerGesture.referenceGestureX += commonDeltaX;
4598 mPointerGesture.referenceGestureY += commonDeltaY;
Jeff Brown2352b972011-04-12 22:39:53 -07004599 }
4600
4601 // Report gestures.
Jeff Brown612891e2011-07-15 20:44:17 -07004602 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS
4603 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4604 // PRESS or SWIPE mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004605#if DEBUG_GESTURES
Jeff Brown612891e2011-07-15 20:44:17 -07004606 LOGD("Gestures: PRESS or SWIPE activeTouchId=%d,"
Jeff Brown2352b972011-04-12 22:39:53 -07004607 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004608 activeTouchId, mPointerGesture.activeGestureId, currentTouchingPointerCount);
Jeff Brown2352b972011-04-12 22:39:53 -07004609#endif
4610 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
4611
4612 mPointerGesture.currentGestureIdBits.clear();
4613 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4614 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004615 mPointerGesture.currentGestureProperties[0].clear();
4616 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4617 mPointerGesture.currentGestureProperties[0].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004618 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004619 mPointerGesture.currentGestureCoords[0].clear();
4620 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4621 mPointerGesture.referenceGestureX);
4622 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4623 mPointerGesture.referenceGestureY);
4624 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brownace13b12011-03-09 17:39:48 -08004625 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4626 // FREEFORM mode.
4627#if DEBUG_GESTURES
4628 LOGD("Gestures: FREEFORM activeTouchId=%d,"
4629 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brownbe1aa822011-07-27 16:04:54 -07004630 activeTouchId, mPointerGesture.activeGestureId, currentTouchingPointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004631#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004632 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004633
Jeff Brownace13b12011-03-09 17:39:48 -08004634 mPointerGesture.currentGestureIdBits.clear();
4635
4636 BitSet32 mappedTouchIdBits;
4637 BitSet32 usedGestureIdBits;
4638 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4639 // Initially, assign the active gesture id to the active touch point
4640 // if there is one. No other touch id bits are mapped yet.
4641 if (!*outCancelPreviousGesture) {
4642 mappedTouchIdBits.markBit(activeTouchId);
4643 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4644 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4645 mPointerGesture.activeGestureId;
4646 } else {
4647 mPointerGesture.activeGestureId = -1;
4648 }
4649 } else {
4650 // Otherwise, assume we mapped all touches from the previous frame.
4651 // Reuse all mappings that are still applicable.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004652 mappedTouchIdBits.value = mLastRawPointerData.touchingIdBits.value
4653 & mCurrentRawPointerData.touchingIdBits.value;
Jeff Brownace13b12011-03-09 17:39:48 -08004654 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4655
4656 // Check whether we need to choose a new active gesture id because the
4657 // current went went up.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004658 for (BitSet32 upTouchIdBits(mLastRawPointerData.touchingIdBits.value
4659 & ~mCurrentRawPointerData.touchingIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004660 !upTouchIdBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004661 uint32_t upTouchId = upTouchIdBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004662 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4663 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4664 mPointerGesture.activeGestureId = -1;
4665 break;
4666 }
4667 }
4668 }
4669
4670#if DEBUG_GESTURES
4671 LOGD("Gestures: FREEFORM follow up "
4672 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4673 "activeGestureId=%d",
4674 mappedTouchIdBits.value, usedGestureIdBits.value,
4675 mPointerGesture.activeGestureId);
4676#endif
4677
Jeff Brownbe1aa822011-07-27 16:04:54 -07004678 BitSet32 idBits(mCurrentRawPointerData.touchingIdBits);
4679 for (uint32_t i = 0; i < currentTouchingPointerCount; i++) {
4680 uint32_t touchId = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004681 uint32_t gestureId;
4682 if (!mappedTouchIdBits.hasBit(touchId)) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004683 gestureId = usedGestureIdBits.markFirstUnmarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004684 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4685#if DEBUG_GESTURES
4686 LOGD("Gestures: FREEFORM "
4687 "new mapping for touch id %d -> gesture id %d",
4688 touchId, gestureId);
4689#endif
4690 } else {
4691 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4692#if DEBUG_GESTURES
4693 LOGD("Gestures: FREEFORM "
4694 "existing mapping for touch id %d -> gesture id %d",
4695 touchId, gestureId);
4696#endif
4697 }
4698 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4699 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4700
Jeff Brownbe1aa822011-07-27 16:04:54 -07004701 const RawPointerData::Pointer& pointer =
4702 mCurrentRawPointerData.pointerForId(touchId);
4703 float deltaX = (pointer.x - mPointerGesture.referenceTouchX)
4704 * mPointerGestureXZoomScale;
4705 float deltaY = (pointer.y - mPointerGesture.referenceTouchY)
4706 * mPointerGestureYZoomScale;
4707 rotateDelta(mSurfaceOrientation, &deltaX, &deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004708
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004709 mPointerGesture.currentGestureProperties[i].clear();
4710 mPointerGesture.currentGestureProperties[i].id = gestureId;
4711 mPointerGesture.currentGestureProperties[i].toolType =
Jeff Brown49754db2011-07-01 17:37:58 -07004712 AMOTION_EVENT_TOOL_TYPE_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004713 mPointerGesture.currentGestureCoords[i].clear();
4714 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004715 AMOTION_EVENT_AXIS_X, mPointerGesture.referenceGestureX + deltaX);
Jeff Brownace13b12011-03-09 17:39:48 -08004716 mPointerGesture.currentGestureCoords[i].setAxisValue(
Jeff Brown612891e2011-07-15 20:44:17 -07004717 AMOTION_EVENT_AXIS_Y, mPointerGesture.referenceGestureY + deltaY);
Jeff Brownace13b12011-03-09 17:39:48 -08004718 mPointerGesture.currentGestureCoords[i].setAxisValue(
4719 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4720 }
4721
4722 if (mPointerGesture.activeGestureId < 0) {
4723 mPointerGesture.activeGestureId =
4724 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4725#if DEBUG_GESTURES
4726 LOGD("Gestures: FREEFORM new "
4727 "activeGestureId=%d", mPointerGesture.activeGestureId);
4728#endif
4729 }
Jeff Brown2352b972011-04-12 22:39:53 -07004730 }
Jeff Brownace13b12011-03-09 17:39:48 -08004731 }
4732
Jeff Brownbe1aa822011-07-27 16:04:54 -07004733 mPointerController->setButtonState(mCurrentButtonState);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004734
Jeff Brownace13b12011-03-09 17:39:48 -08004735#if DEBUG_GESTURES
4736 LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004737 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4738 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004739 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004740 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4741 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004742 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004743 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004744 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004745 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004746 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004747 LOGD(" currentGesture[%d]: index=%d, toolType=%d, "
4748 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4749 id, index, properties.toolType,
4750 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004751 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4752 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4753 }
4754 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004755 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004756 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004757 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004758 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004759 LOGD(" lastGesture[%d]: index=%d, toolType=%d, "
4760 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4761 id, index, properties.toolType,
4762 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004763 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4764 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4765 }
4766#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004767 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004768}
4769
4770void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004771 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
4772 const PointerProperties* properties, const PointerCoords* coords,
4773 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08004774 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
4775 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004776 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08004777 uint32_t pointerCount = 0;
4778 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004779 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004780 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004781 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004782 pointerCoords[pointerCount].copyFrom(coords[index]);
4783
4784 if (changedId >= 0 && id == uint32_t(changedId)) {
4785 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
4786 }
4787
4788 pointerCount += 1;
4789 }
4790
Jeff Brownb6110c22011-04-01 16:15:13 -07004791 LOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004792
4793 if (changedId >= 0 && pointerCount == 1) {
4794 // Replace initial down and final up action.
4795 // We can compare the action without masking off the changed pointer index
4796 // because we know the index is 0.
4797 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
4798 action = AMOTION_EVENT_ACTION_DOWN;
4799 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
4800 action = AMOTION_EVENT_ACTION_UP;
4801 } else {
4802 // Can't happen.
Jeff Brownb6110c22011-04-01 16:15:13 -07004803 LOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08004804 }
4805 }
4806
Jeff Brownbe1aa822011-07-27 16:04:54 -07004807 NotifyMotionArgs args(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004808 action, flags, metaState, buttonState, edgeFlags,
4809 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownbe1aa822011-07-27 16:04:54 -07004810 getListener()->notifyMotion(&args);
Jeff Brownace13b12011-03-09 17:39:48 -08004811}
4812
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004813bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004814 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004815 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
4816 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08004817 bool changed = false;
4818 while (!idBits.isEmpty()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004819 uint32_t id = idBits.clearFirstMarkedBit();
Jeff Brownace13b12011-03-09 17:39:48 -08004820 uint32_t inIndex = inIdToIndex[id];
4821 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004822
4823 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004824 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004825 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004826 PointerCoords& curOutCoords = outCoords[outIndex];
4827
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004828 if (curInProperties != curOutProperties) {
4829 curOutProperties.copyFrom(curInProperties);
4830 changed = true;
4831 }
4832
Jeff Brownace13b12011-03-09 17:39:48 -08004833 if (curInCoords != curOutCoords) {
4834 curOutCoords.copyFrom(curInCoords);
4835 changed = true;
4836 }
4837 }
4838 return changed;
4839}
4840
4841void TouchInputMapper::fadePointer() {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004842 if (mPointerController != NULL) {
4843 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
4844 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07004845}
4846
Jeff Brownbe1aa822011-07-27 16:04:54 -07004847bool TouchInputMapper::isPointInsideSurface(int32_t x, int32_t y) {
4848 return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue
4849 && y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004850}
4851
Jeff Brownbe1aa822011-07-27 16:04:54 -07004852const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(
Jeff Brown6328cdc2010-07-29 18:18:33 -07004853 int32_t x, int32_t y) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004854 size_t numVirtualKeys = mVirtualKeys.size();
Jeff Brown6328cdc2010-07-29 18:18:33 -07004855 for (size_t i = 0; i < numVirtualKeys; i++) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07004856 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004857
4858#if DEBUG_VIRTUAL_KEYS
4859 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
4860 "left=%d, top=%d, right=%d, bottom=%d",
4861 x, y,
4862 virtualKey.keyCode, virtualKey.scanCode,
4863 virtualKey.hitLeft, virtualKey.hitTop,
4864 virtualKey.hitRight, virtualKey.hitBottom);
4865#endif
4866
4867 if (virtualKey.isHit(x, y)) {
4868 return & virtualKey;
4869 }
4870 }
4871
4872 return NULL;
4873}
4874
Jeff Brownbe1aa822011-07-27 16:04:54 -07004875void TouchInputMapper::assignPointerIds() {
4876 uint32_t currentPointerCount = mCurrentRawPointerData.pointerCount;
4877 uint32_t lastPointerCount = mLastRawPointerData.pointerCount;
4878
4879 mCurrentRawPointerData.clearIdBits();
Jeff Brown6d0fec22010-07-23 21:28:06 -07004880
4881 if (currentPointerCount == 0) {
4882 // No pointers to assign.
Jeff Brownbe1aa822011-07-27 16:04:54 -07004883 return;
4884 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004885
Jeff Brownbe1aa822011-07-27 16:04:54 -07004886 if (lastPointerCount == 0) {
4887 // All pointers are new.
4888 for (uint32_t i = 0; i < currentPointerCount; i++) {
4889 uint32_t id = i;
4890 mCurrentRawPointerData.pointers[i].id = id;
4891 mCurrentRawPointerData.idToIndex[id] = i;
4892 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(i));
4893 }
4894 return;
4895 }
4896
4897 if (currentPointerCount == 1 && lastPointerCount == 1
4898 && mCurrentRawPointerData.pointers[0].toolType
4899 == mLastRawPointerData.pointers[0].toolType) {
4900 // Only one pointer and no change in count so it must have the same id as before.
4901 uint32_t id = mLastRawPointerData.pointers[0].id;
4902 mCurrentRawPointerData.pointers[0].id = id;
4903 mCurrentRawPointerData.idToIndex[id] = 0;
4904 mCurrentRawPointerData.markIdBit(id, mCurrentRawPointerData.isHovering(0));
4905 return;
4906 }
4907
4908 // General case.
4909 // We build a heap of squared euclidean distances between current and last pointers
4910 // associated with the current and last pointer indices. Then, we find the best
4911 // match (by distance) for each current pointer.
4912 // The pointers must have the same tool type but it is possible for them to
4913 // transition from hovering to touching or vice-versa while retaining the same id.
4914 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
4915
4916 uint32_t heapSize = 0;
4917 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
4918 currentPointerIndex++) {
4919 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
4920 lastPointerIndex++) {
4921 const RawPointerData::Pointer& currentPointer =
4922 mCurrentRawPointerData.pointers[currentPointerIndex];
4923 const RawPointerData::Pointer& lastPointer =
4924 mLastRawPointerData.pointers[lastPointerIndex];
4925 if (currentPointer.toolType == lastPointer.toolType) {
4926 int64_t deltaX = currentPointer.x - lastPointer.x;
4927 int64_t deltaY = currentPointer.y - lastPointer.y;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004928
4929 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
4930
4931 // Insert new element into the heap (sift up).
4932 heap[heapSize].currentPointerIndex = currentPointerIndex;
4933 heap[heapSize].lastPointerIndex = lastPointerIndex;
4934 heap[heapSize].distance = distance;
4935 heapSize += 1;
4936 }
4937 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004938 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004939
Jeff Brownbe1aa822011-07-27 16:04:54 -07004940 // Heapify
4941 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
4942 startIndex -= 1;
4943 for (uint32_t parentIndex = startIndex; ;) {
4944 uint32_t childIndex = parentIndex * 2 + 1;
4945 if (childIndex >= heapSize) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004946 break;
4947 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004948
4949 if (childIndex + 1 < heapSize
4950 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4951 childIndex += 1;
4952 }
4953
4954 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4955 break;
4956 }
4957
4958 swap(heap[parentIndex], heap[childIndex]);
4959 parentIndex = childIndex;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004960 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07004961 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004962
4963#if DEBUG_POINTER_ASSIGNMENT
Jeff Brownbe1aa822011-07-27 16:04:54 -07004964 LOGD("assignPointerIds - initial distance min-heap: size=%d", heapSize);
4965 for (size_t i = 0; i < heapSize; i++) {
4966 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4967 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4968 heap[i].distance);
4969 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004970#endif
4971
Jeff Brownbe1aa822011-07-27 16:04:54 -07004972 // Pull matches out by increasing order of distance.
4973 // To avoid reassigning pointers that have already been matched, the loop keeps track
4974 // of which last and current pointers have been matched using the matchedXXXBits variables.
4975 // It also tracks the used pointer id bits.
4976 BitSet32 matchedLastBits(0);
4977 BitSet32 matchedCurrentBits(0);
4978 BitSet32 usedIdBits(0);
4979 bool first = true;
4980 for (uint32_t i = min(currentPointerCount, lastPointerCount); heapSize > 0 && i > 0; i--) {
4981 while (heapSize > 0) {
4982 if (first) {
4983 // The first time through the loop, we just consume the root element of
4984 // the heap (the one with smallest distance).
4985 first = false;
4986 } else {
4987 // Previous iterations consumed the root element of the heap.
4988 // Pop root element off of the heap (sift down).
4989 heap[0] = heap[heapSize];
4990 for (uint32_t parentIndex = 0; ;) {
4991 uint32_t childIndex = parentIndex * 2 + 1;
4992 if (childIndex >= heapSize) {
4993 break;
4994 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004995
Jeff Brownbe1aa822011-07-27 16:04:54 -07004996 if (childIndex + 1 < heapSize
4997 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4998 childIndex += 1;
4999 }
5000
5001 if (heap[parentIndex].distance <= heap[childIndex].distance) {
5002 break;
5003 }
5004
5005 swap(heap[parentIndex], heap[childIndex]);
5006 parentIndex = childIndex;
5007 }
5008
5009#if DEBUG_POINTER_ASSIGNMENT
5010 LOGD("assignPointerIds - reduced distance min-heap: size=%d", heapSize);
5011 for (size_t i = 0; i < heapSize; i++) {
5012 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
5013 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
5014 heap[i].distance);
5015 }
5016#endif
5017 }
5018
5019 heapSize -= 1;
5020
5021 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
5022 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
5023
5024 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
5025 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
5026
5027 matchedCurrentBits.markBit(currentPointerIndex);
5028 matchedLastBits.markBit(lastPointerIndex);
5029
5030 uint32_t id = mLastRawPointerData.pointers[lastPointerIndex].id;
5031 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5032 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5033 mCurrentRawPointerData.markIdBit(id,
5034 mCurrentRawPointerData.isHovering(currentPointerIndex));
5035 usedIdBits.markBit(id);
5036
5037#if DEBUG_POINTER_ASSIGNMENT
5038 LOGD("assignPointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
5039 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
5040#endif
5041 break;
5042 }
5043 }
5044
5045 // Assign fresh ids to pointers that were not matched in the process.
5046 for (uint32_t i = currentPointerCount - matchedCurrentBits.count(); i != 0; i--) {
5047 uint32_t currentPointerIndex = matchedCurrentBits.markFirstUnmarkedBit();
5048 uint32_t id = usedIdBits.markFirstUnmarkedBit();
5049
5050 mCurrentRawPointerData.pointers[currentPointerIndex].id = id;
5051 mCurrentRawPointerData.idToIndex[id] = currentPointerIndex;
5052 mCurrentRawPointerData.markIdBit(id,
5053 mCurrentRawPointerData.isHovering(currentPointerIndex));
5054
5055#if DEBUG_POINTER_ASSIGNMENT
5056 LOGD("assignPointerIds - assigned: cur=%d, id=%d",
5057 currentPointerIndex, id);
5058#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005059 }
5060}
5061
Jeff Brown6d0fec22010-07-23 21:28:06 -07005062int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005063 if (mCurrentVirtualKey.down && mCurrentVirtualKey.keyCode == keyCode) {
5064 return AKEY_STATE_VIRTUAL;
5065 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005066
Jeff Brownbe1aa822011-07-27 16:04:54 -07005067 size_t numVirtualKeys = mVirtualKeys.size();
5068 for (size_t i = 0; i < numVirtualKeys; i++) {
5069 const VirtualKey& virtualKey = mVirtualKeys[i];
5070 if (virtualKey.keyCode == keyCode) {
5071 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005072 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005073 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005074
5075 return AKEY_STATE_UNKNOWN;
5076}
5077
5078int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005079 if (mCurrentVirtualKey.down && mCurrentVirtualKey.scanCode == scanCode) {
5080 return AKEY_STATE_VIRTUAL;
5081 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005082
Jeff Brownbe1aa822011-07-27 16:04:54 -07005083 size_t numVirtualKeys = mVirtualKeys.size();
5084 for (size_t i = 0; i < numVirtualKeys; i++) {
5085 const VirtualKey& virtualKey = mVirtualKeys[i];
5086 if (virtualKey.scanCode == scanCode) {
5087 return AKEY_STATE_UP;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005088 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005089 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005090
5091 return AKEY_STATE_UNKNOWN;
5092}
5093
5094bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5095 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005096 size_t numVirtualKeys = mVirtualKeys.size();
5097 for (size_t i = 0; i < numVirtualKeys; i++) {
5098 const VirtualKey& virtualKey = mVirtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005099
Jeff Brownbe1aa822011-07-27 16:04:54 -07005100 for (size_t i = 0; i < numCodes; i++) {
5101 if (virtualKey.keyCode == keyCodes[i]) {
5102 outFlags[i] = 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005103 }
5104 }
Jeff Brownbe1aa822011-07-27 16:04:54 -07005105 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005106
5107 return true;
5108}
5109
5110
5111// --- SingleTouchInputMapper ---
5112
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005113SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5114 TouchInputMapper(device) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005115 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005116}
5117
5118SingleTouchInputMapper::~SingleTouchInputMapper() {
5119}
5120
Jeff Brown80fd47c2011-05-24 01:07:44 -07005121void SingleTouchInputMapper::clearState() {
Jeff Brown49754db2011-07-01 17:37:58 -07005122 mCursorButtonAccumulator.clearButtons();
5123 mTouchButtonAccumulator.clearButtons();
5124 mSingleTouchMotionAccumulator.clearAbsoluteAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005125}
5126
5127void SingleTouchInputMapper::reset() {
5128 TouchInputMapper::reset();
5129
Jeff Brown80fd47c2011-05-24 01:07:44 -07005130 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005131 }
5132
5133void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07005134 mCursorButtonAccumulator.process(rawEvent);
5135 mTouchButtonAccumulator.process(rawEvent);
5136 mSingleTouchMotionAccumulator.process(rawEvent);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005137
Jeff Brown49754db2011-07-01 17:37:58 -07005138 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
5139 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005140 }
5141}
5142
5143void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005144 mCurrentRawPointerData.clear();
5145 mCurrentButtonState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005146
Jeff Brown49754db2011-07-01 17:37:58 -07005147 if (mTouchButtonAccumulator.isActive()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005148 mCurrentRawPointerData.pointerCount = 1;
5149 mCurrentRawPointerData.idToIndex[0] = 0;
Jeff Brown49754db2011-07-01 17:37:58 -07005150
Jeff Brownbe1aa822011-07-27 16:04:54 -07005151 bool isHovering = mTouchButtonAccumulator.isHovering()
5152 || mSingleTouchMotionAccumulator.getAbsoluteDistance() > 0;
5153 mCurrentRawPointerData.markIdBit(0, isHovering);
Jeff Brown49754db2011-07-01 17:37:58 -07005154
Jeff Brownbe1aa822011-07-27 16:04:54 -07005155 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[0];
Jeff Brown49754db2011-07-01 17:37:58 -07005156 outPointer.id = 0;
5157 outPointer.x = mSingleTouchMotionAccumulator.getAbsoluteX();
5158 outPointer.y = mSingleTouchMotionAccumulator.getAbsoluteY();
5159 outPointer.pressure = mSingleTouchMotionAccumulator.getAbsolutePressure();
5160 outPointer.touchMajor = 0;
5161 outPointer.touchMinor = 0;
5162 outPointer.toolMajor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5163 outPointer.toolMinor = mSingleTouchMotionAccumulator.getAbsoluteToolWidth();
5164 outPointer.orientation = 0;
5165 outPointer.distance = mSingleTouchMotionAccumulator.getAbsoluteDistance();
5166 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5167 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5168 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5169 }
5170 outPointer.isHovering = isHovering;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005171
5172 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
5173 | mCursorButtonAccumulator.getButtonState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005174 }
5175
5176 syncTouch(when, true);
5177}
5178
Jeff Brownbe1aa822011-07-27 16:04:54 -07005179void SingleTouchInputMapper::configureRawPointerAxes() {
5180 TouchInputMapper::configureRawPointerAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005181
Jeff Brown49754db2011-07-01 17:37:58 -07005182 mTouchButtonAccumulator.configure(getDevice());
5183
Jeff Brownbe1aa822011-07-27 16:04:54 -07005184 getAbsoluteAxisInfo(ABS_X, &mRawPointerAxes.x);
5185 getAbsoluteAxisInfo(ABS_Y, &mRawPointerAxes.y);
5186 getAbsoluteAxisInfo(ABS_PRESSURE, &mRawPointerAxes.pressure);
5187 getAbsoluteAxisInfo(ABS_TOOL_WIDTH, &mRawPointerAxes.toolMajor);
5188 getAbsoluteAxisInfo(ABS_DISTANCE, &mRawPointerAxes.distance);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005189}
5190
5191
5192// --- MultiTouchInputMapper ---
5193
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005194MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown49754db2011-07-01 17:37:58 -07005195 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005196}
5197
5198MultiTouchInputMapper::~MultiTouchInputMapper() {
5199}
5200
Jeff Brown80fd47c2011-05-24 01:07:44 -07005201void MultiTouchInputMapper::clearState() {
Jeff Brown49754db2011-07-01 17:37:58 -07005202 mCursorButtonAccumulator.clearButtons();
5203 mTouchButtonAccumulator.clearButtons();
Jeff Brown6894a292011-07-01 17:59:27 -07005204 mPointerIdBits.clear();
Jeff Brown2717eff2011-06-30 23:53:07 -07005205
Jeff Brown49754db2011-07-01 17:37:58 -07005206 if (mMultiTouchMotionAccumulator.isUsingSlotsProtocol()) {
Jeff Brown2717eff2011-06-30 23:53:07 -07005207 // Query the driver for the current slot index and use it as the initial slot
5208 // before we start reading events from the device. It is possible that the
5209 // current slot index will not be the same as it was when the first event was
5210 // written into the evdev buffer, which means the input mapper could start
5211 // out of sync with the initial state of the events in the evdev buffer.
5212 // In the extremely unlikely case that this happens, the data from
5213 // two slots will be confused until the next ABS_MT_SLOT event is received.
5214 // This can cause the touch point to "jump", but at least there will be
5215 // no stuck touches.
Jeff Brown49754db2011-07-01 17:37:58 -07005216 int32_t initialSlot;
Jeff Brown2717eff2011-06-30 23:53:07 -07005217 status_t status = getEventHub()->getAbsoluteAxisValue(getDeviceId(), ABS_MT_SLOT,
Jeff Brown49754db2011-07-01 17:37:58 -07005218 &initialSlot);
Jeff Brown2717eff2011-06-30 23:53:07 -07005219 if (status) {
5220 LOGW("Could not retrieve current multitouch slot index. status=%d", status);
Jeff Brown49754db2011-07-01 17:37:58 -07005221 initialSlot = -1;
Jeff Brown2717eff2011-06-30 23:53:07 -07005222 }
Jeff Brown49754db2011-07-01 17:37:58 -07005223 mMultiTouchMotionAccumulator.clearSlots(initialSlot);
5224 } else {
5225 mMultiTouchMotionAccumulator.clearSlots(-1);
Jeff Brown2717eff2011-06-30 23:53:07 -07005226 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005227}
5228
5229void MultiTouchInputMapper::reset() {
5230 TouchInputMapper::reset();
5231
Jeff Brown80fd47c2011-05-24 01:07:44 -07005232 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005233}
5234
5235void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown49754db2011-07-01 17:37:58 -07005236 mCursorButtonAccumulator.process(rawEvent);
5237 mTouchButtonAccumulator.process(rawEvent);
5238 mMultiTouchMotionAccumulator.process(rawEvent);
Jeff Brownace13b12011-03-09 17:39:48 -08005239
Jeff Brown49754db2011-07-01 17:37:58 -07005240 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
5241 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005242 }
5243}
5244
5245void MultiTouchInputMapper::sync(nsecs_t when) {
Jeff Brown49754db2011-07-01 17:37:58 -07005246 size_t inCount = mMultiTouchMotionAccumulator.getSlotCount();
Jeff Brown80fd47c2011-05-24 01:07:44 -07005247 size_t outCount = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005248 bool havePointerIds = true;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005249 BitSet32 newPointerIdBits;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005250
Jeff Brownbe1aa822011-07-27 16:04:54 -07005251 mCurrentRawPointerData.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005252
Jeff Brown80fd47c2011-05-24 01:07:44 -07005253 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown49754db2011-07-01 17:37:58 -07005254 const MultiTouchMotionAccumulator::Slot* inSlot =
5255 mMultiTouchMotionAccumulator.getSlot(inIndex);
5256 if (!inSlot->isInUse()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07005257 continue;
5258 }
5259
Jeff Brown80fd47c2011-05-24 01:07:44 -07005260 if (outCount >= MAX_POINTERS) {
5261#if DEBUG_POINTERS
5262 LOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
5263 "ignoring the rest.",
5264 getDeviceName().string(), MAX_POINTERS);
5265#endif
5266 break; // too many fingers!
5267 }
5268
Jeff Brownbe1aa822011-07-27 16:04:54 -07005269 RawPointerData::Pointer& outPointer = mCurrentRawPointerData.pointers[outCount];
Jeff Brown49754db2011-07-01 17:37:58 -07005270 outPointer.x = inSlot->getX();
5271 outPointer.y = inSlot->getY();
5272 outPointer.pressure = inSlot->getPressure();
5273 outPointer.touchMajor = inSlot->getTouchMajor();
5274 outPointer.touchMinor = inSlot->getTouchMinor();
5275 outPointer.toolMajor = inSlot->getToolMajor();
5276 outPointer.toolMinor = inSlot->getToolMinor();
5277 outPointer.orientation = inSlot->getOrientation();
5278 outPointer.distance = inSlot->getDistance();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005279
Jeff Brown49754db2011-07-01 17:37:58 -07005280 outPointer.toolType = inSlot->getToolType();
5281 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5282 outPointer.toolType = mTouchButtonAccumulator.getToolType();
5283 if (outPointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
5284 outPointer.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
5285 }
Jeff Brown8d608662010-08-30 03:02:23 -07005286 }
5287
Jeff Brownbe1aa822011-07-27 16:04:54 -07005288 bool isHovering = mTouchButtonAccumulator.isHovering()
Jeff Brown49754db2011-07-01 17:37:58 -07005289 || inSlot->getDistance() > 0;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005290 outPointer.isHovering = isHovering;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005291
Jeff Brown8d608662010-08-30 03:02:23 -07005292 // Assign pointer id using tracking id if available.
Jeff Brown6d0fec22010-07-23 21:28:06 -07005293 if (havePointerIds) {
Jeff Brown49754db2011-07-01 17:37:58 -07005294 int32_t trackingId = inSlot->getTrackingId();
Jeff Brown6894a292011-07-01 17:59:27 -07005295 int32_t id = -1;
Jeff Brown49754db2011-07-01 17:37:58 -07005296 if (trackingId >= 0) {
Jeff Brown6894a292011-07-01 17:59:27 -07005297 for (BitSet32 idBits(mPointerIdBits); !idBits.isEmpty(); ) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005298 uint32_t n = idBits.clearFirstMarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005299 if (mPointerTrackingIdMap[n] == trackingId) {
5300 id = n;
5301 }
5302 }
5303
5304 if (id < 0 && !mPointerIdBits.isFull()) {
Jeff Brownbe1aa822011-07-27 16:04:54 -07005305 id = mPointerIdBits.markFirstUnmarkedBit();
Jeff Brown6894a292011-07-01 17:59:27 -07005306 mPointerTrackingIdMap[id] = trackingId;
5307 }
5308 }
5309 if (id < 0) {
5310 havePointerIds = false;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005311 mCurrentRawPointerData.clearIdBits();
5312 newPointerIdBits.clear();
Jeff Brown6894a292011-07-01 17:59:27 -07005313 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005314 outPointer.id = id;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005315 mCurrentRawPointerData.idToIndex[id] = outCount;
5316 mCurrentRawPointerData.markIdBit(id, isHovering);
5317 newPointerIdBits.markBit(id);
Jeff Brown46b9ac02010-04-22 18:58:52 -07005318 }
5319 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005320
Jeff Brown6d0fec22010-07-23 21:28:06 -07005321 outCount += 1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005322 }
5323
Jeff Brownbe1aa822011-07-27 16:04:54 -07005324 mCurrentRawPointerData.pointerCount = outCount;
5325 mCurrentButtonState = mTouchButtonAccumulator.getButtonState()
5326 | mCursorButtonAccumulator.getButtonState();
Jeff Brownace13b12011-03-09 17:39:48 -08005327
Jeff Brownbe1aa822011-07-27 16:04:54 -07005328 mPointerIdBits = newPointerIdBits;
Jeff Brown6894a292011-07-01 17:59:27 -07005329
Jeff Brown6d0fec22010-07-23 21:28:06 -07005330 syncTouch(when, havePointerIds);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005331
Jeff Brown49754db2011-07-01 17:37:58 -07005332 if (!mMultiTouchMotionAccumulator.isUsingSlotsProtocol()) {
5333 mMultiTouchMotionAccumulator.clearSlots(-1);
Jeff Brown441a9c22011-06-02 18:22:25 -07005334 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005335}
5336
Jeff Brownbe1aa822011-07-27 16:04:54 -07005337void MultiTouchInputMapper::configureRawPointerAxes() {
5338 TouchInputMapper::configureRawPointerAxes();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005339
Jeff Brown49754db2011-07-01 17:37:58 -07005340 mTouchButtonAccumulator.configure(getDevice());
5341
Jeff Brownbe1aa822011-07-27 16:04:54 -07005342 getAbsoluteAxisInfo(ABS_MT_POSITION_X, &mRawPointerAxes.x);
5343 getAbsoluteAxisInfo(ABS_MT_POSITION_Y, &mRawPointerAxes.y);
5344 getAbsoluteAxisInfo(ABS_MT_TOUCH_MAJOR, &mRawPointerAxes.touchMajor);
5345 getAbsoluteAxisInfo(ABS_MT_TOUCH_MINOR, &mRawPointerAxes.touchMinor);
5346 getAbsoluteAxisInfo(ABS_MT_WIDTH_MAJOR, &mRawPointerAxes.toolMajor);
5347 getAbsoluteAxisInfo(ABS_MT_WIDTH_MINOR, &mRawPointerAxes.toolMinor);
5348 getAbsoluteAxisInfo(ABS_MT_ORIENTATION, &mRawPointerAxes.orientation);
5349 getAbsoluteAxisInfo(ABS_MT_PRESSURE, &mRawPointerAxes.pressure);
5350 getAbsoluteAxisInfo(ABS_MT_DISTANCE, &mRawPointerAxes.distance);
5351 getAbsoluteAxisInfo(ABS_MT_TRACKING_ID, &mRawPointerAxes.trackingId);
5352 getAbsoluteAxisInfo(ABS_MT_SLOT, &mRawPointerAxes.slot);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005353
Jeff Brownbe1aa822011-07-27 16:04:54 -07005354 if (mRawPointerAxes.trackingId.valid
5355 && mRawPointerAxes.slot.valid
5356 && mRawPointerAxes.slot.minValue == 0 && mRawPointerAxes.slot.maxValue > 0) {
5357 size_t slotCount = mRawPointerAxes.slot.maxValue + 1;
Jeff Brown49754db2011-07-01 17:37:58 -07005358 if (slotCount > MAX_SLOTS) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005359 LOGW("MultiTouch Device %s reported %d slots but the framework "
5360 "only supports a maximum of %d slots at this time.",
Jeff Brown49754db2011-07-01 17:37:58 -07005361 getDeviceName().string(), slotCount, MAX_SLOTS);
5362 slotCount = MAX_SLOTS;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005363 }
Jeff Brown49754db2011-07-01 17:37:58 -07005364 mMultiTouchMotionAccumulator.configure(slotCount, true /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005365 } else {
Jeff Brown49754db2011-07-01 17:37:58 -07005366 mMultiTouchMotionAccumulator.configure(MAX_POINTERS, false /*usingSlotsProtocol*/);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005367 }
5368
Jeff Brown2717eff2011-06-30 23:53:07 -07005369 clearState();
Jeff Brown9c3cda02010-06-15 01:31:58 -07005370}
5371
Jeff Brown46b9ac02010-04-22 18:58:52 -07005372
Jeff Browncb1404e2011-01-15 18:14:15 -08005373// --- JoystickInputMapper ---
5374
5375JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5376 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005377}
5378
5379JoystickInputMapper::~JoystickInputMapper() {
5380}
5381
5382uint32_t JoystickInputMapper::getSources() {
5383 return AINPUT_SOURCE_JOYSTICK;
5384}
5385
5386void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5387 InputMapper::populateDeviceInfo(info);
5388
Jeff Brown6f2fba42011-02-19 01:08:02 -08005389 for (size_t i = 0; i < mAxes.size(); i++) {
5390 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005391 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5392 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005393 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005394 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5395 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005396 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005397 }
5398}
5399
5400void JoystickInputMapper::dump(String8& dump) {
5401 dump.append(INDENT2 "Joystick Input Mapper:\n");
5402
Jeff Brown6f2fba42011-02-19 01:08:02 -08005403 dump.append(INDENT3 "Axes:\n");
5404 size_t numAxes = mAxes.size();
5405 for (size_t i = 0; i < numAxes; i++) {
5406 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005407 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005408 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005409 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005410 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005411 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005412 }
Jeff Brown85297452011-03-04 13:07:49 -08005413 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5414 label = getAxisLabel(axis.axisInfo.highAxis);
5415 if (label) {
5416 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5417 } else {
5418 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5419 axis.axisInfo.splitValue);
5420 }
5421 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5422 dump.append(" (invert)");
5423 }
5424
5425 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5426 axis.min, axis.max, axis.flat, axis.fuzz);
5427 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5428 "highScale=%0.5f, highOffset=%0.5f\n",
5429 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brownb3a2d132011-06-12 18:14:50 -07005430 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
5431 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Jeff Brown6f2fba42011-02-19 01:08:02 -08005432 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Jeff Brownb3a2d132011-06-12 18:14:50 -07005433 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution);
Jeff Browncb1404e2011-01-15 18:14:15 -08005434 }
5435}
5436
Jeff Brown474dcb52011-06-14 20:22:50 -07005437void JoystickInputMapper::configure(const InputReaderConfiguration* config, uint32_t changes) {
5438 InputMapper::configure(config, changes);
Jeff Browncb1404e2011-01-15 18:14:15 -08005439
Jeff Brown474dcb52011-06-14 20:22:50 -07005440 if (!changes) { // first time only
5441 // Collect all axes.
5442 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
5443 RawAbsoluteAxisInfo rawAxisInfo;
Jeff Brownbe1aa822011-07-27 16:04:54 -07005444 getAbsoluteAxisInfo(abs, &rawAxisInfo);
Jeff Brown474dcb52011-06-14 20:22:50 -07005445 if (rawAxisInfo.valid) {
5446 // Map axis.
5447 AxisInfo axisInfo;
5448 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
5449 if (!explicitlyMapped) {
5450 // Axis is not explicitly mapped, will choose a generic axis later.
5451 axisInfo.mode = AxisInfo::MODE_NORMAL;
5452 axisInfo.axis = -1;
5453 }
5454
5455 // Apply flat override.
5456 int32_t rawFlat = axisInfo.flatOverride < 0
5457 ? rawAxisInfo.flat : axisInfo.flatOverride;
5458
5459 // Calculate scaling factors and limits.
5460 Axis axis;
5461 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5462 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5463 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5464 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5465 scale, 0.0f, highScale, 0.0f,
5466 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5467 } else if (isCenteredAxis(axisInfo.axis)) {
5468 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5469 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
5470 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5471 scale, offset, scale, offset,
5472 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5473 } else {
5474 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5475 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5476 scale, 0.0f, scale, 0.0f,
5477 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5478 }
5479
5480 // To eliminate noise while the joystick is at rest, filter out small variations
5481 // in axis values up front.
5482 axis.filter = axis.flat * 0.25f;
5483
5484 mAxes.add(abs, axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005485 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005486 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005487
Jeff Brown474dcb52011-06-14 20:22:50 -07005488 // If there are too many axes, start dropping them.
5489 // Prefer to keep explicitly mapped axes.
5490 if (mAxes.size() > PointerCoords::MAX_AXES) {
5491 LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
5492 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5493 pruneAxes(true);
5494 pruneAxes(false);
5495 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005496
Jeff Brown474dcb52011-06-14 20:22:50 -07005497 // Assign generic axis ids to remaining axes.
5498 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5499 size_t numAxes = mAxes.size();
5500 for (size_t i = 0; i < numAxes; i++) {
5501 Axis& axis = mAxes.editValueAt(i);
5502 if (axis.axisInfo.axis < 0) {
5503 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5504 && haveAxis(nextGenericAxisId)) {
5505 nextGenericAxisId += 1;
5506 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005507
Jeff Brown474dcb52011-06-14 20:22:50 -07005508 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
5509 axis.axisInfo.axis = nextGenericAxisId;
5510 nextGenericAxisId += 1;
5511 } else {
5512 LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
5513 "have already been assigned to other axes.",
5514 getDeviceName().string(), mAxes.keyAt(i));
5515 mAxes.removeItemsAt(i--);
5516 numAxes -= 1;
5517 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005518 }
5519 }
5520 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005521}
5522
Jeff Brown85297452011-03-04 13:07:49 -08005523bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005524 size_t numAxes = mAxes.size();
5525 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005526 const Axis& axis = mAxes.valueAt(i);
5527 if (axis.axisInfo.axis == axisId
5528 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5529 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005530 return true;
5531 }
5532 }
5533 return false;
5534}
Jeff Browncb1404e2011-01-15 18:14:15 -08005535
Jeff Brown6f2fba42011-02-19 01:08:02 -08005536void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5537 size_t i = mAxes.size();
5538 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5539 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5540 continue;
5541 }
5542 LOGI("Discarding joystick '%s' axis %d because there are too many axes.",
5543 getDeviceName().string(), mAxes.keyAt(i));
5544 mAxes.removeItemsAt(i);
5545 }
5546}
5547
5548bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5549 switch (axis) {
5550 case AMOTION_EVENT_AXIS_X:
5551 case AMOTION_EVENT_AXIS_Y:
5552 case AMOTION_EVENT_AXIS_Z:
5553 case AMOTION_EVENT_AXIS_RX:
5554 case AMOTION_EVENT_AXIS_RY:
5555 case AMOTION_EVENT_AXIS_RZ:
5556 case AMOTION_EVENT_AXIS_HAT_X:
5557 case AMOTION_EVENT_AXIS_HAT_Y:
5558 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005559 case AMOTION_EVENT_AXIS_RUDDER:
5560 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005561 return true;
5562 default:
5563 return false;
5564 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005565}
5566
5567void JoystickInputMapper::reset() {
5568 // Recenter all axes.
5569 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Browncb1404e2011-01-15 18:14:15 -08005570
Jeff Brown6f2fba42011-02-19 01:08:02 -08005571 size_t numAxes = mAxes.size();
5572 for (size_t i = 0; i < numAxes; i++) {
5573 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005574 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005575 }
5576
5577 sync(when, true /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005578
5579 InputMapper::reset();
5580}
5581
5582void JoystickInputMapper::process(const RawEvent* rawEvent) {
5583 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005584 case EV_ABS: {
5585 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
5586 if (index >= 0) {
5587 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005588 float newValue, highNewValue;
5589 switch (axis.axisInfo.mode) {
5590 case AxisInfo::MODE_INVERT:
5591 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5592 * axis.scale + axis.offset;
5593 highNewValue = 0.0f;
5594 break;
5595 case AxisInfo::MODE_SPLIT:
5596 if (rawEvent->value < axis.axisInfo.splitValue) {
5597 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5598 * axis.scale + axis.offset;
5599 highNewValue = 0.0f;
5600 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5601 newValue = 0.0f;
5602 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5603 * axis.highScale + axis.highOffset;
5604 } else {
5605 newValue = 0.0f;
5606 highNewValue = 0.0f;
5607 }
5608 break;
5609 default:
5610 newValue = rawEvent->value * axis.scale + axis.offset;
5611 highNewValue = 0.0f;
5612 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005613 }
Jeff Brown85297452011-03-04 13:07:49 -08005614 axis.newValue = newValue;
5615 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005616 }
5617 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005618 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005619
5620 case EV_SYN:
5621 switch (rawEvent->scanCode) {
5622 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005623 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005624 break;
5625 }
5626 break;
5627 }
5628}
5629
Jeff Brown6f2fba42011-02-19 01:08:02 -08005630void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005631 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005632 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005633 }
5634
5635 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005636 int32_t buttonState = 0;
5637
5638 PointerProperties pointerProperties;
5639 pointerProperties.clear();
5640 pointerProperties.id = 0;
5641 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08005642
Jeff Brown6f2fba42011-02-19 01:08:02 -08005643 PointerCoords pointerCoords;
5644 pointerCoords.clear();
5645
5646 size_t numAxes = mAxes.size();
5647 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005648 const Axis& axis = mAxes.valueAt(i);
5649 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
5650 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5651 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
5652 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005653 }
5654
Jeff Brown56194eb2011-03-02 19:23:13 -08005655 // Moving a joystick axis should not wake the devide because joysticks can
5656 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
5657 // button will likely wake the device.
5658 // TODO: Use the input device configuration to control this behavior more finely.
5659 uint32_t policyFlags = 0;
5660
Jeff Brownbe1aa822011-07-27 16:04:54 -07005661 NotifyMotionArgs args(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005662 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
5663 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Brownbe1aa822011-07-27 16:04:54 -07005664 getListener()->notifyMotion(&args);
Jeff Browncb1404e2011-01-15 18:14:15 -08005665}
5666
Jeff Brown85297452011-03-04 13:07:49 -08005667bool JoystickInputMapper::filterAxes(bool force) {
5668 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005669 size_t numAxes = mAxes.size();
5670 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005671 Axis& axis = mAxes.editValueAt(i);
5672 if (force || hasValueChangedSignificantly(axis.filter,
5673 axis.newValue, axis.currentValue, axis.min, axis.max)) {
5674 axis.currentValue = axis.newValue;
5675 atLeastOneSignificantChange = true;
5676 }
5677 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5678 if (force || hasValueChangedSignificantly(axis.filter,
5679 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
5680 axis.highCurrentValue = axis.highNewValue;
5681 atLeastOneSignificantChange = true;
5682 }
5683 }
5684 }
5685 return atLeastOneSignificantChange;
5686}
5687
5688bool JoystickInputMapper::hasValueChangedSignificantly(
5689 float filter, float newValue, float currentValue, float min, float max) {
5690 if (newValue != currentValue) {
5691 // Filter out small changes in value unless the value is converging on the axis
5692 // bounds or center point. This is intended to reduce the amount of information
5693 // sent to applications by particularly noisy joysticks (such as PS3).
5694 if (fabs(newValue - currentValue) > filter
5695 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
5696 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
5697 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
5698 return true;
5699 }
5700 }
5701 return false;
5702}
5703
5704bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
5705 float filter, float newValue, float currentValue, float thresholdValue) {
5706 float newDistance = fabs(newValue - thresholdValue);
5707 if (newDistance < filter) {
5708 float oldDistance = fabs(currentValue - thresholdValue);
5709 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005710 return true;
5711 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005712 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005713 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005714}
5715
Jeff Brown46b9ac02010-04-22 18:58:52 -07005716} // namespace android