blob: c42e3abcd4f36df2fa14a7129d0ba97bd782fd51 [file] [log] [blame]
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070017#define LOG_TAG "InputReader"
18
19//#define LOG_NDEBUG 0
20
21// Log debug messages for each raw event received from the EventHub.
22#define DEBUG_RAW_EVENTS 0
23
24// Log debug messages about touch screen filtering hacks.
Jeff Brown349703e2010-06-22 01:27:15 -070025#define DEBUG_HACKS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070026
27// Log debug messages about virtual key processing.
Jeff Brown349703e2010-06-22 01:27:15 -070028#define DEBUG_VIRTUAL_KEYS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070029
30// Log debug messages about pointers.
Jeff Brown9f2106f2011-05-24 14:40:35 -070031#define DEBUG_POINTERS 0
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070032
Jeff Brown5c225b12010-06-16 01:53:36 -070033// Log debug messages about pointer assignment calculations.
34#define DEBUG_POINTER_ASSIGNMENT 0
35
Jeff Brownace13b12011-03-09 17:39:48 -080036// Log debug messages about gesture detection.
37#define DEBUG_GESTURES 0
38
Jeff Brownb4ff35d2011-01-02 16:37:43 -080039#include "InputReader.h"
40
Jeff Brown1a84fd12011-06-02 01:26:32 -070041#include <cutils/atomic.h>
Jeff Brown46b9ac0a2010-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 Brown46b9ac0a2010-04-22 18:58:52 -070045
46#include <stddef.h>
Jeff Brown8d608662010-08-30 03:02:23 -070047#include <stdlib.h>
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070048#include <unistd.h>
Jeff Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-04-22 18:58:52 -070058namespace android {
59
Jeff Brownace13b12011-03-09 17:39:48 -080060// --- Constants ---
61
Jeff Brown80fd47c2011-05-24 01:07:44 -070062// Maximum number of slots supported when using the slot-based Multitouch Protocol B.
63static const size_t MAX_SLOTS = 32;
64
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070065// --- Static Functions ---
66
67template<typename T>
68inline static T abs(const T& value) {
69 return value < 0 ? - value : value;
70}
71
72template<typename T>
73inline static T min(const T& a, const T& b) {
74 return a < b ? a : b;
75}
76
Jeff Brown5c225b12010-06-16 01:53:36 -070077template<typename T>
78inline static void swap(T& a, T& b) {
79 T temp = a;
80 a = b;
81 b = temp;
82}
83
Jeff Brown8d608662010-08-30 03:02:23 -070084inline static float avg(float x, float y) {
85 return (x + y) / 2;
86}
87
Jeff Brown2352b972011-04-12 22:39:53 -070088inline static float distance(float x1, float y1, float x2, float y2) {
89 return hypotf(x1 - x2, y1 - y2);
Jeff Brownace13b12011-03-09 17:39:48 -080090}
91
Jeff Brown517bb4c2011-01-14 19:09:23 -080092inline static int32_t signExtendNybble(int32_t value) {
93 return value >= 8 ? value - 16 : value;
94}
95
Jeff Brownef3d7e82010-09-30 14:33:04 -070096static inline const char* toString(bool value) {
97 return value ? "true" : "false";
98}
99
Jeff Brown9626b142011-03-03 02:09:54 -0800100static int32_t rotateValueUsingRotationMap(int32_t value, int32_t orientation,
101 const int32_t map[][4], size_t mapSize) {
102 if (orientation != DISPLAY_ORIENTATION_0) {
103 for (size_t i = 0; i < mapSize; i++) {
104 if (value == map[i][0]) {
105 return map[i][orientation];
106 }
107 }
108 }
109 return value;
110}
111
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700112static const int32_t keyCodeRotationMap[][4] = {
113 // key codes enumerated counter-clockwise with the original (unrotated) key first
114 // no rotation, 90 degree rotation, 180 degree rotation, 270 degree rotation
Jeff Brownfd0358292010-06-30 16:10:35 -0700115 { AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT },
116 { AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN },
117 { AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT },
118 { AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_UP },
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700119};
Jeff Brown9626b142011-03-03 02:09:54 -0800120static const size_t keyCodeRotationMapSize =
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700121 sizeof(keyCodeRotationMap) / sizeof(keyCodeRotationMap[0]);
122
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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-04-22 18:58:52 -0700257
Jeff Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-04-22 18:58:52 -0700369 return;
370 }
Jeff Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-04-22 18:58:52 -0700563 }
564 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700565 } // release device registry reader lock
Jeff Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brownf2f487182010-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 Brown19c97d462011-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brownfd0358292010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown51e7fe752010-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 Brown51e7fe752010-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 Brown51e7fe752010-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 Brown19c97d462011-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 Brown19c97d462011-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-04-22 18:58:52 -07001559 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001560 break;
Jeff Brownefd32662011-03-08 15:13:06 -08001561 }
Jeff Brown46b9ac0a2010-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 Brown46b9ac0a2010-04-22 18:58:52 -07001581 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001582 break;
Jeff Brown46b9ac0a2010-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 Brown46b9ac0a2010-04-22 18:58:52 -07001589 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07001590 break;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07001591 }
Jeff Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown46b9ac0a2010-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 Brown19c97d462011-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 Brown19c97d462011-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 Brown19c97d462011-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 Brownb1268222011-06-03 17:06:16 -07001990 // Use the pointer presentation mode for devices that do not support distinct
1991 // multitouch. The spot-based presentation relies on being able to accurately
1992 // locate two or more fingers on the touch pad.
1993 mParameters.gestureMode = getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_SEMI_MT)
1994 ? Parameters::GESTURE_MODE_POINTER : Parameters::GESTURE_MODE_SPOTS;
Jeff Brown2352b972011-04-12 22:39:53 -07001995
Jeff Brown538881e2011-05-25 18:23:38 -07001996 String8 gestureModeString;
1997 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.gestureMode"),
1998 gestureModeString)) {
1999 if (gestureModeString == "pointer") {
2000 mParameters.gestureMode = Parameters::GESTURE_MODE_POINTER;
2001 } else if (gestureModeString == "spots") {
2002 mParameters.gestureMode = Parameters::GESTURE_MODE_SPOTS;
2003 } else if (gestureModeString != "default") {
2004 LOGW("Invalid value for touch.gestureMode: '%s'", gestureModeString.string());
2005 }
2006 }
2007
Jeff Brownace13b12011-03-09 17:39:48 -08002008 if (getEventHub()->hasRelativeAxis(getDeviceId(), REL_X)
2009 || getEventHub()->hasRelativeAxis(getDeviceId(), REL_Y)) {
2010 // The device is a cursor device with a touch pad attached.
2011 // By default don't use the touch pad to move the pointer.
2012 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brown80fd47c2011-05-24 01:07:44 -07002013 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_POINTER)) {
2014 // The device is a pointing device like a track pad.
2015 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2016 } else if (getEventHub()->hasInputProperty(getDeviceId(), INPUT_PROP_DIRECT)) {
2017 // The device is a touch screen.
2018 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownace13b12011-03-09 17:39:48 -08002019 } else {
Jeff Brown80fd47c2011-05-24 01:07:44 -07002020 // The device is a touch pad of unknown purpose.
Jeff Brownace13b12011-03-09 17:39:48 -08002021 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
2022 }
2023
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002024 String8 deviceTypeString;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002025 if (getDevice()->getConfiguration().tryGetProperty(String8("touch.deviceType"),
2026 deviceTypeString)) {
Jeff Brown58a2da82011-01-25 16:02:22 -08002027 if (deviceTypeString == "touchScreen") {
2028 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brownefd32662011-03-08 15:13:06 -08002029 } else if (deviceTypeString == "touchPad") {
2030 mParameters.deviceType = Parameters::DEVICE_TYPE_TOUCH_PAD;
Jeff Brownace13b12011-03-09 17:39:48 -08002031 } else if (deviceTypeString == "pointer") {
2032 mParameters.deviceType = Parameters::DEVICE_TYPE_POINTER;
Jeff Brown538881e2011-05-25 18:23:38 -07002033 } else if (deviceTypeString != "default") {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002034 LOGW("Invalid value for touch.deviceType: '%s'", deviceTypeString.string());
2035 }
2036 }
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002037
Jeff Brownefd32662011-03-08 15:13:06 -08002038 mParameters.orientationAware = mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002039 getDevice()->getConfiguration().tryGetProperty(String8("touch.orientationAware"),
2040 mParameters.orientationAware);
2041
Jeff Brownefd32662011-03-08 15:13:06 -08002042 mParameters.associatedDisplayId = mParameters.orientationAware
2043 || mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN
Jeff Brownace13b12011-03-09 17:39:48 -08002044 || mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
Jeff Brownefd32662011-03-08 15:13:06 -08002045 ? 0 : -1;
Jeff Brown8d608662010-08-30 03:02:23 -07002046}
2047
Jeff Brownef3d7e82010-09-30 14:33:04 -07002048void TouchInputMapper::dumpParameters(String8& dump) {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002049 dump.append(INDENT3 "Parameters:\n");
2050
Jeff Brown538881e2011-05-25 18:23:38 -07002051 switch (mParameters.gestureMode) {
2052 case Parameters::GESTURE_MODE_POINTER:
2053 dump.append(INDENT4 "GestureMode: pointer\n");
2054 break;
2055 case Parameters::GESTURE_MODE_SPOTS:
2056 dump.append(INDENT4 "GestureMode: spots\n");
2057 break;
2058 default:
2059 assert(false);
2060 }
2061
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002062 switch (mParameters.deviceType) {
2063 case Parameters::DEVICE_TYPE_TOUCH_SCREEN:
2064 dump.append(INDENT4 "DeviceType: touchScreen\n");
2065 break;
2066 case Parameters::DEVICE_TYPE_TOUCH_PAD:
2067 dump.append(INDENT4 "DeviceType: touchPad\n");
2068 break;
Jeff Brownace13b12011-03-09 17:39:48 -08002069 case Parameters::DEVICE_TYPE_POINTER:
2070 dump.append(INDENT4 "DeviceType: pointer\n");
2071 break;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002072 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002073 LOG_ASSERT(false);
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002074 }
2075
2076 dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
2077 mParameters.associatedDisplayId);
2078 dump.appendFormat(INDENT4 "OrientationAware: %s\n",
2079 toString(mParameters.orientationAware));
2080
2081 dump.appendFormat(INDENT4 "UseBadTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002082 toString(mParameters.useBadTouchFilter));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002083 dump.appendFormat(INDENT4 "UseAveragingTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002084 toString(mParameters.useAveragingTouchFilter));
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002085 dump.appendFormat(INDENT4 "UseJumpyTouchFilter: %s\n",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002086 toString(mParameters.useJumpyTouchFilter));
Jeff Brownb88102f2010-09-08 11:49:43 -07002087}
2088
Jeff Brown8d608662010-08-30 03:02:23 -07002089void TouchInputMapper::configureRawAxes() {
2090 mRawAxes.x.clear();
2091 mRawAxes.y.clear();
2092 mRawAxes.pressure.clear();
2093 mRawAxes.touchMajor.clear();
2094 mRawAxes.touchMinor.clear();
2095 mRawAxes.toolMajor.clear();
2096 mRawAxes.toolMinor.clear();
2097 mRawAxes.orientation.clear();
Jeff Brown80fd47c2011-05-24 01:07:44 -07002098 mRawAxes.distance.clear();
2099 mRawAxes.trackingId.clear();
2100 mRawAxes.slot.clear();
Jeff Brown8d608662010-08-30 03:02:23 -07002101}
2102
Jeff Brownef3d7e82010-09-30 14:33:04 -07002103void TouchInputMapper::dumpRawAxes(String8& dump) {
2104 dump.append(INDENT3 "Raw Axes:\n");
Jeff Browncb1404e2011-01-15 18:14:15 -08002105 dumpRawAbsoluteAxisInfo(dump, mRawAxes.x, "X");
2106 dumpRawAbsoluteAxisInfo(dump, mRawAxes.y, "Y");
2107 dumpRawAbsoluteAxisInfo(dump, mRawAxes.pressure, "Pressure");
2108 dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMajor, "TouchMajor");
2109 dumpRawAbsoluteAxisInfo(dump, mRawAxes.touchMinor, "TouchMinor");
2110 dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMajor, "ToolMajor");
2111 dumpRawAbsoluteAxisInfo(dump, mRawAxes.toolMinor, "ToolMinor");
2112 dumpRawAbsoluteAxisInfo(dump, mRawAxes.orientation, "Orientation");
Jeff Brown80fd47c2011-05-24 01:07:44 -07002113 dumpRawAbsoluteAxisInfo(dump, mRawAxes.distance, "Distance");
2114 dumpRawAbsoluteAxisInfo(dump, mRawAxes.trackingId, "TrackingId");
2115 dumpRawAbsoluteAxisInfo(dump, mRawAxes.slot, "Slot");
Jeff Brown6d0fec22010-07-23 21:28:06 -07002116}
2117
Jeff Brown6328cdc2010-07-29 18:18:33 -07002118bool TouchInputMapper::configureSurfaceLocked() {
Jeff Brown9626b142011-03-03 02:09:54 -08002119 // Ensure we have valid X and Y axes.
2120 if (!mRawAxes.x.valid || !mRawAxes.y.valid) {
2121 LOGW(INDENT "Touch device '%s' did not report support for X or Y axis! "
2122 "The device will be inoperable.", getDeviceName().string());
2123 return false;
2124 }
2125
Jeff Brown6d0fec22010-07-23 21:28:06 -07002126 // Update orientation and dimensions if needed.
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002127 int32_t orientation = DISPLAY_ORIENTATION_0;
Jeff Brown9626b142011-03-03 02:09:54 -08002128 int32_t width = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2129 int32_t height = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002130
2131 if (mParameters.associatedDisplayId >= 0) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002132 // Note: getDisplayInfo is non-reentrant so we can continue holding the lock.
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002133 if (! getPolicy()->getDisplayInfo(mParameters.associatedDisplayId,
Jeff Brownefd32662011-03-08 15:13:06 -08002134 &mLocked.associatedDisplayWidth, &mLocked.associatedDisplayHeight,
2135 &mLocked.associatedDisplayOrientation)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002136 return false;
2137 }
Jeff Brownefd32662011-03-08 15:13:06 -08002138
2139 // A touch screen inherits the dimensions of the display.
2140 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
2141 width = mLocked.associatedDisplayWidth;
2142 height = mLocked.associatedDisplayHeight;
2143 }
2144
2145 // The device inherits the orientation of the display if it is orientation aware.
2146 if (mParameters.orientationAware) {
2147 orientation = mLocked.associatedDisplayOrientation;
2148 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002149 }
2150
Jeff Brownace13b12011-03-09 17:39:48 -08002151 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER
2152 && mPointerController == NULL) {
2153 mPointerController = getPolicy()->obtainPointerController(getDeviceId());
2154 }
2155
Jeff Brown6328cdc2010-07-29 18:18:33 -07002156 bool orientationChanged = mLocked.surfaceOrientation != orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002157 if (orientationChanged) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07002158 mLocked.surfaceOrientation = orientation;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002159 }
2160
Jeff Brown6328cdc2010-07-29 18:18:33 -07002161 bool sizeChanged = mLocked.surfaceWidth != width || mLocked.surfaceHeight != height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002162 if (sizeChanged) {
Jeff Brownefd32662011-03-08 15:13:06 -08002163 LOGI("Device reconfigured: id=%d, name='%s', surface size is now %dx%d",
Jeff Brownef3d7e82010-09-30 14:33:04 -07002164 getDeviceId(), getDeviceName().string(), width, height);
Jeff Brown8d608662010-08-30 03:02:23 -07002165
Jeff Brown6328cdc2010-07-29 18:18:33 -07002166 mLocked.surfaceWidth = width;
2167 mLocked.surfaceHeight = height;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002168
Jeff Brown8d608662010-08-30 03:02:23 -07002169 // Configure X and Y factors.
Jeff Brown9626b142011-03-03 02:09:54 -08002170 mLocked.xScale = float(width) / (mRawAxes.x.maxValue - mRawAxes.x.minValue + 1);
2171 mLocked.yScale = float(height) / (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1);
2172 mLocked.xPrecision = 1.0f / mLocked.xScale;
2173 mLocked.yPrecision = 1.0f / mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002174
Jeff Brownefd32662011-03-08 15:13:06 -08002175 mLocked.orientedRanges.x.axis = AMOTION_EVENT_AXIS_X;
2176 mLocked.orientedRanges.x.source = mTouchSource;
2177 mLocked.orientedRanges.y.axis = AMOTION_EVENT_AXIS_Y;
2178 mLocked.orientedRanges.y.source = mTouchSource;
2179
Jeff Brown9626b142011-03-03 02:09:54 -08002180 configureVirtualKeysLocked();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002181
Jeff Brown8d608662010-08-30 03:02:23 -07002182 // Scale factor for terms that are not oriented in a particular axis.
2183 // If the pixels are square then xScale == yScale otherwise we fake it
2184 // by choosing an average.
2185 mLocked.geometricScale = avg(mLocked.xScale, mLocked.yScale);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002186
Jeff Brown8d608662010-08-30 03:02:23 -07002187 // Size of diagonal axis.
Jeff Brown2352b972011-04-12 22:39:53 -07002188 float diagonalSize = hypotf(width, height);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002189
Jeff Brown8d608662010-08-30 03:02:23 -07002190 // TouchMajor and TouchMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002191 if (mCalibration.touchSizeCalibration != Calibration::TOUCH_SIZE_CALIBRATION_NONE) {
2192 mLocked.orientedRanges.haveTouchSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002193
2194 mLocked.orientedRanges.touchMajor.axis = AMOTION_EVENT_AXIS_TOUCH_MAJOR;
2195 mLocked.orientedRanges.touchMajor.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002196 mLocked.orientedRanges.touchMajor.min = 0;
2197 mLocked.orientedRanges.touchMajor.max = diagonalSize;
2198 mLocked.orientedRanges.touchMajor.flat = 0;
2199 mLocked.orientedRanges.touchMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002200
Jeff Brown8d608662010-08-30 03:02:23 -07002201 mLocked.orientedRanges.touchMinor = mLocked.orientedRanges.touchMajor;
Jeff Brownefd32662011-03-08 15:13:06 -08002202 mLocked.orientedRanges.touchMinor.axis = AMOTION_EVENT_AXIS_TOUCH_MINOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002203 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002204
Jeff Brown8d608662010-08-30 03:02:23 -07002205 // ToolMajor and ToolMinor factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002206 mLocked.toolSizeLinearScale = 0;
2207 mLocked.toolSizeLinearBias = 0;
2208 mLocked.toolSizeAreaScale = 0;
2209 mLocked.toolSizeAreaBias = 0;
2210 if (mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
2211 if (mCalibration.toolSizeCalibration == Calibration::TOOL_SIZE_CALIBRATION_LINEAR) {
2212 if (mCalibration.haveToolSizeLinearScale) {
2213 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
Jeff Brown8d608662010-08-30 03:02:23 -07002214 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002215 mLocked.toolSizeLinearScale = float(min(width, height))
Jeff Brown8d608662010-08-30 03:02:23 -07002216 / mRawAxes.toolMajor.maxValue;
2217 }
2218
Jeff Brownc6d282b2010-10-14 21:42:15 -07002219 if (mCalibration.haveToolSizeLinearBias) {
2220 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
2221 }
2222 } else if (mCalibration.toolSizeCalibration ==
2223 Calibration::TOOL_SIZE_CALIBRATION_AREA) {
2224 if (mCalibration.haveToolSizeLinearScale) {
2225 mLocked.toolSizeLinearScale = mCalibration.toolSizeLinearScale;
2226 } else {
2227 mLocked.toolSizeLinearScale = min(width, height);
2228 }
2229
2230 if (mCalibration.haveToolSizeLinearBias) {
2231 mLocked.toolSizeLinearBias = mCalibration.toolSizeLinearBias;
2232 }
2233
2234 if (mCalibration.haveToolSizeAreaScale) {
2235 mLocked.toolSizeAreaScale = mCalibration.toolSizeAreaScale;
2236 } else if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
2237 mLocked.toolSizeAreaScale = 1.0f / mRawAxes.toolMajor.maxValue;
2238 }
2239
2240 if (mCalibration.haveToolSizeAreaBias) {
2241 mLocked.toolSizeAreaBias = mCalibration.toolSizeAreaBias;
Jeff Brown8d608662010-08-30 03:02:23 -07002242 }
2243 }
2244
Jeff Brownc6d282b2010-10-14 21:42:15 -07002245 mLocked.orientedRanges.haveToolSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002246
2247 mLocked.orientedRanges.toolMajor.axis = AMOTION_EVENT_AXIS_TOOL_MAJOR;
2248 mLocked.orientedRanges.toolMajor.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002249 mLocked.orientedRanges.toolMajor.min = 0;
2250 mLocked.orientedRanges.toolMajor.max = diagonalSize;
2251 mLocked.orientedRanges.toolMajor.flat = 0;
2252 mLocked.orientedRanges.toolMajor.fuzz = 0;
Jeff Brownefd32662011-03-08 15:13:06 -08002253
Jeff Brown8d608662010-08-30 03:02:23 -07002254 mLocked.orientedRanges.toolMinor = mLocked.orientedRanges.toolMajor;
Jeff Brownefd32662011-03-08 15:13:06 -08002255 mLocked.orientedRanges.toolMinor.axis = AMOTION_EVENT_AXIS_TOOL_MINOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002256 }
2257
2258 // Pressure factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002259 mLocked.pressureScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002260 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE) {
2261 RawAbsoluteAxisInfo rawPressureAxis;
2262 switch (mCalibration.pressureSource) {
2263 case Calibration::PRESSURE_SOURCE_PRESSURE:
2264 rawPressureAxis = mRawAxes.pressure;
2265 break;
2266 case Calibration::PRESSURE_SOURCE_TOUCH:
2267 rawPressureAxis = mRawAxes.touchMajor;
2268 break;
2269 default:
2270 rawPressureAxis.clear();
2271 }
2272
Jeff Brown8d608662010-08-30 03:02:23 -07002273 if (mCalibration.pressureCalibration == Calibration::PRESSURE_CALIBRATION_PHYSICAL
2274 || mCalibration.pressureCalibration
2275 == Calibration::PRESSURE_CALIBRATION_AMPLITUDE) {
2276 if (mCalibration.havePressureScale) {
2277 mLocked.pressureScale = mCalibration.pressureScale;
2278 } else if (rawPressureAxis.valid && rawPressureAxis.maxValue != 0) {
2279 mLocked.pressureScale = 1.0f / rawPressureAxis.maxValue;
2280 }
2281 }
2282
2283 mLocked.orientedRanges.havePressure = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002284
2285 mLocked.orientedRanges.pressure.axis = AMOTION_EVENT_AXIS_PRESSURE;
2286 mLocked.orientedRanges.pressure.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002287 mLocked.orientedRanges.pressure.min = 0;
2288 mLocked.orientedRanges.pressure.max = 1.0;
2289 mLocked.orientedRanges.pressure.flat = 0;
2290 mLocked.orientedRanges.pressure.fuzz = 0;
2291 }
2292
2293 // Size factors.
Jeff Brownc6d282b2010-10-14 21:42:15 -07002294 mLocked.sizeScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002295 if (mCalibration.sizeCalibration != Calibration::SIZE_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002296 if (mCalibration.sizeCalibration == Calibration::SIZE_CALIBRATION_NORMALIZED) {
2297 if (mRawAxes.toolMajor.valid && mRawAxes.toolMajor.maxValue != 0) {
2298 mLocked.sizeScale = 1.0f / mRawAxes.toolMajor.maxValue;
2299 }
2300 }
2301
2302 mLocked.orientedRanges.haveSize = true;
Jeff Brownefd32662011-03-08 15:13:06 -08002303
2304 mLocked.orientedRanges.size.axis = AMOTION_EVENT_AXIS_SIZE;
2305 mLocked.orientedRanges.size.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002306 mLocked.orientedRanges.size.min = 0;
2307 mLocked.orientedRanges.size.max = 1.0;
2308 mLocked.orientedRanges.size.flat = 0;
2309 mLocked.orientedRanges.size.fuzz = 0;
2310 }
2311
2312 // Orientation
Jeff Brownc6d282b2010-10-14 21:42:15 -07002313 mLocked.orientationScale = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07002314 if (mCalibration.orientationCalibration != Calibration::ORIENTATION_CALIBRATION_NONE) {
Jeff Brown8d608662010-08-30 03:02:23 -07002315 if (mCalibration.orientationCalibration
2316 == Calibration::ORIENTATION_CALIBRATION_INTERPOLATED) {
2317 if (mRawAxes.orientation.valid && mRawAxes.orientation.maxValue != 0) {
2318 mLocked.orientationScale = float(M_PI_2) / mRawAxes.orientation.maxValue;
2319 }
2320 }
2321
Jeff Brown80fd47c2011-05-24 01:07:44 -07002322 mLocked.orientedRanges.haveOrientation = true;
2323
Jeff Brownefd32662011-03-08 15:13:06 -08002324 mLocked.orientedRanges.orientation.axis = AMOTION_EVENT_AXIS_ORIENTATION;
2325 mLocked.orientedRanges.orientation.source = mTouchSource;
Jeff Brown8d608662010-08-30 03:02:23 -07002326 mLocked.orientedRanges.orientation.min = - M_PI_2;
2327 mLocked.orientedRanges.orientation.max = M_PI_2;
2328 mLocked.orientedRanges.orientation.flat = 0;
2329 mLocked.orientedRanges.orientation.fuzz = 0;
2330 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002331
2332 // Distance
2333 mLocked.distanceScale = 0;
2334 if (mCalibration.distanceCalibration != Calibration::DISTANCE_CALIBRATION_NONE) {
2335 if (mCalibration.distanceCalibration
2336 == Calibration::DISTANCE_CALIBRATION_SCALED) {
2337 if (mCalibration.haveDistanceScale) {
2338 mLocked.distanceScale = mCalibration.distanceScale;
2339 } else {
2340 mLocked.distanceScale = 1.0f;
2341 }
2342 }
2343
2344 mLocked.orientedRanges.haveDistance = true;
2345
2346 mLocked.orientedRanges.distance.axis = AMOTION_EVENT_AXIS_DISTANCE;
2347 mLocked.orientedRanges.distance.source = mTouchSource;
2348 mLocked.orientedRanges.distance.min =
2349 mRawAxes.distance.minValue * mLocked.distanceScale;
2350 mLocked.orientedRanges.distance.max =
2351 mRawAxes.distance.minValue * mLocked.distanceScale;
2352 mLocked.orientedRanges.distance.flat = 0;
2353 mLocked.orientedRanges.distance.fuzz =
2354 mRawAxes.distance.fuzz * mLocked.distanceScale;
2355 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002356 }
2357
2358 if (orientationChanged || sizeChanged) {
Jeff Brown9626b142011-03-03 02:09:54 -08002359 // Compute oriented surface dimensions, precision, scales and ranges.
2360 // Note that the maximum value reported is an inclusive maximum value so it is one
2361 // unit less than the total width or height of surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07002362 switch (mLocked.surfaceOrientation) {
Jeff Brownb4ff35d2011-01-02 16:37:43 -08002363 case DISPLAY_ORIENTATION_90:
2364 case DISPLAY_ORIENTATION_270:
Jeff Brown6328cdc2010-07-29 18:18:33 -07002365 mLocked.orientedSurfaceWidth = mLocked.surfaceHeight;
2366 mLocked.orientedSurfaceHeight = mLocked.surfaceWidth;
Jeff Brown9626b142011-03-03 02:09:54 -08002367
Jeff Brown6328cdc2010-07-29 18:18:33 -07002368 mLocked.orientedXPrecision = mLocked.yPrecision;
2369 mLocked.orientedYPrecision = mLocked.xPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002370
2371 mLocked.orientedRanges.x.min = 0;
2372 mLocked.orientedRanges.x.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue)
2373 * mLocked.yScale;
2374 mLocked.orientedRanges.x.flat = 0;
2375 mLocked.orientedRanges.x.fuzz = mLocked.yScale;
2376
2377 mLocked.orientedRanges.y.min = 0;
2378 mLocked.orientedRanges.y.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue)
2379 * mLocked.xScale;
2380 mLocked.orientedRanges.y.flat = 0;
2381 mLocked.orientedRanges.y.fuzz = mLocked.xScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002382 break;
Jeff Brown9626b142011-03-03 02:09:54 -08002383
Jeff Brown6d0fec22010-07-23 21:28:06 -07002384 default:
Jeff Brown6328cdc2010-07-29 18:18:33 -07002385 mLocked.orientedSurfaceWidth = mLocked.surfaceWidth;
2386 mLocked.orientedSurfaceHeight = mLocked.surfaceHeight;
Jeff Brown9626b142011-03-03 02:09:54 -08002387
Jeff Brown6328cdc2010-07-29 18:18:33 -07002388 mLocked.orientedXPrecision = mLocked.xPrecision;
2389 mLocked.orientedYPrecision = mLocked.yPrecision;
Jeff Brown9626b142011-03-03 02:09:54 -08002390
2391 mLocked.orientedRanges.x.min = 0;
2392 mLocked.orientedRanges.x.max = (mRawAxes.x.maxValue - mRawAxes.x.minValue)
2393 * mLocked.xScale;
2394 mLocked.orientedRanges.x.flat = 0;
2395 mLocked.orientedRanges.x.fuzz = mLocked.xScale;
2396
2397 mLocked.orientedRanges.y.min = 0;
2398 mLocked.orientedRanges.y.max = (mRawAxes.y.maxValue - mRawAxes.y.minValue)
2399 * mLocked.yScale;
2400 mLocked.orientedRanges.y.flat = 0;
2401 mLocked.orientedRanges.y.fuzz = mLocked.yScale;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002402 break;
2403 }
Jeff Brownace13b12011-03-09 17:39:48 -08002404
2405 // Compute pointer gesture detection parameters.
2406 // TODO: These factors should not be hardcoded.
2407 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
2408 int32_t rawWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2409 int32_t rawHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown2352b972011-04-12 22:39:53 -07002410 float rawDiagonal = hypotf(rawWidth, rawHeight);
2411 float displayDiagonal = hypotf(mLocked.associatedDisplayWidth,
2412 mLocked.associatedDisplayHeight);
Jeff Brownace13b12011-03-09 17:39:48 -08002413
Jeff Brown2352b972011-04-12 22:39:53 -07002414 // Scale movements such that one whole swipe of the touch pad covers a
Jeff Brown19c97d462011-06-01 12:33:19 -07002415 // given area relative to the diagonal size of the display when no acceleration
2416 // is applied.
Jeff Brownace13b12011-03-09 17:39:48 -08002417 // Assume that the touch pad has a square aspect ratio such that movements in
2418 // X and Y of the same number of raw units cover the same physical distance.
Jeff Brown214eaf42011-05-26 19:17:02 -07002419 mLocked.pointerGestureXMovementScale = mConfig->pointerGestureMovementSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002420 * displayDiagonal / rawDiagonal;
Jeff Brownace13b12011-03-09 17:39:48 -08002421 mLocked.pointerGestureYMovementScale = mLocked.pointerGestureXMovementScale;
2422
2423 // Scale zooms to cover a smaller range of the display than movements do.
2424 // This value determines the area around the pointer that is affected by freeform
2425 // pointer gestures.
Jeff Brown214eaf42011-05-26 19:17:02 -07002426 mLocked.pointerGestureXZoomScale = mConfig->pointerGestureZoomSpeedRatio
Jeff Brown2352b972011-04-12 22:39:53 -07002427 * displayDiagonal / rawDiagonal;
2428 mLocked.pointerGestureYZoomScale = mLocked.pointerGestureXZoomScale;
Jeff Brownace13b12011-03-09 17:39:48 -08002429
Jeff Brown2352b972011-04-12 22:39:53 -07002430 // Max width between pointers to detect a swipe gesture is more than some fraction
2431 // of the diagonal axis of the touch pad. Touches that are wider than this are
2432 // translated into freeform gestures.
Jeff Brown214eaf42011-05-26 19:17:02 -07002433 mLocked.pointerGestureMaxSwipeWidth =
2434 mConfig->pointerGestureSwipeMaxWidthRatio * rawDiagonal;
Jeff Brown2352b972011-04-12 22:39:53 -07002435
2436 // Reset the current pointer gesture.
2437 mPointerGesture.reset();
2438
2439 // Remove any current spots.
2440 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
2441 mPointerController->clearSpots();
2442 }
Jeff Brownace13b12011-03-09 17:39:48 -08002443 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002444 }
2445
2446 return true;
2447}
2448
Jeff Brownef3d7e82010-09-30 14:33:04 -07002449void TouchInputMapper::dumpSurfaceLocked(String8& dump) {
2450 dump.appendFormat(INDENT3 "SurfaceWidth: %dpx\n", mLocked.surfaceWidth);
2451 dump.appendFormat(INDENT3 "SurfaceHeight: %dpx\n", mLocked.surfaceHeight);
2452 dump.appendFormat(INDENT3 "SurfaceOrientation: %d\n", mLocked.surfaceOrientation);
Jeff Brownb88102f2010-09-08 11:49:43 -07002453}
2454
Jeff Brown6328cdc2010-07-29 18:18:33 -07002455void TouchInputMapper::configureVirtualKeysLocked() {
Jeff Brown8d608662010-08-30 03:02:23 -07002456 Vector<VirtualKeyDefinition> virtualKeyDefinitions;
Jeff Brown90655042010-12-02 13:50:46 -08002457 getEventHub()->getVirtualKeyDefinitions(getDeviceId(), virtualKeyDefinitions);
Jeff Brown6d0fec22010-07-23 21:28:06 -07002458
Jeff Brown6328cdc2010-07-29 18:18:33 -07002459 mLocked.virtualKeys.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002460
Jeff Brown6328cdc2010-07-29 18:18:33 -07002461 if (virtualKeyDefinitions.size() == 0) {
2462 return;
2463 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002464
Jeff Brown6328cdc2010-07-29 18:18:33 -07002465 mLocked.virtualKeys.setCapacity(virtualKeyDefinitions.size());
2466
Jeff Brown8d608662010-08-30 03:02:23 -07002467 int32_t touchScreenLeft = mRawAxes.x.minValue;
2468 int32_t touchScreenTop = mRawAxes.y.minValue;
Jeff Brown9626b142011-03-03 02:09:54 -08002469 int32_t touchScreenWidth = mRawAxes.x.maxValue - mRawAxes.x.minValue + 1;
2470 int32_t touchScreenHeight = mRawAxes.y.maxValue - mRawAxes.y.minValue + 1;
Jeff Brown6328cdc2010-07-29 18:18:33 -07002471
2472 for (size_t i = 0; i < virtualKeyDefinitions.size(); i++) {
Jeff Brown8d608662010-08-30 03:02:23 -07002473 const VirtualKeyDefinition& virtualKeyDefinition =
Jeff Brown6328cdc2010-07-29 18:18:33 -07002474 virtualKeyDefinitions[i];
2475
2476 mLocked.virtualKeys.add();
2477 VirtualKey& virtualKey = mLocked.virtualKeys.editTop();
2478
2479 virtualKey.scanCode = virtualKeyDefinition.scanCode;
2480 int32_t keyCode;
2481 uint32_t flags;
Jeff Brown6f2fba42011-02-19 01:08:02 -08002482 if (getEventHub()->mapKey(getDeviceId(), virtualKey.scanCode,
Jeff Brown6328cdc2010-07-29 18:18:33 -07002483 & keyCode, & flags)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002484 LOGW(INDENT "VirtualKey %d: could not obtain key code, ignoring",
2485 virtualKey.scanCode);
Jeff Brown6328cdc2010-07-29 18:18:33 -07002486 mLocked.virtualKeys.pop(); // drop the key
2487 continue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002488 }
2489
Jeff Brown6328cdc2010-07-29 18:18:33 -07002490 virtualKey.keyCode = keyCode;
2491 virtualKey.flags = flags;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002492
Jeff Brown6328cdc2010-07-29 18:18:33 -07002493 // convert the key definition's display coordinates into touch coordinates for a hit box
2494 int32_t halfWidth = virtualKeyDefinition.width / 2;
2495 int32_t halfHeight = virtualKeyDefinition.height / 2;
Jeff Brown6d0fec22010-07-23 21:28:06 -07002496
Jeff Brown6328cdc2010-07-29 18:18:33 -07002497 virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth)
2498 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
2499 virtualKey.hitRight= (virtualKeyDefinition.centerX + halfWidth)
2500 * touchScreenWidth / mLocked.surfaceWidth + touchScreenLeft;
2501 virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight)
2502 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
2503 virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight)
2504 * touchScreenHeight / mLocked.surfaceHeight + touchScreenTop;
Jeff Brownef3d7e82010-09-30 14:33:04 -07002505 }
2506}
2507
2508void TouchInputMapper::dumpVirtualKeysLocked(String8& dump) {
2509 if (!mLocked.virtualKeys.isEmpty()) {
2510 dump.append(INDENT3 "Virtual Keys:\n");
2511
2512 for (size_t i = 0; i < mLocked.virtualKeys.size(); i++) {
2513 const VirtualKey& virtualKey = mLocked.virtualKeys.itemAt(i);
2514 dump.appendFormat(INDENT4 "%d: scanCode=%d, keyCode=%d, "
2515 "hitLeft=%d, hitRight=%d, hitTop=%d, hitBottom=%d\n",
2516 i, virtualKey.scanCode, virtualKey.keyCode,
2517 virtualKey.hitLeft, virtualKey.hitRight,
2518 virtualKey.hitTop, virtualKey.hitBottom);
2519 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002520 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07002521}
2522
Jeff Brown8d608662010-08-30 03:02:23 -07002523void TouchInputMapper::parseCalibration() {
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002524 const PropertyMap& in = getDevice()->getConfiguration();
Jeff Brown8d608662010-08-30 03:02:23 -07002525 Calibration& out = mCalibration;
2526
Jeff Brownc6d282b2010-10-14 21:42:15 -07002527 // Touch Size
2528 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT;
2529 String8 touchSizeCalibrationString;
2530 if (in.tryGetProperty(String8("touch.touchSize.calibration"), touchSizeCalibrationString)) {
2531 if (touchSizeCalibrationString == "none") {
2532 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
2533 } else if (touchSizeCalibrationString == "geometric") {
2534 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC;
2535 } else if (touchSizeCalibrationString == "pressure") {
2536 out.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
2537 } else if (touchSizeCalibrationString != "default") {
2538 LOGW("Invalid value for touch.touchSize.calibration: '%s'",
2539 touchSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002540 }
2541 }
2542
Jeff Brownc6d282b2010-10-14 21:42:15 -07002543 // Tool Size
2544 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_DEFAULT;
2545 String8 toolSizeCalibrationString;
2546 if (in.tryGetProperty(String8("touch.toolSize.calibration"), toolSizeCalibrationString)) {
2547 if (toolSizeCalibrationString == "none") {
2548 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
2549 } else if (toolSizeCalibrationString == "geometric") {
2550 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC;
2551 } else if (toolSizeCalibrationString == "linear") {
2552 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
2553 } else if (toolSizeCalibrationString == "area") {
2554 out.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_AREA;
2555 } else if (toolSizeCalibrationString != "default") {
2556 LOGW("Invalid value for touch.toolSize.calibration: '%s'",
2557 toolSizeCalibrationString.string());
Jeff Brown8d608662010-08-30 03:02:23 -07002558 }
2559 }
2560
Jeff Brownc6d282b2010-10-14 21:42:15 -07002561 out.haveToolSizeLinearScale = in.tryGetProperty(String8("touch.toolSize.linearScale"),
2562 out.toolSizeLinearScale);
2563 out.haveToolSizeLinearBias = in.tryGetProperty(String8("touch.toolSize.linearBias"),
2564 out.toolSizeLinearBias);
2565 out.haveToolSizeAreaScale = in.tryGetProperty(String8("touch.toolSize.areaScale"),
2566 out.toolSizeAreaScale);
2567 out.haveToolSizeAreaBias = in.tryGetProperty(String8("touch.toolSize.areaBias"),
2568 out.toolSizeAreaBias);
2569 out.haveToolSizeIsSummed = in.tryGetProperty(String8("touch.toolSize.isSummed"),
2570 out.toolSizeIsSummed);
Jeff Brown8d608662010-08-30 03:02:23 -07002571
2572 // Pressure
2573 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_DEFAULT;
2574 String8 pressureCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002575 if (in.tryGetProperty(String8("touch.pressure.calibration"), pressureCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002576 if (pressureCalibrationString == "none") {
2577 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2578 } else if (pressureCalibrationString == "physical") {
2579 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_PHYSICAL;
2580 } else if (pressureCalibrationString == "amplitude") {
2581 out.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2582 } else if (pressureCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002583 LOGW("Invalid value for touch.pressure.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002584 pressureCalibrationString.string());
2585 }
2586 }
2587
2588 out.pressureSource = Calibration::PRESSURE_SOURCE_DEFAULT;
2589 String8 pressureSourceString;
2590 if (in.tryGetProperty(String8("touch.pressure.source"), pressureSourceString)) {
2591 if (pressureSourceString == "pressure") {
2592 out.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2593 } else if (pressureSourceString == "touch") {
2594 out.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2595 } else if (pressureSourceString != "default") {
2596 LOGW("Invalid value for touch.pressure.source: '%s'",
2597 pressureSourceString.string());
2598 }
2599 }
2600
2601 out.havePressureScale = in.tryGetProperty(String8("touch.pressure.scale"),
2602 out.pressureScale);
2603
2604 // Size
2605 out.sizeCalibration = Calibration::SIZE_CALIBRATION_DEFAULT;
2606 String8 sizeCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002607 if (in.tryGetProperty(String8("touch.size.calibration"), sizeCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002608 if (sizeCalibrationString == "none") {
2609 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2610 } else if (sizeCalibrationString == "normalized") {
2611 out.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
2612 } else if (sizeCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002613 LOGW("Invalid value for touch.size.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002614 sizeCalibrationString.string());
2615 }
2616 }
2617
2618 // Orientation
2619 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_DEFAULT;
2620 String8 orientationCalibrationString;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002621 if (in.tryGetProperty(String8("touch.orientation.calibration"), orientationCalibrationString)) {
Jeff Brown8d608662010-08-30 03:02:23 -07002622 if (orientationCalibrationString == "none") {
2623 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2624 } else if (orientationCalibrationString == "interpolated") {
2625 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002626 } else if (orientationCalibrationString == "vector") {
2627 out.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_VECTOR;
Jeff Brown8d608662010-08-30 03:02:23 -07002628 } else if (orientationCalibrationString != "default") {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002629 LOGW("Invalid value for touch.orientation.calibration: '%s'",
Jeff Brown8d608662010-08-30 03:02:23 -07002630 orientationCalibrationString.string());
2631 }
2632 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002633
2634 // Distance
2635 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_DEFAULT;
2636 String8 distanceCalibrationString;
2637 if (in.tryGetProperty(String8("touch.distance.calibration"), distanceCalibrationString)) {
2638 if (distanceCalibrationString == "none") {
2639 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
2640 } else if (distanceCalibrationString == "scaled") {
2641 out.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
2642 } else if (distanceCalibrationString != "default") {
2643 LOGW("Invalid value for touch.distance.calibration: '%s'",
2644 distanceCalibrationString.string());
2645 }
2646 }
2647
2648 out.haveDistanceScale = in.tryGetProperty(String8("touch.distance.scale"),
2649 out.distanceScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002650}
2651
2652void TouchInputMapper::resolveCalibration() {
2653 // Pressure
2654 switch (mCalibration.pressureSource) {
2655 case Calibration::PRESSURE_SOURCE_DEFAULT:
2656 if (mRawAxes.pressure.valid) {
2657 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_PRESSURE;
2658 } else if (mRawAxes.touchMajor.valid) {
2659 mCalibration.pressureSource = Calibration::PRESSURE_SOURCE_TOUCH;
2660 }
2661 break;
2662
2663 case Calibration::PRESSURE_SOURCE_PRESSURE:
2664 if (! mRawAxes.pressure.valid) {
2665 LOGW("Calibration property touch.pressure.source is 'pressure' but "
2666 "the pressure axis is not available.");
2667 }
2668 break;
2669
2670 case Calibration::PRESSURE_SOURCE_TOUCH:
2671 if (! mRawAxes.touchMajor.valid) {
2672 LOGW("Calibration property touch.pressure.source is 'touch' but "
2673 "the touchMajor axis is not available.");
2674 }
2675 break;
2676
2677 default:
2678 break;
2679 }
2680
2681 switch (mCalibration.pressureCalibration) {
2682 case Calibration::PRESSURE_CALIBRATION_DEFAULT:
2683 if (mCalibration.pressureSource != Calibration::PRESSURE_SOURCE_DEFAULT) {
2684 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_AMPLITUDE;
2685 } else {
2686 mCalibration.pressureCalibration = Calibration::PRESSURE_CALIBRATION_NONE;
2687 }
2688 break;
2689
2690 default:
2691 break;
2692 }
2693
Jeff Brownc6d282b2010-10-14 21:42:15 -07002694 // Tool Size
2695 switch (mCalibration.toolSizeCalibration) {
2696 case Calibration::TOOL_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07002697 if (mRawAxes.toolMajor.valid) {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002698 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_LINEAR;
Jeff Brown8d608662010-08-30 03:02:23 -07002699 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002700 mCalibration.toolSizeCalibration = Calibration::TOOL_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002701 }
2702 break;
2703
2704 default:
2705 break;
2706 }
2707
Jeff Brownc6d282b2010-10-14 21:42:15 -07002708 // Touch Size
2709 switch (mCalibration.touchSizeCalibration) {
2710 case Calibration::TOUCH_SIZE_CALIBRATION_DEFAULT:
Jeff Brown8d608662010-08-30 03:02:23 -07002711 if (mCalibration.pressureCalibration != Calibration::PRESSURE_CALIBRATION_NONE
Jeff Brownc6d282b2010-10-14 21:42:15 -07002712 && mCalibration.toolSizeCalibration != Calibration::TOOL_SIZE_CALIBRATION_NONE) {
2713 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE;
Jeff Brown8d608662010-08-30 03:02:23 -07002714 } else {
Jeff Brownc6d282b2010-10-14 21:42:15 -07002715 mCalibration.touchSizeCalibration = Calibration::TOUCH_SIZE_CALIBRATION_NONE;
Jeff Brown8d608662010-08-30 03:02:23 -07002716 }
2717 break;
2718
2719 default:
2720 break;
2721 }
2722
2723 // Size
2724 switch (mCalibration.sizeCalibration) {
2725 case Calibration::SIZE_CALIBRATION_DEFAULT:
2726 if (mRawAxes.toolMajor.valid) {
2727 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NORMALIZED;
2728 } else {
2729 mCalibration.sizeCalibration = Calibration::SIZE_CALIBRATION_NONE;
2730 }
2731 break;
2732
2733 default:
2734 break;
2735 }
2736
2737 // Orientation
2738 switch (mCalibration.orientationCalibration) {
2739 case Calibration::ORIENTATION_CALIBRATION_DEFAULT:
2740 if (mRawAxes.orientation.valid) {
2741 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_INTERPOLATED;
2742 } else {
2743 mCalibration.orientationCalibration = Calibration::ORIENTATION_CALIBRATION_NONE;
2744 }
2745 break;
2746
2747 default:
2748 break;
2749 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002750
2751 // Distance
2752 switch (mCalibration.distanceCalibration) {
2753 case Calibration::DISTANCE_CALIBRATION_DEFAULT:
2754 if (mRawAxes.distance.valid) {
2755 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_SCALED;
2756 } else {
2757 mCalibration.distanceCalibration = Calibration::DISTANCE_CALIBRATION_NONE;
2758 }
2759 break;
2760
2761 default:
2762 break;
2763 }
Jeff Brown8d608662010-08-30 03:02:23 -07002764}
2765
Jeff Brownef3d7e82010-09-30 14:33:04 -07002766void TouchInputMapper::dumpCalibration(String8& dump) {
2767 dump.append(INDENT3 "Calibration:\n");
Jeff Brownb88102f2010-09-08 11:49:43 -07002768
Jeff Brownc6d282b2010-10-14 21:42:15 -07002769 // Touch Size
2770 switch (mCalibration.touchSizeCalibration) {
2771 case Calibration::TOUCH_SIZE_CALIBRATION_NONE:
2772 dump.append(INDENT4 "touch.touchSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002773 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002774 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
2775 dump.append(INDENT4 "touch.touchSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002776 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002777 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
2778 dump.append(INDENT4 "touch.touchSize.calibration: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002779 break;
2780 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002781 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002782 }
2783
Jeff Brownc6d282b2010-10-14 21:42:15 -07002784 // Tool Size
2785 switch (mCalibration.toolSizeCalibration) {
2786 case Calibration::TOOL_SIZE_CALIBRATION_NONE:
2787 dump.append(INDENT4 "touch.toolSize.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002788 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002789 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
2790 dump.append(INDENT4 "touch.toolSize.calibration: geometric\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002791 break;
Jeff Brownc6d282b2010-10-14 21:42:15 -07002792 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
2793 dump.append(INDENT4 "touch.toolSize.calibration: linear\n");
2794 break;
2795 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
2796 dump.append(INDENT4 "touch.toolSize.calibration: area\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002797 break;
2798 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002799 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002800 }
2801
Jeff Brownc6d282b2010-10-14 21:42:15 -07002802 if (mCalibration.haveToolSizeLinearScale) {
2803 dump.appendFormat(INDENT4 "touch.toolSize.linearScale: %0.3f\n",
2804 mCalibration.toolSizeLinearScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002805 }
2806
Jeff Brownc6d282b2010-10-14 21:42:15 -07002807 if (mCalibration.haveToolSizeLinearBias) {
2808 dump.appendFormat(INDENT4 "touch.toolSize.linearBias: %0.3f\n",
2809 mCalibration.toolSizeLinearBias);
Jeff Brown8d608662010-08-30 03:02:23 -07002810 }
2811
Jeff Brownc6d282b2010-10-14 21:42:15 -07002812 if (mCalibration.haveToolSizeAreaScale) {
2813 dump.appendFormat(INDENT4 "touch.toolSize.areaScale: %0.3f\n",
2814 mCalibration.toolSizeAreaScale);
2815 }
2816
2817 if (mCalibration.haveToolSizeAreaBias) {
2818 dump.appendFormat(INDENT4 "touch.toolSize.areaBias: %0.3f\n",
2819 mCalibration.toolSizeAreaBias);
2820 }
2821
2822 if (mCalibration.haveToolSizeIsSummed) {
Jeff Brown1f245102010-11-18 20:53:46 -08002823 dump.appendFormat(INDENT4 "touch.toolSize.isSummed: %s\n",
Jeff Brown47e6b1b2010-11-29 17:37:49 -08002824 toString(mCalibration.toolSizeIsSummed));
Jeff Brown8d608662010-08-30 03:02:23 -07002825 }
2826
2827 // Pressure
2828 switch (mCalibration.pressureCalibration) {
2829 case Calibration::PRESSURE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002830 dump.append(INDENT4 "touch.pressure.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002831 break;
2832 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002833 dump.append(INDENT4 "touch.pressure.calibration: physical\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002834 break;
2835 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002836 dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002837 break;
2838 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002839 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002840 }
2841
2842 switch (mCalibration.pressureSource) {
2843 case Calibration::PRESSURE_SOURCE_PRESSURE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002844 dump.append(INDENT4 "touch.pressure.source: pressure\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002845 break;
2846 case Calibration::PRESSURE_SOURCE_TOUCH:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002847 dump.append(INDENT4 "touch.pressure.source: touch\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002848 break;
2849 case Calibration::PRESSURE_SOURCE_DEFAULT:
2850 break;
2851 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002852 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002853 }
2854
2855 if (mCalibration.havePressureScale) {
Jeff Brownef3d7e82010-09-30 14:33:04 -07002856 dump.appendFormat(INDENT4 "touch.pressure.scale: %0.3f\n",
2857 mCalibration.pressureScale);
Jeff Brown8d608662010-08-30 03:02:23 -07002858 }
2859
2860 // Size
2861 switch (mCalibration.sizeCalibration) {
2862 case Calibration::SIZE_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002863 dump.append(INDENT4 "touch.size.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002864 break;
2865 case Calibration::SIZE_CALIBRATION_NORMALIZED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002866 dump.append(INDENT4 "touch.size.calibration: normalized\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002867 break;
2868 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002869 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002870 }
2871
2872 // Orientation
2873 switch (mCalibration.orientationCalibration) {
2874 case Calibration::ORIENTATION_CALIBRATION_NONE:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002875 dump.append(INDENT4 "touch.orientation.calibration: none\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002876 break;
2877 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
Jeff Brownef3d7e82010-09-30 14:33:04 -07002878 dump.append(INDENT4 "touch.orientation.calibration: interpolated\n");
Jeff Brown8d608662010-08-30 03:02:23 -07002879 break;
Jeff Brown517bb4c2011-01-14 19:09:23 -08002880 case Calibration::ORIENTATION_CALIBRATION_VECTOR:
2881 dump.append(INDENT4 "touch.orientation.calibration: vector\n");
2882 break;
Jeff Brown8d608662010-08-30 03:02:23 -07002883 default:
Jeff Brownb6110c22011-04-01 16:15:13 -07002884 LOG_ASSERT(false);
Jeff Brown8d608662010-08-30 03:02:23 -07002885 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07002886
2887 // Distance
2888 switch (mCalibration.distanceCalibration) {
2889 case Calibration::DISTANCE_CALIBRATION_NONE:
2890 dump.append(INDENT4 "touch.distance.calibration: none\n");
2891 break;
2892 case Calibration::DISTANCE_CALIBRATION_SCALED:
2893 dump.append(INDENT4 "touch.distance.calibration: scaled\n");
2894 break;
2895 default:
2896 LOG_ASSERT(false);
2897 }
2898
2899 if (mCalibration.haveDistanceScale) {
2900 dump.appendFormat(INDENT4 "touch.distance.scale: %0.3f\n",
2901 mCalibration.distanceScale);
2902 }
Jeff Brown8d608662010-08-30 03:02:23 -07002903}
2904
Jeff Brown6d0fec22010-07-23 21:28:06 -07002905void TouchInputMapper::reset() {
2906 // Synthesize touch up event if touch is currently down.
2907 // This will also take care of finishing virtual key processing if needed.
2908 if (mLastTouch.pointerCount != 0) {
2909 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
2910 mCurrentTouch.clear();
2911 syncTouch(when, true);
2912 }
2913
Jeff Brown6328cdc2010-07-29 18:18:33 -07002914 { // acquire lock
2915 AutoMutex _l(mLock);
2916 initializeLocked();
Jeff Brown2352b972011-04-12 22:39:53 -07002917
2918 if (mPointerController != NULL
2919 && mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
2920 mPointerController->clearSpots();
2921 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07002922 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07002923
Jeff Brown6328cdc2010-07-29 18:18:33 -07002924 InputMapper::reset();
Jeff Brown6d0fec22010-07-23 21:28:06 -07002925}
2926
2927void TouchInputMapper::syncTouch(nsecs_t when, bool havePointerIds) {
Jeff Brownaa3855d2011-03-17 01:34:19 -07002928#if DEBUG_RAW_EVENTS
2929 if (!havePointerIds) {
2930 LOGD("syncTouch: pointerCount=%d, no pointer ids", mCurrentTouch.pointerCount);
2931 } else {
2932 LOGD("syncTouch: pointerCount=%d, up=0x%08x, down=0x%08x, move=0x%08x, "
2933 "last=0x%08x, current=0x%08x", mCurrentTouch.pointerCount,
2934 mLastTouch.idBits.value & ~mCurrentTouch.idBits.value,
2935 mCurrentTouch.idBits.value & ~mLastTouch.idBits.value,
2936 mLastTouch.idBits.value & mCurrentTouch.idBits.value,
2937 mLastTouch.idBits.value, mCurrentTouch.idBits.value);
2938 }
2939#endif
2940
Jeff Brown6328cdc2010-07-29 18:18:33 -07002941 // Preprocess pointer data.
Jeff Brown6d0fec22010-07-23 21:28:06 -07002942 if (mParameters.useBadTouchFilter) {
2943 if (applyBadTouchFilter()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002944 havePointerIds = false;
2945 }
2946 }
2947
Jeff Brown6d0fec22010-07-23 21:28:06 -07002948 if (mParameters.useJumpyTouchFilter) {
2949 if (applyJumpyTouchFilter()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002950 havePointerIds = false;
2951 }
2952 }
2953
Jeff Brownaa3855d2011-03-17 01:34:19 -07002954 if (!havePointerIds) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002955 calculatePointerIds();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002956 }
2957
Jeff Brown6d0fec22010-07-23 21:28:06 -07002958 TouchData temp;
2959 TouchData* savedTouch;
2960 if (mParameters.useAveragingTouchFilter) {
2961 temp.copyFrom(mCurrentTouch);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002962 savedTouch = & temp;
2963
Jeff Brown6d0fec22010-07-23 21:28:06 -07002964 applyAveragingTouchFilter();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002965 } else {
Jeff Brown6d0fec22010-07-23 21:28:06 -07002966 savedTouch = & mCurrentTouch;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002967 }
2968
Jeff Brown56194eb2011-03-02 19:23:13 -08002969 uint32_t policyFlags = 0;
Jeff Brown05dc66a2011-03-02 14:41:58 -08002970 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) {
Jeff Brownefd32662011-03-08 15:13:06 -08002971 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
2972 // If this is a touch screen, hide the pointer on an initial down.
2973 getContext()->fadePointer();
2974 }
Jeff Brown56194eb2011-03-02 19:23:13 -08002975
2976 // Initial downs on external touch devices should wake the device.
2977 // We don't do this for internal touch screens to prevent them from waking
2978 // up in your pocket.
2979 // TODO: Use the input device configuration to control this behavior more finely.
2980 if (getDevice()->isExternal()) {
2981 policyFlags |= POLICY_FLAG_WAKE_DROPPED;
2982 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08002983 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07002984
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07002985 // Synthesize key down from buttons if needed.
2986 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_DOWN, when, getDeviceId(), mTouchSource,
2987 policyFlags, mLastTouch.buttonState, mCurrentTouch.buttonState);
2988
2989 // Send motion events.
Jeff Brown79ac9692011-04-19 21:20:10 -07002990 TouchResult touchResult;
2991 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount == 0
2992 && mLastTouch.buttonState == mCurrentTouch.buttonState) {
2993 // Drop spurious syncs.
2994 touchResult = DROP_STROKE;
2995 } else {
2996 // Process touches and virtual keys.
2997 touchResult = consumeOffScreenTouches(when, policyFlags);
2998 if (touchResult == DISPATCH_TOUCH) {
2999 suppressSwipeOntoVirtualKeys(when);
3000 if (mPointerController != NULL) {
3001 dispatchPointerGestures(when, policyFlags, false /*isTimeout*/);
3002 }
3003 dispatchTouches(when, policyFlags);
Jeff Brownace13b12011-03-09 17:39:48 -08003004 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003005 }
3006
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003007 // Synthesize key up from buttons if needed.
3008 synthesizeButtonKeys(getContext(), AKEY_EVENT_ACTION_UP, when, getDeviceId(), mTouchSource,
3009 policyFlags, mLastTouch.buttonState, mCurrentTouch.buttonState);
3010
Jeff Brown6328cdc2010-07-29 18:18:33 -07003011 // Copy current touch to last touch in preparation for the next cycle.
Jeff Brownace13b12011-03-09 17:39:48 -08003012 // Keep the button state so we can track edge-triggered button state changes.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003013 if (touchResult == DROP_STROKE) {
3014 mLastTouch.clear();
Jeff Brownace13b12011-03-09 17:39:48 -08003015 mLastTouch.buttonState = savedTouch->buttonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003016 } else {
3017 mLastTouch.copyFrom(*savedTouch);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003018 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003019}
3020
Jeff Brown79ac9692011-04-19 21:20:10 -07003021void TouchInputMapper::timeoutExpired(nsecs_t when) {
3022 if (mPointerController != NULL) {
3023 dispatchPointerGestures(when, 0 /*policyFlags*/, true /*isTimeout*/);
3024 }
3025}
3026
Jeff Brown6d0fec22010-07-23 21:28:06 -07003027TouchInputMapper::TouchResult TouchInputMapper::consumeOffScreenTouches(
3028 nsecs_t when, uint32_t policyFlags) {
3029 int32_t keyEventAction, keyEventFlags;
3030 int32_t keyCode, scanCode, downTime;
3031 TouchResult touchResult;
Jeff Brown349703e2010-06-22 01:27:15 -07003032
Jeff Brown6328cdc2010-07-29 18:18:33 -07003033 { // acquire lock
3034 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003035
Jeff Brown6328cdc2010-07-29 18:18:33 -07003036 // Update surface size and orientation, including virtual key positions.
3037 if (! configureSurfaceLocked()) {
3038 return DROP_STROKE;
3039 }
3040
3041 // Check for virtual key press.
3042 if (mLocked.currentVirtualKey.down) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003043 if (mCurrentTouch.pointerCount == 0) {
3044 // Pointer went up while virtual key was down.
Jeff Brown6328cdc2010-07-29 18:18:33 -07003045 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003046#if DEBUG_VIRTUAL_KEYS
3047 LOGD("VirtualKeys: Generating key up: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07003048 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003049#endif
3050 keyEventAction = AKEY_EVENT_ACTION_UP;
3051 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
3052 touchResult = SKIP_TOUCH;
3053 goto DispatchVirtualKey;
3054 }
3055
3056 if (mCurrentTouch.pointerCount == 1) {
3057 int32_t x = mCurrentTouch.pointers[0].x;
3058 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003059 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
3060 if (virtualKey && virtualKey->keyCode == mLocked.currentVirtualKey.keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003061 // Pointer is still within the space of the virtual key.
3062 return SKIP_TOUCH;
3063 }
3064 }
3065
3066 // Pointer left virtual key area or another pointer also went down.
3067 // Send key cancellation and drop the stroke so subsequent motions will be
3068 // considered fresh downs. This is useful when the user swipes away from the
3069 // virtual key area into the main display surface.
Jeff Brown6328cdc2010-07-29 18:18:33 -07003070 mLocked.currentVirtualKey.down = false;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003071#if DEBUG_VIRTUAL_KEYS
3072 LOGD("VirtualKeys: Canceling key: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07003073 mLocked.currentVirtualKey.keyCode, mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003074#endif
3075 keyEventAction = AKEY_EVENT_ACTION_UP;
3076 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
3077 | AKEY_EVENT_FLAG_CANCELED;
Jeff Brownc3db8582010-10-20 15:33:38 -07003078
3079 // Check whether the pointer moved inside the display area where we should
3080 // start a new stroke.
3081 int32_t x = mCurrentTouch.pointers[0].x;
3082 int32_t y = mCurrentTouch.pointers[0].y;
3083 if (isPointInsideSurfaceLocked(x, y)) {
3084 mLastTouch.clear();
3085 touchResult = DISPATCH_TOUCH;
3086 } else {
3087 touchResult = DROP_STROKE;
3088 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07003089 } else {
3090 if (mCurrentTouch.pointerCount >= 1 && mLastTouch.pointerCount == 0) {
3091 // Pointer just went down. Handle off-screen touches, if needed.
3092 int32_t x = mCurrentTouch.pointers[0].x;
3093 int32_t y = mCurrentTouch.pointers[0].y;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003094 if (! isPointInsideSurfaceLocked(x, y)) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07003095 // If exactly one pointer went down, check for virtual key hit.
3096 // Otherwise we will drop the entire stroke.
3097 if (mCurrentTouch.pointerCount == 1) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003098 const VirtualKey* virtualKey = findVirtualKeyHitLocked(x, y);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003099 if (virtualKey) {
Jeff Brownfe508922011-01-18 15:10:10 -08003100 if (mContext->shouldDropVirtualKey(when, getDevice(),
3101 virtualKey->keyCode, virtualKey->scanCode)) {
3102 return DROP_STROKE;
3103 }
3104
Jeff Brown6328cdc2010-07-29 18:18:33 -07003105 mLocked.currentVirtualKey.down = true;
3106 mLocked.currentVirtualKey.downTime = when;
3107 mLocked.currentVirtualKey.keyCode = virtualKey->keyCode;
3108 mLocked.currentVirtualKey.scanCode = virtualKey->scanCode;
Jeff Brown6d0fec22010-07-23 21:28:06 -07003109#if DEBUG_VIRTUAL_KEYS
3110 LOGD("VirtualKeys: Generating key down: keyCode=%d, scanCode=%d",
Jeff Brownc3db8582010-10-20 15:33:38 -07003111 mLocked.currentVirtualKey.keyCode,
3112 mLocked.currentVirtualKey.scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -07003113#endif
3114 keyEventAction = AKEY_EVENT_ACTION_DOWN;
3115 keyEventFlags = AKEY_EVENT_FLAG_FROM_SYSTEM
3116 | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY;
3117 touchResult = SKIP_TOUCH;
3118 goto DispatchVirtualKey;
3119 }
3120 }
3121 return DROP_STROKE;
3122 }
3123 }
3124 return DISPATCH_TOUCH;
3125 }
3126
3127 DispatchVirtualKey:
3128 // Collect remaining state needed to dispatch virtual key.
Jeff Brown6328cdc2010-07-29 18:18:33 -07003129 keyCode = mLocked.currentVirtualKey.keyCode;
3130 scanCode = mLocked.currentVirtualKey.scanCode;
3131 downTime = mLocked.currentVirtualKey.downTime;
3132 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07003133
3134 // Dispatch virtual key.
3135 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brown0eaf3932010-10-01 14:55:30 -07003136 policyFlags |= POLICY_FLAG_VIRTUAL;
Jeff Brownb6997262010-10-08 22:31:17 -07003137 getDispatcher()->notifyKey(when, getDeviceId(), AINPUT_SOURCE_KEYBOARD, policyFlags,
3138 keyEventAction, keyEventFlags, keyCode, scanCode, metaState, downTime);
3139 return touchResult;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003140}
3141
Jeff Brownefd32662011-03-08 15:13:06 -08003142void TouchInputMapper::suppressSwipeOntoVirtualKeys(nsecs_t when) {
Jeff Brownfe508922011-01-18 15:10:10 -08003143 // Disable all virtual key touches that happen within a short time interval of the
3144 // most recent touch. The idea is to filter out stray virtual key presses when
3145 // interacting with the touch screen.
3146 //
3147 // Problems we're trying to solve:
3148 //
3149 // 1. While scrolling a list or dragging the window shade, the user swipes down into a
3150 // virtual key area that is implemented by a separate touch panel and accidentally
3151 // triggers a virtual key.
3152 //
3153 // 2. While typing in the on screen keyboard, the user taps slightly outside the screen
3154 // area and accidentally triggers a virtual key. This often happens when virtual keys
3155 // are layed out below the screen near to where the on screen keyboard's space bar
3156 // is displayed.
Jeff Brown214eaf42011-05-26 19:17:02 -07003157 if (mConfig->virtualKeyQuietTime > 0 && mCurrentTouch.pointerCount != 0) {
3158 mContext->disableVirtualKeysUntil(when + mConfig->virtualKeyQuietTime);
Jeff Brownfe508922011-01-18 15:10:10 -08003159 }
3160}
3161
Jeff Brown6d0fec22010-07-23 21:28:06 -07003162void TouchInputMapper::dispatchTouches(nsecs_t when, uint32_t policyFlags) {
3163 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
3164 uint32_t lastPointerCount = mLastTouch.pointerCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003165 if (currentPointerCount == 0 && lastPointerCount == 0) {
3166 return; // nothing to do!
3167 }
3168
Jeff Brownace13b12011-03-09 17:39:48 -08003169 // Update current touch coordinates.
3170 int32_t edgeFlags;
3171 float xPrecision, yPrecision;
3172 prepareTouches(&edgeFlags, &xPrecision, &yPrecision);
3173
3174 // Dispatch motions.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003175 BitSet32 currentIdBits = mCurrentTouch.idBits;
3176 BitSet32 lastIdBits = mLastTouch.idBits;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003177 int32_t metaState = getContext()->getGlobalMetaState();
3178 int32_t buttonState = mCurrentTouch.buttonState;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003179
3180 if (currentIdBits == lastIdBits) {
3181 // No pointer id changes so this is a move event.
3182 // The dispatcher takes care of batching moves so we don't have to deal with that here.
Jeff Brownace13b12011-03-09 17:39:48 -08003183 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003184 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState,
3185 AMOTION_EVENT_EDGE_FLAG_NONE,
3186 mCurrentTouchProperties, mCurrentTouchCoords,
3187 mCurrentTouch.idToIndex, currentIdBits, -1,
Jeff Brownace13b12011-03-09 17:39:48 -08003188 xPrecision, yPrecision, mDownTime);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003189 } else {
Jeff Brownc3db8582010-10-20 15:33:38 -07003190 // There may be pointers going up and pointers going down and pointers moving
3191 // all at the same time.
Jeff Brownace13b12011-03-09 17:39:48 -08003192 BitSet32 upIdBits(lastIdBits.value & ~currentIdBits.value);
3193 BitSet32 downIdBits(currentIdBits.value & ~lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003194 BitSet32 moveIdBits(lastIdBits.value & currentIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003195 BitSet32 dispatchedIdBits(lastIdBits.value);
Jeff Brownc3db8582010-10-20 15:33:38 -07003196
Jeff Brownace13b12011-03-09 17:39:48 -08003197 // Update last coordinates of pointers that have moved so that we observe the new
3198 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003199 bool moveNeeded = updateMovedPointers(
3200 mCurrentTouchProperties, mCurrentTouchCoords, mCurrentTouch.idToIndex,
3201 mLastTouchProperties, mLastTouchCoords, mLastTouch.idToIndex,
Jeff Brownace13b12011-03-09 17:39:48 -08003202 moveIdBits);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003203 if (buttonState != mLastTouch.buttonState) {
3204 moveNeeded = true;
3205 }
Jeff Brownc3db8582010-10-20 15:33:38 -07003206
Jeff Brownace13b12011-03-09 17:39:48 -08003207 // Dispatch pointer up events.
Jeff Brownc3db8582010-10-20 15:33:38 -07003208 while (!upIdBits.isEmpty()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003209 uint32_t upId = upIdBits.firstMarkedBit();
3210 upIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003211
Jeff Brownace13b12011-03-09 17:39:48 -08003212 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003213 AMOTION_EVENT_ACTION_POINTER_UP, 0, metaState, buttonState, 0,
3214 mLastTouchProperties, mLastTouchCoords,
3215 mLastTouch.idToIndex, dispatchedIdBits, upId,
Jeff Brownace13b12011-03-09 17:39:48 -08003216 xPrecision, yPrecision, mDownTime);
3217 dispatchedIdBits.clearBit(upId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003218 }
3219
Jeff Brownc3db8582010-10-20 15:33:38 -07003220 // Dispatch move events if any of the remaining pointers moved from their old locations.
3221 // Although applications receive new locations as part of individual pointer up
3222 // events, they do not generally handle them except when presented in a move event.
3223 if (moveNeeded) {
Jeff Brownb6110c22011-04-01 16:15:13 -07003224 LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08003225 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003226 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, 0,
3227 mCurrentTouchProperties, mCurrentTouchCoords,
3228 mCurrentTouch.idToIndex, dispatchedIdBits, -1,
Jeff Brownace13b12011-03-09 17:39:48 -08003229 xPrecision, yPrecision, mDownTime);
Jeff Brownc3db8582010-10-20 15:33:38 -07003230 }
3231
3232 // Dispatch pointer down events using the new pointer locations.
3233 while (!downIdBits.isEmpty()) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003234 uint32_t downId = downIdBits.firstMarkedBit();
3235 downIdBits.clearBit(downId);
Jeff Brownace13b12011-03-09 17:39:48 -08003236 dispatchedIdBits.markBit(downId);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003237
Jeff Brownace13b12011-03-09 17:39:48 -08003238 if (dispatchedIdBits.count() == 1) {
3239 // First pointer is going down. Set down time.
Jeff Brown6d0fec22010-07-23 21:28:06 -07003240 mDownTime = when;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003241 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08003242 // Only send edge flags with first pointer down.
3243 edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003244 }
3245
Jeff Brownace13b12011-03-09 17:39:48 -08003246 dispatchMotion(when, policyFlags, mTouchSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003247 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, edgeFlags,
3248 mCurrentTouchProperties, mCurrentTouchCoords,
3249 mCurrentTouch.idToIndex, dispatchedIdBits, downId,
Jeff Brownace13b12011-03-09 17:39:48 -08003250 xPrecision, yPrecision, mDownTime);
3251 }
3252 }
3253
3254 // Update state for next time.
3255 for (uint32_t i = 0; i < currentPointerCount; i++) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003256 mLastTouchProperties[i].copyFrom(mCurrentTouchProperties[i]);
Jeff Brownace13b12011-03-09 17:39:48 -08003257 mLastTouchCoords[i].copyFrom(mCurrentTouchCoords[i]);
3258 }
3259}
3260
3261void TouchInputMapper::prepareTouches(int32_t* outEdgeFlags,
3262 float* outXPrecision, float* outYPrecision) {
3263 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
3264 uint32_t lastPointerCount = mLastTouch.pointerCount;
3265
3266 AutoMutex _l(mLock);
3267
3268 // Walk through the the active pointers and map touch screen coordinates (TouchData) into
3269 // display or surface coordinates (PointerCoords) and adjust for display orientation.
3270 for (uint32_t i = 0; i < currentPointerCount; i++) {
3271 const PointerData& in = mCurrentTouch.pointers[i];
3272
3273 // ToolMajor and ToolMinor
3274 float toolMajor, toolMinor;
3275 switch (mCalibration.toolSizeCalibration) {
3276 case Calibration::TOOL_SIZE_CALIBRATION_GEOMETRIC:
3277 toolMajor = in.toolMajor * mLocked.geometricScale;
3278 if (mRawAxes.toolMinor.valid) {
3279 toolMinor = in.toolMinor * mLocked.geometricScale;
3280 } else {
3281 toolMinor = toolMajor;
3282 }
3283 break;
3284 case Calibration::TOOL_SIZE_CALIBRATION_LINEAR:
3285 toolMajor = in.toolMajor != 0
3286 ? in.toolMajor * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias
3287 : 0;
3288 if (mRawAxes.toolMinor.valid) {
3289 toolMinor = in.toolMinor != 0
3290 ? in.toolMinor * mLocked.toolSizeLinearScale
3291 + mLocked.toolSizeLinearBias
3292 : 0;
3293 } else {
3294 toolMinor = toolMajor;
3295 }
3296 break;
3297 case Calibration::TOOL_SIZE_CALIBRATION_AREA:
3298 if (in.toolMajor != 0) {
3299 float diameter = sqrtf(in.toolMajor
3300 * mLocked.toolSizeAreaScale + mLocked.toolSizeAreaBias);
3301 toolMajor = diameter * mLocked.toolSizeLinearScale + mLocked.toolSizeLinearBias;
3302 } else {
3303 toolMajor = 0;
3304 }
3305 toolMinor = toolMajor;
3306 break;
3307 default:
3308 toolMajor = 0;
3309 toolMinor = 0;
3310 break;
3311 }
3312
3313 if (mCalibration.haveToolSizeIsSummed && mCalibration.toolSizeIsSummed) {
3314 toolMajor /= currentPointerCount;
3315 toolMinor /= currentPointerCount;
3316 }
3317
3318 // Pressure
3319 float rawPressure;
3320 switch (mCalibration.pressureSource) {
3321 case Calibration::PRESSURE_SOURCE_PRESSURE:
3322 rawPressure = in.pressure;
3323 break;
3324 case Calibration::PRESSURE_SOURCE_TOUCH:
3325 rawPressure = in.touchMajor;
3326 break;
3327 default:
3328 rawPressure = 0;
3329 }
3330
3331 float pressure;
3332 switch (mCalibration.pressureCalibration) {
3333 case Calibration::PRESSURE_CALIBRATION_PHYSICAL:
3334 case Calibration::PRESSURE_CALIBRATION_AMPLITUDE:
3335 pressure = rawPressure * mLocked.pressureScale;
3336 break;
3337 default:
3338 pressure = 1;
3339 break;
3340 }
3341
3342 // TouchMajor and TouchMinor
3343 float touchMajor, touchMinor;
3344 switch (mCalibration.touchSizeCalibration) {
3345 case Calibration::TOUCH_SIZE_CALIBRATION_GEOMETRIC:
3346 touchMajor = in.touchMajor * mLocked.geometricScale;
3347 if (mRawAxes.touchMinor.valid) {
3348 touchMinor = in.touchMinor * mLocked.geometricScale;
3349 } else {
3350 touchMinor = touchMajor;
3351 }
3352 break;
3353 case Calibration::TOUCH_SIZE_CALIBRATION_PRESSURE:
3354 touchMajor = toolMajor * pressure;
3355 touchMinor = toolMinor * pressure;
3356 break;
3357 default:
3358 touchMajor = 0;
3359 touchMinor = 0;
3360 break;
3361 }
3362
3363 if (touchMajor > toolMajor) {
3364 touchMajor = toolMajor;
3365 }
3366 if (touchMinor > toolMinor) {
3367 touchMinor = toolMinor;
3368 }
3369
3370 // Size
3371 float size;
3372 switch (mCalibration.sizeCalibration) {
3373 case Calibration::SIZE_CALIBRATION_NORMALIZED: {
3374 float rawSize = mRawAxes.toolMinor.valid
3375 ? avg(in.toolMajor, in.toolMinor)
3376 : in.toolMajor;
3377 size = rawSize * mLocked.sizeScale;
3378 break;
3379 }
3380 default:
3381 size = 0;
3382 break;
3383 }
3384
3385 // Orientation
3386 float orientation;
3387 switch (mCalibration.orientationCalibration) {
3388 case Calibration::ORIENTATION_CALIBRATION_INTERPOLATED:
3389 orientation = in.orientation * mLocked.orientationScale;
3390 break;
3391 case Calibration::ORIENTATION_CALIBRATION_VECTOR: {
3392 int32_t c1 = signExtendNybble((in.orientation & 0xf0) >> 4);
3393 int32_t c2 = signExtendNybble(in.orientation & 0x0f);
3394 if (c1 != 0 || c2 != 0) {
3395 orientation = atan2f(c1, c2) * 0.5f;
Jeff Brown2352b972011-04-12 22:39:53 -07003396 float scale = 1.0f + hypotf(c1, c2) / 16.0f;
Jeff Brownace13b12011-03-09 17:39:48 -08003397 touchMajor *= scale;
3398 touchMinor /= scale;
3399 toolMajor *= scale;
3400 toolMinor /= scale;
3401 } else {
3402 orientation = 0;
3403 }
3404 break;
3405 }
3406 default:
3407 orientation = 0;
3408 }
3409
Jeff Brown80fd47c2011-05-24 01:07:44 -07003410 // Distance
3411 float distance;
3412 switch (mCalibration.distanceCalibration) {
3413 case Calibration::DISTANCE_CALIBRATION_SCALED:
3414 distance = in.distance * mLocked.distanceScale;
3415 break;
3416 default:
3417 distance = 0;
3418 }
3419
Jeff Brownace13b12011-03-09 17:39:48 -08003420 // X and Y
3421 // Adjust coords for surface orientation.
3422 float x, y;
3423 switch (mLocked.surfaceOrientation) {
3424 case DISPLAY_ORIENTATION_90:
3425 x = float(in.y - mRawAxes.y.minValue) * mLocked.yScale;
3426 y = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale;
3427 orientation -= M_PI_2;
3428 if (orientation < - M_PI_2) {
3429 orientation += M_PI;
3430 }
3431 break;
3432 case DISPLAY_ORIENTATION_180:
3433 x = float(mRawAxes.x.maxValue - in.x) * mLocked.xScale;
3434 y = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale;
3435 break;
3436 case DISPLAY_ORIENTATION_270:
3437 x = float(mRawAxes.y.maxValue - in.y) * mLocked.yScale;
3438 y = float(in.x - mRawAxes.x.minValue) * mLocked.xScale;
3439 orientation += M_PI_2;
3440 if (orientation > M_PI_2) {
3441 orientation -= M_PI;
3442 }
3443 break;
3444 default:
3445 x = float(in.x - mRawAxes.x.minValue) * mLocked.xScale;
3446 y = float(in.y - mRawAxes.y.minValue) * mLocked.yScale;
3447 break;
3448 }
3449
3450 // Write output coords.
3451 PointerCoords& out = mCurrentTouchCoords[i];
3452 out.clear();
3453 out.setAxisValue(AMOTION_EVENT_AXIS_X, x);
3454 out.setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3455 out.setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, pressure);
3456 out.setAxisValue(AMOTION_EVENT_AXIS_SIZE, size);
3457 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR, touchMajor);
3458 out.setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR, touchMinor);
3459 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR, toolMajor);
3460 out.setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR, toolMinor);
3461 out.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, orientation);
Jeff Brown80fd47c2011-05-24 01:07:44 -07003462 if (distance != 0) {
3463 out.setAxisValue(AMOTION_EVENT_AXIS_DISTANCE, distance);
3464 }
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003465
3466 // Write output properties.
3467 PointerProperties& properties = mCurrentTouchProperties[i];
3468 properties.clear();
3469 properties.id = mCurrentTouch.pointers[i].id;
3470 properties.toolType = getTouchToolType(mCurrentTouch.pointers[i].isStylus);
Jeff Brownace13b12011-03-09 17:39:48 -08003471 }
3472
3473 // Check edge flags by looking only at the first pointer since the flags are
3474 // global to the event.
3475 *outEdgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
3476 if (lastPointerCount == 0 && currentPointerCount > 0) {
3477 const PointerData& in = mCurrentTouch.pointers[0];
3478
3479 if (in.x <= mRawAxes.x.minValue) {
3480 *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_LEFT,
3481 mLocked.surfaceOrientation);
3482 } else if (in.x >= mRawAxes.x.maxValue) {
3483 *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_RIGHT,
3484 mLocked.surfaceOrientation);
3485 }
3486 if (in.y <= mRawAxes.y.minValue) {
3487 *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_TOP,
3488 mLocked.surfaceOrientation);
3489 } else if (in.y >= mRawAxes.y.maxValue) {
3490 *outEdgeFlags |= rotateEdgeFlag(AMOTION_EVENT_EDGE_FLAG_BOTTOM,
3491 mLocked.surfaceOrientation);
3492 }
3493 }
3494
3495 *outXPrecision = mLocked.orientedXPrecision;
3496 *outYPrecision = mLocked.orientedYPrecision;
3497}
3498
Jeff Brown79ac9692011-04-19 21:20:10 -07003499void TouchInputMapper::dispatchPointerGestures(nsecs_t when, uint32_t policyFlags,
3500 bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003501 // Update current gesture coordinates.
3502 bool cancelPreviousGesture, finishPreviousGesture;
Jeff Brown79ac9692011-04-19 21:20:10 -07003503 bool sendEvents = preparePointerGestures(when,
3504 &cancelPreviousGesture, &finishPreviousGesture, isTimeout);
3505 if (!sendEvents) {
3506 return;
3507 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003508 if (finishPreviousGesture) {
3509 cancelPreviousGesture = false;
3510 }
Jeff Brownace13b12011-03-09 17:39:48 -08003511
Jeff Brown214eaf42011-05-26 19:17:02 -07003512 // Switch pointer presentation.
3513 mPointerController->setPresentation(
3514 mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3515 ? PointerControllerInterface::PRESENTATION_SPOT
3516 : PointerControllerInterface::PRESENTATION_POINTER);
3517
Jeff Brown538881e2011-05-25 18:23:38 -07003518 // Show or hide the pointer if needed.
3519 switch (mPointerGesture.currentGestureMode) {
3520 case PointerGesture::NEUTRAL:
3521 case PointerGesture::QUIET:
3522 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
3523 && (mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3524 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)) {
3525 // Remind the user of where the pointer is after finishing a gesture with spots.
3526 mPointerController->unfade(PointerControllerInterface::TRANSITION_GRADUAL);
3527 }
3528 break;
3529 case PointerGesture::TAP:
3530 case PointerGesture::TAP_DRAG:
3531 case PointerGesture::BUTTON_CLICK_OR_DRAG:
3532 case PointerGesture::HOVER:
3533 case PointerGesture::PRESS:
3534 // Unfade the pointer when the current gesture manipulates the
3535 // area directly under the pointer.
3536 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3537 break;
3538 case PointerGesture::SWIPE:
3539 case PointerGesture::FREEFORM:
3540 // Fade the pointer when the current gesture manipulates a different
3541 // area and there are spots to guide the user experience.
3542 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3543 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
3544 } else {
3545 mPointerController->unfade(PointerControllerInterface::TRANSITION_IMMEDIATE);
3546 }
3547 break;
Jeff Brown2352b972011-04-12 22:39:53 -07003548 }
3549
Jeff Brownace13b12011-03-09 17:39:48 -08003550 // Send events!
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003551 int32_t metaState = getContext()->getGlobalMetaState();
3552 int32_t buttonState = mCurrentTouch.buttonState;
Jeff Brownace13b12011-03-09 17:39:48 -08003553
3554 // Update last coordinates of pointers that have moved so that we observe the new
3555 // pointer positions at the same time as other pointers that have just gone up.
Jeff Brown79ac9692011-04-19 21:20:10 -07003556 bool down = mPointerGesture.currentGestureMode == PointerGesture::TAP
3557 || mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG
3558 || mPointerGesture.currentGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07003559 || mPointerGesture.currentGestureMode == PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08003560 || mPointerGesture.currentGestureMode == PointerGesture::SWIPE
3561 || mPointerGesture.currentGestureMode == PointerGesture::FREEFORM;
3562 bool moveNeeded = false;
3563 if (down && !cancelPreviousGesture && !finishPreviousGesture
Jeff Brown2352b972011-04-12 22:39:53 -07003564 && !mPointerGesture.lastGestureIdBits.isEmpty()
3565 && !mPointerGesture.currentGestureIdBits.isEmpty()) {
Jeff Brownace13b12011-03-09 17:39:48 -08003566 BitSet32 movedGestureIdBits(mPointerGesture.currentGestureIdBits.value
3567 & mPointerGesture.lastGestureIdBits.value);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003568 moveNeeded = updateMovedPointers(mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003569 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003570 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003571 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3572 movedGestureIdBits);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003573 if (buttonState != mLastTouch.buttonState) {
3574 moveNeeded = true;
3575 }
Jeff Brownace13b12011-03-09 17:39:48 -08003576 }
3577
3578 // Send motion events for all pointers that went up or were canceled.
3579 BitSet32 dispatchedGestureIdBits(mPointerGesture.lastGestureIdBits);
3580 if (!dispatchedGestureIdBits.isEmpty()) {
3581 if (cancelPreviousGesture) {
3582 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003583 AMOTION_EVENT_ACTION_CANCEL, 0, metaState, buttonState,
3584 AMOTION_EVENT_EDGE_FLAG_NONE,
3585 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003586 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3587 dispatchedGestureIdBits, -1,
3588 0, 0, mPointerGesture.downTime);
3589
3590 dispatchedGestureIdBits.clear();
3591 } else {
3592 BitSet32 upGestureIdBits;
3593 if (finishPreviousGesture) {
3594 upGestureIdBits = dispatchedGestureIdBits;
3595 } else {
3596 upGestureIdBits.value = dispatchedGestureIdBits.value
3597 & ~mPointerGesture.currentGestureIdBits.value;
3598 }
3599 while (!upGestureIdBits.isEmpty()) {
3600 uint32_t id = upGestureIdBits.firstMarkedBit();
3601 upGestureIdBits.clearBit(id);
3602
3603 dispatchMotion(when, policyFlags, mPointerSource,
3604 AMOTION_EVENT_ACTION_POINTER_UP, 0,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003605 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3606 mPointerGesture.lastGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003607 mPointerGesture.lastGestureCoords, mPointerGesture.lastGestureIdToIndex,
3608 dispatchedGestureIdBits, id,
3609 0, 0, mPointerGesture.downTime);
3610
3611 dispatchedGestureIdBits.clearBit(id);
3612 }
3613 }
3614 }
3615
3616 // Send motion events for all pointers that moved.
3617 if (moveNeeded) {
3618 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003619 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3620 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003621 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3622 dispatchedGestureIdBits, -1,
3623 0, 0, mPointerGesture.downTime);
3624 }
3625
3626 // Send motion events for all pointers that went down.
3627 if (down) {
3628 BitSet32 downGestureIdBits(mPointerGesture.currentGestureIdBits.value
3629 & ~dispatchedGestureIdBits.value);
3630 while (!downGestureIdBits.isEmpty()) {
3631 uint32_t id = downGestureIdBits.firstMarkedBit();
3632 downGestureIdBits.clearBit(id);
3633 dispatchedGestureIdBits.markBit(id);
3634
3635 int32_t edgeFlags = AMOTION_EVENT_EDGE_FLAG_NONE;
3636 if (dispatchedGestureIdBits.count() == 1) {
3637 // First pointer is going down. Calculate edge flags and set down time.
3638 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
3639 const PointerCoords& downCoords = mPointerGesture.currentGestureCoords[index];
3640 edgeFlags = calculateEdgeFlagsUsingPointerBounds(mPointerController,
3641 downCoords.getAxisValue(AMOTION_EVENT_AXIS_X),
3642 downCoords.getAxisValue(AMOTION_EVENT_AXIS_Y));
3643 mPointerGesture.downTime = when;
3644 }
3645
3646 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003647 AMOTION_EVENT_ACTION_POINTER_DOWN, 0, metaState, buttonState, edgeFlags,
3648 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003649 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3650 dispatchedGestureIdBits, id,
3651 0, 0, mPointerGesture.downTime);
3652 }
3653 }
3654
Jeff Brownace13b12011-03-09 17:39:48 -08003655 // Send motion events for hover.
3656 if (mPointerGesture.currentGestureMode == PointerGesture::HOVER) {
3657 dispatchMotion(when, policyFlags, mPointerSource,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003658 AMOTION_EVENT_ACTION_HOVER_MOVE, 0,
3659 metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
3660 mPointerGesture.currentGestureProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08003661 mPointerGesture.currentGestureCoords, mPointerGesture.currentGestureIdToIndex,
3662 mPointerGesture.currentGestureIdBits, -1,
3663 0, 0, mPointerGesture.downTime);
3664 }
3665
3666 // Update state.
3667 mPointerGesture.lastGestureMode = mPointerGesture.currentGestureMode;
3668 if (!down) {
Jeff Brownace13b12011-03-09 17:39:48 -08003669 mPointerGesture.lastGestureIdBits.clear();
3670 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08003671 mPointerGesture.lastGestureIdBits = mPointerGesture.currentGestureIdBits;
3672 for (BitSet32 idBits(mPointerGesture.currentGestureIdBits); !idBits.isEmpty(); ) {
3673 uint32_t id = idBits.firstMarkedBit();
3674 idBits.clearBit(id);
3675 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003676 mPointerGesture.lastGestureProperties[index].copyFrom(
3677 mPointerGesture.currentGestureProperties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08003678 mPointerGesture.lastGestureCoords[index].copyFrom(
3679 mPointerGesture.currentGestureCoords[index]);
3680 mPointerGesture.lastGestureIdToIndex[id] = index;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003681 }
3682 }
3683}
3684
Jeff Brown79ac9692011-04-19 21:20:10 -07003685bool TouchInputMapper::preparePointerGestures(nsecs_t when,
3686 bool* outCancelPreviousGesture, bool* outFinishPreviousGesture, bool isTimeout) {
Jeff Brownace13b12011-03-09 17:39:48 -08003687 *outCancelPreviousGesture = false;
3688 *outFinishPreviousGesture = false;
Jeff Brown6328cdc2010-07-29 18:18:33 -07003689
Jeff Brownace13b12011-03-09 17:39:48 -08003690 AutoMutex _l(mLock);
Jeff Brown6328cdc2010-07-29 18:18:33 -07003691
Jeff Brown79ac9692011-04-19 21:20:10 -07003692 // Handle TAP timeout.
3693 if (isTimeout) {
3694#if DEBUG_GESTURES
3695 LOGD("Gestures: Processing timeout");
3696#endif
3697
3698 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown214eaf42011-05-26 19:17:02 -07003699 if (when <= mPointerGesture.tapUpTime + mConfig->pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003700 // The tap/drag timeout has not yet expired.
Jeff Brown214eaf42011-05-26 19:17:02 -07003701 getContext()->requestTimeoutAtTime(mPointerGesture.tapUpTime
3702 + mConfig->pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07003703 } else {
3704 // The tap is finished.
3705#if DEBUG_GESTURES
3706 LOGD("Gestures: TAP finished");
3707#endif
3708 *outFinishPreviousGesture = true;
3709
3710 mPointerGesture.activeGestureId = -1;
3711 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
3712 mPointerGesture.currentGestureIdBits.clear();
3713
Jeff Brown19c97d462011-06-01 12:33:19 -07003714 mPointerGesture.pointerVelocityControl.reset();
3715
Jeff Brown79ac9692011-04-19 21:20:10 -07003716 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3717 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_NEUTRAL;
3718 mPointerGesture.spotIdBits.clear();
3719 moveSpotsLocked();
3720 }
3721 return true;
3722 }
3723 }
3724
3725 // We did not handle this timeout.
3726 return false;
3727 }
3728
Jeff Brownace13b12011-03-09 17:39:48 -08003729 // Update the velocity tracker.
3730 {
3731 VelocityTracker::Position positions[MAX_POINTERS];
3732 uint32_t count = 0;
3733 for (BitSet32 idBits(mCurrentTouch.idBits); !idBits.isEmpty(); count++) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07003734 uint32_t id = idBits.firstMarkedBit();
3735 idBits.clearBit(id);
Jeff Brownace13b12011-03-09 17:39:48 -08003736 uint32_t index = mCurrentTouch.idToIndex[id];
3737 positions[count].x = mCurrentTouch.pointers[index].x
3738 * mLocked.pointerGestureXMovementScale;
3739 positions[count].y = mCurrentTouch.pointers[index].y
3740 * mLocked.pointerGestureYMovementScale;
3741 }
3742 mPointerGesture.velocityTracker.addMovement(when, mCurrentTouch.idBits, positions);
3743 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003744
Jeff Brownace13b12011-03-09 17:39:48 -08003745 // Pick a new active touch id if needed.
3746 // Choose an arbitrary pointer that just went down, if there is one.
3747 // Otherwise choose an arbitrary remaining pointer.
3748 // This guarantees we always have an active touch id when there is at least one pointer.
Jeff Brown2352b972011-04-12 22:39:53 -07003749 // We keep the same active touch id for as long as possible.
3750 bool activeTouchChanged = false;
3751 int32_t lastActiveTouchId = mPointerGesture.activeTouchId;
3752 int32_t activeTouchId = lastActiveTouchId;
3753 if (activeTouchId < 0) {
3754 if (!mCurrentTouch.idBits.isEmpty()) {
3755 activeTouchChanged = true;
3756 activeTouchId = mPointerGesture.activeTouchId = mCurrentTouch.idBits.firstMarkedBit();
3757 mPointerGesture.firstTouchTime = when;
Jeff Brownace13b12011-03-09 17:39:48 -08003758 }
Jeff Brown2352b972011-04-12 22:39:53 -07003759 } else if (!mCurrentTouch.idBits.hasBit(activeTouchId)) {
3760 activeTouchChanged = true;
3761 if (!mCurrentTouch.idBits.isEmpty()) {
3762 activeTouchId = mPointerGesture.activeTouchId = mCurrentTouch.idBits.firstMarkedBit();
3763 } else {
3764 activeTouchId = mPointerGesture.activeTouchId = -1;
Jeff Brownace13b12011-03-09 17:39:48 -08003765 }
3766 }
3767
3768 // Determine whether we are in quiet time.
Jeff Brown2352b972011-04-12 22:39:53 -07003769 bool isQuietTime = false;
3770 if (activeTouchId < 0) {
3771 mPointerGesture.resetQuietTime();
3772 } else {
Jeff Brown214eaf42011-05-26 19:17:02 -07003773 isQuietTime = when < mPointerGesture.quietTime + mConfig->pointerGestureQuietInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07003774 if (!isQuietTime) {
3775 if ((mPointerGesture.lastGestureMode == PointerGesture::PRESS
3776 || mPointerGesture.lastGestureMode == PointerGesture::SWIPE
3777 || mPointerGesture.lastGestureMode == PointerGesture::FREEFORM)
3778 && mCurrentTouch.pointerCount < 2) {
3779 // Enter quiet time when exiting swipe or freeform state.
3780 // This is to prevent accidentally entering the hover state and flinging the
3781 // pointer when finishing a swipe and there is still one pointer left onscreen.
3782 isQuietTime = true;
Jeff Brown79ac9692011-04-19 21:20:10 -07003783 } else if (mPointerGesture.lastGestureMode == PointerGesture::BUTTON_CLICK_OR_DRAG
Jeff Brown2352b972011-04-12 22:39:53 -07003784 && mCurrentTouch.pointerCount >= 2
3785 && !isPointerDown(mCurrentTouch.buttonState)) {
3786 // Enter quiet time when releasing the button and there are still two or more
3787 // fingers down. This may indicate that one finger was used to press the button
3788 // but it has not gone up yet.
3789 isQuietTime = true;
3790 }
3791 if (isQuietTime) {
3792 mPointerGesture.quietTime = when;
3793 }
Jeff Brownace13b12011-03-09 17:39:48 -08003794 }
3795 }
3796
3797 // Switch states based on button and pointer state.
3798 if (isQuietTime) {
3799 // Case 1: Quiet time. (QUIET)
3800#if DEBUG_GESTURES
3801 LOGD("Gestures: QUIET for next %0.3fms",
3802 (mPointerGesture.quietTime + QUIET_INTERVAL - when) * 0.000001f);
3803#endif
3804 *outFinishPreviousGesture = true;
3805
3806 mPointerGesture.activeGestureId = -1;
3807 mPointerGesture.currentGestureMode = PointerGesture::QUIET;
Jeff Brownace13b12011-03-09 17:39:48 -08003808 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07003809
Jeff Brown19c97d462011-06-01 12:33:19 -07003810 mPointerGesture.pointerVelocityControl.reset();
3811
Jeff Brown2352b972011-04-12 22:39:53 -07003812 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3813 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_NEUTRAL;
3814 mPointerGesture.spotIdBits.clear();
3815 moveSpotsLocked();
3816 }
Jeff Brownace13b12011-03-09 17:39:48 -08003817 } else if (isPointerDown(mCurrentTouch.buttonState)) {
Jeff Brown79ac9692011-04-19 21:20:10 -07003818 // Case 2: Button is pressed. (BUTTON_CLICK_OR_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08003819 // The pointer follows the active touch point.
3820 // Emit DOWN, MOVE, UP events at the pointer location.
3821 //
3822 // Only the active touch matters; other fingers are ignored. This policy helps
3823 // to handle the case where the user places a second finger on the touch pad
3824 // to apply the necessary force to depress an integrated button below the surface.
3825 // We don't want the second finger to be delivered to applications.
3826 //
3827 // For this to work well, we need to make sure to track the pointer that is really
3828 // active. If the user first puts one finger down to click then adds another
3829 // finger to drag then the active pointer should switch to the finger that is
3830 // being dragged.
3831#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07003832 LOGD("Gestures: BUTTON_CLICK_OR_DRAG activeTouchId=%d, "
Jeff Brownace13b12011-03-09 17:39:48 -08003833 "currentTouchPointerCount=%d", activeTouchId, mCurrentTouch.pointerCount);
3834#endif
3835 // Reset state when just starting.
Jeff Brown79ac9692011-04-19 21:20:10 -07003836 if (mPointerGesture.lastGestureMode != PointerGesture::BUTTON_CLICK_OR_DRAG) {
Jeff Brownace13b12011-03-09 17:39:48 -08003837 *outFinishPreviousGesture = true;
3838 mPointerGesture.activeGestureId = 0;
3839 }
3840
3841 // Switch pointers if needed.
3842 // Find the fastest pointer and follow it.
Jeff Brown19c97d462011-06-01 12:33:19 -07003843 if (activeTouchId >= 0 && mCurrentTouch.pointerCount > 1) {
3844 int32_t bestId = -1;
3845 float bestSpeed = mConfig->pointerGestureDragMinSwitchSpeed;
3846 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
3847 uint32_t id = mCurrentTouch.pointers[i].id;
3848 float vx, vy;
3849 if (mPointerGesture.velocityTracker.getVelocity(id, &vx, &vy)) {
3850 float speed = hypotf(vx, vy);
3851 if (speed > bestSpeed) {
3852 bestId = id;
3853 bestSpeed = speed;
Jeff Brownace13b12011-03-09 17:39:48 -08003854 }
Jeff Brown8d608662010-08-30 03:02:23 -07003855 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003856 }
3857 if (bestId >= 0 && bestId != activeTouchId) {
3858 mPointerGesture.activeTouchId = activeTouchId = bestId;
3859 activeTouchChanged = true;
Jeff Brownace13b12011-03-09 17:39:48 -08003860#if DEBUG_GESTURES
Jeff Brown19c97d462011-06-01 12:33:19 -07003861 LOGD("Gestures: BUTTON_CLICK_OR_DRAG switched pointers, "
3862 "bestId=%d, bestSpeed=%0.3f", bestId, bestSpeed);
Jeff Brownace13b12011-03-09 17:39:48 -08003863#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07003864 }
Jeff Brown19c97d462011-06-01 12:33:19 -07003865 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003866
Jeff Brown19c97d462011-06-01 12:33:19 -07003867 if (activeTouchId >= 0 && mLastTouch.idBits.hasBit(activeTouchId)) {
3868 const PointerData& currentPointer =
3869 mCurrentTouch.pointers[mCurrentTouch.idToIndex[activeTouchId]];
3870 const PointerData& lastPointer =
3871 mLastTouch.pointers[mLastTouch.idToIndex[activeTouchId]];
3872 float deltaX = (currentPointer.x - lastPointer.x)
3873 * mLocked.pointerGestureXMovementScale;
3874 float deltaY = (currentPointer.y - lastPointer.y)
3875 * mLocked.pointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07003876
Jeff Brown19c97d462011-06-01 12:33:19 -07003877 mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
3878
3879 // Move the pointer using a relative motion.
3880 // When using spots, the click will occur at the position of the anchor
3881 // spot and all other spots will move there.
3882 mPointerController->move(deltaX, deltaY);
3883 } else {
3884 mPointerGesture.pointerVelocityControl.reset();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07003885 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07003886
Jeff Brownace13b12011-03-09 17:39:48 -08003887 float x, y;
3888 mPointerController->getPosition(&x, &y);
Jeff Brown91c69ab2011-02-14 17:03:18 -08003889
Jeff Brown79ac9692011-04-19 21:20:10 -07003890 mPointerGesture.currentGestureMode = PointerGesture::BUTTON_CLICK_OR_DRAG;
Jeff Brownace13b12011-03-09 17:39:48 -08003891 mPointerGesture.currentGestureIdBits.clear();
3892 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
3893 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003894 mPointerGesture.currentGestureProperties[0].clear();
3895 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
3896 mPointerGesture.currentGestureProperties[0].toolType =
3897 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08003898 mPointerGesture.currentGestureCoords[0].clear();
3899 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
3900 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
3901 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07003902
Jeff Brown2352b972011-04-12 22:39:53 -07003903 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3904 if (activeTouchId >= 0) {
3905 // Collapse all spots into one point at the pointer location.
3906 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_BUTTON_DRAG;
3907 mPointerGesture.spotIdBits.clear();
3908 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
3909 uint32_t id = mCurrentTouch.pointers[i].id;
3910 mPointerGesture.spotIdBits.markBit(id);
3911 mPointerGesture.spotIdToIndex[id] = i;
3912 mPointerGesture.spotCoords[i] = mPointerGesture.currentGestureCoords[0];
3913 }
3914 } else {
3915 // No fingers. Generate a spot at the pointer location so the
3916 // anchor appears to be pressed.
3917 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_BUTTON_CLICK;
3918 mPointerGesture.spotIdBits.clear();
3919 mPointerGesture.spotIdBits.markBit(0);
3920 mPointerGesture.spotIdToIndex[0] = 0;
3921 mPointerGesture.spotCoords[0] = mPointerGesture.currentGestureCoords[0];
3922 }
3923 moveSpotsLocked();
3924 }
Jeff Brownace13b12011-03-09 17:39:48 -08003925 } else if (mCurrentTouch.pointerCount == 0) {
3926 // Case 3. No fingers down and button is not pressed. (NEUTRAL)
3927 *outFinishPreviousGesture = true;
3928
Jeff Brown79ac9692011-04-19 21:20:10 -07003929 // Watch for taps coming out of HOVER or TAP_DRAG mode.
Jeff Brown214eaf42011-05-26 19:17:02 -07003930 // Checking for taps after TAP_DRAG allows us to detect double-taps.
Jeff Brownace13b12011-03-09 17:39:48 -08003931 bool tapped = false;
Jeff Brown79ac9692011-04-19 21:20:10 -07003932 if ((mPointerGesture.lastGestureMode == PointerGesture::HOVER
3933 || mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG)
Jeff Brown2352b972011-04-12 22:39:53 -07003934 && mLastTouch.pointerCount == 1) {
Jeff Brown214eaf42011-05-26 19:17:02 -07003935 if (when <= mPointerGesture.tapDownTime + mConfig->pointerGestureTapInterval) {
Jeff Brownace13b12011-03-09 17:39:48 -08003936 float x, y;
3937 mPointerController->getPosition(&x, &y);
Jeff Brown214eaf42011-05-26 19:17:02 -07003938 if (fabs(x - mPointerGesture.tapX) <= mConfig->pointerGestureTapSlop
3939 && fabs(y - mPointerGesture.tapY) <= mConfig->pointerGestureTapSlop) {
Jeff Brownace13b12011-03-09 17:39:48 -08003940#if DEBUG_GESTURES
3941 LOGD("Gestures: TAP");
3942#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07003943
3944 mPointerGesture.tapUpTime = when;
Jeff Brown214eaf42011-05-26 19:17:02 -07003945 getContext()->requestTimeoutAtTime(when
3946 + mConfig->pointerGestureTapDragInterval);
Jeff Brown79ac9692011-04-19 21:20:10 -07003947
Jeff Brownace13b12011-03-09 17:39:48 -08003948 mPointerGesture.activeGestureId = 0;
3949 mPointerGesture.currentGestureMode = PointerGesture::TAP;
Jeff Brownace13b12011-03-09 17:39:48 -08003950 mPointerGesture.currentGestureIdBits.clear();
3951 mPointerGesture.currentGestureIdBits.markBit(
3952 mPointerGesture.activeGestureId);
3953 mPointerGesture.currentGestureIdToIndex[
3954 mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003955 mPointerGesture.currentGestureProperties[0].clear();
3956 mPointerGesture.currentGestureProperties[0].id =
3957 mPointerGesture.activeGestureId;
3958 mPointerGesture.currentGestureProperties[0].toolType =
3959 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08003960 mPointerGesture.currentGestureCoords[0].clear();
3961 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07003962 AMOTION_EVENT_AXIS_X, mPointerGesture.tapX);
Jeff Brownace13b12011-03-09 17:39:48 -08003963 mPointerGesture.currentGestureCoords[0].setAxisValue(
Jeff Brown2352b972011-04-12 22:39:53 -07003964 AMOTION_EVENT_AXIS_Y, mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08003965 mPointerGesture.currentGestureCoords[0].setAxisValue(
3966 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07003967
Jeff Brown2352b972011-04-12 22:39:53 -07003968 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
3969 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_TAP;
3970 mPointerGesture.spotIdBits.clear();
3971 mPointerGesture.spotIdBits.markBit(lastActiveTouchId);
3972 mPointerGesture.spotIdToIndex[lastActiveTouchId] = 0;
3973 mPointerGesture.spotCoords[0] = mPointerGesture.currentGestureCoords[0];
3974 moveSpotsLocked();
3975 }
3976
Jeff Brownace13b12011-03-09 17:39:48 -08003977 tapped = true;
3978 } else {
3979#if DEBUG_GESTURES
3980 LOGD("Gestures: Not a TAP, deltaX=%f, deltaY=%f",
Jeff Brown2352b972011-04-12 22:39:53 -07003981 x - mPointerGesture.tapX,
3982 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08003983#endif
3984 }
3985 } else {
3986#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07003987 LOGD("Gestures: Not a TAP, %0.3fms since down",
3988 (when - mPointerGesture.tapDownTime) * 0.000001f);
Jeff Brownace13b12011-03-09 17:39:48 -08003989#endif
Jeff Brown6328cdc2010-07-29 18:18:33 -07003990 }
Jeff Brownace13b12011-03-09 17:39:48 -08003991 }
Jeff Brown2352b972011-04-12 22:39:53 -07003992
Jeff Brown19c97d462011-06-01 12:33:19 -07003993 mPointerGesture.pointerVelocityControl.reset();
3994
Jeff Brownace13b12011-03-09 17:39:48 -08003995 if (!tapped) {
3996#if DEBUG_GESTURES
3997 LOGD("Gestures: NEUTRAL");
3998#endif
3999 mPointerGesture.activeGestureId = -1;
4000 mPointerGesture.currentGestureMode = PointerGesture::NEUTRAL;
Jeff Brownace13b12011-03-09 17:39:48 -08004001 mPointerGesture.currentGestureIdBits.clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004002
Jeff Brown2352b972011-04-12 22:39:53 -07004003 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4004 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_NEUTRAL;
4005 mPointerGesture.spotIdBits.clear();
4006 moveSpotsLocked();
4007 }
Jeff Brownace13b12011-03-09 17:39:48 -08004008 }
4009 } else if (mCurrentTouch.pointerCount == 1) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004010 // Case 4. Exactly one finger down, button is not pressed. (HOVER or TAP_DRAG)
Jeff Brownace13b12011-03-09 17:39:48 -08004011 // The pointer follows the active touch point.
Jeff Brown79ac9692011-04-19 21:20:10 -07004012 // When in HOVER, emit HOVER_MOVE events at the pointer location.
4013 // When in TAP_DRAG, emit MOVE events at the pointer location.
Jeff Brownb6110c22011-04-01 16:15:13 -07004014 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004015
Jeff Brown79ac9692011-04-19 21:20:10 -07004016 mPointerGesture.currentGestureMode = PointerGesture::HOVER;
4017 if (mPointerGesture.lastGestureMode == PointerGesture::TAP) {
Jeff Brown214eaf42011-05-26 19:17:02 -07004018 if (when <= mPointerGesture.tapUpTime + mConfig->pointerGestureTapDragInterval) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004019 float x, y;
4020 mPointerController->getPosition(&x, &y);
Jeff Brown214eaf42011-05-26 19:17:02 -07004021 if (fabs(x - mPointerGesture.tapX) <= mConfig->pointerGestureTapSlop
4022 && fabs(y - mPointerGesture.tapY) <= mConfig->pointerGestureTapSlop) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004023 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4024 } else {
Jeff Brownace13b12011-03-09 17:39:48 -08004025#if DEBUG_GESTURES
Jeff Brown79ac9692011-04-19 21:20:10 -07004026 LOGD("Gestures: Not a TAP_DRAG, deltaX=%f, deltaY=%f",
4027 x - mPointerGesture.tapX,
4028 y - mPointerGesture.tapY);
Jeff Brownace13b12011-03-09 17:39:48 -08004029#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004030 }
4031 } else {
4032#if DEBUG_GESTURES
4033 LOGD("Gestures: Not a TAP_DRAG, %0.3fms time since up",
4034 (when - mPointerGesture.tapUpTime) * 0.000001f);
4035#endif
4036 }
4037 } else if (mPointerGesture.lastGestureMode == PointerGesture::TAP_DRAG) {
4038 mPointerGesture.currentGestureMode = PointerGesture::TAP_DRAG;
4039 }
Jeff Brownace13b12011-03-09 17:39:48 -08004040
4041 if (mLastTouch.idBits.hasBit(activeTouchId)) {
4042 const PointerData& currentPointer =
4043 mCurrentTouch.pointers[mCurrentTouch.idToIndex[activeTouchId]];
4044 const PointerData& lastPointer =
4045 mLastTouch.pointers[mLastTouch.idToIndex[activeTouchId]];
4046 float deltaX = (currentPointer.x - lastPointer.x)
4047 * mLocked.pointerGestureXMovementScale;
4048 float deltaY = (currentPointer.y - lastPointer.y)
4049 * mLocked.pointerGestureYMovementScale;
Jeff Brown2352b972011-04-12 22:39:53 -07004050
Jeff Brown19c97d462011-06-01 12:33:19 -07004051 mPointerGesture.pointerVelocityControl.move(when, &deltaX, &deltaY);
4052
Jeff Brown2352b972011-04-12 22:39:53 -07004053 // Move the pointer using a relative motion.
Jeff Brown79ac9692011-04-19 21:20:10 -07004054 // When using spots, the hover or drag will occur at the position of the anchor spot.
Jeff Brownace13b12011-03-09 17:39:48 -08004055 mPointerController->move(deltaX, deltaY);
Jeff Brown19c97d462011-06-01 12:33:19 -07004056 } else {
4057 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004058 }
4059
Jeff Brown79ac9692011-04-19 21:20:10 -07004060 bool down;
4061 if (mPointerGesture.currentGestureMode == PointerGesture::TAP_DRAG) {
4062#if DEBUG_GESTURES
4063 LOGD("Gestures: TAP_DRAG");
4064#endif
4065 down = true;
4066 } else {
4067#if DEBUG_GESTURES
4068 LOGD("Gestures: HOVER");
4069#endif
4070 *outFinishPreviousGesture = true;
4071 mPointerGesture.activeGestureId = 0;
4072 down = false;
4073 }
Jeff Brownace13b12011-03-09 17:39:48 -08004074
4075 float x, y;
4076 mPointerController->getPosition(&x, &y);
4077
Jeff Brownace13b12011-03-09 17:39:48 -08004078 mPointerGesture.currentGestureIdBits.clear();
4079 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4080 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004081 mPointerGesture.currentGestureProperties[0].clear();
4082 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4083 mPointerGesture.currentGestureProperties[0].toolType =
4084 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004085 mPointerGesture.currentGestureCoords[0].clear();
4086 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4087 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
Jeff Brown79ac9692011-04-19 21:20:10 -07004088 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
4089 down ? 1.0f : 0.0f);
4090
Jeff Brownace13b12011-03-09 17:39:48 -08004091 if (mLastTouch.pointerCount == 0 && mCurrentTouch.pointerCount != 0) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004092 mPointerGesture.resetTap();
4093 mPointerGesture.tapDownTime = when;
Jeff Brown2352b972011-04-12 22:39:53 -07004094 mPointerGesture.tapX = x;
4095 mPointerGesture.tapY = y;
4096 }
4097
Jeff Brown2352b972011-04-12 22:39:53 -07004098 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
Jeff Brown79ac9692011-04-19 21:20:10 -07004099 mPointerGesture.spotGesture = down ? PointerControllerInterface::SPOT_GESTURE_DRAG
4100 : PointerControllerInterface::SPOT_GESTURE_HOVER;
Jeff Brown2352b972011-04-12 22:39:53 -07004101 mPointerGesture.spotIdBits.clear();
4102 mPointerGesture.spotIdBits.markBit(activeTouchId);
4103 mPointerGesture.spotIdToIndex[activeTouchId] = 0;
4104 mPointerGesture.spotCoords[0] = mPointerGesture.currentGestureCoords[0];
4105 moveSpotsLocked();
Jeff Brownace13b12011-03-09 17:39:48 -08004106 }
4107 } else {
Jeff Brown2352b972011-04-12 22:39:53 -07004108 // Case 5. At least two fingers down, button is not pressed. (PRESS, SWIPE or FREEFORM)
4109 // We need to provide feedback for each finger that goes down so we cannot wait
4110 // for the fingers to move before deciding what to do.
Jeff Brownace13b12011-03-09 17:39:48 -08004111 //
Jeff Brown2352b972011-04-12 22:39:53 -07004112 // The ambiguous case is deciding what to do when there are two fingers down but they
4113 // have not moved enough to determine whether they are part of a drag or part of a
4114 // freeform gesture, or just a press or long-press at the pointer location.
4115 //
4116 // When there are two fingers we start with the PRESS hypothesis and we generate a
4117 // down at the pointer location.
4118 //
4119 // When the two fingers move enough or when additional fingers are added, we make
4120 // a decision to transition into SWIPE or FREEFORM mode accordingly.
Jeff Brownb6110c22011-04-01 16:15:13 -07004121 LOG_ASSERT(activeTouchId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004122
Jeff Brown214eaf42011-05-26 19:17:02 -07004123 bool settled = when >= mPointerGesture.firstTouchTime
4124 + mConfig->pointerGestureMultitouchSettleInterval;
Jeff Brown2352b972011-04-12 22:39:53 -07004125 if (mPointerGesture.lastGestureMode != PointerGesture::PRESS
Jeff Brownace13b12011-03-09 17:39:48 -08004126 && mPointerGesture.lastGestureMode != PointerGesture::SWIPE
4127 && mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
Jeff Brownace13b12011-03-09 17:39:48 -08004128 *outFinishPreviousGesture = true;
Jeff Brown19c97d462011-06-01 12:33:19 -07004129 } else if (!settled && mCurrentTouch.pointerCount > mLastTouch.pointerCount) {
4130 // Additional pointers have gone down but not yet settled.
4131 // Reset the gesture.
4132#if DEBUG_GESTURES
4133 LOGD("Gestures: Resetting gesture since additional pointers went down for MULTITOUCH, "
4134 "settle time remaining %0.3fms",
4135 (mPointerGesture.firstTouchTime + MULTITOUCH_SETTLE_INTERVAL - when)
4136 * 0.000001f);
4137#endif
4138 *outCancelPreviousGesture = true;
4139 } else {
4140 // Continue previous gesture.
4141 mPointerGesture.currentGestureMode = mPointerGesture.lastGestureMode;
4142 }
4143
4144 if (*outFinishPreviousGesture || *outCancelPreviousGesture) {
Jeff Brown2352b972011-04-12 22:39:53 -07004145 mPointerGesture.currentGestureMode = PointerGesture::PRESS;
4146 mPointerGesture.activeGestureId = 0;
Jeff Brown538881e2011-05-25 18:23:38 -07004147 mPointerGesture.referenceIdBits.clear();
Jeff Brown19c97d462011-06-01 12:33:19 -07004148 mPointerGesture.pointerVelocityControl.reset();
Jeff Brownace13b12011-03-09 17:39:48 -08004149
Jeff Brown2352b972011-04-12 22:39:53 -07004150 if (settled && mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS
4151 && mLastTouch.idBits.hasBit(mPointerGesture.activeTouchId)) {
4152 // The spot is already visible and has settled, use it as the reference point
4153 // for the gesture. Other spots will be positioned relative to this one.
4154#if DEBUG_GESTURES
4155 LOGD("Gestures: Using active spot as reference for MULTITOUCH, "
4156 "settle time expired %0.3fms ago",
4157 (when - mPointerGesture.firstTouchTime - MULTITOUCH_SETTLE_INTERVAL)
4158 * 0.000001f);
4159#endif
4160 const PointerData& d = mLastTouch.pointers[mLastTouch.idToIndex[
4161 mPointerGesture.activeTouchId]];
4162 mPointerGesture.referenceTouchX = d.x;
4163 mPointerGesture.referenceTouchY = d.y;
4164 const PointerCoords& c = mPointerGesture.spotCoords[mPointerGesture.spotIdToIndex[
4165 mPointerGesture.activeTouchId]];
4166 mPointerGesture.referenceGestureX = c.getAxisValue(AMOTION_EVENT_AXIS_X);
4167 mPointerGesture.referenceGestureY = c.getAxisValue(AMOTION_EVENT_AXIS_Y);
4168 } else {
Jeff Brown19c97d462011-06-01 12:33:19 -07004169 // Use the centroid and pointer location as the reference points for the gesture.
Jeff Brown2352b972011-04-12 22:39:53 -07004170#if DEBUG_GESTURES
4171 LOGD("Gestures: Using centroid as reference for MULTITOUCH, "
4172 "settle time remaining %0.3fms",
4173 (mPointerGesture.firstTouchTime + MULTITOUCH_SETTLE_INTERVAL - when)
4174 * 0.000001f);
4175#endif
Jeff Brown19c97d462011-06-01 12:33:19 -07004176 mCurrentTouch.getCentroid(&mPointerGesture.referenceTouchX,
4177 &mPointerGesture.referenceTouchY);
4178 mPointerController->getPosition(&mPointerGesture.referenceGestureX,
4179 &mPointerGesture.referenceGestureY);
Jeff Brownace13b12011-03-09 17:39:48 -08004180 }
Jeff Brown2352b972011-04-12 22:39:53 -07004181 }
Jeff Brownace13b12011-03-09 17:39:48 -08004182
Jeff Brown2352b972011-04-12 22:39:53 -07004183 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4184 float d;
4185 if (mCurrentTouch.pointerCount > 2) {
4186 // There are more than two pointers, switch to FREEFORM.
4187#if DEBUG_GESTURES
4188 LOGD("Gestures: PRESS transitioned to FREEFORM, number of pointers %d > 2",
4189 mCurrentTouch.pointerCount);
4190#endif
4191 *outCancelPreviousGesture = true;
Jeff Brownace13b12011-03-09 17:39:48 -08004192 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown2352b972011-04-12 22:39:53 -07004193 } else if (((d = distance(
4194 mCurrentTouch.pointers[0].x, mCurrentTouch.pointers[0].y,
4195 mCurrentTouch.pointers[1].x, mCurrentTouch.pointers[1].y))
4196 > mLocked.pointerGestureMaxSwipeWidth)) {
4197 // There are two pointers but they are too far apart, switch to FREEFORM.
4198#if DEBUG_GESTURES
4199 LOGD("Gestures: PRESS transitioned to FREEFORM, distance %0.3f > %0.3f",
4200 d, mLocked.pointerGestureMaxSwipeWidth);
4201#endif
4202 *outCancelPreviousGesture = true;
4203 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
4204 } else {
4205 // There are two pointers. Wait for both pointers to start moving
4206 // before deciding whether this is a SWIPE or FREEFORM gesture.
4207 uint32_t id1 = mCurrentTouch.pointers[0].id;
4208 uint32_t id2 = mCurrentTouch.pointers[1].id;
Jeff Brownace13b12011-03-09 17:39:48 -08004209
Jeff Brown2352b972011-04-12 22:39:53 -07004210 float vx1, vy1, vx2, vy2;
4211 mPointerGesture.velocityTracker.getVelocity(id1, &vx1, &vy1);
4212 mPointerGesture.velocityTracker.getVelocity(id2, &vx2, &vy2);
Jeff Brownace13b12011-03-09 17:39:48 -08004213
Jeff Brown2352b972011-04-12 22:39:53 -07004214 float speed1 = hypotf(vx1, vy1);
4215 float speed2 = hypotf(vx2, vy2);
Jeff Brown214eaf42011-05-26 19:17:02 -07004216 if (speed1 >= mConfig->pointerGestureMultitouchMinSpeed
4217 && speed2 >= mConfig->pointerGestureMultitouchMinSpeed) {
Jeff Brown2352b972011-04-12 22:39:53 -07004218 // Calculate the dot product of the velocity vectors.
Jeff Brownace13b12011-03-09 17:39:48 -08004219 // When the vectors are oriented in approximately the same direction,
4220 // the angle betweeen them is near zero and the cosine of the angle
4221 // approches 1.0. Recall that dot(v1, v2) = cos(angle) * mag(v1) * mag(v2).
Jeff Brown2352b972011-04-12 22:39:53 -07004222 float dot = vx1 * vx2 + vy1 * vy2;
4223 float cosine = dot / (speed1 * speed2); // denominator always > 0
Jeff Brown214eaf42011-05-26 19:17:02 -07004224 if (cosine >= mConfig->pointerGestureSwipeTransitionAngleCosine) {
Jeff Brown2352b972011-04-12 22:39:53 -07004225 // Pointers are moving in the same direction. Switch to SWIPE.
4226#if DEBUG_GESTURES
4227 LOGD("Gestures: PRESS transitioned to SWIPE, "
4228 "speed1 %0.3f >= %0.3f, speed2 %0.3f >= %0.3f, "
4229 "cosine %0.3f >= %0.3f",
4230 speed1, MULTITOUCH_MIN_SPEED, speed2, MULTITOUCH_MIN_SPEED,
4231 cosine, SWIPE_TRANSITION_ANGLE_COSINE);
4232#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004233 mPointerGesture.currentGestureMode = PointerGesture::SWIPE;
Jeff Brown2352b972011-04-12 22:39:53 -07004234 } else {
4235 // Pointers are moving in different directions. Switch to FREEFORM.
4236#if DEBUG_GESTURES
4237 LOGD("Gestures: PRESS transitioned to FREEFORM, "
4238 "speed1 %0.3f >= %0.3f, speed2 %0.3f >= %0.3f, "
4239 "cosine %0.3f < %0.3f",
4240 speed1, MULTITOUCH_MIN_SPEED, speed2, MULTITOUCH_MIN_SPEED,
4241 cosine, SWIPE_TRANSITION_ANGLE_COSINE);
4242#endif
4243 *outCancelPreviousGesture = true;
4244 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brownace13b12011-03-09 17:39:48 -08004245 }
4246 }
Jeff Brownace13b12011-03-09 17:39:48 -08004247 }
4248 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
Jeff Brown2352b972011-04-12 22:39:53 -07004249 // Switch from SWIPE to FREEFORM if additional pointers go down.
4250 // Cancel previous gesture.
4251 if (mCurrentTouch.pointerCount > 2) {
4252#if DEBUG_GESTURES
4253 LOGD("Gestures: SWIPE transitioned to FREEFORM, number of pointers %d > 2",
4254 mCurrentTouch.pointerCount);
4255#endif
Jeff Brownace13b12011-03-09 17:39:48 -08004256 *outCancelPreviousGesture = true;
4257 mPointerGesture.currentGestureMode = PointerGesture::FREEFORM;
Jeff Brown6328cdc2010-07-29 18:18:33 -07004258 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004259 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004260
Jeff Brown538881e2011-05-25 18:23:38 -07004261 // Clear the reference deltas for fingers not yet included in the reference calculation.
4262 for (BitSet32 idBits(mCurrentTouch.idBits.value & ~mPointerGesture.referenceIdBits.value);
4263 !idBits.isEmpty(); ) {
4264 uint32_t id = idBits.firstMarkedBit();
4265 idBits.clearBit(id);
4266
4267 mPointerGesture.referenceDeltas[id].dx = 0;
4268 mPointerGesture.referenceDeltas[id].dy = 0;
4269 }
4270 mPointerGesture.referenceIdBits = mCurrentTouch.idBits;
4271
Jeff Brown2352b972011-04-12 22:39:53 -07004272 // Move the reference points based on the overall group motion of the fingers.
4273 // The objective is to calculate a vector delta that is common to the movement
4274 // of all fingers.
4275 BitSet32 commonIdBits(mLastTouch.idBits.value & mCurrentTouch.idBits.value);
4276 if (!commonIdBits.isEmpty()) {
4277 float commonDeltaX = 0, commonDeltaY = 0;
4278 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4279 bool first = (idBits == commonIdBits);
4280 uint32_t id = idBits.firstMarkedBit();
4281 idBits.clearBit(id);
4282
4283 const PointerData& cpd = mCurrentTouch.pointers[mCurrentTouch.idToIndex[id]];
4284 const PointerData& lpd = mLastTouch.pointers[mLastTouch.idToIndex[id]];
Jeff Brown538881e2011-05-25 18:23:38 -07004285 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4286 delta.dx += cpd.x - lpd.x;
4287 delta.dy += cpd.y - lpd.y;
Jeff Brown2352b972011-04-12 22:39:53 -07004288
4289 if (first) {
Jeff Brown538881e2011-05-25 18:23:38 -07004290 commonDeltaX = delta.dx;
4291 commonDeltaY = delta.dy;
Jeff Brown2352b972011-04-12 22:39:53 -07004292 } else {
Jeff Brown538881e2011-05-25 18:23:38 -07004293 commonDeltaX = calculateCommonVector(commonDeltaX, delta.dx);
4294 commonDeltaY = calculateCommonVector(commonDeltaY, delta.dy);
Jeff Brown2352b972011-04-12 22:39:53 -07004295 }
4296 }
4297
Jeff Brown538881e2011-05-25 18:23:38 -07004298 if (commonDeltaX || commonDeltaY) {
4299 for (BitSet32 idBits(commonIdBits); !idBits.isEmpty(); ) {
4300 uint32_t id = idBits.firstMarkedBit();
4301 idBits.clearBit(id);
4302
4303 PointerGesture::Delta& delta = mPointerGesture.referenceDeltas[id];
4304 delta.dx = 0;
4305 delta.dy = 0;
4306 }
4307
4308 mPointerGesture.referenceTouchX += commonDeltaX;
4309 mPointerGesture.referenceTouchY += commonDeltaY;
Jeff Brown19c97d462011-06-01 12:33:19 -07004310
4311 commonDeltaX *= mLocked.pointerGestureXMovementScale;
4312 commonDeltaY *= mLocked.pointerGestureYMovementScale;
4313 mPointerGesture.pointerVelocityControl.move(when, &commonDeltaX, &commonDeltaY);
4314
4315 mPointerGesture.referenceGestureX += commonDeltaX;
4316 mPointerGesture.referenceGestureY += commonDeltaY;
4317
Jeff Brown538881e2011-05-25 18:23:38 -07004318 clampPositionUsingPointerBounds(mPointerController,
4319 &mPointerGesture.referenceGestureX,
4320 &mPointerGesture.referenceGestureY);
4321 }
Jeff Brown2352b972011-04-12 22:39:53 -07004322 }
4323
4324 // Report gestures.
4325 if (mPointerGesture.currentGestureMode == PointerGesture::PRESS) {
4326 // PRESS mode.
Jeff Brownace13b12011-03-09 17:39:48 -08004327#if DEBUG_GESTURES
Jeff Brown2352b972011-04-12 22:39:53 -07004328 LOGD("Gestures: PRESS activeTouchId=%d,"
Jeff Brownace13b12011-03-09 17:39:48 -08004329 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown2352b972011-04-12 22:39:53 -07004330 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004331#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004332 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004333
Jeff Brownace13b12011-03-09 17:39:48 -08004334 mPointerGesture.currentGestureIdBits.clear();
4335 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4336 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004337 mPointerGesture.currentGestureProperties[0].clear();
4338 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4339 mPointerGesture.currentGestureProperties[0].toolType =
4340 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004341 mPointerGesture.currentGestureCoords[0].clear();
Jeff Brown2352b972011-04-12 22:39:53 -07004342 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4343 mPointerGesture.referenceGestureX);
4344 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4345 mPointerGesture.referenceGestureY);
Jeff Brownace13b12011-03-09 17:39:48 -08004346 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
Jeff Brown2352b972011-04-12 22:39:53 -07004347
Jeff Brown2352b972011-04-12 22:39:53 -07004348 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4349 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_PRESS;
4350 }
4351 } else if (mPointerGesture.currentGestureMode == PointerGesture::SWIPE) {
4352 // SWIPE mode.
4353#if DEBUG_GESTURES
4354 LOGD("Gestures: SWIPE activeTouchId=%d,"
4355 "activeGestureId=%d, currentTouchPointerCount=%d",
4356 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
4357#endif
4358 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
4359
4360 mPointerGesture.currentGestureIdBits.clear();
4361 mPointerGesture.currentGestureIdBits.markBit(mPointerGesture.activeGestureId);
4362 mPointerGesture.currentGestureIdToIndex[mPointerGesture.activeGestureId] = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004363 mPointerGesture.currentGestureProperties[0].clear();
4364 mPointerGesture.currentGestureProperties[0].id = mPointerGesture.activeGestureId;
4365 mPointerGesture.currentGestureProperties[0].toolType =
4366 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brown2352b972011-04-12 22:39:53 -07004367 mPointerGesture.currentGestureCoords[0].clear();
4368 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_X,
4369 mPointerGesture.referenceGestureX);
4370 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_Y,
4371 mPointerGesture.referenceGestureY);
4372 mPointerGesture.currentGestureCoords[0].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4373
Jeff Brown2352b972011-04-12 22:39:53 -07004374 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4375 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_SWIPE;
4376 }
Jeff Brownace13b12011-03-09 17:39:48 -08004377 } else if (mPointerGesture.currentGestureMode == PointerGesture::FREEFORM) {
4378 // FREEFORM mode.
4379#if DEBUG_GESTURES
4380 LOGD("Gestures: FREEFORM activeTouchId=%d,"
4381 "activeGestureId=%d, currentTouchPointerCount=%d",
Jeff Brown2352b972011-04-12 22:39:53 -07004382 activeTouchId, mPointerGesture.activeGestureId, mCurrentTouch.pointerCount);
Jeff Brownace13b12011-03-09 17:39:48 -08004383#endif
Jeff Brownb6110c22011-04-01 16:15:13 -07004384 LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004385
Jeff Brownace13b12011-03-09 17:39:48 -08004386 mPointerGesture.currentGestureIdBits.clear();
4387
4388 BitSet32 mappedTouchIdBits;
4389 BitSet32 usedGestureIdBits;
4390 if (mPointerGesture.lastGestureMode != PointerGesture::FREEFORM) {
4391 // Initially, assign the active gesture id to the active touch point
4392 // if there is one. No other touch id bits are mapped yet.
4393 if (!*outCancelPreviousGesture) {
4394 mappedTouchIdBits.markBit(activeTouchId);
4395 usedGestureIdBits.markBit(mPointerGesture.activeGestureId);
4396 mPointerGesture.freeformTouchToGestureIdMap[activeTouchId] =
4397 mPointerGesture.activeGestureId;
4398 } else {
4399 mPointerGesture.activeGestureId = -1;
4400 }
4401 } else {
4402 // Otherwise, assume we mapped all touches from the previous frame.
4403 // Reuse all mappings that are still applicable.
4404 mappedTouchIdBits.value = mLastTouch.idBits.value & mCurrentTouch.idBits.value;
4405 usedGestureIdBits = mPointerGesture.lastGestureIdBits;
4406
4407 // Check whether we need to choose a new active gesture id because the
4408 // current went went up.
4409 for (BitSet32 upTouchIdBits(mLastTouch.idBits.value & ~mCurrentTouch.idBits.value);
4410 !upTouchIdBits.isEmpty(); ) {
4411 uint32_t upTouchId = upTouchIdBits.firstMarkedBit();
4412 upTouchIdBits.clearBit(upTouchId);
4413 uint32_t upGestureId = mPointerGesture.freeformTouchToGestureIdMap[upTouchId];
4414 if (upGestureId == uint32_t(mPointerGesture.activeGestureId)) {
4415 mPointerGesture.activeGestureId = -1;
4416 break;
4417 }
4418 }
4419 }
4420
4421#if DEBUG_GESTURES
4422 LOGD("Gestures: FREEFORM follow up "
4423 "mappedTouchIdBits=0x%08x, usedGestureIdBits=0x%08x, "
4424 "activeGestureId=%d",
4425 mappedTouchIdBits.value, usedGestureIdBits.value,
4426 mPointerGesture.activeGestureId);
4427#endif
4428
Jeff Brown2352b972011-04-12 22:39:53 -07004429 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
Jeff Brownace13b12011-03-09 17:39:48 -08004430 uint32_t touchId = mCurrentTouch.pointers[i].id;
4431 uint32_t gestureId;
4432 if (!mappedTouchIdBits.hasBit(touchId)) {
4433 gestureId = usedGestureIdBits.firstUnmarkedBit();
4434 usedGestureIdBits.markBit(gestureId);
4435 mPointerGesture.freeformTouchToGestureIdMap[touchId] = gestureId;
4436#if DEBUG_GESTURES
4437 LOGD("Gestures: FREEFORM "
4438 "new mapping for touch id %d -> gesture id %d",
4439 touchId, gestureId);
4440#endif
4441 } else {
4442 gestureId = mPointerGesture.freeformTouchToGestureIdMap[touchId];
4443#if DEBUG_GESTURES
4444 LOGD("Gestures: FREEFORM "
4445 "existing mapping for touch id %d -> gesture id %d",
4446 touchId, gestureId);
4447#endif
4448 }
4449 mPointerGesture.currentGestureIdBits.markBit(gestureId);
4450 mPointerGesture.currentGestureIdToIndex[gestureId] = i;
4451
Jeff Brown2352b972011-04-12 22:39:53 -07004452 float x = (mCurrentTouch.pointers[i].x - mPointerGesture.referenceTouchX)
4453 * mLocked.pointerGestureXZoomScale + mPointerGesture.referenceGestureX;
4454 float y = (mCurrentTouch.pointers[i].y - mPointerGesture.referenceTouchY)
4455 * mLocked.pointerGestureYZoomScale + mPointerGesture.referenceGestureY;
Jeff Brownace13b12011-03-09 17:39:48 -08004456
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004457 mPointerGesture.currentGestureProperties[i].clear();
4458 mPointerGesture.currentGestureProperties[i].id = gestureId;
4459 mPointerGesture.currentGestureProperties[i].toolType =
4460 AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
Jeff Brownace13b12011-03-09 17:39:48 -08004461 mPointerGesture.currentGestureCoords[i].clear();
4462 mPointerGesture.currentGestureCoords[i].setAxisValue(
4463 AMOTION_EVENT_AXIS_X, x);
4464 mPointerGesture.currentGestureCoords[i].setAxisValue(
4465 AMOTION_EVENT_AXIS_Y, y);
4466 mPointerGesture.currentGestureCoords[i].setAxisValue(
4467 AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4468 }
4469
4470 if (mPointerGesture.activeGestureId < 0) {
4471 mPointerGesture.activeGestureId =
4472 mPointerGesture.currentGestureIdBits.firstMarkedBit();
4473#if DEBUG_GESTURES
4474 LOGD("Gestures: FREEFORM new "
4475 "activeGestureId=%d", mPointerGesture.activeGestureId);
4476#endif
4477 }
Jeff Brownace13b12011-03-09 17:39:48 -08004478
Jeff Brown2352b972011-04-12 22:39:53 -07004479 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4480 mPointerGesture.spotGesture = PointerControllerInterface::SPOT_GESTURE_FREEFORM;
4481 }
4482 }
4483
4484 // Update spot locations for PRESS, SWIPE and FREEFORM.
4485 // We use the same calculation as we do to calculate the gesture pointers
4486 // for FREEFORM so that the spots smoothly track gestures.
4487 if (mParameters.gestureMode == Parameters::GESTURE_MODE_SPOTS) {
4488 mPointerGesture.spotIdBits.clear();
4489 for (uint32_t i = 0; i < mCurrentTouch.pointerCount; i++) {
4490 uint32_t id = mCurrentTouch.pointers[i].id;
4491 mPointerGesture.spotIdBits.markBit(id);
4492 mPointerGesture.spotIdToIndex[id] = i;
4493
4494 float x = (mCurrentTouch.pointers[i].x - mPointerGesture.referenceTouchX)
4495 * mLocked.pointerGestureXZoomScale + mPointerGesture.referenceGestureX;
4496 float y = (mCurrentTouch.pointers[i].y - mPointerGesture.referenceTouchY)
4497 * mLocked.pointerGestureYZoomScale + mPointerGesture.referenceGestureY;
4498
4499 mPointerGesture.spotCoords[i].clear();
4500 mPointerGesture.spotCoords[i].setAxisValue(AMOTION_EVENT_AXIS_X, x);
4501 mPointerGesture.spotCoords[i].setAxisValue(AMOTION_EVENT_AXIS_Y, y);
4502 mPointerGesture.spotCoords[i].setAxisValue(AMOTION_EVENT_AXIS_PRESSURE, 1.0f);
4503 }
4504 moveSpotsLocked();
4505 }
Jeff Brownace13b12011-03-09 17:39:48 -08004506 }
4507
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004508 mPointerController->setButtonState(mCurrentTouch.buttonState);
4509
Jeff Brownace13b12011-03-09 17:39:48 -08004510#if DEBUG_GESTURES
4511 LOGD("Gestures: finishPreviousGesture=%s, cancelPreviousGesture=%s, "
Jeff Brown2352b972011-04-12 22:39:53 -07004512 "currentGestureMode=%d, currentGestureIdBits=0x%08x, "
4513 "lastGestureMode=%d, lastGestureIdBits=0x%08x",
Jeff Brownace13b12011-03-09 17:39:48 -08004514 toString(*outFinishPreviousGesture), toString(*outCancelPreviousGesture),
Jeff Brown2352b972011-04-12 22:39:53 -07004515 mPointerGesture.currentGestureMode, mPointerGesture.currentGestureIdBits.value,
4516 mPointerGesture.lastGestureMode, mPointerGesture.lastGestureIdBits.value);
Jeff Brownace13b12011-03-09 17:39:48 -08004517 for (BitSet32 idBits = mPointerGesture.currentGestureIdBits; !idBits.isEmpty(); ) {
4518 uint32_t id = idBits.firstMarkedBit();
4519 idBits.clearBit(id);
4520 uint32_t index = mPointerGesture.currentGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004521 const PointerProperties& properties = mPointerGesture.currentGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004522 const PointerCoords& coords = mPointerGesture.currentGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004523 LOGD(" currentGesture[%d]: index=%d, toolType=%d, "
4524 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4525 id, index, properties.toolType,
4526 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004527 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4528 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4529 }
4530 for (BitSet32 idBits = mPointerGesture.lastGestureIdBits; !idBits.isEmpty(); ) {
4531 uint32_t id = idBits.firstMarkedBit();
4532 idBits.clearBit(id);
4533 uint32_t index = mPointerGesture.lastGestureIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004534 const PointerProperties& properties = mPointerGesture.lastGestureProperties[index];
Jeff Brownace13b12011-03-09 17:39:48 -08004535 const PointerCoords& coords = mPointerGesture.lastGestureCoords[index];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004536 LOGD(" lastGesture[%d]: index=%d, toolType=%d, "
4537 "x=%0.3f, y=%0.3f, pressure=%0.3f",
4538 id, index, properties.toolType,
4539 coords.getAxisValue(AMOTION_EVENT_AXIS_X),
Jeff Brownace13b12011-03-09 17:39:48 -08004540 coords.getAxisValue(AMOTION_EVENT_AXIS_Y),
4541 coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
4542 }
4543#endif
Jeff Brown79ac9692011-04-19 21:20:10 -07004544 return true;
Jeff Brownace13b12011-03-09 17:39:48 -08004545}
4546
Jeff Brown2352b972011-04-12 22:39:53 -07004547void TouchInputMapper::moveSpotsLocked() {
4548 mPointerController->setSpots(mPointerGesture.spotGesture,
4549 mPointerGesture.spotCoords, mPointerGesture.spotIdToIndex, mPointerGesture.spotIdBits);
4550}
4551
Jeff Brownace13b12011-03-09 17:39:48 -08004552void TouchInputMapper::dispatchMotion(nsecs_t when, uint32_t policyFlags, uint32_t source,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004553 int32_t action, int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags,
4554 const PointerProperties* properties, const PointerCoords* coords,
4555 const uint32_t* idToIndex, BitSet32 idBits,
Jeff Brownace13b12011-03-09 17:39:48 -08004556 int32_t changedId, float xPrecision, float yPrecision, nsecs_t downTime) {
4557 PointerCoords pointerCoords[MAX_POINTERS];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004558 PointerProperties pointerProperties[MAX_POINTERS];
Jeff Brownace13b12011-03-09 17:39:48 -08004559 uint32_t pointerCount = 0;
4560 while (!idBits.isEmpty()) {
4561 uint32_t id = idBits.firstMarkedBit();
4562 idBits.clearBit(id);
4563 uint32_t index = idToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004564 pointerProperties[pointerCount].copyFrom(properties[index]);
Jeff Brownace13b12011-03-09 17:39:48 -08004565 pointerCoords[pointerCount].copyFrom(coords[index]);
4566
4567 if (changedId >= 0 && id == uint32_t(changedId)) {
4568 action |= pointerCount << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
4569 }
4570
4571 pointerCount += 1;
4572 }
4573
Jeff Brownb6110c22011-04-01 16:15:13 -07004574 LOG_ASSERT(pointerCount != 0);
Jeff Brownace13b12011-03-09 17:39:48 -08004575
4576 if (changedId >= 0 && pointerCount == 1) {
4577 // Replace initial down and final up action.
4578 // We can compare the action without masking off the changed pointer index
4579 // because we know the index is 0.
4580 if (action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
4581 action = AMOTION_EVENT_ACTION_DOWN;
4582 } else if (action == AMOTION_EVENT_ACTION_POINTER_UP) {
4583 action = AMOTION_EVENT_ACTION_UP;
4584 } else {
4585 // Can't happen.
Jeff Brownb6110c22011-04-01 16:15:13 -07004586 LOG_ASSERT(false);
Jeff Brownace13b12011-03-09 17:39:48 -08004587 }
4588 }
4589
4590 getDispatcher()->notifyMotion(when, getDeviceId(), source, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004591 action, flags, metaState, buttonState, edgeFlags,
4592 pointerCount, pointerProperties, pointerCoords, xPrecision, yPrecision, downTime);
Jeff Brownace13b12011-03-09 17:39:48 -08004593}
4594
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004595bool TouchInputMapper::updateMovedPointers(const PointerProperties* inProperties,
Jeff Brownace13b12011-03-09 17:39:48 -08004596 const PointerCoords* inCoords, const uint32_t* inIdToIndex,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004597 PointerProperties* outProperties, PointerCoords* outCoords, const uint32_t* outIdToIndex,
4598 BitSet32 idBits) const {
Jeff Brownace13b12011-03-09 17:39:48 -08004599 bool changed = false;
4600 while (!idBits.isEmpty()) {
4601 uint32_t id = idBits.firstMarkedBit();
4602 idBits.clearBit(id);
4603
4604 uint32_t inIndex = inIdToIndex[id];
4605 uint32_t outIndex = outIdToIndex[id];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004606
4607 const PointerProperties& curInProperties = inProperties[inIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004608 const PointerCoords& curInCoords = inCoords[inIndex];
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004609 PointerProperties& curOutProperties = outProperties[outIndex];
Jeff Brownace13b12011-03-09 17:39:48 -08004610 PointerCoords& curOutCoords = outCoords[outIndex];
4611
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004612 if (curInProperties != curOutProperties) {
4613 curOutProperties.copyFrom(curInProperties);
4614 changed = true;
4615 }
4616
Jeff Brownace13b12011-03-09 17:39:48 -08004617 if (curInCoords != curOutCoords) {
4618 curOutCoords.copyFrom(curInCoords);
4619 changed = true;
4620 }
4621 }
4622 return changed;
4623}
4624
4625void TouchInputMapper::fadePointer() {
4626 { // acquire lock
4627 AutoMutex _l(mLock);
4628 if (mPointerController != NULL) {
Jeff Brown538881e2011-05-25 18:23:38 -07004629 mPointerController->fade(PointerControllerInterface::TRANSITION_GRADUAL);
Jeff Brownace13b12011-03-09 17:39:48 -08004630 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07004631 } // release lock
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07004632}
4633
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07004634int32_t TouchInputMapper::getTouchToolType(bool isStylus) const {
4635 if (mParameters.deviceType == Parameters::DEVICE_TYPE_TOUCH_SCREEN) {
4636 return isStylus ? AMOTION_EVENT_TOOL_TYPE_STYLUS : AMOTION_EVENT_TOOL_TYPE_FINGER;
4637 } else {
4638 return isStylus ? AMOTION_EVENT_TOOL_TYPE_INDIRECT_STYLUS
4639 : AMOTION_EVENT_TOOL_TYPE_INDIRECT_FINGER;
4640 }
4641}
4642
Jeff Brown6328cdc2010-07-29 18:18:33 -07004643bool TouchInputMapper::isPointInsideSurfaceLocked(int32_t x, int32_t y) {
Jeff Brown9626b142011-03-03 02:09:54 -08004644 return x >= mRawAxes.x.minValue && x <= mRawAxes.x.maxValue
4645 && y >= mRawAxes.y.minValue && y <= mRawAxes.y.maxValue;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004646}
4647
Jeff Brown6328cdc2010-07-29 18:18:33 -07004648const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHitLocked(
4649 int32_t x, int32_t y) {
4650 size_t numVirtualKeys = mLocked.virtualKeys.size();
4651 for (size_t i = 0; i < numVirtualKeys; i++) {
4652 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07004653
4654#if DEBUG_VIRTUAL_KEYS
4655 LOGD("VirtualKeys: Hit test (%d, %d): keyCode=%d, scanCode=%d, "
4656 "left=%d, top=%d, right=%d, bottom=%d",
4657 x, y,
4658 virtualKey.keyCode, virtualKey.scanCode,
4659 virtualKey.hitLeft, virtualKey.hitTop,
4660 virtualKey.hitRight, virtualKey.hitBottom);
4661#endif
4662
4663 if (virtualKey.isHit(x, y)) {
4664 return & virtualKey;
4665 }
4666 }
4667
4668 return NULL;
4669}
4670
4671void TouchInputMapper::calculatePointerIds() {
4672 uint32_t currentPointerCount = mCurrentTouch.pointerCount;
4673 uint32_t lastPointerCount = mLastTouch.pointerCount;
4674
4675 if (currentPointerCount == 0) {
4676 // No pointers to assign.
4677 mCurrentTouch.idBits.clear();
4678 } else if (lastPointerCount == 0) {
4679 // All pointers are new.
4680 mCurrentTouch.idBits.clear();
4681 for (uint32_t i = 0; i < currentPointerCount; i++) {
4682 mCurrentTouch.pointers[i].id = i;
4683 mCurrentTouch.idToIndex[i] = i;
4684 mCurrentTouch.idBits.markBit(i);
4685 }
4686 } else if (currentPointerCount == 1 && lastPointerCount == 1) {
4687 // Only one pointer and no change in count so it must have the same id as before.
4688 uint32_t id = mLastTouch.pointers[0].id;
4689 mCurrentTouch.pointers[0].id = id;
4690 mCurrentTouch.idToIndex[id] = 0;
4691 mCurrentTouch.idBits.value = BitSet32::valueForBit(id);
4692 } else {
4693 // General case.
4694 // We build a heap of squared euclidean distances between current and last pointers
4695 // associated with the current and last pointer indices. Then, we find the best
4696 // match (by distance) for each current pointer.
4697 PointerDistanceHeapElement heap[MAX_POINTERS * MAX_POINTERS];
4698
4699 uint32_t heapSize = 0;
4700 for (uint32_t currentPointerIndex = 0; currentPointerIndex < currentPointerCount;
4701 currentPointerIndex++) {
4702 for (uint32_t lastPointerIndex = 0; lastPointerIndex < lastPointerCount;
4703 lastPointerIndex++) {
4704 int64_t deltaX = mCurrentTouch.pointers[currentPointerIndex].x
4705 - mLastTouch.pointers[lastPointerIndex].x;
4706 int64_t deltaY = mCurrentTouch.pointers[currentPointerIndex].y
4707 - mLastTouch.pointers[lastPointerIndex].y;
4708
4709 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
4710
4711 // Insert new element into the heap (sift up).
4712 heap[heapSize].currentPointerIndex = currentPointerIndex;
4713 heap[heapSize].lastPointerIndex = lastPointerIndex;
4714 heap[heapSize].distance = distance;
4715 heapSize += 1;
4716 }
4717 }
4718
4719 // Heapify
4720 for (uint32_t startIndex = heapSize / 2; startIndex != 0; ) {
4721 startIndex -= 1;
4722 for (uint32_t parentIndex = startIndex; ;) {
4723 uint32_t childIndex = parentIndex * 2 + 1;
4724 if (childIndex >= heapSize) {
4725 break;
4726 }
4727
4728 if (childIndex + 1 < heapSize
4729 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4730 childIndex += 1;
4731 }
4732
4733 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4734 break;
4735 }
4736
4737 swap(heap[parentIndex], heap[childIndex]);
4738 parentIndex = childIndex;
4739 }
4740 }
4741
4742#if DEBUG_POINTER_ASSIGNMENT
4743 LOGD("calculatePointerIds - initial distance min-heap: size=%d", heapSize);
4744 for (size_t i = 0; i < heapSize; i++) {
4745 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4746 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4747 heap[i].distance);
4748 }
4749#endif
4750
4751 // Pull matches out by increasing order of distance.
4752 // To avoid reassigning pointers that have already been matched, the loop keeps track
4753 // of which last and current pointers have been matched using the matchedXXXBits variables.
4754 // It also tracks the used pointer id bits.
4755 BitSet32 matchedLastBits(0);
4756 BitSet32 matchedCurrentBits(0);
4757 BitSet32 usedIdBits(0);
4758 bool first = true;
4759 for (uint32_t i = min(currentPointerCount, lastPointerCount); i > 0; i--) {
4760 for (;;) {
4761 if (first) {
4762 // The first time through the loop, we just consume the root element of
4763 // the heap (the one with smallest distance).
4764 first = false;
4765 } else {
4766 // Previous iterations consumed the root element of the heap.
4767 // Pop root element off of the heap (sift down).
4768 heapSize -= 1;
Jeff Brownb6110c22011-04-01 16:15:13 -07004769 LOG_ASSERT(heapSize > 0);
Jeff Brown6d0fec22010-07-23 21:28:06 -07004770
4771 // Sift down.
4772 heap[0] = heap[heapSize];
4773 for (uint32_t parentIndex = 0; ;) {
4774 uint32_t childIndex = parentIndex * 2 + 1;
4775 if (childIndex >= heapSize) {
4776 break;
4777 }
4778
4779 if (childIndex + 1 < heapSize
4780 && heap[childIndex + 1].distance < heap[childIndex].distance) {
4781 childIndex += 1;
4782 }
4783
4784 if (heap[parentIndex].distance <= heap[childIndex].distance) {
4785 break;
4786 }
4787
4788 swap(heap[parentIndex], heap[childIndex]);
4789 parentIndex = childIndex;
4790 }
4791
4792#if DEBUG_POINTER_ASSIGNMENT
4793 LOGD("calculatePointerIds - reduced distance min-heap: size=%d", heapSize);
4794 for (size_t i = 0; i < heapSize; i++) {
4795 LOGD(" heap[%d]: cur=%d, last=%d, distance=%lld",
4796 i, heap[i].currentPointerIndex, heap[i].lastPointerIndex,
4797 heap[i].distance);
4798 }
4799#endif
4800 }
4801
4802 uint32_t currentPointerIndex = heap[0].currentPointerIndex;
4803 if (matchedCurrentBits.hasBit(currentPointerIndex)) continue; // already matched
4804
4805 uint32_t lastPointerIndex = heap[0].lastPointerIndex;
4806 if (matchedLastBits.hasBit(lastPointerIndex)) continue; // already matched
4807
4808 matchedCurrentBits.markBit(currentPointerIndex);
4809 matchedLastBits.markBit(lastPointerIndex);
4810
4811 uint32_t id = mLastTouch.pointers[lastPointerIndex].id;
4812 mCurrentTouch.pointers[currentPointerIndex].id = id;
4813 mCurrentTouch.idToIndex[id] = currentPointerIndex;
4814 usedIdBits.markBit(id);
4815
4816#if DEBUG_POINTER_ASSIGNMENT
4817 LOGD("calculatePointerIds - matched: cur=%d, last=%d, id=%d, distance=%lld",
4818 lastPointerIndex, currentPointerIndex, id, heap[0].distance);
4819#endif
4820 break;
4821 }
4822 }
4823
4824 // Assign fresh ids to new pointers.
4825 if (currentPointerCount > lastPointerCount) {
4826 for (uint32_t i = currentPointerCount - lastPointerCount; ;) {
4827 uint32_t currentPointerIndex = matchedCurrentBits.firstUnmarkedBit();
4828 uint32_t id = usedIdBits.firstUnmarkedBit();
4829
4830 mCurrentTouch.pointers[currentPointerIndex].id = id;
4831 mCurrentTouch.idToIndex[id] = currentPointerIndex;
4832 usedIdBits.markBit(id);
4833
4834#if DEBUG_POINTER_ASSIGNMENT
4835 LOGD("calculatePointerIds - assigned: cur=%d, id=%d",
4836 currentPointerIndex, id);
4837#endif
4838
4839 if (--i == 0) break; // done
4840 matchedCurrentBits.markBit(currentPointerIndex);
4841 }
4842 }
4843
4844 // Fix id bits.
4845 mCurrentTouch.idBits = usedIdBits;
4846 }
4847}
4848
4849/* Special hack for devices that have bad screen data: if one of the
4850 * points has moved more than a screen height from the last position,
4851 * then drop it. */
4852bool TouchInputMapper::applyBadTouchFilter() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004853 uint32_t pointerCount = mCurrentTouch.pointerCount;
4854
4855 // Nothing to do if there are no points.
4856 if (pointerCount == 0) {
4857 return false;
4858 }
4859
4860 // Don't do anything if a finger is going down or up. We run
4861 // here before assigning pointer IDs, so there isn't a good
4862 // way to do per-finger matching.
4863 if (pointerCount != mLastTouch.pointerCount) {
4864 return false;
4865 }
4866
4867 // We consider a single movement across more than a 7/16 of
4868 // the long size of the screen to be bad. This was a magic value
4869 // determined by looking at the maximum distance it is feasible
4870 // to actually move in one sample.
Jeff Brown9626b142011-03-03 02:09:54 -08004871 int32_t maxDeltaY = (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1) * 7 / 16;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004872
4873 // XXX The original code in InputDevice.java included commented out
4874 // code for testing the X axis. Note that when we drop a point
4875 // we don't actually restore the old X either. Strange.
4876 // The old code also tries to track when bad points were previously
4877 // detected but it turns out that due to the placement of a "break"
4878 // at the end of the loop, we never set mDroppedBadPoint to true
4879 // so it is effectively dead code.
4880 // Need to figure out if the old code is busted or just overcomplicated
4881 // but working as intended.
4882
4883 // Look through all new points and see if any are farther than
4884 // acceptable from all previous points.
4885 for (uint32_t i = pointerCount; i-- > 0; ) {
4886 int32_t y = mCurrentTouch.pointers[i].y;
4887 int32_t closestY = INT_MAX;
4888 int32_t closestDeltaY = 0;
4889
4890#if DEBUG_HACKS
4891 LOGD("BadTouchFilter: Looking at next point #%d: y=%d", i, y);
4892#endif
4893
4894 for (uint32_t j = pointerCount; j-- > 0; ) {
4895 int32_t lastY = mLastTouch.pointers[j].y;
4896 int32_t deltaY = abs(y - lastY);
4897
4898#if DEBUG_HACKS
4899 LOGD("BadTouchFilter: Comparing with last point #%d: y=%d deltaY=%d",
4900 j, lastY, deltaY);
4901#endif
4902
4903 if (deltaY < maxDeltaY) {
4904 goto SkipSufficientlyClosePoint;
4905 }
4906 if (deltaY < closestDeltaY) {
4907 closestDeltaY = deltaY;
4908 closestY = lastY;
4909 }
4910 }
4911
4912 // Must not have found a close enough match.
4913#if DEBUG_HACKS
4914 LOGD("BadTouchFilter: Dropping bad point #%d: newY=%d oldY=%d deltaY=%d maxDeltaY=%d",
4915 i, y, closestY, closestDeltaY, maxDeltaY);
4916#endif
4917
4918 mCurrentTouch.pointers[i].y = closestY;
4919 return true; // XXX original code only corrects one point
4920
4921 SkipSufficientlyClosePoint: ;
4922 }
4923
4924 // No change.
4925 return false;
4926}
4927
4928/* Special hack for devices that have bad screen data: drop points where
4929 * the coordinate value for one axis has jumped to the other pointer's location.
4930 */
4931bool TouchInputMapper::applyJumpyTouchFilter() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07004932 uint32_t pointerCount = mCurrentTouch.pointerCount;
4933 if (mLastTouch.pointerCount != pointerCount) {
4934#if DEBUG_HACKS
4935 LOGD("JumpyTouchFilter: Different pointer count %d -> %d",
4936 mLastTouch.pointerCount, pointerCount);
4937 for (uint32_t i = 0; i < pointerCount; i++) {
4938 LOGD(" Pointer %d (%d, %d)", i,
4939 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
4940 }
4941#endif
4942
4943 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_TRANSITION_DROPS) {
4944 if (mLastTouch.pointerCount == 1 && pointerCount == 2) {
4945 // Just drop the first few events going from 1 to 2 pointers.
4946 // They're bad often enough that they're not worth considering.
4947 mCurrentTouch.pointerCount = 1;
4948 mJumpyTouchFilter.jumpyPointsDropped += 1;
4949
4950#if DEBUG_HACKS
4951 LOGD("JumpyTouchFilter: Pointer 2 dropped");
4952#endif
4953 return true;
4954 } else if (mLastTouch.pointerCount == 2 && pointerCount == 1) {
4955 // The event when we go from 2 -> 1 tends to be messed up too
4956 mCurrentTouch.pointerCount = 2;
4957 mCurrentTouch.pointers[0] = mLastTouch.pointers[0];
4958 mCurrentTouch.pointers[1] = mLastTouch.pointers[1];
4959 mJumpyTouchFilter.jumpyPointsDropped += 1;
4960
4961#if DEBUG_HACKS
4962 for (int32_t i = 0; i < 2; i++) {
4963 LOGD("JumpyTouchFilter: Pointer %d replaced (%d, %d)", i,
4964 mCurrentTouch.pointers[i].x, mCurrentTouch.pointers[i].y);
4965 }
4966#endif
4967 return true;
4968 }
4969 }
4970 // Reset jumpy points dropped on other transitions or if limit exceeded.
4971 mJumpyTouchFilter.jumpyPointsDropped = 0;
4972
4973#if DEBUG_HACKS
4974 LOGD("JumpyTouchFilter: Transition - drop limit reset");
4975#endif
4976 return false;
4977 }
4978
4979 // We have the same number of pointers as last time.
4980 // A 'jumpy' point is one where the coordinate value for one axis
4981 // has jumped to the other pointer's location. No need to do anything
4982 // else if we only have one pointer.
4983 if (pointerCount < 2) {
4984 return false;
4985 }
4986
4987 if (mJumpyTouchFilter.jumpyPointsDropped < JUMPY_DROP_LIMIT) {
Jeff Brown9626b142011-03-03 02:09:54 -08004988 int jumpyEpsilon = (mRawAxes.y.maxValue - mRawAxes.y.minValue + 1) / JUMPY_EPSILON_DIVISOR;
Jeff Brown6d0fec22010-07-23 21:28:06 -07004989
4990 // We only replace the single worst jumpy point as characterized by pointer distance
4991 // in a single axis.
4992 int32_t badPointerIndex = -1;
4993 int32_t badPointerReplacementIndex = -1;
4994 int32_t badPointerDistance = INT_MIN; // distance to be corrected
4995
4996 for (uint32_t i = pointerCount; i-- > 0; ) {
4997 int32_t x = mCurrentTouch.pointers[i].x;
4998 int32_t y = mCurrentTouch.pointers[i].y;
4999
5000#if DEBUG_HACKS
5001 LOGD("JumpyTouchFilter: Point %d (%d, %d)", i, x, y);
5002#endif
5003
5004 // Check if a touch point is too close to another's coordinates
5005 bool dropX = false, dropY = false;
5006 for (uint32_t j = 0; j < pointerCount; j++) {
5007 if (i == j) {
5008 continue;
5009 }
5010
5011 if (abs(x - mCurrentTouch.pointers[j].x) <= jumpyEpsilon) {
5012 dropX = true;
5013 break;
5014 }
5015
5016 if (abs(y - mCurrentTouch.pointers[j].y) <= jumpyEpsilon) {
5017 dropY = true;
5018 break;
5019 }
5020 }
5021 if (! dropX && ! dropY) {
5022 continue; // not jumpy
5023 }
5024
5025 // Find a replacement candidate by comparing with older points on the
5026 // complementary (non-jumpy) axis.
5027 int32_t distance = INT_MIN; // distance to be corrected
5028 int32_t replacementIndex = -1;
5029
5030 if (dropX) {
5031 // X looks too close. Find an older replacement point with a close Y.
5032 int32_t smallestDeltaY = INT_MAX;
5033 for (uint32_t j = 0; j < pointerCount; j++) {
5034 int32_t deltaY = abs(y - mLastTouch.pointers[j].y);
5035 if (deltaY < smallestDeltaY) {
5036 smallestDeltaY = deltaY;
5037 replacementIndex = j;
5038 }
5039 }
5040 distance = abs(x - mLastTouch.pointers[replacementIndex].x);
5041 } else {
5042 // Y looks too close. Find an older replacement point with a close X.
5043 int32_t smallestDeltaX = INT_MAX;
5044 for (uint32_t j = 0; j < pointerCount; j++) {
5045 int32_t deltaX = abs(x - mLastTouch.pointers[j].x);
5046 if (deltaX < smallestDeltaX) {
5047 smallestDeltaX = deltaX;
5048 replacementIndex = j;
5049 }
5050 }
5051 distance = abs(y - mLastTouch.pointers[replacementIndex].y);
5052 }
5053
5054 // If replacing this pointer would correct a worse error than the previous ones
5055 // considered, then use this replacement instead.
5056 if (distance > badPointerDistance) {
5057 badPointerIndex = i;
5058 badPointerReplacementIndex = replacementIndex;
5059 badPointerDistance = distance;
5060 }
5061 }
5062
5063 // Correct the jumpy pointer if one was found.
5064 if (badPointerIndex >= 0) {
5065#if DEBUG_HACKS
5066 LOGD("JumpyTouchFilter: Replacing bad pointer %d with (%d, %d)",
5067 badPointerIndex,
5068 mLastTouch.pointers[badPointerReplacementIndex].x,
5069 mLastTouch.pointers[badPointerReplacementIndex].y);
5070#endif
5071
5072 mCurrentTouch.pointers[badPointerIndex].x =
5073 mLastTouch.pointers[badPointerReplacementIndex].x;
5074 mCurrentTouch.pointers[badPointerIndex].y =
5075 mLastTouch.pointers[badPointerReplacementIndex].y;
5076 mJumpyTouchFilter.jumpyPointsDropped += 1;
5077 return true;
5078 }
5079 }
5080
5081 mJumpyTouchFilter.jumpyPointsDropped = 0;
5082 return false;
5083}
5084
5085/* Special hack for devices that have bad screen data: aggregate and
5086 * compute averages of the coordinate data, to reduce the amount of
5087 * jitter seen by applications. */
5088void TouchInputMapper::applyAveragingTouchFilter() {
5089 for (uint32_t currentIndex = 0; currentIndex < mCurrentTouch.pointerCount; currentIndex++) {
5090 uint32_t id = mCurrentTouch.pointers[currentIndex].id;
5091 int32_t x = mCurrentTouch.pointers[currentIndex].x;
5092 int32_t y = mCurrentTouch.pointers[currentIndex].y;
Jeff Brown8d608662010-08-30 03:02:23 -07005093 int32_t pressure;
5094 switch (mCalibration.pressureSource) {
5095 case Calibration::PRESSURE_SOURCE_PRESSURE:
5096 pressure = mCurrentTouch.pointers[currentIndex].pressure;
5097 break;
5098 case Calibration::PRESSURE_SOURCE_TOUCH:
5099 pressure = mCurrentTouch.pointers[currentIndex].touchMajor;
5100 break;
5101 default:
5102 pressure = 1;
5103 break;
5104 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005105
5106 if (mLastTouch.idBits.hasBit(id)) {
5107 // Pointer was down before and is still down now.
5108 // Compute average over history trace.
5109 uint32_t start = mAveragingTouchFilter.historyStart[id];
5110 uint32_t end = mAveragingTouchFilter.historyEnd[id];
5111
5112 int64_t deltaX = x - mAveragingTouchFilter.historyData[end].pointers[id].x;
5113 int64_t deltaY = y - mAveragingTouchFilter.historyData[end].pointers[id].y;
5114 uint64_t distance = uint64_t(deltaX * deltaX + deltaY * deltaY);
5115
5116#if DEBUG_HACKS
5117 LOGD("AveragingTouchFilter: Pointer id %d - Distance from last sample: %lld",
5118 id, distance);
5119#endif
5120
5121 if (distance < AVERAGING_DISTANCE_LIMIT) {
5122 // Increment end index in preparation for recording new historical data.
5123 end += 1;
5124 if (end > AVERAGING_HISTORY_SIZE) {
5125 end = 0;
5126 }
5127
5128 // If the end index has looped back to the start index then we have filled
5129 // the historical trace up to the desired size so we drop the historical
5130 // data at the start of the trace.
5131 if (end == start) {
5132 start += 1;
5133 if (start > AVERAGING_HISTORY_SIZE) {
5134 start = 0;
5135 }
5136 }
5137
5138 // Add the raw data to the historical trace.
5139 mAveragingTouchFilter.historyStart[id] = start;
5140 mAveragingTouchFilter.historyEnd[id] = end;
5141 mAveragingTouchFilter.historyData[end].pointers[id].x = x;
5142 mAveragingTouchFilter.historyData[end].pointers[id].y = y;
5143 mAveragingTouchFilter.historyData[end].pointers[id].pressure = pressure;
5144
5145 // Average over all historical positions in the trace by total pressure.
5146 int32_t averagedX = 0;
5147 int32_t averagedY = 0;
5148 int32_t totalPressure = 0;
5149 for (;;) {
5150 int32_t historicalX = mAveragingTouchFilter.historyData[start].pointers[id].x;
5151 int32_t historicalY = mAveragingTouchFilter.historyData[start].pointers[id].y;
5152 int32_t historicalPressure = mAveragingTouchFilter.historyData[start]
5153 .pointers[id].pressure;
5154
5155 averagedX += historicalX * historicalPressure;
5156 averagedY += historicalY * historicalPressure;
5157 totalPressure += historicalPressure;
5158
5159 if (start == end) {
5160 break;
5161 }
5162
5163 start += 1;
5164 if (start > AVERAGING_HISTORY_SIZE) {
5165 start = 0;
5166 }
5167 }
5168
Jeff Brown8d608662010-08-30 03:02:23 -07005169 if (totalPressure != 0) {
5170 averagedX /= totalPressure;
5171 averagedY /= totalPressure;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005172
5173#if DEBUG_HACKS
Jeff Brown8d608662010-08-30 03:02:23 -07005174 LOGD("AveragingTouchFilter: Pointer id %d - "
5175 "totalPressure=%d, averagedX=%d, averagedY=%d", id, totalPressure,
5176 averagedX, averagedY);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005177#endif
5178
Jeff Brown8d608662010-08-30 03:02:23 -07005179 mCurrentTouch.pointers[currentIndex].x = averagedX;
5180 mCurrentTouch.pointers[currentIndex].y = averagedY;
5181 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005182 } else {
5183#if DEBUG_HACKS
5184 LOGD("AveragingTouchFilter: Pointer id %d - Exceeded max distance", id);
5185#endif
5186 }
5187 } else {
5188#if DEBUG_HACKS
5189 LOGD("AveragingTouchFilter: Pointer id %d - Pointer went up", id);
5190#endif
5191 }
5192
5193 // Reset pointer history.
5194 mAveragingTouchFilter.historyStart[id] = 0;
5195 mAveragingTouchFilter.historyEnd[id] = 0;
5196 mAveragingTouchFilter.historyData[0].pointers[id].x = x;
5197 mAveragingTouchFilter.historyData[0].pointers[id].y = y;
5198 mAveragingTouchFilter.historyData[0].pointers[id].pressure = pressure;
5199 }
5200}
5201
5202int32_t TouchInputMapper::getKeyCodeState(uint32_t sourceMask, int32_t keyCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07005203 { // acquire lock
5204 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005205
Jeff Brown6328cdc2010-07-29 18:18:33 -07005206 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.keyCode == keyCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005207 return AKEY_STATE_VIRTUAL;
5208 }
5209
Jeff Brown6328cdc2010-07-29 18:18:33 -07005210 size_t numVirtualKeys = mLocked.virtualKeys.size();
5211 for (size_t i = 0; i < numVirtualKeys; i++) {
5212 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005213 if (virtualKey.keyCode == keyCode) {
5214 return AKEY_STATE_UP;
5215 }
5216 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07005217 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07005218
5219 return AKEY_STATE_UNKNOWN;
5220}
5221
5222int32_t TouchInputMapper::getScanCodeState(uint32_t sourceMask, int32_t scanCode) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07005223 { // acquire lock
5224 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005225
Jeff Brown6328cdc2010-07-29 18:18:33 -07005226 if (mLocked.currentVirtualKey.down && mLocked.currentVirtualKey.scanCode == scanCode) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005227 return AKEY_STATE_VIRTUAL;
5228 }
5229
Jeff Brown6328cdc2010-07-29 18:18:33 -07005230 size_t numVirtualKeys = mLocked.virtualKeys.size();
5231 for (size_t i = 0; i < numVirtualKeys; i++) {
5232 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005233 if (virtualKey.scanCode == scanCode) {
5234 return AKEY_STATE_UP;
5235 }
5236 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07005237 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07005238
5239 return AKEY_STATE_UNKNOWN;
5240}
5241
5242bool TouchInputMapper::markSupportedKeyCodes(uint32_t sourceMask, size_t numCodes,
5243 const int32_t* keyCodes, uint8_t* outFlags) {
Jeff Brown6328cdc2010-07-29 18:18:33 -07005244 { // acquire lock
5245 AutoMutex _l(mLock);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005246
Jeff Brown6328cdc2010-07-29 18:18:33 -07005247 size_t numVirtualKeys = mLocked.virtualKeys.size();
5248 for (size_t i = 0; i < numVirtualKeys; i++) {
5249 const VirtualKey& virtualKey = mLocked.virtualKeys[i];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005250
5251 for (size_t i = 0; i < numCodes; i++) {
5252 if (virtualKey.keyCode == keyCodes[i]) {
5253 outFlags[i] = 1;
5254 }
5255 }
5256 }
Jeff Brown6328cdc2010-07-29 18:18:33 -07005257 } // release lock
Jeff Brown6d0fec22010-07-23 21:28:06 -07005258
5259 return true;
5260}
5261
5262
5263// --- SingleTouchInputMapper ---
5264
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005265SingleTouchInputMapper::SingleTouchInputMapper(InputDevice* device) :
5266 TouchInputMapper(device) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005267 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005268}
5269
5270SingleTouchInputMapper::~SingleTouchInputMapper() {
5271}
5272
Jeff Brown80fd47c2011-05-24 01:07:44 -07005273void SingleTouchInputMapper::clearState() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005274 mAccumulator.clear();
5275
5276 mDown = false;
5277 mX = 0;
5278 mY = 0;
Jeff Brown8d608662010-08-30 03:02:23 -07005279 mPressure = 0; // default to 0 for devices that don't report pressure
5280 mToolWidth = 0; // default to 0 for devices that don't report tool width
Jeff Brownace13b12011-03-09 17:39:48 -08005281 mButtonState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005282}
5283
5284void SingleTouchInputMapper::reset() {
5285 TouchInputMapper::reset();
5286
Jeff Brown80fd47c2011-05-24 01:07:44 -07005287 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005288 }
5289
5290void SingleTouchInputMapper::process(const RawEvent* rawEvent) {
5291 switch (rawEvent->type) {
5292 case EV_KEY:
5293 switch (rawEvent->scanCode) {
5294 case BTN_TOUCH:
5295 mAccumulator.fields |= Accumulator::FIELD_BTN_TOUCH;
5296 mAccumulator.btnTouch = rawEvent->value != 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005297 // Don't sync immediately. Wait until the next SYN_REPORT since we might
5298 // not have received valid position information yet. This logic assumes that
5299 // BTN_TOUCH is always followed by SYN_REPORT as part of a complete packet.
Jeff Brown6d0fec22010-07-23 21:28:06 -07005300 break;
Jeff Brownace13b12011-03-09 17:39:48 -08005301 default:
5302 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005303 int32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode);
Jeff Brownace13b12011-03-09 17:39:48 -08005304 if (buttonState) {
5305 if (rawEvent->value) {
5306 mAccumulator.buttonDown |= buttonState;
5307 } else {
5308 mAccumulator.buttonUp |= buttonState;
5309 }
5310 mAccumulator.fields |= Accumulator::FIELD_BUTTONS;
5311 }
5312 }
5313 break;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005314 }
5315 break;
5316
5317 case EV_ABS:
5318 switch (rawEvent->scanCode) {
5319 case ABS_X:
5320 mAccumulator.fields |= Accumulator::FIELD_ABS_X;
5321 mAccumulator.absX = rawEvent->value;
5322 break;
5323 case ABS_Y:
5324 mAccumulator.fields |= Accumulator::FIELD_ABS_Y;
5325 mAccumulator.absY = rawEvent->value;
5326 break;
5327 case ABS_PRESSURE:
5328 mAccumulator.fields |= Accumulator::FIELD_ABS_PRESSURE;
5329 mAccumulator.absPressure = rawEvent->value;
5330 break;
5331 case ABS_TOOL_WIDTH:
5332 mAccumulator.fields |= Accumulator::FIELD_ABS_TOOL_WIDTH;
5333 mAccumulator.absToolWidth = rawEvent->value;
5334 break;
5335 }
5336 break;
5337
5338 case EV_SYN:
5339 switch (rawEvent->scanCode) {
5340 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005341 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005342 break;
5343 }
5344 break;
5345 }
5346}
5347
5348void SingleTouchInputMapper::sync(nsecs_t when) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005349 uint32_t fields = mAccumulator.fields;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005350 if (fields == 0) {
5351 return; // no new state changes, so nothing to do
5352 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005353
5354 if (fields & Accumulator::FIELD_BTN_TOUCH) {
5355 mDown = mAccumulator.btnTouch;
5356 }
5357
5358 if (fields & Accumulator::FIELD_ABS_X) {
5359 mX = mAccumulator.absX;
5360 }
5361
5362 if (fields & Accumulator::FIELD_ABS_Y) {
5363 mY = mAccumulator.absY;
5364 }
5365
5366 if (fields & Accumulator::FIELD_ABS_PRESSURE) {
5367 mPressure = mAccumulator.absPressure;
5368 }
5369
5370 if (fields & Accumulator::FIELD_ABS_TOOL_WIDTH) {
Jeff Brown8d608662010-08-30 03:02:23 -07005371 mToolWidth = mAccumulator.absToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005372 }
5373
Jeff Brownace13b12011-03-09 17:39:48 -08005374 if (fields & Accumulator::FIELD_BUTTONS) {
5375 mButtonState = (mButtonState | mAccumulator.buttonDown) & ~mAccumulator.buttonUp;
5376 }
5377
Jeff Brown6d0fec22010-07-23 21:28:06 -07005378 mCurrentTouch.clear();
5379
5380 if (mDown) {
5381 mCurrentTouch.pointerCount = 1;
5382 mCurrentTouch.pointers[0].id = 0;
5383 mCurrentTouch.pointers[0].x = mX;
5384 mCurrentTouch.pointers[0].y = mY;
5385 mCurrentTouch.pointers[0].pressure = mPressure;
Jeff Brown8d608662010-08-30 03:02:23 -07005386 mCurrentTouch.pointers[0].touchMajor = 0;
5387 mCurrentTouch.pointers[0].touchMinor = 0;
5388 mCurrentTouch.pointers[0].toolMajor = mToolWidth;
5389 mCurrentTouch.pointers[0].toolMinor = mToolWidth;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005390 mCurrentTouch.pointers[0].orientation = 0;
Jeff Brown80fd47c2011-05-24 01:07:44 -07005391 mCurrentTouch.pointers[0].distance = 0;
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005392 mCurrentTouch.pointers[0].isStylus = false; // TODO: Set stylus
Jeff Brown6d0fec22010-07-23 21:28:06 -07005393 mCurrentTouch.idToIndex[0] = 0;
5394 mCurrentTouch.idBits.markBit(0);
Jeff Brownace13b12011-03-09 17:39:48 -08005395 mCurrentTouch.buttonState = mButtonState;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005396 }
5397
5398 syncTouch(when, true);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005399
5400 mAccumulator.clear();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005401}
5402
Jeff Brown8d608662010-08-30 03:02:23 -07005403void SingleTouchInputMapper::configureRawAxes() {
5404 TouchInputMapper::configureRawAxes();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005405
Jeff Brown8d608662010-08-30 03:02:23 -07005406 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_X, & mRawAxes.x);
5407 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_Y, & mRawAxes.y);
5408 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_PRESSURE, & mRawAxes.pressure);
5409 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_TOOL_WIDTH, & mRawAxes.toolMajor);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005410}
5411
5412
5413// --- MultiTouchInputMapper ---
5414
Jeff Brown47e6b1b2010-11-29 17:37:49 -08005415MultiTouchInputMapper::MultiTouchInputMapper(InputDevice* device) :
Jeff Brown80fd47c2011-05-24 01:07:44 -07005416 TouchInputMapper(device), mSlotCount(0), mUsingSlotsProtocol(false) {
5417 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005418}
5419
5420MultiTouchInputMapper::~MultiTouchInputMapper() {
5421}
5422
Jeff Brown80fd47c2011-05-24 01:07:44 -07005423void MultiTouchInputMapper::clearState() {
Jeff Brown441a9c22011-06-02 18:22:25 -07005424 mAccumulator.clearSlots(mSlotCount);
5425 mAccumulator.clearButtons();
Jeff Brownace13b12011-03-09 17:39:48 -08005426 mButtonState = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005427}
5428
5429void MultiTouchInputMapper::reset() {
5430 TouchInputMapper::reset();
5431
Jeff Brown80fd47c2011-05-24 01:07:44 -07005432 clearState();
Jeff Brown6d0fec22010-07-23 21:28:06 -07005433}
5434
5435void MultiTouchInputMapper::process(const RawEvent* rawEvent) {
5436 switch (rawEvent->type) {
Jeff Brownace13b12011-03-09 17:39:48 -08005437 case EV_KEY: {
5438 if (mParameters.deviceType == Parameters::DEVICE_TYPE_POINTER) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005439 int32_t buttonState = getButtonStateForScanCode(rawEvent->scanCode);
Jeff Brownace13b12011-03-09 17:39:48 -08005440 if (buttonState) {
5441 if (rawEvent->value) {
5442 mAccumulator.buttonDown |= buttonState;
5443 } else {
5444 mAccumulator.buttonUp |= buttonState;
5445 }
5446 }
5447 }
5448 break;
5449 }
5450
Jeff Brown6d0fec22010-07-23 21:28:06 -07005451 case EV_ABS: {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005452 bool newSlot = false;
5453 if (mUsingSlotsProtocol && rawEvent->scanCode == ABS_MT_SLOT) {
5454 mAccumulator.currentSlot = rawEvent->value;
5455 newSlot = true;
5456 }
5457
5458 if (mAccumulator.currentSlot < 0 || size_t(mAccumulator.currentSlot) >= mSlotCount) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005459#if DEBUG_POINTERS
Jeff Brown441a9c22011-06-02 18:22:25 -07005460 if (newSlot) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005461 LOGW("MultiTouch device %s emitted invalid slot index %d but it "
5462 "should be between 0 and %d; ignoring this slot.",
5463 getDeviceName().string(), mAccumulator.currentSlot, mSlotCount);
Jeff Brown80fd47c2011-05-24 01:07:44 -07005464 }
Jeff Brown441a9c22011-06-02 18:22:25 -07005465#endif
Jeff Brown80fd47c2011-05-24 01:07:44 -07005466 break;
5467 }
5468
5469 Accumulator::Slot* slot = &mAccumulator.slots[mAccumulator.currentSlot];
Jeff Brown6d0fec22010-07-23 21:28:06 -07005470
5471 switch (rawEvent->scanCode) {
5472 case ABS_MT_POSITION_X:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005473 slot->fields |= Accumulator::FIELD_ABS_MT_POSITION_X;
5474 slot->absMTPositionX = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005475 break;
5476 case ABS_MT_POSITION_Y:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005477 slot->fields |= Accumulator::FIELD_ABS_MT_POSITION_Y;
5478 slot->absMTPositionY = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005479 break;
5480 case ABS_MT_TOUCH_MAJOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005481 slot->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MAJOR;
5482 slot->absMTTouchMajor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005483 break;
5484 case ABS_MT_TOUCH_MINOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005485 slot->fields |= Accumulator::FIELD_ABS_MT_TOUCH_MINOR;
5486 slot->absMTTouchMinor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005487 break;
5488 case ABS_MT_WIDTH_MAJOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005489 slot->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MAJOR;
5490 slot->absMTWidthMajor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005491 break;
5492 case ABS_MT_WIDTH_MINOR:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005493 slot->fields |= Accumulator::FIELD_ABS_MT_WIDTH_MINOR;
5494 slot->absMTWidthMinor = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005495 break;
5496 case ABS_MT_ORIENTATION:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005497 slot->fields |= Accumulator::FIELD_ABS_MT_ORIENTATION;
5498 slot->absMTOrientation = rawEvent->value;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005499 break;
5500 case ABS_MT_TRACKING_ID:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005501 if (mUsingSlotsProtocol && rawEvent->value < 0) {
5502 slot->clear();
5503 } else {
5504 slot->fields |= Accumulator::FIELD_ABS_MT_TRACKING_ID;
5505 slot->absMTTrackingId = rawEvent->value;
5506 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005507 break;
Jeff Brown8d608662010-08-30 03:02:23 -07005508 case ABS_MT_PRESSURE:
Jeff Brown80fd47c2011-05-24 01:07:44 -07005509 slot->fields |= Accumulator::FIELD_ABS_MT_PRESSURE;
5510 slot->absMTPressure = rawEvent->value;
5511 break;
5512 case ABS_MT_TOOL_TYPE:
5513 slot->fields |= Accumulator::FIELD_ABS_MT_TOOL_TYPE;
5514 slot->absMTToolType = rawEvent->value;
Jeff Brown8d608662010-08-30 03:02:23 -07005515 break;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005516 }
5517 break;
5518 }
5519
5520 case EV_SYN:
5521 switch (rawEvent->scanCode) {
5522 case SYN_MT_REPORT: {
5523 // MultiTouch Sync: The driver has returned all data for *one* of the pointers.
Jeff Brown80fd47c2011-05-24 01:07:44 -07005524 mAccumulator.currentSlot += 1;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005525 break;
5526 }
5527
5528 case SYN_REPORT:
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005529 sync(rawEvent->when);
Jeff Brown6d0fec22010-07-23 21:28:06 -07005530 break;
5531 }
5532 break;
5533 }
5534}
5535
5536void MultiTouchInputMapper::sync(nsecs_t when) {
5537 static const uint32_t REQUIRED_FIELDS =
Jeff Brown8d608662010-08-30 03:02:23 -07005538 Accumulator::FIELD_ABS_MT_POSITION_X | Accumulator::FIELD_ABS_MT_POSITION_Y;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005539
Jeff Brown80fd47c2011-05-24 01:07:44 -07005540 size_t inCount = mSlotCount;
5541 size_t outCount = 0;
Jeff Brown6d0fec22010-07-23 21:28:06 -07005542 bool havePointerIds = true;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005543
Jeff Brown6d0fec22010-07-23 21:28:06 -07005544 mCurrentTouch.clear();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005545
Jeff Brown80fd47c2011-05-24 01:07:44 -07005546 for (size_t inIndex = 0; inIndex < inCount; inIndex++) {
5547 const Accumulator::Slot& inSlot = mAccumulator.slots[inIndex];
5548 uint32_t fields = inSlot.fields;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005549
Jeff Brown6d0fec22010-07-23 21:28:06 -07005550 if ((fields & REQUIRED_FIELDS) != REQUIRED_FIELDS) {
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005551 // Some drivers send empty MT sync packets without X / Y to indicate a pointer up.
Jeff Brown80fd47c2011-05-24 01:07:44 -07005552 // This may also indicate an unused slot.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005553 // Drop this finger.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005554 continue;
5555 }
5556
Jeff Brown80fd47c2011-05-24 01:07:44 -07005557 if (outCount >= MAX_POINTERS) {
5558#if DEBUG_POINTERS
5559 LOGD("MultiTouch device %s emitted more than maximum of %d pointers; "
5560 "ignoring the rest.",
5561 getDeviceName().string(), MAX_POINTERS);
5562#endif
5563 break; // too many fingers!
5564 }
5565
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005566 PointerData& outPointer = mCurrentTouch.pointers[outCount];
Jeff Brown80fd47c2011-05-24 01:07:44 -07005567 outPointer.x = inSlot.absMTPositionX;
5568 outPointer.y = inSlot.absMTPositionY;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005569
Jeff Brown8d608662010-08-30 03:02:23 -07005570 if (fields & Accumulator::FIELD_ABS_MT_PRESSURE) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005571 outPointer.pressure = inSlot.absMTPressure;
Jeff Brown8d608662010-08-30 03:02:23 -07005572 } else {
5573 // Default pressure to 0 if absent.
5574 outPointer.pressure = 0;
5575 }
5576
5577 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MAJOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005578 if (inSlot.absMTTouchMajor <= 0) {
Jeff Brown8d608662010-08-30 03:02:23 -07005579 // Some devices send sync packets with X / Y but with a 0 touch major to indicate
5580 // a pointer going up. Drop this finger.
5581 continue;
5582 }
Jeff Brown80fd47c2011-05-24 01:07:44 -07005583 outPointer.touchMajor = inSlot.absMTTouchMajor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005584 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005585 // Default touch area to 0 if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005586 outPointer.touchMajor = 0;
5587 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005588
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005589 if (fields & Accumulator::FIELD_ABS_MT_TOUCH_MINOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005590 outPointer.touchMinor = inSlot.absMTTouchMinor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005591 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005592 // Assume touch area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005593 outPointer.touchMinor = outPointer.touchMajor;
5594 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005595
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005596 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MAJOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005597 outPointer.toolMajor = inSlot.absMTWidthMajor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005598 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005599 // Default tool area to 0 if absent.
5600 outPointer.toolMajor = 0;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005601 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005602
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005603 if (fields & Accumulator::FIELD_ABS_MT_WIDTH_MINOR) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005604 outPointer.toolMinor = inSlot.absMTWidthMinor;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005605 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005606 // Assume tool area is circular.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005607 outPointer.toolMinor = outPointer.toolMajor;
5608 }
5609
5610 if (fields & Accumulator::FIELD_ABS_MT_ORIENTATION) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005611 outPointer.orientation = inSlot.absMTOrientation;
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005612 } else {
Jeff Brown8d608662010-08-30 03:02:23 -07005613 // Default orientation to vertical if absent.
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005614 outPointer.orientation = 0;
5615 }
5616
Jeff Brown80fd47c2011-05-24 01:07:44 -07005617 if (fields & Accumulator::FIELD_ABS_MT_DISTANCE) {
5618 outPointer.distance = inSlot.absMTDistance;
5619 } else {
5620 // Default distance is 0 (direct contact).
5621 outPointer.distance = 0;
5622 }
5623
5624 if (fields & Accumulator::FIELD_ABS_MT_TOOL_TYPE) {
5625 outPointer.isStylus = (inSlot.absMTToolType == MT_TOOL_PEN);
5626 } else {
5627 // Assume this is not a stylus.
5628 outPointer.isStylus = false;
5629 }
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005630
Jeff Brown8d608662010-08-30 03:02:23 -07005631 // Assign pointer id using tracking id if available.
Jeff Brown6d0fec22010-07-23 21:28:06 -07005632 if (havePointerIds) {
Jeff Brown80fd47c2011-05-24 01:07:44 -07005633 int32_t id;
5634 if (mUsingSlotsProtocol) {
5635 id = inIndex;
5636 } else if (fields & Accumulator::FIELD_ABS_MT_TRACKING_ID) {
5637 id = inSlot.absMTTrackingId;
5638 } else {
5639 id = -1;
5640 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005641
Jeff Brown80fd47c2011-05-24 01:07:44 -07005642 if (id >= 0 && id <= MAX_POINTER_ID) {
5643 outPointer.id = id;
5644 mCurrentTouch.idToIndex[id] = outCount;
5645 mCurrentTouch.idBits.markBit(id);
5646 } else {
5647 if (id >= 0) {
Jeff Brown6d0fec22010-07-23 21:28:06 -07005648#if DEBUG_POINTERS
Jeff Brown80fd47c2011-05-24 01:07:44 -07005649 LOGD("Pointers: Ignoring driver provided slot index or tracking id %d because "
5650 "it is larger than the maximum supported pointer id %d",
Jeff Brown6d0fec22010-07-23 21:28:06 -07005651 id, MAX_POINTER_ID);
5652#endif
Jeff Brown6d0fec22010-07-23 21:28:06 -07005653 }
Jeff Brown6d0fec22010-07-23 21:28:06 -07005654 havePointerIds = false;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005655 }
5656 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005657
Jeff Brown6d0fec22010-07-23 21:28:06 -07005658 outCount += 1;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005659 }
5660
Jeff Brown6d0fec22010-07-23 21:28:06 -07005661 mCurrentTouch.pointerCount = outCount;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005662
Jeff Brownace13b12011-03-09 17:39:48 -08005663 mButtonState = (mButtonState | mAccumulator.buttonDown) & ~mAccumulator.buttonUp;
5664 mCurrentTouch.buttonState = mButtonState;
5665
Jeff Brown6d0fec22010-07-23 21:28:06 -07005666 syncTouch(when, havePointerIds);
Jeff Brown2dfd7a72010-08-17 20:38:35 -07005667
Jeff Brown441a9c22011-06-02 18:22:25 -07005668 if (!mUsingSlotsProtocol) {
5669 mAccumulator.clearSlots(mSlotCount);
5670 }
5671 mAccumulator.clearButtons();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005672}
5673
Jeff Brown8d608662010-08-30 03:02:23 -07005674void MultiTouchInputMapper::configureRawAxes() {
5675 TouchInputMapper::configureRawAxes();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005676
Jeff Brown80fd47c2011-05-24 01:07:44 -07005677 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_X, &mRawAxes.x);
5678 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_POSITION_Y, &mRawAxes.y);
5679 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MAJOR, &mRawAxes.touchMajor);
5680 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TOUCH_MINOR, &mRawAxes.touchMinor);
5681 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MAJOR, &mRawAxes.toolMajor);
5682 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_WIDTH_MINOR, &mRawAxes.toolMinor);
5683 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_ORIENTATION, &mRawAxes.orientation);
5684 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_PRESSURE, &mRawAxes.pressure);
5685 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_DISTANCE, &mRawAxes.distance);
5686 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_TRACKING_ID, &mRawAxes.trackingId);
5687 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), ABS_MT_SLOT, &mRawAxes.slot);
5688
5689 if (mRawAxes.trackingId.valid
5690 && mRawAxes.slot.valid && mRawAxes.slot.minValue == 0 && mRawAxes.slot.maxValue > 0) {
5691 mSlotCount = mRawAxes.slot.maxValue + 1;
5692 if (mSlotCount > MAX_SLOTS) {
5693 LOGW("MultiTouch Device %s reported %d slots but the framework "
5694 "only supports a maximum of %d slots at this time.",
5695 getDeviceName().string(), mSlotCount, MAX_SLOTS);
5696 mSlotCount = MAX_SLOTS;
5697 }
5698 mUsingSlotsProtocol = true;
5699 } else {
5700 mSlotCount = MAX_POINTERS;
5701 mUsingSlotsProtocol = false;
5702 }
5703
5704 mAccumulator.allocateSlots(mSlotCount);
Jeff Brown9c3cda02010-06-15 01:31:58 -07005705}
5706
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07005707
Jeff Browncb1404e2011-01-15 18:14:15 -08005708// --- JoystickInputMapper ---
5709
5710JoystickInputMapper::JoystickInputMapper(InputDevice* device) :
5711 InputMapper(device) {
Jeff Browncb1404e2011-01-15 18:14:15 -08005712}
5713
5714JoystickInputMapper::~JoystickInputMapper() {
5715}
5716
5717uint32_t JoystickInputMapper::getSources() {
5718 return AINPUT_SOURCE_JOYSTICK;
5719}
5720
5721void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
5722 InputMapper::populateDeviceInfo(info);
5723
Jeff Brown6f2fba42011-02-19 01:08:02 -08005724 for (size_t i = 0; i < mAxes.size(); i++) {
5725 const Axis& axis = mAxes.valueAt(i);
Jeff Brownefd32662011-03-08 15:13:06 -08005726 info->addMotionRange(axis.axisInfo.axis, AINPUT_SOURCE_JOYSTICK,
5727 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005728 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Jeff Brownefd32662011-03-08 15:13:06 -08005729 info->addMotionRange(axis.axisInfo.highAxis, AINPUT_SOURCE_JOYSTICK,
5730 axis.min, axis.max, axis.flat, axis.fuzz);
Jeff Brown85297452011-03-04 13:07:49 -08005731 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005732 }
5733}
5734
5735void JoystickInputMapper::dump(String8& dump) {
5736 dump.append(INDENT2 "Joystick Input Mapper:\n");
5737
Jeff Brown6f2fba42011-02-19 01:08:02 -08005738 dump.append(INDENT3 "Axes:\n");
5739 size_t numAxes = mAxes.size();
5740 for (size_t i = 0; i < numAxes; i++) {
5741 const Axis& axis = mAxes.valueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005742 const char* label = getAxisLabel(axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005743 if (label) {
Jeff Brown85297452011-03-04 13:07:49 -08005744 dump.appendFormat(INDENT4 "%s", label);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005745 } else {
Jeff Brown85297452011-03-04 13:07:49 -08005746 dump.appendFormat(INDENT4 "%d", axis.axisInfo.axis);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005747 }
Jeff Brown85297452011-03-04 13:07:49 -08005748 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5749 label = getAxisLabel(axis.axisInfo.highAxis);
5750 if (label) {
5751 dump.appendFormat(" / %s (split at %d)", label, axis.axisInfo.splitValue);
5752 } else {
5753 dump.appendFormat(" / %d (split at %d)", axis.axisInfo.highAxis,
5754 axis.axisInfo.splitValue);
5755 }
5756 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
5757 dump.append(" (invert)");
5758 }
5759
5760 dump.appendFormat(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f\n",
5761 axis.min, axis.max, axis.flat, axis.fuzz);
5762 dump.appendFormat(INDENT4 " scale=%0.5f, offset=%0.5f, "
5763 "highScale=%0.5f, highOffset=%0.5f\n",
5764 axis.scale, axis.offset, axis.highScale, axis.highOffset);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005765 dump.appendFormat(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, rawFlat=%d, rawFuzz=%d\n",
5766 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
5767 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz);
Jeff Browncb1404e2011-01-15 18:14:15 -08005768 }
5769}
5770
5771void JoystickInputMapper::configure() {
5772 InputMapper::configure();
5773
Jeff Brown6f2fba42011-02-19 01:08:02 -08005774 // Collect all axes.
5775 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
5776 RawAbsoluteAxisInfo rawAxisInfo;
5777 getEventHub()->getAbsoluteAxisInfo(getDeviceId(), abs, &rawAxisInfo);
5778 if (rawAxisInfo.valid) {
Jeff Brown85297452011-03-04 13:07:49 -08005779 // Map axis.
5780 AxisInfo axisInfo;
5781 bool explicitlyMapped = !getEventHub()->mapAxis(getDeviceId(), abs, &axisInfo);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005782 if (!explicitlyMapped) {
5783 // Axis is not explicitly mapped, will choose a generic axis later.
Jeff Brown85297452011-03-04 13:07:49 -08005784 axisInfo.mode = AxisInfo::MODE_NORMAL;
5785 axisInfo.axis = -1;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005786 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005787
Jeff Brown85297452011-03-04 13:07:49 -08005788 // Apply flat override.
5789 int32_t rawFlat = axisInfo.flatOverride < 0
5790 ? rawAxisInfo.flat : axisInfo.flatOverride;
5791
5792 // Calculate scaling factors and limits.
Jeff Brown6f2fba42011-02-19 01:08:02 -08005793 Axis axis;
Jeff Brown85297452011-03-04 13:07:49 -08005794 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
5795 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
5796 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
5797 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5798 scale, 0.0f, highScale, 0.0f,
5799 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
5800 } else if (isCenteredAxis(axisInfo.axis)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005801 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
5802 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
Jeff Brown85297452011-03-04 13:07:49 -08005803 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5804 scale, offset, scale, offset,
5805 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005806 } else {
5807 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
Jeff Brown85297452011-03-04 13:07:49 -08005808 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped,
5809 scale, 0.0f, scale, 0.0f,
5810 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale);
Jeff Brown6f2fba42011-02-19 01:08:02 -08005811 }
5812
5813 // To eliminate noise while the joystick is at rest, filter out small variations
5814 // in axis values up front.
5815 axis.filter = axis.flat * 0.25f;
5816
5817 mAxes.add(abs, axis);
5818 }
5819 }
5820
5821 // If there are too many axes, start dropping them.
5822 // Prefer to keep explicitly mapped axes.
5823 if (mAxes.size() > PointerCoords::MAX_AXES) {
5824 LOGI("Joystick '%s' has %d axes but the framework only supports a maximum of %d.",
5825 getDeviceName().string(), mAxes.size(), PointerCoords::MAX_AXES);
5826 pruneAxes(true);
5827 pruneAxes(false);
5828 }
5829
5830 // Assign generic axis ids to remaining axes.
5831 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
5832 size_t numAxes = mAxes.size();
5833 for (size_t i = 0; i < numAxes; i++) {
5834 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005835 if (axis.axisInfo.axis < 0) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005836 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16
5837 && haveAxis(nextGenericAxisId)) {
5838 nextGenericAxisId += 1;
5839 }
5840
5841 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
Jeff Brown85297452011-03-04 13:07:49 -08005842 axis.axisInfo.axis = nextGenericAxisId;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005843 nextGenericAxisId += 1;
5844 } else {
5845 LOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
5846 "have already been assigned to other axes.",
5847 getDeviceName().string(), mAxes.keyAt(i));
5848 mAxes.removeItemsAt(i--);
5849 numAxes -= 1;
5850 }
5851 }
5852 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005853}
5854
Jeff Brown85297452011-03-04 13:07:49 -08005855bool JoystickInputMapper::haveAxis(int32_t axisId) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005856 size_t numAxes = mAxes.size();
5857 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005858 const Axis& axis = mAxes.valueAt(i);
5859 if (axis.axisInfo.axis == axisId
5860 || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT
5861 && axis.axisInfo.highAxis == axisId)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005862 return true;
5863 }
5864 }
5865 return false;
5866}
Jeff Browncb1404e2011-01-15 18:14:15 -08005867
Jeff Brown6f2fba42011-02-19 01:08:02 -08005868void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
5869 size_t i = mAxes.size();
5870 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
5871 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
5872 continue;
5873 }
5874 LOGI("Discarding joystick '%s' axis %d because there are too many axes.",
5875 getDeviceName().string(), mAxes.keyAt(i));
5876 mAxes.removeItemsAt(i);
5877 }
5878}
5879
5880bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
5881 switch (axis) {
5882 case AMOTION_EVENT_AXIS_X:
5883 case AMOTION_EVENT_AXIS_Y:
5884 case AMOTION_EVENT_AXIS_Z:
5885 case AMOTION_EVENT_AXIS_RX:
5886 case AMOTION_EVENT_AXIS_RY:
5887 case AMOTION_EVENT_AXIS_RZ:
5888 case AMOTION_EVENT_AXIS_HAT_X:
5889 case AMOTION_EVENT_AXIS_HAT_Y:
5890 case AMOTION_EVENT_AXIS_ORIENTATION:
Jeff Brown85297452011-03-04 13:07:49 -08005891 case AMOTION_EVENT_AXIS_RUDDER:
5892 case AMOTION_EVENT_AXIS_WHEEL:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005893 return true;
5894 default:
5895 return false;
5896 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005897}
5898
5899void JoystickInputMapper::reset() {
5900 // Recenter all axes.
5901 nsecs_t when = systemTime(SYSTEM_TIME_MONOTONIC);
Jeff Browncb1404e2011-01-15 18:14:15 -08005902
Jeff Brown6f2fba42011-02-19 01:08:02 -08005903 size_t numAxes = mAxes.size();
5904 for (size_t i = 0; i < numAxes; i++) {
5905 Axis& axis = mAxes.editValueAt(i);
Jeff Brown85297452011-03-04 13:07:49 -08005906 axis.resetValue();
Jeff Brown6f2fba42011-02-19 01:08:02 -08005907 }
5908
5909 sync(when, true /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005910
5911 InputMapper::reset();
5912}
5913
5914void JoystickInputMapper::process(const RawEvent* rawEvent) {
5915 switch (rawEvent->type) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005916 case EV_ABS: {
5917 ssize_t index = mAxes.indexOfKey(rawEvent->scanCode);
5918 if (index >= 0) {
5919 Axis& axis = mAxes.editValueAt(index);
Jeff Brown85297452011-03-04 13:07:49 -08005920 float newValue, highNewValue;
5921 switch (axis.axisInfo.mode) {
5922 case AxisInfo::MODE_INVERT:
5923 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value)
5924 * axis.scale + axis.offset;
5925 highNewValue = 0.0f;
5926 break;
5927 case AxisInfo::MODE_SPLIT:
5928 if (rawEvent->value < axis.axisInfo.splitValue) {
5929 newValue = (axis.axisInfo.splitValue - rawEvent->value)
5930 * axis.scale + axis.offset;
5931 highNewValue = 0.0f;
5932 } else if (rawEvent->value > axis.axisInfo.splitValue) {
5933 newValue = 0.0f;
5934 highNewValue = (rawEvent->value - axis.axisInfo.splitValue)
5935 * axis.highScale + axis.highOffset;
5936 } else {
5937 newValue = 0.0f;
5938 highNewValue = 0.0f;
5939 }
5940 break;
5941 default:
5942 newValue = rawEvent->value * axis.scale + axis.offset;
5943 highNewValue = 0.0f;
5944 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005945 }
Jeff Brown85297452011-03-04 13:07:49 -08005946 axis.newValue = newValue;
5947 axis.highNewValue = highNewValue;
Jeff Browncb1404e2011-01-15 18:14:15 -08005948 }
5949 break;
Jeff Brown6f2fba42011-02-19 01:08:02 -08005950 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005951
5952 case EV_SYN:
5953 switch (rawEvent->scanCode) {
5954 case SYN_REPORT:
Jeff Brown6f2fba42011-02-19 01:08:02 -08005955 sync(rawEvent->when, false /*force*/);
Jeff Browncb1404e2011-01-15 18:14:15 -08005956 break;
5957 }
5958 break;
5959 }
5960}
5961
Jeff Brown6f2fba42011-02-19 01:08:02 -08005962void JoystickInputMapper::sync(nsecs_t when, bool force) {
Jeff Brown85297452011-03-04 13:07:49 -08005963 if (!filterAxes(force)) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08005964 return;
Jeff Browncb1404e2011-01-15 18:14:15 -08005965 }
5966
5967 int32_t metaState = mContext->getGlobalMetaState();
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005968 int32_t buttonState = 0;
5969
5970 PointerProperties pointerProperties;
5971 pointerProperties.clear();
5972 pointerProperties.id = 0;
5973 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
Jeff Browncb1404e2011-01-15 18:14:15 -08005974
Jeff Brown6f2fba42011-02-19 01:08:02 -08005975 PointerCoords pointerCoords;
5976 pointerCoords.clear();
5977
5978 size_t numAxes = mAxes.size();
5979 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08005980 const Axis& axis = mAxes.valueAt(i);
5981 pointerCoords.setAxisValue(axis.axisInfo.axis, axis.currentValue);
5982 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
5983 pointerCoords.setAxisValue(axis.axisInfo.highAxis, axis.highCurrentValue);
5984 }
Jeff Browncb1404e2011-01-15 18:14:15 -08005985 }
5986
Jeff Brown56194eb2011-03-02 19:23:13 -08005987 // Moving a joystick axis should not wake the devide because joysticks can
5988 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
5989 // button will likely wake the device.
5990 // TODO: Use the input device configuration to control this behavior more finely.
5991 uint32_t policyFlags = 0;
5992
Jeff Brown56194eb2011-03-02 19:23:13 -08005993 getDispatcher()->notifyMotion(when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, policyFlags,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005994 AMOTION_EVENT_ACTION_MOVE, 0, metaState, buttonState, AMOTION_EVENT_EDGE_FLAG_NONE,
5995 1, &pointerProperties, &pointerCoords, 0, 0, 0);
Jeff Browncb1404e2011-01-15 18:14:15 -08005996}
5997
Jeff Brown85297452011-03-04 13:07:49 -08005998bool JoystickInputMapper::filterAxes(bool force) {
5999 bool atLeastOneSignificantChange = force;
Jeff Brown6f2fba42011-02-19 01:08:02 -08006000 size_t numAxes = mAxes.size();
6001 for (size_t i = 0; i < numAxes; i++) {
Jeff Brown85297452011-03-04 13:07:49 -08006002 Axis& axis = mAxes.editValueAt(i);
6003 if (force || hasValueChangedSignificantly(axis.filter,
6004 axis.newValue, axis.currentValue, axis.min, axis.max)) {
6005 axis.currentValue = axis.newValue;
6006 atLeastOneSignificantChange = true;
6007 }
6008 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
6009 if (force || hasValueChangedSignificantly(axis.filter,
6010 axis.highNewValue, axis.highCurrentValue, axis.min, axis.max)) {
6011 axis.highCurrentValue = axis.highNewValue;
6012 atLeastOneSignificantChange = true;
6013 }
6014 }
6015 }
6016 return atLeastOneSignificantChange;
6017}
6018
6019bool JoystickInputMapper::hasValueChangedSignificantly(
6020 float filter, float newValue, float currentValue, float min, float max) {
6021 if (newValue != currentValue) {
6022 // Filter out small changes in value unless the value is converging on the axis
6023 // bounds or center point. This is intended to reduce the amount of information
6024 // sent to applications by particularly noisy joysticks (such as PS3).
6025 if (fabs(newValue - currentValue) > filter
6026 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min)
6027 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max)
6028 || hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
6029 return true;
6030 }
6031 }
6032 return false;
6033}
6034
6035bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(
6036 float filter, float newValue, float currentValue, float thresholdValue) {
6037 float newDistance = fabs(newValue - thresholdValue);
6038 if (newDistance < filter) {
6039 float oldDistance = fabs(currentValue - thresholdValue);
6040 if (newDistance < oldDistance) {
Jeff Brown6f2fba42011-02-19 01:08:02 -08006041 return true;
6042 }
Jeff Browncb1404e2011-01-15 18:14:15 -08006043 }
Jeff Brown6f2fba42011-02-19 01:08:02 -08006044 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08006045}
6046
Jeff Brown46b9ac0a2010-04-22 18:58:52 -07006047} // namespace android