blob: 35e08d4f804626f6862a14f96d9ad7caf5feece4 [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070017#define LOG_TAG "InputReader"
18
19//#define LOG_NDEBUG 0
20
21// Log debug messages for each raw event received from the EventHub.
22#define DEBUG_RAW_EVENTS 0
23
24// Log debug messages about touch screen filtering hacks.
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_HACKS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070026
27// Log debug messages about virtual key processing.
Jeff Brown349703e2010-06-22 01:27:15 -070028#define DEBUG_VIRTUAL_KEYS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070029
30// Log debug messages about pointers.
Jeff Brown9f2106f2011-05-24 14:40:35 -070031#define DEBUG_POINTERS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070032
Jeff Brown5c225b12010-06-16 01:53:36 -070033// Log debug messages about pointer assignment calculations.
34#define DEBUG_POINTER_ASSIGNMENT 0
35
Jeff Brownace13b12011-03-09 17:39:48 -080036// Log debug messages about gesture detection.
37#define DEBUG_GESTURES 0
38
Jeff Brownb4ff35d2011-01-02 16:37:43 -080039#include "InputReader.h"
40
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070041#include <cutils/log.h>
Jeff Brown6b53e8d2010-11-10 16:03:06 -080042#include <ui/Keyboard.h>
Jeff Brown90655042010-12-02 13:50:46 -080043#include <ui/VirtualKeyMap.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070044
45#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070046#include <stdlib.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070047#include <unistd.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070048#include <errno.h>
49#include <limits.h>
Jeff Brownc5ed5912010-07-14 18:48:53 -070050#include <math.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070051
Jeff Brown8d608662010-08-30 03:02:23 -070052#define INDENT " "
Jeff Brownef3d7e82010-09-30 14:33:04 -070053#define INDENT2 " "
54#define INDENT3 " "
55#define INDENT4 " "
Jeff Brown8d608662010-08-30 03:02:23 -070056
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070057namespace android {
58
Jeff Brownace13b12011-03-09 17:39:48 -080059// --- Constants ---
60
Jeff Brown80fd47c2011-05-24 01:07:44 -070061// Maximum number of slots supported when using the slot-based Multitouch Protocol B.
62static const size_t MAX_SLOTS = 32;
63
Jeff Brownace13b12011-03-09 17:39:48 -080064// Quiet time between certain gesture transitions.
65// Time to allow for all fingers or buttons to settle into a stable state before
66// starting a new gesture.
67static const nsecs_t QUIET_INTERVAL = 100 * 1000000; // 100 ms
68
69// The minimum speed that a pointer must travel for us to consider switching the active
70// touch pointer to it during a drag. This threshold is set to avoid switching due
71// to noise from a finger resting on the touch pad (perhaps just pressing it down).
72static const float DRAG_MIN_SWITCH_SPEED = 50.0f; // pixels per second
73
74// Tap gesture delay time.
75// The time between down and up must be less than this to be considered a tap.
Jeff Brown2352b972011-04-12 22:39:53 -070076static const nsecs_t TAP_INTERVAL = 150 * 1000000; // 150 ms
Jeff Brownace13b12011-03-09 17:39:48 -080077
Jeff Brown79ac9692011-04-19 21:20:10 -070078// Tap drag gesture delay time.
79// The time between up and the next up must be greater than this to be considered a
80// drag. Otherwise, the previous tap is finished and a new tap begins.
81static const nsecs_t TAP_DRAG_INTERVAL = 150 * 1000000; // 150 ms
82
Jeff Brownace13b12011-03-09 17:39:48 -080083// The distance in pixels that the pointer is allowed to move from initial down
84// to up and still be called a tap.
Jeff Brown79ac9692011-04-19 21:20:10 -070085static const float TAP_SLOP = 10.0f; // 10 pixels
Jeff Brownace13b12011-03-09 17:39:48 -080086
Jeff Brown2352b972011-04-12 22:39:53 -070087// Time after the first touch points go down to settle on an initial centroid.
88// This is intended to be enough time to handle cases where the user puts down two
89// fingers at almost but not quite exactly the same time.
90static const nsecs_t MULTITOUCH_SETTLE_INTERVAL = 100 * 1000000; // 100ms
Jeff Brownace13b12011-03-09 17:39:48 -080091
Jeff Brown2352b972011-04-12 22:39:53 -070092// The transition from PRESS to SWIPE or FREEFORM gesture mode is made when
93// both of the pointers are moving at least this fast.
94static const float MULTITOUCH_MIN_SPEED = 150.0f; // pixels per second
95
96// The transition from PRESS to SWIPE gesture mode can only occur when the
Jeff Brownace13b12011-03-09 17:39:48 -080097// cosine of the angle between the two vectors is greater than or equal to than this value
98// which indicates that the vectors are oriented in the same direction.
99// When the vectors are oriented in the exactly same direction, the cosine is 1.0.
100// (In exactly opposite directions, the cosine is -1.0.)
101static const float SWIPE_TRANSITION_ANGLE_COSINE = 0.5f; // cosine of 45 degrees
102
Jeff Brown2352b972011-04-12 22:39:53 -0700103// The transition from PRESS to SWIPE gesture mode can only occur when the
104// fingers are no more than this far apart relative to the diagonal size of
105// the touch pad. For example, a ratio of 0.5 means that the fingers must be
106// no more than half the diagonal size of the touch pad apart.
107static const float SWIPE_MAX_WIDTH_RATIO = 0.333f; // 1/3
108
109// The gesture movement speed factor relative to the size of the display.
110// Movement speed applies when the fingers are moving in the same direction.
111// Without acceleration, a full swipe of the touch pad diagonal in movement mode
112// will cover this portion of the display diagonal.
113static const float GESTURE_MOVEMENT_SPEED_RATIO = 0.8f;
114
115// The gesture zoom speed factor relative to the size of the display.
116// Zoom speed applies when the fingers are mostly moving relative to each other
117// to execute a scale gesture or similar.
118// Without acceleration, a full swipe of the touch pad diagonal in zoom mode
119// will cover this portion of the display diagonal.
120static const float GESTURE_ZOOM_SPEED_RATIO = 0.3f;
121
Jeff Brownace13b12011-03-09 17:39:48 -0800122
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700123// --- Static Functions ---
124
125template<typename T>
126inline static T abs(const T& value) {
127 return value < 0 ? - value : value;
128}
129
130template<typename T>
131inline static T min(const T& a, const T& b) {
132 return a < b ? a : b;
133}
134
Jeff Brown5c225b12010-06-16 01:53:36 -0700135template<typename T>
136inline static void swap(T& a, T& b) {
137 T temp = a;
138 a = b;
139 b = temp;
140}
141
Jeff Brown8d608662010-08-30 03:02:23 -0700142inline static float avg(float x, float y) {
143 return (x + y) / 2;
144}
145
Jeff Brown2352b972011-04-12 22:39:53 -0700146inline static float distance(float x1, float y1, float x2, float y2) {
147 return hypotf(x1 - x2, y1 - y2);
Jeff Brownace13b12011-03-09 17:39:48 -0800148}
149
Jeff Brown517bb4c2011-01-14 19:09:23 -0800150inline static int32_t signExtendNybble(int32_t value) {
151 return value >= 8 ? value - 16 : value;
152}
153
Jeff Brownef3d7e82010-09-30 14:33:04 -0700154static inline const char* toString(bool value) {
155 return value ? "true" : "false";
156}
157
Jeff Brown9626b142011-03-03 02:09:54 -0800158static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
159 const int32_t map[][4], size_t mapSize) {
160 if (orientation != DISPLAY_ORIENTATION_0) {
161 for (size_t i = 0; i < mapSize; i++) {
162 if (value == map[i][0]) {
163 return map[i][orientation];
164 }
165 }
166 }
167 return value;
168}
169
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700170static const int32_t keyCodeRotationMap[][4] = {
171 // key codes enumerated counter-clockwise with the original (unrotated) key first
172 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd0358292010-06-30 16:10:35 -0700173 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
174 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
175 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
176 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700177};
Jeff Brown9626b142011-03-03 02:09:54 -0800178static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700179 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
180
181int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brown9626b142011-03-03 02:09:54 -0800182 return rotateValueUsingRotationMap(keyCode, orientation,
183 keyCodeRotationMap, keyCodeRotationMapSize);
184}
185
186static const int32_t edgeFlagRotationMap[][4] = {
187 // edge flags enumerated counter-clockwise with the original (unrotated) edge flag first
188 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
189 { AMOTION_EVENT_EDGE_FLAG_BOTTOM, AMOTION_EVENT_EDGE_FLAG_RIGHT,
190 AMOTION_EVENT_EDGE_FLAG_TOP, AMOTION_EVENT_EDGE_FLAG_LEFT },
191 { AMOTION_EVENT_EDGE_FLAG_RIGHT, AMOTION_EVENT_EDGE_FLAG_TOP,
192 AMOTION_EVENT_EDGE_FLAG_LEFT, AMOTION_EVENT_EDGE_FLAG_BOTTOM },
193 { AMOTION_EVENT_EDGE_FLAG_TOP, AMOTION_EVENT_EDGE_FLAG_LEFT,
194 AMOTION_EVENT_EDGE_FLAG_BOTTOM, AMOTION_EVENT_EDGE_FLAG_RIGHT },
195 { AMOTION_EVENT_EDGE_FLAG_LEFT, AMOTION_EVENT_EDGE_FLAG_BOTTOM,
196 AMOTION_EVENT_EDGE_FLAG_RIGHT, AMOTION_EVENT_EDGE_FLAG_TOP },
197};
198static const size_t edgeFlagRotationMapSize =
199 sizeof(edgeFlagRotationMap) / sizeof(edgeFlagRotationMap[0]);
200
201static int32_t rotateEdgeFlag(int32_t edgeFlag, int32_t orientation) {
202 return rotateValueUsingRotationMap(edgeFlag, orientation,
203 edgeFlagRotationMap, edgeFlagRotationMapSize);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700204}
205
Jeff Brown6d0fec22010-07-23 21:28:06 -0700206static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
207 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
208}
209
Jeff Brownefd32662011-03-08 15:13:06 -0800210static uint32_t getButtonStateForScanCode(int32_t scanCode) {
211 // Currently all buttons are mapped to the primary button.
212 switch (scanCode) {
213 case BTN_LEFT:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700214 return AMOTION_EVENT_BUTTON_PRIMARY;
Jeff Brownefd32662011-03-08 15:13:06 -0800215 case BTN_RIGHT:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700216 return AMOTION_EVENT_BUTTON_SECONDARY;
Jeff Brownefd32662011-03-08 15:13:06 -0800217 case BTN_MIDDLE:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700218 return AMOTION_EVENT_BUTTON_TERTIARY;
Jeff Brownefd32662011-03-08 15:13:06 -0800219 case BTN_SIDE:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700220 return AMOTION_EVENT_BUTTON_BACK;
Jeff Brownefd32662011-03-08 15:13:06 -0800221 case BTN_EXTRA:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700222 return AMOTION_EVENT_BUTTON_FORWARD;
Jeff Brownefd32662011-03-08 15:13:06 -0800223 case BTN_FORWARD:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700224 return AMOTION_EVENT_BUTTON_FORWARD;
Jeff Brownefd32662011-03-08 15:13:06 -0800225 case BTN_BACK:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700226 return AMOTION_EVENT_BUTTON_BACK;
Jeff Brownefd32662011-03-08 15:13:06 -0800227 case BTN_TASK:
Jeff Brownefd32662011-03-08 15:13:06 -0800228 default:
229 return 0;
230 }
231}
232
233// Returns true if the pointer should be reported as being down given the specified
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700234// button states. This determines whether the event is reported as a touch event.
235static bool isPointerDown(int32_t buttonState) {
236 return buttonState &
237 (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY
238 | AMOTION_EVENT_BUTTON_TERTIARY
239 | AMOTION_EVENT_BUTTON_ERASER);
Jeff Brownefd32662011-03-08 15:13:06 -0800240}
241
242static int32_t calculateEdgeFlagsUsingPointerBounds(
243 const sp<PointerControllerInterface>& pointerController, float x, float y) {
244 int32_t edgeFlags = 0;
245 float minX, minY, maxX, maxY;
246 if (pointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
247 if (x <= minX) {
248 edgeFlags |= AMOTION_EVENT_EDGE_FLAG_LEFT;
249 } else if (x >= maxX) {
250 edgeFlags |= AMOTION_EVENT_EDGE_FLAG_RIGHT;
251 }
252 if (y <= minY) {
253 edgeFlags |= AMOTION_EVENT_EDGE_FLAG_TOP;
254 } else if (y >= maxY) {
255 edgeFlags |= AMOTION_EVENT_EDGE_FLAG_BOTTOM;
256 }
257 }
258 return edgeFlags;
259}
260
Jeff Brown2352b972011-04-12 22:39:53 -0700261static void clampPositionUsingPointerBounds(
262 const sp<PointerControllerInterface>& pointerController, float* x, float* y) {
263 float minX, minY, maxX, maxY;
264 if (pointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
265 if (*x < minX) {
266 *x = minX;
267 } else if (*x > maxX) {
268 *x = maxX;
269 }
270 if (*y < minY) {
271 *y = minY;
272 } else if (*y > maxY) {
273 *y = maxY;
274 }
275 }
276}
277
278static float calculateCommonVector(float a, float b) {
279 if (a > 0 && b > 0) {
280 return a < b ? a : b;
281 } else if (a < 0 && b < 0) {
282 return a > b ? a : b;
283 } else {
284 return 0;
285 }
286}
287
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700288static void synthesizeButtonKey(InputReaderContext* context, int32_t action,
289 nsecs_t when, int32_t deviceId, uint32_t source,
290 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState,
291 int32_t buttonState, int32_t keyCode) {
292 if (
293 (action == AKEY_EVENT_ACTION_DOWN
294 && !(lastButtonState & buttonState)
295 && (currentButtonState & buttonState))
296 || (action == AKEY_EVENT_ACTION_UP
297 && (lastButtonState & buttonState)
298 && !(currentButtonState & buttonState))) {
299 context->getDispatcher()->notifyKey(when, deviceId, source, policyFlags,
300 action, 0, keyCode, 0, context->getGlobalMetaState(), when);
301 }
302}
303
304static void synthesizeButtonKeys(InputReaderContext* context, int32_t action,
305 nsecs_t when, int32_t deviceId, uint32_t source,
306 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState) {
307 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
308 lastButtonState, currentButtonState,
309 AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK);
310 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
311 lastButtonState, currentButtonState,
312 AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD);
313}
314
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700315
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700316// --- InputReader ---
317
318InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700319 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700320 const sp<InputDispatcherInterface>& dispatcher) :
Jeff Brown6d0fec22010-07-23 21:28:06 -0700321 mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher),
Jeff Brownaa3855d2011-03-17 01:34:19 -0700322 mGlobalMetaState(0), mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700323 configureExcludedDevices();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700324 updateGlobalMetaState();
325 updateInputConfiguration();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700326}
327
328InputReader::~InputReader() {
329 for (size_t i = 0; i < mDevices.size(); i++) {
330 delete mDevices.valueAt(i);
331 }
332}
333
334void InputReader::loopOnce() {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700335 int32_t timeoutMillis = -1;
336 if (mNextTimeout != LLONG_MAX) {
337 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
338 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
339 }
340
Jeff Brownb7198742011-03-18 18:14:26 -0700341 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
342 if (count) {
343 processEvents(mEventBuffer, count);
344 }
345 if (!count || timeoutMillis == 0) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700346 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
347#if DEBUG_RAW_EVENTS
348 LOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
349#endif
350 mNextTimeout = LLONG_MAX;
351 timeoutExpired(now);
352 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700353}
354
Jeff Brownb7198742011-03-18 18:14:26 -0700355void InputReader::processEvents(const RawEvent* rawEvents, size_t count) {
356 for (const RawEvent* rawEvent = rawEvents; count;) {
357 int32_t type = rawEvent->type;
358 size_t batchSize = 1;
359 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
360 int32_t deviceId = rawEvent->deviceId;
361 while (batchSize < count) {
362 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
363 || rawEvent[batchSize].deviceId != deviceId) {
364 break;
365 }
366 batchSize += 1;
367 }
368#if DEBUG_RAW_EVENTS
369 LOGD("BatchSize: %d Count: %d", batchSize, count);
370#endif
371 processEventsForDevice(deviceId, rawEvent, batchSize);
372 } else {
373 switch (rawEvent->type) {
374 case EventHubInterface::DEVICE_ADDED:
375 addDevice(rawEvent->deviceId);
376 break;
377 case EventHubInterface::DEVICE_REMOVED:
378 removeDevice(rawEvent->deviceId);
379 break;
380 case EventHubInterface::FINISHED_DEVICE_SCAN:
381 handleConfigurationChanged(rawEvent->when);
382 break;
383 default:
Jeff Brownb6110c22011-04-01 16:15:13 -0700384 LOG_ASSERT(false); // can't happen
Jeff Brownb7198742011-03-18 18:14:26 -0700385 break;
386 }
387 }
388 count -= batchSize;
389 rawEvent += batchSize;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700390 }
391}
392
Jeff Brown7342bb92010-10-01 18:55:43 -0700393void InputReader::addDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700394 String8 name = mEventHub->getDeviceName(deviceId);
395 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
396
397 InputDevice* device = createDevice(deviceId, name, classes);
398 device->configure();
399
Jeff Brown8d608662010-08-30 03:02:23 -0700400 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800401 LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700402 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800403 LOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700404 device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700405 }
406
Jeff Brown6d0fec22010-07-23 21:28:06 -0700407 bool added = false;
408 { // acquire device registry writer lock
409 RWLock::AutoWLock _wl(mDeviceRegistryLock);
410
411 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
412 if (deviceIndex < 0) {
413 mDevices.add(deviceId, device);
414 added = true;
415 }
416 } // release device registry writer lock
417
418 if (! added) {
419 LOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
420 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700421 return;
422 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700423}
424
Jeff Brown7342bb92010-10-01 18:55:43 -0700425void InputReader::removeDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700426 bool removed = false;
427 InputDevice* device = NULL;
428 { // acquire device registry writer lock
429 RWLock::AutoWLock _wl(mDeviceRegistryLock);
430
431 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
432 if (deviceIndex >= 0) {
433 device = mDevices.valueAt(deviceIndex);
434 mDevices.removeItemsAt(deviceIndex, 1);
435 removed = true;
436 }
437 } // release device registry writer lock
438
439 if (! removed) {
440 LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700441 return;
442 }
443
Jeff Brown6d0fec22010-07-23 21:28:06 -0700444 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800445 LOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700446 device->getId(), device->getName().string());
447 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800448 LOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700449 device->getId(), device->getName().string(), device->getSources());
450 }
451
Jeff Brown8d608662010-08-30 03:02:23 -0700452 device->reset();
453
Jeff Brown6d0fec22010-07-23 21:28:06 -0700454 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700455}
456
Jeff Brown6d0fec22010-07-23 21:28:06 -0700457InputDevice* InputReader::createDevice(int32_t deviceId, const String8& name, uint32_t classes) {
458 InputDevice* device = new InputDevice(this, deviceId, name);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700459
Jeff Brown56194eb2011-03-02 19:23:13 -0800460 // External devices.
461 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
462 device->setExternal(true);
463 }
464
Jeff Brown6d0fec22010-07-23 21:28:06 -0700465 // Switch-like devices.
466 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
467 device->addMapper(new SwitchInputMapper(device));
468 }
469
470 // Keyboard-like devices.
Jeff Brownefd32662011-03-08 15:13:06 -0800471 uint32_t keyboardSource = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700472 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
473 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800474 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700475 }
476 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
477 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
478 }
479 if (classes & INPUT_DEVICE_CLASS_DPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800480 keyboardSource |= AINPUT_SOURCE_DPAD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700481 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800482 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800483 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
Jeff Browncb1404e2011-01-15 18:14:15 -0800484 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700485
Jeff Brownefd32662011-03-08 15:13:06 -0800486 if (keyboardSource != 0) {
487 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700488 }
489
Jeff Brown83c09682010-12-23 17:50:18 -0800490 // Cursor-like devices.
491 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
492 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700493 }
494
Jeff Brown58a2da82011-01-25 16:02:22 -0800495 // Touchscreens and touchpad devices.
496 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800497 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800498 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800499 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700500 }
501
Jeff Browncb1404e2011-01-15 18:14:15 -0800502 // Joystick-like devices.
503 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
504 device->addMapper(new JoystickInputMapper(device));
505 }
506
Jeff Brown6d0fec22010-07-23 21:28:06 -0700507 return device;
508}
509
Jeff Brownb7198742011-03-18 18:14:26 -0700510void InputReader::processEventsForDevice(int32_t deviceId,
511 const RawEvent* rawEvents, size_t count) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700512 { // acquire device registry reader lock
513 RWLock::AutoRLock _rl(mDeviceRegistryLock);
514
515 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
516 if (deviceIndex < 0) {
517 LOGW("Discarding event for unknown deviceId %d.", deviceId);
518 return;
519 }
520
521 InputDevice* device = mDevices.valueAt(deviceIndex);
522 if (device->isIgnored()) {
523 //LOGD("Discarding event for ignored deviceId %d.", deviceId);
524 return;
525 }
526
Jeff Brownb7198742011-03-18 18:14:26 -0700527 device->process(rawEvents, count);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700528 } // release device registry reader lock
529}
530
Jeff Brownaa3855d2011-03-17 01:34:19 -0700531void InputReader::timeoutExpired(nsecs_t when) {
532 { // acquire device registry reader lock
533 RWLock::AutoRLock _rl(mDeviceRegistryLock);
534
535 for (size_t i = 0; i < mDevices.size(); i++) {
536 InputDevice* device = mDevices.valueAt(i);
537 if (!device->isIgnored()) {
538 device->timeoutExpired(when);
539 }
540 }
541 } // release device registry reader lock
542}
543
Jeff Brownc3db8582010-10-20 15:33:38 -0700544void InputReader::handleConfigurationChanged(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700545 // Reset global meta state because it depends on the list of all configured devices.
546 updateGlobalMetaState();
547
548 // Update input configuration.
549 updateInputConfiguration();
550
551 // Enqueue configuration changed.
552 mDispatcher->notifyConfigurationChanged(when);
553}
554
555void InputReader::configureExcludedDevices() {
556 Vector<String8> excludedDeviceNames;
557 mPolicy->getExcludedDeviceNames(excludedDeviceNames);
558
559 for (size_t i = 0; i < excludedDeviceNames.size(); i++) {
560 mEventHub->addExcludedDevice(excludedDeviceNames[i]);
561 }
562}
563
564void InputReader::updateGlobalMetaState() {
565 { // acquire state lock
566 AutoMutex _l(mStateLock);
567
568 mGlobalMetaState = 0;
569
570 { // acquire device registry reader lock
571 RWLock::AutoRLock _rl(mDeviceRegistryLock);
572
573 for (size_t i = 0; i < mDevices.size(); i++) {
574 InputDevice* device = mDevices.valueAt(i);
575 mGlobalMetaState |= device->getMetaState();
576 }
577 } // release device registry reader lock
578 } // release state lock
579}
580
581int32_t InputReader::getGlobalMetaState() {
582 { // acquire state lock
583 AutoMutex _l(mStateLock);
584
585 return mGlobalMetaState;
586 } // release state lock
587}
588
589void InputReader::updateInputConfiguration() {
590 { // acquire state lock
591 AutoMutex _l(mStateLock);
592
593 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
594 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
595 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
596 { // acquire device registry reader lock
597 RWLock::AutoRLock _rl(mDeviceRegistryLock);
598
599 InputDeviceInfo deviceInfo;
600 for (size_t i = 0; i < mDevices.size(); i++) {
601 InputDevice* device = mDevices.valueAt(i);
602 device->getDeviceInfo(& deviceInfo);
603 uint32_t sources = deviceInfo.getSources();
604
605 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
606 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
607 }
608 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
609 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
610 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
611 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
612 }
613 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
614 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700615 }
616 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700617 } // release device registry reader lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700618
Jeff Brown6d0fec22010-07-23 21:28:06 -0700619 mInputConfiguration.touchScreen = touchScreenConfig;
620 mInputConfiguration.keyboard = keyboardConfig;
621 mInputConfiguration.navigation = navigationConfig;
622 } // release state lock
623}
624
Jeff Brownfe508922011-01-18 15:10:10 -0800625void InputReader::disableVirtualKeysUntil(nsecs_t time) {
626 mDisableVirtualKeysTimeout = time;
627}
628
629bool InputReader::shouldDropVirtualKey(nsecs_t now,
630 InputDevice* device, int32_t keyCode, int32_t scanCode) {
631 if (now < mDisableVirtualKeysTimeout) {
632 LOGI("Dropping virtual key from device %s because virtual keys are "
633 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
634 device->getName().string(),
635 (mDisableVirtualKeysTimeout - now) * 0.000001,
636 keyCode, scanCode);
637 return true;
638 } else {
639 return false;
640 }
641}
642
Jeff Brown05dc66a2011-03-02 14:41:58 -0800643void InputReader::fadePointer() {
644 { // acquire device registry reader lock
645 RWLock::AutoRLock _rl(mDeviceRegistryLock);
646
647 for (size_t i = 0; i < mDevices.size(); i++) {
648 InputDevice* device = mDevices.valueAt(i);
649 device->fadePointer();
650 }
651 } // release device registry reader lock
652}
653
Jeff Brownaa3855d2011-03-17 01:34:19 -0700654void InputReader::requestTimeoutAtTime(nsecs_t when) {
655 if (when < mNextTimeout) {
656 mNextTimeout = when;
657 }
658}
659
Jeff Brown6d0fec22010-07-23 21:28:06 -0700660void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
661 { // acquire state lock
662 AutoMutex _l(mStateLock);
663
664 *outConfiguration = mInputConfiguration;
665 } // release state lock
666}
667
668status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
669 { // acquire device registry reader lock
670 RWLock::AutoRLock _rl(mDeviceRegistryLock);
671
672 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
673 if (deviceIndex < 0) {
674 return NAME_NOT_FOUND;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700675 }
676
Jeff Brown6d0fec22010-07-23 21:28:06 -0700677 InputDevice* device = mDevices.valueAt(deviceIndex);
678 if (device->isIgnored()) {
679 return NAME_NOT_FOUND;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700680 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700681
682 device->getDeviceInfo(outDeviceInfo);
683 return OK;
684 } // release device registy reader lock
685}
686
687void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
688 outDeviceIds.clear();
689
690 { // acquire device registry reader lock
691 RWLock::AutoRLock _rl(mDeviceRegistryLock);
692
693 size_t numDevices = mDevices.size();
694 for (size_t i = 0; i < numDevices; i++) {
695 InputDevice* device = mDevices.valueAt(i);
696 if (! device->isIgnored()) {
697 outDeviceIds.add(device->getId());
698 }
699 }
700 } // release device registy reader lock
701}
702
703int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
704 int32_t keyCode) {
705 return getState(deviceId, sourceMask, keyCode, & InputDevice::getKeyCodeState);
706}
707
708int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
709 int32_t scanCode) {
710 return getState(deviceId, sourceMask, scanCode, & InputDevice::getScanCodeState);
711}
712
713int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
714 return getState(deviceId, sourceMask, switchCode, & InputDevice::getSwitchState);
715}
716
717int32_t InputReader::getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
718 GetStateFunc getStateFunc) {
719 { // acquire device registry reader lock
720 RWLock::AutoRLock _rl(mDeviceRegistryLock);
721
722 int32_t result = AKEY_STATE_UNKNOWN;
723 if (deviceId >= 0) {
724 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
725 if (deviceIndex >= 0) {
726 InputDevice* device = mDevices.valueAt(deviceIndex);
727 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
728 result = (device->*getStateFunc)(sourceMask, code);
729 }
730 }
731 } else {
732 size_t numDevices = mDevices.size();
733 for (size_t i = 0; i < numDevices; i++) {
734 InputDevice* device = mDevices.valueAt(i);
735 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
736 result = (device->*getStateFunc)(sourceMask, code);
737 if (result >= AKEY_STATE_DOWN) {
738 return result;
739 }
740 }
741 }
742 }
743 return result;
744 } // release device registy reader lock
745}
746
747bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
748 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
749 memset(outFlags, 0, numCodes);
750 return markSupportedKeyCodes(deviceId, sourceMask, numCodes, keyCodes, outFlags);
751}
752
753bool InputReader::markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
754 const int32_t* keyCodes, uint8_t* outFlags) {
755 { // acquire device registry reader lock
756 RWLock::AutoRLock _rl(mDeviceRegistryLock);
757 bool result = false;
758 if (deviceId >= 0) {
759 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
760 if (deviceIndex >= 0) {
761 InputDevice* device = mDevices.valueAt(deviceIndex);
762 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
763 result = device->markSupportedKeyCodes(sourceMask,
764 numCodes, keyCodes, outFlags);
765 }
766 }
767 } else {
768 size_t numDevices = mDevices.size();
769 for (size_t i = 0; i < numDevices; i++) {
770 InputDevice* device = mDevices.valueAt(i);
771 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
772 result |= device->markSupportedKeyCodes(sourceMask,
773 numCodes, keyCodes, outFlags);
774 }
775 }
776 }
777 return result;
778 } // release device registy reader lock
779}
780
Jeff Brownb88102f2010-09-08 11:49:43 -0700781void InputReader::dump(String8& dump) {
Jeff Brownf2f487182010-10-01 17:46:21 -0700782 mEventHub->dump(dump);
783 dump.append("\n");
784
785 dump.append("Input Reader State:\n");
786
Jeff Brownef3d7e82010-09-30 14:33:04 -0700787 { // acquire device registry reader lock
788 RWLock::AutoRLock _rl(mDeviceRegistryLock);
Jeff Brownb88102f2010-09-08 11:49:43 -0700789
Jeff Brownef3d7e82010-09-30 14:33:04 -0700790 for (size_t i = 0; i < mDevices.size(); i++) {
791 mDevices.valueAt(i)->dump(dump);
Jeff Brownb88102f2010-09-08 11:49:43 -0700792 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700793 } // release device registy reader lock
Jeff Brownb88102f2010-09-08 11:49:43 -0700794}
795
Jeff Brown6d0fec22010-07-23 21:28:06 -0700796
797// --- InputReaderThread ---
798
799InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
800 Thread(/*canCallJava*/ true), mReader(reader) {
801}
802
803InputReaderThread::~InputReaderThread() {
804}
805
806bool InputReaderThread::threadLoop() {
807 mReader->loopOnce();
808 return true;
809}
810
811
812// --- InputDevice ---
813
814InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) :
Jeff Brown80fd47c2011-05-24 01:07:44 -0700815 mContext(context), mId(id), mName(name), mSources(0),
816 mIsExternal(false), mDropUntilNextSync(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700817}
818
819InputDevice::~InputDevice() {
820 size_t numMappers = mMappers.size();
821 for (size_t i = 0; i < numMappers; i++) {
822 delete mMappers[i];
823 }
824 mMappers.clear();
825}
826
Jeff Brownef3d7e82010-09-30 14:33:04 -0700827void InputDevice::dump(String8& dump) {
828 InputDeviceInfo deviceInfo;
829 getDeviceInfo(& deviceInfo);
830
Jeff Brown90655042010-12-02 13:50:46 -0800831 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700832 deviceInfo.getName().string());
Jeff Brown56194eb2011-03-02 19:23:13 -0800833 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700834 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
835 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800836
Jeff Brownefd32662011-03-08 15:13:06 -0800837 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800838 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700839 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800840 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800841 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
842 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800843 char name[32];
844 if (label) {
845 strncpy(name, label, sizeof(name));
846 name[sizeof(name) - 1] = '\0';
847 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800848 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800849 }
Jeff Brownefd32662011-03-08 15:13:06 -0800850 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
851 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
852 name, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Browncc0c1592011-02-19 05:07:28 -0800853 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700854 }
855
856 size_t numMappers = mMappers.size();
857 for (size_t i = 0; i < numMappers; i++) {
858 InputMapper* mapper = mMappers[i];
859 mapper->dump(dump);
860 }
861}
862
Jeff Brown6d0fec22010-07-23 21:28:06 -0700863void InputDevice::addMapper(InputMapper* mapper) {
864 mMappers.add(mapper);
865}
866
867void InputDevice::configure() {
Jeff Brown8d608662010-08-30 03:02:23 -0700868 if (! isIgnored()) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800869 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
Jeff Brown8d608662010-08-30 03:02:23 -0700870 }
871
Jeff Brown6d0fec22010-07-23 21:28:06 -0700872 mSources = 0;
873
874 size_t numMappers = mMappers.size();
875 for (size_t i = 0; i < numMappers; i++) {
876 InputMapper* mapper = mMappers[i];
877 mapper->configure();
878 mSources |= mapper->getSources();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700879 }
880}
881
Jeff Brown6d0fec22010-07-23 21:28:06 -0700882void InputDevice::reset() {
883 size_t numMappers = mMappers.size();
884 for (size_t i = 0; i < numMappers; i++) {
885 InputMapper* mapper = mMappers[i];
886 mapper->reset();
887 }
888}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700889
Jeff Brownb7198742011-03-18 18:14:26 -0700890void InputDevice::process(const RawEvent* rawEvents, size_t count) {
891 // Process all of the events in order for each mapper.
892 // We cannot simply ask each mapper to process them in bulk because mappers may
893 // have side-effects that must be interleaved. For example, joystick movement events and
894 // gamepad button presses are handled by different mappers but they should be dispatched
895 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700896 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700897 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
898#if DEBUG_RAW_EVENTS
899 LOGD("Input event: device=%d type=0x%04x scancode=0x%04x "
900 "keycode=0x%04x value=0x%04x flags=0x%08x",
901 rawEvent->deviceId, rawEvent->type, rawEvent->scanCode, rawEvent->keyCode,
902 rawEvent->value, rawEvent->flags);
903#endif
904
Jeff Brown80fd47c2011-05-24 01:07:44 -0700905 if (mDropUntilNextSync) {
906 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
907 mDropUntilNextSync = false;
908#if DEBUG_RAW_EVENTS
909 LOGD("Recovered from input event buffer overrun.");
910#endif
911 } else {
912#if DEBUG_RAW_EVENTS
913 LOGD("Dropped input event while waiting for next input sync.");
914#endif
915 }
916 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_DROPPED) {
917 LOGI("Detected input event buffer overrun for device %s.", mName.string());
918 mDropUntilNextSync = true;
919 reset();
920 } else {
921 for (size_t i = 0; i < numMappers; i++) {
922 InputMapper* mapper = mMappers[i];
923 mapper->process(rawEvent);
924 }
Jeff Brownb7198742011-03-18 18:14:26 -0700925 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700926 }
927}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700928
Jeff Brownaa3855d2011-03-17 01:34:19 -0700929void InputDevice::timeoutExpired(nsecs_t when) {
930 size_t numMappers = mMappers.size();
931 for (size_t i = 0; i < numMappers; i++) {
932 InputMapper* mapper = mMappers[i];
933 mapper->timeoutExpired(when);
934 }
935}
936
Jeff Brown6d0fec22010-07-23 21:28:06 -0700937void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
938 outDeviceInfo->initialize(mId, mName);
939
940 size_t numMappers = mMappers.size();
941 for (size_t i = 0; i < numMappers; i++) {
942 InputMapper* mapper = mMappers[i];
943 mapper->populateDeviceInfo(outDeviceInfo);
944 }
945}
946
947int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
948 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
949}
950
951int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
952 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
953}
954
955int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
956 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
957}
958
959int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
960 int32_t result = AKEY_STATE_UNKNOWN;
961 size_t numMappers = mMappers.size();
962 for (size_t i = 0; i < numMappers; i++) {
963 InputMapper* mapper = mMappers[i];
964 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
965 result = (mapper->*getStateFunc)(sourceMask, code);
966 if (result >= AKEY_STATE_DOWN) {
967 return result;
968 }
969 }
970 }
971 return result;
972}
973
974bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
975 const int32_t* keyCodes, uint8_t* outFlags) {
976 bool result = false;
977 size_t numMappers = mMappers.size();
978 for (size_t i = 0; i < numMappers; i++) {
979 InputMapper* mapper = mMappers[i];
980 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
981 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
982 }
983 }
984 return result;
985}
986
987int32_t InputDevice::getMetaState() {
988 int32_t result = 0;
989 size_t numMappers = mMappers.size();
990 for (size_t i = 0; i < numMappers; i++) {
991 InputMapper* mapper = mMappers[i];
992 result |= mapper->getMetaState();
993 }
994 return result;
995}
996
Jeff Brown05dc66a2011-03-02 14:41:58 -0800997void InputDevice::fadePointer() {
998 size_t numMappers = mMappers.size();
999 for (size_t i = 0; i < numMappers; i++) {
1000 InputMapper* mapper = mMappers[i];
1001 mapper->fadePointer();
1002 }
1003}
1004
Jeff Brown6d0fec22010-07-23 21:28:06 -07001005
1006// --- InputMapper ---
1007
1008InputMapper::InputMapper(InputDevice* device) :
1009 mDevice(device), mContext(device->getContext()) {
1010}
1011
1012InputMapper::~InputMapper() {
1013}
1014
1015void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1016 info->addSource(getSources());
1017}
1018
Jeff Brownef3d7e82010-09-30 14:33:04 -07001019void InputMapper::dump(String8& dump) {
1020}
1021
Jeff Brown6d0fec22010-07-23 21:28:06 -07001022void InputMapper::configure() {
1023}
1024
1025void InputMapper::reset() {
1026}
1027
Jeff Brownaa3855d2011-03-17 01:34:19 -07001028void InputMapper::timeoutExpired(nsecs_t when) {
1029}
1030
Jeff Brown6d0fec22010-07-23 21:28:06 -07001031int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1032 return AKEY_STATE_UNKNOWN;
1033}
1034
1035int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1036 return AKEY_STATE_UNKNOWN;
1037}
1038
1039int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1040 return AKEY_STATE_UNKNOWN;
1041}
1042
1043bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1044 const int32_t* keyCodes, uint8_t* outFlags) {
1045 return false;
1046}
1047
1048int32_t InputMapper::getMetaState() {
1049 return 0;
1050}
1051
Jeff Brown05dc66a2011-03-02 14:41:58 -08001052void InputMapper::fadePointer() {
1053}
1054
Jeff Browncb1404e2011-01-15 18:14:15 -08001055void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1056 const RawAbsoluteAxisInfo& axis, const char* name) {
1057 if (axis.valid) {
1058 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d\n",
1059 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz);
1060 } else {
1061 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1062 }
1063}
1064
Jeff Brown6d0fec22010-07-23 21:28:06 -07001065
1066// --- SwitchInputMapper ---
1067
1068SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1069 InputMapper(device) {
1070}
1071
1072SwitchInputMapper::~SwitchInputMapper() {
1073}
1074
1075uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001076 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001077}
1078
1079void SwitchInputMapper::process(const RawEvent* rawEvent) {
1080 switch (rawEvent->type) {
1081 case EV_SW:
1082 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
1083 break;
1084 }
1085}
1086
1087void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownb6997262010-10-08 22:31:17 -07001088 getDispatcher()->notifySwitch(when, switchCode, switchValue, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001089}
1090
1091int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1092 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1093}
1094
1095
1096// --- KeyboardInputMapper ---
1097
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001098KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001099 uint32_t source, int32_t keyboardType) :
1100 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001101 mKeyboardType(keyboardType) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001102 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001103}
1104
1105KeyboardInputMapper::~KeyboardInputMapper() {
1106}
1107
Jeff Brown6328cdc2010-07-29 18:18:33 -07001108void KeyboardInputMapper::initializeLocked() {
1109 mLocked.metaState = AMETA_NONE;
1110 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001111}
1112
1113uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001114 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001115}
1116
1117void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1118 InputMapper::populateDeviceInfo(info);
1119
1120 info->setKeyboardType(mKeyboardType);
1121}
1122
Jeff Brownef3d7e82010-09-30 14:33:04 -07001123void KeyboardInputMapper::dump(String8& dump) {
1124 { // acquire lock
1125 AutoMutex _l(mLock);
1126 dump.append(INDENT2 "Keyboard Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001127 dumpParameters(dump);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001128 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
1129 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mLocked.keyDowns.size());
1130 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mLocked.metaState);
1131 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1132 } // release lock
1133}
1134
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001135
1136void KeyboardInputMapper::configure() {
1137 InputMapper::configure();
1138
1139 // Configure basic parameters.
1140 configureParameters();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001141
1142 // Reset LEDs.
1143 {
1144 AutoMutex _l(mLock);
1145 resetLedStateLocked();
1146 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001147}
1148
1149void KeyboardInputMapper::configureParameters() {
1150 mParameters.orientationAware = false;
1151 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
1152 mParameters.orientationAware);
1153
1154 mParameters.associatedDisplayId = mParameters.orientationAware ? 0 : -1;
1155}
1156
1157void KeyboardInputMapper::dumpParameters(String8& dump) {
1158 dump.append(INDENT3 "Parameters:\n");
1159 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1160 mParameters.associatedDisplayId);
1161 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1162 toString(mParameters.orientationAware));
1163}
1164
Jeff Brown6d0fec22010-07-23 21:28:06 -07001165void KeyboardInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001166 for (;;) {
1167 int32_t keyCode, scanCode;
1168 { // acquire lock
1169 AutoMutex _l(mLock);
1170
1171 // Synthesize key up event on reset if keys are currently down.
1172 if (mLocked.keyDowns.isEmpty()) {
1173 initializeLocked();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001174 resetLedStateLocked();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001175 break; // done
1176 }
1177
1178 const KeyDown& keyDown = mLocked.keyDowns.top();
1179 keyCode = keyDown.keyCode;
1180 scanCode = keyDown.scanCode;
1181 } // release lock
1182
Jeff Brown6d0fec22010-07-23 21:28:06 -07001183 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001184 processKey(when, false, keyCode, scanCode, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001185 }
1186
1187 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001188 getContext()->updateGlobalMetaState();
1189}
1190
1191void KeyboardInputMapper::process(const RawEvent* rawEvent) {
1192 switch (rawEvent->type) {
1193 case EV_KEY: {
1194 int32_t scanCode = rawEvent->scanCode;
1195 if (isKeyboardOrGamepadKey(scanCode)) {
1196 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
1197 rawEvent->flags);
1198 }
1199 break;
1200 }
1201 }
1202}
1203
1204bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
1205 return scanCode < BTN_MOUSE
1206 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001207 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08001208 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001209}
1210
Jeff Brown6328cdc2010-07-29 18:18:33 -07001211void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
1212 int32_t scanCode, uint32_t policyFlags) {
1213 int32_t newMetaState;
1214 nsecs_t downTime;
1215 bool metaStateChanged = false;
1216
1217 { // acquire lock
1218 AutoMutex _l(mLock);
1219
1220 if (down) {
1221 // Rotate key codes according to orientation if needed.
1222 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001223 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001224 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001225 if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1226 NULL, NULL, & orientation)) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001227 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001228 }
1229
1230 keyCode = rotateKeyCode(keyCode, orientation);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001231 }
1232
Jeff Brown6328cdc2010-07-29 18:18:33 -07001233 // Add key down.
1234 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
1235 if (keyDownIndex >= 0) {
1236 // key repeat, be sure to use same keycode as before in case of rotation
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001237 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001238 } else {
1239 // key down
Jeff Brownfe508922011-01-18 15:10:10 -08001240 if ((policyFlags & POLICY_FLAG_VIRTUAL)
1241 && mContext->shouldDropVirtualKey(when,
1242 getDevice(), keyCode, scanCode)) {
1243 return;
1244 }
1245
Jeff Brown6328cdc2010-07-29 18:18:33 -07001246 mLocked.keyDowns.push();
1247 KeyDown& keyDown = mLocked.keyDowns.editTop();
1248 keyDown.keyCode = keyCode;
1249 keyDown.scanCode = scanCode;
1250 }
1251
1252 mLocked.downTime = when;
1253 } else {
1254 // Remove key down.
1255 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
1256 if (keyDownIndex >= 0) {
1257 // key up, be sure to use same keycode as before in case of rotation
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001258 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001259 mLocked.keyDowns.removeAt(size_t(keyDownIndex));
1260 } else {
1261 // key was not actually down
1262 LOGI("Dropping key up from device %s because the key was not down. "
1263 "keyCode=%d, scanCode=%d",
1264 getDeviceName().string(), keyCode, scanCode);
1265 return;
1266 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001267 }
1268
Jeff Brown6328cdc2010-07-29 18:18:33 -07001269 int32_t oldMetaState = mLocked.metaState;
1270 newMetaState = updateMetaState(keyCode, down, oldMetaState);
1271 if (oldMetaState != newMetaState) {
1272 mLocked.metaState = newMetaState;
1273 metaStateChanged = true;
Jeff Brown497a92c2010-09-12 17:55:08 -07001274 updateLedStateLocked(false);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001275 }
Jeff Brownfd0358292010-06-30 16:10:35 -07001276
Jeff Brown6328cdc2010-07-29 18:18:33 -07001277 downTime = mLocked.downTime;
1278 } // release lock
1279
Jeff Brown56194eb2011-03-02 19:23:13 -08001280 // Key down on external an keyboard should wake the device.
1281 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
1282 // For internal keyboards, the key layout file should specify the policy flags for
1283 // each wake key individually.
1284 // TODO: Use the input device configuration to control this behavior more finely.
1285 if (down && getDevice()->isExternal()
1286 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1287 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1288 }
1289
Jeff Brown6328cdc2010-07-29 18:18:33 -07001290 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001291 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001292 }
1293
Jeff Brown05dc66a2011-03-02 14:41:58 -08001294 if (down && !isMetaKey(keyCode)) {
1295 getContext()->fadePointer();
1296 }
1297
Jeff Brownefd32662011-03-08 15:13:06 -08001298 getDispatcher()->notifyKey(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001299 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1300 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001301}
1302
Jeff Brown6328cdc2010-07-29 18:18:33 -07001303ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) {
1304 size_t n = mLocked.keyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001305 for (size_t i = 0; i < n; i++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001306 if (mLocked.keyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001307 return i;
1308 }
1309 }
1310 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001311}
1312
Jeff Brown6d0fec22010-07-23 21:28:06 -07001313int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1314 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1315}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001316
Jeff Brown6d0fec22010-07-23 21:28:06 -07001317int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1318 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1319}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001320
Jeff Brown6d0fec22010-07-23 21:28:06 -07001321bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1322 const int32_t* keyCodes, uint8_t* outFlags) {
1323 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1324}
1325
1326int32_t KeyboardInputMapper::getMetaState() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001327 { // acquire lock
1328 AutoMutex _l(mLock);
1329 return mLocked.metaState;
1330 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001331}
1332
Jeff Brown49ed71d2010-12-06 17:13:33 -08001333void KeyboardInputMapper::resetLedStateLocked() {
1334 initializeLedStateLocked(mLocked.capsLockLedState, LED_CAPSL);
1335 initializeLedStateLocked(mLocked.numLockLedState, LED_NUML);
1336 initializeLedStateLocked(mLocked.scrollLockLedState, LED_SCROLLL);
1337
1338 updateLedStateLocked(true);
1339}
1340
1341void KeyboardInputMapper::initializeLedStateLocked(LockedState::LedState& ledState, int32_t led) {
1342 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
1343 ledState.on = false;
1344}
1345
Jeff Brown497a92c2010-09-12 17:55:08 -07001346void KeyboardInputMapper::updateLedStateLocked(bool reset) {
1347 updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001348 AMETA_CAPS_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001349 updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001350 AMETA_NUM_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001351 updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001352 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001353}
1354
1355void KeyboardInputMapper::updateLedStateForModifierLocked(LockedState::LedState& ledState,
1356 int32_t led, int32_t modifier, bool reset) {
1357 if (ledState.avail) {
1358 bool desiredState = (mLocked.metaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001359 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001360 getEventHub()->setLedState(getDeviceId(), led, desiredState);
1361 ledState.on = desiredState;
1362 }
1363 }
1364}
1365
Jeff Brown6d0fec22010-07-23 21:28:06 -07001366
Jeff Brown83c09682010-12-23 17:50:18 -08001367// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07001368
Jeff Brown83c09682010-12-23 17:50:18 -08001369CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001370 InputMapper(device) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001371 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001372}
1373
Jeff Brown83c09682010-12-23 17:50:18 -08001374CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001375}
1376
Jeff Brown83c09682010-12-23 17:50:18 -08001377uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001378 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001379}
1380
Jeff Brown83c09682010-12-23 17:50:18 -08001381void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001382 InputMapper::populateDeviceInfo(info);
1383
Jeff Brown83c09682010-12-23 17:50:18 -08001384 if (mParameters.mode == Parameters::MODE_POINTER) {
1385 float minX, minY, maxX, maxY;
1386 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08001387 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
1388 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08001389 }
1390 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08001391 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
1392 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08001393 }
Jeff Brownefd32662011-03-08 15:13:06 -08001394 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001395
1396 if (mHaveVWheel) {
Jeff Brownefd32662011-03-08 15:13:06 -08001397 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001398 }
1399 if (mHaveHWheel) {
Jeff Brownefd32662011-03-08 15:13:06 -08001400 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001401 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001402}
1403
Jeff Brown83c09682010-12-23 17:50:18 -08001404void CursorInputMapper::dump(String8& dump) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07001405 { // acquire lock
1406 AutoMutex _l(mLock);
Jeff Brown83c09682010-12-23 17:50:18 -08001407 dump.append(INDENT2 "Cursor Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001408 dumpParameters(dump);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001409 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
1410 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001411 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1412 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001413 dump.appendFormat(INDENT3 "HaveVWheel: %s\n", toString(mHaveVWheel));
1414 dump.appendFormat(INDENT3 "HaveHWheel: %s\n", toString(mHaveHWheel));
1415 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
1416 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brownefd32662011-03-08 15:13:06 -08001417 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mLocked.buttonState);
1418 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mLocked.buttonState)));
Jeff Brownef3d7e82010-09-30 14:33:04 -07001419 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1420 } // release lock
1421}
1422
Jeff Brown83c09682010-12-23 17:50:18 -08001423void CursorInputMapper::configure() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001424 InputMapper::configure();
1425
1426 // Configure basic parameters.
1427 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08001428
1429 // Configure device mode.
1430 switch (mParameters.mode) {
1431 case Parameters::MODE_POINTER:
Jeff Brownefd32662011-03-08 15:13:06 -08001432 mSource = AINPUT_SOURCE_MOUSE;
Jeff Brown83c09682010-12-23 17:50:18 -08001433 mXPrecision = 1.0f;
1434 mYPrecision = 1.0f;
1435 mXScale = 1.0f;
1436 mYScale = 1.0f;
1437 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
1438 break;
1439 case Parameters::MODE_NAVIGATION:
Jeff Brownefd32662011-03-08 15:13:06 -08001440 mSource = AINPUT_SOURCE_TRACKBALL;
Jeff Brown83c09682010-12-23 17:50:18 -08001441 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1442 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1443 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1444 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1445 break;
1446 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001447
1448 mVWheelScale = 1.0f;
1449 mHWheelScale = 1.0f;
Jeff Browncc0c1592011-02-19 05:07:28 -08001450
1451 mHaveVWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_WHEEL);
1452 mHaveHWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_HWHEEL);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001453}
1454
Jeff Brown83c09682010-12-23 17:50:18 -08001455void CursorInputMapper::configureParameters() {
1456 mParameters.mode = Parameters::MODE_POINTER;
1457 String8 cursorModeString;
1458 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
1459 if (cursorModeString == "navigation") {
1460 mParameters.mode = Parameters::MODE_NAVIGATION;
1461 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
1462 LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
1463 }
1464 }
1465
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001466 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08001467 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001468 mParameters.orientationAware);
1469
Jeff Brown83c09682010-12-23 17:50:18 -08001470 mParameters.associatedDisplayId = mParameters.mode == Parameters::MODE_POINTER
1471 || mParameters.orientationAware ? 0 : -1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001472}
1473
Jeff Brown83c09682010-12-23 17:50:18 -08001474void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001475 dump.append(INDENT3 "Parameters:\n");
1476 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1477 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08001478
1479 switch (mParameters.mode) {
1480 case Parameters::MODE_POINTER:
1481 dump.append(INDENT4 "Mode: pointer\n");
1482 break;
1483 case Parameters::MODE_NAVIGATION:
1484 dump.append(INDENT4 "Mode: navigation\n");
1485 break;
1486 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07001487 LOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08001488 }
1489
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001490 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1491 toString(mParameters.orientationAware));
1492}
1493
Jeff Brown83c09682010-12-23 17:50:18 -08001494void CursorInputMapper::initializeLocked() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001495 mAccumulator.clear();
1496
Jeff Brownefd32662011-03-08 15:13:06 -08001497 mLocked.buttonState = 0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001498 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001499}
1500
Jeff Brown83c09682010-12-23 17:50:18 -08001501void CursorInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001502 for (;;) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001503 int32_t buttonState;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001504 { // acquire lock
1505 AutoMutex _l(mLock);
1506
Jeff Brownefd32662011-03-08 15:13:06 -08001507 buttonState = mLocked.buttonState;
1508 if (!buttonState) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001509 initializeLocked();
1510 break; // done
1511 }
1512 } // release lock
1513
Jeff Brown83c09682010-12-23 17:50:18 -08001514 // Synthesize button up event on reset.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001515 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownefd32662011-03-08 15:13:06 -08001516 mAccumulator.clear();
1517 mAccumulator.buttonDown = 0;
1518 mAccumulator.buttonUp = buttonState;
1519 mAccumulator.fields = Accumulator::FIELD_BUTTONS;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001520 sync(when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001521 }
1522
Jeff Brown6d0fec22010-07-23 21:28:06 -07001523 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001524}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001525
Jeff Brown83c09682010-12-23 17:50:18 -08001526void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001527 switch (rawEvent->type) {
Jeff Brownefd32662011-03-08 15:13:06 -08001528 case EV_KEY: {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001529 int32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode);
Jeff Brownefd32662011-03-08 15:13:06 -08001530 if (buttonState) {
1531 if (rawEvent->value) {
1532 mAccumulator.buttonDown = buttonState;
1533 mAccumulator.buttonUp = 0;
1534 } else {
1535 mAccumulator.buttonDown = 0;
1536 mAccumulator.buttonUp = buttonState;
1537 }
1538 mAccumulator.fields |= Accumulator::FIELD_BUTTONS;
1539
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001540 // Sync now since BTN_MOUSE is not necessarily followed by SYN_REPORT and
1541 // we need to ensure that we report the up/down promptly.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001542 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001543 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001544 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001545 break;
Jeff Brownefd32662011-03-08 15:13:06 -08001546 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001547
Jeff Brown6d0fec22010-07-23 21:28:06 -07001548 case EV_REL:
1549 switch (rawEvent->scanCode) {
1550 case REL_X:
1551 mAccumulator.fields |= Accumulator::FIELD_REL_X;
1552 mAccumulator.relX = rawEvent->value;
1553 break;
1554 case REL_Y:
1555 mAccumulator.fields |= Accumulator::FIELD_REL_Y;
1556 mAccumulator.relY = rawEvent->value;
1557 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001558 case REL_WHEEL:
1559 mAccumulator.fields |= Accumulator::FIELD_REL_WHEEL;
1560 mAccumulator.relWheel = rawEvent->value;
1561 break;
1562 case REL_HWHEEL:
1563 mAccumulator.fields |= Accumulator::FIELD_REL_HWHEEL;
1564 mAccumulator.relHWheel = rawEvent->value;
1565 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001566 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001567 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001568
Jeff Brown6d0fec22010-07-23 21:28:06 -07001569 case EV_SYN:
1570 switch (rawEvent->scanCode) {
1571 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001572 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001573 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001574 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001575 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001576 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001577}
1578
Jeff Brown83c09682010-12-23 17:50:18 -08001579void CursorInputMapper::sync(nsecs_t when) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001580 uint32_t fields = mAccumulator.fields;
1581 if (fields == 0) {
1582 return; // no new state changes, so nothing to do
1583 }
1584
Jeff Brown9626b142011-03-03 02:09:54 -08001585 int32_t motionEventAction;
1586 int32_t motionEventEdgeFlags;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001587 int32_t lastButtonState, currentButtonState;
1588 PointerProperties pointerProperties;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001589 PointerCoords pointerCoords;
1590 nsecs_t downTime;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001591 float vscroll, hscroll;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001592 { // acquire lock
1593 AutoMutex _l(mLock);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001594
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001595 lastButtonState = mLocked.buttonState;
1596
Jeff Brownefd32662011-03-08 15:13:06 -08001597 bool down, downChanged;
1598 bool wasDown = isPointerDown(mLocked.buttonState);
1599 bool buttonsChanged = fields & Accumulator::FIELD_BUTTONS;
1600 if (buttonsChanged) {
1601 mLocked.buttonState = (mLocked.buttonState | mAccumulator.buttonDown)
1602 & ~mAccumulator.buttonUp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001603
Jeff Brownefd32662011-03-08 15:13:06 -08001604 down = isPointerDown(mLocked.buttonState);
1605
1606 if (!wasDown && down) {
1607 mLocked.downTime = when;
1608 downChanged = true;
1609 } else if (wasDown && !down) {
1610 downChanged = true;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001611 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08001612 downChanged = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001613 }
Jeff Brownefd32662011-03-08 15:13:06 -08001614 } else {
1615 down = wasDown;
1616 downChanged = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001617 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001618
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001619 currentButtonState = mLocked.buttonState;
1620
Jeff Brown6328cdc2010-07-29 18:18:33 -07001621 downTime = mLocked.downTime;
Jeff Brown83c09682010-12-23 17:50:18 -08001622 float deltaX = fields & Accumulator::FIELD_REL_X ? mAccumulator.relX * mXScale : 0.0f;
1623 float deltaY = fields & Accumulator::FIELD_REL_Y ? mAccumulator.relY * mYScale : 0.0f;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001624
Jeff Brown6328cdc2010-07-29 18:18:33 -07001625 if (downChanged) {
Jeff Brownefd32662011-03-08 15:13:06 -08001626 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
1627 } else if (down || mPointerController == NULL) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001628 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browncc0c1592011-02-19 05:07:28 -08001629 } else {
1630 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001631 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001632
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001633 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
Jeff Brown83c09682010-12-23 17:50:18 -08001634 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001635 // Rotate motion based on display orientation if needed.
1636 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1637 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001638 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1639 NULL, NULL, & orientation)) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001640 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001641 }
1642
1643 float temp;
1644 switch (orientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001645 case DISPLAY_ORIENTATION_90:
Jeff Brown83c09682010-12-23 17:50:18 -08001646 temp = deltaX;
1647 deltaX = deltaY;
1648 deltaY = -temp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001649 break;
1650
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001651 case DISPLAY_ORIENTATION_180:
Jeff Brown83c09682010-12-23 17:50:18 -08001652 deltaX = -deltaX;
1653 deltaY = -deltaY;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001654 break;
1655
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001656 case DISPLAY_ORIENTATION_270:
Jeff Brown83c09682010-12-23 17:50:18 -08001657 temp = deltaX;
1658 deltaX = -deltaY;
1659 deltaY = temp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001660 break;
1661 }
1662 }
Jeff Brown83c09682010-12-23 17:50:18 -08001663
Jeff Brown9626b142011-03-03 02:09:54 -08001664 motionEventEdgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
1665
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001666 pointerProperties.clear();
1667 pointerProperties.id = 0;
1668 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
1669
1670 pointerCoords.clear();
1671
Jeff Brown2352b972011-04-12 22:39:53 -07001672 if (mHaveVWheel && (fields & Accumulator::FIELD_REL_WHEEL)) {
1673 vscroll = mAccumulator.relWheel;
1674 } else {
1675 vscroll = 0;
1676 }
1677 if (mHaveHWheel && (fields & Accumulator::FIELD_REL_HWHEEL)) {
1678 hscroll = mAccumulator.relHWheel;
1679 } else {
1680 hscroll = 0;
1681 }
1682
Jeff Brown83c09682010-12-23 17:50:18 -08001683 if (mPointerController != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -07001684 if (deltaX != 0 || deltaY != 0 || vscroll != 0 || hscroll != 0
1685 || buttonsChanged) {
1686 mPointerController->setPresentation(
1687 PointerControllerInterface::PRESENTATION_POINTER);
1688
1689 if (deltaX != 0 || deltaY != 0) {
1690 mPointerController->move(deltaX, deltaY);
1691 }
1692
1693 if (buttonsChanged) {
1694 mPointerController->setButtonState(mLocked.buttonState);
1695 }
1696
Jeff Brown538881e2011-05-25 18:23:38 -07001697 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08001698 }
Jeff Brownefd32662011-03-08 15:13:06 -08001699
Jeff Brown91c69ab2011-02-14 17:03:18 -08001700 float x, y;
1701 mPointerController->getPosition(&x, &y);
Jeff Brownebbd5d12011-02-17 13:01:34 -08001702 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
1703 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown9626b142011-03-03 02:09:54 -08001704
1705 if (motionEventAction == AMOTION_EVENT_ACTION_DOWN) {
Jeff Brownefd32662011-03-08 15:13:06 -08001706 motionEventEdgeFlags = calculateEdgeFlagsUsingPointerBounds(
1707 mPointerController, x, y);
Jeff Brown9626b142011-03-03 02:09:54 -08001708 }
Jeff Brown83c09682010-12-23 17:50:18 -08001709 } else {
Jeff Brownebbd5d12011-02-17 13:01:34 -08001710 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
1711 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
Jeff Brown83c09682010-12-23 17:50:18 -08001712 }
1713
Jeff Brownefd32662011-03-08 15:13:06 -08001714 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001715 } // release lock
1716
Jeff Brown56194eb2011-03-02 19:23:13 -08001717 // Moving an external trackball or mouse should wake the device.
1718 // We don't do this for internal cursor devices to prevent them from waking up
1719 // the device in your pocket.
1720 // TODO: Use the input device configuration to control this behavior more finely.
1721 uint32_t policyFlags = 0;
1722 if (getDevice()->isExternal()) {
1723 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1724 }
1725
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001726 // Synthesize key down from buttons if needed.
1727 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
1728 policyFlags, lastButtonState, currentButtonState);
1729
1730 // Send motion event.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001731 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownefd32662011-03-08 15:13:06 -08001732 getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001733 motionEventAction, 0, metaState, currentButtonState, motionEventEdgeFlags,
1734 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownb6997262010-10-08 22:31:17 -07001735
Jeff Browna032cc02011-03-07 16:56:21 -08001736 // Send hover move after UP to tell the application that the mouse is hovering now.
1737 if (motionEventAction == AMOTION_EVENT_ACTION_UP
1738 && mPointerController != NULL) {
1739 getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001740 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
1741 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
1742 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Browna032cc02011-03-07 16:56:21 -08001743 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001744
Jeff Browna032cc02011-03-07 16:56:21 -08001745 // Send scroll events.
Jeff Brown33bbfd22011-02-24 20:55:35 -08001746 if (vscroll != 0 || hscroll != 0) {
1747 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
1748 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
1749
Jeff Brownefd32662011-03-08 15:13:06 -08001750 getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001751 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
1752 AMOTION_EVENT_EDGE_FLAG_NONE,
1753 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brown33bbfd22011-02-24 20:55:35 -08001754 }
Jeff Browna032cc02011-03-07 16:56:21 -08001755
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001756 // Synthesize key up from buttons if needed.
1757 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
1758 policyFlags, lastButtonState, currentButtonState);
1759
Jeff Browna032cc02011-03-07 16:56:21 -08001760 mAccumulator.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001761}
1762
Jeff Brown83c09682010-12-23 17:50:18 -08001763int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07001764 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
1765 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1766 } else {
1767 return AKEY_STATE_UNKNOWN;
1768 }
1769}
1770
Jeff Brown05dc66a2011-03-02 14:41:58 -08001771void CursorInputMapper::fadePointer() {
1772 { // acquire lock
1773 AutoMutex _l(mLock);
Jeff Brownefd32662011-03-08 15:13:06 -08001774 if (mPointerController != NULL) {
Jeff Brown538881e2011-05-25 18:23:38 -07001775 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
Jeff Brownefd32662011-03-08 15:13:06 -08001776 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08001777 } // release lock
1778}
1779
Jeff Brown6d0fec22010-07-23 21:28:06 -07001780
1781// --- TouchInputMapper ---
1782
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001783TouchInputMapper::TouchInputMapper(InputDevice* device) :
1784 InputMapper(device) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001785 mLocked.surfaceOrientation = -1;
1786 mLocked.surfaceWidth = -1;
1787 mLocked.surfaceHeight = -1;
1788
1789 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001790}
1791
1792TouchInputMapper::~TouchInputMapper() {
1793}
1794
1795uint32_t TouchInputMapper::getSources() {
Jeff Brownace13b12011-03-09 17:39:48 -08001796 return mTouchSource | mPointerSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001797}
1798
1799void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1800 InputMapper::populateDeviceInfo(info);
1801
Jeff Brown6328cdc2010-07-29 18:18:33 -07001802 { // acquire lock
1803 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001804
Jeff Brown6328cdc2010-07-29 18:18:33 -07001805 // Ensure surface information is up to date so that orientation changes are
1806 // noticed immediately.
Jeff Brownefd32662011-03-08 15:13:06 -08001807 if (!configureSurfaceLocked()) {
1808 return;
1809 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001810
Jeff Brownefd32662011-03-08 15:13:06 -08001811 info->addMotionRange(mLocked.orientedRanges.x);
1812 info->addMotionRange(mLocked.orientedRanges.y);
Jeff Brown8d608662010-08-30 03:02:23 -07001813
1814 if (mLocked.orientedRanges.havePressure) {
Jeff Brownefd32662011-03-08 15:13:06 -08001815 info->addMotionRange(mLocked.orientedRanges.pressure);
Jeff Brown8d608662010-08-30 03:02:23 -07001816 }
1817
1818 if (mLocked.orientedRanges.haveSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08001819 info->addMotionRange(mLocked.orientedRanges.size);
Jeff Brown8d608662010-08-30 03:02:23 -07001820 }
1821
Jeff Brownc6d282b2010-10-14 21:42:15 -07001822 if (mLocked.orientedRanges.haveTouchSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08001823 info->addMotionRange(mLocked.orientedRanges.touchMajor);
1824 info->addMotionRange(mLocked.orientedRanges.touchMinor);
Jeff Brown8d608662010-08-30 03:02:23 -07001825 }
1826
Jeff Brownc6d282b2010-10-14 21:42:15 -07001827 if (mLocked.orientedRanges.haveToolSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08001828 info->addMotionRange(mLocked.orientedRanges.toolMajor);
1829 info->addMotionRange(mLocked.orientedRanges.toolMinor);
Jeff Brown8d608662010-08-30 03:02:23 -07001830 }
1831
1832 if (mLocked.orientedRanges.haveOrientation) {
Jeff Brownefd32662011-03-08 15:13:06 -08001833 info->addMotionRange(mLocked.orientedRanges.orientation);
Jeff Brown8d608662010-08-30 03:02:23 -07001834 }
Jeff Brownace13b12011-03-09 17:39:48 -08001835
Jeff Brown80fd47c2011-05-24 01:07:44 -07001836 if (mLocked.orientedRanges.haveDistance) {
1837 info->addMotionRange(mLocked.orientedRanges.distance);
1838 }
1839
Jeff Brownace13b12011-03-09 17:39:48 -08001840 if (mPointerController != NULL) {
1841 float minX, minY, maxX, maxY;
1842 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
1843 info->addMotionRange(AMOTION_EVENT_AXIS_X, mPointerSource,
1844 minX, maxX, 0.0f, 0.0f);
1845 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mPointerSource,
1846 minY, maxY, 0.0f, 0.0f);
1847 }
1848 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mPointerSource,
1849 0.0f, 1.0f, 0.0f, 0.0f);
1850 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001851 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001852}
1853
Jeff Brownef3d7e82010-09-30 14:33:04 -07001854void TouchInputMapper::dump(String8& dump) {
1855 { // acquire lock
1856 AutoMutex _l(mLock);
1857 dump.append(INDENT2 "Touch Input Mapper:\n");
Jeff Brownef3d7e82010-09-30 14:33:04 -07001858 dumpParameters(dump);
1859 dumpVirtualKeysLocked(dump);
1860 dumpRawAxes(dump);
1861 dumpCalibration(dump);
1862 dumpSurfaceLocked(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08001863
Jeff Brown511ee5f2010-10-18 13:32:20 -07001864 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
Jeff Brownc6d282b2010-10-14 21:42:15 -07001865 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mLocked.xScale);
1866 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mLocked.yScale);
1867 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mLocked.xPrecision);
1868 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mLocked.yPrecision);
1869 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mLocked.geometricScale);
1870 dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mLocked.toolSizeLinearScale);
1871 dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mLocked.toolSizeLinearBias);
1872 dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mLocked.toolSizeAreaScale);
1873 dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mLocked.toolSizeAreaBias);
1874 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mLocked.pressureScale);
1875 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mLocked.sizeScale);
Jeff Brownefd32662011-03-08 15:13:06 -08001876 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mLocked.orientationScale);
Jeff Brown80fd47c2011-05-24 01:07:44 -07001877 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mLocked.distanceScale);
Jeff Brownefd32662011-03-08 15:13:06 -08001878
1879 dump.appendFormat(INDENT3 "Last Touch:\n");
1880 dump.appendFormat(INDENT4 "Pointer Count: %d\n", mLastTouch.pointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08001881 dump.appendFormat(INDENT4 "Button State: 0x%08x\n", mLastTouch.buttonState);
1882
1883 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
1884 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
1885 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
1886 mLocked.pointerGestureXMovementScale);
1887 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
1888 mLocked.pointerGestureYMovementScale);
1889 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
1890 mLocked.pointerGestureXZoomScale);
1891 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
1892 mLocked.pointerGestureYZoomScale);
Jeff Brown2352b972011-04-12 22:39:53 -07001893 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
1894 mLocked.pointerGestureMaxSwipeWidth);
Jeff Brownace13b12011-03-09 17:39:48 -08001895 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07001896 } // release lock
1897}
1898
Jeff Brown6328cdc2010-07-29 18:18:33 -07001899void TouchInputMapper::initializeLocked() {
1900 mCurrentTouch.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001901 mLastTouch.clear();
1902 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001903
1904 for (uint32_t i = 0; i < MAX_POINTERS; i++) {
1905 mAveragingTouchFilter.historyStart[i] = 0;
1906 mAveragingTouchFilter.historyEnd[i] = 0;
1907 }
1908
1909 mJumpyTouchFilter.jumpyPointsDropped = 0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001910
1911 mLocked.currentVirtualKey.down = false;
Jeff Brown8d608662010-08-30 03:02:23 -07001912
1913 mLocked.orientedRanges.havePressure = false;
1914 mLocked.orientedRanges.haveSize = false;
Jeff Brownc6d282b2010-10-14 21:42:15 -07001915 mLocked.orientedRanges.haveTouchSize = false;
1916 mLocked.orientedRanges.haveToolSize = false;
Jeff Brown8d608662010-08-30 03:02:23 -07001917 mLocked.orientedRanges.haveOrientation = false;
Jeff Brown80fd47c2011-05-24 01:07:44 -07001918 mLocked.orientedRanges.haveDistance = false;
Jeff Brownace13b12011-03-09 17:39:48 -08001919
1920 mPointerGesture.reset();
Jeff Brown8d608662010-08-30 03:02:23 -07001921}
1922
Jeff Brown6d0fec22010-07-23 21:28:06 -07001923void TouchInputMapper::configure() {
1924 InputMapper::configure();
1925
1926 // Configure basic parameters.
Jeff Brown8d608662010-08-30 03:02:23 -07001927 configureParameters();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001928
Jeff Brown83c09682010-12-23 17:50:18 -08001929 // Configure sources.
1930 switch (mParameters.deviceType) {
1931 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
Jeff Brownefd32662011-03-08 15:13:06 -08001932 mTouchSource = AINPUT_SOURCE_TOUCHSCREEN;
Jeff Brownace13b12011-03-09 17:39:48 -08001933 mPointerSource = 0;
Jeff Brown83c09682010-12-23 17:50:18 -08001934 break;
1935 case Parameters::DEVICE_TYPE_TOUCH_PAD:
Jeff Brownefd32662011-03-08 15:13:06 -08001936 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
Jeff Brownace13b12011-03-09 17:39:48 -08001937 mPointerSource = 0;
1938 break;
1939 case Parameters::DEVICE_TYPE_POINTER:
1940 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
1941 mPointerSource = AINPUT_SOURCE_MOUSE;
Jeff Brown83c09682010-12-23 17:50:18 -08001942 break;
1943 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07001944 LOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08001945 }
1946
Jeff Brown6d0fec22010-07-23 21:28:06 -07001947 // Configure absolute axis information.
Jeff Brown8d608662010-08-30 03:02:23 -07001948 configureRawAxes();
Jeff Brown8d608662010-08-30 03:02:23 -07001949
1950 // Prepare input device calibration.
1951 parseCalibration();
1952 resolveCalibration();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001953
Jeff Brown6328cdc2010-07-29 18:18:33 -07001954 { // acquire lock
1955 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001956
Jeff Brown8d608662010-08-30 03:02:23 -07001957 // Configure surface dimensions and orientation.
Jeff Brown6328cdc2010-07-29 18:18:33 -07001958 configureSurfaceLocked();
1959 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001960}
1961
Jeff Brown8d608662010-08-30 03:02:23 -07001962void TouchInputMapper::configureParameters() {
1963 mParameters.useBadTouchFilter = getPolicy()->filterTouchEvents();
1964 mParameters.useAveragingTouchFilter = getPolicy()->filterTouchEvents();
1965 mParameters.useJumpyTouchFilter = getPolicy()->filterJumpyTouchEvents();
Jeff Brownfe508922011-01-18 15:10:10 -08001966 mParameters.virtualKeyQuietTime = getPolicy()->getVirtualKeyQuietTime();
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001967
Jeff Brown538881e2011-05-25 18:23:38 -07001968 // TODO: select the default gesture mode based on whether the device supports
1969 // distinct multitouch
Jeff Brown2352b972011-04-12 22:39:53 -07001970 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
1971
Jeff Brown538881e2011-05-25 18:23:38 -07001972 String8 gestureModeString;
1973 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
1974 gestureModeString)) {
1975 if (gestureModeString == "pointer") {
1976 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
1977 } else if (gestureModeString == "spots") {
1978 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
1979 } else if (gestureModeString != "default") {
1980 LOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
1981 }
1982 }
1983
Jeff Brownace13b12011-03-09 17:39:48 -08001984 if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
1985 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
1986 // The device is a cursor device with a touch pad attached.
1987 // By default don't use the touch pad to move the pointer.
1988 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brown80fd47c2011-05-24 01:07:44 -07001989 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
1990 // The device is a pointing device like a track pad.
1991 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
1992 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
1993 // The device is a touch screen.
1994 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownace13b12011-03-09 17:39:48 -08001995 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07001996 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08001997 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
1998 }
1999
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002000 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002001 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2002 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002003 if (deviceTypeString == "touchScreen") {
2004 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002005 } else if (deviceTypeString == "touchPad") {
2006 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002007 } else if (deviceTypeString == "pointer") {
2008 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002009 } else if (deviceTypeString != "default") {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002010 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
2011 }
2012 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002013
Jeff Brownefd32662011-03-08 15:13:06 -08002014 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002015 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2016 mParameters.orientationAware);
2017
Jeff Brownefd32662011-03-08 15:13:06 -08002018 mParameters.associatedDisplayId = mParameters.orientationAware
2019 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownace13b12011-03-09 17:39:48 -08002020 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
Jeff Brownefd32662011-03-08 15:13:06 -08002021 ? 0 : -1;
Jeff Brown8d608662010-08-30 03:02:23 -07002022}
2023
Jeff Brownef3d7e82010-09-30 14:33:04 -07002024void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002025 dump.append(INDENT3 "Parameters:\n");
2026
Jeff Brown538881e2011-05-25 18:23:38 -07002027 switch (mParameters.gestureMode) {
2028 case Parameters::GESTURE_MODE_POINTER:
2029 dump.append(INDENT4 "GestureMode: pointer\n");
2030 break;
2031 case Parameters::GESTURE_MODE_SPOTS:
2032 dump.append(INDENT4 "GestureMode: spots\n");
2033 break;
2034 default:
2035 assert(false);
2036 }
2037
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002038 switch (mParameters.deviceType) {
2039 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2040 dump.append(INDENT4 "DeviceType: touchScreen\n");
2041 break;
2042 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2043 dump.append(INDENT4 "DeviceType: touchPad\n");
2044 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002045 case Parameters::DEVICE_TYPE_POINTER:
2046 dump.append(INDENT4 "DeviceType: pointer\n");
2047 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002048 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002049 LOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002050 }
2051
2052 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2053 mParameters.associatedDisplayId);
2054 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2055 toString(mParameters.orientationAware));
2056
2057 dump.appendFormat(INDENT4 "UseBadTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002058 toString(mParameters.useBadTouchFilter));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002059 dump.appendFormat(INDENT4 "UseAveragingTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002060 toString(mParameters.useAveragingTouchFilter));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002061 dump.appendFormat(INDENT4 "UseJumpyTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002062 toString(mParameters.useJumpyTouchFilter));
Jeff Brownb88102f2010-09-08 11:49:43 -07002063}
2064
Jeff Brown8d608662010-08-30 03:02:23 -07002065void TouchInputMapper::configureRawAxes() {
2066 mRawAxes.x.clear();
2067 mRawAxes.y.clear();
2068 mRawAxes.pressure.clear();
2069 mRawAxes.touchMajor.clear();
2070 mRawAxes.touchMinor.clear();
2071 mRawAxes.toolMajor.clear();
2072 mRawAxes.toolMinor.clear();
2073 mRawAxes.orientation.clear();
Jeff Brown80fd47c2011-05-24 01:07:44 -07002074 mRawAxes.distance.clear();
2075 mRawAxes.trackingId.clear();
2076 mRawAxes.slot.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002077}
2078
Jeff Brownef3d7e82010-09-30 14:33:04 -07002079void TouchInputMapper::dumpRawAxes(String8& dump) {
2080 dump.append(INDENT3 "Raw Axes:\n");
Jeff Browncb1404e2011-01-15 18:14:15 -08002081 dumpRawAbsoluteAxisInfo(dump, mRawAxes.x, "X");
2082 dumpRawAbsoluteAxisInfo(dump, mRawAxes.y, "Y");
2083 dumpRawAbsoluteAxisInfo(dump, mRawAxes.pressure, "Pressure");
2084 dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor");
2085 dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor");
2086 dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor");
2087 dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor");
2088 dumpRawAbsoluteAxisInfo(dump, mRawAxes.orientation, "Orientation");
Jeff Brown80fd47c2011-05-24 01:07:44 -07002089 dumpRawAbsoluteAxisInfo(dump, mRawAxes.distance, "Distance");
2090 dumpRawAbsoluteAxisInfo(dump, mRawAxes.trackingId, "TrackingId");
2091 dumpRawAbsoluteAxisInfo(dump, mRawAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002092}
2093
Jeff Brown6328cdc2010-07-29 18:18:33 -07002094bool TouchInputMapper::configureSurfaceLocked() {
Jeff Brown9626b142011-03-03 02:09:54 -08002095 // Ensure we have valid X and Y axes.
2096 if (!mRawAxes.x.valid || !mRawAxes.y.valid) {
2097 LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
2098 "The device will be inoperable.", getDeviceName().string());
2099 return false;
2100 }
2101
Jeff Brown6d0fec22010-07-23 21:28:06 -07002102 // Update orientation and dimensions if needed.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002103 int32_t orientation = DISPLAY_ORIENTATION_0;
Jeff Brown9626b142011-03-03 02:09:54 -08002104 int32_t width = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2105 int32_t height = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002106
2107 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002108 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002109 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownefd32662011-03-08 15:13:06 -08002110 &mLocked.associatedDisplayWidth, &mLocked.associatedDisplayHeight,
2111 &mLocked.associatedDisplayOrientation)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002112 return false;
2113 }
Jeff Brownefd32662011-03-08 15:13:06 -08002114
2115 // A touch screen inherits the dimensions of the display.
2116 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
2117 width = mLocked.associatedDisplayWidth;
2118 height = mLocked.associatedDisplayHeight;
2119 }
2120
2121 // The device inherits the orientation of the display if it is orientation aware.
2122 if (mParameters.orientationAware) {
2123 orientation = mLocked.associatedDisplayOrientation;
2124 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002125 }
2126
Jeff Brownace13b12011-03-09 17:39:48 -08002127 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2128 && mPointerController == NULL) {
2129 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2130 }
2131
Jeff Brown6328cdc2010-07-29 18:18:33 -07002132 bool orientationChanged = mLocked.surfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002133 if (orientationChanged) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002134 mLocked.surfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002135 }
2136
Jeff Brown6328cdc2010-07-29 18:18:33 -07002137 bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002138 if (sizeChanged) {
Jeff Brownefd32662011-03-08 15:13:06 -08002139 LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002140 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown8d608662010-08-30 03:02:23 -07002141
Jeff Brown6328cdc2010-07-29 18:18:33 -07002142 mLocked.surfaceWidth = width;
2143 mLocked.surfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002144
Jeff Brown8d608662010-08-30 03:02:23 -07002145 // Configure X and Y factors.
Jeff Brown9626b142011-03-03 02:09:54 -08002146 mLocked.xScale = float(width) / (mRawAxes.x.maxValue - mRawAxes.x.minValue + 1);
2147 mLocked.yScale = float(height) / (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1);
2148 mLocked.xPrecision = 1.0f / mLocked.xScale;
2149 mLocked.yPrecision = 1.0f / mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002150
Jeff Brownefd32662011-03-08 15:13:06 -08002151 mLocked.orientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
2152 mLocked.orientedRanges.x.source = mTouchSource;
2153 mLocked.orientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
2154 mLocked.orientedRanges.y.source = mTouchSource;
2155
Jeff Brown9626b142011-03-03 02:09:54 -08002156 configureVirtualKeysLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002157
Jeff Brown8d608662010-08-30 03:02:23 -07002158 // Scale factor for terms that are not oriented in a particular axis.
2159 // If the pixels are square then xScale == yScale otherwise we fake it
2160 // by choosing an average.
2161 mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002162
Jeff Brown8d608662010-08-30 03:02:23 -07002163 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002164 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002165
Jeff Brown8d608662010-08-30 03:02:23 -07002166 // TouchMajor and TouchMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002167 if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) {
2168 mLocked.orientedRanges.haveTouchSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002169
2170 mLocked.orientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
2171 mLocked.orientedRanges.touchMajor.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002172 mLocked.orientedRanges.touchMajor.min = 0;
2173 mLocked.orientedRanges.touchMajor.max = diagonalSize;
2174 mLocked.orientedRanges.touchMajor.flat = 0;
2175 mLocked.orientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002176
Jeff Brown8d608662010-08-30 03:02:23 -07002177 mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor;
Jeff Brownefd32662011-03-08 15:13:06 -08002178 mLocked.orientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002179 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002180
Jeff Brown8d608662010-08-30 03:02:23 -07002181 // ToolMajor and ToolMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002182 mLocked.toolSizeLinearScale = 0;
2183 mLocked.toolSizeLinearBias = 0;
2184 mLocked.toolSizeAreaScale = 0;
2185 mLocked.toolSizeAreaBias = 0;
2186 if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
2187 if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) {
2188 if (mCalibration.haveToolSizeLinearScale) {
2189 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
Jeff Brown8d608662010-08-30 03:02:23 -07002190 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002191 mLocked.toolSizeLinearScale = float(min(width, height))
Jeff Brown8d608662010-08-30 03:02:23 -07002192 / mRawAxes.toolMajor.maxValue;
2193 }
2194
Jeff Brownc6d282b2010-10-14 21:42:15 -07002195 if (mCalibration.haveToolSizeLinearBias) {
2196 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
2197 }
2198 } else if (mCalibration.toolSizeCalibration ==
2199 Calibration::TOOL_SIZE_CALIBRATION_AREA) {
2200 if (mCalibration.haveToolSizeLinearScale) {
2201 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
2202 } else {
2203 mLocked.toolSizeLinearScale = min(width, height);
2204 }
2205
2206 if (mCalibration.haveToolSizeLinearBias) {
2207 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
2208 }
2209
2210 if (mCalibration.haveToolSizeAreaScale) {
2211 mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale;
2212 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
2213 mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue;
2214 }
2215
2216 if (mCalibration.haveToolSizeAreaBias) {
2217 mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -07002218 }
2219 }
2220
Jeff Brownc6d282b2010-10-14 21:42:15 -07002221 mLocked.orientedRanges.haveToolSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002222
2223 mLocked.orientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
2224 mLocked.orientedRanges.toolMajor.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002225 mLocked.orientedRanges.toolMajor.min = 0;
2226 mLocked.orientedRanges.toolMajor.max = diagonalSize;
2227 mLocked.orientedRanges.toolMajor.flat = 0;
2228 mLocked.orientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002229
Jeff Brown8d608662010-08-30 03:02:23 -07002230 mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor;
Jeff Brownefd32662011-03-08 15:13:06 -08002231 mLocked.orientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002232 }
2233
2234 // Pressure factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002235 mLocked.pressureScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002236 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
2237 RawAbsoluteAxisInfo rawPressureAxis;
2238 switch (mCalibration.pressureSource) {
2239 case Calibration::PRESSURE_SOURCE_PRESSURE:
2240 rawPressureAxis = mRawAxes.pressure;
2241 break;
2242 case Calibration::PRESSURE_SOURCE_TOUCH:
2243 rawPressureAxis = mRawAxes.touchMajor;
2244 break;
2245 default:
2246 rawPressureAxis.clear();
2247 }
2248
Jeff Brown8d608662010-08-30 03:02:23 -07002249 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2250 || mCalibration.pressureCalibration
2251 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2252 if (mCalibration.havePressureScale) {
2253 mLocked.pressureScale = mCalibration.pressureScale;
2254 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
2255 mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue;
2256 }
2257 }
2258
2259 mLocked.orientedRanges.havePressure = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002260
2261 mLocked.orientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2262 mLocked.orientedRanges.pressure.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002263 mLocked.orientedRanges.pressure.min = 0;
2264 mLocked.orientedRanges.pressure.max = 1.0;
2265 mLocked.orientedRanges.pressure.flat = 0;
2266 mLocked.orientedRanges.pressure.fuzz = 0;
2267 }
2268
2269 // Size factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002270 mLocked.sizeScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002271 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002272 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
2273 if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
2274 mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue;
2275 }
2276 }
2277
2278 mLocked.orientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002279
2280 mLocked.orientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
2281 mLocked.orientedRanges.size.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002282 mLocked.orientedRanges.size.min = 0;
2283 mLocked.orientedRanges.size.max = 1.0;
2284 mLocked.orientedRanges.size.flat = 0;
2285 mLocked.orientedRanges.size.fuzz = 0;
2286 }
2287
2288 // Orientation
Jeff Brownc6d282b2010-10-14 21:42:15 -07002289 mLocked.orientationScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002290 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002291 if (mCalibration.orientationCalibration
2292 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
2293 if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) {
2294 mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue;
2295 }
2296 }
2297
Jeff Brown80fd47c2011-05-24 01:07:44 -07002298 mLocked.orientedRanges.haveOrientation = true;
2299
Jeff Brownefd32662011-03-08 15:13:06 -08002300 mLocked.orientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2301 mLocked.orientedRanges.orientation.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002302 mLocked.orientedRanges.orientation.min = - M_PI_2;
2303 mLocked.orientedRanges.orientation.max = M_PI_2;
2304 mLocked.orientedRanges.orientation.flat = 0;
2305 mLocked.orientedRanges.orientation.fuzz = 0;
2306 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002307
2308 // Distance
2309 mLocked.distanceScale = 0;
2310 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
2311 if (mCalibration.distanceCalibration
2312 == Calibration::DISTANCE_CALIBRATION_SCALED) {
2313 if (mCalibration.haveDistanceScale) {
2314 mLocked.distanceScale = mCalibration.distanceScale;
2315 } else {
2316 mLocked.distanceScale = 1.0f;
2317 }
2318 }
2319
2320 mLocked.orientedRanges.haveDistance = true;
2321
2322 mLocked.orientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
2323 mLocked.orientedRanges.distance.source = mTouchSource;
2324 mLocked.orientedRanges.distance.min =
2325 mRawAxes.distance.minValue * mLocked.distanceScale;
2326 mLocked.orientedRanges.distance.max =
2327 mRawAxes.distance.minValue * mLocked.distanceScale;
2328 mLocked.orientedRanges.distance.flat = 0;
2329 mLocked.orientedRanges.distance.fuzz =
2330 mRawAxes.distance.fuzz * mLocked.distanceScale;
2331 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002332 }
2333
2334 if (orientationChanged || sizeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002335 // Compute oriented surface dimensions, precision, scales and ranges.
2336 // Note that the maximum value reported is an inclusive maximum value so it is one
2337 // unit less than the total width or height of surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002338 switch (mLocked.surfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002339 case DISPLAY_ORIENTATION_90:
2340 case DISPLAY_ORIENTATION_270:
Jeff Brown6328cdc2010-07-29 18:18:33 -07002341 mLocked.orientedSurfaceWidth = mLocked.surfaceHeight;
2342 mLocked.orientedSurfaceHeight = mLocked.surfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002343
Jeff Brown6328cdc2010-07-29 18:18:33 -07002344 mLocked.orientedXPrecision = mLocked.yPrecision;
2345 mLocked.orientedYPrecision = mLocked.xPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002346
2347 mLocked.orientedRanges.x.min = 0;
2348 mLocked.orientedRanges.x.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue)
2349 * mLocked.yScale;
2350 mLocked.orientedRanges.x.flat = 0;
2351 mLocked.orientedRanges.x.fuzz = mLocked.yScale;
2352
2353 mLocked.orientedRanges.y.min = 0;
2354 mLocked.orientedRanges.y.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue)
2355 * mLocked.xScale;
2356 mLocked.orientedRanges.y.flat = 0;
2357 mLocked.orientedRanges.y.fuzz = mLocked.xScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002358 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002359
Jeff Brown6d0fec22010-07-23 21:28:06 -07002360 default:
Jeff Brown6328cdc2010-07-29 18:18:33 -07002361 mLocked.orientedSurfaceWidth = mLocked.surfaceWidth;
2362 mLocked.orientedSurfaceHeight = mLocked.surfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002363
Jeff Brown6328cdc2010-07-29 18:18:33 -07002364 mLocked.orientedXPrecision = mLocked.xPrecision;
2365 mLocked.orientedYPrecision = mLocked.yPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002366
2367 mLocked.orientedRanges.x.min = 0;
2368 mLocked.orientedRanges.x.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue)
2369 * mLocked.xScale;
2370 mLocked.orientedRanges.x.flat = 0;
2371 mLocked.orientedRanges.x.fuzz = mLocked.xScale;
2372
2373 mLocked.orientedRanges.y.min = 0;
2374 mLocked.orientedRanges.y.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue)
2375 * mLocked.yScale;
2376 mLocked.orientedRanges.y.flat = 0;
2377 mLocked.orientedRanges.y.fuzz = mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002378 break;
2379 }
Jeff Brownace13b12011-03-09 17:39:48 -08002380
2381 // Compute pointer gesture detection parameters.
2382 // TODO: These factors should not be hardcoded.
2383 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2384 int32_t rawWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2385 int32_t rawHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07002386 float rawDiagonal = hypotf(rawWidth, rawHeight);
2387 float displayDiagonal = hypotf(mLocked.associatedDisplayWidth,
2388 mLocked.associatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08002389
Jeff Brown2352b972011-04-12 22:39:53 -07002390 // Scale movements such that one whole swipe of the touch pad covers a
2391 // given area relative to the diagonal size of the display.
Jeff Brownace13b12011-03-09 17:39:48 -08002392 // Assume that the touch pad has a square aspect ratio such that movements in
2393 // X and Y of the same number of raw units cover the same physical distance.
2394 const float scaleFactor = 0.8f;
2395
Jeff Brown2352b972011-04-12 22:39:53 -07002396 mLocked.pointerGestureXMovementScale = GESTURE_MOVEMENT_SPEED_RATIO
2397 * displayDiagonal / rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08002398 mLocked.pointerGestureYMovementScale = mLocked.pointerGestureXMovementScale;
2399
2400 // Scale zooms to cover a smaller range of the display than movements do.
2401 // This value determines the area around the pointer that is affected by freeform
2402 // pointer gestures.
Jeff Brown2352b972011-04-12 22:39:53 -07002403 mLocked.pointerGestureXZoomScale = GESTURE_ZOOM_SPEED_RATIO
2404 * displayDiagonal / rawDiagonal;
2405 mLocked.pointerGestureYZoomScale = mLocked.pointerGestureXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002406
Jeff Brown2352b972011-04-12 22:39:53 -07002407 // Max width between pointers to detect a swipe gesture is more than some fraction
2408 // of the diagonal axis of the touch pad. Touches that are wider than this are
2409 // translated into freeform gestures.
2410 mLocked.pointerGestureMaxSwipeWidth = SWIPE_MAX_WIDTH_RATIO * rawDiagonal;
2411
2412 // Reset the current pointer gesture.
2413 mPointerGesture.reset();
2414
2415 // Remove any current spots.
2416 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
2417 mPointerController->clearSpots();
2418 }
Jeff Brownace13b12011-03-09 17:39:48 -08002419 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002420 }
2421
2422 return true;
2423}
2424
Jeff Brownef3d7e82010-09-30 14:33:04 -07002425void TouchInputMapper::dumpSurfaceLocked(String8& dump) {
2426 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth);
2427 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight);
2428 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07002429}
2430
Jeff Brown6328cdc2010-07-29 18:18:33 -07002431void TouchInputMapper::configureVirtualKeysLocked() {
Jeff Brown8d608662010-08-30 03:02:23 -07002432 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08002433 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002434
Jeff Brown6328cdc2010-07-29 18:18:33 -07002435 mLocked.virtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002436
Jeff Brown6328cdc2010-07-29 18:18:33 -07002437 if (virtualKeyDefinitions.size() == 0) {
2438 return;
2439 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002440
Jeff Brown6328cdc2010-07-29 18:18:33 -07002441 mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size());
2442
Jeff Brown8d608662010-08-30 03:02:23 -07002443 int32_t touchScreenLeft = mRawAxes.x.minValue;
2444 int32_t touchScreenTop = mRawAxes.y.minValue;
Jeff Brown9626b142011-03-03 02:09:54 -08002445 int32_t touchScreenWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2446 int32_t touchScreenHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002447
2448 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07002449 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07002450 virtualKeyDefinitions[i];
2451
2452 mLocked.virtualKeys.add();
2453 VirtualKey& virtualKey = mLocked.virtualKeys.editTop();
2454
2455 virtualKey.scanCode = virtualKeyDefinition.scanCode;
2456 int32_t keyCode;
2457 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08002458 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07002459 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002460 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
2461 virtualKey.scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002462 mLocked.virtualKeys.pop(); // drop the key
2463 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002464 }
2465
Jeff Brown6328cdc2010-07-29 18:18:33 -07002466 virtualKey.keyCode = keyCode;
2467 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002468
Jeff Brown6328cdc2010-07-29 18:18:33 -07002469 // convert the key definition's display coordinates into touch coordinates for a hit box
2470 int32_t halfWidth = virtualKeyDefinition.width / 2;
2471 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002472
Jeff Brown6328cdc2010-07-29 18:18:33 -07002473 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
2474 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
2475 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
2476 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
2477 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
2478 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
2479 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
2480 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07002481 }
2482}
2483
2484void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) {
2485 if (!mLocked.virtualKeys.isEmpty()) {
2486 dump.append(INDENT3 "Virtual Keys:\n");
2487
2488 for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) {
2489 const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i);
2490 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
2491 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
2492 i, virtualKey.scanCode, virtualKey.keyCode,
2493 virtualKey.hitLeft, virtualKey.hitRight,
2494 virtualKey.hitTop, virtualKey.hitBottom);
2495 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002496 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002497}
2498
Jeff Brown8d608662010-08-30 03:02:23 -07002499void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002500 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07002501 Calibration& out = mCalibration;
2502
Jeff Brownc6d282b2010-10-14 21:42:15 -07002503 // Touch Size
2504 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT;
2505 String8 touchSizeCalibrationString;
2506 if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) {
2507 if (touchSizeCalibrationString == "none") {
2508 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
2509 } else if (touchSizeCalibrationString == "geometric") {
2510 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC;
2511 } else if (touchSizeCalibrationString == "pressure") {
2512 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
2513 } else if (touchSizeCalibrationString != "default") {
2514 LOGW("Invalid value for touch.touchSize.calibration: '%s'",
2515 touchSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002516 }
2517 }
2518
Jeff Brownc6d282b2010-10-14 21:42:15 -07002519 // Tool Size
2520 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT;
2521 String8 toolSizeCalibrationString;
2522 if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) {
2523 if (toolSizeCalibrationString == "none") {
2524 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
2525 } else if (toolSizeCalibrationString == "geometric") {
2526 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC;
2527 } else if (toolSizeCalibrationString == "linear") {
2528 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
2529 } else if (toolSizeCalibrationString == "area") {
2530 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA;
2531 } else if (toolSizeCalibrationString != "default") {
2532 LOGW("Invalid value for touch.toolSize.calibration: '%s'",
2533 toolSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002534 }
2535 }
2536
Jeff Brownc6d282b2010-10-14 21:42:15 -07002537 out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"),
2538 out.toolSizeLinearScale);
2539 out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"),
2540 out.toolSizeLinearBias);
2541 out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"),
2542 out.toolSizeAreaScale);
2543 out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"),
2544 out.toolSizeAreaBias);
2545 out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"),
2546 out.toolSizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07002547
2548 // Pressure
2549 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
2550 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002551 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002552 if (pressureCalibrationString == "none") {
2553 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2554 } else if (pressureCalibrationString == "physical") {
2555 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
2556 } else if (pressureCalibrationString == "amplitude") {
2557 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2558 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002559 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002560 pressureCalibrationString.string());
2561 }
2562 }
2563
2564 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
2565 String8 pressureSourceString;
2566 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
2567 if (pressureSourceString == "pressure") {
2568 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2569 } else if (pressureSourceString == "touch") {
2570 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2571 } else if (pressureSourceString != "default") {
2572 LOGW("Invalid value for touch.pressure.source: '%s'",
2573 pressureSourceString.string());
2574 }
2575 }
2576
2577 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
2578 out.pressureScale);
2579
2580 // Size
2581 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
2582 String8 sizeCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002583 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002584 if (sizeCalibrationString == "none") {
2585 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2586 } else if (sizeCalibrationString == "normalized") {
2587 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
2588 } else if (sizeCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002589 LOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002590 sizeCalibrationString.string());
2591 }
2592 }
2593
2594 // Orientation
2595 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
2596 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002597 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002598 if (orientationCalibrationString == "none") {
2599 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2600 } else if (orientationCalibrationString == "interpolated") {
2601 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002602 } else if (orientationCalibrationString == "vector") {
2603 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002604 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002605 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002606 orientationCalibrationString.string());
2607 }
2608 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002609
2610 // Distance
2611 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
2612 String8 distanceCalibrationString;
2613 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
2614 if (distanceCalibrationString == "none") {
2615 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
2616 } else if (distanceCalibrationString == "scaled") {
2617 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
2618 } else if (distanceCalibrationString != "default") {
2619 LOGW("Invalid value for touch.distance.calibration: '%s'",
2620 distanceCalibrationString.string());
2621 }
2622 }
2623
2624 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
2625 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002626}
2627
2628void TouchInputMapper::resolveCalibration() {
2629 // Pressure
2630 switch (mCalibration.pressureSource) {
2631 case Calibration::PRESSURE_SOURCE_DEFAULT:
2632 if (mRawAxes.pressure.valid) {
2633 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2634 } else if (mRawAxes.touchMajor.valid) {
2635 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2636 }
2637 break;
2638
2639 case Calibration::PRESSURE_SOURCE_PRESSURE:
2640 if (! mRawAxes.pressure.valid) {
2641 LOGW("Calibration property touch.pressure.source is 'pressure' but "
2642 "the pressure axis is not available.");
2643 }
2644 break;
2645
2646 case Calibration::PRESSURE_SOURCE_TOUCH:
2647 if (! mRawAxes.touchMajor.valid) {
2648 LOGW("Calibration property touch.pressure.source is 'touch' but "
2649 "the touchMajor axis is not available.");
2650 }
2651 break;
2652
2653 default:
2654 break;
2655 }
2656
2657 switch (mCalibration.pressureCalibration) {
2658 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
2659 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
2660 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2661 } else {
2662 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2663 }
2664 break;
2665
2666 default:
2667 break;
2668 }
2669
Jeff Brownc6d282b2010-10-14 21:42:15 -07002670 // Tool Size
2671 switch (mCalibration.toolSizeCalibration) {
2672 case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07002673 if (mRawAxes.toolMajor.valid) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002674 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
Jeff Brown8d608662010-08-30 03:02:23 -07002675 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002676 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002677 }
2678 break;
2679
2680 default:
2681 break;
2682 }
2683
Jeff Brownc6d282b2010-10-14 21:42:15 -07002684 // Touch Size
2685 switch (mCalibration.touchSizeCalibration) {
2686 case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07002687 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
Jeff Brownc6d282b2010-10-14 21:42:15 -07002688 && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
2689 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
Jeff Brown8d608662010-08-30 03:02:23 -07002690 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002691 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002692 }
2693 break;
2694
2695 default:
2696 break;
2697 }
2698
2699 // Size
2700 switch (mCalibration.sizeCalibration) {
2701 case Calibration::SIZE_CALIBRATION_DEFAULT:
2702 if (mRawAxes.toolMajor.valid) {
2703 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
2704 } else {
2705 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2706 }
2707 break;
2708
2709 default:
2710 break;
2711 }
2712
2713 // Orientation
2714 switch (mCalibration.orientationCalibration) {
2715 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
2716 if (mRawAxes.orientation.valid) {
2717 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
2718 } else {
2719 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2720 }
2721 break;
2722
2723 default:
2724 break;
2725 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002726
2727 // Distance
2728 switch (mCalibration.distanceCalibration) {
2729 case Calibration::DISTANCE_CALIBRATION_DEFAULT:
2730 if (mRawAxes.distance.valid) {
2731 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
2732 } else {
2733 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
2734 }
2735 break;
2736
2737 default:
2738 break;
2739 }
Jeff Brown8d608662010-08-30 03:02:23 -07002740}
2741
Jeff Brownef3d7e82010-09-30 14:33:04 -07002742void TouchInputMapper::dumpCalibration(String8& dump) {
2743 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07002744
Jeff Brownc6d282b2010-10-14 21:42:15 -07002745 // Touch Size
2746 switch (mCalibration.touchSizeCalibration) {
2747 case Calibration::TOUCH_SIZE_CALIBRATION_NONE:
2748 dump.append(INDENT4 "touch.touchSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002749 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002750 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
2751 dump.append(INDENT4 "touch.touchSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002752 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002753 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
2754 dump.append(INDENT4 "touch.touchSize.calibration: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002755 break;
2756 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002757 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002758 }
2759
Jeff Brownc6d282b2010-10-14 21:42:15 -07002760 // Tool Size
2761 switch (mCalibration.toolSizeCalibration) {
2762 case Calibration::TOOL_SIZE_CALIBRATION_NONE:
2763 dump.append(INDENT4 "touch.toolSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002764 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002765 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
2766 dump.append(INDENT4 "touch.toolSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002767 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002768 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
2769 dump.append(INDENT4 "touch.toolSize.calibration: linear\n");
2770 break;
2771 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2772 dump.append(INDENT4 "touch.toolSize.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002773 break;
2774 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002775 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002776 }
2777
Jeff Brownc6d282b2010-10-14 21:42:15 -07002778 if (mCalibration.haveToolSizeLinearScale) {
2779 dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n",
2780 mCalibration.toolSizeLinearScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002781 }
2782
Jeff Brownc6d282b2010-10-14 21:42:15 -07002783 if (mCalibration.haveToolSizeLinearBias) {
2784 dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n",
2785 mCalibration.toolSizeLinearBias);
Jeff Brown8d608662010-08-30 03:02:23 -07002786 }
2787
Jeff Brownc6d282b2010-10-14 21:42:15 -07002788 if (mCalibration.haveToolSizeAreaScale) {
2789 dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n",
2790 mCalibration.toolSizeAreaScale);
2791 }
2792
2793 if (mCalibration.haveToolSizeAreaBias) {
2794 dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n",
2795 mCalibration.toolSizeAreaBias);
2796 }
2797
2798 if (mCalibration.haveToolSizeIsSummed) {
Jeff Brown1f245102010-11-18 20:53:46 -08002799 dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %s\n",
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002800 toString(mCalibration.toolSizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07002801 }
2802
2803 // Pressure
2804 switch (mCalibration.pressureCalibration) {
2805 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002806 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002807 break;
2808 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002809 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002810 break;
2811 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002812 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002813 break;
2814 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002815 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002816 }
2817
2818 switch (mCalibration.pressureSource) {
2819 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002820 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002821 break;
2822 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002823 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002824 break;
2825 case Calibration::PRESSURE_SOURCE_DEFAULT:
2826 break;
2827 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002828 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002829 }
2830
2831 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07002832 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
2833 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002834 }
2835
2836 // Size
2837 switch (mCalibration.sizeCalibration) {
2838 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002839 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002840 break;
2841 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002842 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002843 break;
2844 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002845 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002846 }
2847
2848 // Orientation
2849 switch (mCalibration.orientationCalibration) {
2850 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002851 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002852 break;
2853 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002854 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002855 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002856 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
2857 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
2858 break;
Jeff Brown8d608662010-08-30 03:02:23 -07002859 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002860 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002861 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002862
2863 // Distance
2864 switch (mCalibration.distanceCalibration) {
2865 case Calibration::DISTANCE_CALIBRATION_NONE:
2866 dump.append(INDENT4 "touch.distance.calibration: none\n");
2867 break;
2868 case Calibration::DISTANCE_CALIBRATION_SCALED:
2869 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
2870 break;
2871 default:
2872 LOG_ASSERT(false);
2873 }
2874
2875 if (mCalibration.haveDistanceScale) {
2876 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
2877 mCalibration.distanceScale);
2878 }
Jeff Brown8d608662010-08-30 03:02:23 -07002879}
2880
Jeff Brown6d0fec22010-07-23 21:28:06 -07002881void TouchInputMapper::reset() {
2882 // Synthesize touch up event if touch is currently down.
2883 // This will also take care of finishing virtual key processing if needed.
2884 if (mLastTouch.pointerCount != 0) {
2885 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
2886 mCurrentTouch.clear();
2887 syncTouch(when, true);
2888 }
2889
Jeff Brown6328cdc2010-07-29 18:18:33 -07002890 { // acquire lock
2891 AutoMutex _l(mLock);
2892 initializeLocked();
Jeff Brown2352b972011-04-12 22:39:53 -07002893
2894 if (mPointerController != NULL
2895 && mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
2896 mPointerController->clearSpots();
2897 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002898 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07002899
Jeff Brown6328cdc2010-07-29 18:18:33 -07002900 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002901}
2902
2903void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Brownaa3855d2011-03-17 01:34:19 -07002904#if DEBUG_RAW_EVENTS
2905 if (!havePointerIds) {
2906 LOGD("syncTouch: pointerCount=%d, no pointer ids", mCurrentTouch.pointerCount);
2907 } else {
2908 LOGD("syncTouch: pointerCount=%d, up=0x%08x, down=0x%08x, move=0x%08x, "
2909 "last=0x%08x, current=0x%08x", mCurrentTouch.pointerCount,
2910 mLastTouch.idBits.value & ~mCurrentTouch.idBits.value,
2911 mCurrentTouch.idBits.value & ~mLastTouch.idBits.value,
2912 mLastTouch.idBits.value & mCurrentTouch.idBits.value,
2913 mLastTouch.idBits.value, mCurrentTouch.idBits.value);
2914 }
2915#endif
2916
Jeff Brown6328cdc2010-07-29 18:18:33 -07002917 // Preprocess pointer data.
Jeff Brown6d0fec22010-07-23 21:28:06 -07002918 if (mParameters.useBadTouchFilter) {
2919 if (applyBadTouchFilter()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002920 havePointerIds = false;
2921 }
2922 }
2923
Jeff Brown6d0fec22010-07-23 21:28:06 -07002924 if (mParameters.useJumpyTouchFilter) {
2925 if (applyJumpyTouchFilter()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002926 havePointerIds = false;
2927 }
2928 }
2929
Jeff Brownaa3855d2011-03-17 01:34:19 -07002930 if (!havePointerIds) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002931 calculatePointerIds();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002932 }
2933
Jeff Brown6d0fec22010-07-23 21:28:06 -07002934 TouchData temp;
2935 TouchData* savedTouch;
2936 if (mParameters.useAveragingTouchFilter) {
2937 temp.copyFrom(mCurrentTouch);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002938 savedTouch = & temp;
2939
Jeff Brown6d0fec22010-07-23 21:28:06 -07002940 applyAveragingTouchFilter();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002941 } else {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002942 savedTouch = & mCurrentTouch;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002943 }
2944
Jeff Brown56194eb2011-03-02 19:23:13 -08002945 uint32_t policyFlags = 0;
Jeff Brown05dc66a2011-03-02 14:41:58 -08002946 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) {
Jeff Brownefd32662011-03-08 15:13:06 -08002947 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
2948 // If this is a touch screen, hide the pointer on an initial down.
2949 getContext()->fadePointer();
2950 }
Jeff Brown56194eb2011-03-02 19:23:13 -08002951
2952 // Initial downs on external touch devices should wake the device.
2953 // We don't do this for internal touch screens to prevent them from waking
2954 // up in your pocket.
2955 // TODO: Use the input device configuration to control this behavior more finely.
2956 if (getDevice()->isExternal()) {
2957 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2958 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002959 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002960
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002961 // Synthesize key down from buttons if needed.
2962 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mTouchSource,
2963 policyFlags, mLastTouch.buttonState, mCurrentTouch.buttonState);
2964
2965 // Send motion events.
Jeff Brown79ac9692011-04-19 21:20:10 -07002966 TouchResult touchResult;
2967 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount == 0
2968 && mLastTouch.buttonState == mCurrentTouch.buttonState) {
2969 // Drop spurious syncs.
2970 touchResult = DROP_STROKE;
2971 } else {
2972 // Process touches and virtual keys.
2973 touchResult = consumeOffScreenTouches(when, policyFlags);
2974 if (touchResult == DISPATCH_TOUCH) {
2975 suppressSwipeOntoVirtualKeys(when);
2976 if (mPointerController != NULL) {
2977 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
2978 }
2979 dispatchTouches(when, policyFlags);
Jeff Brownace13b12011-03-09 17:39:48 -08002980 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002981 }
2982
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002983 // Synthesize key up from buttons if needed.
2984 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mTouchSource,
2985 policyFlags, mLastTouch.buttonState, mCurrentTouch.buttonState);
2986
Jeff Brown6328cdc2010-07-29 18:18:33 -07002987 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownace13b12011-03-09 17:39:48 -08002988 // Keep the button state so we can track edge-triggered button state changes.
Jeff Brown6d0fec22010-07-23 21:28:06 -07002989 if (touchResult == DROP_STROKE) {
2990 mLastTouch.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08002991 mLastTouch.buttonState = savedTouch->buttonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002992 } else {
2993 mLastTouch.copyFrom(*savedTouch);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002994 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002995}
2996
Jeff Brown79ac9692011-04-19 21:20:10 -07002997void TouchInputMapper::timeoutExpired(nsecs_t when) {
2998 if (mPointerController != NULL) {
2999 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3000 }
3001}
3002
Jeff Brown6d0fec22010-07-23 21:28:06 -07003003TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches(
3004 nsecs_t when, uint32_t policyFlags) {
3005 int32_t keyEventAction, keyEventFlags;
3006 int32_t keyCode, scanCode, downTime;
3007 TouchResult touchResult;
Jeff Brown349703e2010-06-22 01:27:15 -07003008
Jeff Brown6328cdc2010-07-29 18:18:33 -07003009 { // acquire lock
3010 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003011
Jeff Brown6328cdc2010-07-29 18:18:33 -07003012 // Update surface size and orientation, including virtual key positions.
3013 if (! configureSurfaceLocked()) {
3014 return DROP_STROKE;
3015 }
3016
3017 // Check for virtual key press.
3018 if (mLocked.currentVirtualKey.down) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003019 if (mCurrentTouch.pointerCount == 0) {
3020 // Pointer went up while virtual key was down.
Jeff Brown6328cdc2010-07-29 18:18:33 -07003021 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003022#if DEBUG_VIRTUAL_KEYS
3023 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07003024 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003025#endif
3026 keyEventAction = AKEY_EVENT_ACTION_UP;
3027 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
3028 touchResult = SKIP_TOUCH;
3029 goto DispatchVirtualKey;
3030 }
3031
3032 if (mCurrentTouch.pointerCount == 1) {
3033 int32_t x = mCurrentTouch.pointers[0].x;
3034 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003035 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
3036 if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003037 // Pointer is still within the space of the virtual key.
3038 return SKIP_TOUCH;
3039 }
3040 }
3041
3042 // Pointer left virtual key area or another pointer also went down.
3043 // Send key cancellation and drop the stroke so subsequent motions will be
3044 // considered fresh downs. This is useful when the user swipes away from the
3045 // virtual key area into the main display surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07003046 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003047#if DEBUG_VIRTUAL_KEYS
3048 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07003049 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003050#endif
3051 keyEventAction = AKEY_EVENT_ACTION_UP;
3052 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3053 | AKEY_EVENT_FLAG_CANCELED;
Jeff Brownc3db8582010-10-20 15:33:38 -07003054
3055 // Check whether the pointer moved inside the display area where we should
3056 // start a new stroke.
3057 int32_t x = mCurrentTouch.pointers[0].x;
3058 int32_t y = mCurrentTouch.pointers[0].y;
3059 if (isPointInsideSurfaceLocked(x, y)) {
3060 mLastTouch.clear();
3061 touchResult = DISPATCH_TOUCH;
3062 } else {
3063 touchResult = DROP_STROKE;
3064 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003065 } else {
3066 if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) {
3067 // Pointer just went down. Handle off-screen touches, if needed.
3068 int32_t x = mCurrentTouch.pointers[0].x;
3069 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003070 if (! isPointInsideSurfaceLocked(x, y)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003071 // If exactly one pointer went down, check for virtual key hit.
3072 // Otherwise we will drop the entire stroke.
3073 if (mCurrentTouch.pointerCount == 1) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003074 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003075 if (virtualKey) {
Jeff Brownfe508922011-01-18 15:10:10 -08003076 if (mContext->shouldDropVirtualKey(when, getDevice(),
3077 virtualKey->keyCode, virtualKey->scanCode)) {
3078 return DROP_STROKE;
3079 }
3080
Jeff Brown6328cdc2010-07-29 18:18:33 -07003081 mLocked.currentVirtualKey.down = true;
3082 mLocked.currentVirtualKey.downTime = when;
3083 mLocked.currentVirtualKey.keyCode = virtualKey->keyCode;
3084 mLocked.currentVirtualKey.scanCode = virtualKey->scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003085#if DEBUG_VIRTUAL_KEYS
3086 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07003087 mLocked.currentVirtualKey.keyCode,
3088 mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003089#endif
3090 keyEventAction = AKEY_EVENT_ACTION_DOWN;
3091 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM
3092 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
3093 touchResult = SKIP_TOUCH;
3094 goto DispatchVirtualKey;
3095 }
3096 }
3097 return DROP_STROKE;
3098 }
3099 }
3100 return DISPATCH_TOUCH;
3101 }
3102
3103 DispatchVirtualKey:
3104 // Collect remaining state needed to dispatch virtual key.
Jeff Brown6328cdc2010-07-29 18:18:33 -07003105 keyCode = mLocked.currentVirtualKey.keyCode;
3106 scanCode = mLocked.currentVirtualKey.scanCode;
3107 downTime = mLocked.currentVirtualKey.downTime;
3108 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07003109
3110 // Dispatch virtual key.
3111 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown0eaf3932010-10-01 14:55:30 -07003112 policyFlags |= POLICY_FLAG_VIRTUAL;
Jeff Brownb6997262010-10-08 22:31:17 -07003113 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3114 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3115 return touchResult;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003116}
3117
Jeff Brownefd32662011-03-08 15:13:06 -08003118void TouchInputMapper::suppressSwipeOntoVirtualKeys(nsecs_t when) {
Jeff Brownfe508922011-01-18 15:10:10 -08003119 // Disable all virtual key touches that happen within a short time interval of the
3120 // most recent touch. The idea is to filter out stray virtual key presses when
3121 // interacting with the touch screen.
3122 //
3123 // Problems we're trying to solve:
3124 //
3125 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3126 // virtual key area that is implemented by a separate touch panel and accidentally
3127 // triggers a virtual key.
3128 //
3129 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3130 // area and accidentally triggers a virtual key. This often happens when virtual keys
3131 // are layed out below the screen near to where the on screen keyboard's space bar
3132 // is displayed.
3133 if (mParameters.virtualKeyQuietTime > 0 && mCurrentTouch.pointerCount != 0) {
3134 mContext->disableVirtualKeysUntil(when + mParameters.virtualKeyQuietTime);
3135 }
3136}
3137
Jeff Brown6d0fec22010-07-23 21:28:06 -07003138void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
3139 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
3140 uint32_t lastPointerCount = mLastTouch.pointerCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003141 if (currentPointerCount == 0 && lastPointerCount == 0) {
3142 return; // nothing to do!
3143 }
3144
Jeff Brownace13b12011-03-09 17:39:48 -08003145 // Update current touch coordinates.
3146 int32_t edgeFlags;
3147 float xPrecision, yPrecision;
3148 prepareTouches(&edgeFlags, &xPrecision, &yPrecision);
3149
3150 // Dispatch motions.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003151 BitSet32 currentIdBits = mCurrentTouch.idBits;
3152 BitSet32 lastIdBits = mLastTouch.idBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003153 int32_t metaState = getContext()->getGlobalMetaState();
3154 int32_t buttonState = mCurrentTouch.buttonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003155
3156 if (currentIdBits == lastIdBits) {
3157 // No pointer id changes so this is a move event.
3158 // The dispatcher takes care of batching moves so we don't have to deal with that here.
Jeff Brownace13b12011-03-09 17:39:48 -08003159 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003160 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3161 AMOTION_EVENT_EDGE_FLAG_NONE,
3162 mCurrentTouchProperties, mCurrentTouchCoords,
3163 mCurrentTouch.idToIndex, currentIdBits, -1,
Jeff Brownace13b12011-03-09 17:39:48 -08003164 xPrecision, yPrecision, mDownTime);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003165 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003166 // There may be pointers going up and pointers going down and pointers moving
3167 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003168 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3169 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003170 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003171 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003172
Jeff Brownace13b12011-03-09 17:39:48 -08003173 // Update last coordinates of pointers that have moved so that we observe the new
3174 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003175 bool moveNeeded = updateMovedPointers(
3176 mCurrentTouchProperties, mCurrentTouchCoords, mCurrentTouch.idToIndex,
3177 mLastTouchProperties, mLastTouchCoords, mLastTouch.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003178 moveIdBits);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003179 if (buttonState != mLastTouch.buttonState) {
3180 moveNeeded = true;
3181 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003182
Jeff Brownace13b12011-03-09 17:39:48 -08003183 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003184 while (!upIdBits.isEmpty()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003185 uint32_t upId = upIdBits.firstMarkedBit();
3186 upIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003187
Jeff Brownace13b12011-03-09 17:39:48 -08003188 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003189 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
3190 mLastTouchProperties, mLastTouchCoords,
3191 mLastTouch.idToIndex, dispatchedIdBits, upId,
Jeff Brownace13b12011-03-09 17:39:48 -08003192 xPrecision, yPrecision, mDownTime);
3193 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003194 }
3195
Jeff Brownc3db8582010-10-20 15:33:38 -07003196 // Dispatch move events if any of the remaining pointers moved from their old locations.
3197 // Although applications receive new locations as part of individual pointer up
3198 // events, they do not generally handle them except when presented in a move event.
3199 if (moveNeeded) {
Jeff Brownb6110c22011-04-01 16:15:13 -07003200 LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003201 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003202 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
3203 mCurrentTouchProperties, mCurrentTouchCoords,
3204 mCurrentTouch.idToIndex, dispatchedIdBits, -1,
Jeff Brownace13b12011-03-09 17:39:48 -08003205 xPrecision, yPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003206 }
3207
3208 // Dispatch pointer down events using the new pointer locations.
3209 while (!downIdBits.isEmpty()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003210 uint32_t downId = downIdBits.firstMarkedBit();
3211 downIdBits.clearBit(downId);
Jeff Brownace13b12011-03-09 17:39:48 -08003212 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003213
Jeff Brownace13b12011-03-09 17:39:48 -08003214 if (dispatchedIdBits.count() == 1) {
3215 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003216 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003217 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08003218 // Only send edge flags with first pointer down.
3219 edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003220 }
3221
Jeff Brownace13b12011-03-09 17:39:48 -08003222 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003223 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, edgeFlags,
3224 mCurrentTouchProperties, mCurrentTouchCoords,
3225 mCurrentTouch.idToIndex, dispatchedIdBits, downId,
Jeff Brownace13b12011-03-09 17:39:48 -08003226 xPrecision, yPrecision, mDownTime);
3227 }
3228 }
3229
3230 // Update state for next time.
3231 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003232 mLastTouchProperties[i].copyFrom(mCurrentTouchProperties[i]);
Jeff Brownace13b12011-03-09 17:39:48 -08003233 mLastTouchCoords[i].copyFrom(mCurrentTouchCoords[i]);
3234 }
3235}
3236
3237void TouchInputMapper::prepareTouches(int32_t* outEdgeFlags,
3238 float* outXPrecision, float* outYPrecision) {
3239 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
3240 uint32_t lastPointerCount = mLastTouch.pointerCount;
3241
3242 AutoMutex _l(mLock);
3243
3244 // Walk through the the active pointers and map touch screen coordinates (TouchData) into
3245 // display or surface coordinates (PointerCoords) and adjust for display orientation.
3246 for (uint32_t i = 0; i < currentPointerCount; i++) {
3247 const PointerData& in = mCurrentTouch.pointers[i];
3248
3249 // ToolMajor and ToolMinor
3250 float toolMajor, toolMinor;
3251 switch (mCalibration.toolSizeCalibration) {
3252 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
3253 toolMajor = in.toolMajor * mLocked.geometricScale;
3254 if (mRawAxes.toolMinor.valid) {
3255 toolMinor = in.toolMinor * mLocked.geometricScale;
3256 } else {
3257 toolMinor = toolMajor;
3258 }
3259 break;
3260 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
3261 toolMajor = in.toolMajor != 0
3262 ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias
3263 : 0;
3264 if (mRawAxes.toolMinor.valid) {
3265 toolMinor = in.toolMinor != 0
3266 ? in.toolMinor * mLocked.toolSizeLinearScale
3267 + mLocked.toolSizeLinearBias
3268 : 0;
3269 } else {
3270 toolMinor = toolMajor;
3271 }
3272 break;
3273 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
3274 if (in.toolMajor != 0) {
3275 float diameter = sqrtf(in.toolMajor
3276 * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias);
3277 toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias;
3278 } else {
3279 toolMajor = 0;
3280 }
3281 toolMinor = toolMajor;
3282 break;
3283 default:
3284 toolMajor = 0;
3285 toolMinor = 0;
3286 break;
3287 }
3288
3289 if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) {
3290 toolMajor /= currentPointerCount;
3291 toolMinor /= currentPointerCount;
3292 }
3293
3294 // Pressure
3295 float rawPressure;
3296 switch (mCalibration.pressureSource) {
3297 case Calibration::PRESSURE_SOURCE_PRESSURE:
3298 rawPressure = in.pressure;
3299 break;
3300 case Calibration::PRESSURE_SOURCE_TOUCH:
3301 rawPressure = in.touchMajor;
3302 break;
3303 default:
3304 rawPressure = 0;
3305 }
3306
3307 float pressure;
3308 switch (mCalibration.pressureCalibration) {
3309 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3310 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
3311 pressure = rawPressure * mLocked.pressureScale;
3312 break;
3313 default:
3314 pressure = 1;
3315 break;
3316 }
3317
3318 // TouchMajor and TouchMinor
3319 float touchMajor, touchMinor;
3320 switch (mCalibration.touchSizeCalibration) {
3321 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
3322 touchMajor = in.touchMajor * mLocked.geometricScale;
3323 if (mRawAxes.touchMinor.valid) {
3324 touchMinor = in.touchMinor * mLocked.geometricScale;
3325 } else {
3326 touchMinor = touchMajor;
3327 }
3328 break;
3329 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
3330 touchMajor = toolMajor * pressure;
3331 touchMinor = toolMinor * pressure;
3332 break;
3333 default:
3334 touchMajor = 0;
3335 touchMinor = 0;
3336 break;
3337 }
3338
3339 if (touchMajor > toolMajor) {
3340 touchMajor = toolMajor;
3341 }
3342 if (touchMinor > toolMinor) {
3343 touchMinor = toolMinor;
3344 }
3345
3346 // Size
3347 float size;
3348 switch (mCalibration.sizeCalibration) {
3349 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
3350 float rawSize = mRawAxes.toolMinor.valid
3351 ? avg(in.toolMajor, in.toolMinor)
3352 : in.toolMajor;
3353 size = rawSize * mLocked.sizeScale;
3354 break;
3355 }
3356 default:
3357 size = 0;
3358 break;
3359 }
3360
3361 // Orientation
3362 float orientation;
3363 switch (mCalibration.orientationCalibration) {
3364 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
3365 orientation = in.orientation * mLocked.orientationScale;
3366 break;
3367 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3368 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3369 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3370 if (c1 != 0 || c2 != 0) {
3371 orientation = atan2f(c1, c2) * 0.5f;
Jeff Brown2352b972011-04-12 22:39:53 -07003372 float scale = 1.0f + hypotf(c1, c2) / 16.0f;
Jeff Brownace13b12011-03-09 17:39:48 -08003373 touchMajor *= scale;
3374 touchMinor /= scale;
3375 toolMajor *= scale;
3376 toolMinor /= scale;
3377 } else {
3378 orientation = 0;
3379 }
3380 break;
3381 }
3382 default:
3383 orientation = 0;
3384 }
3385
Jeff Brown80fd47c2011-05-24 01:07:44 -07003386 // Distance
3387 float distance;
3388 switch (mCalibration.distanceCalibration) {
3389 case Calibration::DISTANCE_CALIBRATION_SCALED:
3390 distance = in.distance * mLocked.distanceScale;
3391 break;
3392 default:
3393 distance = 0;
3394 }
3395
Jeff Brownace13b12011-03-09 17:39:48 -08003396 // X and Y
3397 // Adjust coords for surface orientation.
3398 float x, y;
3399 switch (mLocked.surfaceOrientation) {
3400 case DISPLAY_ORIENTATION_90:
3401 x = float(in.y - mRawAxes.y.minValue) * mLocked.yScale;
3402 y = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale;
3403 orientation -= M_PI_2;
3404 if (orientation < - M_PI_2) {
3405 orientation += M_PI;
3406 }
3407 break;
3408 case DISPLAY_ORIENTATION_180:
3409 x = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale;
3410 y = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale;
3411 break;
3412 case DISPLAY_ORIENTATION_270:
3413 x = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale;
3414 y = float(in.x - mRawAxes.x.minValue) * mLocked.xScale;
3415 orientation += M_PI_2;
3416 if (orientation > M_PI_2) {
3417 orientation -= M_PI;
3418 }
3419 break;
3420 default:
3421 x = float(in.x - mRawAxes.x.minValue) * mLocked.xScale;
3422 y = float(in.y - mRawAxes.y.minValue) * mLocked.yScale;
3423 break;
3424 }
3425
3426 // Write output coords.
3427 PointerCoords& out = mCurrentTouchCoords[i];
3428 out.clear();
3429 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3430 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3431 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3432 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3433 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3434 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3435 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3436 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3437 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown80fd47c2011-05-24 01:07:44 -07003438 if (distance != 0) {
3439 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
3440 }
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003441
3442 // Write output properties.
3443 PointerProperties& properties = mCurrentTouchProperties[i];
3444 properties.clear();
3445 properties.id = mCurrentTouch.pointers[i].id;
3446 properties.toolType = getTouchToolType(mCurrentTouch.pointers[i].isStylus);
Jeff Brownace13b12011-03-09 17:39:48 -08003447 }
3448
3449 // Check edge flags by looking only at the first pointer since the flags are
3450 // global to the event.
3451 *outEdgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
3452 if (lastPointerCount == 0 && currentPointerCount > 0) {
3453 const PointerData& in = mCurrentTouch.pointers[0];
3454
3455 if (in.x <= mRawAxes.x.minValue) {
3456 *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_LEFT,
3457 mLocked.surfaceOrientation);
3458 } else if (in.x >= mRawAxes.x.maxValue) {
3459 *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_RIGHT,
3460 mLocked.surfaceOrientation);
3461 }
3462 if (in.y <= mRawAxes.y.minValue) {
3463 *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_TOP,
3464 mLocked.surfaceOrientation);
3465 } else if (in.y >= mRawAxes.y.maxValue) {
3466 *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_BOTTOM,
3467 mLocked.surfaceOrientation);
3468 }
3469 }
3470
3471 *outXPrecision = mLocked.orientedXPrecision;
3472 *outYPrecision = mLocked.orientedYPrecision;
3473}
3474
Jeff Brown79ac9692011-04-19 21:20:10 -07003475void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3476 bool isTimeout) {
Jeff Brown2352b972011-04-12 22:39:53 -07003477 // Switch pointer presentation.
3478 mPointerController->setPresentation(
3479 mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3480 ? PointerControllerInterface::PRESENTATION_SPOT
3481 : PointerControllerInterface::PRESENTATION_POINTER);
3482
Jeff Brownace13b12011-03-09 17:39:48 -08003483 // Update current gesture coordinates.
3484 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07003485 bool sendEvents = preparePointerGestures(when,
3486 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3487 if (!sendEvents) {
3488 return;
3489 }
Jeff Brownace13b12011-03-09 17:39:48 -08003490
Jeff Brown538881e2011-05-25 18:23:38 -07003491 // Show or hide the pointer if needed.
3492 switch (mPointerGesture.currentGestureMode) {
3493 case PointerGesture::NEUTRAL:
3494 case PointerGesture::QUIET:
3495 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3496 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3497 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
3498 // Remind the user of where the pointer is after finishing a gesture with spots.
3499 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
3500 }
3501 break;
3502 case PointerGesture::TAP:
3503 case PointerGesture::TAP_DRAG:
3504 case PointerGesture::BUTTON_CLICK_OR_DRAG:
3505 case PointerGesture::HOVER:
3506 case PointerGesture::PRESS:
3507 // Unfade the pointer when the current gesture manipulates the
3508 // area directly under the pointer.
3509 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3510 break;
3511 case PointerGesture::SWIPE:
3512 case PointerGesture::FREEFORM:
3513 // Fade the pointer when the current gesture manipulates a different
3514 // area and there are spots to guide the user experience.
3515 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3516 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3517 } else {
3518 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3519 }
3520 break;
Jeff Brown2352b972011-04-12 22:39:53 -07003521 }
3522
Jeff Brownace13b12011-03-09 17:39:48 -08003523 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003524 int32_t metaState = getContext()->getGlobalMetaState();
3525 int32_t buttonState = mCurrentTouch.buttonState;
Jeff Brownace13b12011-03-09 17:39:48 -08003526
3527 // Update last coordinates of pointers that have moved so that we observe the new
3528 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07003529 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
3530 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
3531 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07003532 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08003533 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
3534 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
3535 bool moveNeeded = false;
3536 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07003537 && !mPointerGesture.lastGestureIdBits.isEmpty()
3538 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08003539 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
3540 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003541 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003542 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003543 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003544 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3545 movedGestureIdBits);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003546 if (buttonState != mLastTouch.buttonState) {
3547 moveNeeded = true;
3548 }
Jeff Brownace13b12011-03-09 17:39:48 -08003549 }
3550
3551 // Send motion events for all pointers that went up or were canceled.
3552 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
3553 if (!dispatchedGestureIdBits.isEmpty()) {
3554 if (cancelPreviousGesture) {
3555 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003556 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
3557 AMOTION_EVENT_EDGE_FLAG_NONE,
3558 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003559 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3560 dispatchedGestureIdBits, -1,
3561 0, 0, mPointerGesture.downTime);
3562
3563 dispatchedGestureIdBits.clear();
3564 } else {
3565 BitSet32 upGestureIdBits;
3566 if (finishPreviousGesture) {
3567 upGestureIdBits = dispatchedGestureIdBits;
3568 } else {
3569 upGestureIdBits.value = dispatchedGestureIdBits.value
3570 & ~mPointerGesture.currentGestureIdBits.value;
3571 }
3572 while (!upGestureIdBits.isEmpty()) {
3573 uint32_t id = upGestureIdBits.firstMarkedBit();
3574 upGestureIdBits.clearBit(id);
3575
3576 dispatchMotion(when, policyFlags, mPointerSource,
3577 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003578 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3579 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003580 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3581 dispatchedGestureIdBits, id,
3582 0, 0, mPointerGesture.downTime);
3583
3584 dispatchedGestureIdBits.clearBit(id);
3585 }
3586 }
3587 }
3588
3589 // Send motion events for all pointers that moved.
3590 if (moveNeeded) {
3591 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003592 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3593 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003594 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3595 dispatchedGestureIdBits, -1,
3596 0, 0, mPointerGesture.downTime);
3597 }
3598
3599 // Send motion events for all pointers that went down.
3600 if (down) {
3601 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
3602 & ~dispatchedGestureIdBits.value);
3603 while (!downGestureIdBits.isEmpty()) {
3604 uint32_t id = downGestureIdBits.firstMarkedBit();
3605 downGestureIdBits.clearBit(id);
3606 dispatchedGestureIdBits.markBit(id);
3607
3608 int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
3609 if (dispatchedGestureIdBits.count() == 1) {
3610 // First pointer is going down. Calculate edge flags and set down time.
3611 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
3612 const PointerCoords& downCoords = mPointerGesture.currentGestureCoords[index];
3613 edgeFlags = calculateEdgeFlagsUsingPointerBounds(mPointerController,
3614 downCoords.getAxisValue(AMOTION_EVENT_AXIS_X),
3615 downCoords.getAxisValue(AMOTION_EVENT_AXIS_Y));
3616 mPointerGesture.downTime = when;
3617 }
3618
3619 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003620 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, edgeFlags,
3621 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003622 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3623 dispatchedGestureIdBits, id,
3624 0, 0, mPointerGesture.downTime);
3625 }
3626 }
3627
Jeff Brownace13b12011-03-09 17:39:48 -08003628 // Send motion events for hover.
3629 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
3630 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003631 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
3632 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3633 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003634 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3635 mPointerGesture.currentGestureIdBits, -1,
3636 0, 0, mPointerGesture.downTime);
3637 }
3638
3639 // Update state.
3640 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
3641 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08003642 mPointerGesture.lastGestureIdBits.clear();
3643 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08003644 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
3645 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
3646 uint32_t id = idBits.firstMarkedBit();
3647 idBits.clearBit(id);
3648 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003649 mPointerGesture.lastGestureProperties[index].copyFrom(
3650 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08003651 mPointerGesture.lastGestureCoords[index].copyFrom(
3652 mPointerGesture.currentGestureCoords[index]);
3653 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003654 }
3655 }
3656}
3657
Jeff Brown79ac9692011-04-19 21:20:10 -07003658bool TouchInputMapper::preparePointerGestures(nsecs_t when,
3659 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003660 *outCancelPreviousGesture = false;
3661 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003662
Jeff Brownace13b12011-03-09 17:39:48 -08003663 AutoMutex _l(mLock);
Jeff Brown6328cdc2010-07-29 18:18:33 -07003664
Jeff Brown79ac9692011-04-19 21:20:10 -07003665 // Handle TAP timeout.
3666 if (isTimeout) {
3667#if DEBUG_GESTURES
3668 LOGD("Gestures: Processing timeout");
3669#endif
3670
3671 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
3672 if (when <= mPointerGesture.tapUpTime + TAP_DRAG_INTERVAL) {
3673 // The tap/drag timeout has not yet expired.
3674 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime + TAP_DRAG_INTERVAL);
3675 } else {
3676 // The tap is finished.
3677#if DEBUG_GESTURES
3678 LOGD("Gestures: TAP finished");
3679#endif
3680 *outFinishPreviousGesture = true;
3681
3682 mPointerGesture.activeGestureId = -1;
3683 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
3684 mPointerGesture.currentGestureIdBits.clear();
3685
Jeff Brown79ac9692011-04-19 21:20:10 -07003686 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3687 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_NEUTRAL;
3688 mPointerGesture.spotIdBits.clear();
3689 moveSpotsLocked();
3690 }
3691 return true;
3692 }
3693 }
3694
3695 // We did not handle this timeout.
3696 return false;
3697 }
3698
Jeff Brownace13b12011-03-09 17:39:48 -08003699 // Update the velocity tracker.
3700 {
3701 VelocityTracker::Position positions[MAX_POINTERS];
3702 uint32_t count = 0;
3703 for (BitSet32 idBits(mCurrentTouch.idBits); !idBits.isEmpty(); count++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003704 uint32_t id = idBits.firstMarkedBit();
3705 idBits.clearBit(id);
Jeff Brownace13b12011-03-09 17:39:48 -08003706 uint32_t index = mCurrentTouch.idToIndex[id];
3707 positions[count].x = mCurrentTouch.pointers[index].x
3708 * mLocked.pointerGestureXMovementScale;
3709 positions[count].y = mCurrentTouch.pointers[index].y
3710 * mLocked.pointerGestureYMovementScale;
3711 }
3712 mPointerGesture.velocityTracker.addMovement(when, mCurrentTouch.idBits, positions);
3713 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003714
Jeff Brownace13b12011-03-09 17:39:48 -08003715 // Pick a new active touch id if needed.
3716 // Choose an arbitrary pointer that just went down, if there is one.
3717 // Otherwise choose an arbitrary remaining pointer.
3718 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07003719 // We keep the same active touch id for as long as possible.
3720 bool activeTouchChanged = false;
3721 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
3722 int32_t activeTouchId = lastActiveTouchId;
3723 if (activeTouchId < 0) {
3724 if (!mCurrentTouch.idBits.isEmpty()) {
3725 activeTouchChanged = true;
3726 activeTouchId = mPointerGesture.activeTouchId = mCurrentTouch.idBits.firstMarkedBit();
3727 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08003728 }
Jeff Brown2352b972011-04-12 22:39:53 -07003729 } else if (!mCurrentTouch.idBits.hasBit(activeTouchId)) {
3730 activeTouchChanged = true;
3731 if (!mCurrentTouch.idBits.isEmpty()) {
3732 activeTouchId = mPointerGesture.activeTouchId = mCurrentTouch.idBits.firstMarkedBit();
3733 } else {
3734 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08003735 }
3736 }
3737
3738 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07003739 bool isQuietTime = false;
3740 if (activeTouchId < 0) {
3741 mPointerGesture.resetQuietTime();
3742 } else {
3743 isQuietTime = when < mPointerGesture.quietTime + QUIET_INTERVAL;
3744 if (!isQuietTime) {
3745 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
3746 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3747 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
3748 && mCurrentTouch.pointerCount < 2) {
3749 // Enter quiet time when exiting swipe or freeform state.
3750 // This is to prevent accidentally entering the hover state and flinging the
3751 // pointer when finishing a swipe and there is still one pointer left onscreen.
3752 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07003753 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07003754 && mCurrentTouch.pointerCount >= 2
3755 && !isPointerDown(mCurrentTouch.buttonState)) {
3756 // Enter quiet time when releasing the button and there are still two or more
3757 // fingers down. This may indicate that one finger was used to press the button
3758 // but it has not gone up yet.
3759 isQuietTime = true;
3760 }
3761 if (isQuietTime) {
3762 mPointerGesture.quietTime = when;
3763 }
Jeff Brownace13b12011-03-09 17:39:48 -08003764 }
3765 }
3766
3767 // Switch states based on button and pointer state.
3768 if (isQuietTime) {
3769 // Case 1: Quiet time. (QUIET)
3770#if DEBUG_GESTURES
3771 LOGD("Gestures: QUIET for next %0.3fms",
3772 (mPointerGesture.quietTime + QUIET_INTERVAL - when) * 0.000001f);
3773#endif
3774 *outFinishPreviousGesture = true;
3775
3776 mPointerGesture.activeGestureId = -1;
3777 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08003778 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07003779
Jeff Brown2352b972011-04-12 22:39:53 -07003780 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3781 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_NEUTRAL;
3782 mPointerGesture.spotIdBits.clear();
3783 moveSpotsLocked();
3784 }
Jeff Brownace13b12011-03-09 17:39:48 -08003785 } else if (isPointerDown(mCurrentTouch.buttonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003786 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08003787 // The pointer follows the active touch point.
3788 // Emit DOWN, MOVE, UP events at the pointer location.
3789 //
3790 // Only the active touch matters; other fingers are ignored. This policy helps
3791 // to handle the case where the user places a second finger on the touch pad
3792 // to apply the necessary force to depress an integrated button below the surface.
3793 // We don't want the second finger to be delivered to applications.
3794 //
3795 // For this to work well, we need to make sure to track the pointer that is really
3796 // active. If the user first puts one finger down to click then adds another
3797 // finger to drag then the active pointer should switch to the finger that is
3798 // being dragged.
3799#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07003800 LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brownace13b12011-03-09 17:39:48 -08003801 "currentTouchPointerCount=%d", activeTouchId, mCurrentTouch.pointerCount);
3802#endif
3803 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07003804 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08003805 *outFinishPreviousGesture = true;
3806 mPointerGesture.activeGestureId = 0;
3807 }
3808
3809 // Switch pointers if needed.
3810 // Find the fastest pointer and follow it.
3811 if (activeTouchId >= 0) {
3812 if (mCurrentTouch.pointerCount > 1) {
3813 int32_t bestId = -1;
3814 float bestSpeed = DRAG_MIN_SWITCH_SPEED;
3815 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
3816 uint32_t id = mCurrentTouch.pointers[i].id;
3817 float vx, vy;
3818 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
Jeff Brown2352b972011-04-12 22:39:53 -07003819 float speed = hypotf(vx, vy);
Jeff Brownace13b12011-03-09 17:39:48 -08003820 if (speed > bestSpeed) {
3821 bestId = id;
3822 bestSpeed = speed;
3823 }
3824 }
Jeff Brown8d608662010-08-30 03:02:23 -07003825 }
Jeff Brownace13b12011-03-09 17:39:48 -08003826 if (bestId >= 0 && bestId != activeTouchId) {
3827 mPointerGesture.activeTouchId = activeTouchId = bestId;
Jeff Brown2352b972011-04-12 22:39:53 -07003828 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08003829#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07003830 LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
Jeff Brownace13b12011-03-09 17:39:48 -08003831 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
3832#endif
Jeff Brown8d608662010-08-30 03:02:23 -07003833 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003834 }
3835
Jeff Brownace13b12011-03-09 17:39:48 -08003836 if (mLastTouch.idBits.hasBit(activeTouchId)) {
3837 const PointerData& currentPointer =
3838 mCurrentTouch.pointers[mCurrentTouch.idToIndex[activeTouchId]];
3839 const PointerData& lastPointer =
3840 mLastTouch.pointers[mLastTouch.idToIndex[activeTouchId]];
3841 float deltaX = (currentPointer.x - lastPointer.x)
3842 * mLocked.pointerGestureXMovementScale;
3843 float deltaY = (currentPointer.y - lastPointer.y)
3844 * mLocked.pointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07003845
3846 // Move the pointer using a relative motion.
3847 // When using spots, the click will occur at the position of the anchor
3848 // spot and all other spots will move there.
Jeff Brownace13b12011-03-09 17:39:48 -08003849 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07003850 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003851 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003852
Jeff Brownace13b12011-03-09 17:39:48 -08003853 float x, y;
3854 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08003855
Jeff Brown79ac9692011-04-19 21:20:10 -07003856 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08003857 mPointerGesture.currentGestureIdBits.clear();
3858 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3859 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003860 mPointerGesture.currentGestureProperties[0].clear();
3861 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3862 mPointerGesture.currentGestureProperties[0].toolType =
3863 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08003864 mPointerGesture.currentGestureCoords[0].clear();
3865 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3866 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3867 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07003868
Jeff Brown2352b972011-04-12 22:39:53 -07003869 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3870 if (activeTouchId >= 0) {
3871 // Collapse all spots into one point at the pointer location.
3872 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_BUTTON_DRAG;
3873 mPointerGesture.spotIdBits.clear();
3874 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
3875 uint32_t id = mCurrentTouch.pointers[i].id;
3876 mPointerGesture.spotIdBits.markBit(id);
3877 mPointerGesture.spotIdToIndex[id] = i;
3878 mPointerGesture.spotCoords[i] = mPointerGesture.currentGestureCoords[0];
3879 }
3880 } else {
3881 // No fingers. Generate a spot at the pointer location so the
3882 // anchor appears to be pressed.
3883 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_BUTTON_CLICK;
3884 mPointerGesture.spotIdBits.clear();
3885 mPointerGesture.spotIdBits.markBit(0);
3886 mPointerGesture.spotIdToIndex[0] = 0;
3887 mPointerGesture.spotCoords[0] = mPointerGesture.currentGestureCoords[0];
3888 }
3889 moveSpotsLocked();
3890 }
Jeff Brownace13b12011-03-09 17:39:48 -08003891 } else if (mCurrentTouch.pointerCount == 0) {
3892 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
3893 *outFinishPreviousGesture = true;
3894
Jeff Brown79ac9692011-04-19 21:20:10 -07003895 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brownace13b12011-03-09 17:39:48 -08003896 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07003897 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
3898 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown2352b972011-04-12 22:39:53 -07003899 && mLastTouch.pointerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003900 if (when <= mPointerGesture.tapDownTime + TAP_INTERVAL) {
Jeff Brownace13b12011-03-09 17:39:48 -08003901 float x, y;
3902 mPointerController->getPosition(&x, &y);
Jeff Brown2352b972011-04-12 22:39:53 -07003903 if (fabs(x - mPointerGesture.tapX) <= TAP_SLOP
3904 && fabs(y - mPointerGesture.tapY) <= TAP_SLOP) {
Jeff Brownace13b12011-03-09 17:39:48 -08003905#if DEBUG_GESTURES
3906 LOGD("Gestures: TAP");
3907#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07003908
3909 mPointerGesture.tapUpTime = when;
3910 getContext()->requestTimeoutAtTime(when + TAP_DRAG_INTERVAL);
3911
Jeff Brownace13b12011-03-09 17:39:48 -08003912 mPointerGesture.activeGestureId = 0;
3913 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08003914 mPointerGesture.currentGestureIdBits.clear();
3915 mPointerGesture.currentGestureIdBits.markBit(
3916 mPointerGesture.activeGestureId);
3917 mPointerGesture.currentGestureIdToIndex[
3918 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003919 mPointerGesture.currentGestureProperties[0].clear();
3920 mPointerGesture.currentGestureProperties[0].id =
3921 mPointerGesture.activeGestureId;
3922 mPointerGesture.currentGestureProperties[0].toolType =
3923 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08003924 mPointerGesture.currentGestureCoords[0].clear();
3925 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07003926 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08003927 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07003928 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08003929 mPointerGesture.currentGestureCoords[0].setAxisValue(
3930 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07003931
Jeff Brown2352b972011-04-12 22:39:53 -07003932 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3933 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_TAP;
3934 mPointerGesture.spotIdBits.clear();
3935 mPointerGesture.spotIdBits.markBit(lastActiveTouchId);
3936 mPointerGesture.spotIdToIndex[lastActiveTouchId] = 0;
3937 mPointerGesture.spotCoords[0] = mPointerGesture.currentGestureCoords[0];
3938 moveSpotsLocked();
3939 }
3940
Jeff Brownace13b12011-03-09 17:39:48 -08003941 tapped = true;
3942 } else {
3943#if DEBUG_GESTURES
3944 LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07003945 x - mPointerGesture.tapX,
3946 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08003947#endif
3948 }
3949 } else {
3950#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07003951 LOGD("Gestures: Not a TAP, %0.3fms since down",
3952 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08003953#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07003954 }
Jeff Brownace13b12011-03-09 17:39:48 -08003955 }
Jeff Brown2352b972011-04-12 22:39:53 -07003956
Jeff Brownace13b12011-03-09 17:39:48 -08003957 if (!tapped) {
3958#if DEBUG_GESTURES
3959 LOGD("Gestures: NEUTRAL");
3960#endif
3961 mPointerGesture.activeGestureId = -1;
3962 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08003963 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07003964
Jeff Brown2352b972011-04-12 22:39:53 -07003965 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3966 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_NEUTRAL;
3967 mPointerGesture.spotIdBits.clear();
3968 moveSpotsLocked();
3969 }
Jeff Brownace13b12011-03-09 17:39:48 -08003970 }
3971 } else if (mCurrentTouch.pointerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003972 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08003973 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07003974 // When in HOVER, emit HOVER_MOVE events at the pointer location.
3975 // When in TAP_DRAG, emit MOVE events at the pointer location.
Jeff Brownb6110c22011-04-01 16:15:13 -07003976 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08003977
Jeff Brown79ac9692011-04-19 21:20:10 -07003978 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
3979 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
3980 if (when <= mPointerGesture.tapUpTime + TAP_DRAG_INTERVAL) {
3981 float x, y;
3982 mPointerController->getPosition(&x, &y);
3983 if (fabs(x - mPointerGesture.tapX) <= TAP_SLOP
3984 && fabs(y - mPointerGesture.tapY) <= TAP_SLOP) {
3985 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
3986 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08003987#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07003988 LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
3989 x - mPointerGesture.tapX,
3990 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08003991#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07003992 }
3993 } else {
3994#if DEBUG_GESTURES
3995 LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
3996 (when - mPointerGesture.tapUpTime) * 0.000001f);
3997#endif
3998 }
3999 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4000 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4001 }
Jeff Brownace13b12011-03-09 17:39:48 -08004002
4003 if (mLastTouch.idBits.hasBit(activeTouchId)) {
4004 const PointerData& currentPointer =
4005 mCurrentTouch.pointers[mCurrentTouch.idToIndex[activeTouchId]];
4006 const PointerData& lastPointer =
4007 mLastTouch.pointers[mLastTouch.idToIndex[activeTouchId]];
4008 float deltaX = (currentPointer.x - lastPointer.x)
4009 * mLocked.pointerGestureXMovementScale;
4010 float deltaY = (currentPointer.y - lastPointer.y)
4011 * mLocked.pointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004012
4013 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004014 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004015 mPointerController->move(deltaX, deltaY);
4016 }
4017
Jeff Brown79ac9692011-04-19 21:20:10 -07004018 bool down;
4019 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4020#if DEBUG_GESTURES
4021 LOGD("Gestures: TAP_DRAG");
4022#endif
4023 down = true;
4024 } else {
4025#if DEBUG_GESTURES
4026 LOGD("Gestures: HOVER");
4027#endif
4028 *outFinishPreviousGesture = true;
4029 mPointerGesture.activeGestureId = 0;
4030 down = false;
4031 }
Jeff Brownace13b12011-03-09 17:39:48 -08004032
4033 float x, y;
4034 mPointerController->getPosition(&x, &y);
4035
Jeff Brownace13b12011-03-09 17:39:48 -08004036 mPointerGesture.currentGestureIdBits.clear();
4037 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4038 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004039 mPointerGesture.currentGestureProperties[0].clear();
4040 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4041 mPointerGesture.currentGestureProperties[0].toolType =
4042 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004043 mPointerGesture.currentGestureCoords[0].clear();
4044 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4045 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004046 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4047 down ? 1.0f : 0.0f);
4048
Jeff Brownace13b12011-03-09 17:39:48 -08004049 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004050 mPointerGesture.resetTap();
4051 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004052 mPointerGesture.tapX = x;
4053 mPointerGesture.tapY = y;
4054 }
4055
Jeff Brown2352b972011-04-12 22:39:53 -07004056 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004057 mPointerGesture.spotGesture = down ? PointerControllerInterface::SPOT_GESTURE_DRAG
4058 : PointerControllerInterface::SPOT_GESTURE_HOVER;
Jeff Brown2352b972011-04-12 22:39:53 -07004059 mPointerGesture.spotIdBits.clear();
4060 mPointerGesture.spotIdBits.markBit(activeTouchId);
4061 mPointerGesture.spotIdToIndex[activeTouchId] = 0;
4062 mPointerGesture.spotCoords[0] = mPointerGesture.currentGestureCoords[0];
4063 moveSpotsLocked();
Jeff Brownace13b12011-03-09 17:39:48 -08004064 }
4065 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004066 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4067 // We need to provide feedback for each finger that goes down so we cannot wait
4068 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004069 //
Jeff Brown2352b972011-04-12 22:39:53 -07004070 // The ambiguous case is deciding what to do when there are two fingers down but they
4071 // have not moved enough to determine whether they are part of a drag or part of a
4072 // freeform gesture, or just a press or long-press at the pointer location.
4073 //
4074 // When there are two fingers we start with the PRESS hypothesis and we generate a
4075 // down at the pointer location.
4076 //
4077 // When the two fingers move enough or when additional fingers are added, we make
4078 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Jeff Brownb6110c22011-04-01 16:15:13 -07004079 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004080
Jeff Brown2352b972011-04-12 22:39:53 -07004081 bool needReference = false;
4082 bool settled = when >= mPointerGesture.firstTouchTime + MULTITOUCH_SETTLE_INTERVAL;
4083 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004084 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4085 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004086 *outFinishPreviousGesture = true;
Jeff Brown2352b972011-04-12 22:39:53 -07004087 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4088 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004089 mPointerGesture.referenceIdBits.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08004090
Jeff Brown2352b972011-04-12 22:39:53 -07004091 if (settled && mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
4092 && mLastTouch.idBits.hasBit(mPointerGesture.activeTouchId)) {
4093 // The spot is already visible and has settled, use it as the reference point
4094 // for the gesture. Other spots will be positioned relative to this one.
4095#if DEBUG_GESTURES
4096 LOGD("Gestures: Using active spot as reference for MULTITOUCH, "
4097 "settle time expired %0.3fms ago",
4098 (when - mPointerGesture.firstTouchTime - MULTITOUCH_SETTLE_INTERVAL)
4099 * 0.000001f);
4100#endif
4101 const PointerData& d = mLastTouch.pointers[mLastTouch.idToIndex[
4102 mPointerGesture.activeTouchId]];
4103 mPointerGesture.referenceTouchX = d.x;
4104 mPointerGesture.referenceTouchY = d.y;
4105 const PointerCoords& c = mPointerGesture.spotCoords[mPointerGesture.spotIdToIndex[
4106 mPointerGesture.activeTouchId]];
4107 mPointerGesture.referenceGestureX = c.getAxisValue(AMOTION_EVENT_AXIS_X);
4108 mPointerGesture.referenceGestureY = c.getAxisValue(AMOTION_EVENT_AXIS_Y);
4109 } else {
4110#if DEBUG_GESTURES
4111 LOGD("Gestures: Using centroid as reference for MULTITOUCH, "
4112 "settle time remaining %0.3fms",
4113 (mPointerGesture.firstTouchTime + MULTITOUCH_SETTLE_INTERVAL - when)
4114 * 0.000001f);
4115#endif
4116 needReference = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004117 }
Jeff Brown2352b972011-04-12 22:39:53 -07004118 } else if (!settled && mCurrentTouch.pointerCount > mLastTouch.pointerCount) {
4119 // Additional pointers have gone down but not yet settled.
4120 // Reset the gesture.
4121#if DEBUG_GESTURES
4122 LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
4123 "settle time remaining %0.3fms",
4124 (mPointerGesture.firstTouchTime + MULTITOUCH_SETTLE_INTERVAL - when)
4125 * 0.000001f);
4126#endif
4127 *outCancelPreviousGesture = true;
4128 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4129 mPointerGesture.activeGestureId = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08004130 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004131 // Continue previous gesture.
Jeff Brownace13b12011-03-09 17:39:48 -08004132 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4133 }
4134
Jeff Brown2352b972011-04-12 22:39:53 -07004135 if (needReference) {
4136 // Use the centroid and pointer location as the reference points for the gesture.
4137 mCurrentTouch.getCentroid(&mPointerGesture.referenceTouchX,
4138 &mPointerGesture.referenceTouchY);
4139 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4140 &mPointerGesture.referenceGestureY);
4141 }
Jeff Brownace13b12011-03-09 17:39:48 -08004142
Jeff Brown2352b972011-04-12 22:39:53 -07004143 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4144 float d;
4145 if (mCurrentTouch.pointerCount > 2) {
4146 // There are more than two pointers, switch to FREEFORM.
4147#if DEBUG_GESTURES
4148 LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
4149 mCurrentTouch.pointerCount);
4150#endif
4151 *outCancelPreviousGesture = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004152 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown2352b972011-04-12 22:39:53 -07004153 } else if (((d = distance(
4154 mCurrentTouch.pointers[0].x, mCurrentTouch.pointers[0].y,
4155 mCurrentTouch.pointers[1].x, mCurrentTouch.pointers[1].y))
4156 > mLocked.pointerGestureMaxSwipeWidth)) {
4157 // There are two pointers but they are too far apart, switch to FREEFORM.
4158#if DEBUG_GESTURES
4159 LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
4160 d, mLocked.pointerGestureMaxSwipeWidth);
4161#endif
4162 *outCancelPreviousGesture = true;
4163 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4164 } else {
4165 // There are two pointers. Wait for both pointers to start moving
4166 // before deciding whether this is a SWIPE or FREEFORM gesture.
4167 uint32_t id1 = mCurrentTouch.pointers[0].id;
4168 uint32_t id2 = mCurrentTouch.pointers[1].id;
Jeff Brownace13b12011-03-09 17:39:48 -08004169
Jeff Brown2352b972011-04-12 22:39:53 -07004170 float vx1, vy1, vx2, vy2;
4171 mPointerGesture.velocityTracker.getVelocity(id1, &vx1, &vy1);
4172 mPointerGesture.velocityTracker.getVelocity(id2, &vx2, &vy2);
Jeff Brownace13b12011-03-09 17:39:48 -08004173
Jeff Brown2352b972011-04-12 22:39:53 -07004174 float speed1 = hypotf(vx1, vy1);
4175 float speed2 = hypotf(vx2, vy2);
4176 if (speed1 >= MULTITOUCH_MIN_SPEED && speed2 >= MULTITOUCH_MIN_SPEED) {
4177 // Calculate the dot product of the velocity vectors.
Jeff Brownace13b12011-03-09 17:39:48 -08004178 // When the vectors are oriented in approximately the same direction,
4179 // the angle betweeen them is near zero and the cosine of the angle
4180 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
Jeff Brown2352b972011-04-12 22:39:53 -07004181 float dot = vx1 * vx2 + vy1 * vy2;
4182 float cosine = dot / (speed1 * speed2); // denominator always > 0
4183 if (cosine >= SWIPE_TRANSITION_ANGLE_COSINE) {
4184 // Pointers are moving in the same direction. Switch to SWIPE.
4185#if DEBUG_GESTURES
4186 LOGD("Gestures: PRESS transitioned to SWIPE, "
4187 "speed1 %0.3f >= %0.3f, speed2 %0.3f >= %0.3f, "
4188 "cosine %0.3f >= %0.3f",
4189 speed1, MULTITOUCH_MIN_SPEED, speed2, MULTITOUCH_MIN_SPEED,
4190 cosine, SWIPE_TRANSITION_ANGLE_COSINE);
4191#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004192 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
Jeff Brown2352b972011-04-12 22:39:53 -07004193 } else {
4194 // Pointers are moving in different directions. Switch to FREEFORM.
4195#if DEBUG_GESTURES
4196 LOGD("Gestures: PRESS transitioned to FREEFORM, "
4197 "speed1 %0.3f >= %0.3f, speed2 %0.3f >= %0.3f, "
4198 "cosine %0.3f < %0.3f",
4199 speed1, MULTITOUCH_MIN_SPEED, speed2, MULTITOUCH_MIN_SPEED,
4200 cosine, SWIPE_TRANSITION_ANGLE_COSINE);
4201#endif
4202 *outCancelPreviousGesture = true;
4203 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brownace13b12011-03-09 17:39:48 -08004204 }
4205 }
Jeff Brownace13b12011-03-09 17:39:48 -08004206 }
4207 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004208 // Switch from SWIPE to FREEFORM if additional pointers go down.
4209 // Cancel previous gesture.
4210 if (mCurrentTouch.pointerCount > 2) {
4211#if DEBUG_GESTURES
4212 LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
4213 mCurrentTouch.pointerCount);
4214#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004215 *outCancelPreviousGesture = true;
4216 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004217 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004218 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004219
Jeff Brown538881e2011-05-25 18:23:38 -07004220 // Clear the reference deltas for fingers not yet included in the reference calculation.
4221 for (BitSet32 idBits(mCurrentTouch.idBits.value & ~mPointerGesture.referenceIdBits.value);
4222 !idBits.isEmpty(); ) {
4223 uint32_t id = idBits.firstMarkedBit();
4224 idBits.clearBit(id);
4225
4226 mPointerGesture.referenceDeltas[id].dx = 0;
4227 mPointerGesture.referenceDeltas[id].dy = 0;
4228 }
4229 mPointerGesture.referenceIdBits = mCurrentTouch.idBits;
4230
Jeff Brown2352b972011-04-12 22:39:53 -07004231 // Move the reference points based on the overall group motion of the fingers.
4232 // The objective is to calculate a vector delta that is common to the movement
4233 // of all fingers.
4234 BitSet32 commonIdBits(mLastTouch.idBits.value & mCurrentTouch.idBits.value);
4235 if (!commonIdBits.isEmpty()) {
4236 float commonDeltaX = 0, commonDeltaY = 0;
4237 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4238 bool first = (idBits == commonIdBits);
4239 uint32_t id = idBits.firstMarkedBit();
4240 idBits.clearBit(id);
4241
4242 const PointerData& cpd = mCurrentTouch.pointers[mCurrentTouch.idToIndex[id]];
4243 const PointerData& lpd = mLastTouch.pointers[mLastTouch.idToIndex[id]];
Jeff Brown538881e2011-05-25 18:23:38 -07004244 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4245 delta.dx += cpd.x - lpd.x;
4246 delta.dy += cpd.y - lpd.y;
Jeff Brown2352b972011-04-12 22:39:53 -07004247
4248 if (first) {
Jeff Brown538881e2011-05-25 18:23:38 -07004249 commonDeltaX = delta.dx;
4250 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004251 } else {
Jeff Brown538881e2011-05-25 18:23:38 -07004252 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4253 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
Jeff Brown2352b972011-04-12 22:39:53 -07004254 }
4255 }
4256
Jeff Brown538881e2011-05-25 18:23:38 -07004257 if (commonDeltaX || commonDeltaY) {
4258 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4259 uint32_t id = idBits.firstMarkedBit();
4260 idBits.clearBit(id);
4261
4262 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4263 delta.dx = 0;
4264 delta.dy = 0;
4265 }
4266
4267 mPointerGesture.referenceTouchX += commonDeltaX;
4268 mPointerGesture.referenceTouchY += commonDeltaY;
4269 mPointerGesture.referenceGestureX +=
4270 commonDeltaX * mLocked.pointerGestureXMovementScale;
4271 mPointerGesture.referenceGestureY +=
4272 commonDeltaY * mLocked.pointerGestureYMovementScale;
4273 clampPositionUsingPointerBounds(mPointerController,
4274 &mPointerGesture.referenceGestureX,
4275 &mPointerGesture.referenceGestureY);
4276 }
Jeff Brown2352b972011-04-12 22:39:53 -07004277 }
4278
4279 // Report gestures.
4280 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4281 // PRESS mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004282#if DEBUG_GESTURES
Jeff Brown2352b972011-04-12 22:39:53 -07004283 LOGD("Gestures: PRESS activeTouchId=%d,"
Jeff Brownace13b12011-03-09 17:39:48 -08004284 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown2352b972011-04-12 22:39:53 -07004285 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004286#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004287 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004288
Jeff Brownace13b12011-03-09 17:39:48 -08004289 mPointerGesture.currentGestureIdBits.clear();
4290 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4291 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004292 mPointerGesture.currentGestureProperties[0].clear();
4293 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4294 mPointerGesture.currentGestureProperties[0].toolType =
4295 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004296 mPointerGesture.currentGestureCoords[0].clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004297 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4298 mPointerGesture.referenceGestureX);
4299 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4300 mPointerGesture.referenceGestureY);
Jeff Brownace13b12011-03-09 17:39:48 -08004301 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004302
Jeff Brown2352b972011-04-12 22:39:53 -07004303 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4304 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_PRESS;
4305 }
4306 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4307 // SWIPE mode.
4308#if DEBUG_GESTURES
4309 LOGD("Gestures: SWIPE activeTouchId=%d,"
4310 "activeGestureId=%d, currentTouchPointerCount=%d",
4311 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
4312#endif
4313 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
4314
4315 mPointerGesture.currentGestureIdBits.clear();
4316 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4317 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004318 mPointerGesture.currentGestureProperties[0].clear();
4319 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4320 mPointerGesture.currentGestureProperties[0].toolType =
4321 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004322 mPointerGesture.currentGestureCoords[0].clear();
4323 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4324 mPointerGesture.referenceGestureX);
4325 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4326 mPointerGesture.referenceGestureY);
4327 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4328
Jeff Brown2352b972011-04-12 22:39:53 -07004329 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4330 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_SWIPE;
4331 }
Jeff Brownace13b12011-03-09 17:39:48 -08004332 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4333 // FREEFORM mode.
4334#if DEBUG_GESTURES
4335 LOGD("Gestures: FREEFORM activeTouchId=%d,"
4336 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown2352b972011-04-12 22:39:53 -07004337 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004338#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004339 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004340
Jeff Brownace13b12011-03-09 17:39:48 -08004341 mPointerGesture.currentGestureIdBits.clear();
4342
4343 BitSet32 mappedTouchIdBits;
4344 BitSet32 usedGestureIdBits;
4345 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4346 // Initially, assign the active gesture id to the active touch point
4347 // if there is one. No other touch id bits are mapped yet.
4348 if (!*outCancelPreviousGesture) {
4349 mappedTouchIdBits.markBit(activeTouchId);
4350 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4351 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4352 mPointerGesture.activeGestureId;
4353 } else {
4354 mPointerGesture.activeGestureId = -1;
4355 }
4356 } else {
4357 // Otherwise, assume we mapped all touches from the previous frame.
4358 // Reuse all mappings that are still applicable.
4359 mappedTouchIdBits.value = mLastTouch.idBits.value & mCurrentTouch.idBits.value;
4360 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4361
4362 // Check whether we need to choose a new active gesture id because the
4363 // current went went up.
4364 for (BitSet32 upTouchIdBits(mLastTouch.idBits.value & ~mCurrentTouch.idBits.value);
4365 !upTouchIdBits.isEmpty(); ) {
4366 uint32_t upTouchId = upTouchIdBits.firstMarkedBit();
4367 upTouchIdBits.clearBit(upTouchId);
4368 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4369 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4370 mPointerGesture.activeGestureId = -1;
4371 break;
4372 }
4373 }
4374 }
4375
4376#if DEBUG_GESTURES
4377 LOGD("Gestures: FREEFORM follow up "
4378 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4379 "activeGestureId=%d",
4380 mappedTouchIdBits.value, usedGestureIdBits.value,
4381 mPointerGesture.activeGestureId);
4382#endif
4383
Jeff Brown2352b972011-04-12 22:39:53 -07004384 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
Jeff Brownace13b12011-03-09 17:39:48 -08004385 uint32_t touchId = mCurrentTouch.pointers[i].id;
4386 uint32_t gestureId;
4387 if (!mappedTouchIdBits.hasBit(touchId)) {
4388 gestureId = usedGestureIdBits.firstUnmarkedBit();
4389 usedGestureIdBits.markBit(gestureId);
4390 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4391#if DEBUG_GESTURES
4392 LOGD("Gestures: FREEFORM "
4393 "new mapping for touch id %d -> gesture id %d",
4394 touchId, gestureId);
4395#endif
4396 } else {
4397 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4398#if DEBUG_GESTURES
4399 LOGD("Gestures: FREEFORM "
4400 "existing mapping for touch id %d -> gesture id %d",
4401 touchId, gestureId);
4402#endif
4403 }
4404 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4405 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4406
Jeff Brown2352b972011-04-12 22:39:53 -07004407 float x = (mCurrentTouch.pointers[i].x - mPointerGesture.referenceTouchX)
4408 * mLocked.pointerGestureXZoomScale + mPointerGesture.referenceGestureX;
4409 float y = (mCurrentTouch.pointers[i].y - mPointerGesture.referenceTouchY)
4410 * mLocked.pointerGestureYZoomScale + mPointerGesture.referenceGestureY;
Jeff Brownace13b12011-03-09 17:39:48 -08004411
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004412 mPointerGesture.currentGestureProperties[i].clear();
4413 mPointerGesture.currentGestureProperties[i].id = gestureId;
4414 mPointerGesture.currentGestureProperties[i].toolType =
4415 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004416 mPointerGesture.currentGestureCoords[i].clear();
4417 mPointerGesture.currentGestureCoords[i].setAxisValue(
4418 AMOTION_EVENT_AXIS_X, x);
4419 mPointerGesture.currentGestureCoords[i].setAxisValue(
4420 AMOTION_EVENT_AXIS_Y, y);
4421 mPointerGesture.currentGestureCoords[i].setAxisValue(
4422 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4423 }
4424
4425 if (mPointerGesture.activeGestureId < 0) {
4426 mPointerGesture.activeGestureId =
4427 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4428#if DEBUG_GESTURES
4429 LOGD("Gestures: FREEFORM new "
4430 "activeGestureId=%d", mPointerGesture.activeGestureId);
4431#endif
4432 }
Jeff Brownace13b12011-03-09 17:39:48 -08004433
Jeff Brown2352b972011-04-12 22:39:53 -07004434 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4435 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_FREEFORM;
4436 }
4437 }
4438
4439 // Update spot locations for PRESS, SWIPE and FREEFORM.
4440 // We use the same calculation as we do to calculate the gesture pointers
4441 // for FREEFORM so that the spots smoothly track gestures.
4442 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4443 mPointerGesture.spotIdBits.clear();
4444 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
4445 uint32_t id = mCurrentTouch.pointers[i].id;
4446 mPointerGesture.spotIdBits.markBit(id);
4447 mPointerGesture.spotIdToIndex[id] = i;
4448
4449 float x = (mCurrentTouch.pointers[i].x - mPointerGesture.referenceTouchX)
4450 * mLocked.pointerGestureXZoomScale + mPointerGesture.referenceGestureX;
4451 float y = (mCurrentTouch.pointers[i].y - mPointerGesture.referenceTouchY)
4452 * mLocked.pointerGestureYZoomScale + mPointerGesture.referenceGestureY;
4453
4454 mPointerGesture.spotCoords[i].clear();
4455 mPointerGesture.spotCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4456 mPointerGesture.spotCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4457 mPointerGesture.spotCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4458 }
4459 moveSpotsLocked();
4460 }
Jeff Brownace13b12011-03-09 17:39:48 -08004461 }
4462
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004463 mPointerController->setButtonState(mCurrentTouch.buttonState);
4464
Jeff Brownace13b12011-03-09 17:39:48 -08004465#if DEBUG_GESTURES
4466 LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004467 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4468 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004469 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004470 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4471 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004472 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
4473 uint32_t id = idBits.firstMarkedBit();
4474 idBits.clearBit(id);
4475 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004476 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004477 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004478 LOGD(" currentGesture[%d]: index=%d, toolType=%d, "
4479 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4480 id, index, properties.toolType,
4481 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004482 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4483 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4484 }
4485 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
4486 uint32_t id = idBits.firstMarkedBit();
4487 idBits.clearBit(id);
4488 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004489 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004490 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004491 LOGD(" lastGesture[%d]: index=%d, toolType=%d, "
4492 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4493 id, index, properties.toolType,
4494 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004495 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4496 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4497 }
4498#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004499 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004500}
4501
Jeff Brown2352b972011-04-12 22:39:53 -07004502void TouchInputMapper::moveSpotsLocked() {
4503 mPointerController->setSpots(mPointerGesture.spotGesture,
4504 mPointerGesture.spotCoords, mPointerGesture.spotIdToIndex, mPointerGesture.spotIdBits);
4505}
4506
Jeff Brownace13b12011-03-09 17:39:48 -08004507void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004508 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
4509 const PointerProperties* properties, const PointerCoords* coords,
4510 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08004511 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
4512 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004513 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08004514 uint32_t pointerCount = 0;
4515 while (!idBits.isEmpty()) {
4516 uint32_t id = idBits.firstMarkedBit();
4517 idBits.clearBit(id);
4518 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004519 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004520 pointerCoords[pointerCount].copyFrom(coords[index]);
4521
4522 if (changedId >= 0 && id == uint32_t(changedId)) {
4523 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
4524 }
4525
4526 pointerCount += 1;
4527 }
4528
Jeff Brownb6110c22011-04-01 16:15:13 -07004529 LOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004530
4531 if (changedId >= 0 && pointerCount == 1) {
4532 // Replace initial down and final up action.
4533 // We can compare the action without masking off the changed pointer index
4534 // because we know the index is 0.
4535 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
4536 action = AMOTION_EVENT_ACTION_DOWN;
4537 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
4538 action = AMOTION_EVENT_ACTION_UP;
4539 } else {
4540 // Can't happen.
Jeff Brownb6110c22011-04-01 16:15:13 -07004541 LOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08004542 }
4543 }
4544
4545 getDispatcher()->notifyMotion(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004546 action, flags, metaState, buttonState, edgeFlags,
4547 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownace13b12011-03-09 17:39:48 -08004548}
4549
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004550bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004551 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004552 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
4553 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08004554 bool changed = false;
4555 while (!idBits.isEmpty()) {
4556 uint32_t id = idBits.firstMarkedBit();
4557 idBits.clearBit(id);
4558
4559 uint32_t inIndex = inIdToIndex[id];
4560 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004561
4562 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004563 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004564 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004565 PointerCoords& curOutCoords = outCoords[outIndex];
4566
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004567 if (curInProperties != curOutProperties) {
4568 curOutProperties.copyFrom(curInProperties);
4569 changed = true;
4570 }
4571
Jeff Brownace13b12011-03-09 17:39:48 -08004572 if (curInCoords != curOutCoords) {
4573 curOutCoords.copyFrom(curInCoords);
4574 changed = true;
4575 }
4576 }
4577 return changed;
4578}
4579
4580void TouchInputMapper::fadePointer() {
4581 { // acquire lock
4582 AutoMutex _l(mLock);
4583 if (mPointerController != NULL) {
Jeff Brown538881e2011-05-25 18:23:38 -07004584 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
Jeff Brownace13b12011-03-09 17:39:48 -08004585 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004586 } // release lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004587}
4588
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004589int32_t TouchInputMapper::getTouchToolType(bool isStylus) const {
4590 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
4591 return isStylus ? AMOTION_EVENT_TOOL_TYPE_STYLUS : AMOTION_EVENT_TOOL_TYPE_FINGER;
4592 } else {
4593 return isStylus ? AMOTION_EVENT_TOOL_TYPE_INDIRECT_STYLUS
4594 : AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
4595 }
4596}
4597
Jeff Brown6328cdc2010-07-29 18:18:33 -07004598bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) {
Jeff Brown9626b142011-03-03 02:09:54 -08004599 return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue
4600 && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004601}
4602
Jeff Brown6328cdc2010-07-29 18:18:33 -07004603const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked(
4604 int32_t x, int32_t y) {
4605 size_t numVirtualKeys = mLocked.virtualKeys.size();
4606 for (size_t i = 0; i < numVirtualKeys; i++) {
4607 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004608
4609#if DEBUG_VIRTUAL_KEYS
4610 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
4611 "left=%d, top=%d, right=%d, bottom=%d",
4612 x, y,
4613 virtualKey.keyCode, virtualKey.scanCode,
4614 virtualKey.hitLeft, virtualKey.hitTop,
4615 virtualKey.hitRight, virtualKey.hitBottom);
4616#endif
4617
4618 if (virtualKey.isHit(x, y)) {
4619 return & virtualKey;
4620 }
4621 }
4622
4623 return NULL;
4624}
4625
4626void TouchInputMapper::calculatePointerIds() {
4627 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
4628 uint32_t lastPointerCount = mLastTouch.pointerCount;
4629
4630 if (currentPointerCount == 0) {
4631 // No pointers to assign.
4632 mCurrentTouch.idBits.clear();
4633 } else if (lastPointerCount == 0) {
4634 // All pointers are new.
4635 mCurrentTouch.idBits.clear();
4636 for (uint32_t i = 0; i < currentPointerCount; i++) {
4637 mCurrentTouch.pointers[i].id = i;
4638 mCurrentTouch.idToIndex[i] = i;
4639 mCurrentTouch.idBits.markBit(i);
4640 }
4641 } else if (currentPointerCount == 1 && lastPointerCount == 1) {
4642 // Only one pointer and no change in count so it must have the same id as before.
4643 uint32_t id = mLastTouch.pointers[0].id;
4644 mCurrentTouch.pointers[0].id = id;
4645 mCurrentTouch.idToIndex[id] = 0;
4646 mCurrentTouch.idBits.value = BitSet32::valueForBit(id);
4647 } else {
4648 // General case.
4649 // We build a heap of squared euclidean distances between current and last pointers
4650 // associated with the current and last pointer indices. Then, we find the best
4651 // match (by distance) for each current pointer.
4652 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
4653
4654 uint32_t heapSize = 0;
4655 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
4656 currentPointerIndex++) {
4657 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
4658 lastPointerIndex++) {
4659 int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x
4660 - mLastTouch.pointers[lastPointerIndex].x;
4661 int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y
4662 - mLastTouch.pointers[lastPointerIndex].y;
4663
4664 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
4665
4666 // Insert new element into the heap (sift up).
4667 heap[heapSize].currentPointerIndex = currentPointerIndex;
4668 heap[heapSize].lastPointerIndex = lastPointerIndex;
4669 heap[heapSize].distance = distance;
4670 heapSize += 1;
4671 }
4672 }
4673
4674 // Heapify
4675 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
4676 startIndex -= 1;
4677 for (uint32_t parentIndex = startIndex; ;) {
4678 uint32_t childIndex = parentIndex * 2 + 1;
4679 if (childIndex >= heapSize) {
4680 break;
4681 }
4682
4683 if (childIndex + 1 < heapSize
4684 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4685 childIndex += 1;
4686 }
4687
4688 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4689 break;
4690 }
4691
4692 swap(heap[parentIndex], heap[childIndex]);
4693 parentIndex = childIndex;
4694 }
4695 }
4696
4697#if DEBUG_POINTER_ASSIGNMENT
4698 LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize);
4699 for (size_t i = 0; i < heapSize; i++) {
4700 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4701 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4702 heap[i].distance);
4703 }
4704#endif
4705
4706 // Pull matches out by increasing order of distance.
4707 // To avoid reassigning pointers that have already been matched, the loop keeps track
4708 // of which last and current pointers have been matched using the matchedXXXBits variables.
4709 // It also tracks the used pointer id bits.
4710 BitSet32 matchedLastBits(0);
4711 BitSet32 matchedCurrentBits(0);
4712 BitSet32 usedIdBits(0);
4713 bool first = true;
4714 for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) {
4715 for (;;) {
4716 if (first) {
4717 // The first time through the loop, we just consume the root element of
4718 // the heap (the one with smallest distance).
4719 first = false;
4720 } else {
4721 // Previous iterations consumed the root element of the heap.
4722 // Pop root element off of the heap (sift down).
4723 heapSize -= 1;
Jeff Brownb6110c22011-04-01 16:15:13 -07004724 LOG_ASSERT(heapSize > 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004725
4726 // Sift down.
4727 heap[0] = heap[heapSize];
4728 for (uint32_t parentIndex = 0; ;) {
4729 uint32_t childIndex = parentIndex * 2 + 1;
4730 if (childIndex >= heapSize) {
4731 break;
4732 }
4733
4734 if (childIndex + 1 < heapSize
4735 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4736 childIndex += 1;
4737 }
4738
4739 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4740 break;
4741 }
4742
4743 swap(heap[parentIndex], heap[childIndex]);
4744 parentIndex = childIndex;
4745 }
4746
4747#if DEBUG_POINTER_ASSIGNMENT
4748 LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize);
4749 for (size_t i = 0; i < heapSize; i++) {
4750 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4751 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4752 heap[i].distance);
4753 }
4754#endif
4755 }
4756
4757 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
4758 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
4759
4760 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
4761 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
4762
4763 matchedCurrentBits.markBit(currentPointerIndex);
4764 matchedLastBits.markBit(lastPointerIndex);
4765
4766 uint32_t id = mLastTouch.pointers[lastPointerIndex].id;
4767 mCurrentTouch.pointers[currentPointerIndex].id = id;
4768 mCurrentTouch.idToIndex[id] = currentPointerIndex;
4769 usedIdBits.markBit(id);
4770
4771#if DEBUG_POINTER_ASSIGNMENT
4772 LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
4773 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
4774#endif
4775 break;
4776 }
4777 }
4778
4779 // Assign fresh ids to new pointers.
4780 if (currentPointerCount > lastPointerCount) {
4781 for (uint32_t i = currentPointerCount - lastPointerCount; ;) {
4782 uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit();
4783 uint32_t id = usedIdBits.firstUnmarkedBit();
4784
4785 mCurrentTouch.pointers[currentPointerIndex].id = id;
4786 mCurrentTouch.idToIndex[id] = currentPointerIndex;
4787 usedIdBits.markBit(id);
4788
4789#if DEBUG_POINTER_ASSIGNMENT
4790 LOGD("calculatePointerIds - assigned: cur=%d, id=%d",
4791 currentPointerIndex, id);
4792#endif
4793
4794 if (--i == 0) break; // done
4795 matchedCurrentBits.markBit(currentPointerIndex);
4796 }
4797 }
4798
4799 // Fix id bits.
4800 mCurrentTouch.idBits = usedIdBits;
4801 }
4802}
4803
4804/* Special hack for devices that have bad screen data: if one of the
4805 * points has moved more than a screen height from the last position,
4806 * then drop it. */
4807bool TouchInputMapper::applyBadTouchFilter() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004808 uint32_t pointerCount = mCurrentTouch.pointerCount;
4809
4810 // Nothing to do if there are no points.
4811 if (pointerCount == 0) {
4812 return false;
4813 }
4814
4815 // Don't do anything if a finger is going down or up. We run
4816 // here before assigning pointer IDs, so there isn't a good
4817 // way to do per-finger matching.
4818 if (pointerCount != mLastTouch.pointerCount) {
4819 return false;
4820 }
4821
4822 // We consider a single movement across more than a 7/16 of
4823 // the long size of the screen to be bad. This was a magic value
4824 // determined by looking at the maximum distance it is feasible
4825 // to actually move in one sample.
Jeff Brown9626b142011-03-03 02:09:54 -08004826 int32_t maxDeltaY = (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1) * 7 / 16;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004827
4828 // XXX The original code in InputDevice.java included commented out
4829 // code for testing the X axis. Note that when we drop a point
4830 // we don't actually restore the old X either. Strange.
4831 // The old code also tries to track when bad points were previously
4832 // detected but it turns out that due to the placement of a "break"
4833 // at the end of the loop, we never set mDroppedBadPoint to true
4834 // so it is effectively dead code.
4835 // Need to figure out if the old code is busted or just overcomplicated
4836 // but working as intended.
4837
4838 // Look through all new points and see if any are farther than
4839 // acceptable from all previous points.
4840 for (uint32_t i = pointerCount; i-- > 0; ) {
4841 int32_t y = mCurrentTouch.pointers[i].y;
4842 int32_t closestY = INT_MAX;
4843 int32_t closestDeltaY = 0;
4844
4845#if DEBUG_HACKS
4846 LOGD("BadTouchFilter: Looking at next point #%d: y=%d", i, y);
4847#endif
4848
4849 for (uint32_t j = pointerCount; j-- > 0; ) {
4850 int32_t lastY = mLastTouch.pointers[j].y;
4851 int32_t deltaY = abs(y - lastY);
4852
4853#if DEBUG_HACKS
4854 LOGD("BadTouchFilter: Comparing with last point #%d: y=%d deltaY=%d",
4855 j, lastY, deltaY);
4856#endif
4857
4858 if (deltaY < maxDeltaY) {
4859 goto SkipSufficientlyClosePoint;
4860 }
4861 if (deltaY < closestDeltaY) {
4862 closestDeltaY = deltaY;
4863 closestY = lastY;
4864 }
4865 }
4866
4867 // Must not have found a close enough match.
4868#if DEBUG_HACKS
4869 LOGD("BadTouchFilter: Dropping bad point #%d: newY=%d oldY=%d deltaY=%d maxDeltaY=%d",
4870 i, y, closestY, closestDeltaY, maxDeltaY);
4871#endif
4872
4873 mCurrentTouch.pointers[i].y = closestY;
4874 return true; // XXX original code only corrects one point
4875
4876 SkipSufficientlyClosePoint: ;
4877 }
4878
4879 // No change.
4880 return false;
4881}
4882
4883/* Special hack for devices that have bad screen data: drop points where
4884 * the coordinate value for one axis has jumped to the other pointer's location.
4885 */
4886bool TouchInputMapper::applyJumpyTouchFilter() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004887 uint32_t pointerCount = mCurrentTouch.pointerCount;
4888 if (mLastTouch.pointerCount != pointerCount) {
4889#if DEBUG_HACKS
4890 LOGD("JumpyTouchFilter: Different pointer count %d -> %d",
4891 mLastTouch.pointerCount, pointerCount);
4892 for (uint32_t i = 0; i < pointerCount; i++) {
4893 LOGD(" Pointer %d (%d, %d)", i,
4894 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
4895 }
4896#endif
4897
4898 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_TRANSITION_DROPS) {
4899 if (mLastTouch.pointerCount == 1 && pointerCount == 2) {
4900 // Just drop the first few events going from 1 to 2 pointers.
4901 // They're bad often enough that they're not worth considering.
4902 mCurrentTouch.pointerCount = 1;
4903 mJumpyTouchFilter.jumpyPointsDropped += 1;
4904
4905#if DEBUG_HACKS
4906 LOGD("JumpyTouchFilter: Pointer 2 dropped");
4907#endif
4908 return true;
4909 } else if (mLastTouch.pointerCount == 2 && pointerCount == 1) {
4910 // The event when we go from 2 -> 1 tends to be messed up too
4911 mCurrentTouch.pointerCount = 2;
4912 mCurrentTouch.pointers[0] = mLastTouch.pointers[0];
4913 mCurrentTouch.pointers[1] = mLastTouch.pointers[1];
4914 mJumpyTouchFilter.jumpyPointsDropped += 1;
4915
4916#if DEBUG_HACKS
4917 for (int32_t i = 0; i < 2; i++) {
4918 LOGD("JumpyTouchFilter: Pointer %d replaced (%d, %d)", i,
4919 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
4920 }
4921#endif
4922 return true;
4923 }
4924 }
4925 // Reset jumpy points dropped on other transitions or if limit exceeded.
4926 mJumpyTouchFilter.jumpyPointsDropped = 0;
4927
4928#if DEBUG_HACKS
4929 LOGD("JumpyTouchFilter: Transition - drop limit reset");
4930#endif
4931 return false;
4932 }
4933
4934 // We have the same number of pointers as last time.
4935 // A 'jumpy' point is one where the coordinate value for one axis
4936 // has jumped to the other pointer's location. No need to do anything
4937 // else if we only have one pointer.
4938 if (pointerCount < 2) {
4939 return false;
4940 }
4941
4942 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_DROP_LIMIT) {
Jeff Brown9626b142011-03-03 02:09:54 -08004943 int jumpyEpsilon = (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1) / JUMPY_EPSILON_DIVISOR;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004944
4945 // We only replace the single worst jumpy point as characterized by pointer distance
4946 // in a single axis.
4947 int32_t badPointerIndex = -1;
4948 int32_t badPointerReplacementIndex = -1;
4949 int32_t badPointerDistance = INT_MIN; // distance to be corrected
4950
4951 for (uint32_t i = pointerCount; i-- > 0; ) {
4952 int32_t x = mCurrentTouch.pointers[i].x;
4953 int32_t y = mCurrentTouch.pointers[i].y;
4954
4955#if DEBUG_HACKS
4956 LOGD("JumpyTouchFilter: Point %d (%d, %d)", i, x, y);
4957#endif
4958
4959 // Check if a touch point is too close to another's coordinates
4960 bool dropX = false, dropY = false;
4961 for (uint32_t j = 0; j < pointerCount; j++) {
4962 if (i == j) {
4963 continue;
4964 }
4965
4966 if (abs(x - mCurrentTouch.pointers[j].x) <= jumpyEpsilon) {
4967 dropX = true;
4968 break;
4969 }
4970
4971 if (abs(y - mCurrentTouch.pointers[j].y) <= jumpyEpsilon) {
4972 dropY = true;
4973 break;
4974 }
4975 }
4976 if (! dropX && ! dropY) {
4977 continue; // not jumpy
4978 }
4979
4980 // Find a replacement candidate by comparing with older points on the
4981 // complementary (non-jumpy) axis.
4982 int32_t distance = INT_MIN; // distance to be corrected
4983 int32_t replacementIndex = -1;
4984
4985 if (dropX) {
4986 // X looks too close. Find an older replacement point with a close Y.
4987 int32_t smallestDeltaY = INT_MAX;
4988 for (uint32_t j = 0; j < pointerCount; j++) {
4989 int32_t deltaY = abs(y - mLastTouch.pointers[j].y);
4990 if (deltaY < smallestDeltaY) {
4991 smallestDeltaY = deltaY;
4992 replacementIndex = j;
4993 }
4994 }
4995 distance = abs(x - mLastTouch.pointers[replacementIndex].x);
4996 } else {
4997 // Y looks too close. Find an older replacement point with a close X.
4998 int32_t smallestDeltaX = INT_MAX;
4999 for (uint32_t j = 0; j < pointerCount; j++) {
5000 int32_t deltaX = abs(x - mLastTouch.pointers[j].x);
5001 if (deltaX < smallestDeltaX) {
5002 smallestDeltaX = deltaX;
5003 replacementIndex = j;
5004 }
5005 }
5006 distance = abs(y - mLastTouch.pointers[replacementIndex].y);
5007 }
5008
5009 // If replacing this pointer would correct a worse error than the previous ones
5010 // considered, then use this replacement instead.
5011 if (distance > badPointerDistance) {
5012 badPointerIndex = i;
5013 badPointerReplacementIndex = replacementIndex;
5014 badPointerDistance = distance;
5015 }
5016 }
5017
5018 // Correct the jumpy pointer if one was found.
5019 if (badPointerIndex >= 0) {
5020#if DEBUG_HACKS
5021 LOGD("JumpyTouchFilter: Replacing bad pointer %d with (%d, %d)",
5022 badPointerIndex,
5023 mLastTouch.pointers[badPointerReplacementIndex].x,
5024 mLastTouch.pointers[badPointerReplacementIndex].y);
5025#endif
5026
5027 mCurrentTouch.pointers[badPointerIndex].x =
5028 mLastTouch.pointers[badPointerReplacementIndex].x;
5029 mCurrentTouch.pointers[badPointerIndex].y =
5030 mLastTouch.pointers[badPointerReplacementIndex].y;
5031 mJumpyTouchFilter.jumpyPointsDropped += 1;
5032 return true;
5033 }
5034 }
5035
5036 mJumpyTouchFilter.jumpyPointsDropped = 0;
5037 return false;
5038}
5039
5040/* Special hack for devices that have bad screen data: aggregate and
5041 * compute averages of the coordinate data, to reduce the amount of
5042 * jitter seen by applications. */
5043void TouchInputMapper::applyAveragingTouchFilter() {
5044 for (uint32_t currentIndex = 0; currentIndex < mCurrentTouch.pointerCount; currentIndex++) {
5045 uint32_t id = mCurrentTouch.pointers[currentIndex].id;
5046 int32_t x = mCurrentTouch.pointers[currentIndex].x;
5047 int32_t y = mCurrentTouch.pointers[currentIndex].y;
Jeff Brown8d608662010-08-30 03:02:23 -07005048 int32_t pressure;
5049 switch (mCalibration.pressureSource) {
5050 case Calibration::PRESSURE_SOURCE_PRESSURE:
5051 pressure = mCurrentTouch.pointers[currentIndex].pressure;
5052 break;
5053 case Calibration::PRESSURE_SOURCE_TOUCH:
5054 pressure = mCurrentTouch.pointers[currentIndex].touchMajor;
5055 break;
5056 default:
5057 pressure = 1;
5058 break;
5059 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005060
5061 if (mLastTouch.idBits.hasBit(id)) {
5062 // Pointer was down before and is still down now.
5063 // Compute average over history trace.
5064 uint32_t start = mAveragingTouchFilter.historyStart[id];
5065 uint32_t end = mAveragingTouchFilter.historyEnd[id];
5066
5067 int64_t deltaX = x - mAveragingTouchFilter.historyData[end].pointers[id].x;
5068 int64_t deltaY = y - mAveragingTouchFilter.historyData[end].pointers[id].y;
5069 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5070
5071#if DEBUG_HACKS
5072 LOGD("AveragingTouchFilter: Pointer id %d - Distance from last sample: %lld",
5073 id, distance);
5074#endif
5075
5076 if (distance < AVERAGING_DISTANCE_LIMIT) {
5077 // Increment end index in preparation for recording new historical data.
5078 end += 1;
5079 if (end > AVERAGING_HISTORY_SIZE) {
5080 end = 0;
5081 }
5082
5083 // If the end index has looped back to the start index then we have filled
5084 // the historical trace up to the desired size so we drop the historical
5085 // data at the start of the trace.
5086 if (end == start) {
5087 start += 1;
5088 if (start > AVERAGING_HISTORY_SIZE) {
5089 start = 0;
5090 }
5091 }
5092
5093 // Add the raw data to the historical trace.
5094 mAveragingTouchFilter.historyStart[id] = start;
5095 mAveragingTouchFilter.historyEnd[id] = end;
5096 mAveragingTouchFilter.historyData[end].pointers[id].x = x;
5097 mAveragingTouchFilter.historyData[end].pointers[id].y = y;
5098 mAveragingTouchFilter.historyData[end].pointers[id].pressure = pressure;
5099
5100 // Average over all historical positions in the trace by total pressure.
5101 int32_t averagedX = 0;
5102 int32_t averagedY = 0;
5103 int32_t totalPressure = 0;
5104 for (;;) {
5105 int32_t historicalX = mAveragingTouchFilter.historyData[start].pointers[id].x;
5106 int32_t historicalY = mAveragingTouchFilter.historyData[start].pointers[id].y;
5107 int32_t historicalPressure = mAveragingTouchFilter.historyData[start]
5108 .pointers[id].pressure;
5109
5110 averagedX += historicalX * historicalPressure;
5111 averagedY += historicalY * historicalPressure;
5112 totalPressure += historicalPressure;
5113
5114 if (start == end) {
5115 break;
5116 }
5117
5118 start += 1;
5119 if (start > AVERAGING_HISTORY_SIZE) {
5120 start = 0;
5121 }
5122 }
5123
Jeff Brown8d608662010-08-30 03:02:23 -07005124 if (totalPressure != 0) {
5125 averagedX /= totalPressure;
5126 averagedY /= totalPressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005127
5128#if DEBUG_HACKS
Jeff Brown8d608662010-08-30 03:02:23 -07005129 LOGD("AveragingTouchFilter: Pointer id %d - "
5130 "totalPressure=%d, averagedX=%d, averagedY=%d", id, totalPressure,
5131 averagedX, averagedY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005132#endif
5133
Jeff Brown8d608662010-08-30 03:02:23 -07005134 mCurrentTouch.pointers[currentIndex].x = averagedX;
5135 mCurrentTouch.pointers[currentIndex].y = averagedY;
5136 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005137 } else {
5138#if DEBUG_HACKS
5139 LOGD("AveragingTouchFilter: Pointer id %d - Exceeded max distance", id);
5140#endif
5141 }
5142 } else {
5143#if DEBUG_HACKS
5144 LOGD("AveragingTouchFilter: Pointer id %d - Pointer went up", id);
5145#endif
5146 }
5147
5148 // Reset pointer history.
5149 mAveragingTouchFilter.historyStart[id] = 0;
5150 mAveragingTouchFilter.historyEnd[id] = 0;
5151 mAveragingTouchFilter.historyData[0].pointers[id].x = x;
5152 mAveragingTouchFilter.historyData[0].pointers[id].y = y;
5153 mAveragingTouchFilter.historyData[0].pointers[id].pressure = pressure;
5154 }
5155}
5156
5157int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07005158 { // acquire lock
5159 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005160
Jeff Brown6328cdc2010-07-29 18:18:33 -07005161 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005162 return AKEY_STATE_VIRTUAL;
5163 }
5164
Jeff Brown6328cdc2010-07-29 18:18:33 -07005165 size_t numVirtualKeys = mLocked.virtualKeys.size();
5166 for (size_t i = 0; i < numVirtualKeys; i++) {
5167 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005168 if (virtualKey.keyCode == keyCode) {
5169 return AKEY_STATE_UP;
5170 }
5171 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07005172 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07005173
5174 return AKEY_STATE_UNKNOWN;
5175}
5176
5177int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07005178 { // acquire lock
5179 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005180
Jeff Brown6328cdc2010-07-29 18:18:33 -07005181 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005182 return AKEY_STATE_VIRTUAL;
5183 }
5184
Jeff Brown6328cdc2010-07-29 18:18:33 -07005185 size_t numVirtualKeys = mLocked.virtualKeys.size();
5186 for (size_t i = 0; i < numVirtualKeys; i++) {
5187 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005188 if (virtualKey.scanCode == scanCode) {
5189 return AKEY_STATE_UP;
5190 }
5191 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07005192 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07005193
5194 return AKEY_STATE_UNKNOWN;
5195}
5196
5197bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5198 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07005199 { // acquire lock
5200 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005201
Jeff Brown6328cdc2010-07-29 18:18:33 -07005202 size_t numVirtualKeys = mLocked.virtualKeys.size();
5203 for (size_t i = 0; i < numVirtualKeys; i++) {
5204 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005205
5206 for (size_t i = 0; i < numCodes; i++) {
5207 if (virtualKey.keyCode == keyCodes[i]) {
5208 outFlags[i] = 1;
5209 }
5210 }
5211 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07005212 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07005213
5214 return true;
5215}
5216
5217
5218// --- SingleTouchInputMapper ---
5219
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005220SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5221 TouchInputMapper(device) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005222 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005223}
5224
5225SingleTouchInputMapper::~SingleTouchInputMapper() {
5226}
5227
Jeff Brown80fd47c2011-05-24 01:07:44 -07005228void SingleTouchInputMapper::clearState() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005229 mAccumulator.clear();
5230
5231 mDown = false;
5232 mX = 0;
5233 mY = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07005234 mPressure = 0; // default to 0 for devices that don't report pressure
5235 mToolWidth = 0; // default to 0 for devices that don't report tool width
Jeff Brownace13b12011-03-09 17:39:48 -08005236 mButtonState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005237}
5238
5239void SingleTouchInputMapper::reset() {
5240 TouchInputMapper::reset();
5241
Jeff Brown80fd47c2011-05-24 01:07:44 -07005242 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005243 }
5244
5245void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
5246 switch (rawEvent->type) {
5247 case EV_KEY:
5248 switch (rawEvent->scanCode) {
5249 case BTN_TOUCH:
5250 mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH;
5251 mAccumulator.btnTouch = rawEvent->value != 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005252 // Don't sync immediately. Wait until the next SYN_REPORT since we might
5253 // not have received valid position information yet. This logic assumes that
5254 // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet.
Jeff Brown6d0fec22010-07-23 21:28:06 -07005255 break;
Jeff Brownace13b12011-03-09 17:39:48 -08005256 default:
5257 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005258 int32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode);
Jeff Brownace13b12011-03-09 17:39:48 -08005259 if (buttonState) {
5260 if (rawEvent->value) {
5261 mAccumulator.buttonDown |= buttonState;
5262 } else {
5263 mAccumulator.buttonUp |= buttonState;
5264 }
5265 mAccumulator.fields |= Accumulator::FIELD_BUTTONS;
5266 }
5267 }
5268 break;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005269 }
5270 break;
5271
5272 case EV_ABS:
5273 switch (rawEvent->scanCode) {
5274 case ABS_X:
5275 mAccumulator.fields |= Accumulator::FIELD_ABS_X;
5276 mAccumulator.absX = rawEvent->value;
5277 break;
5278 case ABS_Y:
5279 mAccumulator.fields |= Accumulator::FIELD_ABS_Y;
5280 mAccumulator.absY = rawEvent->value;
5281 break;
5282 case ABS_PRESSURE:
5283 mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE;
5284 mAccumulator.absPressure = rawEvent->value;
5285 break;
5286 case ABS_TOOL_WIDTH:
5287 mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH;
5288 mAccumulator.absToolWidth = rawEvent->value;
5289 break;
5290 }
5291 break;
5292
5293 case EV_SYN:
5294 switch (rawEvent->scanCode) {
5295 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005296 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005297 break;
5298 }
5299 break;
5300 }
5301}
5302
5303void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005304 uint32_t fields = mAccumulator.fields;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005305 if (fields == 0) {
5306 return; // no new state changes, so nothing to do
5307 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005308
5309 if (fields & Accumulator::FIELD_BTN_TOUCH) {
5310 mDown = mAccumulator.btnTouch;
5311 }
5312
5313 if (fields & Accumulator::FIELD_ABS_X) {
5314 mX = mAccumulator.absX;
5315 }
5316
5317 if (fields & Accumulator::FIELD_ABS_Y) {
5318 mY = mAccumulator.absY;
5319 }
5320
5321 if (fields & Accumulator::FIELD_ABS_PRESSURE) {
5322 mPressure = mAccumulator.absPressure;
5323 }
5324
5325 if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) {
Jeff Brown8d608662010-08-30 03:02:23 -07005326 mToolWidth = mAccumulator.absToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005327 }
5328
Jeff Brownace13b12011-03-09 17:39:48 -08005329 if (fields & Accumulator::FIELD_BUTTONS) {
5330 mButtonState = (mButtonState | mAccumulator.buttonDown) & ~mAccumulator.buttonUp;
5331 }
5332
Jeff Brown6d0fec22010-07-23 21:28:06 -07005333 mCurrentTouch.clear();
5334
5335 if (mDown) {
5336 mCurrentTouch.pointerCount = 1;
5337 mCurrentTouch.pointers[0].id = 0;
5338 mCurrentTouch.pointers[0].x = mX;
5339 mCurrentTouch.pointers[0].y = mY;
5340 mCurrentTouch.pointers[0].pressure = mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -07005341 mCurrentTouch.pointers[0].touchMajor = 0;
5342 mCurrentTouch.pointers[0].touchMinor = 0;
5343 mCurrentTouch.pointers[0].toolMajor = mToolWidth;
5344 mCurrentTouch.pointers[0].toolMinor = mToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005345 mCurrentTouch.pointers[0].orientation = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005346 mCurrentTouch.pointers[0].distance = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005347 mCurrentTouch.pointers[0].isStylus = false; // TODO: Set stylus
Jeff Brown6d0fec22010-07-23 21:28:06 -07005348 mCurrentTouch.idToIndex[0] = 0;
5349 mCurrentTouch.idBits.markBit(0);
Jeff Brownace13b12011-03-09 17:39:48 -08005350 mCurrentTouch.buttonState = mButtonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005351 }
5352
5353 syncTouch(when, true);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005354
5355 mAccumulator.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005356}
5357
Jeff Brown8d608662010-08-30 03:02:23 -07005358void SingleTouchInputMapper::configureRawAxes() {
5359 TouchInputMapper::configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005360
Jeff Brown8d608662010-08-30 03:02:23 -07005361 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x);
5362 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y);
5363 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure);
5364 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005365}
5366
5367
5368// --- MultiTouchInputMapper ---
5369
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005370MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown80fd47c2011-05-24 01:07:44 -07005371 TouchInputMapper(device), mSlotCount(0), mUsingSlotsProtocol(false) {
5372 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005373}
5374
5375MultiTouchInputMapper::~MultiTouchInputMapper() {
5376}
5377
Jeff Brown80fd47c2011-05-24 01:07:44 -07005378void MultiTouchInputMapper::clearState() {
5379 mAccumulator.clear(mSlotCount);
Jeff Brownace13b12011-03-09 17:39:48 -08005380 mButtonState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005381}
5382
5383void MultiTouchInputMapper::reset() {
5384 TouchInputMapper::reset();
5385
Jeff Brown80fd47c2011-05-24 01:07:44 -07005386 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005387}
5388
5389void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
5390 switch (rawEvent->type) {
Jeff Brownace13b12011-03-09 17:39:48 -08005391 case EV_KEY: {
5392 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005393 int32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode);
Jeff Brownace13b12011-03-09 17:39:48 -08005394 if (buttonState) {
5395 if (rawEvent->value) {
5396 mAccumulator.buttonDown |= buttonState;
5397 } else {
5398 mAccumulator.buttonUp |= buttonState;
5399 }
5400 }
5401 }
5402 break;
5403 }
5404
Jeff Brown6d0fec22010-07-23 21:28:06 -07005405 case EV_ABS: {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005406 bool newSlot = false;
5407 if (mUsingSlotsProtocol && rawEvent->scanCode == ABS_MT_SLOT) {
5408 mAccumulator.currentSlot = rawEvent->value;
5409 newSlot = true;
5410 }
5411
5412 if (mAccumulator.currentSlot < 0 || size_t(mAccumulator.currentSlot) >= mSlotCount) {
5413 if (newSlot) {
5414#if DEBUG_POINTERS
5415 LOGW("MultiTouch device %s emitted invalid slot index %d but it "
5416 "should be between 0 and %d; ignoring this slot.",
5417 getDeviceName().string(), mAccumulator.currentSlot, mSlotCount);
5418#endif
5419 }
5420 break;
5421 }
5422
5423 Accumulator::Slot* slot = &mAccumulator.slots[mAccumulator.currentSlot];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005424
5425 switch (rawEvent->scanCode) {
5426 case ABS_MT_POSITION_X:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005427 slot->fields |= Accumulator::FIELD_ABS_MT_POSITION_X;
5428 slot->absMTPositionX = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005429 break;
5430 case ABS_MT_POSITION_Y:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005431 slot->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y;
5432 slot->absMTPositionY = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005433 break;
5434 case ABS_MT_TOUCH_MAJOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005435 slot->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR;
5436 slot->absMTTouchMajor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005437 break;
5438 case ABS_MT_TOUCH_MINOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005439 slot->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR;
5440 slot->absMTTouchMinor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005441 break;
5442 case ABS_MT_WIDTH_MAJOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005443 slot->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR;
5444 slot->absMTWidthMajor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005445 break;
5446 case ABS_MT_WIDTH_MINOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005447 slot->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR;
5448 slot->absMTWidthMinor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005449 break;
5450 case ABS_MT_ORIENTATION:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005451 slot->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION;
5452 slot->absMTOrientation = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005453 break;
5454 case ABS_MT_TRACKING_ID:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005455 if (mUsingSlotsProtocol && rawEvent->value < 0) {
5456 slot->clear();
5457 } else {
5458 slot->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID;
5459 slot->absMTTrackingId = rawEvent->value;
5460 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005461 break;
Jeff Brown8d608662010-08-30 03:02:23 -07005462 case ABS_MT_PRESSURE:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005463 slot->fields |= Accumulator::FIELD_ABS_MT_PRESSURE;
5464 slot->absMTPressure = rawEvent->value;
5465 break;
5466 case ABS_MT_TOOL_TYPE:
5467 slot->fields |= Accumulator::FIELD_ABS_MT_TOOL_TYPE;
5468 slot->absMTToolType = rawEvent->value;
Jeff Brown8d608662010-08-30 03:02:23 -07005469 break;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005470 }
5471 break;
5472 }
5473
5474 case EV_SYN:
5475 switch (rawEvent->scanCode) {
5476 case SYN_MT_REPORT: {
5477 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
Jeff Brown80fd47c2011-05-24 01:07:44 -07005478 mAccumulator.currentSlot += 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005479 break;
5480 }
5481
5482 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005483 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005484 break;
5485 }
5486 break;
5487 }
5488}
5489
5490void MultiTouchInputMapper::sync(nsecs_t when) {
5491 static const uint32_t REQUIRED_FIELDS =
Jeff Brown8d608662010-08-30 03:02:23 -07005492 Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005493
Jeff Brown80fd47c2011-05-24 01:07:44 -07005494 size_t inCount = mSlotCount;
5495 size_t outCount = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005496 bool havePointerIds = true;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005497
Jeff Brown6d0fec22010-07-23 21:28:06 -07005498 mCurrentTouch.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005499
Jeff Brown80fd47c2011-05-24 01:07:44 -07005500 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
5501 const Accumulator::Slot& inSlot = mAccumulator.slots[inIndex];
5502 uint32_t fields = inSlot.fields;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005503
Jeff Brown6d0fec22010-07-23 21:28:06 -07005504 if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005505 // Some drivers send empty MT sync packets without X / Y to indicate a pointer up.
Jeff Brown80fd47c2011-05-24 01:07:44 -07005506 // This may also indicate an unused slot.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005507 // Drop this finger.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005508 continue;
5509 }
5510
Jeff Brown80fd47c2011-05-24 01:07:44 -07005511 if (outCount >= MAX_POINTERS) {
5512#if DEBUG_POINTERS
5513 LOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
5514 "ignoring the rest.",
5515 getDeviceName().string(), MAX_POINTERS);
5516#endif
5517 break; // too many fingers!
5518 }
5519
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005520 PointerData& outPointer = mCurrentTouch.pointers[outCount];
Jeff Brown80fd47c2011-05-24 01:07:44 -07005521 outPointer.x = inSlot.absMTPositionX;
5522 outPointer.y = inSlot.absMTPositionY;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005523
Jeff Brown8d608662010-08-30 03:02:23 -07005524 if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005525 outPointer.pressure = inSlot.absMTPressure;
Jeff Brown8d608662010-08-30 03:02:23 -07005526 } else {
5527 // Default pressure to 0 if absent.
5528 outPointer.pressure = 0;
5529 }
5530
5531 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005532 if (inSlot.absMTTouchMajor <= 0) {
Jeff Brown8d608662010-08-30 03:02:23 -07005533 // Some devices send sync packets with X / Y but with a 0 touch major to indicate
5534 // a pointer going up. Drop this finger.
5535 continue;
5536 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07005537 outPointer.touchMajor = inSlot.absMTTouchMajor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005538 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005539 // Default touch area to 0 if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005540 outPointer.touchMajor = 0;
5541 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005542
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005543 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005544 outPointer.touchMinor = inSlot.absMTTouchMinor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005545 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005546 // Assume touch area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005547 outPointer.touchMinor = outPointer.touchMajor;
5548 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005549
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005550 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005551 outPointer.toolMajor = inSlot.absMTWidthMajor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005552 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005553 // Default tool area to 0 if absent.
5554 outPointer.toolMajor = 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005555 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005556
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005557 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005558 outPointer.toolMinor = inSlot.absMTWidthMinor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005559 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005560 // Assume tool area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005561 outPointer.toolMinor = outPointer.toolMajor;
5562 }
5563
5564 if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005565 outPointer.orientation = inSlot.absMTOrientation;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005566 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005567 // Default orientation to vertical if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005568 outPointer.orientation = 0;
5569 }
5570
Jeff Brown80fd47c2011-05-24 01:07:44 -07005571 if (fields & Accumulator::FIELD_ABS_MT_DISTANCE) {
5572 outPointer.distance = inSlot.absMTDistance;
5573 } else {
5574 // Default distance is 0 (direct contact).
5575 outPointer.distance = 0;
5576 }
5577
5578 if (fields & Accumulator::FIELD_ABS_MT_TOOL_TYPE) {
5579 outPointer.isStylus = (inSlot.absMTToolType == MT_TOOL_PEN);
5580 } else {
5581 // Assume this is not a stylus.
5582 outPointer.isStylus = false;
5583 }
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005584
Jeff Brown8d608662010-08-30 03:02:23 -07005585 // Assign pointer id using tracking id if available.
Jeff Brown6d0fec22010-07-23 21:28:06 -07005586 if (havePointerIds) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005587 int32_t id;
5588 if (mUsingSlotsProtocol) {
5589 id = inIndex;
5590 } else if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) {
5591 id = inSlot.absMTTrackingId;
5592 } else {
5593 id = -1;
5594 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005595
Jeff Brown80fd47c2011-05-24 01:07:44 -07005596 if (id >= 0 && id <= MAX_POINTER_ID) {
5597 outPointer.id = id;
5598 mCurrentTouch.idToIndex[id] = outCount;
5599 mCurrentTouch.idBits.markBit(id);
5600 } else {
5601 if (id >= 0) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005602#if DEBUG_POINTERS
Jeff Brown80fd47c2011-05-24 01:07:44 -07005603 LOGD("Pointers: Ignoring driver provided slot index or tracking id %d because "
5604 "it is larger than the maximum supported pointer id %d",
Jeff Brown6d0fec22010-07-23 21:28:06 -07005605 id, MAX_POINTER_ID);
5606#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005607 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005608 havePointerIds = false;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005609 }
5610 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005611
Jeff Brown6d0fec22010-07-23 21:28:06 -07005612 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005613 }
5614
Jeff Brown6d0fec22010-07-23 21:28:06 -07005615 mCurrentTouch.pointerCount = outCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005616
Jeff Brownace13b12011-03-09 17:39:48 -08005617 mButtonState = (mButtonState | mAccumulator.buttonDown) & ~mAccumulator.buttonUp;
5618 mCurrentTouch.buttonState = mButtonState;
5619
Jeff Brown6d0fec22010-07-23 21:28:06 -07005620 syncTouch(when, havePointerIds);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005621
Jeff Brown80fd47c2011-05-24 01:07:44 -07005622 mAccumulator.clear(mUsingSlotsProtocol ? 0 : mSlotCount);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005623}
5624
Jeff Brown8d608662010-08-30 03:02:23 -07005625void MultiTouchInputMapper::configureRawAxes() {
5626 TouchInputMapper::configureRawAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005627
Jeff Brown80fd47c2011-05-24 01:07:44 -07005628 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, &mRawAxes.x);
5629 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, &mRawAxes.y);
5630 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, &mRawAxes.touchMajor);
5631 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, &mRawAxes.touchMinor);
5632 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, &mRawAxes.toolMajor);
5633 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, &mRawAxes.toolMinor);
5634 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, &mRawAxes.orientation);
5635 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, &mRawAxes.pressure);
5636 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_DISTANCE, &mRawAxes.distance);
5637 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TRACKING_ID, &mRawAxes.trackingId);
5638 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_SLOT, &mRawAxes.slot);
5639
5640 if (mRawAxes.trackingId.valid
5641 && mRawAxes.slot.valid && mRawAxes.slot.minValue == 0 && mRawAxes.slot.maxValue > 0) {
5642 mSlotCount = mRawAxes.slot.maxValue + 1;
5643 if (mSlotCount > MAX_SLOTS) {
5644 LOGW("MultiTouch Device %s reported %d slots but the framework "
5645 "only supports a maximum of %d slots at this time.",
5646 getDeviceName().string(), mSlotCount, MAX_SLOTS);
5647 mSlotCount = MAX_SLOTS;
5648 }
5649 mUsingSlotsProtocol = true;
5650 } else {
5651 mSlotCount = MAX_POINTERS;
5652 mUsingSlotsProtocol = false;
5653 }
5654
5655 mAccumulator.allocateSlots(mSlotCount);
Jeff Brown9c3cda02010-06-15 01:31:58 -07005656}
5657
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005658
Jeff Browncb1404e2011-01-15 18:14:15 -08005659// --- JoystickInputMapper ---
5660
5661JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5662 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005663}
5664
5665JoystickInputMapper::~JoystickInputMapper() {
5666}
5667
5668uint32_t JoystickInputMapper::getSources() {
5669 return AINPUT_SOURCE_JOYSTICK;
5670}
5671
5672void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5673 InputMapper::populateDeviceInfo(info);
5674
Jeff Brown6f2fba42011-02-19 01:08:02 -08005675 for (size_t i = 0; i < mAxes.size(); i++) {
5676 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005677 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5678 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005679 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005680 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5681 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005682 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005683 }
5684}
5685
5686void JoystickInputMapper::dump(String8& dump) {
5687 dump.append(INDENT2 "Joystick Input Mapper:\n");
5688
Jeff Brown6f2fba42011-02-19 01:08:02 -08005689 dump.append(INDENT3 "Axes:\n");
5690 size_t numAxes = mAxes.size();
5691 for (size_t i = 0; i < numAxes; i++) {
5692 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005693 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005694 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005695 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005696 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005697 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005698 }
Jeff Brown85297452011-03-04 13:07:49 -08005699 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5700 label = getAxisLabel(axis.axisInfo.highAxis);
5701 if (label) {
5702 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5703 } else {
5704 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5705 axis.axisInfo.splitValue);
5706 }
5707 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5708 dump.append(" (invert)");
5709 }
5710
5711 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5712 axis.min, axis.max, axis.flat, axis.fuzz);
5713 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5714 "highScale=%0.5f, highOffset=%0.5f\n",
5715 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005716 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, rawFlat=%d, rawFuzz=%d\n",
5717 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
5718 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz);
Jeff Browncb1404e2011-01-15 18:14:15 -08005719 }
5720}
5721
5722void JoystickInputMapper::configure() {
5723 InputMapper::configure();
5724
Jeff Brown6f2fba42011-02-19 01:08:02 -08005725 // Collect all axes.
5726 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
5727 RawAbsoluteAxisInfo rawAxisInfo;
5728 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), abs, &rawAxisInfo);
5729 if (rawAxisInfo.valid) {
Jeff Brown85297452011-03-04 13:07:49 -08005730 // Map axis.
5731 AxisInfo axisInfo;
5732 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005733 if (!explicitlyMapped) {
5734 // Axis is not explicitly mapped, will choose a generic axis later.
Jeff Brown85297452011-03-04 13:07:49 -08005735 axisInfo.mode = AxisInfo::MODE_NORMAL;
5736 axisInfo.axis = -1;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005737 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005738
Jeff Brown85297452011-03-04 13:07:49 -08005739 // Apply flat override.
5740 int32_t rawFlat = axisInfo.flatOverride < 0
5741 ? rawAxisInfo.flat : axisInfo.flatOverride;
5742
5743 // Calculate scaling factors and limits.
Jeff Brown6f2fba42011-02-19 01:08:02 -08005744 Axis axis;
Jeff Brown85297452011-03-04 13:07:49 -08005745 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5746 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5747 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5748 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5749 scale, 0.0f, highScale, 0.0f,
5750 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5751 } else if (isCenteredAxis(axisInfo.axis)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005752 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5753 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
Jeff Brown85297452011-03-04 13:07:49 -08005754 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5755 scale, offset, scale, offset,
5756 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005757 } else {
5758 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
Jeff Brown85297452011-03-04 13:07:49 -08005759 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5760 scale, 0.0f, scale, 0.0f,
5761 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005762 }
5763
5764 // To eliminate noise while the joystick is at rest, filter out small variations
5765 // in axis values up front.
5766 axis.filter = axis.flat * 0.25f;
5767
5768 mAxes.add(abs, axis);
5769 }
5770 }
5771
5772 // If there are too many axes, start dropping them.
5773 // Prefer to keep explicitly mapped axes.
5774 if (mAxes.size() > PointerCoords::MAX_AXES) {
5775 LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
5776 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5777 pruneAxes(true);
5778 pruneAxes(false);
5779 }
5780
5781 // Assign generic axis ids to remaining axes.
5782 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5783 size_t numAxes = mAxes.size();
5784 for (size_t i = 0; i < numAxes; i++) {
5785 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005786 if (axis.axisInfo.axis < 0) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005787 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5788 && haveAxis(nextGenericAxisId)) {
5789 nextGenericAxisId += 1;
5790 }
5791
5792 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
Jeff Brown85297452011-03-04 13:07:49 -08005793 axis.axisInfo.axis = nextGenericAxisId;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005794 nextGenericAxisId += 1;
5795 } else {
5796 LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
5797 "have already been assigned to other axes.",
5798 getDeviceName().string(), mAxes.keyAt(i));
5799 mAxes.removeItemsAt(i--);
5800 numAxes -= 1;
5801 }
5802 }
5803 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005804}
5805
Jeff Brown85297452011-03-04 13:07:49 -08005806bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005807 size_t numAxes = mAxes.size();
5808 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005809 const Axis& axis = mAxes.valueAt(i);
5810 if (axis.axisInfo.axis == axisId
5811 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5812 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005813 return true;
5814 }
5815 }
5816 return false;
5817}
Jeff Browncb1404e2011-01-15 18:14:15 -08005818
Jeff Brown6f2fba42011-02-19 01:08:02 -08005819void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5820 size_t i = mAxes.size();
5821 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5822 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5823 continue;
5824 }
5825 LOGI("Discarding joystick '%s' axis %d because there are too many axes.",
5826 getDeviceName().string(), mAxes.keyAt(i));
5827 mAxes.removeItemsAt(i);
5828 }
5829}
5830
5831bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5832 switch (axis) {
5833 case AMOTION_EVENT_AXIS_X:
5834 case AMOTION_EVENT_AXIS_Y:
5835 case AMOTION_EVENT_AXIS_Z:
5836 case AMOTION_EVENT_AXIS_RX:
5837 case AMOTION_EVENT_AXIS_RY:
5838 case AMOTION_EVENT_AXIS_RZ:
5839 case AMOTION_EVENT_AXIS_HAT_X:
5840 case AMOTION_EVENT_AXIS_HAT_Y:
5841 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005842 case AMOTION_EVENT_AXIS_RUDDER:
5843 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005844 return true;
5845 default:
5846 return false;
5847 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005848}
5849
5850void JoystickInputMapper::reset() {
5851 // Recenter all axes.
5852 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Browncb1404e2011-01-15 18:14:15 -08005853
Jeff Brown6f2fba42011-02-19 01:08:02 -08005854 size_t numAxes = mAxes.size();
5855 for (size_t i = 0; i < numAxes; i++) {
5856 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005857 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005858 }
5859
5860 sync(when, true /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005861
5862 InputMapper::reset();
5863}
5864
5865void JoystickInputMapper::process(const RawEvent* rawEvent) {
5866 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005867 case EV_ABS: {
5868 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
5869 if (index >= 0) {
5870 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005871 float newValue, highNewValue;
5872 switch (axis.axisInfo.mode) {
5873 case AxisInfo::MODE_INVERT:
5874 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5875 * axis.scale + axis.offset;
5876 highNewValue = 0.0f;
5877 break;
5878 case AxisInfo::MODE_SPLIT:
5879 if (rawEvent->value < axis.axisInfo.splitValue) {
5880 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5881 * axis.scale + axis.offset;
5882 highNewValue = 0.0f;
5883 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5884 newValue = 0.0f;
5885 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5886 * axis.highScale + axis.highOffset;
5887 } else {
5888 newValue = 0.0f;
5889 highNewValue = 0.0f;
5890 }
5891 break;
5892 default:
5893 newValue = rawEvent->value * axis.scale + axis.offset;
5894 highNewValue = 0.0f;
5895 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005896 }
Jeff Brown85297452011-03-04 13:07:49 -08005897 axis.newValue = newValue;
5898 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005899 }
5900 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005901 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005902
5903 case EV_SYN:
5904 switch (rawEvent->scanCode) {
5905 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005906 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005907 break;
5908 }
5909 break;
5910 }
5911}
5912
Jeff Brown6f2fba42011-02-19 01:08:02 -08005913void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005914 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005915 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005916 }
5917
5918 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005919 int32_t buttonState = 0;
5920
5921 PointerProperties pointerProperties;
5922 pointerProperties.clear();
5923 pointerProperties.id = 0;
5924 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08005925
Jeff Brown6f2fba42011-02-19 01:08:02 -08005926 PointerCoords pointerCoords;
5927 pointerCoords.clear();
5928
5929 size_t numAxes = mAxes.size();
5930 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005931 const Axis& axis = mAxes.valueAt(i);
5932 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
5933 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5934 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
5935 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005936 }
5937
Jeff Brown56194eb2011-03-02 19:23:13 -08005938 // Moving a joystick axis should not wake the devide because joysticks can
5939 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
5940 // button will likely wake the device.
5941 // TODO: Use the input device configuration to control this behavior more finely.
5942 uint32_t policyFlags = 0;
5943
Jeff Brown56194eb2011-03-02 19:23:13 -08005944 getDispatcher()->notifyMotion(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005945 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
5946 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Browncb1404e2011-01-15 18:14:15 -08005947}
5948
Jeff Brown85297452011-03-04 13:07:49 -08005949bool JoystickInputMapper::filterAxes(bool force) {
5950 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005951 size_t numAxes = mAxes.size();
5952 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005953 Axis& axis = mAxes.editValueAt(i);
5954 if (force || hasValueChangedSignificantly(axis.filter,
5955 axis.newValue, axis.currentValue, axis.min, axis.max)) {
5956 axis.currentValue = axis.newValue;
5957 atLeastOneSignificantChange = true;
5958 }
5959 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5960 if (force || hasValueChangedSignificantly(axis.filter,
5961 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
5962 axis.highCurrentValue = axis.highNewValue;
5963 atLeastOneSignificantChange = true;
5964 }
5965 }
5966 }
5967 return atLeastOneSignificantChange;
5968}
5969
5970bool JoystickInputMapper::hasValueChangedSignificantly(
5971 float filter, float newValue, float currentValue, float min, float max) {
5972 if (newValue != currentValue) {
5973 // Filter out small changes in value unless the value is converging on the axis
5974 // bounds or center point. This is intended to reduce the amount of information
5975 // sent to applications by particularly noisy joysticks (such as PS3).
5976 if (fabs(newValue - currentValue) > filter
5977 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
5978 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
5979 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
5980 return true;
5981 }
5982 }
5983 return false;
5984}
5985
5986bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
5987 float filter, float newValue, float currentValue, float thresholdValue) {
5988 float newDistance = fabs(newValue - thresholdValue);
5989 if (newDistance < filter) {
5990 float oldDistance = fabs(currentValue - thresholdValue);
5991 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005992 return true;
5993 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005994 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005995 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005996}
5997
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005998} // namespace android