blob: 2924d3edd54eb119268d541988fd55a115ef303f [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Brown46b9ac02010-04-22 18:58:52 -070017#define LOG_TAG "InputReader"
18
19//#define LOG_NDEBUG 0
20
21// Log debug messages for each raw event received from the EventHub.
22#define DEBUG_RAW_EVENTS 0
23
24// Log debug messages about touch screen filtering hacks.
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_HACKS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070026
27// Log debug messages about virtual key processing.
Jeff Brown349703e2010-06-22 01:27:15 -070028#define DEBUG_VIRTUAL_KEYS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070029
30// Log debug messages about pointers.
Jeff Brown9f2106f2011-05-24 14:40:35 -070031#define DEBUG_POINTERS 0
Jeff Brown46b9ac02010-04-22 18:58:52 -070032
Jeff Brown5c225b12010-06-16 01:53:36 -070033// Log debug messages about pointer assignment calculations.
34#define DEBUG_POINTER_ASSIGNMENT 0
35
Jeff Brownace13b12011-03-09 17:39:48 -080036// Log debug messages about gesture detection.
37#define DEBUG_GESTURES 0
38
Jeff Brownb4ff35d2011-01-02 16:37:43 -080039#include "InputReader.h"
40
Jeff Brown1a84fd12011-06-02 01:26:32 -070041#include <cutils/atomic.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070042#include <cutils/log.h>
Jeff Brown6b53e8d2010-11-10 16:03:06 -080043#include <ui/Keyboard.h>
Jeff Brown90655042010-12-02 13:50:46 -080044#include <ui/VirtualKeyMap.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070045
46#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070047#include <stdlib.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070048#include <unistd.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070049#include <errno.h>
50#include <limits.h>
Jeff Brownc5ed5912010-07-14 18:48:53 -070051#include <math.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070052
Jeff Brown8d608662010-08-30 03:02:23 -070053#define INDENT " "
Jeff Brownef3d7e82010-09-30 14:33:04 -070054#define INDENT2 " "
55#define INDENT3 " "
56#define INDENT4 " "
Jeff Brown8d608662010-08-30 03:02:23 -070057
Jeff Brown46b9ac02010-04-22 18:58:52 -070058namespace android {
59
Jeff Brownace13b12011-03-09 17:39:48 -080060// --- Constants ---
61
Jeff Brown80fd47c2011-05-24 01:07:44 -070062// Maximum number of slots supported when using the slot-based Multitouch Protocol B.
63static const size_t MAX_SLOTS = 32;
64
Jeff Brown46b9ac02010-04-22 18:58:52 -070065// --- Static Functions ---
66
67template<typename T>
68inline static T abs(const T& value) {
69 return value < 0 ? - value : value;
70}
71
72template<typename T>
73inline static T min(const T& a, const T& b) {
74 return a < b ? a : b;
75}
76
Jeff Brown5c225b12010-06-16 01:53:36 -070077template<typename T>
78inline static void swap(T& a, T& b) {
79 T temp = a;
80 a = b;
81 b = temp;
82}
83
Jeff Brown8d608662010-08-30 03:02:23 -070084inline static float avg(float x, float y) {
85 return (x + y) / 2;
86}
87
Jeff Brown2352b972011-04-12 22:39:53 -070088inline static float distance(float x1, float y1, float x2, float y2) {
89 return hypotf(x1 - x2, y1 - y2);
Jeff Brownace13b12011-03-09 17:39:48 -080090}
91
Jeff Brown517bb4c2011-01-14 19:09:23 -080092inline static int32_t signExtendNybble(int32_t value) {
93 return value >= 8 ? value - 16 : value;
94}
95
Jeff Brownef3d7e82010-09-30 14:33:04 -070096static inline const char* toString(bool value) {
97 return value ? "true" : "false";
98}
99
Jeff Brown9626b142011-03-03 02:09:54 -0800100static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
101 const int32_t map[][4], size_t mapSize) {
102 if (orientation != DISPLAY_ORIENTATION_0) {
103 for (size_t i = 0; i < mapSize; i++) {
104 if (value == map[i][0]) {
105 return map[i][orientation];
106 }
107 }
108 }
109 return value;
110}
111
Jeff Brown46b9ac02010-04-22 18:58:52 -0700112static const int32_t keyCodeRotationMap[][4] = {
113 // key codes enumerated counter-clockwise with the original (unrotated) key first
114 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd035822010-06-30 16:10:35 -0700115 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
116 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
117 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
118 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac02010-04-22 18:58:52 -0700119};
Jeff Brown9626b142011-03-03 02:09:54 -0800120static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac02010-04-22 18:58:52 -0700121 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
122
123int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
Jeff Brown9626b142011-03-03 02:09:54 -0800124 return rotateValueUsingRotationMap(keyCode, orientation,
125 keyCodeRotationMap, keyCodeRotationMapSize);
126}
127
128static const int32_t edgeFlagRotationMap[][4] = {
129 // edge flags enumerated counter-clockwise with the original (unrotated) edge flag first
130 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
131 { AMOTION_EVENT_EDGE_FLAG_BOTTOM, AMOTION_EVENT_EDGE_FLAG_RIGHT,
132 AMOTION_EVENT_EDGE_FLAG_TOP, AMOTION_EVENT_EDGE_FLAG_LEFT },
133 { AMOTION_EVENT_EDGE_FLAG_RIGHT, AMOTION_EVENT_EDGE_FLAG_TOP,
134 AMOTION_EVENT_EDGE_FLAG_LEFT, AMOTION_EVENT_EDGE_FLAG_BOTTOM },
135 { AMOTION_EVENT_EDGE_FLAG_TOP, AMOTION_EVENT_EDGE_FLAG_LEFT,
136 AMOTION_EVENT_EDGE_FLAG_BOTTOM, AMOTION_EVENT_EDGE_FLAG_RIGHT },
137 { AMOTION_EVENT_EDGE_FLAG_LEFT, AMOTION_EVENT_EDGE_FLAG_BOTTOM,
138 AMOTION_EVENT_EDGE_FLAG_RIGHT, AMOTION_EVENT_EDGE_FLAG_TOP },
139};
140static const size_t edgeFlagRotationMapSize =
141 sizeof(edgeFlagRotationMap) / sizeof(edgeFlagRotationMap[0]);
142
143static int32_t rotateEdgeFlag(int32_t edgeFlag, int32_t orientation) {
144 return rotateValueUsingRotationMap(edgeFlag, orientation,
145 edgeFlagRotationMap, edgeFlagRotationMapSize);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700146}
147
Jeff Brown6d0fec22010-07-23 21:28:06 -0700148static inline bool sourcesMatchMask(uint32_t sources, uint32_t sourceMask) {
149 return (sources & sourceMask & ~ AINPUT_SOURCE_CLASS_MASK) != 0;
150}
151
Jeff Brownefd32662011-03-08 15:13:06 -0800152static uint32_t getButtonStateForScanCode(int32_t scanCode) {
153 // Currently all buttons are mapped to the primary button.
154 switch (scanCode) {
155 case BTN_LEFT:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700156 return AMOTION_EVENT_BUTTON_PRIMARY;
Jeff Brownefd32662011-03-08 15:13:06 -0800157 case BTN_RIGHT:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700158 return AMOTION_EVENT_BUTTON_SECONDARY;
Jeff Brownefd32662011-03-08 15:13:06 -0800159 case BTN_MIDDLE:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700160 return AMOTION_EVENT_BUTTON_TERTIARY;
Jeff Brownefd32662011-03-08 15:13:06 -0800161 case BTN_SIDE:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700162 return AMOTION_EVENT_BUTTON_BACK;
Jeff Brownefd32662011-03-08 15:13:06 -0800163 case BTN_EXTRA:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700164 return AMOTION_EVENT_BUTTON_FORWARD;
Jeff Brownefd32662011-03-08 15:13:06 -0800165 case BTN_FORWARD:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700166 return AMOTION_EVENT_BUTTON_FORWARD;
Jeff Brownefd32662011-03-08 15:13:06 -0800167 case BTN_BACK:
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700168 return AMOTION_EVENT_BUTTON_BACK;
Jeff Brownefd32662011-03-08 15:13:06 -0800169 case BTN_TASK:
Jeff Brownefd32662011-03-08 15:13:06 -0800170 default:
171 return 0;
172 }
173}
174
175// Returns true if the pointer should be reported as being down given the specified
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700176// button states. This determines whether the event is reported as a touch event.
177static bool isPointerDown(int32_t buttonState) {
178 return buttonState &
179 (AMOTION_EVENT_BUTTON_PRIMARY | AMOTION_EVENT_BUTTON_SECONDARY
180 | AMOTION_EVENT_BUTTON_TERTIARY
181 | AMOTION_EVENT_BUTTON_ERASER);
Jeff Brownefd32662011-03-08 15:13:06 -0800182}
183
184static int32_t calculateEdgeFlagsUsingPointerBounds(
185 const sp<PointerControllerInterface>& pointerController, float x, float y) {
186 int32_t edgeFlags = 0;
187 float minX, minY, maxX, maxY;
188 if (pointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
189 if (x <= minX) {
190 edgeFlags |= AMOTION_EVENT_EDGE_FLAG_LEFT;
191 } else if (x >= maxX) {
192 edgeFlags |= AMOTION_EVENT_EDGE_FLAG_RIGHT;
193 }
194 if (y <= minY) {
195 edgeFlags |= AMOTION_EVENT_EDGE_FLAG_TOP;
196 } else if (y >= maxY) {
197 edgeFlags |= AMOTION_EVENT_EDGE_FLAG_BOTTOM;
198 }
199 }
200 return edgeFlags;
201}
202
Jeff Brown2352b972011-04-12 22:39:53 -0700203static void clampPositionUsingPointerBounds(
204 const sp<PointerControllerInterface>& pointerController, float* x, float* y) {
205 float minX, minY, maxX, maxY;
206 if (pointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
207 if (*x < minX) {
208 *x = minX;
209 } else if (*x > maxX) {
210 *x = maxX;
211 }
212 if (*y < minY) {
213 *y = minY;
214 } else if (*y > maxY) {
215 *y = maxY;
216 }
217 }
218}
219
220static float calculateCommonVector(float a, float b) {
221 if (a > 0 && b > 0) {
222 return a < b ? a : b;
223 } else if (a < 0 && b < 0) {
224 return a > b ? a : b;
225 } else {
226 return 0;
227 }
228}
229
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700230static void synthesizeButtonKey(InputReaderContext* context, int32_t action,
231 nsecs_t when, int32_t deviceId, uint32_t source,
232 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState,
233 int32_t buttonState, int32_t keyCode) {
234 if (
235 (action == AKEY_EVENT_ACTION_DOWN
236 && !(lastButtonState & buttonState)
237 && (currentButtonState & buttonState))
238 || (action == AKEY_EVENT_ACTION_UP
239 && (lastButtonState & buttonState)
240 && !(currentButtonState & buttonState))) {
241 context->getDispatcher()->notifyKey(when, deviceId, source, policyFlags,
242 action, 0, keyCode, 0, context->getGlobalMetaState(), when);
243 }
244}
245
246static void synthesizeButtonKeys(InputReaderContext* context, int32_t action,
247 nsecs_t when, int32_t deviceId, uint32_t source,
248 uint32_t policyFlags, int32_t lastButtonState, int32_t currentButtonState) {
249 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
250 lastButtonState, currentButtonState,
251 AMOTION_EVENT_BUTTON_BACK, AKEYCODE_BACK);
252 synthesizeButtonKey(context, action, when, deviceId, source, policyFlags,
253 lastButtonState, currentButtonState,
254 AMOTION_EVENT_BUTTON_FORWARD, AKEYCODE_FORWARD);
255}
256
Jeff Brown46b9ac02010-04-22 18:58:52 -0700257
Jeff Brown46b9ac02010-04-22 18:58:52 -0700258// --- InputReader ---
259
260InputReader::InputReader(const sp<EventHubInterface>& eventHub,
Jeff Brown9c3cda02010-06-15 01:31:58 -0700261 const sp<InputReaderPolicyInterface>& policy,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700262 const sp<InputDispatcherInterface>& dispatcher) :
Jeff Brown6d0fec22010-07-23 21:28:06 -0700263 mEventHub(eventHub), mPolicy(policy), mDispatcher(dispatcher),
Jeff Brown1a84fd12011-06-02 01:26:32 -0700264 mGlobalMetaState(0), mDisableVirtualKeysTimeout(LLONG_MIN), mNextTimeout(LLONG_MAX),
265 mRefreshConfiguration(0) {
266 configure(true /*firstTime*/);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700267 updateGlobalMetaState();
268 updateInputConfiguration();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700269}
270
271InputReader::~InputReader() {
272 for (size_t i = 0; i < mDevices.size(); i++) {
273 delete mDevices.valueAt(i);
274 }
275}
276
277void InputReader::loopOnce() {
Jeff Brown1a84fd12011-06-02 01:26:32 -0700278 if (android_atomic_acquire_load(&mRefreshConfiguration)) {
279 android_atomic_release_store(0, &mRefreshConfiguration);
280 configure(false /*firstTime*/);
281 }
282
Jeff Brownaa3855d2011-03-17 01:34:19 -0700283 int32_t timeoutMillis = -1;
284 if (mNextTimeout != LLONG_MAX) {
285 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
286 timeoutMillis = toMillisecondTimeoutDelay(now, mNextTimeout);
287 }
288
Jeff Brownb7198742011-03-18 18:14:26 -0700289 size_t count = mEventHub->getEvents(timeoutMillis, mEventBuffer, EVENT_BUFFER_SIZE);
290 if (count) {
291 processEvents(mEventBuffer, count);
292 }
293 if (!count || timeoutMillis == 0) {
Jeff Brownaa3855d2011-03-17 01:34:19 -0700294 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
295#if DEBUG_RAW_EVENTS
296 LOGD("Timeout expired, latency=%0.3fms", (now - mNextTimeout) * 0.000001f);
297#endif
298 mNextTimeout = LLONG_MAX;
299 timeoutExpired(now);
300 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700301}
302
Jeff Brownb7198742011-03-18 18:14:26 -0700303void InputReader::processEvents(const RawEvent* rawEvents, size_t count) {
304 for (const RawEvent* rawEvent = rawEvents; count;) {
305 int32_t type = rawEvent->type;
306 size_t batchSize = 1;
307 if (type < EventHubInterface::FIRST_SYNTHETIC_EVENT) {
308 int32_t deviceId = rawEvent->deviceId;
309 while (batchSize < count) {
310 if (rawEvent[batchSize].type >= EventHubInterface::FIRST_SYNTHETIC_EVENT
311 || rawEvent[batchSize].deviceId != deviceId) {
312 break;
313 }
314 batchSize += 1;
315 }
316#if DEBUG_RAW_EVENTS
317 LOGD("BatchSize: %d Count: %d", batchSize, count);
318#endif
319 processEventsForDevice(deviceId, rawEvent, batchSize);
320 } else {
321 switch (rawEvent->type) {
322 case EventHubInterface::DEVICE_ADDED:
323 addDevice(rawEvent->deviceId);
324 break;
325 case EventHubInterface::DEVICE_REMOVED:
326 removeDevice(rawEvent->deviceId);
327 break;
328 case EventHubInterface::FINISHED_DEVICE_SCAN:
329 handleConfigurationChanged(rawEvent->when);
330 break;
331 default:
Jeff Brownb6110c22011-04-01 16:15:13 -0700332 LOG_ASSERT(false); // can't happen
Jeff Brownb7198742011-03-18 18:14:26 -0700333 break;
334 }
335 }
336 count -= batchSize;
337 rawEvent += batchSize;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700338 }
339}
340
Jeff Brown7342bb92010-10-01 18:55:43 -0700341void InputReader::addDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700342 String8 name = mEventHub->getDeviceName(deviceId);
343 uint32_t classes = mEventHub->getDeviceClasses(deviceId);
344
345 InputDevice* device = createDevice(deviceId, name, classes);
346 device->configure();
347
Jeff Brown8d608662010-08-30 03:02:23 -0700348 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800349 LOGI("Device added: id=%d, name='%s' (ignored non-input device)", deviceId, name.string());
Jeff Brown8d608662010-08-30 03:02:23 -0700350 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800351 LOGI("Device added: id=%d, name='%s', sources=0x%08x", deviceId, name.string(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700352 device->getSources());
Jeff Brown8d608662010-08-30 03:02:23 -0700353 }
354
Jeff Brown6d0fec22010-07-23 21:28:06 -0700355 bool added = false;
356 { // acquire device registry writer lock
357 RWLock::AutoWLock _wl(mDeviceRegistryLock);
358
359 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
360 if (deviceIndex < 0) {
361 mDevices.add(deviceId, device);
362 added = true;
363 }
364 } // release device registry writer lock
365
366 if (! added) {
367 LOGW("Ignoring spurious device added event for deviceId %d.", deviceId);
368 delete device;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700369 return;
370 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700371}
372
Jeff Brown7342bb92010-10-01 18:55:43 -0700373void InputReader::removeDevice(int32_t deviceId) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700374 bool removed = false;
375 InputDevice* device = NULL;
376 { // acquire device registry writer lock
377 RWLock::AutoWLock _wl(mDeviceRegistryLock);
378
379 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
380 if (deviceIndex >= 0) {
381 device = mDevices.valueAt(deviceIndex);
382 mDevices.removeItemsAt(deviceIndex, 1);
383 removed = true;
384 }
385 } // release device registry writer lock
386
387 if (! removed) {
388 LOGW("Ignoring spurious device removed event for deviceId %d.", deviceId);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700389 return;
390 }
391
Jeff Brown6d0fec22010-07-23 21:28:06 -0700392 if (device->isIgnored()) {
Jeff Brown90655042010-12-02 13:50:46 -0800393 LOGI("Device removed: id=%d, name='%s' (ignored non-input device)",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700394 device->getId(), device->getName().string());
395 } else {
Jeff Brown90655042010-12-02 13:50:46 -0800396 LOGI("Device removed: id=%d, name='%s', sources=0x%08x",
Jeff Brown6d0fec22010-07-23 21:28:06 -0700397 device->getId(), device->getName().string(), device->getSources());
398 }
399
Jeff Brown8d608662010-08-30 03:02:23 -0700400 device->reset();
401
Jeff Brown6d0fec22010-07-23 21:28:06 -0700402 delete device;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700403}
404
Jeff Brown6d0fec22010-07-23 21:28:06 -0700405InputDevice* InputReader::createDevice(int32_t deviceId, const String8& name, uint32_t classes) {
406 InputDevice* device = new InputDevice(this, deviceId, name);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700407
Jeff Brown56194eb2011-03-02 19:23:13 -0800408 // External devices.
409 if (classes & INPUT_DEVICE_CLASS_EXTERNAL) {
410 device->setExternal(true);
411 }
412
Jeff Brown6d0fec22010-07-23 21:28:06 -0700413 // Switch-like devices.
414 if (classes & INPUT_DEVICE_CLASS_SWITCH) {
415 device->addMapper(new SwitchInputMapper(device));
416 }
417
418 // Keyboard-like devices.
Jeff Brownefd32662011-03-08 15:13:06 -0800419 uint32_t keyboardSource = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700420 int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC;
421 if (classes & INPUT_DEVICE_CLASS_KEYBOARD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800422 keyboardSource |= AINPUT_SOURCE_KEYBOARD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700423 }
424 if (classes & INPUT_DEVICE_CLASS_ALPHAKEY) {
425 keyboardType = AINPUT_KEYBOARD_TYPE_ALPHABETIC;
426 }
427 if (classes & INPUT_DEVICE_CLASS_DPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800428 keyboardSource |= AINPUT_SOURCE_DPAD;
Jeff Brown6d0fec22010-07-23 21:28:06 -0700429 }
Jeff Browncb1404e2011-01-15 18:14:15 -0800430 if (classes & INPUT_DEVICE_CLASS_GAMEPAD) {
Jeff Brownefd32662011-03-08 15:13:06 -0800431 keyboardSource |= AINPUT_SOURCE_GAMEPAD;
Jeff Browncb1404e2011-01-15 18:14:15 -0800432 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700433
Jeff Brownefd32662011-03-08 15:13:06 -0800434 if (keyboardSource != 0) {
435 device->addMapper(new KeyboardInputMapper(device, keyboardSource, keyboardType));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700436 }
437
Jeff Brown83c09682010-12-23 17:50:18 -0800438 // Cursor-like devices.
439 if (classes & INPUT_DEVICE_CLASS_CURSOR) {
440 device->addMapper(new CursorInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700441 }
442
Jeff Brown58a2da82011-01-25 16:02:22 -0800443 // Touchscreens and touchpad devices.
444 if (classes & INPUT_DEVICE_CLASS_TOUCH_MT) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800445 device->addMapper(new MultiTouchInputMapper(device));
Jeff Brown58a2da82011-01-25 16:02:22 -0800446 } else if (classes & INPUT_DEVICE_CLASS_TOUCH) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800447 device->addMapper(new SingleTouchInputMapper(device));
Jeff Brown6d0fec22010-07-23 21:28:06 -0700448 }
449
Jeff Browncb1404e2011-01-15 18:14:15 -0800450 // Joystick-like devices.
451 if (classes & INPUT_DEVICE_CLASS_JOYSTICK) {
452 device->addMapper(new JoystickInputMapper(device));
453 }
454
Jeff Brown6d0fec22010-07-23 21:28:06 -0700455 return device;
456}
457
Jeff Brownb7198742011-03-18 18:14:26 -0700458void InputReader::processEventsForDevice(int32_t deviceId,
459 const RawEvent* rawEvents, size_t count) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700460 { // acquire device registry reader lock
461 RWLock::AutoRLock _rl(mDeviceRegistryLock);
462
463 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
464 if (deviceIndex < 0) {
465 LOGW("Discarding event for unknown deviceId %d.", deviceId);
466 return;
467 }
468
469 InputDevice* device = mDevices.valueAt(deviceIndex);
470 if (device->isIgnored()) {
471 //LOGD("Discarding event for ignored deviceId %d.", deviceId);
472 return;
473 }
474
Jeff Brownb7198742011-03-18 18:14:26 -0700475 device->process(rawEvents, count);
Jeff Brown6d0fec22010-07-23 21:28:06 -0700476 } // release device registry reader lock
477}
478
Jeff Brownaa3855d2011-03-17 01:34:19 -0700479void InputReader::timeoutExpired(nsecs_t when) {
480 { // acquire device registry reader lock
481 RWLock::AutoRLock _rl(mDeviceRegistryLock);
482
483 for (size_t i = 0; i < mDevices.size(); i++) {
484 InputDevice* device = mDevices.valueAt(i);
485 if (!device->isIgnored()) {
486 device->timeoutExpired(when);
487 }
488 }
489 } // release device registry reader lock
490}
491
Jeff Brownc3db8582010-10-20 15:33:38 -0700492void InputReader::handleConfigurationChanged(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700493 // Reset global meta state because it depends on the list of all configured devices.
494 updateGlobalMetaState();
495
496 // Update input configuration.
497 updateInputConfiguration();
498
499 // Enqueue configuration changed.
500 mDispatcher->notifyConfigurationChanged(when);
501}
502
Jeff Brown1a84fd12011-06-02 01:26:32 -0700503void InputReader::configure(bool firstTime) {
504 mPolicy->getReaderConfiguration(&mConfig);
505 mEventHub->setExcludedDevices(mConfig.excludedDeviceNames);
506
507 if (!firstTime) {
508 mEventHub->reopenDevices();
Jeff Brown6d0fec22010-07-23 21:28:06 -0700509 }
510}
511
512void InputReader::updateGlobalMetaState() {
513 { // acquire state lock
514 AutoMutex _l(mStateLock);
515
516 mGlobalMetaState = 0;
517
518 { // acquire device registry reader lock
519 RWLock::AutoRLock _rl(mDeviceRegistryLock);
520
521 for (size_t i = 0; i < mDevices.size(); i++) {
522 InputDevice* device = mDevices.valueAt(i);
523 mGlobalMetaState |= device->getMetaState();
524 }
525 } // release device registry reader lock
526 } // release state lock
527}
528
529int32_t InputReader::getGlobalMetaState() {
530 { // acquire state lock
531 AutoMutex _l(mStateLock);
532
533 return mGlobalMetaState;
534 } // release state lock
535}
536
537void InputReader::updateInputConfiguration() {
538 { // acquire state lock
539 AutoMutex _l(mStateLock);
540
541 int32_t touchScreenConfig = InputConfiguration::TOUCHSCREEN_NOTOUCH;
542 int32_t keyboardConfig = InputConfiguration::KEYBOARD_NOKEYS;
543 int32_t navigationConfig = InputConfiguration::NAVIGATION_NONAV;
544 { // acquire device registry reader lock
545 RWLock::AutoRLock _rl(mDeviceRegistryLock);
546
547 InputDeviceInfo deviceInfo;
548 for (size_t i = 0; i < mDevices.size(); i++) {
549 InputDevice* device = mDevices.valueAt(i);
550 device->getDeviceInfo(& deviceInfo);
551 uint32_t sources = deviceInfo.getSources();
552
553 if ((sources & AINPUT_SOURCE_TOUCHSCREEN) == AINPUT_SOURCE_TOUCHSCREEN) {
554 touchScreenConfig = InputConfiguration::TOUCHSCREEN_FINGER;
555 }
556 if ((sources & AINPUT_SOURCE_TRACKBALL) == AINPUT_SOURCE_TRACKBALL) {
557 navigationConfig = InputConfiguration::NAVIGATION_TRACKBALL;
558 } else if ((sources & AINPUT_SOURCE_DPAD) == AINPUT_SOURCE_DPAD) {
559 navigationConfig = InputConfiguration::NAVIGATION_DPAD;
560 }
561 if (deviceInfo.getKeyboardType() == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
562 keyboardConfig = InputConfiguration::KEYBOARD_QWERTY;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700563 }
564 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700565 } // release device registry reader lock
Jeff Brown46b9ac02010-04-22 18:58:52 -0700566
Jeff Brown6d0fec22010-07-23 21:28:06 -0700567 mInputConfiguration.touchScreen = touchScreenConfig;
568 mInputConfiguration.keyboard = keyboardConfig;
569 mInputConfiguration.navigation = navigationConfig;
570 } // release state lock
571}
572
Jeff Brownfe508922011-01-18 15:10:10 -0800573void InputReader::disableVirtualKeysUntil(nsecs_t time) {
574 mDisableVirtualKeysTimeout = time;
575}
576
577bool InputReader::shouldDropVirtualKey(nsecs_t now,
578 InputDevice* device, int32_t keyCode, int32_t scanCode) {
579 if (now < mDisableVirtualKeysTimeout) {
580 LOGI("Dropping virtual key from device %s because virtual keys are "
581 "temporarily disabled for the next %0.3fms. keyCode=%d, scanCode=%d",
582 device->getName().string(),
583 (mDisableVirtualKeysTimeout - now) * 0.000001,
584 keyCode, scanCode);
585 return true;
586 } else {
587 return false;
588 }
589}
590
Jeff Brown05dc66a2011-03-02 14:41:58 -0800591void InputReader::fadePointer() {
592 { // acquire device registry reader lock
593 RWLock::AutoRLock _rl(mDeviceRegistryLock);
594
595 for (size_t i = 0; i < mDevices.size(); i++) {
596 InputDevice* device = mDevices.valueAt(i);
597 device->fadePointer();
598 }
599 } // release device registry reader lock
600}
601
Jeff Brownaa3855d2011-03-17 01:34:19 -0700602void InputReader::requestTimeoutAtTime(nsecs_t when) {
603 if (when < mNextTimeout) {
604 mNextTimeout = when;
605 }
606}
607
Jeff Brown6d0fec22010-07-23 21:28:06 -0700608void InputReader::getInputConfiguration(InputConfiguration* outConfiguration) {
609 { // acquire state lock
610 AutoMutex _l(mStateLock);
611
612 *outConfiguration = mInputConfiguration;
613 } // release state lock
614}
615
616status_t InputReader::getInputDeviceInfo(int32_t deviceId, InputDeviceInfo* outDeviceInfo) {
617 { // acquire device registry reader lock
618 RWLock::AutoRLock _rl(mDeviceRegistryLock);
619
620 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
621 if (deviceIndex < 0) {
622 return NAME_NOT_FOUND;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700623 }
624
Jeff Brown6d0fec22010-07-23 21:28:06 -0700625 InputDevice* device = mDevices.valueAt(deviceIndex);
626 if (device->isIgnored()) {
627 return NAME_NOT_FOUND;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700628 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700629
630 device->getDeviceInfo(outDeviceInfo);
631 return OK;
632 } // release device registy reader lock
633}
634
635void InputReader::getInputDeviceIds(Vector<int32_t>& outDeviceIds) {
636 outDeviceIds.clear();
637
638 { // acquire device registry reader lock
639 RWLock::AutoRLock _rl(mDeviceRegistryLock);
640
641 size_t numDevices = mDevices.size();
642 for (size_t i = 0; i < numDevices; i++) {
643 InputDevice* device = mDevices.valueAt(i);
644 if (! device->isIgnored()) {
645 outDeviceIds.add(device->getId());
646 }
647 }
648 } // release device registy reader lock
649}
650
651int32_t InputReader::getKeyCodeState(int32_t deviceId, uint32_t sourceMask,
652 int32_t keyCode) {
653 return getState(deviceId, sourceMask, keyCode, & InputDevice::getKeyCodeState);
654}
655
656int32_t InputReader::getScanCodeState(int32_t deviceId, uint32_t sourceMask,
657 int32_t scanCode) {
658 return getState(deviceId, sourceMask, scanCode, & InputDevice::getScanCodeState);
659}
660
661int32_t InputReader::getSwitchState(int32_t deviceId, uint32_t sourceMask, int32_t switchCode) {
662 return getState(deviceId, sourceMask, switchCode, & InputDevice::getSwitchState);
663}
664
665int32_t InputReader::getState(int32_t deviceId, uint32_t sourceMask, int32_t code,
666 GetStateFunc getStateFunc) {
667 { // acquire device registry reader lock
668 RWLock::AutoRLock _rl(mDeviceRegistryLock);
669
670 int32_t result = AKEY_STATE_UNKNOWN;
671 if (deviceId >= 0) {
672 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
673 if (deviceIndex >= 0) {
674 InputDevice* device = mDevices.valueAt(deviceIndex);
675 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
676 result = (device->*getStateFunc)(sourceMask, code);
677 }
678 }
679 } else {
680 size_t numDevices = mDevices.size();
681 for (size_t i = 0; i < numDevices; i++) {
682 InputDevice* device = mDevices.valueAt(i);
683 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
684 result = (device->*getStateFunc)(sourceMask, code);
685 if (result >= AKEY_STATE_DOWN) {
686 return result;
687 }
688 }
689 }
690 }
691 return result;
692 } // release device registy reader lock
693}
694
695bool InputReader::hasKeys(int32_t deviceId, uint32_t sourceMask,
696 size_t numCodes, const int32_t* keyCodes, uint8_t* outFlags) {
697 memset(outFlags, 0, numCodes);
698 return markSupportedKeyCodes(deviceId, sourceMask, numCodes, keyCodes, outFlags);
699}
700
701bool InputReader::markSupportedKeyCodes(int32_t deviceId, uint32_t sourceMask, size_t numCodes,
702 const int32_t* keyCodes, uint8_t* outFlags) {
703 { // acquire device registry reader lock
704 RWLock::AutoRLock _rl(mDeviceRegistryLock);
705 bool result = false;
706 if (deviceId >= 0) {
707 ssize_t deviceIndex = mDevices.indexOfKey(deviceId);
708 if (deviceIndex >= 0) {
709 InputDevice* device = mDevices.valueAt(deviceIndex);
710 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
711 result = device->markSupportedKeyCodes(sourceMask,
712 numCodes, keyCodes, outFlags);
713 }
714 }
715 } else {
716 size_t numDevices = mDevices.size();
717 for (size_t i = 0; i < numDevices; i++) {
718 InputDevice* device = mDevices.valueAt(i);
719 if (! device->isIgnored() && sourcesMatchMask(device->getSources(), sourceMask)) {
720 result |= device->markSupportedKeyCodes(sourceMask,
721 numCodes, keyCodes, outFlags);
722 }
723 }
724 }
725 return result;
726 } // release device registy reader lock
727}
728
Jeff Brown1a84fd12011-06-02 01:26:32 -0700729void InputReader::refreshConfiguration() {
730 android_atomic_release_store(1, &mRefreshConfiguration);
731}
732
Jeff Brownb88102f2010-09-08 11:49:43 -0700733void InputReader::dump(String8& dump) {
Jeff Brownf2f48712010-10-01 17:46:21 -0700734 mEventHub->dump(dump);
735 dump.append("\n");
736
737 dump.append("Input Reader State:\n");
738
Jeff Brownef3d7e82010-09-30 14:33:04 -0700739 { // acquire device registry reader lock
740 RWLock::AutoRLock _rl(mDeviceRegistryLock);
Jeff Brownb88102f2010-09-08 11:49:43 -0700741
Jeff Brownef3d7e82010-09-30 14:33:04 -0700742 for (size_t i = 0; i < mDevices.size(); i++) {
743 mDevices.valueAt(i)->dump(dump);
Jeff Brownb88102f2010-09-08 11:49:43 -0700744 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700745 } // release device registy reader lock
Jeff Brown214eaf42011-05-26 19:17:02 -0700746
747 dump.append(INDENT "Configuration:\n");
748 dump.append(INDENT2 "ExcludedDeviceNames: [");
749 for (size_t i = 0; i < mConfig.excludedDeviceNames.size(); i++) {
750 if (i != 0) {
751 dump.append(", ");
752 }
753 dump.append(mConfig.excludedDeviceNames.itemAt(i).string());
754 }
755 dump.append("]\n");
756 dump.appendFormat(INDENT2 "FilterTouchEvents: %s\n",
757 toString(mConfig.filterTouchEvents));
758 dump.appendFormat(INDENT2 "FilterJumpyTouchEvents: %s\n",
759 toString(mConfig.filterJumpyTouchEvents));
760 dump.appendFormat(INDENT2 "VirtualKeyQuietTime: %0.1fms\n",
761 mConfig.virtualKeyQuietTime * 0.000001f);
762
Jeff Brown19c97d42011-06-01 12:33:19 -0700763 dump.appendFormat(INDENT2 "PointerVelocityControlParameters: "
764 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
765 mConfig.pointerVelocityControlParameters.scale,
766 mConfig.pointerVelocityControlParameters.lowThreshold,
767 mConfig.pointerVelocityControlParameters.highThreshold,
768 mConfig.pointerVelocityControlParameters.acceleration);
769
770 dump.appendFormat(INDENT2 "WheelVelocityControlParameters: "
771 "scale=%0.3f, lowThreshold=%0.3f, highThreshold=%0.3f, acceleration=%0.3f\n",
772 mConfig.wheelVelocityControlParameters.scale,
773 mConfig.wheelVelocityControlParameters.lowThreshold,
774 mConfig.wheelVelocityControlParameters.highThreshold,
775 mConfig.wheelVelocityControlParameters.acceleration);
776
Jeff Brown214eaf42011-05-26 19:17:02 -0700777 dump.appendFormat(INDENT2 "PointerGesture:\n");
778 dump.appendFormat(INDENT3 "QuietInterval: %0.1fms\n",
779 mConfig.pointerGestureQuietInterval * 0.000001f);
780 dump.appendFormat(INDENT3 "DragMinSwitchSpeed: %0.1fpx/s\n",
781 mConfig.pointerGestureDragMinSwitchSpeed);
782 dump.appendFormat(INDENT3 "TapInterval: %0.1fms\n",
783 mConfig.pointerGestureTapInterval * 0.000001f);
784 dump.appendFormat(INDENT3 "TapDragInterval: %0.1fms\n",
785 mConfig.pointerGestureTapDragInterval * 0.000001f);
786 dump.appendFormat(INDENT3 "TapSlop: %0.1fpx\n",
787 mConfig.pointerGestureTapSlop);
788 dump.appendFormat(INDENT3 "MultitouchSettleInterval: %0.1fms\n",
789 mConfig.pointerGestureMultitouchSettleInterval * 0.000001f);
790 dump.appendFormat(INDENT3 "MultitouchMinSpeed: %0.1fpx/s\n",
791 mConfig.pointerGestureMultitouchMinSpeed);
792 dump.appendFormat(INDENT3 "SwipeTransitionAngleCosine: %0.1f\n",
793 mConfig.pointerGestureSwipeTransitionAngleCosine);
794 dump.appendFormat(INDENT3 "SwipeMaxWidthRatio: %0.1f\n",
795 mConfig.pointerGestureSwipeMaxWidthRatio);
796 dump.appendFormat(INDENT3 "MovementSpeedRatio: %0.1f\n",
797 mConfig.pointerGestureMovementSpeedRatio);
798 dump.appendFormat(INDENT3 "ZoomSpeedRatio: %0.1f\n",
799 mConfig.pointerGestureZoomSpeedRatio);
Jeff Brownb88102f2010-09-08 11:49:43 -0700800}
801
Jeff Brown6d0fec22010-07-23 21:28:06 -0700802
803// --- InputReaderThread ---
804
805InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
806 Thread(/*canCallJava*/ true), mReader(reader) {
807}
808
809InputReaderThread::~InputReaderThread() {
810}
811
812bool InputReaderThread::threadLoop() {
813 mReader->loopOnce();
814 return true;
815}
816
817
818// --- InputDevice ---
819
820InputDevice::InputDevice(InputReaderContext* context, int32_t id, const String8& name) :
Jeff Brown80fd47c2011-05-24 01:07:44 -0700821 mContext(context), mId(id), mName(name), mSources(0),
822 mIsExternal(false), mDropUntilNextSync(false) {
Jeff Brown6d0fec22010-07-23 21:28:06 -0700823}
824
825InputDevice::~InputDevice() {
826 size_t numMappers = mMappers.size();
827 for (size_t i = 0; i < numMappers; i++) {
828 delete mMappers[i];
829 }
830 mMappers.clear();
831}
832
Jeff Brownef3d7e82010-09-30 14:33:04 -0700833void InputDevice::dump(String8& dump) {
834 InputDeviceInfo deviceInfo;
835 getDeviceInfo(& deviceInfo);
836
Jeff Brown90655042010-12-02 13:50:46 -0800837 dump.appendFormat(INDENT "Device %d: %s\n", deviceInfo.getId(),
Jeff Brownef3d7e82010-09-30 14:33:04 -0700838 deviceInfo.getName().string());
Jeff Brown56194eb2011-03-02 19:23:13 -0800839 dump.appendFormat(INDENT2 "IsExternal: %s\n", toString(mIsExternal));
Jeff Brownef3d7e82010-09-30 14:33:04 -0700840 dump.appendFormat(INDENT2 "Sources: 0x%08x\n", deviceInfo.getSources());
841 dump.appendFormat(INDENT2 "KeyboardType: %d\n", deviceInfo.getKeyboardType());
Jeff Browncc0c1592011-02-19 05:07:28 -0800842
Jeff Brownefd32662011-03-08 15:13:06 -0800843 const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
Jeff Browncc0c1592011-02-19 05:07:28 -0800844 if (!ranges.isEmpty()) {
Jeff Brownef3d7e82010-09-30 14:33:04 -0700845 dump.append(INDENT2 "Motion Ranges:\n");
Jeff Browncc0c1592011-02-19 05:07:28 -0800846 for (size_t i = 0; i < ranges.size(); i++) {
Jeff Brownefd32662011-03-08 15:13:06 -0800847 const InputDeviceInfo::MotionRange& range = ranges.itemAt(i);
848 const char* label = getAxisLabel(range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800849 char name[32];
850 if (label) {
851 strncpy(name, label, sizeof(name));
852 name[sizeof(name) - 1] = '\0';
853 } else {
Jeff Brownefd32662011-03-08 15:13:06 -0800854 snprintf(name, sizeof(name), "%d", range.axis);
Jeff Browncc0c1592011-02-19 05:07:28 -0800855 }
Jeff Brownefd32662011-03-08 15:13:06 -0800856 dump.appendFormat(INDENT3 "%s: source=0x%08x, "
857 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f\n",
858 name, range.source, range.min, range.max, range.flat, range.fuzz);
Jeff Browncc0c1592011-02-19 05:07:28 -0800859 }
Jeff Brownef3d7e82010-09-30 14:33:04 -0700860 }
861
862 size_t numMappers = mMappers.size();
863 for (size_t i = 0; i < numMappers; i++) {
864 InputMapper* mapper = mMappers[i];
865 mapper->dump(dump);
866 }
867}
868
Jeff Brown6d0fec22010-07-23 21:28:06 -0700869void InputDevice::addMapper(InputMapper* mapper) {
870 mMappers.add(mapper);
871}
872
873void InputDevice::configure() {
Jeff Brown8d608662010-08-30 03:02:23 -0700874 if (! isIgnored()) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -0800875 mContext->getEventHub()->getConfiguration(mId, &mConfiguration);
Jeff Brown8d608662010-08-30 03:02:23 -0700876 }
877
Jeff Brown6d0fec22010-07-23 21:28:06 -0700878 mSources = 0;
879
880 size_t numMappers = mMappers.size();
881 for (size_t i = 0; i < numMappers; i++) {
882 InputMapper* mapper = mMappers[i];
883 mapper->configure();
884 mSources |= mapper->getSources();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700885 }
886}
887
Jeff Brown6d0fec22010-07-23 21:28:06 -0700888void InputDevice::reset() {
889 size_t numMappers = mMappers.size();
890 for (size_t i = 0; i < numMappers; i++) {
891 InputMapper* mapper = mMappers[i];
892 mapper->reset();
893 }
894}
Jeff Brown46b9ac02010-04-22 18:58:52 -0700895
Jeff Brownb7198742011-03-18 18:14:26 -0700896void InputDevice::process(const RawEvent* rawEvents, size_t count) {
897 // Process all of the events in order for each mapper.
898 // We cannot simply ask each mapper to process them in bulk because mappers may
899 // have side-effects that must be interleaved. For example, joystick movement events and
900 // gamepad button presses are handled by different mappers but they should be dispatched
901 // in the order received.
Jeff Brown6d0fec22010-07-23 21:28:06 -0700902 size_t numMappers = mMappers.size();
Jeff Brownb7198742011-03-18 18:14:26 -0700903 for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
904#if DEBUG_RAW_EVENTS
905 LOGD("Input event: device=%d type=0x%04x scancode=0x%04x "
906 "keycode=0x%04x value=0x%04x flags=0x%08x",
907 rawEvent->deviceId, rawEvent->type, rawEvent->scanCode, rawEvent->keyCode,
908 rawEvent->value, rawEvent->flags);
909#endif
910
Jeff Brown80fd47c2011-05-24 01:07:44 -0700911 if (mDropUntilNextSync) {
912 if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_REPORT) {
913 mDropUntilNextSync = false;
914#if DEBUG_RAW_EVENTS
915 LOGD("Recovered from input event buffer overrun.");
916#endif
917 } else {
918#if DEBUG_RAW_EVENTS
919 LOGD("Dropped input event while waiting for next input sync.");
920#endif
921 }
922 } else if (rawEvent->type == EV_SYN && rawEvent->scanCode == SYN_DROPPED) {
923 LOGI("Detected input event buffer overrun for device %s.", mName.string());
924 mDropUntilNextSync = true;
925 reset();
926 } else {
927 for (size_t i = 0; i < numMappers; i++) {
928 InputMapper* mapper = mMappers[i];
929 mapper->process(rawEvent);
930 }
Jeff Brownb7198742011-03-18 18:14:26 -0700931 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700932 }
933}
Jeff Brown46b9ac02010-04-22 18:58:52 -0700934
Jeff Brownaa3855d2011-03-17 01:34:19 -0700935void InputDevice::timeoutExpired(nsecs_t when) {
936 size_t numMappers = mMappers.size();
937 for (size_t i = 0; i < numMappers; i++) {
938 InputMapper* mapper = mMappers[i];
939 mapper->timeoutExpired(when);
940 }
941}
942
Jeff Brown6d0fec22010-07-23 21:28:06 -0700943void InputDevice::getDeviceInfo(InputDeviceInfo* outDeviceInfo) {
944 outDeviceInfo->initialize(mId, mName);
945
946 size_t numMappers = mMappers.size();
947 for (size_t i = 0; i < numMappers; i++) {
948 InputMapper* mapper = mMappers[i];
949 mapper->populateDeviceInfo(outDeviceInfo);
950 }
951}
952
953int32_t InputDevice::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
954 return getState(sourceMask, keyCode, & InputMapper::getKeyCodeState);
955}
956
957int32_t InputDevice::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
958 return getState(sourceMask, scanCode, & InputMapper::getScanCodeState);
959}
960
961int32_t InputDevice::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
962 return getState(sourceMask, switchCode, & InputMapper::getSwitchState);
963}
964
965int32_t InputDevice::getState(uint32_t sourceMask, int32_t code, GetStateFunc getStateFunc) {
966 int32_t result = AKEY_STATE_UNKNOWN;
967 size_t numMappers = mMappers.size();
968 for (size_t i = 0; i < numMappers; i++) {
969 InputMapper* mapper = mMappers[i];
970 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
971 result = (mapper->*getStateFunc)(sourceMask, code);
972 if (result >= AKEY_STATE_DOWN) {
973 return result;
974 }
975 }
976 }
977 return result;
978}
979
980bool InputDevice::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
981 const int32_t* keyCodes, uint8_t* outFlags) {
982 bool result = false;
983 size_t numMappers = mMappers.size();
984 for (size_t i = 0; i < numMappers; i++) {
985 InputMapper* mapper = mMappers[i];
986 if (sourcesMatchMask(mapper->getSources(), sourceMask)) {
987 result |= mapper->markSupportedKeyCodes(sourceMask, numCodes, keyCodes, outFlags);
988 }
989 }
990 return result;
991}
992
993int32_t InputDevice::getMetaState() {
994 int32_t result = 0;
995 size_t numMappers = mMappers.size();
996 for (size_t i = 0; i < numMappers; i++) {
997 InputMapper* mapper = mMappers[i];
998 result |= mapper->getMetaState();
999 }
1000 return result;
1001}
1002
Jeff Brown05dc66a2011-03-02 14:41:58 -08001003void InputDevice::fadePointer() {
1004 size_t numMappers = mMappers.size();
1005 for (size_t i = 0; i < numMappers; i++) {
1006 InputMapper* mapper = mMappers[i];
1007 mapper->fadePointer();
1008 }
1009}
1010
Jeff Brown6d0fec22010-07-23 21:28:06 -07001011
1012// --- InputMapper ---
1013
1014InputMapper::InputMapper(InputDevice* device) :
1015 mDevice(device), mContext(device->getContext()) {
1016}
1017
1018InputMapper::~InputMapper() {
1019}
1020
1021void InputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1022 info->addSource(getSources());
1023}
1024
Jeff Brownef3d7e82010-09-30 14:33:04 -07001025void InputMapper::dump(String8& dump) {
1026}
1027
Jeff Brown6d0fec22010-07-23 21:28:06 -07001028void InputMapper::configure() {
1029}
1030
1031void InputMapper::reset() {
1032}
1033
Jeff Brownaa3855d2011-03-17 01:34:19 -07001034void InputMapper::timeoutExpired(nsecs_t when) {
1035}
1036
Jeff Brown6d0fec22010-07-23 21:28:06 -07001037int32_t InputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1038 return AKEY_STATE_UNKNOWN;
1039}
1040
1041int32_t InputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1042 return AKEY_STATE_UNKNOWN;
1043}
1044
1045int32_t InputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1046 return AKEY_STATE_UNKNOWN;
1047}
1048
1049bool InputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1050 const int32_t* keyCodes, uint8_t* outFlags) {
1051 return false;
1052}
1053
1054int32_t InputMapper::getMetaState() {
1055 return 0;
1056}
1057
Jeff Brown05dc66a2011-03-02 14:41:58 -08001058void InputMapper::fadePointer() {
1059}
1060
Jeff Browncb1404e2011-01-15 18:14:15 -08001061void InputMapper::dumpRawAbsoluteAxisInfo(String8& dump,
1062 const RawAbsoluteAxisInfo& axis, const char* name) {
1063 if (axis.valid) {
1064 dump.appendFormat(INDENT4 "%s: min=%d, max=%d, flat=%d, fuzz=%d\n",
1065 name, axis.minValue, axis.maxValue, axis.flat, axis.fuzz);
1066 } else {
1067 dump.appendFormat(INDENT4 "%s: unknown range\n", name);
1068 }
1069}
1070
Jeff Brown6d0fec22010-07-23 21:28:06 -07001071
1072// --- SwitchInputMapper ---
1073
1074SwitchInputMapper::SwitchInputMapper(InputDevice* device) :
1075 InputMapper(device) {
1076}
1077
1078SwitchInputMapper::~SwitchInputMapper() {
1079}
1080
1081uint32_t SwitchInputMapper::getSources() {
Jeff Brown89de57a2011-01-19 18:41:38 -08001082 return AINPUT_SOURCE_SWITCH;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001083}
1084
1085void SwitchInputMapper::process(const RawEvent* rawEvent) {
1086 switch (rawEvent->type) {
1087 case EV_SW:
1088 processSwitch(rawEvent->when, rawEvent->scanCode, rawEvent->value);
1089 break;
1090 }
1091}
1092
1093void SwitchInputMapper::processSwitch(nsecs_t when, int32_t switchCode, int32_t switchValue) {
Jeff Brownb6997262010-10-08 22:31:17 -07001094 getDispatcher()->notifySwitch(when, switchCode, switchValue, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001095}
1096
1097int32_t SwitchInputMapper::getSwitchState(uint32_t sourceMask, int32_t switchCode) {
1098 return getEventHub()->getSwitchState(getDeviceId(), switchCode);
1099}
1100
1101
1102// --- KeyboardInputMapper ---
1103
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001104KeyboardInputMapper::KeyboardInputMapper(InputDevice* device,
Jeff Brownefd32662011-03-08 15:13:06 -08001105 uint32_t source, int32_t keyboardType) :
1106 InputMapper(device), mSource(source),
Jeff Brown6d0fec22010-07-23 21:28:06 -07001107 mKeyboardType(keyboardType) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001108 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001109}
1110
1111KeyboardInputMapper::~KeyboardInputMapper() {
1112}
1113
Jeff Brown6328cdc2010-07-29 18:18:33 -07001114void KeyboardInputMapper::initializeLocked() {
1115 mLocked.metaState = AMETA_NONE;
1116 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001117}
1118
1119uint32_t KeyboardInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001120 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001121}
1122
1123void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1124 InputMapper::populateDeviceInfo(info);
1125
1126 info->setKeyboardType(mKeyboardType);
1127}
1128
Jeff Brownef3d7e82010-09-30 14:33:04 -07001129void KeyboardInputMapper::dump(String8& dump) {
1130 { // acquire lock
1131 AutoMutex _l(mLock);
1132 dump.append(INDENT2 "Keyboard Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001133 dumpParameters(dump);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001134 dump.appendFormat(INDENT3 "KeyboardType: %d\n", mKeyboardType);
1135 dump.appendFormat(INDENT3 "KeyDowns: %d keys currently down\n", mLocked.keyDowns.size());
1136 dump.appendFormat(INDENT3 "MetaState: 0x%0x\n", mLocked.metaState);
1137 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1138 } // release lock
1139}
1140
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001141
1142void KeyboardInputMapper::configure() {
1143 InputMapper::configure();
1144
1145 // Configure basic parameters.
1146 configureParameters();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001147
1148 // Reset LEDs.
1149 {
1150 AutoMutex _l(mLock);
1151 resetLedStateLocked();
1152 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001153}
1154
1155void KeyboardInputMapper::configureParameters() {
1156 mParameters.orientationAware = false;
1157 getDevice()->getConfiguration().tryGetProperty(String8("keyboard.orientationAware"),
1158 mParameters.orientationAware);
1159
1160 mParameters.associatedDisplayId = mParameters.orientationAware ? 0 : -1;
1161}
1162
1163void KeyboardInputMapper::dumpParameters(String8& dump) {
1164 dump.append(INDENT3 "Parameters:\n");
1165 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1166 mParameters.associatedDisplayId);
1167 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1168 toString(mParameters.orientationAware));
1169}
1170
Jeff Brown6d0fec22010-07-23 21:28:06 -07001171void KeyboardInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001172 for (;;) {
1173 int32_t keyCode, scanCode;
1174 { // acquire lock
1175 AutoMutex _l(mLock);
1176
1177 // Synthesize key up event on reset if keys are currently down.
1178 if (mLocked.keyDowns.isEmpty()) {
1179 initializeLocked();
Jeff Brown49ed71d2010-12-06 17:13:33 -08001180 resetLedStateLocked();
Jeff Brown6328cdc2010-07-29 18:18:33 -07001181 break; // done
1182 }
1183
1184 const KeyDown& keyDown = mLocked.keyDowns.top();
1185 keyCode = keyDown.keyCode;
1186 scanCode = keyDown.scanCode;
1187 } // release lock
1188
Jeff Brown6d0fec22010-07-23 21:28:06 -07001189 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001190 processKey(when, false, keyCode, scanCode, 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001191 }
1192
1193 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001194 getContext()->updateGlobalMetaState();
1195}
1196
1197void KeyboardInputMapper::process(const RawEvent* rawEvent) {
1198 switch (rawEvent->type) {
1199 case EV_KEY: {
1200 int32_t scanCode = rawEvent->scanCode;
1201 if (isKeyboardOrGamepadKey(scanCode)) {
1202 processKey(rawEvent->when, rawEvent->value != 0, rawEvent->keyCode, scanCode,
1203 rawEvent->flags);
1204 }
1205 break;
1206 }
1207 }
1208}
1209
1210bool KeyboardInputMapper::isKeyboardOrGamepadKey(int32_t scanCode) {
1211 return scanCode < BTN_MOUSE
1212 || scanCode >= KEY_OK
Jeff Brown9e8e40c2011-03-03 03:39:29 -08001213 || (scanCode >= BTN_MISC && scanCode < BTN_MOUSE)
Jeff Browncb1404e2011-01-15 18:14:15 -08001214 || (scanCode >= BTN_JOYSTICK && scanCode < BTN_DIGI);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001215}
1216
Jeff Brown6328cdc2010-07-29 18:18:33 -07001217void KeyboardInputMapper::processKey(nsecs_t when, bool down, int32_t keyCode,
1218 int32_t scanCode, uint32_t policyFlags) {
1219 int32_t newMetaState;
1220 nsecs_t downTime;
1221 bool metaStateChanged = false;
1222
1223 { // acquire lock
1224 AutoMutex _l(mLock);
1225
1226 if (down) {
1227 // Rotate key codes according to orientation if needed.
1228 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001229 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001230 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001231 if (!getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1232 NULL, NULL, & orientation)) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001233 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001234 }
1235
1236 keyCode = rotateKeyCode(keyCode, orientation);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001237 }
1238
Jeff Brown6328cdc2010-07-29 18:18:33 -07001239 // Add key down.
1240 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
1241 if (keyDownIndex >= 0) {
1242 // key repeat, be sure to use same keycode as before in case of rotation
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001243 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001244 } else {
1245 // key down
Jeff Brownfe508922011-01-18 15:10:10 -08001246 if ((policyFlags & POLICY_FLAG_VIRTUAL)
1247 && mContext->shouldDropVirtualKey(when,
1248 getDevice(), keyCode, scanCode)) {
1249 return;
1250 }
1251
Jeff Brown6328cdc2010-07-29 18:18:33 -07001252 mLocked.keyDowns.push();
1253 KeyDown& keyDown = mLocked.keyDowns.editTop();
1254 keyDown.keyCode = keyCode;
1255 keyDown.scanCode = scanCode;
1256 }
1257
1258 mLocked.downTime = when;
1259 } else {
1260 // Remove key down.
1261 ssize_t keyDownIndex = findKeyDownLocked(scanCode);
1262 if (keyDownIndex >= 0) {
1263 // key up, be sure to use same keycode as before in case of rotation
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001264 keyCode = mLocked.keyDowns.itemAt(keyDownIndex).keyCode;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001265 mLocked.keyDowns.removeAt(size_t(keyDownIndex));
1266 } else {
1267 // key was not actually down
1268 LOGI("Dropping key up from device %s because the key was not down. "
1269 "keyCode=%d, scanCode=%d",
1270 getDeviceName().string(), keyCode, scanCode);
1271 return;
1272 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001273 }
1274
Jeff Brown6328cdc2010-07-29 18:18:33 -07001275 int32_t oldMetaState = mLocked.metaState;
1276 newMetaState = updateMetaState(keyCode, down, oldMetaState);
1277 if (oldMetaState != newMetaState) {
1278 mLocked.metaState = newMetaState;
1279 metaStateChanged = true;
Jeff Brown497a92c2010-09-12 17:55:08 -07001280 updateLedStateLocked(false);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001281 }
Jeff Brownfd035822010-06-30 16:10:35 -07001282
Jeff Brown6328cdc2010-07-29 18:18:33 -07001283 downTime = mLocked.downTime;
1284 } // release lock
1285
Jeff Brown56194eb2011-03-02 19:23:13 -08001286 // Key down on external an keyboard should wake the device.
1287 // We don't do this for internal keyboards to prevent them from waking up in your pocket.
1288 // For internal keyboards, the key layout file should specify the policy flags for
1289 // each wake key individually.
1290 // TODO: Use the input device configuration to control this behavior more finely.
1291 if (down && getDevice()->isExternal()
1292 && !(policyFlags & (POLICY_FLAG_WAKE | POLICY_FLAG_WAKE_DROPPED))) {
1293 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1294 }
1295
Jeff Brown6328cdc2010-07-29 18:18:33 -07001296 if (metaStateChanged) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001297 getContext()->updateGlobalMetaState();
Jeff Brown46b9ac02010-04-22 18:58:52 -07001298 }
1299
Jeff Brown05dc66a2011-03-02 14:41:58 -08001300 if (down && !isMetaKey(keyCode)) {
1301 getContext()->fadePointer();
1302 }
1303
Jeff Brownefd32662011-03-08 15:13:06 -08001304 getDispatcher()->notifyKey(when, getDeviceId(), mSource, policyFlags,
Jeff Brownb6997262010-10-08 22:31:17 -07001305 down ? AKEY_EVENT_ACTION_DOWN : AKEY_EVENT_ACTION_UP,
1306 AKEY_EVENT_FLAG_FROM_SYSTEM, keyCode, scanCode, newMetaState, downTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001307}
1308
Jeff Brown6328cdc2010-07-29 18:18:33 -07001309ssize_t KeyboardInputMapper::findKeyDownLocked(int32_t scanCode) {
1310 size_t n = mLocked.keyDowns.size();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001311 for (size_t i = 0; i < n; i++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001312 if (mLocked.keyDowns[i].scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001313 return i;
1314 }
1315 }
1316 return -1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001317}
1318
Jeff Brown6d0fec22010-07-23 21:28:06 -07001319int32_t KeyboardInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
1320 return getEventHub()->getKeyCodeState(getDeviceId(), keyCode);
1321}
Jeff Brown46b9ac02010-04-22 18:58:52 -07001322
Jeff Brown6d0fec22010-07-23 21:28:06 -07001323int32_t KeyboardInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
1324 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1325}
Jeff Brown46b9ac02010-04-22 18:58:52 -07001326
Jeff Brown6d0fec22010-07-23 21:28:06 -07001327bool KeyboardInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
1328 const int32_t* keyCodes, uint8_t* outFlags) {
1329 return getEventHub()->markSupportedKeyCodes(getDeviceId(), numCodes, keyCodes, outFlags);
1330}
1331
1332int32_t KeyboardInputMapper::getMetaState() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001333 { // acquire lock
1334 AutoMutex _l(mLock);
1335 return mLocked.metaState;
1336 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001337}
1338
Jeff Brown49ed71d2010-12-06 17:13:33 -08001339void KeyboardInputMapper::resetLedStateLocked() {
1340 initializeLedStateLocked(mLocked.capsLockLedState, LED_CAPSL);
1341 initializeLedStateLocked(mLocked.numLockLedState, LED_NUML);
1342 initializeLedStateLocked(mLocked.scrollLockLedState, LED_SCROLLL);
1343
1344 updateLedStateLocked(true);
1345}
1346
1347void KeyboardInputMapper::initializeLedStateLocked(LockedState::LedState& ledState, int32_t led) {
1348 ledState.avail = getEventHub()->hasLed(getDeviceId(), led);
1349 ledState.on = false;
1350}
1351
Jeff Brown497a92c2010-09-12 17:55:08 -07001352void KeyboardInputMapper::updateLedStateLocked(bool reset) {
1353 updateLedStateForModifierLocked(mLocked.capsLockLedState, LED_CAPSL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001354 AMETA_CAPS_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001355 updateLedStateForModifierLocked(mLocked.numLockLedState, LED_NUML,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001356 AMETA_NUM_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001357 updateLedStateForModifierLocked(mLocked.scrollLockLedState, LED_SCROLLL,
Jeff Brown51e7fe72010-10-29 22:19:53 -07001358 AMETA_SCROLL_LOCK_ON, reset);
Jeff Brown497a92c2010-09-12 17:55:08 -07001359}
1360
1361void KeyboardInputMapper::updateLedStateForModifierLocked(LockedState::LedState& ledState,
1362 int32_t led, int32_t modifier, bool reset) {
1363 if (ledState.avail) {
1364 bool desiredState = (mLocked.metaState & modifier) != 0;
Jeff Brown49ed71d2010-12-06 17:13:33 -08001365 if (reset || ledState.on != desiredState) {
Jeff Brown497a92c2010-09-12 17:55:08 -07001366 getEventHub()->setLedState(getDeviceId(), led, desiredState);
1367 ledState.on = desiredState;
1368 }
1369 }
1370}
1371
Jeff Brown6d0fec22010-07-23 21:28:06 -07001372
Jeff Brown83c09682010-12-23 17:50:18 -08001373// --- CursorInputMapper ---
Jeff Brown6d0fec22010-07-23 21:28:06 -07001374
Jeff Brown83c09682010-12-23 17:50:18 -08001375CursorInputMapper::CursorInputMapper(InputDevice* device) :
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001376 InputMapper(device) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001377 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001378}
1379
Jeff Brown83c09682010-12-23 17:50:18 -08001380CursorInputMapper::~CursorInputMapper() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001381}
1382
Jeff Brown83c09682010-12-23 17:50:18 -08001383uint32_t CursorInputMapper::getSources() {
Jeff Brownefd32662011-03-08 15:13:06 -08001384 return mSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001385}
1386
Jeff Brown83c09682010-12-23 17:50:18 -08001387void CursorInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001388 InputMapper::populateDeviceInfo(info);
1389
Jeff Brown83c09682010-12-23 17:50:18 -08001390 if (mParameters.mode == Parameters::MODE_POINTER) {
1391 float minX, minY, maxX, maxY;
1392 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
Jeff Brownefd32662011-03-08 15:13:06 -08001393 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, minX, maxX, 0.0f, 0.0f);
1394 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, minY, maxY, 0.0f, 0.0f);
Jeff Brown83c09682010-12-23 17:50:18 -08001395 }
1396 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08001397 info->addMotionRange(AMOTION_EVENT_AXIS_X, mSource, -1.0f, 1.0f, 0.0f, mXScale);
1398 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mSource, -1.0f, 1.0f, 0.0f, mYScale);
Jeff Brown83c09682010-12-23 17:50:18 -08001399 }
Jeff Brownefd32662011-03-08 15:13:06 -08001400 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mSource, 0.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001401
1402 if (mHaveVWheel) {
Jeff Brownefd32662011-03-08 15:13:06 -08001403 info->addMotionRange(AMOTION_EVENT_AXIS_VSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001404 }
1405 if (mHaveHWheel) {
Jeff Brownefd32662011-03-08 15:13:06 -08001406 info->addMotionRange(AMOTION_EVENT_AXIS_HSCROLL, mSource, -1.0f, 1.0f, 0.0f, 0.0f);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001407 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001408}
1409
Jeff Brown83c09682010-12-23 17:50:18 -08001410void CursorInputMapper::dump(String8& dump) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07001411 { // acquire lock
1412 AutoMutex _l(mLock);
Jeff Brown83c09682010-12-23 17:50:18 -08001413 dump.append(INDENT2 "Cursor Input Mapper:\n");
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001414 dumpParameters(dump);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001415 dump.appendFormat(INDENT3 "XScale: %0.3f\n", mXScale);
1416 dump.appendFormat(INDENT3 "YScale: %0.3f\n", mYScale);
Jeff Brownef3d7e82010-09-30 14:33:04 -07001417 dump.appendFormat(INDENT3 "XPrecision: %0.3f\n", mXPrecision);
1418 dump.appendFormat(INDENT3 "YPrecision: %0.3f\n", mYPrecision);
Jeff Brown6f2fba42011-02-19 01:08:02 -08001419 dump.appendFormat(INDENT3 "HaveVWheel: %s\n", toString(mHaveVWheel));
1420 dump.appendFormat(INDENT3 "HaveHWheel: %s\n", toString(mHaveHWheel));
1421 dump.appendFormat(INDENT3 "VWheelScale: %0.3f\n", mVWheelScale);
1422 dump.appendFormat(INDENT3 "HWheelScale: %0.3f\n", mHWheelScale);
Jeff Brownefd32662011-03-08 15:13:06 -08001423 dump.appendFormat(INDENT3 "ButtonState: 0x%08x\n", mLocked.buttonState);
1424 dump.appendFormat(INDENT3 "Down: %s\n", toString(isPointerDown(mLocked.buttonState)));
Jeff Brownef3d7e82010-09-30 14:33:04 -07001425 dump.appendFormat(INDENT3 "DownTime: %lld\n", mLocked.downTime);
1426 } // release lock
1427}
1428
Jeff Brown83c09682010-12-23 17:50:18 -08001429void CursorInputMapper::configure() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001430 InputMapper::configure();
1431
1432 // Configure basic parameters.
1433 configureParameters();
Jeff Brown83c09682010-12-23 17:50:18 -08001434
1435 // Configure device mode.
1436 switch (mParameters.mode) {
1437 case Parameters::MODE_POINTER:
Jeff Brownefd32662011-03-08 15:13:06 -08001438 mSource = AINPUT_SOURCE_MOUSE;
Jeff Brown83c09682010-12-23 17:50:18 -08001439 mXPrecision = 1.0f;
1440 mYPrecision = 1.0f;
1441 mXScale = 1.0f;
1442 mYScale = 1.0f;
1443 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
1444 break;
1445 case Parameters::MODE_NAVIGATION:
Jeff Brownefd32662011-03-08 15:13:06 -08001446 mSource = AINPUT_SOURCE_TRACKBALL;
Jeff Brown83c09682010-12-23 17:50:18 -08001447 mXPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1448 mYPrecision = TRACKBALL_MOVEMENT_THRESHOLD;
1449 mXScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1450 mYScale = 1.0f / TRACKBALL_MOVEMENT_THRESHOLD;
1451 break;
1452 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08001453
1454 mVWheelScale = 1.0f;
1455 mHWheelScale = 1.0f;
Jeff Browncc0c1592011-02-19 05:07:28 -08001456
1457 mHaveVWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_WHEEL);
1458 mHaveHWheel = getEventHub()->hasRelativeAxis(getDeviceId(), REL_HWHEEL);
Jeff Brown19c97d42011-06-01 12:33:19 -07001459
1460 mPointerVelocityControl.setParameters(getConfig()->pointerVelocityControlParameters);
1461 mWheelXVelocityControl.setParameters(getConfig()->wheelVelocityControlParameters);
1462 mWheelYVelocityControl.setParameters(getConfig()->wheelVelocityControlParameters);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001463}
1464
Jeff Brown83c09682010-12-23 17:50:18 -08001465void CursorInputMapper::configureParameters() {
1466 mParameters.mode = Parameters::MODE_POINTER;
1467 String8 cursorModeString;
1468 if (getDevice()->getConfiguration().tryGetProperty(String8("cursor.mode"), cursorModeString)) {
1469 if (cursorModeString == "navigation") {
1470 mParameters.mode = Parameters::MODE_NAVIGATION;
1471 } else if (cursorModeString != "pointer" && cursorModeString != "default") {
1472 LOGW("Invalid value for cursor.mode: '%s'", cursorModeString.string());
1473 }
1474 }
1475
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001476 mParameters.orientationAware = false;
Jeff Brown83c09682010-12-23 17:50:18 -08001477 getDevice()->getConfiguration().tryGetProperty(String8("cursor.orientationAware"),
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001478 mParameters.orientationAware);
1479
Jeff Brown83c09682010-12-23 17:50:18 -08001480 mParameters.associatedDisplayId = mParameters.mode == Parameters::MODE_POINTER
1481 || mParameters.orientationAware ? 0 : -1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001482}
1483
Jeff Brown83c09682010-12-23 17:50:18 -08001484void CursorInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001485 dump.append(INDENT3 "Parameters:\n");
1486 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
1487 mParameters.associatedDisplayId);
Jeff Brown83c09682010-12-23 17:50:18 -08001488
1489 switch (mParameters.mode) {
1490 case Parameters::MODE_POINTER:
1491 dump.append(INDENT4 "Mode: pointer\n");
1492 break;
1493 case Parameters::MODE_NAVIGATION:
1494 dump.append(INDENT4 "Mode: navigation\n");
1495 break;
1496 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07001497 LOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08001498 }
1499
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001500 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
1501 toString(mParameters.orientationAware));
1502}
1503
Jeff Brown83c09682010-12-23 17:50:18 -08001504void CursorInputMapper::initializeLocked() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001505 mAccumulator.clear();
1506
Jeff Brownefd32662011-03-08 15:13:06 -08001507 mLocked.buttonState = 0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001508 mLocked.downTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001509}
1510
Jeff Brown83c09682010-12-23 17:50:18 -08001511void CursorInputMapper::reset() {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001512 for (;;) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001513 int32_t buttonState;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001514 { // acquire lock
1515 AutoMutex _l(mLock);
1516
Jeff Brownefd32662011-03-08 15:13:06 -08001517 buttonState = mLocked.buttonState;
1518 if (!buttonState) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001519 initializeLocked();
1520 break; // done
1521 }
1522 } // release lock
1523
Jeff Brown19c97d42011-06-01 12:33:19 -07001524 // Reset velocity.
1525 mPointerVelocityControl.reset();
1526 mWheelXVelocityControl.reset();
1527 mWheelYVelocityControl.reset();
1528
Jeff Brown83c09682010-12-23 17:50:18 -08001529 // Synthesize button up event on reset.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001530 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Brownefd32662011-03-08 15:13:06 -08001531 mAccumulator.clear();
1532 mAccumulator.buttonDown = 0;
1533 mAccumulator.buttonUp = buttonState;
1534 mAccumulator.fields = Accumulator::FIELD_BUTTONS;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001535 sync(when);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001536 }
1537
Jeff Brown6d0fec22010-07-23 21:28:06 -07001538 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001539}
Jeff Brown46b9ac02010-04-22 18:58:52 -07001540
Jeff Brown83c09682010-12-23 17:50:18 -08001541void CursorInputMapper::process(const RawEvent* rawEvent) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001542 switch (rawEvent->type) {
Jeff Brownefd32662011-03-08 15:13:06 -08001543 case EV_KEY: {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001544 int32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode);
Jeff Brownefd32662011-03-08 15:13:06 -08001545 if (buttonState) {
1546 if (rawEvent->value) {
1547 mAccumulator.buttonDown = buttonState;
1548 mAccumulator.buttonUp = 0;
1549 } else {
1550 mAccumulator.buttonDown = 0;
1551 mAccumulator.buttonUp = buttonState;
1552 }
1553 mAccumulator.fields |= Accumulator::FIELD_BUTTONS;
1554
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001555 // Sync now since BTN_MOUSE is not necessarily followed by SYN_REPORT and
1556 // we need to ensure that we report the up/down promptly.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001557 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001558 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001559 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001560 break;
Jeff Brownefd32662011-03-08 15:13:06 -08001561 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001562
Jeff Brown6d0fec22010-07-23 21:28:06 -07001563 case EV_REL:
1564 switch (rawEvent->scanCode) {
1565 case REL_X:
1566 mAccumulator.fields |= Accumulator::FIELD_REL_X;
1567 mAccumulator.relX = rawEvent->value;
1568 break;
1569 case REL_Y:
1570 mAccumulator.fields |= Accumulator::FIELD_REL_Y;
1571 mAccumulator.relY = rawEvent->value;
1572 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08001573 case REL_WHEEL:
1574 mAccumulator.fields |= Accumulator::FIELD_REL_WHEEL;
1575 mAccumulator.relWheel = rawEvent->value;
1576 break;
1577 case REL_HWHEEL:
1578 mAccumulator.fields |= Accumulator::FIELD_REL_HWHEEL;
1579 mAccumulator.relHWheel = rawEvent->value;
1580 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001581 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001582 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001583
Jeff Brown6d0fec22010-07-23 21:28:06 -07001584 case EV_SYN:
1585 switch (rawEvent->scanCode) {
1586 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001587 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001588 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001589 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001590 break;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001591 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001592}
1593
Jeff Brown83c09682010-12-23 17:50:18 -08001594void CursorInputMapper::sync(nsecs_t when) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07001595 uint32_t fields = mAccumulator.fields;
1596 if (fields == 0) {
1597 return; // no new state changes, so nothing to do
1598 }
1599
Jeff Brown9626b142011-03-03 02:09:54 -08001600 int32_t motionEventAction;
1601 int32_t motionEventEdgeFlags;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001602 int32_t lastButtonState, currentButtonState;
1603 PointerProperties pointerProperties;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001604 PointerCoords pointerCoords;
1605 nsecs_t downTime;
Jeff Brown33bbfd22011-02-24 20:55:35 -08001606 float vscroll, hscroll;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001607 { // acquire lock
1608 AutoMutex _l(mLock);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001609
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001610 lastButtonState = mLocked.buttonState;
1611
Jeff Brownefd32662011-03-08 15:13:06 -08001612 bool down, downChanged;
1613 bool wasDown = isPointerDown(mLocked.buttonState);
1614 bool buttonsChanged = fields & Accumulator::FIELD_BUTTONS;
1615 if (buttonsChanged) {
1616 mLocked.buttonState = (mLocked.buttonState | mAccumulator.buttonDown)
1617 & ~mAccumulator.buttonUp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001618
Jeff Brownefd32662011-03-08 15:13:06 -08001619 down = isPointerDown(mLocked.buttonState);
1620
1621 if (!wasDown && down) {
1622 mLocked.downTime = when;
1623 downChanged = true;
1624 } else if (wasDown && !down) {
1625 downChanged = true;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001626 } else {
Jeff Brownefd32662011-03-08 15:13:06 -08001627 downChanged = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001628 }
Jeff Brownefd32662011-03-08 15:13:06 -08001629 } else {
1630 down = wasDown;
1631 downChanged = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001632 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001633
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001634 currentButtonState = mLocked.buttonState;
1635
Jeff Brown6328cdc2010-07-29 18:18:33 -07001636 downTime = mLocked.downTime;
Jeff Brown83c09682010-12-23 17:50:18 -08001637 float deltaX = fields & Accumulator::FIELD_REL_X ? mAccumulator.relX * mXScale : 0.0f;
1638 float deltaY = fields & Accumulator::FIELD_REL_Y ? mAccumulator.relY * mYScale : 0.0f;
Jeff Brown46b9ac02010-04-22 18:58:52 -07001639
Jeff Brown6328cdc2010-07-29 18:18:33 -07001640 if (downChanged) {
Jeff Brownefd32662011-03-08 15:13:06 -08001641 motionEventAction = down ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP;
1642 } else if (down || mPointerController == NULL) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001643 motionEventAction = AMOTION_EVENT_ACTION_MOVE;
Jeff Browncc0c1592011-02-19 05:07:28 -08001644 } else {
1645 motionEventAction = AMOTION_EVENT_ACTION_HOVER_MOVE;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001646 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001647
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001648 if (mParameters.orientationAware && mParameters.associatedDisplayId >= 0
Jeff Brown83c09682010-12-23 17:50:18 -08001649 && (deltaX != 0.0f || deltaY != 0.0f)) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07001650 // Rotate motion based on display orientation if needed.
1651 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
1652 int32_t orientation;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001653 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
1654 NULL, NULL, & orientation)) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001655 orientation = DISPLAY_ORIENTATION_0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001656 }
1657
1658 float temp;
1659 switch (orientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001660 case DISPLAY_ORIENTATION_90:
Jeff Brown83c09682010-12-23 17:50:18 -08001661 temp = deltaX;
1662 deltaX = deltaY;
1663 deltaY = -temp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001664 break;
1665
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001666 case DISPLAY_ORIENTATION_180:
Jeff Brown83c09682010-12-23 17:50:18 -08001667 deltaX = -deltaX;
1668 deltaY = -deltaY;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001669 break;
1670
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001671 case DISPLAY_ORIENTATION_270:
Jeff Brown83c09682010-12-23 17:50:18 -08001672 temp = deltaX;
1673 deltaX = -deltaY;
1674 deltaY = temp;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001675 break;
1676 }
1677 }
Jeff Brown83c09682010-12-23 17:50:18 -08001678
Jeff Brown9626b142011-03-03 02:09:54 -08001679 motionEventEdgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
1680
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001681 pointerProperties.clear();
1682 pointerProperties.id = 0;
1683 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_MOUSE;
1684
1685 pointerCoords.clear();
1686
Jeff Brown2352b972011-04-12 22:39:53 -07001687 if (mHaveVWheel && (fields & Accumulator::FIELD_REL_WHEEL)) {
1688 vscroll = mAccumulator.relWheel;
1689 } else {
1690 vscroll = 0;
1691 }
Jeff Brown19c97d42011-06-01 12:33:19 -07001692 mWheelYVelocityControl.move(when, NULL, &vscroll);
1693
Jeff Brown2352b972011-04-12 22:39:53 -07001694 if (mHaveHWheel && (fields & Accumulator::FIELD_REL_HWHEEL)) {
1695 hscroll = mAccumulator.relHWheel;
1696 } else {
1697 hscroll = 0;
1698 }
Jeff Brown19c97d42011-06-01 12:33:19 -07001699 mWheelXVelocityControl.move(when, &hscroll, NULL);
1700
1701 mPointerVelocityControl.move(when, &deltaX, &deltaY);
Jeff Brown2352b972011-04-12 22:39:53 -07001702
Jeff Brown83c09682010-12-23 17:50:18 -08001703 if (mPointerController != NULL) {
Jeff Brown2352b972011-04-12 22:39:53 -07001704 if (deltaX != 0 || deltaY != 0 || vscroll != 0 || hscroll != 0
1705 || buttonsChanged) {
1706 mPointerController->setPresentation(
1707 PointerControllerInterface::PRESENTATION_POINTER);
1708
1709 if (deltaX != 0 || deltaY != 0) {
1710 mPointerController->move(deltaX, deltaY);
1711 }
1712
1713 if (buttonsChanged) {
1714 mPointerController->setButtonState(mLocked.buttonState);
1715 }
1716
Jeff Brown538881e2011-05-25 18:23:38 -07001717 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
Jeff Brown83c09682010-12-23 17:50:18 -08001718 }
Jeff Brownefd32662011-03-08 15:13:06 -08001719
Jeff Brown91c69ab2011-02-14 17:03:18 -08001720 float x, y;
1721 mPointerController->getPosition(&x, &y);
Jeff Brownebbd5d12011-02-17 13:01:34 -08001722 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, x);
1723 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown9626b142011-03-03 02:09:54 -08001724
1725 if (motionEventAction == AMOTION_EVENT_ACTION_DOWN) {
Jeff Brownefd32662011-03-08 15:13:06 -08001726 motionEventEdgeFlags = calculateEdgeFlagsUsingPointerBounds(
1727 mPointerController, x, y);
Jeff Brown9626b142011-03-03 02:09:54 -08001728 }
Jeff Brown83c09682010-12-23 17:50:18 -08001729 } else {
Jeff Brownebbd5d12011-02-17 13:01:34 -08001730 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_X, deltaX);
1731 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_Y, deltaY);
Jeff Brown83c09682010-12-23 17:50:18 -08001732 }
1733
Jeff Brownefd32662011-03-08 15:13:06 -08001734 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, down ? 1.0f : 0.0f);
Jeff Brown6328cdc2010-07-29 18:18:33 -07001735 } // release lock
1736
Jeff Brown56194eb2011-03-02 19:23:13 -08001737 // Moving an external trackball or mouse should wake the device.
1738 // We don't do this for internal cursor devices to prevent them from waking up
1739 // the device in your pocket.
1740 // TODO: Use the input device configuration to control this behavior more finely.
1741 uint32_t policyFlags = 0;
1742 if (getDevice()->isExternal()) {
1743 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
1744 }
1745
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001746 // Synthesize key down from buttons if needed.
1747 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mSource,
1748 policyFlags, lastButtonState, currentButtonState);
1749
1750 // Send motion event.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001751 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownefd32662011-03-08 15:13:06 -08001752 getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001753 motionEventAction, 0, metaState, currentButtonState, motionEventEdgeFlags,
1754 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brownb6997262010-10-08 22:31:17 -07001755
Jeff Browna032cc02011-03-07 16:56:21 -08001756 // Send hover move after UP to tell the application that the mouse is hovering now.
1757 if (motionEventAction == AMOTION_EVENT_ACTION_UP
1758 && mPointerController != NULL) {
1759 getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001760 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
1761 metaState, currentButtonState, AMOTION_EVENT_EDGE_FLAG_NONE,
1762 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Browna032cc02011-03-07 16:56:21 -08001763 }
Jeff Brown33bbfd22011-02-24 20:55:35 -08001764
Jeff Browna032cc02011-03-07 16:56:21 -08001765 // Send scroll events.
Jeff Brown33bbfd22011-02-24 20:55:35 -08001766 if (vscroll != 0 || hscroll != 0) {
1767 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_VSCROLL, vscroll);
1768 pointerCoords.setAxisValue(AMOTION_EVENT_AXIS_HSCROLL, hscroll);
1769
Jeff Brownefd32662011-03-08 15:13:06 -08001770 getDispatcher()->notifyMotion(when, getDeviceId(), mSource, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001771 AMOTION_EVENT_ACTION_SCROLL, 0, metaState, currentButtonState,
1772 AMOTION_EVENT_EDGE_FLAG_NONE,
1773 1, &pointerProperties, &pointerCoords, mXPrecision, mYPrecision, downTime);
Jeff Brown33bbfd22011-02-24 20:55:35 -08001774 }
Jeff Browna032cc02011-03-07 16:56:21 -08001775
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07001776 // Synthesize key up from buttons if needed.
1777 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mSource,
1778 policyFlags, lastButtonState, currentButtonState);
1779
Jeff Browna032cc02011-03-07 16:56:21 -08001780 mAccumulator.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001781}
1782
Jeff Brown83c09682010-12-23 17:50:18 -08001783int32_t CursorInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brownc3fc2d02010-08-10 15:47:53 -07001784 if (scanCode >= BTN_MOUSE && scanCode < BTN_JOYSTICK) {
1785 return getEventHub()->getScanCodeState(getDeviceId(), scanCode);
1786 } else {
1787 return AKEY_STATE_UNKNOWN;
1788 }
1789}
1790
Jeff Brown05dc66a2011-03-02 14:41:58 -08001791void CursorInputMapper::fadePointer() {
1792 { // acquire lock
1793 AutoMutex _l(mLock);
Jeff Brownefd32662011-03-08 15:13:06 -08001794 if (mPointerController != NULL) {
Jeff Brown538881e2011-05-25 18:23:38 -07001795 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
Jeff Brownefd32662011-03-08 15:13:06 -08001796 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08001797 } // release lock
1798}
1799
Jeff Brown6d0fec22010-07-23 21:28:06 -07001800
1801// --- TouchInputMapper ---
1802
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001803TouchInputMapper::TouchInputMapper(InputDevice* device) :
1804 InputMapper(device) {
Jeff Brown214eaf42011-05-26 19:17:02 -07001805 mConfig = getConfig();
1806
Jeff Brown6328cdc2010-07-29 18:18:33 -07001807 mLocked.surfaceOrientation = -1;
1808 mLocked.surfaceWidth = -1;
1809 mLocked.surfaceHeight = -1;
1810
1811 initializeLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001812}
1813
1814TouchInputMapper::~TouchInputMapper() {
1815}
1816
1817uint32_t TouchInputMapper::getSources() {
Jeff Brownace13b12011-03-09 17:39:48 -08001818 return mTouchSource | mPointerSource;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001819}
1820
1821void TouchInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
1822 InputMapper::populateDeviceInfo(info);
1823
Jeff Brown6328cdc2010-07-29 18:18:33 -07001824 { // acquire lock
1825 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001826
Jeff Brown6328cdc2010-07-29 18:18:33 -07001827 // Ensure surface information is up to date so that orientation changes are
1828 // noticed immediately.
Jeff Brownefd32662011-03-08 15:13:06 -08001829 if (!configureSurfaceLocked()) {
1830 return;
1831 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001832
Jeff Brownefd32662011-03-08 15:13:06 -08001833 info->addMotionRange(mLocked.orientedRanges.x);
1834 info->addMotionRange(mLocked.orientedRanges.y);
Jeff Brown8d608662010-08-30 03:02:23 -07001835
1836 if (mLocked.orientedRanges.havePressure) {
Jeff Brownefd32662011-03-08 15:13:06 -08001837 info->addMotionRange(mLocked.orientedRanges.pressure);
Jeff Brown8d608662010-08-30 03:02:23 -07001838 }
1839
1840 if (mLocked.orientedRanges.haveSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08001841 info->addMotionRange(mLocked.orientedRanges.size);
Jeff Brown8d608662010-08-30 03:02:23 -07001842 }
1843
Jeff Brownc6d282b2010-10-14 21:42:15 -07001844 if (mLocked.orientedRanges.haveTouchSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08001845 info->addMotionRange(mLocked.orientedRanges.touchMajor);
1846 info->addMotionRange(mLocked.orientedRanges.touchMinor);
Jeff Brown8d608662010-08-30 03:02:23 -07001847 }
1848
Jeff Brownc6d282b2010-10-14 21:42:15 -07001849 if (mLocked.orientedRanges.haveToolSize) {
Jeff Brownefd32662011-03-08 15:13:06 -08001850 info->addMotionRange(mLocked.orientedRanges.toolMajor);
1851 info->addMotionRange(mLocked.orientedRanges.toolMinor);
Jeff Brown8d608662010-08-30 03:02:23 -07001852 }
1853
1854 if (mLocked.orientedRanges.haveOrientation) {
Jeff Brownefd32662011-03-08 15:13:06 -08001855 info->addMotionRange(mLocked.orientedRanges.orientation);
Jeff Brown8d608662010-08-30 03:02:23 -07001856 }
Jeff Brownace13b12011-03-09 17:39:48 -08001857
Jeff Brown80fd47c2011-05-24 01:07:44 -07001858 if (mLocked.orientedRanges.haveDistance) {
1859 info->addMotionRange(mLocked.orientedRanges.distance);
1860 }
1861
Jeff Brownace13b12011-03-09 17:39:48 -08001862 if (mPointerController != NULL) {
1863 float minX, minY, maxX, maxY;
1864 if (mPointerController->getBounds(&minX, &minY, &maxX, &maxY)) {
1865 info->addMotionRange(AMOTION_EVENT_AXIS_X, mPointerSource,
1866 minX, maxX, 0.0f, 0.0f);
1867 info->addMotionRange(AMOTION_EVENT_AXIS_Y, mPointerSource,
1868 minY, maxY, 0.0f, 0.0f);
1869 }
1870 info->addMotionRange(AMOTION_EVENT_AXIS_PRESSURE, mPointerSource,
1871 0.0f, 1.0f, 0.0f, 0.0f);
1872 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07001873 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001874}
1875
Jeff Brownef3d7e82010-09-30 14:33:04 -07001876void TouchInputMapper::dump(String8& dump) {
1877 { // acquire lock
1878 AutoMutex _l(mLock);
1879 dump.append(INDENT2 "Touch Input Mapper:\n");
Jeff Brownef3d7e82010-09-30 14:33:04 -07001880 dumpParameters(dump);
1881 dumpVirtualKeysLocked(dump);
1882 dumpRawAxes(dump);
1883 dumpCalibration(dump);
1884 dumpSurfaceLocked(dump);
Jeff Brownefd32662011-03-08 15:13:06 -08001885
Jeff Brown511ee5f2010-10-18 13:32:20 -07001886 dump.appendFormat(INDENT3 "Translation and Scaling Factors:\n");
Jeff Brownc6d282b2010-10-14 21:42:15 -07001887 dump.appendFormat(INDENT4 "XScale: %0.3f\n", mLocked.xScale);
1888 dump.appendFormat(INDENT4 "YScale: %0.3f\n", mLocked.yScale);
1889 dump.appendFormat(INDENT4 "XPrecision: %0.3f\n", mLocked.xPrecision);
1890 dump.appendFormat(INDENT4 "YPrecision: %0.3f\n", mLocked.yPrecision);
1891 dump.appendFormat(INDENT4 "GeometricScale: %0.3f\n", mLocked.geometricScale);
1892 dump.appendFormat(INDENT4 "ToolSizeLinearScale: %0.3f\n", mLocked.toolSizeLinearScale);
1893 dump.appendFormat(INDENT4 "ToolSizeLinearBias: %0.3f\n", mLocked.toolSizeLinearBias);
1894 dump.appendFormat(INDENT4 "ToolSizeAreaScale: %0.3f\n", mLocked.toolSizeAreaScale);
1895 dump.appendFormat(INDENT4 "ToolSizeAreaBias: %0.3f\n", mLocked.toolSizeAreaBias);
1896 dump.appendFormat(INDENT4 "PressureScale: %0.3f\n", mLocked.pressureScale);
1897 dump.appendFormat(INDENT4 "SizeScale: %0.3f\n", mLocked.sizeScale);
Jeff Brownefd32662011-03-08 15:13:06 -08001898 dump.appendFormat(INDENT4 "OrientationScale: %0.3f\n", mLocked.orientationScale);
Jeff Brown80fd47c2011-05-24 01:07:44 -07001899 dump.appendFormat(INDENT4 "DistanceScale: %0.3f\n", mLocked.distanceScale);
Jeff Brownefd32662011-03-08 15:13:06 -08001900
1901 dump.appendFormat(INDENT3 "Last Touch:\n");
1902 dump.appendFormat(INDENT4 "Pointer Count: %d\n", mLastTouch.pointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08001903 dump.appendFormat(INDENT4 "Button State: 0x%08x\n", mLastTouch.buttonState);
1904
1905 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
1906 dump.appendFormat(INDENT3 "Pointer Gesture Detector:\n");
1907 dump.appendFormat(INDENT4 "XMovementScale: %0.3f\n",
1908 mLocked.pointerGestureXMovementScale);
1909 dump.appendFormat(INDENT4 "YMovementScale: %0.3f\n",
1910 mLocked.pointerGestureYMovementScale);
1911 dump.appendFormat(INDENT4 "XZoomScale: %0.3f\n",
1912 mLocked.pointerGestureXZoomScale);
1913 dump.appendFormat(INDENT4 "YZoomScale: %0.3f\n",
1914 mLocked.pointerGestureYZoomScale);
Jeff Brown2352b972011-04-12 22:39:53 -07001915 dump.appendFormat(INDENT4 "MaxSwipeWidth: %f\n",
1916 mLocked.pointerGestureMaxSwipeWidth);
Jeff Brownace13b12011-03-09 17:39:48 -08001917 }
Jeff Brownef3d7e82010-09-30 14:33:04 -07001918 } // release lock
1919}
1920
Jeff Brown6328cdc2010-07-29 18:18:33 -07001921void TouchInputMapper::initializeLocked() {
1922 mCurrentTouch.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001923 mLastTouch.clear();
1924 mDownTime = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07001925
1926 for (uint32_t i = 0; i < MAX_POINTERS; i++) {
1927 mAveragingTouchFilter.historyStart[i] = 0;
1928 mAveragingTouchFilter.historyEnd[i] = 0;
1929 }
1930
1931 mJumpyTouchFilter.jumpyPointsDropped = 0;
Jeff Brown6328cdc2010-07-29 18:18:33 -07001932
1933 mLocked.currentVirtualKey.down = false;
Jeff Brown8d608662010-08-30 03:02:23 -07001934
1935 mLocked.orientedRanges.havePressure = false;
1936 mLocked.orientedRanges.haveSize = false;
Jeff Brownc6d282b2010-10-14 21:42:15 -07001937 mLocked.orientedRanges.haveTouchSize = false;
1938 mLocked.orientedRanges.haveToolSize = false;
Jeff Brown8d608662010-08-30 03:02:23 -07001939 mLocked.orientedRanges.haveOrientation = false;
Jeff Brown80fd47c2011-05-24 01:07:44 -07001940 mLocked.orientedRanges.haveDistance = false;
Jeff Brownace13b12011-03-09 17:39:48 -08001941
1942 mPointerGesture.reset();
Jeff Brown19c97d42011-06-01 12:33:19 -07001943 mPointerGesture.pointerVelocityControl.setParameters(mConfig->pointerVelocityControlParameters);
Jeff Brown8d608662010-08-30 03:02:23 -07001944}
1945
Jeff Brown6d0fec22010-07-23 21:28:06 -07001946void TouchInputMapper::configure() {
1947 InputMapper::configure();
1948
1949 // Configure basic parameters.
Jeff Brown8d608662010-08-30 03:02:23 -07001950 configureParameters();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001951
Jeff Brown83c09682010-12-23 17:50:18 -08001952 // Configure sources.
1953 switch (mParameters.deviceType) {
1954 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
Jeff Brownefd32662011-03-08 15:13:06 -08001955 mTouchSource = AINPUT_SOURCE_TOUCHSCREEN;
Jeff Brownace13b12011-03-09 17:39:48 -08001956 mPointerSource = 0;
Jeff Brown83c09682010-12-23 17:50:18 -08001957 break;
1958 case Parameters::DEVICE_TYPE_TOUCH_PAD:
Jeff Brownefd32662011-03-08 15:13:06 -08001959 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
Jeff Brownace13b12011-03-09 17:39:48 -08001960 mPointerSource = 0;
1961 break;
1962 case Parameters::DEVICE_TYPE_POINTER:
1963 mTouchSource = AINPUT_SOURCE_TOUCHPAD;
1964 mPointerSource = AINPUT_SOURCE_MOUSE;
Jeff Brown83c09682010-12-23 17:50:18 -08001965 break;
1966 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07001967 LOG_ASSERT(false);
Jeff Brown83c09682010-12-23 17:50:18 -08001968 }
1969
Jeff Brown6d0fec22010-07-23 21:28:06 -07001970 // Configure absolute axis information.
Jeff Brown8d608662010-08-30 03:02:23 -07001971 configureRawAxes();
Jeff Brown8d608662010-08-30 03:02:23 -07001972
1973 // Prepare input device calibration.
1974 parseCalibration();
1975 resolveCalibration();
Jeff Brown6d0fec22010-07-23 21:28:06 -07001976
Jeff Brown6328cdc2010-07-29 18:18:33 -07001977 { // acquire lock
1978 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07001979
Jeff Brown8d608662010-08-30 03:02:23 -07001980 // Configure surface dimensions and orientation.
Jeff Brown6328cdc2010-07-29 18:18:33 -07001981 configureSurfaceLocked();
1982 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07001983}
1984
Jeff Brown8d608662010-08-30 03:02:23 -07001985void TouchInputMapper::configureParameters() {
Jeff Brown214eaf42011-05-26 19:17:02 -07001986 mParameters.useBadTouchFilter = mConfig->filterTouchEvents;
1987 mParameters.useAveragingTouchFilter = mConfig->filterTouchEvents;
1988 mParameters.useJumpyTouchFilter = mConfig->filterJumpyTouchEvents;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08001989
Jeff Brown538881e2011-05-25 18:23:38 -07001990 // TODO: select the default gesture mode based on whether the device supports
1991 // distinct multitouch
Jeff Brown2352b972011-04-12 22:39:53 -07001992 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
1993
Jeff Brown538881e2011-05-25 18:23:38 -07001994 String8 gestureModeString;
1995 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
1996 gestureModeString)) {
1997 if (gestureModeString == "pointer") {
1998 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
1999 } else if (gestureModeString == "spots") {
2000 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2001 } else if (gestureModeString != "default") {
2002 LOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
2003 }
2004 }
2005
Jeff Brownace13b12011-03-09 17:39:48 -08002006 if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
2007 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2008 // The device is a cursor device with a touch pad attached.
2009 // By default don't use the touch pad to move the pointer.
2010 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002011 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2012 // The device is a pointing device like a track pad.
2013 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2014 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2015 // The device is a touch screen.
2016 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownace13b12011-03-09 17:39:48 -08002017 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002018 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002019 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2020 }
2021
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002022 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002023 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2024 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002025 if (deviceTypeString == "touchScreen") {
2026 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002027 } else if (deviceTypeString == "touchPad") {
2028 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002029 } else if (deviceTypeString == "pointer") {
2030 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002031 } else if (deviceTypeString != "default") {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002032 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
2033 }
2034 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002035
Jeff Brownefd32662011-03-08 15:13:06 -08002036 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002037 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2038 mParameters.orientationAware);
2039
Jeff Brownefd32662011-03-08 15:13:06 -08002040 mParameters.associatedDisplayId = mParameters.orientationAware
2041 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownace13b12011-03-09 17:39:48 -08002042 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
Jeff Brownefd32662011-03-08 15:13:06 -08002043 ? 0 : -1;
Jeff Brown8d608662010-08-30 03:02:23 -07002044}
2045
Jeff Brownef3d7e82010-09-30 14:33:04 -07002046void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002047 dump.append(INDENT3 "Parameters:\n");
2048
Jeff Brown538881e2011-05-25 18:23:38 -07002049 switch (mParameters.gestureMode) {
2050 case Parameters::GESTURE_MODE_POINTER:
2051 dump.append(INDENT4 "GestureMode: pointer\n");
2052 break;
2053 case Parameters::GESTURE_MODE_SPOTS:
2054 dump.append(INDENT4 "GestureMode: spots\n");
2055 break;
2056 default:
2057 assert(false);
2058 }
2059
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002060 switch (mParameters.deviceType) {
2061 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2062 dump.append(INDENT4 "DeviceType: touchScreen\n");
2063 break;
2064 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2065 dump.append(INDENT4 "DeviceType: touchPad\n");
2066 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002067 case Parameters::DEVICE_TYPE_POINTER:
2068 dump.append(INDENT4 "DeviceType: pointer\n");
2069 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002070 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002071 LOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002072 }
2073
2074 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2075 mParameters.associatedDisplayId);
2076 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2077 toString(mParameters.orientationAware));
2078
2079 dump.appendFormat(INDENT4 "UseBadTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002080 toString(mParameters.useBadTouchFilter));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002081 dump.appendFormat(INDENT4 "UseAveragingTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002082 toString(mParameters.useAveragingTouchFilter));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002083 dump.appendFormat(INDENT4 "UseJumpyTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002084 toString(mParameters.useJumpyTouchFilter));
Jeff Brownb88102f2010-09-08 11:49:43 -07002085}
2086
Jeff Brown8d608662010-08-30 03:02:23 -07002087void TouchInputMapper::configureRawAxes() {
2088 mRawAxes.x.clear();
2089 mRawAxes.y.clear();
2090 mRawAxes.pressure.clear();
2091 mRawAxes.touchMajor.clear();
2092 mRawAxes.touchMinor.clear();
2093 mRawAxes.toolMajor.clear();
2094 mRawAxes.toolMinor.clear();
2095 mRawAxes.orientation.clear();
Jeff Brown80fd47c2011-05-24 01:07:44 -07002096 mRawAxes.distance.clear();
2097 mRawAxes.trackingId.clear();
2098 mRawAxes.slot.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002099}
2100
Jeff Brownef3d7e82010-09-30 14:33:04 -07002101void TouchInputMapper::dumpRawAxes(String8& dump) {
2102 dump.append(INDENT3 "Raw Axes:\n");
Jeff Browncb1404e2011-01-15 18:14:15 -08002103 dumpRawAbsoluteAxisInfo(dump, mRawAxes.x, "X");
2104 dumpRawAbsoluteAxisInfo(dump, mRawAxes.y, "Y");
2105 dumpRawAbsoluteAxisInfo(dump, mRawAxes.pressure, "Pressure");
2106 dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor");
2107 dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor");
2108 dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor");
2109 dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor");
2110 dumpRawAbsoluteAxisInfo(dump, mRawAxes.orientation, "Orientation");
Jeff Brown80fd47c2011-05-24 01:07:44 -07002111 dumpRawAbsoluteAxisInfo(dump, mRawAxes.distance, "Distance");
2112 dumpRawAbsoluteAxisInfo(dump, mRawAxes.trackingId, "TrackingId");
2113 dumpRawAbsoluteAxisInfo(dump, mRawAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002114}
2115
Jeff Brown6328cdc2010-07-29 18:18:33 -07002116bool TouchInputMapper::configureSurfaceLocked() {
Jeff Brown9626b142011-03-03 02:09:54 -08002117 // Ensure we have valid X and Y axes.
2118 if (!mRawAxes.x.valid || !mRawAxes.y.valid) {
2119 LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
2120 "The device will be inoperable.", getDeviceName().string());
2121 return false;
2122 }
2123
Jeff Brown6d0fec22010-07-23 21:28:06 -07002124 // Update orientation and dimensions if needed.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002125 int32_t orientation = DISPLAY_ORIENTATION_0;
Jeff Brown9626b142011-03-03 02:09:54 -08002126 int32_t width = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2127 int32_t height = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002128
2129 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002130 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002131 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownefd32662011-03-08 15:13:06 -08002132 &mLocked.associatedDisplayWidth, &mLocked.associatedDisplayHeight,
2133 &mLocked.associatedDisplayOrientation)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002134 return false;
2135 }
Jeff Brownefd32662011-03-08 15:13:06 -08002136
2137 // A touch screen inherits the dimensions of the display.
2138 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
2139 width = mLocked.associatedDisplayWidth;
2140 height = mLocked.associatedDisplayHeight;
2141 }
2142
2143 // The device inherits the orientation of the display if it is orientation aware.
2144 if (mParameters.orientationAware) {
2145 orientation = mLocked.associatedDisplayOrientation;
2146 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002147 }
2148
Jeff Brownace13b12011-03-09 17:39:48 -08002149 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2150 && mPointerController == NULL) {
2151 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2152 }
2153
Jeff Brown6328cdc2010-07-29 18:18:33 -07002154 bool orientationChanged = mLocked.surfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002155 if (orientationChanged) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002156 mLocked.surfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002157 }
2158
Jeff Brown6328cdc2010-07-29 18:18:33 -07002159 bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002160 if (sizeChanged) {
Jeff Brownefd32662011-03-08 15:13:06 -08002161 LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002162 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown8d608662010-08-30 03:02:23 -07002163
Jeff Brown6328cdc2010-07-29 18:18:33 -07002164 mLocked.surfaceWidth = width;
2165 mLocked.surfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002166
Jeff Brown8d608662010-08-30 03:02:23 -07002167 // Configure X and Y factors.
Jeff Brown9626b142011-03-03 02:09:54 -08002168 mLocked.xScale = float(width) / (mRawAxes.x.maxValue - mRawAxes.x.minValue + 1);
2169 mLocked.yScale = float(height) / (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1);
2170 mLocked.xPrecision = 1.0f / mLocked.xScale;
2171 mLocked.yPrecision = 1.0f / mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002172
Jeff Brownefd32662011-03-08 15:13:06 -08002173 mLocked.orientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
2174 mLocked.orientedRanges.x.source = mTouchSource;
2175 mLocked.orientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
2176 mLocked.orientedRanges.y.source = mTouchSource;
2177
Jeff Brown9626b142011-03-03 02:09:54 -08002178 configureVirtualKeysLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002179
Jeff Brown8d608662010-08-30 03:02:23 -07002180 // Scale factor for terms that are not oriented in a particular axis.
2181 // If the pixels are square then xScale == yScale otherwise we fake it
2182 // by choosing an average.
2183 mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002184
Jeff Brown8d608662010-08-30 03:02:23 -07002185 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002186 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002187
Jeff Brown8d608662010-08-30 03:02:23 -07002188 // TouchMajor and TouchMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002189 if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) {
2190 mLocked.orientedRanges.haveTouchSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002191
2192 mLocked.orientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
2193 mLocked.orientedRanges.touchMajor.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002194 mLocked.orientedRanges.touchMajor.min = 0;
2195 mLocked.orientedRanges.touchMajor.max = diagonalSize;
2196 mLocked.orientedRanges.touchMajor.flat = 0;
2197 mLocked.orientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002198
Jeff Brown8d608662010-08-30 03:02:23 -07002199 mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor;
Jeff Brownefd32662011-03-08 15:13:06 -08002200 mLocked.orientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002201 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002202
Jeff Brown8d608662010-08-30 03:02:23 -07002203 // ToolMajor and ToolMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002204 mLocked.toolSizeLinearScale = 0;
2205 mLocked.toolSizeLinearBias = 0;
2206 mLocked.toolSizeAreaScale = 0;
2207 mLocked.toolSizeAreaBias = 0;
2208 if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
2209 if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) {
2210 if (mCalibration.haveToolSizeLinearScale) {
2211 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
Jeff Brown8d608662010-08-30 03:02:23 -07002212 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002213 mLocked.toolSizeLinearScale = float(min(width, height))
Jeff Brown8d608662010-08-30 03:02:23 -07002214 / mRawAxes.toolMajor.maxValue;
2215 }
2216
Jeff Brownc6d282b2010-10-14 21:42:15 -07002217 if (mCalibration.haveToolSizeLinearBias) {
2218 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
2219 }
2220 } else if (mCalibration.toolSizeCalibration ==
2221 Calibration::TOOL_SIZE_CALIBRATION_AREA) {
2222 if (mCalibration.haveToolSizeLinearScale) {
2223 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
2224 } else {
2225 mLocked.toolSizeLinearScale = min(width, height);
2226 }
2227
2228 if (mCalibration.haveToolSizeLinearBias) {
2229 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
2230 }
2231
2232 if (mCalibration.haveToolSizeAreaScale) {
2233 mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale;
2234 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
2235 mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue;
2236 }
2237
2238 if (mCalibration.haveToolSizeAreaBias) {
2239 mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -07002240 }
2241 }
2242
Jeff Brownc6d282b2010-10-14 21:42:15 -07002243 mLocked.orientedRanges.haveToolSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002244
2245 mLocked.orientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
2246 mLocked.orientedRanges.toolMajor.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002247 mLocked.orientedRanges.toolMajor.min = 0;
2248 mLocked.orientedRanges.toolMajor.max = diagonalSize;
2249 mLocked.orientedRanges.toolMajor.flat = 0;
2250 mLocked.orientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002251
Jeff Brown8d608662010-08-30 03:02:23 -07002252 mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor;
Jeff Brownefd32662011-03-08 15:13:06 -08002253 mLocked.orientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002254 }
2255
2256 // Pressure factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002257 mLocked.pressureScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002258 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
2259 RawAbsoluteAxisInfo rawPressureAxis;
2260 switch (mCalibration.pressureSource) {
2261 case Calibration::PRESSURE_SOURCE_PRESSURE:
2262 rawPressureAxis = mRawAxes.pressure;
2263 break;
2264 case Calibration::PRESSURE_SOURCE_TOUCH:
2265 rawPressureAxis = mRawAxes.touchMajor;
2266 break;
2267 default:
2268 rawPressureAxis.clear();
2269 }
2270
Jeff Brown8d608662010-08-30 03:02:23 -07002271 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2272 || mCalibration.pressureCalibration
2273 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2274 if (mCalibration.havePressureScale) {
2275 mLocked.pressureScale = mCalibration.pressureScale;
2276 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
2277 mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue;
2278 }
2279 }
2280
2281 mLocked.orientedRanges.havePressure = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002282
2283 mLocked.orientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2284 mLocked.orientedRanges.pressure.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002285 mLocked.orientedRanges.pressure.min = 0;
2286 mLocked.orientedRanges.pressure.max = 1.0;
2287 mLocked.orientedRanges.pressure.flat = 0;
2288 mLocked.orientedRanges.pressure.fuzz = 0;
2289 }
2290
2291 // Size factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002292 mLocked.sizeScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002293 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002294 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
2295 if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
2296 mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue;
2297 }
2298 }
2299
2300 mLocked.orientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002301
2302 mLocked.orientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
2303 mLocked.orientedRanges.size.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002304 mLocked.orientedRanges.size.min = 0;
2305 mLocked.orientedRanges.size.max = 1.0;
2306 mLocked.orientedRanges.size.flat = 0;
2307 mLocked.orientedRanges.size.fuzz = 0;
2308 }
2309
2310 // Orientation
Jeff Brownc6d282b2010-10-14 21:42:15 -07002311 mLocked.orientationScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002312 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002313 if (mCalibration.orientationCalibration
2314 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
2315 if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) {
2316 mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue;
2317 }
2318 }
2319
Jeff Brown80fd47c2011-05-24 01:07:44 -07002320 mLocked.orientedRanges.haveOrientation = true;
2321
Jeff Brownefd32662011-03-08 15:13:06 -08002322 mLocked.orientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2323 mLocked.orientedRanges.orientation.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002324 mLocked.orientedRanges.orientation.min = - M_PI_2;
2325 mLocked.orientedRanges.orientation.max = M_PI_2;
2326 mLocked.orientedRanges.orientation.flat = 0;
2327 mLocked.orientedRanges.orientation.fuzz = 0;
2328 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002329
2330 // Distance
2331 mLocked.distanceScale = 0;
2332 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
2333 if (mCalibration.distanceCalibration
2334 == Calibration::DISTANCE_CALIBRATION_SCALED) {
2335 if (mCalibration.haveDistanceScale) {
2336 mLocked.distanceScale = mCalibration.distanceScale;
2337 } else {
2338 mLocked.distanceScale = 1.0f;
2339 }
2340 }
2341
2342 mLocked.orientedRanges.haveDistance = true;
2343
2344 mLocked.orientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
2345 mLocked.orientedRanges.distance.source = mTouchSource;
2346 mLocked.orientedRanges.distance.min =
2347 mRawAxes.distance.minValue * mLocked.distanceScale;
2348 mLocked.orientedRanges.distance.max =
2349 mRawAxes.distance.minValue * mLocked.distanceScale;
2350 mLocked.orientedRanges.distance.flat = 0;
2351 mLocked.orientedRanges.distance.fuzz =
2352 mRawAxes.distance.fuzz * mLocked.distanceScale;
2353 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002354 }
2355
2356 if (orientationChanged || sizeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002357 // Compute oriented surface dimensions, precision, scales and ranges.
2358 // Note that the maximum value reported is an inclusive maximum value so it is one
2359 // unit less than the total width or height of surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002360 switch (mLocked.surfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002361 case DISPLAY_ORIENTATION_90:
2362 case DISPLAY_ORIENTATION_270:
Jeff Brown6328cdc2010-07-29 18:18:33 -07002363 mLocked.orientedSurfaceWidth = mLocked.surfaceHeight;
2364 mLocked.orientedSurfaceHeight = mLocked.surfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002365
Jeff Brown6328cdc2010-07-29 18:18:33 -07002366 mLocked.orientedXPrecision = mLocked.yPrecision;
2367 mLocked.orientedYPrecision = mLocked.xPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002368
2369 mLocked.orientedRanges.x.min = 0;
2370 mLocked.orientedRanges.x.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue)
2371 * mLocked.yScale;
2372 mLocked.orientedRanges.x.flat = 0;
2373 mLocked.orientedRanges.x.fuzz = mLocked.yScale;
2374
2375 mLocked.orientedRanges.y.min = 0;
2376 mLocked.orientedRanges.y.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue)
2377 * mLocked.xScale;
2378 mLocked.orientedRanges.y.flat = 0;
2379 mLocked.orientedRanges.y.fuzz = mLocked.xScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002380 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002381
Jeff Brown6d0fec22010-07-23 21:28:06 -07002382 default:
Jeff Brown6328cdc2010-07-29 18:18:33 -07002383 mLocked.orientedSurfaceWidth = mLocked.surfaceWidth;
2384 mLocked.orientedSurfaceHeight = mLocked.surfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002385
Jeff Brown6328cdc2010-07-29 18:18:33 -07002386 mLocked.orientedXPrecision = mLocked.xPrecision;
2387 mLocked.orientedYPrecision = mLocked.yPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002388
2389 mLocked.orientedRanges.x.min = 0;
2390 mLocked.orientedRanges.x.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue)
2391 * mLocked.xScale;
2392 mLocked.orientedRanges.x.flat = 0;
2393 mLocked.orientedRanges.x.fuzz = mLocked.xScale;
2394
2395 mLocked.orientedRanges.y.min = 0;
2396 mLocked.orientedRanges.y.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue)
2397 * mLocked.yScale;
2398 mLocked.orientedRanges.y.flat = 0;
2399 mLocked.orientedRanges.y.fuzz = mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002400 break;
2401 }
Jeff Brownace13b12011-03-09 17:39:48 -08002402
2403 // Compute pointer gesture detection parameters.
2404 // TODO: These factors should not be hardcoded.
2405 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2406 int32_t rawWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2407 int32_t rawHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07002408 float rawDiagonal = hypotf(rawWidth, rawHeight);
2409 float displayDiagonal = hypotf(mLocked.associatedDisplayWidth,
2410 mLocked.associatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08002411
Jeff Brown2352b972011-04-12 22:39:53 -07002412 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d42011-06-01 12:33:19 -07002413 // given area relative to the diagonal size of the display when no acceleration
2414 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08002415 // Assume that the touch pad has a square aspect ratio such that movements in
2416 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown214eaf42011-05-26 19:17:02 -07002417 mLocked.pointerGestureXMovementScale = mConfig->pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002418 * displayDiagonal / rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08002419 mLocked.pointerGestureYMovementScale = mLocked.pointerGestureXMovementScale;
2420
2421 // Scale zooms to cover a smaller range of the display than movements do.
2422 // This value determines the area around the pointer that is affected by freeform
2423 // pointer gestures.
Jeff Brown214eaf42011-05-26 19:17:02 -07002424 mLocked.pointerGestureXZoomScale = mConfig->pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002425 * displayDiagonal / rawDiagonal;
2426 mLocked.pointerGestureYZoomScale = mLocked.pointerGestureXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002427
Jeff Brown2352b972011-04-12 22:39:53 -07002428 // Max width between pointers to detect a swipe gesture is more than some fraction
2429 // of the diagonal axis of the touch pad. Touches that are wider than this are
2430 // translated into freeform gestures.
Jeff Brown214eaf42011-05-26 19:17:02 -07002431 mLocked.pointerGestureMaxSwipeWidth =
2432 mConfig->pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brown2352b972011-04-12 22:39:53 -07002433
2434 // Reset the current pointer gesture.
2435 mPointerGesture.reset();
2436
2437 // Remove any current spots.
2438 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
2439 mPointerController->clearSpots();
2440 }
Jeff Brownace13b12011-03-09 17:39:48 -08002441 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002442 }
2443
2444 return true;
2445}
2446
Jeff Brownef3d7e82010-09-30 14:33:04 -07002447void TouchInputMapper::dumpSurfaceLocked(String8& dump) {
2448 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth);
2449 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight);
2450 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07002451}
2452
Jeff Brown6328cdc2010-07-29 18:18:33 -07002453void TouchInputMapper::configureVirtualKeysLocked() {
Jeff Brown8d608662010-08-30 03:02:23 -07002454 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08002455 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002456
Jeff Brown6328cdc2010-07-29 18:18:33 -07002457 mLocked.virtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002458
Jeff Brown6328cdc2010-07-29 18:18:33 -07002459 if (virtualKeyDefinitions.size() == 0) {
2460 return;
2461 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002462
Jeff Brown6328cdc2010-07-29 18:18:33 -07002463 mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size());
2464
Jeff Brown8d608662010-08-30 03:02:23 -07002465 int32_t touchScreenLeft = mRawAxes.x.minValue;
2466 int32_t touchScreenTop = mRawAxes.y.minValue;
Jeff Brown9626b142011-03-03 02:09:54 -08002467 int32_t touchScreenWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2468 int32_t touchScreenHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002469
2470 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07002471 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07002472 virtualKeyDefinitions[i];
2473
2474 mLocked.virtualKeys.add();
2475 VirtualKey& virtualKey = mLocked.virtualKeys.editTop();
2476
2477 virtualKey.scanCode = virtualKeyDefinition.scanCode;
2478 int32_t keyCode;
2479 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08002480 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07002481 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002482 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
2483 virtualKey.scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002484 mLocked.virtualKeys.pop(); // drop the key
2485 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002486 }
2487
Jeff Brown6328cdc2010-07-29 18:18:33 -07002488 virtualKey.keyCode = keyCode;
2489 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002490
Jeff Brown6328cdc2010-07-29 18:18:33 -07002491 // convert the key definition's display coordinates into touch coordinates for a hit box
2492 int32_t halfWidth = virtualKeyDefinition.width / 2;
2493 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002494
Jeff Brown6328cdc2010-07-29 18:18:33 -07002495 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
2496 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
2497 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
2498 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
2499 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
2500 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
2501 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
2502 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07002503 }
2504}
2505
2506void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) {
2507 if (!mLocked.virtualKeys.isEmpty()) {
2508 dump.append(INDENT3 "Virtual Keys:\n");
2509
2510 for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) {
2511 const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i);
2512 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
2513 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
2514 i, virtualKey.scanCode, virtualKey.keyCode,
2515 virtualKey.hitLeft, virtualKey.hitRight,
2516 virtualKey.hitTop, virtualKey.hitBottom);
2517 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002518 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002519}
2520
Jeff Brown8d608662010-08-30 03:02:23 -07002521void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002522 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07002523 Calibration& out = mCalibration;
2524
Jeff Brownc6d282b2010-10-14 21:42:15 -07002525 // Touch Size
2526 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT;
2527 String8 touchSizeCalibrationString;
2528 if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) {
2529 if (touchSizeCalibrationString == "none") {
2530 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
2531 } else if (touchSizeCalibrationString == "geometric") {
2532 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC;
2533 } else if (touchSizeCalibrationString == "pressure") {
2534 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
2535 } else if (touchSizeCalibrationString != "default") {
2536 LOGW("Invalid value for touch.touchSize.calibration: '%s'",
2537 touchSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002538 }
2539 }
2540
Jeff Brownc6d282b2010-10-14 21:42:15 -07002541 // Tool Size
2542 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT;
2543 String8 toolSizeCalibrationString;
2544 if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) {
2545 if (toolSizeCalibrationString == "none") {
2546 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
2547 } else if (toolSizeCalibrationString == "geometric") {
2548 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC;
2549 } else if (toolSizeCalibrationString == "linear") {
2550 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
2551 } else if (toolSizeCalibrationString == "area") {
2552 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA;
2553 } else if (toolSizeCalibrationString != "default") {
2554 LOGW("Invalid value for touch.toolSize.calibration: '%s'",
2555 toolSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002556 }
2557 }
2558
Jeff Brownc6d282b2010-10-14 21:42:15 -07002559 out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"),
2560 out.toolSizeLinearScale);
2561 out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"),
2562 out.toolSizeLinearBias);
2563 out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"),
2564 out.toolSizeAreaScale);
2565 out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"),
2566 out.toolSizeAreaBias);
2567 out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"),
2568 out.toolSizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07002569
2570 // Pressure
2571 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
2572 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002573 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002574 if (pressureCalibrationString == "none") {
2575 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2576 } else if (pressureCalibrationString == "physical") {
2577 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
2578 } else if (pressureCalibrationString == "amplitude") {
2579 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2580 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002581 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002582 pressureCalibrationString.string());
2583 }
2584 }
2585
2586 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
2587 String8 pressureSourceString;
2588 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
2589 if (pressureSourceString == "pressure") {
2590 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2591 } else if (pressureSourceString == "touch") {
2592 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2593 } else if (pressureSourceString != "default") {
2594 LOGW("Invalid value for touch.pressure.source: '%s'",
2595 pressureSourceString.string());
2596 }
2597 }
2598
2599 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
2600 out.pressureScale);
2601
2602 // Size
2603 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
2604 String8 sizeCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002605 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002606 if (sizeCalibrationString == "none") {
2607 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2608 } else if (sizeCalibrationString == "normalized") {
2609 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
2610 } else if (sizeCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002611 LOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002612 sizeCalibrationString.string());
2613 }
2614 }
2615
2616 // Orientation
2617 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
2618 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002619 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002620 if (orientationCalibrationString == "none") {
2621 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2622 } else if (orientationCalibrationString == "interpolated") {
2623 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002624 } else if (orientationCalibrationString == "vector") {
2625 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002626 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002627 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002628 orientationCalibrationString.string());
2629 }
2630 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002631
2632 // Distance
2633 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
2634 String8 distanceCalibrationString;
2635 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
2636 if (distanceCalibrationString == "none") {
2637 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
2638 } else if (distanceCalibrationString == "scaled") {
2639 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
2640 } else if (distanceCalibrationString != "default") {
2641 LOGW("Invalid value for touch.distance.calibration: '%s'",
2642 distanceCalibrationString.string());
2643 }
2644 }
2645
2646 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
2647 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002648}
2649
2650void TouchInputMapper::resolveCalibration() {
2651 // Pressure
2652 switch (mCalibration.pressureSource) {
2653 case Calibration::PRESSURE_SOURCE_DEFAULT:
2654 if (mRawAxes.pressure.valid) {
2655 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2656 } else if (mRawAxes.touchMajor.valid) {
2657 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2658 }
2659 break;
2660
2661 case Calibration::PRESSURE_SOURCE_PRESSURE:
2662 if (! mRawAxes.pressure.valid) {
2663 LOGW("Calibration property touch.pressure.source is 'pressure' but "
2664 "the pressure axis is not available.");
2665 }
2666 break;
2667
2668 case Calibration::PRESSURE_SOURCE_TOUCH:
2669 if (! mRawAxes.touchMajor.valid) {
2670 LOGW("Calibration property touch.pressure.source is 'touch' but "
2671 "the touchMajor axis is not available.");
2672 }
2673 break;
2674
2675 default:
2676 break;
2677 }
2678
2679 switch (mCalibration.pressureCalibration) {
2680 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
2681 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
2682 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2683 } else {
2684 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2685 }
2686 break;
2687
2688 default:
2689 break;
2690 }
2691
Jeff Brownc6d282b2010-10-14 21:42:15 -07002692 // Tool Size
2693 switch (mCalibration.toolSizeCalibration) {
2694 case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07002695 if (mRawAxes.toolMajor.valid) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002696 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
Jeff Brown8d608662010-08-30 03:02:23 -07002697 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002698 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002699 }
2700 break;
2701
2702 default:
2703 break;
2704 }
2705
Jeff Brownc6d282b2010-10-14 21:42:15 -07002706 // Touch Size
2707 switch (mCalibration.touchSizeCalibration) {
2708 case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07002709 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
Jeff Brownc6d282b2010-10-14 21:42:15 -07002710 && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
2711 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
Jeff Brown8d608662010-08-30 03:02:23 -07002712 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002713 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002714 }
2715 break;
2716
2717 default:
2718 break;
2719 }
2720
2721 // Size
2722 switch (mCalibration.sizeCalibration) {
2723 case Calibration::SIZE_CALIBRATION_DEFAULT:
2724 if (mRawAxes.toolMajor.valid) {
2725 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
2726 } else {
2727 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2728 }
2729 break;
2730
2731 default:
2732 break;
2733 }
2734
2735 // Orientation
2736 switch (mCalibration.orientationCalibration) {
2737 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
2738 if (mRawAxes.orientation.valid) {
2739 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
2740 } else {
2741 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2742 }
2743 break;
2744
2745 default:
2746 break;
2747 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002748
2749 // Distance
2750 switch (mCalibration.distanceCalibration) {
2751 case Calibration::DISTANCE_CALIBRATION_DEFAULT:
2752 if (mRawAxes.distance.valid) {
2753 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
2754 } else {
2755 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
2756 }
2757 break;
2758
2759 default:
2760 break;
2761 }
Jeff Brown8d608662010-08-30 03:02:23 -07002762}
2763
Jeff Brownef3d7e82010-09-30 14:33:04 -07002764void TouchInputMapper::dumpCalibration(String8& dump) {
2765 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07002766
Jeff Brownc6d282b2010-10-14 21:42:15 -07002767 // Touch Size
2768 switch (mCalibration.touchSizeCalibration) {
2769 case Calibration::TOUCH_SIZE_CALIBRATION_NONE:
2770 dump.append(INDENT4 "touch.touchSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002771 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002772 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
2773 dump.append(INDENT4 "touch.touchSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002774 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002775 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
2776 dump.append(INDENT4 "touch.touchSize.calibration: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002777 break;
2778 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002779 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002780 }
2781
Jeff Brownc6d282b2010-10-14 21:42:15 -07002782 // Tool Size
2783 switch (mCalibration.toolSizeCalibration) {
2784 case Calibration::TOOL_SIZE_CALIBRATION_NONE:
2785 dump.append(INDENT4 "touch.toolSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002786 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002787 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
2788 dump.append(INDENT4 "touch.toolSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002789 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002790 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
2791 dump.append(INDENT4 "touch.toolSize.calibration: linear\n");
2792 break;
2793 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2794 dump.append(INDENT4 "touch.toolSize.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002795 break;
2796 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002797 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002798 }
2799
Jeff Brownc6d282b2010-10-14 21:42:15 -07002800 if (mCalibration.haveToolSizeLinearScale) {
2801 dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n",
2802 mCalibration.toolSizeLinearScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002803 }
2804
Jeff Brownc6d282b2010-10-14 21:42:15 -07002805 if (mCalibration.haveToolSizeLinearBias) {
2806 dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n",
2807 mCalibration.toolSizeLinearBias);
Jeff Brown8d608662010-08-30 03:02:23 -07002808 }
2809
Jeff Brownc6d282b2010-10-14 21:42:15 -07002810 if (mCalibration.haveToolSizeAreaScale) {
2811 dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n",
2812 mCalibration.toolSizeAreaScale);
2813 }
2814
2815 if (mCalibration.haveToolSizeAreaBias) {
2816 dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n",
2817 mCalibration.toolSizeAreaBias);
2818 }
2819
2820 if (mCalibration.haveToolSizeIsSummed) {
Jeff Brown1f245102010-11-18 20:53:46 -08002821 dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %s\n",
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002822 toString(mCalibration.toolSizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07002823 }
2824
2825 // Pressure
2826 switch (mCalibration.pressureCalibration) {
2827 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002828 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002829 break;
2830 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002831 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002832 break;
2833 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002834 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002835 break;
2836 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002837 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002838 }
2839
2840 switch (mCalibration.pressureSource) {
2841 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002842 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002843 break;
2844 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002845 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002846 break;
2847 case Calibration::PRESSURE_SOURCE_DEFAULT:
2848 break;
2849 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002850 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002851 }
2852
2853 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07002854 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
2855 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002856 }
2857
2858 // Size
2859 switch (mCalibration.sizeCalibration) {
2860 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002861 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002862 break;
2863 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002864 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002865 break;
2866 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002867 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002868 }
2869
2870 // Orientation
2871 switch (mCalibration.orientationCalibration) {
2872 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002873 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002874 break;
2875 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002876 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002877 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002878 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
2879 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
2880 break;
Jeff Brown8d608662010-08-30 03:02:23 -07002881 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002882 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002883 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002884
2885 // Distance
2886 switch (mCalibration.distanceCalibration) {
2887 case Calibration::DISTANCE_CALIBRATION_NONE:
2888 dump.append(INDENT4 "touch.distance.calibration: none\n");
2889 break;
2890 case Calibration::DISTANCE_CALIBRATION_SCALED:
2891 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
2892 break;
2893 default:
2894 LOG_ASSERT(false);
2895 }
2896
2897 if (mCalibration.haveDistanceScale) {
2898 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
2899 mCalibration.distanceScale);
2900 }
Jeff Brown8d608662010-08-30 03:02:23 -07002901}
2902
Jeff Brown6d0fec22010-07-23 21:28:06 -07002903void TouchInputMapper::reset() {
2904 // Synthesize touch up event if touch is currently down.
2905 // This will also take care of finishing virtual key processing if needed.
2906 if (mLastTouch.pointerCount != 0) {
2907 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
2908 mCurrentTouch.clear();
2909 syncTouch(when, true);
2910 }
2911
Jeff Brown6328cdc2010-07-29 18:18:33 -07002912 { // acquire lock
2913 AutoMutex _l(mLock);
2914 initializeLocked();
Jeff Brown2352b972011-04-12 22:39:53 -07002915
2916 if (mPointerController != NULL
2917 && mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
2918 mPointerController->clearSpots();
2919 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002920 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07002921
Jeff Brown6328cdc2010-07-29 18:18:33 -07002922 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002923}
2924
2925void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Brownaa3855d2011-03-17 01:34:19 -07002926#if DEBUG_RAW_EVENTS
2927 if (!havePointerIds) {
2928 LOGD("syncTouch: pointerCount=%d, no pointer ids", mCurrentTouch.pointerCount);
2929 } else {
2930 LOGD("syncTouch: pointerCount=%d, up=0x%08x, down=0x%08x, move=0x%08x, "
2931 "last=0x%08x, current=0x%08x", mCurrentTouch.pointerCount,
2932 mLastTouch.idBits.value & ~mCurrentTouch.idBits.value,
2933 mCurrentTouch.idBits.value & ~mLastTouch.idBits.value,
2934 mLastTouch.idBits.value & mCurrentTouch.idBits.value,
2935 mLastTouch.idBits.value, mCurrentTouch.idBits.value);
2936 }
2937#endif
2938
Jeff Brown6328cdc2010-07-29 18:18:33 -07002939 // Preprocess pointer data.
Jeff Brown6d0fec22010-07-23 21:28:06 -07002940 if (mParameters.useBadTouchFilter) {
2941 if (applyBadTouchFilter()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002942 havePointerIds = false;
2943 }
2944 }
2945
Jeff Brown6d0fec22010-07-23 21:28:06 -07002946 if (mParameters.useJumpyTouchFilter) {
2947 if (applyJumpyTouchFilter()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002948 havePointerIds = false;
2949 }
2950 }
2951
Jeff Brownaa3855d2011-03-17 01:34:19 -07002952 if (!havePointerIds) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002953 calculatePointerIds();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002954 }
2955
Jeff Brown6d0fec22010-07-23 21:28:06 -07002956 TouchData temp;
2957 TouchData* savedTouch;
2958 if (mParameters.useAveragingTouchFilter) {
2959 temp.copyFrom(mCurrentTouch);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002960 savedTouch = & temp;
2961
Jeff Brown6d0fec22010-07-23 21:28:06 -07002962 applyAveragingTouchFilter();
Jeff Brown46b9ac02010-04-22 18:58:52 -07002963 } else {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002964 savedTouch = & mCurrentTouch;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002965 }
2966
Jeff Brown56194eb2011-03-02 19:23:13 -08002967 uint32_t policyFlags = 0;
Jeff Brown05dc66a2011-03-02 14:41:58 -08002968 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) {
Jeff Brownefd32662011-03-08 15:13:06 -08002969 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
2970 // If this is a touch screen, hide the pointer on an initial down.
2971 getContext()->fadePointer();
2972 }
Jeff Brown56194eb2011-03-02 19:23:13 -08002973
2974 // Initial downs on external touch devices should wake the device.
2975 // We don't do this for internal touch screens to prevent them from waking
2976 // up in your pocket.
2977 // TODO: Use the input device configuration to control this behavior more finely.
2978 if (getDevice()->isExternal()) {
2979 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2980 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002981 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002982
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002983 // Synthesize key down from buttons if needed.
2984 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mTouchSource,
2985 policyFlags, mLastTouch.buttonState, mCurrentTouch.buttonState);
2986
2987 // Send motion events.
Jeff Brown79ac9692011-04-19 21:20:10 -07002988 TouchResult touchResult;
2989 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount == 0
2990 && mLastTouch.buttonState == mCurrentTouch.buttonState) {
2991 // Drop spurious syncs.
2992 touchResult = DROP_STROKE;
2993 } else {
2994 // Process touches and virtual keys.
2995 touchResult = consumeOffScreenTouches(when, policyFlags);
2996 if (touchResult == DISPATCH_TOUCH) {
2997 suppressSwipeOntoVirtualKeys(when);
2998 if (mPointerController != NULL) {
2999 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
3000 }
3001 dispatchTouches(when, policyFlags);
Jeff Brownace13b12011-03-09 17:39:48 -08003002 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003003 }
3004
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003005 // Synthesize key up from buttons if needed.
3006 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mTouchSource,
3007 policyFlags, mLastTouch.buttonState, mCurrentTouch.buttonState);
3008
Jeff Brown6328cdc2010-07-29 18:18:33 -07003009 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownace13b12011-03-09 17:39:48 -08003010 // Keep the button state so we can track edge-triggered button state changes.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003011 if (touchResult == DROP_STROKE) {
3012 mLastTouch.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08003013 mLastTouch.buttonState = savedTouch->buttonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003014 } else {
3015 mLastTouch.copyFrom(*savedTouch);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003016 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003017}
3018
Jeff Brown79ac9692011-04-19 21:20:10 -07003019void TouchInputMapper::timeoutExpired(nsecs_t when) {
3020 if (mPointerController != NULL) {
3021 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3022 }
3023}
3024
Jeff Brown6d0fec22010-07-23 21:28:06 -07003025TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches(
3026 nsecs_t when, uint32_t policyFlags) {
3027 int32_t keyEventAction, keyEventFlags;
3028 int32_t keyCode, scanCode, downTime;
3029 TouchResult touchResult;
Jeff Brown349703e2010-06-22 01:27:15 -07003030
Jeff Brown6328cdc2010-07-29 18:18:33 -07003031 { // acquire lock
3032 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003033
Jeff Brown6328cdc2010-07-29 18:18:33 -07003034 // Update surface size and orientation, including virtual key positions.
3035 if (! configureSurfaceLocked()) {
3036 return DROP_STROKE;
3037 }
3038
3039 // Check for virtual key press.
3040 if (mLocked.currentVirtualKey.down) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003041 if (mCurrentTouch.pointerCount == 0) {
3042 // Pointer went up while virtual key was down.
Jeff Brown6328cdc2010-07-29 18:18:33 -07003043 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003044#if DEBUG_VIRTUAL_KEYS
3045 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07003046 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003047#endif
3048 keyEventAction = AKEY_EVENT_ACTION_UP;
3049 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
3050 touchResult = SKIP_TOUCH;
3051 goto DispatchVirtualKey;
3052 }
3053
3054 if (mCurrentTouch.pointerCount == 1) {
3055 int32_t x = mCurrentTouch.pointers[0].x;
3056 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003057 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
3058 if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003059 // Pointer is still within the space of the virtual key.
3060 return SKIP_TOUCH;
3061 }
3062 }
3063
3064 // Pointer left virtual key area or another pointer also went down.
3065 // Send key cancellation and drop the stroke so subsequent motions will be
3066 // considered fresh downs. This is useful when the user swipes away from the
3067 // virtual key area into the main display surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07003068 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003069#if DEBUG_VIRTUAL_KEYS
3070 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07003071 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003072#endif
3073 keyEventAction = AKEY_EVENT_ACTION_UP;
3074 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3075 | AKEY_EVENT_FLAG_CANCELED;
Jeff Brownc3db8582010-10-20 15:33:38 -07003076
3077 // Check whether the pointer moved inside the display area where we should
3078 // start a new stroke.
3079 int32_t x = mCurrentTouch.pointers[0].x;
3080 int32_t y = mCurrentTouch.pointers[0].y;
3081 if (isPointInsideSurfaceLocked(x, y)) {
3082 mLastTouch.clear();
3083 touchResult = DISPATCH_TOUCH;
3084 } else {
3085 touchResult = DROP_STROKE;
3086 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003087 } else {
3088 if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) {
3089 // Pointer just went down. Handle off-screen touches, if needed.
3090 int32_t x = mCurrentTouch.pointers[0].x;
3091 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003092 if (! isPointInsideSurfaceLocked(x, y)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003093 // If exactly one pointer went down, check for virtual key hit.
3094 // Otherwise we will drop the entire stroke.
3095 if (mCurrentTouch.pointerCount == 1) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003096 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003097 if (virtualKey) {
Jeff Brownfe508922011-01-18 15:10:10 -08003098 if (mContext->shouldDropVirtualKey(when, getDevice(),
3099 virtualKey->keyCode, virtualKey->scanCode)) {
3100 return DROP_STROKE;
3101 }
3102
Jeff Brown6328cdc2010-07-29 18:18:33 -07003103 mLocked.currentVirtualKey.down = true;
3104 mLocked.currentVirtualKey.downTime = when;
3105 mLocked.currentVirtualKey.keyCode = virtualKey->keyCode;
3106 mLocked.currentVirtualKey.scanCode = virtualKey->scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003107#if DEBUG_VIRTUAL_KEYS
3108 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07003109 mLocked.currentVirtualKey.keyCode,
3110 mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003111#endif
3112 keyEventAction = AKEY_EVENT_ACTION_DOWN;
3113 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM
3114 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
3115 touchResult = SKIP_TOUCH;
3116 goto DispatchVirtualKey;
3117 }
3118 }
3119 return DROP_STROKE;
3120 }
3121 }
3122 return DISPATCH_TOUCH;
3123 }
3124
3125 DispatchVirtualKey:
3126 // Collect remaining state needed to dispatch virtual key.
Jeff Brown6328cdc2010-07-29 18:18:33 -07003127 keyCode = mLocked.currentVirtualKey.keyCode;
3128 scanCode = mLocked.currentVirtualKey.scanCode;
3129 downTime = mLocked.currentVirtualKey.downTime;
3130 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07003131
3132 // Dispatch virtual key.
3133 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown0eaf3932010-10-01 14:55:30 -07003134 policyFlags |= POLICY_FLAG_VIRTUAL;
Jeff Brownb6997262010-10-08 22:31:17 -07003135 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3136 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3137 return touchResult;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003138}
3139
Jeff Brownefd32662011-03-08 15:13:06 -08003140void TouchInputMapper::suppressSwipeOntoVirtualKeys(nsecs_t when) {
Jeff Brownfe508922011-01-18 15:10:10 -08003141 // Disable all virtual key touches that happen within a short time interval of the
3142 // most recent touch. The idea is to filter out stray virtual key presses when
3143 // interacting with the touch screen.
3144 //
3145 // Problems we're trying to solve:
3146 //
3147 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3148 // virtual key area that is implemented by a separate touch panel and accidentally
3149 // triggers a virtual key.
3150 //
3151 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3152 // area and accidentally triggers a virtual key. This often happens when virtual keys
3153 // are layed out below the screen near to where the on screen keyboard's space bar
3154 // is displayed.
Jeff Brown214eaf42011-05-26 19:17:02 -07003155 if (mConfig->virtualKeyQuietTime > 0 && mCurrentTouch.pointerCount != 0) {
3156 mContext->disableVirtualKeysUntil(when + mConfig->virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003157 }
3158}
3159
Jeff Brown6d0fec22010-07-23 21:28:06 -07003160void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
3161 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
3162 uint32_t lastPointerCount = mLastTouch.pointerCount;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003163 if (currentPointerCount == 0 && lastPointerCount == 0) {
3164 return; // nothing to do!
3165 }
3166
Jeff Brownace13b12011-03-09 17:39:48 -08003167 // Update current touch coordinates.
3168 int32_t edgeFlags;
3169 float xPrecision, yPrecision;
3170 prepareTouches(&edgeFlags, &xPrecision, &yPrecision);
3171
3172 // Dispatch motions.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003173 BitSet32 currentIdBits = mCurrentTouch.idBits;
3174 BitSet32 lastIdBits = mLastTouch.idBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003175 int32_t metaState = getContext()->getGlobalMetaState();
3176 int32_t buttonState = mCurrentTouch.buttonState;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003177
3178 if (currentIdBits == lastIdBits) {
3179 // No pointer id changes so this is a move event.
3180 // The dispatcher takes care of batching moves so we don't have to deal with that here.
Jeff Brownace13b12011-03-09 17:39:48 -08003181 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003182 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3183 AMOTION_EVENT_EDGE_FLAG_NONE,
3184 mCurrentTouchProperties, mCurrentTouchCoords,
3185 mCurrentTouch.idToIndex, currentIdBits, -1,
Jeff Brownace13b12011-03-09 17:39:48 -08003186 xPrecision, yPrecision, mDownTime);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003187 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003188 // There may be pointers going up and pointers going down and pointers moving
3189 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003190 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3191 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003192 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003193 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003194
Jeff Brownace13b12011-03-09 17:39:48 -08003195 // Update last coordinates of pointers that have moved so that we observe the new
3196 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003197 bool moveNeeded = updateMovedPointers(
3198 mCurrentTouchProperties, mCurrentTouchCoords, mCurrentTouch.idToIndex,
3199 mLastTouchProperties, mLastTouchCoords, mLastTouch.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003200 moveIdBits);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003201 if (buttonState != mLastTouch.buttonState) {
3202 moveNeeded = true;
3203 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003204
Jeff Brownace13b12011-03-09 17:39:48 -08003205 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003206 while (!upIdBits.isEmpty()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003207 uint32_t upId = upIdBits.firstMarkedBit();
3208 upIdBits.clearBit(upId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003209
Jeff Brownace13b12011-03-09 17:39:48 -08003210 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003211 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
3212 mLastTouchProperties, mLastTouchCoords,
3213 mLastTouch.idToIndex, dispatchedIdBits, upId,
Jeff Brownace13b12011-03-09 17:39:48 -08003214 xPrecision, yPrecision, mDownTime);
3215 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003216 }
3217
Jeff Brownc3db8582010-10-20 15:33:38 -07003218 // Dispatch move events if any of the remaining pointers moved from their old locations.
3219 // Although applications receive new locations as part of individual pointer up
3220 // events, they do not generally handle them except when presented in a move event.
3221 if (moveNeeded) {
Jeff Brownb6110c22011-04-01 16:15:13 -07003222 LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003223 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003224 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
3225 mCurrentTouchProperties, mCurrentTouchCoords,
3226 mCurrentTouch.idToIndex, dispatchedIdBits, -1,
Jeff Brownace13b12011-03-09 17:39:48 -08003227 xPrecision, yPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003228 }
3229
3230 // Dispatch pointer down events using the new pointer locations.
3231 while (!downIdBits.isEmpty()) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003232 uint32_t downId = downIdBits.firstMarkedBit();
3233 downIdBits.clearBit(downId);
Jeff Brownace13b12011-03-09 17:39:48 -08003234 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003235
Jeff Brownace13b12011-03-09 17:39:48 -08003236 if (dispatchedIdBits.count() == 1) {
3237 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003238 mDownTime = when;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003239 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08003240 // Only send edge flags with first pointer down.
3241 edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003242 }
3243
Jeff Brownace13b12011-03-09 17:39:48 -08003244 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003245 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, edgeFlags,
3246 mCurrentTouchProperties, mCurrentTouchCoords,
3247 mCurrentTouch.idToIndex, dispatchedIdBits, downId,
Jeff Brownace13b12011-03-09 17:39:48 -08003248 xPrecision, yPrecision, mDownTime);
3249 }
3250 }
3251
3252 // Update state for next time.
3253 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003254 mLastTouchProperties[i].copyFrom(mCurrentTouchProperties[i]);
Jeff Brownace13b12011-03-09 17:39:48 -08003255 mLastTouchCoords[i].copyFrom(mCurrentTouchCoords[i]);
3256 }
3257}
3258
3259void TouchInputMapper::prepareTouches(int32_t* outEdgeFlags,
3260 float* outXPrecision, float* outYPrecision) {
3261 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
3262 uint32_t lastPointerCount = mLastTouch.pointerCount;
3263
3264 AutoMutex _l(mLock);
3265
3266 // Walk through the the active pointers and map touch screen coordinates (TouchData) into
3267 // display or surface coordinates (PointerCoords) and adjust for display orientation.
3268 for (uint32_t i = 0; i < currentPointerCount; i++) {
3269 const PointerData& in = mCurrentTouch.pointers[i];
3270
3271 // ToolMajor and ToolMinor
3272 float toolMajor, toolMinor;
3273 switch (mCalibration.toolSizeCalibration) {
3274 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
3275 toolMajor = in.toolMajor * mLocked.geometricScale;
3276 if (mRawAxes.toolMinor.valid) {
3277 toolMinor = in.toolMinor * mLocked.geometricScale;
3278 } else {
3279 toolMinor = toolMajor;
3280 }
3281 break;
3282 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
3283 toolMajor = in.toolMajor != 0
3284 ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias
3285 : 0;
3286 if (mRawAxes.toolMinor.valid) {
3287 toolMinor = in.toolMinor != 0
3288 ? in.toolMinor * mLocked.toolSizeLinearScale
3289 + mLocked.toolSizeLinearBias
3290 : 0;
3291 } else {
3292 toolMinor = toolMajor;
3293 }
3294 break;
3295 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
3296 if (in.toolMajor != 0) {
3297 float diameter = sqrtf(in.toolMajor
3298 * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias);
3299 toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias;
3300 } else {
3301 toolMajor = 0;
3302 }
3303 toolMinor = toolMajor;
3304 break;
3305 default:
3306 toolMajor = 0;
3307 toolMinor = 0;
3308 break;
3309 }
3310
3311 if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) {
3312 toolMajor /= currentPointerCount;
3313 toolMinor /= currentPointerCount;
3314 }
3315
3316 // Pressure
3317 float rawPressure;
3318 switch (mCalibration.pressureSource) {
3319 case Calibration::PRESSURE_SOURCE_PRESSURE:
3320 rawPressure = in.pressure;
3321 break;
3322 case Calibration::PRESSURE_SOURCE_TOUCH:
3323 rawPressure = in.touchMajor;
3324 break;
3325 default:
3326 rawPressure = 0;
3327 }
3328
3329 float pressure;
3330 switch (mCalibration.pressureCalibration) {
3331 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3332 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
3333 pressure = rawPressure * mLocked.pressureScale;
3334 break;
3335 default:
3336 pressure = 1;
3337 break;
3338 }
3339
3340 // TouchMajor and TouchMinor
3341 float touchMajor, touchMinor;
3342 switch (mCalibration.touchSizeCalibration) {
3343 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
3344 touchMajor = in.touchMajor * mLocked.geometricScale;
3345 if (mRawAxes.touchMinor.valid) {
3346 touchMinor = in.touchMinor * mLocked.geometricScale;
3347 } else {
3348 touchMinor = touchMajor;
3349 }
3350 break;
3351 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
3352 touchMajor = toolMajor * pressure;
3353 touchMinor = toolMinor * pressure;
3354 break;
3355 default:
3356 touchMajor = 0;
3357 touchMinor = 0;
3358 break;
3359 }
3360
3361 if (touchMajor > toolMajor) {
3362 touchMajor = toolMajor;
3363 }
3364 if (touchMinor > toolMinor) {
3365 touchMinor = toolMinor;
3366 }
3367
3368 // Size
3369 float size;
3370 switch (mCalibration.sizeCalibration) {
3371 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
3372 float rawSize = mRawAxes.toolMinor.valid
3373 ? avg(in.toolMajor, in.toolMinor)
3374 : in.toolMajor;
3375 size = rawSize * mLocked.sizeScale;
3376 break;
3377 }
3378 default:
3379 size = 0;
3380 break;
3381 }
3382
3383 // Orientation
3384 float orientation;
3385 switch (mCalibration.orientationCalibration) {
3386 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
3387 orientation = in.orientation * mLocked.orientationScale;
3388 break;
3389 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3390 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3391 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3392 if (c1 != 0 || c2 != 0) {
3393 orientation = atan2f(c1, c2) * 0.5f;
Jeff Brown2352b972011-04-12 22:39:53 -07003394 float scale = 1.0f + hypotf(c1, c2) / 16.0f;
Jeff Brownace13b12011-03-09 17:39:48 -08003395 touchMajor *= scale;
3396 touchMinor /= scale;
3397 toolMajor *= scale;
3398 toolMinor /= scale;
3399 } else {
3400 orientation = 0;
3401 }
3402 break;
3403 }
3404 default:
3405 orientation = 0;
3406 }
3407
Jeff Brown80fd47c2011-05-24 01:07:44 -07003408 // Distance
3409 float distance;
3410 switch (mCalibration.distanceCalibration) {
3411 case Calibration::DISTANCE_CALIBRATION_SCALED:
3412 distance = in.distance * mLocked.distanceScale;
3413 break;
3414 default:
3415 distance = 0;
3416 }
3417
Jeff Brownace13b12011-03-09 17:39:48 -08003418 // X and Y
3419 // Adjust coords for surface orientation.
3420 float x, y;
3421 switch (mLocked.surfaceOrientation) {
3422 case DISPLAY_ORIENTATION_90:
3423 x = float(in.y - mRawAxes.y.minValue) * mLocked.yScale;
3424 y = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale;
3425 orientation -= M_PI_2;
3426 if (orientation < - M_PI_2) {
3427 orientation += M_PI;
3428 }
3429 break;
3430 case DISPLAY_ORIENTATION_180:
3431 x = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale;
3432 y = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale;
3433 break;
3434 case DISPLAY_ORIENTATION_270:
3435 x = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale;
3436 y = float(in.x - mRawAxes.x.minValue) * mLocked.xScale;
3437 orientation += M_PI_2;
3438 if (orientation > M_PI_2) {
3439 orientation -= M_PI;
3440 }
3441 break;
3442 default:
3443 x = float(in.x - mRawAxes.x.minValue) * mLocked.xScale;
3444 y = float(in.y - mRawAxes.y.minValue) * mLocked.yScale;
3445 break;
3446 }
3447
3448 // Write output coords.
3449 PointerCoords& out = mCurrentTouchCoords[i];
3450 out.clear();
3451 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3452 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3453 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3454 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3455 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3456 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3457 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3458 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3459 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown80fd47c2011-05-24 01:07:44 -07003460 if (distance != 0) {
3461 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
3462 }
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003463
3464 // Write output properties.
3465 PointerProperties& properties = mCurrentTouchProperties[i];
3466 properties.clear();
3467 properties.id = mCurrentTouch.pointers[i].id;
3468 properties.toolType = getTouchToolType(mCurrentTouch.pointers[i].isStylus);
Jeff Brownace13b12011-03-09 17:39:48 -08003469 }
3470
3471 // Check edge flags by looking only at the first pointer since the flags are
3472 // global to the event.
3473 *outEdgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
3474 if (lastPointerCount == 0 && currentPointerCount > 0) {
3475 const PointerData& in = mCurrentTouch.pointers[0];
3476
3477 if (in.x <= mRawAxes.x.minValue) {
3478 *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_LEFT,
3479 mLocked.surfaceOrientation);
3480 } else if (in.x >= mRawAxes.x.maxValue) {
3481 *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_RIGHT,
3482 mLocked.surfaceOrientation);
3483 }
3484 if (in.y <= mRawAxes.y.minValue) {
3485 *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_TOP,
3486 mLocked.surfaceOrientation);
3487 } else if (in.y >= mRawAxes.y.maxValue) {
3488 *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_BOTTOM,
3489 mLocked.surfaceOrientation);
3490 }
3491 }
3492
3493 *outXPrecision = mLocked.orientedXPrecision;
3494 *outYPrecision = mLocked.orientedYPrecision;
3495}
3496
Jeff Brown79ac9692011-04-19 21:20:10 -07003497void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3498 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003499 // Update current gesture coordinates.
3500 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07003501 bool sendEvents = preparePointerGestures(when,
3502 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3503 if (!sendEvents) {
3504 return;
3505 }
Jeff Brown19c97d42011-06-01 12:33:19 -07003506 if (finishPreviousGesture) {
3507 cancelPreviousGesture = false;
3508 }
Jeff Brownace13b12011-03-09 17:39:48 -08003509
Jeff Brown214eaf42011-05-26 19:17:02 -07003510 // Switch pointer presentation.
3511 mPointerController->setPresentation(
3512 mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3513 ? PointerControllerInterface::PRESENTATION_SPOT
3514 : PointerControllerInterface::PRESENTATION_POINTER);
3515
Jeff Brown538881e2011-05-25 18:23:38 -07003516 // Show or hide the pointer if needed.
3517 switch (mPointerGesture.currentGestureMode) {
3518 case PointerGesture::NEUTRAL:
3519 case PointerGesture::QUIET:
3520 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3521 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3522 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
3523 // Remind the user of where the pointer is after finishing a gesture with spots.
3524 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
3525 }
3526 break;
3527 case PointerGesture::TAP:
3528 case PointerGesture::TAP_DRAG:
3529 case PointerGesture::BUTTON_CLICK_OR_DRAG:
3530 case PointerGesture::HOVER:
3531 case PointerGesture::PRESS:
3532 // Unfade the pointer when the current gesture manipulates the
3533 // area directly under the pointer.
3534 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3535 break;
3536 case PointerGesture::SWIPE:
3537 case PointerGesture::FREEFORM:
3538 // Fade the pointer when the current gesture manipulates a different
3539 // area and there are spots to guide the user experience.
3540 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3541 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3542 } else {
3543 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3544 }
3545 break;
Jeff Brown2352b972011-04-12 22:39:53 -07003546 }
3547
Jeff Brownace13b12011-03-09 17:39:48 -08003548 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003549 int32_t metaState = getContext()->getGlobalMetaState();
3550 int32_t buttonState = mCurrentTouch.buttonState;
Jeff Brownace13b12011-03-09 17:39:48 -08003551
3552 // Update last coordinates of pointers that have moved so that we observe the new
3553 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07003554 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
3555 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
3556 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07003557 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08003558 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
3559 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
3560 bool moveNeeded = false;
3561 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07003562 && !mPointerGesture.lastGestureIdBits.isEmpty()
3563 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08003564 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
3565 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003566 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003567 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003568 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003569 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3570 movedGestureIdBits);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003571 if (buttonState != mLastTouch.buttonState) {
3572 moveNeeded = true;
3573 }
Jeff Brownace13b12011-03-09 17:39:48 -08003574 }
3575
3576 // Send motion events for all pointers that went up or were canceled.
3577 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
3578 if (!dispatchedGestureIdBits.isEmpty()) {
3579 if (cancelPreviousGesture) {
3580 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003581 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
3582 AMOTION_EVENT_EDGE_FLAG_NONE,
3583 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003584 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3585 dispatchedGestureIdBits, -1,
3586 0, 0, mPointerGesture.downTime);
3587
3588 dispatchedGestureIdBits.clear();
3589 } else {
3590 BitSet32 upGestureIdBits;
3591 if (finishPreviousGesture) {
3592 upGestureIdBits = dispatchedGestureIdBits;
3593 } else {
3594 upGestureIdBits.value = dispatchedGestureIdBits.value
3595 & ~mPointerGesture.currentGestureIdBits.value;
3596 }
3597 while (!upGestureIdBits.isEmpty()) {
3598 uint32_t id = upGestureIdBits.firstMarkedBit();
3599 upGestureIdBits.clearBit(id);
3600
3601 dispatchMotion(when, policyFlags, mPointerSource,
3602 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003603 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3604 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003605 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3606 dispatchedGestureIdBits, id,
3607 0, 0, mPointerGesture.downTime);
3608
3609 dispatchedGestureIdBits.clearBit(id);
3610 }
3611 }
3612 }
3613
3614 // Send motion events for all pointers that moved.
3615 if (moveNeeded) {
3616 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003617 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3618 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003619 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3620 dispatchedGestureIdBits, -1,
3621 0, 0, mPointerGesture.downTime);
3622 }
3623
3624 // Send motion events for all pointers that went down.
3625 if (down) {
3626 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
3627 & ~dispatchedGestureIdBits.value);
3628 while (!downGestureIdBits.isEmpty()) {
3629 uint32_t id = downGestureIdBits.firstMarkedBit();
3630 downGestureIdBits.clearBit(id);
3631 dispatchedGestureIdBits.markBit(id);
3632
3633 int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
3634 if (dispatchedGestureIdBits.count() == 1) {
3635 // First pointer is going down. Calculate edge flags and set down time.
3636 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
3637 const PointerCoords& downCoords = mPointerGesture.currentGestureCoords[index];
3638 edgeFlags = calculateEdgeFlagsUsingPointerBounds(mPointerController,
3639 downCoords.getAxisValue(AMOTION_EVENT_AXIS_X),
3640 downCoords.getAxisValue(AMOTION_EVENT_AXIS_Y));
3641 mPointerGesture.downTime = when;
3642 }
3643
3644 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003645 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, edgeFlags,
3646 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003647 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3648 dispatchedGestureIdBits, id,
3649 0, 0, mPointerGesture.downTime);
3650 }
3651 }
3652
Jeff Brownace13b12011-03-09 17:39:48 -08003653 // Send motion events for hover.
3654 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
3655 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003656 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
3657 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3658 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003659 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3660 mPointerGesture.currentGestureIdBits, -1,
3661 0, 0, mPointerGesture.downTime);
3662 }
3663
3664 // Update state.
3665 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
3666 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08003667 mPointerGesture.lastGestureIdBits.clear();
3668 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08003669 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
3670 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
3671 uint32_t id = idBits.firstMarkedBit();
3672 idBits.clearBit(id);
3673 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003674 mPointerGesture.lastGestureProperties[index].copyFrom(
3675 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08003676 mPointerGesture.lastGestureCoords[index].copyFrom(
3677 mPointerGesture.currentGestureCoords[index]);
3678 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003679 }
3680 }
3681}
3682
Jeff Brown79ac9692011-04-19 21:20:10 -07003683bool TouchInputMapper::preparePointerGestures(nsecs_t when,
3684 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003685 *outCancelPreviousGesture = false;
3686 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003687
Jeff Brownace13b12011-03-09 17:39:48 -08003688 AutoMutex _l(mLock);
Jeff Brown6328cdc2010-07-29 18:18:33 -07003689
Jeff Brown79ac9692011-04-19 21:20:10 -07003690 // Handle TAP timeout.
3691 if (isTimeout) {
3692#if DEBUG_GESTURES
3693 LOGD("Gestures: Processing timeout");
3694#endif
3695
3696 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown214eaf42011-05-26 19:17:02 -07003697 if (when <= mPointerGesture.tapUpTime + mConfig->pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003698 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07003699 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
3700 + mConfig->pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07003701 } else {
3702 // The tap is finished.
3703#if DEBUG_GESTURES
3704 LOGD("Gestures: TAP finished");
3705#endif
3706 *outFinishPreviousGesture = true;
3707
3708 mPointerGesture.activeGestureId = -1;
3709 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
3710 mPointerGesture.currentGestureIdBits.clear();
3711
Jeff Brown19c97d42011-06-01 12:33:19 -07003712 mPointerGesture.pointerVelocityControl.reset();
3713
Jeff Brown79ac9692011-04-19 21:20:10 -07003714 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3715 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_NEUTRAL;
3716 mPointerGesture.spotIdBits.clear();
3717 moveSpotsLocked();
3718 }
3719 return true;
3720 }
3721 }
3722
3723 // We did not handle this timeout.
3724 return false;
3725 }
3726
Jeff Brownace13b12011-03-09 17:39:48 -08003727 // Update the velocity tracker.
3728 {
3729 VelocityTracker::Position positions[MAX_POINTERS];
3730 uint32_t count = 0;
3731 for (BitSet32 idBits(mCurrentTouch.idBits); !idBits.isEmpty(); count++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003732 uint32_t id = idBits.firstMarkedBit();
3733 idBits.clearBit(id);
Jeff Brownace13b12011-03-09 17:39:48 -08003734 uint32_t index = mCurrentTouch.idToIndex[id];
3735 positions[count].x = mCurrentTouch.pointers[index].x
3736 * mLocked.pointerGestureXMovementScale;
3737 positions[count].y = mCurrentTouch.pointers[index].y
3738 * mLocked.pointerGestureYMovementScale;
3739 }
3740 mPointerGesture.velocityTracker.addMovement(when, mCurrentTouch.idBits, positions);
3741 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003742
Jeff Brownace13b12011-03-09 17:39:48 -08003743 // Pick a new active touch id if needed.
3744 // Choose an arbitrary pointer that just went down, if there is one.
3745 // Otherwise choose an arbitrary remaining pointer.
3746 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07003747 // We keep the same active touch id for as long as possible.
3748 bool activeTouchChanged = false;
3749 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
3750 int32_t activeTouchId = lastActiveTouchId;
3751 if (activeTouchId < 0) {
3752 if (!mCurrentTouch.idBits.isEmpty()) {
3753 activeTouchChanged = true;
3754 activeTouchId = mPointerGesture.activeTouchId = mCurrentTouch.idBits.firstMarkedBit();
3755 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08003756 }
Jeff Brown2352b972011-04-12 22:39:53 -07003757 } else if (!mCurrentTouch.idBits.hasBit(activeTouchId)) {
3758 activeTouchChanged = true;
3759 if (!mCurrentTouch.idBits.isEmpty()) {
3760 activeTouchId = mPointerGesture.activeTouchId = mCurrentTouch.idBits.firstMarkedBit();
3761 } else {
3762 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08003763 }
3764 }
3765
3766 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07003767 bool isQuietTime = false;
3768 if (activeTouchId < 0) {
3769 mPointerGesture.resetQuietTime();
3770 } else {
Jeff Brown214eaf42011-05-26 19:17:02 -07003771 isQuietTime = when < mPointerGesture.quietTime + mConfig->pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07003772 if (!isQuietTime) {
3773 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
3774 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3775 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
3776 && mCurrentTouch.pointerCount < 2) {
3777 // Enter quiet time when exiting swipe or freeform state.
3778 // This is to prevent accidentally entering the hover state and flinging the
3779 // pointer when finishing a swipe and there is still one pointer left onscreen.
3780 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07003781 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07003782 && mCurrentTouch.pointerCount >= 2
3783 && !isPointerDown(mCurrentTouch.buttonState)) {
3784 // Enter quiet time when releasing the button and there are still two or more
3785 // fingers down. This may indicate that one finger was used to press the button
3786 // but it has not gone up yet.
3787 isQuietTime = true;
3788 }
3789 if (isQuietTime) {
3790 mPointerGesture.quietTime = when;
3791 }
Jeff Brownace13b12011-03-09 17:39:48 -08003792 }
3793 }
3794
3795 // Switch states based on button and pointer state.
3796 if (isQuietTime) {
3797 // Case 1: Quiet time. (QUIET)
3798#if DEBUG_GESTURES
3799 LOGD("Gestures: QUIET for next %0.3fms",
3800 (mPointerGesture.quietTime + QUIET_INTERVAL - when) * 0.000001f);
3801#endif
3802 *outFinishPreviousGesture = true;
3803
3804 mPointerGesture.activeGestureId = -1;
3805 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08003806 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07003807
Jeff Brown19c97d42011-06-01 12:33:19 -07003808 mPointerGesture.pointerVelocityControl.reset();
3809
Jeff Brown2352b972011-04-12 22:39:53 -07003810 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3811 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_NEUTRAL;
3812 mPointerGesture.spotIdBits.clear();
3813 moveSpotsLocked();
3814 }
Jeff Brownace13b12011-03-09 17:39:48 -08003815 } else if (isPointerDown(mCurrentTouch.buttonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003816 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08003817 // The pointer follows the active touch point.
3818 // Emit DOWN, MOVE, UP events at the pointer location.
3819 //
3820 // Only the active touch matters; other fingers are ignored. This policy helps
3821 // to handle the case where the user places a second finger on the touch pad
3822 // to apply the necessary force to depress an integrated button below the surface.
3823 // We don't want the second finger to be delivered to applications.
3824 //
3825 // For this to work well, we need to make sure to track the pointer that is really
3826 // active. If the user first puts one finger down to click then adds another
3827 // finger to drag then the active pointer should switch to the finger that is
3828 // being dragged.
3829#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07003830 LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brownace13b12011-03-09 17:39:48 -08003831 "currentTouchPointerCount=%d", activeTouchId, mCurrentTouch.pointerCount);
3832#endif
3833 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07003834 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08003835 *outFinishPreviousGesture = true;
3836 mPointerGesture.activeGestureId = 0;
3837 }
3838
3839 // Switch pointers if needed.
3840 // Find the fastest pointer and follow it.
Jeff Brown19c97d42011-06-01 12:33:19 -07003841 if (activeTouchId >= 0 && mCurrentTouch.pointerCount > 1) {
3842 int32_t bestId = -1;
3843 float bestSpeed = mConfig->pointerGestureDragMinSwitchSpeed;
3844 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
3845 uint32_t id = mCurrentTouch.pointers[i].id;
3846 float vx, vy;
3847 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
3848 float speed = hypotf(vx, vy);
3849 if (speed > bestSpeed) {
3850 bestId = id;
3851 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08003852 }
Jeff Brown8d608662010-08-30 03:02:23 -07003853 }
Jeff Brown19c97d42011-06-01 12:33:19 -07003854 }
3855 if (bestId >= 0 && bestId != activeTouchId) {
3856 mPointerGesture.activeTouchId = activeTouchId = bestId;
3857 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08003858#if DEBUG_GESTURES
Jeff Brown19c97d42011-06-01 12:33:19 -07003859 LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
3860 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08003861#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07003862 }
Jeff Brown19c97d42011-06-01 12:33:19 -07003863 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003864
Jeff Brown19c97d42011-06-01 12:33:19 -07003865 if (activeTouchId >= 0 && mLastTouch.idBits.hasBit(activeTouchId)) {
3866 const PointerData& currentPointer =
3867 mCurrentTouch.pointers[mCurrentTouch.idToIndex[activeTouchId]];
3868 const PointerData& lastPointer =
3869 mLastTouch.pointers[mLastTouch.idToIndex[activeTouchId]];
3870 float deltaX = (currentPointer.x - lastPointer.x)
3871 * mLocked.pointerGestureXMovementScale;
3872 float deltaY = (currentPointer.y - lastPointer.y)
3873 * mLocked.pointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07003874
Jeff Brown19c97d42011-06-01 12:33:19 -07003875 mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
3876
3877 // Move the pointer using a relative motion.
3878 // When using spots, the click will occur at the position of the anchor
3879 // spot and all other spots will move there.
3880 mPointerController->move(deltaX, deltaY);
3881 } else {
3882 mPointerGesture.pointerVelocityControl.reset();
Jeff Brown46b9ac02010-04-22 18:58:52 -07003883 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003884
Jeff Brownace13b12011-03-09 17:39:48 -08003885 float x, y;
3886 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08003887
Jeff Brown79ac9692011-04-19 21:20:10 -07003888 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08003889 mPointerGesture.currentGestureIdBits.clear();
3890 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3891 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003892 mPointerGesture.currentGestureProperties[0].clear();
3893 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3894 mPointerGesture.currentGestureProperties[0].toolType =
3895 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08003896 mPointerGesture.currentGestureCoords[0].clear();
3897 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3898 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3899 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07003900
Jeff Brown2352b972011-04-12 22:39:53 -07003901 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3902 if (activeTouchId >= 0) {
3903 // Collapse all spots into one point at the pointer location.
3904 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_BUTTON_DRAG;
3905 mPointerGesture.spotIdBits.clear();
3906 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
3907 uint32_t id = mCurrentTouch.pointers[i].id;
3908 mPointerGesture.spotIdBits.markBit(id);
3909 mPointerGesture.spotIdToIndex[id] = i;
3910 mPointerGesture.spotCoords[i] = mPointerGesture.currentGestureCoords[0];
3911 }
3912 } else {
3913 // No fingers. Generate a spot at the pointer location so the
3914 // anchor appears to be pressed.
3915 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_BUTTON_CLICK;
3916 mPointerGesture.spotIdBits.clear();
3917 mPointerGesture.spotIdBits.markBit(0);
3918 mPointerGesture.spotIdToIndex[0] = 0;
3919 mPointerGesture.spotCoords[0] = mPointerGesture.currentGestureCoords[0];
3920 }
3921 moveSpotsLocked();
3922 }
Jeff Brownace13b12011-03-09 17:39:48 -08003923 } else if (mCurrentTouch.pointerCount == 0) {
3924 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
3925 *outFinishPreviousGesture = true;
3926
Jeff Brown79ac9692011-04-19 21:20:10 -07003927 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07003928 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08003929 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07003930 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
3931 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown2352b972011-04-12 22:39:53 -07003932 && mLastTouch.pointerCount == 1) {
Jeff Brown214eaf42011-05-26 19:17:02 -07003933 if (when <= mPointerGesture.tapDownTime + mConfig->pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08003934 float x, y;
3935 mPointerController->getPosition(&x, &y);
Jeff Brown214eaf42011-05-26 19:17:02 -07003936 if (fabs(x - mPointerGesture.tapX) <= mConfig->pointerGestureTapSlop
3937 && fabs(y - mPointerGesture.tapY) <= mConfig->pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08003938#if DEBUG_GESTURES
3939 LOGD("Gestures: TAP");
3940#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07003941
3942 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07003943 getContext()->requestTimeoutAtTime(when
3944 + mConfig->pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07003945
Jeff Brownace13b12011-03-09 17:39:48 -08003946 mPointerGesture.activeGestureId = 0;
3947 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08003948 mPointerGesture.currentGestureIdBits.clear();
3949 mPointerGesture.currentGestureIdBits.markBit(
3950 mPointerGesture.activeGestureId);
3951 mPointerGesture.currentGestureIdToIndex[
3952 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003953 mPointerGesture.currentGestureProperties[0].clear();
3954 mPointerGesture.currentGestureProperties[0].id =
3955 mPointerGesture.activeGestureId;
3956 mPointerGesture.currentGestureProperties[0].toolType =
3957 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08003958 mPointerGesture.currentGestureCoords[0].clear();
3959 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07003960 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08003961 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07003962 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08003963 mPointerGesture.currentGestureCoords[0].setAxisValue(
3964 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07003965
Jeff Brown2352b972011-04-12 22:39:53 -07003966 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3967 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_TAP;
3968 mPointerGesture.spotIdBits.clear();
3969 mPointerGesture.spotIdBits.markBit(lastActiveTouchId);
3970 mPointerGesture.spotIdToIndex[lastActiveTouchId] = 0;
3971 mPointerGesture.spotCoords[0] = mPointerGesture.currentGestureCoords[0];
3972 moveSpotsLocked();
3973 }
3974
Jeff Brownace13b12011-03-09 17:39:48 -08003975 tapped = true;
3976 } else {
3977#if DEBUG_GESTURES
3978 LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07003979 x - mPointerGesture.tapX,
3980 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08003981#endif
3982 }
3983 } else {
3984#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07003985 LOGD("Gestures: Not a TAP, %0.3fms since down",
3986 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08003987#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07003988 }
Jeff Brownace13b12011-03-09 17:39:48 -08003989 }
Jeff Brown2352b972011-04-12 22:39:53 -07003990
Jeff Brown19c97d42011-06-01 12:33:19 -07003991 mPointerGesture.pointerVelocityControl.reset();
3992
Jeff Brownace13b12011-03-09 17:39:48 -08003993 if (!tapped) {
3994#if DEBUG_GESTURES
3995 LOGD("Gestures: NEUTRAL");
3996#endif
3997 mPointerGesture.activeGestureId = -1;
3998 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08003999 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004000
Jeff Brown2352b972011-04-12 22:39:53 -07004001 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4002 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_NEUTRAL;
4003 mPointerGesture.spotIdBits.clear();
4004 moveSpotsLocked();
4005 }
Jeff Brownace13b12011-03-09 17:39:48 -08004006 }
4007 } else if (mCurrentTouch.pointerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004008 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004009 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004010 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4011 // When in TAP_DRAG, emit MOVE events at the pointer location.
Jeff Brownb6110c22011-04-01 16:15:13 -07004012 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004013
Jeff Brown79ac9692011-04-19 21:20:10 -07004014 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4015 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown214eaf42011-05-26 19:17:02 -07004016 if (when <= mPointerGesture.tapUpTime + mConfig->pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004017 float x, y;
4018 mPointerController->getPosition(&x, &y);
Jeff Brown214eaf42011-05-26 19:17:02 -07004019 if (fabs(x - mPointerGesture.tapX) <= mConfig->pointerGestureTapSlop
4020 && fabs(y - mPointerGesture.tapY) <= mConfig->pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004021 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4022 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004023#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004024 LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
4025 x - mPointerGesture.tapX,
4026 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004027#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004028 }
4029 } else {
4030#if DEBUG_GESTURES
4031 LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
4032 (when - mPointerGesture.tapUpTime) * 0.000001f);
4033#endif
4034 }
4035 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4036 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4037 }
Jeff Brownace13b12011-03-09 17:39:48 -08004038
4039 if (mLastTouch.idBits.hasBit(activeTouchId)) {
4040 const PointerData& currentPointer =
4041 mCurrentTouch.pointers[mCurrentTouch.idToIndex[activeTouchId]];
4042 const PointerData& lastPointer =
4043 mLastTouch.pointers[mLastTouch.idToIndex[activeTouchId]];
4044 float deltaX = (currentPointer.x - lastPointer.x)
4045 * mLocked.pointerGestureXMovementScale;
4046 float deltaY = (currentPointer.y - lastPointer.y)
4047 * mLocked.pointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004048
Jeff Brown19c97d42011-06-01 12:33:19 -07004049 mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
4050
Jeff Brown2352b972011-04-12 22:39:53 -07004051 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004052 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004053 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d42011-06-01 12:33:19 -07004054 } else {
4055 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004056 }
4057
Jeff Brown79ac9692011-04-19 21:20:10 -07004058 bool down;
4059 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4060#if DEBUG_GESTURES
4061 LOGD("Gestures: TAP_DRAG");
4062#endif
4063 down = true;
4064 } else {
4065#if DEBUG_GESTURES
4066 LOGD("Gestures: HOVER");
4067#endif
4068 *outFinishPreviousGesture = true;
4069 mPointerGesture.activeGestureId = 0;
4070 down = false;
4071 }
Jeff Brownace13b12011-03-09 17:39:48 -08004072
4073 float x, y;
4074 mPointerController->getPosition(&x, &y);
4075
Jeff Brownace13b12011-03-09 17:39:48 -08004076 mPointerGesture.currentGestureIdBits.clear();
4077 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4078 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004079 mPointerGesture.currentGestureProperties[0].clear();
4080 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4081 mPointerGesture.currentGestureProperties[0].toolType =
4082 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004083 mPointerGesture.currentGestureCoords[0].clear();
4084 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4085 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004086 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4087 down ? 1.0f : 0.0f);
4088
Jeff Brownace13b12011-03-09 17:39:48 -08004089 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004090 mPointerGesture.resetTap();
4091 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004092 mPointerGesture.tapX = x;
4093 mPointerGesture.tapY = y;
4094 }
4095
Jeff Brown2352b972011-04-12 22:39:53 -07004096 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004097 mPointerGesture.spotGesture = down ? PointerControllerInterface::SPOT_GESTURE_DRAG
4098 : PointerControllerInterface::SPOT_GESTURE_HOVER;
Jeff Brown2352b972011-04-12 22:39:53 -07004099 mPointerGesture.spotIdBits.clear();
4100 mPointerGesture.spotIdBits.markBit(activeTouchId);
4101 mPointerGesture.spotIdToIndex[activeTouchId] = 0;
4102 mPointerGesture.spotCoords[0] = mPointerGesture.currentGestureCoords[0];
4103 moveSpotsLocked();
Jeff Brownace13b12011-03-09 17:39:48 -08004104 }
4105 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004106 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4107 // We need to provide feedback for each finger that goes down so we cannot wait
4108 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004109 //
Jeff Brown2352b972011-04-12 22:39:53 -07004110 // The ambiguous case is deciding what to do when there are two fingers down but they
4111 // have not moved enough to determine whether they are part of a drag or part of a
4112 // freeform gesture, or just a press or long-press at the pointer location.
4113 //
4114 // When there are two fingers we start with the PRESS hypothesis and we generate a
4115 // down at the pointer location.
4116 //
4117 // When the two fingers move enough or when additional fingers are added, we make
4118 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Jeff Brownb6110c22011-04-01 16:15:13 -07004119 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004120
Jeff Brown214eaf42011-05-26 19:17:02 -07004121 bool settled = when >= mPointerGesture.firstTouchTime
4122 + mConfig->pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004123 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004124 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4125 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004126 *outFinishPreviousGesture = true;
Jeff Brown19c97d42011-06-01 12:33:19 -07004127 } else if (!settled && mCurrentTouch.pointerCount > mLastTouch.pointerCount) {
4128 // Additional pointers have gone down but not yet settled.
4129 // Reset the gesture.
4130#if DEBUG_GESTURES
4131 LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
4132 "settle time remaining %0.3fms",
4133 (mPointerGesture.firstTouchTime + MULTITOUCH_SETTLE_INTERVAL - when)
4134 * 0.000001f);
4135#endif
4136 *outCancelPreviousGesture = true;
4137 } else {
4138 // Continue previous gesture.
4139 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4140 }
4141
4142 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004143 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4144 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004145 mPointerGesture.referenceIdBits.clear();
Jeff Brown19c97d42011-06-01 12:33:19 -07004146 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004147
Jeff Brown2352b972011-04-12 22:39:53 -07004148 if (settled && mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
4149 && mLastTouch.idBits.hasBit(mPointerGesture.activeTouchId)) {
4150 // The spot is already visible and has settled, use it as the reference point
4151 // for the gesture. Other spots will be positioned relative to this one.
4152#if DEBUG_GESTURES
4153 LOGD("Gestures: Using active spot as reference for MULTITOUCH, "
4154 "settle time expired %0.3fms ago",
4155 (when - mPointerGesture.firstTouchTime - MULTITOUCH_SETTLE_INTERVAL)
4156 * 0.000001f);
4157#endif
4158 const PointerData& d = mLastTouch.pointers[mLastTouch.idToIndex[
4159 mPointerGesture.activeTouchId]];
4160 mPointerGesture.referenceTouchX = d.x;
4161 mPointerGesture.referenceTouchY = d.y;
4162 const PointerCoords& c = mPointerGesture.spotCoords[mPointerGesture.spotIdToIndex[
4163 mPointerGesture.activeTouchId]];
4164 mPointerGesture.referenceGestureX = c.getAxisValue(AMOTION_EVENT_AXIS_X);
4165 mPointerGesture.referenceGestureY = c.getAxisValue(AMOTION_EVENT_AXIS_Y);
4166 } else {
Jeff Brown19c97d42011-06-01 12:33:19 -07004167 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004168#if DEBUG_GESTURES
4169 LOGD("Gestures: Using centroid as reference for MULTITOUCH, "
4170 "settle time remaining %0.3fms",
4171 (mPointerGesture.firstTouchTime + MULTITOUCH_SETTLE_INTERVAL - when)
4172 * 0.000001f);
4173#endif
Jeff Brown19c97d42011-06-01 12:33:19 -07004174 mCurrentTouch.getCentroid(&mPointerGesture.referenceTouchX,
4175 &mPointerGesture.referenceTouchY);
4176 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4177 &mPointerGesture.referenceGestureY);
Jeff Brownace13b12011-03-09 17:39:48 -08004178 }
Jeff Brown2352b972011-04-12 22:39:53 -07004179 }
Jeff Brownace13b12011-03-09 17:39:48 -08004180
Jeff Brown2352b972011-04-12 22:39:53 -07004181 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4182 float d;
4183 if (mCurrentTouch.pointerCount > 2) {
4184 // There are more than two pointers, switch to FREEFORM.
4185#if DEBUG_GESTURES
4186 LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
4187 mCurrentTouch.pointerCount);
4188#endif
4189 *outCancelPreviousGesture = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004190 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown2352b972011-04-12 22:39:53 -07004191 } else if (((d = distance(
4192 mCurrentTouch.pointers[0].x, mCurrentTouch.pointers[0].y,
4193 mCurrentTouch.pointers[1].x, mCurrentTouch.pointers[1].y))
4194 > mLocked.pointerGestureMaxSwipeWidth)) {
4195 // There are two pointers but they are too far apart, switch to FREEFORM.
4196#if DEBUG_GESTURES
4197 LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
4198 d, mLocked.pointerGestureMaxSwipeWidth);
4199#endif
4200 *outCancelPreviousGesture = true;
4201 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4202 } else {
4203 // There are two pointers. Wait for both pointers to start moving
4204 // before deciding whether this is a SWIPE or FREEFORM gesture.
4205 uint32_t id1 = mCurrentTouch.pointers[0].id;
4206 uint32_t id2 = mCurrentTouch.pointers[1].id;
Jeff Brownace13b12011-03-09 17:39:48 -08004207
Jeff Brown2352b972011-04-12 22:39:53 -07004208 float vx1, vy1, vx2, vy2;
4209 mPointerGesture.velocityTracker.getVelocity(id1, &vx1, &vy1);
4210 mPointerGesture.velocityTracker.getVelocity(id2, &vx2, &vy2);
Jeff Brownace13b12011-03-09 17:39:48 -08004211
Jeff Brown2352b972011-04-12 22:39:53 -07004212 float speed1 = hypotf(vx1, vy1);
4213 float speed2 = hypotf(vx2, vy2);
Jeff Brown214eaf42011-05-26 19:17:02 -07004214 if (speed1 >= mConfig->pointerGestureMultitouchMinSpeed
4215 && speed2 >= mConfig->pointerGestureMultitouchMinSpeed) {
Jeff Brown2352b972011-04-12 22:39:53 -07004216 // Calculate the dot product of the velocity vectors.
Jeff Brownace13b12011-03-09 17:39:48 -08004217 // When the vectors are oriented in approximately the same direction,
4218 // the angle betweeen them is near zero and the cosine of the angle
4219 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
Jeff Brown2352b972011-04-12 22:39:53 -07004220 float dot = vx1 * vx2 + vy1 * vy2;
4221 float cosine = dot / (speed1 * speed2); // denominator always > 0
Jeff Brown214eaf42011-05-26 19:17:02 -07004222 if (cosine >= mConfig->pointerGestureSwipeTransitionAngleCosine) {
Jeff Brown2352b972011-04-12 22:39:53 -07004223 // Pointers are moving in the same direction. Switch to SWIPE.
4224#if DEBUG_GESTURES
4225 LOGD("Gestures: PRESS transitioned to SWIPE, "
4226 "speed1 %0.3f >= %0.3f, speed2 %0.3f >= %0.3f, "
4227 "cosine %0.3f >= %0.3f",
4228 speed1, MULTITOUCH_MIN_SPEED, speed2, MULTITOUCH_MIN_SPEED,
4229 cosine, SWIPE_TRANSITION_ANGLE_COSINE);
4230#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004231 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
Jeff Brown2352b972011-04-12 22:39:53 -07004232 } else {
4233 // Pointers are moving in different directions. Switch to FREEFORM.
4234#if DEBUG_GESTURES
4235 LOGD("Gestures: PRESS transitioned to FREEFORM, "
4236 "speed1 %0.3f >= %0.3f, speed2 %0.3f >= %0.3f, "
4237 "cosine %0.3f < %0.3f",
4238 speed1, MULTITOUCH_MIN_SPEED, speed2, MULTITOUCH_MIN_SPEED,
4239 cosine, SWIPE_TRANSITION_ANGLE_COSINE);
4240#endif
4241 *outCancelPreviousGesture = true;
4242 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brownace13b12011-03-09 17:39:48 -08004243 }
4244 }
Jeff Brownace13b12011-03-09 17:39:48 -08004245 }
4246 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004247 // Switch from SWIPE to FREEFORM if additional pointers go down.
4248 // Cancel previous gesture.
4249 if (mCurrentTouch.pointerCount > 2) {
4250#if DEBUG_GESTURES
4251 LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
4252 mCurrentTouch.pointerCount);
4253#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004254 *outCancelPreviousGesture = true;
4255 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004256 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07004257 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004258
Jeff Brown538881e2011-05-25 18:23:38 -07004259 // Clear the reference deltas for fingers not yet included in the reference calculation.
4260 for (BitSet32 idBits(mCurrentTouch.idBits.value & ~mPointerGesture.referenceIdBits.value);
4261 !idBits.isEmpty(); ) {
4262 uint32_t id = idBits.firstMarkedBit();
4263 idBits.clearBit(id);
4264
4265 mPointerGesture.referenceDeltas[id].dx = 0;
4266 mPointerGesture.referenceDeltas[id].dy = 0;
4267 }
4268 mPointerGesture.referenceIdBits = mCurrentTouch.idBits;
4269
Jeff Brown2352b972011-04-12 22:39:53 -07004270 // Move the reference points based on the overall group motion of the fingers.
4271 // The objective is to calculate a vector delta that is common to the movement
4272 // of all fingers.
4273 BitSet32 commonIdBits(mLastTouch.idBits.value & mCurrentTouch.idBits.value);
4274 if (!commonIdBits.isEmpty()) {
4275 float commonDeltaX = 0, commonDeltaY = 0;
4276 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4277 bool first = (idBits == commonIdBits);
4278 uint32_t id = idBits.firstMarkedBit();
4279 idBits.clearBit(id);
4280
4281 const PointerData& cpd = mCurrentTouch.pointers[mCurrentTouch.idToIndex[id]];
4282 const PointerData& lpd = mLastTouch.pointers[mLastTouch.idToIndex[id]];
Jeff Brown538881e2011-05-25 18:23:38 -07004283 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4284 delta.dx += cpd.x - lpd.x;
4285 delta.dy += cpd.y - lpd.y;
Jeff Brown2352b972011-04-12 22:39:53 -07004286
4287 if (first) {
Jeff Brown538881e2011-05-25 18:23:38 -07004288 commonDeltaX = delta.dx;
4289 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004290 } else {
Jeff Brown538881e2011-05-25 18:23:38 -07004291 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4292 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
Jeff Brown2352b972011-04-12 22:39:53 -07004293 }
4294 }
4295
Jeff Brown538881e2011-05-25 18:23:38 -07004296 if (commonDeltaX || commonDeltaY) {
4297 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4298 uint32_t id = idBits.firstMarkedBit();
4299 idBits.clearBit(id);
4300
4301 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4302 delta.dx = 0;
4303 delta.dy = 0;
4304 }
4305
4306 mPointerGesture.referenceTouchX += commonDeltaX;
4307 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown19c97d42011-06-01 12:33:19 -07004308
4309 commonDeltaX *= mLocked.pointerGestureXMovementScale;
4310 commonDeltaY *= mLocked.pointerGestureYMovementScale;
4311 mPointerGesture.pointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
4312
4313 mPointerGesture.referenceGestureX += commonDeltaX;
4314 mPointerGesture.referenceGestureY += commonDeltaY;
4315
Jeff Brown538881e2011-05-25 18:23:38 -07004316 clampPositionUsingPointerBounds(mPointerController,
4317 &mPointerGesture.referenceGestureX,
4318 &mPointerGesture.referenceGestureY);
4319 }
Jeff Brown2352b972011-04-12 22:39:53 -07004320 }
4321
4322 // Report gestures.
4323 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4324 // PRESS mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004325#if DEBUG_GESTURES
Jeff Brown2352b972011-04-12 22:39:53 -07004326 LOGD("Gestures: PRESS activeTouchId=%d,"
Jeff Brownace13b12011-03-09 17:39:48 -08004327 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown2352b972011-04-12 22:39:53 -07004328 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004329#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004330 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004331
Jeff Brownace13b12011-03-09 17:39:48 -08004332 mPointerGesture.currentGestureIdBits.clear();
4333 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4334 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004335 mPointerGesture.currentGestureProperties[0].clear();
4336 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4337 mPointerGesture.currentGestureProperties[0].toolType =
4338 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004339 mPointerGesture.currentGestureCoords[0].clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004340 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4341 mPointerGesture.referenceGestureX);
4342 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4343 mPointerGesture.referenceGestureY);
Jeff Brownace13b12011-03-09 17:39:48 -08004344 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004345
Jeff Brown2352b972011-04-12 22:39:53 -07004346 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4347 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_PRESS;
4348 }
4349 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4350 // SWIPE mode.
4351#if DEBUG_GESTURES
4352 LOGD("Gestures: SWIPE activeTouchId=%d,"
4353 "activeGestureId=%d, currentTouchPointerCount=%d",
4354 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
4355#endif
4356 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
4357
4358 mPointerGesture.currentGestureIdBits.clear();
4359 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4360 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004361 mPointerGesture.currentGestureProperties[0].clear();
4362 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4363 mPointerGesture.currentGestureProperties[0].toolType =
4364 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004365 mPointerGesture.currentGestureCoords[0].clear();
4366 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4367 mPointerGesture.referenceGestureX);
4368 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4369 mPointerGesture.referenceGestureY);
4370 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4371
Jeff Brown2352b972011-04-12 22:39:53 -07004372 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4373 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_SWIPE;
4374 }
Jeff Brownace13b12011-03-09 17:39:48 -08004375 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4376 // FREEFORM mode.
4377#if DEBUG_GESTURES
4378 LOGD("Gestures: FREEFORM activeTouchId=%d,"
4379 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown2352b972011-04-12 22:39:53 -07004380 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004381#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004382 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004383
Jeff Brownace13b12011-03-09 17:39:48 -08004384 mPointerGesture.currentGestureIdBits.clear();
4385
4386 BitSet32 mappedTouchIdBits;
4387 BitSet32 usedGestureIdBits;
4388 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4389 // Initially, assign the active gesture id to the active touch point
4390 // if there is one. No other touch id bits are mapped yet.
4391 if (!*outCancelPreviousGesture) {
4392 mappedTouchIdBits.markBit(activeTouchId);
4393 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4394 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4395 mPointerGesture.activeGestureId;
4396 } else {
4397 mPointerGesture.activeGestureId = -1;
4398 }
4399 } else {
4400 // Otherwise, assume we mapped all touches from the previous frame.
4401 // Reuse all mappings that are still applicable.
4402 mappedTouchIdBits.value = mLastTouch.idBits.value & mCurrentTouch.idBits.value;
4403 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4404
4405 // Check whether we need to choose a new active gesture id because the
4406 // current went went up.
4407 for (BitSet32 upTouchIdBits(mLastTouch.idBits.value & ~mCurrentTouch.idBits.value);
4408 !upTouchIdBits.isEmpty(); ) {
4409 uint32_t upTouchId = upTouchIdBits.firstMarkedBit();
4410 upTouchIdBits.clearBit(upTouchId);
4411 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4412 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4413 mPointerGesture.activeGestureId = -1;
4414 break;
4415 }
4416 }
4417 }
4418
4419#if DEBUG_GESTURES
4420 LOGD("Gestures: FREEFORM follow up "
4421 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4422 "activeGestureId=%d",
4423 mappedTouchIdBits.value, usedGestureIdBits.value,
4424 mPointerGesture.activeGestureId);
4425#endif
4426
Jeff Brown2352b972011-04-12 22:39:53 -07004427 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
Jeff Brownace13b12011-03-09 17:39:48 -08004428 uint32_t touchId = mCurrentTouch.pointers[i].id;
4429 uint32_t gestureId;
4430 if (!mappedTouchIdBits.hasBit(touchId)) {
4431 gestureId = usedGestureIdBits.firstUnmarkedBit();
4432 usedGestureIdBits.markBit(gestureId);
4433 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4434#if DEBUG_GESTURES
4435 LOGD("Gestures: FREEFORM "
4436 "new mapping for touch id %d -> gesture id %d",
4437 touchId, gestureId);
4438#endif
4439 } else {
4440 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4441#if DEBUG_GESTURES
4442 LOGD("Gestures: FREEFORM "
4443 "existing mapping for touch id %d -> gesture id %d",
4444 touchId, gestureId);
4445#endif
4446 }
4447 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4448 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4449
Jeff Brown2352b972011-04-12 22:39:53 -07004450 float x = (mCurrentTouch.pointers[i].x - mPointerGesture.referenceTouchX)
4451 * mLocked.pointerGestureXZoomScale + mPointerGesture.referenceGestureX;
4452 float y = (mCurrentTouch.pointers[i].y - mPointerGesture.referenceTouchY)
4453 * mLocked.pointerGestureYZoomScale + mPointerGesture.referenceGestureY;
Jeff Brownace13b12011-03-09 17:39:48 -08004454
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004455 mPointerGesture.currentGestureProperties[i].clear();
4456 mPointerGesture.currentGestureProperties[i].id = gestureId;
4457 mPointerGesture.currentGestureProperties[i].toolType =
4458 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004459 mPointerGesture.currentGestureCoords[i].clear();
4460 mPointerGesture.currentGestureCoords[i].setAxisValue(
4461 AMOTION_EVENT_AXIS_X, x);
4462 mPointerGesture.currentGestureCoords[i].setAxisValue(
4463 AMOTION_EVENT_AXIS_Y, y);
4464 mPointerGesture.currentGestureCoords[i].setAxisValue(
4465 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4466 }
4467
4468 if (mPointerGesture.activeGestureId < 0) {
4469 mPointerGesture.activeGestureId =
4470 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4471#if DEBUG_GESTURES
4472 LOGD("Gestures: FREEFORM new "
4473 "activeGestureId=%d", mPointerGesture.activeGestureId);
4474#endif
4475 }
Jeff Brownace13b12011-03-09 17:39:48 -08004476
Jeff Brown2352b972011-04-12 22:39:53 -07004477 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4478 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_FREEFORM;
4479 }
4480 }
4481
4482 // Update spot locations for PRESS, SWIPE and FREEFORM.
4483 // We use the same calculation as we do to calculate the gesture pointers
4484 // for FREEFORM so that the spots smoothly track gestures.
4485 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4486 mPointerGesture.spotIdBits.clear();
4487 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
4488 uint32_t id = mCurrentTouch.pointers[i].id;
4489 mPointerGesture.spotIdBits.markBit(id);
4490 mPointerGesture.spotIdToIndex[id] = i;
4491
4492 float x = (mCurrentTouch.pointers[i].x - mPointerGesture.referenceTouchX)
4493 * mLocked.pointerGestureXZoomScale + mPointerGesture.referenceGestureX;
4494 float y = (mCurrentTouch.pointers[i].y - mPointerGesture.referenceTouchY)
4495 * mLocked.pointerGestureYZoomScale + mPointerGesture.referenceGestureY;
4496
4497 mPointerGesture.spotCoords[i].clear();
4498 mPointerGesture.spotCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4499 mPointerGesture.spotCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4500 mPointerGesture.spotCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4501 }
4502 moveSpotsLocked();
4503 }
Jeff Brownace13b12011-03-09 17:39:48 -08004504 }
4505
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004506 mPointerController->setButtonState(mCurrentTouch.buttonState);
4507
Jeff Brownace13b12011-03-09 17:39:48 -08004508#if DEBUG_GESTURES
4509 LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004510 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4511 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004512 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004513 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4514 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004515 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
4516 uint32_t id = idBits.firstMarkedBit();
4517 idBits.clearBit(id);
4518 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004519 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004520 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004521 LOGD(" currentGesture[%d]: index=%d, toolType=%d, "
4522 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4523 id, index, properties.toolType,
4524 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004525 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4526 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4527 }
4528 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
4529 uint32_t id = idBits.firstMarkedBit();
4530 idBits.clearBit(id);
4531 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004532 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004533 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004534 LOGD(" lastGesture[%d]: index=%d, toolType=%d, "
4535 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4536 id, index, properties.toolType,
4537 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004538 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4539 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4540 }
4541#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004542 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004543}
4544
Jeff Brown2352b972011-04-12 22:39:53 -07004545void TouchInputMapper::moveSpotsLocked() {
4546 mPointerController->setSpots(mPointerGesture.spotGesture,
4547 mPointerGesture.spotCoords, mPointerGesture.spotIdToIndex, mPointerGesture.spotIdBits);
4548}
4549
Jeff Brownace13b12011-03-09 17:39:48 -08004550void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004551 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
4552 const PointerProperties* properties, const PointerCoords* coords,
4553 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08004554 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
4555 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004556 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08004557 uint32_t pointerCount = 0;
4558 while (!idBits.isEmpty()) {
4559 uint32_t id = idBits.firstMarkedBit();
4560 idBits.clearBit(id);
4561 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004562 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004563 pointerCoords[pointerCount].copyFrom(coords[index]);
4564
4565 if (changedId >= 0 && id == uint32_t(changedId)) {
4566 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
4567 }
4568
4569 pointerCount += 1;
4570 }
4571
Jeff Brownb6110c22011-04-01 16:15:13 -07004572 LOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004573
4574 if (changedId >= 0 && pointerCount == 1) {
4575 // Replace initial down and final up action.
4576 // We can compare the action without masking off the changed pointer index
4577 // because we know the index is 0.
4578 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
4579 action = AMOTION_EVENT_ACTION_DOWN;
4580 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
4581 action = AMOTION_EVENT_ACTION_UP;
4582 } else {
4583 // Can't happen.
Jeff Brownb6110c22011-04-01 16:15:13 -07004584 LOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08004585 }
4586 }
4587
4588 getDispatcher()->notifyMotion(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004589 action, flags, metaState, buttonState, edgeFlags,
4590 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownace13b12011-03-09 17:39:48 -08004591}
4592
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004593bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004594 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004595 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
4596 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08004597 bool changed = false;
4598 while (!idBits.isEmpty()) {
4599 uint32_t id = idBits.firstMarkedBit();
4600 idBits.clearBit(id);
4601
4602 uint32_t inIndex = inIdToIndex[id];
4603 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004604
4605 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004606 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004607 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004608 PointerCoords& curOutCoords = outCoords[outIndex];
4609
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004610 if (curInProperties != curOutProperties) {
4611 curOutProperties.copyFrom(curInProperties);
4612 changed = true;
4613 }
4614
Jeff Brownace13b12011-03-09 17:39:48 -08004615 if (curInCoords != curOutCoords) {
4616 curOutCoords.copyFrom(curInCoords);
4617 changed = true;
4618 }
4619 }
4620 return changed;
4621}
4622
4623void TouchInputMapper::fadePointer() {
4624 { // acquire lock
4625 AutoMutex _l(mLock);
4626 if (mPointerController != NULL) {
Jeff Brown538881e2011-05-25 18:23:38 -07004627 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
Jeff Brownace13b12011-03-09 17:39:48 -08004628 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004629 } // release lock
Jeff Brown46b9ac02010-04-22 18:58:52 -07004630}
4631
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004632int32_t TouchInputMapper::getTouchToolType(bool isStylus) const {
4633 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
4634 return isStylus ? AMOTION_EVENT_TOOL_TYPE_STYLUS : AMOTION_EVENT_TOOL_TYPE_FINGER;
4635 } else {
4636 return isStylus ? AMOTION_EVENT_TOOL_TYPE_INDIRECT_STYLUS
4637 : AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
4638 }
4639}
4640
Jeff Brown6328cdc2010-07-29 18:18:33 -07004641bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) {
Jeff Brown9626b142011-03-03 02:09:54 -08004642 return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue
4643 && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004644}
4645
Jeff Brown6328cdc2010-07-29 18:18:33 -07004646const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked(
4647 int32_t x, int32_t y) {
4648 size_t numVirtualKeys = mLocked.virtualKeys.size();
4649 for (size_t i = 0; i < numVirtualKeys; i++) {
4650 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004651
4652#if DEBUG_VIRTUAL_KEYS
4653 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
4654 "left=%d, top=%d, right=%d, bottom=%d",
4655 x, y,
4656 virtualKey.keyCode, virtualKey.scanCode,
4657 virtualKey.hitLeft, virtualKey.hitTop,
4658 virtualKey.hitRight, virtualKey.hitBottom);
4659#endif
4660
4661 if (virtualKey.isHit(x, y)) {
4662 return & virtualKey;
4663 }
4664 }
4665
4666 return NULL;
4667}
4668
4669void TouchInputMapper::calculatePointerIds() {
4670 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
4671 uint32_t lastPointerCount = mLastTouch.pointerCount;
4672
4673 if (currentPointerCount == 0) {
4674 // No pointers to assign.
4675 mCurrentTouch.idBits.clear();
4676 } else if (lastPointerCount == 0) {
4677 // All pointers are new.
4678 mCurrentTouch.idBits.clear();
4679 for (uint32_t i = 0; i < currentPointerCount; i++) {
4680 mCurrentTouch.pointers[i].id = i;
4681 mCurrentTouch.idToIndex[i] = i;
4682 mCurrentTouch.idBits.markBit(i);
4683 }
4684 } else if (currentPointerCount == 1 && lastPointerCount == 1) {
4685 // Only one pointer and no change in count so it must have the same id as before.
4686 uint32_t id = mLastTouch.pointers[0].id;
4687 mCurrentTouch.pointers[0].id = id;
4688 mCurrentTouch.idToIndex[id] = 0;
4689 mCurrentTouch.idBits.value = BitSet32::valueForBit(id);
4690 } else {
4691 // General case.
4692 // We build a heap of squared euclidean distances between current and last pointers
4693 // associated with the current and last pointer indices. Then, we find the best
4694 // match (by distance) for each current pointer.
4695 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
4696
4697 uint32_t heapSize = 0;
4698 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
4699 currentPointerIndex++) {
4700 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
4701 lastPointerIndex++) {
4702 int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x
4703 - mLastTouch.pointers[lastPointerIndex].x;
4704 int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y
4705 - mLastTouch.pointers[lastPointerIndex].y;
4706
4707 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
4708
4709 // Insert new element into the heap (sift up).
4710 heap[heapSize].currentPointerIndex = currentPointerIndex;
4711 heap[heapSize].lastPointerIndex = lastPointerIndex;
4712 heap[heapSize].distance = distance;
4713 heapSize += 1;
4714 }
4715 }
4716
4717 // Heapify
4718 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
4719 startIndex -= 1;
4720 for (uint32_t parentIndex = startIndex; ;) {
4721 uint32_t childIndex = parentIndex * 2 + 1;
4722 if (childIndex >= heapSize) {
4723 break;
4724 }
4725
4726 if (childIndex + 1 < heapSize
4727 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4728 childIndex += 1;
4729 }
4730
4731 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4732 break;
4733 }
4734
4735 swap(heap[parentIndex], heap[childIndex]);
4736 parentIndex = childIndex;
4737 }
4738 }
4739
4740#if DEBUG_POINTER_ASSIGNMENT
4741 LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize);
4742 for (size_t i = 0; i < heapSize; i++) {
4743 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4744 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4745 heap[i].distance);
4746 }
4747#endif
4748
4749 // Pull matches out by increasing order of distance.
4750 // To avoid reassigning pointers that have already been matched, the loop keeps track
4751 // of which last and current pointers have been matched using the matchedXXXBits variables.
4752 // It also tracks the used pointer id bits.
4753 BitSet32 matchedLastBits(0);
4754 BitSet32 matchedCurrentBits(0);
4755 BitSet32 usedIdBits(0);
4756 bool first = true;
4757 for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) {
4758 for (;;) {
4759 if (first) {
4760 // The first time through the loop, we just consume the root element of
4761 // the heap (the one with smallest distance).
4762 first = false;
4763 } else {
4764 // Previous iterations consumed the root element of the heap.
4765 // Pop root element off of the heap (sift down).
4766 heapSize -= 1;
Jeff Brownb6110c22011-04-01 16:15:13 -07004767 LOG_ASSERT(heapSize > 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004768
4769 // Sift down.
4770 heap[0] = heap[heapSize];
4771 for (uint32_t parentIndex = 0; ;) {
4772 uint32_t childIndex = parentIndex * 2 + 1;
4773 if (childIndex >= heapSize) {
4774 break;
4775 }
4776
4777 if (childIndex + 1 < heapSize
4778 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4779 childIndex += 1;
4780 }
4781
4782 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4783 break;
4784 }
4785
4786 swap(heap[parentIndex], heap[childIndex]);
4787 parentIndex = childIndex;
4788 }
4789
4790#if DEBUG_POINTER_ASSIGNMENT
4791 LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize);
4792 for (size_t i = 0; i < heapSize; i++) {
4793 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4794 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4795 heap[i].distance);
4796 }
4797#endif
4798 }
4799
4800 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
4801 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
4802
4803 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
4804 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
4805
4806 matchedCurrentBits.markBit(currentPointerIndex);
4807 matchedLastBits.markBit(lastPointerIndex);
4808
4809 uint32_t id = mLastTouch.pointers[lastPointerIndex].id;
4810 mCurrentTouch.pointers[currentPointerIndex].id = id;
4811 mCurrentTouch.idToIndex[id] = currentPointerIndex;
4812 usedIdBits.markBit(id);
4813
4814#if DEBUG_POINTER_ASSIGNMENT
4815 LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
4816 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
4817#endif
4818 break;
4819 }
4820 }
4821
4822 // Assign fresh ids to new pointers.
4823 if (currentPointerCount > lastPointerCount) {
4824 for (uint32_t i = currentPointerCount - lastPointerCount; ;) {
4825 uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit();
4826 uint32_t id = usedIdBits.firstUnmarkedBit();
4827
4828 mCurrentTouch.pointers[currentPointerIndex].id = id;
4829 mCurrentTouch.idToIndex[id] = currentPointerIndex;
4830 usedIdBits.markBit(id);
4831
4832#if DEBUG_POINTER_ASSIGNMENT
4833 LOGD("calculatePointerIds - assigned: cur=%d, id=%d",
4834 currentPointerIndex, id);
4835#endif
4836
4837 if (--i == 0) break; // done
4838 matchedCurrentBits.markBit(currentPointerIndex);
4839 }
4840 }
4841
4842 // Fix id bits.
4843 mCurrentTouch.idBits = usedIdBits;
4844 }
4845}
4846
4847/* Special hack for devices that have bad screen data: if one of the
4848 * points has moved more than a screen height from the last position,
4849 * then drop it. */
4850bool TouchInputMapper::applyBadTouchFilter() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004851 uint32_t pointerCount = mCurrentTouch.pointerCount;
4852
4853 // Nothing to do if there are no points.
4854 if (pointerCount == 0) {
4855 return false;
4856 }
4857
4858 // Don't do anything if a finger is going down or up. We run
4859 // here before assigning pointer IDs, so there isn't a good
4860 // way to do per-finger matching.
4861 if (pointerCount != mLastTouch.pointerCount) {
4862 return false;
4863 }
4864
4865 // We consider a single movement across more than a 7/16 of
4866 // the long size of the screen to be bad. This was a magic value
4867 // determined by looking at the maximum distance it is feasible
4868 // to actually move in one sample.
Jeff Brown9626b142011-03-03 02:09:54 -08004869 int32_t maxDeltaY = (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1) * 7 / 16;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004870
4871 // XXX The original code in InputDevice.java included commented out
4872 // code for testing the X axis. Note that when we drop a point
4873 // we don't actually restore the old X either. Strange.
4874 // The old code also tries to track when bad points were previously
4875 // detected but it turns out that due to the placement of a "break"
4876 // at the end of the loop, we never set mDroppedBadPoint to true
4877 // so it is effectively dead code.
4878 // Need to figure out if the old code is busted or just overcomplicated
4879 // but working as intended.
4880
4881 // Look through all new points and see if any are farther than
4882 // acceptable from all previous points.
4883 for (uint32_t i = pointerCount; i-- > 0; ) {
4884 int32_t y = mCurrentTouch.pointers[i].y;
4885 int32_t closestY = INT_MAX;
4886 int32_t closestDeltaY = 0;
4887
4888#if DEBUG_HACKS
4889 LOGD("BadTouchFilter: Looking at next point #%d: y=%d", i, y);
4890#endif
4891
4892 for (uint32_t j = pointerCount; j-- > 0; ) {
4893 int32_t lastY = mLastTouch.pointers[j].y;
4894 int32_t deltaY = abs(y - lastY);
4895
4896#if DEBUG_HACKS
4897 LOGD("BadTouchFilter: Comparing with last point #%d: y=%d deltaY=%d",
4898 j, lastY, deltaY);
4899#endif
4900
4901 if (deltaY < maxDeltaY) {
4902 goto SkipSufficientlyClosePoint;
4903 }
4904 if (deltaY < closestDeltaY) {
4905 closestDeltaY = deltaY;
4906 closestY = lastY;
4907 }
4908 }
4909
4910 // Must not have found a close enough match.
4911#if DEBUG_HACKS
4912 LOGD("BadTouchFilter: Dropping bad point #%d: newY=%d oldY=%d deltaY=%d maxDeltaY=%d",
4913 i, y, closestY, closestDeltaY, maxDeltaY);
4914#endif
4915
4916 mCurrentTouch.pointers[i].y = closestY;
4917 return true; // XXX original code only corrects one point
4918
4919 SkipSufficientlyClosePoint: ;
4920 }
4921
4922 // No change.
4923 return false;
4924}
4925
4926/* Special hack for devices that have bad screen data: drop points where
4927 * the coordinate value for one axis has jumped to the other pointer's location.
4928 */
4929bool TouchInputMapper::applyJumpyTouchFilter() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004930 uint32_t pointerCount = mCurrentTouch.pointerCount;
4931 if (mLastTouch.pointerCount != pointerCount) {
4932#if DEBUG_HACKS
4933 LOGD("JumpyTouchFilter: Different pointer count %d -> %d",
4934 mLastTouch.pointerCount, pointerCount);
4935 for (uint32_t i = 0; i < pointerCount; i++) {
4936 LOGD(" Pointer %d (%d, %d)", i,
4937 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
4938 }
4939#endif
4940
4941 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_TRANSITION_DROPS) {
4942 if (mLastTouch.pointerCount == 1 && pointerCount == 2) {
4943 // Just drop the first few events going from 1 to 2 pointers.
4944 // They're bad often enough that they're not worth considering.
4945 mCurrentTouch.pointerCount = 1;
4946 mJumpyTouchFilter.jumpyPointsDropped += 1;
4947
4948#if DEBUG_HACKS
4949 LOGD("JumpyTouchFilter: Pointer 2 dropped");
4950#endif
4951 return true;
4952 } else if (mLastTouch.pointerCount == 2 && pointerCount == 1) {
4953 // The event when we go from 2 -> 1 tends to be messed up too
4954 mCurrentTouch.pointerCount = 2;
4955 mCurrentTouch.pointers[0] = mLastTouch.pointers[0];
4956 mCurrentTouch.pointers[1] = mLastTouch.pointers[1];
4957 mJumpyTouchFilter.jumpyPointsDropped += 1;
4958
4959#if DEBUG_HACKS
4960 for (int32_t i = 0; i < 2; i++) {
4961 LOGD("JumpyTouchFilter: Pointer %d replaced (%d, %d)", i,
4962 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
4963 }
4964#endif
4965 return true;
4966 }
4967 }
4968 // Reset jumpy points dropped on other transitions or if limit exceeded.
4969 mJumpyTouchFilter.jumpyPointsDropped = 0;
4970
4971#if DEBUG_HACKS
4972 LOGD("JumpyTouchFilter: Transition - drop limit reset");
4973#endif
4974 return false;
4975 }
4976
4977 // We have the same number of pointers as last time.
4978 // A 'jumpy' point is one where the coordinate value for one axis
4979 // has jumped to the other pointer's location. No need to do anything
4980 // else if we only have one pointer.
4981 if (pointerCount < 2) {
4982 return false;
4983 }
4984
4985 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_DROP_LIMIT) {
Jeff Brown9626b142011-03-03 02:09:54 -08004986 int jumpyEpsilon = (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1) / JUMPY_EPSILON_DIVISOR;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004987
4988 // We only replace the single worst jumpy point as characterized by pointer distance
4989 // in a single axis.
4990 int32_t badPointerIndex = -1;
4991 int32_t badPointerReplacementIndex = -1;
4992 int32_t badPointerDistance = INT_MIN; // distance to be corrected
4993
4994 for (uint32_t i = pointerCount; i-- > 0; ) {
4995 int32_t x = mCurrentTouch.pointers[i].x;
4996 int32_t y = mCurrentTouch.pointers[i].y;
4997
4998#if DEBUG_HACKS
4999 LOGD("JumpyTouchFilter: Point %d (%d, %d)", i, x, y);
5000#endif
5001
5002 // Check if a touch point is too close to another's coordinates
5003 bool dropX = false, dropY = false;
5004 for (uint32_t j = 0; j < pointerCount; j++) {
5005 if (i == j) {
5006 continue;
5007 }
5008
5009 if (abs(x - mCurrentTouch.pointers[j].x) <= jumpyEpsilon) {
5010 dropX = true;
5011 break;
5012 }
5013
5014 if (abs(y - mCurrentTouch.pointers[j].y) <= jumpyEpsilon) {
5015 dropY = true;
5016 break;
5017 }
5018 }
5019 if (! dropX && ! dropY) {
5020 continue; // not jumpy
5021 }
5022
5023 // Find a replacement candidate by comparing with older points on the
5024 // complementary (non-jumpy) axis.
5025 int32_t distance = INT_MIN; // distance to be corrected
5026 int32_t replacementIndex = -1;
5027
5028 if (dropX) {
5029 // X looks too close. Find an older replacement point with a close Y.
5030 int32_t smallestDeltaY = INT_MAX;
5031 for (uint32_t j = 0; j < pointerCount; j++) {
5032 int32_t deltaY = abs(y - mLastTouch.pointers[j].y);
5033 if (deltaY < smallestDeltaY) {
5034 smallestDeltaY = deltaY;
5035 replacementIndex = j;
5036 }
5037 }
5038 distance = abs(x - mLastTouch.pointers[replacementIndex].x);
5039 } else {
5040 // Y looks too close. Find an older replacement point with a close X.
5041 int32_t smallestDeltaX = INT_MAX;
5042 for (uint32_t j = 0; j < pointerCount; j++) {
5043 int32_t deltaX = abs(x - mLastTouch.pointers[j].x);
5044 if (deltaX < smallestDeltaX) {
5045 smallestDeltaX = deltaX;
5046 replacementIndex = j;
5047 }
5048 }
5049 distance = abs(y - mLastTouch.pointers[replacementIndex].y);
5050 }
5051
5052 // If replacing this pointer would correct a worse error than the previous ones
5053 // considered, then use this replacement instead.
5054 if (distance > badPointerDistance) {
5055 badPointerIndex = i;
5056 badPointerReplacementIndex = replacementIndex;
5057 badPointerDistance = distance;
5058 }
5059 }
5060
5061 // Correct the jumpy pointer if one was found.
5062 if (badPointerIndex >= 0) {
5063#if DEBUG_HACKS
5064 LOGD("JumpyTouchFilter: Replacing bad pointer %d with (%d, %d)",
5065 badPointerIndex,
5066 mLastTouch.pointers[badPointerReplacementIndex].x,
5067 mLastTouch.pointers[badPointerReplacementIndex].y);
5068#endif
5069
5070 mCurrentTouch.pointers[badPointerIndex].x =
5071 mLastTouch.pointers[badPointerReplacementIndex].x;
5072 mCurrentTouch.pointers[badPointerIndex].y =
5073 mLastTouch.pointers[badPointerReplacementIndex].y;
5074 mJumpyTouchFilter.jumpyPointsDropped += 1;
5075 return true;
5076 }
5077 }
5078
5079 mJumpyTouchFilter.jumpyPointsDropped = 0;
5080 return false;
5081}
5082
5083/* Special hack for devices that have bad screen data: aggregate and
5084 * compute averages of the coordinate data, to reduce the amount of
5085 * jitter seen by applications. */
5086void TouchInputMapper::applyAveragingTouchFilter() {
5087 for (uint32_t currentIndex = 0; currentIndex < mCurrentTouch.pointerCount; currentIndex++) {
5088 uint32_t id = mCurrentTouch.pointers[currentIndex].id;
5089 int32_t x = mCurrentTouch.pointers[currentIndex].x;
5090 int32_t y = mCurrentTouch.pointers[currentIndex].y;
Jeff Brown8d608662010-08-30 03:02:23 -07005091 int32_t pressure;
5092 switch (mCalibration.pressureSource) {
5093 case Calibration::PRESSURE_SOURCE_PRESSURE:
5094 pressure = mCurrentTouch.pointers[currentIndex].pressure;
5095 break;
5096 case Calibration::PRESSURE_SOURCE_TOUCH:
5097 pressure = mCurrentTouch.pointers[currentIndex].touchMajor;
5098 break;
5099 default:
5100 pressure = 1;
5101 break;
5102 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005103
5104 if (mLastTouch.idBits.hasBit(id)) {
5105 // Pointer was down before and is still down now.
5106 // Compute average over history trace.
5107 uint32_t start = mAveragingTouchFilter.historyStart[id];
5108 uint32_t end = mAveragingTouchFilter.historyEnd[id];
5109
5110 int64_t deltaX = x - mAveragingTouchFilter.historyData[end].pointers[id].x;
5111 int64_t deltaY = y - mAveragingTouchFilter.historyData[end].pointers[id].y;
5112 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5113
5114#if DEBUG_HACKS
5115 LOGD("AveragingTouchFilter: Pointer id %d - Distance from last sample: %lld",
5116 id, distance);
5117#endif
5118
5119 if (distance < AVERAGING_DISTANCE_LIMIT) {
5120 // Increment end index in preparation for recording new historical data.
5121 end += 1;
5122 if (end > AVERAGING_HISTORY_SIZE) {
5123 end = 0;
5124 }
5125
5126 // If the end index has looped back to the start index then we have filled
5127 // the historical trace up to the desired size so we drop the historical
5128 // data at the start of the trace.
5129 if (end == start) {
5130 start += 1;
5131 if (start > AVERAGING_HISTORY_SIZE) {
5132 start = 0;
5133 }
5134 }
5135
5136 // Add the raw data to the historical trace.
5137 mAveragingTouchFilter.historyStart[id] = start;
5138 mAveragingTouchFilter.historyEnd[id] = end;
5139 mAveragingTouchFilter.historyData[end].pointers[id].x = x;
5140 mAveragingTouchFilter.historyData[end].pointers[id].y = y;
5141 mAveragingTouchFilter.historyData[end].pointers[id].pressure = pressure;
5142
5143 // Average over all historical positions in the trace by total pressure.
5144 int32_t averagedX = 0;
5145 int32_t averagedY = 0;
5146 int32_t totalPressure = 0;
5147 for (;;) {
5148 int32_t historicalX = mAveragingTouchFilter.historyData[start].pointers[id].x;
5149 int32_t historicalY = mAveragingTouchFilter.historyData[start].pointers[id].y;
5150 int32_t historicalPressure = mAveragingTouchFilter.historyData[start]
5151 .pointers[id].pressure;
5152
5153 averagedX += historicalX * historicalPressure;
5154 averagedY += historicalY * historicalPressure;
5155 totalPressure += historicalPressure;
5156
5157 if (start == end) {
5158 break;
5159 }
5160
5161 start += 1;
5162 if (start > AVERAGING_HISTORY_SIZE) {
5163 start = 0;
5164 }
5165 }
5166
Jeff Brown8d608662010-08-30 03:02:23 -07005167 if (totalPressure != 0) {
5168 averagedX /= totalPressure;
5169 averagedY /= totalPressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005170
5171#if DEBUG_HACKS
Jeff Brown8d608662010-08-30 03:02:23 -07005172 LOGD("AveragingTouchFilter: Pointer id %d - "
5173 "totalPressure=%d, averagedX=%d, averagedY=%d", id, totalPressure,
5174 averagedX, averagedY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005175#endif
5176
Jeff Brown8d608662010-08-30 03:02:23 -07005177 mCurrentTouch.pointers[currentIndex].x = averagedX;
5178 mCurrentTouch.pointers[currentIndex].y = averagedY;
5179 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005180 } else {
5181#if DEBUG_HACKS
5182 LOGD("AveragingTouchFilter: Pointer id %d - Exceeded max distance", id);
5183#endif
5184 }
5185 } else {
5186#if DEBUG_HACKS
5187 LOGD("AveragingTouchFilter: Pointer id %d - Pointer went up", id);
5188#endif
5189 }
5190
5191 // Reset pointer history.
5192 mAveragingTouchFilter.historyStart[id] = 0;
5193 mAveragingTouchFilter.historyEnd[id] = 0;
5194 mAveragingTouchFilter.historyData[0].pointers[id].x = x;
5195 mAveragingTouchFilter.historyData[0].pointers[id].y = y;
5196 mAveragingTouchFilter.historyData[0].pointers[id].pressure = pressure;
5197 }
5198}
5199
5200int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07005201 { // acquire lock
5202 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005203
Jeff Brown6328cdc2010-07-29 18:18:33 -07005204 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005205 return AKEY_STATE_VIRTUAL;
5206 }
5207
Jeff Brown6328cdc2010-07-29 18:18:33 -07005208 size_t numVirtualKeys = mLocked.virtualKeys.size();
5209 for (size_t i = 0; i < numVirtualKeys; i++) {
5210 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005211 if (virtualKey.keyCode == keyCode) {
5212 return AKEY_STATE_UP;
5213 }
5214 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07005215 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07005216
5217 return AKEY_STATE_UNKNOWN;
5218}
5219
5220int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07005221 { // acquire lock
5222 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005223
Jeff Brown6328cdc2010-07-29 18:18:33 -07005224 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005225 return AKEY_STATE_VIRTUAL;
5226 }
5227
Jeff Brown6328cdc2010-07-29 18:18:33 -07005228 size_t numVirtualKeys = mLocked.virtualKeys.size();
5229 for (size_t i = 0; i < numVirtualKeys; i++) {
5230 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005231 if (virtualKey.scanCode == scanCode) {
5232 return AKEY_STATE_UP;
5233 }
5234 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07005235 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07005236
5237 return AKEY_STATE_UNKNOWN;
5238}
5239
5240bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5241 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07005242 { // acquire lock
5243 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005244
Jeff Brown6328cdc2010-07-29 18:18:33 -07005245 size_t numVirtualKeys = mLocked.virtualKeys.size();
5246 for (size_t i = 0; i < numVirtualKeys; i++) {
5247 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005248
5249 for (size_t i = 0; i < numCodes; i++) {
5250 if (virtualKey.keyCode == keyCodes[i]) {
5251 outFlags[i] = 1;
5252 }
5253 }
5254 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07005255 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07005256
5257 return true;
5258}
5259
5260
5261// --- SingleTouchInputMapper ---
5262
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005263SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5264 TouchInputMapper(device) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005265 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005266}
5267
5268SingleTouchInputMapper::~SingleTouchInputMapper() {
5269}
5270
Jeff Brown80fd47c2011-05-24 01:07:44 -07005271void SingleTouchInputMapper::clearState() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005272 mAccumulator.clear();
5273
5274 mDown = false;
5275 mX = 0;
5276 mY = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07005277 mPressure = 0; // default to 0 for devices that don't report pressure
5278 mToolWidth = 0; // default to 0 for devices that don't report tool width
Jeff Brownace13b12011-03-09 17:39:48 -08005279 mButtonState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005280}
5281
5282void SingleTouchInputMapper::reset() {
5283 TouchInputMapper::reset();
5284
Jeff Brown80fd47c2011-05-24 01:07:44 -07005285 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005286 }
5287
5288void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
5289 switch (rawEvent->type) {
5290 case EV_KEY:
5291 switch (rawEvent->scanCode) {
5292 case BTN_TOUCH:
5293 mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH;
5294 mAccumulator.btnTouch = rawEvent->value != 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005295 // Don't sync immediately. Wait until the next SYN_REPORT since we might
5296 // not have received valid position information yet. This logic assumes that
5297 // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet.
Jeff Brown6d0fec22010-07-23 21:28:06 -07005298 break;
Jeff Brownace13b12011-03-09 17:39:48 -08005299 default:
5300 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005301 int32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode);
Jeff Brownace13b12011-03-09 17:39:48 -08005302 if (buttonState) {
5303 if (rawEvent->value) {
5304 mAccumulator.buttonDown |= buttonState;
5305 } else {
5306 mAccumulator.buttonUp |= buttonState;
5307 }
5308 mAccumulator.fields |= Accumulator::FIELD_BUTTONS;
5309 }
5310 }
5311 break;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005312 }
5313 break;
5314
5315 case EV_ABS:
5316 switch (rawEvent->scanCode) {
5317 case ABS_X:
5318 mAccumulator.fields |= Accumulator::FIELD_ABS_X;
5319 mAccumulator.absX = rawEvent->value;
5320 break;
5321 case ABS_Y:
5322 mAccumulator.fields |= Accumulator::FIELD_ABS_Y;
5323 mAccumulator.absY = rawEvent->value;
5324 break;
5325 case ABS_PRESSURE:
5326 mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE;
5327 mAccumulator.absPressure = rawEvent->value;
5328 break;
5329 case ABS_TOOL_WIDTH:
5330 mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH;
5331 mAccumulator.absToolWidth = rawEvent->value;
5332 break;
5333 }
5334 break;
5335
5336 case EV_SYN:
5337 switch (rawEvent->scanCode) {
5338 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005339 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005340 break;
5341 }
5342 break;
5343 }
5344}
5345
5346void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005347 uint32_t fields = mAccumulator.fields;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005348 if (fields == 0) {
5349 return; // no new state changes, so nothing to do
5350 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005351
5352 if (fields & Accumulator::FIELD_BTN_TOUCH) {
5353 mDown = mAccumulator.btnTouch;
5354 }
5355
5356 if (fields & Accumulator::FIELD_ABS_X) {
5357 mX = mAccumulator.absX;
5358 }
5359
5360 if (fields & Accumulator::FIELD_ABS_Y) {
5361 mY = mAccumulator.absY;
5362 }
5363
5364 if (fields & Accumulator::FIELD_ABS_PRESSURE) {
5365 mPressure = mAccumulator.absPressure;
5366 }
5367
5368 if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) {
Jeff Brown8d608662010-08-30 03:02:23 -07005369 mToolWidth = mAccumulator.absToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005370 }
5371
Jeff Brownace13b12011-03-09 17:39:48 -08005372 if (fields & Accumulator::FIELD_BUTTONS) {
5373 mButtonState = (mButtonState | mAccumulator.buttonDown) & ~mAccumulator.buttonUp;
5374 }
5375
Jeff Brown6d0fec22010-07-23 21:28:06 -07005376 mCurrentTouch.clear();
5377
5378 if (mDown) {
5379 mCurrentTouch.pointerCount = 1;
5380 mCurrentTouch.pointers[0].id = 0;
5381 mCurrentTouch.pointers[0].x = mX;
5382 mCurrentTouch.pointers[0].y = mY;
5383 mCurrentTouch.pointers[0].pressure = mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -07005384 mCurrentTouch.pointers[0].touchMajor = 0;
5385 mCurrentTouch.pointers[0].touchMinor = 0;
5386 mCurrentTouch.pointers[0].toolMajor = mToolWidth;
5387 mCurrentTouch.pointers[0].toolMinor = mToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005388 mCurrentTouch.pointers[0].orientation = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005389 mCurrentTouch.pointers[0].distance = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005390 mCurrentTouch.pointers[0].isStylus = false; // TODO: Set stylus
Jeff Brown6d0fec22010-07-23 21:28:06 -07005391 mCurrentTouch.idToIndex[0] = 0;
5392 mCurrentTouch.idBits.markBit(0);
Jeff Brownace13b12011-03-09 17:39:48 -08005393 mCurrentTouch.buttonState = mButtonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005394 }
5395
5396 syncTouch(when, true);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005397
5398 mAccumulator.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005399}
5400
Jeff Brown8d608662010-08-30 03:02:23 -07005401void SingleTouchInputMapper::configureRawAxes() {
5402 TouchInputMapper::configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005403
Jeff Brown8d608662010-08-30 03:02:23 -07005404 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x);
5405 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y);
5406 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure);
5407 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005408}
5409
5410
5411// --- MultiTouchInputMapper ---
5412
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005413MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown80fd47c2011-05-24 01:07:44 -07005414 TouchInputMapper(device), mSlotCount(0), mUsingSlotsProtocol(false) {
5415 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005416}
5417
5418MultiTouchInputMapper::~MultiTouchInputMapper() {
5419}
5420
Jeff Brown80fd47c2011-05-24 01:07:44 -07005421void MultiTouchInputMapper::clearState() {
Jeff Brown441a9c22011-06-02 18:22:25 -07005422 mAccumulator.clearSlots(mSlotCount);
5423 mAccumulator.clearButtons();
Jeff Brownace13b12011-03-09 17:39:48 -08005424 mButtonState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005425}
5426
5427void MultiTouchInputMapper::reset() {
5428 TouchInputMapper::reset();
5429
Jeff Brown80fd47c2011-05-24 01:07:44 -07005430 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005431}
5432
5433void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
5434 switch (rawEvent->type) {
Jeff Brownace13b12011-03-09 17:39:48 -08005435 case EV_KEY: {
5436 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005437 int32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode);
Jeff Brownace13b12011-03-09 17:39:48 -08005438 if (buttonState) {
5439 if (rawEvent->value) {
5440 mAccumulator.buttonDown |= buttonState;
5441 } else {
5442 mAccumulator.buttonUp |= buttonState;
5443 }
5444 }
5445 }
5446 break;
5447 }
5448
Jeff Brown6d0fec22010-07-23 21:28:06 -07005449 case EV_ABS: {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005450 bool newSlot = false;
5451 if (mUsingSlotsProtocol && rawEvent->scanCode == ABS_MT_SLOT) {
5452 mAccumulator.currentSlot = rawEvent->value;
5453 newSlot = true;
5454 }
5455
5456 if (mAccumulator.currentSlot < 0 || size_t(mAccumulator.currentSlot) >= mSlotCount) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005457#if DEBUG_POINTERS
Jeff Brown441a9c22011-06-02 18:22:25 -07005458 if (newSlot) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005459 LOGW("MultiTouch device %s emitted invalid slot index %d but it "
5460 "should be between 0 and %d; ignoring this slot.",
5461 getDeviceName().string(), mAccumulator.currentSlot, mSlotCount);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005462 }
Jeff Brown441a9c22011-06-02 18:22:25 -07005463#endif
Jeff Brown80fd47c2011-05-24 01:07:44 -07005464 break;
5465 }
5466
5467 Accumulator::Slot* slot = &mAccumulator.slots[mAccumulator.currentSlot];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005468
5469 switch (rawEvent->scanCode) {
5470 case ABS_MT_POSITION_X:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005471 slot->fields |= Accumulator::FIELD_ABS_MT_POSITION_X;
5472 slot->absMTPositionX = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005473 break;
5474 case ABS_MT_POSITION_Y:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005475 slot->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y;
5476 slot->absMTPositionY = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005477 break;
5478 case ABS_MT_TOUCH_MAJOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005479 slot->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR;
5480 slot->absMTTouchMajor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005481 break;
5482 case ABS_MT_TOUCH_MINOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005483 slot->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR;
5484 slot->absMTTouchMinor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005485 break;
5486 case ABS_MT_WIDTH_MAJOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005487 slot->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR;
5488 slot->absMTWidthMajor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005489 break;
5490 case ABS_MT_WIDTH_MINOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005491 slot->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR;
5492 slot->absMTWidthMinor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005493 break;
5494 case ABS_MT_ORIENTATION:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005495 slot->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION;
5496 slot->absMTOrientation = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005497 break;
5498 case ABS_MT_TRACKING_ID:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005499 if (mUsingSlotsProtocol && rawEvent->value < 0) {
5500 slot->clear();
5501 } else {
5502 slot->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID;
5503 slot->absMTTrackingId = rawEvent->value;
5504 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005505 break;
Jeff Brown8d608662010-08-30 03:02:23 -07005506 case ABS_MT_PRESSURE:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005507 slot->fields |= Accumulator::FIELD_ABS_MT_PRESSURE;
5508 slot->absMTPressure = rawEvent->value;
5509 break;
5510 case ABS_MT_TOOL_TYPE:
5511 slot->fields |= Accumulator::FIELD_ABS_MT_TOOL_TYPE;
5512 slot->absMTToolType = rawEvent->value;
Jeff Brown8d608662010-08-30 03:02:23 -07005513 break;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005514 }
5515 break;
5516 }
5517
5518 case EV_SYN:
5519 switch (rawEvent->scanCode) {
5520 case SYN_MT_REPORT: {
5521 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
Jeff Brown80fd47c2011-05-24 01:07:44 -07005522 mAccumulator.currentSlot += 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005523 break;
5524 }
5525
5526 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005527 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005528 break;
5529 }
5530 break;
5531 }
5532}
5533
5534void MultiTouchInputMapper::sync(nsecs_t when) {
5535 static const uint32_t REQUIRED_FIELDS =
Jeff Brown8d608662010-08-30 03:02:23 -07005536 Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005537
Jeff Brown80fd47c2011-05-24 01:07:44 -07005538 size_t inCount = mSlotCount;
5539 size_t outCount = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005540 bool havePointerIds = true;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005541
Jeff Brown6d0fec22010-07-23 21:28:06 -07005542 mCurrentTouch.clear();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005543
Jeff Brown80fd47c2011-05-24 01:07:44 -07005544 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
5545 const Accumulator::Slot& inSlot = mAccumulator.slots[inIndex];
5546 uint32_t fields = inSlot.fields;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005547
Jeff Brown6d0fec22010-07-23 21:28:06 -07005548 if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005549 // Some drivers send empty MT sync packets without X / Y to indicate a pointer up.
Jeff Brown80fd47c2011-05-24 01:07:44 -07005550 // This may also indicate an unused slot.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005551 // Drop this finger.
Jeff Brown46b9ac02010-04-22 18:58:52 -07005552 continue;
5553 }
5554
Jeff Brown80fd47c2011-05-24 01:07:44 -07005555 if (outCount >= MAX_POINTERS) {
5556#if DEBUG_POINTERS
5557 LOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
5558 "ignoring the rest.",
5559 getDeviceName().string(), MAX_POINTERS);
5560#endif
5561 break; // too many fingers!
5562 }
5563
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005564 PointerData& outPointer = mCurrentTouch.pointers[outCount];
Jeff Brown80fd47c2011-05-24 01:07:44 -07005565 outPointer.x = inSlot.absMTPositionX;
5566 outPointer.y = inSlot.absMTPositionY;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005567
Jeff Brown8d608662010-08-30 03:02:23 -07005568 if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005569 outPointer.pressure = inSlot.absMTPressure;
Jeff Brown8d608662010-08-30 03:02:23 -07005570 } else {
5571 // Default pressure to 0 if absent.
5572 outPointer.pressure = 0;
5573 }
5574
5575 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005576 if (inSlot.absMTTouchMajor <= 0) {
Jeff Brown8d608662010-08-30 03:02:23 -07005577 // Some devices send sync packets with X / Y but with a 0 touch major to indicate
5578 // a pointer going up. Drop this finger.
5579 continue;
5580 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07005581 outPointer.touchMajor = inSlot.absMTTouchMajor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005582 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005583 // Default touch area to 0 if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005584 outPointer.touchMajor = 0;
5585 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005586
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005587 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005588 outPointer.touchMinor = inSlot.absMTTouchMinor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005589 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005590 // Assume touch area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005591 outPointer.touchMinor = outPointer.touchMajor;
5592 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005593
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005594 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005595 outPointer.toolMajor = inSlot.absMTWidthMajor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005596 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005597 // Default tool area to 0 if absent.
5598 outPointer.toolMajor = 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005599 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005600
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005601 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005602 outPointer.toolMinor = inSlot.absMTWidthMinor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005603 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005604 // Assume tool area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005605 outPointer.toolMinor = outPointer.toolMajor;
5606 }
5607
5608 if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005609 outPointer.orientation = inSlot.absMTOrientation;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005610 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005611 // Default orientation to vertical if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005612 outPointer.orientation = 0;
5613 }
5614
Jeff Brown80fd47c2011-05-24 01:07:44 -07005615 if (fields & Accumulator::FIELD_ABS_MT_DISTANCE) {
5616 outPointer.distance = inSlot.absMTDistance;
5617 } else {
5618 // Default distance is 0 (direct contact).
5619 outPointer.distance = 0;
5620 }
5621
5622 if (fields & Accumulator::FIELD_ABS_MT_TOOL_TYPE) {
5623 outPointer.isStylus = (inSlot.absMTToolType == MT_TOOL_PEN);
5624 } else {
5625 // Assume this is not a stylus.
5626 outPointer.isStylus = false;
5627 }
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005628
Jeff Brown8d608662010-08-30 03:02:23 -07005629 // Assign pointer id using tracking id if available.
Jeff Brown6d0fec22010-07-23 21:28:06 -07005630 if (havePointerIds) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005631 int32_t id;
5632 if (mUsingSlotsProtocol) {
5633 id = inIndex;
5634 } else if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) {
5635 id = inSlot.absMTTrackingId;
5636 } else {
5637 id = -1;
5638 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005639
Jeff Brown80fd47c2011-05-24 01:07:44 -07005640 if (id >= 0 && id <= MAX_POINTER_ID) {
5641 outPointer.id = id;
5642 mCurrentTouch.idToIndex[id] = outCount;
5643 mCurrentTouch.idBits.markBit(id);
5644 } else {
5645 if (id >= 0) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005646#if DEBUG_POINTERS
Jeff Brown80fd47c2011-05-24 01:07:44 -07005647 LOGD("Pointers: Ignoring driver provided slot index or tracking id %d because "
5648 "it is larger than the maximum supported pointer id %d",
Jeff Brown6d0fec22010-07-23 21:28:06 -07005649 id, MAX_POINTER_ID);
5650#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005651 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005652 havePointerIds = false;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005653 }
5654 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07005655
Jeff Brown6d0fec22010-07-23 21:28:06 -07005656 outCount += 1;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005657 }
5658
Jeff Brown6d0fec22010-07-23 21:28:06 -07005659 mCurrentTouch.pointerCount = outCount;
Jeff Brown46b9ac02010-04-22 18:58:52 -07005660
Jeff Brownace13b12011-03-09 17:39:48 -08005661 mButtonState = (mButtonState | mAccumulator.buttonDown) & ~mAccumulator.buttonUp;
5662 mCurrentTouch.buttonState = mButtonState;
5663
Jeff Brown6d0fec22010-07-23 21:28:06 -07005664 syncTouch(when, havePointerIds);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005665
Jeff Brown441a9c22011-06-02 18:22:25 -07005666 if (!mUsingSlotsProtocol) {
5667 mAccumulator.clearSlots(mSlotCount);
5668 }
5669 mAccumulator.clearButtons();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005670}
5671
Jeff Brown8d608662010-08-30 03:02:23 -07005672void MultiTouchInputMapper::configureRawAxes() {
5673 TouchInputMapper::configureRawAxes();
Jeff Brown46b9ac02010-04-22 18:58:52 -07005674
Jeff Brown80fd47c2011-05-24 01:07:44 -07005675 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, &mRawAxes.x);
5676 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, &mRawAxes.y);
5677 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, &mRawAxes.touchMajor);
5678 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, &mRawAxes.touchMinor);
5679 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, &mRawAxes.toolMajor);
5680 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, &mRawAxes.toolMinor);
5681 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, &mRawAxes.orientation);
5682 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, &mRawAxes.pressure);
5683 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_DISTANCE, &mRawAxes.distance);
5684 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TRACKING_ID, &mRawAxes.trackingId);
5685 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_SLOT, &mRawAxes.slot);
5686
5687 if (mRawAxes.trackingId.valid
5688 && mRawAxes.slot.valid && mRawAxes.slot.minValue == 0 && mRawAxes.slot.maxValue > 0) {
5689 mSlotCount = mRawAxes.slot.maxValue + 1;
5690 if (mSlotCount > MAX_SLOTS) {
5691 LOGW("MultiTouch Device %s reported %d slots but the framework "
5692 "only supports a maximum of %d slots at this time.",
5693 getDeviceName().string(), mSlotCount, MAX_SLOTS);
5694 mSlotCount = MAX_SLOTS;
5695 }
5696 mUsingSlotsProtocol = true;
5697 } else {
5698 mSlotCount = MAX_POINTERS;
5699 mUsingSlotsProtocol = false;
5700 }
5701
5702 mAccumulator.allocateSlots(mSlotCount);
Jeff Brown9c3cda02010-06-15 01:31:58 -07005703}
5704
Jeff Brown46b9ac02010-04-22 18:58:52 -07005705
Jeff Browncb1404e2011-01-15 18:14:15 -08005706// --- JoystickInputMapper ---
5707
5708JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5709 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005710}
5711
5712JoystickInputMapper::~JoystickInputMapper() {
5713}
5714
5715uint32_t JoystickInputMapper::getSources() {
5716 return AINPUT_SOURCE_JOYSTICK;
5717}
5718
5719void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5720 InputMapper::populateDeviceInfo(info);
5721
Jeff Brown6f2fba42011-02-19 01:08:02 -08005722 for (size_t i = 0; i < mAxes.size(); i++) {
5723 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005724 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5725 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005726 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005727 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5728 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005729 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005730 }
5731}
5732
5733void JoystickInputMapper::dump(String8& dump) {
5734 dump.append(INDENT2 "Joystick Input Mapper:\n");
5735
Jeff Brown6f2fba42011-02-19 01:08:02 -08005736 dump.append(INDENT3 "Axes:\n");
5737 size_t numAxes = mAxes.size();
5738 for (size_t i = 0; i < numAxes; i++) {
5739 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005740 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005741 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005742 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005743 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005744 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005745 }
Jeff Brown85297452011-03-04 13:07:49 -08005746 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5747 label = getAxisLabel(axis.axisInfo.highAxis);
5748 if (label) {
5749 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5750 } else {
5751 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5752 axis.axisInfo.splitValue);
5753 }
5754 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5755 dump.append(" (invert)");
5756 }
5757
5758 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5759 axis.min, axis.max, axis.flat, axis.fuzz);
5760 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5761 "highScale=%0.5f, highOffset=%0.5f\n",
5762 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005763 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, rawFlat=%d, rawFuzz=%d\n",
5764 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
5765 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz);
Jeff Browncb1404e2011-01-15 18:14:15 -08005766 }
5767}
5768
5769void JoystickInputMapper::configure() {
5770 InputMapper::configure();
5771
Jeff Brown6f2fba42011-02-19 01:08:02 -08005772 // Collect all axes.
5773 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
5774 RawAbsoluteAxisInfo rawAxisInfo;
5775 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), abs, &rawAxisInfo);
5776 if (rawAxisInfo.valid) {
Jeff Brown85297452011-03-04 13:07:49 -08005777 // Map axis.
5778 AxisInfo axisInfo;
5779 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005780 if (!explicitlyMapped) {
5781 // Axis is not explicitly mapped, will choose a generic axis later.
Jeff Brown85297452011-03-04 13:07:49 -08005782 axisInfo.mode = AxisInfo::MODE_NORMAL;
5783 axisInfo.axis = -1;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005784 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005785
Jeff Brown85297452011-03-04 13:07:49 -08005786 // Apply flat override.
5787 int32_t rawFlat = axisInfo.flatOverride < 0
5788 ? rawAxisInfo.flat : axisInfo.flatOverride;
5789
5790 // Calculate scaling factors and limits.
Jeff Brown6f2fba42011-02-19 01:08:02 -08005791 Axis axis;
Jeff Brown85297452011-03-04 13:07:49 -08005792 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5793 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5794 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5795 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5796 scale, 0.0f, highScale, 0.0f,
5797 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5798 } else if (isCenteredAxis(axisInfo.axis)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005799 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5800 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
Jeff Brown85297452011-03-04 13:07:49 -08005801 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5802 scale, offset, scale, offset,
5803 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005804 } else {
5805 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
Jeff Brown85297452011-03-04 13:07:49 -08005806 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5807 scale, 0.0f, scale, 0.0f,
5808 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005809 }
5810
5811 // To eliminate noise while the joystick is at rest, filter out small variations
5812 // in axis values up front.
5813 axis.filter = axis.flat * 0.25f;
5814
5815 mAxes.add(abs, axis);
5816 }
5817 }
5818
5819 // If there are too many axes, start dropping them.
5820 // Prefer to keep explicitly mapped axes.
5821 if (mAxes.size() > PointerCoords::MAX_AXES) {
5822 LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
5823 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5824 pruneAxes(true);
5825 pruneAxes(false);
5826 }
5827
5828 // Assign generic axis ids to remaining axes.
5829 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5830 size_t numAxes = mAxes.size();
5831 for (size_t i = 0; i < numAxes; i++) {
5832 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005833 if (axis.axisInfo.axis < 0) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005834 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5835 && haveAxis(nextGenericAxisId)) {
5836 nextGenericAxisId += 1;
5837 }
5838
5839 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
Jeff Brown85297452011-03-04 13:07:49 -08005840 axis.axisInfo.axis = nextGenericAxisId;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005841 nextGenericAxisId += 1;
5842 } else {
5843 LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
5844 "have already been assigned to other axes.",
5845 getDeviceName().string(), mAxes.keyAt(i));
5846 mAxes.removeItemsAt(i--);
5847 numAxes -= 1;
5848 }
5849 }
5850 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005851}
5852
Jeff Brown85297452011-03-04 13:07:49 -08005853bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005854 size_t numAxes = mAxes.size();
5855 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005856 const Axis& axis = mAxes.valueAt(i);
5857 if (axis.axisInfo.axis == axisId
5858 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5859 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005860 return true;
5861 }
5862 }
5863 return false;
5864}
Jeff Browncb1404e2011-01-15 18:14:15 -08005865
Jeff Brown6f2fba42011-02-19 01:08:02 -08005866void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5867 size_t i = mAxes.size();
5868 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5869 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5870 continue;
5871 }
5872 LOGI("Discarding joystick '%s' axis %d because there are too many axes.",
5873 getDeviceName().string(), mAxes.keyAt(i));
5874 mAxes.removeItemsAt(i);
5875 }
5876}
5877
5878bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5879 switch (axis) {
5880 case AMOTION_EVENT_AXIS_X:
5881 case AMOTION_EVENT_AXIS_Y:
5882 case AMOTION_EVENT_AXIS_Z:
5883 case AMOTION_EVENT_AXIS_RX:
5884 case AMOTION_EVENT_AXIS_RY:
5885 case AMOTION_EVENT_AXIS_RZ:
5886 case AMOTION_EVENT_AXIS_HAT_X:
5887 case AMOTION_EVENT_AXIS_HAT_Y:
5888 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005889 case AMOTION_EVENT_AXIS_RUDDER:
5890 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005891 return true;
5892 default:
5893 return false;
5894 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005895}
5896
5897void JoystickInputMapper::reset() {
5898 // Recenter all axes.
5899 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Browncb1404e2011-01-15 18:14:15 -08005900
Jeff Brown6f2fba42011-02-19 01:08:02 -08005901 size_t numAxes = mAxes.size();
5902 for (size_t i = 0; i < numAxes; i++) {
5903 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005904 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005905 }
5906
5907 sync(when, true /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005908
5909 InputMapper::reset();
5910}
5911
5912void JoystickInputMapper::process(const RawEvent* rawEvent) {
5913 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005914 case EV_ABS: {
5915 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
5916 if (index >= 0) {
5917 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005918 float newValue, highNewValue;
5919 switch (axis.axisInfo.mode) {
5920 case AxisInfo::MODE_INVERT:
5921 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5922 * axis.scale + axis.offset;
5923 highNewValue = 0.0f;
5924 break;
5925 case AxisInfo::MODE_SPLIT:
5926 if (rawEvent->value < axis.axisInfo.splitValue) {
5927 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5928 * axis.scale + axis.offset;
5929 highNewValue = 0.0f;
5930 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5931 newValue = 0.0f;
5932 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5933 * axis.highScale + axis.highOffset;
5934 } else {
5935 newValue = 0.0f;
5936 highNewValue = 0.0f;
5937 }
5938 break;
5939 default:
5940 newValue = rawEvent->value * axis.scale + axis.offset;
5941 highNewValue = 0.0f;
5942 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005943 }
Jeff Brown85297452011-03-04 13:07:49 -08005944 axis.newValue = newValue;
5945 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005946 }
5947 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005948 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005949
5950 case EV_SYN:
5951 switch (rawEvent->scanCode) {
5952 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005953 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005954 break;
5955 }
5956 break;
5957 }
5958}
5959
Jeff Brown6f2fba42011-02-19 01:08:02 -08005960void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005961 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005962 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005963 }
5964
5965 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005966 int32_t buttonState = 0;
5967
5968 PointerProperties pointerProperties;
5969 pointerProperties.clear();
5970 pointerProperties.id = 0;
5971 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08005972
Jeff Brown6f2fba42011-02-19 01:08:02 -08005973 PointerCoords pointerCoords;
5974 pointerCoords.clear();
5975
5976 size_t numAxes = mAxes.size();
5977 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005978 const Axis& axis = mAxes.valueAt(i);
5979 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
5980 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5981 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
5982 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005983 }
5984
Jeff Brown56194eb2011-03-02 19:23:13 -08005985 // Moving a joystick axis should not wake the devide because joysticks can
5986 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
5987 // button will likely wake the device.
5988 // TODO: Use the input device configuration to control this behavior more finely.
5989 uint32_t policyFlags = 0;
5990
Jeff Brown56194eb2011-03-02 19:23:13 -08005991 getDispatcher()->notifyMotion(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005992 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
5993 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Browncb1404e2011-01-15 18:14:15 -08005994}
5995
Jeff Brown85297452011-03-04 13:07:49 -08005996bool JoystickInputMapper::filterAxes(bool force) {
5997 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005998 size_t numAxes = mAxes.size();
5999 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006000 Axis& axis = mAxes.editValueAt(i);
6001 if (force || hasValueChangedSignificantly(axis.filter,
6002 axis.newValue, axis.currentValue, axis.min, axis.max)) {
6003 axis.currentValue = axis.newValue;
6004 atLeastOneSignificantChange = true;
6005 }
6006 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6007 if (force || hasValueChangedSignificantly(axis.filter,
6008 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
6009 axis.highCurrentValue = axis.highNewValue;
6010 atLeastOneSignificantChange = true;
6011 }
6012 }
6013 }
6014 return atLeastOneSignificantChange;
6015}
6016
6017bool JoystickInputMapper::hasValueChangedSignificantly(
6018 float filter, float newValue, float currentValue, float min, float max) {
6019 if (newValue != currentValue) {
6020 // Filter out small changes in value unless the value is converging on the axis
6021 // bounds or center point. This is intended to reduce the amount of information
6022 // sent to applications by particularly noisy joysticks (such as PS3).
6023 if (fabs(newValue - currentValue) > filter
6024 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
6025 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
6026 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
6027 return true;
6028 }
6029 }
6030 return false;
6031}
6032
6033bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
6034 float filter, float newValue, float currentValue, float thresholdValue) {
6035 float newDistance = fabs(newValue - thresholdValue);
6036 if (newDistance < filter) {
6037 float oldDistance = fabs(currentValue - thresholdValue);
6038 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006039 return true;
6040 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006041 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006042 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006043}
6044
Jeff Brown46b9ac02010-04-22 18:58:52 -07006045} // namespace android