blob: 6003207ba1a685c54e0d7412f035850b28346b9c [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 Brown349703e2010-06-22 01:27:15 -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
61// Quiet time between certain gesture transitions.
62// Time to allow for all fingers or buttons to settle into a stable state before
63// starting a new gesture.
64static const nsecs_t QUIET_INTERVAL = 100 * 1000000; // 100 ms
65
66// The minimum speed that a pointer must travel for us to consider switching the active
67// touch pointer to it during a drag. This threshold is set to avoid switching due
68// to noise from a finger resting on the touch pad (perhaps just pressing it down).
69static const float DRAG_MIN_SWITCH_SPEED = 50.0f; // pixels per second
70
71// Tap gesture delay time.
72// The time between down and up must be less than this to be considered a tap.
Jeff Brown2352b972011-04-12 22:39:53 -070073static const nsecs_t TAP_INTERVAL = 150 * 1000000; // 150 ms
Jeff Brownace13b12011-03-09 17:39:48 -080074
Jeff Brown79ac9692011-04-19 21:20:10 -070075// Tap drag gesture delay time.
76// The time between up and the next up must be greater than this to be considered a
77// drag. Otherwise, the previous tap is finished and a new tap begins.
78static const nsecs_t TAP_DRAG_INTERVAL = 150 * 1000000; // 150 ms
79
Jeff Brownace13b12011-03-09 17:39:48 -080080// The distance in pixels that the pointer is allowed to move from initial down
81// to up and still be called a tap.
Jeff Brown79ac9692011-04-19 21:20:10 -070082static const float TAP_SLOP = 10.0f; // 10 pixels
Jeff Brownace13b12011-03-09 17:39:48 -080083
Jeff Brown2352b972011-04-12 22:39:53 -070084// Time after the first touch points go down to settle on an initial centroid.
85// This is intended to be enough time to handle cases where the user puts down two
86// fingers at almost but not quite exactly the same time.
87static const nsecs_t MULTITOUCH_SETTLE_INTERVAL = 100 * 1000000; // 100ms
Jeff Brownace13b12011-03-09 17:39:48 -080088
Jeff Brown2352b972011-04-12 22:39:53 -070089// The transition from PRESS to SWIPE or FREEFORM gesture mode is made when
90// both of the pointers are moving at least this fast.
91static const float MULTITOUCH_MIN_SPEED = 150.0f; // pixels per second
92
93// The transition from PRESS to SWIPE gesture mode can only occur when the
Jeff Brownace13b12011-03-09 17:39:48 -080094// cosine of the angle between the two vectors is greater than or equal to than this value
95// which indicates that the vectors are oriented in the same direction.
96// When the vectors are oriented in the exactly same direction, the cosine is 1.0.
97// (In exactly opposite directions, the cosine is -1.0.)
98static const float SWIPE_TRANSITION_ANGLE_COSINE = 0.5f; // cosine of 45 degrees
99
Jeff Brown2352b972011-04-12 22:39:53 -0700100// The transition from PRESS to SWIPE gesture mode can only occur when the
101// fingers are no more than this far apart relative to the diagonal size of
102// the touch pad. For example, a ratio of 0.5 means that the fingers must be
103// no more than half the diagonal size of the touch pad apart.
104static const float SWIPE_MAX_WIDTH_RATIO = 0.333f; // 1/3
105
106// The gesture movement speed factor relative to the size of the display.
107// Movement speed applies when the fingers are moving in the same direction.
108// Without acceleration, a full swipe of the touch pad diagonal in movement mode
109// will cover this portion of the display diagonal.
110static const float GESTURE_MOVEMENT_SPEED_RATIO = 0.8f;
111
112// The gesture zoom speed factor relative to the size of the display.
113// Zoom speed applies when the fingers are mostly moving relative to each other
114// to execute a scale gesture or similar.
115// Without acceleration, a full swipe of the touch pad diagonal in zoom mode
116// will cover this portion of the display diagonal.
117static const float GESTURE_ZOOM_SPEED_RATIO = 0.3f;
118
Jeff Brownace13b12011-03-09 17:39:48 -0800119
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700120// --- Static Functions ---
121
122template<typename T>
123inline static T abs(const T& value) {
124 return value < 0 ? - value : value;
125}
126
127template<typename T>
128inline static T min(const T& a, const T& b) {
129 return a < b ? a : b;
130}
131
Jeff Brown5c225b12010-06-16 01:53:36 -0700132template<typename T>
133inline static void swap(T& a, T& b) {
134 T temp = a;
135 a = b;
136 b = temp;
137}
138
Jeff Brown8d608662010-08-30 03:02:23 -0700139inline static float avg(float x, float y) {
140 return (x + y) / 2;
141}
142
Jeff Brown2352b972011-04-12 22:39:53 -0700143inline static float distance(float x1, float y1, float x2, float y2) {
144 return hypotf(x1 - x2, y1 - y2);
Jeff Brownace13b12011-03-09 17:39:48 -0800145}
146
Jeff Brown517bb4c2011-01-14 19:09:23 -0800147inline static int32_t signExtendNybble(int32_t value) {
148 return value >= 8 ? value - 16 : value;
149}
150
Jeff Brownef3d7e82010-09-30 14:33:04 -0700151static inline const char* toString(bool value) {
152 return value ? "true" : "false";
153}
154
Jeff Brown9626b142011-03-03 02:09:54 -0800155static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
156 const int32_t map[][4], size_t mapSize) {
157 if (orientation != DISPLAY_ORIENTATION_0) {
158 for (size_t i = 0; i < mapSize; i++) {
159 if (value == map[i][0]) {
160 return map[i][orientation];
161 }
162 }
163 }
164 return value;
165}
166
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700167static const int32_t keyCodeRotationMap[][4] = {
168 // key codes enumerated counter-clockwise with the original (unrotated) key first
169 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd0358292010-06-30 16:10:35 -0700170 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
171 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
172 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
173 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700174};
Jeff Brown9626b142011-03-03 02:09:54 -0800175static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700176 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
177
178int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brown9626b142011-03-03 02:09:54 -0800179 return rotateValueUsingRotationMap(keyCode, orientation,
180 keyCodeRotationMap, keyCodeRotationMapSize);
181}
182
183static const int32_t edgeFlagRotationMap[][4] = {
184 // edge flags enumerated counter-clockwise with the original (unrotated) edge flag first
185 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
186 { AMOTION_EVENT_EDGE_FLAG_BOTTOM, AMOTION_EVENT_EDGE_FLAG_RIGHT,
187 AMOTION_EVENT_EDGE_FLAG_TOP, AMOTION_EVENT_EDGE_FLAG_LEFT },
188 { AMOTION_EVENT_EDGE_FLAG_RIGHT, AMOTION_EVENT_EDGE_FLAG_TOP,
189 AMOTION_EVENT_EDGE_FLAG_LEFT, AMOTION_EVENT_EDGE_FLAG_BOTTOM },
190 { AMOTION_EVENT_EDGE_FLAG_TOP, AMOTION_EVENT_EDGE_FLAG_LEFT,
191 AMOTION_EVENT_EDGE_FLAG_BOTTOM, AMOTION_EVENT_EDGE_FLAG_RIGHT },
192 { AMOTION_EVENT_EDGE_FLAG_LEFT, AMOTION_EVENT_EDGE_FLAG_BOTTOM,
193 AMOTION_EVENT_EDGE_FLAG_RIGHT, AMOTION_EVENT_EDGE_FLAG_TOP },
194};
195static const size_t edgeFlagRotationMapSize =
196 sizeof(edgeFlagRotationMap) / sizeof(edgeFlagRotationMap[0]);
197
198static int32_t rotateEdgeFlag(int32_t edgeFlag, int32_t orientation) {
199 return rotateValueUsingRotationMap(edgeFlag, orientation,
200 edgeFlagRotationMap, edgeFlagRotationMapSize);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700201}
202
Jeff Brown6d0fec22010-07-23 21:28:06 -0700203static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
204 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
205}
206
Jeff Brownefd32662011-03-08 15:13:06 -0800207static uint32_t getButtonStateForScanCode(int32_t scanCode) {
208 // Currently all buttons are mapped to the primary button.
209 switch (scanCode) {
210 case BTN_LEFT:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700211 return AMOTION_EVENT_BUTTON_PRIMARY;
Jeff Brownefd32662011-03-08 15:13:06 -0800212 case BTN_RIGHT:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700213 return AMOTION_EVENT_BUTTON_SECONDARY;
Jeff Brownefd32662011-03-08 15:13:06 -0800214 case BTN_MIDDLE:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700215 return AMOTION_EVENT_BUTTON_TERTIARY;
Jeff Brownefd32662011-03-08 15:13:06 -0800216 case BTN_SIDE:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700217 return AMOTION_EVENT_BUTTON_BACK;
Jeff Brownefd32662011-03-08 15:13:06 -0800218 case BTN_EXTRA:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700219 return AMOTION_EVENT_BUTTON_FORWARD;
Jeff Brownefd32662011-03-08 15:13:06 -0800220 case BTN_FORWARD:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700221 return AMOTION_EVENT_BUTTON_FORWARD;
Jeff Brownefd32662011-03-08 15:13:06 -0800222 case BTN_BACK:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700223 return AMOTION_EVENT_BUTTON_BACK;
Jeff Brownefd32662011-03-08 15:13:06 -0800224 case BTN_TASK:
Jeff Brownefd32662011-03-08 15:13:06 -0800225 default:
226 return 0;
227 }
228}
229
230// Returns true if the pointer should be reported as being down given the specified
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700231// button states. This determines whether the event is reported as a touch event.
232static bool isPointerDown(int32_t buttonState) {
233 return buttonState &
234 (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY
235 | AMOTION_EVENT_BUTTON_TERTIARY
236 | AMOTION_EVENT_BUTTON_ERASER);
Jeff Brownefd32662011-03-08 15:13:06 -0800237}
238
239static int32_t calculateEdgeFlagsUsingPointerBounds(
240 const sp<PointerControllerInterface>& pointerController, float x, float y) {
241 int32_t edgeFlags = 0;
242 float minX, minY, maxX, maxY;
243 if (pointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
244 if (x <= minX) {
245 edgeFlags |= AMOTION_EVENT_EDGE_FLAG_LEFT;
246 } else if (x >= maxX) {
247 edgeFlags |= AMOTION_EVENT_EDGE_FLAG_RIGHT;
248 }
249 if (y <= minY) {
250 edgeFlags |= AMOTION_EVENT_EDGE_FLAG_TOP;
251 } else if (y >= maxY) {
252 edgeFlags |= AMOTION_EVENT_EDGE_FLAG_BOTTOM;
253 }
254 }
255 return edgeFlags;
256}
257
Jeff Brown2352b972011-04-12 22:39:53 -0700258static void clampPositionUsingPointerBounds(
259 const sp<PointerControllerInterface>& pointerController, float* x, float* y) {
260 float minX, minY, maxX, maxY;
261 if (pointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
262 if (*x < minX) {
263 *x = minX;
264 } else if (*x > maxX) {
265 *x = maxX;
266 }
267 if (*y < minY) {
268 *y = minY;
269 } else if (*y > maxY) {
270 *y = maxY;
271 }
272 }
273}
274
275static float calculateCommonVector(float a, float b) {
276 if (a > 0 && b > 0) {
277 return a < b ? a : b;
278 } else if (a < 0 && b < 0) {
279 return a > b ? a : b;
280 } else {
281 return 0;
282 }
283}
284
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700285static void synthesizeButtonKey(InputReaderContext* context, int32_t action,
286 nsecs_t when, int32_t deviceId, uint32_t source,
287 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState,
288 int32_t buttonState, int32_t keyCode) {
289 if (
290 (action == AKEY_EVENT_ACTION_DOWN
291 && !(lastButtonState & buttonState)
292 && (currentButtonState & buttonState))
293 || (action == AKEY_EVENT_ACTION_UP
294 && (lastButtonState & buttonState)
295 && !(currentButtonState & buttonState))) {
296 context->getDispatcher()->notifyKey(when, deviceId, source, policyFlags,
297 action, 0, keyCode, 0, context->getGlobalMetaState(), when);
298 }
299}
300
301static void synthesizeButtonKeys(InputReaderContext* context, int32_t action,
302 nsecs_t when, int32_t deviceId, uint32_t source,
303 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState) {
304 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
305 lastButtonState, currentButtonState,
306 AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK);
307 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
308 lastButtonState, currentButtonState,
309 AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD);
310}
311
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700312
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700313// --- InputReader ---
314
315InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700316 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700317 const sp<InputDispatcherInterface>& dispatcher) :
Jeff Brown6d0fec22010-07-23 21:28:06 -0700318 mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher),
Jeff Brownaa3855d2011-03-17 01:34:19 -0700319 mGlobalMetaState(0), mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX) {
Jeff Brown9c3cda02010-06-15 01:31:58 -0700320 configureExcludedDevices();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700321 updateGlobalMetaState();
322 updateInputConfiguration();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700323}
324
325InputReader::~InputReader() {
326 for (size_t i = 0; i < mDevices.size(); i++) {
327 delete mDevices.valueAt(i);
328 }
329}
330
331void InputReader::loopOnce() {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700332 int32_t timeoutMillis = -1;
333 if (mNextTimeout != LLONG_MAX) {
334 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
335 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
336 }
337
Jeff Brownb7198742011-03-18 18:14:26 -0700338 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
339 if (count) {
340 processEvents(mEventBuffer, count);
341 }
342 if (!count || timeoutMillis == 0) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700343 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
344#if DEBUG_RAW_EVENTS
345 LOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
346#endif
347 mNextTimeout = LLONG_MAX;
348 timeoutExpired(now);
349 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700350}
351
Jeff Brownb7198742011-03-18 18:14:26 -0700352void InputReader::processEvents(const RawEvent* rawEvents, size_t count) {
353 for (const RawEvent* rawEvent = rawEvents; count;) {
354 int32_t type = rawEvent->type;
355 size_t batchSize = 1;
356 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
357 int32_t deviceId = rawEvent->deviceId;
358 while (batchSize < count) {
359 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
360 || rawEvent[batchSize].deviceId != deviceId) {
361 break;
362 }
363 batchSize += 1;
364 }
365#if DEBUG_RAW_EVENTS
366 LOGD("BatchSize: %d Count: %d", batchSize, count);
367#endif
368 processEventsForDevice(deviceId, rawEvent, batchSize);
369 } else {
370 switch (rawEvent->type) {
371 case EventHubInterface::DEVICE_ADDED:
372 addDevice(rawEvent->deviceId);
373 break;
374 case EventHubInterface::DEVICE_REMOVED:
375 removeDevice(rawEvent->deviceId);
376 break;
377 case EventHubInterface::FINISHED_DEVICE_SCAN:
378 handleConfigurationChanged(rawEvent->when);
379 break;
380 default:
Jeff Brownb6110c22011-04-01 16:15:13 -0700381 LOG_ASSERT(false); // can't happen
Jeff Brownb7198742011-03-18 18:14:26 -0700382 break;
383 }
384 }
385 count -= batchSize;
386 rawEvent += batchSize;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700387 }
388}
389
Jeff Brown7342bb92010-10-01 18:55:43 -0700390void InputReader::addDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700391 String8 name = mEventHub->getDeviceName(deviceId);
392 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
393
394 InputDevice* device = createDevice(deviceId, name, classes);
395 device->configure();
396
Jeff Brown8d608662010-08-30 03:02:23 -0700397 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800398 LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700399 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800400 LOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700401 device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700402 }
403
Jeff Brown6d0fec22010-07-23 21:28:06 -0700404 bool added = false;
405 { // acquire device registry writer lock
406 RWLock::AutoWLock _wl(mDeviceRegistryLock);
407
408 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
409 if (deviceIndex < 0) {
410 mDevices.add(deviceId, device);
411 added = true;
412 }
413 } // release device registry writer lock
414
415 if (! added) {
416 LOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
417 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700418 return;
419 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700420}
421
Jeff Brown7342bb92010-10-01 18:55:43 -0700422void InputReader::removeDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700423 bool removed = false;
424 InputDevice* device = NULL;
425 { // acquire device registry writer lock
426 RWLock::AutoWLock _wl(mDeviceRegistryLock);
427
428 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
429 if (deviceIndex >= 0) {
430 device = mDevices.valueAt(deviceIndex);
431 mDevices.removeItemsAt(deviceIndex, 1);
432 removed = true;
433 }
434 } // release device registry writer lock
435
436 if (! removed) {
437 LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700438 return;
439 }
440
Jeff Brown6d0fec22010-07-23 21:28:06 -0700441 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800442 LOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700443 device->getId(), device->getName().string());
444 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800445 LOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700446 device->getId(), device->getName().string(), device->getSources());
447 }
448
Jeff Brown8d608662010-08-30 03:02:23 -0700449 device->reset();
450
Jeff Brown6d0fec22010-07-23 21:28:06 -0700451 delete device;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700452}
453
Jeff Brown6d0fec22010-07-23 21:28:06 -0700454InputDevice* InputReader::createDevice(int32_t deviceId, const String8& name, uint32_t classes) {
455 InputDevice* device = new InputDevice(this, deviceId, name);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700456
Jeff Brown56194eb2011-03-02 19:23:13 -0800457 // External devices.
458 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
459 device->setExternal(true);
460 }
461
Jeff Brown6d0fec22010-07-23 21:28:06 -0700462 // Switch-like devices.
463 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
464 device->addMapper(new SwitchInputMapper(device));
465 }
466
467 // Keyboard-like devices.
Jeff Brownefd32662011-03-08 15:13:06 -0800468 uint32_t keyboardSource = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700469 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
470 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800471 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700472 }
473 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
474 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
475 }
476 if (classes & INPUT_DEVICE_CLASS_DPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800477 keyboardSource |= AINPUT_SOURCE_DPAD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700478 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800479 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800480 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
Jeff Browncb1404e2011-01-15 18:14:15 -0800481 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700482
Jeff Brownefd32662011-03-08 15:13:06 -0800483 if (keyboardSource != 0) {
484 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700485 }
486
Jeff Brown83c09682010-12-23 17:50:18 -0800487 // Cursor-like devices.
488 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
489 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700490 }
491
Jeff Brown58a2da82011-01-25 16:02:22 -0800492 // Touchscreens and touchpad devices.
493 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800494 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800495 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800496 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700497 }
498
Jeff Browncb1404e2011-01-15 18:14:15 -0800499 // Joystick-like devices.
500 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
501 device->addMapper(new JoystickInputMapper(device));
502 }
503
Jeff Brown6d0fec22010-07-23 21:28:06 -0700504 return device;
505}
506
Jeff Brownb7198742011-03-18 18:14:26 -0700507void InputReader::processEventsForDevice(int32_t deviceId,
508 const RawEvent* rawEvents, size_t count) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700509 { // acquire device registry reader lock
510 RWLock::AutoRLock _rl(mDeviceRegistryLock);
511
512 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
513 if (deviceIndex < 0) {
514 LOGW("Discarding event for unknown deviceId %d.", deviceId);
515 return;
516 }
517
518 InputDevice* device = mDevices.valueAt(deviceIndex);
519 if (device->isIgnored()) {
520 //LOGD("Discarding event for ignored deviceId %d.", deviceId);
521 return;
522 }
523
Jeff Brownb7198742011-03-18 18:14:26 -0700524 device->process(rawEvents, count);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700525 } // release device registry reader lock
526}
527
Jeff Brownaa3855d2011-03-17 01:34:19 -0700528void InputReader::timeoutExpired(nsecs_t when) {
529 { // acquire device registry reader lock
530 RWLock::AutoRLock _rl(mDeviceRegistryLock);
531
532 for (size_t i = 0; i < mDevices.size(); i++) {
533 InputDevice* device = mDevices.valueAt(i);
534 if (!device->isIgnored()) {
535 device->timeoutExpired(when);
536 }
537 }
538 } // release device registry reader lock
539}
540
Jeff Brownc3db8582010-10-20 15:33:38 -0700541void InputReader::handleConfigurationChanged(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700542 // Reset global meta state because it depends on the list of all configured devices.
543 updateGlobalMetaState();
544
545 // Update input configuration.
546 updateInputConfiguration();
547
548 // Enqueue configuration changed.
549 mDispatcher->notifyConfigurationChanged(when);
550}
551
552void InputReader::configureExcludedDevices() {
553 Vector<String8> excludedDeviceNames;
554 mPolicy->getExcludedDeviceNames(excludedDeviceNames);
555
556 for (size_t i = 0; i < excludedDeviceNames.size(); i++) {
557 mEventHub->addExcludedDevice(excludedDeviceNames[i]);
558 }
559}
560
561void InputReader::updateGlobalMetaState() {
562 { // acquire state lock
563 AutoMutex _l(mStateLock);
564
565 mGlobalMetaState = 0;
566
567 { // acquire device registry reader lock
568 RWLock::AutoRLock _rl(mDeviceRegistryLock);
569
570 for (size_t i = 0; i < mDevices.size(); i++) {
571 InputDevice* device = mDevices.valueAt(i);
572 mGlobalMetaState |= device->getMetaState();
573 }
574 } // release device registry reader lock
575 } // release state lock
576}
577
578int32_t InputReader::getGlobalMetaState() {
579 { // acquire state lock
580 AutoMutex _l(mStateLock);
581
582 return mGlobalMetaState;
583 } // release state lock
584}
585
586void InputReader::updateInputConfiguration() {
587 { // acquire state lock
588 AutoMutex _l(mStateLock);
589
590 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
591 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
592 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
593 { // acquire device registry reader lock
594 RWLock::AutoRLock _rl(mDeviceRegistryLock);
595
596 InputDeviceInfo deviceInfo;
597 for (size_t i = 0; i < mDevices.size(); i++) {
598 InputDevice* device = mDevices.valueAt(i);
599 device->getDeviceInfo(& deviceInfo);
600 uint32_t sources = deviceInfo.getSources();
601
602 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
603 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
604 }
605 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
606 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
607 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
608 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
609 }
610 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
611 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700612 }
613 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700614 } // release device registry reader lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700615
Jeff Brown6d0fec22010-07-23 21:28:06 -0700616 mInputConfiguration.touchScreen = touchScreenConfig;
617 mInputConfiguration.keyboard = keyboardConfig;
618 mInputConfiguration.navigation = navigationConfig;
619 } // release state lock
620}
621
Jeff Brownfe508922011-01-18 15:10:10 -0800622void InputReader::disableVirtualKeysUntil(nsecs_t time) {
623 mDisableVirtualKeysTimeout = time;
624}
625
626bool InputReader::shouldDropVirtualKey(nsecs_t now,
627 InputDevice* device, int32_t keyCode, int32_t scanCode) {
628 if (now < mDisableVirtualKeysTimeout) {
629 LOGI("Dropping virtual key from device %s because virtual keys are "
630 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
631 device->getName().string(),
632 (mDisableVirtualKeysTimeout - now) * 0.000001,
633 keyCode, scanCode);
634 return true;
635 } else {
636 return false;
637 }
638}
639
Jeff Brown05dc66a2011-03-02 14:41:58 -0800640void InputReader::fadePointer() {
641 { // acquire device registry reader lock
642 RWLock::AutoRLock _rl(mDeviceRegistryLock);
643
644 for (size_t i = 0; i < mDevices.size(); i++) {
645 InputDevice* device = mDevices.valueAt(i);
646 device->fadePointer();
647 }
648 } // release device registry reader lock
649}
650
Jeff Brownaa3855d2011-03-17 01:34:19 -0700651void InputReader::requestTimeoutAtTime(nsecs_t when) {
652 if (when < mNextTimeout) {
653 mNextTimeout = when;
654 }
655}
656
Jeff Brown6d0fec22010-07-23 21:28:06 -0700657void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
658 { // acquire state lock
659 AutoMutex _l(mStateLock);
660
661 *outConfiguration = mInputConfiguration;
662 } // release state lock
663}
664
665status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
666 { // acquire device registry reader lock
667 RWLock::AutoRLock _rl(mDeviceRegistryLock);
668
669 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
670 if (deviceIndex < 0) {
671 return NAME_NOT_FOUND;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700672 }
673
Jeff Brown6d0fec22010-07-23 21:28:06 -0700674 InputDevice* device = mDevices.valueAt(deviceIndex);
675 if (device->isIgnored()) {
676 return NAME_NOT_FOUND;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700677 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700678
679 device->getDeviceInfo(outDeviceInfo);
680 return OK;
681 } // release device registy reader lock
682}
683
684void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
685 outDeviceIds.clear();
686
687 { // acquire device registry reader lock
688 RWLock::AutoRLock _rl(mDeviceRegistryLock);
689
690 size_t numDevices = mDevices.size();
691 for (size_t i = 0; i < numDevices; i++) {
692 InputDevice* device = mDevices.valueAt(i);
693 if (! device->isIgnored()) {
694 outDeviceIds.add(device->getId());
695 }
696 }
697 } // release device registy reader lock
698}
699
700int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
701 int32_t keyCode) {
702 return getState(deviceId, sourceMask, keyCode, & InputDevice::getKeyCodeState);
703}
704
705int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
706 int32_t scanCode) {
707 return getState(deviceId, sourceMask, scanCode, & InputDevice::getScanCodeState);
708}
709
710int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
711 return getState(deviceId, sourceMask, switchCode, & InputDevice::getSwitchState);
712}
713
714int32_t InputReader::getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
715 GetStateFunc getStateFunc) {
716 { // acquire device registry reader lock
717 RWLock::AutoRLock _rl(mDeviceRegistryLock);
718
719 int32_t result = AKEY_STATE_UNKNOWN;
720 if (deviceId >= 0) {
721 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
722 if (deviceIndex >= 0) {
723 InputDevice* device = mDevices.valueAt(deviceIndex);
724 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
725 result = (device->*getStateFunc)(sourceMask, code);
726 }
727 }
728 } else {
729 size_t numDevices = mDevices.size();
730 for (size_t i = 0; i < numDevices; i++) {
731 InputDevice* device = mDevices.valueAt(i);
732 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
733 result = (device->*getStateFunc)(sourceMask, code);
734 if (result >= AKEY_STATE_DOWN) {
735 return result;
736 }
737 }
738 }
739 }
740 return result;
741 } // release device registy reader lock
742}
743
744bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
745 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
746 memset(outFlags, 0, numCodes);
747 return markSupportedKeyCodes(deviceId, sourceMask, numCodes, keyCodes, outFlags);
748}
749
750bool InputReader::markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
751 const int32_t* keyCodes, uint8_t* outFlags) {
752 { // acquire device registry reader lock
753 RWLock::AutoRLock _rl(mDeviceRegistryLock);
754 bool result = false;
755 if (deviceId >= 0) {
756 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
757 if (deviceIndex >= 0) {
758 InputDevice* device = mDevices.valueAt(deviceIndex);
759 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
760 result = device->markSupportedKeyCodes(sourceMask,
761 numCodes, keyCodes, outFlags);
762 }
763 }
764 } else {
765 size_t numDevices = mDevices.size();
766 for (size_t i = 0; i < numDevices; i++) {
767 InputDevice* device = mDevices.valueAt(i);
768 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
769 result |= device->markSupportedKeyCodes(sourceMask,
770 numCodes, keyCodes, outFlags);
771 }
772 }
773 }
774 return result;
775 } // release device registy reader lock
776}
777
Jeff Brownb88102f2010-09-08 11:49:43 -0700778void InputReader::dump(String8& dump) {
Jeff Brownf2f487182010-10-01 17:46:21 -0700779 mEventHub->dump(dump);
780 dump.append("\n");
781
782 dump.append("Input Reader State:\n");
783
Jeff Brownef3d7e82010-09-30 14:33:04 -0700784 { // acquire device registry reader lock
785 RWLock::AutoRLock _rl(mDeviceRegistryLock);
Jeff Brownb88102f2010-09-08 11:49:43 -0700786
Jeff Brownef3d7e82010-09-30 14:33:04 -0700787 for (size_t i = 0; i < mDevices.size(); i++) {
788 mDevices.valueAt(i)->dump(dump);
Jeff Brownb88102f2010-09-08 11:49:43 -0700789 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700790 } // release device registy reader lock
Jeff Brownb88102f2010-09-08 11:49:43 -0700791}
792
Jeff Brown6d0fec22010-07-23 21:28:06 -0700793
794// --- InputReaderThread ---
795
796InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
797 Thread(/*canCallJava*/ true), mReader(reader) {
798}
799
800InputReaderThread::~InputReaderThread() {
801}
802
803bool InputReaderThread::threadLoop() {
804 mReader->loopOnce();
805 return true;
806}
807
808
809// --- InputDevice ---
810
811InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) :
Jeff Brown56194eb2011-03-02 19:23:13 -0800812 mContext(context), mId(id), mName(name), mSources(0), mIsExternal(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700813}
814
815InputDevice::~InputDevice() {
816 size_t numMappers = mMappers.size();
817 for (size_t i = 0; i < numMappers; i++) {
818 delete mMappers[i];
819 }
820 mMappers.clear();
821}
822
Jeff Brownef3d7e82010-09-30 14:33:04 -0700823void InputDevice::dump(String8& dump) {
824 InputDeviceInfo deviceInfo;
825 getDeviceInfo(& deviceInfo);
826
Jeff Brown90655042010-12-02 13:50:46 -0800827 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700828 deviceInfo.getName().string());
Jeff Brown56194eb2011-03-02 19:23:13 -0800829 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700830 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
831 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800832
Jeff Brownefd32662011-03-08 15:13:06 -0800833 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800834 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700835 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800836 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800837 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
838 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800839 char name[32];
840 if (label) {
841 strncpy(name, label, sizeof(name));
842 name[sizeof(name) - 1] = '\0';
843 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800844 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800845 }
Jeff Brownefd32662011-03-08 15:13:06 -0800846 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
847 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
848 name, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Browncc0c1592011-02-19 05:07:28 -0800849 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700850 }
851
852 size_t numMappers = mMappers.size();
853 for (size_t i = 0; i < numMappers; i++) {
854 InputMapper* mapper = mMappers[i];
855 mapper->dump(dump);
856 }
857}
858
Jeff Brown6d0fec22010-07-23 21:28:06 -0700859void InputDevice::addMapper(InputMapper* mapper) {
860 mMappers.add(mapper);
861}
862
863void InputDevice::configure() {
Jeff Brown8d608662010-08-30 03:02:23 -0700864 if (! isIgnored()) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800865 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
Jeff Brown8d608662010-08-30 03:02:23 -0700866 }
867
Jeff Brown6d0fec22010-07-23 21:28:06 -0700868 mSources = 0;
869
870 size_t numMappers = mMappers.size();
871 for (size_t i = 0; i < numMappers; i++) {
872 InputMapper* mapper = mMappers[i];
873 mapper->configure();
874 mSources |= mapper->getSources();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700875 }
876}
877
Jeff Brown6d0fec22010-07-23 21:28:06 -0700878void InputDevice::reset() {
879 size_t numMappers = mMappers.size();
880 for (size_t i = 0; i < numMappers; i++) {
881 InputMapper* mapper = mMappers[i];
882 mapper->reset();
883 }
884}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700885
Jeff Brownb7198742011-03-18 18:14:26 -0700886void InputDevice::process(const RawEvent* rawEvents, size_t count) {
887 // Process all of the events in order for each mapper.
888 // We cannot simply ask each mapper to process them in bulk because mappers may
889 // have side-effects that must be interleaved. For example, joystick movement events and
890 // gamepad button presses are handled by different mappers but they should be dispatched
891 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700892 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700893 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
894#if DEBUG_RAW_EVENTS
895 LOGD("Input event: device=%d type=0x%04x scancode=0x%04x "
896 "keycode=0x%04x value=0x%04x flags=0x%08x",
897 rawEvent->deviceId, rawEvent->type, rawEvent->scanCode, rawEvent->keyCode,
898 rawEvent->value, rawEvent->flags);
899#endif
900
901 for (size_t i = 0; i < numMappers; i++) {
902 InputMapper* mapper = mMappers[i];
903 mapper->process(rawEvent);
904 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700905 }
906}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700907
Jeff Brownaa3855d2011-03-17 01:34:19 -0700908void InputDevice::timeoutExpired(nsecs_t when) {
909 size_t numMappers = mMappers.size();
910 for (size_t i = 0; i < numMappers; i++) {
911 InputMapper* mapper = mMappers[i];
912 mapper->timeoutExpired(when);
913 }
914}
915
Jeff Brown6d0fec22010-07-23 21:28:06 -0700916void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
917 outDeviceInfo->initialize(mId, mName);
918
919 size_t numMappers = mMappers.size();
920 for (size_t i = 0; i < numMappers; i++) {
921 InputMapper* mapper = mMappers[i];
922 mapper->populateDeviceInfo(outDeviceInfo);
923 }
924}
925
926int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
927 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
928}
929
930int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
931 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
932}
933
934int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
935 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
936}
937
938int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
939 int32_t result = AKEY_STATE_UNKNOWN;
940 size_t numMappers = mMappers.size();
941 for (size_t i = 0; i < numMappers; i++) {
942 InputMapper* mapper = mMappers[i];
943 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
944 result = (mapper->*getStateFunc)(sourceMask, code);
945 if (result >= AKEY_STATE_DOWN) {
946 return result;
947 }
948 }
949 }
950 return result;
951}
952
953bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
954 const int32_t* keyCodes, uint8_t* outFlags) {
955 bool result = false;
956 size_t numMappers = mMappers.size();
957 for (size_t i = 0; i < numMappers; i++) {
958 InputMapper* mapper = mMappers[i];
959 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
960 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
961 }
962 }
963 return result;
964}
965
966int32_t InputDevice::getMetaState() {
967 int32_t result = 0;
968 size_t numMappers = mMappers.size();
969 for (size_t i = 0; i < numMappers; i++) {
970 InputMapper* mapper = mMappers[i];
971 result |= mapper->getMetaState();
972 }
973 return result;
974}
975
Jeff Brown05dc66a2011-03-02 14:41:58 -0800976void InputDevice::fadePointer() {
977 size_t numMappers = mMappers.size();
978 for (size_t i = 0; i < numMappers; i++) {
979 InputMapper* mapper = mMappers[i];
980 mapper->fadePointer();
981 }
982}
983
Jeff Brown6d0fec22010-07-23 21:28:06 -0700984
985// --- InputMapper ---
986
987InputMapper::InputMapper(InputDevice* device) :
988 mDevice(device), mContext(device->getContext()) {
989}
990
991InputMapper::~InputMapper() {
992}
993
994void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
995 info->addSource(getSources());
996}
997
Jeff Brownef3d7e82010-09-30 14:33:04 -0700998void InputMapper::dump(String8& dump) {
999}
1000
Jeff Brown6d0fec22010-07-23 21:28:06 -07001001void InputMapper::configure() {
1002}
1003
1004void InputMapper::reset() {
1005}
1006
Jeff Brownaa3855d2011-03-17 01:34:19 -07001007void InputMapper::timeoutExpired(nsecs_t when) {
1008}
1009
Jeff Brown6d0fec22010-07-23 21:28:06 -07001010int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1011 return AKEY_STATE_UNKNOWN;
1012}
1013
1014int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1015 return AKEY_STATE_UNKNOWN;
1016}
1017
1018int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1019 return AKEY_STATE_UNKNOWN;
1020}
1021
1022bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1023 const int32_t* keyCodes, uint8_t* outFlags) {
1024 return false;
1025}
1026
1027int32_t InputMapper::getMetaState() {
1028 return 0;
1029}
1030
Jeff Brown05dc66a2011-03-02 14:41:58 -08001031void InputMapper::fadePointer() {
1032}
1033
Jeff Browncb1404e2011-01-15 18:14:15 -08001034void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1035 const RawAbsoluteAxisInfo& axis, const char* name) {
1036 if (axis.valid) {
1037 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d\n",
1038 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz);
1039 } else {
1040 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1041 }
1042}
1043
Jeff Brown6d0fec22010-07-23 21:28:06 -07001044
1045// --- SwitchInputMapper ---
1046
1047SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1048 InputMapper(device) {
1049}
1050
1051SwitchInputMapper::~SwitchInputMapper() {
1052}
1053
1054uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001055 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001056}
1057
1058void SwitchInputMapper::process(const RawEvent* rawEvent) {
1059 switch (rawEvent->type) {
1060 case EV_SW:
1061 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
1062 break;
1063 }
1064}
1065
1066void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownb6997262010-10-08 22:31:17 -07001067 getDispatcher()->notifySwitch(when, switchCode, switchValue, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001068}
1069
1070int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1071 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1072}
1073
1074
1075// --- KeyboardInputMapper ---
1076
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001077KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001078 uint32_t source, int32_t keyboardType) :
1079 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001080 mKeyboardType(keyboardType) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001081 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001082}
1083
1084KeyboardInputMapper::~KeyboardInputMapper() {
1085}
1086
Jeff Brown6328cdc2010-07-29 18:18:33 -07001087void KeyboardInputMapper::initializeLocked() {
1088 mLocked.metaState = AMETA_NONE;
1089 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001090}
1091
1092uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001093 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001094}
1095
1096void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1097 InputMapper::populateDeviceInfo(info);
1098
1099 info->setKeyboardType(mKeyboardType);
1100}
1101
Jeff Brownef3d7e82010-09-30 14:33:04 -07001102void KeyboardInputMapper::dump(String8& dump) {
1103 { // acquire lock
1104 AutoMutex _l(mLock);
1105 dump.append(INDENT2 "Keyboard Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001106 dumpParameters(dump);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001107 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
1108 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mLocked.keyDowns.size());
1109 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mLocked.metaState);
1110 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1111 } // release lock
1112}
1113
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001114
1115void KeyboardInputMapper::configure() {
1116 InputMapper::configure();
1117
1118 // Configure basic parameters.
1119 configureParameters();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001120
1121 // Reset LEDs.
1122 {
1123 AutoMutex _l(mLock);
1124 resetLedStateLocked();
1125 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001126}
1127
1128void KeyboardInputMapper::configureParameters() {
1129 mParameters.orientationAware = false;
1130 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
1131 mParameters.orientationAware);
1132
1133 mParameters.associatedDisplayId = mParameters.orientationAware ? 0 : -1;
1134}
1135
1136void KeyboardInputMapper::dumpParameters(String8& dump) {
1137 dump.append(INDENT3 "Parameters:\n");
1138 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1139 mParameters.associatedDisplayId);
1140 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1141 toString(mParameters.orientationAware));
1142}
1143
Jeff Brown6d0fec22010-07-23 21:28:06 -07001144void KeyboardInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001145 for (;;) {
1146 int32_t keyCode, scanCode;
1147 { // acquire lock
1148 AutoMutex _l(mLock);
1149
1150 // Synthesize key up event on reset if keys are currently down.
1151 if (mLocked.keyDowns.isEmpty()) {
1152 initializeLocked();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001153 resetLedStateLocked();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001154 break; // done
1155 }
1156
1157 const KeyDown& keyDown = mLocked.keyDowns.top();
1158 keyCode = keyDown.keyCode;
1159 scanCode = keyDown.scanCode;
1160 } // release lock
1161
Jeff Brown6d0fec22010-07-23 21:28:06 -07001162 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001163 processKey(when, false, keyCode, scanCode, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001164 }
1165
1166 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001167 getContext()->updateGlobalMetaState();
1168}
1169
1170void KeyboardInputMapper::process(const RawEvent* rawEvent) {
1171 switch (rawEvent->type) {
1172 case EV_KEY: {
1173 int32_t scanCode = rawEvent->scanCode;
1174 if (isKeyboardOrGamepadKey(scanCode)) {
1175 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
1176 rawEvent->flags);
1177 }
1178 break;
1179 }
1180 }
1181}
1182
1183bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
1184 return scanCode < BTN_MOUSE
1185 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001186 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08001187 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001188}
1189
Jeff Brown6328cdc2010-07-29 18:18:33 -07001190void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
1191 int32_t scanCode, uint32_t policyFlags) {
1192 int32_t newMetaState;
1193 nsecs_t downTime;
1194 bool metaStateChanged = false;
1195
1196 { // acquire lock
1197 AutoMutex _l(mLock);
1198
1199 if (down) {
1200 // Rotate key codes according to orientation if needed.
1201 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001202 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001203 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001204 if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1205 NULL, NULL, & orientation)) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001206 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001207 }
1208
1209 keyCode = rotateKeyCode(keyCode, orientation);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001210 }
1211
Jeff Brown6328cdc2010-07-29 18:18:33 -07001212 // Add key down.
1213 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
1214 if (keyDownIndex >= 0) {
1215 // key repeat, be sure to use same keycode as before in case of rotation
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001216 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001217 } else {
1218 // key down
Jeff Brownfe508922011-01-18 15:10:10 -08001219 if ((policyFlags & POLICY_FLAG_VIRTUAL)
1220 && mContext->shouldDropVirtualKey(when,
1221 getDevice(), keyCode, scanCode)) {
1222 return;
1223 }
1224
Jeff Brown6328cdc2010-07-29 18:18:33 -07001225 mLocked.keyDowns.push();
1226 KeyDown& keyDown = mLocked.keyDowns.editTop();
1227 keyDown.keyCode = keyCode;
1228 keyDown.scanCode = scanCode;
1229 }
1230
1231 mLocked.downTime = when;
1232 } else {
1233 // Remove key down.
1234 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
1235 if (keyDownIndex >= 0) {
1236 // key up, 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 mLocked.keyDowns.removeAt(size_t(keyDownIndex));
1239 } else {
1240 // key was not actually down
1241 LOGI("Dropping key up from device %s because the key was not down. "
1242 "keyCode=%d, scanCode=%d",
1243 getDeviceName().string(), keyCode, scanCode);
1244 return;
1245 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001246 }
1247
Jeff Brown6328cdc2010-07-29 18:18:33 -07001248 int32_t oldMetaState = mLocked.metaState;
1249 newMetaState = updateMetaState(keyCode, down, oldMetaState);
1250 if (oldMetaState != newMetaState) {
1251 mLocked.metaState = newMetaState;
1252 metaStateChanged = true;
Jeff Brown497a92c2010-09-12 17:55:08 -07001253 updateLedStateLocked(false);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001254 }
Jeff Brownfd0358292010-06-30 16:10:35 -07001255
Jeff Brown6328cdc2010-07-29 18:18:33 -07001256 downTime = mLocked.downTime;
1257 } // release lock
1258
Jeff Brown56194eb2011-03-02 19:23:13 -08001259 // Key down on external an keyboard should wake the device.
1260 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
1261 // For internal keyboards, the key layout file should specify the policy flags for
1262 // each wake key individually.
1263 // TODO: Use the input device configuration to control this behavior more finely.
1264 if (down && getDevice()->isExternal()
1265 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1266 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1267 }
1268
Jeff Brown6328cdc2010-07-29 18:18:33 -07001269 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001270 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001271 }
1272
Jeff Brown05dc66a2011-03-02 14:41:58 -08001273 if (down && !isMetaKey(keyCode)) {
1274 getContext()->fadePointer();
1275 }
1276
Jeff Brownefd32662011-03-08 15:13:06 -08001277 getDispatcher()->notifyKey(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001278 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1279 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001280}
1281
Jeff Brown6328cdc2010-07-29 18:18:33 -07001282ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) {
1283 size_t n = mLocked.keyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001284 for (size_t i = 0; i < n; i++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001285 if (mLocked.keyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001286 return i;
1287 }
1288 }
1289 return -1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001290}
1291
Jeff Brown6d0fec22010-07-23 21:28:06 -07001292int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1293 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1294}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001295
Jeff Brown6d0fec22010-07-23 21:28:06 -07001296int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1297 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1298}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001299
Jeff Brown6d0fec22010-07-23 21:28:06 -07001300bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1301 const int32_t* keyCodes, uint8_t* outFlags) {
1302 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1303}
1304
1305int32_t KeyboardInputMapper::getMetaState() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001306 { // acquire lock
1307 AutoMutex _l(mLock);
1308 return mLocked.metaState;
1309 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001310}
1311
Jeff Brown49ed71d2010-12-06 17:13:33 -08001312void KeyboardInputMapper::resetLedStateLocked() {
1313 initializeLedStateLocked(mLocked.capsLockLedState, LED_CAPSL);
1314 initializeLedStateLocked(mLocked.numLockLedState, LED_NUML);
1315 initializeLedStateLocked(mLocked.scrollLockLedState, LED_SCROLLL);
1316
1317 updateLedStateLocked(true);
1318}
1319
1320void KeyboardInputMapper::initializeLedStateLocked(LockedState::LedState& ledState, int32_t led) {
1321 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
1322 ledState.on = false;
1323}
1324
Jeff Brown497a92c2010-09-12 17:55:08 -07001325void KeyboardInputMapper::updateLedStateLocked(bool reset) {
1326 updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001327 AMETA_CAPS_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001328 updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001329 AMETA_NUM_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001330 updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe752010-10-29 22:19:53 -07001331 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001332}
1333
1334void KeyboardInputMapper::updateLedStateForModifierLocked(LockedState::LedState& ledState,
1335 int32_t led, int32_t modifier, bool reset) {
1336 if (ledState.avail) {
1337 bool desiredState = (mLocked.metaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001338 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001339 getEventHub()->setLedState(getDeviceId(), led, desiredState);
1340 ledState.on = desiredState;
1341 }
1342 }
1343}
1344
Jeff Brown6d0fec22010-07-23 21:28:06 -07001345
Jeff Brown83c09682010-12-23 17:50:18 -08001346// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07001347
Jeff Brown83c09682010-12-23 17:50:18 -08001348CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001349 InputMapper(device) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001350 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001351}
1352
Jeff Brown83c09682010-12-23 17:50:18 -08001353CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001354}
1355
Jeff Brown83c09682010-12-23 17:50:18 -08001356uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001357 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001358}
1359
Jeff Brown83c09682010-12-23 17:50:18 -08001360void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001361 InputMapper::populateDeviceInfo(info);
1362
Jeff Brown83c09682010-12-23 17:50:18 -08001363 if (mParameters.mode == Parameters::MODE_POINTER) {
1364 float minX, minY, maxX, maxY;
1365 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08001366 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
1367 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08001368 }
1369 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08001370 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
1371 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08001372 }
Jeff Brownefd32662011-03-08 15:13:06 -08001373 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001374
1375 if (mHaveVWheel) {
Jeff Brownefd32662011-03-08 15:13:06 -08001376 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001377 }
1378 if (mHaveHWheel) {
Jeff Brownefd32662011-03-08 15:13:06 -08001379 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001380 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001381}
1382
Jeff Brown83c09682010-12-23 17:50:18 -08001383void CursorInputMapper::dump(String8& dump) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07001384 { // acquire lock
1385 AutoMutex _l(mLock);
Jeff Brown83c09682010-12-23 17:50:18 -08001386 dump.append(INDENT2 "Cursor Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001387 dumpParameters(dump);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001388 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
1389 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001390 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1391 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001392 dump.appendFormat(INDENT3 "HaveVWheel: %s\n", toString(mHaveVWheel));
1393 dump.appendFormat(INDENT3 "HaveHWheel: %s\n", toString(mHaveHWheel));
1394 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
1395 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brownefd32662011-03-08 15:13:06 -08001396 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mLocked.buttonState);
1397 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mLocked.buttonState)));
Jeff Brownef3d7e82010-09-30 14:33:04 -07001398 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1399 } // release lock
1400}
1401
Jeff Brown83c09682010-12-23 17:50:18 -08001402void CursorInputMapper::configure() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001403 InputMapper::configure();
1404
1405 // Configure basic parameters.
1406 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08001407
1408 // Configure device mode.
1409 switch (mParameters.mode) {
1410 case Parameters::MODE_POINTER:
Jeff Brownefd32662011-03-08 15:13:06 -08001411 mSource = AINPUT_SOURCE_MOUSE;
Jeff Brown83c09682010-12-23 17:50:18 -08001412 mXPrecision = 1.0f;
1413 mYPrecision = 1.0f;
1414 mXScale = 1.0f;
1415 mYScale = 1.0f;
1416 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
1417 break;
1418 case Parameters::MODE_NAVIGATION:
Jeff Brownefd32662011-03-08 15:13:06 -08001419 mSource = AINPUT_SOURCE_TRACKBALL;
Jeff Brown83c09682010-12-23 17:50:18 -08001420 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1421 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1422 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1423 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1424 break;
1425 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001426
1427 mVWheelScale = 1.0f;
1428 mHWheelScale = 1.0f;
Jeff Browncc0c1592011-02-19 05:07:28 -08001429
1430 mHaveVWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_WHEEL);
1431 mHaveHWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_HWHEEL);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001432}
1433
Jeff Brown83c09682010-12-23 17:50:18 -08001434void CursorInputMapper::configureParameters() {
1435 mParameters.mode = Parameters::MODE_POINTER;
1436 String8 cursorModeString;
1437 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
1438 if (cursorModeString == "navigation") {
1439 mParameters.mode = Parameters::MODE_NAVIGATION;
1440 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
1441 LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
1442 }
1443 }
1444
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001445 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08001446 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001447 mParameters.orientationAware);
1448
Jeff Brown83c09682010-12-23 17:50:18 -08001449 mParameters.associatedDisplayId = mParameters.mode == Parameters::MODE_POINTER
1450 || mParameters.orientationAware ? 0 : -1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001451}
1452
Jeff Brown83c09682010-12-23 17:50:18 -08001453void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001454 dump.append(INDENT3 "Parameters:\n");
1455 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1456 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08001457
1458 switch (mParameters.mode) {
1459 case Parameters::MODE_POINTER:
1460 dump.append(INDENT4 "Mode: pointer\n");
1461 break;
1462 case Parameters::MODE_NAVIGATION:
1463 dump.append(INDENT4 "Mode: navigation\n");
1464 break;
1465 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07001466 LOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08001467 }
1468
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001469 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1470 toString(mParameters.orientationAware));
1471}
1472
Jeff Brown83c09682010-12-23 17:50:18 -08001473void CursorInputMapper::initializeLocked() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001474 mAccumulator.clear();
1475
Jeff Brownefd32662011-03-08 15:13:06 -08001476 mLocked.buttonState = 0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001477 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001478}
1479
Jeff Brown83c09682010-12-23 17:50:18 -08001480void CursorInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001481 for (;;) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001482 int32_t buttonState;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001483 { // acquire lock
1484 AutoMutex _l(mLock);
1485
Jeff Brownefd32662011-03-08 15:13:06 -08001486 buttonState = mLocked.buttonState;
1487 if (!buttonState) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001488 initializeLocked();
1489 break; // done
1490 }
1491 } // release lock
1492
Jeff Brown83c09682010-12-23 17:50:18 -08001493 // Synthesize button up event on reset.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001494 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownefd32662011-03-08 15:13:06 -08001495 mAccumulator.clear();
1496 mAccumulator.buttonDown = 0;
1497 mAccumulator.buttonUp = buttonState;
1498 mAccumulator.fields = Accumulator::FIELD_BUTTONS;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001499 sync(when);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001500 }
1501
Jeff Brown6d0fec22010-07-23 21:28:06 -07001502 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001503}
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001504
Jeff Brown83c09682010-12-23 17:50:18 -08001505void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001506 switch (rawEvent->type) {
Jeff Brownefd32662011-03-08 15:13:06 -08001507 case EV_KEY: {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001508 int32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode);
Jeff Brownefd32662011-03-08 15:13:06 -08001509 if (buttonState) {
1510 if (rawEvent->value) {
1511 mAccumulator.buttonDown = buttonState;
1512 mAccumulator.buttonUp = 0;
1513 } else {
1514 mAccumulator.buttonDown = 0;
1515 mAccumulator.buttonUp = buttonState;
1516 }
1517 mAccumulator.fields |= Accumulator::FIELD_BUTTONS;
1518
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001519 // Sync now since BTN_MOUSE is not necessarily followed by SYN_REPORT and
1520 // we need to ensure that we report the up/down promptly.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001521 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001522 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001523 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001524 break;
Jeff Brownefd32662011-03-08 15:13:06 -08001525 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001526
Jeff Brown6d0fec22010-07-23 21:28:06 -07001527 case EV_REL:
1528 switch (rawEvent->scanCode) {
1529 case REL_X:
1530 mAccumulator.fields |= Accumulator::FIELD_REL_X;
1531 mAccumulator.relX = rawEvent->value;
1532 break;
1533 case REL_Y:
1534 mAccumulator.fields |= Accumulator::FIELD_REL_Y;
1535 mAccumulator.relY = rawEvent->value;
1536 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001537 case REL_WHEEL:
1538 mAccumulator.fields |= Accumulator::FIELD_REL_WHEEL;
1539 mAccumulator.relWheel = rawEvent->value;
1540 break;
1541 case REL_HWHEEL:
1542 mAccumulator.fields |= Accumulator::FIELD_REL_HWHEEL;
1543 mAccumulator.relHWheel = rawEvent->value;
1544 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001545 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001546 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001547
Jeff Brown6d0fec22010-07-23 21:28:06 -07001548 case EV_SYN:
1549 switch (rawEvent->scanCode) {
1550 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001551 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001552 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001553 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001554 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001555 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001556}
1557
Jeff Brown83c09682010-12-23 17:50:18 -08001558void CursorInputMapper::sync(nsecs_t when) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001559 uint32_t fields = mAccumulator.fields;
1560 if (fields == 0) {
1561 return; // no new state changes, so nothing to do
1562 }
1563
Jeff Brown9626b142011-03-03 02:09:54 -08001564 int32_t motionEventAction;
1565 int32_t motionEventEdgeFlags;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001566 int32_t lastButtonState, currentButtonState;
1567 PointerProperties pointerProperties;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001568 PointerCoords pointerCoords;
1569 nsecs_t downTime;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001570 float vscroll, hscroll;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001571 { // acquire lock
1572 AutoMutex _l(mLock);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001573
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001574 lastButtonState = mLocked.buttonState;
1575
Jeff Brownefd32662011-03-08 15:13:06 -08001576 bool down, downChanged;
1577 bool wasDown = isPointerDown(mLocked.buttonState);
1578 bool buttonsChanged = fields & Accumulator::FIELD_BUTTONS;
1579 if (buttonsChanged) {
1580 mLocked.buttonState = (mLocked.buttonState | mAccumulator.buttonDown)
1581 & ~mAccumulator.buttonUp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001582
Jeff Brownefd32662011-03-08 15:13:06 -08001583 down = isPointerDown(mLocked.buttonState);
1584
1585 if (!wasDown && down) {
1586 mLocked.downTime = when;
1587 downChanged = true;
1588 } else if (wasDown && !down) {
1589 downChanged = true;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001590 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08001591 downChanged = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001592 }
Jeff Brownefd32662011-03-08 15:13:06 -08001593 } else {
1594 down = wasDown;
1595 downChanged = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001596 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001597
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001598 currentButtonState = mLocked.buttonState;
1599
Jeff Brown6328cdc2010-07-29 18:18:33 -07001600 downTime = mLocked.downTime;
Jeff Brown83c09682010-12-23 17:50:18 -08001601 float deltaX = fields & Accumulator::FIELD_REL_X ? mAccumulator.relX * mXScale : 0.0f;
1602 float deltaY = fields & Accumulator::FIELD_REL_Y ? mAccumulator.relY * mYScale : 0.0f;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001603
Jeff Brown6328cdc2010-07-29 18:18:33 -07001604 if (downChanged) {
Jeff Brownefd32662011-03-08 15:13:06 -08001605 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
1606 } else if (down || mPointerController == NULL) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001607 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browncc0c1592011-02-19 05:07:28 -08001608 } else {
1609 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001610 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001611
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001612 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
Jeff Brown83c09682010-12-23 17:50:18 -08001613 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001614 // Rotate motion based on display orientation if needed.
1615 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1616 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001617 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1618 NULL, NULL, & orientation)) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001619 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001620 }
1621
1622 float temp;
1623 switch (orientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001624 case DISPLAY_ORIENTATION_90:
Jeff Brown83c09682010-12-23 17:50:18 -08001625 temp = deltaX;
1626 deltaX = deltaY;
1627 deltaY = -temp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001628 break;
1629
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001630 case DISPLAY_ORIENTATION_180:
Jeff Brown83c09682010-12-23 17:50:18 -08001631 deltaX = -deltaX;
1632 deltaY = -deltaY;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001633 break;
1634
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001635 case DISPLAY_ORIENTATION_270:
Jeff Brown83c09682010-12-23 17:50:18 -08001636 temp = deltaX;
1637 deltaX = -deltaY;
1638 deltaY = temp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001639 break;
1640 }
1641 }
Jeff Brown83c09682010-12-23 17:50:18 -08001642
Jeff Brown9626b142011-03-03 02:09:54 -08001643 motionEventEdgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
1644
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001645 pointerProperties.clear();
1646 pointerProperties.id = 0;
1647 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
1648
1649 pointerCoords.clear();
1650
Jeff Brown2352b972011-04-12 22:39:53 -07001651 if (mHaveVWheel && (fields & Accumulator::FIELD_REL_WHEEL)) {
1652 vscroll = mAccumulator.relWheel;
1653 } else {
1654 vscroll = 0;
1655 }
1656 if (mHaveHWheel && (fields & Accumulator::FIELD_REL_HWHEEL)) {
1657 hscroll = mAccumulator.relHWheel;
1658 } else {
1659 hscroll = 0;
1660 }
1661
Jeff Brown83c09682010-12-23 17:50:18 -08001662 if (mPointerController != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -07001663 if (deltaX != 0 || deltaY != 0 || vscroll != 0 || hscroll != 0
1664 || buttonsChanged) {
1665 mPointerController->setPresentation(
1666 PointerControllerInterface::PRESENTATION_POINTER);
1667
1668 if (deltaX != 0 || deltaY != 0) {
1669 mPointerController->move(deltaX, deltaY);
1670 }
1671
1672 if (buttonsChanged) {
1673 mPointerController->setButtonState(mLocked.buttonState);
1674 }
1675
1676 mPointerController->unfade();
Jeff Brown83c09682010-12-23 17:50:18 -08001677 }
Jeff Brownefd32662011-03-08 15:13:06 -08001678
Jeff Brown91c69ab2011-02-14 17:03:18 -08001679 float x, y;
1680 mPointerController->getPosition(&x, &y);
Jeff Brownebbd5d12011-02-17 13:01:34 -08001681 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
1682 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown9626b142011-03-03 02:09:54 -08001683
1684 if (motionEventAction == AMOTION_EVENT_ACTION_DOWN) {
Jeff Brownefd32662011-03-08 15:13:06 -08001685 motionEventEdgeFlags = calculateEdgeFlagsUsingPointerBounds(
1686 mPointerController, x, y);
Jeff Brown9626b142011-03-03 02:09:54 -08001687 }
Jeff Brown83c09682010-12-23 17:50:18 -08001688 } else {
Jeff Brownebbd5d12011-02-17 13:01:34 -08001689 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
1690 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
Jeff Brown83c09682010-12-23 17:50:18 -08001691 }
1692
Jeff Brownefd32662011-03-08 15:13:06 -08001693 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001694 } // release lock
1695
Jeff Brown56194eb2011-03-02 19:23:13 -08001696 // Moving an external trackball or mouse should wake the device.
1697 // We don't do this for internal cursor devices to prevent them from waking up
1698 // the device in your pocket.
1699 // TODO: Use the input device configuration to control this behavior more finely.
1700 uint32_t policyFlags = 0;
1701 if (getDevice()->isExternal()) {
1702 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1703 }
1704
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001705 // Synthesize key down from buttons if needed.
1706 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
1707 policyFlags, lastButtonState, currentButtonState);
1708
1709 // Send motion event.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001710 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownefd32662011-03-08 15:13:06 -08001711 getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001712 motionEventAction, 0, metaState, currentButtonState, motionEventEdgeFlags,
1713 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownb6997262010-10-08 22:31:17 -07001714
Jeff Browna032cc02011-03-07 16:56:21 -08001715 // Send hover move after UP to tell the application that the mouse is hovering now.
1716 if (motionEventAction == AMOTION_EVENT_ACTION_UP
1717 && mPointerController != NULL) {
1718 getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001719 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
1720 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
1721 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Browna032cc02011-03-07 16:56:21 -08001722 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001723
Jeff Browna032cc02011-03-07 16:56:21 -08001724 // Send scroll events.
Jeff Brown33bbfd22011-02-24 20:55:35 -08001725 if (vscroll != 0 || hscroll != 0) {
1726 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
1727 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
1728
Jeff Brownefd32662011-03-08 15:13:06 -08001729 getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001730 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
1731 AMOTION_EVENT_EDGE_FLAG_NONE,
1732 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brown33bbfd22011-02-24 20:55:35 -08001733 }
Jeff Browna032cc02011-03-07 16:56:21 -08001734
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001735 // Synthesize key up from buttons if needed.
1736 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
1737 policyFlags, lastButtonState, currentButtonState);
1738
Jeff Browna032cc02011-03-07 16:56:21 -08001739 mAccumulator.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001740}
1741
Jeff Brown83c09682010-12-23 17:50:18 -08001742int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07001743 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
1744 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1745 } else {
1746 return AKEY_STATE_UNKNOWN;
1747 }
1748}
1749
Jeff Brown05dc66a2011-03-02 14:41:58 -08001750void CursorInputMapper::fadePointer() {
1751 { // acquire lock
1752 AutoMutex _l(mLock);
Jeff Brownefd32662011-03-08 15:13:06 -08001753 if (mPointerController != NULL) {
1754 mPointerController->fade();
1755 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08001756 } // release lock
1757}
1758
Jeff Brown6d0fec22010-07-23 21:28:06 -07001759
1760// --- TouchInputMapper ---
1761
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001762TouchInputMapper::TouchInputMapper(InputDevice* device) :
1763 InputMapper(device) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001764 mLocked.surfaceOrientation = -1;
1765 mLocked.surfaceWidth = -1;
1766 mLocked.surfaceHeight = -1;
1767
1768 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001769}
1770
1771TouchInputMapper::~TouchInputMapper() {
1772}
1773
1774uint32_t TouchInputMapper::getSources() {
Jeff Brownace13b12011-03-09 17:39:48 -08001775 return mTouchSource | mPointerSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001776}
1777
1778void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1779 InputMapper::populateDeviceInfo(info);
1780
Jeff Brown6328cdc2010-07-29 18:18:33 -07001781 { // acquire lock
1782 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001783
Jeff Brown6328cdc2010-07-29 18:18:33 -07001784 // Ensure surface information is up to date so that orientation changes are
1785 // noticed immediately.
Jeff Brownefd32662011-03-08 15:13:06 -08001786 if (!configureSurfaceLocked()) {
1787 return;
1788 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001789
Jeff Brownefd32662011-03-08 15:13:06 -08001790 info->addMotionRange(mLocked.orientedRanges.x);
1791 info->addMotionRange(mLocked.orientedRanges.y);
Jeff Brown8d608662010-08-30 03:02:23 -07001792
1793 if (mLocked.orientedRanges.havePressure) {
Jeff Brownefd32662011-03-08 15:13:06 -08001794 info->addMotionRange(mLocked.orientedRanges.pressure);
Jeff Brown8d608662010-08-30 03:02:23 -07001795 }
1796
1797 if (mLocked.orientedRanges.haveSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08001798 info->addMotionRange(mLocked.orientedRanges.size);
Jeff Brown8d608662010-08-30 03:02:23 -07001799 }
1800
Jeff Brownc6d282b2010-10-14 21:42:15 -07001801 if (mLocked.orientedRanges.haveTouchSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08001802 info->addMotionRange(mLocked.orientedRanges.touchMajor);
1803 info->addMotionRange(mLocked.orientedRanges.touchMinor);
Jeff Brown8d608662010-08-30 03:02:23 -07001804 }
1805
Jeff Brownc6d282b2010-10-14 21:42:15 -07001806 if (mLocked.orientedRanges.haveToolSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08001807 info->addMotionRange(mLocked.orientedRanges.toolMajor);
1808 info->addMotionRange(mLocked.orientedRanges.toolMinor);
Jeff Brown8d608662010-08-30 03:02:23 -07001809 }
1810
1811 if (mLocked.orientedRanges.haveOrientation) {
Jeff Brownefd32662011-03-08 15:13:06 -08001812 info->addMotionRange(mLocked.orientedRanges.orientation);
Jeff Brown8d608662010-08-30 03:02:23 -07001813 }
Jeff Brownace13b12011-03-09 17:39:48 -08001814
1815 if (mPointerController != NULL) {
1816 float minX, minY, maxX, maxY;
1817 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
1818 info->addMotionRange(AMOTION_EVENT_AXIS_X, mPointerSource,
1819 minX, maxX, 0.0f, 0.0f);
1820 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mPointerSource,
1821 minY, maxY, 0.0f, 0.0f);
1822 }
1823 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mPointerSource,
1824 0.0f, 1.0f, 0.0f, 0.0f);
1825 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001826 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001827}
1828
Jeff Brownef3d7e82010-09-30 14:33:04 -07001829void TouchInputMapper::dump(String8& dump) {
1830 { // acquire lock
1831 AutoMutex _l(mLock);
1832 dump.append(INDENT2 "Touch Input Mapper:\n");
Jeff Brownef3d7e82010-09-30 14:33:04 -07001833 dumpParameters(dump);
1834 dumpVirtualKeysLocked(dump);
1835 dumpRawAxes(dump);
1836 dumpCalibration(dump);
1837 dumpSurfaceLocked(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08001838
Jeff Brown511ee5f2010-10-18 13:32:20 -07001839 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
Jeff Brownc6d282b2010-10-14 21:42:15 -07001840 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mLocked.xScale);
1841 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mLocked.yScale);
1842 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mLocked.xPrecision);
1843 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mLocked.yPrecision);
1844 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mLocked.geometricScale);
1845 dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mLocked.toolSizeLinearScale);
1846 dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mLocked.toolSizeLinearBias);
1847 dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mLocked.toolSizeAreaScale);
1848 dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mLocked.toolSizeAreaBias);
1849 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mLocked.pressureScale);
1850 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mLocked.sizeScale);
Jeff Brownefd32662011-03-08 15:13:06 -08001851 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mLocked.orientationScale);
1852
1853 dump.appendFormat(INDENT3 "Last Touch:\n");
1854 dump.appendFormat(INDENT4 "Pointer Count: %d\n", mLastTouch.pointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08001855 dump.appendFormat(INDENT4 "Button State: 0x%08x\n", mLastTouch.buttonState);
1856
1857 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
1858 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
1859 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
1860 mLocked.pointerGestureXMovementScale);
1861 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
1862 mLocked.pointerGestureYMovementScale);
1863 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
1864 mLocked.pointerGestureXZoomScale);
1865 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
1866 mLocked.pointerGestureYZoomScale);
Jeff Brown2352b972011-04-12 22:39:53 -07001867 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
1868 mLocked.pointerGestureMaxSwipeWidth);
Jeff Brownace13b12011-03-09 17:39:48 -08001869 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07001870 } // release lock
1871}
1872
Jeff Brown6328cdc2010-07-29 18:18:33 -07001873void TouchInputMapper::initializeLocked() {
1874 mCurrentTouch.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001875 mLastTouch.clear();
1876 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001877
1878 for (uint32_t i = 0; i < MAX_POINTERS; i++) {
1879 mAveragingTouchFilter.historyStart[i] = 0;
1880 mAveragingTouchFilter.historyEnd[i] = 0;
1881 }
1882
1883 mJumpyTouchFilter.jumpyPointsDropped = 0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001884
1885 mLocked.currentVirtualKey.down = false;
Jeff Brown8d608662010-08-30 03:02:23 -07001886
1887 mLocked.orientedRanges.havePressure = false;
1888 mLocked.orientedRanges.haveSize = false;
Jeff Brownc6d282b2010-10-14 21:42:15 -07001889 mLocked.orientedRanges.haveTouchSize = false;
1890 mLocked.orientedRanges.haveToolSize = false;
Jeff Brown8d608662010-08-30 03:02:23 -07001891 mLocked.orientedRanges.haveOrientation = false;
Jeff Brownace13b12011-03-09 17:39:48 -08001892
1893 mPointerGesture.reset();
Jeff Brown8d608662010-08-30 03:02:23 -07001894}
1895
Jeff Brown6d0fec22010-07-23 21:28:06 -07001896void TouchInputMapper::configure() {
1897 InputMapper::configure();
1898
1899 // Configure basic parameters.
Jeff Brown8d608662010-08-30 03:02:23 -07001900 configureParameters();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001901
Jeff Brown83c09682010-12-23 17:50:18 -08001902 // Configure sources.
1903 switch (mParameters.deviceType) {
1904 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
Jeff Brownefd32662011-03-08 15:13:06 -08001905 mTouchSource = AINPUT_SOURCE_TOUCHSCREEN;
Jeff Brownace13b12011-03-09 17:39:48 -08001906 mPointerSource = 0;
Jeff Brown83c09682010-12-23 17:50:18 -08001907 break;
1908 case Parameters::DEVICE_TYPE_TOUCH_PAD:
Jeff Brownefd32662011-03-08 15:13:06 -08001909 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
Jeff Brownace13b12011-03-09 17:39:48 -08001910 mPointerSource = 0;
1911 break;
1912 case Parameters::DEVICE_TYPE_POINTER:
1913 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
1914 mPointerSource = AINPUT_SOURCE_MOUSE;
Jeff Brown83c09682010-12-23 17:50:18 -08001915 break;
1916 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07001917 LOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08001918 }
1919
Jeff Brown6d0fec22010-07-23 21:28:06 -07001920 // Configure absolute axis information.
Jeff Brown8d608662010-08-30 03:02:23 -07001921 configureRawAxes();
Jeff Brown8d608662010-08-30 03:02:23 -07001922
1923 // Prepare input device calibration.
1924 parseCalibration();
1925 resolveCalibration();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001926
Jeff Brown6328cdc2010-07-29 18:18:33 -07001927 { // acquire lock
1928 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001929
Jeff Brown8d608662010-08-30 03:02:23 -07001930 // Configure surface dimensions and orientation.
Jeff Brown6328cdc2010-07-29 18:18:33 -07001931 configureSurfaceLocked();
1932 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001933}
1934
Jeff Brown8d608662010-08-30 03:02:23 -07001935void TouchInputMapper::configureParameters() {
1936 mParameters.useBadTouchFilter = getPolicy()->filterTouchEvents();
1937 mParameters.useAveragingTouchFilter = getPolicy()->filterTouchEvents();
1938 mParameters.useJumpyTouchFilter = getPolicy()->filterJumpyTouchEvents();
Jeff Brownfe508922011-01-18 15:10:10 -08001939 mParameters.virtualKeyQuietTime = getPolicy()->getVirtualKeyQuietTime();
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001940
Jeff Brown2352b972011-04-12 22:39:53 -07001941 // TODO: Make this configurable.
1942 //mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
1943 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
1944
Jeff Brownace13b12011-03-09 17:39:48 -08001945 if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
1946 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
1947 // The device is a cursor device with a touch pad attached.
1948 // By default don't use the touch pad to move the pointer.
1949 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
1950 } else {
1951 // The device is just a touch pad.
1952 // By default use the touch pad to move the pointer and to perform related gestures.
1953 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
1954 }
1955
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001956 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001957 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
1958 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08001959 if (deviceTypeString == "touchScreen") {
1960 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08001961 } else if (deviceTypeString == "touchPad") {
1962 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08001963 } else if (deviceTypeString == "pointer") {
1964 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brownefd32662011-03-08 15:13:06 -08001965 } else {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001966 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
1967 }
1968 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001969
Jeff Brownefd32662011-03-08 15:13:06 -08001970 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001971 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
1972 mParameters.orientationAware);
1973
Jeff Brownefd32662011-03-08 15:13:06 -08001974 mParameters.associatedDisplayId = mParameters.orientationAware
1975 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownace13b12011-03-09 17:39:48 -08001976 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
Jeff Brownefd32662011-03-08 15:13:06 -08001977 ? 0 : -1;
Jeff Brown8d608662010-08-30 03:02:23 -07001978}
1979
Jeff Brownef3d7e82010-09-30 14:33:04 -07001980void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001981 dump.append(INDENT3 "Parameters:\n");
1982
1983 switch (mParameters.deviceType) {
1984 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
1985 dump.append(INDENT4 "DeviceType: touchScreen\n");
1986 break;
1987 case Parameters::DEVICE_TYPE_TOUCH_PAD:
1988 dump.append(INDENT4 "DeviceType: touchPad\n");
1989 break;
Jeff Brownace13b12011-03-09 17:39:48 -08001990 case Parameters::DEVICE_TYPE_POINTER:
1991 dump.append(INDENT4 "DeviceType: pointer\n");
1992 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001993 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07001994 LOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001995 }
1996
1997 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1998 mParameters.associatedDisplayId);
1999 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2000 toString(mParameters.orientationAware));
2001
2002 dump.appendFormat(INDENT4 "UseBadTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002003 toString(mParameters.useBadTouchFilter));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002004 dump.appendFormat(INDENT4 "UseAveragingTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002005 toString(mParameters.useAveragingTouchFilter));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002006 dump.appendFormat(INDENT4 "UseJumpyTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002007 toString(mParameters.useJumpyTouchFilter));
Jeff Brownb88102f2010-09-08 11:49:43 -07002008}
2009
Jeff Brown8d608662010-08-30 03:02:23 -07002010void TouchInputMapper::configureRawAxes() {
2011 mRawAxes.x.clear();
2012 mRawAxes.y.clear();
2013 mRawAxes.pressure.clear();
2014 mRawAxes.touchMajor.clear();
2015 mRawAxes.touchMinor.clear();
2016 mRawAxes.toolMajor.clear();
2017 mRawAxes.toolMinor.clear();
2018 mRawAxes.orientation.clear();
2019}
2020
Jeff Brownef3d7e82010-09-30 14:33:04 -07002021void TouchInputMapper::dumpRawAxes(String8& dump) {
2022 dump.append(INDENT3 "Raw Axes:\n");
Jeff Browncb1404e2011-01-15 18:14:15 -08002023 dumpRawAbsoluteAxisInfo(dump, mRawAxes.x, "X");
2024 dumpRawAbsoluteAxisInfo(dump, mRawAxes.y, "Y");
2025 dumpRawAbsoluteAxisInfo(dump, mRawAxes.pressure, "Pressure");
2026 dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor");
2027 dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor");
2028 dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor");
2029 dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor");
2030 dumpRawAbsoluteAxisInfo(dump, mRawAxes.orientation, "Orientation");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002031}
2032
Jeff Brown6328cdc2010-07-29 18:18:33 -07002033bool TouchInputMapper::configureSurfaceLocked() {
Jeff Brown9626b142011-03-03 02:09:54 -08002034 // Ensure we have valid X and Y axes.
2035 if (!mRawAxes.x.valid || !mRawAxes.y.valid) {
2036 LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
2037 "The device will be inoperable.", getDeviceName().string());
2038 return false;
2039 }
2040
Jeff Brown6d0fec22010-07-23 21:28:06 -07002041 // Update orientation and dimensions if needed.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002042 int32_t orientation = DISPLAY_ORIENTATION_0;
Jeff Brown9626b142011-03-03 02:09:54 -08002043 int32_t width = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2044 int32_t height = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002045
2046 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002047 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002048 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownefd32662011-03-08 15:13:06 -08002049 &mLocked.associatedDisplayWidth, &mLocked.associatedDisplayHeight,
2050 &mLocked.associatedDisplayOrientation)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002051 return false;
2052 }
Jeff Brownefd32662011-03-08 15:13:06 -08002053
2054 // A touch screen inherits the dimensions of the display.
2055 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
2056 width = mLocked.associatedDisplayWidth;
2057 height = mLocked.associatedDisplayHeight;
2058 }
2059
2060 // The device inherits the orientation of the display if it is orientation aware.
2061 if (mParameters.orientationAware) {
2062 orientation = mLocked.associatedDisplayOrientation;
2063 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002064 }
2065
Jeff Brownace13b12011-03-09 17:39:48 -08002066 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2067 && mPointerController == NULL) {
2068 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2069 }
2070
Jeff Brown6328cdc2010-07-29 18:18:33 -07002071 bool orientationChanged = mLocked.surfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002072 if (orientationChanged) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002073 mLocked.surfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002074 }
2075
Jeff Brown6328cdc2010-07-29 18:18:33 -07002076 bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002077 if (sizeChanged) {
Jeff Brownefd32662011-03-08 15:13:06 -08002078 LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002079 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown8d608662010-08-30 03:02:23 -07002080
Jeff Brown6328cdc2010-07-29 18:18:33 -07002081 mLocked.surfaceWidth = width;
2082 mLocked.surfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002083
Jeff Brown8d608662010-08-30 03:02:23 -07002084 // Configure X and Y factors.
Jeff Brown9626b142011-03-03 02:09:54 -08002085 mLocked.xScale = float(width) / (mRawAxes.x.maxValue - mRawAxes.x.minValue + 1);
2086 mLocked.yScale = float(height) / (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1);
2087 mLocked.xPrecision = 1.0f / mLocked.xScale;
2088 mLocked.yPrecision = 1.0f / mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002089
Jeff Brownefd32662011-03-08 15:13:06 -08002090 mLocked.orientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
2091 mLocked.orientedRanges.x.source = mTouchSource;
2092 mLocked.orientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
2093 mLocked.orientedRanges.y.source = mTouchSource;
2094
Jeff Brown9626b142011-03-03 02:09:54 -08002095 configureVirtualKeysLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002096
Jeff Brown8d608662010-08-30 03:02:23 -07002097 // Scale factor for terms that are not oriented in a particular axis.
2098 // If the pixels are square then xScale == yScale otherwise we fake it
2099 // by choosing an average.
2100 mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002101
Jeff Brown8d608662010-08-30 03:02:23 -07002102 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002103 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002104
Jeff Brown8d608662010-08-30 03:02:23 -07002105 // TouchMajor and TouchMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002106 if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) {
2107 mLocked.orientedRanges.haveTouchSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002108
2109 mLocked.orientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
2110 mLocked.orientedRanges.touchMajor.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002111 mLocked.orientedRanges.touchMajor.min = 0;
2112 mLocked.orientedRanges.touchMajor.max = diagonalSize;
2113 mLocked.orientedRanges.touchMajor.flat = 0;
2114 mLocked.orientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002115
Jeff Brown8d608662010-08-30 03:02:23 -07002116 mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor;
Jeff Brownefd32662011-03-08 15:13:06 -08002117 mLocked.orientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002118 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002119
Jeff Brown8d608662010-08-30 03:02:23 -07002120 // ToolMajor and ToolMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002121 mLocked.toolSizeLinearScale = 0;
2122 mLocked.toolSizeLinearBias = 0;
2123 mLocked.toolSizeAreaScale = 0;
2124 mLocked.toolSizeAreaBias = 0;
2125 if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
2126 if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) {
2127 if (mCalibration.haveToolSizeLinearScale) {
2128 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
Jeff Brown8d608662010-08-30 03:02:23 -07002129 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002130 mLocked.toolSizeLinearScale = float(min(width, height))
Jeff Brown8d608662010-08-30 03:02:23 -07002131 / mRawAxes.toolMajor.maxValue;
2132 }
2133
Jeff Brownc6d282b2010-10-14 21:42:15 -07002134 if (mCalibration.haveToolSizeLinearBias) {
2135 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
2136 }
2137 } else if (mCalibration.toolSizeCalibration ==
2138 Calibration::TOOL_SIZE_CALIBRATION_AREA) {
2139 if (mCalibration.haveToolSizeLinearScale) {
2140 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
2141 } else {
2142 mLocked.toolSizeLinearScale = min(width, height);
2143 }
2144
2145 if (mCalibration.haveToolSizeLinearBias) {
2146 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
2147 }
2148
2149 if (mCalibration.haveToolSizeAreaScale) {
2150 mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale;
2151 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
2152 mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue;
2153 }
2154
2155 if (mCalibration.haveToolSizeAreaBias) {
2156 mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -07002157 }
2158 }
2159
Jeff Brownc6d282b2010-10-14 21:42:15 -07002160 mLocked.orientedRanges.haveToolSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002161
2162 mLocked.orientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
2163 mLocked.orientedRanges.toolMajor.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002164 mLocked.orientedRanges.toolMajor.min = 0;
2165 mLocked.orientedRanges.toolMajor.max = diagonalSize;
2166 mLocked.orientedRanges.toolMajor.flat = 0;
2167 mLocked.orientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002168
Jeff Brown8d608662010-08-30 03:02:23 -07002169 mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor;
Jeff Brownefd32662011-03-08 15:13:06 -08002170 mLocked.orientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002171 }
2172
2173 // Pressure factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002174 mLocked.pressureScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002175 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
2176 RawAbsoluteAxisInfo rawPressureAxis;
2177 switch (mCalibration.pressureSource) {
2178 case Calibration::PRESSURE_SOURCE_PRESSURE:
2179 rawPressureAxis = mRawAxes.pressure;
2180 break;
2181 case Calibration::PRESSURE_SOURCE_TOUCH:
2182 rawPressureAxis = mRawAxes.touchMajor;
2183 break;
2184 default:
2185 rawPressureAxis.clear();
2186 }
2187
Jeff Brown8d608662010-08-30 03:02:23 -07002188 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2189 || mCalibration.pressureCalibration
2190 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2191 if (mCalibration.havePressureScale) {
2192 mLocked.pressureScale = mCalibration.pressureScale;
2193 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
2194 mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue;
2195 }
2196 }
2197
2198 mLocked.orientedRanges.havePressure = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002199
2200 mLocked.orientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2201 mLocked.orientedRanges.pressure.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002202 mLocked.orientedRanges.pressure.min = 0;
2203 mLocked.orientedRanges.pressure.max = 1.0;
2204 mLocked.orientedRanges.pressure.flat = 0;
2205 mLocked.orientedRanges.pressure.fuzz = 0;
2206 }
2207
2208 // Size factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002209 mLocked.sizeScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002210 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002211 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
2212 if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
2213 mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue;
2214 }
2215 }
2216
2217 mLocked.orientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002218
2219 mLocked.orientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
2220 mLocked.orientedRanges.size.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002221 mLocked.orientedRanges.size.min = 0;
2222 mLocked.orientedRanges.size.max = 1.0;
2223 mLocked.orientedRanges.size.flat = 0;
2224 mLocked.orientedRanges.size.fuzz = 0;
2225 }
2226
2227 // Orientation
Jeff Brownc6d282b2010-10-14 21:42:15 -07002228 mLocked.orientationScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002229 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002230 if (mCalibration.orientationCalibration
2231 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
2232 if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) {
2233 mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue;
2234 }
2235 }
2236
Jeff Brownefd32662011-03-08 15:13:06 -08002237 mLocked.orientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2238 mLocked.orientedRanges.orientation.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002239 mLocked.orientedRanges.orientation.min = - M_PI_2;
2240 mLocked.orientedRanges.orientation.max = M_PI_2;
2241 mLocked.orientedRanges.orientation.flat = 0;
2242 mLocked.orientedRanges.orientation.fuzz = 0;
2243 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002244 }
2245
2246 if (orientationChanged || sizeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002247 // Compute oriented surface dimensions, precision, scales and ranges.
2248 // Note that the maximum value reported is an inclusive maximum value so it is one
2249 // unit less than the total width or height of surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002250 switch (mLocked.surfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002251 case DISPLAY_ORIENTATION_90:
2252 case DISPLAY_ORIENTATION_270:
Jeff Brown6328cdc2010-07-29 18:18:33 -07002253 mLocked.orientedSurfaceWidth = mLocked.surfaceHeight;
2254 mLocked.orientedSurfaceHeight = mLocked.surfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002255
Jeff Brown6328cdc2010-07-29 18:18:33 -07002256 mLocked.orientedXPrecision = mLocked.yPrecision;
2257 mLocked.orientedYPrecision = mLocked.xPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002258
2259 mLocked.orientedRanges.x.min = 0;
2260 mLocked.orientedRanges.x.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue)
2261 * mLocked.yScale;
2262 mLocked.orientedRanges.x.flat = 0;
2263 mLocked.orientedRanges.x.fuzz = mLocked.yScale;
2264
2265 mLocked.orientedRanges.y.min = 0;
2266 mLocked.orientedRanges.y.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue)
2267 * mLocked.xScale;
2268 mLocked.orientedRanges.y.flat = 0;
2269 mLocked.orientedRanges.y.fuzz = mLocked.xScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002270 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002271
Jeff Brown6d0fec22010-07-23 21:28:06 -07002272 default:
Jeff Brown6328cdc2010-07-29 18:18:33 -07002273 mLocked.orientedSurfaceWidth = mLocked.surfaceWidth;
2274 mLocked.orientedSurfaceHeight = mLocked.surfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002275
Jeff Brown6328cdc2010-07-29 18:18:33 -07002276 mLocked.orientedXPrecision = mLocked.xPrecision;
2277 mLocked.orientedYPrecision = mLocked.yPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002278
2279 mLocked.orientedRanges.x.min = 0;
2280 mLocked.orientedRanges.x.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue)
2281 * mLocked.xScale;
2282 mLocked.orientedRanges.x.flat = 0;
2283 mLocked.orientedRanges.x.fuzz = mLocked.xScale;
2284
2285 mLocked.orientedRanges.y.min = 0;
2286 mLocked.orientedRanges.y.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue)
2287 * mLocked.yScale;
2288 mLocked.orientedRanges.y.flat = 0;
2289 mLocked.orientedRanges.y.fuzz = mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002290 break;
2291 }
Jeff Brownace13b12011-03-09 17:39:48 -08002292
2293 // Compute pointer gesture detection parameters.
2294 // TODO: These factors should not be hardcoded.
2295 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2296 int32_t rawWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2297 int32_t rawHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07002298 float rawDiagonal = hypotf(rawWidth, rawHeight);
2299 float displayDiagonal = hypotf(mLocked.associatedDisplayWidth,
2300 mLocked.associatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08002301
Jeff Brown2352b972011-04-12 22:39:53 -07002302 // Scale movements such that one whole swipe of the touch pad covers a
2303 // given area relative to the diagonal size of the display.
Jeff Brownace13b12011-03-09 17:39:48 -08002304 // Assume that the touch pad has a square aspect ratio such that movements in
2305 // X and Y of the same number of raw units cover the same physical distance.
2306 const float scaleFactor = 0.8f;
2307
Jeff Brown2352b972011-04-12 22:39:53 -07002308 mLocked.pointerGestureXMovementScale = GESTURE_MOVEMENT_SPEED_RATIO
2309 * displayDiagonal / rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08002310 mLocked.pointerGestureYMovementScale = mLocked.pointerGestureXMovementScale;
2311
2312 // Scale zooms to cover a smaller range of the display than movements do.
2313 // This value determines the area around the pointer that is affected by freeform
2314 // pointer gestures.
Jeff Brown2352b972011-04-12 22:39:53 -07002315 mLocked.pointerGestureXZoomScale = GESTURE_ZOOM_SPEED_RATIO
2316 * displayDiagonal / rawDiagonal;
2317 mLocked.pointerGestureYZoomScale = mLocked.pointerGestureXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002318
Jeff Brown2352b972011-04-12 22:39:53 -07002319 // Max width between pointers to detect a swipe gesture is more than some fraction
2320 // of the diagonal axis of the touch pad. Touches that are wider than this are
2321 // translated into freeform gestures.
2322 mLocked.pointerGestureMaxSwipeWidth = SWIPE_MAX_WIDTH_RATIO * rawDiagonal;
2323
2324 // Reset the current pointer gesture.
2325 mPointerGesture.reset();
2326
2327 // Remove any current spots.
2328 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
2329 mPointerController->clearSpots();
2330 }
Jeff Brownace13b12011-03-09 17:39:48 -08002331 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002332 }
2333
2334 return true;
2335}
2336
Jeff Brownef3d7e82010-09-30 14:33:04 -07002337void TouchInputMapper::dumpSurfaceLocked(String8& dump) {
2338 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth);
2339 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight);
2340 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07002341}
2342
Jeff Brown6328cdc2010-07-29 18:18:33 -07002343void TouchInputMapper::configureVirtualKeysLocked() {
Jeff Brown8d608662010-08-30 03:02:23 -07002344 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08002345 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002346
Jeff Brown6328cdc2010-07-29 18:18:33 -07002347 mLocked.virtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002348
Jeff Brown6328cdc2010-07-29 18:18:33 -07002349 if (virtualKeyDefinitions.size() == 0) {
2350 return;
2351 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002352
Jeff Brown6328cdc2010-07-29 18:18:33 -07002353 mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size());
2354
Jeff Brown8d608662010-08-30 03:02:23 -07002355 int32_t touchScreenLeft = mRawAxes.x.minValue;
2356 int32_t touchScreenTop = mRawAxes.y.minValue;
Jeff Brown9626b142011-03-03 02:09:54 -08002357 int32_t touchScreenWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2358 int32_t touchScreenHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002359
2360 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07002361 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07002362 virtualKeyDefinitions[i];
2363
2364 mLocked.virtualKeys.add();
2365 VirtualKey& virtualKey = mLocked.virtualKeys.editTop();
2366
2367 virtualKey.scanCode = virtualKeyDefinition.scanCode;
2368 int32_t keyCode;
2369 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08002370 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07002371 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002372 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
2373 virtualKey.scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002374 mLocked.virtualKeys.pop(); // drop the key
2375 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002376 }
2377
Jeff Brown6328cdc2010-07-29 18:18:33 -07002378 virtualKey.keyCode = keyCode;
2379 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002380
Jeff Brown6328cdc2010-07-29 18:18:33 -07002381 // convert the key definition's display coordinates into touch coordinates for a hit box
2382 int32_t halfWidth = virtualKeyDefinition.width / 2;
2383 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002384
Jeff Brown6328cdc2010-07-29 18:18:33 -07002385 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
2386 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
2387 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
2388 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
2389 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
2390 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
2391 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
2392 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07002393 }
2394}
2395
2396void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) {
2397 if (!mLocked.virtualKeys.isEmpty()) {
2398 dump.append(INDENT3 "Virtual Keys:\n");
2399
2400 for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) {
2401 const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i);
2402 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
2403 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
2404 i, virtualKey.scanCode, virtualKey.keyCode,
2405 virtualKey.hitLeft, virtualKey.hitRight,
2406 virtualKey.hitTop, virtualKey.hitBottom);
2407 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002408 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002409}
2410
Jeff Brown8d608662010-08-30 03:02:23 -07002411void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002412 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07002413 Calibration& out = mCalibration;
2414
Jeff Brownc6d282b2010-10-14 21:42:15 -07002415 // Touch Size
2416 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT;
2417 String8 touchSizeCalibrationString;
2418 if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) {
2419 if (touchSizeCalibrationString == "none") {
2420 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
2421 } else if (touchSizeCalibrationString == "geometric") {
2422 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC;
2423 } else if (touchSizeCalibrationString == "pressure") {
2424 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
2425 } else if (touchSizeCalibrationString != "default") {
2426 LOGW("Invalid value for touch.touchSize.calibration: '%s'",
2427 touchSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002428 }
2429 }
2430
Jeff Brownc6d282b2010-10-14 21:42:15 -07002431 // Tool Size
2432 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT;
2433 String8 toolSizeCalibrationString;
2434 if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) {
2435 if (toolSizeCalibrationString == "none") {
2436 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
2437 } else if (toolSizeCalibrationString == "geometric") {
2438 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC;
2439 } else if (toolSizeCalibrationString == "linear") {
2440 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
2441 } else if (toolSizeCalibrationString == "area") {
2442 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA;
2443 } else if (toolSizeCalibrationString != "default") {
2444 LOGW("Invalid value for touch.toolSize.calibration: '%s'",
2445 toolSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002446 }
2447 }
2448
Jeff Brownc6d282b2010-10-14 21:42:15 -07002449 out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"),
2450 out.toolSizeLinearScale);
2451 out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"),
2452 out.toolSizeLinearBias);
2453 out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"),
2454 out.toolSizeAreaScale);
2455 out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"),
2456 out.toolSizeAreaBias);
2457 out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"),
2458 out.toolSizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07002459
2460 // Pressure
2461 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
2462 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002463 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002464 if (pressureCalibrationString == "none") {
2465 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2466 } else if (pressureCalibrationString == "physical") {
2467 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
2468 } else if (pressureCalibrationString == "amplitude") {
2469 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2470 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002471 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002472 pressureCalibrationString.string());
2473 }
2474 }
2475
2476 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
2477 String8 pressureSourceString;
2478 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
2479 if (pressureSourceString == "pressure") {
2480 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2481 } else if (pressureSourceString == "touch") {
2482 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2483 } else if (pressureSourceString != "default") {
2484 LOGW("Invalid value for touch.pressure.source: '%s'",
2485 pressureSourceString.string());
2486 }
2487 }
2488
2489 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
2490 out.pressureScale);
2491
2492 // Size
2493 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
2494 String8 sizeCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002495 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002496 if (sizeCalibrationString == "none") {
2497 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2498 } else if (sizeCalibrationString == "normalized") {
2499 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
2500 } else if (sizeCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002501 LOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002502 sizeCalibrationString.string());
2503 }
2504 }
2505
2506 // Orientation
2507 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
2508 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002509 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002510 if (orientationCalibrationString == "none") {
2511 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2512 } else if (orientationCalibrationString == "interpolated") {
2513 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002514 } else if (orientationCalibrationString == "vector") {
2515 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002516 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002517 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002518 orientationCalibrationString.string());
2519 }
2520 }
2521}
2522
2523void TouchInputMapper::resolveCalibration() {
2524 // Pressure
2525 switch (mCalibration.pressureSource) {
2526 case Calibration::PRESSURE_SOURCE_DEFAULT:
2527 if (mRawAxes.pressure.valid) {
2528 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2529 } else if (mRawAxes.touchMajor.valid) {
2530 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2531 }
2532 break;
2533
2534 case Calibration::PRESSURE_SOURCE_PRESSURE:
2535 if (! mRawAxes.pressure.valid) {
2536 LOGW("Calibration property touch.pressure.source is 'pressure' but "
2537 "the pressure axis is not available.");
2538 }
2539 break;
2540
2541 case Calibration::PRESSURE_SOURCE_TOUCH:
2542 if (! mRawAxes.touchMajor.valid) {
2543 LOGW("Calibration property touch.pressure.source is 'touch' but "
2544 "the touchMajor axis is not available.");
2545 }
2546 break;
2547
2548 default:
2549 break;
2550 }
2551
2552 switch (mCalibration.pressureCalibration) {
2553 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
2554 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
2555 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2556 } else {
2557 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2558 }
2559 break;
2560
2561 default:
2562 break;
2563 }
2564
Jeff Brownc6d282b2010-10-14 21:42:15 -07002565 // Tool Size
2566 switch (mCalibration.toolSizeCalibration) {
2567 case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07002568 if (mRawAxes.toolMajor.valid) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002569 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
Jeff Brown8d608662010-08-30 03:02:23 -07002570 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002571 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002572 }
2573 break;
2574
2575 default:
2576 break;
2577 }
2578
Jeff Brownc6d282b2010-10-14 21:42:15 -07002579 // Touch Size
2580 switch (mCalibration.touchSizeCalibration) {
2581 case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07002582 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
Jeff Brownc6d282b2010-10-14 21:42:15 -07002583 && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
2584 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
Jeff Brown8d608662010-08-30 03:02:23 -07002585 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002586 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002587 }
2588 break;
2589
2590 default:
2591 break;
2592 }
2593
2594 // Size
2595 switch (mCalibration.sizeCalibration) {
2596 case Calibration::SIZE_CALIBRATION_DEFAULT:
2597 if (mRawAxes.toolMajor.valid) {
2598 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
2599 } else {
2600 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2601 }
2602 break;
2603
2604 default:
2605 break;
2606 }
2607
2608 // Orientation
2609 switch (mCalibration.orientationCalibration) {
2610 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
2611 if (mRawAxes.orientation.valid) {
2612 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
2613 } else {
2614 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2615 }
2616 break;
2617
2618 default:
2619 break;
2620 }
2621}
2622
Jeff Brownef3d7e82010-09-30 14:33:04 -07002623void TouchInputMapper::dumpCalibration(String8& dump) {
2624 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07002625
Jeff Brownc6d282b2010-10-14 21:42:15 -07002626 // Touch Size
2627 switch (mCalibration.touchSizeCalibration) {
2628 case Calibration::TOUCH_SIZE_CALIBRATION_NONE:
2629 dump.append(INDENT4 "touch.touchSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002630 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002631 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
2632 dump.append(INDENT4 "touch.touchSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002633 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002634 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
2635 dump.append(INDENT4 "touch.touchSize.calibration: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002636 break;
2637 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002638 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002639 }
2640
Jeff Brownc6d282b2010-10-14 21:42:15 -07002641 // Tool Size
2642 switch (mCalibration.toolSizeCalibration) {
2643 case Calibration::TOOL_SIZE_CALIBRATION_NONE:
2644 dump.append(INDENT4 "touch.toolSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002645 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002646 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
2647 dump.append(INDENT4 "touch.toolSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002648 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002649 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
2650 dump.append(INDENT4 "touch.toolSize.calibration: linear\n");
2651 break;
2652 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2653 dump.append(INDENT4 "touch.toolSize.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002654 break;
2655 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002656 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002657 }
2658
Jeff Brownc6d282b2010-10-14 21:42:15 -07002659 if (mCalibration.haveToolSizeLinearScale) {
2660 dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n",
2661 mCalibration.toolSizeLinearScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002662 }
2663
Jeff Brownc6d282b2010-10-14 21:42:15 -07002664 if (mCalibration.haveToolSizeLinearBias) {
2665 dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n",
2666 mCalibration.toolSizeLinearBias);
Jeff Brown8d608662010-08-30 03:02:23 -07002667 }
2668
Jeff Brownc6d282b2010-10-14 21:42:15 -07002669 if (mCalibration.haveToolSizeAreaScale) {
2670 dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n",
2671 mCalibration.toolSizeAreaScale);
2672 }
2673
2674 if (mCalibration.haveToolSizeAreaBias) {
2675 dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n",
2676 mCalibration.toolSizeAreaBias);
2677 }
2678
2679 if (mCalibration.haveToolSizeIsSummed) {
Jeff Brown1f245102010-11-18 20:53:46 -08002680 dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %s\n",
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002681 toString(mCalibration.toolSizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07002682 }
2683
2684 // Pressure
2685 switch (mCalibration.pressureCalibration) {
2686 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002687 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002688 break;
2689 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002690 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002691 break;
2692 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002693 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002694 break;
2695 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002696 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002697 }
2698
2699 switch (mCalibration.pressureSource) {
2700 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002701 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002702 break;
2703 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002704 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002705 break;
2706 case Calibration::PRESSURE_SOURCE_DEFAULT:
2707 break;
2708 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002709 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002710 }
2711
2712 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07002713 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
2714 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002715 }
2716
2717 // Size
2718 switch (mCalibration.sizeCalibration) {
2719 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002720 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002721 break;
2722 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002723 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002724 break;
2725 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002726 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002727 }
2728
2729 // Orientation
2730 switch (mCalibration.orientationCalibration) {
2731 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002732 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002733 break;
2734 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002735 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002736 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002737 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
2738 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
2739 break;
Jeff Brown8d608662010-08-30 03:02:23 -07002740 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002741 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002742 }
2743}
2744
Jeff Brown6d0fec22010-07-23 21:28:06 -07002745void TouchInputMapper::reset() {
2746 // Synthesize touch up event if touch is currently down.
2747 // This will also take care of finishing virtual key processing if needed.
2748 if (mLastTouch.pointerCount != 0) {
2749 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
2750 mCurrentTouch.clear();
2751 syncTouch(when, true);
2752 }
2753
Jeff Brown6328cdc2010-07-29 18:18:33 -07002754 { // acquire lock
2755 AutoMutex _l(mLock);
2756 initializeLocked();
Jeff Brown2352b972011-04-12 22:39:53 -07002757
2758 if (mPointerController != NULL
2759 && mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
2760 mPointerController->clearSpots();
2761 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002762 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07002763
Jeff Brown6328cdc2010-07-29 18:18:33 -07002764 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002765}
2766
2767void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Brownaa3855d2011-03-17 01:34:19 -07002768#if DEBUG_RAW_EVENTS
2769 if (!havePointerIds) {
2770 LOGD("syncTouch: pointerCount=%d, no pointer ids", mCurrentTouch.pointerCount);
2771 } else {
2772 LOGD("syncTouch: pointerCount=%d, up=0x%08x, down=0x%08x, move=0x%08x, "
2773 "last=0x%08x, current=0x%08x", mCurrentTouch.pointerCount,
2774 mLastTouch.idBits.value & ~mCurrentTouch.idBits.value,
2775 mCurrentTouch.idBits.value & ~mLastTouch.idBits.value,
2776 mLastTouch.idBits.value & mCurrentTouch.idBits.value,
2777 mLastTouch.idBits.value, mCurrentTouch.idBits.value);
2778 }
2779#endif
2780
Jeff Brown6328cdc2010-07-29 18:18:33 -07002781 // Preprocess pointer data.
Jeff Brown6d0fec22010-07-23 21:28:06 -07002782 if (mParameters.useBadTouchFilter) {
2783 if (applyBadTouchFilter()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002784 havePointerIds = false;
2785 }
2786 }
2787
Jeff Brown6d0fec22010-07-23 21:28:06 -07002788 if (mParameters.useJumpyTouchFilter) {
2789 if (applyJumpyTouchFilter()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002790 havePointerIds = false;
2791 }
2792 }
2793
Jeff Brownaa3855d2011-03-17 01:34:19 -07002794 if (!havePointerIds) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002795 calculatePointerIds();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002796 }
2797
Jeff Brown6d0fec22010-07-23 21:28:06 -07002798 TouchData temp;
2799 TouchData* savedTouch;
2800 if (mParameters.useAveragingTouchFilter) {
2801 temp.copyFrom(mCurrentTouch);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002802 savedTouch = & temp;
2803
Jeff Brown6d0fec22010-07-23 21:28:06 -07002804 applyAveragingTouchFilter();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002805 } else {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002806 savedTouch = & mCurrentTouch;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002807 }
2808
Jeff Brown56194eb2011-03-02 19:23:13 -08002809 uint32_t policyFlags = 0;
Jeff Brown05dc66a2011-03-02 14:41:58 -08002810 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) {
Jeff Brownefd32662011-03-08 15:13:06 -08002811 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
2812 // If this is a touch screen, hide the pointer on an initial down.
2813 getContext()->fadePointer();
2814 }
Jeff Brown56194eb2011-03-02 19:23:13 -08002815
2816 // Initial downs on external touch devices should wake the device.
2817 // We don't do this for internal touch screens to prevent them from waking
2818 // up in your pocket.
2819 // TODO: Use the input device configuration to control this behavior more finely.
2820 if (getDevice()->isExternal()) {
2821 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2822 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002823 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002824
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002825 // Synthesize key down from buttons if needed.
2826 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mTouchSource,
2827 policyFlags, mLastTouch.buttonState, mCurrentTouch.buttonState);
2828
2829 // Send motion events.
Jeff Brown79ac9692011-04-19 21:20:10 -07002830 TouchResult touchResult;
2831 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount == 0
2832 && mLastTouch.buttonState == mCurrentTouch.buttonState) {
2833 // Drop spurious syncs.
2834 touchResult = DROP_STROKE;
2835 } else {
2836 // Process touches and virtual keys.
2837 touchResult = consumeOffScreenTouches(when, policyFlags);
2838 if (touchResult == DISPATCH_TOUCH) {
2839 suppressSwipeOntoVirtualKeys(when);
2840 if (mPointerController != NULL) {
2841 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
2842 }
2843 dispatchTouches(when, policyFlags);
Jeff Brownace13b12011-03-09 17:39:48 -08002844 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002845 }
2846
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002847 // Synthesize key up from buttons if needed.
2848 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mTouchSource,
2849 policyFlags, mLastTouch.buttonState, mCurrentTouch.buttonState);
2850
Jeff Brown6328cdc2010-07-29 18:18:33 -07002851 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownace13b12011-03-09 17:39:48 -08002852 // Keep the button state so we can track edge-triggered button state changes.
Jeff Brown6d0fec22010-07-23 21:28:06 -07002853 if (touchResult == DROP_STROKE) {
2854 mLastTouch.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08002855 mLastTouch.buttonState = savedTouch->buttonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002856 } else {
2857 mLastTouch.copyFrom(*savedTouch);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002858 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002859}
2860
Jeff Brown79ac9692011-04-19 21:20:10 -07002861void TouchInputMapper::timeoutExpired(nsecs_t when) {
2862 if (mPointerController != NULL) {
2863 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
2864 }
2865}
2866
Jeff Brown6d0fec22010-07-23 21:28:06 -07002867TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches(
2868 nsecs_t when, uint32_t policyFlags) {
2869 int32_t keyEventAction, keyEventFlags;
2870 int32_t keyCode, scanCode, downTime;
2871 TouchResult touchResult;
Jeff Brown349703e2010-06-22 01:27:15 -07002872
Jeff Brown6328cdc2010-07-29 18:18:33 -07002873 { // acquire lock
2874 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002875
Jeff Brown6328cdc2010-07-29 18:18:33 -07002876 // Update surface size and orientation, including virtual key positions.
2877 if (! configureSurfaceLocked()) {
2878 return DROP_STROKE;
2879 }
2880
2881 // Check for virtual key press.
2882 if (mLocked.currentVirtualKey.down) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002883 if (mCurrentTouch.pointerCount == 0) {
2884 // Pointer went up while virtual key was down.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002885 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002886#if DEBUG_VIRTUAL_KEYS
2887 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07002888 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002889#endif
2890 keyEventAction = AKEY_EVENT_ACTION_UP;
2891 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2892 touchResult = SKIP_TOUCH;
2893 goto DispatchVirtualKey;
2894 }
2895
2896 if (mCurrentTouch.pointerCount == 1) {
2897 int32_t x = mCurrentTouch.pointers[0].x;
2898 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002899 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
2900 if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002901 // Pointer is still within the space of the virtual key.
2902 return SKIP_TOUCH;
2903 }
2904 }
2905
2906 // Pointer left virtual key area or another pointer also went down.
2907 // Send key cancellation and drop the stroke so subsequent motions will be
2908 // considered fresh downs. This is useful when the user swipes away from the
2909 // virtual key area into the main display surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002910 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002911#if DEBUG_VIRTUAL_KEYS
2912 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07002913 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002914#endif
2915 keyEventAction = AKEY_EVENT_ACTION_UP;
2916 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
2917 | AKEY_EVENT_FLAG_CANCELED;
Jeff Brownc3db8582010-10-20 15:33:38 -07002918
2919 // Check whether the pointer moved inside the display area where we should
2920 // start a new stroke.
2921 int32_t x = mCurrentTouch.pointers[0].x;
2922 int32_t y = mCurrentTouch.pointers[0].y;
2923 if (isPointInsideSurfaceLocked(x, y)) {
2924 mLastTouch.clear();
2925 touchResult = DISPATCH_TOUCH;
2926 } else {
2927 touchResult = DROP_STROKE;
2928 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002929 } else {
2930 if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) {
2931 // Pointer just went down. Handle off-screen touches, if needed.
2932 int32_t x = mCurrentTouch.pointers[0].x;
2933 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002934 if (! isPointInsideSurfaceLocked(x, y)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002935 // If exactly one pointer went down, check for virtual key hit.
2936 // Otherwise we will drop the entire stroke.
2937 if (mCurrentTouch.pointerCount == 1) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002938 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002939 if (virtualKey) {
Jeff Brownfe508922011-01-18 15:10:10 -08002940 if (mContext->shouldDropVirtualKey(when, getDevice(),
2941 virtualKey->keyCode, virtualKey->scanCode)) {
2942 return DROP_STROKE;
2943 }
2944
Jeff Brown6328cdc2010-07-29 18:18:33 -07002945 mLocked.currentVirtualKey.down = true;
2946 mLocked.currentVirtualKey.downTime = when;
2947 mLocked.currentVirtualKey.keyCode = virtualKey->keyCode;
2948 mLocked.currentVirtualKey.scanCode = virtualKey->scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002949#if DEBUG_VIRTUAL_KEYS
2950 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07002951 mLocked.currentVirtualKey.keyCode,
2952 mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002953#endif
2954 keyEventAction = AKEY_EVENT_ACTION_DOWN;
2955 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM
2956 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
2957 touchResult = SKIP_TOUCH;
2958 goto DispatchVirtualKey;
2959 }
2960 }
2961 return DROP_STROKE;
2962 }
2963 }
2964 return DISPATCH_TOUCH;
2965 }
2966
2967 DispatchVirtualKey:
2968 // Collect remaining state needed to dispatch virtual key.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002969 keyCode = mLocked.currentVirtualKey.keyCode;
2970 scanCode = mLocked.currentVirtualKey.scanCode;
2971 downTime = mLocked.currentVirtualKey.downTime;
2972 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07002973
2974 // Dispatch virtual key.
2975 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown0eaf3932010-10-01 14:55:30 -07002976 policyFlags |= POLICY_FLAG_VIRTUAL;
Jeff Brownb6997262010-10-08 22:31:17 -07002977 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
2978 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
2979 return touchResult;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002980}
2981
Jeff Brownefd32662011-03-08 15:13:06 -08002982void TouchInputMapper::suppressSwipeOntoVirtualKeys(nsecs_t when) {
Jeff Brownfe508922011-01-18 15:10:10 -08002983 // Disable all virtual key touches that happen within a short time interval of the
2984 // most recent touch. The idea is to filter out stray virtual key presses when
2985 // interacting with the touch screen.
2986 //
2987 // Problems we're trying to solve:
2988 //
2989 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
2990 // virtual key area that is implemented by a separate touch panel and accidentally
2991 // triggers a virtual key.
2992 //
2993 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
2994 // area and accidentally triggers a virtual key. This often happens when virtual keys
2995 // are layed out below the screen near to where the on screen keyboard's space bar
2996 // is displayed.
2997 if (mParameters.virtualKeyQuietTime > 0 && mCurrentTouch.pointerCount != 0) {
2998 mContext->disableVirtualKeysUntil(when + mParameters.virtualKeyQuietTime);
2999 }
3000}
3001
Jeff Brown6d0fec22010-07-23 21:28:06 -07003002void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
3003 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
3004 uint32_t lastPointerCount = mLastTouch.pointerCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003005 if (currentPointerCount == 0 && lastPointerCount == 0) {
3006 return; // nothing to do!
3007 }
3008
Jeff Brownace13b12011-03-09 17:39:48 -08003009 // Update current touch coordinates.
3010 int32_t edgeFlags;
3011 float xPrecision, yPrecision;
3012 prepareTouches(&edgeFlags, &xPrecision, &yPrecision);
3013
3014 // Dispatch motions.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003015 BitSet32 currentIdBits = mCurrentTouch.idBits;
3016 BitSet32 lastIdBits = mLastTouch.idBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003017 int32_t metaState = getContext()->getGlobalMetaState();
3018 int32_t buttonState = mCurrentTouch.buttonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003019
3020 if (currentIdBits == lastIdBits) {
3021 // No pointer id changes so this is a move event.
3022 // The dispatcher takes care of batching moves so we don't have to deal with that here.
Jeff Brownace13b12011-03-09 17:39:48 -08003023 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003024 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3025 AMOTION_EVENT_EDGE_FLAG_NONE,
3026 mCurrentTouchProperties, mCurrentTouchCoords,
3027 mCurrentTouch.idToIndex, currentIdBits, -1,
Jeff Brownace13b12011-03-09 17:39:48 -08003028 xPrecision, yPrecision, mDownTime);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003029 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003030 // There may be pointers going up and pointers going down and pointers moving
3031 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003032 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3033 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003034 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003035 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003036
Jeff Brownace13b12011-03-09 17:39:48 -08003037 // Update last coordinates of pointers that have moved so that we observe the new
3038 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003039 bool moveNeeded = updateMovedPointers(
3040 mCurrentTouchProperties, mCurrentTouchCoords, mCurrentTouch.idToIndex,
3041 mLastTouchProperties, mLastTouchCoords, mLastTouch.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003042 moveIdBits);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003043 if (buttonState != mLastTouch.buttonState) {
3044 moveNeeded = true;
3045 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003046
Jeff Brownace13b12011-03-09 17:39:48 -08003047 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003048 while (!upIdBits.isEmpty()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003049 uint32_t upId = upIdBits.firstMarkedBit();
3050 upIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003051
Jeff Brownace13b12011-03-09 17:39:48 -08003052 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003053 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
3054 mLastTouchProperties, mLastTouchCoords,
3055 mLastTouch.idToIndex, dispatchedIdBits, upId,
Jeff Brownace13b12011-03-09 17:39:48 -08003056 xPrecision, yPrecision, mDownTime);
3057 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003058 }
3059
Jeff Brownc3db8582010-10-20 15:33:38 -07003060 // Dispatch move events if any of the remaining pointers moved from their old locations.
3061 // Although applications receive new locations as part of individual pointer up
3062 // events, they do not generally handle them except when presented in a move event.
3063 if (moveNeeded) {
Jeff Brownb6110c22011-04-01 16:15:13 -07003064 LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003065 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003066 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
3067 mCurrentTouchProperties, mCurrentTouchCoords,
3068 mCurrentTouch.idToIndex, dispatchedIdBits, -1,
Jeff Brownace13b12011-03-09 17:39:48 -08003069 xPrecision, yPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003070 }
3071
3072 // Dispatch pointer down events using the new pointer locations.
3073 while (!downIdBits.isEmpty()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003074 uint32_t downId = downIdBits.firstMarkedBit();
3075 downIdBits.clearBit(downId);
Jeff Brownace13b12011-03-09 17:39:48 -08003076 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003077
Jeff Brownace13b12011-03-09 17:39:48 -08003078 if (dispatchedIdBits.count() == 1) {
3079 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003080 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003081 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08003082 // Only send edge flags with first pointer down.
3083 edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003084 }
3085
Jeff Brownace13b12011-03-09 17:39:48 -08003086 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003087 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, edgeFlags,
3088 mCurrentTouchProperties, mCurrentTouchCoords,
3089 mCurrentTouch.idToIndex, dispatchedIdBits, downId,
Jeff Brownace13b12011-03-09 17:39:48 -08003090 xPrecision, yPrecision, mDownTime);
3091 }
3092 }
3093
3094 // Update state for next time.
3095 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003096 mLastTouchProperties[i].copyFrom(mCurrentTouchProperties[i]);
Jeff Brownace13b12011-03-09 17:39:48 -08003097 mLastTouchCoords[i].copyFrom(mCurrentTouchCoords[i]);
3098 }
3099}
3100
3101void TouchInputMapper::prepareTouches(int32_t* outEdgeFlags,
3102 float* outXPrecision, float* outYPrecision) {
3103 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
3104 uint32_t lastPointerCount = mLastTouch.pointerCount;
3105
3106 AutoMutex _l(mLock);
3107
3108 // Walk through the the active pointers and map touch screen coordinates (TouchData) into
3109 // display or surface coordinates (PointerCoords) and adjust for display orientation.
3110 for (uint32_t i = 0; i < currentPointerCount; i++) {
3111 const PointerData& in = mCurrentTouch.pointers[i];
3112
3113 // ToolMajor and ToolMinor
3114 float toolMajor, toolMinor;
3115 switch (mCalibration.toolSizeCalibration) {
3116 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
3117 toolMajor = in.toolMajor * mLocked.geometricScale;
3118 if (mRawAxes.toolMinor.valid) {
3119 toolMinor = in.toolMinor * mLocked.geometricScale;
3120 } else {
3121 toolMinor = toolMajor;
3122 }
3123 break;
3124 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
3125 toolMajor = in.toolMajor != 0
3126 ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias
3127 : 0;
3128 if (mRawAxes.toolMinor.valid) {
3129 toolMinor = in.toolMinor != 0
3130 ? in.toolMinor * mLocked.toolSizeLinearScale
3131 + mLocked.toolSizeLinearBias
3132 : 0;
3133 } else {
3134 toolMinor = toolMajor;
3135 }
3136 break;
3137 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
3138 if (in.toolMajor != 0) {
3139 float diameter = sqrtf(in.toolMajor
3140 * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias);
3141 toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias;
3142 } else {
3143 toolMajor = 0;
3144 }
3145 toolMinor = toolMajor;
3146 break;
3147 default:
3148 toolMajor = 0;
3149 toolMinor = 0;
3150 break;
3151 }
3152
3153 if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) {
3154 toolMajor /= currentPointerCount;
3155 toolMinor /= currentPointerCount;
3156 }
3157
3158 // Pressure
3159 float rawPressure;
3160 switch (mCalibration.pressureSource) {
3161 case Calibration::PRESSURE_SOURCE_PRESSURE:
3162 rawPressure = in.pressure;
3163 break;
3164 case Calibration::PRESSURE_SOURCE_TOUCH:
3165 rawPressure = in.touchMajor;
3166 break;
3167 default:
3168 rawPressure = 0;
3169 }
3170
3171 float pressure;
3172 switch (mCalibration.pressureCalibration) {
3173 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3174 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
3175 pressure = rawPressure * mLocked.pressureScale;
3176 break;
3177 default:
3178 pressure = 1;
3179 break;
3180 }
3181
3182 // TouchMajor and TouchMinor
3183 float touchMajor, touchMinor;
3184 switch (mCalibration.touchSizeCalibration) {
3185 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
3186 touchMajor = in.touchMajor * mLocked.geometricScale;
3187 if (mRawAxes.touchMinor.valid) {
3188 touchMinor = in.touchMinor * mLocked.geometricScale;
3189 } else {
3190 touchMinor = touchMajor;
3191 }
3192 break;
3193 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
3194 touchMajor = toolMajor * pressure;
3195 touchMinor = toolMinor * pressure;
3196 break;
3197 default:
3198 touchMajor = 0;
3199 touchMinor = 0;
3200 break;
3201 }
3202
3203 if (touchMajor > toolMajor) {
3204 touchMajor = toolMajor;
3205 }
3206 if (touchMinor > toolMinor) {
3207 touchMinor = toolMinor;
3208 }
3209
3210 // Size
3211 float size;
3212 switch (mCalibration.sizeCalibration) {
3213 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
3214 float rawSize = mRawAxes.toolMinor.valid
3215 ? avg(in.toolMajor, in.toolMinor)
3216 : in.toolMajor;
3217 size = rawSize * mLocked.sizeScale;
3218 break;
3219 }
3220 default:
3221 size = 0;
3222 break;
3223 }
3224
3225 // Orientation
3226 float orientation;
3227 switch (mCalibration.orientationCalibration) {
3228 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
3229 orientation = in.orientation * mLocked.orientationScale;
3230 break;
3231 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3232 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3233 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3234 if (c1 != 0 || c2 != 0) {
3235 orientation = atan2f(c1, c2) * 0.5f;
Jeff Brown2352b972011-04-12 22:39:53 -07003236 float scale = 1.0f + hypotf(c1, c2) / 16.0f;
Jeff Brownace13b12011-03-09 17:39:48 -08003237 touchMajor *= scale;
3238 touchMinor /= scale;
3239 toolMajor *= scale;
3240 toolMinor /= scale;
3241 } else {
3242 orientation = 0;
3243 }
3244 break;
3245 }
3246 default:
3247 orientation = 0;
3248 }
3249
3250 // X and Y
3251 // Adjust coords for surface orientation.
3252 float x, y;
3253 switch (mLocked.surfaceOrientation) {
3254 case DISPLAY_ORIENTATION_90:
3255 x = float(in.y - mRawAxes.y.minValue) * mLocked.yScale;
3256 y = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale;
3257 orientation -= M_PI_2;
3258 if (orientation < - M_PI_2) {
3259 orientation += M_PI;
3260 }
3261 break;
3262 case DISPLAY_ORIENTATION_180:
3263 x = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale;
3264 y = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale;
3265 break;
3266 case DISPLAY_ORIENTATION_270:
3267 x = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale;
3268 y = float(in.x - mRawAxes.x.minValue) * mLocked.xScale;
3269 orientation += M_PI_2;
3270 if (orientation > M_PI_2) {
3271 orientation -= M_PI;
3272 }
3273 break;
3274 default:
3275 x = float(in.x - mRawAxes.x.minValue) * mLocked.xScale;
3276 y = float(in.y - mRawAxes.y.minValue) * mLocked.yScale;
3277 break;
3278 }
3279
3280 // Write output coords.
3281 PointerCoords& out = mCurrentTouchCoords[i];
3282 out.clear();
3283 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3284 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3285 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3286 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3287 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3288 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3289 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3290 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3291 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003292
3293 // Write output properties.
3294 PointerProperties& properties = mCurrentTouchProperties[i];
3295 properties.clear();
3296 properties.id = mCurrentTouch.pointers[i].id;
3297 properties.toolType = getTouchToolType(mCurrentTouch.pointers[i].isStylus);
Jeff Brownace13b12011-03-09 17:39:48 -08003298 }
3299
3300 // Check edge flags by looking only at the first pointer since the flags are
3301 // global to the event.
3302 *outEdgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
3303 if (lastPointerCount == 0 && currentPointerCount > 0) {
3304 const PointerData& in = mCurrentTouch.pointers[0];
3305
3306 if (in.x <= mRawAxes.x.minValue) {
3307 *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_LEFT,
3308 mLocked.surfaceOrientation);
3309 } else if (in.x >= mRawAxes.x.maxValue) {
3310 *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_RIGHT,
3311 mLocked.surfaceOrientation);
3312 }
3313 if (in.y <= mRawAxes.y.minValue) {
3314 *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_TOP,
3315 mLocked.surfaceOrientation);
3316 } else if (in.y >= mRawAxes.y.maxValue) {
3317 *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_BOTTOM,
3318 mLocked.surfaceOrientation);
3319 }
3320 }
3321
3322 *outXPrecision = mLocked.orientedXPrecision;
3323 *outYPrecision = mLocked.orientedYPrecision;
3324}
3325
Jeff Brown79ac9692011-04-19 21:20:10 -07003326void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3327 bool isTimeout) {
Jeff Brown2352b972011-04-12 22:39:53 -07003328 // Switch pointer presentation.
3329 mPointerController->setPresentation(
3330 mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3331 ? PointerControllerInterface::PRESENTATION_SPOT
3332 : PointerControllerInterface::PRESENTATION_POINTER);
3333
Jeff Brownace13b12011-03-09 17:39:48 -08003334 // Update current gesture coordinates.
3335 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07003336 bool sendEvents = preparePointerGestures(when,
3337 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3338 if (!sendEvents) {
3339 return;
3340 }
Jeff Brownace13b12011-03-09 17:39:48 -08003341
Jeff Brown2352b972011-04-12 22:39:53 -07003342 // Show the pointer if needed.
3343 if (mPointerGesture.currentGestureMode != PointerGesture::NEUTRAL
3344 && mPointerGesture.currentGestureMode != PointerGesture::QUIET) {
3345 mPointerController->unfade();
3346 }
3347
Jeff Brownace13b12011-03-09 17:39:48 -08003348 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003349 int32_t metaState = getContext()->getGlobalMetaState();
3350 int32_t buttonState = mCurrentTouch.buttonState;
Jeff Brownace13b12011-03-09 17:39:48 -08003351
3352 // Update last coordinates of pointers that have moved so that we observe the new
3353 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07003354 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
3355 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
3356 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07003357 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08003358 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
3359 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
3360 bool moveNeeded = false;
3361 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07003362 && !mPointerGesture.lastGestureIdBits.isEmpty()
3363 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08003364 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
3365 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003366 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003367 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003368 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003369 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3370 movedGestureIdBits);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003371 if (buttonState != mLastTouch.buttonState) {
3372 moveNeeded = true;
3373 }
Jeff Brownace13b12011-03-09 17:39:48 -08003374 }
3375
3376 // Send motion events for all pointers that went up or were canceled.
3377 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
3378 if (!dispatchedGestureIdBits.isEmpty()) {
3379 if (cancelPreviousGesture) {
3380 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003381 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
3382 AMOTION_EVENT_EDGE_FLAG_NONE,
3383 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003384 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3385 dispatchedGestureIdBits, -1,
3386 0, 0, mPointerGesture.downTime);
3387
3388 dispatchedGestureIdBits.clear();
3389 } else {
3390 BitSet32 upGestureIdBits;
3391 if (finishPreviousGesture) {
3392 upGestureIdBits = dispatchedGestureIdBits;
3393 } else {
3394 upGestureIdBits.value = dispatchedGestureIdBits.value
3395 & ~mPointerGesture.currentGestureIdBits.value;
3396 }
3397 while (!upGestureIdBits.isEmpty()) {
3398 uint32_t id = upGestureIdBits.firstMarkedBit();
3399 upGestureIdBits.clearBit(id);
3400
3401 dispatchMotion(when, policyFlags, mPointerSource,
3402 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003403 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3404 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003405 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3406 dispatchedGestureIdBits, id,
3407 0, 0, mPointerGesture.downTime);
3408
3409 dispatchedGestureIdBits.clearBit(id);
3410 }
3411 }
3412 }
3413
3414 // Send motion events for all pointers that moved.
3415 if (moveNeeded) {
3416 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003417 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3418 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003419 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3420 dispatchedGestureIdBits, -1,
3421 0, 0, mPointerGesture.downTime);
3422 }
3423
3424 // Send motion events for all pointers that went down.
3425 if (down) {
3426 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
3427 & ~dispatchedGestureIdBits.value);
3428 while (!downGestureIdBits.isEmpty()) {
3429 uint32_t id = downGestureIdBits.firstMarkedBit();
3430 downGestureIdBits.clearBit(id);
3431 dispatchedGestureIdBits.markBit(id);
3432
3433 int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
3434 if (dispatchedGestureIdBits.count() == 1) {
3435 // First pointer is going down. Calculate edge flags and set down time.
3436 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
3437 const PointerCoords& downCoords = mPointerGesture.currentGestureCoords[index];
3438 edgeFlags = calculateEdgeFlagsUsingPointerBounds(mPointerController,
3439 downCoords.getAxisValue(AMOTION_EVENT_AXIS_X),
3440 downCoords.getAxisValue(AMOTION_EVENT_AXIS_Y));
3441 mPointerGesture.downTime = when;
3442 }
3443
3444 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003445 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, edgeFlags,
3446 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003447 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3448 dispatchedGestureIdBits, id,
3449 0, 0, mPointerGesture.downTime);
3450 }
3451 }
3452
Jeff Brownace13b12011-03-09 17:39:48 -08003453 // Send motion events for hover.
3454 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
3455 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003456 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
3457 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3458 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003459 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3460 mPointerGesture.currentGestureIdBits, -1,
3461 0, 0, mPointerGesture.downTime);
3462 }
3463
3464 // Update state.
3465 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
3466 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08003467 mPointerGesture.lastGestureIdBits.clear();
3468 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08003469 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
3470 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
3471 uint32_t id = idBits.firstMarkedBit();
3472 idBits.clearBit(id);
3473 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003474 mPointerGesture.lastGestureProperties[index].copyFrom(
3475 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08003476 mPointerGesture.lastGestureCoords[index].copyFrom(
3477 mPointerGesture.currentGestureCoords[index]);
3478 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003479 }
3480 }
3481}
3482
Jeff Brown79ac9692011-04-19 21:20:10 -07003483bool TouchInputMapper::preparePointerGestures(nsecs_t when,
3484 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003485 *outCancelPreviousGesture = false;
3486 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003487
Jeff Brownace13b12011-03-09 17:39:48 -08003488 AutoMutex _l(mLock);
Jeff Brown6328cdc2010-07-29 18:18:33 -07003489
Jeff Brown79ac9692011-04-19 21:20:10 -07003490 // Handle TAP timeout.
3491 if (isTimeout) {
3492#if DEBUG_GESTURES
3493 LOGD("Gestures: Processing timeout");
3494#endif
3495
3496 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
3497 if (when <= mPointerGesture.tapUpTime + TAP_DRAG_INTERVAL) {
3498 // The tap/drag timeout has not yet expired.
3499 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime + TAP_DRAG_INTERVAL);
3500 } else {
3501 // The tap is finished.
3502#if DEBUG_GESTURES
3503 LOGD("Gestures: TAP finished");
3504#endif
3505 *outFinishPreviousGesture = true;
3506
3507 mPointerGesture.activeGestureId = -1;
3508 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
3509 mPointerGesture.currentGestureIdBits.clear();
3510
Jeff Brown79ac9692011-04-19 21:20:10 -07003511 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3512 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_NEUTRAL;
3513 mPointerGesture.spotIdBits.clear();
3514 moveSpotsLocked();
3515 }
3516 return true;
3517 }
3518 }
3519
3520 // We did not handle this timeout.
3521 return false;
3522 }
3523
Jeff Brownace13b12011-03-09 17:39:48 -08003524 // Update the velocity tracker.
3525 {
3526 VelocityTracker::Position positions[MAX_POINTERS];
3527 uint32_t count = 0;
3528 for (BitSet32 idBits(mCurrentTouch.idBits); !idBits.isEmpty(); count++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003529 uint32_t id = idBits.firstMarkedBit();
3530 idBits.clearBit(id);
Jeff Brownace13b12011-03-09 17:39:48 -08003531 uint32_t index = mCurrentTouch.idToIndex[id];
3532 positions[count].x = mCurrentTouch.pointers[index].x
3533 * mLocked.pointerGestureXMovementScale;
3534 positions[count].y = mCurrentTouch.pointers[index].y
3535 * mLocked.pointerGestureYMovementScale;
3536 }
3537 mPointerGesture.velocityTracker.addMovement(when, mCurrentTouch.idBits, positions);
3538 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003539
Jeff Brownace13b12011-03-09 17:39:48 -08003540 // Pick a new active touch id if needed.
3541 // Choose an arbitrary pointer that just went down, if there is one.
3542 // Otherwise choose an arbitrary remaining pointer.
3543 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07003544 // We keep the same active touch id for as long as possible.
3545 bool activeTouchChanged = false;
3546 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
3547 int32_t activeTouchId = lastActiveTouchId;
3548 if (activeTouchId < 0) {
3549 if (!mCurrentTouch.idBits.isEmpty()) {
3550 activeTouchChanged = true;
3551 activeTouchId = mPointerGesture.activeTouchId = mCurrentTouch.idBits.firstMarkedBit();
3552 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08003553 }
Jeff Brown2352b972011-04-12 22:39:53 -07003554 } else if (!mCurrentTouch.idBits.hasBit(activeTouchId)) {
3555 activeTouchChanged = true;
3556 if (!mCurrentTouch.idBits.isEmpty()) {
3557 activeTouchId = mPointerGesture.activeTouchId = mCurrentTouch.idBits.firstMarkedBit();
3558 } else {
3559 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08003560 }
3561 }
3562
3563 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07003564 bool isQuietTime = false;
3565 if (activeTouchId < 0) {
3566 mPointerGesture.resetQuietTime();
3567 } else {
3568 isQuietTime = when < mPointerGesture.quietTime + QUIET_INTERVAL;
3569 if (!isQuietTime) {
3570 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
3571 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3572 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
3573 && mCurrentTouch.pointerCount < 2) {
3574 // Enter quiet time when exiting swipe or freeform state.
3575 // This is to prevent accidentally entering the hover state and flinging the
3576 // pointer when finishing a swipe and there is still one pointer left onscreen.
3577 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07003578 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07003579 && mCurrentTouch.pointerCount >= 2
3580 && !isPointerDown(mCurrentTouch.buttonState)) {
3581 // Enter quiet time when releasing the button and there are still two or more
3582 // fingers down. This may indicate that one finger was used to press the button
3583 // but it has not gone up yet.
3584 isQuietTime = true;
3585 }
3586 if (isQuietTime) {
3587 mPointerGesture.quietTime = when;
3588 }
Jeff Brownace13b12011-03-09 17:39:48 -08003589 }
3590 }
3591
3592 // Switch states based on button and pointer state.
3593 if (isQuietTime) {
3594 // Case 1: Quiet time. (QUIET)
3595#if DEBUG_GESTURES
3596 LOGD("Gestures: QUIET for next %0.3fms",
3597 (mPointerGesture.quietTime + QUIET_INTERVAL - when) * 0.000001f);
3598#endif
3599 *outFinishPreviousGesture = true;
3600
3601 mPointerGesture.activeGestureId = -1;
3602 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08003603 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07003604
Jeff Brown2352b972011-04-12 22:39:53 -07003605 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3606 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_NEUTRAL;
3607 mPointerGesture.spotIdBits.clear();
3608 moveSpotsLocked();
3609 }
Jeff Brownace13b12011-03-09 17:39:48 -08003610 } else if (isPointerDown(mCurrentTouch.buttonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003611 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08003612 // The pointer follows the active touch point.
3613 // Emit DOWN, MOVE, UP events at the pointer location.
3614 //
3615 // Only the active touch matters; other fingers are ignored. This policy helps
3616 // to handle the case where the user places a second finger on the touch pad
3617 // to apply the necessary force to depress an integrated button below the surface.
3618 // We don't want the second finger to be delivered to applications.
3619 //
3620 // For this to work well, we need to make sure to track the pointer that is really
3621 // active. If the user first puts one finger down to click then adds another
3622 // finger to drag then the active pointer should switch to the finger that is
3623 // being dragged.
3624#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07003625 LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brownace13b12011-03-09 17:39:48 -08003626 "currentTouchPointerCount=%d", activeTouchId, mCurrentTouch.pointerCount);
3627#endif
3628 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07003629 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08003630 *outFinishPreviousGesture = true;
3631 mPointerGesture.activeGestureId = 0;
3632 }
3633
3634 // Switch pointers if needed.
3635 // Find the fastest pointer and follow it.
3636 if (activeTouchId >= 0) {
3637 if (mCurrentTouch.pointerCount > 1) {
3638 int32_t bestId = -1;
3639 float bestSpeed = DRAG_MIN_SWITCH_SPEED;
3640 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
3641 uint32_t id = mCurrentTouch.pointers[i].id;
3642 float vx, vy;
3643 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
Jeff Brown2352b972011-04-12 22:39:53 -07003644 float speed = hypotf(vx, vy);
Jeff Brownace13b12011-03-09 17:39:48 -08003645 if (speed > bestSpeed) {
3646 bestId = id;
3647 bestSpeed = speed;
3648 }
3649 }
Jeff Brown8d608662010-08-30 03:02:23 -07003650 }
Jeff Brownace13b12011-03-09 17:39:48 -08003651 if (bestId >= 0 && bestId != activeTouchId) {
3652 mPointerGesture.activeTouchId = activeTouchId = bestId;
Jeff Brown2352b972011-04-12 22:39:53 -07003653 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08003654#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07003655 LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
Jeff Brownace13b12011-03-09 17:39:48 -08003656 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
3657#endif
Jeff Brown8d608662010-08-30 03:02:23 -07003658 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003659 }
3660
Jeff Brownace13b12011-03-09 17:39:48 -08003661 if (mLastTouch.idBits.hasBit(activeTouchId)) {
3662 const PointerData& currentPointer =
3663 mCurrentTouch.pointers[mCurrentTouch.idToIndex[activeTouchId]];
3664 const PointerData& lastPointer =
3665 mLastTouch.pointers[mLastTouch.idToIndex[activeTouchId]];
3666 float deltaX = (currentPointer.x - lastPointer.x)
3667 * mLocked.pointerGestureXMovementScale;
3668 float deltaY = (currentPointer.y - lastPointer.y)
3669 * mLocked.pointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07003670
3671 // Move the pointer using a relative motion.
3672 // When using spots, the click will occur at the position of the anchor
3673 // spot and all other spots will move there.
Jeff Brownace13b12011-03-09 17:39:48 -08003674 mPointerController->move(deltaX, deltaY);
Jeff Brown6328cdc2010-07-29 18:18:33 -07003675 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003676 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003677
Jeff Brownace13b12011-03-09 17:39:48 -08003678 float x, y;
3679 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08003680
Jeff Brown79ac9692011-04-19 21:20:10 -07003681 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08003682 mPointerGesture.currentGestureIdBits.clear();
3683 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3684 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003685 mPointerGesture.currentGestureProperties[0].clear();
3686 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3687 mPointerGesture.currentGestureProperties[0].toolType =
3688 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08003689 mPointerGesture.currentGestureCoords[0].clear();
3690 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3691 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3692 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07003693
Jeff Brown2352b972011-04-12 22:39:53 -07003694 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3695 if (activeTouchId >= 0) {
3696 // Collapse all spots into one point at the pointer location.
3697 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_BUTTON_DRAG;
3698 mPointerGesture.spotIdBits.clear();
3699 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
3700 uint32_t id = mCurrentTouch.pointers[i].id;
3701 mPointerGesture.spotIdBits.markBit(id);
3702 mPointerGesture.spotIdToIndex[id] = i;
3703 mPointerGesture.spotCoords[i] = mPointerGesture.currentGestureCoords[0];
3704 }
3705 } else {
3706 // No fingers. Generate a spot at the pointer location so the
3707 // anchor appears to be pressed.
3708 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_BUTTON_CLICK;
3709 mPointerGesture.spotIdBits.clear();
3710 mPointerGesture.spotIdBits.markBit(0);
3711 mPointerGesture.spotIdToIndex[0] = 0;
3712 mPointerGesture.spotCoords[0] = mPointerGesture.currentGestureCoords[0];
3713 }
3714 moveSpotsLocked();
3715 }
Jeff Brownace13b12011-03-09 17:39:48 -08003716 } else if (mCurrentTouch.pointerCount == 0) {
3717 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
3718 *outFinishPreviousGesture = true;
3719
Jeff Brown79ac9692011-04-19 21:20:10 -07003720 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brownace13b12011-03-09 17:39:48 -08003721 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07003722 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
3723 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown2352b972011-04-12 22:39:53 -07003724 && mLastTouch.pointerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003725 if (when <= mPointerGesture.tapDownTime + TAP_INTERVAL) {
Jeff Brownace13b12011-03-09 17:39:48 -08003726 float x, y;
3727 mPointerController->getPosition(&x, &y);
Jeff Brown2352b972011-04-12 22:39:53 -07003728 if (fabs(x - mPointerGesture.tapX) <= TAP_SLOP
3729 && fabs(y - mPointerGesture.tapY) <= TAP_SLOP) {
Jeff Brownace13b12011-03-09 17:39:48 -08003730#if DEBUG_GESTURES
3731 LOGD("Gestures: TAP");
3732#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07003733
3734 mPointerGesture.tapUpTime = when;
3735 getContext()->requestTimeoutAtTime(when + TAP_DRAG_INTERVAL);
3736
Jeff Brownace13b12011-03-09 17:39:48 -08003737 mPointerGesture.activeGestureId = 0;
3738 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08003739 mPointerGesture.currentGestureIdBits.clear();
3740 mPointerGesture.currentGestureIdBits.markBit(
3741 mPointerGesture.activeGestureId);
3742 mPointerGesture.currentGestureIdToIndex[
3743 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003744 mPointerGesture.currentGestureProperties[0].clear();
3745 mPointerGesture.currentGestureProperties[0].id =
3746 mPointerGesture.activeGestureId;
3747 mPointerGesture.currentGestureProperties[0].toolType =
3748 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08003749 mPointerGesture.currentGestureCoords[0].clear();
3750 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07003751 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08003752 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07003753 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08003754 mPointerGesture.currentGestureCoords[0].setAxisValue(
3755 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07003756
Jeff Brown2352b972011-04-12 22:39:53 -07003757 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3758 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_TAP;
3759 mPointerGesture.spotIdBits.clear();
3760 mPointerGesture.spotIdBits.markBit(lastActiveTouchId);
3761 mPointerGesture.spotIdToIndex[lastActiveTouchId] = 0;
3762 mPointerGesture.spotCoords[0] = mPointerGesture.currentGestureCoords[0];
3763 moveSpotsLocked();
3764 }
3765
Jeff Brownace13b12011-03-09 17:39:48 -08003766 tapped = true;
3767 } else {
3768#if DEBUG_GESTURES
3769 LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07003770 x - mPointerGesture.tapX,
3771 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08003772#endif
3773 }
3774 } else {
3775#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07003776 LOGD("Gestures: Not a TAP, %0.3fms since down",
3777 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08003778#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07003779 }
Jeff Brownace13b12011-03-09 17:39:48 -08003780 }
Jeff Brown2352b972011-04-12 22:39:53 -07003781
Jeff Brownace13b12011-03-09 17:39:48 -08003782 if (!tapped) {
3783#if DEBUG_GESTURES
3784 LOGD("Gestures: NEUTRAL");
3785#endif
3786 mPointerGesture.activeGestureId = -1;
3787 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08003788 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07003789
Jeff Brown2352b972011-04-12 22:39:53 -07003790 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3791 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_NEUTRAL;
3792 mPointerGesture.spotIdBits.clear();
3793 moveSpotsLocked();
3794 }
Jeff Brownace13b12011-03-09 17:39:48 -08003795 }
3796 } else if (mCurrentTouch.pointerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003797 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08003798 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07003799 // When in HOVER, emit HOVER_MOVE events at the pointer location.
3800 // When in TAP_DRAG, emit MOVE events at the pointer location.
Jeff Brownb6110c22011-04-01 16:15:13 -07003801 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08003802
Jeff Brown79ac9692011-04-19 21:20:10 -07003803 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
3804 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
3805 if (when <= mPointerGesture.tapUpTime + TAP_DRAG_INTERVAL) {
3806 float x, y;
3807 mPointerController->getPosition(&x, &y);
3808 if (fabs(x - mPointerGesture.tapX) <= TAP_SLOP
3809 && fabs(y - mPointerGesture.tapY) <= TAP_SLOP) {
3810 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
3811 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08003812#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07003813 LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
3814 x - mPointerGesture.tapX,
3815 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08003816#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07003817 }
3818 } else {
3819#if DEBUG_GESTURES
3820 LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
3821 (when - mPointerGesture.tapUpTime) * 0.000001f);
3822#endif
3823 }
3824 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
3825 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
3826 }
Jeff Brownace13b12011-03-09 17:39:48 -08003827
3828 if (mLastTouch.idBits.hasBit(activeTouchId)) {
3829 const PointerData& currentPointer =
3830 mCurrentTouch.pointers[mCurrentTouch.idToIndex[activeTouchId]];
3831 const PointerData& lastPointer =
3832 mLastTouch.pointers[mLastTouch.idToIndex[activeTouchId]];
3833 float deltaX = (currentPointer.x - lastPointer.x)
3834 * mLocked.pointerGestureXMovementScale;
3835 float deltaY = (currentPointer.y - lastPointer.y)
3836 * mLocked.pointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07003837
3838 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07003839 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08003840 mPointerController->move(deltaX, deltaY);
3841 }
3842
Jeff Brown79ac9692011-04-19 21:20:10 -07003843 bool down;
3844 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
3845#if DEBUG_GESTURES
3846 LOGD("Gestures: TAP_DRAG");
3847#endif
3848 down = true;
3849 } else {
3850#if DEBUG_GESTURES
3851 LOGD("Gestures: HOVER");
3852#endif
3853 *outFinishPreviousGesture = true;
3854 mPointerGesture.activeGestureId = 0;
3855 down = false;
3856 }
Jeff Brownace13b12011-03-09 17:39:48 -08003857
3858 float x, y;
3859 mPointerController->getPosition(&x, &y);
3860
Jeff Brownace13b12011-03-09 17:39:48 -08003861 mPointerGesture.currentGestureIdBits.clear();
3862 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3863 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003864 mPointerGesture.currentGestureProperties[0].clear();
3865 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3866 mPointerGesture.currentGestureProperties[0].toolType =
3867 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08003868 mPointerGesture.currentGestureCoords[0].clear();
3869 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3870 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07003871 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
3872 down ? 1.0f : 0.0f);
3873
Jeff Brownace13b12011-03-09 17:39:48 -08003874 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003875 mPointerGesture.resetTap();
3876 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07003877 mPointerGesture.tapX = x;
3878 mPointerGesture.tapY = y;
3879 }
3880
Jeff Brown2352b972011-04-12 22:39:53 -07003881 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003882 mPointerGesture.spotGesture = down ? PointerControllerInterface::SPOT_GESTURE_DRAG
3883 : PointerControllerInterface::SPOT_GESTURE_HOVER;
Jeff Brown2352b972011-04-12 22:39:53 -07003884 mPointerGesture.spotIdBits.clear();
3885 mPointerGesture.spotIdBits.markBit(activeTouchId);
3886 mPointerGesture.spotIdToIndex[activeTouchId] = 0;
3887 mPointerGesture.spotCoords[0] = mPointerGesture.currentGestureCoords[0];
3888 moveSpotsLocked();
Jeff Brownace13b12011-03-09 17:39:48 -08003889 }
3890 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07003891 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
3892 // We need to provide feedback for each finger that goes down so we cannot wait
3893 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08003894 //
Jeff Brown2352b972011-04-12 22:39:53 -07003895 // The ambiguous case is deciding what to do when there are two fingers down but they
3896 // have not moved enough to determine whether they are part of a drag or part of a
3897 // freeform gesture, or just a press or long-press at the pointer location.
3898 //
3899 // When there are two fingers we start with the PRESS hypothesis and we generate a
3900 // down at the pointer location.
3901 //
3902 // When the two fingers move enough or when additional fingers are added, we make
3903 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Jeff Brownb6110c22011-04-01 16:15:13 -07003904 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08003905
Jeff Brown2352b972011-04-12 22:39:53 -07003906 bool needReference = false;
3907 bool settled = when >= mPointerGesture.firstTouchTime + MULTITOUCH_SETTLE_INTERVAL;
3908 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08003909 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
3910 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08003911 *outFinishPreviousGesture = true;
Jeff Brown2352b972011-04-12 22:39:53 -07003912 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
3913 mPointerGesture.activeGestureId = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003914
Jeff Brown2352b972011-04-12 22:39:53 -07003915 if (settled && mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3916 && mLastTouch.idBits.hasBit(mPointerGesture.activeTouchId)) {
3917 // The spot is already visible and has settled, use it as the reference point
3918 // for the gesture. Other spots will be positioned relative to this one.
3919#if DEBUG_GESTURES
3920 LOGD("Gestures: Using active spot as reference for MULTITOUCH, "
3921 "settle time expired %0.3fms ago",
3922 (when - mPointerGesture.firstTouchTime - MULTITOUCH_SETTLE_INTERVAL)
3923 * 0.000001f);
3924#endif
3925 const PointerData& d = mLastTouch.pointers[mLastTouch.idToIndex[
3926 mPointerGesture.activeTouchId]];
3927 mPointerGesture.referenceTouchX = d.x;
3928 mPointerGesture.referenceTouchY = d.y;
3929 const PointerCoords& c = mPointerGesture.spotCoords[mPointerGesture.spotIdToIndex[
3930 mPointerGesture.activeTouchId]];
3931 mPointerGesture.referenceGestureX = c.getAxisValue(AMOTION_EVENT_AXIS_X);
3932 mPointerGesture.referenceGestureY = c.getAxisValue(AMOTION_EVENT_AXIS_Y);
3933 } else {
3934#if DEBUG_GESTURES
3935 LOGD("Gestures: Using centroid as reference for MULTITOUCH, "
3936 "settle time remaining %0.3fms",
3937 (mPointerGesture.firstTouchTime + MULTITOUCH_SETTLE_INTERVAL - when)
3938 * 0.000001f);
3939#endif
3940 needReference = true;
Jeff Brownace13b12011-03-09 17:39:48 -08003941 }
Jeff Brown2352b972011-04-12 22:39:53 -07003942 } else if (!settled && mCurrentTouch.pointerCount > mLastTouch.pointerCount) {
3943 // Additional pointers have gone down but not yet settled.
3944 // Reset the gesture.
3945#if DEBUG_GESTURES
3946 LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
3947 "settle time remaining %0.3fms",
3948 (mPointerGesture.firstTouchTime + MULTITOUCH_SETTLE_INTERVAL - when)
3949 * 0.000001f);
3950#endif
3951 *outCancelPreviousGesture = true;
3952 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
3953 mPointerGesture.activeGestureId = 0;
Jeff Brownace13b12011-03-09 17:39:48 -08003954 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07003955 // Continue previous gesture.
Jeff Brownace13b12011-03-09 17:39:48 -08003956 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
3957 }
3958
Jeff Brown2352b972011-04-12 22:39:53 -07003959 if (needReference) {
3960 // Use the centroid and pointer location as the reference points for the gesture.
3961 mCurrentTouch.getCentroid(&mPointerGesture.referenceTouchX,
3962 &mPointerGesture.referenceTouchY);
3963 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
3964 &mPointerGesture.referenceGestureY);
3965 }
Jeff Brownace13b12011-03-09 17:39:48 -08003966
Jeff Brown2352b972011-04-12 22:39:53 -07003967 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
3968 float d;
3969 if (mCurrentTouch.pointerCount > 2) {
3970 // There are more than two pointers, switch to FREEFORM.
3971#if DEBUG_GESTURES
3972 LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
3973 mCurrentTouch.pointerCount);
3974#endif
3975 *outCancelPreviousGesture = true;
Jeff Brownace13b12011-03-09 17:39:48 -08003976 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown2352b972011-04-12 22:39:53 -07003977 } else if (((d = distance(
3978 mCurrentTouch.pointers[0].x, mCurrentTouch.pointers[0].y,
3979 mCurrentTouch.pointers[1].x, mCurrentTouch.pointers[1].y))
3980 > mLocked.pointerGestureMaxSwipeWidth)) {
3981 // There are two pointers but they are too far apart, switch to FREEFORM.
3982#if DEBUG_GESTURES
3983 LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
3984 d, mLocked.pointerGestureMaxSwipeWidth);
3985#endif
3986 *outCancelPreviousGesture = true;
3987 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
3988 } else {
3989 // There are two pointers. Wait for both pointers to start moving
3990 // before deciding whether this is a SWIPE or FREEFORM gesture.
3991 uint32_t id1 = mCurrentTouch.pointers[0].id;
3992 uint32_t id2 = mCurrentTouch.pointers[1].id;
Jeff Brownace13b12011-03-09 17:39:48 -08003993
Jeff Brown2352b972011-04-12 22:39:53 -07003994 float vx1, vy1, vx2, vy2;
3995 mPointerGesture.velocityTracker.getVelocity(id1, &vx1, &vy1);
3996 mPointerGesture.velocityTracker.getVelocity(id2, &vx2, &vy2);
Jeff Brownace13b12011-03-09 17:39:48 -08003997
Jeff Brown2352b972011-04-12 22:39:53 -07003998 float speed1 = hypotf(vx1, vy1);
3999 float speed2 = hypotf(vx2, vy2);
4000 if (speed1 >= MULTITOUCH_MIN_SPEED && speed2 >= MULTITOUCH_MIN_SPEED) {
4001 // Calculate the dot product of the velocity vectors.
Jeff Brownace13b12011-03-09 17:39:48 -08004002 // When the vectors are oriented in approximately the same direction,
4003 // the angle betweeen them is near zero and the cosine of the angle
4004 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
Jeff Brown2352b972011-04-12 22:39:53 -07004005 float dot = vx1 * vx2 + vy1 * vy2;
4006 float cosine = dot / (speed1 * speed2); // denominator always > 0
4007 if (cosine >= SWIPE_TRANSITION_ANGLE_COSINE) {
4008 // Pointers are moving in the same direction. Switch to SWIPE.
4009#if DEBUG_GESTURES
4010 LOGD("Gestures: PRESS transitioned to SWIPE, "
4011 "speed1 %0.3f >= %0.3f, speed2 %0.3f >= %0.3f, "
4012 "cosine %0.3f >= %0.3f",
4013 speed1, MULTITOUCH_MIN_SPEED, speed2, MULTITOUCH_MIN_SPEED,
4014 cosine, SWIPE_TRANSITION_ANGLE_COSINE);
4015#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004016 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
Jeff Brown2352b972011-04-12 22:39:53 -07004017 } else {
4018 // Pointers are moving in different directions. Switch to FREEFORM.
4019#if DEBUG_GESTURES
4020 LOGD("Gestures: PRESS transitioned to FREEFORM, "
4021 "speed1 %0.3f >= %0.3f, speed2 %0.3f >= %0.3f, "
4022 "cosine %0.3f < %0.3f",
4023 speed1, MULTITOUCH_MIN_SPEED, speed2, MULTITOUCH_MIN_SPEED,
4024 cosine, SWIPE_TRANSITION_ANGLE_COSINE);
4025#endif
4026 *outCancelPreviousGesture = true;
4027 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brownace13b12011-03-09 17:39:48 -08004028 }
4029 }
Jeff Brownace13b12011-03-09 17:39:48 -08004030 }
4031 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004032 // Switch from SWIPE to FREEFORM if additional pointers go down.
4033 // Cancel previous gesture.
4034 if (mCurrentTouch.pointerCount > 2) {
4035#if DEBUG_GESTURES
4036 LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
4037 mCurrentTouch.pointerCount);
4038#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004039 *outCancelPreviousGesture = true;
4040 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004041 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004042 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004043
Jeff Brown2352b972011-04-12 22:39:53 -07004044 // Move the reference points based on the overall group motion of the fingers.
4045 // The objective is to calculate a vector delta that is common to the movement
4046 // of all fingers.
4047 BitSet32 commonIdBits(mLastTouch.idBits.value & mCurrentTouch.idBits.value);
4048 if (!commonIdBits.isEmpty()) {
4049 float commonDeltaX = 0, commonDeltaY = 0;
4050 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4051 bool first = (idBits == commonIdBits);
4052 uint32_t id = idBits.firstMarkedBit();
4053 idBits.clearBit(id);
4054
4055 const PointerData& cpd = mCurrentTouch.pointers[mCurrentTouch.idToIndex[id]];
4056 const PointerData& lpd = mLastTouch.pointers[mLastTouch.idToIndex[id]];
4057 float deltaX = cpd.x - lpd.x;
4058 float deltaY = cpd.y - lpd.y;
4059
4060 if (first) {
4061 commonDeltaX = deltaX;
4062 commonDeltaY = deltaY;
4063 } else {
4064 commonDeltaX = calculateCommonVector(commonDeltaX, deltaX);
4065 commonDeltaY = calculateCommonVector(commonDeltaY, deltaY);
4066 }
4067 }
4068
4069 mPointerGesture.referenceTouchX += commonDeltaX;
4070 mPointerGesture.referenceTouchY += commonDeltaY;
4071 mPointerGesture.referenceGestureX +=
4072 commonDeltaX * mLocked.pointerGestureXMovementScale;
4073 mPointerGesture.referenceGestureY +=
4074 commonDeltaY * mLocked.pointerGestureYMovementScale;
4075 clampPositionUsingPointerBounds(mPointerController,
4076 &mPointerGesture.referenceGestureX,
4077 &mPointerGesture.referenceGestureY);
4078 }
4079
4080 // Report gestures.
4081 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4082 // PRESS mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004083#if DEBUG_GESTURES
Jeff Brown2352b972011-04-12 22:39:53 -07004084 LOGD("Gestures: PRESS activeTouchId=%d,"
Jeff Brownace13b12011-03-09 17:39:48 -08004085 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown2352b972011-04-12 22:39:53 -07004086 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004087#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004088 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004089
Jeff Brownace13b12011-03-09 17:39:48 -08004090 mPointerGesture.currentGestureIdBits.clear();
4091 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4092 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004093 mPointerGesture.currentGestureProperties[0].clear();
4094 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4095 mPointerGesture.currentGestureProperties[0].toolType =
4096 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004097 mPointerGesture.currentGestureCoords[0].clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004098 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4099 mPointerGesture.referenceGestureX);
4100 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4101 mPointerGesture.referenceGestureY);
Jeff Brownace13b12011-03-09 17:39:48 -08004102 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004103
Jeff Brown2352b972011-04-12 22:39:53 -07004104 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4105 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_PRESS;
4106 }
4107 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4108 // SWIPE mode.
4109#if DEBUG_GESTURES
4110 LOGD("Gestures: SWIPE activeTouchId=%d,"
4111 "activeGestureId=%d, currentTouchPointerCount=%d",
4112 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
4113#endif
4114 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
4115
4116 mPointerGesture.currentGestureIdBits.clear();
4117 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4118 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004119 mPointerGesture.currentGestureProperties[0].clear();
4120 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4121 mPointerGesture.currentGestureProperties[0].toolType =
4122 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004123 mPointerGesture.currentGestureCoords[0].clear();
4124 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4125 mPointerGesture.referenceGestureX);
4126 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4127 mPointerGesture.referenceGestureY);
4128 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4129
Jeff Brown2352b972011-04-12 22:39:53 -07004130 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4131 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_SWIPE;
4132 }
Jeff Brownace13b12011-03-09 17:39:48 -08004133 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4134 // FREEFORM mode.
4135#if DEBUG_GESTURES
4136 LOGD("Gestures: FREEFORM activeTouchId=%d,"
4137 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown2352b972011-04-12 22:39:53 -07004138 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004139#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004140 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004141
Jeff Brownace13b12011-03-09 17:39:48 -08004142 mPointerGesture.currentGestureIdBits.clear();
4143
4144 BitSet32 mappedTouchIdBits;
4145 BitSet32 usedGestureIdBits;
4146 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4147 // Initially, assign the active gesture id to the active touch point
4148 // if there is one. No other touch id bits are mapped yet.
4149 if (!*outCancelPreviousGesture) {
4150 mappedTouchIdBits.markBit(activeTouchId);
4151 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4152 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4153 mPointerGesture.activeGestureId;
4154 } else {
4155 mPointerGesture.activeGestureId = -1;
4156 }
4157 } else {
4158 // Otherwise, assume we mapped all touches from the previous frame.
4159 // Reuse all mappings that are still applicable.
4160 mappedTouchIdBits.value = mLastTouch.idBits.value & mCurrentTouch.idBits.value;
4161 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4162
4163 // Check whether we need to choose a new active gesture id because the
4164 // current went went up.
4165 for (BitSet32 upTouchIdBits(mLastTouch.idBits.value & ~mCurrentTouch.idBits.value);
4166 !upTouchIdBits.isEmpty(); ) {
4167 uint32_t upTouchId = upTouchIdBits.firstMarkedBit();
4168 upTouchIdBits.clearBit(upTouchId);
4169 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4170 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4171 mPointerGesture.activeGestureId = -1;
4172 break;
4173 }
4174 }
4175 }
4176
4177#if DEBUG_GESTURES
4178 LOGD("Gestures: FREEFORM follow up "
4179 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4180 "activeGestureId=%d",
4181 mappedTouchIdBits.value, usedGestureIdBits.value,
4182 mPointerGesture.activeGestureId);
4183#endif
4184
Jeff Brown2352b972011-04-12 22:39:53 -07004185 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
Jeff Brownace13b12011-03-09 17:39:48 -08004186 uint32_t touchId = mCurrentTouch.pointers[i].id;
4187 uint32_t gestureId;
4188 if (!mappedTouchIdBits.hasBit(touchId)) {
4189 gestureId = usedGestureIdBits.firstUnmarkedBit();
4190 usedGestureIdBits.markBit(gestureId);
4191 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4192#if DEBUG_GESTURES
4193 LOGD("Gestures: FREEFORM "
4194 "new mapping for touch id %d -> gesture id %d",
4195 touchId, gestureId);
4196#endif
4197 } else {
4198 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4199#if DEBUG_GESTURES
4200 LOGD("Gestures: FREEFORM "
4201 "existing mapping for touch id %d -> gesture id %d",
4202 touchId, gestureId);
4203#endif
4204 }
4205 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4206 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4207
Jeff Brown2352b972011-04-12 22:39:53 -07004208 float x = (mCurrentTouch.pointers[i].x - mPointerGesture.referenceTouchX)
4209 * mLocked.pointerGestureXZoomScale + mPointerGesture.referenceGestureX;
4210 float y = (mCurrentTouch.pointers[i].y - mPointerGesture.referenceTouchY)
4211 * mLocked.pointerGestureYZoomScale + mPointerGesture.referenceGestureY;
Jeff Brownace13b12011-03-09 17:39:48 -08004212
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004213 mPointerGesture.currentGestureProperties[i].clear();
4214 mPointerGesture.currentGestureProperties[i].id = gestureId;
4215 mPointerGesture.currentGestureProperties[i].toolType =
4216 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004217 mPointerGesture.currentGestureCoords[i].clear();
4218 mPointerGesture.currentGestureCoords[i].setAxisValue(
4219 AMOTION_EVENT_AXIS_X, x);
4220 mPointerGesture.currentGestureCoords[i].setAxisValue(
4221 AMOTION_EVENT_AXIS_Y, y);
4222 mPointerGesture.currentGestureCoords[i].setAxisValue(
4223 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4224 }
4225
4226 if (mPointerGesture.activeGestureId < 0) {
4227 mPointerGesture.activeGestureId =
4228 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4229#if DEBUG_GESTURES
4230 LOGD("Gestures: FREEFORM new "
4231 "activeGestureId=%d", mPointerGesture.activeGestureId);
4232#endif
4233 }
Jeff Brownace13b12011-03-09 17:39:48 -08004234
Jeff Brown2352b972011-04-12 22:39:53 -07004235 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4236 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_FREEFORM;
4237 }
4238 }
4239
4240 // Update spot locations for PRESS, SWIPE and FREEFORM.
4241 // We use the same calculation as we do to calculate the gesture pointers
4242 // for FREEFORM so that the spots smoothly track gestures.
4243 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4244 mPointerGesture.spotIdBits.clear();
4245 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
4246 uint32_t id = mCurrentTouch.pointers[i].id;
4247 mPointerGesture.spotIdBits.markBit(id);
4248 mPointerGesture.spotIdToIndex[id] = i;
4249
4250 float x = (mCurrentTouch.pointers[i].x - mPointerGesture.referenceTouchX)
4251 * mLocked.pointerGestureXZoomScale + mPointerGesture.referenceGestureX;
4252 float y = (mCurrentTouch.pointers[i].y - mPointerGesture.referenceTouchY)
4253 * mLocked.pointerGestureYZoomScale + mPointerGesture.referenceGestureY;
4254
4255 mPointerGesture.spotCoords[i].clear();
4256 mPointerGesture.spotCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4257 mPointerGesture.spotCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4258 mPointerGesture.spotCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4259 }
4260 moveSpotsLocked();
4261 }
Jeff Brownace13b12011-03-09 17:39:48 -08004262 }
4263
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004264 mPointerController->setButtonState(mCurrentTouch.buttonState);
4265
Jeff Brownace13b12011-03-09 17:39:48 -08004266#if DEBUG_GESTURES
4267 LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004268 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4269 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004270 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004271 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4272 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004273 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
4274 uint32_t id = idBits.firstMarkedBit();
4275 idBits.clearBit(id);
4276 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004277 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004278 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004279 LOGD(" currentGesture[%d]: index=%d, toolType=%d, "
4280 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4281 id, index, properties.toolType,
4282 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004283 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4284 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4285 }
4286 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
4287 uint32_t id = idBits.firstMarkedBit();
4288 idBits.clearBit(id);
4289 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004290 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004291 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004292 LOGD(" lastGesture[%d]: index=%d, toolType=%d, "
4293 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4294 id, index, properties.toolType,
4295 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004296 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4297 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4298 }
4299#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004300 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004301}
4302
Jeff Brown2352b972011-04-12 22:39:53 -07004303void TouchInputMapper::moveSpotsLocked() {
4304 mPointerController->setSpots(mPointerGesture.spotGesture,
4305 mPointerGesture.spotCoords, mPointerGesture.spotIdToIndex, mPointerGesture.spotIdBits);
4306}
4307
Jeff Brownace13b12011-03-09 17:39:48 -08004308void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004309 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
4310 const PointerProperties* properties, const PointerCoords* coords,
4311 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08004312 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
4313 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004314 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08004315 uint32_t pointerCount = 0;
4316 while (!idBits.isEmpty()) {
4317 uint32_t id = idBits.firstMarkedBit();
4318 idBits.clearBit(id);
4319 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004320 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004321 pointerCoords[pointerCount].copyFrom(coords[index]);
4322
4323 if (changedId >= 0 && id == uint32_t(changedId)) {
4324 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
4325 }
4326
4327 pointerCount += 1;
4328 }
4329
Jeff Brownb6110c22011-04-01 16:15:13 -07004330 LOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004331
4332 if (changedId >= 0 && pointerCount == 1) {
4333 // Replace initial down and final up action.
4334 // We can compare the action without masking off the changed pointer index
4335 // because we know the index is 0.
4336 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
4337 action = AMOTION_EVENT_ACTION_DOWN;
4338 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
4339 action = AMOTION_EVENT_ACTION_UP;
4340 } else {
4341 // Can't happen.
Jeff Brownb6110c22011-04-01 16:15:13 -07004342 LOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08004343 }
4344 }
4345
4346 getDispatcher()->notifyMotion(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004347 action, flags, metaState, buttonState, edgeFlags,
4348 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownace13b12011-03-09 17:39:48 -08004349}
4350
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004351bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004352 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004353 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
4354 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08004355 bool changed = false;
4356 while (!idBits.isEmpty()) {
4357 uint32_t id = idBits.firstMarkedBit();
4358 idBits.clearBit(id);
4359
4360 uint32_t inIndex = inIdToIndex[id];
4361 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004362
4363 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004364 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004365 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004366 PointerCoords& curOutCoords = outCoords[outIndex];
4367
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004368 if (curInProperties != curOutProperties) {
4369 curOutProperties.copyFrom(curInProperties);
4370 changed = true;
4371 }
4372
Jeff Brownace13b12011-03-09 17:39:48 -08004373 if (curInCoords != curOutCoords) {
4374 curOutCoords.copyFrom(curInCoords);
4375 changed = true;
4376 }
4377 }
4378 return changed;
4379}
4380
4381void TouchInputMapper::fadePointer() {
4382 { // acquire lock
4383 AutoMutex _l(mLock);
4384 if (mPointerController != NULL) {
4385 mPointerController->fade();
4386 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004387 } // release lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004388}
4389
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004390int32_t TouchInputMapper::getTouchToolType(bool isStylus) const {
4391 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
4392 return isStylus ? AMOTION_EVENT_TOOL_TYPE_STYLUS : AMOTION_EVENT_TOOL_TYPE_FINGER;
4393 } else {
4394 return isStylus ? AMOTION_EVENT_TOOL_TYPE_INDIRECT_STYLUS
4395 : AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
4396 }
4397}
4398
Jeff Brown6328cdc2010-07-29 18:18:33 -07004399bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) {
Jeff Brown9626b142011-03-03 02:09:54 -08004400 return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue
4401 && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004402}
4403
Jeff Brown6328cdc2010-07-29 18:18:33 -07004404const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked(
4405 int32_t x, int32_t y) {
4406 size_t numVirtualKeys = mLocked.virtualKeys.size();
4407 for (size_t i = 0; i < numVirtualKeys; i++) {
4408 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004409
4410#if DEBUG_VIRTUAL_KEYS
4411 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
4412 "left=%d, top=%d, right=%d, bottom=%d",
4413 x, y,
4414 virtualKey.keyCode, virtualKey.scanCode,
4415 virtualKey.hitLeft, virtualKey.hitTop,
4416 virtualKey.hitRight, virtualKey.hitBottom);
4417#endif
4418
4419 if (virtualKey.isHit(x, y)) {
4420 return & virtualKey;
4421 }
4422 }
4423
4424 return NULL;
4425}
4426
4427void TouchInputMapper::calculatePointerIds() {
4428 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
4429 uint32_t lastPointerCount = mLastTouch.pointerCount;
4430
4431 if (currentPointerCount == 0) {
4432 // No pointers to assign.
4433 mCurrentTouch.idBits.clear();
4434 } else if (lastPointerCount == 0) {
4435 // All pointers are new.
4436 mCurrentTouch.idBits.clear();
4437 for (uint32_t i = 0; i < currentPointerCount; i++) {
4438 mCurrentTouch.pointers[i].id = i;
4439 mCurrentTouch.idToIndex[i] = i;
4440 mCurrentTouch.idBits.markBit(i);
4441 }
4442 } else if (currentPointerCount == 1 && lastPointerCount == 1) {
4443 // Only one pointer and no change in count so it must have the same id as before.
4444 uint32_t id = mLastTouch.pointers[0].id;
4445 mCurrentTouch.pointers[0].id = id;
4446 mCurrentTouch.idToIndex[id] = 0;
4447 mCurrentTouch.idBits.value = BitSet32::valueForBit(id);
4448 } else {
4449 // General case.
4450 // We build a heap of squared euclidean distances between current and last pointers
4451 // associated with the current and last pointer indices. Then, we find the best
4452 // match (by distance) for each current pointer.
4453 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
4454
4455 uint32_t heapSize = 0;
4456 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
4457 currentPointerIndex++) {
4458 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
4459 lastPointerIndex++) {
4460 int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x
4461 - mLastTouch.pointers[lastPointerIndex].x;
4462 int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y
4463 - mLastTouch.pointers[lastPointerIndex].y;
4464
4465 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
4466
4467 // Insert new element into the heap (sift up).
4468 heap[heapSize].currentPointerIndex = currentPointerIndex;
4469 heap[heapSize].lastPointerIndex = lastPointerIndex;
4470 heap[heapSize].distance = distance;
4471 heapSize += 1;
4472 }
4473 }
4474
4475 // Heapify
4476 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
4477 startIndex -= 1;
4478 for (uint32_t parentIndex = startIndex; ;) {
4479 uint32_t childIndex = parentIndex * 2 + 1;
4480 if (childIndex >= heapSize) {
4481 break;
4482 }
4483
4484 if (childIndex + 1 < heapSize
4485 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4486 childIndex += 1;
4487 }
4488
4489 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4490 break;
4491 }
4492
4493 swap(heap[parentIndex], heap[childIndex]);
4494 parentIndex = childIndex;
4495 }
4496 }
4497
4498#if DEBUG_POINTER_ASSIGNMENT
4499 LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize);
4500 for (size_t i = 0; i < heapSize; i++) {
4501 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4502 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4503 heap[i].distance);
4504 }
4505#endif
4506
4507 // Pull matches out by increasing order of distance.
4508 // To avoid reassigning pointers that have already been matched, the loop keeps track
4509 // of which last and current pointers have been matched using the matchedXXXBits variables.
4510 // It also tracks the used pointer id bits.
4511 BitSet32 matchedLastBits(0);
4512 BitSet32 matchedCurrentBits(0);
4513 BitSet32 usedIdBits(0);
4514 bool first = true;
4515 for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) {
4516 for (;;) {
4517 if (first) {
4518 // The first time through the loop, we just consume the root element of
4519 // the heap (the one with smallest distance).
4520 first = false;
4521 } else {
4522 // Previous iterations consumed the root element of the heap.
4523 // Pop root element off of the heap (sift down).
4524 heapSize -= 1;
Jeff Brownb6110c22011-04-01 16:15:13 -07004525 LOG_ASSERT(heapSize > 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004526
4527 // Sift down.
4528 heap[0] = heap[heapSize];
4529 for (uint32_t parentIndex = 0; ;) {
4530 uint32_t childIndex = parentIndex * 2 + 1;
4531 if (childIndex >= heapSize) {
4532 break;
4533 }
4534
4535 if (childIndex + 1 < heapSize
4536 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4537 childIndex += 1;
4538 }
4539
4540 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4541 break;
4542 }
4543
4544 swap(heap[parentIndex], heap[childIndex]);
4545 parentIndex = childIndex;
4546 }
4547
4548#if DEBUG_POINTER_ASSIGNMENT
4549 LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize);
4550 for (size_t i = 0; i < heapSize; i++) {
4551 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4552 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4553 heap[i].distance);
4554 }
4555#endif
4556 }
4557
4558 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
4559 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
4560
4561 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
4562 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
4563
4564 matchedCurrentBits.markBit(currentPointerIndex);
4565 matchedLastBits.markBit(lastPointerIndex);
4566
4567 uint32_t id = mLastTouch.pointers[lastPointerIndex].id;
4568 mCurrentTouch.pointers[currentPointerIndex].id = id;
4569 mCurrentTouch.idToIndex[id] = currentPointerIndex;
4570 usedIdBits.markBit(id);
4571
4572#if DEBUG_POINTER_ASSIGNMENT
4573 LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
4574 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
4575#endif
4576 break;
4577 }
4578 }
4579
4580 // Assign fresh ids to new pointers.
4581 if (currentPointerCount > lastPointerCount) {
4582 for (uint32_t i = currentPointerCount - lastPointerCount; ;) {
4583 uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit();
4584 uint32_t id = usedIdBits.firstUnmarkedBit();
4585
4586 mCurrentTouch.pointers[currentPointerIndex].id = id;
4587 mCurrentTouch.idToIndex[id] = currentPointerIndex;
4588 usedIdBits.markBit(id);
4589
4590#if DEBUG_POINTER_ASSIGNMENT
4591 LOGD("calculatePointerIds - assigned: cur=%d, id=%d",
4592 currentPointerIndex, id);
4593#endif
4594
4595 if (--i == 0) break; // done
4596 matchedCurrentBits.markBit(currentPointerIndex);
4597 }
4598 }
4599
4600 // Fix id bits.
4601 mCurrentTouch.idBits = usedIdBits;
4602 }
4603}
4604
4605/* Special hack for devices that have bad screen data: if one of the
4606 * points has moved more than a screen height from the last position,
4607 * then drop it. */
4608bool TouchInputMapper::applyBadTouchFilter() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004609 uint32_t pointerCount = mCurrentTouch.pointerCount;
4610
4611 // Nothing to do if there are no points.
4612 if (pointerCount == 0) {
4613 return false;
4614 }
4615
4616 // Don't do anything if a finger is going down or up. We run
4617 // here before assigning pointer IDs, so there isn't a good
4618 // way to do per-finger matching.
4619 if (pointerCount != mLastTouch.pointerCount) {
4620 return false;
4621 }
4622
4623 // We consider a single movement across more than a 7/16 of
4624 // the long size of the screen to be bad. This was a magic value
4625 // determined by looking at the maximum distance it is feasible
4626 // to actually move in one sample.
Jeff Brown9626b142011-03-03 02:09:54 -08004627 int32_t maxDeltaY = (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1) * 7 / 16;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004628
4629 // XXX The original code in InputDevice.java included commented out
4630 // code for testing the X axis. Note that when we drop a point
4631 // we don't actually restore the old X either. Strange.
4632 // The old code also tries to track when bad points were previously
4633 // detected but it turns out that due to the placement of a "break"
4634 // at the end of the loop, we never set mDroppedBadPoint to true
4635 // so it is effectively dead code.
4636 // Need to figure out if the old code is busted or just overcomplicated
4637 // but working as intended.
4638
4639 // Look through all new points and see if any are farther than
4640 // acceptable from all previous points.
4641 for (uint32_t i = pointerCount; i-- > 0; ) {
4642 int32_t y = mCurrentTouch.pointers[i].y;
4643 int32_t closestY = INT_MAX;
4644 int32_t closestDeltaY = 0;
4645
4646#if DEBUG_HACKS
4647 LOGD("BadTouchFilter: Looking at next point #%d: y=%d", i, y);
4648#endif
4649
4650 for (uint32_t j = pointerCount; j-- > 0; ) {
4651 int32_t lastY = mLastTouch.pointers[j].y;
4652 int32_t deltaY = abs(y - lastY);
4653
4654#if DEBUG_HACKS
4655 LOGD("BadTouchFilter: Comparing with last point #%d: y=%d deltaY=%d",
4656 j, lastY, deltaY);
4657#endif
4658
4659 if (deltaY < maxDeltaY) {
4660 goto SkipSufficientlyClosePoint;
4661 }
4662 if (deltaY < closestDeltaY) {
4663 closestDeltaY = deltaY;
4664 closestY = lastY;
4665 }
4666 }
4667
4668 // Must not have found a close enough match.
4669#if DEBUG_HACKS
4670 LOGD("BadTouchFilter: Dropping bad point #%d: newY=%d oldY=%d deltaY=%d maxDeltaY=%d",
4671 i, y, closestY, closestDeltaY, maxDeltaY);
4672#endif
4673
4674 mCurrentTouch.pointers[i].y = closestY;
4675 return true; // XXX original code only corrects one point
4676
4677 SkipSufficientlyClosePoint: ;
4678 }
4679
4680 // No change.
4681 return false;
4682}
4683
4684/* Special hack for devices that have bad screen data: drop points where
4685 * the coordinate value for one axis has jumped to the other pointer's location.
4686 */
4687bool TouchInputMapper::applyJumpyTouchFilter() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004688 uint32_t pointerCount = mCurrentTouch.pointerCount;
4689 if (mLastTouch.pointerCount != pointerCount) {
4690#if DEBUG_HACKS
4691 LOGD("JumpyTouchFilter: Different pointer count %d -> %d",
4692 mLastTouch.pointerCount, pointerCount);
4693 for (uint32_t i = 0; i < pointerCount; i++) {
4694 LOGD(" Pointer %d (%d, %d)", i,
4695 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
4696 }
4697#endif
4698
4699 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_TRANSITION_DROPS) {
4700 if (mLastTouch.pointerCount == 1 && pointerCount == 2) {
4701 // Just drop the first few events going from 1 to 2 pointers.
4702 // They're bad often enough that they're not worth considering.
4703 mCurrentTouch.pointerCount = 1;
4704 mJumpyTouchFilter.jumpyPointsDropped += 1;
4705
4706#if DEBUG_HACKS
4707 LOGD("JumpyTouchFilter: Pointer 2 dropped");
4708#endif
4709 return true;
4710 } else if (mLastTouch.pointerCount == 2 && pointerCount == 1) {
4711 // The event when we go from 2 -> 1 tends to be messed up too
4712 mCurrentTouch.pointerCount = 2;
4713 mCurrentTouch.pointers[0] = mLastTouch.pointers[0];
4714 mCurrentTouch.pointers[1] = mLastTouch.pointers[1];
4715 mJumpyTouchFilter.jumpyPointsDropped += 1;
4716
4717#if DEBUG_HACKS
4718 for (int32_t i = 0; i < 2; i++) {
4719 LOGD("JumpyTouchFilter: Pointer %d replaced (%d, %d)", i,
4720 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
4721 }
4722#endif
4723 return true;
4724 }
4725 }
4726 // Reset jumpy points dropped on other transitions or if limit exceeded.
4727 mJumpyTouchFilter.jumpyPointsDropped = 0;
4728
4729#if DEBUG_HACKS
4730 LOGD("JumpyTouchFilter: Transition - drop limit reset");
4731#endif
4732 return false;
4733 }
4734
4735 // We have the same number of pointers as last time.
4736 // A 'jumpy' point is one where the coordinate value for one axis
4737 // has jumped to the other pointer's location. No need to do anything
4738 // else if we only have one pointer.
4739 if (pointerCount < 2) {
4740 return false;
4741 }
4742
4743 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_DROP_LIMIT) {
Jeff Brown9626b142011-03-03 02:09:54 -08004744 int jumpyEpsilon = (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1) / JUMPY_EPSILON_DIVISOR;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004745
4746 // We only replace the single worst jumpy point as characterized by pointer distance
4747 // in a single axis.
4748 int32_t badPointerIndex = -1;
4749 int32_t badPointerReplacementIndex = -1;
4750 int32_t badPointerDistance = INT_MIN; // distance to be corrected
4751
4752 for (uint32_t i = pointerCount; i-- > 0; ) {
4753 int32_t x = mCurrentTouch.pointers[i].x;
4754 int32_t y = mCurrentTouch.pointers[i].y;
4755
4756#if DEBUG_HACKS
4757 LOGD("JumpyTouchFilter: Point %d (%d, %d)", i, x, y);
4758#endif
4759
4760 // Check if a touch point is too close to another's coordinates
4761 bool dropX = false, dropY = false;
4762 for (uint32_t j = 0; j < pointerCount; j++) {
4763 if (i == j) {
4764 continue;
4765 }
4766
4767 if (abs(x - mCurrentTouch.pointers[j].x) <= jumpyEpsilon) {
4768 dropX = true;
4769 break;
4770 }
4771
4772 if (abs(y - mCurrentTouch.pointers[j].y) <= jumpyEpsilon) {
4773 dropY = true;
4774 break;
4775 }
4776 }
4777 if (! dropX && ! dropY) {
4778 continue; // not jumpy
4779 }
4780
4781 // Find a replacement candidate by comparing with older points on the
4782 // complementary (non-jumpy) axis.
4783 int32_t distance = INT_MIN; // distance to be corrected
4784 int32_t replacementIndex = -1;
4785
4786 if (dropX) {
4787 // X looks too close. Find an older replacement point with a close Y.
4788 int32_t smallestDeltaY = INT_MAX;
4789 for (uint32_t j = 0; j < pointerCount; j++) {
4790 int32_t deltaY = abs(y - mLastTouch.pointers[j].y);
4791 if (deltaY < smallestDeltaY) {
4792 smallestDeltaY = deltaY;
4793 replacementIndex = j;
4794 }
4795 }
4796 distance = abs(x - mLastTouch.pointers[replacementIndex].x);
4797 } else {
4798 // Y looks too close. Find an older replacement point with a close X.
4799 int32_t smallestDeltaX = INT_MAX;
4800 for (uint32_t j = 0; j < pointerCount; j++) {
4801 int32_t deltaX = abs(x - mLastTouch.pointers[j].x);
4802 if (deltaX < smallestDeltaX) {
4803 smallestDeltaX = deltaX;
4804 replacementIndex = j;
4805 }
4806 }
4807 distance = abs(y - mLastTouch.pointers[replacementIndex].y);
4808 }
4809
4810 // If replacing this pointer would correct a worse error than the previous ones
4811 // considered, then use this replacement instead.
4812 if (distance > badPointerDistance) {
4813 badPointerIndex = i;
4814 badPointerReplacementIndex = replacementIndex;
4815 badPointerDistance = distance;
4816 }
4817 }
4818
4819 // Correct the jumpy pointer if one was found.
4820 if (badPointerIndex >= 0) {
4821#if DEBUG_HACKS
4822 LOGD("JumpyTouchFilter: Replacing bad pointer %d with (%d, %d)",
4823 badPointerIndex,
4824 mLastTouch.pointers[badPointerReplacementIndex].x,
4825 mLastTouch.pointers[badPointerReplacementIndex].y);
4826#endif
4827
4828 mCurrentTouch.pointers[badPointerIndex].x =
4829 mLastTouch.pointers[badPointerReplacementIndex].x;
4830 mCurrentTouch.pointers[badPointerIndex].y =
4831 mLastTouch.pointers[badPointerReplacementIndex].y;
4832 mJumpyTouchFilter.jumpyPointsDropped += 1;
4833 return true;
4834 }
4835 }
4836
4837 mJumpyTouchFilter.jumpyPointsDropped = 0;
4838 return false;
4839}
4840
4841/* Special hack for devices that have bad screen data: aggregate and
4842 * compute averages of the coordinate data, to reduce the amount of
4843 * jitter seen by applications. */
4844void TouchInputMapper::applyAveragingTouchFilter() {
4845 for (uint32_t currentIndex = 0; currentIndex < mCurrentTouch.pointerCount; currentIndex++) {
4846 uint32_t id = mCurrentTouch.pointers[currentIndex].id;
4847 int32_t x = mCurrentTouch.pointers[currentIndex].x;
4848 int32_t y = mCurrentTouch.pointers[currentIndex].y;
Jeff Brown8d608662010-08-30 03:02:23 -07004849 int32_t pressure;
4850 switch (mCalibration.pressureSource) {
4851 case Calibration::PRESSURE_SOURCE_PRESSURE:
4852 pressure = mCurrentTouch.pointers[currentIndex].pressure;
4853 break;
4854 case Calibration::PRESSURE_SOURCE_TOUCH:
4855 pressure = mCurrentTouch.pointers[currentIndex].touchMajor;
4856 break;
4857 default:
4858 pressure = 1;
4859 break;
4860 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004861
4862 if (mLastTouch.idBits.hasBit(id)) {
4863 // Pointer was down before and is still down now.
4864 // Compute average over history trace.
4865 uint32_t start = mAveragingTouchFilter.historyStart[id];
4866 uint32_t end = mAveragingTouchFilter.historyEnd[id];
4867
4868 int64_t deltaX = x - mAveragingTouchFilter.historyData[end].pointers[id].x;
4869 int64_t deltaY = y - mAveragingTouchFilter.historyData[end].pointers[id].y;
4870 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
4871
4872#if DEBUG_HACKS
4873 LOGD("AveragingTouchFilter: Pointer id %d - Distance from last sample: %lld",
4874 id, distance);
4875#endif
4876
4877 if (distance < AVERAGING_DISTANCE_LIMIT) {
4878 // Increment end index in preparation for recording new historical data.
4879 end += 1;
4880 if (end > AVERAGING_HISTORY_SIZE) {
4881 end = 0;
4882 }
4883
4884 // If the end index has looped back to the start index then we have filled
4885 // the historical trace up to the desired size so we drop the historical
4886 // data at the start of the trace.
4887 if (end == start) {
4888 start += 1;
4889 if (start > AVERAGING_HISTORY_SIZE) {
4890 start = 0;
4891 }
4892 }
4893
4894 // Add the raw data to the historical trace.
4895 mAveragingTouchFilter.historyStart[id] = start;
4896 mAveragingTouchFilter.historyEnd[id] = end;
4897 mAveragingTouchFilter.historyData[end].pointers[id].x = x;
4898 mAveragingTouchFilter.historyData[end].pointers[id].y = y;
4899 mAveragingTouchFilter.historyData[end].pointers[id].pressure = pressure;
4900
4901 // Average over all historical positions in the trace by total pressure.
4902 int32_t averagedX = 0;
4903 int32_t averagedY = 0;
4904 int32_t totalPressure = 0;
4905 for (;;) {
4906 int32_t historicalX = mAveragingTouchFilter.historyData[start].pointers[id].x;
4907 int32_t historicalY = mAveragingTouchFilter.historyData[start].pointers[id].y;
4908 int32_t historicalPressure = mAveragingTouchFilter.historyData[start]
4909 .pointers[id].pressure;
4910
4911 averagedX += historicalX * historicalPressure;
4912 averagedY += historicalY * historicalPressure;
4913 totalPressure += historicalPressure;
4914
4915 if (start == end) {
4916 break;
4917 }
4918
4919 start += 1;
4920 if (start > AVERAGING_HISTORY_SIZE) {
4921 start = 0;
4922 }
4923 }
4924
Jeff Brown8d608662010-08-30 03:02:23 -07004925 if (totalPressure != 0) {
4926 averagedX /= totalPressure;
4927 averagedY /= totalPressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004928
4929#if DEBUG_HACKS
Jeff Brown8d608662010-08-30 03:02:23 -07004930 LOGD("AveragingTouchFilter: Pointer id %d - "
4931 "totalPressure=%d, averagedX=%d, averagedY=%d", id, totalPressure,
4932 averagedX, averagedY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004933#endif
4934
Jeff Brown8d608662010-08-30 03:02:23 -07004935 mCurrentTouch.pointers[currentIndex].x = averagedX;
4936 mCurrentTouch.pointers[currentIndex].y = averagedY;
4937 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07004938 } else {
4939#if DEBUG_HACKS
4940 LOGD("AveragingTouchFilter: Pointer id %d - Exceeded max distance", id);
4941#endif
4942 }
4943 } else {
4944#if DEBUG_HACKS
4945 LOGD("AveragingTouchFilter: Pointer id %d - Pointer went up", id);
4946#endif
4947 }
4948
4949 // Reset pointer history.
4950 mAveragingTouchFilter.historyStart[id] = 0;
4951 mAveragingTouchFilter.historyEnd[id] = 0;
4952 mAveragingTouchFilter.historyData[0].pointers[id].x = x;
4953 mAveragingTouchFilter.historyData[0].pointers[id].y = y;
4954 mAveragingTouchFilter.historyData[0].pointers[id].pressure = pressure;
4955 }
4956}
4957
4958int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07004959 { // acquire lock
4960 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004961
Jeff Brown6328cdc2010-07-29 18:18:33 -07004962 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004963 return AKEY_STATE_VIRTUAL;
4964 }
4965
Jeff Brown6328cdc2010-07-29 18:18:33 -07004966 size_t numVirtualKeys = mLocked.virtualKeys.size();
4967 for (size_t i = 0; i < numVirtualKeys; i++) {
4968 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004969 if (virtualKey.keyCode == keyCode) {
4970 return AKEY_STATE_UP;
4971 }
4972 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004973 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07004974
4975 return AKEY_STATE_UNKNOWN;
4976}
4977
4978int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07004979 { // acquire lock
4980 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004981
Jeff Brown6328cdc2010-07-29 18:18:33 -07004982 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004983 return AKEY_STATE_VIRTUAL;
4984 }
4985
Jeff Brown6328cdc2010-07-29 18:18:33 -07004986 size_t numVirtualKeys = mLocked.virtualKeys.size();
4987 for (size_t i = 0; i < numVirtualKeys; i++) {
4988 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004989 if (virtualKey.scanCode == scanCode) {
4990 return AKEY_STATE_UP;
4991 }
4992 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004993 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07004994
4995 return AKEY_STATE_UNKNOWN;
4996}
4997
4998bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
4999 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07005000 { // acquire lock
5001 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005002
Jeff Brown6328cdc2010-07-29 18:18:33 -07005003 size_t numVirtualKeys = mLocked.virtualKeys.size();
5004 for (size_t i = 0; i < numVirtualKeys; i++) {
5005 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005006
5007 for (size_t i = 0; i < numCodes; i++) {
5008 if (virtualKey.keyCode == keyCodes[i]) {
5009 outFlags[i] = 1;
5010 }
5011 }
5012 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07005013 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07005014
5015 return true;
5016}
5017
5018
5019// --- SingleTouchInputMapper ---
5020
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005021SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5022 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005023 initialize();
5024}
5025
5026SingleTouchInputMapper::~SingleTouchInputMapper() {
5027}
5028
5029void SingleTouchInputMapper::initialize() {
5030 mAccumulator.clear();
5031
5032 mDown = false;
5033 mX = 0;
5034 mY = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07005035 mPressure = 0; // default to 0 for devices that don't report pressure
5036 mToolWidth = 0; // default to 0 for devices that don't report tool width
Jeff Brownace13b12011-03-09 17:39:48 -08005037 mButtonState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005038}
5039
5040void SingleTouchInputMapper::reset() {
5041 TouchInputMapper::reset();
5042
Jeff Brown6d0fec22010-07-23 21:28:06 -07005043 initialize();
5044 }
5045
5046void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
5047 switch (rawEvent->type) {
5048 case EV_KEY:
5049 switch (rawEvent->scanCode) {
5050 case BTN_TOUCH:
5051 mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH;
5052 mAccumulator.btnTouch = rawEvent->value != 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005053 // Don't sync immediately. Wait until the next SYN_REPORT since we might
5054 // not have received valid position information yet. This logic assumes that
5055 // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet.
Jeff Brown6d0fec22010-07-23 21:28:06 -07005056 break;
Jeff Brownace13b12011-03-09 17:39:48 -08005057 default:
5058 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005059 int32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode);
Jeff Brownace13b12011-03-09 17:39:48 -08005060 if (buttonState) {
5061 if (rawEvent->value) {
5062 mAccumulator.buttonDown |= buttonState;
5063 } else {
5064 mAccumulator.buttonUp |= buttonState;
5065 }
5066 mAccumulator.fields |= Accumulator::FIELD_BUTTONS;
5067 }
5068 }
5069 break;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005070 }
5071 break;
5072
5073 case EV_ABS:
5074 switch (rawEvent->scanCode) {
5075 case ABS_X:
5076 mAccumulator.fields |= Accumulator::FIELD_ABS_X;
5077 mAccumulator.absX = rawEvent->value;
5078 break;
5079 case ABS_Y:
5080 mAccumulator.fields |= Accumulator::FIELD_ABS_Y;
5081 mAccumulator.absY = rawEvent->value;
5082 break;
5083 case ABS_PRESSURE:
5084 mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE;
5085 mAccumulator.absPressure = rawEvent->value;
5086 break;
5087 case ABS_TOOL_WIDTH:
5088 mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH;
5089 mAccumulator.absToolWidth = rawEvent->value;
5090 break;
5091 }
5092 break;
5093
5094 case EV_SYN:
5095 switch (rawEvent->scanCode) {
5096 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005097 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005098 break;
5099 }
5100 break;
5101 }
5102}
5103
5104void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005105 uint32_t fields = mAccumulator.fields;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005106 if (fields == 0) {
5107 return; // no new state changes, so nothing to do
5108 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005109
5110 if (fields & Accumulator::FIELD_BTN_TOUCH) {
5111 mDown = mAccumulator.btnTouch;
5112 }
5113
5114 if (fields & Accumulator::FIELD_ABS_X) {
5115 mX = mAccumulator.absX;
5116 }
5117
5118 if (fields & Accumulator::FIELD_ABS_Y) {
5119 mY = mAccumulator.absY;
5120 }
5121
5122 if (fields & Accumulator::FIELD_ABS_PRESSURE) {
5123 mPressure = mAccumulator.absPressure;
5124 }
5125
5126 if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) {
Jeff Brown8d608662010-08-30 03:02:23 -07005127 mToolWidth = mAccumulator.absToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005128 }
5129
Jeff Brownace13b12011-03-09 17:39:48 -08005130 if (fields & Accumulator::FIELD_BUTTONS) {
5131 mButtonState = (mButtonState | mAccumulator.buttonDown) & ~mAccumulator.buttonUp;
5132 }
5133
Jeff Brown6d0fec22010-07-23 21:28:06 -07005134 mCurrentTouch.clear();
5135
5136 if (mDown) {
5137 mCurrentTouch.pointerCount = 1;
5138 mCurrentTouch.pointers[0].id = 0;
5139 mCurrentTouch.pointers[0].x = mX;
5140 mCurrentTouch.pointers[0].y = mY;
5141 mCurrentTouch.pointers[0].pressure = mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -07005142 mCurrentTouch.pointers[0].touchMajor = 0;
5143 mCurrentTouch.pointers[0].touchMinor = 0;
5144 mCurrentTouch.pointers[0].toolMajor = mToolWidth;
5145 mCurrentTouch.pointers[0].toolMinor = mToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005146 mCurrentTouch.pointers[0].orientation = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005147 mCurrentTouch.pointers[0].isStylus = false; // TODO: Set stylus
Jeff Brown6d0fec22010-07-23 21:28:06 -07005148 mCurrentTouch.idToIndex[0] = 0;
5149 mCurrentTouch.idBits.markBit(0);
Jeff Brownace13b12011-03-09 17:39:48 -08005150 mCurrentTouch.buttonState = mButtonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005151 }
5152
5153 syncTouch(when, true);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005154
5155 mAccumulator.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005156}
5157
Jeff Brown8d608662010-08-30 03:02:23 -07005158void SingleTouchInputMapper::configureRawAxes() {
5159 TouchInputMapper::configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005160
Jeff Brown8d608662010-08-30 03:02:23 -07005161 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x);
5162 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y);
5163 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure);
5164 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005165}
5166
5167
5168// --- MultiTouchInputMapper ---
5169
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005170MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
5171 TouchInputMapper(device) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005172 initialize();
5173}
5174
5175MultiTouchInputMapper::~MultiTouchInputMapper() {
5176}
5177
5178void MultiTouchInputMapper::initialize() {
5179 mAccumulator.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08005180 mButtonState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005181}
5182
5183void MultiTouchInputMapper::reset() {
5184 TouchInputMapper::reset();
5185
Jeff Brown6d0fec22010-07-23 21:28:06 -07005186 initialize();
5187}
5188
5189void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
5190 switch (rawEvent->type) {
Jeff Brownace13b12011-03-09 17:39:48 -08005191 case EV_KEY: {
5192 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005193 int32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode);
Jeff Brownace13b12011-03-09 17:39:48 -08005194 if (buttonState) {
5195 if (rawEvent->value) {
5196 mAccumulator.buttonDown |= buttonState;
5197 } else {
5198 mAccumulator.buttonUp |= buttonState;
5199 }
5200 }
5201 }
5202 break;
5203 }
5204
Jeff Brown6d0fec22010-07-23 21:28:06 -07005205 case EV_ABS: {
5206 uint32_t pointerIndex = mAccumulator.pointerCount;
5207 Accumulator::Pointer* pointer = & mAccumulator.pointers[pointerIndex];
5208
5209 switch (rawEvent->scanCode) {
5210 case ABS_MT_POSITION_X:
5211 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_X;
5212 pointer->absMTPositionX = rawEvent->value;
5213 break;
5214 case ABS_MT_POSITION_Y:
5215 pointer->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y;
5216 pointer->absMTPositionY = rawEvent->value;
5217 break;
5218 case ABS_MT_TOUCH_MAJOR:
5219 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR;
5220 pointer->absMTTouchMajor = rawEvent->value;
5221 break;
5222 case ABS_MT_TOUCH_MINOR:
5223 pointer->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR;
5224 pointer->absMTTouchMinor = rawEvent->value;
5225 break;
5226 case ABS_MT_WIDTH_MAJOR:
5227 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR;
5228 pointer->absMTWidthMajor = rawEvent->value;
5229 break;
5230 case ABS_MT_WIDTH_MINOR:
5231 pointer->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR;
5232 pointer->absMTWidthMinor = rawEvent->value;
5233 break;
5234 case ABS_MT_ORIENTATION:
5235 pointer->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION;
5236 pointer->absMTOrientation = rawEvent->value;
5237 break;
5238 case ABS_MT_TRACKING_ID:
5239 pointer->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID;
5240 pointer->absMTTrackingId = rawEvent->value;
5241 break;
Jeff Brown8d608662010-08-30 03:02:23 -07005242 case ABS_MT_PRESSURE:
5243 pointer->fields |= Accumulator::FIELD_ABS_MT_PRESSURE;
5244 pointer->absMTPressure = rawEvent->value;
5245 break;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005246 }
5247 break;
5248 }
5249
5250 case EV_SYN:
5251 switch (rawEvent->scanCode) {
5252 case SYN_MT_REPORT: {
5253 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
5254 uint32_t pointerIndex = mAccumulator.pointerCount;
5255
5256 if (mAccumulator.pointers[pointerIndex].fields) {
5257 if (pointerIndex == MAX_POINTERS) {
5258 LOGW("MultiTouch device driver returned more than maximum of %d pointers.",
5259 MAX_POINTERS);
5260 } else {
5261 pointerIndex += 1;
5262 mAccumulator.pointerCount = pointerIndex;
5263 }
5264 }
5265
5266 mAccumulator.pointers[pointerIndex].clear();
5267 break;
5268 }
5269
5270 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005271 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005272 break;
5273 }
5274 break;
5275 }
5276}
5277
5278void MultiTouchInputMapper::sync(nsecs_t when) {
5279 static const uint32_t REQUIRED_FIELDS =
Jeff Brown8d608662010-08-30 03:02:23 -07005280 Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005281
Jeff Brown6d0fec22010-07-23 21:28:06 -07005282 uint32_t inCount = mAccumulator.pointerCount;
5283 uint32_t outCount = 0;
5284 bool havePointerIds = true;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005285
Jeff Brown6d0fec22010-07-23 21:28:06 -07005286 mCurrentTouch.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005287
Jeff Brown6d0fec22010-07-23 21:28:06 -07005288 for (uint32_t inIndex = 0; inIndex < inCount; inIndex++) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005289 const Accumulator::Pointer& inPointer = mAccumulator.pointers[inIndex];
5290 uint32_t fields = inPointer.fields;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005291
Jeff Brown6d0fec22010-07-23 21:28:06 -07005292 if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005293 // Some drivers send empty MT sync packets without X / Y to indicate a pointer up.
5294 // Drop this finger.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005295 continue;
5296 }
5297
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005298 PointerData& outPointer = mCurrentTouch.pointers[outCount];
5299 outPointer.x = inPointer.absMTPositionX;
5300 outPointer.y = inPointer.absMTPositionY;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005301
Jeff Brown8d608662010-08-30 03:02:23 -07005302 if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) {
5303 if (inPointer.absMTPressure <= 0) {
Jeff Brownc3db8582010-10-20 15:33:38 -07005304 // Some devices send sync packets with X / Y but with a 0 pressure to indicate
5305 // a pointer going up. Drop this finger.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005306 continue;
5307 }
Jeff Brown8d608662010-08-30 03:02:23 -07005308 outPointer.pressure = inPointer.absMTPressure;
5309 } else {
5310 // Default pressure to 0 if absent.
5311 outPointer.pressure = 0;
5312 }
5313
5314 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) {
5315 if (inPointer.absMTTouchMajor <= 0) {
5316 // Some devices send sync packets with X / Y but with a 0 touch major to indicate
5317 // a pointer going up. Drop this finger.
5318 continue;
5319 }
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005320 outPointer.touchMajor = inPointer.absMTTouchMajor;
5321 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005322 // Default touch area to 0 if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005323 outPointer.touchMajor = 0;
5324 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005325
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005326 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) {
5327 outPointer.touchMinor = inPointer.absMTTouchMinor;
5328 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005329 // Assume touch area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005330 outPointer.touchMinor = outPointer.touchMajor;
5331 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005332
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005333 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) {
5334 outPointer.toolMajor = inPointer.absMTWidthMajor;
5335 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005336 // Default tool area to 0 if absent.
5337 outPointer.toolMajor = 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005338 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005339
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005340 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) {
5341 outPointer.toolMinor = inPointer.absMTWidthMinor;
5342 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005343 // Assume tool area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005344 outPointer.toolMinor = outPointer.toolMajor;
5345 }
5346
5347 if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) {
5348 outPointer.orientation = inPointer.absMTOrientation;
5349 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005350 // Default orientation to vertical if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005351 outPointer.orientation = 0;
5352 }
5353
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005354 outPointer.isStylus = false; // TODO: Handle stylus
5355
Jeff Brown8d608662010-08-30 03:02:23 -07005356 // Assign pointer id using tracking id if available.
Jeff Brown6d0fec22010-07-23 21:28:06 -07005357 if (havePointerIds) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005358 if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) {
5359 uint32_t id = uint32_t(inPointer.absMTTrackingId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005360
Jeff Brown6d0fec22010-07-23 21:28:06 -07005361 if (id > MAX_POINTER_ID) {
5362#if DEBUG_POINTERS
5363 LOGD("Pointers: Ignoring driver provided pointer id %d because "
Jeff Brown01ce2e92010-09-26 22:20:12 -07005364 "it is larger than max supported id %d",
Jeff Brown6d0fec22010-07-23 21:28:06 -07005365 id, MAX_POINTER_ID);
5366#endif
5367 havePointerIds = false;
5368 }
5369 else {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005370 outPointer.id = id;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005371 mCurrentTouch.idToIndex[id] = outCount;
5372 mCurrentTouch.idBits.markBit(id);
5373 }
5374 } else {
5375 havePointerIds = false;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005376 }
5377 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005378
Jeff Brown6d0fec22010-07-23 21:28:06 -07005379 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005380 }
5381
Jeff Brown6d0fec22010-07-23 21:28:06 -07005382 mCurrentTouch.pointerCount = outCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005383
Jeff Brownace13b12011-03-09 17:39:48 -08005384 mButtonState = (mButtonState | mAccumulator.buttonDown) & ~mAccumulator.buttonUp;
5385 mCurrentTouch.buttonState = mButtonState;
5386
Jeff Brown6d0fec22010-07-23 21:28:06 -07005387 syncTouch(when, havePointerIds);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005388
5389 mAccumulator.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005390}
5391
Jeff Brown8d608662010-08-30 03:02:23 -07005392void MultiTouchInputMapper::configureRawAxes() {
5393 TouchInputMapper::configureRawAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005394
Jeff Brown8d608662010-08-30 03:02:23 -07005395 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, & mRawAxes.x);
5396 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, & mRawAxes.y);
5397 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, & mRawAxes.touchMajor);
5398 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, & mRawAxes.touchMinor);
5399 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, & mRawAxes.toolMajor);
5400 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, & mRawAxes.toolMinor);
5401 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, & mRawAxes.orientation);
5402 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, & mRawAxes.pressure);
Jeff Brown9c3cda02010-06-15 01:31:58 -07005403}
5404
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005405
Jeff Browncb1404e2011-01-15 18:14:15 -08005406// --- JoystickInputMapper ---
5407
5408JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5409 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005410}
5411
5412JoystickInputMapper::~JoystickInputMapper() {
5413}
5414
5415uint32_t JoystickInputMapper::getSources() {
5416 return AINPUT_SOURCE_JOYSTICK;
5417}
5418
5419void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5420 InputMapper::populateDeviceInfo(info);
5421
Jeff Brown6f2fba42011-02-19 01:08:02 -08005422 for (size_t i = 0; i < mAxes.size(); i++) {
5423 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005424 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5425 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005426 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005427 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5428 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005429 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005430 }
5431}
5432
5433void JoystickInputMapper::dump(String8& dump) {
5434 dump.append(INDENT2 "Joystick Input Mapper:\n");
5435
Jeff Brown6f2fba42011-02-19 01:08:02 -08005436 dump.append(INDENT3 "Axes:\n");
5437 size_t numAxes = mAxes.size();
5438 for (size_t i = 0; i < numAxes; i++) {
5439 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005440 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005441 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005442 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005443 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005444 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005445 }
Jeff Brown85297452011-03-04 13:07:49 -08005446 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5447 label = getAxisLabel(axis.axisInfo.highAxis);
5448 if (label) {
5449 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5450 } else {
5451 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5452 axis.axisInfo.splitValue);
5453 }
5454 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5455 dump.append(" (invert)");
5456 }
5457
5458 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5459 axis.min, axis.max, axis.flat, axis.fuzz);
5460 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5461 "highScale=%0.5f, highOffset=%0.5f\n",
5462 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005463 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, rawFlat=%d, rawFuzz=%d\n",
5464 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
5465 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz);
Jeff Browncb1404e2011-01-15 18:14:15 -08005466 }
5467}
5468
5469void JoystickInputMapper::configure() {
5470 InputMapper::configure();
5471
Jeff Brown6f2fba42011-02-19 01:08:02 -08005472 // Collect all axes.
5473 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
5474 RawAbsoluteAxisInfo rawAxisInfo;
5475 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), abs, &rawAxisInfo);
5476 if (rawAxisInfo.valid) {
Jeff Brown85297452011-03-04 13:07:49 -08005477 // Map axis.
5478 AxisInfo axisInfo;
5479 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005480 if (!explicitlyMapped) {
5481 // Axis is not explicitly mapped, will choose a generic axis later.
Jeff Brown85297452011-03-04 13:07:49 -08005482 axisInfo.mode = AxisInfo::MODE_NORMAL;
5483 axisInfo.axis = -1;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005484 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005485
Jeff Brown85297452011-03-04 13:07:49 -08005486 // Apply flat override.
5487 int32_t rawFlat = axisInfo.flatOverride < 0
5488 ? rawAxisInfo.flat : axisInfo.flatOverride;
5489
5490 // Calculate scaling factors and limits.
Jeff Brown6f2fba42011-02-19 01:08:02 -08005491 Axis axis;
Jeff Brown85297452011-03-04 13:07:49 -08005492 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5493 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5494 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5495 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5496 scale, 0.0f, highScale, 0.0f,
5497 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5498 } else if (isCenteredAxis(axisInfo.axis)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005499 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5500 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
Jeff Brown85297452011-03-04 13:07:49 -08005501 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5502 scale, offset, scale, offset,
5503 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005504 } else {
5505 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
Jeff Brown85297452011-03-04 13:07:49 -08005506 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5507 scale, 0.0f, scale, 0.0f,
5508 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005509 }
5510
5511 // To eliminate noise while the joystick is at rest, filter out small variations
5512 // in axis values up front.
5513 axis.filter = axis.flat * 0.25f;
5514
5515 mAxes.add(abs, axis);
5516 }
5517 }
5518
5519 // If there are too many axes, start dropping them.
5520 // Prefer to keep explicitly mapped axes.
5521 if (mAxes.size() > PointerCoords::MAX_AXES) {
5522 LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
5523 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5524 pruneAxes(true);
5525 pruneAxes(false);
5526 }
5527
5528 // Assign generic axis ids to remaining axes.
5529 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5530 size_t numAxes = mAxes.size();
5531 for (size_t i = 0; i < numAxes; i++) {
5532 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005533 if (axis.axisInfo.axis < 0) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005534 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5535 && haveAxis(nextGenericAxisId)) {
5536 nextGenericAxisId += 1;
5537 }
5538
5539 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
Jeff Brown85297452011-03-04 13:07:49 -08005540 axis.axisInfo.axis = nextGenericAxisId;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005541 nextGenericAxisId += 1;
5542 } else {
5543 LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
5544 "have already been assigned to other axes.",
5545 getDeviceName().string(), mAxes.keyAt(i));
5546 mAxes.removeItemsAt(i--);
5547 numAxes -= 1;
5548 }
5549 }
5550 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005551}
5552
Jeff Brown85297452011-03-04 13:07:49 -08005553bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005554 size_t numAxes = mAxes.size();
5555 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005556 const Axis& axis = mAxes.valueAt(i);
5557 if (axis.axisInfo.axis == axisId
5558 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5559 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005560 return true;
5561 }
5562 }
5563 return false;
5564}
Jeff Browncb1404e2011-01-15 18:14:15 -08005565
Jeff Brown6f2fba42011-02-19 01:08:02 -08005566void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5567 size_t i = mAxes.size();
5568 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5569 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5570 continue;
5571 }
5572 LOGI("Discarding joystick '%s' axis %d because there are too many axes.",
5573 getDeviceName().string(), mAxes.keyAt(i));
5574 mAxes.removeItemsAt(i);
5575 }
5576}
5577
5578bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5579 switch (axis) {
5580 case AMOTION_EVENT_AXIS_X:
5581 case AMOTION_EVENT_AXIS_Y:
5582 case AMOTION_EVENT_AXIS_Z:
5583 case AMOTION_EVENT_AXIS_RX:
5584 case AMOTION_EVENT_AXIS_RY:
5585 case AMOTION_EVENT_AXIS_RZ:
5586 case AMOTION_EVENT_AXIS_HAT_X:
5587 case AMOTION_EVENT_AXIS_HAT_Y:
5588 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005589 case AMOTION_EVENT_AXIS_RUDDER:
5590 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005591 return true;
5592 default:
5593 return false;
5594 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005595}
5596
5597void JoystickInputMapper::reset() {
5598 // Recenter all axes.
5599 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Browncb1404e2011-01-15 18:14:15 -08005600
Jeff Brown6f2fba42011-02-19 01:08:02 -08005601 size_t numAxes = mAxes.size();
5602 for (size_t i = 0; i < numAxes; i++) {
5603 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005604 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005605 }
5606
5607 sync(when, true /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005608
5609 InputMapper::reset();
5610}
5611
5612void JoystickInputMapper::process(const RawEvent* rawEvent) {
5613 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005614 case EV_ABS: {
5615 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
5616 if (index >= 0) {
5617 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005618 float newValue, highNewValue;
5619 switch (axis.axisInfo.mode) {
5620 case AxisInfo::MODE_INVERT:
5621 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5622 * axis.scale + axis.offset;
5623 highNewValue = 0.0f;
5624 break;
5625 case AxisInfo::MODE_SPLIT:
5626 if (rawEvent->value < axis.axisInfo.splitValue) {
5627 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5628 * axis.scale + axis.offset;
5629 highNewValue = 0.0f;
5630 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5631 newValue = 0.0f;
5632 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5633 * axis.highScale + axis.highOffset;
5634 } else {
5635 newValue = 0.0f;
5636 highNewValue = 0.0f;
5637 }
5638 break;
5639 default:
5640 newValue = rawEvent->value * axis.scale + axis.offset;
5641 highNewValue = 0.0f;
5642 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005643 }
Jeff Brown85297452011-03-04 13:07:49 -08005644 axis.newValue = newValue;
5645 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005646 }
5647 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005648 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005649
5650 case EV_SYN:
5651 switch (rawEvent->scanCode) {
5652 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005653 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005654 break;
5655 }
5656 break;
5657 }
5658}
5659
Jeff Brown6f2fba42011-02-19 01:08:02 -08005660void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005661 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005662 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005663 }
5664
5665 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005666 int32_t buttonState = 0;
5667
5668 PointerProperties pointerProperties;
5669 pointerProperties.clear();
5670 pointerProperties.id = 0;
5671 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08005672
Jeff Brown6f2fba42011-02-19 01:08:02 -08005673 PointerCoords pointerCoords;
5674 pointerCoords.clear();
5675
5676 size_t numAxes = mAxes.size();
5677 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005678 const Axis& axis = mAxes.valueAt(i);
5679 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
5680 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5681 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
5682 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005683 }
5684
Jeff Brown56194eb2011-03-02 19:23:13 -08005685 // Moving a joystick axis should not wake the devide because joysticks can
5686 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
5687 // button will likely wake the device.
5688 // TODO: Use the input device configuration to control this behavior more finely.
5689 uint32_t policyFlags = 0;
5690
Jeff Brown56194eb2011-03-02 19:23:13 -08005691 getDispatcher()->notifyMotion(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005692 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
5693 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Browncb1404e2011-01-15 18:14:15 -08005694}
5695
Jeff Brown85297452011-03-04 13:07:49 -08005696bool JoystickInputMapper::filterAxes(bool force) {
5697 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005698 size_t numAxes = mAxes.size();
5699 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005700 Axis& axis = mAxes.editValueAt(i);
5701 if (force || hasValueChangedSignificantly(axis.filter,
5702 axis.newValue, axis.currentValue, axis.min, axis.max)) {
5703 axis.currentValue = axis.newValue;
5704 atLeastOneSignificantChange = true;
5705 }
5706 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5707 if (force || hasValueChangedSignificantly(axis.filter,
5708 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
5709 axis.highCurrentValue = axis.highNewValue;
5710 atLeastOneSignificantChange = true;
5711 }
5712 }
5713 }
5714 return atLeastOneSignificantChange;
5715}
5716
5717bool JoystickInputMapper::hasValueChangedSignificantly(
5718 float filter, float newValue, float currentValue, float min, float max) {
5719 if (newValue != currentValue) {
5720 // Filter out small changes in value unless the value is converging on the axis
5721 // bounds or center point. This is intended to reduce the amount of information
5722 // sent to applications by particularly noisy joysticks (such as PS3).
5723 if (fabs(newValue - currentValue) > filter
5724 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
5725 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
5726 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
5727 return true;
5728 }
5729 }
5730 return false;
5731}
5732
5733bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
5734 float filter, float newValue, float currentValue, float thresholdValue) {
5735 float newDistance = fabs(newValue - thresholdValue);
5736 if (newDistance < filter) {
5737 float oldDistance = fabs(currentValue - thresholdValue);
5738 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005739 return true;
5740 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005741 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08005742 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005743}
5744
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005745} // namespace android